data-structure-typed 2.4.5 → 2.5.1

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 (240) 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 +3 -1
  7. package/README.md +78 -31
  8. package/dist/cjs/binary-tree.cjs +23698 -0
  9. package/dist/cjs/graph.cjs +5236 -0
  10. package/dist/cjs/hash.cjs +1262 -0
  11. package/dist/cjs/heap.cjs +1540 -0
  12. package/dist/cjs/index.cjs +24509 -2899
  13. package/dist/cjs/linked-list.cjs +4370 -0
  14. package/dist/cjs/matrix.cjs +1042 -0
  15. package/dist/cjs/priority-queue.cjs +1314 -0
  16. package/dist/cjs/queue.cjs +4090 -0
  17. package/dist/cjs/stack.cjs +861 -0
  18. package/dist/cjs/trie.cjs +1173 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +23730 -0
  20. package/dist/cjs-legacy/graph.cjs +5234 -0
  21. package/dist/cjs-legacy/hash.cjs +1262 -0
  22. package/dist/cjs-legacy/heap.cjs +1537 -0
  23. package/dist/cjs-legacy/index.cjs +32555 -10936
  24. package/dist/cjs-legacy/linked-list.cjs +4376 -0
  25. package/dist/cjs-legacy/matrix.cjs +1045 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1312 -0
  27. package/dist/cjs-legacy/queue.cjs +4088 -0
  28. package/dist/cjs-legacy/stack.cjs +861 -0
  29. package/dist/cjs-legacy/trie.cjs +1172 -0
  30. package/dist/esm/binary-tree.mjs +23683 -0
  31. package/dist/esm/graph.mjs +5223 -0
  32. package/dist/esm/hash.mjs +1259 -0
  33. package/dist/esm/heap.mjs +1534 -0
  34. package/dist/esm/index.mjs +24507 -2898
  35. package/dist/esm/linked-list.mjs +4363 -0
  36. package/dist/esm/matrix.mjs +1038 -0
  37. package/dist/esm/priority-queue.mjs +1310 -0
  38. package/dist/esm/queue.mjs +4086 -0
  39. package/dist/esm/stack.mjs +859 -0
  40. package/dist/esm/trie.mjs +1170 -0
  41. package/dist/esm-legacy/binary-tree.mjs +23715 -0
  42. package/dist/esm-legacy/graph.mjs +5221 -0
  43. package/dist/esm-legacy/hash.mjs +1259 -0
  44. package/dist/esm-legacy/heap.mjs +1531 -0
  45. package/dist/esm-legacy/index.mjs +32553 -10935
  46. package/dist/esm-legacy/linked-list.mjs +4369 -0
  47. package/dist/esm-legacy/matrix.mjs +1041 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1308 -0
  49. package/dist/esm-legacy/queue.mjs +4084 -0
  50. package/dist/esm-legacy/stack.mjs +859 -0
  51. package/dist/esm-legacy/trie.mjs +1169 -0
  52. package/dist/types/data-structures/base/index.d.ts +1 -0
  53. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  54. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  55. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +368 -51
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +473 -147
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +931 -80
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +792 -29
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +592 -32
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +320 -135
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3662 -6
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3487 -201
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2778 -65
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3414 -6
  66. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  67. package/dist/types/data-structures/graph/directed-graph.d.ts +419 -47
  68. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +384 -59
  70. package/dist/types/data-structures/hash/hash-map.d.ts +462 -89
  71. package/dist/types/data-structures/heap/heap.d.ts +567 -99
  72. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  73. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  74. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +631 -49
  75. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +581 -68
  76. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +775 -12
  77. package/dist/types/data-structures/matrix/matrix.d.ts +491 -0
  78. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  79. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  80. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  81. package/dist/types/data-structures/queue/deque.d.ts +578 -71
  82. package/dist/types/data-structures/queue/queue.d.ts +451 -42
  83. package/dist/types/data-structures/stack/stack.d.ts +374 -32
  84. package/dist/types/data-structures/trie/trie.d.ts +458 -48
  85. package/dist/types/interfaces/graph.d.ts +1 -1
  86. package/dist/types/types/common.d.ts +2 -2
  87. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  88. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  89. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  90. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  91. package/dist/types/types/utils/validate-type.d.ts +4 -4
  92. package/dist/umd/data-structure-typed.js +32432 -10808
  93. package/dist/umd/data-structure-typed.min.js +10 -4
  94. package/docs-site-docusaurus/README.md +41 -0
  95. package/docs-site-docusaurus/docs/api/README.md +52 -0
  96. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
  97. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  98. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  99. package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
  100. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  101. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  102. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  103. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  104. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  105. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  106. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  107. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  108. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  109. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  110. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  111. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  112. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  113. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  116. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  117. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  118. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  119. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  120. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  121. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  122. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  123. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  124. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  125. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  126. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  127. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  128. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
  129. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  130. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  131. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  132. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  133. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  134. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
  135. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
  136. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
  137. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  138. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  139. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  140. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  141. package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
  142. package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
  143. package/docs-site-docusaurus/docs/guide/guides.md +611 -0
  144. package/docs-site-docusaurus/docs/guide/installation.md +60 -0
  145. package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
  146. package/docs-site-docusaurus/docs/guide/overview.md +638 -0
  147. package/docs-site-docusaurus/docs/guide/performance.md +833 -0
  148. package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
  149. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  150. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  151. package/docs-site-docusaurus/package-lock.json +18667 -0
  152. package/docs-site-docusaurus/package.json +50 -0
  153. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  154. package/docs-site-docusaurus/sidebars.ts +23 -0
  155. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  156. package/docs-site-docusaurus/src/css/custom.css +96 -0
  157. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  158. package/docs-site-docusaurus/src/pages/index.tsx +71 -0
  159. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  160. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  161. package/docs-site-docusaurus/static/.nojekyll +0 -0
  162. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  163. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  164. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  165. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  166. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  167. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  168. package/docs-site-docusaurus/static/img/logo.png +0 -0
  169. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  170. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  171. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  172. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  173. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  174. package/docs-site-docusaurus/static/robots.txt +4 -0
  175. package/docs-site-docusaurus/typedoc.json +23 -0
  176. package/package.json +109 -12
  177. package/src/data-structures/base/index.ts +1 -0
  178. package/src/data-structures/base/iterable-element-base.ts +4 -5
  179. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  180. package/src/data-structures/base/linear-base.ts +3 -3
  181. package/src/data-structures/binary-tree/avl-tree.ts +386 -51
  182. package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
  183. package/src/data-structures/binary-tree/binary-tree.ts +956 -81
  184. package/src/data-structures/binary-tree/bst.ts +840 -35
  185. package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
  186. package/src/data-structures/binary-tree/segment-tree.ts +498 -249
  187. package/src/data-structures/binary-tree/tree-map.ts +3784 -7
  188. package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
  189. package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
  190. package/src/data-structures/binary-tree/tree-set.ts +3531 -10
  191. package/src/data-structures/graph/abstract-graph.ts +4 -4
  192. package/src/data-structures/graph/directed-graph.ts +429 -47
  193. package/src/data-structures/graph/map-graph.ts +59 -1
  194. package/src/data-structures/graph/undirected-graph.ts +393 -59
  195. package/src/data-structures/hash/hash-map.ts +476 -92
  196. package/src/data-structures/heap/heap.ts +581 -99
  197. package/src/data-structures/heap/max-heap.ts +46 -0
  198. package/src/data-structures/heap/min-heap.ts +59 -0
  199. package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
  200. package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
  201. package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
  202. package/src/data-structures/matrix/matrix.ts +584 -12
  203. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  204. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  205. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  206. package/src/data-structures/queue/deque.ts +592 -70
  207. package/src/data-structures/queue/queue.ts +463 -42
  208. package/src/data-structures/stack/stack.ts +384 -32
  209. package/src/data-structures/trie/trie.ts +470 -48
  210. package/src/interfaces/graph.ts +1 -1
  211. package/src/types/common.ts +2 -2
  212. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  213. package/src/types/data-structures/heap/heap.ts +1 -0
  214. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  215. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  216. package/src/types/utils/validate-type.ts +4 -4
  217. package/vercel.json +6 -0
  218. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  219. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  220. package/dist/leetcode/avl-tree.mjs +0 -2720
  221. package/dist/leetcode/binary-tree.mjs +0 -1594
  222. package/dist/leetcode/bst.mjs +0 -2398
  223. package/dist/leetcode/deque.mjs +0 -683
  224. package/dist/leetcode/directed-graph.mjs +0 -1733
  225. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  226. package/dist/leetcode/hash-map.mjs +0 -493
  227. package/dist/leetcode/heap.mjs +0 -542
  228. package/dist/leetcode/max-heap.mjs +0 -375
  229. package/dist/leetcode/max-priority-queue.mjs +0 -383
  230. package/dist/leetcode/min-heap.mjs +0 -363
  231. package/dist/leetcode/min-priority-queue.mjs +0 -371
  232. package/dist/leetcode/priority-queue.mjs +0 -363
  233. package/dist/leetcode/queue.mjs +0 -943
  234. package/dist/leetcode/red-black-tree.mjs +0 -2765
  235. package/dist/leetcode/singly-linked-list.mjs +0 -754
  236. package/dist/leetcode/stack.mjs +0 -217
  237. package/dist/leetcode/tree-counter.mjs +0 -3039
  238. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  239. package/dist/leetcode/trie.mjs +0 -413
  240. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,1310 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/data-structures/base/iterable-element-base.ts
