deque-typed 2.1.0 → 2.1.2
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/cjs/index.cjs +1223 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +1219 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +11 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +3 -3
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/deque-typed.js +1232 -0
- package/dist/umd/deque-typed.js.map +1 -0
- package/dist/umd/deque-typed.min.js +9 -0
- package/dist/umd/deque-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +8 -11
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +6 -11
- package/src/data-structures/binary-tree/avl-tree.ts +6 -8
- package/src/data-structures/binary-tree/binary-tree.ts +13 -15
- package/src/data-structures/binary-tree/bst.ts +6 -11
- package/src/data-structures/binary-tree/red-black-tree.ts +6 -11
- package/src/data-structures/binary-tree/tree-counter.ts +8 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -11
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +3 -3
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -28
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -95,10 +95,7 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
|
95
95
|
* @template V
|
|
96
96
|
* @template R
|
|
97
97
|
*/
|
|
98
|
-
export class AVLTreeCounter<K = any, V = any, R extends
|
|
99
|
-
extends AVLTree<K, V, R>
|
|
100
|
-
implements IBinaryTree<K, V, R>
|
|
101
|
-
{
|
|
98
|
+
export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
102
99
|
/**
|
|
103
100
|
* Create a AVLTreeCounter instance
|
|
104
101
|
* @remarks Time O(n), Space O(n)
|
|
@@ -133,7 +130,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
133
130
|
return sum;
|
|
134
131
|
}
|
|
135
132
|
|
|
136
|
-
override
|
|
133
|
+
override createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V> {
|
|
137
134
|
return new AVLTreeCounterNode(key, this._isMapMode ? undefined : value, count) as AVLTreeCounterNode<K, V>;
|
|
138
135
|
}
|
|
139
136
|
|
|
@@ -314,7 +311,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
314
311
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
315
312
|
* @returns A new AVLTreeCounter with mapped entries.
|
|
316
313
|
*/
|
|
317
|
-
override map<MK = K, MV = V, MR
|
|
314
|
+
override map<MK = K, MV = V, MR = any>(
|
|
318
315
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
319
316
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
320
317
|
thisArg?: unknown
|
|
@@ -337,7 +334,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
337
334
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
338
335
|
* @returns An empty like-kind instance.
|
|
339
336
|
*/
|
|
340
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
337
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(
|
|
341
338
|
options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>
|
|
342
339
|
): this {
|
|
343
340
|
const Ctor = this.constructor as unknown as new (
|
|
@@ -359,7 +356,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
359
356
|
* @param [options] - Options merged with the current snapshot.
|
|
360
357
|
* @returns A like-kind AVLTreeCounter built from the iterable.
|
|
361
358
|
*/
|
|
362
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
359
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
363
360
|
iter: Iterable<
|
|
364
361
|
TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
365
362
|
> = [],
|
|
@@ -394,10 +391,10 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
394
391
|
const [key, entryValue] = keyNodeOrEntry;
|
|
395
392
|
if (key === undefined || key === null) return [undefined, undefined];
|
|
396
393
|
const finalValue = value ?? entryValue;
|
|
397
|
-
return [this.
|
|
394
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
398
395
|
}
|
|
399
396
|
|
|
400
|
-
return [this.
|
|
397
|
+
return [this.createNode(keyNodeOrEntry, value, count), value];
|
|
401
398
|
}
|
|
402
399
|
|
|
403
400
|
/**
|
|
@@ -415,7 +412,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
415
412
|
destNode = this.ensureNode(destNode);
|
|
416
413
|
if (srcNode && destNode) {
|
|
417
414
|
const { key, value, count, height } = destNode;
|
|
418
|
-
const tempNode = this.
|
|
415
|
+
const tempNode = this.createNode(key, value, count);
|
|
419
416
|
if (tempNode) {
|
|
420
417
|
tempNode.height = height;
|
|
421
418
|
|
|
@@ -93,10 +93,7 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
|
93
93
|
* @template V
|
|
94
94
|
* @template R
|
|
95
95
|
*/
|
|
96
|
-
export class AVLTreeMultiMap<K = any, V = any, R extends
|
|
97
|
-
extends AVLTree<K, V[], R>
|
|
98
|
-
implements IBinaryTree<K, V[], R>
|
|
99
|
-
{
|
|
96
|
+
export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
100
97
|
/**
|
|
101
98
|
* Create an AVLTreeMultiMap and optionally bulk-insert items.
|
|
102
99
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -116,7 +113,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
116
113
|
}
|
|
117
114
|
}
|
|
118
115
|
|
|
119
|
-
override
|
|
116
|
+
override createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
|
|
120
117
|
return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
|
|
121
118
|
}
|
|
122
119
|
|
|
@@ -251,7 +248,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
251
248
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
252
249
|
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
253
250
|
*/
|
|
254
|
-
override map<MK = K, MVArr extends unknown[] = V[], MR
|
|
251
|
+
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
255
252
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
256
253
|
options?: Partial<AVLTreeOptions<MK, MVArr, MR>>,
|
|
257
254
|
thisArg?: unknown
|
|
@@ -268,7 +265,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
268
265
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
269
266
|
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
270
267
|
*/
|
|
271
|
-
override map<MK = K, MV = V[], MR
|
|
268
|
+
override map<MK = K, MV = V[], MR = any>(
|
|
272
269
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
273
270
|
options?: Partial<AVLTreeOptions<MK, MV, MR>>,
|
|
274
271
|
thisArg?: unknown
|
|
@@ -305,9 +302,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
305
302
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
306
303
|
* @returns An empty like-kind instance.
|
|
307
304
|
*/
|
|
308
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
309
|
-
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
310
|
-
): this {
|
|
305
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this {
|
|
311
306
|
const Ctor = this.constructor as unknown as new (
|
|
312
307
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
313
308
|
opts?: AVLTreeOptions<TK, TV, TR>
|
|
@@ -325,7 +320,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
325
320
|
* @param [options] - Options merged with the current snapshot.
|
|
326
321
|
* @returns A like-kind AVLTree built from the iterable.
|
|
327
322
|
*/
|
|
328
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
323
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
329
324
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
330
325
|
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
331
326
|
): AVLTree<TK, TV, TR> {
|
|
@@ -169,7 +169,7 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
169
169
|
* // { minute: 15, temperature: 58.6 }
|
|
170
170
|
* // ]
|
|
171
171
|
*/
|
|
172
|
-
export class AVLTree<K = any, V = any, R
|
|
172
|
+
export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
173
173
|
/**
|
|
174
174
|
* Creates an instance of AVLTree.
|
|
175
175
|
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
@@ -196,7 +196,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
196
196
|
* @param [value] - The value for the new node.
|
|
197
197
|
* @returns The newly created AVLTreeNode.
|
|
198
198
|
*/
|
|
199
|
-
override
|
|
199
|
+
override createNode(key: K, value?: V): AVLTreeNode<K, V> {
|
|
200
200
|
return new AVLTreeNode<K, V>(key, this._isMapMode ? undefined : value) as AVLTreeNode<K, V>;
|
|
201
201
|
}
|
|
202
202
|
|
|
@@ -301,7 +301,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
301
301
|
* @param [thisArg] - `this` context for the callback.
|
|
302
302
|
* @returns A new, mapped AVLTree.
|
|
303
303
|
*/
|
|
304
|
-
override map<MK = K, MV = V, MR
|
|
304
|
+
override map<MK = K, MV = V, MR = any>(
|
|
305
305
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
306
306
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
307
307
|
thisArg?: unknown
|
|
@@ -325,9 +325,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
325
325
|
* @param [options] - Options for the new tree.
|
|
326
326
|
* @returns A new, empty tree.
|
|
327
327
|
*/
|
|
328
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
329
|
-
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
330
|
-
): this {
|
|
328
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
331
329
|
const Ctor = this.constructor as unknown as new (
|
|
332
330
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
333
331
|
opts?: BSTOptions<TK, TV, TR>
|
|
@@ -344,7 +342,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
344
342
|
* @param [options] - Options for the new tree.
|
|
345
343
|
* @returns A new AVLTree.
|
|
346
344
|
*/
|
|
347
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
345
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
348
346
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
349
347
|
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
350
348
|
): AVLTree<TK, TV, TR> {
|
|
@@ -372,7 +370,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
372
370
|
|
|
373
371
|
if (srcNodeEnsured && destNodeEnsured) {
|
|
374
372
|
const { key, value, height } = destNodeEnsured;
|
|
375
|
-
const tempNode = this.
|
|
373
|
+
const tempNode = this.createNode(key, value);
|
|
376
374
|
|
|
377
375
|
if (tempNode) {
|
|
378
376
|
tempNode.height = height;
|
|
@@ -266,7 +266,7 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
266
266
|
*
|
|
267
267
|
* console.log(evaluate(expressionTree.root)); // -27
|
|
268
268
|
*/
|
|
269
|
-
export class BinaryTree<K = any, V = any, R
|
|
269
|
+
export class BinaryTree<K = any, V = any, R = any>
|
|
270
270
|
extends IterableEntryBase<K, V | undefined>
|
|
271
271
|
implements IBinaryTree<K, V, R>
|
|
272
272
|
{
|
|
@@ -390,7 +390,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
390
390
|
* @param [value] - The value for the new node (used if not in Map mode).
|
|
391
391
|
* @returns The newly created node.
|
|
392
392
|
*/
|
|
393
|
-
|
|
393
|
+
createNode(key: K, value?: V): BinaryTreeNode<K, V> {
|
|
394
394
|
return new BinaryTreeNode<K, V>(key, this._isMapMode ? undefined : value);
|
|
395
395
|
}
|
|
396
396
|
|
|
@@ -1178,9 +1178,9 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1178
1178
|
iterationType: IterationType = this.iterationType
|
|
1179
1179
|
): ReturnType<C> {
|
|
1180
1180
|
if (this.isNIL(startNode)) return callback(undefined);
|
|
1181
|
-
|
|
1181
|
+
const ensuredStartNode = this.ensureNode(startNode);
|
|
1182
1182
|
|
|
1183
|
-
if (!this.isRealNode(
|
|
1183
|
+
if (!this.isRealNode(ensuredStartNode)) return callback(undefined);
|
|
1184
1184
|
if (iterationType === 'RECURSIVE') {
|
|
1185
1185
|
const dfs = (cur: BinaryTreeNode<K, V>): BinaryTreeNode<K, V> => {
|
|
1186
1186
|
const { left } = cur;
|
|
@@ -1188,7 +1188,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1188
1188
|
return dfs(left);
|
|
1189
1189
|
};
|
|
1190
1190
|
|
|
1191
|
-
return callback(dfs(
|
|
1191
|
+
return callback(dfs(ensuredStartNode));
|
|
1192
1192
|
} else {
|
|
1193
1193
|
// Iterative (trampolined to prevent stack overflow, though 'ITERATIVE' usually means a loop)
|
|
1194
1194
|
const dfs = makeTrampoline((cur: BinaryTreeNode<K, V>): Trampoline<BinaryTreeNode<K, V>> => {
|
|
@@ -1197,7 +1197,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1197
1197
|
return makeTrampolineThunk(() => dfs(left));
|
|
1198
1198
|
});
|
|
1199
1199
|
|
|
1200
|
-
return callback(dfs(
|
|
1200
|
+
return callback(dfs(ensuredStartNode));
|
|
1201
1201
|
}
|
|
1202
1202
|
}
|
|
1203
1203
|
|
|
@@ -1689,7 +1689,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1689
1689
|
* @param [thisArg] - `this` context for the callback.
|
|
1690
1690
|
* @returns A new, mapped tree.
|
|
1691
1691
|
*/
|
|
1692
|
-
map<MK = K, MV = V, MR
|
|
1692
|
+
map<MK = K, MV = V, MR = any>(
|
|
1693
1693
|
cb: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
1694
1694
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
1695
1695
|
thisArg?: unknown
|
|
@@ -1948,7 +1948,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1948
1948
|
* @template TK, TV, TR - Generic types for the options.
|
|
1949
1949
|
* @returns The options object.
|
|
1950
1950
|
*/
|
|
1951
|
-
protected _snapshotOptions<TK = K, TV = V, TR
|
|
1951
|
+
protected _snapshotOptions<TK = K, TV = V, TR = R>(): BinaryTreeOptions<TK, TV, TR> {
|
|
1952
1952
|
return {
|
|
1953
1953
|
iterationType: this.iterationType,
|
|
1954
1954
|
toEntryFn: this.toEntryFn as unknown as BinaryTreeOptions<TK, TV, TR>['toEntryFn'],
|
|
@@ -1965,9 +1965,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1965
1965
|
* @param [options] - Options for the new tree.
|
|
1966
1966
|
* @returns A new, empty tree.
|
|
1967
1967
|
*/
|
|
1968
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
1969
|
-
options?: Partial<BinaryTreeOptions<TK, TV, TR>>
|
|
1970
|
-
): this {
|
|
1968
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BinaryTreeOptions<TK, TV, TR>>): this {
|
|
1971
1969
|
const Ctor = this.constructor as unknown as new (
|
|
1972
1970
|
iter?: Iterable<TK | BinaryTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
1973
1971
|
opts?: BinaryTreeOptions<TK, TV, TR>
|
|
@@ -1984,7 +1982,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1984
1982
|
* @param [options] - Options for the new tree.
|
|
1985
1983
|
* @returns A new tree.
|
|
1986
1984
|
*/
|
|
1987
|
-
protected _createLike<TK = K, TV = V, TR
|
|
1985
|
+
protected _createLike<TK = K, TV = V, TR = R>(
|
|
1988
1986
|
iter: Iterable<TK | BinaryTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
1989
1987
|
options?: Partial<BinaryTreeOptions<TK, TV, TR>>
|
|
1990
1988
|
): BinaryTree<TK, TV, TR> {
|
|
@@ -2021,10 +2019,10 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
2021
2019
|
if (key === undefined) return [undefined, undefined];
|
|
2022
2020
|
else if (key === null) return [null, undefined];
|
|
2023
2021
|
const finalValue = value ?? entryValue;
|
|
2024
|
-
return [this.
|
|
2022
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
2025
2023
|
}
|
|
2026
2024
|
|
|
2027
|
-
return [this.
|
|
2025
|
+
return [this.createNode(keyNodeOrEntry, value), value];
|
|
2028
2026
|
}
|
|
2029
2027
|
|
|
2030
2028
|
/**
|
|
@@ -2149,7 +2147,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
2149
2147
|
|
|
2150
2148
|
if (srcNode && destNode) {
|
|
2151
2149
|
const { key, value } = destNode;
|
|
2152
|
-
const tempNode = this.
|
|
2150
|
+
const tempNode = this.createNode(key, value); // Use a temp node to hold dest properties
|
|
2153
2151
|
|
|
2154
2152
|
if (tempNode) {
|
|
2155
2153
|
// Copy src to dest
|
|
@@ -165,10 +165,7 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
165
165
|
* console.log(findLCA(5, 35)); // 15
|
|
166
166
|
* console.log(findLCA(20, 30)); // 25
|
|
167
167
|
*/
|
|
168
|
-
export class BST<K = any, V = any, R extends
|
|
169
|
-
extends BinaryTree<K, V, R>
|
|
170
|
-
implements IBinaryTree<K, V, R>
|
|
171
|
-
{
|
|
168
|
+
export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
172
169
|
/**
|
|
173
170
|
* Creates an instance of BST.
|
|
174
171
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -271,7 +268,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
271
268
|
* @param [value] - The value for the new node (used if not in Map mode).
|
|
272
269
|
* @returns The newly created BSTNode.
|
|
273
270
|
*/
|
|
274
|
-
override
|
|
271
|
+
override createNode(key: K, value?: V): BSTNode<K, V> {
|
|
275
272
|
return new BSTNode<K, V>(key, this._isMapMode ? undefined : value);
|
|
276
273
|
}
|
|
277
274
|
|
|
@@ -811,7 +808,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
811
808
|
* @param [thisArg] - `this` context for the callback.
|
|
812
809
|
* @returns A new, mapped BST.
|
|
813
810
|
*/
|
|
814
|
-
override map<MK = K, MV = V, MR
|
|
811
|
+
override map<MK = K, MV = V, MR = any>(
|
|
815
812
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
816
813
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
817
814
|
thisArg?: unknown
|
|
@@ -864,9 +861,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
864
861
|
* @param [options] - Options for the new BST.
|
|
865
862
|
* @returns A new, empty BST.
|
|
866
863
|
*/
|
|
867
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
868
|
-
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
869
|
-
): this {
|
|
864
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
870
865
|
const Ctor = this.constructor as unknown as new (
|
|
871
866
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
872
867
|
opts?: BSTOptions<TK, TV, TR>
|
|
@@ -883,7 +878,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
883
878
|
* @param [options] - Options for the new BST.
|
|
884
879
|
* @returns A new BST.
|
|
885
880
|
*/
|
|
886
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
881
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
887
882
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
888
883
|
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
889
884
|
): BST<TK, TV, TR> {
|
|
@@ -901,7 +896,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
901
896
|
* @template TK, TV, TR - Generic types for the options.
|
|
902
897
|
* @returns The options object.
|
|
903
898
|
*/
|
|
904
|
-
protected override _snapshotOptions<TK = K, TV = V, TR
|
|
899
|
+
protected override _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR> {
|
|
905
900
|
return {
|
|
906
901
|
...super._snapshotOptions<TK, TV, TR>(),
|
|
907
902
|
specifyComparable: this.specifyComparable as BSTOptions<TK, TV, TR>['specifyComparable'],
|
|
@@ -89,7 +89,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
92
|
+
* Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
93
93
|
* @remarks Time O(1), Space O(1)
|
|
94
94
|
* @template K
|
|
95
95
|
* @template V
|
|
@@ -141,10 +141,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
141
141
|
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
142
142
|
*/
|
|
143
143
|
|
|
144
|
-
export class RedBlackTree<K = any, V = any, R extends
|
|
145
|
-
extends BST<K, V, R>
|
|
146
|
-
implements IBinaryTree<K, V, R>
|
|
147
|
-
{
|
|
144
|
+
export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
148
145
|
constructor(
|
|
149
146
|
keysNodesEntriesOrRaws: Iterable<
|
|
150
147
|
K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
@@ -179,7 +176,7 @@ export class RedBlackTree<K = any, V = any, R extends object = object>
|
|
|
179
176
|
* @param color - See parameter type for details.
|
|
180
177
|
* @returns A new RedBlackTreeNode instance.
|
|
181
178
|
*/
|
|
182
|
-
override
|
|
179
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): RedBlackTreeNode<K, V> {
|
|
183
180
|
return new RedBlackTreeNode<K, V>(key, this._isMapMode ? undefined : value, color);
|
|
184
181
|
}
|
|
185
182
|
|
|
@@ -324,7 +321,7 @@ export class RedBlackTree<K = any, V = any, R extends object = object>
|
|
|
324
321
|
* @returns A new RedBlackTree with mapped entries.
|
|
325
322
|
*/
|
|
326
323
|
|
|
327
|
-
override map<MK = K, MV = V, MR
|
|
324
|
+
override map<MK = K, MV = V, MR = any>(
|
|
328
325
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
329
326
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
330
327
|
thisArg?: unknown
|
|
@@ -338,9 +335,7 @@ export class RedBlackTree<K = any, V = any, R extends object = object>
|
|
|
338
335
|
return out;
|
|
339
336
|
}
|
|
340
337
|
|
|
341
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
342
|
-
options?: Partial<RedBlackTreeOptions<TK, TV, TR>>
|
|
343
|
-
): this {
|
|
338
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this {
|
|
344
339
|
const Ctor = this.constructor as unknown as new (
|
|
345
340
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
346
341
|
opts?: RedBlackTreeOptions<TK, TV, TR>
|
|
@@ -348,7 +343,7 @@ export class RedBlackTree<K = any, V = any, R extends object = object>
|
|
|
348
343
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
349
344
|
}
|
|
350
345
|
|
|
351
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
346
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
352
347
|
iter: Iterable<
|
|
353
348
|
TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
354
349
|
> = [],
|
|
@@ -100,10 +100,7 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
100
100
|
* @template V
|
|
101
101
|
* @template R
|
|
102
102
|
*/
|
|
103
|
-
export class TreeCounter<K = any, V = any, R extends
|
|
104
|
-
extends RedBlackTree<K, V, R>
|
|
105
|
-
implements IBinaryTree<K, V, R>
|
|
106
|
-
{
|
|
103
|
+
export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
107
104
|
/**
|
|
108
105
|
* Create a TreeCounter and optionally bulk-insert items.
|
|
109
106
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -145,7 +142,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
145
142
|
return sum;
|
|
146
143
|
}
|
|
147
144
|
|
|
148
|
-
override
|
|
145
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
|
|
149
146
|
return new TreeCounterNode(key, this._isMapMode ? undefined : value, count, color) as TreeCounterNode<K, V>;
|
|
150
147
|
}
|
|
151
148
|
|
|
@@ -342,7 +339,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
342
339
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
343
340
|
* @returns A new TreeCounter with mapped entries.
|
|
344
341
|
*/
|
|
345
|
-
override map<MK = K, MV = V, MR
|
|
342
|
+
override map<MK = K, MV = V, MR = any>(
|
|
346
343
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
347
344
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
348
345
|
thisArg?: unknown
|
|
@@ -377,9 +374,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
377
374
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
378
375
|
* @returns An empty like-kind instance.
|
|
379
376
|
*/
|
|
380
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
381
|
-
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
382
|
-
): this {
|
|
377
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
383
378
|
const Ctor = this.constructor as unknown as new (
|
|
384
379
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
385
380
|
opts?: BSTOptions<TK, TV, TR>
|
|
@@ -397,7 +392,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
397
392
|
* @param [options] - Options merged with the current snapshot.
|
|
398
393
|
* @returns A like-kind TreeCounter built from the iterable.
|
|
399
394
|
*/
|
|
400
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
395
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
401
396
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
402
397
|
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
403
398
|
): TreeCounter<TK, TV, TR> {
|
|
@@ -432,10 +427,10 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
432
427
|
const [key, entryValue] = keyNodeOrEntry;
|
|
433
428
|
if (key === undefined || key === null) return [undefined, undefined];
|
|
434
429
|
const finalValue = value ?? entryValue;
|
|
435
|
-
return [this.
|
|
430
|
+
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
436
431
|
}
|
|
437
432
|
|
|
438
|
-
return [this.
|
|
433
|
+
return [this.createNode(keyNodeOrEntry, value, 'BLACK', count), value];
|
|
439
434
|
}
|
|
440
435
|
|
|
441
436
|
/**
|
|
@@ -453,7 +448,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
453
448
|
destNode = this.ensureNode(destNode);
|
|
454
449
|
if (srcNode && destNode) {
|
|
455
450
|
const { key, value, count, color } = destNode;
|
|
456
|
-
const tempNode = this.
|
|
451
|
+
const tempNode = this.createNode(key, value, color, count);
|
|
457
452
|
if (tempNode) {
|
|
458
453
|
tempNode.color = color;
|
|
459
454
|
|
|
@@ -250,10 +250,7 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
|
|
|
250
250
|
* // ]
|
|
251
251
|
* // ]
|
|
252
252
|
*/
|
|
253
|
-
export class TreeMultiMap<K = any, V = any, R extends
|
|
254
|
-
extends RedBlackTree<K, V[], R>
|
|
255
|
-
implements IBinaryTree<K, V[], R>
|
|
256
|
-
{
|
|
253
|
+
export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
257
254
|
/**
|
|
258
255
|
* Create a TreeMultiMap and optionally bulk-insert items.
|
|
259
256
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -273,7 +270,7 @@ export class TreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
273
270
|
}
|
|
274
271
|
}
|
|
275
272
|
|
|
276
|
-
override
|
|
273
|
+
override createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
|
|
277
274
|
return new TreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
|
|
278
275
|
}
|
|
279
276
|
|
|
@@ -365,13 +362,13 @@ export class TreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
365
362
|
return false;
|
|
366
363
|
}
|
|
367
364
|
|
|
368
|
-
override map<MK = K, MVArr extends unknown[] = V[], MR
|
|
365
|
+
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
369
366
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
370
367
|
options?: Partial<RedBlackTreeOptions<MK, MVArr, MR>>,
|
|
371
368
|
thisArg?: unknown
|
|
372
369
|
): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
373
370
|
|
|
374
|
-
override map<MK = K, MV = V[], MR
|
|
371
|
+
override map<MK = K, MV = V[], MR = any>(
|
|
375
372
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
376
373
|
options?: Partial<RedBlackTreeOptions<MK, MV, MR>>,
|
|
377
374
|
thisArg?: unknown
|
|
@@ -408,9 +405,7 @@ export class TreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
408
405
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
409
406
|
* @returns An empty like-kind instance.
|
|
410
407
|
*/
|
|
411
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
412
|
-
options?: Partial<RedBlackTreeOptions<TK, TV, TR>>
|
|
413
|
-
): this {
|
|
408
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this {
|
|
414
409
|
const Ctor = this.constructor as unknown as new (
|
|
415
410
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
416
411
|
opts?: RedBlackTreeOptions<TK, TV, TR>
|
|
@@ -428,7 +423,7 @@ export class TreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
428
423
|
* @param [options] - Options merged with the current snapshot.
|
|
429
424
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
430
425
|
*/
|
|
431
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
426
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
432
427
|
iter: Iterable<
|
|
433
428
|
TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
434
429
|
> = [],
|
|
@@ -190,7 +190,7 @@ import { IterableElementBase } from '../base';
|
|
|
190
190
|
* ]);
|
|
191
191
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap
|
|
192
192
|
*/
|
|
193
|
-
export class Heap<E =
|
|
193
|
+
export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
194
194
|
protected _equals: (a: E, b: E) => boolean = Object.is;
|
|
195
195
|
|
|
196
196
|
/**
|
|
@@ -255,7 +255,7 @@ export class Heap<E = unknown, R = never> extends IterableElementBase<E, R> {
|
|
|
255
255
|
* @returns A new heap instance of this class.
|
|
256
256
|
*/
|
|
257
257
|
|
|
258
|
-
static from<T, R =
|
|
258
|
+
static from<T, R = any, S extends Heap<T, R> = Heap<T, R>>(
|
|
259
259
|
this: new (elements?: Iterable<T | R>, options?: HeapOptions<T, R>) => S,
|
|
260
260
|
elements?: Iterable<T | R>,
|
|
261
261
|
options?: HeapOptions<T, R>
|
|
@@ -273,7 +273,7 @@ export class Heap<E = unknown, R = never> extends IterableElementBase<E, R> {
|
|
|
273
273
|
* @returns A new Heap built from elements.
|
|
274
274
|
*/
|
|
275
275
|
|
|
276
|
-
static heapify<EE =
|
|
276
|
+
static heapify<EE = any, RR = any>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR> {
|
|
277
277
|
return new Heap<EE, RR>(elements, options);
|
|
278
278
|
}
|
|
279
279
|
|
|
@@ -788,7 +788,7 @@ export class FibonacciHeap<E> {
|
|
|
788
788
|
*/
|
|
789
789
|
|
|
790
790
|
push(element: E): this {
|
|
791
|
-
const node = this.
|
|
791
|
+
const node = this.createNode(element);
|
|
792
792
|
node.left = node;
|
|
793
793
|
node.right = node;
|
|
794
794
|
this.mergeWithRoot(node);
|
|
@@ -900,7 +900,7 @@ export class FibonacciHeap<E> {
|
|
|
900
900
|
heapToMerge.clear();
|
|
901
901
|
}
|
|
902
902
|
|
|
903
|
-
|
|
903
|
+
createNode(element: E): FibonacciHeapNode<E> {
|
|
904
904
|
return new FibonacciHeapNode<E>(element);
|
|
905
905
|
}
|
|
906
906
|
|
|
@@ -781,7 +781,7 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
781
781
|
* @returns A new SinglyLinkedListNode instance.
|
|
782
782
|
*/
|
|
783
783
|
|
|
784
|
-
protected
|
|
784
|
+
protected createNode(value: E): SinglyLinkedListNode<E> {
|
|
785
785
|
return new SinglyLinkedListNode<E>(value);
|
|
786
786
|
}
|
|
787
787
|
|
|
@@ -807,7 +807,7 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
807
807
|
|
|
808
808
|
protected _ensureNode(elementOrNode: E | SinglyLinkedListNode<E>) {
|
|
809
809
|
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
810
|
-
return this.
|
|
810
|
+
return this.createNode(elementOrNode);
|
|
811
811
|
}
|
|
812
812
|
|
|
813
813
|
/**
|
|
@@ -18,7 +18,7 @@ import type {
|
|
|
18
18
|
* K = key, V = value, R = raw/record used with toEntryFn (optional).
|
|
19
19
|
* Transforming methods like `map` use method-level generics MK/MV/MR.
|
|
20
20
|
*/
|
|
21
|
-
export interface IBinaryTree<K = any, V = any, R
|
|
21
|
+
export interface IBinaryTree<K = any, V = any, R = any> {
|
|
22
22
|
// ---- state ----
|
|
23
23
|
readonly size: number;
|
|
24
24
|
readonly root: BinaryTreeNode<K, V> | null | undefined;
|
|
@@ -32,7 +32,7 @@ export interface IBinaryTree<K = any, V = any, R extends object = object> {
|
|
|
32
32
|
readonly isDuplicate: boolean;
|
|
33
33
|
|
|
34
34
|
// ---- construction / mutation ----
|
|
35
|
-
|
|
35
|
+
createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
|
|
36
36
|
|
|
37
37
|
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R>;
|
|
38
38
|
|
|
@@ -232,7 +232,7 @@ export interface IBinaryTree<K = any, V = any, R extends object = object> {
|
|
|
232
232
|
|
|
233
233
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
|
|
234
234
|
|
|
235
|
-
map<MK = K, MV = V, MR
|
|
235
|
+
map<MK = K, MV = V, MR = any>(
|
|
236
236
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
237
237
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
238
238
|
thisArg?: unknown
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"allowSyntheticDefaultImports": true,
|
|
5
|
+
"baseUrl": "./",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"inlineSourceMap": false,
|
|
9
|
+
"lib": ["esnext"],
|
|
10
|
+
"listEmittedFiles": false,
|
|
11
|
+
"listFiles": false,
|
|
12
|
+
"noFallthroughCasesInSwitch": true,
|
|
13
|
+
"pretty": true,
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"strict": true,
|
|
17
|
+
"traceResolution": false,
|
|
18
|
+
"noImplicitOverride": true,
|
|
19
|
+
"types": ["node"]
|
|
20
|
+
},
|
|
21
|
+
"compileOnSave": false,
|
|
22
|
+
"exclude": ["node_modules", "dist"]
|
|
23
|
+
}
|