data-structure-typed 2.5.2 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +3 -1
- package/MIGRATION.md +217 -0
- package/README.md +80 -8
- package/README_CN.md +569 -143
- package/SPECIFICATION.md +44 -14
- package/SPECIFICATION.zh-CN.md +44 -14
- package/dist/cjs/binary-tree.cjs +5841 -1678
- package/dist/cjs/graph.cjs +422 -14
- package/dist/cjs/hash.cjs +95 -7
- package/dist/cjs/heap.cjs +174 -16
- package/dist/cjs/index.cjs +7751 -2449
- package/dist/cjs/linked-list.cjs +443 -2
- package/dist/cjs/matrix.cjs +56 -0
- package/dist/cjs/priority-queue.cjs +172 -14
- package/dist/cjs/queue.cjs +435 -0
- package/dist/cjs/stack.cjs +103 -4
- package/dist/cjs/trie.cjs +106 -0
- package/dist/cjs-legacy/binary-tree.cjs +5933 -1772
- package/dist/cjs-legacy/graph.cjs +422 -14
- package/dist/cjs-legacy/hash.cjs +95 -7
- package/dist/cjs-legacy/heap.cjs +174 -16
- package/dist/cjs-legacy/index.cjs +8154 -2854
- package/dist/cjs-legacy/linked-list.cjs +443 -2
- package/dist/cjs-legacy/matrix.cjs +56 -0
- package/dist/cjs-legacy/priority-queue.cjs +172 -14
- package/dist/cjs-legacy/queue.cjs +435 -0
- package/dist/cjs-legacy/stack.cjs +103 -4
- package/dist/cjs-legacy/trie.cjs +106 -0
- package/dist/esm/binary-tree.mjs +5841 -1678
- package/dist/esm/graph.mjs +422 -14
- package/dist/esm/hash.mjs +95 -7
- package/dist/esm/heap.mjs +174 -16
- package/dist/esm/index.mjs +7751 -2449
- package/dist/esm/linked-list.mjs +443 -2
- package/dist/esm/matrix.mjs +56 -0
- package/dist/esm/priority-queue.mjs +172 -14
- package/dist/esm/queue.mjs +435 -0
- package/dist/esm/stack.mjs +103 -4
- package/dist/esm/trie.mjs +106 -0
- package/dist/esm-legacy/binary-tree.mjs +5933 -1772
- package/dist/esm-legacy/graph.mjs +422 -14
- package/dist/esm-legacy/hash.mjs +95 -7
- package/dist/esm-legacy/heap.mjs +174 -16
- package/dist/esm-legacy/index.mjs +8154 -2854
- package/dist/esm-legacy/linked-list.mjs +443 -2
- package/dist/esm-legacy/matrix.mjs +56 -0
- package/dist/esm-legacy/priority-queue.mjs +172 -14
- package/dist/esm-legacy/queue.mjs +435 -0
- package/dist/esm-legacy/stack.mjs +103 -4
- package/dist/esm-legacy/trie.mjs +106 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
- package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
- package/dist/types/data-structures/heap/heap.d.ts +140 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
- package/dist/types/data-structures/queue/deque.d.ts +171 -0
- package/dist/types/data-structures/queue/queue.d.ts +97 -0
- package/dist/types/data-structures/stack/stack.d.ts +72 -2
- package/dist/types/data-structures/trie/trie.d.ts +84 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/data-structure-typed.js +7784 -2484
- 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 +46 -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/jest.integration.config.js +1 -2
- package/package.json +10 -7
- package/src/data-structures/base/iterable-element-base.ts +32 -0
- package/src/data-structures/base/linear-base.ts +11 -0
- package/src/data-structures/binary-tree/avl-tree.ts +88 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
- package/src/data-structures/binary-tree/binary-tree.ts +242 -81
- package/src/data-structures/binary-tree/bst.ts +173 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +948 -36
- package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
- package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
- package/src/data-structures/binary-tree/tree-set.ts +1260 -251
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +100 -12
- package/src/data-structures/heap/heap.ts +149 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
- package/src/data-structures/matrix/matrix.ts +56 -0
- package/src/data-structures/queue/deque.ts +187 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +84 -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-legacy/hash.mjs
CHANGED
|
@@ -318,6 +318,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
318
318
|
|
|
319
319
|
|
|
320
320
|
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
321
328
|
|
|
322
329
|
|
|
323
330
|
|
|
@@ -363,6 +370,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
363
370
|
|
|
364
371
|
|
|
365
372
|
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
366
380
|
|
|
367
381
|
|
|
368
382
|
|
|
@@ -451,6 +465,20 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
451
465
|
|
|
452
466
|
|
|
453
467
|
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
454
482
|
|
|
455
483
|
|
|
456
484
|
|
|
@@ -494,7 +522,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
494
522
|
if (this.store[strKey] === void 0) this._size++;
|
|
495
523
|
this._store[strKey] = { key, value };
|
|
496
524
|
}
|
|
497
|
-
return
|
|
525
|
+
return this;
|
|
498
526
|
}
|
|
499
527
|
/**
|
|
500
528
|
* Insert many entries from an iterable.
|
|
@@ -526,6 +554,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
526
554
|
|
|
527
555
|
|
|
528
556
|
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
529
564
|
|
|
530
565
|
|
|
531
566
|
|
|
@@ -546,7 +581,11 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
546
581
|
let key, value;
|
|
547
582
|
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
548
583
|
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
549
|
-
if (key !== void 0 && value !== void 0)
|
|
584
|
+
if (key !== void 0 && value !== void 0) {
|
|
585
|
+
const sizeBefore = this._size;
|
|
586
|
+
this.set(key, value);
|
|
587
|
+
results.push(sizeBefore < this._size);
|
|
588
|
+
}
|
|
550
589
|
}
|
|
551
590
|
return results;
|
|
552
591
|
}
|
|
@@ -582,6 +621,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
582
621
|
|
|
583
622
|
|
|
584
623
|
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
585
631
|
|
|
586
632
|
|
|
587
633
|
|
|
@@ -650,6 +696,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
650
696
|
|
|
651
697
|
|
|
652
698
|
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
653
706
|
|
|
654
707
|
|
|
655
708
|
|
|
@@ -702,6 +755,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
702
755
|
|
|
703
756
|
|
|
704
757
|
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
705
765
|
|
|
706
766
|
|
|
707
767
|
|
|
@@ -772,6 +832,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
772
832
|
|
|
773
833
|
|
|
774
834
|
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
775
842
|
|
|
776
843
|
|
|
777
844
|
|
|
@@ -825,6 +892,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
825
892
|
|
|
826
893
|
|
|
827
894
|
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
828
902
|
|
|
829
903
|
|
|
830
904
|
|
|
@@ -880,6 +954,13 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
880
954
|
|
|
881
955
|
|
|
882
956
|
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
883
964
|
|
|
884
965
|
|
|
885
966
|
|
|
@@ -1092,7 +1173,7 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
1092
1173
|
* @remarks Time O(1), Space O(1)
|
|
1093
1174
|
* @param key - Key.
|
|
1094
1175
|
* @param [value] - Value.
|
|
1095
|
-
* @returns
|
|
1176
|
+
* @returns This map (for chaining).
|
|
1096
1177
|
*/
|
|
1097
1178
|
set(key, value) {
|
|
1098
1179
|
let node;
|
|
@@ -1127,7 +1208,7 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
1127
1208
|
this._sentinel.prev = node;
|
|
1128
1209
|
this._size++;
|
|
1129
1210
|
}
|
|
1130
|
-
return
|
|
1211
|
+
return this;
|
|
1131
1212
|
}
|
|
1132
1213
|
setMany(entryOrRawElements) {
|
|
1133
1214
|
const results = [];
|
|
@@ -1135,7 +1216,11 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
1135
1216
|
let key, value;
|
|
1136
1217
|
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
1137
1218
|
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
1138
|
-
if (key !== void 0 && value !== void 0)
|
|
1219
|
+
if (key !== void 0 && value !== void 0) {
|
|
1220
|
+
const sizeBefore = this._size;
|
|
1221
|
+
this.set(key, value);
|
|
1222
|
+
results.push(sizeBefore < this._size);
|
|
1223
|
+
}
|
|
1139
1224
|
}
|
|
1140
1225
|
return results;
|
|
1141
1226
|
}
|
|
@@ -1213,13 +1298,16 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
1213
1298
|
* Delete the entry at a given index.
|
|
1214
1299
|
* @remarks Time O(N), Space O(1)
|
|
1215
1300
|
* @param index - Zero-based index.
|
|
1216
|
-
* @returns
|
|
1301
|
+
* @returns The removed entry [key, value].
|
|
1302
|
+
* @throws {RangeError} If index is out of bounds.
|
|
1217
1303
|
*/
|
|
1218
1304
|
deleteAt(index) {
|
|
1219
1305
|
rangeCheck(index, 0, this._size - 1);
|
|
1220
1306
|
let node = this.head;
|
|
1221
1307
|
while (index--) node = node.next;
|
|
1222
|
-
|
|
1308
|
+
const entry = [node.key, node.value];
|
|
1309
|
+
this._deleteNode(node);
|
|
1310
|
+
return entry;
|
|
1223
1311
|
}
|
|
1224
1312
|
isEmpty() {
|
|
1225
1313
|
return this._size === 0;
|
package/dist/esm-legacy/heap.mjs
CHANGED
|
@@ -183,6 +183,35 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
183
183
|
for (const ele of this) if (ele === element) return true;
|
|
184
184
|
return false;
|
|
185
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Check whether a value exists (Array-compatible alias for `has`).
|
|
188
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
189
|
+
* @param element - Element to search for (uses `===`).
|
|
190
|
+
* @returns `true` if found.
|
|
191
|
+
*/
|
|
192
|
+
includes(element) {
|
|
193
|
+
return this.has(element);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Return an iterator of `[index, value]` pairs (Array-compatible).
|
|
197
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
198
|
+
*/
|
|
199
|
+
*entries() {
|
|
200
|
+
let index = 0;
|
|
201
|
+
for (const value of this) {
|
|
202
|
+
yield [index++, value];
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Return an iterator of numeric indices (Array-compatible).
|
|
207
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
208
|
+
*/
|
|
209
|
+
*keys() {
|
|
210
|
+
let index = 0;
|
|
211
|
+
for (const _ of this) {
|
|
212
|
+
yield index++;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
186
215
|
/**
|
|
187
216
|
* Reduces all elements to a single accumulated value.
|
|
188
217
|
*
|
|
@@ -323,6 +352,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
323
352
|
|
|
324
353
|
|
|
325
354
|
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
326
362
|
|
|
327
363
|
|
|
328
364
|
|
|
@@ -380,7 +416,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
380
416
|
}
|
|
381
417
|
/**
|
|
382
418
|
* Insert an element.
|
|
383
|
-
* @remarks Time O(
|
|
419
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
384
420
|
* @param element - Element to insert.
|
|
385
421
|
* @returns True.
|
|
386
422
|
|
|
@@ -410,6 +446,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
410
446
|
|
|
411
447
|
|
|
412
448
|
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
413
456
|
|
|
414
457
|
|
|
415
458
|
|
|
@@ -467,6 +510,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
467
510
|
|
|
468
511
|
|
|
469
512
|
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
470
520
|
|
|
471
521
|
|
|
472
522
|
|
|
@@ -531,6 +581,40 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
531
581
|
|
|
532
582
|
|
|
533
583
|
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
* @example
|
|
591
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
592
|
+
* interface Task {
|
|
593
|
+
* id: number;
|
|
594
|
+
* priority: number;
|
|
595
|
+
* name: string;
|
|
596
|
+
* }
|
|
597
|
+
*
|
|
598
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
599
|
+
* const tasks: Task[] = [
|
|
600
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
601
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
602
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
603
|
+
* ];
|
|
604
|
+
*
|
|
605
|
+
* const maxHeap = new Heap(tasks, {
|
|
606
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
607
|
+
* });
|
|
608
|
+
*
|
|
609
|
+
* console.log(maxHeap.size); // 3;
|
|
610
|
+
*
|
|
611
|
+
* // Peek returns highest priority task
|
|
612
|
+
* const topTask = maxHeap.peek();
|
|
613
|
+
* console.log(topTask?.priority); // 8;
|
|
614
|
+
* console.log(topTask?.name); // 'Alert';
|
|
615
|
+
*/
|
|
616
|
+
/**
|
|
617
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
534
618
|
|
|
535
619
|
|
|
536
620
|
|
|
@@ -561,6 +645,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
561
645
|
* console.log(topTask?.name); // 'Alert';
|
|
562
646
|
*/
|
|
563
647
|
poll() {
|
|
648
|
+
return this.pop();
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
652
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
653
|
+
* @returns The removed top element, or undefined if empty.
|
|
654
|
+
*/
|
|
655
|
+
pop() {
|
|
564
656
|
if (this.elements.length === 0) return;
|
|
565
657
|
const value = this.elements[0];
|
|
566
658
|
const last = this.elements.pop();
|
|
@@ -601,6 +693,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
601
693
|
|
|
602
694
|
|
|
603
695
|
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
604
703
|
|
|
605
704
|
|
|
606
705
|
|
|
@@ -701,6 +800,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
701
800
|
|
|
702
801
|
|
|
703
802
|
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
704
810
|
|
|
705
811
|
|
|
706
812
|
|
|
@@ -748,6 +854,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
748
854
|
|
|
749
855
|
|
|
750
856
|
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
751
864
|
|
|
752
865
|
|
|
753
866
|
|
|
@@ -765,16 +878,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
765
878
|
clear() {
|
|
766
879
|
this._elements = [];
|
|
767
880
|
}
|
|
768
|
-
/**
|
|
769
|
-
* Replace the backing array and rebuild the heap.
|
|
770
|
-
* @remarks Time O(N), Space O(N)
|
|
771
|
-
* @param elements - Iterable used to refill the heap.
|
|
772
|
-
* @returns Array of per-node results from fixing steps.
|
|
773
|
-
*/
|
|
774
|
-
refill(elements) {
|
|
775
|
-
this._elements = Array.from(elements);
|
|
776
|
-
return this.fix();
|
|
777
|
-
}
|
|
778
881
|
/**
|
|
779
882
|
* Check if an equal element exists in the heap.
|
|
780
883
|
* @remarks Time O(N), Space O(1)
|
|
@@ -798,6 +901,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
798
901
|
|
|
799
902
|
|
|
800
903
|
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
|
|
801
911
|
|
|
802
912
|
|
|
803
913
|
|
|
@@ -845,6 +955,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
845
955
|
|
|
846
956
|
|
|
847
957
|
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
848
965
|
|
|
849
966
|
|
|
850
967
|
|
|
@@ -869,7 +986,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
869
986
|
}
|
|
870
987
|
if (index < 0) return false;
|
|
871
988
|
if (index === 0) {
|
|
872
|
-
this.
|
|
989
|
+
this.pop();
|
|
873
990
|
} else if (index === this.elements.length - 1) {
|
|
874
991
|
this.elements.pop();
|
|
875
992
|
} else {
|
|
@@ -879,13 +996,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
879
996
|
}
|
|
880
997
|
return true;
|
|
881
998
|
}
|
|
999
|
+
/**
|
|
1000
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
1001
|
+
*/
|
|
1002
|
+
deleteBy(predicate) {
|
|
1003
|
+
return this.deleteWhere(predicate);
|
|
1004
|
+
}
|
|
882
1005
|
/**
|
|
883
1006
|
* Delete the first element that matches a predicate.
|
|
884
1007
|
* @remarks Time O(N), Space O(1)
|
|
885
1008
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
886
1009
|
* @returns True if an element was removed.
|
|
887
1010
|
*/
|
|
888
|
-
|
|
1011
|
+
deleteWhere(predicate) {
|
|
889
1012
|
let idx = -1;
|
|
890
1013
|
for (let i = 0; i < this.elements.length; i++) {
|
|
891
1014
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -895,7 +1018,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
895
1018
|
}
|
|
896
1019
|
if (idx < 0) return false;
|
|
897
1020
|
if (idx === 0) {
|
|
898
|
-
this.
|
|
1021
|
+
this.pop();
|
|
899
1022
|
} else if (idx === this.elements.length - 1) {
|
|
900
1023
|
this.elements.pop();
|
|
901
1024
|
} else {
|
|
@@ -938,6 +1061,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
938
1061
|
|
|
939
1062
|
|
|
940
1063
|
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
941
1071
|
|
|
942
1072
|
|
|
943
1073
|
|
|
@@ -1018,6 +1148,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1018
1148
|
|
|
1019
1149
|
|
|
1020
1150
|
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1021
1158
|
|
|
1022
1159
|
|
|
1023
1160
|
|
|
@@ -1071,6 +1208,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1071
1208
|
|
|
1072
1209
|
|
|
1073
1210
|
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1074
1218
|
|
|
1075
1219
|
|
|
1076
1220
|
|
|
@@ -1123,6 +1267,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1123
1267
|
|
|
1124
1268
|
|
|
1125
1269
|
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
|
|
1126
1277
|
|
|
1127
1278
|
|
|
1128
1279
|
|
|
@@ -1182,6 +1333,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1182
1333
|
|
|
1183
1334
|
|
|
1184
1335
|
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1185
1343
|
|
|
1186
1344
|
|
|
1187
1345
|
|
|
@@ -1367,7 +1525,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1367
1525
|
* Push an element into the root list.
|
|
1368
1526
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1369
1527
|
* @param element - Element to insert.
|
|
1370
|
-
* @returns
|
|
1528
|
+
* @returns True when the element is added.
|
|
1371
1529
|
*/
|
|
1372
1530
|
push(element) {
|
|
1373
1531
|
const node = this.createNode(element);
|
|
@@ -1376,7 +1534,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1376
1534
|
this.mergeWithRoot(node);
|
|
1377
1535
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1378
1536
|
this._size++;
|
|
1379
|
-
return
|
|
1537
|
+
return true;
|
|
1380
1538
|
}
|
|
1381
1539
|
peek() {
|
|
1382
1540
|
return this.min ? this.min.element : void 0;
|