@procedures/collections 0.0.0-stage → 0.1.0

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 (205) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +394 -2
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/async/async-queue.d.ts +155 -0
  6. package/dist/async/async-queue.d.ts.map +1 -0
  7. package/dist/async/async-queue.js +342 -0
  8. package/dist/async/async-queue.js.map +1 -0
  9. package/dist/async/channels.d.ts +188 -0
  10. package/dist/async/channels.d.ts.map +1 -0
  11. package/dist/async/channels.js +314 -0
  12. package/dist/async/channels.js.map +1 -0
  13. package/dist/behaviors/bounded.d.ts +79 -0
  14. package/dist/behaviors/bounded.d.ts.map +1 -0
  15. package/dist/behaviors/bounded.js +384 -0
  16. package/dist/behaviors/bounded.js.map +1 -0
  17. package/dist/behaviors/composite.d.ts +17 -0
  18. package/dist/behaviors/composite.d.ts.map +1 -0
  19. package/dist/behaviors/composite.js +93 -0
  20. package/dist/behaviors/composite.js.map +1 -0
  21. package/dist/behaviors/evented.d.ts +154 -0
  22. package/dist/behaviors/evented.d.ts.map +1 -0
  23. package/dist/behaviors/evented.js +299 -0
  24. package/dist/behaviors/evented.js.map +1 -0
  25. package/dist/behaviors/lru.d.ts +84 -0
  26. package/dist/behaviors/lru.d.ts.map +1 -0
  27. package/dist/behaviors/lru.js +376 -0
  28. package/dist/behaviors/lru.js.map +1 -0
  29. package/dist/behaviors/readonly.d.ts +73 -0
  30. package/dist/behaviors/readonly.d.ts.map +1 -0
  31. package/dist/behaviors/readonly.js +177 -0
  32. package/dist/behaviors/readonly.js.map +1 -0
  33. package/dist/behaviors/safe.d.ts +163 -0
  34. package/dist/behaviors/safe.d.ts.map +1 -0
  35. package/dist/behaviors/safe.js +176 -0
  36. package/dist/behaviors/safe.js.map +1 -0
  37. package/dist/behaviors/synchronized.d.ts +88 -0
  38. package/dist/behaviors/synchronized.d.ts.map +1 -0
  39. package/dist/behaviors/synchronized.js +285 -0
  40. package/dist/behaviors/synchronized.js.map +1 -0
  41. package/dist/behaviors/ttl.d.ts +118 -0
  42. package/dist/behaviors/ttl.d.ts.map +1 -0
  43. package/dist/behaviors/ttl.js +354 -0
  44. package/dist/behaviors/ttl.js.map +1 -0
  45. package/dist/contracts/model.d.ts +29 -0
  46. package/dist/contracts/model.d.ts.map +1 -0
  47. package/dist/contracts/model.js +26 -0
  48. package/dist/contracts/model.js.map +1 -0
  49. package/dist/core/effects.d.ts +176 -0
  50. package/dist/core/effects.d.ts.map +1 -0
  51. package/dist/core/effects.js +180 -0
  52. package/dist/core/effects.js.map +1 -0
  53. package/dist/core/events.d.ts +192 -0
  54. package/dist/core/events.d.ts.map +1 -0
  55. package/dist/core/events.js +291 -0
  56. package/dist/core/events.js.map +1 -0
  57. package/dist/core/middleware.d.ts +231 -0
  58. package/dist/core/middleware.d.ts.map +1 -0
  59. package/dist/core/middleware.js +161 -0
  60. package/dist/core/middleware.js.map +1 -0
  61. package/dist/core/policies.d.ts +220 -0
  62. package/dist/core/policies.d.ts.map +1 -0
  63. package/dist/core/policies.js +48 -0
  64. package/dist/core/policies.js.map +1 -0
  65. package/dist/core/traits.d.ts +142 -0
  66. package/dist/core/traits.d.ts.map +1 -0
  67. package/dist/core/traits.js +8 -0
  68. package/dist/core/traits.js.map +1 -0
  69. package/dist/fx/collectors.d.ts +193 -0
  70. package/dist/fx/collectors.d.ts.map +1 -0
  71. package/dist/fx/collectors.js +375 -0
  72. package/dist/fx/collectors.js.map +1 -0
  73. package/dist/fx/iter.d.ts +166 -0
  74. package/dist/fx/iter.d.ts.map +1 -0
  75. package/dist/fx/iter.js +421 -0
  76. package/dist/fx/iter.js.map +1 -0
  77. package/dist/impl/array-deque.d.ts +112 -0
  78. package/dist/impl/array-deque.d.ts.map +1 -0
  79. package/dist/impl/array-deque.js +380 -0
  80. package/dist/impl/array-deque.js.map +1 -0
  81. package/dist/impl/array-list.d.ts +101 -0
  82. package/dist/impl/array-list.d.ts.map +1 -0
  83. package/dist/impl/array-list.js +387 -0
  84. package/dist/impl/array-list.js.map +1 -0
  85. package/dist/impl/hash-map.d.ts +149 -0
  86. package/dist/impl/hash-map.d.ts.map +1 -0
  87. package/dist/impl/hash-map.js +473 -0
  88. package/dist/impl/hash-map.js.map +1 -0
  89. package/dist/impl/hash-set.d.ts +125 -0
  90. package/dist/impl/hash-set.d.ts.map +1 -0
  91. package/dist/impl/hash-set.js +264 -0
  92. package/dist/impl/hash-set.js.map +1 -0
  93. package/dist/impl/linked-hash-map.d.ts +141 -0
  94. package/dist/impl/linked-hash-map.d.ts.map +1 -0
  95. package/dist/impl/linked-hash-map.js +442 -0
  96. package/dist/impl/linked-hash-map.js.map +1 -0
  97. package/dist/impl/linked-list.d.ts +109 -0
  98. package/dist/impl/linked-list.d.ts.map +1 -0
  99. package/dist/impl/linked-list.js +494 -0
  100. package/dist/impl/linked-list.js.map +1 -0
  101. package/dist/impl/priority-queue.d.ts +139 -0
  102. package/dist/impl/priority-queue.d.ts.map +1 -0
  103. package/dist/impl/priority-queue.js +343 -0
  104. package/dist/impl/priority-queue.js.map +1 -0
  105. package/dist/impl/tree-map.d.ts +153 -0
  106. package/dist/impl/tree-map.d.ts.map +1 -0
  107. package/dist/impl/tree-map.js +788 -0
  108. package/dist/impl/tree-map.js.map +1 -0
  109. package/dist/impl/tree-set.d.ts +112 -0
  110. package/dist/impl/tree-set.d.ts.map +1 -0
  111. package/dist/impl/tree-set.js +374 -0
  112. package/dist/impl/tree-set.js.map +1 -0
  113. package/dist/index.d.ts +106 -0
  114. package/dist/index.d.ts.map +1 -0
  115. package/dist/index.js +139 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/interfaces/collection.d.ts +119 -0
  118. package/dist/interfaces/collection.d.ts.map +1 -0
  119. package/dist/interfaces/collection.js +8 -0
  120. package/dist/interfaces/collection.js.map +1 -0
  121. package/dist/interfaces/list.d.ts +247 -0
  122. package/dist/interfaces/list.d.ts.map +1 -0
  123. package/dist/interfaces/list.js +8 -0
  124. package/dist/interfaces/list.js.map +1 -0
  125. package/dist/interfaces/map.d.ts +344 -0
  126. package/dist/interfaces/map.d.ts.map +1 -0
  127. package/dist/interfaces/map.js +8 -0
  128. package/dist/interfaces/map.js.map +1 -0
  129. package/dist/interfaces/queue.d.ts +304 -0
  130. package/dist/interfaces/queue.d.ts.map +1 -0
  131. package/dist/interfaces/queue.js +8 -0
  132. package/dist/interfaces/queue.js.map +1 -0
  133. package/dist/interfaces/set.d.ts +137 -0
  134. package/dist/interfaces/set.d.ts.map +1 -0
  135. package/dist/interfaces/set.js +8 -0
  136. package/dist/interfaces/set.js.map +1 -0
  137. package/dist/register.d.ts +8 -0
  138. package/dist/register.d.ts.map +1 -0
  139. package/dist/register.js +8 -0
  140. package/dist/register.js.map +1 -0
  141. package/dist/storage/index.d.ts +15 -0
  142. package/dist/storage/index.d.ts.map +1 -0
  143. package/dist/storage/index.js +14 -0
  144. package/dist/storage/index.js.map +1 -0
  145. package/dist/storage/interface.d.ts +141 -0
  146. package/dist/storage/interface.d.ts.map +1 -0
  147. package/dist/storage/interface.js +23 -0
  148. package/dist/storage/interface.js.map +1 -0
  149. package/dist/storage/memory.d.ts +68 -0
  150. package/dist/storage/memory.d.ts.map +1 -0
  151. package/dist/storage/memory.js +154 -0
  152. package/dist/storage/memory.js.map +1 -0
  153. package/dist/utils/defaults.d.ts +135 -0
  154. package/dist/utils/defaults.d.ts.map +1 -0
  155. package/dist/utils/defaults.js +322 -0
  156. package/dist/utils/defaults.js.map +1 -0
  157. package/dist/utils/factories.d.ts +144 -0
  158. package/dist/utils/factories.d.ts.map +1 -0
  159. package/dist/utils/factories.js +293 -0
  160. package/dist/utils/factories.js.map +1 -0
  161. package/dist/utils/helpers.d.ts +137 -0
  162. package/dist/utils/helpers.d.ts.map +1 -0
  163. package/dist/utils/helpers.js +221 -0
  164. package/dist/utils/helpers.js.map +1 -0
  165. package/package.json +68 -3
  166. package/src/async/async-queue.ts +417 -0
  167. package/src/async/channels.ts +374 -0
  168. package/src/behaviors/bounded.ts +477 -0
  169. package/src/behaviors/composite.ts +99 -0
  170. package/src/behaviors/evented.ts +464 -0
  171. package/src/behaviors/lru.ts +447 -0
  172. package/src/behaviors/readonly.ts +224 -0
  173. package/src/behaviors/safe.ts +372 -0
  174. package/src/behaviors/synchronized.ts +316 -0
  175. package/src/behaviors/ttl.ts +462 -0
  176. package/src/contracts/model.ts +44 -0
  177. package/src/core/effects.ts +323 -0
  178. package/src/core/events.ts +356 -0
  179. package/src/core/middleware.ts +415 -0
  180. package/src/core/policies.ts +316 -0
  181. package/src/core/traits.ts +164 -0
  182. package/src/fx/collectors.ts +500 -0
  183. package/src/fx/iter.ts +536 -0
  184. package/src/impl/array-deque.ts +463 -0
  185. package/src/impl/array-list.ts +490 -0
  186. package/src/impl/hash-map.ts +623 -0
  187. package/src/impl/hash-set.ts +342 -0
  188. package/src/impl/linked-hash-map.ts +572 -0
  189. package/src/impl/linked-list.ts +606 -0
  190. package/src/impl/priority-queue.ts +448 -0
  191. package/src/impl/tree-map.ts +912 -0
  192. package/src/impl/tree-set.ts +469 -0
  193. package/src/index.ts +277 -0
  194. package/src/interfaces/collection.ts +141 -0
  195. package/src/interfaces/list.ts +281 -0
  196. package/src/interfaces/map.ts +405 -0
  197. package/src/interfaces/queue.ts +353 -0
  198. package/src/interfaces/set.ts +158 -0
  199. package/src/register.ts +8 -0
  200. package/src/storage/index.ts +16 -0
  201. package/src/storage/interface.ts +179 -0
  202. package/src/storage/memory.ts +180 -0
  203. package/src/utils/defaults.ts +345 -0
  204. package/src/utils/factories.ts +357 -0
  205. package/src/utils/helpers.ts +248 -0
