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,1259 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+
6
+ // src/data-structures/base/iterable-entry-base.ts
7
+ var _IterableEntryBase = class _IterableEntryBase {
8
+ /**
9
+ * Default iterator yielding `[key, value]` entries.
10
+ * @returns Iterator of `[K, V]`.
11
+ * @remarks Time O(n) to iterate, Space O(1)
12
+ */
13
+ *[Symbol.iterator](...args) {
14
+ yield* this._getIterator(...args);
15
+ }
16
+ /**
17
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
18
+ * @returns Iterator of `[K, V | undefined]`.
19
+ * @remarks Time O(n), Space O(1)
20
+ */
21
+ *entries() {
22
+ for (const item of this) {
23
+ yield item;
24
+ }
25
+ }
26
+ /**
27
+ * Iterate over keys only.
28
+ * @returns Iterator of keys.
29
+ * @remarks Time O(n), Space O(1)
30
+ */
31
+ *keys() {
32
+ for (const item of this) {
33
+ yield item[0];
34
+ }
35
+ }
36
+ /**
37
+ * Iterate over values only.
38
+ * @returns Iterator of values.
39
+ * @remarks Time O(n), Space O(1)
40
+ */
41
+ *values() {
42
+ for (const item of this) {
43
+ yield item[1];
44
+ }
45
+ }
46
+ /**
47
+ * Test whether all entries satisfy the predicate.
48
+ * @param predicate - `(key, value, index, self) => boolean`.
49
+ * @param thisArg - Optional `this` for callback.
50
+ * @returns `true` if all pass; otherwise `false`.
51
+ * @remarks Time O(n), Space O(1)
52
+ */
53
+ every(predicate, thisArg) {
54
+ let index = 0;
55
+ for (const item of this) {
56
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
57
+ return false;
58
+ }
59
+ }
60
+ return true;
61
+ }
62
+ /**
63
+ * Test whether any entry satisfies the predicate.
64
+ * @param predicate - `(key, value, index, self) => boolean`.
65
+ * @param thisArg - Optional `this` for callback.
66
+ * @returns `true` if any passes; otherwise `false`.
67
+ * @remarks Time O(n), Space O(1)
68
+ */
69
+ some(predicate, thisArg) {
70
+ let index = 0;
71
+ for (const item of this) {
72
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
73
+ return true;
74
+ }
75
+ }
76
+ return false;
77
+ }
78
+ /**
79
+ * Visit each entry, left-to-right.
80
+ * @param callbackfn - `(key, value, index, self) => void`.
81
+ * @param thisArg - Optional `this` for callback.
82
+ * @remarks Time O(n), Space O(1)
83
+ */
84
+ forEach(callbackfn, thisArg) {
85
+ let index = 0;
86
+ for (const item of this) {
87
+ const [key, value] = item;
88
+ callbackfn.call(thisArg, value, key, index++, this);
89
+ }
90
+ }
91
+ /**
92
+ * Find the first entry that matches a predicate.
93
+ * @param callbackfn - `(key, value, index, self) => boolean`.
94
+ * @param thisArg - Optional `this` for callback.
95
+ * @returns Matching `[key, value]` or `undefined`.
96
+ * @remarks Time O(n), Space O(1)
97
+ */
98
+ find(callbackfn, thisArg) {
99
+ let index = 0;
100
+ for (const item of this) {
101
+ const [key, value] = item;
102
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
103
+ }
104
+ return;
105
+ }
106
+ /**
107
+ * Whether the given key exists.
108
+ * @param key - Key to test.
109
+ * @returns `true` if found; otherwise `false`.
110
+ * @remarks Time O(n) generic, Space O(1)
111
+ */
112
+ has(key) {
113
+ for (const item of this) {
114
+ const [itemKey] = item;
115
+ if (itemKey === key) return true;
116
+ }
117
+ return false;
118
+ }
119
+ /**
120
+ * Whether there exists an entry with the given value.
121
+ * @param value - Value to test.
122
+ * @returns `true` if found; otherwise `false`.
123
+ * @remarks Time O(n), Space O(1)
124
+ */
125
+ hasValue(value) {
126
+ for (const [, elementValue] of this) {
127
+ if (elementValue === value) return true;
128
+ }
129
+ return false;
130
+ }
131
+ /**
132
+ * Get the value under a key.
133
+ * @param key - Key to look up.
134
+ * @returns Value or `undefined`.
135
+ * @remarks Time O(n) generic, Space O(1)
136
+ */
137
+ get(key) {
138
+ for (const item of this) {
139
+ const [itemKey, value] = item;
140
+ if (itemKey === key) return value;
141
+ }
142
+ return;
143
+ }
144
+ /**
145
+ * Reduce entries into a single accumulator.
146
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
147
+ * @param initialValue - Initial accumulator.
148
+ * @returns Final accumulator.
149
+ * @remarks Time O(n), Space O(1)
150
+ */
151
+ reduce(callbackfn, initialValue) {
152
+ let accumulator = initialValue;
153
+ let index = 0;
154
+ for (const item of this) {
155
+ const [key, value] = item;
156
+ accumulator = callbackfn(accumulator, value, key, index++, this);
157
+ }
158
+ return accumulator;
159
+ }
160
+ /**
161
+ * Converts data structure to `[key, value]` pairs.
162
+ * @returns Array of entries.
163
+ * @remarks Time O(n), Space O(n)
164
+ */
165
+ toArray() {
166
+ return [...this];
167
+ }
168
+ /**
169
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
170
+ * @returns Array of entries (default) or a string.
171
+ * @remarks Time O(n), Space O(n)
172
+ */
173
+ toVisual() {
174
+ return [...this];
175
+ }
176
+ /**
177
+ * Print a human-friendly representation to the console.
178
+ * @remarks Time O(n), Space O(n)
179
+ */
180
+ print() {
181
+ console.log(this.toVisual());
182
+ }
183
+ };
184
+ __name(_IterableEntryBase, "IterableEntryBase");
185
+ var IterableEntryBase = _IterableEntryBase;
186
+
187
+ // src/utils/utils.ts
188
+ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
189
+ if (index < min || index > max) {
190
+ throw new RangeError(message != null ? message : `Index ${index} is out of range [${min}, ${max}].`);
191
+ }
192
+ }, "rangeCheck");
193
+ var isWeakKey = /* @__PURE__ */ __name((input) => {
194
+ const inputType = typeof input;
195
+ return inputType === "object" && input !== null || inputType === "function";
196
+ }, "isWeakKey");
197
+
198
+ // src/common/error.ts
199
+ var ERR = {
200
+ // Range / index
201
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
202
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
203
+ // Type / argument
204
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
205
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
206
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
207
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
208
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
209
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
210
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
211
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
212
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
213
+ // State / operation
214
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
215
+ // Matrix
216
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
217
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
218
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
219
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
220
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
221
+ };
222
+
223
+ // src/data-structures/hash/hash-map.ts
224
+ var _HashMap = class _HashMap extends IterableEntryBase {
225
+ /**
226
+ * Create a HashMap and optionally bulk-insert entries.
227
+ * @remarks Time O(N), Space O(N)
228
+ * @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
229
+ * @param [options] - Options: hash function and optional record-to-entry converter.
230
+ * @returns New HashMap instance.
231
+ */
232
+ constructor(entryOrRawElements = [], options) {
233
+ super();
234
+ __publicField(this, "_store", {});
235
+ __publicField(this, "_objMap", /* @__PURE__ */ new Map());
236
+ __publicField(this, "_toEntryFn");
237
+ __publicField(this, "_size", 0);
238
+ __publicField(this, "_hashFn", /* @__PURE__ */ __name((key) => String(key), "_hashFn"));
239
+ if (options) {
240
+ const { hashFn, toEntryFn } = options;
241
+ if (hashFn) this._hashFn = hashFn;
242
+ if (toEntryFn) this._toEntryFn = toEntryFn;
243
+ }
244
+ if (entryOrRawElements) this.setMany(entryOrRawElements);
245
+ }
246
+ /**
247
+ * Get the internal store for non-object keys.
248
+ * @remarks Time O(1), Space O(1)
249
+ * @returns Internal record of string→{key,value}.
250
+ */
251
+ get store() {
252
+ return this._store;
253
+ }
254
+ /**
255
+ * Get the internal Map used for object/function keys.
256
+ * @remarks Time O(1), Space O(1)
257
+ * @returns Map of object→value.
258
+ */
259
+ get objMap() {
260
+ return this._objMap;
261
+ }
262
+ /**
263
+ * Get the raw→entry converter function if present.
264
+ * @remarks Time O(1), Space O(1)
265
+ * @returns Converter function or undefined.
266
+ */
267
+ get toEntryFn() {
268
+ return this._toEntryFn;
269
+ }
270
+ /**
271
+ * Get the number of distinct keys stored.
272
+ * @remarks Time O(1), Space O(1)
273
+ * @returns Current size.
274
+ */
275
+ get size() {
276
+ return this._size;
277
+ }
278
+ /**
279
+ * Get the current hash function for non-object keys.
280
+ * @remarks Time O(1), Space O(1)
281
+ * @returns Hash function.
282
+ */
283
+ get hashFn() {
284
+ return this._hashFn;
285
+ }
286
+ /**
287
+ * Check whether the map is empty.
288
+ * @remarks Time O(1), Space O(1)
289
+ * @returns True if size is 0.
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
+
318
+
319
+ * @example
320
+ * // Check if empty
321
+ * const map = new HashMap();
322
+ * console.log(map.isEmpty()); // true;
323
+ */
324
+ isEmpty() {
325
+ return this._size === 0;
326
+ }
327
+ /**
328
+ * Remove all entries and reset counters.
329
+ * @remarks Time O(N), Space O(1)
330
+ * @returns void
331
+
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+
357
+
358
+
359
+
360
+ * @example
361
+ * // Remove all entries
362
+ * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
363
+ * map.clear();
364
+ * console.log(map.isEmpty()); // true;
365
+ */
366
+ clear() {
367
+ this._store = {};
368
+ this._objMap.clear();
369
+ this._size = 0;
370
+ }
371
+ /**
372
+ * Type guard: check if a raw value is a [key, value] entry.
373
+ * @remarks Time O(1), Space O(1)
374
+ * @returns True if the value is a 2-tuple.
375
+ */
376
+ isEntry(rawElement) {
377
+ return Array.isArray(rawElement) && rawElement.length === 2;
378
+ }
379
+ /**
380
+ * Insert or replace a single entry.
381
+ * @remarks Time O(1), Space O(1)
382
+ * @param key - Key.
383
+ * @param value - Value.
384
+ * @returns True when the operation succeeds.
385
+
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+
424
+
425
+
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+ * @example
448
+ * // basic HashMap creation and set operation
449
+ * // Create a simple HashMap with key-value pairs
450
+ * const map = new HashMap<number, string>([
451
+ * [1, 'one'],
452
+ * [2, 'two'],
453
+ * [3, 'three']
454
+ * ]);
455
+ *
456
+ * // Verify size
457
+ * console.log(map.size); // 3;
458
+ *
459
+ * // Set a new key-value pair
460
+ * map.set(4, 'four');
461
+ * console.log(map.size); // 4;
462
+ *
463
+ * // Verify entries
464
+ * console.log([...map.entries()]); // length: 4;
465
+ */
466
+ set(key, value) {
467
+ if (this._isObjKey(key)) {
468
+ if (!this.objMap.has(key)) this._size++;
469
+ this.objMap.set(key, value);
470
+ } else {
471
+ const strKey = this._getNoObjKey(key);
472
+ if (this.store[strKey] === void 0) this._size++;
473
+ this._store[strKey] = { key, value };
474
+ }
475
+ return true;
476
+ }
477
+ /**
478
+ * Insert many entries from an iterable.
479
+ * @remarks Time O(N), Space O(N)
480
+ * @param entryOrRawElements - Iterable of entries or raw elements to insert.
481
+ * @returns Array of per-entry results.
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
+
508
+
509
+
510
+
511
+ * @example
512
+ * // Add multiple entries
513
+ * const map = new HashMap<string, number>();
514
+ * map.setMany([['a', 1], ['b', 2], ['c', 3]]);
515
+ * console.log(map.size); // 3;
516
+ */
517
+ setMany(entryOrRawElements) {
518
+ const results = [];
519
+ for (const rawEle of entryOrRawElements) {
520
+ let key, value;
521
+ if (this.isEntry(rawEle)) [key, value] = rawEle;
522
+ else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
523
+ if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
524
+ }
525
+ return results;
526
+ }
527
+ /**
528
+ * Get the value for a key.
529
+ * @remarks Time O(1), Space O(1)
530
+ * @param key - Key to look up.
531
+ * @returns Value or undefined.
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
544
+
545
+
546
+
547
+
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+
556
+
557
+
558
+
559
+
560
+
561
+
562
+
563
+ * @example
564
+ * // HashMap get and has operations
565
+ * const map = new HashMap<string, number>([
566
+ * ['apple', 1],
567
+ * ['banana', 2],
568
+ * ['cherry', 3]
569
+ * ]);
570
+ *
571
+ * // Check if key exists
572
+ * console.log(map.has('apple')); // true;
573
+ * console.log(map.has('date')); // false;
574
+ *
575
+ * // Get value by key
576
+ * console.log(map.get('banana')); // 2;
577
+ * console.log(map.get('grape')); // undefined;
578
+ *
579
+ * // Get all keys and values
580
+ * const keys = [...map.keys()];
581
+ * const values = [...map.values()];
582
+ * console.log(keys); // contains 'apple';
583
+ * console.log(values); // contains 3;
584
+ */
585
+ get(key) {
586
+ var _a;
587
+ if (this._isObjKey(key)) return this.objMap.get(key);
588
+ const strKey = this._getNoObjKey(key);
589
+ return (_a = this._store[strKey]) == null ? void 0 : _a.value;
590
+ }
591
+ /**
592
+ * Check if a key exists.
593
+ * @remarks Time O(1), Space O(1)
594
+ * @param key - Key to test.
595
+ * @returns True if present.
596
+
597
+
598
+
599
+
600
+
601
+
602
+
603
+
604
+
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+
615
+
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+ * @example
628
+ * // Check key existence
629
+ * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
630
+ *
631
+ * console.log(map.has('a')); // true;
632
+ * console.log(map.has('z')); // false;
633
+ */
634
+ has(key) {
635
+ if (this._isObjKey(key)) return this.objMap.has(key);
636
+ const strKey = this._getNoObjKey(key);
637
+ return strKey in this.store;
638
+ }
639
+ /**
640
+ * Delete an entry by key.
641
+ * @remarks Time O(1), Space O(1)
642
+ * @param key - Key to delete.
643
+ * @returns True if the key was found and removed.
644
+
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
+
675
+ * @example
676
+ * // Remove entries by key
677
+ * const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
678
+ *
679
+ * console.log(map.delete('y')); // true;
680
+ * console.log(map.has('y')); // false;
681
+ * console.log(map.size); // 2;
682
+ */
683
+ delete(key) {
684
+ if (this._isObjKey(key)) {
685
+ if (this.objMap.has(key)) this._size--;
686
+ return this.objMap.delete(key);
687
+ }
688
+ const strKey = this._getNoObjKey(key);
689
+ if (strKey in this.store) {
690
+ delete this.store[strKey];
691
+ this._size--;
692
+ return true;
693
+ }
694
+ return false;
695
+ }
696
+ /**
697
+ * Replace the hash function and rehash the non-object store.
698
+ * @remarks Time O(N), Space O(N)
699
+ * @param fn - New hash function for non-object keys.
700
+ * @returns This map instance.
701
+ */
702
+ setHashFn(fn) {
703
+ if (this._hashFn === fn) return this;
704
+ this._hashFn = fn;
705
+ this._rehashNoObj();
706
+ return this;
707
+ }
708
+ /**
709
+ * Deep clone this map, preserving hashing behavior.
710
+ * @remarks Time O(N), Space O(N)
711
+ * @returns A new map with the same content.
712
+
713
+
714
+
715
+
716
+
717
+
718
+
719
+
720
+
721
+
722
+
723
+
724
+
725
+
726
+
727
+
728
+
729
+
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+
738
+
739
+
740
+
741
+ * @example
742
+ * // Create independent copy
743
+ * const map = new HashMap<string, number>([['a', 1]]);
744
+ * const copy = map.clone();
745
+ * copy.set('a', 99);
746
+ * console.log(map.get('a')); // 1;
747
+ */
748
+ clone() {
749
+ const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
750
+ return this._createLike(this, opts);
751
+ }
752
+ /**
753
+ * Map values to a new map with the same keys.
754
+ * @remarks Time O(N), Space O(N)
755
+ * @template VM
756
+ * @param callbackfn - Mapping function (key, value, index, map) → newValue.
757
+ * @param [thisArg] - Value for `this` inside the callback.
758
+ * @returns A new map with transformed values.
759
+
760
+
761
+
762
+
763
+
764
+
765
+
766
+
767
+
768
+
769
+
770
+
771
+
772
+
773
+
774
+
775
+
776
+
777
+
778
+
779
+
780
+
781
+
782
+
783
+
784
+
785
+
786
+
787
+
788
+
789
+
790
+ * @example
791
+ * // Transform all values
792
+ * const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
793
+ *
794
+ * const doubled = prices.map(v => (v ?? 0) * 2);
795
+ * console.log(doubled.get('apple')); // 2;
796
+ * console.log(doubled.get('banana')); // 4;
797
+ */
798
+ map(callbackfn, thisArg) {
799
+ const out = this._createLike();
800
+ let index = 0;
801
+ for (const [key, value] of this) out.set(key, callbackfn.call(thisArg, value, key, index++, this));
802
+ return out;
803
+ }
804
+ /**
805
+ * Filter entries into a new map.
806
+ * @remarks Time O(N), Space O(N)
807
+ * @param predicate - Predicate (key, value, index, map) → boolean.
808
+ * @param [thisArg] - Value for `this` inside the predicate.
809
+ * @returns A new map containing entries that satisfied the predicate.
810
+
811
+
812
+
813
+
814
+
815
+
816
+
817
+
818
+
819
+
820
+
821
+
822
+
823
+
824
+
825
+
826
+
827
+
828
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+ * @example
842
+ * // HashMap iteration and filter operations
843
+ * const map = new HashMap<number, string>([
844
+ * [1, 'Alice'],
845
+ * [2, 'Bob'],
846
+ * [3, 'Charlie'],
847
+ * [4, 'Diana'],
848
+ * [5, 'Eve']
849
+ * ]);
850
+ *
851
+ * // Iterate through entries
852
+ * const entries: [number, string][] = [];
853
+ * for (const [key, value] of map) {
854
+ * entries.push([key, value]);
855
+ * }
856
+ * console.log(entries); // length: 5;
857
+ *
858
+ * // Filter operation (for iteration with collection methods)
859
+ * const filtered = [...map].filter(([key]) => key > 2);
860
+ * console.log(filtered.length); // 3;
861
+ *
862
+ * // Map operation
863
+ * const values = [...map.values()].map(v => v.length);
864
+ * console.log(values); // contains 3; // 'Bob', 'Eve'
865
+ * console.log(values); // contains 7;
866
+ */
867
+ filter(predicate, thisArg) {
868
+ const out = this._createLike();
869
+ let index = 0;
870
+ for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
871
+ return out;
872
+ }
873
+ /**
874
+ * (Protected) Create a like-kind instance and seed it from an iterable.
875
+ * @remarks Time O(N), Space O(N)
876
+ * @template TK
877
+ * @template TV
878
+ * @template TR
879
+ * @param [entries] - Iterable used to seed the new map.
880
+ * @param [options] - Options forwarded to the constructor.
881
+ * @returns A like-kind map instance.
882
+ */
883
+ _createLike(entries = [], options) {
884
+ const Ctor = this.constructor;
885
+ return new Ctor(entries, options);
886
+ }
887
+ _rehashNoObj() {
888
+ const fresh = {};
889
+ for (const { key, value } of Object.values(this._store)) {
890
+ const sk = this._getNoObjKey(key);
891
+ fresh[sk] = { key, value };
892
+ }
893
+ this._store = fresh;
894
+ }
895
+ *_getIterator() {
896
+ for (const node of Object.values(this.store)) yield [node.key, node.value];
897
+ for (const node of this.objMap) yield node;
898
+ }
899
+ _isObjKey(key) {
900
+ const keyType = typeof key;
901
+ return (keyType === "object" || keyType === "function") && key !== null;
902
+ }
903
+ _getNoObjKey(key) {
904
+ const keyType = typeof key;
905
+ let strKey;
906
+ if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
907
+ strKey = this._hashFn(key);
908
+ } else {
909
+ if (keyType === "number") {
910
+ strKey = key;
911
+ } else {
912
+ strKey = key;
913
+ }
914
+ }
915
+ return strKey;
916
+ }
917
+ };
918
+ __name(_HashMap, "HashMap");
919
+ var HashMap = _HashMap;
920
+ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
921
+ /**
922
+ * Create a LinkedHashMap and optionally bulk-insert entries.
923
+ * @remarks Time O(N), Space O(N)
924
+ * @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
925
+ * @param [options] - Options: hash functions and optional record-to-entry converter.
926
+ * @returns New LinkedHashMap instance.
927
+ */
928
+ constructor(entryOrRawElements = [], options) {
929
+ super();
930
+ __publicField(this, "_sentinel");
931
+ __publicField(this, "_hashFn", /* @__PURE__ */ __name((key) => String(key), "_hashFn"));
932
+ __publicField(this, "_objHashFn", /* @__PURE__ */ __name((key) => key, "_objHashFn"));
933
+ __publicField(this, "_noObjMap", {});
934
+ __publicField(this, "_objMap", /* @__PURE__ */ new WeakMap());
935
+ __publicField(this, "_head");
936
+ __publicField(this, "_tail");
937
+ __publicField(this, "_toEntryFn", /* @__PURE__ */ __name((rawElement) => {
938
+ if (this.isEntry(rawElement)) {
939
+ return rawElement;
940
+ }
941
+ throw new TypeError(
942
+ ERR.invalidArgument("If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.", "HashMap")
943
+ );
944
+ }, "_toEntryFn"));
945
+ __publicField(this, "_size", 0);
946
+ this._sentinel = {};
947
+ this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
948
+ if (options) {
949
+ const { hashFn, objHashFn, toEntryFn } = options;
950
+ if (hashFn) this._hashFn = hashFn;
951
+ if (objHashFn) this._objHashFn = objHashFn;
952
+ if (toEntryFn) this._toEntryFn = toEntryFn;
953
+ }
954
+ if (entryOrRawElements) this.setMany(entryOrRawElements);
955
+ }
956
+ get hashFn() {
957
+ return this._hashFn;
958
+ }
959
+ /**
960
+ * Get the hash function for object/weak keys.
961
+ * @remarks Time O(1), Space O(1)
962
+ * @returns Object-hash function.
963
+ */
964
+ get objHashFn() {
965
+ return this._objHashFn;
966
+ }
967
+ /**
968
+ * Get the internal record for non-object keys.
969
+ * @remarks Time O(1), Space O(1)
970
+ * @returns Record of hash→node.
971
+ */
972
+ get noObjMap() {
973
+ return this._noObjMap;
974
+ }
975
+ get objMap() {
976
+ return this._objMap;
977
+ }
978
+ /**
979
+ * Get the head node (first entry) sentinel link.
980
+ * @remarks Time O(1), Space O(1)
981
+ * @returns Head node or sentinel.
982
+ */
983
+ get head() {
984
+ return this._head;
985
+ }
986
+ /**
987
+ * Get the tail node (last entry) sentinel link.
988
+ * @remarks Time O(1), Space O(1)
989
+ * @returns Tail node or sentinel.
990
+ */
991
+ get tail() {
992
+ return this._tail;
993
+ }
994
+ get toEntryFn() {
995
+ return this._toEntryFn;
996
+ }
997
+ get size() {
998
+ return this._size;
999
+ }
1000
+ /**
1001
+ * Get the first [key, value] pair.
1002
+ * @remarks Time O(1), Space O(1)
1003
+ * @returns First entry or undefined when empty.
1004
+ */
1005
+ get first() {
1006
+ if (this._size === 0) return;
1007
+ return [this.head.key, this.head.value];
1008
+ }
1009
+ /**
1010
+ * Get the last [key, value] pair.
1011
+ * @remarks Time O(1), Space O(1)
1012
+ * @returns Last entry or undefined when empty.
1013
+ */
1014
+ get last() {
1015
+ if (this._size === 0) return;
1016
+ return [this.tail.key, this.tail.value];
1017
+ }
1018
+ /**
1019
+ * Iterate from head → tail.
1020
+ * @remarks Time O(N), Space O(1)
1021
+ * @returns Iterator of [key, value].
1022
+ */
1023
+ *begin() {
1024
+ let node = this.head;
1025
+ while (node !== this._sentinel) {
1026
+ yield [node.key, node.value];
1027
+ node = node.next;
1028
+ }
1029
+ }
1030
+ /**
1031
+ * Iterate from tail → head.
1032
+ * @remarks Time O(N), Space O(1)
1033
+ * @returns Iterator of [key, value].
1034
+ */
1035
+ *reverseBegin() {
1036
+ let node = this.tail;
1037
+ while (node !== this._sentinel) {
1038
+ yield [node.key, node.value];
1039
+ node = node.prev;
1040
+ }
1041
+ }
1042
+ /**
1043
+ * Insert or replace a single entry; preserves insertion order.
1044
+ * @remarks Time O(1), Space O(1)
1045
+ * @param key - Key.
1046
+ * @param [value] - Value.
1047
+ * @returns True when the operation succeeds.
1048
+ */
1049
+ set(key, value) {
1050
+ let node;
1051
+ const isNewKey = !this.has(key);
1052
+ if (isWeakKey(key)) {
1053
+ const hash = this._objHashFn(key);
1054
+ node = this.objMap.get(hash);
1055
+ if (!node && isNewKey) {
1056
+ node = { key: hash, value, prev: this.tail, next: this._sentinel };
1057
+ this.objMap.set(hash, node);
1058
+ } else if (node) {
1059
+ node.value = value;
1060
+ }
1061
+ } else {
1062
+ const hash = this._hashFn(key);
1063
+ node = this.noObjMap[hash];
1064
+ if (!node && isNewKey) {
1065
+ this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
1066
+ } else if (node) {
1067
+ node.value = value;
1068
+ }
1069
+ }
1070
+ if (node && isNewKey) {
1071
+ if (this._size === 0) {
1072
+ this._head = node;
1073
+ this._sentinel.next = node;
1074
+ } else {
1075
+ this.tail.next = node;
1076
+ node.prev = this.tail;
1077
+ }
1078
+ this._tail = node;
1079
+ this._sentinel.prev = node;
1080
+ this._size++;
1081
+ }
1082
+ return true;
1083
+ }
1084
+ setMany(entryOrRawElements) {
1085
+ const results = [];
1086
+ for (const rawEle of entryOrRawElements) {
1087
+ let key, value;
1088
+ if (this.isEntry(rawEle)) [key, value] = rawEle;
1089
+ else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
1090
+ if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
1091
+ }
1092
+ return results;
1093
+ }
1094
+ has(key) {
1095
+ if (isWeakKey(key)) {
1096
+ const hash2 = this._objHashFn(key);
1097
+ return this.objMap.has(hash2);
1098
+ }
1099
+ const hash = this._hashFn(key);
1100
+ return hash in this.noObjMap;
1101
+ }
1102
+ get(key) {
1103
+ if (isWeakKey(key)) {
1104
+ const hash2 = this._objHashFn(key);
1105
+ const node2 = this.objMap.get(hash2);
1106
+ return node2 ? node2.value : void 0;
1107
+ }
1108
+ const hash = this._hashFn(key);
1109
+ const node = this.noObjMap[hash];
1110
+ return node ? node.value : void 0;
1111
+ }
1112
+ /**
1113
+ * Get the value at a given index in insertion order.
1114
+ * @remarks Time O(N), Space O(1)
1115
+ * @param index - Zero-based index.
1116
+ * @returns Value at the index.
1117
+ */
1118
+ at(index) {
1119
+ rangeCheck(index, 0, this._size - 1);
1120
+ let node = this.head;
1121
+ while (index--) node = node.next;
1122
+ return node.value;
1123
+ }
1124
+ delete(key) {
1125
+ let node;
1126
+ if (isWeakKey(key)) {
1127
+ const hash = this._objHashFn(key);
1128
+ node = this.objMap.get(hash);
1129
+ if (!node) return false;
1130
+ this.objMap.delete(hash);
1131
+ } else {
1132
+ const hash = this._hashFn(key);
1133
+ node = this.noObjMap[hash];
1134
+ if (!node) return false;
1135
+ delete this.noObjMap[hash];
1136
+ }
1137
+ return this._deleteNode(node);
1138
+ }
1139
+ /**
1140
+ * Delete the first entry that matches a predicate.
1141
+ * @remarks Time O(N), Space O(1)
1142
+ * @param predicate - Function (key, value, index, map) → boolean to decide deletion.
1143
+ * @returns True if an entry was removed.
1144
+ */
1145
+ deleteWhere(predicate) {
1146
+ let node = this._head;
1147
+ let i = 0;
1148
+ while (node !== this._sentinel) {
1149
+ const cur = node;
1150
+ node = node.next;
1151
+ if (predicate(cur.key, cur.value, i++, this)) {
1152
+ const keyToCheck = cur.key;
1153
+ if (isWeakKey(keyToCheck)) {
1154
+ this._objMap.delete(keyToCheck);
1155
+ } else {
1156
+ const hash = this._hashFn(cur.key);
1157
+ delete this._noObjMap[hash];
1158
+ }
1159
+ return this._deleteNode(cur);
1160
+ }
1161
+ }
1162
+ return false;
1163
+ }
1164
+ /**
1165
+ * Delete the entry at a given index.
1166
+ * @remarks Time O(N), Space O(1)
1167
+ * @param index - Zero-based index.
1168
+ * @returns True if removed.
1169
+ */
1170
+ deleteAt(index) {
1171
+ rangeCheck(index, 0, this._size - 1);
1172
+ let node = this.head;
1173
+ while (index--) node = node.next;
1174
+ return this._deleteNode(node);
1175
+ }
1176
+ isEmpty() {
1177
+ return this._size === 0;
1178
+ }
1179
+ isEntry(rawElement) {
1180
+ return Array.isArray(rawElement) && rawElement.length === 2;
1181
+ }
1182
+ clear() {
1183
+ this._noObjMap = {};
1184
+ this._size = 0;
1185
+ this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
1186
+ }
1187
+ clone() {
1188
+ const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
1189
+ return this._createLike(this, opts);
1190
+ }
1191
+ filter(predicate, thisArg) {
1192
+ const out = this._createLike();
1193
+ let index = 0;
1194
+ for (const [key, value] of this) {
1195
+ if (predicate.call(thisArg, value, key, index, this)) out.set(key, value);
1196
+ index++;
1197
+ }
1198
+ return out;
1199
+ }
1200
+ /**
1201
+ * Map each entry to a new [key, value] pair and preserve order.
1202
+ * @remarks Time O(N), Space O(N)
1203
+ * @template MK
1204
+ * @template MV
1205
+ * @param callback - Mapping function (key, value, index, map) → [newKey, newValue].
1206
+ * @param [thisArg] - Value for `this` inside the callback.
1207
+ * @returns A new map of the same class with transformed entries.
1208
+ */
1209
+ map(callback, thisArg) {
1210
+ const out = this._createLike();
1211
+ let index = 0;
1212
+ for (const [key, value] of this) {
1213
+ const [newKey, newValue] = callback.call(thisArg, value, key, index, this);
1214
+ out.set(newKey, newValue);
1215
+ index++;
1216
+ }
1217
+ return out;
1218
+ }
1219
+ *_getIterator() {
1220
+ let node = this.head;
1221
+ while (node !== this._sentinel) {
1222
+ yield [node.key, node.value];
1223
+ node = node.next;
1224
+ }
1225
+ }
1226
+ _deleteNode(node) {
1227
+ const key = node.key;
1228
+ if (isWeakKey(key)) {
1229
+ this._objMap.delete(key);
1230
+ } else {
1231
+ const hash = this._hashFn(key);
1232
+ delete this._noObjMap[hash];
1233
+ }
1234
+ const { prev, next } = node;
1235
+ prev.next = next;
1236
+ next.prev = prev;
1237
+ if (node === this.head) this._head = next;
1238
+ if (node === this.tail) this._tail = prev;
1239
+ this._size -= 1;
1240
+ return true;
1241
+ }
1242
+ _createLike(entries = [], options) {
1243
+ const Ctor = this.constructor;
1244
+ return new Ctor(entries, options);
1245
+ }
1246
+ };
1247
+ __name(_LinkedHashMap, "LinkedHashMap");
1248
+ var LinkedHashMap = _LinkedHashMap;
1249
+ /**
1250
+ * data-structure-typed
1251
+ *
1252
+ * @author Pablo Zeng
1253
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1254
+ * @license MIT License
1255
+ */
1256
+
1257
+ export { HashMap, LinkedHashMap };
1258
+ //# sourceMappingURL=hash.mjs.map
1259
+ //# sourceMappingURL=hash.mjs.map