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.
Files changed (246) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +5 -1
  7. package/README.md +124 -29
  8. package/dist/cjs/binary-tree.cjs +26282 -0
  9. package/dist/cjs/graph.cjs +5422 -0
  10. package/dist/cjs/hash.cjs +1310 -0
  11. package/dist/cjs/heap.cjs +1602 -0
  12. package/dist/cjs/index.cjs +31257 -14673
  13. package/dist/cjs/linked-list.cjs +4576 -0
  14. package/dist/cjs/matrix.cjs +1080 -0
  15. package/dist/cjs/priority-queue.cjs +1376 -0
  16. package/dist/cjs/queue.cjs +4264 -0
  17. package/dist/cjs/stack.cjs +907 -0
  18. package/dist/cjs/trie.cjs +1223 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +26319 -0
  20. package/dist/cjs-legacy/graph.cjs +5420 -0
  21. package/dist/cjs-legacy/hash.cjs +1310 -0
  22. package/dist/cjs-legacy/heap.cjs +1599 -0
  23. package/dist/cjs-legacy/index.cjs +31268 -14679
  24. package/dist/cjs-legacy/linked-list.cjs +4582 -0
  25. package/dist/cjs-legacy/matrix.cjs +1083 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1374 -0
  27. package/dist/cjs-legacy/queue.cjs +4262 -0
  28. package/dist/cjs-legacy/stack.cjs +907 -0
  29. package/dist/cjs-legacy/trie.cjs +1222 -0
  30. package/dist/esm/binary-tree.mjs +26267 -0
  31. package/dist/esm/graph.mjs +5409 -0
  32. package/dist/esm/hash.mjs +1307 -0
  33. package/dist/esm/heap.mjs +1596 -0
  34. package/dist/esm/index.mjs +31254 -14674
  35. package/dist/esm/linked-list.mjs +4569 -0
  36. package/dist/esm/matrix.mjs +1076 -0
  37. package/dist/esm/priority-queue.mjs +1372 -0
  38. package/dist/esm/queue.mjs +4260 -0
  39. package/dist/esm/stack.mjs +905 -0
  40. package/dist/esm/trie.mjs +1220 -0
  41. package/dist/esm-legacy/binary-tree.mjs +26304 -0
  42. package/dist/esm-legacy/graph.mjs +5407 -0
  43. package/dist/esm-legacy/hash.mjs +1307 -0
  44. package/dist/esm-legacy/heap.mjs +1593 -0
  45. package/dist/esm-legacy/index.mjs +31265 -14680
  46. package/dist/esm-legacy/linked-list.mjs +4575 -0
  47. package/dist/esm-legacy/matrix.mjs +1079 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1370 -0
  49. package/dist/esm-legacy/queue.mjs +4258 -0
  50. package/dist/esm-legacy/stack.mjs +905 -0
  51. package/dist/esm-legacy/trie.mjs +1219 -0
  52. package/dist/types/common/error.d.ts +9 -0
  53. package/dist/types/common/index.d.ts +1 -1
  54. package/dist/types/data-structures/base/index.d.ts +1 -0
  55. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  56. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  57. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  61. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  62. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  63. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  64. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  65. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  66. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  70. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  71. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  72. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  73. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  74. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  75. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  76. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  77. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  78. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  79. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  80. package/dist/types/interfaces/graph.d.ts +1 -1
  81. package/dist/types/types/common.d.ts +2 -2
  82. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  83. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  84. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  85. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  86. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  87. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  88. package/dist/types/types/utils/validate-type.d.ts +4 -4
  89. package/dist/umd/data-structure-typed.js +31196 -14608
  90. package/dist/umd/data-structure-typed.min.js +11 -5
  91. package/docs-site-docusaurus/README.md +41 -0
  92. package/docs-site-docusaurus/docs/api/README.md +52 -0
  93. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
  94. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  95. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  96. package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
  97. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  98. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  99. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  100. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  101. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  102. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  103. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  104. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  105. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  106. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  107. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  108. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  109. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  110. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  111. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  112. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  113. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  116. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  117. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  118. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  119. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  120. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  121. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  122. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  123. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  124. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  125. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
  126. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  127. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  128. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  129. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  130. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  131. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
  132. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
  133. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
  134. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  135. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  136. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  137. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  138. package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
  139. package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
  140. package/docs-site-docusaurus/docs/guide/faq.md +180 -0
  141. package/docs-site-docusaurus/docs/guide/guides.md +597 -0
  142. package/docs-site-docusaurus/docs/guide/installation.md +62 -0
  143. package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
  144. package/docs-site-docusaurus/docs/guide/overview.md +645 -0
  145. package/docs-site-docusaurus/docs/guide/performance.md +835 -0
  146. package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
  147. package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
  148. package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
  149. package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
  150. package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
  151. package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
  152. package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
  153. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  154. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  155. package/docs-site-docusaurus/package-lock.json +18667 -0
  156. package/docs-site-docusaurus/package.json +50 -0
  157. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  158. package/docs-site-docusaurus/sidebars.ts +23 -0
  159. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  160. package/docs-site-docusaurus/src/css/custom.css +96 -0
  161. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  162. package/docs-site-docusaurus/src/pages/index.tsx +120 -0
  163. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  164. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  165. package/docs-site-docusaurus/static/.nojekyll +0 -0
  166. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  167. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  168. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  169. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  170. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  171. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  172. package/docs-site-docusaurus/static/img/logo.png +0 -0
  173. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  174. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  175. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  176. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  177. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  178. package/docs-site-docusaurus/static/llms.txt +37 -0
  179. package/docs-site-docusaurus/static/robots.txt +4 -0
  180. package/docs-site-docusaurus/typedoc.json +23 -0
  181. package/llms.txt +37 -0
  182. package/package.json +159 -55
  183. package/src/common/error.ts +19 -1
  184. package/src/common/index.ts +1 -1
  185. package/src/data-structures/base/index.ts +1 -0
  186. package/src/data-structures/base/iterable-element-base.ts +3 -2
  187. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  188. package/src/data-structures/base/linear-base.ts +3 -3
  189. package/src/data-structures/binary-tree/avl-tree.ts +287 -0
  190. package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
  191. package/src/data-structures/binary-tree/binary-tree.ts +581 -6
  192. package/src/data-structures/binary-tree/bst.ts +922 -7
  193. package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
  194. package/src/data-structures/binary-tree/segment-tree.ts +139 -2
  195. package/src/data-structures/binary-tree/tree-map.ts +3300 -495
  196. package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
  197. package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
  198. package/src/data-structures/binary-tree/tree-set.ts +3122 -440
  199. package/src/data-structures/graph/abstract-graph.ts +6 -6
  200. package/src/data-structures/graph/directed-graph.ts +230 -0
  201. package/src/data-structures/graph/undirected-graph.ts +207 -0
  202. package/src/data-structures/hash/hash-map.ts +270 -19
  203. package/src/data-structures/heap/heap.ts +326 -4
  204. package/src/data-structures/heap/max-heap.ts +2 -2
  205. package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
  206. package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
  207. package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
  208. package/src/data-structures/matrix/matrix.ts +194 -10
  209. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  210. package/src/data-structures/queue/deque.ts +350 -5
  211. package/src/data-structures/queue/queue.ts +276 -0
  212. package/src/data-structures/stack/stack.ts +230 -0
  213. package/src/data-structures/trie/trie.ts +283 -7
  214. package/src/interfaces/graph.ts +1 -1
  215. package/src/types/common.ts +2 -2
  216. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  217. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  218. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  219. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  220. package/src/types/data-structures/heap/heap.ts +1 -0
  221. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  222. package/src/types/utils/validate-type.ts +4 -4
  223. package/vercel.json +6 -0
  224. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  225. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  226. package/dist/leetcode/avl-tree.mjs +0 -2720
  227. package/dist/leetcode/binary-tree.mjs +0 -1594
  228. package/dist/leetcode/bst.mjs +0 -2398
  229. package/dist/leetcode/deque.mjs +0 -683
  230. package/dist/leetcode/directed-graph.mjs +0 -1733
  231. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  232. package/dist/leetcode/hash-map.mjs +0 -493
  233. package/dist/leetcode/heap.mjs +0 -542
  234. package/dist/leetcode/max-heap.mjs +0 -375
  235. package/dist/leetcode/max-priority-queue.mjs +0 -383
  236. package/dist/leetcode/min-heap.mjs +0 -363
  237. package/dist/leetcode/min-priority-queue.mjs +0 -371
  238. package/dist/leetcode/priority-queue.mjs +0 -363
  239. package/dist/leetcode/queue.mjs +0 -943
  240. package/dist/leetcode/red-black-tree.mjs +0 -2765
  241. package/dist/leetcode/singly-linked-list.mjs +0 -754
  242. package/dist/leetcode/stack.mjs +0 -217
  243. package/dist/leetcode/tree-counter.mjs +0 -3039
  244. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  245. package/dist/leetcode/trie.mjs +0 -413
  246. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,1376 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
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 {
39
+ static {
40
+ __name(this, "IterableElementBase");
41
+ }
42
+ /**
43
+ * Create a new iterable base.
44
+ *
45
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
46
+ * is used to convert a raw element (`R`) into a public element (`E`).
47
+ *
48
+ * @remarks
49
+ * Time O(1), Space O(1).
50
+ */
51
+ constructor(options) {
52
+ if (options) {
53
+ const { toElementFn } = options;
54
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
55
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
56
+ }
57
+ }
58
+ /**
59
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
60
+ *
61
+ * @remarks
62
+ * Time O(1), Space O(1).
63
+ */
64
+ _toElementFn;
65
+ /**
66
+ * Exposes the current `toElementFn`, if configured.
67
+ *
68
+ * @returns The converter function or `undefined` when not set.
69
+ * @remarks
70
+ * Time O(1), Space O(1).
71
+ */
72
+ get toElementFn() {
73
+ return this._toElementFn;
74
+ }
75
+ /**
76
+ * Returns an iterator over the structure's elements.
77
+ *
78
+ * @param args Optional iterator arguments forwarded to the internal iterator.
79
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
80
+ *
81
+ * @remarks
82
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
83
+ */
84
+ *[Symbol.iterator](...args) {
85
+ yield* this._getIterator(...args);
86
+ }
87
+ /**
88
+ * Returns an iterator over the values (alias of the default iterator).
89
+ *
90
+ * @returns An `IterableIterator<E>` over all elements.
91
+ * @remarks
92
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
93
+ */
94
+ *values() {
95
+ for (const item of this) yield item;
96
+ }
97
+ /**
98
+ * Tests whether all elements satisfy the predicate.
99
+ *
100
+ * @template TReturn
101
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
102
+ * @param thisArg Optional `this` binding for the predicate.
103
+ * @returns `true` if every element passes; otherwise `false`.
104
+ *
105
+ * @remarks
106
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
107
+ */
108
+ every(predicate, thisArg) {
109
+ let index = 0;
110
+ for (const item of this) {
111
+ if (thisArg === void 0) {
112
+ if (!predicate(item, index++, this)) return false;
113
+ } else {
114
+ const fn = predicate;
115
+ if (!fn.call(thisArg, item, index++, this)) return false;
116
+ }
117
+ }
118
+ return true;
119
+ }
120
+ /**
121
+ * Tests whether at least one element satisfies the predicate.
122
+ *
123
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
124
+ * @param thisArg Optional `this` binding for the predicate.
125
+ * @returns `true` if any element passes; otherwise `false`.
126
+ *
127
+ * @remarks
128
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
129
+ */
130
+ some(predicate, thisArg) {
131
+ let index = 0;
132
+ for (const item of this) {
133
+ if (thisArg === void 0) {
134
+ if (predicate(item, index++, this)) return true;
135
+ } else {
136
+ const fn = predicate;
137
+ if (fn.call(thisArg, item, index++, this)) return true;
138
+ }
139
+ }
140
+ return false;
141
+ }
142
+ /**
143
+ * Invokes a callback for each element in iteration order.
144
+ *
145
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
146
+ * @param thisArg Optional `this` binding for the callback.
147
+ * @returns `void`.
148
+ *
149
+ * @remarks
150
+ * Time O(n), Space O(1).
151
+ */
152
+ forEach(callbackfn, thisArg) {
153
+ let index = 0;
154
+ for (const item of this) {
155
+ if (thisArg === void 0) {
156
+ callbackfn(item, index++, this);
157
+ } else {
158
+ const fn = callbackfn;
159
+ fn.call(thisArg, item, index++, this);
160
+ }
161
+ }
162
+ }
163
+ // Implementation signature
164
+ find(predicate, thisArg) {
165
+ let index = 0;
166
+ for (const item of this) {
167
+ if (thisArg === void 0) {
168
+ if (predicate(item, index++, this)) return item;
169
+ } else {
170
+ const fn = predicate;
171
+ if (fn.call(thisArg, item, index++, this)) return item;
172
+ }
173
+ }
174
+ return;
175
+ }
176
+ /**
177
+ * Checks whether a strictly-equal element exists in the structure.
178
+ *
179
+ * @param element The element to test with `===` equality.
180
+ * @returns `true` if an equal element is found; otherwise `false`.
181
+ *
182
+ * @remarks
183
+ * Time O(n) in the worst case. Space O(1).
184
+ */
185
+ has(element) {
186
+ for (const ele of this) if (ele === element) return true;
187
+ return false;
188
+ }
189
+ /**
190
+ * Reduces all elements to a single accumulated value.
191
+ *
192
+ * @overload
193
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
194
+ * @returns The final accumulated value typed as `E`.
195
+ *
196
+ * @overload
197
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
198
+ * @param initialValue The initial accumulator value of type `E`.
199
+ * @returns The final accumulated value typed as `E`.
200
+ *
201
+ * @overload
202
+ * @template U The accumulator type when it differs from `E`.
203
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
204
+ * @param initialValue The initial accumulator value of type `U`.
205
+ * @returns The final accumulated value typed as `U`.
206
+ *
207
+ * @remarks
208
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
209
+ */
210
+ reduce(callbackfn, initialValue) {
211
+ let index = 0;
212
+ const iter = this[Symbol.iterator]();
213
+ let acc;
214
+ if (arguments.length >= 2) {
215
+ acc = initialValue;
216
+ } else {
217
+ const first = iter.next();
218
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
219
+ acc = first.value;
220
+ index = 1;
221
+ }
222
+ for (const value of iter) {
223
+ acc = callbackfn(acc, value, index++, this);
224
+ }
225
+ return acc;
226
+ }
227
+ /**
228
+ * Materializes the elements into a new array.
229
+ *
230
+ * @returns A shallow array copy of the iteration order.
231
+ * @remarks
232
+ * Time O(n), Space O(n).
233
+ */
234
+ toArray() {
235
+ return [...this];
236
+ }
237
+ /**
238
+ * Returns a representation of the structure suitable for quick visualization.
239
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
240
+ *
241
+ * @returns A visual representation (array by default).
242
+ * @remarks
243
+ * Time O(n), Space O(n).
244
+ */
245
+ toVisual() {
246
+ return [...this];
247
+ }
248
+ /**
249
+ * Prints `toVisual()` to the console. Intended for quick debugging.
250
+ *
251
+ * @returns `void`.
252
+ * @remarks
253
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
254
+ */
255
+ print() {
256
+ console.log(this.toVisual());
257
+ }
258
+ };
259
+
260
+ // src/data-structures/heap/heap.ts
261
+ var Heap = class _Heap extends IterableElementBase {
262
+ static {
263
+ __name(this, "Heap");
264
+ }
265
+ _equals = Object.is;
266
+ /**
267
+ * Create a Heap and optionally bulk-insert elements.
268
+ * @remarks Time O(N), Space O(N)
269
+ * @param [elements] - Iterable of elements (or raw values if toElementFn is set).
270
+ * @param [options] - Options such as comparator and toElementFn.
271
+ * @returns New Heap instance.
272
+ */
273
+ constructor(elements = [], options) {
274
+ super(options);
275
+ if (options) {
276
+ const { comparator } = options;
277
+ if (comparator) this._comparator = comparator;
278
+ }
279
+ this.addMany(elements);
280
+ }
281
+ _elements = [];
282
+ /**
283
+ * Get the backing array of the heap.
284
+ * @remarks Time O(1), Space O(1)
285
+ * @returns Internal elements array.
286
+ */
287
+ get elements() {
288
+ return this._elements;
289
+ }
290
+ /**
291
+ * Get the number of elements.
292
+ * @remarks Time O(1), Space O(1)
293
+ * @returns Heap size.
294
+
295
+
296
+
297
+
298
+
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
+ * @example
330
+ * // Track heap capacity
331
+ * const heap = new Heap<number>();
332
+ * console.log(heap.size); // 0;
333
+ * heap.add(10);
334
+ * heap.add(20);
335
+ * console.log(heap.size); // 2;
336
+ * heap.poll();
337
+ * console.log(heap.size); // 1;
338
+ */
339
+ get size() {
340
+ return this.elements.length;
341
+ }
342
+ /**
343
+ * Get the last leaf element.
344
+ * @remarks Time O(1), Space O(1)
345
+ * @returns Last element or undefined.
346
+ */
347
+ get leaf() {
348
+ return this.elements[this.size - 1] ?? void 0;
349
+ }
350
+ /**
351
+ * Create a heap of the same class from an iterable.
352
+ * @remarks Time O(N), Space O(N)
353
+ * @template T
354
+ * @template R
355
+ * @template S
356
+ * @param [elements] - Iterable of elements or raw records.
357
+ * @param [options] - Heap options including comparator.
358
+ * @returns A new heap instance of this class.
359
+ */
360
+ static from(elements, options) {
361
+ return new this(elements, options);
362
+ }
363
+ /**
364
+ * Build a Heap from an iterable in linear time given a comparator.
365
+ * @remarks Time O(N), Space O(N)
366
+ * @template EE
367
+ * @template RR
368
+ * @param elements - Iterable of elements.
369
+ * @param options - Heap options including comparator.
370
+ * @returns A new Heap built from elements.
371
+ */
372
+ static heapify(elements, options) {
373
+ return new _Heap(elements, options);
374
+ }
375
+ /**
376
+ * Insert an element.
377
+ * @remarks Time O(1) amortized, Space O(1)
378
+ * @param element - Element to insert.
379
+ * @returns True.
380
+
381
+
382
+
383
+
384
+
385
+
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
+ * @example
416
+ * // basic Heap creation and add operation
417
+ * // Create a min heap (default)
418
+ * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
419
+ *
420
+ * // Verify size
421
+ * console.log(minHeap.size); // 6;
422
+ *
423
+ * // Add new element
424
+ * minHeap.add(4);
425
+ * console.log(minHeap.size); // 7;
426
+ *
427
+ * // Min heap property: smallest element at root
428
+ * const min = minHeap.peek();
429
+ * console.log(min); // 1;
430
+ */
431
+ add(element) {
432
+ this._elements.push(element);
433
+ return this._bubbleUp(this.elements.length - 1);
434
+ }
435
+ /**
436
+ * Insert many elements from an iterable.
437
+ * @remarks Time O(N log N), Space O(1)
438
+ * @param elements - Iterable of elements or raw values.
439
+ * @returns Array of per-element success flags.
440
+
441
+
442
+
443
+
444
+
445
+
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
+ * @example
473
+ * // Add multiple elements
474
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
475
+ * heap.addMany([5, 3, 7, 1]);
476
+ * console.log(heap.peek()); // 1;
477
+ * console.log(heap.size); // 4;
478
+ */
479
+ addMany(elements) {
480
+ const flags = [];
481
+ for (const el of elements) {
482
+ if (this.toElementFn) {
483
+ const ok = this.add(this.toElementFn(el));
484
+ flags.push(ok);
485
+ } else {
486
+ const ok = this.add(el);
487
+ flags.push(ok);
488
+ }
489
+ }
490
+ return flags;
491
+ }
492
+ /**
493
+ * Remove and return the top element.
494
+ * @remarks Time O(log N), Space O(1)
495
+ * @returns Top element or undefined.
496
+
497
+
498
+
499
+
500
+
501
+
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
+ * @example
532
+ * // Heap with custom comparator (MaxHeap behavior)
533
+ * interface Task {
534
+ * id: number;
535
+ * priority: number;
536
+ * name: string;
537
+ * }
538
+ *
539
+ * // Custom comparator for max heap behavior (higher priority first)
540
+ * const tasks: Task[] = [
541
+ * { id: 1, priority: 5, name: 'Email' },
542
+ * { id: 2, priority: 3, name: 'Chat' },
543
+ * { id: 3, priority: 8, name: 'Alert' }
544
+ * ];
545
+ *
546
+ * const maxHeap = new Heap(tasks, {
547
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
548
+ * });
549
+ *
550
+ * console.log(maxHeap.size); // 3;
551
+ *
552
+ * // Peek returns highest priority task
553
+ * const topTask = maxHeap.peek();
554
+ * console.log(topTask?.priority); // 8;
555
+ * console.log(topTask?.name); // 'Alert';
556
+ */
557
+ poll() {
558
+ if (this.elements.length === 0) return;
559
+ const value = this.elements[0];
560
+ const last = this.elements.pop();
561
+ if (this.elements.length) {
562
+ this.elements[0] = last;
563
+ this._sinkDown(0, this.elements.length >> 1);
564
+ }
565
+ return value;
566
+ }
567
+ /**
568
+ * Get the current top element without removing it.
569
+ * @remarks Time O(1), Space O(1)
570
+ * @returns Top element or undefined.
571
+
572
+
573
+
574
+
575
+
576
+
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
+ * @example
607
+ * // Heap for event processing with priority
608
+ * interface Event {
609
+ * id: number;
610
+ * type: 'critical' | 'warning' | 'info';
611
+ * timestamp: number;
612
+ * message: string;
613
+ * }
614
+ *
615
+ * // Custom priority: critical > warning > info
616
+ * const priorityMap = { critical: 3, warning: 2, info: 1 };
617
+ *
618
+ * const eventHeap = new Heap<Event>([], {
619
+ * comparator: (a: Event, b: Event) => {
620
+ * const priorityA = priorityMap[a.type];
621
+ * const priorityB = priorityMap[b.type];
622
+ * return priorityB - priorityA; // Higher priority first
623
+ * }
624
+ * });
625
+ *
626
+ * // Add events in random order
627
+ * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
628
+ * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
629
+ * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
630
+ * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
631
+ * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
632
+ *
633
+ * console.log(eventHeap.size); // 5;
634
+ *
635
+ * // Process events by priority (critical first)
636
+ * const processedOrder: Event[] = [];
637
+ * while (eventHeap.size > 0) {
638
+ * const event = eventHeap.poll();
639
+ * if (event) {
640
+ * processedOrder.push(event);
641
+ * }
642
+ * }
643
+ *
644
+ * // Verify critical events came first
645
+ * console.log(processedOrder[0].type); // 'critical';
646
+ * console.log(processedOrder[1].type); // 'critical';
647
+ * console.log(processedOrder[2].type); // 'warning';
648
+ * console.log(processedOrder[3].type); // 'info';
649
+ * console.log(processedOrder[4].type); // 'info';
650
+ *
651
+ * // Verify O(log n) operations
652
+ * const newHeap = new Heap<number>([5, 3, 7, 1]);
653
+ *
654
+ * // Add - O(log n)
655
+ * newHeap.add(2);
656
+ * console.log(newHeap.size); // 5;
657
+ *
658
+ * // Poll - O(log n)
659
+ * const removed = newHeap.poll();
660
+ * console.log(removed); // 1;
661
+ *
662
+ * // Peek - O(1)
663
+ * const top = newHeap.peek();
664
+ * console.log(top); // 2;
665
+ */
666
+ peek() {
667
+ return this.elements[0];
668
+ }
669
+ /**
670
+ * Check whether the heap is empty.
671
+ * @remarks Time O(1), Space O(1)
672
+ * @returns True if size is 0.
673
+
674
+
675
+
676
+
677
+
678
+
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
+ * @example
707
+ * // Check if heap is empty
708
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
709
+ * console.log(heap.isEmpty()); // true;
710
+ * heap.add(1);
711
+ * console.log(heap.isEmpty()); // false;
712
+ */
713
+ isEmpty() {
714
+ return this.size === 0;
715
+ }
716
+ /**
717
+ * Remove all elements.
718
+ * @remarks Time O(1), Space O(1)
719
+ * @returns void
720
+
721
+
722
+
723
+
724
+
725
+
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
+ * @example
754
+ * // Remove all elements
755
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
756
+ * heap.clear();
757
+ * console.log(heap.isEmpty()); // true;
758
+ */
759
+ clear() {
760
+ this._elements = [];
761
+ }
762
+ /**
763
+ * Replace the backing array and rebuild the heap.
764
+ * @remarks Time O(N), Space O(N)
765
+ * @param elements - Iterable used to refill the heap.
766
+ * @returns Array of per-node results from fixing steps.
767
+ */
768
+ refill(elements) {
769
+ this._elements = Array.from(elements);
770
+ return this.fix();
771
+ }
772
+ /**
773
+ * Check if an equal element exists in the heap.
774
+ * @remarks Time O(N), Space O(1)
775
+ * @param element - Element to search for.
776
+ * @returns True if found.
777
+
778
+
779
+
780
+
781
+
782
+
783
+
784
+
785
+
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
803
+ * @example
804
+ * // Check element existence
805
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
806
+ * console.log(heap.has(1)); // true;
807
+ * console.log(heap.has(99)); // false;
808
+ */
809
+ has(element) {
810
+ for (const el of this.elements) if (this._equals(el, element)) return true;
811
+ return false;
812
+ }
813
+ /**
814
+ * Delete one occurrence of an element.
815
+ * @remarks Time O(N), Space O(1)
816
+ * @param element - Element to delete.
817
+ * @returns True if an element was removed.
818
+
819
+
820
+
821
+
822
+
823
+
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
+ * @example
851
+ * // Remove specific element
852
+ * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
853
+ * heap.delete(4);
854
+ * console.log(heap.toArray().includes(4)); // false;
855
+ */
856
+ delete(element) {
857
+ let index = -1;
858
+ for (let i = 0; i < this.elements.length; i++) {
859
+ if (this._equals(this.elements[i], element)) {
860
+ index = i;
861
+ break;
862
+ }
863
+ }
864
+ if (index < 0) return false;
865
+ if (index === 0) {
866
+ this.poll();
867
+ } else if (index === this.elements.length - 1) {
868
+ this.elements.pop();
869
+ } else {
870
+ this.elements.splice(index, 1, this.elements.pop());
871
+ this._bubbleUp(index);
872
+ this._sinkDown(index, this.elements.length >> 1);
873
+ }
874
+ return true;
875
+ }
876
+ /**
877
+ * Delete the first element that matches a predicate.
878
+ * @remarks Time O(N), Space O(1)
879
+ * @param predicate - Function (element, index, heap) → boolean.
880
+ * @returns True if an element was removed.
881
+ */
882
+ deleteBy(predicate) {
883
+ let idx = -1;
884
+ for (let i = 0; i < this.elements.length; i++) {
885
+ if (predicate(this.elements[i], i, this)) {
886
+ idx = i;
887
+ break;
888
+ }
889
+ }
890
+ if (idx < 0) return false;
891
+ if (idx === 0) {
892
+ this.poll();
893
+ } else if (idx === this.elements.length - 1) {
894
+ this.elements.pop();
895
+ } else {
896
+ this.elements.splice(idx, 1, this.elements.pop());
897
+ this._bubbleUp(idx);
898
+ this._sinkDown(idx, this.elements.length >> 1);
899
+ }
900
+ return true;
901
+ }
902
+ /**
903
+ * Set the equality comparator used by has/delete operations.
904
+ * @remarks Time O(1), Space O(1)
905
+ * @param equals - Equality predicate (a, b) → boolean.
906
+ * @returns This heap.
907
+ */
908
+ setEquality(equals) {
909
+ this._equals = equals;
910
+ return this;
911
+ }
912
+ /**
913
+ * Traverse the binary heap as a complete binary tree and collect elements.
914
+ * @remarks Time O(N), Space O(H)
915
+ * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
916
+ * @returns Array of visited elements.
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+
937
+
938
+
939
+
940
+
941
+
942
+
943
+ * @example
944
+ * // Depth-first traversal
945
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
946
+ * const result = heap.dfs('IN');
947
+ * console.log(result.length); // 3;
948
+ */
949
+ dfs(order = "PRE") {
950
+ const result = [];
951
+ const _dfs = /* @__PURE__ */ __name((index) => {
952
+ const left = 2 * index + 1, right = left + 1;
953
+ if (index < this.size) {
954
+ if (order === "IN") {
955
+ _dfs(left);
956
+ result.push(this.elements[index]);
957
+ _dfs(right);
958
+ } else if (order === "PRE") {
959
+ result.push(this.elements[index]);
960
+ _dfs(left);
961
+ _dfs(right);
962
+ } else if (order === "POST") {
963
+ _dfs(left);
964
+ _dfs(right);
965
+ result.push(this.elements[index]);
966
+ }
967
+ }
968
+ }, "_dfs");
969
+ _dfs(0);
970
+ return result;
971
+ }
972
+ /**
973
+ * Restore heap order bottom-up (heapify in-place).
974
+ * @remarks Time O(N), Space O(1)
975
+ * @returns Array of per-node results from fixing steps.
976
+ */
977
+ fix() {
978
+ const results = [];
979
+ for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
980
+ results.push(this._sinkDown(i, this.elements.length >> 1));
981
+ }
982
+ return results;
983
+ }
984
+ /**
985
+ * Return all elements in ascending order by repeatedly polling.
986
+ * @remarks Time O(N log N), Space O(N)
987
+ * @returns Sorted array of elements.
988
+
989
+
990
+
991
+
992
+
993
+
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
+ * @example
1024
+ * // Sort elements using heap
1025
+ * const heap = new Heap<number>([5, 1, 3, 2, 4]);
1026
+ * const sorted = heap.sort();
1027
+ * console.log(sorted); // [1, 2, 3, 4, 5];
1028
+ */
1029
+ sort() {
1030
+ const visited = [];
1031
+ const cloned = this._createInstance();
1032
+ for (const x of this.elements) cloned.add(x);
1033
+ while (!cloned.isEmpty()) {
1034
+ const top = cloned.poll();
1035
+ if (top !== void 0) visited.push(top);
1036
+ }
1037
+ return visited;
1038
+ }
1039
+ /**
1040
+ * Deep clone this heap.
1041
+ * @remarks Time O(N), Space O(N)
1042
+ * @returns A new heap with the same elements.
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
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
+ * @example
1077
+ * // Create independent copy
1078
+ * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
1079
+ * const copy = heap.clone();
1080
+ * copy.poll();
1081
+ * console.log(heap.size); // 3;
1082
+ * console.log(copy.size); // 2;
1083
+ */
1084
+ clone() {
1085
+ const next = this._createInstance();
1086
+ for (const x of this.elements) next.add(x);
1087
+ return next;
1088
+ }
1089
+ /**
1090
+ * Filter elements into a new heap of the same class.
1091
+ * @remarks Time O(N log N), Space O(N)
1092
+ * @param callback - Predicate (element, index, heap) → boolean to keep element.
1093
+ * @param [thisArg] - Value for `this` inside the callback.
1094
+ * @returns A new heap with the kept elements.
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
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
+ * @example
1129
+ * // Filter elements
1130
+ * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
1131
+ * const evens = heap.filter(x => x % 2 === 0);
1132
+ * console.log(evens.size); // 2;
1133
+ */
1134
+ filter(callback, thisArg) {
1135
+ const out = this._createInstance();
1136
+ let i = 0;
1137
+ for (const x of this) {
1138
+ if (thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this)) {
1139
+ out.add(x);
1140
+ } else {
1141
+ i++;
1142
+ }
1143
+ }
1144
+ return out;
1145
+ }
1146
+ /**
1147
+ * Map elements into a new heap of possibly different element type.
1148
+ * @remarks Time O(N log N), Space O(N)
1149
+ * @template EM
1150
+ * @template RM
1151
+ * @param callback - Mapping function (element, index, heap) → newElement.
1152
+ * @param options - Options for the output heap, including comparator for EM.
1153
+ * @param [thisArg] - Value for `this` inside the callback.
1154
+ * @returns A new heap with mapped elements.
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
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
+ * @example
1188
+ * // Transform elements
1189
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
1190
+ * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
1191
+ * console.log(doubled.peek()); // 2;
1192
+ */
1193
+ map(callback, options, thisArg) {
1194
+ const { comparator, toElementFn, ...rest } = options ?? {};
1195
+ if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
1196
+ const out = this._createLike([], { ...rest, comparator, toElementFn });
1197
+ let i = 0;
1198
+ for (const x of this) {
1199
+ const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
1200
+ out.add(v);
1201
+ }
1202
+ return out;
1203
+ }
1204
+ /**
1205
+ * Map elements into a new heap of the same element type.
1206
+ * @remarks Time O(N log N), Space O(N)
1207
+ * @param callback - Mapping function (element, index, heap) → element.
1208
+ * @param [thisArg] - Value for `this` inside the callback.
1209
+ * @returns A new heap with mapped elements.
1210
+ */
1211
+ mapSame(callback, thisArg) {
1212
+ const out = this._createInstance();
1213
+ let i = 0;
1214
+ for (const x of this) {
1215
+ const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
1216
+ out.add(v);
1217
+ }
1218
+ return out;
1219
+ }
1220
+ _DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
1221
+ if (typeof a === "object" || typeof b === "object") {
1222
+ raise(TypeError, ERR.comparatorRequired("Heap"));
1223
+ }
1224
+ if (a > b) return 1;
1225
+ if (a < b) return -1;
1226
+ return 0;
1227
+ }, "_DEFAULT_COMPARATOR");
1228
+ _comparator = this._DEFAULT_COMPARATOR;
1229
+ /**
1230
+ * Get the comparator used to order elements.
1231
+ * @remarks Time O(1), Space O(1)
1232
+ * @returns Comparator function.
1233
+ */
1234
+ get comparator() {
1235
+ return this._comparator;
1236
+ }
1237
+ *_getIterator() {
1238
+ for (const element of this.elements) yield element;
1239
+ }
1240
+ _bubbleUp(index) {
1241
+ const element = this.elements[index];
1242
+ while (index > 0) {
1243
+ const parent = index - 1 >> 1;
1244
+ const parentItem = this.elements[parent];
1245
+ if (this.comparator(parentItem, element) <= 0) break;
1246
+ this.elements[index] = parentItem;
1247
+ index = parent;
1248
+ }
1249
+ this.elements[index] = element;
1250
+ return true;
1251
+ }
1252
+ _sinkDown(index, halfLength) {
1253
+ const element = this.elements[index];
1254
+ while (index < halfLength) {
1255
+ let left = index << 1 | 1;
1256
+ const right = left + 1;
1257
+ let minItem = this.elements[left];
1258
+ if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
1259
+ left = right;
1260
+ minItem = this.elements[right];
1261
+ }
1262
+ if (this.comparator(minItem, element) >= 0) break;
1263
+ this.elements[index] = minItem;
1264
+ index = left;
1265
+ }
1266
+ this.elements[index] = element;
1267
+ return true;
1268
+ }
1269
+ /**
1270
+ * (Protected) Create an empty instance of the same concrete class.
1271
+ * @remarks Time O(1), Space O(1)
1272
+ * @param [options] - Options to override comparator or toElementFn.
1273
+ * @returns A like-kind empty heap instance.
1274
+ */
1275
+ _createInstance(options) {
1276
+ const Ctor = this.constructor;
1277
+ return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options ?? {} });
1278
+ }
1279
+ /**
1280
+ * (Protected) Create a like-kind instance seeded by elements.
1281
+ * @remarks Time O(N log N), Space O(N)
1282
+ * @template EM
1283
+ * @template RM
1284
+ * @param [elements] - Iterable of elements or raw values to seed.
1285
+ * @param [options] - Options forwarded to the constructor.
1286
+ * @returns A like-kind heap instance.
1287
+ */
1288
+ _createLike(elements = [], options) {
1289
+ const Ctor = this.constructor;
1290
+ return new Ctor(elements, options);
1291
+ }
1292
+ /**
1293
+ * (Protected) Spawn an empty like-kind heap instance.
1294
+ * @remarks Time O(1), Space O(1)
1295
+ * @template EM
1296
+ * @template RM
1297
+ * @param [options] - Options forwarded to the constructor.
1298
+ * @returns An empty like-kind heap instance.
1299
+ */
1300
+ _spawnLike(options) {
1301
+ return this._createLike([], options);
1302
+ }
1303
+ };
1304
+
1305
+ // src/data-structures/priority-queue/priority-queue.ts
1306
+ var PriorityQueue = class extends Heap {
1307
+ static {
1308
+ __name(this, "PriorityQueue");
1309
+ }
1310
+ constructor(elements = [], options) {
1311
+ super(elements, options);
1312
+ }
1313
+ };
1314
+
1315
+ // src/data-structures/priority-queue/min-priority-queue.ts
1316
+ var MinPriorityQueue = class extends PriorityQueue {
1317
+ static {
1318
+ __name(this, "MinPriorityQueue");
1319
+ }
1320
+ /**
1321
+ * Creates a min-priority queue.
1322
+ * @param elements Optional initial elements to insert.
1323
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
1324
+ * @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
1325
+ */
1326
+ constructor(elements = [], options) {
1327
+ super(elements, options);
1328
+ }
1329
+ };
1330
+
1331
+ // src/data-structures/priority-queue/max-priority-queue.ts
1332
+ var MaxPriorityQueue = class extends PriorityQueue {
1333
+ static {
1334
+ __name(this, "MaxPriorityQueue");
1335
+ }
1336
+ /**
1337
+ * Creates a max-priority queue.
1338
+ * @param elements Optional initial elements to insert.
1339
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
1340
+ * @throws {TypeError} Thrown when using the default comparator with object elements (provide a custom comparator).
1341
+ * @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
1342
+ */
1343
+ constructor(elements = [], options) {
1344
+ super(elements, {
1345
+ comparator: /* @__PURE__ */ __name((a, b) => {
1346
+ if (typeof a === "object" || typeof b === "object") {
1347
+ raise(TypeError, ERR.comparatorRequired("MaxPriorityQueue"));
1348
+ }
1349
+ if (a < b) return 1;
1350
+ if (a > b) return -1;
1351
+ return 0;
1352
+ }, "comparator"),
1353
+ ...options
1354
+ });
1355
+ }
1356
+ };
1357
+ /**
1358
+ * data-structure-typed
1359
+ *
1360
+ * @author Pablo Zeng
1361
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1362
+ * @license MIT License
1363
+ */
1364
+ /**
1365
+ * data-structure-typed
1366
+ *
1367
+ * @author Kirk Qi
1368
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
1369
+ * @license MIT License
1370
+ */
1371
+
1372
+ exports.MaxPriorityQueue = MaxPriorityQueue;
1373
+ exports.MinPriorityQueue = MinPriorityQueue;
1374
+ exports.PriorityQueue = PriorityQueue;
1375
+ //# sourceMappingURL=priority-queue.cjs.map
1376
+ //# sourceMappingURL=priority-queue.cjs.map