data-structure-typed 2.5.1 → 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 +5 -1
- package/MIGRATION.md +169 -0
- package/README.md +135 -23
- package/README_CN.md +551 -143
- package/SPECIFICATION.md +20 -14
- package/SPECIFICATION.zh-CN.md +20 -14
- package/dist/cjs/binary-tree.cjs +6460 -1591
- package/dist/cjs/graph.cjs +440 -20
- package/dist/cjs/hash.cjs +125 -22
- package/dist/cjs/heap.cjs +196 -47
- package/dist/cjs/index.cjs +8486 -2429
- package/dist/cjs/linked-list.cjs +456 -31
- package/dist/cjs/matrix.cjs +79 -9
- package/dist/cjs/priority-queue.cjs +193 -44
- package/dist/cjs/queue.cjs +391 -2
- package/dist/cjs/stack.cjs +92 -6
- package/dist/cjs/trie.cjs +122 -28
- package/dist/cjs-legacy/binary-tree.cjs +6484 -1612
- package/dist/cjs-legacy/graph.cjs +440 -20
- package/dist/cjs-legacy/hash.cjs +125 -22
- package/dist/cjs-legacy/heap.cjs +196 -47
- package/dist/cjs-legacy/index.cjs +8654 -2594
- package/dist/cjs-legacy/linked-list.cjs +456 -31
- package/dist/cjs-legacy/matrix.cjs +79 -9
- package/dist/cjs-legacy/priority-queue.cjs +193 -44
- package/dist/cjs-legacy/queue.cjs +391 -2
- package/dist/cjs-legacy/stack.cjs +92 -6
- package/dist/cjs-legacy/trie.cjs +122 -28
- package/dist/esm/binary-tree.mjs +6460 -1591
- package/dist/esm/graph.mjs +440 -20
- package/dist/esm/hash.mjs +125 -22
- package/dist/esm/heap.mjs +196 -47
- package/dist/esm/index.mjs +8486 -2430
- package/dist/esm/linked-list.mjs +456 -31
- package/dist/esm/matrix.mjs +79 -9
- package/dist/esm/priority-queue.mjs +193 -44
- package/dist/esm/queue.mjs +391 -2
- package/dist/esm/stack.mjs +92 -6
- package/dist/esm/trie.mjs +122 -28
- package/dist/esm-legacy/binary-tree.mjs +6484 -1612
- package/dist/esm-legacy/graph.mjs +440 -20
- package/dist/esm-legacy/hash.mjs +125 -22
- package/dist/esm-legacy/heap.mjs +196 -47
- package/dist/esm-legacy/index.mjs +8654 -2595
- package/dist/esm-legacy/linked-list.mjs +456 -31
- package/dist/esm-legacy/matrix.mjs +79 -9
- package/dist/esm-legacy/priority-queue.mjs +193 -44
- package/dist/esm-legacy/queue.mjs +391 -2
- package/dist/esm-legacy/stack.mjs +92 -6
- package/dist/esm-legacy/trie.mjs +122 -28
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +112 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +214 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +294 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +155 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +48 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1370 -323
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1329 -316
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +1116 -295
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1330 -326
- package/dist/types/data-structures/graph/directed-graph.d.ts +80 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +72 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +95 -6
- package/dist/types/data-structures/heap/heap.d.ts +154 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +143 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +144 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +64 -0
- package/dist/types/data-structures/queue/deque.d.ts +142 -0
- package/dist/types/data-structures/queue/queue.d.ts +109 -0
- package/dist/types/data-structures/stack/stack.d.ts +82 -2
- package/dist/types/data-structures/trie/trie.d.ts +96 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/umd/data-structure-typed.js +8623 -2564
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +696 -194
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +71 -71
- package/docs-site-docusaurus/docs/api/classes/BST.md +639 -189
- 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 +148 -160
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +105 -91
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +104 -74
- 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 +51 -51
- 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 +33 -33
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +50 -50
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +78 -78
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +82 -82
- 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 +112 -60
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +708 -206
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +79 -79
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +44 -44
- package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +236 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +162 -46
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +232 -32
- package/docs-site-docusaurus/docs/api/classes/Trie.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +81 -81
- package/docs-site-docusaurus/docs/guide/architecture.md +75 -5
- package/docs-site-docusaurus/docs/guide/concepts.md +53 -3
- package/docs-site-docusaurus/docs/guide/faq.md +233 -0
- package/docs-site-docusaurus/docs/guide/guides.md +43 -58
- package/docs-site-docusaurus/docs/guide/installation.md +2 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +75 -176
- package/docs-site-docusaurus/docs/guide/overview.md +132 -11
- package/docs-site-docusaurus/docs/guide/performance.md +2 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +31 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +1 -1
- package/docs-site-docusaurus/src/pages/index.tsx +55 -2
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/typedoc.json +1 -0
- package/llms.txt +37 -0
- package/package.json +65 -56
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +99 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
- package/src/data-structures/binary-tree/binary-tree.ts +239 -78
- package/src/data-structures/binary-tree/bst.ts +542 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +1223 -261
- package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
- package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
- package/src/data-structures/binary-tree/tree-set.ts +1018 -99
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- 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 +102 -16
- package/src/data-structures/heap/heap.ts +153 -23
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
- package/src/data-structures/matrix/matrix.ts +65 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +130 -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 +86 -2
- package/src/interfaces/binary-tree.ts +1 -9
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- 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/heap.mjs
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
+
// src/common/error.ts
|
|
5
|
+
function raise(ErrorClass, message) {
|
|
6
|
+
throw new ErrorClass(message);
|
|
7
|
+
}
|
|
8
|
+
__name(raise, "raise");
|
|
9
|
+
var ERR = {
|
|
10
|
+
// Range / index
|
|
11
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
12
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
13
|
+
// Type / argument
|
|
14
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
15
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
16
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
17
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
18
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
19
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
20
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
21
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
22
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
23
|
+
// State / operation
|
|
24
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
25
|
+
// Matrix
|
|
26
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
27
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
28
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
29
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
30
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
31
|
+
// Order statistic
|
|
32
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
33
|
+
};
|
|
34
|
+
|
|
4
35
|
// src/data-structures/base/iterable-element-base.ts
|
|
5
36
|
var IterableElementBase = class {
|
|
6
37
|
static {
|
|
@@ -19,7 +50,7 @@ var IterableElementBase = class {
|
|
|
19
50
|
if (options) {
|
|
20
51
|
const { toElementFn } = options;
|
|
21
52
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
22
|
-
else if (toElementFn)
|
|
53
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
23
54
|
}
|
|
24
55
|
}
|
|
25
56
|
/**
|
|
@@ -182,7 +213,7 @@ var IterableElementBase = class {
|
|
|
182
213
|
acc = initialValue;
|
|
183
214
|
} else {
|
|
184
215
|
const first = iter.next();
|
|
185
|
-
if (first.done)
|
|
216
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
186
217
|
acc = first.value;
|
|
187
218
|
index = 1;
|
|
188
219
|
}
|
|
@@ -224,31 +255,6 @@ var IterableElementBase = class {
|
|
|
224
255
|
}
|
|
225
256
|
};
|
|
226
257
|
|
|
227
|
-
// src/common/error.ts
|
|
228
|
-
var ERR = {
|
|
229
|
-
// Range / index
|
|
230
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
231
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
232
|
-
// Type / argument
|
|
233
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
234
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
235
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
236
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
237
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
238
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
239
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
240
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
241
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
242
|
-
// State / operation
|
|
243
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
244
|
-
// Matrix
|
|
245
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
246
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
247
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
248
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
249
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
250
|
-
};
|
|
251
|
-
|
|
252
258
|
// src/data-structures/heap/heap.ts
|
|
253
259
|
var Heap = class _Heap extends IterableElementBase {
|
|
254
260
|
static {
|
|
@@ -305,6 +311,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
305
311
|
|
|
306
312
|
|
|
307
313
|
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
308
322
|
|
|
309
323
|
|
|
310
324
|
|
|
@@ -362,7 +376,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
362
376
|
}
|
|
363
377
|
/**
|
|
364
378
|
* Insert an element.
|
|
365
|
-
* @remarks Time O(
|
|
379
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
366
380
|
* @param element - Element to insert.
|
|
367
381
|
* @returns True.
|
|
368
382
|
|
|
@@ -387,6 +401,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
387
401
|
|
|
388
402
|
|
|
389
403
|
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
390
412
|
|
|
391
413
|
|
|
392
414
|
|
|
@@ -440,6 +462,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
440
462
|
|
|
441
463
|
|
|
442
464
|
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
443
473
|
|
|
444
474
|
|
|
445
475
|
|
|
@@ -496,6 +526,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
496
526
|
|
|
497
527
|
|
|
498
528
|
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
499
536
|
|
|
500
537
|
|
|
501
538
|
|
|
@@ -504,6 +541,34 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
504
541
|
|
|
505
542
|
|
|
506
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.
|
|
507
572
|
* @example
|
|
508
573
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
509
574
|
* interface Task {
|
|
@@ -531,6 +596,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
531
596
|
* console.log(topTask?.name); // 'Alert';
|
|
532
597
|
*/
|
|
533
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() {
|
|
534
607
|
if (this.elements.length === 0) return;
|
|
535
608
|
const value = this.elements[0];
|
|
536
609
|
const last = this.elements.pop();
|
|
@@ -566,6 +639,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
566
639
|
|
|
567
640
|
|
|
568
641
|
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
569
650
|
|
|
570
651
|
|
|
571
652
|
|
|
@@ -662,6 +743,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
662
743
|
|
|
663
744
|
|
|
664
745
|
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
665
754
|
|
|
666
755
|
|
|
667
756
|
|
|
@@ -705,6 +794,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
705
794
|
|
|
706
795
|
|
|
707
796
|
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
708
805
|
|
|
709
806
|
|
|
710
807
|
|
|
@@ -723,16 +820,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
723
820
|
clear() {
|
|
724
821
|
this._elements = [];
|
|
725
822
|
}
|
|
726
|
-
/**
|
|
727
|
-
* Replace the backing array and rebuild the heap.
|
|
728
|
-
* @remarks Time O(N), Space O(N)
|
|
729
|
-
* @param elements - Iterable used to refill the heap.
|
|
730
|
-
* @returns Array of per-node results from fixing steps.
|
|
731
|
-
*/
|
|
732
|
-
refill(elements) {
|
|
733
|
-
this._elements = Array.from(elements);
|
|
734
|
-
return this.fix();
|
|
735
|
-
}
|
|
736
823
|
/**
|
|
737
824
|
* Check if an equal element exists in the heap.
|
|
738
825
|
* @remarks Time O(N), Space O(1)
|
|
@@ -751,6 +838,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
751
838
|
|
|
752
839
|
|
|
753
840
|
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
754
849
|
|
|
755
850
|
|
|
756
851
|
|
|
@@ -794,6 +889,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
794
889
|
|
|
795
890
|
|
|
796
891
|
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
797
900
|
|
|
798
901
|
|
|
799
902
|
|
|
@@ -819,7 +922,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
819
922
|
}
|
|
820
923
|
if (index < 0) return false;
|
|
821
924
|
if (index === 0) {
|
|
822
|
-
this.
|
|
925
|
+
this.pop();
|
|
823
926
|
} else if (index === this.elements.length - 1) {
|
|
824
927
|
this.elements.pop();
|
|
825
928
|
} else {
|
|
@@ -829,13 +932,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
829
932
|
}
|
|
830
933
|
return true;
|
|
831
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
|
+
}
|
|
832
941
|
/**
|
|
833
942
|
* Delete the first element that matches a predicate.
|
|
834
943
|
* @remarks Time O(N), Space O(1)
|
|
835
944
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
836
945
|
* @returns True if an element was removed.
|
|
837
946
|
*/
|
|
838
|
-
|
|
947
|
+
deleteWhere(predicate) {
|
|
839
948
|
let idx = -1;
|
|
840
949
|
for (let i = 0; i < this.elements.length; i++) {
|
|
841
950
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -845,7 +954,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
845
954
|
}
|
|
846
955
|
if (idx < 0) return false;
|
|
847
956
|
if (idx === 0) {
|
|
848
|
-
this.
|
|
957
|
+
this.pop();
|
|
849
958
|
} else if (idx === this.elements.length - 1) {
|
|
850
959
|
this.elements.pop();
|
|
851
960
|
} else {
|
|
@@ -883,6 +992,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
883
992
|
|
|
884
993
|
|
|
885
994
|
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
886
1003
|
|
|
887
1004
|
|
|
888
1005
|
|
|
@@ -959,6 +1076,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
959
1076
|
|
|
960
1077
|
|
|
961
1078
|
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
962
1087
|
|
|
963
1088
|
|
|
964
1089
|
|
|
@@ -1008,6 +1133,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1008
1133
|
|
|
1009
1134
|
|
|
1010
1135
|
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1011
1144
|
|
|
1012
1145
|
|
|
1013
1146
|
|
|
@@ -1056,6 +1189,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1056
1189
|
|
|
1057
1190
|
|
|
1058
1191
|
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1059
1200
|
|
|
1060
1201
|
|
|
1061
1202
|
|
|
@@ -1111,6 +1252,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1111
1252
|
|
|
1112
1253
|
|
|
1113
1254
|
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1114
1263
|
|
|
1115
1264
|
|
|
1116
1265
|
|
|
@@ -1128,7 +1277,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1128
1277
|
*/
|
|
1129
1278
|
map(callback, options, thisArg) {
|
|
1130
1279
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
1131
|
-
if (!comparator)
|
|
1280
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1132
1281
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1133
1282
|
let i = 0;
|
|
1134
1283
|
for (const x of this) {
|
|
@@ -1155,7 +1304,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1155
1304
|
}
|
|
1156
1305
|
_DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
|
|
1157
1306
|
if (typeof a === "object" || typeof b === "object") {
|
|
1158
|
-
|
|
1307
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
1159
1308
|
}
|
|
1160
1309
|
if (a > b) return 1;
|
|
1161
1310
|
if (a < b) return -1;
|
|
@@ -1267,7 +1416,7 @@ var FibonacciHeap = class {
|
|
|
1267
1416
|
constructor(comparator) {
|
|
1268
1417
|
this.clear();
|
|
1269
1418
|
this._comparator = comparator || this._defaultComparator;
|
|
1270
|
-
if (typeof this.comparator !== "function")
|
|
1419
|
+
if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
1271
1420
|
}
|
|
1272
1421
|
_root;
|
|
1273
1422
|
/**
|
|
@@ -1308,7 +1457,7 @@ var FibonacciHeap = class {
|
|
|
1308
1457
|
* Push an element into the root list.
|
|
1309
1458
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1310
1459
|
* @param element - Element to insert.
|
|
1311
|
-
* @returns
|
|
1460
|
+
* @returns True when the element is added.
|
|
1312
1461
|
*/
|
|
1313
1462
|
push(element) {
|
|
1314
1463
|
const node = this.createNode(element);
|
|
@@ -1317,7 +1466,7 @@ var FibonacciHeap = class {
|
|
|
1317
1466
|
this.mergeWithRoot(node);
|
|
1318
1467
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1319
1468
|
this._size++;
|
|
1320
|
-
return
|
|
1469
|
+
return true;
|
|
1321
1470
|
}
|
|
1322
1471
|
peek() {
|
|
1323
1472
|
return this.min ? this.min.element : void 0;
|
|
@@ -1483,7 +1632,7 @@ var MaxHeap = class extends Heap {
|
|
|
1483
1632
|
super(elements, {
|
|
1484
1633
|
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
1485
1634
|
if (typeof a === "object" || typeof b === "object") {
|
|
1486
|
-
|
|
1635
|
+
raise(TypeError, ERR.comparatorRequired("MaxHeap"));
|
|
1487
1636
|
}
|
|
1488
1637
|
if (a < b) return 1;
|
|
1489
1638
|
if (a > b) return -1;
|