data-structure-typed 2.4.5 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +3 -1
  7. package/README.md +78 -31
  8. package/dist/cjs/binary-tree.cjs +23698 -0
  9. package/dist/cjs/graph.cjs +5236 -0
  10. package/dist/cjs/hash.cjs +1262 -0
  11. package/dist/cjs/heap.cjs +1540 -0
  12. package/dist/cjs/index.cjs +24509 -2899
  13. package/dist/cjs/linked-list.cjs +4370 -0
  14. package/dist/cjs/matrix.cjs +1042 -0
  15. package/dist/cjs/priority-queue.cjs +1314 -0
  16. package/dist/cjs/queue.cjs +4090 -0
  17. package/dist/cjs/stack.cjs +861 -0
  18. package/dist/cjs/trie.cjs +1173 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +23730 -0
  20. package/dist/cjs-legacy/graph.cjs +5234 -0
  21. package/dist/cjs-legacy/hash.cjs +1262 -0
  22. package/dist/cjs-legacy/heap.cjs +1537 -0
  23. package/dist/cjs-legacy/index.cjs +32555 -10936
  24. package/dist/cjs-legacy/linked-list.cjs +4376 -0
  25. package/dist/cjs-legacy/matrix.cjs +1045 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1312 -0
  27. package/dist/cjs-legacy/queue.cjs +4088 -0
  28. package/dist/cjs-legacy/stack.cjs +861 -0
  29. package/dist/cjs-legacy/trie.cjs +1172 -0
  30. package/dist/esm/binary-tree.mjs +23683 -0
  31. package/dist/esm/graph.mjs +5223 -0
  32. package/dist/esm/hash.mjs +1259 -0
  33. package/dist/esm/heap.mjs +1534 -0
  34. package/dist/esm/index.mjs +24507 -2898
  35. package/dist/esm/linked-list.mjs +4363 -0
  36. package/dist/esm/matrix.mjs +1038 -0
  37. package/dist/esm/priority-queue.mjs +1310 -0
  38. package/dist/esm/queue.mjs +4086 -0
  39. package/dist/esm/stack.mjs +859 -0
  40. package/dist/esm/trie.mjs +1170 -0
  41. package/dist/esm-legacy/binary-tree.mjs +23715 -0
  42. package/dist/esm-legacy/graph.mjs +5221 -0
  43. package/dist/esm-legacy/hash.mjs +1259 -0
  44. package/dist/esm-legacy/heap.mjs +1531 -0
  45. package/dist/esm-legacy/index.mjs +32553 -10935
  46. package/dist/esm-legacy/linked-list.mjs +4369 -0
  47. package/dist/esm-legacy/matrix.mjs +1041 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1308 -0
  49. package/dist/esm-legacy/queue.mjs +4084 -0
  50. package/dist/esm-legacy/stack.mjs +859 -0
  51. package/dist/esm-legacy/trie.mjs +1169 -0
  52. package/dist/types/data-structures/base/index.d.ts +1 -0
  53. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  54. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  55. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +368 -51
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +473 -147
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +931 -80
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +792 -29
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +592 -32
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +320 -135
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3662 -6
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3487 -201
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2778 -65
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3414 -6
  66. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  67. package/dist/types/data-structures/graph/directed-graph.d.ts +419 -47
  68. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +384 -59
  70. package/dist/types/data-structures/hash/hash-map.d.ts +462 -89
  71. package/dist/types/data-structures/heap/heap.d.ts +567 -99
  72. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  73. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  74. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +631 -49
  75. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +581 -68
  76. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +775 -12
  77. package/dist/types/data-structures/matrix/matrix.d.ts +491 -0
  78. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  79. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  80. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  81. package/dist/types/data-structures/queue/deque.d.ts +578 -71
  82. package/dist/types/data-structures/queue/queue.d.ts +451 -42
  83. package/dist/types/data-structures/stack/stack.d.ts +374 -32
  84. package/dist/types/data-structures/trie/trie.d.ts +458 -48
  85. package/dist/types/interfaces/graph.d.ts +1 -1
  86. package/dist/types/types/common.d.ts +2 -2
  87. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  88. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  89. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  90. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  91. package/dist/types/types/utils/validate-type.d.ts +4 -4
  92. package/dist/umd/data-structure-typed.js +32432 -10808
  93. package/dist/umd/data-structure-typed.min.js +10 -4
  94. package/docs-site-docusaurus/README.md +41 -0
  95. package/docs-site-docusaurus/docs/api/README.md +52 -0
  96. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
  97. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  98. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  99. package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
  100. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  101. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  102. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  103. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  104. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  105. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  106. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  107. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  108. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  109. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  110. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  111. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  112. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  113. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  116. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  117. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  118. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  119. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  120. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  121. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  122. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  123. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  124. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  125. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  126. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  127. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  128. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
  129. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  130. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  131. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  132. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  133. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  134. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
  135. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
  136. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
  137. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  138. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  139. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  140. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  141. package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
  142. package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
  143. package/docs-site-docusaurus/docs/guide/guides.md +611 -0
  144. package/docs-site-docusaurus/docs/guide/installation.md +60 -0
  145. package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
  146. package/docs-site-docusaurus/docs/guide/overview.md +638 -0
  147. package/docs-site-docusaurus/docs/guide/performance.md +833 -0
  148. package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
  149. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  150. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  151. package/docs-site-docusaurus/package-lock.json +18667 -0
  152. package/docs-site-docusaurus/package.json +50 -0
  153. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  154. package/docs-site-docusaurus/sidebars.ts +23 -0
  155. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  156. package/docs-site-docusaurus/src/css/custom.css +96 -0
  157. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  158. package/docs-site-docusaurus/src/pages/index.tsx +71 -0
  159. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  160. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  161. package/docs-site-docusaurus/static/.nojekyll +0 -0
  162. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  163. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  164. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  165. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  166. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  167. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  168. package/docs-site-docusaurus/static/img/logo.png +0 -0
  169. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  170. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  171. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  172. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  173. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  174. package/docs-site-docusaurus/static/robots.txt +4 -0
  175. package/docs-site-docusaurus/typedoc.json +23 -0
  176. package/package.json +109 -12
  177. package/src/data-structures/base/index.ts +1 -0
  178. package/src/data-structures/base/iterable-element-base.ts +4 -5
  179. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  180. package/src/data-structures/base/linear-base.ts +3 -3
  181. package/src/data-structures/binary-tree/avl-tree.ts +386 -51
  182. package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
  183. package/src/data-structures/binary-tree/binary-tree.ts +956 -81
  184. package/src/data-structures/binary-tree/bst.ts +840 -35
  185. package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
  186. package/src/data-structures/binary-tree/segment-tree.ts +498 -249
  187. package/src/data-structures/binary-tree/tree-map.ts +3784 -7
  188. package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
  189. package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
  190. package/src/data-structures/binary-tree/tree-set.ts +3531 -10
  191. package/src/data-structures/graph/abstract-graph.ts +4 -4
  192. package/src/data-structures/graph/directed-graph.ts +429 -47
  193. package/src/data-structures/graph/map-graph.ts +59 -1
  194. package/src/data-structures/graph/undirected-graph.ts +393 -59
  195. package/src/data-structures/hash/hash-map.ts +476 -92
  196. package/src/data-structures/heap/heap.ts +581 -99
  197. package/src/data-structures/heap/max-heap.ts +46 -0
  198. package/src/data-structures/heap/min-heap.ts +59 -0
  199. package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
  200. package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
  201. package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
  202. package/src/data-structures/matrix/matrix.ts +584 -12
  203. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  204. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  205. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  206. package/src/data-structures/queue/deque.ts +592 -70
  207. package/src/data-structures/queue/queue.ts +463 -42
  208. package/src/data-structures/stack/stack.ts +384 -32
  209. package/src/data-structures/trie/trie.ts +470 -48
  210. package/src/interfaces/graph.ts +1 -1
  211. package/src/types/common.ts +2 -2
  212. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  213. package/src/types/data-structures/heap/heap.ts +1 -0
  214. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  215. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  216. package/src/types/utils/validate-type.ts +4 -4
  217. package/vercel.json +6 -0
  218. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  219. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  220. package/dist/leetcode/avl-tree.mjs +0 -2720
  221. package/dist/leetcode/binary-tree.mjs +0 -1594
  222. package/dist/leetcode/bst.mjs +0 -2398
  223. package/dist/leetcode/deque.mjs +0 -683
  224. package/dist/leetcode/directed-graph.mjs +0 -1733
  225. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  226. package/dist/leetcode/hash-map.mjs +0 -493
  227. package/dist/leetcode/heap.mjs +0 -542
  228. package/dist/leetcode/max-heap.mjs +0 -375
  229. package/dist/leetcode/max-priority-queue.mjs +0 -383
  230. package/dist/leetcode/min-heap.mjs +0 -363
  231. package/dist/leetcode/min-priority-queue.mjs +0 -371
  232. package/dist/leetcode/priority-queue.mjs +0 -363
  233. package/dist/leetcode/queue.mjs +0 -943
  234. package/dist/leetcode/red-black-tree.mjs +0 -2765
  235. package/dist/leetcode/singly-linked-list.mjs +0 -754
  236. package/dist/leetcode/stack.mjs +0 -217
  237. package/dist/leetcode/tree-counter.mjs +0 -3039
  238. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  239. package/dist/leetcode/trie.mjs +0 -413
  240. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,1262 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/data-structures/base/iterable-entry-base.ts
