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