data-structure-typed 2.0.5 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/COMMANDS.md +17 -0
- package/benchmark/report.html +13 -77
- package/benchmark/report.json +145 -177
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/cjs/data-structures/base/iterable-element-base.js +149 -107
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/cjs/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +250 -192
- package/dist/cjs/data-structures/base/linear-base.js +137 -274
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +594 -865
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/cjs/data-structures/binary-tree/bst.js +505 -481
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/cjs/data-structures/graph/abstract-graph.js +267 -237
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/cjs/data-structures/graph/directed-graph.js +146 -233
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/cjs/data-structures/graph/map-graph.js +56 -59
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/cjs/data-structures/graph/undirected-graph.js +129 -149
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/cjs/data-structures/hash/hash-map.js +270 -457
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +214 -289
- package/dist/cjs/data-structures/heap/heap.js +340 -349
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/cjs/data-structures/heap/max-heap.js +11 -66
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/cjs/data-structures/heap/min-heap.js +11 -66
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +227 -254
- package/dist/cjs/data-structures/queue/deque.js +309 -348
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +180 -201
- package/dist/cjs/data-structures/queue/queue.js +265 -248
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +124 -102
- package/dist/cjs/data-structures/stack/stack.js +181 -125
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +164 -165
- package/dist/cjs/data-structures/trie/trie.js +189 -172
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +56 -6
- package/dist/cjs/interfaces/graph.d.ts +16 -0
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.d.ts +1 -1
- package/dist/cjs/utils/utils.js +2 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/esm/data-structures/base/iterable-element-base.js +155 -107
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/esm/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/esm/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +250 -192
- package/dist/esm/data-structures/base/linear-base.js +137 -274
- package/dist/esm/data-structures/base/linear-base.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +171 -212
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +133 -94
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/esm/data-structures/binary-tree/avl-tree.js +206 -200
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/esm/data-structures/binary-tree/binary-tree.js +598 -874
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/esm/data-structures/binary-tree/bst.js +507 -487
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +114 -215
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/esm/data-structures/binary-tree/tree-counter.js +175 -209
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +103 -92
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/esm/data-structures/graph/abstract-graph.js +267 -237
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/esm/data-structures/graph/directed-graph.js +145 -233
- package/dist/esm/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/esm/data-structures/graph/map-graph.js +56 -59
- package/dist/esm/data-structures/graph/map-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/esm/data-structures/graph/undirected-graph.js +128 -149
- package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/esm/data-structures/hash/hash-map.js +270 -457
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +214 -289
- package/dist/esm/data-structures/heap/heap.js +329 -349
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/esm/data-structures/heap/max-heap.js +11 -66
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/esm/data-structures/heap/min-heap.js +11 -66
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +368 -495
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +448 -467
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/esm/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +227 -254
- package/dist/esm/data-structures/queue/deque.js +313 -348
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +180 -201
- package/dist/esm/data-structures/queue/queue.js +263 -248
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +124 -102
- package/dist/esm/data-structures/stack/stack.js +181 -125
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +164 -165
- package/dist/esm/data-structures/trie/trie.js +193 -172
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/interfaces/binary-tree.d.ts +56 -6
- package/dist/esm/interfaces/graph.d.ts +16 -0
- package/dist/esm/types/data-structures/base/base.d.ts +1 -1
- package/dist/esm/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/esm/types/utils/utils.d.ts +1 -0
- package/dist/esm/utils/utils.d.ts +1 -1
- package/dist/esm/utils/utils.js +2 -1
- package/dist/esm/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +4685 -6477
- package/dist/umd/data-structure-typed.min.js +8 -6
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +3 -4
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +660 -889
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- package/src/utils/utils.ts +9 -14
- package/test/integration/index.html +1 -1
- package/test/performance/benchmark-runner.ts +528 -0
- package/test/performance/reportor.mjs +43 -43
- package/test/performance/runner-config.json +39 -0
- package/test/performance/single-suite-runner.ts +69 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -9
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -3
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +14 -21
- package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
- package/test/unit/data-structures/queue/queue.test.ts +4 -5
- package/test/unit/utils/utils.test.ts +0 -1
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
|
@@ -1,31 +1,66 @@
|
|
|
1
1
|
import { IterableElementBase } from './iterable-element-base';
|
|
2
|
+
/**
|
|
3
|
+
* Singly-linked list node.
|
|
4
|
+
* @template E - Element type.
|
|
5
|
+
* @remarks Time O(1), Space O(1)
|
|
6
|
+
*/
|
|
2
7
|
export class LinkedListNode {
|
|
8
|
+
/**
|
|
9
|
+
* Initialize a node.
|
|
10
|
+
* @param value - Element value.
|
|
11
|
+
* @remarks Time O(1), Space O(1)
|
|
12
|
+
*/
|
|
3
13
|
constructor(value) {
|
|
4
14
|
this._value = value;
|
|
5
15
|
this._next = undefined;
|
|
6
16
|
}
|
|
7
17
|
_value;
|
|
18
|
+
/**
|
|
19
|
+
* Element payload getter.
|
|
20
|
+
* @returns Element value.
|
|
21
|
+
* @remarks Time O(1), Space O(1)
|
|
22
|
+
*/
|
|
8
23
|
get value() {
|
|
9
24
|
return this._value;
|
|
10
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Element payload setter.
|
|
28
|
+
* @param value - New value.
|
|
29
|
+
* @remarks Time O(1), Space O(1)
|
|
30
|
+
*/
|
|
11
31
|
set value(value) {
|
|
12
32
|
this._value = value;
|
|
13
33
|
}
|
|
14
34
|
_next;
|
|
35
|
+
/**
|
|
36
|
+
* Next node getter.
|
|
37
|
+
* @returns Next node or `undefined`.
|
|
38
|
+
* @remarks Time O(1), Space O(1)
|
|
39
|
+
*/
|
|
15
40
|
get next() {
|
|
16
41
|
return this._next;
|
|
17
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Next node setter.
|
|
45
|
+
* @param value - Next node or `undefined`.
|
|
46
|
+
* @remarks Time O(1), Space O(1)
|
|
47
|
+
*/
|
|
18
48
|
set next(value) {
|
|
19
49
|
this._next = value;
|
|
20
50
|
}
|
|
21
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Abstract linear container with array-like utilities.
|
|
54
|
+
* @template E - Element type.
|
|
55
|
+
* @template R - Return type for mapped/derived views.
|
|
56
|
+
* @template NODE - Linked node type used by some implementations.
|
|
57
|
+
* @remarks Time O(1), Space O(1)
|
|
58
|
+
*/
|
|
22
59
|
export class LinearBase extends IterableElementBase {
|
|
23
60
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @
|
|
27
|
-
* constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
|
|
28
|
-
* object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
|
|
61
|
+
* Construct a linear container with runtime options.
|
|
62
|
+
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
63
|
+
* @remarks Time O(1), Space O(1)
|
|
29
64
|
*/
|
|
30
65
|
constructor(options) {
|
|
31
66
|
super(options);
|
|
@@ -36,58 +71,41 @@ export class LinearBase extends IterableElementBase {
|
|
|
36
71
|
}
|
|
37
72
|
}
|
|
38
73
|
_maxLen = -1;
|
|
74
|
+
/**
|
|
75
|
+
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
76
|
+
* @returns Maximum allowed length.
|
|
77
|
+
* @remarks Time O(1), Space O(1)
|
|
78
|
+
*/
|
|
39
79
|
get maxLen() {
|
|
40
80
|
return this._maxLen;
|
|
41
81
|
}
|
|
42
82
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
|
|
49
|
-
* element that you want to find within the array. The function will search for this element starting
|
|
50
|
-
* from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
|
|
51
|
-
* within the
|
|
52
|
-
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
|
|
53
|
-
* index at which to start searching for the `searchElement` within the array. If provided, the
|
|
54
|
-
* search will begin at this index and continue to the end of the array. If `fromIndex` is not
|
|
55
|
-
* specified, the default
|
|
56
|
-
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
57
|
-
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
83
|
+
* First index of a value from the left.
|
|
84
|
+
* @param searchElement - Value to match.
|
|
85
|
+
* @param fromIndex - Start position (supports negative index).
|
|
86
|
+
* @returns Index or `-1` if not found.
|
|
87
|
+
* @remarks Time O(n), Space O(1)
|
|
58
88
|
*/
|
|
59
89
|
indexOf(searchElement, fromIndex = 0) {
|
|
60
|
-
// Boundary checks and adjustments
|
|
61
90
|
if (this.length === 0)
|
|
62
91
|
return -1;
|
|
63
92
|
if (fromIndex < 0)
|
|
64
93
|
fromIndex = this.length + fromIndex;
|
|
65
94
|
if (fromIndex < 0)
|
|
66
95
|
fromIndex = 0;
|
|
67
|
-
// Iterating from the specified index to the end
|
|
68
96
|
for (let i = fromIndex; i < this.length; i++) {
|
|
69
97
|
const element = this.at(i);
|
|
70
98
|
if (element === searchElement)
|
|
71
99
|
return i;
|
|
72
100
|
}
|
|
73
|
-
return -1;
|
|
101
|
+
return -1;
|
|
74
102
|
}
|
|
75
103
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
|
|
82
|
-
* last index of within the array. The `lastIndexOf` method will search the array starting from the
|
|
83
|
-
* `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
|
|
84
|
-
* of the
|
|
85
|
-
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
86
|
-
* index at which to start searching for the `searchElement` in the array. By default, it starts
|
|
87
|
-
* searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
|
|
88
|
-
* provided
|
|
89
|
-
* @returns The last index of the `searchElement` in the array is being returned. If the
|
|
90
|
-
* `searchElement` is not found in the array, -1 is returned.
|
|
104
|
+
* Last index of a value from the right.
|
|
105
|
+
* @param searchElement - Value to match.
|
|
106
|
+
* @param fromIndex - Start position (supports negative index).
|
|
107
|
+
* @returns Index or `-1` if not found.
|
|
108
|
+
* @remarks Time O(n), Space O(1)
|
|
91
109
|
*/
|
|
92
110
|
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
93
111
|
if (this.length === 0)
|
|
@@ -104,19 +122,11 @@ export class LinearBase extends IterableElementBase {
|
|
|
104
122
|
return -1;
|
|
105
123
|
}
|
|
106
124
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
|
|
113
|
-
* that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
|
|
114
|
-
* value indicating whether the current element satisfies the condition being checked for.
|
|
115
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
|
|
116
|
-
* parameter that specifies the value to use as `this` when executing the `predicate` function. If
|
|
117
|
-
* provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
|
|
118
|
-
* @returns The `findIndex` method is returning the index of the first element in the array that
|
|
119
|
-
* satisfies the provided predicate function. If no such element is found, it returns -1.
|
|
125
|
+
* Find the first index matching a predicate.
|
|
126
|
+
* @param predicate - `(element, index, self) => boolean`.
|
|
127
|
+
* @param thisArg - Optional `this` for callback.
|
|
128
|
+
* @returns Index or `-1`.
|
|
129
|
+
* @remarks Time O(n), Space O(1)
|
|
120
130
|
*/
|
|
121
131
|
findIndex(predicate, thisArg) {
|
|
122
132
|
for (let i = 0; i < this.length; i++) {
|
|
@@ -127,15 +137,10 @@ export class LinearBase extends IterableElementBase {
|
|
|
127
137
|
return -1;
|
|
128
138
|
}
|
|
129
139
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* individual elements and instances of `LinearBase`.
|
|
135
|
-
* @param {(E | this)[]} items - The `concat` method takes in an array of items, where
|
|
136
|
-
* each item can be either of type `E` or an instance of `LinearBase<E, R>`.
|
|
137
|
-
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
138
|
-
* items passed as arguments concatenated to it.
|
|
140
|
+
* Concatenate elements and/or containers.
|
|
141
|
+
* @param items - Elements or other containers.
|
|
142
|
+
* @returns New container with combined elements (`this` type).
|
|
143
|
+
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
139
144
|
*/
|
|
140
145
|
concat(...items) {
|
|
141
146
|
const newList = this.clone();
|
|
@@ -150,16 +155,10 @@ export class LinearBase extends IterableElementBase {
|
|
|
150
155
|
return newList;
|
|
151
156
|
}
|
|
152
157
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* function.
|
|
158
|
-
* @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
|
|
159
|
-
* two elements `a` and `b` as input and returns a number indicating their relative order. If the
|
|
160
|
-
* returned value is negative, `a` comes before `b`. If the returned value is positive, `
|
|
161
|
-
* @returns The `sort` method is returning the instance of the object on which it is called (this),
|
|
162
|
-
* after sorting the elements based on the provided comparison function (compareFn).
|
|
158
|
+
* In-place stable order via array sort semantics.
|
|
159
|
+
* @param compareFn - Comparator `(a, b) => number`.
|
|
160
|
+
* @returns This container.
|
|
161
|
+
* @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
|
|
163
162
|
*/
|
|
164
163
|
sort(compareFn) {
|
|
165
164
|
const arr = this.toArray();
|
|
@@ -170,69 +169,42 @@ export class LinearBase extends IterableElementBase {
|
|
|
170
169
|
return this;
|
|
171
170
|
}
|
|
172
171
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* elements
|
|
178
|
-
* @
|
|
179
|
-
* to start modifying the array. If `start` is a negative number, it will count from the end of the
|
|
180
|
-
* array.
|
|
181
|
-
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
182
|
-
* number of elements to remove from the array starting at the specified `start` index. If
|
|
183
|
-
* `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
|
|
184
|
-
* at the `start`
|
|
185
|
-
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
186
|
-
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
187
|
-
* and you can pass multiple elements separated by commas. The `splice` method will insert these
|
|
188
|
-
* items into the array at the
|
|
189
|
-
* @returns The `splice` method returns a list of elements that were removed from the original list
|
|
190
|
-
* during the operation.
|
|
172
|
+
* Remove and/or insert elements at a position (array-compatible).
|
|
173
|
+
* @param start - Start index (supports negative index).
|
|
174
|
+
* @param deleteCount - How many to remove.
|
|
175
|
+
* @param items - Elements to insert.
|
|
176
|
+
* @returns Removed elements as a new list (`this` type).
|
|
177
|
+
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
191
178
|
*/
|
|
192
179
|
splice(start, deleteCount = 0, ...items) {
|
|
193
180
|
const removedList = this._createInstance();
|
|
194
|
-
// Handling negative indexes and bounds
|
|
195
181
|
start = start < 0 ? this.length + start : start;
|
|
196
182
|
start = Math.max(0, Math.min(start, this.length));
|
|
197
183
|
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
198
|
-
// Delete elements
|
|
199
184
|
for (let i = 0; i < deleteCount; i++) {
|
|
200
|
-
const removed = this.deleteAt(start);
|
|
185
|
+
const removed = this.deleteAt(start);
|
|
201
186
|
if (removed !== undefined) {
|
|
202
|
-
removedList.push(removed);
|
|
187
|
+
removedList.push(removed);
|
|
203
188
|
}
|
|
204
189
|
}
|
|
205
|
-
// Insert new element
|
|
206
190
|
for (let i = 0; i < items.length; i++) {
|
|
207
|
-
this.addAt(start + i, items[i]);
|
|
191
|
+
this.addAt(start + i, items[i]);
|
|
208
192
|
}
|
|
209
|
-
return removedList;
|
|
193
|
+
return removedList;
|
|
210
194
|
}
|
|
211
195
|
/**
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* specified separator.
|
|
217
|
-
* @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
|
|
218
|
-
* or characters that will be used to separate each element when joining them into a single string.
|
|
219
|
-
* By default, the separator is set to a comma (`,`), but you can provide a different separator if
|
|
220
|
-
* needed.
|
|
221
|
-
* @returns The `join` method is being returned, which takes an optional `separator` parameter
|
|
222
|
-
* (defaulting to a comma) and returns a string created by joining all elements of the array after
|
|
223
|
-
* converting it to an array.
|
|
196
|
+
* Join all elements into a string.
|
|
197
|
+
* @param separator - Separator string.
|
|
198
|
+
* @returns Concatenated string.
|
|
199
|
+
* @remarks Time O(n), Space O(n)
|
|
224
200
|
*/
|
|
225
201
|
join(separator = ',') {
|
|
226
202
|
return this.toArray().join(separator);
|
|
227
203
|
}
|
|
228
204
|
/**
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* The function `toReversedArray` takes an array and returns a new array with its elements in reverse
|
|
233
|
-
* order.
|
|
234
|
-
* @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
|
|
235
|
-
* order.
|
|
205
|
+
* Snapshot elements into a reversed array.
|
|
206
|
+
* @returns New reversed array.
|
|
207
|
+
* @remarks Time O(n), Space O(n)
|
|
236
208
|
*/
|
|
237
209
|
toReversedArray() {
|
|
238
210
|
const array = [];
|
|
@@ -241,21 +213,6 @@ export class LinearBase extends IterableElementBase {
|
|
|
241
213
|
}
|
|
242
214
|
return array;
|
|
243
215
|
}
|
|
244
|
-
/**
|
|
245
|
-
* Time Complexity: O(n)
|
|
246
|
-
* Space Complexity: O(1)
|
|
247
|
-
*
|
|
248
|
-
* The `reduceRight` function in TypeScript iterates over an array from right to left and applies a
|
|
249
|
-
* callback function to each element, accumulating a single result.
|
|
250
|
-
* @param callbackfn - The `callbackfn` parameter in the `reduceRight` method is a function that will
|
|
251
|
-
* be called on each element in the array from right to left. It takes four arguments:
|
|
252
|
-
* @param {U} [initialValue] - The `initialValue` parameter in the `reduceRight` method is an
|
|
253
|
-
* optional parameter that specifies the initial value of the accumulator. If provided, the
|
|
254
|
-
* `accumulator` will start with this initial value before iterating over the elements of the array.
|
|
255
|
-
* If `initialValue` is not provided, the accumulator will
|
|
256
|
-
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
257
|
-
* callback function to each element in the array from right to left.
|
|
258
|
-
*/
|
|
259
216
|
reduceRight(callbackfn, initialValue) {
|
|
260
217
|
let accumulator = initialValue ?? 0;
|
|
261
218
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
@@ -264,20 +221,11 @@ export class LinearBase extends IterableElementBase {
|
|
|
264
221
|
return accumulator;
|
|
265
222
|
}
|
|
266
223
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
|
|
273
|
-
* which to begin extracting elements from an array-like object. If no `start` parameter is provided,
|
|
274
|
-
* the default value is 0, meaning the extraction will start from the beginning of the array.
|
|
275
|
-
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
276
|
-
* end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
|
|
277
|
-
* array (i.e., `this.length`).
|
|
278
|
-
* @returns The `slice` method is returning a new instance of the object with elements sliced from
|
|
279
|
-
* the specified start index (default is 0) to the specified end index (default is the length of the
|
|
280
|
-
* object).
|
|
224
|
+
* Create a shallow copy of a subrange.
|
|
225
|
+
* @param start - Inclusive start (supports negative index).
|
|
226
|
+
* @param end - Exclusive end (supports negative index).
|
|
227
|
+
* @returns New list with the range (`this` type).
|
|
228
|
+
* @remarks Time O(n), Space O(n)
|
|
281
229
|
*/
|
|
282
230
|
slice(start = 0, end = this.length) {
|
|
283
231
|
start = start < 0 ? this.length + start : start;
|
|
@@ -289,48 +237,36 @@ export class LinearBase extends IterableElementBase {
|
|
|
289
237
|
return newList;
|
|
290
238
|
}
|
|
291
239
|
/**
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* @
|
|
298
|
-
* used to fill the specified range in the array.
|
|
299
|
-
* @param [start=0] - The `start` parameter specifies the index at which to start filling the array
|
|
300
|
-
* with the specified value. If not provided, it defaults to 0, indicating the beginning of the
|
|
301
|
-
* array.
|
|
302
|
-
* @param end - The `end` parameter in the `fill` function represents the index at which the filling
|
|
303
|
-
* of values should stop. It specifies the end of the range within the array where the `value` should
|
|
304
|
-
* be filled.
|
|
305
|
-
* @returns The `fill` method is returning the modified object (`this`) after filling the specified
|
|
306
|
-
* range with the provided value.
|
|
240
|
+
* Fill a range with a value.
|
|
241
|
+
* @param value - Value to set.
|
|
242
|
+
* @param start - Inclusive start.
|
|
243
|
+
* @param end - Exclusive end.
|
|
244
|
+
* @returns This list.
|
|
245
|
+
* @remarks Time O(n), Space O(1)
|
|
307
246
|
*/
|
|
308
247
|
fill(value, start = 0, end = this.length) {
|
|
309
|
-
// Handling negative indexes
|
|
310
248
|
start = start < 0 ? this.length + start : start;
|
|
311
249
|
end = end < 0 ? this.length + end : end;
|
|
312
|
-
// Boundary processing
|
|
313
250
|
if (start < 0)
|
|
314
251
|
start = 0;
|
|
315
252
|
if (end > this.length)
|
|
316
253
|
end = this.length;
|
|
317
254
|
if (start >= end)
|
|
318
255
|
return this;
|
|
319
|
-
// Iterate through the specified range and fill in the values
|
|
320
256
|
for (let i = start; i < end; i++) {
|
|
321
257
|
this.setAt(i, value);
|
|
322
258
|
}
|
|
323
259
|
return this;
|
|
324
260
|
}
|
|
325
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* Linked-list specialized linear container.
|
|
264
|
+
* @template E - Element type.
|
|
265
|
+
* @template R - Return type for mapped/derived views.
|
|
266
|
+
* @template NODE - Linked node type.
|
|
267
|
+
* @remarks Time O(1), Space O(1)
|
|
268
|
+
*/
|
|
326
269
|
export class LinearLinkedBase extends LinearBase {
|
|
327
|
-
/**
|
|
328
|
-
* The constructor initializes the LinearBase class with optional options, setting the maximum length
|
|
329
|
-
* if provided and valid.
|
|
330
|
-
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
331
|
-
* constructor. It is of type `LinearBaseOptions<E, R>`. This object may contain properties such as
|
|
332
|
-
* `maxLen`, which is a number representing the maximum length. If `maxLen` is a positive integer,
|
|
333
|
-
*/
|
|
334
270
|
constructor(options) {
|
|
335
271
|
super(options);
|
|
336
272
|
if (options) {
|
|
@@ -340,23 +276,13 @@ export class LinearLinkedBase extends LinearBase {
|
|
|
340
276
|
}
|
|
341
277
|
}
|
|
342
278
|
/**
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
* @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
|
|
349
|
-
* within the array. The `indexOf` method will return the index of the first occurrence of this
|
|
350
|
-
* element within the array.
|
|
351
|
-
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
|
|
352
|
-
* index in the array at which to start the search for the `searchElement`. If provided, the search
|
|
353
|
-
* will begin at the specified index and continue to the end of the array. If not provided, the
|
|
354
|
-
* search will start at index
|
|
355
|
-
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
356
|
-
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
279
|
+
* Linked-list optimized `indexOf` (forwards scan).
|
|
280
|
+
* @param searchElement - Value to match.
|
|
281
|
+
* @param fromIndex - Start position.
|
|
282
|
+
* @returns Index or `-1`.
|
|
283
|
+
* @remarks Time O(n), Space O(1)
|
|
357
284
|
*/
|
|
358
285
|
indexOf(searchElement, fromIndex = 0) {
|
|
359
|
-
// In order to improve performance, it is best to override this method in the subclass of the array implementation
|
|
360
286
|
const iterator = this._getIterator();
|
|
361
287
|
let current = iterator.next();
|
|
362
288
|
let index = 0;
|
|
@@ -373,26 +299,13 @@ export class LinearLinkedBase extends LinearBase {
|
|
|
373
299
|
return -1;
|
|
374
300
|
}
|
|
375
301
|
/**
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find
|
|
382
|
-
* within the array. The `lastIndexOf` method searches the array for this element starting from the
|
|
383
|
-
* end of the array (or from the specified `fromIndex` if provided) and returns the index of the last
|
|
384
|
-
* occurrence of the element
|
|
385
|
-
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
386
|
-
* index at which to start searching for the `searchElement` in the array. If provided, the search
|
|
387
|
-
* will begin at this index and move towards the beginning of the array. If not provided, the search
|
|
388
|
-
* will start at the
|
|
389
|
-
* @returns The `lastIndexOf` method is being overridden to search for the `searchElement` starting
|
|
390
|
-
* from the specified `fromIndex` (defaulting to the end of the array). It iterates over the array in
|
|
391
|
-
* reverse order using a custom iterator `_getReverseIterator` and returns the index of the last
|
|
392
|
-
* occurrence of the `searchElement` if found, or -1 if not found.
|
|
302
|
+
* Linked-list optimized `lastIndexOf` (reverse scan).
|
|
303
|
+
* @param searchElement - Value to match.
|
|
304
|
+
* @param fromIndex - Start position.
|
|
305
|
+
* @returns Index or `-1`.
|
|
306
|
+
* @remarks Time O(n), Space O(1)
|
|
393
307
|
*/
|
|
394
308
|
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
395
|
-
// In order to improve performance, it is best to override this method in the subclass of the array implementation
|
|
396
309
|
const iterator = this._getReverseIterator();
|
|
397
310
|
let current = iterator.next();
|
|
398
311
|
let index = this.length - 1;
|
|
@@ -408,18 +321,6 @@ export class LinearLinkedBase extends LinearBase {
|
|
|
408
321
|
}
|
|
409
322
|
return -1;
|
|
410
323
|
}
|
|
411
|
-
/**
|
|
412
|
-
* Time Complexity: O(n + m)
|
|
413
|
-
* Space Complexity: O(n + m)
|
|
414
|
-
*
|
|
415
|
-
* The `concat` function in TypeScript overrides the default behavior to concatenate items into a new
|
|
416
|
-
* list, handling both individual elements and instances of `LinearBase`.
|
|
417
|
-
* @param {(E | LinearBase<E, R>)[]} items - The `concat` method you provided takes in a variable
|
|
418
|
-
* number of arguments of type `E` or `LinearBase<E, R>`. The method concatenates these items to the
|
|
419
|
-
* current list and returns a new list with the concatenated items.
|
|
420
|
-
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
421
|
-
* items passed as arguments concatenated to it.
|
|
422
|
-
*/
|
|
423
324
|
concat(...items) {
|
|
424
325
|
const newList = this.clone();
|
|
425
326
|
for (const item of items) {
|
|
@@ -433,21 +334,13 @@ export class LinearLinkedBase extends LinearBase {
|
|
|
433
334
|
return newList;
|
|
434
335
|
}
|
|
435
336
|
/**
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
* @param {number} [start=0] - The `start` parameter in the `slice` method specifies the index at
|
|
442
|
-
* which to begin extracting elements from the array. If no `start` parameter is provided, the
|
|
443
|
-
* default value is 0, indicating that extraction should start from the beginning of the array.
|
|
444
|
-
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
445
|
-
* end the slicing of the array. If not provided, it defaults to the length of the array.
|
|
446
|
-
* @returns The `slice` method is returning a new instance of the array implementation with elements
|
|
447
|
-
* sliced from the original array based on the `start` and `end` parameters.
|
|
337
|
+
* Slice via forward iteration (no random access required).
|
|
338
|
+
* @param start - Inclusive start (supports negative index).
|
|
339
|
+
* @param end - Exclusive end (supports negative index).
|
|
340
|
+
* @returns New list (`this` type).
|
|
341
|
+
* @remarks Time O(n), Space O(n)
|
|
448
342
|
*/
|
|
449
343
|
slice(start = 0, end = this.length) {
|
|
450
|
-
// In order to improve performance, it is best to override this method in the subclass of the array implementation
|
|
451
344
|
start = start < 0 ? this.length + start : start;
|
|
452
345
|
end = end < 0 ? this.length + end : end;
|
|
453
346
|
const newList = this._createInstance();
|
|
@@ -465,78 +358,48 @@ export class LinearLinkedBase extends LinearBase {
|
|
|
465
358
|
return newList;
|
|
466
359
|
}
|
|
467
360
|
/**
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
* @
|
|
474
|
-
* to start modifying the array.
|
|
475
|
-
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
476
|
-
* number of elements to remove from the array starting at the specified `start` index. If
|
|
477
|
-
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
478
|
-
* elements can still be inserted at
|
|
479
|
-
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
480
|
-
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
481
|
-
* and there can be multiple elements passed as arguments to be inserted into the array.
|
|
482
|
-
* @returns The `splice` method is returning a new instance of the data structure that was modified
|
|
483
|
-
* by removing elements specified by the `start` and `deleteCount` parameters, and inserting new
|
|
484
|
-
* elements provided in the `items` array.
|
|
361
|
+
* Splice by walking node iterators from the start index.
|
|
362
|
+
* @param start - Start index.
|
|
363
|
+
* @param deleteCount - How many elements to remove.
|
|
364
|
+
* @param items - Elements to insert after the splice point.
|
|
365
|
+
* @returns Removed elements as a new list (`this` type).
|
|
366
|
+
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
485
367
|
*/
|
|
486
368
|
splice(start, deleteCount = 0, ...items) {
|
|
487
|
-
const removedList = this._createInstance();
|
|
488
|
-
// Handling negative indexes
|
|
369
|
+
const removedList = this._createInstance();
|
|
489
370
|
start = start < 0 ? this.length + start : start;
|
|
490
|
-
start = Math.max(0, Math.min(start, this.length));
|
|
491
|
-
deleteCount = Math.max(0, deleteCount);
|
|
371
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
372
|
+
deleteCount = Math.max(0, deleteCount);
|
|
492
373
|
let currentIndex = 0;
|
|
493
374
|
let currentNode = undefined;
|
|
494
375
|
let previousNode = undefined;
|
|
495
|
-
// Find the starting point using an iterator
|
|
496
376
|
const iterator = this._getNodeIterator();
|
|
497
377
|
for (const node of iterator) {
|
|
498
378
|
if (currentIndex === start) {
|
|
499
|
-
currentNode = node;
|
|
379
|
+
currentNode = node;
|
|
500
380
|
break;
|
|
501
381
|
}
|
|
502
|
-
previousNode = node;
|
|
382
|
+
previousNode = node;
|
|
503
383
|
currentIndex++;
|
|
504
384
|
}
|
|
505
|
-
// Delete nodes
|
|
506
385
|
for (let i = 0; i < deleteCount && currentNode; i++) {
|
|
507
|
-
removedList.push(currentNode.value);
|
|
508
|
-
const nextNode = currentNode.next;
|
|
509
|
-
this.delete(currentNode);
|
|
386
|
+
removedList.push(currentNode.value);
|
|
387
|
+
const nextNode = currentNode.next;
|
|
388
|
+
this.delete(currentNode);
|
|
510
389
|
currentNode = nextNode;
|
|
511
390
|
}
|
|
512
|
-
// Insert new value
|
|
513
391
|
for (let i = 0; i < items.length; i++) {
|
|
514
392
|
if (previousNode) {
|
|
515
|
-
this.addAfter(previousNode, items[i]);
|
|
516
|
-
previousNode = previousNode.next;
|
|
393
|
+
this.addAfter(previousNode, items[i]);
|
|
394
|
+
previousNode = previousNode.next;
|
|
517
395
|
}
|
|
518
396
|
else {
|
|
519
|
-
this.addAt(0, items[i]);
|
|
520
|
-
previousNode = this._getNodeIterator().next().value;
|
|
397
|
+
this.addAt(0, items[i]);
|
|
398
|
+
previousNode = this._getNodeIterator().next().value;
|
|
521
399
|
}
|
|
522
400
|
}
|
|
523
401
|
return removedList;
|
|
524
402
|
}
|
|
525
|
-
/**
|
|
526
|
-
* Time Complexity: O(n)
|
|
527
|
-
* Space Complexity: O(1)
|
|
528
|
-
*
|
|
529
|
-
* The function `reduceRight` iterates over an array in reverse order and applies a callback function
|
|
530
|
-
* to each element, accumulating a single result.
|
|
531
|
-
* @param callbackfn - The `callbackfn` parameter is a function that will be called on each element
|
|
532
|
-
* of the array from right to left. It takes four arguments:
|
|
533
|
-
* @param {U} [initialValue] - The `initialValue` parameter is an optional value that is used as the
|
|
534
|
-
* initial accumulator value in the reduce operation. If provided, the reduce operation starts with
|
|
535
|
-
* this initial value and iterates over the elements of the array, applying the callback function to
|
|
536
|
-
* each element and the current accumulator value. If `initial
|
|
537
|
-
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
538
|
-
* callback function to each element in the array from right to left.
|
|
539
|
-
*/
|
|
540
403
|
reduceRight(callbackfn, initialValue) {
|
|
541
404
|
let accumulator = initialValue ?? 0;
|
|
542
405
|
let index = this.length - 1;
|