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
package/dist/esm/hash.mjs
CHANGED
|
@@ -323,6 +323,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
323
323
|
|
|
324
324
|
|
|
325
325
|
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
326
330
|
|
|
327
331
|
|
|
328
332
|
|
|
@@ -368,6 +372,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
368
372
|
|
|
369
373
|
|
|
370
374
|
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
371
379
|
|
|
372
380
|
|
|
373
381
|
|
|
@@ -459,6 +467,14 @@ var HashMap = class extends IterableEntryBase {
|
|
|
459
467
|
|
|
460
468
|
|
|
461
469
|
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
462
478
|
|
|
463
479
|
|
|
464
480
|
|
|
@@ -496,7 +512,7 @@ var HashMap = class extends IterableEntryBase {
|
|
|
496
512
|
if (this.store[strKey] === void 0) this._size++;
|
|
497
513
|
this._store[strKey] = { key, value };
|
|
498
514
|
}
|
|
499
|
-
return
|
|
515
|
+
return this;
|
|
500
516
|
}
|
|
501
517
|
/**
|
|
502
518
|
* Insert many entries from an iterable.
|
|
@@ -531,6 +547,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
531
547
|
|
|
532
548
|
|
|
533
549
|
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
534
554
|
|
|
535
555
|
|
|
536
556
|
|
|
@@ -548,7 +568,11 @@ var HashMap = class extends IterableEntryBase {
|
|
|
548
568
|
let key, value;
|
|
549
569
|
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
550
570
|
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
551
|
-
if (key !== void 0 && value !== void 0)
|
|
571
|
+
if (key !== void 0 && value !== void 0) {
|
|
572
|
+
const sizeBefore = this._size;
|
|
573
|
+
this.set(key, value);
|
|
574
|
+
results.push(sizeBefore < this._size);
|
|
575
|
+
}
|
|
552
576
|
}
|
|
553
577
|
return results;
|
|
554
578
|
}
|
|
@@ -587,6 +611,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
587
611
|
|
|
588
612
|
|
|
589
613
|
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
590
618
|
|
|
591
619
|
|
|
592
620
|
|
|
@@ -654,6 +682,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
654
682
|
|
|
655
683
|
|
|
656
684
|
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
657
689
|
|
|
658
690
|
|
|
659
691
|
|
|
@@ -706,6 +738,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
706
738
|
|
|
707
739
|
|
|
708
740
|
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
709
745
|
|
|
710
746
|
|
|
711
747
|
|
|
@@ -776,6 +812,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
776
812
|
|
|
777
813
|
|
|
778
814
|
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
779
819
|
|
|
780
820
|
|
|
781
821
|
|
|
@@ -829,6 +869,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
829
869
|
|
|
830
870
|
|
|
831
871
|
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
832
876
|
|
|
833
877
|
|
|
834
878
|
|
|
@@ -884,6 +928,10 @@ var HashMap = class extends IterableEntryBase {
|
|
|
884
928
|
|
|
885
929
|
|
|
886
930
|
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
887
935
|
|
|
888
936
|
|
|
889
937
|
|
|
@@ -1094,7 +1142,7 @@ var LinkedHashMap = class extends IterableEntryBase {
|
|
|
1094
1142
|
* @remarks Time O(1), Space O(1)
|
|
1095
1143
|
* @param key - Key.
|
|
1096
1144
|
* @param [value] - Value.
|
|
1097
|
-
* @returns
|
|
1145
|
+
* @returns This map (for chaining).
|
|
1098
1146
|
*/
|
|
1099
1147
|
set(key, value) {
|
|
1100
1148
|
let node;
|
|
@@ -1129,7 +1177,7 @@ var LinkedHashMap = class extends IterableEntryBase {
|
|
|
1129
1177
|
this._sentinel.prev = node;
|
|
1130
1178
|
this._size++;
|
|
1131
1179
|
}
|
|
1132
|
-
return
|
|
1180
|
+
return this;
|
|
1133
1181
|
}
|
|
1134
1182
|
setMany(entryOrRawElements) {
|
|
1135
1183
|
const results = [];
|
|
@@ -1137,7 +1185,11 @@ var LinkedHashMap = class extends IterableEntryBase {
|
|
|
1137
1185
|
let key, value;
|
|
1138
1186
|
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
1139
1187
|
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
1140
|
-
if (key !== void 0 && value !== void 0)
|
|
1188
|
+
if (key !== void 0 && value !== void 0) {
|
|
1189
|
+
const sizeBefore = this._size;
|
|
1190
|
+
this.set(key, value);
|
|
1191
|
+
results.push(sizeBefore < this._size);
|
|
1192
|
+
}
|
|
1141
1193
|
}
|
|
1142
1194
|
return results;
|
|
1143
1195
|
}
|
|
@@ -1215,13 +1267,16 @@ var LinkedHashMap = class extends IterableEntryBase {
|
|
|
1215
1267
|
* Delete the entry at a given index.
|
|
1216
1268
|
* @remarks Time O(N), Space O(1)
|
|
1217
1269
|
* @param index - Zero-based index.
|
|
1218
|
-
* @returns
|
|
1270
|
+
* @returns The removed entry [key, value].
|
|
1271
|
+
* @throws {RangeError} If index is out of bounds.
|
|
1219
1272
|
*/
|
|
1220
1273
|
deleteAt(index) {
|
|
1221
1274
|
rangeCheck(index, 0, this._size - 1);
|
|
1222
1275
|
let node = this.head;
|
|
1223
1276
|
while (index--) node = node.next;
|
|
1224
|
-
|
|
1277
|
+
const entry = [node.key, node.value];
|
|
1278
|
+
this._deleteNode(node);
|
|
1279
|
+
return entry;
|
|
1225
1280
|
}
|
|
1226
1281
|
isEmpty() {
|
|
1227
1282
|
return this._size === 0;
|
package/dist/esm/heap.mjs
CHANGED
|
@@ -319,6 +319,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
319
319
|
|
|
320
320
|
|
|
321
321
|
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
322
326
|
|
|
323
327
|
|
|
324
328
|
|
|
@@ -372,7 +376,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
372
376
|
}
|
|
373
377
|
/**
|
|
374
378
|
* Insert an element.
|
|
375
|
-
* @remarks Time O(
|
|
379
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
376
380
|
* @param element - Element to insert.
|
|
377
381
|
* @returns True.
|
|
378
382
|
|
|
@@ -405,6 +409,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
405
409
|
|
|
406
410
|
|
|
407
411
|
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
408
416
|
|
|
409
417
|
|
|
410
418
|
|
|
@@ -462,6 +470,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
462
470
|
|
|
463
471
|
|
|
464
472
|
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
465
477
|
|
|
466
478
|
|
|
467
479
|
|
|
@@ -522,10 +534,41 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
522
534
|
|
|
523
535
|
|
|
524
536
|
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
525
540
|
|
|
526
541
|
|
|
527
542
|
|
|
528
543
|
|
|
544
|
+
* @example
|
|
545
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
546
|
+
* interface Task {
|
|
547
|
+
* id: number;
|
|
548
|
+
* priority: number;
|
|
549
|
+
* name: string;
|
|
550
|
+
* }
|
|
551
|
+
*
|
|
552
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
553
|
+
* const tasks: Task[] = [
|
|
554
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
555
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
556
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
557
|
+
* ];
|
|
558
|
+
*
|
|
559
|
+
* const maxHeap = new Heap(tasks, {
|
|
560
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
561
|
+
* });
|
|
562
|
+
*
|
|
563
|
+
* console.log(maxHeap.size); // 3;
|
|
564
|
+
*
|
|
565
|
+
* // Peek returns highest priority task
|
|
566
|
+
* const topTask = maxHeap.peek();
|
|
567
|
+
* console.log(topTask?.priority); // 8;
|
|
568
|
+
* console.log(topTask?.name); // 'Alert';
|
|
569
|
+
*/
|
|
570
|
+
/**
|
|
571
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
529
572
|
* @example
|
|
530
573
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
531
574
|
* interface Task {
|
|
@@ -553,6 +596,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
553
596
|
* console.log(topTask?.name); // 'Alert';
|
|
554
597
|
*/
|
|
555
598
|
poll() {
|
|
599
|
+
return this.pop();
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
603
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
604
|
+
* @returns The removed top element, or undefined if empty.
|
|
605
|
+
*/
|
|
606
|
+
pop() {
|
|
556
607
|
if (this.elements.length === 0) return;
|
|
557
608
|
const value = this.elements[0];
|
|
558
609
|
const last = this.elements.pop();
|
|
@@ -596,6 +647,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
596
647
|
|
|
597
648
|
|
|
598
649
|
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
599
654
|
|
|
600
655
|
|
|
601
656
|
|
|
@@ -696,6 +751,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
696
751
|
|
|
697
752
|
|
|
698
753
|
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
699
758
|
|
|
700
759
|
|
|
701
760
|
|
|
@@ -743,6 +802,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
743
802
|
|
|
744
803
|
|
|
745
804
|
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
746
809
|
|
|
747
810
|
|
|
748
811
|
|
|
@@ -757,16 +820,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
757
820
|
clear() {
|
|
758
821
|
this._elements = [];
|
|
759
822
|
}
|
|
760
|
-
/**
|
|
761
|
-
* Replace the backing array and rebuild the heap.
|
|
762
|
-
* @remarks Time O(N), Space O(N)
|
|
763
|
-
* @param elements - Iterable used to refill the heap.
|
|
764
|
-
* @returns Array of per-node results from fixing steps.
|
|
765
|
-
*/
|
|
766
|
-
refill(elements) {
|
|
767
|
-
this._elements = Array.from(elements);
|
|
768
|
-
return this.fix();
|
|
769
|
-
}
|
|
770
823
|
/**
|
|
771
824
|
* Check if an equal element exists in the heap.
|
|
772
825
|
* @remarks Time O(N), Space O(1)
|
|
@@ -793,6 +846,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
793
846
|
|
|
794
847
|
|
|
795
848
|
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
796
853
|
|
|
797
854
|
|
|
798
855
|
|
|
@@ -840,6 +897,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
840
897
|
|
|
841
898
|
|
|
842
899
|
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
|
|
843
904
|
|
|
844
905
|
|
|
845
906
|
|
|
@@ -861,7 +922,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
861
922
|
}
|
|
862
923
|
if (index < 0) return false;
|
|
863
924
|
if (index === 0) {
|
|
864
|
-
this.
|
|
925
|
+
this.pop();
|
|
865
926
|
} else if (index === this.elements.length - 1) {
|
|
866
927
|
this.elements.pop();
|
|
867
928
|
} else {
|
|
@@ -871,13 +932,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
871
932
|
}
|
|
872
933
|
return true;
|
|
873
934
|
}
|
|
935
|
+
/**
|
|
936
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
937
|
+
*/
|
|
938
|
+
deleteBy(predicate) {
|
|
939
|
+
return this.deleteWhere(predicate);
|
|
940
|
+
}
|
|
874
941
|
/**
|
|
875
942
|
* Delete the first element that matches a predicate.
|
|
876
943
|
* @remarks Time O(N), Space O(1)
|
|
877
944
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
878
945
|
* @returns True if an element was removed.
|
|
879
946
|
*/
|
|
880
|
-
|
|
947
|
+
deleteWhere(predicate) {
|
|
881
948
|
let idx = -1;
|
|
882
949
|
for (let i = 0; i < this.elements.length; i++) {
|
|
883
950
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -887,7 +954,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
887
954
|
}
|
|
888
955
|
if (idx < 0) return false;
|
|
889
956
|
if (idx === 0) {
|
|
890
|
-
this.
|
|
957
|
+
this.pop();
|
|
891
958
|
} else if (idx === this.elements.length - 1) {
|
|
892
959
|
this.elements.pop();
|
|
893
960
|
} else {
|
|
@@ -933,6 +1000,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
933
1000
|
|
|
934
1001
|
|
|
935
1002
|
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
936
1007
|
|
|
937
1008
|
|
|
938
1009
|
|
|
@@ -1013,6 +1084,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1013
1084
|
|
|
1014
1085
|
|
|
1015
1086
|
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1016
1091
|
|
|
1017
1092
|
|
|
1018
1093
|
|
|
@@ -1066,6 +1141,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1066
1141
|
|
|
1067
1142
|
|
|
1068
1143
|
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1069
1148
|
|
|
1070
1149
|
|
|
1071
1150
|
|
|
@@ -1118,6 +1197,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1118
1197
|
|
|
1119
1198
|
|
|
1120
1199
|
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1121
1204
|
|
|
1122
1205
|
|
|
1123
1206
|
|
|
@@ -1177,6 +1260,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1177
1260
|
|
|
1178
1261
|
|
|
1179
1262
|
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1180
1267
|
|
|
1181
1268
|
|
|
1182
1269
|
|
|
@@ -1370,7 +1457,7 @@ var FibonacciHeap = class {
|
|
|
1370
1457
|
* Push an element into the root list.
|
|
1371
1458
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1372
1459
|
* @param element - Element to insert.
|
|
1373
|
-
* @returns
|
|
1460
|
+
* @returns True when the element is added.
|
|
1374
1461
|
*/
|
|
1375
1462
|
push(element) {
|
|
1376
1463
|
const node = this.createNode(element);
|
|
@@ -1379,7 +1466,7 @@ var FibonacciHeap = class {
|
|
|
1379
1466
|
this.mergeWithRoot(node);
|
|
1380
1467
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1381
1468
|
this._size++;
|
|
1382
|
-
return
|
|
1469
|
+
return true;
|
|
1383
1470
|
}
|
|
1384
1471
|
peek() {
|
|
1385
1472
|
return this.min ? this.min.element : void 0;
|