graph-typed 2.0.4 → 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 +612 -879
- 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 +6 -6
- package/dist/utils/utils.d.ts +110 -49
- package/dist/utils/utils.js +148 -73
- 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 +681 -905
- 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 +9 -5
- package/src/utils/utils.ts +152 -86
|
@@ -1,10 +1,25 @@
|
|
|
1
|
-
import { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
1
|
+
import type { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Base class that makes a data structure iterable and provides common
|
|
5
|
+
* element-wise utilities (e.g., map/filter/reduce/find).
|
|
6
|
+
*
|
|
7
|
+
* @template E The public element type yielded by the structure.
|
|
8
|
+
* @template R The underlying "raw" element type used internally or by converters.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This class implements the JavaScript iteration protocol (via `Symbol.iterator`)
|
|
12
|
+
* and offers array-like helpers with predictable time/space complexity.
|
|
13
|
+
*/
|
|
14
|
+
export abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
4
15
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @param
|
|
16
|
+
* Create a new iterable base.
|
|
17
|
+
*
|
|
18
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
19
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* Time O(1), Space O(1).
|
|
8
23
|
*/
|
|
9
24
|
protected constructor(options?: IterableElementBaseOptions<E, R>) {
|
|
10
25
|
if (options) {
|
|
@@ -14,147 +29,164 @@ export abstract class IterableElementBase<E, R> {
|
|
|
14
29
|
}
|
|
15
30
|
}
|
|
16
31
|
|
|
32
|
+
/**
|
|
33
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Time O(1), Space O(1).
|
|
37
|
+
*/
|
|
17
38
|
protected _toElementFn?: (rawElement: R) => E;
|
|
18
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Exposes the current `toElementFn`, if configured.
|
|
42
|
+
*
|
|
43
|
+
* @returns The converter function or `undefined` when not set.
|
|
44
|
+
* @remarks
|
|
45
|
+
* Time O(1), Space O(1).
|
|
46
|
+
*/
|
|
19
47
|
get toElementFn(): ((rawElement: R) => E) | undefined {
|
|
20
48
|
return this._toElementFn;
|
|
21
49
|
}
|
|
22
50
|
|
|
23
51
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
52
|
+
* Returns an iterator over the structure's elements.
|
|
53
|
+
*
|
|
54
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
55
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
26
56
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
30
|
-
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
57
|
+
* @remarks
|
|
58
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
31
59
|
*/
|
|
32
|
-
*[Symbol.iterator](...args:
|
|
60
|
+
*[Symbol.iterator](...args: unknown[]): IterableIterator<E> {
|
|
33
61
|
yield* this._getIterator(...args);
|
|
34
62
|
}
|
|
35
63
|
|
|
36
64
|
/**
|
|
37
|
-
*
|
|
38
|
-
* Space Complexity: O(n)
|
|
65
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
39
66
|
*
|
|
40
|
-
*
|
|
67
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
68
|
+
* @remarks
|
|
69
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
41
70
|
*/
|
|
42
71
|
*values(): IterableIterator<E> {
|
|
43
|
-
for (const item of this)
|
|
44
|
-
yield item;
|
|
45
|
-
}
|
|
72
|
+
for (const item of this) yield item;
|
|
46
73
|
}
|
|
47
74
|
|
|
48
75
|
/**
|
|
49
|
-
*
|
|
50
|
-
* Space Complexity: O(1)
|
|
76
|
+
* Tests whether all elements satisfy the predicate.
|
|
51
77
|
*
|
|
52
|
-
*
|
|
53
|
-
* @param predicate
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
60
|
-
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
78
|
+
* @template TReturn
|
|
79
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
80
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
81
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
61
85
|
*/
|
|
62
|
-
every(predicate: ElementCallback<E, R, boolean>, thisArg?:
|
|
86
|
+
every(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): boolean {
|
|
63
87
|
let index = 0;
|
|
64
88
|
for (const item of this) {
|
|
65
|
-
if (
|
|
66
|
-
return false;
|
|
89
|
+
if (thisArg === undefined) {
|
|
90
|
+
if (!predicate(item, index++, this)) return false;
|
|
91
|
+
} else {
|
|
92
|
+
const fn = predicate as (this: unknown, v: E, i: number, self: this) => boolean;
|
|
93
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
67
94
|
}
|
|
68
95
|
}
|
|
69
96
|
return true;
|
|
70
97
|
}
|
|
71
98
|
|
|
72
99
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
100
|
+
* Tests whether at least one element satisfies the predicate.
|
|
101
|
+
*
|
|
102
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
103
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
104
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
75
105
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
79
|
-
* element satisfies the condition.
|
|
80
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
81
|
-
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
82
|
-
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
83
|
-
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
84
|
-
* in the collection, and false otherwise.
|
|
106
|
+
* @remarks
|
|
107
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
85
108
|
*/
|
|
86
|
-
some(predicate: ElementCallback<E, R, boolean>, thisArg?:
|
|
109
|
+
some(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): boolean {
|
|
87
110
|
let index = 0;
|
|
88
111
|
for (const item of this) {
|
|
89
|
-
if (
|
|
90
|
-
return true;
|
|
112
|
+
if (thisArg === undefined) {
|
|
113
|
+
if (predicate(item, index++, this)) return true;
|
|
114
|
+
} else {
|
|
115
|
+
const fn = predicate as (this: unknown, v: E, i: number, self: this) => boolean;
|
|
116
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
91
117
|
}
|
|
92
118
|
}
|
|
93
119
|
return false;
|
|
94
120
|
}
|
|
95
121
|
|
|
96
122
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
123
|
+
* Invokes a callback for each element in iteration order.
|
|
124
|
+
*
|
|
125
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
126
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
127
|
+
* @returns `void`.
|
|
99
128
|
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
103
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
104
|
-
* element, and the array that forEach was called upon.
|
|
105
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
106
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
107
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
129
|
+
* @remarks
|
|
130
|
+
* Time O(n), Space O(1).
|
|
108
131
|
*/
|
|
109
|
-
forEach(callbackfn: ElementCallback<E, R, void>, thisArg?:
|
|
132
|
+
forEach(callbackfn: ElementCallback<E, R, void>, thisArg?: unknown): void {
|
|
110
133
|
let index = 0;
|
|
111
134
|
for (const item of this) {
|
|
112
|
-
|
|
135
|
+
if (thisArg === undefined) {
|
|
136
|
+
callbackfn(item, index++, this);
|
|
137
|
+
} else {
|
|
138
|
+
const fn = callbackfn as (this: unknown, v: E, i: number, self: this) => void;
|
|
139
|
+
fn.call(thisArg, item, index++, this);
|
|
140
|
+
}
|
|
113
141
|
}
|
|
114
142
|
}
|
|
115
143
|
|
|
116
|
-
find<S extends E>(predicate: ElementCallback<E, R, S>, thisArg?: any): S | undefined;
|
|
117
|
-
find(predicate: ElementCallback<E, R, unknown>, thisArg?: any): E | undefined;
|
|
118
|
-
|
|
119
144
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
145
|
+
* Finds the first element that satisfies the predicate and returns it.
|
|
146
|
+
*
|
|
147
|
+
* @overload
|
|
148
|
+
* Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
|
|
149
|
+
* @template S
|
|
150
|
+
* @param predicate Type-guard predicate: `(value, index, self) => value is S`.
|
|
151
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
152
|
+
* @returns The matched element typed as `S`, or `undefined` if not found.
|
|
153
|
+
*
|
|
154
|
+
* @overload
|
|
155
|
+
* @param predicate Boolean predicate: `(value, index, self) => boolean`.
|
|
156
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
157
|
+
* @returns The first matching element as `E`, or `undefined` if not found.
|
|
122
158
|
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* @param predicate - The predicate parameter is a function that will be called for each element in
|
|
126
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
127
|
-
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
128
|
-
* current element matches the desired condition.
|
|
129
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
130
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
131
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
132
|
-
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
133
|
-
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
159
|
+
* @remarks
|
|
160
|
+
* Time O(n) in the worst case; may exit early on the first match. Space O(1).
|
|
134
161
|
*/
|
|
135
|
-
find(predicate: ElementCallback<E, R,
|
|
162
|
+
find<S extends E>(predicate: ElementCallback<E, R, S>, thisArg?: unknown): S | undefined;
|
|
163
|
+
find(predicate: ElementCallback<E, R, unknown>, thisArg?: unknown): E | undefined;
|
|
164
|
+
|
|
165
|
+
// Implementation signature
|
|
166
|
+
find(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): E | undefined {
|
|
136
167
|
let index = 0;
|
|
137
168
|
for (const item of this) {
|
|
138
|
-
if (
|
|
169
|
+
if (thisArg === undefined) {
|
|
170
|
+
if (predicate(item, index++, this)) return item;
|
|
171
|
+
} else {
|
|
172
|
+
const fn = predicate as (this: unknown, v: E, i: number, self: this) => boolean;
|
|
173
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
174
|
+
}
|
|
139
175
|
}
|
|
140
|
-
|
|
141
176
|
return;
|
|
142
177
|
}
|
|
143
178
|
|
|
144
179
|
/**
|
|
145
|
-
*
|
|
146
|
-
* Space Complexity: O(1)
|
|
180
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
147
181
|
*
|
|
148
|
-
*
|
|
149
|
-
* @
|
|
150
|
-
*
|
|
151
|
-
* @
|
|
152
|
-
*
|
|
182
|
+
* @param element The element to test with `===` equality.
|
|
183
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
184
|
+
*
|
|
185
|
+
* @remarks
|
|
186
|
+
* Time O(n) in the worst case. Space O(1).
|
|
153
187
|
*/
|
|
154
188
|
has(element: E): boolean {
|
|
155
|
-
for (const ele of this)
|
|
156
|
-
if (ele === element) return true;
|
|
157
|
-
}
|
|
189
|
+
for (const ele of this) if (ele === element) return true;
|
|
158
190
|
return false;
|
|
159
191
|
}
|
|
160
192
|
|
|
@@ -163,67 +195,158 @@ export abstract class IterableElementBase<E, R> {
|
|
|
163
195
|
reduce<U>(callbackfn: ReduceElementCallback<E, R, U>, initialValue: U): U;
|
|
164
196
|
|
|
165
197
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
198
|
+
* Reduces all elements to a single accumulated value.
|
|
199
|
+
*
|
|
200
|
+
* @overload
|
|
201
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
202
|
+
* @returns The final accumulated value typed as `E`.
|
|
203
|
+
*
|
|
204
|
+
* @overload
|
|
205
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
206
|
+
* @param initialValue The initial accumulator value of type `E`.
|
|
207
|
+
* @returns The final accumulated value typed as `E`.
|
|
168
208
|
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* @param callbackfn
|
|
172
|
-
*
|
|
173
|
-
* @
|
|
174
|
-
*
|
|
175
|
-
* @
|
|
176
|
-
*
|
|
209
|
+
* @overload
|
|
210
|
+
* @template U The accumulator type when it differs from `E`.
|
|
211
|
+
* @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
|
|
212
|
+
* @param initialValue The initial accumulator value of type `U`.
|
|
213
|
+
* @returns The final accumulated value typed as `U`.
|
|
214
|
+
*
|
|
215
|
+
* @remarks
|
|
216
|
+
* Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
177
217
|
*/
|
|
178
218
|
reduce<U>(callbackfn: ReduceElementCallback<E, R, U>, initialValue?: U): U {
|
|
179
|
-
let accumulator = initialValue ?? (0 as U);
|
|
180
219
|
let index = 0;
|
|
181
|
-
|
|
182
|
-
|
|
220
|
+
const iter = this[Symbol.iterator]();
|
|
221
|
+
let acc: U;
|
|
222
|
+
|
|
223
|
+
if (arguments.length >= 2) {
|
|
224
|
+
acc = initialValue as U;
|
|
225
|
+
} else {
|
|
226
|
+
const first = iter.next();
|
|
227
|
+
if (first.done) throw new TypeError('Reduce of empty structure with no initial value');
|
|
228
|
+
acc = first.value as unknown as U;
|
|
229
|
+
index = 1;
|
|
183
230
|
}
|
|
184
|
-
|
|
231
|
+
|
|
232
|
+
for (const value of iter as unknown as Iterable<E>) {
|
|
233
|
+
acc = callbackfn(acc, value, index++, this);
|
|
234
|
+
}
|
|
235
|
+
return acc;
|
|
185
236
|
}
|
|
186
237
|
|
|
187
238
|
/**
|
|
188
|
-
*
|
|
189
|
-
* Space Complexity: O(n)
|
|
239
|
+
* Materializes the elements into a new array.
|
|
190
240
|
*
|
|
191
|
-
*
|
|
192
|
-
* @
|
|
241
|
+
* @returns A shallow array copy of the iteration order.
|
|
242
|
+
* @remarks
|
|
243
|
+
* Time O(n), Space O(n).
|
|
193
244
|
*/
|
|
194
245
|
toArray(): E[] {
|
|
195
246
|
return [...this];
|
|
196
247
|
}
|
|
197
248
|
|
|
198
249
|
/**
|
|
199
|
-
*
|
|
200
|
-
*
|
|
250
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
251
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
201
252
|
*
|
|
202
|
-
*
|
|
253
|
+
* @returns A visual representation (array by default).
|
|
254
|
+
* @remarks
|
|
255
|
+
* Time O(n), Space O(n).
|
|
203
256
|
*/
|
|
204
257
|
toVisual(): E[] {
|
|
205
258
|
return [...this];
|
|
206
259
|
}
|
|
207
260
|
|
|
208
261
|
/**
|
|
209
|
-
*
|
|
210
|
-
* Space Complexity: O(n)
|
|
262
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
211
263
|
*
|
|
212
|
-
*
|
|
264
|
+
* @returns `void`.
|
|
265
|
+
* @remarks
|
|
266
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
213
267
|
*/
|
|
214
268
|
print(): void {
|
|
215
269
|
console.log(this.toVisual());
|
|
216
270
|
}
|
|
217
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Indicates whether the structure currently contains no elements.
|
|
274
|
+
*
|
|
275
|
+
* @returns `true` if empty; otherwise `false`.
|
|
276
|
+
* @remarks
|
|
277
|
+
* Expected Time O(1), Space O(1) for most implementations.
|
|
278
|
+
*/
|
|
218
279
|
abstract isEmpty(): boolean;
|
|
219
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Removes all elements from the structure.
|
|
283
|
+
*
|
|
284
|
+
* @returns `void`.
|
|
285
|
+
* @remarks
|
|
286
|
+
* Expected Time O(1) or O(n) depending on the implementation; Space O(1).
|
|
287
|
+
*/
|
|
220
288
|
abstract clear(): void;
|
|
221
289
|
|
|
222
|
-
|
|
290
|
+
/**
|
|
291
|
+
* Creates a structural copy with the same element values and configuration.
|
|
292
|
+
*
|
|
293
|
+
* @returns A clone of the current instance (same concrete type).
|
|
294
|
+
* @remarks
|
|
295
|
+
* Expected Time O(n) to copy elements; Space O(n).
|
|
296
|
+
*/
|
|
297
|
+
abstract clone(): this;
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Maps each element to a new element and returns a new iterable structure.
|
|
301
|
+
*
|
|
302
|
+
* @template EM The mapped element type.
|
|
303
|
+
* @template RM The mapped raw element type used internally by the target structure.
|
|
304
|
+
* @param callback Function with signature `(value, index, self) => mapped`.
|
|
305
|
+
* @param options Optional options for the returned structure, including its `toElementFn`.
|
|
306
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
307
|
+
* @returns A new `IterableElementBase<EM, RM>` containing mapped elements.
|
|
308
|
+
*
|
|
309
|
+
* @remarks
|
|
310
|
+
* Time O(n), Space O(n).
|
|
311
|
+
*/
|
|
312
|
+
abstract map<EM, RM>(
|
|
313
|
+
callback: ElementCallback<E, R, EM>,
|
|
314
|
+
options?: IterableElementBaseOptions<EM, RM>,
|
|
315
|
+
thisArg?: unknown
|
|
316
|
+
): IterableElementBase<EM, RM>;
|
|
223
317
|
|
|
224
|
-
|
|
318
|
+
/**
|
|
319
|
+
* Maps each element to the same element type and returns the same concrete structure type.
|
|
320
|
+
*
|
|
321
|
+
* @param callback Function with signature `(value, index, self) => mappedValue`.
|
|
322
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
323
|
+
* @returns A new instance of the same concrete type with mapped elements.
|
|
324
|
+
*
|
|
325
|
+
* @remarks
|
|
326
|
+
* Time O(n), Space O(n).
|
|
327
|
+
*/
|
|
328
|
+
abstract mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
|
|
225
329
|
|
|
226
|
-
|
|
330
|
+
/**
|
|
331
|
+
* Filters elements using the provided predicate and returns the same concrete structure type.
|
|
332
|
+
*
|
|
333
|
+
* @param predicate Function with signature `(value, index, self) => boolean`.
|
|
334
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
335
|
+
* @returns A new instance of the same concrete type containing only elements that pass the predicate.
|
|
336
|
+
*
|
|
337
|
+
* @remarks
|
|
338
|
+
* Time O(n), Space O(k) where `k` is the number of kept elements.
|
|
339
|
+
*/
|
|
340
|
+
abstract filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
227
341
|
|
|
228
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Internal iterator factory used by the default iterator.
|
|
344
|
+
*
|
|
345
|
+
* @param args Optional iterator arguments.
|
|
346
|
+
* @returns An iterator over elements.
|
|
347
|
+
*
|
|
348
|
+
* @remarks
|
|
349
|
+
* Implementations should yield in O(1) per element with O(1) extra space when possible.
|
|
350
|
+
*/
|
|
351
|
+
protected abstract _getIterator(...args: unknown[]): IterableIterator<E>;
|
|
229
352
|
}
|