@procedures/collections 0.46.0 → 0.47.2

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