max-priority-queue-typed 2.4.0 → 2.4.1
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/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
- package/package.json +2 -2
- package/src/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +45 -21
- package/src/data-structures/binary-tree/bst.ts +85 -10
- package/src/data-structures/binary-tree/index.ts +3 -3
- package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
- package/src/data-structures/binary-tree/tree-map.ts +439 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
- package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
- package/src/data-structures/binary-tree/tree-set.ts +407 -0
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/index.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
- package/src/data-structures/binary-tree/tree-counter.ts +0 -575
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
- package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
|
@@ -1,438 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Pablo Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
AVLTreeMultiMapOptions,
|
|
11
|
-
BTNOptKeyOrNull,
|
|
12
|
-
ElemOf,
|
|
13
|
-
EntryCallback,
|
|
14
|
-
FamilyPosition,
|
|
15
|
-
IterationType,
|
|
16
|
-
RBTNColor
|
|
17
|
-
} from '../../types';
|
|
18
|
-
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
19
|
-
import { IBinaryTree } from '../../interfaces';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
|
|
23
|
-
* @remarks Time O(1), Space O(1)
|
|
24
|
-
* @template K
|
|
25
|
-
* @template V
|
|
26
|
-
*/
|
|
27
|
-
export class AVLTreeMultiMapNode<K = any, V = any> {
|
|
28
|
-
key: K;
|
|
29
|
-
value?: V[];
|
|
30
|
-
parent?: AVLTreeMultiMapNode<K, V> = undefined;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Create an AVLTreeMultiMap node with a value bucket.
|
|
34
|
-
* @remarks Time O(1), Space O(1)
|
|
35
|
-
* @param key - Key of the node.
|
|
36
|
-
* @param value - Initial array of values.
|
|
37
|
-
* @returns New AVLTreeMultiMapNode instance.
|
|
38
|
-
*/
|
|
39
|
-
constructor(key: K, value: V[] = []) {
|
|
40
|
-
this.key = key;
|
|
41
|
-
this.value = value;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
_left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Get the left child pointer.
|
|
48
|
-
* @remarks Time O(1), Space O(1)
|
|
49
|
-
* @returns Left child node, or null/undefined.
|
|
50
|
-
*/
|
|
51
|
-
get left(): AVLTreeMultiMapNode<K, V> | null | undefined {
|
|
52
|
-
return this._left;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Set the left child and update its parent pointer.
|
|
57
|
-
* @remarks Time O(1), Space O(1)
|
|
58
|
-
* @param v - New left child node, or null/undefined.
|
|
59
|
-
* @returns void
|
|
60
|
-
*/
|
|
61
|
-
set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
|
|
62
|
-
if (v) {
|
|
63
|
-
v.parent = this;
|
|
64
|
-
}
|
|
65
|
-
this._left = v;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
_right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Get the right child pointer.
|
|
72
|
-
* @remarks Time O(1), Space O(1)
|
|
73
|
-
* @returns Right child node, or null/undefined.
|
|
74
|
-
*/
|
|
75
|
-
get right(): AVLTreeMultiMapNode<K, V> | null | undefined {
|
|
76
|
-
return this._right;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Set the right child and update its parent pointer.
|
|
81
|
-
* @remarks Time O(1), Space O(1)
|
|
82
|
-
* @param v - New right child node, or null/undefined.
|
|
83
|
-
* @returns void
|
|
84
|
-
*/
|
|
85
|
-
set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
|
|
86
|
-
if (v) {
|
|
87
|
-
v.parent = this;
|
|
88
|
-
}
|
|
89
|
-
this._right = v;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
_height: number = 0;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Gets the height of the node (used in self-balancing trees).
|
|
96
|
-
* @remarks Time O(1), Space O(1)
|
|
97
|
-
*
|
|
98
|
-
* @returns The height.
|
|
99
|
-
*/
|
|
100
|
-
get height(): number {
|
|
101
|
-
return this._height;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Sets the height of the node.
|
|
106
|
-
* @remarks Time O(1), Space O(1)
|
|
107
|
-
*
|
|
108
|
-
* @param value - The new height.
|
|
109
|
-
*/
|
|
110
|
-
set height(value: number) {
|
|
111
|
-
this._height = value;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
_color: RBTNColor = 'BLACK';
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Gets the color of the node (used in Red-Black trees).
|
|
118
|
-
* @remarks Time O(1), Space O(1)
|
|
119
|
-
*
|
|
120
|
-
* @returns The node's color.
|
|
121
|
-
*/
|
|
122
|
-
get color(): RBTNColor {
|
|
123
|
-
return this._color;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Sets the color of the node.
|
|
128
|
-
* @remarks Time O(1), Space O(1)
|
|
129
|
-
*
|
|
130
|
-
* @param value - The new color.
|
|
131
|
-
*/
|
|
132
|
-
set color(value: RBTNColor) {
|
|
133
|
-
this._color = value;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
_count: number = 1;
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
140
|
-
* @remarks Time O(1), Space O(1)
|
|
141
|
-
*
|
|
142
|
-
* @returns The subtree node count.
|
|
143
|
-
*/
|
|
144
|
-
get count(): number {
|
|
145
|
-
return this._count;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Sets the count of nodes in the subtree.
|
|
150
|
-
* @remarks Time O(1), Space O(1)
|
|
151
|
-
*
|
|
152
|
-
* @param value - The new count.
|
|
153
|
-
*/
|
|
154
|
-
set count(value: number) {
|
|
155
|
-
this._count = value;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Gets the position of the node relative to its parent.
|
|
160
|
-
* @remarks Time O(1), Space O(1)
|
|
161
|
-
*
|
|
162
|
-
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
163
|
-
*/
|
|
164
|
-
get familyPosition(): FamilyPosition {
|
|
165
|
-
if (!this.parent) {
|
|
166
|
-
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (this.parent.left === this) {
|
|
170
|
-
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
171
|
-
} else if (this.parent.right === this) {
|
|
172
|
-
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return 'MAL_NODE';
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
|
|
181
|
-
* @remarks Time O(1), Space O(1)
|
|
182
|
-
* @template K
|
|
183
|
-
* @template V
|
|
184
|
-
* @template R
|
|
185
|
-
*/
|
|
186
|
-
export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
187
|
-
/**
|
|
188
|
-
* Create an AVLTreeMultiMap and optionally bulk-insert items.
|
|
189
|
-
* @remarks Time O(N log N), Space O(N)
|
|
190
|
-
* @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
|
|
191
|
-
* @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
|
|
192
|
-
* @returns New AVLTreeMultiMap instance.
|
|
193
|
-
*/
|
|
194
|
-
constructor(
|
|
195
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
196
|
-
K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R
|
|
197
|
-
> = [],
|
|
198
|
-
options?: AVLTreeMultiMapOptions<K, V[], R>
|
|
199
|
-
) {
|
|
200
|
-
super([], { ...options, isMapMode: true });
|
|
201
|
-
if (keysNodesEntriesOrRaws) {
|
|
202
|
-
this.setMany(keysNodesEntriesOrRaws);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
override createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
|
|
207
|
-
return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Checks if the given item is a `AVLTreeMultiMapNode` instance.
|
|
212
|
-
* @remarks Time O(1), Space O(1)
|
|
213
|
-
*
|
|
214
|
-
* @param keyNodeOrEntry - The item to check.
|
|
215
|
-
* @returns True if it's a AVLTreeMultiMapNode, false otherwise.
|
|
216
|
-
*/
|
|
217
|
-
override isNode(
|
|
218
|
-
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
219
|
-
): keyNodeOrEntry is AVLTreeMultiMapNode<K, V> {
|
|
220
|
-
return keyNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
override set(
|
|
224
|
-
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
225
|
-
): boolean;
|
|
226
|
-
|
|
227
|
-
override set(key: K, value: V): boolean;
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
231
|
-
* @remarks Time O(log N + M), Space O(1)
|
|
232
|
-
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
|
|
233
|
-
* @param [value] - Single value to set when a bare key is provided.
|
|
234
|
-
* @returns True if inserted or appended; false if ignored.
|
|
235
|
-
*/
|
|
236
|
-
override set(
|
|
237
|
-
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
238
|
-
value?: V
|
|
239
|
-
): boolean {
|
|
240
|
-
if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
|
|
241
|
-
|
|
242
|
-
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
243
|
-
if (key === undefined || key === null) return false;
|
|
244
|
-
|
|
245
|
-
const _setToValues = () => {
|
|
246
|
-
const existingValues = this.get(key);
|
|
247
|
-
if (existingValues !== undefined && values !== undefined) {
|
|
248
|
-
for (const value of values) existingValues.push(value);
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
return false;
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
const _setByNode = () => {
|
|
255
|
-
const existingNode = this.getNode(key);
|
|
256
|
-
if (this.isRealNode(existingNode)) {
|
|
257
|
-
const existingValues = this.get(existingNode);
|
|
258
|
-
if (existingValues === undefined) {
|
|
259
|
-
super.set(key, values);
|
|
260
|
-
return true;
|
|
261
|
-
}
|
|
262
|
-
if (values !== undefined) {
|
|
263
|
-
for (const value of values) existingValues.push(value);
|
|
264
|
-
return true;
|
|
265
|
-
} else {
|
|
266
|
-
return false;
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
return super.set(key, values);
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
if (this._isMapMode) {
|
|
274
|
-
return _setByNode() || _setToValues();
|
|
275
|
-
}
|
|
276
|
-
return _setToValues() || _setByNode();
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
if (this.isEntry(keyNodeOrEntry)) {
|
|
280
|
-
const [key, values] = keyNodeOrEntry;
|
|
281
|
-
return _commonAdd(key, value !== undefined ? [value] : values);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
|
|
289
|
-
* @remarks Time O(log N), Space O(1)
|
|
290
|
-
* @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
|
|
291
|
-
* @param value - Value to remove from the bucket.
|
|
292
|
-
* @returns True if the value was removed; false if not found.
|
|
293
|
-
*/
|
|
294
|
-
deleteValue(
|
|
295
|
-
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
296
|
-
value: V
|
|
297
|
-
): boolean {
|
|
298
|
-
const values = this.get(keyNodeOrEntry);
|
|
299
|
-
if (Array.isArray(values)) {
|
|
300
|
-
const index = values.indexOf(value);
|
|
301
|
-
if (index === -1) return false;
|
|
302
|
-
values.splice(index, 1);
|
|
303
|
-
|
|
304
|
-
if (values.length === 0) this.delete(keyNodeOrEntry);
|
|
305
|
-
|
|
306
|
-
return true;
|
|
307
|
-
}
|
|
308
|
-
return false;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Rebuild the tree into a perfectly balanced form using in-order nodes.
|
|
313
|
-
* @remarks Time O(N), Space O(N)
|
|
314
|
-
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
|
|
315
|
-
* @returns True if rebalancing succeeded (tree not empty).
|
|
316
|
-
*/
|
|
317
|
-
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
318
|
-
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
319
|
-
const n = nodes.length;
|
|
320
|
-
if (n === 0) return false;
|
|
321
|
-
|
|
322
|
-
this._clearNodes();
|
|
323
|
-
|
|
324
|
-
const build = (l: number, r: number, parent?: any): any | undefined => {
|
|
325
|
-
if (l > r) return undefined;
|
|
326
|
-
const m = l + ((r - l) >> 1);
|
|
327
|
-
const root = nodes[m];
|
|
328
|
-
root.left = build(l, m - 1, root);
|
|
329
|
-
root.right = build(m + 1, r, root);
|
|
330
|
-
root.parent = parent;
|
|
331
|
-
const lh = root.left ? root.left.height : -1;
|
|
332
|
-
const rh = root.right ? root.right.height : -1;
|
|
333
|
-
root.height = Math.max(lh, rh) + 1;
|
|
334
|
-
return root;
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
const newRoot = build(0, n - 1, undefined);
|
|
338
|
-
this._setRoot(newRoot);
|
|
339
|
-
this._size = n;
|
|
340
|
-
return true;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Create a new tree by mapping each [key, values] bucket.
|
|
345
|
-
* @remarks Time O(N log N), Space O(N)
|
|
346
|
-
* @template MK
|
|
347
|
-
* @template MVArr
|
|
348
|
-
* @template MR
|
|
349
|
-
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
|
|
350
|
-
* @param [options] - Options for the output tree.
|
|
351
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
352
|
-
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
353
|
-
*/
|
|
354
|
-
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
355
|
-
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
356
|
-
options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>,
|
|
357
|
-
thisArg?: unknown
|
|
358
|
-
): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* Create a new tree by mapping each [key, values] bucket.
|
|
362
|
-
* @remarks Time O(N log N), Space O(N)
|
|
363
|
-
* @template MK
|
|
364
|
-
* @template MV
|
|
365
|
-
* @template MR
|
|
366
|
-
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
|
|
367
|
-
* @param [options] - Options for the output tree.
|
|
368
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
369
|
-
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
370
|
-
*/
|
|
371
|
-
override map<MK = K, MV = V[], MR = any>(
|
|
372
|
-
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
373
|
-
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
374
|
-
thisArg?: unknown
|
|
375
|
-
): AVLTree<MK, MV, MR>;
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Create a new tree by mapping each [key, values] bucket.
|
|
379
|
-
* @remarks Time O(N log N), Space O(N)
|
|
380
|
-
* @template MK
|
|
381
|
-
* @template MV
|
|
382
|
-
* @template MR
|
|
383
|
-
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
|
|
384
|
-
* @param [options] - Options for the output tree.
|
|
385
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
386
|
-
* @returns The mapped AVLTree or AVLTreeMultiMap depending on MV; see overloads.
|
|
387
|
-
*/
|
|
388
|
-
override map<MK, MV, MR extends object>(
|
|
389
|
-
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
390
|
-
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
391
|
-
thisArg?: unknown
|
|
392
|
-
): AVLTree<MK, MV, MR> {
|
|
393
|
-
const out = this._createLike<MK, MV, MR>([], options);
|
|
394
|
-
let i = 0;
|
|
395
|
-
for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
|
|
396
|
-
return out;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* (Protected) Create an empty instance of the same concrete class.
|
|
401
|
-
* @remarks Time O(1), Space O(1)
|
|
402
|
-
* @template TK
|
|
403
|
-
* @template TV
|
|
404
|
-
* @template TR
|
|
405
|
-
* @param [options] - Optional constructor options for the like-kind instance.
|
|
406
|
-
* @returns An empty like-kind instance.
|
|
407
|
-
*/
|
|
408
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(
|
|
409
|
-
options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
|
|
410
|
-
): this {
|
|
411
|
-
const Ctor = this.constructor as unknown as new (
|
|
412
|
-
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
413
|
-
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
414
|
-
) => AVLTree<TK, TV, TR>;
|
|
415
|
-
return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
420
|
-
* @remarks Time O(N log N), Space O(N)
|
|
421
|
-
* @template TK
|
|
422
|
-
* @template TV
|
|
423
|
-
* @template TR
|
|
424
|
-
* @param iter - Iterable used to seed the new tree.
|
|
425
|
-
* @param [options] - Options merged with the current snapshot.
|
|
426
|
-
* @returns A like-kind AVLTree built from the iterable.
|
|
427
|
-
*/
|
|
428
|
-
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
429
|
-
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
430
|
-
options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
|
|
431
|
-
): AVLTree<TK, TV, TR> {
|
|
432
|
-
const Ctor = this.constructor as unknown as new (
|
|
433
|
-
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
434
|
-
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
435
|
-
) => AVLTree<TK, TV, TR>;
|
|
436
|
-
return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
|
|
437
|
-
}
|
|
438
|
-
}
|