@procedures/collections 0.47.1 → 0.47.2

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 (238) hide show
  1. package/CHANGELOG.md +18 -8
  2. package/README.md +2 -0
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +1 -59
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -95
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/index.d.ts +2 -2
  9. package/dist/storage/index.d.ts.map +1 -1
  10. package/dist/storage/index.js +1 -1
  11. package/dist/storage/index.js.map +1 -1
  12. package/dist/storage/memory.d.ts +1 -1
  13. package/dist/storage/memory.d.ts.map +1 -1
  14. package/dist/storage/memory.js +1 -1
  15. package/dist/storage/memory.js.map +1 -1
  16. package/dist/testing/index.d.ts +1 -1
  17. package/dist/testing/index.d.ts.map +1 -1
  18. package/dist/testing/index.js +1 -1
  19. package/dist/testing/index.js.map +1 -1
  20. package/dist/testing/storage-contract.js +1 -1
  21. package/dist/testing/storage-contract.js.map +1 -1
  22. package/dist/undo/index.d.ts +1 -1
  23. package/dist/undo/index.js +1 -1
  24. package/dist/undo/undo-log.d.ts +1 -1
  25. package/dist/undo/undo-log.js +1 -1
  26. package/package.json +4 -4
  27. package/src/index.ts +338 -214
  28. package/src/storage/index.ts +2 -2
  29. package/src/storage/memory.ts +1 -1
  30. package/src/testing/index.ts +1 -1
  31. package/src/testing/storage-contract.ts +1 -1
  32. package/src/undo/index.ts +1 -1
  33. package/src/undo/undo-log.ts +1 -1
  34. package/dist/async/async-queue.d.ts +0 -170
  35. package/dist/async/async-queue.d.ts.map +0 -1
  36. package/dist/async/async-queue.js +0 -404
  37. package/dist/async/async-queue.js.map +0 -1
  38. package/dist/async/channels.d.ts +0 -211
  39. package/dist/async/channels.d.ts.map +0 -1
  40. package/dist/async/channels.js +0 -393
  41. package/dist/async/channels.js.map +0 -1
  42. package/dist/async/settled.d.ts +0 -23
  43. package/dist/async/settled.d.ts.map +0 -1
  44. package/dist/async/settled.js +0 -32
  45. package/dist/async/settled.js.map +0 -1
  46. package/dist/behaviors/bounded.d.ts +0 -79
  47. package/dist/behaviors/bounded.d.ts.map +0 -1
  48. package/dist/behaviors/bounded.js +0 -393
  49. package/dist/behaviors/bounded.js.map +0 -1
  50. package/dist/behaviors/composite.d.ts +0 -17
  51. package/dist/behaviors/composite.d.ts.map +0 -1
  52. package/dist/behaviors/composite.js +0 -93
  53. package/dist/behaviors/composite.js.map +0 -1
  54. package/dist/behaviors/evented.d.ts +0 -154
  55. package/dist/behaviors/evented.d.ts.map +0 -1
  56. package/dist/behaviors/evented.js +0 -299
  57. package/dist/behaviors/evented.js.map +0 -1
  58. package/dist/behaviors/lru.d.ts +0 -84
  59. package/dist/behaviors/lru.d.ts.map +0 -1
  60. package/dist/behaviors/lru.js +0 -376
  61. package/dist/behaviors/lru.js.map +0 -1
  62. package/dist/behaviors/readonly.d.ts +0 -73
  63. package/dist/behaviors/readonly.d.ts.map +0 -1
  64. package/dist/behaviors/readonly.js +0 -189
  65. package/dist/behaviors/readonly.js.map +0 -1
  66. package/dist/behaviors/safe.d.ts +0 -163
  67. package/dist/behaviors/safe.d.ts.map +0 -1
  68. package/dist/behaviors/safe.js +0 -176
  69. package/dist/behaviors/safe.js.map +0 -1
  70. package/dist/behaviors/synchronized.d.ts +0 -88
  71. package/dist/behaviors/synchronized.d.ts.map +0 -1
  72. package/dist/behaviors/synchronized.js +0 -285
  73. package/dist/behaviors/synchronized.js.map +0 -1
  74. package/dist/behaviors/ttl.d.ts +0 -118
  75. package/dist/behaviors/ttl.d.ts.map +0 -1
  76. package/dist/behaviors/ttl.js +0 -354
  77. package/dist/behaviors/ttl.js.map +0 -1
  78. package/dist/contracts/model.d.ts +0 -29
  79. package/dist/contracts/model.d.ts.map +0 -1
  80. package/dist/contracts/model.js +0 -26
  81. package/dist/contracts/model.js.map +0 -1
  82. package/dist/core/effects.d.ts +0 -176
  83. package/dist/core/effects.d.ts.map +0 -1
  84. package/dist/core/effects.js +0 -180
  85. package/dist/core/effects.js.map +0 -1
  86. package/dist/core/events.d.ts +0 -192
  87. package/dist/core/events.d.ts.map +0 -1
  88. package/dist/core/events.js +0 -291
  89. package/dist/core/events.js.map +0 -1
  90. package/dist/core/middleware.d.ts +0 -231
  91. package/dist/core/middleware.d.ts.map +0 -1
  92. package/dist/core/middleware.js +0 -161
  93. package/dist/core/middleware.js.map +0 -1
  94. package/dist/core/policies.d.ts +0 -220
  95. package/dist/core/policies.d.ts.map +0 -1
  96. package/dist/core/policies.js +0 -48
  97. package/dist/core/policies.js.map +0 -1
  98. package/dist/core/traits.d.ts +0 -142
  99. package/dist/core/traits.d.ts.map +0 -1
  100. package/dist/core/traits.js +0 -8
  101. package/dist/core/traits.js.map +0 -1
  102. package/dist/fx/collectors.d.ts +0 -193
  103. package/dist/fx/collectors.d.ts.map +0 -1
  104. package/dist/fx/collectors.js +0 -375
  105. package/dist/fx/collectors.js.map +0 -1
  106. package/dist/fx/iter.d.ts +0 -166
  107. package/dist/fx/iter.d.ts.map +0 -1
  108. package/dist/fx/iter.js +0 -421
  109. package/dist/fx/iter.js.map +0 -1
  110. package/dist/impl/array-deque.d.ts +0 -112
  111. package/dist/impl/array-deque.d.ts.map +0 -1
  112. package/dist/impl/array-deque.js +0 -380
  113. package/dist/impl/array-deque.js.map +0 -1
  114. package/dist/impl/array-list.d.ts +0 -101
  115. package/dist/impl/array-list.d.ts.map +0 -1
  116. package/dist/impl/array-list.js +0 -387
  117. package/dist/impl/array-list.js.map +0 -1
  118. package/dist/impl/hash-map.d.ts +0 -149
  119. package/dist/impl/hash-map.d.ts.map +0 -1
  120. package/dist/impl/hash-map.js +0 -473
  121. package/dist/impl/hash-map.js.map +0 -1
  122. package/dist/impl/hash-set.d.ts +0 -125
  123. package/dist/impl/hash-set.d.ts.map +0 -1
  124. package/dist/impl/hash-set.js +0 -264
  125. package/dist/impl/hash-set.js.map +0 -1
  126. package/dist/impl/linked-hash-map.d.ts +0 -141
  127. package/dist/impl/linked-hash-map.d.ts.map +0 -1
  128. package/dist/impl/linked-hash-map.js +0 -442
  129. package/dist/impl/linked-hash-map.js.map +0 -1
  130. package/dist/impl/linked-list.d.ts +0 -109
  131. package/dist/impl/linked-list.d.ts.map +0 -1
  132. package/dist/impl/linked-list.js +0 -494
  133. package/dist/impl/linked-list.js.map +0 -1
  134. package/dist/impl/priority-queue.d.ts +0 -139
  135. package/dist/impl/priority-queue.d.ts.map +0 -1
  136. package/dist/impl/priority-queue.js +0 -343
  137. package/dist/impl/priority-queue.js.map +0 -1
  138. package/dist/impl/summary-tree.d.ts +0 -146
  139. package/dist/impl/summary-tree.d.ts.map +0 -1
  140. package/dist/impl/summary-tree.js +0 -524
  141. package/dist/impl/summary-tree.js.map +0 -1
  142. package/dist/impl/tree-map.d.ts +0 -153
  143. package/dist/impl/tree-map.d.ts.map +0 -1
  144. package/dist/impl/tree-map.js +0 -788
  145. package/dist/impl/tree-map.js.map +0 -1
  146. package/dist/impl/tree-set.d.ts +0 -112
  147. package/dist/impl/tree-set.d.ts.map +0 -1
  148. package/dist/impl/tree-set.js +0 -374
  149. package/dist/impl/tree-set.js.map +0 -1
  150. package/dist/interfaces/collection.d.ts +0 -119
  151. package/dist/interfaces/collection.d.ts.map +0 -1
  152. package/dist/interfaces/collection.js +0 -8
  153. package/dist/interfaces/collection.js.map +0 -1
  154. package/dist/interfaces/list.d.ts +0 -247
  155. package/dist/interfaces/list.d.ts.map +0 -1
  156. package/dist/interfaces/list.js +0 -8
  157. package/dist/interfaces/list.js.map +0 -1
  158. package/dist/interfaces/map.d.ts +0 -344
  159. package/dist/interfaces/map.d.ts.map +0 -1
  160. package/dist/interfaces/map.js +0 -8
  161. package/dist/interfaces/map.js.map +0 -1
  162. package/dist/interfaces/queue.d.ts +0 -304
  163. package/dist/interfaces/queue.d.ts.map +0 -1
  164. package/dist/interfaces/queue.js +0 -8
  165. package/dist/interfaces/queue.js.map +0 -1
  166. package/dist/interfaces/set.d.ts +0 -137
  167. package/dist/interfaces/set.d.ts.map +0 -1
  168. package/dist/interfaces/set.js +0 -8
  169. package/dist/interfaces/set.js.map +0 -1
  170. package/dist/order/index.d.ts +0 -7
  171. package/dist/order/index.d.ts.map +0 -1
  172. package/dist/order/index.js +0 -7
  173. package/dist/order/index.js.map +0 -1
  174. package/dist/order/rank.d.ts +0 -100
  175. package/dist/order/rank.d.ts.map +0 -1
  176. package/dist/order/rank.js +0 -286
  177. package/dist/order/rank.js.map +0 -1
  178. package/dist/storage/listeners.d.ts +0 -39
  179. package/dist/storage/listeners.d.ts.map +0 -1
  180. package/dist/storage/listeners.js +0 -65
  181. package/dist/storage/listeners.js.map +0 -1
  182. package/dist/testing/check.d.ts +0 -25
  183. package/dist/testing/check.d.ts.map +0 -1
  184. package/dist/testing/check.js +0 -85
  185. package/dist/testing/check.js.map +0 -1
  186. package/dist/utils/defaults.d.ts +0 -135
  187. package/dist/utils/defaults.d.ts.map +0 -1
  188. package/dist/utils/defaults.js +0 -322
  189. package/dist/utils/defaults.js.map +0 -1
  190. package/dist/utils/factories.d.ts +0 -144
  191. package/dist/utils/factories.d.ts.map +0 -1
  192. package/dist/utils/factories.js +0 -293
  193. package/dist/utils/factories.js.map +0 -1
  194. package/dist/utils/helpers.d.ts +0 -137
  195. package/dist/utils/helpers.d.ts.map +0 -1
  196. package/dist/utils/helpers.js +0 -221
  197. package/dist/utils/helpers.js.map +0 -1
  198. package/src/async/async-queue.ts +0 -481
  199. package/src/async/channels.ts +0 -450
  200. package/src/async/settled.ts +0 -41
  201. package/src/behaviors/bounded.ts +0 -485
  202. package/src/behaviors/composite.ts +0 -99
  203. package/src/behaviors/evented.ts +0 -464
  204. package/src/behaviors/lru.ts +0 -447
  205. package/src/behaviors/readonly.ts +0 -236
  206. package/src/behaviors/safe.ts +0 -372
  207. package/src/behaviors/synchronized.ts +0 -316
  208. package/src/behaviors/ttl.ts +0 -462
  209. package/src/contracts/model.ts +0 -44
  210. package/src/core/effects.ts +0 -323
  211. package/src/core/events.ts +0 -356
  212. package/src/core/middleware.ts +0 -415
  213. package/src/core/policies.ts +0 -316
  214. package/src/core/traits.ts +0 -164
  215. package/src/fx/collectors.ts +0 -500
  216. package/src/fx/iter.ts +0 -536
  217. package/src/impl/array-deque.ts +0 -463
  218. package/src/impl/array-list.ts +0 -490
  219. package/src/impl/hash-map.ts +0 -623
  220. package/src/impl/hash-set.ts +0 -342
  221. package/src/impl/linked-hash-map.ts +0 -572
  222. package/src/impl/linked-list.ts +0 -606
  223. package/src/impl/priority-queue.ts +0 -448
  224. package/src/impl/summary-tree.ts +0 -586
  225. package/src/impl/tree-map.ts +0 -912
  226. package/src/impl/tree-set.ts +0 -469
  227. package/src/interfaces/collection.ts +0 -141
  228. package/src/interfaces/list.ts +0 -281
  229. package/src/interfaces/map.ts +0 -405
  230. package/src/interfaces/queue.ts +0 -353
  231. package/src/interfaces/set.ts +0 -158
  232. package/src/order/index.ts +0 -6
  233. package/src/order/rank.ts +0 -271
  234. package/src/storage/listeners.ts +0 -72
  235. package/src/testing/check.ts +0 -81
  236. package/src/utils/defaults.ts +0 -345
  237. package/src/utils/factories.ts +0 -357
  238. package/src/utils/helpers.ts +0 -248
@@ -1,623 +0,0 @@
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
- }