@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,912 @@
1
+ /**
2
+ * TreeMap - Red-black tree based sorted map implementation.
3
+ *
4
+ * Implements a self-balancing binary search tree that maintains keys in sorted
5
+ * order. Mirrors java.util.TreeMap.
6
+ *
7
+ * Key features:
8
+ * - O(log n) access, insert, delete
9
+ * - Keys maintained in sorted order (natural or custom comparator)
10
+ * - Navigation methods (floor, ceiling, lower, higher)
11
+ * - Range view operations (subMap, headMap, tailMap)
12
+ *
13
+ * @example
14
+ * const map = treeMap<string, number>()
15
+ * map.set('c', 3)
16
+ * map.set('a', 1)
17
+ * map.set('b', 2)
18
+ * // Iteration order: a, b, c (sorted)
19
+ *
20
+ * @example
21
+ * // Custom comparator for descending order
22
+ * const map = treeMap<number, string>({
23
+ * compare: (a, b) => b - a
24
+ * })
25
+ */
26
+
27
+ import type { NavigableMap, SortedMap, Entry } from "../interfaces/map.js";
28
+ import type { Eq, Compare } from "../core/traits.js";
29
+ import { defaultEq, defaultCompare } from "../utils/defaults.js";
30
+
31
+ /**
32
+ * Node color in red-black tree.
33
+ */
34
+ enum Color {
35
+ RED = "RED",
36
+ BLACK = "BLACK",
37
+ }
38
+
39
+ /**
40
+ * Node in the red-black tree.
41
+ */
42
+ interface TreeNode<K, V> {
43
+ key: K;
44
+ value: V;
45
+ color: Color;
46
+ left: TreeNode<K, V> | null;
47
+ right: TreeNode<K, V> | null;
48
+ parent: TreeNode<K, V> | null;
49
+ }
50
+
51
+ // Null-safe helpers of the red-black algorithms: a null leaf is black
52
+ function colorOf<K, V>(node: TreeNode<K, V> | null): Color {
53
+ return node === null ? Color.BLACK : node.color;
54
+ }
55
+ function parentOf<K, V>(node: TreeNode<K, V> | null): TreeNode<K, V> | null {
56
+ return node === null ? null : node.parent;
57
+ }
58
+ function leftOf<K, V>(node: TreeNode<K, V> | null): TreeNode<K, V> | null {
59
+ return node === null ? null : node.left;
60
+ }
61
+ function rightOf<K, V>(node: TreeNode<K, V> | null): TreeNode<K, V> | null {
62
+ return node === null ? null : node.right;
63
+ }
64
+ function setColor<K, V>(node: TreeNode<K, V> | null, color: Color): void {
65
+ if (node !== null) node.color = color;
66
+ }
67
+
68
+ /**
69
+ * Options for creating a TreeMap.
70
+ */
71
+ export interface TreeMapOptions<K, V> {
72
+ /**
73
+ * Comparison function for keys.
74
+ * @default defaultCompare
75
+ */
76
+ compare?: Compare<K>;
77
+
78
+ /**
79
+ * Equality function for keys.
80
+ * @default defaultEq
81
+ */
82
+ eq?: Eq<K>;
83
+
84
+ /**
85
+ * Equality function for values.
86
+ * @default defaultEq
87
+ */
88
+ valueEq?: Eq<V>;
89
+ }
90
+
91
+ /**
92
+ * TreeMap<K, V> - Red-black tree based sorted map.
93
+ *
94
+ * Maintains keys in sorted order using a self-balancing binary search tree.
95
+ * Provides O(log n) operations and navigation methods.
96
+ */
97
+ export class TreeMap<K, V> implements NavigableMap<K, V> {
98
+ private _root: TreeNode<K, V> | null = null;
99
+ private _size: number = 0;
100
+ private readonly _compare: Compare<K>;
101
+ private readonly _eq: Eq<K>;
102
+ private readonly _valueEq: Eq<V>;
103
+
104
+ constructor(options: TreeMapOptions<K, V> = {}) {
105
+ const {
106
+ compare = defaultCompare,
107
+ eq = defaultEq,
108
+ valueEq = defaultEq,
109
+ } = options;
110
+
111
+ this._compare = compare;
112
+ this._eq = eq;
113
+ this._valueEq = valueEq;
114
+ }
115
+
116
+ // ========================================================================
117
+ // Size and state
118
+ // ========================================================================
119
+
120
+ get size(): number {
121
+ return this._size;
122
+ }
123
+
124
+ get isEmpty(): boolean {
125
+ return this._size === 0;
126
+ }
127
+
128
+ get comparator(): Compare<K> {
129
+ return this._compare;
130
+ }
131
+
132
+ get keyEq(): Eq<K> {
133
+ return this._eq;
134
+ }
135
+
136
+ get valueEq(): Eq<V> {
137
+ return this._valueEq;
138
+ }
139
+
140
+ // ========================================================================
141
+ // Lookup operations
142
+ // ========================================================================
143
+
144
+ has(key: K): boolean {
145
+ return this.getNode(key) !== null;
146
+ }
147
+
148
+ containsKey(key: K): boolean {
149
+ return this.has(key);
150
+ }
151
+
152
+ containsValue(value: V): boolean {
153
+ return this.findValue(this._root, value);
154
+ }
155
+
156
+ get(key: K): V {
157
+ const node = this.getNode(key);
158
+ if (node === null) {
159
+ throw new Error("Key not found");
160
+ }
161
+ return node.value;
162
+ }
163
+
164
+ getOrUndefined(key: K): V | undefined {
165
+ const node = this.getNode(key);
166
+ return node?.value;
167
+ }
168
+
169
+ getOrDefault(key: K, defaultValue: V): V {
170
+ const node = this.getNode(key);
171
+ return node !== null ? node.value : defaultValue;
172
+ }
173
+
174
+ // ========================================================================
175
+ // Modification operations
176
+ // ========================================================================
177
+
178
+ set(key: K, value: V): V | undefined {
179
+ if (this._root === null) {
180
+ this._root = this.createNode(key, value, Color.BLACK);
181
+ this._size++;
182
+ return undefined;
183
+ }
184
+
185
+ // Find insertion point
186
+ let node: TreeNode<K, V> | null = this._root;
187
+ let parent: TreeNode<K, V> | null = null;
188
+
189
+ while (node !== null) {
190
+ parent = node;
191
+ const cmp = this._compare(key, node.key);
192
+
193
+ if (cmp === 0) {
194
+ // Key exists, replace value
195
+ const oldValue = node.value;
196
+ node.value = value;
197
+ return oldValue;
198
+ } else if (cmp < 0) {
199
+ node = node.left;
200
+ } else {
201
+ node = node.right;
202
+ }
203
+ }
204
+
205
+ // Insert new node
206
+ const newNode = this.createNode(key, value, Color.RED);
207
+ newNode.parent = parent;
208
+
209
+ if (parent !== null) {
210
+ const cmp = this._compare(key, parent.key);
211
+ if (cmp < 0) {
212
+ parent.left = newNode;
213
+ } else {
214
+ parent.right = newNode;
215
+ }
216
+ }
217
+
218
+ this._size++;
219
+ this.fixAfterInsertion(newNode);
220
+ return undefined;
221
+ }
222
+
223
+ setIfAbsent(key: K, value: V): V {
224
+ const existing = this.getNode(key);
225
+ if (existing !== null) {
226
+ return existing.value;
227
+ }
228
+ this.set(key, value);
229
+ return value;
230
+ }
231
+
232
+ replace(key: K, value: V): V | undefined {
233
+ const node = this.getNode(key);
234
+ if (node === null) return undefined;
235
+ const oldValue = node.value;
236
+ node.value = value;
237
+ return oldValue;
238
+ }
239
+
240
+ replaceEntry(key: K, oldValue: V, newValue: V): boolean {
241
+ const node = this.getNode(key);
242
+ if (node === null || !this._valueEq(node.value, oldValue)) {
243
+ return false;
244
+ }
245
+ node.value = newValue;
246
+ return true;
247
+ }
248
+
249
+ delete(key: K): V | undefined {
250
+ const node = this.getNode(key);
251
+ if (node === null) return undefined;
252
+
253
+ const oldValue = node.value;
254
+ this.deleteNode(node);
255
+ this._size--;
256
+ return oldValue;
257
+ }
258
+
259
+ deleteEntry(key: K, value: V): boolean {
260
+ const node = this.getNode(key);
261
+ if (node === null || !this._valueEq(node.value, value)) {
262
+ return false;
263
+ }
264
+ this.deleteNode(node);
265
+ this._size--;
266
+ return true;
267
+ }
268
+
269
+ clear(): void {
270
+ this._root = null;
271
+ this._size = 0;
272
+ }
273
+
274
+ // ========================================================================
275
+ // Bulk operations
276
+ // ========================================================================
277
+
278
+ putAll(other: NavigableMap<K, V> | Iterable<Entry<K, V>>): void {
279
+ if ('entries' in other && typeof other.entries === 'function') {
280
+ for (const entry of other.entries()) {
281
+ this.set(entry.key, entry.value);
282
+ }
283
+ } else {
284
+ for (const entry of other as Iterable<Entry<K, V>>) {
285
+ this.set(entry.key, entry.value);
286
+ }
287
+ }
288
+ }
289
+
290
+ // ========================================================================
291
+ // Computed operations
292
+ // ========================================================================
293
+
294
+ computeIfAbsent(key: K, mappingFunction: (key: K) => V): V {
295
+ const existing = this.getNode(key);
296
+ if (existing !== null) {
297
+ return existing.value;
298
+ }
299
+ const value = mappingFunction(key);
300
+ this.set(key, value);
301
+ return value;
302
+ }
303
+
304
+ computeIfPresent(key: K, remappingFunction: (key: K, value: V) => V | undefined): V | undefined {
305
+ const existing = this.getNode(key);
306
+ if (existing === null) return undefined;
307
+
308
+ const newValue = remappingFunction(key, existing.value);
309
+ if (newValue === undefined) {
310
+ this.delete(key);
311
+ return undefined;
312
+ }
313
+ existing.value = newValue;
314
+ return newValue;
315
+ }
316
+
317
+ compute(key: K, remappingFunction: (key: K, value: V | undefined) => V | undefined): V | undefined {
318
+ const existing = this.getNode(key);
319
+ const newValue = remappingFunction(key, existing?.value);
320
+
321
+ if (newValue === undefined) {
322
+ if (existing !== null) {
323
+ this.delete(key);
324
+ }
325
+ return undefined;
326
+ }
327
+
328
+ this.set(key, newValue);
329
+ return newValue;
330
+ }
331
+
332
+ merge(key: K, value: V, remappingFunction: (oldValue: V, newValue: V) => V | undefined): V | undefined {
333
+ const existing = this.getNode(key);
334
+
335
+ if (existing === null) {
336
+ this.set(key, value);
337
+ return value;
338
+ }
339
+
340
+ const merged = remappingFunction(existing.value, value);
341
+ if (merged === undefined) {
342
+ this.delete(key);
343
+ return undefined;
344
+ }
345
+
346
+ existing.value = merged;
347
+ return merged;
348
+ }
349
+
350
+ // ========================================================================
351
+ // SortedMap operations
352
+ // ========================================================================
353
+
354
+ firstKey(): K {
355
+ if (this._root === null) {
356
+ throw new Error("Map is empty");
357
+ }
358
+ return this.getFirstNode(this._root).key;
359
+ }
360
+
361
+ lastKey(): K {
362
+ if (this._root === null) {
363
+ throw new Error("Map is empty");
364
+ }
365
+ return this.getLastNode(this._root).key;
366
+ }
367
+
368
+ headMap(toKey: K): SortedMap<K, V> {
369
+ // Return a view (simplified: create new map with filtered entries)
370
+ const result = new TreeMap<K, V>({
371
+ compare: this._compare,
372
+ eq: this._eq,
373
+ valueEq: this._valueEq,
374
+ });
375
+
376
+ for (const entry of this.entries()) {
377
+ if (this._compare(entry.key, toKey) < 0) {
378
+ result.set(entry.key, entry.value);
379
+ } else {
380
+ break; // Keys are sorted, so we can stop
381
+ }
382
+ }
383
+
384
+ return result;
385
+ }
386
+
387
+ tailMap(fromKey: K): SortedMap<K, V> {
388
+ const result = new TreeMap<K, V>({
389
+ compare: this._compare,
390
+ eq: this._eq,
391
+ valueEq: this._valueEq,
392
+ });
393
+
394
+ let started = false;
395
+ for (const entry of this.entries()) {
396
+ if (!started && this._compare(entry.key, fromKey) < 0) {
397
+ continue;
398
+ }
399
+ started = true;
400
+ result.set(entry.key, entry.value);
401
+ }
402
+
403
+ return result;
404
+ }
405
+
406
+ subMap(fromKey: K, toKey: K): SortedMap<K, V> {
407
+ const result = new TreeMap<K, V>({
408
+ compare: this._compare,
409
+ eq: this._eq,
410
+ valueEq: this._valueEq,
411
+ });
412
+
413
+ for (const entry of this.entries()) {
414
+ const cmpFrom = this._compare(entry.key, fromKey);
415
+ const cmpTo = this._compare(entry.key, toKey);
416
+
417
+ if (cmpFrom >= 0 && cmpTo < 0) {
418
+ result.set(entry.key, entry.value);
419
+ } else if (cmpTo >= 0) {
420
+ break; // Past the range
421
+ }
422
+ }
423
+
424
+ return result;
425
+ }
426
+
427
+ // ========================================================================
428
+ // NavigableMap operations
429
+ // ========================================================================
430
+
431
+ floorKey(key: K): K | undefined {
432
+ return this.floorEntry(key)?.key;
433
+ }
434
+
435
+ ceilingKey(key: K): K | undefined {
436
+ return this.ceilingEntry(key)?.key;
437
+ }
438
+
439
+ lowerKey(key: K): K | undefined {
440
+ return this.lowerEntry(key)?.key;
441
+ }
442
+
443
+ higherKey(key: K): K | undefined {
444
+ return this.higherEntry(key)?.key;
445
+ }
446
+
447
+ floorEntry(key: K): Entry<K, V> | undefined {
448
+ let node = this._root;
449
+ let result: TreeNode<K, V> | null = null;
450
+
451
+ while (node !== null) {
452
+ const cmp = this._compare(key, node.key);
453
+ if (cmp === 0) {
454
+ return { key: node.key, value: node.value };
455
+ } else if (cmp < 0) {
456
+ node = node.left;
457
+ } else {
458
+ result = node; // This node is <= key
459
+ node = node.right;
460
+ }
461
+ }
462
+
463
+ return result ? { key: result.key, value: result.value } : undefined;
464
+ }
465
+
466
+ ceilingEntry(key: K): Entry<K, V> | undefined {
467
+ let node = this._root;
468
+ let result: TreeNode<K, V> | null = null;
469
+
470
+ while (node !== null) {
471
+ const cmp = this._compare(key, node.key);
472
+ if (cmp === 0) {
473
+ return { key: node.key, value: node.value };
474
+ } else if (cmp > 0) {
475
+ node = node.right;
476
+ } else {
477
+ result = node; // This node is >= key
478
+ node = node.left;
479
+ }
480
+ }
481
+
482
+ return result ? { key: result.key, value: result.value } : undefined;
483
+ }
484
+
485
+ lowerEntry(key: K): Entry<K, V> | undefined {
486
+ let node = this._root;
487
+ let result: TreeNode<K, V> | null = null;
488
+
489
+ while (node !== null) {
490
+ const cmp = this._compare(key, node.key);
491
+ if (cmp <= 0) {
492
+ node = node.left;
493
+ } else {
494
+ result = node; // This node is < key
495
+ node = node.right;
496
+ }
497
+ }
498
+
499
+ return result ? { key: result.key, value: result.value } : undefined;
500
+ }
501
+
502
+ higherEntry(key: K): Entry<K, V> | undefined {
503
+ let node = this._root;
504
+ let result: TreeNode<K, V> | null = null;
505
+
506
+ while (node !== null) {
507
+ const cmp = this._compare(key, node.key);
508
+ if (cmp >= 0) {
509
+ node = node.right;
510
+ } else {
511
+ result = node; // This node is > key
512
+ node = node.left;
513
+ }
514
+ }
515
+
516
+ return result ? { key: result.key, value: result.value } : undefined;
517
+ }
518
+
519
+ firstEntry(): Entry<K, V> | undefined {
520
+ if (this._root === null) return undefined;
521
+ const node = this.getFirstNode(this._root);
522
+ return { key: node.key, value: node.value };
523
+ }
524
+
525
+ lastEntry(): Entry<K, V> | undefined {
526
+ if (this._root === null) return undefined;
527
+ const node = this.getLastNode(this._root);
528
+ return { key: node.key, value: node.value };
529
+ }
530
+
531
+ pollFirstEntry(): Entry<K, V> | undefined {
532
+ if (this._root === null) return undefined;
533
+ const node = this.getFirstNode(this._root);
534
+ const entry = { key: node.key, value: node.value };
535
+ this.deleteNode(node);
536
+ this._size--;
537
+ return entry;
538
+ }
539
+
540
+ pollLastEntry(): Entry<K, V> | undefined {
541
+ if (this._root === null) return undefined;
542
+ const node = this.getLastNode(this._root);
543
+ const entry = { key: node.key, value: node.value };
544
+ this.deleteNode(node);
545
+ this._size--;
546
+ return entry;
547
+ }
548
+
549
+ descendingMap(): NavigableMap<K, V> {
550
+ // Create a new TreeMap with reversed comparator
551
+ const reversed = new TreeMap<K, V>({
552
+ compare: (a, b) => this._compare(b, a),
553
+ eq: this._eq,
554
+ valueEq: this._valueEq,
555
+ });
556
+
557
+ for (const entry of this.entries()) {
558
+ reversed.set(entry.key, entry.value);
559
+ }
560
+
561
+ return reversed;
562
+ }
563
+
564
+ *descendingKeys(): IterableIterator<K> {
565
+ yield* this.reverseInOrderTraversal(this._root, (node) => node.key);
566
+ }
567
+
568
+ // ========================================================================
569
+ // View operations
570
+ // ========================================================================
571
+
572
+ *keys(): IterableIterator<K> {
573
+ yield* this.inOrderTraversal(this._root, (node) => node.key);
574
+ }
575
+
576
+ *values(): IterableIterator<V> {
577
+ yield* this.inOrderTraversal(this._root, (node) => node.value);
578
+ }
579
+
580
+ *entries(): IterableIterator<Entry<K, V>> {
581
+ yield* this.inOrderTraversal(this._root, (node) => ({
582
+ key: node.key,
583
+ value: node.value,
584
+ }));
585
+ }
586
+
587
+ *[Symbol.iterator](): Iterator<Entry<K, V>> {
588
+ yield* this.entries();
589
+ }
590
+
591
+ forEach(action: (value: V, key: K, map: this) => void): void {
592
+ for (const entry of this.entries()) {
593
+ action(entry.value, entry.key, this);
594
+ }
595
+ }
596
+
597
+ toArray(): Entry<K, V>[] {
598
+ return Array.from(this.entries());
599
+ }
600
+
601
+ // ========================================================================
602
+ // Private helpers - Tree operations
603
+ // ========================================================================
604
+
605
+ private createNode(key: K, value: V, color: Color): TreeNode<K, V> {
606
+ return {
607
+ key,
608
+ value,
609
+ color,
610
+ left: null,
611
+ right: null,
612
+ parent: null,
613
+ };
614
+ }
615
+
616
+ private getNode(key: K): TreeNode<K, V> | null {
617
+ let node = this._root;
618
+
619
+ while (node !== null) {
620
+ const cmp = this._compare(key, node.key);
621
+ if (cmp === 0) {
622
+ return node;
623
+ } else if (cmp < 0) {
624
+ node = node.left;
625
+ } else {
626
+ node = node.right;
627
+ }
628
+ }
629
+
630
+ return null;
631
+ }
632
+
633
+ private getFirstNode(node: TreeNode<K, V>): TreeNode<K, V> {
634
+ while (node.left !== null) {
635
+ node = node.left;
636
+ }
637
+ return node;
638
+ }
639
+
640
+ private getLastNode(node: TreeNode<K, V>): TreeNode<K, V> {
641
+ while (node.right !== null) {
642
+ node = node.right;
643
+ }
644
+ return node;
645
+ }
646
+
647
+ private findValue(node: TreeNode<K, V> | null, value: V): boolean {
648
+ if (node === null) return false;
649
+ if (this._valueEq(node.value, value)) return true;
650
+ return this.findValue(node.left, value) || this.findValue(node.right, value);
651
+ }
652
+
653
+ private *inOrderTraversal<T>(
654
+ node: TreeNode<K, V> | null,
655
+ mapper: (node: TreeNode<K, V>) => T
656
+ ): IterableIterator<T> {
657
+ if (node === null) return;
658
+ yield* this.inOrderTraversal(node.left, mapper);
659
+ yield mapper(node);
660
+ yield* this.inOrderTraversal(node.right, mapper);
661
+ }
662
+
663
+ private *reverseInOrderTraversal<T>(
664
+ node: TreeNode<K, V> | null,
665
+ mapper: (node: TreeNode<K, V>) => T
666
+ ): IterableIterator<T> {
667
+ if (node === null) return;
668
+ yield* this.reverseInOrderTraversal(node.right, mapper);
669
+ yield mapper(node);
670
+ yield* this.reverseInOrderTraversal(node.left, mapper);
671
+ }
672
+
673
+ // ========================================================================
674
+ // Private helpers - Red-black tree balancing
675
+ // ========================================================================
676
+
677
+ private rotateLeft(node: TreeNode<K, V> | null): void {
678
+ if (node === null || node.right === null) return;
679
+ const right = node.right;
680
+ node.right = right.left;
681
+
682
+ if (right.left !== null) {
683
+ right.left.parent = node;
684
+ }
685
+
686
+ right.parent = node.parent;
687
+
688
+ if (node.parent === null) {
689
+ this._root = right;
690
+ } else if (node === node.parent.left) {
691
+ node.parent.left = right;
692
+ } else {
693
+ node.parent.right = right;
694
+ }
695
+
696
+ right.left = node;
697
+ node.parent = right;
698
+ }
699
+
700
+ private rotateRight(node: TreeNode<K, V> | null): void {
701
+ if (node === null || node.left === null) return;
702
+ const left = node.left;
703
+ node.left = left.right;
704
+
705
+ if (left.right !== null) {
706
+ left.right.parent = node;
707
+ }
708
+
709
+ left.parent = node.parent;
710
+
711
+ if (node.parent === null) {
712
+ this._root = left;
713
+ } else if (node === node.parent.right) {
714
+ node.parent.right = left;
715
+ } else {
716
+ node.parent.left = left;
717
+ }
718
+
719
+ left.right = node;
720
+ node.parent = left;
721
+ }
722
+
723
+ private fixAfterInsertion(node: TreeNode<K, V>): void {
724
+ while (node.parent !== null && node.parent.color === Color.RED) {
725
+ if (node.parent === node.parent.parent?.left) {
726
+ const uncle = node.parent.parent.right;
727
+
728
+ if (uncle?.color === Color.RED) {
729
+ // Case 1: Uncle is red
730
+ node.parent.color = Color.BLACK;
731
+ uncle.color = Color.BLACK;
732
+ node.parent.parent.color = Color.RED;
733
+ node = node.parent.parent;
734
+ } else {
735
+ if (node === node.parent.right) {
736
+ // Case 2: Node is right child
737
+ node = node.parent;
738
+ this.rotateLeft(node);
739
+ }
740
+ // Case 3: Node is left child
741
+ node.parent!.color = Color.BLACK;
742
+ node.parent!.parent!.color = Color.RED;
743
+ this.rotateRight(node.parent!.parent!);
744
+ }
745
+ } else {
746
+ const uncle = node.parent.parent?.left ?? null;
747
+
748
+ if (uncle?.color === Color.RED) {
749
+ // Case 1: Uncle is red
750
+ node.parent.color = Color.BLACK;
751
+ uncle.color = Color.BLACK;
752
+ node.parent.parent!.color = Color.RED;
753
+ node = node.parent.parent!;
754
+ } else {
755
+ if (node === node.parent.left) {
756
+ // Case 2: Node is left child
757
+ node = node.parent;
758
+ this.rotateRight(node);
759
+ }
760
+ // Case 3: Node is right child
761
+ node.parent!.color = Color.BLACK;
762
+ node.parent!.parent!.color = Color.RED;
763
+ this.rotateLeft(node.parent!.parent!);
764
+ }
765
+ }
766
+ }
767
+
768
+ this._root!.color = Color.BLACK;
769
+ }
770
+
771
+ /**
772
+ * Deletes a node (the CLRS algorithm, as in java.util.TreeMap). A null leaf counts as black.
773
+ *
774
+ * Before (BUGS-2026-07 L10): the deletion of a black leaf skipped the fix-up, so the black
775
+ * heights drifted and the tree became unbalanced, and the fix-up treated a null leaf as
776
+ * "not black", so it chose the wrong case and rotated at null (a TypeError).
777
+ */
778
+ private deleteNode(target: TreeNode<K, V>): void {
779
+ let node = target;
780
+ // A node with two children takes the key and value of its successor, then the successor goes
781
+ if (node.left !== null && node.right !== null) {
782
+ const successor = this.getFirstNode(node.right);
783
+ node.key = successor.key;
784
+ node.value = successor.value;
785
+ node = successor;
786
+ }
787
+
788
+ const replacement = node.left ?? node.right;
789
+ if (replacement !== null) {
790
+ replacement.parent = node.parent;
791
+ if (node.parent === null) this._root = replacement;
792
+ else if (node === node.parent.left) node.parent.left = replacement;
793
+ else node.parent.right = replacement;
794
+ node.left = node.right = node.parent = null;
795
+ if (node.color === Color.BLACK) this.fixAfterDeletion(replacement);
796
+ } else if (node.parent === null) {
797
+ // The only node
798
+ this._root = null;
799
+ } else {
800
+ // A leaf: it is its own phantom replacement during the fix-up, then it goes
801
+ if (node.color === Color.BLACK) this.fixAfterDeletion(node);
802
+ if (node.parent !== null) {
803
+ if (node === node.parent.left) node.parent.left = null;
804
+ else if (node === node.parent.right) node.parent.right = null;
805
+ node.parent = null;
806
+ }
807
+ }
808
+ }
809
+
810
+ private fixAfterDeletion(start: TreeNode<K, V>): void {
811
+ let x: TreeNode<K, V> | null = start;
812
+ while (x !== this._root && colorOf(x) === Color.BLACK) {
813
+ if (x === leftOf(parentOf(x))) {
814
+ let sibling = rightOf(parentOf(x));
815
+ if (colorOf(sibling) === Color.RED) {
816
+ setColor(sibling, Color.BLACK);
817
+ setColor(parentOf(x), Color.RED);
818
+ this.rotateLeft(parentOf(x));
819
+ sibling = rightOf(parentOf(x));
820
+ }
821
+ if (colorOf(leftOf(sibling)) === Color.BLACK && colorOf(rightOf(sibling)) === Color.BLACK) {
822
+ setColor(sibling, Color.RED);
823
+ x = parentOf(x);
824
+ } else {
825
+ if (colorOf(rightOf(sibling)) === Color.BLACK) {
826
+ setColor(leftOf(sibling), Color.BLACK);
827
+ setColor(sibling, Color.RED);
828
+ this.rotateRight(sibling);
829
+ sibling = rightOf(parentOf(x));
830
+ }
831
+ setColor(sibling, colorOf(parentOf(x)));
832
+ setColor(parentOf(x), Color.BLACK);
833
+ setColor(rightOf(sibling), Color.BLACK);
834
+ this.rotateLeft(parentOf(x));
835
+ x = this._root;
836
+ }
837
+ } else {
838
+ let sibling = leftOf(parentOf(x));
839
+ if (colorOf(sibling) === Color.RED) {
840
+ setColor(sibling, Color.BLACK);
841
+ setColor(parentOf(x), Color.RED);
842
+ this.rotateRight(parentOf(x));
843
+ sibling = leftOf(parentOf(x));
844
+ }
845
+ if (colorOf(rightOf(sibling)) === Color.BLACK && colorOf(leftOf(sibling)) === Color.BLACK) {
846
+ setColor(sibling, Color.RED);
847
+ x = parentOf(x);
848
+ } else {
849
+ if (colorOf(leftOf(sibling)) === Color.BLACK) {
850
+ setColor(rightOf(sibling), Color.BLACK);
851
+ setColor(sibling, Color.RED);
852
+ this.rotateLeft(sibling);
853
+ sibling = leftOf(parentOf(x));
854
+ }
855
+ setColor(sibling, colorOf(parentOf(x)));
856
+ setColor(parentOf(x), Color.BLACK);
857
+ setColor(leftOf(sibling), Color.BLACK);
858
+ this.rotateRight(parentOf(x));
859
+ x = this._root;
860
+ }
861
+ }
862
+ }
863
+ setColor(x, Color.BLACK);
864
+ }
865
+
866
+ // ========================================================================
867
+ // Utility methods
868
+ // ========================================================================
869
+
870
+ toString(): string {
871
+ const entries = Array.from(this.entries())
872
+ .map(({ key, value }) => `${key}=${value}`)
873
+ .join(', ');
874
+ return `TreeMap{${entries}}`;
875
+ }
876
+ }
877
+
878
+ /**
879
+ * Factory function to create a TreeMap.
880
+ *
881
+ * @example
882
+ * const map = treeMap<string, number>()
883
+ * map.set('c', 3)
884
+ * map.set('a', 1)
885
+ * map.set('b', 2)
886
+ * // Iteration order: a, b, c
887
+ *
888
+ * @example
889
+ * // With initial entries
890
+ * const map = treeMap<string, number>([
891
+ * ['c', 3],
892
+ * ['a', 1],
893
+ * ['b', 2]
894
+ * ])
895
+ *
896
+ * @example
897
+ * // Custom comparator for descending order
898
+ * const map = treeMap<number, string>({ compare: (a, b) => b - a })
899
+ */
900
+ export function treeMap<K, V>(
901
+ optionsOrEntries?: TreeMapOptions<K, V> | Iterable<[K, V]>
902
+ ): TreeMap<K, V> {
903
+ if (optionsOrEntries && Symbol.iterator in optionsOrEntries) {
904
+ const map = new TreeMap<K, V>();
905
+ for (const [key, value] of optionsOrEntries as Iterable<[K, V]>) {
906
+ map.set(key, value);
907
+ }
908
+ return map;
909
+ }
910
+
911
+ return new TreeMap<K, V>(optionsOrEntries as TreeMapOptions<K, V>);
912
+ }