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