7
+ var IterableEntryBase = class {
8
+ static {
9
+ __name(this, "IterableEntryBase");
10
+ }
11
+ /**
12
+ * Default iterator yielding `[key, value]` entries.
13
+ * @returns Iterator of `[K, V]`.
14
+ * @remarks Time O(n) to iterate, Space O(1)
15
+ */
16
+ *[Symbol.iterator](...args) {
17
+ yield* this._getIterator(...args);
18
+ }
19
+ /**
20
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
21
+ * @returns Iterator of `[K, V | undefined]`.
22
+ * @remarks Time O(n), Space O(1)
23
+ */
24
+ *entries() {
25
+ for (const item of this) {
26
+ yield item;
27
+ }
28
+ }
29
+ /**
30
+ * Iterate over keys only.
31
+ * @returns Iterator of keys.
32
+ * @remarks Time O(n), Space O(1)
33
+ */
34
+ *keys() {
35
+ for (const item of this) {
36
+ yield item[0];
37
+ }
38
+ }
39
+ /**
40
+ * Iterate over values only.
41
+ * @returns Iterator of values.
42
+ * @remarks Time O(n), Space O(1)
43
+ */
44
+ *values() {
45
+ for (const item of this) {
46
+ yield item[1];
47
+ }
48
+ }
49
+ /**
50
+ * Test whether all entries satisfy the predicate.
51
+ * @param predicate - `(key, value, index, self) => boolean`.
52
+ * @param thisArg - Optional `this` for callback.
53
+ * @returns `true` if all pass; otherwise `false`.
54
+ * @remarks Time O(n), Space O(1)
55
+ */
56
+ every(predicate, thisArg) {
57
+ let index = 0;
58
+ for (const item of this) {
59
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
60
+ return false;
61
+ }
62
+ }
63
+ return true;
64
+ }
65
+ /**
66
+ * Test whether any entry satisfies the predicate.
67
+ * @param predicate - `(key, value, index, self) => boolean`.
68
+ * @param thisArg - Optional `this` for callback.
69
+ * @returns `true` if any passes; otherwise `false`.
70
+ * @remarks Time O(n), Space O(1)
71
+ */
72
+ some(predicate, thisArg) {
73
+ let index = 0;
74
+ for (const item of this) {
75
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
76
+ return true;
77
+ }
78
+ }
79
+ return false;
80
+ }
81
+ /**
82
+ * Visit each entry, left-to-right.
83
+ * @param callbackfn - `(key, value, index, self) => void`.
84
+ * @param thisArg - Optional `this` for callback.
85
+ * @remarks Time O(n), Space O(1)
86
+ */
87
+ forEach(callbackfn, thisArg) {
88
+ let index = 0;
89
+ for (const item of this) {
90
+ const [key, value] = item;
91
+ callbackfn.call(thisArg, value, key, index++, this);
92
+ }
93
+ }
94
+ /**
95
+ * Find the first entry that matches a predicate.
96
+ * @param callbackfn - `(key, value, index, self) => boolean`.
97
+ * @param thisArg - Optional `this` for callback.
98
+ * @returns Matching `[key, value]` or `undefined`.
99
+ * @remarks Time O(n), Space O(1)
100
+ */
101
+ find(callbackfn, thisArg) {
102
+ let index = 0;
103
+ for (const item of this) {
104
+ const [key, value] = item;
105
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
106
+ }
107
+ return;
108
+ }
109
+ /**
110
+ * Whether the given key exists.
111
+ * @param key - Key to test.
112
+ * @returns `true` if found; otherwise `false`.
113
+ * @remarks Time O(n) generic, Space O(1)
114
+ */
115
+ has(key) {
116
+ for (const item of this) {
117
+ const [itemKey] = item;
118
+ if (itemKey === key) return true;
119
+ }
120
+ return false;
121
+ }
122
+ /**
123
+ * Whether there exists an entry with the given value.
124
+ * @param value - Value to test.
125
+ * @returns `true` if found; otherwise `false`.
126
+ * @remarks Time O(n), Space O(1)
127
+ */
128
+ hasValue(value) {
129
+ for (const [, elementValue] of this) {
130
+ if (elementValue === value) return true;
131
+ }
132
+ return false;
133
+ }
134
+ /**
135
+ * Get the value under a key.
136
+ * @param key - Key to look up.
137
+ * @returns Value or `undefined`.
138
+ * @remarks Time O(n) generic, Space O(1)
139
+ */
140
+ get(key) {
141
+ for (const item of this) {
142
+ const [itemKey, value] = item;
143
+ if (itemKey === key) return value;
144
+ }
145
+ return;
146
+ }
147
+ /**
148
+ * Reduce entries into a single accumulator.
149
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
150
+ * @param initialValue - Initial accumulator.
151
+ * @returns Final accumulator.
152
+ * @remarks Time O(n), Space O(1)
153
+ */
154
+ reduce(callbackfn, initialValue) {
155
+ let accumulator = initialValue;
156
+ let index = 0;
157
+ for (const item of this) {
158
+ const [key, value] = item;
159
+ accumulator = callbackfn(accumulator, value, key, index++, this);
160
+ }
161
+ return accumulator;
162
+ }
163
+ /**
164
+ * Converts data structure to `[key, value]` pairs.
165
+ * @returns Array of entries.
166
+ * @remarks Time O(n), Space O(n)
167
+ */
168
+ toArray() {
169
+ return [...this];
170
+ }
171
+ /**
172
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
173
+ * @returns Array of entries (default) or a string.
174
+ * @remarks Time O(n), Space O(n)
175
+ */
176
+ toVisual() {
177
+ return [...this];
178
+ }
179
+ /**
180
+ * Print a human-friendly representation to the console.
181
+ * @remarks Time O(n), Space O(n)
182
+ */
183
+ print() {
184
+ console.log(this.toVisual());
185
+ }
186
+ };
187
+
188
+ // src/utils/utils.ts
189
+ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
190
+ if (index < min || index > max) {
191
+ throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
192
+ }
193
+ }, "rangeCheck");
194
+ var isWeakKey = /* @__PURE__ */ __name((input) => {
195
+ const inputType = typeof input;
196
+ return inputType === "object" && input !== null || inputType === "function";
197
+ }, "isWeakKey");
198
+
199
+ // src/common/error.ts
200
+ var ERR = {
201
+ // Range / index
202
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
203
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
204
+ // Type / argument
205
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
206
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
207
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
208
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
209
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
210
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
211
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
212
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
213
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
214
+ // State / operation
215
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
216
+ // Matrix
217
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
218
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
219
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
220
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
221
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
222
+ };
223
+
224
+ // src/data-structures/hash/hash-map.ts
225
+ var HashMap = class extends IterableEntryBase {
226
+ static {
227
+ __name(this, "HashMap");
228
+ }
229
+ /**
230
+ * Create a HashMap and optionally bulk-insert entries.
231
+ * @remarks Time O(N), Space O(N)
232
+ * @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
233
+ * @param [options] - Options: hash function and optional record-to-entry converter.
234
+ * @returns New HashMap instance.
235
+ */
236
+ constructor(entryOrRawElements = [], options) {
237
+ super();
238
+ if (options) {
239
+ const { hashFn, toEntryFn } = options;
240
+ if (hashFn) this._hashFn = hashFn;
241
+ if (toEntryFn) this._toEntryFn = toEntryFn;
242
+ }
243
+ if (entryOrRawElements) this.setMany(entryOrRawElements);
244
+ }
245
+ _store = {};
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
+ _objMap = /* @__PURE__ */ new Map();
255
+ /**
256
+ * Get the internal Map used for object/function keys.
257
+ * @remarks Time O(1), Space O(1)
258
+ * @returns Map of object→value.
259
+ */
260
+ get objMap() {
261
+ return this._objMap;
262
+ }
263
+ _toEntryFn;
264
+ /**
265
+ * Get the raw→entry converter function if present.
266
+ * @remarks Time O(1), Space O(1)
267
+ * @returns Converter function or undefined.
268
+ */
269
+ get toEntryFn() {
270
+ return this._toEntryFn;
271
+ }
272
+ _size = 0;
273
+ /**
274
+ * Get the number of distinct keys stored.
275
+ * @remarks Time O(1), Space O(1)
276
+ * @returns Current size.
277
+ */
278
+ get size() {
279
+ return this._size;
280
+ }
281
+ _hashFn = /* @__PURE__ */ __name((key) => String(key), "_hashFn");
282
+ /**
283
+ * Get the current hash function for non-object keys.
284
+ * @remarks Time O(1), Space O(1)
285
+ * @returns Hash function.
286
+ */
287
+ get hashFn() {
288
+ return this._hashFn;
289
+ }
290
+ /**
291
+ * Check whether the map is empty.
292
+ * @remarks Time O(1), Space O(1)
293
+ * @returns True if size is 0.
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+ * @example
324
+ * // Check if empty
325
+ * const map = new HashMap();
326
+ * console.log(map.isEmpty()); // true;
327
+ */
328
+ isEmpty() {
329
+ return this._size === 0;
330
+ }
331
+ /**
332
+ * Remove all entries and reset counters.
333
+ * @remarks Time O(N), Space O(1)
334
+ * @returns void
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
+
361
+
362
+
363
+
364
+ * @example
365
+ * // Remove all entries
366
+ * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
367
+ * map.clear();
368
+ * console.log(map.isEmpty()); // true;
369
+ */
370
+ clear() {
371
+ this._store = {};
372
+ this._objMap.clear();
373
+ this._size = 0;
374
+ }
375
+ /**
376
+ * Type guard: check if a raw value is a [key, value] entry.
377
+ * @remarks Time O(1), Space O(1)
378
+ * @returns True if the value is a 2-tuple.
379
+ */
380
+ isEntry(rawElement) {
381
+ return Array.isArray(rawElement) && rawElement.length === 2;
382
+ }
383
+ /**
384
+ * Insert or replace a single entry.
385
+ * @remarks Time O(1), Space O(1)
386
+ * @param key - Key.
387
+ * @param value - Value.
388
+ * @returns True when the operation succeeds.
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
+
448
+
449
+
450
+
451
+ * @example
452
+ * // basic HashMap creation and set operation
453
+ * // Create a simple HashMap with key-value pairs
454
+ * const map = new HashMap<number, string>([
455
+ * [1, 'one'],
456
+ * [2, 'two'],
457
+ * [3, 'three']
458
+ * ]);
459
+ *
460
+ * // Verify size
461
+ * console.log(map.size); // 3;
462
+ *
463
+ * // Set a new key-value pair
464
+ * map.set(4, 'four');
465
+ * console.log(map.size); // 4;
466
+ *
467
+ * // Verify entries
468
+ * console.log([...map.entries()]); // length: 4;
469
+ */
470
+ set(key, value) {
471
+ if (this._isObjKey(key)) {
472
+ if (!this.objMap.has(key)) this._size++;
473
+ this.objMap.set(key, value);
474
+ } else {
475
+ const strKey = this._getNoObjKey(key);
476
+ if (this.store[strKey] === void 0) this._size++;
477
+ this._store[strKey] = { key, value };
478
+ }
479
+ return true;
480
+ }
481
+ /**
482
+ * Insert many entries from an iterable.
483
+ * @remarks Time O(N), Space O(N)
484
+ * @param entryOrRawElements - Iterable of entries or raw elements to insert.
485
+ * @returns Array of per-entry results.
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
+ * @example
516
+ * // Add multiple entries
517
+ * const map = new HashMap<string, number>();
518
+ * map.setMany([['a', 1], ['b', 2], ['c', 3]]);
519
+ * console.log(map.size); // 3;
520
+ */
521
+ setMany(entryOrRawElements) {
522
+ const results = [];
523
+ for (const rawEle of entryOrRawElements) {
524
+ let key, value;
525
+ if (this.isEntry(rawEle)) [key, value] = rawEle;
526
+ else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
527
+ if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
528
+ }
529
+ return results;
530
+ }
531
+ /**
532
+ * Get the value for a key.
533
+ * @remarks Time O(1), Space O(1)
534
+ * @param key - Key to look up.
535
+ * @returns Value or undefined.
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
+
564
+
565
+
566
+
567
+ * @example
568
+ * // HashMap get and has operations
569
+ * const map = new HashMap<string, number>([
570
+ * ['apple', 1],
571
+ * ['banana', 2],
572
+ * ['cherry', 3]
573
+ * ]);
574
+ *
575
+ * // Check if key exists
576
+ * console.log(map.has('apple')); // true;
577
+ * console.log(map.has('date')); // false;
578
+ *
579
+ * // Get value by key
580
+ * console.log(map.get('banana')); // 2;
581
+ * console.log(map.get('grape')); // undefined;
582
+ *
583
+ * // Get all keys and values
584
+ * const keys = [...map.keys()];
585
+ * const values = [...map.values()];
586
+ * console.log(keys); // contains 'apple';
587
+ * console.log(values); // contains 3;
588
+ */
589
+ get(key) {
590
+ if (this._isObjKey(key)) return this.objMap.get(key);
591
+ const strKey = this._getNoObjKey(key);
592
+ return this._store[strKey]?.value;
593
+ }
594
+ /**
595
+ * Check if a key exists.
596
+ * @remarks Time O(1), Space O(1)
597
+ * @param key - Key to test.
598
+ * @returns True if present.
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
+
628
+
629
+
630
+ * @example
631
+ * // Check key existence
632
+ * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
633
+ *
634
+ * console.log(map.has('a')); // true;
635
+ * console.log(map.has('z')); // false;
636
+ */
637
+ has(key) {
638
+ if (this._isObjKey(key)) return this.objMap.has(key);
639
+ const strKey = this._getNoObjKey(key);
640
+ return strKey in this.store;
641
+ }
642
+ /**
643
+ * Delete an entry by key.
644
+ * @remarks Time O(1), Space O(1)
645
+ * @param key - Key to delete.
646
+ * @returns True if the key was found and removed.
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
+
676
+
677
+
678
+ * @example
679
+ * // Remove entries by key
680
+ * const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
681
+ *
682
+ * console.log(map.delete('y')); // true;
683
+ * console.log(map.has('y')); // false;
684
+ * console.log(map.size); // 2;
685
+ */
686
+ delete(key) {
687
+ if (this._isObjKey(key)) {
688
+ if (this.objMap.has(key)) this._size--;
689
+ return this.objMap.delete(key);
690
+ }
691
+ const strKey = this._getNoObjKey(key);
692
+ if (strKey in this.store) {
693
+ delete this.store[strKey];
694
+ this._size--;
695
+ return true;
696
+ }
697
+ return false;
698
+ }
699
+ /**
700
+ * Replace the hash function and rehash the non-object store.
701
+ * @remarks Time O(N), Space O(N)
702
+ * @param fn - New hash function for non-object keys.
703
+ * @returns This map instance.
704
+ */
705
+ setHashFn(fn) {
706
+ if (this._hashFn === fn) return this;
707
+ this._hashFn = fn;
708
+ this._rehashNoObj();
709
+ return this;
710
+ }
711
+ /**
712
+ * Deep clone this map, preserving hashing behavior.
713
+ * @remarks Time O(N), Space O(N)
714
+ * @returns A new map with the same content.
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
+
742
+
743
+
744
+ * @example
745
+ * // Create independent copy
746
+ * const map = new HashMap<string, number>([['a', 1]]);
747
+ * const copy = map.clone();
748
+ * copy.set('a', 99);
749
+ * console.log(map.get('a')); // 1;
750
+ */
751
+ clone() {
752
+ const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
753
+ return this._createLike(this, opts);
754
+ }
755
+ /**
756
+ * Map values to a new map with the same keys.
757
+ * @remarks Time O(N), Space O(N)
758
+ * @template VM
759
+ * @param callbackfn - Mapping function (key, value, index, map) → newValue.
760
+ * @param [thisArg] - Value for `this` inside the callback.
761
+ * @returns A new map with transformed values.
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
+
791
+
792
+
793
+ * @example
794
+ * // Transform all values
795
+ * const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
796
+ *
797
+ * const doubled = prices.map(v => (v ?? 0) * 2);
798
+ * console.log(doubled.get('apple')); // 2;
799
+ * console.log(doubled.get('banana')); // 4;
800
+ */
801
+ map(callbackfn, thisArg) {
802
+ const out = this._createLike();
803
+ let index = 0;
804
+ for (const [key, value] of this) out.set(key, callbackfn.call(thisArg, value, key, index++, this));
805
+ return out;
806
+ }
807
+ /**
808
+ * Filter entries into a new map.
809
+ * @remarks Time O(N), Space O(N)
810
+ * @param predicate - Predicate (key, value, index, map) → boolean.
811
+ * @param [thisArg] - Value for `this` inside the predicate.
812
+ * @returns A new map containing entries that satisfied the predicate.
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
+
842
+
843
+
844
+ * @example
845
+ * // HashMap iteration and filter operations
846
+ * const map = new HashMap<number, string>([
847
+ * [1, 'Alice'],
848
+ * [2, 'Bob'],
849
+ * [3, 'Charlie'],
850
+ * [4, 'Diana'],
851
+ * [5, 'Eve']
852
+ * ]);
853
+ *
854
+ * // Iterate through entries
855
+ * const entries: [number, string][] = [];
856
+ * for (const [key, value] of map) {
857
+ * entries.push([key, value]);
858
+ * }
859
+ * console.log(entries); // length: 5;
860
+ *
861
+ * // Filter operation (for iteration with collection methods)
862
+ * const filtered = [...map].filter(([key]) => key > 2);
863
+ * console.log(filtered.length); // 3;
864
+ *
865
+ * // Map operation
866
+ * const values = [...map.values()].map(v => v.length);
867
+ * console.log(values); // contains 3; // 'Bob', 'Eve'
868
+ * console.log(values); // contains 7;
869
+ */
870
+ filter(predicate, thisArg) {
871
+ const out = this._createLike();
872
+ let index = 0;
873
+ for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
874
+ return out;
875
+ }
876
+ /**
877
+ * (Protected) Create a like-kind instance and seed it from an iterable.
878
+ * @remarks Time O(N), Space O(N)
879
+ * @template TK
880
+ * @template TV
881
+ * @template TR
882
+ * @param [entries] - Iterable used to seed the new map.
883
+ * @param [options] - Options forwarded to the constructor.
884
+ * @returns A like-kind map instance.
885
+ */
886
+ _createLike(entries = [], options) {
887
+ const Ctor = this.constructor;
888
+ return new Ctor(entries, options);
889
+ }
890
+ _rehashNoObj() {
891
+ const fresh = {};
892
+ for (const { key, value } of Object.values(this._store)) {
893
+ const sk = this._getNoObjKey(key);
894
+ fresh[sk] = { key, value };
895
+ }
896
+ this._store = fresh;
897
+ }
898
+ *_getIterator() {
899
+ for (const node of Object.values(this.store)) yield [node.key, node.value];
900
+ for (const node of this.objMap) yield node;
901
+ }
902
+ _isObjKey(key) {
903
+ const keyType = typeof key;
904
+ return (keyType === "object" || keyType === "function") && key !== null;
905
+ }
906
+ _getNoObjKey(key) {
907
+ const keyType = typeof key;
908
+ let strKey;
909
+ if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
910
+ strKey = this._hashFn(key);
911
+ } else {
912
+ if (keyType === "number") {
913
+ strKey = key;
914
+ } else {
915
+ strKey = key;
916
+ }
917
+ }
918
+ return strKey;
919
+ }
920
+ };
921
+ var LinkedHashMap = class extends IterableEntryBase {
922
+ static {
923
+ __name(this, "LinkedHashMap");
924
+ }
925
+ _sentinel;
926
+ /**
927
+ * Create a LinkedHashMap and optionally bulk-insert entries.
928
+ * @remarks Time O(N), Space O(N)
929
+ * @param [entryOrRawElements] - Iterable of entries or raw elements to insert.
930
+ * @param [options] - Options: hash functions and optional record-to-entry converter.
931
+ * @returns New LinkedHashMap instance.
932
+ */
933
+ constructor(entryOrRawElements = [], options) {
934
+ super();
935
+ this._sentinel = {};
936
+ this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
937
+ if (options) {
938
+ const { hashFn, objHashFn, toEntryFn } = options;
939
+ if (hashFn) this._hashFn = hashFn;
940
+ if (objHashFn) this._objHashFn = objHashFn;
941
+ if (toEntryFn) this._toEntryFn = toEntryFn;
942
+ }
943
+ if (entryOrRawElements) this.setMany(entryOrRawElements);
944
+ }
945
+ _hashFn = /* @__PURE__ */ __name((key) => String(key), "_hashFn");
946
+ get hashFn() {
947
+ return this._hashFn;
948
+ }
949
+ _objHashFn = /* @__PURE__ */ __name((key) => key, "_objHashFn");
950
+ /**
951
+ * Get the hash function for object/weak keys.
952
+ * @remarks Time O(1), Space O(1)
953
+ * @returns Object-hash function.
954
+ */
955
+ get objHashFn() {
956
+ return this._objHashFn;
957
+ }
958
+ _noObjMap = {};
959
+ /**
960
+ * Get the internal record for non-object keys.
961
+ * @remarks Time O(1), Space O(1)
962
+ * @returns Record of hash→node.
963
+ */
964
+ get noObjMap() {
965
+ return this._noObjMap;
966
+ }
967
+ _objMap = /* @__PURE__ */ new WeakMap();
968
+ get objMap() {
969
+ return this._objMap;
970
+ }
971
+ _head;
972
+ /**
973
+ * Get the head node (first entry) sentinel link.
974
+ * @remarks Time O(1), Space O(1)
975
+ * @returns Head node or sentinel.
976
+ */
977
+ get head() {
978
+ return this._head;
979
+ }
980
+ _tail;
981
+ /**
982
+ * Get the tail node (last entry) sentinel link.
983
+ * @remarks Time O(1), Space O(1)
984
+ * @returns Tail node or sentinel.
985
+ */
986
+ get tail() {
987
+ return this._tail;
988
+ }
989
+ _toEntryFn = /* @__PURE__ */ __name((rawElement) => {
990
+ if (this.isEntry(rawElement)) {
991
+ return rawElement;
992
+ }
993
+ throw new TypeError(
994
+ ERR.invalidArgument("If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.", "HashMap")
995
+ );
996
+ }, "_toEntryFn");
997
+ get toEntryFn() {
998
+ return this._toEntryFn;
999
+ }
1000
+ _size = 0;
1001
+ get size() {
1002
+ return this._size;
1003
+ }
1004
+ /**
1005
+ * Get the first [key, value] pair.
1006
+ * @remarks Time O(1), Space O(1)
1007
+ * @returns First entry or undefined when empty.
1008
+ */
1009
+ get first() {
1010
+ if (this._size === 0) return;
1011
+ return [this.head.key, this.head.value];
1012
+ }
1013
+ /**
1014
+ * Get the last [key, value] pair.
1015
+ * @remarks Time O(1), Space O(1)
1016
+ * @returns Last entry or undefined when empty.
1017
+ */
1018
+ get last() {
1019
+ if (this._size === 0) return;
1020
+ return [this.tail.key, this.tail.value];
1021
+ }
1022
+ /**
1023
+ * Iterate from head → tail.
1024
+ * @remarks Time O(N), Space O(1)
1025
+ * @returns Iterator of [key, value].
1026
+ */
1027
+ *begin() {
1028
+ let node = this.head;
1029
+ while (node !== this._sentinel) {
1030
+ yield [node.key, node.value];
1031
+ node = node.next;
1032
+ }
1033
+ }
1034
+ /**
1035
+ * Iterate from tail → head.
1036
+ * @remarks Time O(N), Space O(1)
1037
+ * @returns Iterator of [key, value].
1038
+ */
1039
+ *reverseBegin() {
1040
+ let node = this.tail;
1041
+ while (node !== this._sentinel) {
1042
+ yield [node.key, node.value];
1043
+ node = node.prev;
1044
+ }
1045
+ }
1046
+ /**
1047
+ * Insert or replace a single entry; preserves insertion order.
1048
+ * @remarks Time O(1), Space O(1)
1049
+ * @param key - Key.
1050
+ * @param [value] - Value.
1051
+ * @returns True when the operation succeeds.
1052
+ */
1053
+ set(key, value) {
1054
+ let node;
1055
+ const isNewKey = !this.has(key);
1056
+ if (isWeakKey(key)) {
1057
+ const hash = this._objHashFn(key);
1058
+ node = this.objMap.get(hash);
1059
+ if (!node && isNewKey) {
1060
+ node = { key: hash, value, prev: this.tail, next: this._sentinel };
1061
+ this.objMap.set(hash, node);
1062
+ } else if (node) {
1063
+ node.value = value;
1064
+ }
1065
+ } else {
1066
+ const hash = this._hashFn(key);
1067
+ node = this.noObjMap[hash];
1068
+ if (!node && isNewKey) {
1069
+ this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
1070
+ } else if (node) {
1071
+ node.value = value;
1072
+ }
1073
+ }
1074
+ if (node && isNewKey) {
1075
+ if (this._size === 0) {
1076
+ this._head = node;
1077
+ this._sentinel.next = node;
1078
+ } else {
1079
+ this.tail.next = node;
1080
+ node.prev = this.tail;
1081
+ }
1082
+ this._tail = node;
1083
+ this._sentinel.prev = node;
1084
+ this._size++;
1085
+ }
1086
+ return true;
1087
+ }
1088
+ setMany(entryOrRawElements) {
1089
+ const results = [];
1090
+ for (const rawEle of entryOrRawElements) {
1091
+ let key, value;
1092
+ if (this.isEntry(rawEle)) [key, value] = rawEle;
1093
+ else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
1094
+ if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
1095
+ }
1096
+ return results;
1097
+ }
1098
+ has(key) {
1099
+ if (isWeakKey(key)) {
1100
+ const hash2 = this._objHashFn(key);
1101
+ return this.objMap.has(hash2);
1102
+ }
1103
+ const hash = this._hashFn(key);
1104
+ return hash in this.noObjMap;
1105
+ }
1106
+ get(key) {
1107
+ if (isWeakKey(key)) {
1108
+ const hash2 = this._objHashFn(key);
1109
+ const node2 = this.objMap.get(hash2);
1110
+ return node2 ? node2.value : void 0;
1111
+ }
1112
+ const hash = this._hashFn(key);
1113
+ const node = this.noObjMap[hash];
1114
+ return node ? node.value : void 0;
1115
+ }
1116
+ /**
1117
+ * Get the value at a given index in insertion order.
1118
+ * @remarks Time O(N), Space O(1)
1119
+ * @param index - Zero-based index.
1120
+ * @returns Value at the index.
1121
+ */
1122
+ at(index) {
1123
+ rangeCheck(index, 0, this._size - 1);
1124
+ let node = this.head;
1125
+ while (index--) node = node.next;
1126
+ return node.value;
1127
+ }
1128
+ delete(key) {
1129
+ let node;
1130
+ if (isWeakKey(key)) {
1131
+ const hash = this._objHashFn(key);
1132
+ node = this.objMap.get(hash);
1133
+ if (!node) return false;
1134
+ this.objMap.delete(hash);
1135
+ } else {
1136
+ const hash = this._hashFn(key);
1137
+ node = this.noObjMap[hash];
1138
+ if (!node) return false;
1139
+ delete this.noObjMap[hash];
1140
+ }
1141
+ return this._deleteNode(node);
1142
+ }
1143
+ /**
1144
+ * Delete the first entry that matches a predicate.
1145
+ * @remarks Time O(N), Space O(1)
1146
+ * @param predicate - Function (key, value, index, map) → boolean to decide deletion.
1147
+ * @returns True if an entry was removed.
1148
+ */
1149
+ deleteWhere(predicate) {
1150
+ let node = this._head;
1151
+ let i = 0;
1152
+ while (node !== this._sentinel) {
1153
+ const cur = node;
1154
+ node = node.next;
1155
+ if (predicate(cur.key, cur.value, i++, this)) {
1156
+ const keyToCheck = cur.key;
1157
+ if (isWeakKey(keyToCheck)) {
1158
+ this._objMap.delete(keyToCheck);
1159
+ } else {
1160
+ const hash = this._hashFn(cur.key);
1161
+ delete this._noObjMap[hash];
1162
+ }
1163
+ return this._deleteNode(cur);
1164
+ }
1165
+ }
1166
+ return false;
1167
+ }
1168
+ /**
1169
+ * Delete the entry at a given index.
1170
+ * @remarks Time O(N), Space O(1)
1171
+ * @param index - Zero-based index.
1172
+ * @returns True if removed.
1173
+ */
1174
+ deleteAt(index) {
1175
+ rangeCheck(index, 0, this._size - 1);
1176
+ let node = this.head;
1177
+ while (index--) node = node.next;
1178
+ return this._deleteNode(node);
1179
+ }
1180
+ isEmpty() {
1181
+ return this._size === 0;
1182
+ }
1183
+ isEntry(rawElement) {
1184
+ return Array.isArray(rawElement) && rawElement.length === 2;
1185
+ }
1186
+ clear() {
1187
+ this._noObjMap = {};
1188
+ this._size = 0;
1189
+ this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
1190
+ }
1191
+ clone() {
1192
+ const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
1193
+ return this._createLike(this, opts);
1194
+ }
1195
+ filter(predicate, thisArg) {
1196
+ const out = this._createLike();
1197
+ let index = 0;
1198
+ for (const [key, value] of this) {
1199
+ if (predicate.call(thisArg, value, key, index, this)) out.set(key, value);
1200
+ index++;
1201
+ }
1202
+ return out;
1203
+ }
1204
+ /**
1205
+ * Map each entry to a new [key, value] pair and preserve order.
1206
+ * @remarks Time O(N), Space O(N)
1207
+ * @template MK
1208
+ * @template MV
1209
+ * @param callback - Mapping function (key, value, index, map) → [newKey, newValue].
1210
+ * @param [thisArg] - Value for `this` inside the callback.
1211
+ * @returns A new map of the same class with transformed entries.
1212
+ */
1213
+ map(callback, thisArg) {
1214
+ const out = this._createLike();
1215
+ let index = 0;
1216
+ for (const [key, value] of this) {
1217
+ const [newKey, newValue] = callback.call(thisArg, value, key, index, this);
1218
+ out.set(newKey, newValue);
1219
+ index++;
1220
+ }
1221
+ return out;
1222
+ }
1223
+ *_getIterator() {
1224
+ let node = this.head;
1225
+ while (node !== this._sentinel) {
1226
+ yield [node.key, node.value];
1227
+ node = node.next;
1228
+ }
1229
+ }
1230
+ _deleteNode(node) {
1231
+ const key = node.key;
1232
+ if (isWeakKey(key)) {
1233
+ this._objMap.delete(key);
1234
+ } else {
1235
+ const hash = this._hashFn(key);
1236
+ delete this._noObjMap[hash];
1237
+ }
1238
+ const { prev, next } = node;
1239
+ prev.next = next;
1240
+ next.prev = prev;
1241
+ if (node === this.head) this._head = next;
1242
+ if (node === this.tail) this._tail = prev;
1243
+ this._size -= 1;
1244
+ return true;
1245
+ }
1246
+ _createLike(entries = [], options) {
1247
+ const Ctor = this.constructor;
1248
+ return new Ctor(entries, options);
1249
+ }
1250
+ };
1251
+ /**
1252
+ * data-structure-typed
1253
+ *
1254
+ * @author Pablo Zeng
1255
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1256
+ * @license MIT License
1257
+ */
1258
+
1259
+ exports.HashMap = HashMap;
1260
+ exports.LinkedHashMap = LinkedHashMap;
1261
+ //# sourceMappingURL=hash.cjs.map
1262
+ //# sourceMappingURL=hash.cjs.map