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