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/cjs/heap.cjs
CHANGED
|
@@ -3,6 +3,37 @@
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
|
|
6
|
+
// src/common/error.ts
|
|
7
|
+
function raise(ErrorClass, message) {
|
|
8
|
+
throw new ErrorClass(message);
|
|
9
|
+
}
|
|
10
|
+
__name(raise, "raise");
|
|
11
|
+
var ERR = {
|
|
12
|
+
// Range / index
|
|
13
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
14
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
15
|
+
// Type / argument
|
|
16
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
17
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
18
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
19
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
20
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
21
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
22
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
23
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
24
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
25
|
+
// State / operation
|
|
26
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
27
|
+
// Matrix
|
|
28
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
29
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
30
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
31
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
32
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
33
|
+
// Order statistic
|
|
34
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
35
|
+
};
|
|
36
|
+
|
|
6
37
|
// src/data-structures/base/iterable-element-base.ts
|
|
7
38
|
var IterableElementBase = class {
|
|
8
39
|
static {
|
|
@@ -21,7 +52,7 @@ var IterableElementBase = class {
|
|
|
21
52
|
if (options) {
|
|
22
53
|
const { toElementFn } = options;
|
|
23
54
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
24
|
-
else if (toElementFn)
|
|
55
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
25
56
|
}
|
|
26
57
|
}
|
|
27
58
|
/**
|
|
@@ -184,7 +215,7 @@ var IterableElementBase = class {
|
|
|
184
215
|
acc = initialValue;
|
|
185
216
|
} else {
|
|
186
217
|
const first = iter.next();
|
|
187
|
-
if (first.done)
|
|
218
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
188
219
|
acc = first.value;
|
|
189
220
|
index = 1;
|
|
190
221
|
}
|
|
@@ -226,31 +257,6 @@ var IterableElementBase = class {
|
|
|
226
257
|
}
|
|
227
258
|
};
|
|
228
259
|
|
|
229
|
-
// src/common/error.ts
|
|
230
|
-
var ERR = {
|
|
231
|
-
// Range / index
|
|
232
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
233
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
234
|
-
// Type / argument
|
|
235
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
236
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
237
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
238
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
239
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
240
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
241
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
242
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
243
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
244
|
-
// State / operation
|
|
245
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
246
|
-
// Matrix
|
|
247
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
248
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
249
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
250
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
251
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
252
|
-
};
|
|
253
|
-
|
|
254
260
|
// src/data-structures/heap/heap.ts
|
|
255
261
|
var Heap = class _Heap extends IterableElementBase {
|
|
256
262
|
static {
|
|
@@ -307,6 +313,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
307
313
|
|
|
308
314
|
|
|
309
315
|
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
310
324
|
|
|
311
325
|
|
|
312
326
|
|
|
@@ -364,7 +378,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
364
378
|
}
|
|
365
379
|
/**
|
|
366
380
|
* Insert an element.
|
|
367
|
-
* @remarks Time O(
|
|
381
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
368
382
|
* @param element - Element to insert.
|
|
369
383
|
* @returns True.
|
|
370
384
|
|
|
@@ -389,6 +403,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
389
403
|
|
|
390
404
|
|
|
391
405
|
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
392
414
|
|
|
393
415
|
|
|
394
416
|
|
|
@@ -442,6 +464,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
442
464
|
|
|
443
465
|
|
|
444
466
|
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
445
475
|
|
|
446
476
|
|
|
447
477
|
|
|
@@ -498,6 +528,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
498
528
|
|
|
499
529
|
|
|
500
530
|
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
501
538
|
|
|
502
539
|
|
|
503
540
|
|
|
@@ -506,6 +543,34 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
506
543
|
|
|
507
544
|
|
|
508
545
|
|
|
546
|
+
* @example
|
|
547
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
548
|
+
* interface Task {
|
|
549
|
+
* id: number;
|
|
550
|
+
* priority: number;
|
|
551
|
+
* name: string;
|
|
552
|
+
* }
|
|
553
|
+
*
|
|
554
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
555
|
+
* const tasks: Task[] = [
|
|
556
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
557
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
558
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
559
|
+
* ];
|
|
560
|
+
*
|
|
561
|
+
* const maxHeap = new Heap(tasks, {
|
|
562
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
563
|
+
* });
|
|
564
|
+
*
|
|
565
|
+
* console.log(maxHeap.size); // 3;
|
|
566
|
+
*
|
|
567
|
+
* // Peek returns highest priority task
|
|
568
|
+
* const topTask = maxHeap.peek();
|
|
569
|
+
* console.log(topTask?.priority); // 8;
|
|
570
|
+
* console.log(topTask?.name); // 'Alert';
|
|
571
|
+
*/
|
|
572
|
+
/**
|
|
573
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
509
574
|
* @example
|
|
510
575
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
511
576
|
* interface Task {
|
|
@@ -533,6 +598,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
533
598
|
* console.log(topTask?.name); // 'Alert';
|
|
534
599
|
*/
|
|
535
600
|
poll() {
|
|
601
|
+
return this.pop();
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
605
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
606
|
+
* @returns The removed top element, or undefined if empty.
|
|
607
|
+
*/
|
|
608
|
+
pop() {
|
|
536
609
|
if (this.elements.length === 0) return;
|
|
537
610
|
const value = this.elements[0];
|
|
538
611
|
const last = this.elements.pop();
|
|
@@ -568,6 +641,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
568
641
|
|
|
569
642
|
|
|
570
643
|
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
571
652
|
|
|
572
653
|
|
|
573
654
|
|
|
@@ -664,6 +745,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
664
745
|
|
|
665
746
|
|
|
666
747
|
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
667
756
|
|
|
668
757
|
|
|
669
758
|
|
|
@@ -707,6 +796,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
707
796
|
|
|
708
797
|
|
|
709
798
|
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
710
807
|
|
|
711
808
|
|
|
712
809
|
|
|
@@ -725,16 +822,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
725
822
|
clear() {
|
|
726
823
|
this._elements = [];
|
|
727
824
|
}
|
|
728
|
-
/**
|
|
729
|
-
* Replace the backing array and rebuild the heap.
|
|
730
|
-
* @remarks Time O(N), Space O(N)
|
|
731
|
-
* @param elements - Iterable used to refill the heap.
|
|
732
|
-
* @returns Array of per-node results from fixing steps.
|
|
733
|
-
*/
|
|
734
|
-
refill(elements) {
|
|
735
|
-
this._elements = Array.from(elements);
|
|
736
|
-
return this.fix();
|
|
737
|
-
}
|
|
738
825
|
/**
|
|
739
826
|
* Check if an equal element exists in the heap.
|
|
740
827
|
* @remarks Time O(N), Space O(1)
|
|
@@ -753,6 +840,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
753
840
|
|
|
754
841
|
|
|
755
842
|
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
756
851
|
|
|
757
852
|
|
|
758
853
|
|
|
@@ -796,6 +891,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
796
891
|
|
|
797
892
|
|
|
798
893
|
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
799
902
|
|
|
800
903
|
|
|
801
904
|
|
|
@@ -821,7 +924,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
821
924
|
}
|
|
822
925
|
if (index < 0) return false;
|
|
823
926
|
if (index === 0) {
|
|
824
|
-
this.
|
|
927
|
+
this.pop();
|
|
825
928
|
} else if (index === this.elements.length - 1) {
|
|
826
929
|
this.elements.pop();
|
|
827
930
|
} else {
|
|
@@ -831,13 +934,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
831
934
|
}
|
|
832
935
|
return true;
|
|
833
936
|
}
|
|
937
|
+
/**
|
|
938
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
939
|
+
*/
|
|
940
|
+
deleteBy(predicate) {
|
|
941
|
+
return this.deleteWhere(predicate);
|
|
942
|
+
}
|
|
834
943
|
/**
|
|
835
944
|
* Delete the first element that matches a predicate.
|
|
836
945
|
* @remarks Time O(N), Space O(1)
|
|
837
946
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
838
947
|
* @returns True if an element was removed.
|
|
839
948
|
*/
|
|
840
|
-
|
|
949
|
+
deleteWhere(predicate) {
|
|
841
950
|
let idx = -1;
|
|
842
951
|
for (let i = 0; i < this.elements.length; i++) {
|
|
843
952
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -847,7 +956,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
847
956
|
}
|
|
848
957
|
if (idx < 0) return false;
|
|
849
958
|
if (idx === 0) {
|
|
850
|
-
this.
|
|
959
|
+
this.pop();
|
|
851
960
|
} else if (idx === this.elements.length - 1) {
|
|
852
961
|
this.elements.pop();
|
|
853
962
|
} else {
|
|
@@ -885,6 +994,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
885
994
|
|
|
886
995
|
|
|
887
996
|
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
888
1005
|
|
|
889
1006
|
|
|
890
1007
|
|
|
@@ -961,6 +1078,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
961
1078
|
|
|
962
1079
|
|
|
963
1080
|
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
964
1089
|
|
|
965
1090
|
|
|
966
1091
|
|
|
@@ -1010,6 +1135,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1010
1135
|
|
|
1011
1136
|
|
|
1012
1137
|
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1013
1146
|
|
|
1014
1147
|
|
|
1015
1148
|
|
|
@@ -1058,6 +1191,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1058
1191
|
|
|
1059
1192
|
|
|
1060
1193
|
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1061
1202
|
|
|
1062
1203
|
|
|
1063
1204
|
|
|
@@ -1113,6 +1254,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1113
1254
|
|
|
1114
1255
|
|
|
1115
1256
|
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1116
1265
|
|
|
1117
1266
|
|
|
1118
1267
|
|
|
@@ -1130,7 +1279,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1130
1279
|
*/
|
|
1131
1280
|
map(callback, options, thisArg) {
|
|
1132
1281
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
1133
|
-
if (!comparator)
|
|
1282
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1134
1283
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1135
1284
|
let i = 0;
|
|
1136
1285
|
for (const x of this) {
|
|
@@ -1157,7 +1306,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1157
1306
|
}
|
|
1158
1307
|
_DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
|
|
1159
1308
|
if (typeof a === "object" || typeof b === "object") {
|
|
1160
|
-
|
|
1309
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
1161
1310
|
}
|
|
1162
1311
|
if (a > b) return 1;
|
|
1163
1312
|
if (a < b) return -1;
|
|
@@ -1269,7 +1418,7 @@ var FibonacciHeap = class {
|
|
|
1269
1418
|
constructor(comparator) {
|
|
1270
1419
|
this.clear();
|
|
1271
1420
|
this._comparator = comparator || this._defaultComparator;
|
|
1272
|
-
if (typeof this.comparator !== "function")
|
|
1421
|
+
if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
1273
1422
|
}
|
|
1274
1423
|
_root;
|
|
1275
1424
|
/**
|
|
@@ -1310,7 +1459,7 @@ var FibonacciHeap = class {
|
|
|
1310
1459
|
* Push an element into the root list.
|
|
1311
1460
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1312
1461
|
* @param element - Element to insert.
|
|
1313
|
-
* @returns
|
|
1462
|
+
* @returns True when the element is added.
|
|
1314
1463
|
*/
|
|
1315
1464
|
push(element) {
|
|
1316
1465
|
const node = this.createNode(element);
|
|
@@ -1319,7 +1468,7 @@ var FibonacciHeap = class {
|
|
|
1319
1468
|
this.mergeWithRoot(node);
|
|
1320
1469
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1321
1470
|
this._size++;
|
|
1322
|
-
return
|
|
1471
|
+
return true;
|
|
1323
1472
|
}
|
|
1324
1473
|
peek() {
|
|
1325
1474
|
return this.min ? this.min.element : void 0;
|
|
@@ -1485,7 +1634,7 @@ var MaxHeap = class extends Heap {
|
|
|
1485
1634
|
super(elements, {
|
|
1486
1635
|
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
1487
1636
|
if (typeof a === "object" || typeof b === "object") {
|
|
1488
|
-
|
|
1637
|
+
raise(TypeError, ERR.comparatorRequired("MaxHeap"));
|
|
1489
1638
|
}
|
|
1490
1639
|
if (a < b) return 1;
|
|
1491
1640
|
if (a > b) return -1;
|