@procedures/collections 0.0.0-stage → 0.2.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 +9 -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,447 @@
1
+ /**
2
+ * LRU (Least Recently Used) eviction behavior.
3
+ *
4
+ * Tracks access times and automatically evicts the least recently used
5
+ * elements when capacity is reached. Perfect for caching.
6
+ *
7
+ * @example
8
+ * const cache = compose(
9
+ * lruMap({ capacity: 100 })
10
+ * )(hashMap<string, Data>())
11
+ *
12
+ * cache.set('key1', data1) // Accessed
13
+ * cache.get('key1') // Accessed again (moves to front)
14
+ * // When full, least recently accessed keys are evicted
15
+ */
16
+
17
+ import type { Middleware } from "../core/middleware.js";
18
+ import type { MapLike } from "../interfaces/map.js";
19
+ import { compositeMapMethod } from "./composite.js";
20
+
21
+ /**
22
+ * Options for LRU behavior.
23
+ */
24
+ export interface LRUOptions {
25
+ /**
26
+ * Maximum capacity. When exceeded, least recently used items are evicted.
27
+ */
28
+ capacity: number;
29
+
30
+ /**
31
+ * Optional callback invoked when an item is evicted.
32
+ */
33
+ onEvict?: (info: { key: any; value: any; timestamp: number }) => void;
34
+ }
35
+
36
+ /**
37
+ * Metadata for tracking LRU access.
38
+ */
39
+ interface LRUNode<K, V> {
40
+ key: K;
41
+ value: V;
42
+ lastAccessed: number;
43
+ prev: LRUNode<K, V> | null;
44
+ next: LRUNode<K, V> | null;
45
+ }
46
+
47
+ /**
48
+ * Creates an LRU Map middleware.
49
+ *
50
+ * Maintains a doubly-linked list to track access order.
51
+ * Most recently accessed items are at the head.
52
+ * When capacity is exceeded, items at the tail are evicted.
53
+ */
54
+ export function lruMap<K, V>(
55
+ options: LRUOptions
56
+ ): Middleware<MapLike<K, V>, MapLike<K, V> & { readonly isFull: boolean }> {
57
+ const { capacity, onEvict } = options;
58
+
59
+ return (next: MapLike<K, V>): MapLike<K, V> & { readonly isFull: boolean } => {
60
+ // Doubly-linked list for LRU tracking
61
+ let head: LRUNode<K, V> | null = null;
62
+ let tail: LRUNode<K, V> | null = null;
63
+ const nodeMap = new Map<K, LRUNode<K, V>>();
64
+
65
+ const moveToHead = (node: LRUNode<K, V>): void => {
66
+ // Remove from current position
67
+ if (node.prev) {
68
+ node.prev.next = node.next;
69
+ } else {
70
+ head = node.next;
71
+ }
72
+
73
+ if (node.next) {
74
+ node.next.prev = node.prev;
75
+ } else {
76
+ tail = node.prev;
77
+ }
78
+
79
+ // Add to head
80
+ node.prev = null;
81
+ node.next = head;
82
+ if (head) {
83
+ head.prev = node;
84
+ }
85
+ head = node;
86
+
87
+ if (!tail) {
88
+ tail = node;
89
+ }
90
+
91
+ node.lastAccessed = Date.now();
92
+ };
93
+
94
+ const addNode = (key: K, value: V): void => {
95
+ const node: LRUNode<K, V> = {
96
+ key,
97
+ value,
98
+ lastAccessed: Date.now(),
99
+ prev: null,
100
+ next: head,
101
+ };
102
+
103
+ if (head) {
104
+ head.prev = node;
105
+ }
106
+ head = node;
107
+
108
+ if (!tail) {
109
+ tail = node;
110
+ }
111
+
112
+ nodeMap.set(key, node);
113
+ };
114
+
115
+ const removeNode = (node: LRUNode<K, V>): void => {
116
+ if (node.prev) {
117
+ node.prev.next = node.next;
118
+ } else {
119
+ head = node.next;
120
+ }
121
+
122
+ if (node.next) {
123
+ node.next.prev = node.prev;
124
+ } else {
125
+ tail = node.prev;
126
+ }
127
+
128
+ nodeMap.delete(node.key);
129
+ };
130
+
131
+ // A layer below this one (for example ttlMap, in compose(lruMap, ttlMap)) can remove an
132
+ // entry without this layer seeing it. Its node is then stale: the list must not keep it
133
+ // (it holds the value, and it would grow without bound) and evicting it is not an eviction.
134
+ const dropStaleNodes = (): void => {
135
+ for (const node of [...nodeMap.values()]) {
136
+ if (!next.has(node.key)) {
137
+ removeNode(node);
138
+ }
139
+ }
140
+ };
141
+
142
+ const evictLRU = (): void => {
143
+ while (tail) {
144
+ const candidate = tail;
145
+ removeNode(candidate);
146
+ if (!next.has(candidate.key)) {
147
+ // Already removed below: skip it and evict the next least recently used entry
148
+ continue;
149
+ }
150
+ next.delete(candidate.key);
151
+
152
+ if (onEvict) {
153
+ onEvict({
154
+ key: candidate.key,
155
+ value: candidate.value,
156
+ timestamp: candidate.lastAccessed,
157
+ });
158
+ }
159
+ return;
160
+ }
161
+ };
162
+
163
+ return new Proxy(next, {
164
+ get(target, prop, receiver) {
165
+ // Add isFull property
166
+ if (prop === "isFull") {
167
+ return target.size >= capacity;
168
+ }
169
+
170
+ // Composite methods (setIfAbsent, compute, merge, putAll and others) go through this
171
+ // proxy's own primitives, so they keep this behavior's rules (BUGS-2026-07 L13)
172
+ const composite = compositeMapMethod(receiver as MapLike<K, V>, prop);
173
+ if (composite) {
174
+ return composite;
175
+ }
176
+
177
+ const value = Reflect.get(target, prop, receiver);
178
+
179
+ if (typeof value === "function") {
180
+ switch (prop) {
181
+ case "get":
182
+ return function (key: K): V {
183
+ let result: V;
184
+ try {
185
+ result = (value as Function).call(target, key);
186
+ } catch (error) {
187
+ // The entry is gone below (for example expired): forget its node
188
+ const stale = nodeMap.get(key);
189
+ if (stale) {
190
+ removeNode(stale);
191
+ }
192
+ throw error;
193
+ }
194
+ // Update access time
195
+ const node = nodeMap.get(key);
196
+ if (node) {
197
+ moveToHead(node);
198
+ }
199
+ return result;
200
+ };
201
+
202
+ case "has":
203
+ return function (key: K): boolean {
204
+ const result = (value as Function).call(target, key);
205
+ const node = nodeMap.get(key);
206
+ if (node) {
207
+ if (result) {
208
+ // Update access time
209
+ moveToHead(node);
210
+ } else {
211
+ // The entry is gone below (for example expired): forget its node
212
+ removeNode(node);
213
+ }
214
+ }
215
+ return result;
216
+ };
217
+
218
+ case "set":
219
+ return function (key: K, val: V): V | undefined {
220
+ const hadKey = target.has(key);
221
+
222
+ if (hadKey) {
223
+ // Update existing
224
+ const oldValue = (value as Function).call(target, key, val);
225
+ const node = nodeMap.get(key);
226
+ if (node) {
227
+ node.value = val;
228
+ moveToHead(node);
229
+ }
230
+ return oldValue;
231
+ } else {
232
+ // Add new. A stale node for this key (its entry was removed below) must not
233
+ // stay in the list: evicting it later would delete the new node's map entry.
234
+ const stale = nodeMap.get(key);
235
+ if (stale) {
236
+ removeNode(stale);
237
+ }
238
+ // Keep the node list bounded: when it reaches capacity, drop stale nodes first
239
+ if (nodeMap.size >= capacity) {
240
+ dropStaleNodes();
241
+ }
242
+ if (target.size >= capacity) {
243
+ evictLRU();
244
+ }
245
+ const result = (value as Function).call(target, key, val);
246
+ addNode(key, val);
247
+ return result;
248
+ }
249
+ };
250
+
251
+ case "delete":
252
+ return function (key: K): V | undefined {
253
+ const result = (value as Function).call(target, key);
254
+ const node = nodeMap.get(key);
255
+ if (node) {
256
+ removeNode(node);
257
+ }
258
+ return result;
259
+ };
260
+
261
+ case "clear":
262
+ return function (): void {
263
+ (value as Function).call(target);
264
+ head = null;
265
+ tail = null;
266
+ nodeMap.clear();
267
+ };
268
+ }
269
+ }
270
+
271
+ return value;
272
+ },
273
+ }) as MapLike<K, V> & { readonly isFull: boolean };
274
+ };
275
+ }
276
+
277
+ /**
278
+ * Creates an LRU cache with a simpler API.
279
+ * This is a standalone LRU cache implementation.
280
+ *
281
+ * @example
282
+ * const cache = lruCache<string, Data>(100)
283
+ * cache.set('key', value)
284
+ * cache.get('key') // Moves to front
285
+ */
286
+ export class LRUCache<K, V> {
287
+ private map: Map<K, { value: V; node: LRUNode<K, V> }> = new Map();
288
+ private head: LRUNode<K, V> | null = null;
289
+ private tail: LRUNode<K, V> | null = null;
290
+
291
+ constructor(
292
+ private readonly capacity: number,
293
+ private readonly onEvict?: (key: K, value: V) => void
294
+ ) {}
295
+
296
+ get size(): number {
297
+ return this.map.size;
298
+ }
299
+
300
+ get isFull(): boolean {
301
+ return this.map.size >= this.capacity;
302
+ }
303
+
304
+ get(key: K): V | undefined {
305
+ const entry = this.map.get(key);
306
+ if (!entry) return undefined;
307
+
308
+ this.moveToHead(entry.node);
309
+ return entry.value;
310
+ }
311
+
312
+ set(key: K, value: V): void {
313
+ const entry = this.map.get(key);
314
+
315
+ if (entry) {
316
+ // Update existing
317
+ entry.value = value;
318
+ entry.node.value = value;
319
+ this.moveToHead(entry.node);
320
+ } else {
321
+ // Add new
322
+ if (this.map.size >= this.capacity) {
323
+ this.evictLRU();
324
+ }
325
+
326
+ const node: LRUNode<K, V> = {
327
+ key,
328
+ value,
329
+ lastAccessed: Date.now(),
330
+ prev: null,
331
+ next: this.head,
332
+ };
333
+
334
+ if (this.head) {
335
+ this.head.prev = node;
336
+ }
337
+ this.head = node;
338
+
339
+ if (!this.tail) {
340
+ this.tail = node;
341
+ }
342
+
343
+ this.map.set(key, { value, node });
344
+ }
345
+ }
346
+
347
+ has(key: K): boolean {
348
+ return this.map.has(key);
349
+ }
350
+
351
+ delete(key: K): boolean {
352
+ const entry = this.map.get(key);
353
+ if (!entry) return false;
354
+
355
+ this.removeNode(entry.node);
356
+ this.map.delete(key);
357
+ return true;
358
+ }
359
+
360
+ clear(): void {
361
+ this.map.clear();
362
+ this.head = null;
363
+ this.tail = null;
364
+ }
365
+
366
+ private moveToHead(node: LRUNode<K, V>): void {
367
+ if (node === this.head) return;
368
+
369
+ // Remove from current position
370
+ if (node.prev) {
371
+ node.prev.next = node.next;
372
+ }
373
+ if (node.next) {
374
+ node.next.prev = node.prev;
375
+ } else {
376
+ this.tail = node.prev;
377
+ }
378
+
379
+ // Add to head
380
+ node.prev = null;
381
+ node.next = this.head;
382
+ if (this.head) {
383
+ this.head.prev = node;
384
+ }
385
+ this.head = node;
386
+
387
+ node.lastAccessed = Date.now();
388
+ }
389
+
390
+ private removeNode(node: LRUNode<K, V>): void {
391
+ if (node.prev) {
392
+ node.prev.next = node.next;
393
+ } else {
394
+ this.head = node.next;
395
+ }
396
+
397
+ if (node.next) {
398
+ node.next.prev = node.prev;
399
+ } else {
400
+ this.tail = node.prev;
401
+ }
402
+ }
403
+
404
+ private evictLRU(): void {
405
+ if (!this.tail) return;
406
+
407
+ const evicted = this.tail;
408
+ this.removeNode(evicted);
409
+ this.map.delete(evicted.key);
410
+
411
+ if (this.onEvict) {
412
+ this.onEvict(evicted.key, evicted.value);
413
+ }
414
+ }
415
+
416
+ /**
417
+ * Returns keys in LRU order (most recent first).
418
+ */
419
+ *keys(): IterableIterator<K> {
420
+ let node = this.head;
421
+ while (node) {
422
+ yield node.key;
423
+ node = node.next;
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Returns entries in LRU order (most recent first).
429
+ */
430
+ *entries(): IterableIterator<[K, V]> {
431
+ let node = this.head;
432
+ while (node) {
433
+ yield [node.key, node.value];
434
+ node = node.next;
435
+ }
436
+ }
437
+ }
438
+
439
+ /**
440
+ * Factory function to create an LRU cache.
441
+ */
442
+ export function lruCache<K, V>(
443
+ capacity: number,
444
+ onEvict?: (key: K, value: V) => void
445
+ ): LRUCache<K, V> {
446
+ return new LRUCache(capacity, onEvict);
447
+ }
@@ -0,0 +1,224 @@
1
+ /**
2
+ * Readonly behavior - Creates immutable views of collections.
3
+ *
4
+ * Wraps collections to prevent modifications by throwing errors
5
+ * on any mutation attempt. Mirrors Java's Collections.unmodifiable*() methods.
6
+ *
7
+ * @example
8
+ * const list = compose(
9
+ * readonlyList()
10
+ * )(arrayList([1, 2, 3]))
11
+ *
12
+ * list.get(0) // OK: 1
13
+ * list.add(4) // Error: Cannot modify readonly list
14
+ */
15
+
16
+ import type { Middleware } from "../core/middleware.js";
17
+ import type { ReadonlyList } from "../interfaces/list.js";
18
+ import type { ReadonlyQueue } from "../interfaces/queue.js";
19
+ import type { ReadonlyMapLike } from "../interfaces/map.js";
20
+ import type { ReadonlyCollection } from "../interfaces/collection.js";
21
+
22
+ /**
23
+ * Options for readonly behavior.
24
+ */
25
+ export interface ReadonlyOptions {
26
+ /**
27
+ * Custom error message to throw on mutation attempts.
28
+ * @default 'Cannot modify readonly collection'
29
+ */
30
+ errorMessage?: string;
31
+ }
32
+
33
+ /**
34
+ * Set of method names that mutate collections.
35
+ */
36
+ const MUTATING_METHODS = new Set([
37
+ // Collection mutations
38
+ "add",
39
+ "addAll",
40
+ "remove",
41
+ "removeAll",
42
+ "retainAll",
43
+ "clear",
44
+
45
+ // List mutations
46
+ "set",
47
+ "insert",
48
+ "insertAll",
49
+ "removeAt",
50
+ "removeRange",
51
+ "push",
52
+ "pop",
53
+ "shift",
54
+ "unshift",
55
+ "sort",
56
+ "reverse",
57
+
58
+ // Queue/Deque mutations
59
+ "offer",
60
+ "offerFirst",
61
+ "offerLast",
62
+ "poll",
63
+ "pollFirst",
64
+ "pollLast",
65
+ "enqueue",
66
+ "dequeue",
67
+ "addFirst",
68
+ "addLast",
69
+ "removeFirst",
70
+ "removeLast",
71
+
72
+ // Map mutations
73
+ "set",
74
+ "setIfAbsent",
75
+ "forcePut",
76
+ "pollFirstEntry",
77
+ "pollLastEntry",
78
+ "delete",
79
+ "deleteEntry",
80
+ "replace",
81
+ "replaceEntry",
82
+ "computeIfAbsent",
83
+ "computeIfPresent",
84
+ "compute",
85
+ "merge",
86
+ "putAll",
87
+ ]);
88
+
89
+ /** Methods that return a view of the collection: the view is readonly too. */
90
+ const VIEW_METHODS = new Set([
91
+ "subList",
92
+ "subMap",
93
+ "subSet",
94
+ "headMap",
95
+ "tailMap",
96
+ "headSet",
97
+ "tailSet",
98
+ "descendingMap",
99
+ "descendingSet",
100
+ "inverse",
101
+ ]);
102
+
103
+ /**
104
+ * Creates a readonly middleware for any collection type.
105
+ */
106
+ function createReadonly<C extends object>(
107
+ errorMessage: string = "Cannot modify readonly collection"
108
+ ): Middleware<C> {
109
+ return (next: C): C => {
110
+ return new Proxy(next, {
111
+ get(target, prop, receiver) {
112
+ const value = Reflect.get(target, prop, receiver);
113
+
114
+ // Block mutating methods
115
+ if (typeof prop === "string" && MUTATING_METHODS.has(prop)) {
116
+ if (typeof value === "function") {
117
+ return () => {
118
+ throw new Error(errorMessage);
119
+ };
120
+ }
121
+ }
122
+
123
+ // A view (a sublist, a submap, a head or tail set, a descending map) is readonly too.
124
+ // Before, only subList and subMap were wrapped, and setIfAbsent, pollFirstEntry and
125
+ // pollLastEntry were not blocked (BUGS-2026-07 L14).
126
+ if (typeof value === "function" && typeof prop === "string" && VIEW_METHODS.has(prop)) {
127
+ return function (this: any, ...args: any[]) {
128
+ const result = value.apply(target, args);
129
+ // Recursively apply readonly to the result
130
+ return createReadonly<typeof result>(errorMessage)(result);
131
+ };
132
+ }
133
+
134
+ return value;
135
+ },
136
+
137
+ set(_target, _prop, _value, _receiver) {
138
+ // Block property assignments
139
+ throw new Error(errorMessage);
140
+ },
141
+
142
+ deleteProperty(_target, _prop) {
143
+ // Block property deletions
144
+ throw new Error(errorMessage);
145
+ },
146
+ });
147
+ };
148
+ }
149
+
150
+ /**
151
+ * Creates a readonly List middleware.
152
+ *
153
+ * All mutation methods will throw an error.
154
+ * Read operations work normally.
155
+ *
156
+ * @example
157
+ * const readonlyNumbers = compose(
158
+ * readonlyList()
159
+ * )(arrayList([1, 2, 3]))
160
+ */
161
+ export function readonlyList<T>(
162
+ options: ReadonlyOptions = {}
163
+ ): Middleware<ReadonlyList<T>> {
164
+ const errorMessage = options.errorMessage ?? "Cannot modify readonly list";
165
+ return createReadonly<any>(errorMessage);
166
+ }
167
+
168
+ /**
169
+ * Creates a readonly Queue middleware.
170
+ */
171
+ export function readonlyQueue<T>(
172
+ options: ReadonlyOptions = {}
173
+ ): Middleware<ReadonlyQueue<T>> {
174
+ const errorMessage = options.errorMessage ?? "Cannot modify readonly queue";
175
+ return createReadonly<any>(errorMessage);
176
+ }
177
+
178
+ /**
179
+ * Creates a readonly Map middleware.
180
+ */
181
+ export function readonlyMap<K, V>(
182
+ options: ReadonlyOptions = {}
183
+ ): Middleware<ReadonlyMapLike<K, V>> {
184
+ const errorMessage = options.errorMessage ?? "Cannot modify readonly map";
185
+ return createReadonly<any>(errorMessage);
186
+ }
187
+
188
+ /**
189
+ * Creates a readonly Collection middleware.
190
+ *
191
+ * Generic version that works with any collection type.
192
+ */
193
+ export function readonlyCollection<T>(
194
+ options: ReadonlyOptions = {}
195
+ ): Middleware<ReadonlyCollection<T>> {
196
+ const errorMessage = options.errorMessage ?? "Cannot modify readonly collection";
197
+ return createReadonly<any>(errorMessage);
198
+ }
199
+
200
+ /**
201
+ * Generic readonly middleware.
202
+ * Alias for readonlyCollection.
203
+ */
204
+ export function readonly<C extends object>(
205
+ options: ReadonlyOptions = {}
206
+ ): Middleware<C> {
207
+ const errorMessage = options.errorMessage ?? "Cannot modify readonly collection";
208
+ return createReadonly<C>(errorMessage);
209
+ }
210
+
211
+ /**
212
+ * Alias for Java compatibility: unmodifiableList
213
+ */
214
+ export const unmodifiableList : typeof readonlyList = readonlyList;
215
+
216
+ /**
217
+ * Alias for Java compatibility: unmodifiableMap
218
+ */
219
+ export const unmodifiableMap : typeof readonlyMap = readonlyMap;
220
+
221
+ /**
222
+ * Alias for Java compatibility: unmodifiableCollection
223
+ */
224
+ export const unmodifiableCollection : typeof readonlyCollection = readonlyCollection;