@procedures/client 0.2.0 → 0.2.1

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