@procedures/collections 0.47.1 → 0.47.2

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