@@ -0,0 +1,623 @@
1
+ /**
2
+ * HashMap implementation - A hash table with custom equality and hashing.
3
+ *
4
+ * Provides O(1) average case lookup, insertion, and deletion.
5
+ * Supports custom equality and hash functions for keys.
6
+ * Mirrors java.util.HashMap.
7
+ *
8
+ * @example
9
+ * const map = hashMap<string, number>()
10
+ * map.set('one', 1)
11
+ * map.set('two', 2)
12
+ * console.log(map.get('one')) // 1
13
+ *
14
+ * // Custom equality for object keys
15
+ * const userMap = hashMap<User, Data>({
16
+ * keyEq: (a, b) => a.id === b.id,
17
+ * keyHash: (u) => hashString(u.id)
18
+ * })
19
+ */
20
+
21
+ import type { MapLike, Entry } from "../interfaces/map.js";
22
+ import type { Eq, Hash } from "../core/traits.js";
23
+ import { defaultEq, defaultHash } from "../utils/defaults.js";
24
+
25
+ /**
26
+ * Internal entry node for hash buckets.
27
+ */
28
+ interface Node<K, V> {
29
+ key: K;
30
+ value: V;
31
+ hash: number;
32
+ next: Node<K, V> | null;
33
+ }
34
+
35
+ /**
36
+ * Options for creating a HashMap.
37
+ */
38
+ export interface HashMapOptions<K, V> {
39
+ /**
40
+ * Initial capacity (number of buckets).
41
+ * Must be a power of 2.
42
+ * @default 16
43
+ */
44
+ initialCapacity?: number;
45
+
46
+ /**
47
+ * Load factor threshold for resizing (0-1).
48
+ * When size > capacity * loadFactor, the map resizes.
49
+ * @default 0.75
50
+ */
51
+ loadFactor?: number;
52
+
53
+ /**
54
+ * Equality function for keys.
55
+ * @default defaultEq (===)
56
+ */
57
+ keyEq?: Eq<K>;
58
+
59
+ /**
60
+ * Hash function for keys.
61
+ * @default defaultHash
62
+ */
63
+ keyHash?: Hash<K>;
64
+
65
+ /**
66
+ * Equality function for values (used in containsValue).
67
+ * @default defaultEq (===)
68
+ */
69
+ valueEq?: Eq<V>;
70
+ }
71
+
72
+ /**
73
+ * HashMap<K, V> - Hash table implementation with custom equality/hashing.
74
+ *
75
+ * Uses separate chaining for collision resolution.
76
+ * Automatically resizes when load factor is exceeded.
77
+ *
78
+ * Not thread-safe. Use synchronized() behavior for concurrent access.
79
+ */
80
+ export class HashMap<K, V> implements MapLike<K, V> {
81
+ private _buckets: (Node<K, V> | null)[];
82
+ private _size: number;
83
+ private readonly _loadFactor: number;
84
+ private readonly _keyEq: Eq<K>;
85
+ private readonly _keyHash: Hash<K>;
86
+ private readonly _valueEq: Eq<V>;
87
+ private _threshold: number;
88
+ /**
89
+ * The count of structural changes (a key added or removed, a clear, a resize). An iterator
90
+ * records it at its start and throws when it changes. Before, a resize during iteration made
91
+ * the iterator skip or repeat entries (BUGS-2026-07 L15).
92
+ */
93
+ private _modCount = 0;
94
+
95
+ constructor(options: HashMapOptions<K, V> = {}) {
96
+ const {
97
+ initialCapacity = 16,
98
+ loadFactor = 0.75,
99
+ keyEq = defaultEq,
100
+ keyHash = defaultHash,
101
+ valueEq = defaultEq,
102
+ } = options;
103
+
104
+ const capacity = this.nextPowerOfTwo(Math.max(4, initialCapacity));
105
+ this._buckets = new Array(capacity).fill(null);
106
+ this._size = 0;
107
+ this._loadFactor = loadFactor;
108
+ this._keyEq = keyEq;
109
+ this._keyHash = keyHash;
110
+ this._valueEq = valueEq;
111
+ this._threshold = Math.floor(capacity * loadFactor);
112
+ }
113
+
114
+ // ========================================================================
115
+ // Size
116
+ // ========================================================================
117
+
118
+ get size(): number {
119
+ return this._size;
120
+ }
121
+
122
+ get isEmpty(): boolean {
123
+ return this._size === 0;
124
+ }
125
+
126
+ get keyEq(): Eq<K> {
127
+ return this._keyEq;
128
+ }
129
+
130
+ get valueEq(): Eq<V> {
131
+ return this._valueEq;
132
+ }
133
+
134
+ // ========================================================================
135
+ // Lookup
136
+ // ========================================================================
137
+
138
+ has(key: K): boolean {
139
+ const hash = this._keyHash(key);
140
+ const index = this.bucketIndex(hash);
141
+ let node = this._buckets[index] ?? null;
142
+
143
+ while (node !== null) {
144
+ if (node.hash === hash && this._keyEq(node.key, key)) {
145
+ return true;
146
+ }
147
+ node = node.next;
148
+ }
149
+
150
+ return false;
151
+ }
152
+
153
+ get(key: K): V {
154
+ const value = this.getOrUndefined(key);
155
+ if (value === undefined) {
156
+ throw new Error(`Key not found: ${key}`);
157
+ }
158
+ return value;
159
+ }
160
+
161
+ getOrUndefined(key: K): V | undefined {
162
+ const hash = this._keyHash(key);
163
+ const index = this.bucketIndex(hash);
164
+ let node = this._buckets[index] ?? null;
165
+
166
+ while (node !== null) {
167
+ if (node.hash === hash && this._keyEq(node.key, key)) {
168
+ return node.value;
169
+ }
170
+ node = node.next;
171
+ }
172
+
173
+ return undefined;
174
+ }
175
+
176
+ getOrDefault(key: K, defaultValue: V): V {
177
+ const value = this.getOrUndefined(key);
178
+ return value !== undefined ? value : defaultValue;
179
+ }
180
+
181
+ containsValue(value: V): boolean {
182
+ for (const bucket of this._buckets) {
183
+ let node = bucket;
184
+ while (node !== null) {
185
+ if (this._valueEq(node.value, value)) {
186
+ return true;
187
+ }
188
+ node = node.next;
189
+ }
190
+ }
191
+ return false;
192
+ }
193
+
194
+ // ========================================================================
195
+ // Modification
196
+ // ========================================================================
197
+
198
+ set(key: K, value: V): V | undefined {
199
+ const hash = this._keyHash(key);
200
+ const index = this.bucketIndex(hash);
201
+ let node = this._buckets[index] ?? null;
202
+
203
+ // Check if key already exists
204
+ while (node !== null) {
205
+ if (node.hash === hash && this._keyEq(node.key, key)) {
206
+ const oldValue = node.value;
207
+ node.value = value;
208
+ return oldValue;
209
+ }
210
+ node = node.next;
211
+ }
212
+
213
+ // Key doesn't exist, add new entry
214
+ const newNode: Node<K, V> = {
215
+ key,
216
+ value,
217
+ hash,
218
+ next: this._buckets[index] ?? null,
219
+ };
220
+ this._buckets[index] = newNode;
221
+ this._size++;
222
+ this._modCount++;
223
+
224
+ // Check if resize is needed
225
+ if (this._size > this._threshold) {
226
+ this.resize();
227
+ }
228
+
229
+ return undefined;
230
+ }
231
+
232
+ setIfAbsent(key: K, value: V): V {
233
+ const existing = this.getOrUndefined(key);
234
+ if (existing !== undefined) {
235
+ return existing;
236
+ }
237
+ this.set(key, value);
238
+ return value;
239
+ }
240
+
241
+ delete(key: K): V | undefined {
242
+ const hash = this._keyHash(key);
243
+ const index = this.bucketIndex(hash);
244
+ let node = this._buckets[index] ?? null;
245
+ let prev: Node<K, V> | null = null;
246
+
247
+ while (node !== null) {
248
+ if (node.hash === hash && this._keyEq(node.key, key)) {
249
+ // Found the node to delete
250
+ if (prev === null) {
251
+ // Node is at head of bucket
252
+ this._buckets[index] = node.next;
253
+ } else {
254
+ // Node is in middle or end
255
+ prev.next = node.next;
256
+ }
257
+ this._size--;
258
+ this._modCount++;
259
+ return node.value;
260
+ }
261
+ prev = node;
262
+ node = node.next;
263
+ }
264
+
265
+ return undefined;
266
+ }
267
+
268
+ deleteEntry(key: K, value: V): boolean {
269
+ const hash = this._keyHash(key);
270
+ const index = this.bucketIndex(hash);
271
+ let node = this._buckets[index] ?? null;
272
+ let prev: Node<K, V> | null = null;
273
+
274
+ while (node !== null) {
275
+ if (
276
+ node.hash === hash &&
277
+ this._keyEq(node.key, key) &&
278
+ this._valueEq(node.value, value)
279
+ ) {
280
+ // Found the entry to delete
281
+ if (prev === null) {
282
+ this._buckets[index] = node.next;
283
+ } else {
284
+ prev.next = node.next;
285
+ }
286
+ this._size--;
287
+ this._modCount++;
288
+ return true;
289
+ }
290
+ prev = node;
291
+ node = node.next;
292
+ }
293
+
294
+ return false;
295
+ }
296
+
297
+ replace(key: K, value: V): V | undefined {
298
+ const hash = this._keyHash(key);
299
+ const index = this.bucketIndex(hash);
300
+ let node = this._buckets[index] ?? null;
301
+
302
+ while (node !== null) {
303
+ if (node.hash === hash && this._keyEq(node.key, key)) {
304
+ const oldValue = node.value;
305
+ node.value = value;
306
+ return oldValue;
307
+ }
308
+ node = node.next;
309
+ }
310
+
311
+ return undefined;
312
+ }
313
+
314
+ replaceEntry(key: K, oldValue: V, newValue: V): boolean {
315
+ const hash = this._keyHash(key);
316
+ const index = this.bucketIndex(hash);
317
+ let node = this._buckets[index] ?? null;
318
+
319
+ while (node !== null) {
320
+ if (
321
+ node.hash === hash &&
322
+ this._keyEq(node.key, key) &&
323
+ this._valueEq(node.value, oldValue)
324
+ ) {
325
+ node.value = newValue;
326
+ return true;
327
+ }
328
+ node = node.next;
329
+ }
330
+
331
+ return false;
332
+ }
333
+
334
+ // ========================================================================
335
+ // Compute operations
336
+ // ========================================================================
337
+
338
+ computeIfAbsent(key: K, mappingFunction: (key: K) => V): V {
339
+ const existing = this.getOrUndefined(key);
340
+ if (existing !== undefined) {
341
+ return existing;
342
+ }
343
+
344
+ const newValue = mappingFunction(key);
345
+ this.set(key, newValue);
346
+ return newValue;
347
+ }
348
+
349
+ computeIfPresent(
350
+ key: K,
351
+ remappingFunction: (key: K, value: V) => V | undefined
352
+ ): V | undefined {
353
+ const existing = this.getOrUndefined(key);
354
+ if (existing === undefined) {
355
+ return undefined;
356
+ }
357
+
358
+ const newValue = remappingFunction(key, existing);
359
+ if (newValue === undefined) {
360
+ this.delete(key);
361
+ return undefined;
362
+ }
363
+
364
+ this.set(key, newValue);
365
+ return newValue;
366
+ }
367
+
368
+ compute(
369
+ key: K,
370
+ remappingFunction: (key: K, value: V | undefined) => V | undefined
371
+ ): V | undefined {
372
+ const existing = this.getOrUndefined(key);
373
+ const newValue = remappingFunction(key, existing);
374
+
375
+ if (newValue === undefined) {
376
+ if (existing !== undefined) {
377
+ this.delete(key);
378
+ }
379
+ return undefined;
380
+ }
381
+
382
+ this.set(key, newValue);
383
+ return newValue;
384
+ }
385
+
386
+ merge(
387
+ key: K,
388
+ value: V,
389
+ remappingFunction: (oldValue: V, newValue: V) => V | undefined
390
+ ): V | undefined {
391
+ const existing = this.getOrUndefined(key);
392
+
393
+ if (existing === undefined) {
394
+ this.set(key, value);
395
+ return value;
396
+ }
397
+
398
+ const merged = remappingFunction(existing, value);
399
+ if (merged === undefined) {
400
+ this.delete(key);
401
+ return undefined;
402
+ }
403
+
404
+ this.set(key, merged);
405
+ return merged;
406
+ }
407
+
408
+ // ========================================================================
409
+ // Bulk operations
410
+ // ========================================================================
411
+
412
+ putAll(other: MapLike<K, V> | Iterable<Entry<K, V>>): void {
413
+ for (const entry of other) {
414
+ this.set(entry.key, entry.value);
415
+ }
416
+ }
417
+
418
+ clear(): void {
419
+ this._buckets.fill(null);
420
+ this._size = 0;
421
+ this._modCount++;
422
+ }
423
+
424
+ // ========================================================================
425
+ // Views
426
+ // ========================================================================
427
+
428
+ *keys(): IterableIterator<K> {
429
+ const expected = this._modCount;
430
+ for (const bucket of this._buckets) {
431
+ let node = bucket;
432
+ while (node !== null) {
433
+ yield node.key;
434
+ this.checkUnchanged(expected);
435
+ node = node.next;
436
+ }
437
+ }
438
+ }
439
+
440
+ *values(): IterableIterator<V> {
441
+ const expected = this._modCount;
442
+ for (const bucket of this._buckets) {
443
+ let node = bucket;
444
+ while (node !== null) {
445
+ yield node.value;
446
+ this.checkUnchanged(expected);
447
+ node = node.next;
448
+ }
449
+ }
450
+ }
451
+
452
+ *entries(): IterableIterator<Entry<K, V>> {
453
+ const expected = this._modCount;
454
+ for (const bucket of this._buckets) {
455
+ let node = bucket;
456
+ while (node !== null) {
457
+ yield { key: node.key, value: node.value };
458
+ this.checkUnchanged(expected);
459
+ node = node.next;
460
+ }
461
+ }
462
+ }
463
+
464
+ // ========================================================================
465
+ // Iteration
466
+ // ========================================================================
467
+
468
+ *[Symbol.iterator](): Iterator<Entry<K, V>> {
469
+ yield* this.entries();
470
+ }
471
+
472
+ forEach(action: (value: V, key: K, map: this) => void): void {
473
+ for (const entry of this.entries()) {
474
+ action(entry.value, entry.key, this);
475
+ }
476
+ }
477
+
478
+ // ========================================================================
479
+ // Internal helpers
480
+ // ========================================================================
481
+
482
+ /** This method throws when the map changed structurally after an iterator started. */
483
+ private checkUnchanged(expected: number): void {
484
+ if (this._modCount !== expected) {
485
+ throw new Error("HashMap changed during iteration");
486
+ }
487
+ }
488
+
489
+ /**
490
+ * Returns the bucket index for a given hash.
491
+ */
492
+ private bucketIndex(hash: number): number {
493
+ // Use bitwise AND with (length - 1) for efficient modulo
494
+ // This works because length is always a power of 2
495
+ return hash & (this._buckets.length - 1);
496
+ }
497
+
498
+ /**
499
+ * Resizes the hash table to double its current capacity.
500
+ */
501
+ private resize(): void {
502
+ const oldBuckets = this._buckets;
503
+ const newCapacity = oldBuckets.length << 1; // Double capacity
504
+
505
+ if (newCapacity < 0) {
506
+ throw new Error("Map too large");
507
+ }
508
+
509
+ this._buckets = new Array(newCapacity).fill(null);
510
+ this._threshold = Math.floor(newCapacity * this._loadFactor);
511
+ this._modCount++;
512
+
513
+ // Rehash all entries
514
+ for (const bucket of oldBuckets) {
515
+ let node = bucket;
516
+ while (node !== null) {
517
+ const next = node.next;
518
+ const index = this.bucketIndex(node.hash);
519
+ node.next = this._buckets[index] ?? null;
520
+ this._buckets[index] = node;
521
+ node = next;
522
+ }
523
+ }
524
+ }
525
+
526
+ /**
527
+ * Returns the next power of 2 >= n.
528
+ */
529
+ private nextPowerOfTwo(n: number): number {
530
+ if (n <= 0) return 1;
531
+ n--;
532
+ n |= n >> 1;
533
+ n |= n >> 2;
534
+ n |= n >> 4;
535
+ n |= n >> 8;
536
+ n |= n >> 16;
537
+ return n + 1;
538
+ }
539
+
540
+ // ========================================================================
541
+ // Inspection
542
+ // ========================================================================
543
+
544
+ /**
545
+ * Returns the current number of buckets.
546
+ */
547
+ get capacity(): number {
548
+ return this._buckets.length;
549
+ }
550
+
551
+ /**
552
+ * Returns the current load factor (size / capacity).
553
+ */
554
+ get loadFactor(): number {
555
+ return this._size / this._buckets.length;
556
+ }
557
+
558
+ /**
559
+ * Returns statistics about bucket distribution (for debugging).
560
+ */
561
+ getStats(): {
562
+ size: number;
563
+ capacity: number;
564
+ loadFactor: number;
565
+ avgChainLength: number;
566
+ maxChainLength: number;
567
+ emptyBuckets: number;
568
+ } {
569
+ let maxChainLength = 0;
570
+ let totalChainLength = 0;
571
+ let emptyBuckets = 0;
572
+
573
+ for (const bucket of this._buckets) {
574
+ if (bucket === null) {
575
+ emptyBuckets++;
576
+ } else {
577
+ let length = 0;
578
+ let node: Node<K, V> | null = bucket;
579
+ while (node !== null) {
580
+ length++;
581
+ node = node.next;
582
+ }
583
+ totalChainLength += length;
584
+ maxChainLength = Math.max(maxChainLength, length);
585
+ }
586
+ }
587
+
588
+ return {
589
+ size: this._size,
590
+ capacity: this._buckets.length,
591
+ loadFactor: this.loadFactor,
592
+ avgChainLength: this._size > 0 ? totalChainLength / (this._buckets.length - emptyBuckets) : 0,
593
+ maxChainLength,
594
+ emptyBuckets,
595
+ };
596
+ }
597
+ }
598
+
599
+ /**
600
+ * Factory function to create a HashMap.
601
+ *
602
+ * @example
603
+ * const map = hashMap<string, number>()
604
+ * const mapWithOptions = hashMap<User, Data>({
605
+ * keyEq: (a, b) => a.id === b.id,
606
+ * keyHash: (u) => hashString(u.id)
607
+ * })
608
+ * const mapFromEntries = hashMap([['a', 1], ['b', 2]])
609
+ */
610
+ export function hashMap<K, V>(
611
+ optionsOrEntries?: HashMapOptions<K, V> | Iterable<readonly [K, V]>
612
+ ): HashMap<K, V> {
613
+ // Check if it's an iterable of entries
614
+ if (optionsOrEntries && Symbol.iterator in optionsOrEntries) {
615
+ const map = new HashMap<K, V>();
616
+ for (const [key, value] of optionsOrEntries as Iterable<readonly [K, V]>) {
617
+ map.set(key, value);
618
+ }
619
+ return map;
620
+ }
621
+
622
+ return new HashMap<K, V>(optionsOrEntries as HashMapOptions<K, V>);
623
+ }