data-structure-typed 2.5.0 → 2.5.2
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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +5 -1
- package/README.md +124 -29
- package/dist/cjs/binary-tree.cjs +26282 -0
- package/dist/cjs/graph.cjs +5422 -0
- package/dist/cjs/hash.cjs +1310 -0
- package/dist/cjs/heap.cjs +1602 -0
- package/dist/cjs/index.cjs +31257 -14673
- package/dist/cjs/linked-list.cjs +4576 -0
- package/dist/cjs/matrix.cjs +1080 -0
- package/dist/cjs/priority-queue.cjs +1376 -0
- package/dist/cjs/queue.cjs +4264 -0
- package/dist/cjs/stack.cjs +907 -0
- package/dist/cjs/trie.cjs +1223 -0
- package/dist/cjs-legacy/binary-tree.cjs +26319 -0
- package/dist/cjs-legacy/graph.cjs +5420 -0
- package/dist/cjs-legacy/hash.cjs +1310 -0
- package/dist/cjs-legacy/heap.cjs +1599 -0
- package/dist/cjs-legacy/index.cjs +31268 -14679
- package/dist/cjs-legacy/linked-list.cjs +4582 -0
- package/dist/cjs-legacy/matrix.cjs +1083 -0
- package/dist/cjs-legacy/priority-queue.cjs +1374 -0
- package/dist/cjs-legacy/queue.cjs +4262 -0
- package/dist/cjs-legacy/stack.cjs +907 -0
- package/dist/cjs-legacy/trie.cjs +1222 -0
- package/dist/esm/binary-tree.mjs +26267 -0
- package/dist/esm/graph.mjs +5409 -0
- package/dist/esm/hash.mjs +1307 -0
- package/dist/esm/heap.mjs +1596 -0
- package/dist/esm/index.mjs +31254 -14674
- package/dist/esm/linked-list.mjs +4569 -0
- package/dist/esm/matrix.mjs +1076 -0
- package/dist/esm/priority-queue.mjs +1372 -0
- package/dist/esm/queue.mjs +4260 -0
- package/dist/esm/stack.mjs +905 -0
- package/dist/esm/trie.mjs +1220 -0
- package/dist/esm-legacy/binary-tree.mjs +26304 -0
- package/dist/esm-legacy/graph.mjs +5407 -0
- package/dist/esm-legacy/hash.mjs +1307 -0
- package/dist/esm-legacy/heap.mjs +1593 -0
- package/dist/esm-legacy/index.mjs +31265 -14680
- package/dist/esm-legacy/linked-list.mjs +4575 -0
- package/dist/esm-legacy/matrix.mjs +1079 -0
- package/dist/esm-legacy/priority-queue.mjs +1370 -0
- package/dist/esm-legacy/queue.mjs +4258 -0
- package/dist/esm-legacy/stack.mjs +905 -0
- package/dist/esm-legacy/trie.mjs +1219 -0
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- 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/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +31196 -14608
- package/dist/umd/data-structure-typed.min.js +11 -5
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +597 -0
- package/docs-site-docusaurus/docs/guide/installation.md +62 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
- package/docs-site-docusaurus/docs/guide/overview.md +645 -0
- package/docs-site-docusaurus/docs/guide/performance.md +835 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +104 -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 +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +120 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/llms.txt +37 -0
- package/package.json +159 -55
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +287 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
- package/src/data-structures/binary-tree/binary-tree.ts +581 -6
- package/src/data-structures/binary-tree/bst.ts +922 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
- package/src/data-structures/binary-tree/segment-tree.ts +139 -2
- package/src/data-structures/binary-tree/tree-map.ts +3300 -495
- package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
- package/src/data-structures/binary-tree/tree-set.ts +3122 -440
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +230 -0
- package/src/data-structures/graph/undirected-graph.ts +207 -0
- package/src/data-structures/hash/hash-map.ts +270 -19
- package/src/data-structures/heap/heap.ts +326 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
- package/src/data-structures/matrix/matrix.ts +194 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +350 -5
- package/src/data-structures/queue/queue.ts +276 -0
- package/src/data-structures/stack/stack.ts +230 -0
- package/src/data-structures/trie/trie.ts +283 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- 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/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -0,0 +1,1593 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
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
|
+
|
|
37
|
+
// src/data-structures/base/iterable-element-base.ts
|
|
38
|
+
var _IterableElementBase = class _IterableElementBase {
|
|
39
|
+
/**
|
|
40
|
+
* Create a new iterable base.
|
|
41
|
+
*
|
|
42
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
43
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* Time O(1), Space O(1).
|
|
47
|
+
*/
|
|
48
|
+
constructor(options) {
|
|
49
|
+
/**
|
|
50
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* Time O(1), Space O(1).
|
|
54
|
+
*/
|
|
55
|
+
__publicField(this, "_toElementFn");
|
|
56
|
+
if (options) {
|
|
57
|
+
const { toElementFn } = options;
|
|
58
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
59
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Exposes the current `toElementFn`, if configured.
|
|
64
|
+
*
|
|
65
|
+
* @returns The converter function or `undefined` when not set.
|
|
66
|
+
* @remarks
|
|
67
|
+
* Time O(1), Space O(1).
|
|
68
|
+
*/
|
|
69
|
+
get toElementFn() {
|
|
70
|
+
return this._toElementFn;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns an iterator over the structure's elements.
|
|
74
|
+
*
|
|
75
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
76
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
80
|
+
*/
|
|
81
|
+
*[Symbol.iterator](...args) {
|
|
82
|
+
yield* this._getIterator(...args);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
86
|
+
*
|
|
87
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
88
|
+
* @remarks
|
|
89
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
90
|
+
*/
|
|
91
|
+
*values() {
|
|
92
|
+
for (const item of this) yield item;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Tests whether all elements satisfy the predicate.
|
|
96
|
+
*
|
|
97
|
+
* @template TReturn
|
|
98
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
99
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
100
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
101
|
+
*
|
|
102
|
+
* @remarks
|
|
103
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
104
|
+
*/
|
|
105
|
+
every(predicate, thisArg) {
|
|
106
|
+
let index = 0;
|
|
107
|
+
for (const item of this) {
|
|
108
|
+
if (thisArg === void 0) {
|
|
109
|
+
if (!predicate(item, index++, this)) return false;
|
|
110
|
+
} else {
|
|
111
|
+
const fn = predicate;
|
|
112
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Tests whether at least one element satisfies the predicate.
|
|
119
|
+
*
|
|
120
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
121
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
122
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
126
|
+
*/
|
|
127
|
+
some(predicate, thisArg) {
|
|
128
|
+
let index = 0;
|
|
129
|
+
for (const item of this) {
|
|
130
|
+
if (thisArg === void 0) {
|
|
131
|
+
if (predicate(item, index++, this)) return true;
|
|
132
|
+
} else {
|
|
133
|
+
const fn = predicate;
|
|
134
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Invokes a callback for each element in iteration order.
|
|
141
|
+
*
|
|
142
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
143
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
144
|
+
* @returns `void`.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
* Time O(n), Space O(1).
|
|
148
|
+
*/
|
|
149
|
+
forEach(callbackfn, thisArg) {
|
|
150
|
+
let index = 0;
|
|
151
|
+
for (const item of this) {
|
|
152
|
+
if (thisArg === void 0) {
|
|
153
|
+
callbackfn(item, index++, this);
|
|
154
|
+
} else {
|
|
155
|
+
const fn = callbackfn;
|
|
156
|
+
fn.call(thisArg, item, index++, this);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Implementation signature
|
|
161
|
+
find(predicate, thisArg) {
|
|
162
|
+
let index = 0;
|
|
163
|
+
for (const item of this) {
|
|
164
|
+
if (thisArg === void 0) {
|
|
165
|
+
if (predicate(item, index++, this)) return item;
|
|
166
|
+
} else {
|
|
167
|
+
const fn = predicate;
|
|
168
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
175
|
+
*
|
|
176
|
+
* @param element The element to test with `===` equality.
|
|
177
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
178
|
+
*
|
|
179
|
+
* @remarks
|
|
180
|
+
* Time O(n) in the worst case. Space O(1).
|
|
181
|
+
*/
|
|
182
|
+
has(element) {
|
|
183
|
+
for (const ele of this) if (ele === element) return true;
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Reduces all elements to a single accumulated value.
|
|
188
|
+
*
|
|
189
|
+
* @overload
|
|
190
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
191
|
+
* @returns The final accumulated value typed as `E`.
|
|
192
|
+
*
|
|
193
|
+
* @overload
|
|
194
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
195
|
+
* @param initialValue The initial accumulator value of type `E`.
|
|
196
|
+
* @returns The final accumulated value typed as `E`.
|
|
197
|
+
*
|
|
198
|
+
* @overload
|
|
199
|
+
* @template U The accumulator type when it differs from `E`.
|
|
200
|
+
* @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
|
|
201
|
+
* @param initialValue The initial accumulator value of type `U`.
|
|
202
|
+
* @returns The final accumulated value typed as `U`.
|
|
203
|
+
*
|
|
204
|
+
* @remarks
|
|
205
|
+
* Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
206
|
+
*/
|
|
207
|
+
reduce(callbackfn, initialValue) {
|
|
208
|
+
let index = 0;
|
|
209
|
+
const iter = this[Symbol.iterator]();
|
|
210
|
+
let acc;
|
|
211
|
+
if (arguments.length >= 2) {
|
|
212
|
+
acc = initialValue;
|
|
213
|
+
} else {
|
|
214
|
+
const first = iter.next();
|
|
215
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
216
|
+
acc = first.value;
|
|
217
|
+
index = 1;
|
|
218
|
+
}
|
|
219
|
+
for (const value of iter) {
|
|
220
|
+
acc = callbackfn(acc, value, index++, this);
|
|
221
|
+
}
|
|
222
|
+
return acc;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Materializes the elements into a new array.
|
|
226
|
+
*
|
|
227
|
+
* @returns A shallow array copy of the iteration order.
|
|
228
|
+
* @remarks
|
|
229
|
+
* Time O(n), Space O(n).
|
|
230
|
+
*/
|
|
231
|
+
toArray() {
|
|
232
|
+
return [...this];
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
236
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
237
|
+
*
|
|
238
|
+
* @returns A visual representation (array by default).
|
|
239
|
+
* @remarks
|
|
240
|
+
* Time O(n), Space O(n).
|
|
241
|
+
*/
|
|
242
|
+
toVisual() {
|
|
243
|
+
return [...this];
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
247
|
+
*
|
|
248
|
+
* @returns `void`.
|
|
249
|
+
* @remarks
|
|
250
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
251
|
+
*/
|
|
252
|
+
print() {
|
|
253
|
+
console.log(this.toVisual());
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
__name(_IterableElementBase, "IterableElementBase");
|
|
257
|
+
var IterableElementBase = _IterableElementBase;
|
|
258
|
+
|
|
259
|
+
// src/data-structures/heap/heap.ts
|
|
260
|
+
var _Heap = class _Heap extends IterableElementBase {
|
|
261
|
+
/**
|
|
262
|
+
* Create a Heap and optionally bulk-insert elements.
|
|
263
|
+
* @remarks Time O(N), Space O(N)
|
|
264
|
+
* @param [elements] - Iterable of elements (or raw values if toElementFn is set).
|
|
265
|
+
* @param [options] - Options such as comparator and toElementFn.
|
|
266
|
+
* @returns New Heap instance.
|
|
267
|
+
*/
|
|
268
|
+
constructor(elements = [], options) {
|
|
269
|
+
super(options);
|
|
270
|
+
__publicField(this, "_equals", Object.is);
|
|
271
|
+
__publicField(this, "_elements", []);
|
|
272
|
+
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
273
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
274
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
275
|
+
}
|
|
276
|
+
if (a > b) return 1;
|
|
277
|
+
if (a < b) return -1;
|
|
278
|
+
return 0;
|
|
279
|
+
}, "_DEFAULT_COMPARATOR"));
|
|
280
|
+
__publicField(this, "_comparator", this._DEFAULT_COMPARATOR);
|
|
281
|
+
if (options) {
|
|
282
|
+
const { comparator } = options;
|
|
283
|
+
if (comparator) this._comparator = comparator;
|
|
284
|
+
}
|
|
285
|
+
this.addMany(elements);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Get the backing array of the heap.
|
|
289
|
+
* @remarks Time O(1), Space O(1)
|
|
290
|
+
* @returns Internal elements array.
|
|
291
|
+
*/
|
|
292
|
+
get elements() {
|
|
293
|
+
return this._elements;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Get the number of elements.
|
|
297
|
+
* @remarks Time O(1), Space O(1)
|
|
298
|
+
* @returns Heap size.
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
* @example
|
|
335
|
+
* // Track heap capacity
|
|
336
|
+
* const heap = new Heap<number>();
|
|
337
|
+
* console.log(heap.size); // 0;
|
|
338
|
+
* heap.add(10);
|
|
339
|
+
* heap.add(20);
|
|
340
|
+
* console.log(heap.size); // 2;
|
|
341
|
+
* heap.poll();
|
|
342
|
+
* console.log(heap.size); // 1;
|
|
343
|
+
*/
|
|
344
|
+
get size() {
|
|
345
|
+
return this.elements.length;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Get the last leaf element.
|
|
349
|
+
* @remarks Time O(1), Space O(1)
|
|
350
|
+
* @returns Last element or undefined.
|
|
351
|
+
*/
|
|
352
|
+
get leaf() {
|
|
353
|
+
var _a;
|
|
354
|
+
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Create a heap of the same class from an iterable.
|
|
358
|
+
* @remarks Time O(N), Space O(N)
|
|
359
|
+
* @template T
|
|
360
|
+
* @template R
|
|
361
|
+
* @template S
|
|
362
|
+
* @param [elements] - Iterable of elements or raw records.
|
|
363
|
+
* @param [options] - Heap options including comparator.
|
|
364
|
+
* @returns A new heap instance of this class.
|
|
365
|
+
*/
|
|
366
|
+
static from(elements, options) {
|
|
367
|
+
return new this(elements, options);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Build a Heap from an iterable in linear time given a comparator.
|
|
371
|
+
* @remarks Time O(N), Space O(N)
|
|
372
|
+
* @template EE
|
|
373
|
+
* @template RR
|
|
374
|
+
* @param elements - Iterable of elements.
|
|
375
|
+
* @param options - Heap options including comparator.
|
|
376
|
+
* @returns A new Heap built from elements.
|
|
377
|
+
*/
|
|
378
|
+
static heapify(elements, options) {
|
|
379
|
+
return new _Heap(elements, options);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Insert an element.
|
|
383
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
384
|
+
* @param element - Element to insert.
|
|
385
|
+
* @returns True.
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
* @example
|
|
422
|
+
* // basic Heap creation and add operation
|
|
423
|
+
* // Create a min heap (default)
|
|
424
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
425
|
+
*
|
|
426
|
+
* // Verify size
|
|
427
|
+
* console.log(minHeap.size); // 6;
|
|
428
|
+
*
|
|
429
|
+
* // Add new element
|
|
430
|
+
* minHeap.add(4);
|
|
431
|
+
* console.log(minHeap.size); // 7;
|
|
432
|
+
*
|
|
433
|
+
* // Min heap property: smallest element at root
|
|
434
|
+
* const min = minHeap.peek();
|
|
435
|
+
* console.log(min); // 1;
|
|
436
|
+
*/
|
|
437
|
+
add(element) {
|
|
438
|
+
this._elements.push(element);
|
|
439
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Insert many elements from an iterable.
|
|
443
|
+
* @remarks Time O(N log N), Space O(1)
|
|
444
|
+
* @param elements - Iterable of elements or raw values.
|
|
445
|
+
* @returns Array of per-element success flags.
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
* @example
|
|
479
|
+
* // Add multiple elements
|
|
480
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
481
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
482
|
+
* console.log(heap.peek()); // 1;
|
|
483
|
+
* console.log(heap.size); // 4;
|
|
484
|
+
*/
|
|
485
|
+
addMany(elements) {
|
|
486
|
+
const flags = [];
|
|
487
|
+
for (const el of elements) {
|
|
488
|
+
if (this.toElementFn) {
|
|
489
|
+
const ok = this.add(this.toElementFn(el));
|
|
490
|
+
flags.push(ok);
|
|
491
|
+
} else {
|
|
492
|
+
const ok = this.add(el);
|
|
493
|
+
flags.push(ok);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return flags;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Remove and return the top element.
|
|
500
|
+
* @remarks Time O(log N), Space O(1)
|
|
501
|
+
* @returns Top element or undefined.
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
* @example
|
|
538
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
539
|
+
* interface Task {
|
|
540
|
+
* id: number;
|
|
541
|
+
* priority: number;
|
|
542
|
+
* name: string;
|
|
543
|
+
* }
|
|
544
|
+
*
|
|
545
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
546
|
+
* const tasks: Task[] = [
|
|
547
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
548
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
549
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
550
|
+
* ];
|
|
551
|
+
*
|
|
552
|
+
* const maxHeap = new Heap(tasks, {
|
|
553
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
554
|
+
* });
|
|
555
|
+
*
|
|
556
|
+
* console.log(maxHeap.size); // 3;
|
|
557
|
+
*
|
|
558
|
+
* // Peek returns highest priority task
|
|
559
|
+
* const topTask = maxHeap.peek();
|
|
560
|
+
* console.log(topTask?.priority); // 8;
|
|
561
|
+
* console.log(topTask?.name); // 'Alert';
|
|
562
|
+
*/
|
|
563
|
+
poll() {
|
|
564
|
+
if (this.elements.length === 0) return;
|
|
565
|
+
const value = this.elements[0];
|
|
566
|
+
const last = this.elements.pop();
|
|
567
|
+
if (this.elements.length) {
|
|
568
|
+
this.elements[0] = last;
|
|
569
|
+
this._sinkDown(0, this.elements.length >> 1);
|
|
570
|
+
}
|
|
571
|
+
return value;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Get the current top element without removing it.
|
|
575
|
+
* @remarks Time O(1), Space O(1)
|
|
576
|
+
* @returns Top element or undefined.
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
* @example
|
|
613
|
+
* // Heap for event processing with priority
|
|
614
|
+
* interface Event {
|
|
615
|
+
* id: number;
|
|
616
|
+
* type: 'critical' | 'warning' | 'info';
|
|
617
|
+
* timestamp: number;
|
|
618
|
+
* message: string;
|
|
619
|
+
* }
|
|
620
|
+
*
|
|
621
|
+
* // Custom priority: critical > warning > info
|
|
622
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
623
|
+
*
|
|
624
|
+
* const eventHeap = new Heap<Event>([], {
|
|
625
|
+
* comparator: (a: Event, b: Event) => {
|
|
626
|
+
* const priorityA = priorityMap[a.type];
|
|
627
|
+
* const priorityB = priorityMap[b.type];
|
|
628
|
+
* return priorityB - priorityA; // Higher priority first
|
|
629
|
+
* }
|
|
630
|
+
* });
|
|
631
|
+
*
|
|
632
|
+
* // Add events in random order
|
|
633
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
634
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
635
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
636
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
637
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
638
|
+
*
|
|
639
|
+
* console.log(eventHeap.size); // 5;
|
|
640
|
+
*
|
|
641
|
+
* // Process events by priority (critical first)
|
|
642
|
+
* const processedOrder: Event[] = [];
|
|
643
|
+
* while (eventHeap.size > 0) {
|
|
644
|
+
* const event = eventHeap.poll();
|
|
645
|
+
* if (event) {
|
|
646
|
+
* processedOrder.push(event);
|
|
647
|
+
* }
|
|
648
|
+
* }
|
|
649
|
+
*
|
|
650
|
+
* // Verify critical events came first
|
|
651
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
652
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
653
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
654
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
655
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
656
|
+
*
|
|
657
|
+
* // Verify O(log n) operations
|
|
658
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
659
|
+
*
|
|
660
|
+
* // Add - O(log n)
|
|
661
|
+
* newHeap.add(2);
|
|
662
|
+
* console.log(newHeap.size); // 5;
|
|
663
|
+
*
|
|
664
|
+
* // Poll - O(log n)
|
|
665
|
+
* const removed = newHeap.poll();
|
|
666
|
+
* console.log(removed); // 1;
|
|
667
|
+
*
|
|
668
|
+
* // Peek - O(1)
|
|
669
|
+
* const top = newHeap.peek();
|
|
670
|
+
* console.log(top); // 2;
|
|
671
|
+
*/
|
|
672
|
+
peek() {
|
|
673
|
+
return this.elements[0];
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Check whether the heap is empty.
|
|
677
|
+
* @remarks Time O(1), Space O(1)
|
|
678
|
+
* @returns True if size is 0.
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
* @example
|
|
713
|
+
* // Check if heap is empty
|
|
714
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
715
|
+
* console.log(heap.isEmpty()); // true;
|
|
716
|
+
* heap.add(1);
|
|
717
|
+
* console.log(heap.isEmpty()); // false;
|
|
718
|
+
*/
|
|
719
|
+
isEmpty() {
|
|
720
|
+
return this.size === 0;
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Remove all elements.
|
|
724
|
+
* @remarks Time O(1), Space O(1)
|
|
725
|
+
* @returns void
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
* @example
|
|
760
|
+
* // Remove all elements
|
|
761
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
762
|
+
* heap.clear();
|
|
763
|
+
* console.log(heap.isEmpty()); // true;
|
|
764
|
+
*/
|
|
765
|
+
clear() {
|
|
766
|
+
this._elements = [];
|
|
767
|
+
}
|
|
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
|
+
/**
|
|
779
|
+
* Check if an equal element exists in the heap.
|
|
780
|
+
* @remarks Time O(N), Space O(1)
|
|
781
|
+
* @param element - Element to search for.
|
|
782
|
+
* @returns True if found.
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
* @example
|
|
810
|
+
* // Check element existence
|
|
811
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
812
|
+
* console.log(heap.has(1)); // true;
|
|
813
|
+
* console.log(heap.has(99)); // false;
|
|
814
|
+
*/
|
|
815
|
+
has(element) {
|
|
816
|
+
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
817
|
+
return false;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Delete one occurrence of an element.
|
|
821
|
+
* @remarks Time O(N), Space O(1)
|
|
822
|
+
* @param element - Element to delete.
|
|
823
|
+
* @returns True if an element was removed.
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
* @example
|
|
857
|
+
* // Remove specific element
|
|
858
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
859
|
+
* heap.delete(4);
|
|
860
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
861
|
+
*/
|
|
862
|
+
delete(element) {
|
|
863
|
+
let index = -1;
|
|
864
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
865
|
+
if (this._equals(this.elements[i], element)) {
|
|
866
|
+
index = i;
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
if (index < 0) return false;
|
|
871
|
+
if (index === 0) {
|
|
872
|
+
this.poll();
|
|
873
|
+
} else if (index === this.elements.length - 1) {
|
|
874
|
+
this.elements.pop();
|
|
875
|
+
} else {
|
|
876
|
+
this.elements.splice(index, 1, this.elements.pop());
|
|
877
|
+
this._bubbleUp(index);
|
|
878
|
+
this._sinkDown(index, this.elements.length >> 1);
|
|
879
|
+
}
|
|
880
|
+
return true;
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Delete the first element that matches a predicate.
|
|
884
|
+
* @remarks Time O(N), Space O(1)
|
|
885
|
+
* @param predicate - Function (element, index, heap) → boolean.
|
|
886
|
+
* @returns True if an element was removed.
|
|
887
|
+
*/
|
|
888
|
+
deleteBy(predicate) {
|
|
889
|
+
let idx = -1;
|
|
890
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
891
|
+
if (predicate(this.elements[i], i, this)) {
|
|
892
|
+
idx = i;
|
|
893
|
+
break;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (idx < 0) return false;
|
|
897
|
+
if (idx === 0) {
|
|
898
|
+
this.poll();
|
|
899
|
+
} else if (idx === this.elements.length - 1) {
|
|
900
|
+
this.elements.pop();
|
|
901
|
+
} else {
|
|
902
|
+
this.elements.splice(idx, 1, this.elements.pop());
|
|
903
|
+
this._bubbleUp(idx);
|
|
904
|
+
this._sinkDown(idx, this.elements.length >> 1);
|
|
905
|
+
}
|
|
906
|
+
return true;
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* Set the equality comparator used by has/delete operations.
|
|
910
|
+
* @remarks Time O(1), Space O(1)
|
|
911
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
912
|
+
* @returns This heap.
|
|
913
|
+
*/
|
|
914
|
+
setEquality(equals) {
|
|
915
|
+
this._equals = equals;
|
|
916
|
+
return this;
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
920
|
+
* @remarks Time O(N), Space O(H)
|
|
921
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
922
|
+
* @returns Array of visited elements.
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
* @example
|
|
950
|
+
* // Depth-first traversal
|
|
951
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
952
|
+
* const result = heap.dfs('IN');
|
|
953
|
+
* console.log(result.length); // 3;
|
|
954
|
+
*/
|
|
955
|
+
dfs(order = "PRE") {
|
|
956
|
+
const result = [];
|
|
957
|
+
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
958
|
+
const left = 2 * index + 1, right = left + 1;
|
|
959
|
+
if (index < this.size) {
|
|
960
|
+
if (order === "IN") {
|
|
961
|
+
_dfs(left);
|
|
962
|
+
result.push(this.elements[index]);
|
|
963
|
+
_dfs(right);
|
|
964
|
+
} else if (order === "PRE") {
|
|
965
|
+
result.push(this.elements[index]);
|
|
966
|
+
_dfs(left);
|
|
967
|
+
_dfs(right);
|
|
968
|
+
} else if (order === "POST") {
|
|
969
|
+
_dfs(left);
|
|
970
|
+
_dfs(right);
|
|
971
|
+
result.push(this.elements[index]);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}, "_dfs");
|
|
975
|
+
_dfs(0);
|
|
976
|
+
return result;
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Restore heap order bottom-up (heapify in-place).
|
|
980
|
+
* @remarks Time O(N), Space O(1)
|
|
981
|
+
* @returns Array of per-node results from fixing steps.
|
|
982
|
+
*/
|
|
983
|
+
fix() {
|
|
984
|
+
const results = [];
|
|
985
|
+
for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
|
|
986
|
+
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
987
|
+
}
|
|
988
|
+
return results;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
992
|
+
* @remarks Time O(N log N), Space O(N)
|
|
993
|
+
* @returns Sorted array of elements.
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
* @example
|
|
1030
|
+
* // Sort elements using heap
|
|
1031
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1032
|
+
* const sorted = heap.sort();
|
|
1033
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1034
|
+
*/
|
|
1035
|
+
sort() {
|
|
1036
|
+
const visited = [];
|
|
1037
|
+
const cloned = this._createInstance();
|
|
1038
|
+
for (const x of this.elements) cloned.add(x);
|
|
1039
|
+
while (!cloned.isEmpty()) {
|
|
1040
|
+
const top = cloned.poll();
|
|
1041
|
+
if (top !== void 0) visited.push(top);
|
|
1042
|
+
}
|
|
1043
|
+
return visited;
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* Deep clone this heap.
|
|
1047
|
+
* @remarks Time O(N), Space O(N)
|
|
1048
|
+
* @returns A new heap with the same elements.
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
* @example
|
|
1083
|
+
* // Create independent copy
|
|
1084
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1085
|
+
* const copy = heap.clone();
|
|
1086
|
+
* copy.poll();
|
|
1087
|
+
* console.log(heap.size); // 3;
|
|
1088
|
+
* console.log(copy.size); // 2;
|
|
1089
|
+
*/
|
|
1090
|
+
clone() {
|
|
1091
|
+
const next = this._createInstance();
|
|
1092
|
+
for (const x of this.elements) next.add(x);
|
|
1093
|
+
return next;
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Filter elements into a new heap of the same class.
|
|
1097
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1098
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1099
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1100
|
+
* @returns A new heap with the kept elements.
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
* @example
|
|
1135
|
+
* // Filter elements
|
|
1136
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1137
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1138
|
+
* console.log(evens.size); // 2;
|
|
1139
|
+
*/
|
|
1140
|
+
filter(callback, thisArg) {
|
|
1141
|
+
const out = this._createInstance();
|
|
1142
|
+
let i = 0;
|
|
1143
|
+
for (const x of this) {
|
|
1144
|
+
if (thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this)) {
|
|
1145
|
+
out.add(x);
|
|
1146
|
+
} else {
|
|
1147
|
+
i++;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
return out;
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Map elements into a new heap of possibly different element type.
|
|
1154
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1155
|
+
* @template EM
|
|
1156
|
+
* @template RM
|
|
1157
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1158
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1159
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1160
|
+
* @returns A new heap with mapped elements.
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
* @example
|
|
1194
|
+
* // Transform elements
|
|
1195
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1196
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1197
|
+
* console.log(doubled.peek()); // 2;
|
|
1198
|
+
*/
|
|
1199
|
+
map(callback, options, thisArg) {
|
|
1200
|
+
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
1201
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1202
|
+
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1203
|
+
let i = 0;
|
|
1204
|
+
for (const x of this) {
|
|
1205
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
1206
|
+
out.add(v);
|
|
1207
|
+
}
|
|
1208
|
+
return out;
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Map elements into a new heap of the same element type.
|
|
1212
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1213
|
+
* @param callback - Mapping function (element, index, heap) → element.
|
|
1214
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1215
|
+
* @returns A new heap with mapped elements.
|
|
1216
|
+
*/
|
|
1217
|
+
mapSame(callback, thisArg) {
|
|
1218
|
+
const out = this._createInstance();
|
|
1219
|
+
let i = 0;
|
|
1220
|
+
for (const x of this) {
|
|
1221
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
1222
|
+
out.add(v);
|
|
1223
|
+
}
|
|
1224
|
+
return out;
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* Get the comparator used to order elements.
|
|
1228
|
+
* @remarks Time O(1), Space O(1)
|
|
1229
|
+
* @returns Comparator function.
|
|
1230
|
+
*/
|
|
1231
|
+
get comparator() {
|
|
1232
|
+
return this._comparator;
|
|
1233
|
+
}
|
|
1234
|
+
*_getIterator() {
|
|
1235
|
+
for (const element of this.elements) yield element;
|
|
1236
|
+
}
|
|
1237
|
+
_bubbleUp(index) {
|
|
1238
|
+
const element = this.elements[index];
|
|
1239
|
+
while (index > 0) {
|
|
1240
|
+
const parent = index - 1 >> 1;
|
|
1241
|
+
const parentItem = this.elements[parent];
|
|
1242
|
+
if (this.comparator(parentItem, element) <= 0) break;
|
|
1243
|
+
this.elements[index] = parentItem;
|
|
1244
|
+
index = parent;
|
|
1245
|
+
}
|
|
1246
|
+
this.elements[index] = element;
|
|
1247
|
+
return true;
|
|
1248
|
+
}
|
|
1249
|
+
_sinkDown(index, halfLength) {
|
|
1250
|
+
const element = this.elements[index];
|
|
1251
|
+
while (index < halfLength) {
|
|
1252
|
+
let left = index << 1 | 1;
|
|
1253
|
+
const right = left + 1;
|
|
1254
|
+
let minItem = this.elements[left];
|
|
1255
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
1256
|
+
left = right;
|
|
1257
|
+
minItem = this.elements[right];
|
|
1258
|
+
}
|
|
1259
|
+
if (this.comparator(minItem, element) >= 0) break;
|
|
1260
|
+
this.elements[index] = minItem;
|
|
1261
|
+
index = left;
|
|
1262
|
+
}
|
|
1263
|
+
this.elements[index] = element;
|
|
1264
|
+
return true;
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
1268
|
+
* @remarks Time O(1), Space O(1)
|
|
1269
|
+
* @param [options] - Options to override comparator or toElementFn.
|
|
1270
|
+
* @returns A like-kind empty heap instance.
|
|
1271
|
+
*/
|
|
1272
|
+
_createInstance(options) {
|
|
1273
|
+
const Ctor = this.constructor;
|
|
1274
|
+
return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options != null ? options : {} });
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1277
|
+
* (Protected) Create a like-kind instance seeded by elements.
|
|
1278
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1279
|
+
* @template EM
|
|
1280
|
+
* @template RM
|
|
1281
|
+
* @param [elements] - Iterable of elements or raw values to seed.
|
|
1282
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1283
|
+
* @returns A like-kind heap instance.
|
|
1284
|
+
*/
|
|
1285
|
+
_createLike(elements = [], options) {
|
|
1286
|
+
const Ctor = this.constructor;
|
|
1287
|
+
return new Ctor(elements, options);
|
|
1288
|
+
}
|
|
1289
|
+
/**
|
|
1290
|
+
* (Protected) Spawn an empty like-kind heap instance.
|
|
1291
|
+
* @remarks Time O(1), Space O(1)
|
|
1292
|
+
* @template EM
|
|
1293
|
+
* @template RM
|
|
1294
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1295
|
+
* @returns An empty like-kind heap instance.
|
|
1296
|
+
*/
|
|
1297
|
+
_spawnLike(options) {
|
|
1298
|
+
return this._createLike([], options);
|
|
1299
|
+
}
|
|
1300
|
+
};
|
|
1301
|
+
__name(_Heap, "Heap");
|
|
1302
|
+
var Heap = _Heap;
|
|
1303
|
+
var _FibonacciHeapNode = class _FibonacciHeapNode {
|
|
1304
|
+
constructor(element, degree = 0) {
|
|
1305
|
+
__publicField(this, "element");
|
|
1306
|
+
__publicField(this, "degree");
|
|
1307
|
+
__publicField(this, "left");
|
|
1308
|
+
__publicField(this, "right");
|
|
1309
|
+
__publicField(this, "child");
|
|
1310
|
+
__publicField(this, "parent");
|
|
1311
|
+
__publicField(this, "marked");
|
|
1312
|
+
this.element = element;
|
|
1313
|
+
this.degree = degree;
|
|
1314
|
+
this.marked = false;
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
__name(_FibonacciHeapNode, "FibonacciHeapNode");
|
|
1318
|
+
var FibonacciHeapNode = _FibonacciHeapNode;
|
|
1319
|
+
var _FibonacciHeap = class _FibonacciHeap {
|
|
1320
|
+
/**
|
|
1321
|
+
* Create a FibonacciHeap.
|
|
1322
|
+
* @remarks Time O(1), Space O(1)
|
|
1323
|
+
* @param [comparator] - Comparator to order elements (min-heap by default).
|
|
1324
|
+
* @returns New FibonacciHeap instance.
|
|
1325
|
+
*/
|
|
1326
|
+
constructor(comparator) {
|
|
1327
|
+
__publicField(this, "_root");
|
|
1328
|
+
__publicField(this, "_size", 0);
|
|
1329
|
+
__publicField(this, "_min");
|
|
1330
|
+
__publicField(this, "_comparator");
|
|
1331
|
+
this.clear();
|
|
1332
|
+
this._comparator = comparator || this._defaultComparator;
|
|
1333
|
+
if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
1334
|
+
}
|
|
1335
|
+
/**
|
|
1336
|
+
* Get the circular root list head.
|
|
1337
|
+
* @remarks Time O(1), Space O(1)
|
|
1338
|
+
* @returns Root node or undefined.
|
|
1339
|
+
*/
|
|
1340
|
+
get root() {
|
|
1341
|
+
return this._root;
|
|
1342
|
+
}
|
|
1343
|
+
get size() {
|
|
1344
|
+
return this._size;
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* Get the current minimum node.
|
|
1348
|
+
* @remarks Time O(1), Space O(1)
|
|
1349
|
+
* @returns Min node or undefined.
|
|
1350
|
+
*/
|
|
1351
|
+
get min() {
|
|
1352
|
+
return this._min;
|
|
1353
|
+
}
|
|
1354
|
+
get comparator() {
|
|
1355
|
+
return this._comparator;
|
|
1356
|
+
}
|
|
1357
|
+
clear() {
|
|
1358
|
+
this._root = void 0;
|
|
1359
|
+
this._min = void 0;
|
|
1360
|
+
this._size = 0;
|
|
1361
|
+
}
|
|
1362
|
+
add(element) {
|
|
1363
|
+
this.push(element);
|
|
1364
|
+
return true;
|
|
1365
|
+
}
|
|
1366
|
+
/**
|
|
1367
|
+
* Push an element into the root list.
|
|
1368
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1369
|
+
* @param element - Element to insert.
|
|
1370
|
+
* @returns This heap.
|
|
1371
|
+
*/
|
|
1372
|
+
push(element) {
|
|
1373
|
+
const node = this.createNode(element);
|
|
1374
|
+
node.left = node;
|
|
1375
|
+
node.right = node;
|
|
1376
|
+
this.mergeWithRoot(node);
|
|
1377
|
+
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1378
|
+
this._size++;
|
|
1379
|
+
return this;
|
|
1380
|
+
}
|
|
1381
|
+
peek() {
|
|
1382
|
+
return this.min ? this.min.element : void 0;
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* Collect nodes from a circular doubly linked list starting at head.
|
|
1386
|
+
* @remarks Time O(K), Space O(K)
|
|
1387
|
+
* @param [head] - Start node of the circular list.
|
|
1388
|
+
* @returns Array of nodes from the list.
|
|
1389
|
+
*/
|
|
1390
|
+
consumeLinkedList(head) {
|
|
1391
|
+
const elements = [];
|
|
1392
|
+
if (!head) return elements;
|
|
1393
|
+
let node = head;
|
|
1394
|
+
let started = false;
|
|
1395
|
+
while (true) {
|
|
1396
|
+
if (node === head && started) break;
|
|
1397
|
+
else if (node === head) started = true;
|
|
1398
|
+
elements.push(node);
|
|
1399
|
+
node = node.right;
|
|
1400
|
+
}
|
|
1401
|
+
return elements;
|
|
1402
|
+
}
|
|
1403
|
+
/**
|
|
1404
|
+
* Insert a node into a parent's child list (circular).
|
|
1405
|
+
* @remarks Time O(1), Space O(1)
|
|
1406
|
+
* @param parent - Parent node.
|
|
1407
|
+
* @param node - Child node to insert.
|
|
1408
|
+
* @returns void
|
|
1409
|
+
*/
|
|
1410
|
+
mergeWithChild(parent, node) {
|
|
1411
|
+
if (!parent.child) parent.child = node;
|
|
1412
|
+
else {
|
|
1413
|
+
node.right = parent.child.right;
|
|
1414
|
+
node.left = parent.child;
|
|
1415
|
+
parent.child.right.left = node;
|
|
1416
|
+
parent.child.right = node;
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
poll() {
|
|
1420
|
+
return this.pop();
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Remove and return the minimum element, consolidating the root list.
|
|
1424
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
1425
|
+
* @returns Minimum element or undefined.
|
|
1426
|
+
*/
|
|
1427
|
+
pop() {
|
|
1428
|
+
if (this._size === 0) return void 0;
|
|
1429
|
+
const z = this.min;
|
|
1430
|
+
if (z.child) {
|
|
1431
|
+
const elements = this.consumeLinkedList(z.child);
|
|
1432
|
+
for (const node of elements) {
|
|
1433
|
+
this.mergeWithRoot(node);
|
|
1434
|
+
node.parent = void 0;
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
this.removeFromRoot(z);
|
|
1438
|
+
if (z === z.right) {
|
|
1439
|
+
this._min = void 0;
|
|
1440
|
+
this._root = void 0;
|
|
1441
|
+
} else {
|
|
1442
|
+
this._min = z.right;
|
|
1443
|
+
this._consolidate();
|
|
1444
|
+
}
|
|
1445
|
+
this._size--;
|
|
1446
|
+
return z.element;
|
|
1447
|
+
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Meld another heap into this heap.
|
|
1450
|
+
* @remarks Time O(1), Space O(1)
|
|
1451
|
+
* @param heapToMerge - Another FibonacciHeap to meld into this one.
|
|
1452
|
+
* @returns void
|
|
1453
|
+
*/
|
|
1454
|
+
merge(heapToMerge) {
|
|
1455
|
+
if (heapToMerge.size === 0) return;
|
|
1456
|
+
if (this.root && heapToMerge.root) {
|
|
1457
|
+
const thisRoot = this.root, otherRoot = heapToMerge.root;
|
|
1458
|
+
const thisRootRight = thisRoot.right, otherRootLeft = otherRoot.left;
|
|
1459
|
+
thisRoot.right = otherRoot;
|
|
1460
|
+
otherRoot.left = thisRoot;
|
|
1461
|
+
thisRootRight.left = otherRootLeft;
|
|
1462
|
+
otherRootLeft.right = thisRootRight;
|
|
1463
|
+
} else if (!this.root && heapToMerge.root) {
|
|
1464
|
+
this._root = heapToMerge.root;
|
|
1465
|
+
}
|
|
1466
|
+
if (!this.min || heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0) {
|
|
1467
|
+
this._min = heapToMerge.min;
|
|
1468
|
+
}
|
|
1469
|
+
this._size += heapToMerge.size;
|
|
1470
|
+
heapToMerge.clear();
|
|
1471
|
+
}
|
|
1472
|
+
createNode(element) {
|
|
1473
|
+
return new FibonacciHeapNode(element);
|
|
1474
|
+
}
|
|
1475
|
+
isEmpty() {
|
|
1476
|
+
return this._size === 0;
|
|
1477
|
+
}
|
|
1478
|
+
_defaultComparator(a, b) {
|
|
1479
|
+
if (a < b) return -1;
|
|
1480
|
+
if (a > b) return 1;
|
|
1481
|
+
return 0;
|
|
1482
|
+
}
|
|
1483
|
+
mergeWithRoot(node) {
|
|
1484
|
+
if (!this.root) this._root = node;
|
|
1485
|
+
else {
|
|
1486
|
+
node.right = this.root.right;
|
|
1487
|
+
node.left = this.root;
|
|
1488
|
+
this.root.right.left = node;
|
|
1489
|
+
this.root.right = node;
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
removeFromRoot(node) {
|
|
1493
|
+
if (this.root === node) this._root = node.right;
|
|
1494
|
+
if (node.left) node.left.right = node.right;
|
|
1495
|
+
if (node.right) node.right.left = node.left;
|
|
1496
|
+
}
|
|
1497
|
+
_link(y, x) {
|
|
1498
|
+
this.removeFromRoot(y);
|
|
1499
|
+
y.left = y;
|
|
1500
|
+
y.right = y;
|
|
1501
|
+
this.mergeWithChild(x, y);
|
|
1502
|
+
x.degree++;
|
|
1503
|
+
y.parent = x;
|
|
1504
|
+
}
|
|
1505
|
+
_consolidate() {
|
|
1506
|
+
const A = new Array(this._size);
|
|
1507
|
+
const elements = this.consumeLinkedList(this.root);
|
|
1508
|
+
let x, y, d, t;
|
|
1509
|
+
for (const node of elements) {
|
|
1510
|
+
x = node;
|
|
1511
|
+
d = x.degree;
|
|
1512
|
+
while (A[d]) {
|
|
1513
|
+
y = A[d];
|
|
1514
|
+
if (this.comparator(x.element, y.element) > 0) {
|
|
1515
|
+
t = x;
|
|
1516
|
+
x = y;
|
|
1517
|
+
y = t;
|
|
1518
|
+
}
|
|
1519
|
+
this._link(y, x);
|
|
1520
|
+
A[d] = void 0;
|
|
1521
|
+
d++;
|
|
1522
|
+
}
|
|
1523
|
+
A[d] = x;
|
|
1524
|
+
}
|
|
1525
|
+
for (let i = 0; i < A.length; i++) {
|
|
1526
|
+
if (A[i] && (!this.min || this.comparator(A[i].element, this.min.element) <= 0)) this._min = A[i];
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
};
|
|
1530
|
+
__name(_FibonacciHeap, "FibonacciHeap");
|
|
1531
|
+
var FibonacciHeap = _FibonacciHeap;
|
|
1532
|
+
|
|
1533
|
+
// src/data-structures/heap/max-heap.ts
|
|
1534
|
+
var _MaxHeap = class _MaxHeap extends Heap {
|
|
1535
|
+
/**
|
|
1536
|
+
* Create a max-heap. For objects, supply a custom comparator.
|
|
1537
|
+
* @param elements Optional initial elements.
|
|
1538
|
+
* @param options Optional configuration.
|
|
1539
|
+
*/
|
|
1540
|
+
constructor(elements = [], options) {
|
|
1541
|
+
super(elements, {
|
|
1542
|
+
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
1543
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
1544
|
+
raise(TypeError, ERR.comparatorRequired("MaxHeap"));
|
|
1545
|
+
}
|
|
1546
|
+
if (a < b) return 1;
|
|
1547
|
+
if (a > b) return -1;
|
|
1548
|
+
return 0;
|
|
1549
|
+
}, "comparator"),
|
|
1550
|
+
...options
|
|
1551
|
+
});
|
|
1552
|
+
}
|
|
1553
|
+
};
|
|
1554
|
+
__name(_MaxHeap, "MaxHeap");
|
|
1555
|
+
var MaxHeap = _MaxHeap;
|
|
1556
|
+
|
|
1557
|
+
// src/data-structures/heap/min-heap.ts
|
|
1558
|
+
var _MinHeap = class _MinHeap extends Heap {
|
|
1559
|
+
/**
|
|
1560
|
+
* Create a min-heap.
|
|
1561
|
+
* @param elements Optional initial elements.
|
|
1562
|
+
* @param options Optional configuration.
|
|
1563
|
+
*/
|
|
1564
|
+
constructor(elements = [], options) {
|
|
1565
|
+
super(elements, options);
|
|
1566
|
+
}
|
|
1567
|
+
};
|
|
1568
|
+
__name(_MinHeap, "MinHeap");
|
|
1569
|
+
var MinHeap = _MinHeap;
|
|
1570
|
+
/**
|
|
1571
|
+
* data-structure-typed
|
|
1572
|
+
*
|
|
1573
|
+
* @author Pablo Zeng
|
|
1574
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
1575
|
+
* @license MIT License
|
|
1576
|
+
*/
|
|
1577
|
+
/**
|
|
1578
|
+
* data-structure-typed
|
|
1579
|
+
* @author Kirk Qi
|
|
1580
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
1581
|
+
* @license MIT License
|
|
1582
|
+
*/
|
|
1583
|
+
/**
|
|
1584
|
+
* @remarks Time O(n log n), Space O(n).
|
|
1585
|
+
* data-structure-typed
|
|
1586
|
+
* @author Kirk Qi
|
|
1587
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
1588
|
+
* @license MIT License
|
|
1589
|
+
*/
|
|
1590
|
+
|
|
1591
|
+
export { FibonacciHeap, FibonacciHeapNode, Heap, MaxHeap, MinHeap };
|
|
1592
|
+
//# sourceMappingURL=heap.mjs.map
|
|
1593
|
+
//# sourceMappingURL=heap.mjs.map
|