data-structure-typed 2.5.2 → 2.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -1
- package/MIGRATION.md +169 -0
- package/README.md +60 -6
- package/README_CN.md +551 -143
- package/SPECIFICATION.md +20 -14
- package/SPECIFICATION.zh-CN.md +20 -14
- package/dist/cjs/binary-tree.cjs +2417 -132
- package/dist/cjs/graph.cjs +248 -14
- package/dist/cjs/hash.cjs +62 -7
- package/dist/cjs/heap.cjs +103 -16
- package/dist/cjs/index.cjs +3046 -124
- package/dist/cjs/linked-list.cjs +219 -0
- package/dist/cjs/matrix.cjs +32 -0
- package/dist/cjs/priority-queue.cjs +101 -14
- package/dist/cjs/queue.cjs +215 -0
- package/dist/cjs/stack.cjs +44 -4
- package/dist/cjs/trie.cjs +44 -0
- package/dist/cjs-legacy/binary-tree.cjs +2406 -123
- package/dist/cjs-legacy/graph.cjs +248 -14
- package/dist/cjs-legacy/hash.cjs +62 -7
- package/dist/cjs-legacy/heap.cjs +103 -16
- package/dist/cjs-legacy/index.cjs +3105 -185
- package/dist/cjs-legacy/linked-list.cjs +219 -0
- package/dist/cjs-legacy/matrix.cjs +32 -0
- package/dist/cjs-legacy/priority-queue.cjs +101 -14
- package/dist/cjs-legacy/queue.cjs +215 -0
- package/dist/cjs-legacy/stack.cjs +44 -4
- package/dist/cjs-legacy/trie.cjs +44 -0
- package/dist/esm/binary-tree.mjs +2417 -132
- package/dist/esm/graph.mjs +248 -14
- package/dist/esm/hash.mjs +62 -7
- package/dist/esm/heap.mjs +103 -16
- package/dist/esm/index.mjs +3046 -124
- package/dist/esm/linked-list.mjs +219 -0
- package/dist/esm/matrix.mjs +32 -0
- package/dist/esm/priority-queue.mjs +101 -14
- package/dist/esm/queue.mjs +215 -0
- package/dist/esm/stack.mjs +44 -4
- package/dist/esm/trie.mjs +44 -0
- package/dist/esm-legacy/binary-tree.mjs +2406 -123
- package/dist/esm-legacy/graph.mjs +248 -14
- package/dist/esm-legacy/hash.mjs +62 -7
- package/dist/esm-legacy/heap.mjs +103 -16
- package/dist/esm-legacy/index.mjs +3105 -185
- package/dist/esm-legacy/linked-list.mjs +219 -0
- package/dist/esm-legacy/matrix.mjs +32 -0
- package/dist/esm-legacy/priority-queue.mjs +101 -14
- package/dist/esm-legacy/queue.mjs +215 -0
- package/dist/esm-legacy/stack.mjs +44 -4
- package/dist/esm-legacy/trie.mjs +44 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +50 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +56 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +116 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +99 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +79 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +24 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +520 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +489 -1
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +393 -1
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +500 -1
- package/dist/types/data-structures/graph/directed-graph.d.ts +40 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +36 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +51 -6
- package/dist/types/data-structures/heap/heap.d.ts +98 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +61 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +72 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +32 -0
- package/dist/types/data-structures/queue/deque.d.ts +82 -0
- package/dist/types/data-structures/queue/queue.d.ts +61 -0
- package/dist/types/data-structures/stack/stack.d.ts +42 -2
- package/dist/types/data-structures/trie/trie.d.ts +48 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/data-structure-typed.js +3105 -185
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +188 -200
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +62 -62
- package/docs-site-docusaurus/docs/api/classes/BST.md +183 -195
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +15 -15
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +143 -155
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +99 -85
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +73 -73
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +100 -70
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +12 -12
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +38 -38
- package/docs-site-docusaurus/docs/api/classes/Heap.md +96 -85
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +25 -25
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +48 -48
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +52 -52
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +74 -74
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +73 -73
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +31 -31
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +103 -88
- package/docs-site-docusaurus/docs/api/classes/Queue.md +111 -59
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +200 -212
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +75 -75
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +37 -37
- package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +107 -36
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +43 -43
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +106 -35
- package/docs-site-docusaurus/docs/api/classes/Trie.md +43 -43
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +72 -72
- package/docs-site-docusaurus/docs/guide/architecture.md +75 -7
- package/docs-site-docusaurus/docs/guide/concepts.md +53 -34
- package/docs-site-docusaurus/docs/guide/faq.md +53 -0
- package/docs-site-docusaurus/docs/guide/guides.md +8 -9
- package/docs-site-docusaurus/docs/guide/integrations.md +74 -177
- package/docs-site-docusaurus/docs/guide/overview.md +131 -17
- package/docs-site-docusaurus/src/pages/index.tsx +4 -0
- package/docs-site-docusaurus/typedoc.json +1 -0
- package/package.json +7 -6
- package/src/data-structures/binary-tree/avl-tree.ts +52 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +56 -0
- package/src/data-structures/binary-tree/binary-tree.ts +167 -81
- package/src/data-structures/binary-tree/bst.ts +101 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +82 -15
- package/src/data-structures/binary-tree/segment-tree.ts +24 -0
- package/src/data-structures/binary-tree/tree-map.ts +540 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +490 -2
- package/src/data-structures/binary-tree/tree-multi-set.ts +393 -1
- package/src/data-structures/binary-tree/tree-set.ts +520 -3
- package/src/data-structures/graph/directed-graph.ts +41 -1
- package/src/data-structures/graph/undirected-graph.ts +37 -1
- package/src/data-structures/hash/hash-map.ts +67 -12
- package/src/data-structures/heap/heap.ts +107 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +88 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +61 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +72 -0
- package/src/data-structures/matrix/matrix.ts +32 -0
- package/src/data-structures/queue/deque.ts +85 -0
- package/src/data-structures/queue/queue.ts +73 -0
- package/src/data-structures/stack/stack.ts +45 -5
- package/src/data-structures/trie/trie.ts +48 -0
- package/src/interfaces/binary-tree.ts +1 -9
- package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +0 -12984
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +0 -3
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +0 -4505
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +0 -9731
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +0 -347
|
@@ -244,6 +244,10 @@ export class DirectedGraph<
|
|
|
244
244
|
|
|
245
245
|
|
|
246
246
|
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
247
251
|
|
|
248
252
|
|
|
249
253
|
|
|
@@ -339,6 +343,10 @@ export class DirectedGraph<
|
|
|
339
343
|
|
|
340
344
|
|
|
341
345
|
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
342
350
|
|
|
343
351
|
|
|
344
352
|
|
|
@@ -429,6 +437,10 @@ export class DirectedGraph<
|
|
|
429
437
|
|
|
430
438
|
|
|
431
439
|
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
432
444
|
|
|
433
445
|
|
|
434
446
|
|
|
@@ -519,6 +531,10 @@ export class DirectedGraph<
|
|
|
519
531
|
|
|
520
532
|
|
|
521
533
|
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
522
538
|
|
|
523
539
|
|
|
524
540
|
|
|
@@ -574,6 +590,10 @@ export class DirectedGraph<
|
|
|
574
590
|
|
|
575
591
|
|
|
576
592
|
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
577
597
|
|
|
578
598
|
|
|
579
599
|
|
|
@@ -701,6 +721,10 @@ export class DirectedGraph<
|
|
|
701
721
|
|
|
702
722
|
|
|
703
723
|
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
704
728
|
|
|
705
729
|
|
|
706
730
|
|
|
@@ -795,6 +819,10 @@ export class DirectedGraph<
|
|
|
795
819
|
|
|
796
820
|
|
|
797
821
|
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
798
826
|
|
|
799
827
|
|
|
800
828
|
|
|
@@ -846,6 +874,10 @@ export class DirectedGraph<
|
|
|
846
874
|
|
|
847
875
|
|
|
848
876
|
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
849
881
|
|
|
850
882
|
|
|
851
883
|
|
|
@@ -909,7 +941,7 @@ export class DirectedGraph<
|
|
|
909
941
|
* Remove all vertices and edges.
|
|
910
942
|
* @remarks Time O(V + E), Space O(1)
|
|
911
943
|
*/
|
|
912
|
-
clear() {
|
|
944
|
+
clear(): void {
|
|
913
945
|
this._vertexMap = new Map<VertexKey, VO>();
|
|
914
946
|
this._inEdgeMap = new Map<VO, EO[]>();
|
|
915
947
|
this._outEdgeMap = new Map<VO, EO[]>();
|
|
@@ -955,6 +987,10 @@ export class DirectedGraph<
|
|
|
955
987
|
|
|
956
988
|
|
|
957
989
|
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
958
994
|
|
|
959
995
|
|
|
960
996
|
|
|
@@ -1074,6 +1110,10 @@ export class DirectedGraph<
|
|
|
1074
1110
|
|
|
1075
1111
|
|
|
1076
1112
|
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1077
1117
|
|
|
1078
1118
|
|
|
1079
1119
|
|
|
@@ -253,6 +253,10 @@ export class UndirectedGraph<
|
|
|
253
253
|
|
|
254
254
|
|
|
255
255
|
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
256
260
|
|
|
257
261
|
|
|
258
262
|
|
|
@@ -344,6 +348,10 @@ export class UndirectedGraph<
|
|
|
344
348
|
|
|
345
349
|
|
|
346
350
|
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
347
355
|
|
|
348
356
|
|
|
349
357
|
|
|
@@ -430,6 +438,10 @@ export class UndirectedGraph<
|
|
|
430
438
|
|
|
431
439
|
|
|
432
440
|
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
433
445
|
|
|
434
446
|
|
|
435
447
|
|
|
@@ -540,6 +552,10 @@ export class UndirectedGraph<
|
|
|
540
552
|
|
|
541
553
|
|
|
542
554
|
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
543
559
|
|
|
544
560
|
|
|
545
561
|
|
|
@@ -595,6 +611,10 @@ export class UndirectedGraph<
|
|
|
595
611
|
|
|
596
612
|
|
|
597
613
|
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
598
618
|
|
|
599
619
|
|
|
600
620
|
|
|
@@ -679,7 +699,7 @@ export class UndirectedGraph<
|
|
|
679
699
|
* Remove all vertices and edges.
|
|
680
700
|
* @remarks Time O(V + E), Space O(1)
|
|
681
701
|
*/
|
|
682
|
-
clear() {
|
|
702
|
+
clear(): void {
|
|
683
703
|
this._vertexMap = new Map<VertexKey, VO>();
|
|
684
704
|
this._edgeMap = new Map<VO, EO[]>();
|
|
685
705
|
}
|
|
@@ -724,6 +744,10 @@ export class UndirectedGraph<
|
|
|
724
744
|
|
|
725
745
|
|
|
726
746
|
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
727
751
|
|
|
728
752
|
|
|
729
753
|
|
|
@@ -897,6 +921,10 @@ export class UndirectedGraph<
|
|
|
897
921
|
|
|
898
922
|
|
|
899
923
|
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
900
928
|
|
|
901
929
|
|
|
902
930
|
|
|
@@ -968,6 +996,10 @@ export class UndirectedGraph<
|
|
|
968
996
|
|
|
969
997
|
|
|
970
998
|
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
971
1003
|
|
|
972
1004
|
|
|
973
1005
|
|
|
@@ -1019,6 +1051,10 @@ export class UndirectedGraph<
|
|
|
1019
1051
|
|
|
1020
1052
|
|
|
1021
1053
|
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1022
1058
|
|
|
1023
1059
|
|
|
1024
1060
|
|
|
@@ -199,6 +199,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
199
199
|
|
|
200
200
|
|
|
201
201
|
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
202
206
|
|
|
203
207
|
|
|
204
208
|
|
|
@@ -245,6 +249,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
245
249
|
|
|
246
250
|
|
|
247
251
|
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
248
256
|
|
|
249
257
|
|
|
250
258
|
|
|
@@ -338,6 +346,14 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
338
346
|
|
|
339
347
|
|
|
340
348
|
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
341
357
|
|
|
342
358
|
|
|
343
359
|
|
|
@@ -366,7 +382,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
366
382
|
* // Verify entries
|
|
367
383
|
* console.log([...map.entries()]); // length: 4;
|
|
368
384
|
*/
|
|
369
|
-
set(key: K, value: V):
|
|
385
|
+
set(key: K, value: V): this {
|
|
370
386
|
if (this._isObjKey(key)) {
|
|
371
387
|
if (!this.objMap.has(key)) this._size++;
|
|
372
388
|
this.objMap.set(key, value);
|
|
@@ -375,7 +391,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
375
391
|
if (this.store[strKey] === undefined) this._size++;
|
|
376
392
|
this._store[strKey] = { key, value };
|
|
377
393
|
}
|
|
378
|
-
return
|
|
394
|
+
return this;
|
|
379
395
|
}
|
|
380
396
|
|
|
381
397
|
/**
|
|
@@ -411,6 +427,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
411
427
|
|
|
412
428
|
|
|
413
429
|
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
414
434
|
|
|
415
435
|
|
|
416
436
|
|
|
@@ -428,7 +448,11 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
428
448
|
let key: K | undefined, value: V | undefined;
|
|
429
449
|
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
430
450
|
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
431
|
-
if (key !== undefined && value !== undefined)
|
|
451
|
+
if (key !== undefined && value !== undefined) {
|
|
452
|
+
const sizeBefore = this._size;
|
|
453
|
+
this.set(key, value);
|
|
454
|
+
results.push(sizeBefore < this._size);
|
|
455
|
+
}
|
|
432
456
|
}
|
|
433
457
|
return results;
|
|
434
458
|
}
|
|
@@ -468,6 +492,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
468
492
|
|
|
469
493
|
|
|
470
494
|
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
471
499
|
|
|
472
500
|
|
|
473
501
|
|
|
@@ -536,6 +564,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
536
564
|
|
|
537
565
|
|
|
538
566
|
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
539
571
|
|
|
540
572
|
|
|
541
573
|
|
|
@@ -589,6 +621,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
589
621
|
|
|
590
622
|
|
|
591
623
|
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
592
628
|
|
|
593
629
|
|
|
594
630
|
|
|
@@ -661,6 +697,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
661
697
|
|
|
662
698
|
|
|
663
699
|
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
664
704
|
|
|
665
705
|
|
|
666
706
|
|
|
@@ -715,6 +755,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
715
755
|
|
|
716
756
|
|
|
717
757
|
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
718
762
|
|
|
719
763
|
|
|
720
764
|
|
|
@@ -771,6 +815,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
771
815
|
|
|
772
816
|
|
|
773
817
|
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
774
822
|
|
|
775
823
|
|
|
776
824
|
|
|
@@ -803,11 +851,11 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
803
851
|
* console.log(values); // contains 7;
|
|
804
852
|
*/
|
|
805
853
|
|
|
806
|
-
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown):
|
|
854
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): this {
|
|
807
855
|
const out = this._createLike<K, V, [K, V]>();
|
|
808
856
|
let index = 0;
|
|
809
857
|
for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
|
|
810
|
-
return out;
|
|
858
|
+
return out as this;
|
|
811
859
|
}
|
|
812
860
|
|
|
813
861
|
/**
|
|
@@ -1015,9 +1063,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
1015
1063
|
* @remarks Time O(1), Space O(1)
|
|
1016
1064
|
* @param key - Key.
|
|
1017
1065
|
* @param [value] - Value.
|
|
1018
|
-
* @returns
|
|
1066
|
+
* @returns This map (for chaining).
|
|
1019
1067
|
*/
|
|
1020
|
-
set(key: K, value?: V):
|
|
1068
|
+
set(key: K, value?: V): this {
|
|
1021
1069
|
let node: HashMapLinkedNode<K, V | undefined> | undefined;
|
|
1022
1070
|
const isNewKey = !this.has(key);
|
|
1023
1071
|
|
|
@@ -1053,7 +1101,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
1053
1101
|
this._size++;
|
|
1054
1102
|
}
|
|
1055
1103
|
|
|
1056
|
-
return
|
|
1104
|
+
return this;
|
|
1057
1105
|
}
|
|
1058
1106
|
|
|
1059
1107
|
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
|
|
@@ -1062,7 +1110,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
1062
1110
|
let key: K | undefined, value: V | undefined;
|
|
1063
1111
|
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
1064
1112
|
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
1065
|
-
if (key !== undefined && value !== undefined)
|
|
1113
|
+
if (key !== undefined && value !== undefined) {
|
|
1114
|
+
const sizeBefore = this._size;
|
|
1115
|
+
this.set(key, value);
|
|
1116
|
+
results.push(sizeBefore < this._size);
|
|
1117
|
+
}
|
|
1066
1118
|
}
|
|
1067
1119
|
return results;
|
|
1068
1120
|
}
|
|
@@ -1146,13 +1198,16 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
1146
1198
|
* Delete the entry at a given index.
|
|
1147
1199
|
* @remarks Time O(N), Space O(1)
|
|
1148
1200
|
* @param index - Zero-based index.
|
|
1149
|
-
* @returns
|
|
1201
|
+
* @returns The removed entry [key, value].
|
|
1202
|
+
* @throws {RangeError} If index is out of bounds.
|
|
1150
1203
|
*/
|
|
1151
|
-
deleteAt(index: number):
|
|
1204
|
+
deleteAt(index: number): [K, V | undefined] {
|
|
1152
1205
|
rangeCheck(index, 0, this._size - 1);
|
|
1153
1206
|
let node = this.head;
|
|
1154
1207
|
while (index--) node = node.next;
|
|
1155
|
-
|
|
1208
|
+
const entry: [K, V | undefined] = [node.key as K, node.value];
|
|
1209
|
+
this._deleteNode(node);
|
|
1210
|
+
return entry;
|
|
1156
1211
|
}
|
|
1157
1212
|
|
|
1158
1213
|
isEmpty(): boolean {
|
|
@@ -12,7 +12,7 @@ import { ERR, raise } from '../../common';
|
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
|
|
15
|
-
* @remarks
|
|
15
|
+
* @remarks Typical operations: O(log N) insert/remove, O(1) peek. Space O(N).
|
|
16
16
|
* @template E
|
|
17
17
|
* @template R
|
|
18
18
|
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
@@ -215,6 +215,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
215
215
|
|
|
216
216
|
|
|
217
217
|
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
218
222
|
|
|
219
223
|
|
|
220
224
|
|
|
@@ -280,7 +284,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
280
284
|
|
|
281
285
|
/**
|
|
282
286
|
* Insert an element.
|
|
283
|
-
* @remarks Time O(
|
|
287
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
284
288
|
* @param element - Element to insert.
|
|
285
289
|
* @returns True.
|
|
286
290
|
|
|
@@ -313,6 +317,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
313
317
|
|
|
314
318
|
|
|
315
319
|
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
316
324
|
|
|
317
325
|
|
|
318
326
|
|
|
@@ -372,6 +380,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
372
380
|
|
|
373
381
|
|
|
374
382
|
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
375
387
|
|
|
376
388
|
|
|
377
389
|
|
|
@@ -434,6 +446,9 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
434
446
|
|
|
435
447
|
|
|
436
448
|
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
437
452
|
|
|
438
453
|
|
|
439
454
|
|
|
@@ -465,7 +480,44 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
465
480
|
* console.log(topTask?.name); // 'Alert';
|
|
466
481
|
*/
|
|
467
482
|
|
|
483
|
+
/**
|
|
484
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
485
|
+
* @example
|
|
486
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
487
|
+
* interface Task {
|
|
488
|
+
* id: number;
|
|
489
|
+
* priority: number;
|
|
490
|
+
* name: string;
|
|
491
|
+
* }
|
|
492
|
+
*
|
|
493
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
494
|
+
* const tasks: Task[] = [
|
|
495
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
496
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
497
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
498
|
+
* ];
|
|
499
|
+
*
|
|
500
|
+
* const maxHeap = new Heap(tasks, {
|
|
501
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
502
|
+
* });
|
|
503
|
+
*
|
|
504
|
+
* console.log(maxHeap.size); // 3;
|
|
505
|
+
*
|
|
506
|
+
* // Peek returns highest priority task
|
|
507
|
+
* const topTask = maxHeap.peek();
|
|
508
|
+
* console.log(topTask?.priority); // 8;
|
|
509
|
+
* console.log(topTask?.name); // 'Alert';
|
|
510
|
+
*/
|
|
468
511
|
poll(): E | undefined {
|
|
512
|
+
return this.pop();
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
517
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
518
|
+
* @returns The removed top element, or undefined if empty.
|
|
519
|
+
*/
|
|
520
|
+
pop(): E | undefined {
|
|
469
521
|
if (this.elements.length === 0) return;
|
|
470
522
|
const value = this.elements[0];
|
|
471
523
|
const last = this.elements.pop()!;
|
|
@@ -510,6 +562,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
510
562
|
|
|
511
563
|
|
|
512
564
|
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
513
569
|
|
|
514
570
|
|
|
515
571
|
|
|
@@ -612,6 +668,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
612
668
|
|
|
613
669
|
|
|
614
670
|
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
615
675
|
|
|
616
676
|
|
|
617
677
|
|
|
@@ -661,6 +721,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
661
721
|
|
|
662
722
|
|
|
663
723
|
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
664
728
|
|
|
665
729
|
|
|
666
730
|
|
|
@@ -677,17 +741,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
677
741
|
this._elements = [];
|
|
678
742
|
}
|
|
679
743
|
|
|
680
|
-
/**
|
|
681
|
-
* Replace the backing array and rebuild the heap.
|
|
682
|
-
* @remarks Time O(N), Space O(N)
|
|
683
|
-
* @param elements - Iterable used to refill the heap.
|
|
684
|
-
* @returns Array of per-node results from fixing steps.
|
|
685
|
-
*/
|
|
686
744
|
|
|
687
|
-
refill(elements: Iterable<E>): boolean[] {
|
|
688
|
-
this._elements = Array.from(elements);
|
|
689
|
-
return this.fix();
|
|
690
|
-
}
|
|
691
745
|
|
|
692
746
|
/**
|
|
693
747
|
* Check if an equal element exists in the heap.
|
|
@@ -715,6 +769,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
715
769
|
|
|
716
770
|
|
|
717
771
|
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
718
776
|
|
|
719
777
|
|
|
720
778
|
|
|
@@ -764,6 +822,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
764
822
|
|
|
765
823
|
|
|
766
824
|
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
767
829
|
|
|
768
830
|
|
|
769
831
|
|
|
@@ -786,7 +848,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
786
848
|
}
|
|
787
849
|
if (index < 0) return false;
|
|
788
850
|
if (index === 0) {
|
|
789
|
-
this.
|
|
851
|
+
this.pop();
|
|
790
852
|
} else if (index === this.elements.length - 1) {
|
|
791
853
|
this.elements.pop();
|
|
792
854
|
} else {
|
|
@@ -797,14 +859,20 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
797
859
|
return true;
|
|
798
860
|
}
|
|
799
861
|
|
|
862
|
+
/**
|
|
863
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
864
|
+
*/
|
|
865
|
+
deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean {
|
|
866
|
+
return this.deleteWhere(predicate);
|
|
867
|
+
}
|
|
868
|
+
|
|
800
869
|
/**
|
|
801
870
|
* Delete the first element that matches a predicate.
|
|
802
871
|
* @remarks Time O(N), Space O(1)
|
|
803
872
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
804
873
|
* @returns True if an element was removed.
|
|
805
874
|
*/
|
|
806
|
-
|
|
807
|
-
deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean {
|
|
875
|
+
deleteWhere(predicate: (element: E, index: number, heap: this) => boolean): boolean {
|
|
808
876
|
let idx = -1;
|
|
809
877
|
for (let i = 0; i < this.elements.length; i++) {
|
|
810
878
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -814,7 +882,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
814
882
|
}
|
|
815
883
|
if (idx < 0) return false;
|
|
816
884
|
if (idx === 0) {
|
|
817
|
-
this.
|
|
885
|
+
this.pop();
|
|
818
886
|
} else if (idx === this.elements.length - 1) {
|
|
819
887
|
this.elements.pop();
|
|
820
888
|
} else {
|
|
@@ -863,6 +931,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
863
931
|
|
|
864
932
|
|
|
865
933
|
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
866
938
|
|
|
867
939
|
|
|
868
940
|
|
|
@@ -948,6 +1020,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
948
1020
|
|
|
949
1021
|
|
|
950
1022
|
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
951
1027
|
|
|
952
1028
|
|
|
953
1029
|
|
|
@@ -1003,6 +1079,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1003
1079
|
|
|
1004
1080
|
|
|
1005
1081
|
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1006
1086
|
|
|
1007
1087
|
|
|
1008
1088
|
|
|
@@ -1057,6 +1137,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1057
1137
|
|
|
1058
1138
|
|
|
1059
1139
|
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1060
1144
|
|
|
1061
1145
|
|
|
1062
1146
|
|
|
@@ -1118,6 +1202,10 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1118
1202
|
|
|
1119
1203
|
|
|
1120
1204
|
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1121
1209
|
|
|
1122
1210
|
|
|
1123
1211
|
|
|
@@ -1361,17 +1449,17 @@ export class FibonacciHeap<E> {
|
|
|
1361
1449
|
* Push an element into the root list.
|
|
1362
1450
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1363
1451
|
* @param element - Element to insert.
|
|
1364
|
-
* @returns
|
|
1452
|
+
* @returns True when the element is added.
|
|
1365
1453
|
*/
|
|
1366
1454
|
|
|
1367
|
-
push(element: E):
|
|
1455
|
+
push(element: E): boolean {
|
|
1368
1456
|
const node = this.createNode(element);
|
|
1369
1457
|
node.left = node;
|
|
1370
1458
|
node.right = node;
|
|
1371
1459
|
this.mergeWithRoot(node);
|
|
1372
1460
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1373
1461
|
this._size++;
|
|
1374
|
-
return
|
|
1462
|
+
return true;
|
|
1375
1463
|
}
|
|
1376
1464
|
|
|
1377
1465
|
peek(): E | undefined {
|