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-legacy/heap.mjs
CHANGED
|
@@ -3,6 +3,37 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
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 _IterableElementBase {
|
|
8
39
|
/**
|
|
@@ -25,7 +56,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
25
56
|
if (options) {
|
|
26
57
|
const { toElementFn } = options;
|
|
27
58
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
28
|
-
else if (toElementFn)
|
|
59
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
29
60
|
}
|
|
30
61
|
}
|
|
31
62
|
/**
|
|
@@ -181,7 +212,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
181
212
|
acc = initialValue;
|
|
182
213
|
} else {
|
|
183
214
|
const first = iter.next();
|
|
184
|
-
if (first.done)
|
|
215
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
185
216
|
acc = first.value;
|
|
186
217
|
index = 1;
|
|
187
218
|
}
|
|
@@ -225,31 +256,6 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
225
256
|
__name(_IterableElementBase, "IterableElementBase");
|
|
226
257
|
var IterableElementBase = _IterableElementBase;
|
|
227
258
|
|
|
228
|
-
// src/common/error.ts
|
|
229
|
-
var ERR = {
|
|
230
|
-
// Range / index
|
|
231
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
232
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
233
|
-
// Type / argument
|
|
234
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
235
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
236
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
237
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
238
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
239
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
240
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
241
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
242
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
243
|
-
// State / operation
|
|
244
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
245
|
-
// Matrix
|
|
246
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
247
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
248
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
249
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
250
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
251
|
-
};
|
|
252
|
-
|
|
253
259
|
// src/data-structures/heap/heap.ts
|
|
254
260
|
var _Heap = class _Heap extends IterableElementBase {
|
|
255
261
|
/**
|
|
@@ -265,7 +271,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
265
271
|
__publicField(this, "_elements", []);
|
|
266
272
|
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
267
273
|
if (typeof a === "object" || typeof b === "object") {
|
|
268
|
-
|
|
274
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
269
275
|
}
|
|
270
276
|
if (a > b) return 1;
|
|
271
277
|
if (a < b) return -1;
|
|
@@ -312,6 +318,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
312
318
|
|
|
313
319
|
|
|
314
320
|
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
315
329
|
|
|
316
330
|
|
|
317
331
|
|
|
@@ -370,7 +384,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
370
384
|
}
|
|
371
385
|
/**
|
|
372
386
|
* Insert an element.
|
|
373
|
-
* @remarks Time O(
|
|
387
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
374
388
|
* @param element - Element to insert.
|
|
375
389
|
* @returns True.
|
|
376
390
|
|
|
@@ -395,6 +409,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
395
409
|
|
|
396
410
|
|
|
397
411
|
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
398
420
|
|
|
399
421
|
|
|
400
422
|
|
|
@@ -448,6 +470,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
448
470
|
|
|
449
471
|
|
|
450
472
|
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
451
481
|
|
|
452
482
|
|
|
453
483
|
|
|
@@ -504,6 +534,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
504
534
|
|
|
505
535
|
|
|
506
536
|
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
507
544
|
|
|
508
545
|
|
|
509
546
|
|
|
@@ -512,6 +549,34 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
512
549
|
|
|
513
550
|
|
|
514
551
|
|
|
552
|
+
* @example
|
|
553
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
554
|
+
* interface Task {
|
|
555
|
+
* id: number;
|
|
556
|
+
* priority: number;
|
|
557
|
+
* name: string;
|
|
558
|
+
* }
|
|
559
|
+
*
|
|
560
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
561
|
+
* const tasks: Task[] = [
|
|
562
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
563
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
564
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
565
|
+
* ];
|
|
566
|
+
*
|
|
567
|
+
* const maxHeap = new Heap(tasks, {
|
|
568
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
569
|
+
* });
|
|
570
|
+
*
|
|
571
|
+
* console.log(maxHeap.size); // 3;
|
|
572
|
+
*
|
|
573
|
+
* // Peek returns highest priority task
|
|
574
|
+
* const topTask = maxHeap.peek();
|
|
575
|
+
* console.log(topTask?.priority); // 8;
|
|
576
|
+
* console.log(topTask?.name); // 'Alert';
|
|
577
|
+
*/
|
|
578
|
+
/**
|
|
579
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
515
580
|
* @example
|
|
516
581
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
517
582
|
* interface Task {
|
|
@@ -539,6 +604,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
539
604
|
* console.log(topTask?.name); // 'Alert';
|
|
540
605
|
*/
|
|
541
606
|
poll() {
|
|
607
|
+
return this.pop();
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
611
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
612
|
+
* @returns The removed top element, or undefined if empty.
|
|
613
|
+
*/
|
|
614
|
+
pop() {
|
|
542
615
|
if (this.elements.length === 0) return;
|
|
543
616
|
const value = this.elements[0];
|
|
544
617
|
const last = this.elements.pop();
|
|
@@ -574,6 +647,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
574
647
|
|
|
575
648
|
|
|
576
649
|
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
577
658
|
|
|
578
659
|
|
|
579
660
|
|
|
@@ -670,6 +751,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
670
751
|
|
|
671
752
|
|
|
672
753
|
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
673
762
|
|
|
674
763
|
|
|
675
764
|
|
|
@@ -713,6 +802,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
713
802
|
|
|
714
803
|
|
|
715
804
|
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
716
813
|
|
|
717
814
|
|
|
718
815
|
|
|
@@ -731,16 +828,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
731
828
|
clear() {
|
|
732
829
|
this._elements = [];
|
|
733
830
|
}
|
|
734
|
-
/**
|
|
735
|
-
* Replace the backing array and rebuild the heap.
|
|
736
|
-
* @remarks Time O(N), Space O(N)
|
|
737
|
-
* @param elements - Iterable used to refill the heap.
|
|
738
|
-
* @returns Array of per-node results from fixing steps.
|
|
739
|
-
*/
|
|
740
|
-
refill(elements) {
|
|
741
|
-
this._elements = Array.from(elements);
|
|
742
|
-
return this.fix();
|
|
743
|
-
}
|
|
744
831
|
/**
|
|
745
832
|
* Check if an equal element exists in the heap.
|
|
746
833
|
* @remarks Time O(N), Space O(1)
|
|
@@ -759,6 +846,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
759
846
|
|
|
760
847
|
|
|
761
848
|
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
762
857
|
|
|
763
858
|
|
|
764
859
|
|
|
@@ -802,6 +897,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
802
897
|
|
|
803
898
|
|
|
804
899
|
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
805
908
|
|
|
806
909
|
|
|
807
910
|
|
|
@@ -827,7 +930,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
827
930
|
}
|
|
828
931
|
if (index < 0) return false;
|
|
829
932
|
if (index === 0) {
|
|
830
|
-
this.
|
|
933
|
+
this.pop();
|
|
831
934
|
} else if (index === this.elements.length - 1) {
|
|
832
935
|
this.elements.pop();
|
|
833
936
|
} else {
|
|
@@ -837,13 +940,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
837
940
|
}
|
|
838
941
|
return true;
|
|
839
942
|
}
|
|
943
|
+
/**
|
|
944
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
945
|
+
*/
|
|
946
|
+
deleteBy(predicate) {
|
|
947
|
+
return this.deleteWhere(predicate);
|
|
948
|
+
}
|
|
840
949
|
/**
|
|
841
950
|
* Delete the first element that matches a predicate.
|
|
842
951
|
* @remarks Time O(N), Space O(1)
|
|
843
952
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
844
953
|
* @returns True if an element was removed.
|
|
845
954
|
*/
|
|
846
|
-
|
|
955
|
+
deleteWhere(predicate) {
|
|
847
956
|
let idx = -1;
|
|
848
957
|
for (let i = 0; i < this.elements.length; i++) {
|
|
849
958
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -853,7 +962,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
853
962
|
}
|
|
854
963
|
if (idx < 0) return false;
|
|
855
964
|
if (idx === 0) {
|
|
856
|
-
this.
|
|
965
|
+
this.pop();
|
|
857
966
|
} else if (idx === this.elements.length - 1) {
|
|
858
967
|
this.elements.pop();
|
|
859
968
|
} else {
|
|
@@ -891,6 +1000,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
891
1000
|
|
|
892
1001
|
|
|
893
1002
|
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
894
1011
|
|
|
895
1012
|
|
|
896
1013
|
|
|
@@ -967,6 +1084,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
967
1084
|
|
|
968
1085
|
|
|
969
1086
|
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
|
|
970
1095
|
|
|
971
1096
|
|
|
972
1097
|
|
|
@@ -1016,6 +1141,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1016
1141
|
|
|
1017
1142
|
|
|
1018
1143
|
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1019
1152
|
|
|
1020
1153
|
|
|
1021
1154
|
|
|
@@ -1064,6 +1197,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1064
1197
|
|
|
1065
1198
|
|
|
1066
1199
|
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1067
1208
|
|
|
1068
1209
|
|
|
1069
1210
|
|
|
@@ -1119,6 +1260,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1119
1260
|
|
|
1120
1261
|
|
|
1121
1262
|
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1122
1271
|
|
|
1123
1272
|
|
|
1124
1273
|
|
|
@@ -1136,7 +1285,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1136
1285
|
*/
|
|
1137
1286
|
map(callback, options, thisArg) {
|
|
1138
1287
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
1139
|
-
if (!comparator)
|
|
1288
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1140
1289
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1141
1290
|
let i = 0;
|
|
1142
1291
|
for (const x of this) {
|
|
@@ -1268,7 +1417,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1268
1417
|
__publicField(this, "_comparator");
|
|
1269
1418
|
this.clear();
|
|
1270
1419
|
this._comparator = comparator || this._defaultComparator;
|
|
1271
|
-
if (typeof this.comparator !== "function")
|
|
1420
|
+
if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
1272
1421
|
}
|
|
1273
1422
|
/**
|
|
1274
1423
|
* Get the circular root list head.
|
|
@@ -1305,7 +1454,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1305
1454
|
* Push an element into the root list.
|
|
1306
1455
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1307
1456
|
* @param element - Element to insert.
|
|
1308
|
-
* @returns
|
|
1457
|
+
* @returns True when the element is added.
|
|
1309
1458
|
*/
|
|
1310
1459
|
push(element) {
|
|
1311
1460
|
const node = this.createNode(element);
|
|
@@ -1314,7 +1463,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1314
1463
|
this.mergeWithRoot(node);
|
|
1315
1464
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1316
1465
|
this._size++;
|
|
1317
|
-
return
|
|
1466
|
+
return true;
|
|
1318
1467
|
}
|
|
1319
1468
|
peek() {
|
|
1320
1469
|
return this.min ? this.min.element : void 0;
|
|
@@ -1479,7 +1628,7 @@ var _MaxHeap = class _MaxHeap extends Heap {
|
|
|
1479
1628
|
super(elements, {
|
|
1480
1629
|
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
1481
1630
|
if (typeof a === "object" || typeof b === "object") {
|
|
1482
|
-
|
|
1631
|
+
raise(TypeError, ERR.comparatorRequired("MaxHeap"));
|
|
1483
1632
|
}
|
|
1484
1633
|
if (a < b) return 1;
|
|
1485
1634
|
if (a > b) return -1;
|