5
+ var IterableElementBase = class {
6
+ static {
7
+ __name(this, "IterableElementBase");
8
+ }
9
+ /**
10
+ * Create a new iterable base.
11
+ *
12
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
13
+ * is used to convert a raw element (`R`) into a public element (`E`).
14
+ *
15
+ * @remarks
16
+ * Time O(1), Space O(1).
17
+ */
18
+ constructor(options) {
19
+ if (options) {
20
+ const { toElementFn } = options;
21
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
22
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
23
+ }
24
+ }
25
+ /**
26
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
27
+ *
28
+ * @remarks
29
+ * Time O(1), Space O(1).
30
+ */
31
+ _toElementFn;
32
+ /**
33
+ * Exposes the current `toElementFn`, if configured.
34
+ *
35
+ * @returns The converter function or `undefined` when not set.
36
+ * @remarks
37
+ * Time O(1), Space O(1).
38
+ */
39
+ get toElementFn() {
40
+ return this._toElementFn;
41
+ }
42
+ /**
43
+ * Returns an iterator over the structure's elements.
44
+ *
45
+ * @param args Optional iterator arguments forwarded to the internal iterator.
46
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
47
+ *
48
+ * @remarks
49
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
50
+ */
51
+ *[Symbol.iterator](...args) {
52
+ yield* this._getIterator(...args);
53
+ }
54
+ /**
55
+ * Returns an iterator over the values (alias of the default iterator).
56
+ *
57
+ * @returns An `IterableIterator<E>` over all elements.
58
+ * @remarks
59
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
60
+ */
61
+ *values() {
62
+ for (const item of this) yield item;
63
+ }
64
+ /**
65
+ * Tests whether all elements satisfy the predicate.
66
+ *
67
+ * @template TReturn
68
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
69
+ * @param thisArg Optional `this` binding for the predicate.
70
+ * @returns `true` if every element passes; otherwise `false`.
71
+ *
72
+ * @remarks
73
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
74
+ */
75
+ every(predicate, thisArg) {
76
+ let index = 0;
77
+ for (const item of this) {
78
+ if (thisArg === void 0) {
79
+ if (!predicate(item, index++, this)) return false;
80
+ } else {
81
+ const fn = predicate;
82
+ if (!fn.call(thisArg, item, index++, this)) return false;
83
+ }
84
+ }
85
+ return true;
86
+ }
87
+ /**
88
+ * Tests whether at least one element satisfies the predicate.
89
+ *
90
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
91
+ * @param thisArg Optional `this` binding for the predicate.
92
+ * @returns `true` if any element passes; otherwise `false`.
93
+ *
94
+ * @remarks
95
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
96
+ */
97
+ some(predicate, thisArg) {
98
+ let index = 0;
99
+ for (const item of this) {
100
+ if (thisArg === void 0) {
101
+ if (predicate(item, index++, this)) return true;
102
+ } else {
103
+ const fn = predicate;
104
+ if (fn.call(thisArg, item, index++, this)) return true;
105
+ }
106
+ }
107
+ return false;
108
+ }
109
+ /**
110
+ * Invokes a callback for each element in iteration order.
111
+ *
112
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
113
+ * @param thisArg Optional `this` binding for the callback.
114
+ * @returns `void`.
115
+ *
116
+ * @remarks
117
+ * Time O(n), Space O(1).
118
+ */
119
+ forEach(callbackfn, thisArg) {
120
+ let index = 0;
121
+ for (const item of this) {
122
+ if (thisArg === void 0) {
123
+ callbackfn(item, index++, this);
124
+ } else {
125
+ const fn = callbackfn;
126
+ fn.call(thisArg, item, index++, this);
127
+ }
128
+ }
129
+ }
130
+ // Implementation signature
131
+ find(predicate, thisArg) {
132
+ let index = 0;
133
+ for (const item of this) {
134
+ if (thisArg === void 0) {
135
+ if (predicate(item, index++, this)) return item;
136
+ } else {
137
+ const fn = predicate;
138
+ if (fn.call(thisArg, item, index++, this)) return item;
139
+ }
140
+ }
141
+ return;
142
+ }
143
+ /**
144
+ * Checks whether a strictly-equal element exists in the structure.
145
+ *
146
+ * @param element The element to test with `===` equality.
147
+ * @returns `true` if an equal element is found; otherwise `false`.
148
+ *
149
+ * @remarks
150
+ * Time O(n) in the worst case. Space O(1).
151
+ */
152
+ has(element) {
153
+ for (const ele of this) if (ele === element) return true;
154
+ return false;
155
+ }
156
+ /**
157
+ * Reduces all elements to a single accumulated value.
158
+ *
159
+ * @overload
160
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
161
+ * @returns The final accumulated value typed as `E`.
162
+ *
163
+ * @overload
164
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
165
+ * @param initialValue The initial accumulator value of type `E`.
166
+ * @returns The final accumulated value typed as `E`.
167
+ *
168
+ * @overload
169
+ * @template U The accumulator type when it differs from `E`.
170
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
171
+ * @param initialValue The initial accumulator value of type `U`.
172
+ * @returns The final accumulated value typed as `U`.
173
+ *
174
+ * @remarks
175
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
176
+ */
177
+ reduce(callbackfn, initialValue) {
178
+ let index = 0;
179
+ const iter = this[Symbol.iterator]();
180
+ let acc;
181
+ if (arguments.length >= 2) {
182
+ acc = initialValue;
183
+ } else {
184
+ const first = iter.next();
185
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
186
+ acc = first.value;
187
+ index = 1;
188
+ }
189
+ for (const value of iter) {
190
+ acc = callbackfn(acc, value, index++, this);
191
+ }
192
+ return acc;
193
+ }
194
+ /**
195
+ * Materializes the elements into a new array.
196
+ *
197
+ * @returns A shallow array copy of the iteration order.
198
+ * @remarks
199
+ * Time O(n), Space O(n).
200
+ */
201
+ toArray() {
202
+ return [...this];
203
+ }
204
+ /**
205
+ * Returns a representation of the structure suitable for quick visualization.
206
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
207
+ *
208
+ * @returns A visual representation (array by default).
209
+ * @remarks
210
+ * Time O(n), Space O(n).
211
+ */
212
+ toVisual() {
213
+ return [...this];
214
+ }
215
+ /**
216
+ * Prints `toVisual()` to the console. Intended for quick debugging.
217
+ *
218
+ * @returns `void`.
219
+ * @remarks
220
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
221
+ */
222
+ print() {
223
+ console.log(this.toVisual());
224
+ }
225
+ };
226
+
227
+ // src/common/error.ts
228
+ var ERR = {
229
+ // Range / index
230
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
231
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
232
+ // Type / argument
233
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
234
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
235
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
236
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
237
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
238
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
239
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
240
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
241
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
242
+ // State / operation
243
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
244
+ // Matrix
245
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
246
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
247
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
248
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
249
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
250
+ };
251
+
252
+ // src/data-structures/heap/heap.ts
253
+ var Heap = class _Heap extends IterableElementBase {
254
+ static {
255
+ __name(this, "Heap");
256
+ }
257
+ _equals = Object.is;
258
+ /**
259
+ * Create a Heap and optionally bulk-insert elements.
260
+ * @remarks Time O(N), Space O(N)
261
+ * @param [elements] - Iterable of elements (or raw values if toElementFn is set).
262
+ * @param [options] - Options such as comparator and toElementFn.
263
+ * @returns New Heap instance.
264
+ */
265
+ constructor(elements = [], options) {
266
+ super(options);
267
+ if (options) {
268
+ const { comparator } = options;
269
+ if (comparator) this._comparator = comparator;
270
+ }
271
+ this.addMany(elements);
272
+ }
273
+ _elements = [];
274
+ /**
275
+ * Get the backing array of the heap.
276
+ * @remarks Time O(1), Space O(1)
277
+ * @returns Internal elements array.
278
+ */
279
+ get elements() {
280
+ return this._elements;
281
+ }
282
+ /**
283
+ * Get the number of elements.
284
+ * @remarks Time O(1), Space O(1)
285
+ * @returns Heap size.
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+ * @example
318
+ * // Track heap capacity
319
+ * const heap = new Heap<number>();
320
+ * console.log(heap.size); // 0;
321
+ * heap.add(10);
322
+ * heap.add(20);
323
+ * console.log(heap.size); // 2;
324
+ * heap.poll();
325
+ * console.log(heap.size); // 1;
326
+ */
327
+ get size() {
328
+ return this.elements.length;
329
+ }
330
+ /**
331
+ * Get the last leaf element.
332
+ * @remarks Time O(1), Space O(1)
333
+ * @returns Last element or undefined.
334
+ */
335
+ get leaf() {
336
+ return this.elements[this.size - 1] ?? void 0;
337
+ }
338
+ /**
339
+ * Create a heap of the same class from an iterable.
340
+ * @remarks Time O(N), Space O(N)
341
+ * @template T
342
+ * @template R
343
+ * @template S
344
+ * @param [elements] - Iterable of elements or raw records.
345
+ * @param [options] - Heap options including comparator.
346
+ * @returns A new heap instance of this class.
347
+ */
348
+ static from(elements, options) {
349
+ return new this(elements, options);
350
+ }
351
+ /**
352
+ * Build a Heap from an iterable in linear time given a comparator.
353
+ * @remarks Time O(N), Space O(N)
354
+ * @template EE
355
+ * @template RR
356
+ * @param elements - Iterable of elements.
357
+ * @param options - Heap options including comparator.
358
+ * @returns A new Heap built from elements.
359
+ */
360
+ static heapify(elements, options) {
361
+ return new _Heap(elements, options);
362
+ }
363
+ /**
364
+ * Insert an element.
365
+ * @remarks Time O(1) amortized, Space O(1)
366
+ * @param element - Element to insert.
367
+ * @returns True.
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+ * @example
400
+ * // basic Heap creation and add operation
401
+ * // Create a min heap (default)
402
+ * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
403
+ *
404
+ * // Verify size
405
+ * console.log(minHeap.size); // 6;
406
+ *
407
+ * // Add new element
408
+ * minHeap.add(4);
409
+ * console.log(minHeap.size); // 7;
410
+ *
411
+ * // Min heap property: smallest element at root
412
+ * const min = minHeap.peek();
413
+ * console.log(min); // 1;
414
+ */
415
+ add(element) {
416
+ this._elements.push(element);
417
+ return this._bubbleUp(this.elements.length - 1);
418
+ }
419
+ /**
420
+ * Insert many elements from an iterable.
421
+ * @remarks Time O(N log N), Space O(1)
422
+ * @param elements - Iterable of elements or raw values.
423
+ * @returns Array of per-element success flags.
424
+
425
+
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+ * @example
453
+ * // Add multiple elements
454
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
455
+ * heap.addMany([5, 3, 7, 1]);
456
+ * console.log(heap.peek()); // 1;
457
+ * console.log(heap.size); // 4;
458
+ */
459
+ addMany(elements) {
460
+ const flags = [];
461
+ for (const el of elements) {
462
+ if (this.toElementFn) {
463
+ const ok = this.add(this.toElementFn(el));
464
+ flags.push(ok);
465
+ } else {
466
+ const ok = this.add(el);
467
+ flags.push(ok);
468
+ }
469
+ }
470
+ return flags;
471
+ }
472
+ /**
473
+ * Remove and return the top element.
474
+ * @remarks Time O(log N), Space O(1)
475
+ * @returns Top element or undefined.
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+
488
+
489
+
490
+
491
+
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+
502
+
503
+
504
+
505
+
506
+
507
+ * @example
508
+ * // Heap with custom comparator (MaxHeap behavior)
509
+ * interface Task {
510
+ * id: number;
511
+ * priority: number;
512
+ * name: string;
513
+ * }
514
+ *
515
+ * // Custom comparator for max heap behavior (higher priority first)
516
+ * const tasks: Task[] = [
517
+ * { id: 1, priority: 5, name: 'Email' },
518
+ * { id: 2, priority: 3, name: 'Chat' },
519
+ * { id: 3, priority: 8, name: 'Alert' }
520
+ * ];
521
+ *
522
+ * const maxHeap = new Heap(tasks, {
523
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
524
+ * });
525
+ *
526
+ * console.log(maxHeap.size); // 3;
527
+ *
528
+ * // Peek returns highest priority task
529
+ * const topTask = maxHeap.peek();
530
+ * console.log(topTask?.priority); // 8;
531
+ * console.log(topTask?.name); // 'Alert';
532
+ */
533
+ poll() {
534
+ if (this.elements.length === 0) return;
535
+ const value = this.elements[0];
536
+ const last = this.elements.pop();
537
+ if (this.elements.length) {
538
+ this.elements[0] = last;
539
+ this._sinkDown(0, this.elements.length >> 1);
540
+ }
541
+ return value;
542
+ }
543
+ /**
544
+ * Get the current top element without removing it.
545
+ * @remarks Time O(1), Space O(1)
546
+ * @returns Top element or undefined.
547
+
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+
556
+
557
+
558
+
559
+
560
+
561
+
562
+
563
+
564
+
565
+
566
+
567
+
568
+
569
+
570
+
571
+
572
+
573
+
574
+
575
+
576
+
577
+
578
+ * @example
579
+ * // Heap for event processing with priority
580
+ * interface Event {
581
+ * id: number;
582
+ * type: 'critical' | 'warning' | 'info';
583
+ * timestamp: number;
584
+ * message: string;
585
+ * }
586
+ *
587
+ * // Custom priority: critical > warning > info
588
+ * const priorityMap = { critical: 3, warning: 2, info: 1 };
589
+ *
590
+ * const eventHeap = new Heap<Event>([], {
591
+ * comparator: (a: Event, b: Event) => {
592
+ * const priorityA = priorityMap[a.type];
593
+ * const priorityB = priorityMap[b.type];
594
+ * return priorityB - priorityA; // Higher priority first
595
+ * }
596
+ * });
597
+ *
598
+ * // Add events in random order
599
+ * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
600
+ * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
601
+ * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
602
+ * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
603
+ * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
604
+ *
605
+ * console.log(eventHeap.size); // 5;
606
+ *
607
+ * // Process events by priority (critical first)
608
+ * const processedOrder: Event[] = [];
609
+ * while (eventHeap.size > 0) {
610
+ * const event = eventHeap.poll();
611
+ * if (event) {
612
+ * processedOrder.push(event);
613
+ * }
614
+ * }
615
+ *
616
+ * // Verify critical events came first
617
+ * console.log(processedOrder[0].type); // 'critical';
618
+ * console.log(processedOrder[1].type); // 'critical';
619
+ * console.log(processedOrder[2].type); // 'warning';
620
+ * console.log(processedOrder[3].type); // 'info';
621
+ * console.log(processedOrder[4].type); // 'info';
622
+ *
623
+ * // Verify O(log n) operations
624
+ * const newHeap = new Heap<number>([5, 3, 7, 1]);
625
+ *
626
+ * // Add - O(log n)
627
+ * newHeap.add(2);
628
+ * console.log(newHeap.size); // 5;
629
+ *
630
+ * // Poll - O(log n)
631
+ * const removed = newHeap.poll();
632
+ * console.log(removed); // 1;
633
+ *
634
+ * // Peek - O(1)
635
+ * const top = newHeap.peek();
636
+ * console.log(top); // 2;
637
+ */
638
+ peek() {
639
+ return this.elements[0];
640
+ }
641
+ /**
642
+ * Check whether the heap is empty.
643
+ * @remarks Time O(1), Space O(1)
644
+ * @returns True if size is 0.
645
+
646
+
647
+
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+
656
+
657
+
658
+
659
+
660
+
661
+
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+ * @example
675
+ * // Check if heap is empty
676
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
677
+ * console.log(heap.isEmpty()); // true;
678
+ * heap.add(1);
679
+ * console.log(heap.isEmpty()); // false;
680
+ */
681
+ isEmpty() {
682
+ return this.size === 0;
683
+ }
684
+ /**
685
+ * Remove all elements.
686
+ * @remarks Time O(1), Space O(1)
687
+ * @returns void
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
+
713
+
714
+
715
+
716
+
717
+ * @example
718
+ * // Remove all elements
719
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
720
+ * heap.clear();
721
+ * console.log(heap.isEmpty()); // true;
722
+ */
723
+ clear() {
724
+ this._elements = [];
725
+ }
726
+ /**
727
+ * Replace the backing array and rebuild the heap.
728
+ * @remarks Time O(N), Space O(N)
729
+ * @param elements - Iterable used to refill the heap.
730
+ * @returns Array of per-node results from fixing steps.
731
+ */
732
+ refill(elements) {
733
+ this._elements = Array.from(elements);
734
+ return this.fix();
735
+ }
736
+ /**
737
+ * Check if an equal element exists in the heap.
738
+ * @remarks Time O(N), Space O(1)
739
+ * @param element - Element to search for.
740
+ * @returns True if found.
741
+
742
+
743
+
744
+
745
+
746
+
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+
756
+
757
+
758
+
759
+
760
+
761
+
762
+
763
+ * @example
764
+ * // Check element existence
765
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
766
+ * console.log(heap.has(1)); // true;
767
+ * console.log(heap.has(99)); // false;
768
+ */
769
+ has(element) {
770
+ for (const el of this.elements) if (this._equals(el, element)) return true;
771
+ return false;
772
+ }
773
+ /**
774
+ * Delete one occurrence of an element.
775
+ * @remarks Time O(N), Space O(1)
776
+ * @param element - Element to delete.
777
+ * @returns True if an element was removed.
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
+
804
+
805
+
806
+ * @example
807
+ * // Remove specific element
808
+ * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
809
+ * heap.delete(4);
810
+ * console.log(heap.toArray().includes(4)); // false;
811
+ */
812
+ delete(element) {
813
+ let index = -1;
814
+ for (let i = 0; i < this.elements.length; i++) {
815
+ if (this._equals(this.elements[i], element)) {
816
+ index = i;
817
+ break;
818
+ }
819
+ }
820
+ if (index < 0) return false;
821
+ if (index === 0) {
822
+ this.poll();
823
+ } else if (index === this.elements.length - 1) {
824
+ this.elements.pop();
825
+ } else {
826
+ this.elements.splice(index, 1, this.elements.pop());
827
+ this._bubbleUp(index);
828
+ this._sinkDown(index, this.elements.length >> 1);
829
+ }
830
+ return true;
831
+ }
832
+ /**
833
+ * Delete the first element that matches a predicate.
834
+ * @remarks Time O(N), Space O(1)
835
+ * @param predicate - Function (element, index, heap) → boolean.
836
+ * @returns True if an element was removed.
837
+ */
838
+ deleteBy(predicate) {
839
+ let idx = -1;
840
+ for (let i = 0; i < this.elements.length; i++) {
841
+ if (predicate(this.elements[i], i, this)) {
842
+ idx = i;
843
+ break;
844
+ }
845
+ }
846
+ if (idx < 0) return false;
847
+ if (idx === 0) {
848
+ this.poll();
849
+ } else if (idx === this.elements.length - 1) {
850
+ this.elements.pop();
851
+ } else {
852
+ this.elements.splice(idx, 1, this.elements.pop());
853
+ this._bubbleUp(idx);
854
+ this._sinkDown(idx, this.elements.length >> 1);
855
+ }
856
+ return true;
857
+ }
858
+ /**
859
+ * Set the equality comparator used by has/delete operations.
860
+ * @remarks Time O(1), Space O(1)
861
+ * @param equals - Equality predicate (a, b) → boolean.
862
+ * @returns This heap.
863
+ */
864
+ setEquality(equals) {
865
+ this._equals = equals;
866
+ return this;
867
+ }
868
+ /**
869
+ * Traverse the binary heap as a complete binary tree and collect elements.
870
+ * @remarks Time O(N), Space O(H)
871
+ * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
872
+ * @returns Array of visited elements.
873
+
874
+
875
+
876
+
877
+
878
+
879
+
880
+
881
+
882
+
883
+
884
+
885
+
886
+
887
+
888
+
889
+
890
+
891
+
892
+
893
+
894
+
895
+ * @example
896
+ * // Depth-first traversal
897
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
898
+ * const result = heap.dfs('IN');
899
+ * console.log(result.length); // 3;
900
+ */
901
+ dfs(order = "PRE") {
902
+ const result = [];
903
+ const _dfs = /* @__PURE__ */ __name((index) => {
904
+ const left = 2 * index + 1, right = left + 1;
905
+ if (index < this.size) {
906
+ if (order === "IN") {
907
+ _dfs(left);
908
+ result.push(this.elements[index]);
909
+ _dfs(right);
910
+ } else if (order === "PRE") {
911
+ result.push(this.elements[index]);
912
+ _dfs(left);
913
+ _dfs(right);
914
+ } else if (order === "POST") {
915
+ _dfs(left);
916
+ _dfs(right);
917
+ result.push(this.elements[index]);
918
+ }
919
+ }
920
+ }, "_dfs");
921
+ _dfs(0);
922
+ return result;
923
+ }
924
+ /**
925
+ * Restore heap order bottom-up (heapify in-place).
926
+ * @remarks Time O(N), Space O(1)
927
+ * @returns Array of per-node results from fixing steps.
928
+ */
929
+ fix() {
930
+ const results = [];
931
+ for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
932
+ results.push(this._sinkDown(i, this.elements.length >> 1));
933
+ }
934
+ return results;
935
+ }
936
+ /**
937
+ * Return all elements in ascending order by repeatedly polling.
938
+ * @remarks Time O(N log N), Space O(N)
939
+ * @returns Sorted array of elements.
940
+
941
+
942
+
943
+
944
+
945
+
946
+
947
+
948
+
949
+
950
+
951
+
952
+
953
+
954
+
955
+
956
+
957
+
958
+
959
+
960
+
961
+
962
+
963
+
964
+
965
+
966
+
967
+
968
+
969
+
970
+
971
+ * @example
972
+ * // Sort elements using heap
973
+ * const heap = new Heap<number>([5, 1, 3, 2, 4]);
974
+ * const sorted = heap.sort();
975
+ * console.log(sorted); // [1, 2, 3, 4, 5];
976
+ */
977
+ sort() {
978
+ const visited = [];
979
+ const cloned = this._createInstance();
980
+ for (const x of this.elements) cloned.add(x);
981
+ while (!cloned.isEmpty()) {
982
+ const top = cloned.poll();
983
+ if (top !== void 0) visited.push(top);
984
+ }
985
+ return visited;
986
+ }
987
+ /**
988
+ * Deep clone this heap.
989
+ * @remarks Time O(N), Space O(N)
990
+ * @returns A new heap with the same elements.
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
+ * @example
1021
+ * // Create independent copy
1022
+ * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
1023
+ * const copy = heap.clone();
1024
+ * copy.poll();
1025
+ * console.log(heap.size); // 3;
1026
+ * console.log(copy.size); // 2;
1027
+ */
1028
+ clone() {
1029
+ const next = this._createInstance();
1030
+ for (const x of this.elements) next.add(x);
1031
+ return next;
1032
+ }
1033
+ /**
1034
+ * Filter elements into a new heap of the same class.
1035
+ * @remarks Time O(N log N), Space O(N)
1036
+ * @param callback - Predicate (element, index, heap) → boolean to keep element.
1037
+ * @param [thisArg] - Value for `this` inside the callback.
1038
+ * @returns A new heap with the kept elements.
1039
+
1040
+
1041
+
1042
+
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
+ * @example
1069
+ * // Filter elements
1070
+ * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
1071
+ * const evens = heap.filter(x => x % 2 === 0);
1072
+ * console.log(evens.size); // 2;
1073
+ */
1074
+ filter(callback, thisArg) {
1075
+ const out = this._createInstance();
1076
+ let i = 0;
1077
+ for (const x of this) {
1078
+ if (thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this)) {
1079
+ out.add(x);
1080
+ } else {
1081
+ i++;
1082
+ }
1083
+ }
1084
+ return out;
1085
+ }
1086
+ /**
1087
+ * Map elements into a new heap of possibly different element type.
1088
+ * @remarks Time O(N log N), Space O(N)
1089
+ * @template EM
1090
+ * @template RM
1091
+ * @param callback - Mapping function (element, index, heap) → newElement.
1092
+ * @param options - Options for the output heap, including comparator for EM.
1093
+ * @param [thisArg] - Value for `this` inside the callback.
1094
+ * @returns A new heap with mapped 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
+ * @example
1124
+ * // Transform elements
1125
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
1126
+ * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
1127
+ * console.log(doubled.peek()); // 2;
1128
+ */
1129
+ map(callback, options, thisArg) {
1130
+ const { comparator, toElementFn, ...rest } = options ?? {};
1131
+ if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
1132
+ const out = this._createLike([], { ...rest, comparator, toElementFn });
1133
+ let i = 0;
1134
+ for (const x of this) {
1135
+ const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
1136
+ out.add(v);
1137
+ }
1138
+ return out;
1139
+ }
1140
+ /**
1141
+ * Map elements into a new heap of the same element type.
1142
+ * @remarks Time O(N log N), Space O(N)
1143
+ * @param callback - Mapping function (element, index, heap) → element.
1144
+ * @param [thisArg] - Value for `this` inside the callback.
1145
+ * @returns A new heap with mapped elements.
1146
+ */
1147
+ mapSame(callback, thisArg) {
1148
+ const out = this._createInstance();
1149
+ let i = 0;
1150
+ for (const x of this) {
1151
+ const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
1152
+ out.add(v);
1153
+ }
1154
+ return out;
1155
+ }
1156
+ _DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
1157
+ if (typeof a === "object" || typeof b === "object") {
1158
+ throw new TypeError(ERR.comparatorRequired("Heap"));
1159
+ }
1160
+ if (a > b) return 1;
1161
+ if (a < b) return -1;
1162
+ return 0;
1163
+ }, "_DEFAULT_COMPARATOR");
1164
+ _comparator = this._DEFAULT_COMPARATOR;
1165
+ /**
1166
+ * Get the comparator used to order elements.
1167
+ * @remarks Time O(1), Space O(1)
1168
+ * @returns Comparator function.
1169
+ */
1170
+ get comparator() {
1171
+ return this._comparator;
1172
+ }
1173
+ *_getIterator() {
1174
+ for (const element of this.elements) yield element;
1175
+ }
1176
+ _bubbleUp(index) {
1177
+ const element = this.elements[index];
1178
+ while (index > 0) {
1179
+ const parent = index - 1 >> 1;
1180
+ const parentItem = this.elements[parent];
1181
+ if (this.comparator(parentItem, element) <= 0) break;
1182
+ this.elements[index] = parentItem;
1183
+ index = parent;
1184
+ }
1185
+ this.elements[index] = element;
1186
+ return true;
1187
+ }
1188
+ _sinkDown(index, halfLength) {
1189
+ const element = this.elements[index];
1190
+ while (index < halfLength) {
1191
+ let left = index << 1 | 1;
1192
+ const right = left + 1;
1193
+ let minItem = this.elements[left];
1194
+ if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
1195
+ left = right;
1196
+ minItem = this.elements[right];
1197
+ }
1198
+ if (this.comparator(minItem, element) >= 0) break;
1199
+ this.elements[index] = minItem;
1200
+ index = left;
1201
+ }
1202
+ this.elements[index] = element;
1203
+ return true;
1204
+ }
1205
+ /**
1206
+ * (Protected) Create an empty instance of the same concrete class.
1207
+ * @remarks Time O(1), Space O(1)
1208
+ * @param [options] - Options to override comparator or toElementFn.
1209
+ * @returns A like-kind empty heap instance.
1210
+ */
1211
+ _createInstance(options) {
1212
+ const Ctor = this.constructor;
1213
+ return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options ?? {} });
1214
+ }
1215
+ /**
1216
+ * (Protected) Create a like-kind instance seeded by elements.
1217
+ * @remarks Time O(N log N), Space O(N)
1218
+ * @template EM
1219
+ * @template RM
1220
+ * @param [elements] - Iterable of elements or raw values to seed.
1221
+ * @param [options] - Options forwarded to the constructor.
1222
+ * @returns A like-kind heap instance.
1223
+ */
1224
+ _createLike(elements = [], options) {
1225
+ const Ctor = this.constructor;
1226
+ return new Ctor(elements, options);
1227
+ }
1228
+ /**
1229
+ * (Protected) Spawn an empty like-kind heap instance.
1230
+ * @remarks Time O(1), Space O(1)
1231
+ * @template EM
1232
+ * @template RM
1233
+ * @param [options] - Options forwarded to the constructor.
1234
+ * @returns An empty like-kind heap instance.
1235
+ */
1236
+ _spawnLike(options) {
1237
+ return this._createLike([], options);
1238
+ }
1239
+ };
1240
+
1241
+ // src/data-structures/priority-queue/priority-queue.ts
1242
+ var PriorityQueue = class extends Heap {
1243
+ static {
1244
+ __name(this, "PriorityQueue");
1245
+ }
1246
+ constructor(elements = [], options) {
1247
+ super(elements, options);
1248
+ }
1249
+ };
1250
+
1251
+ // src/data-structures/priority-queue/min-priority-queue.ts
1252
+ var MinPriorityQueue = class extends PriorityQueue {
1253
+ static {
1254
+ __name(this, "MinPriorityQueue");
1255
+ }
1256
+ /**
1257
+ * Creates a min-priority queue.
1258
+ * @param elements Optional initial elements to insert.
1259
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
1260
+ * @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
1261
+ */
1262
+ constructor(elements = [], options) {
1263
+ super(elements, options);
1264
+ }
1265
+ };
1266
+
1267
+ // src/data-structures/priority-queue/max-priority-queue.ts
1268
+ var MaxPriorityQueue = class extends PriorityQueue {
1269
+ static {
1270
+ __name(this, "MaxPriorityQueue");
1271
+ }
1272
+ /**
1273
+ * Creates a max-priority queue.
1274
+ * @param elements Optional initial elements to insert.
1275
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
1276
+ * @throws {TypeError} Thrown when using the default comparator with object elements (provide a custom comparator).
1277
+ * @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
1278
+ */
1279
+ constructor(elements = [], options) {
1280
+ super(elements, {
1281
+ comparator: /* @__PURE__ */ __name((a, b) => {
1282
+ if (typeof a === "object" || typeof b === "object") {
1283
+ throw new TypeError(ERR.comparatorRequired("MaxPriorityQueue"));
1284
+ }
1285
+ if (a < b) return 1;
1286
+ if (a > b) return -1;
1287
+ return 0;
1288
+ }, "comparator"),
1289
+ ...options
1290
+ });
1291
+ }
1292
+ };
1293
+ /**
1294
+ * data-structure-typed
1295
+ *
1296
+ * @author Pablo Zeng
1297
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1298
+ * @license MIT License
1299
+ */
1300
+ /**
1301
+ * data-structure-typed
1302
+ *
1303
+ * @author Kirk Qi
1304
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
1305
+ * @license MIT License
1306
+ */
1307
+
1308
+ export { MaxPriorityQueue, MinPriorityQueue, PriorityQueue };
1309
+ //# sourceMappingURL=priority-queue.mjs.map
1310
+ //# sourceMappingURL=priority-queue.mjs.map