@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.
- package/CHANGELOG.md +18 -8
- package/README.md +2 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -59
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -95
- package/dist/index.js.map +1 -1
- package/dist/storage/index.d.ts +2 -2
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +1 -1
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/memory.d.ts +1 -1
- package/dist/storage/memory.d.ts.map +1 -1
- package/dist/storage/memory.js +1 -1
- package/dist/storage/memory.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/storage-contract.js +1 -1
- package/dist/testing/storage-contract.js.map +1 -1
- package/dist/undo/index.d.ts +1 -1
- package/dist/undo/index.js +1 -1
- package/dist/undo/undo-log.d.ts +1 -1
- package/dist/undo/undo-log.js +1 -1
- package/package.json +4 -4
- package/src/index.ts +338 -214
- package/src/storage/index.ts +2 -2
- package/src/storage/memory.ts +1 -1
- package/src/testing/index.ts +1 -1
- package/src/testing/storage-contract.ts +1 -1
- package/src/undo/index.ts +1 -1
- package/src/undo/undo-log.ts +1 -1
- package/dist/async/async-queue.d.ts +0 -170
- package/dist/async/async-queue.d.ts.map +0 -1
- package/dist/async/async-queue.js +0 -404
- package/dist/async/async-queue.js.map +0 -1
- package/dist/async/channels.d.ts +0 -211
- package/dist/async/channels.d.ts.map +0 -1
- package/dist/async/channels.js +0 -393
- package/dist/async/channels.js.map +0 -1
- package/dist/async/settled.d.ts +0 -23
- package/dist/async/settled.d.ts.map +0 -1
- package/dist/async/settled.js +0 -32
- package/dist/async/settled.js.map +0 -1
- package/dist/behaviors/bounded.d.ts +0 -79
- package/dist/behaviors/bounded.d.ts.map +0 -1
- package/dist/behaviors/bounded.js +0 -393
- package/dist/behaviors/bounded.js.map +0 -1
- package/dist/behaviors/composite.d.ts +0 -17
- package/dist/behaviors/composite.d.ts.map +0 -1
- package/dist/behaviors/composite.js +0 -93
- package/dist/behaviors/composite.js.map +0 -1
- package/dist/behaviors/evented.d.ts +0 -154
- package/dist/behaviors/evented.d.ts.map +0 -1
- package/dist/behaviors/evented.js +0 -299
- package/dist/behaviors/evented.js.map +0 -1
- package/dist/behaviors/lru.d.ts +0 -84
- package/dist/behaviors/lru.d.ts.map +0 -1
- package/dist/behaviors/lru.js +0 -376
- package/dist/behaviors/lru.js.map +0 -1
- package/dist/behaviors/readonly.d.ts +0 -73
- package/dist/behaviors/readonly.d.ts.map +0 -1
- package/dist/behaviors/readonly.js +0 -189
- package/dist/behaviors/readonly.js.map +0 -1
- package/dist/behaviors/safe.d.ts +0 -163
- package/dist/behaviors/safe.d.ts.map +0 -1
- package/dist/behaviors/safe.js +0 -176
- package/dist/behaviors/safe.js.map +0 -1
- package/dist/behaviors/synchronized.d.ts +0 -88
- package/dist/behaviors/synchronized.d.ts.map +0 -1
- package/dist/behaviors/synchronized.js +0 -285
- package/dist/behaviors/synchronized.js.map +0 -1
- package/dist/behaviors/ttl.d.ts +0 -118
- package/dist/behaviors/ttl.d.ts.map +0 -1
- package/dist/behaviors/ttl.js +0 -354
- package/dist/behaviors/ttl.js.map +0 -1
- package/dist/contracts/model.d.ts +0 -29
- package/dist/contracts/model.d.ts.map +0 -1
- package/dist/contracts/model.js +0 -26
- package/dist/contracts/model.js.map +0 -1
- package/dist/core/effects.d.ts +0 -176
- package/dist/core/effects.d.ts.map +0 -1
- package/dist/core/effects.js +0 -180
- package/dist/core/effects.js.map +0 -1
- package/dist/core/events.d.ts +0 -192
- package/dist/core/events.d.ts.map +0 -1
- package/dist/core/events.js +0 -291
- package/dist/core/events.js.map +0 -1
- package/dist/core/middleware.d.ts +0 -231
- package/dist/core/middleware.d.ts.map +0 -1
- package/dist/core/middleware.js +0 -161
- package/dist/core/middleware.js.map +0 -1
- package/dist/core/policies.d.ts +0 -220
- package/dist/core/policies.d.ts.map +0 -1
- package/dist/core/policies.js +0 -48
- package/dist/core/policies.js.map +0 -1
- package/dist/core/traits.d.ts +0 -142
- package/dist/core/traits.d.ts.map +0 -1
- package/dist/core/traits.js +0 -8
- package/dist/core/traits.js.map +0 -1
- package/dist/fx/collectors.d.ts +0 -193
- package/dist/fx/collectors.d.ts.map +0 -1
- package/dist/fx/collectors.js +0 -375
- package/dist/fx/collectors.js.map +0 -1
- package/dist/fx/iter.d.ts +0 -166
- package/dist/fx/iter.d.ts.map +0 -1
- package/dist/fx/iter.js +0 -421
- package/dist/fx/iter.js.map +0 -1
- package/dist/impl/array-deque.d.ts +0 -112
- package/dist/impl/array-deque.d.ts.map +0 -1
- package/dist/impl/array-deque.js +0 -380
- package/dist/impl/array-deque.js.map +0 -1
- package/dist/impl/array-list.d.ts +0 -101
- package/dist/impl/array-list.d.ts.map +0 -1
- package/dist/impl/array-list.js +0 -387
- package/dist/impl/array-list.js.map +0 -1
- package/dist/impl/hash-map.d.ts +0 -149
- package/dist/impl/hash-map.d.ts.map +0 -1
- package/dist/impl/hash-map.js +0 -473
- package/dist/impl/hash-map.js.map +0 -1
- package/dist/impl/hash-set.d.ts +0 -125
- package/dist/impl/hash-set.d.ts.map +0 -1
- package/dist/impl/hash-set.js +0 -264
- package/dist/impl/hash-set.js.map +0 -1
- package/dist/impl/linked-hash-map.d.ts +0 -141
- package/dist/impl/linked-hash-map.d.ts.map +0 -1
- package/dist/impl/linked-hash-map.js +0 -442
- package/dist/impl/linked-hash-map.js.map +0 -1
- package/dist/impl/linked-list.d.ts +0 -109
- package/dist/impl/linked-list.d.ts.map +0 -1
- package/dist/impl/linked-list.js +0 -494
- package/dist/impl/linked-list.js.map +0 -1
- package/dist/impl/priority-queue.d.ts +0 -139
- package/dist/impl/priority-queue.d.ts.map +0 -1
- package/dist/impl/priority-queue.js +0 -343
- package/dist/impl/priority-queue.js.map +0 -1
- package/dist/impl/summary-tree.d.ts +0 -146
- package/dist/impl/summary-tree.d.ts.map +0 -1
- package/dist/impl/summary-tree.js +0 -524
- package/dist/impl/summary-tree.js.map +0 -1
- package/dist/impl/tree-map.d.ts +0 -153
- package/dist/impl/tree-map.d.ts.map +0 -1
- package/dist/impl/tree-map.js +0 -788
- package/dist/impl/tree-map.js.map +0 -1
- package/dist/impl/tree-set.d.ts +0 -112
- package/dist/impl/tree-set.d.ts.map +0 -1
- package/dist/impl/tree-set.js +0 -374
- package/dist/impl/tree-set.js.map +0 -1
- package/dist/interfaces/collection.d.ts +0 -119
- package/dist/interfaces/collection.d.ts.map +0 -1
- package/dist/interfaces/collection.js +0 -8
- package/dist/interfaces/collection.js.map +0 -1
- package/dist/interfaces/list.d.ts +0 -247
- package/dist/interfaces/list.d.ts.map +0 -1
- package/dist/interfaces/list.js +0 -8
- package/dist/interfaces/list.js.map +0 -1
- package/dist/interfaces/map.d.ts +0 -344
- package/dist/interfaces/map.d.ts.map +0 -1
- package/dist/interfaces/map.js +0 -8
- package/dist/interfaces/map.js.map +0 -1
- package/dist/interfaces/queue.d.ts +0 -304
- package/dist/interfaces/queue.d.ts.map +0 -1
- package/dist/interfaces/queue.js +0 -8
- package/dist/interfaces/queue.js.map +0 -1
- package/dist/interfaces/set.d.ts +0 -137
- package/dist/interfaces/set.d.ts.map +0 -1
- package/dist/interfaces/set.js +0 -8
- package/dist/interfaces/set.js.map +0 -1
- package/dist/order/index.d.ts +0 -7
- package/dist/order/index.d.ts.map +0 -1
- package/dist/order/index.js +0 -7
- package/dist/order/index.js.map +0 -1
- package/dist/order/rank.d.ts +0 -100
- package/dist/order/rank.d.ts.map +0 -1
- package/dist/order/rank.js +0 -286
- package/dist/order/rank.js.map +0 -1
- package/dist/storage/listeners.d.ts +0 -39
- package/dist/storage/listeners.d.ts.map +0 -1
- package/dist/storage/listeners.js +0 -65
- package/dist/storage/listeners.js.map +0 -1
- package/dist/testing/check.d.ts +0 -25
- package/dist/testing/check.d.ts.map +0 -1
- package/dist/testing/check.js +0 -85
- package/dist/testing/check.js.map +0 -1
- package/dist/utils/defaults.d.ts +0 -135
- package/dist/utils/defaults.d.ts.map +0 -1
- package/dist/utils/defaults.js +0 -322
- package/dist/utils/defaults.js.map +0 -1
- package/dist/utils/factories.d.ts +0 -144
- package/dist/utils/factories.d.ts.map +0 -1
- package/dist/utils/factories.js +0 -293
- package/dist/utils/factories.js.map +0 -1
- package/dist/utils/helpers.d.ts +0 -137
- package/dist/utils/helpers.d.ts.map +0 -1
- package/dist/utils/helpers.js +0 -221
- package/dist/utils/helpers.js.map +0 -1
- package/src/async/async-queue.ts +0 -481
- package/src/async/channels.ts +0 -450
- package/src/async/settled.ts +0 -41
- package/src/behaviors/bounded.ts +0 -485
- package/src/behaviors/composite.ts +0 -99
- package/src/behaviors/evented.ts +0 -464
- package/src/behaviors/lru.ts +0 -447
- package/src/behaviors/readonly.ts +0 -236
- package/src/behaviors/safe.ts +0 -372
- package/src/behaviors/synchronized.ts +0 -316
- package/src/behaviors/ttl.ts +0 -462
- package/src/contracts/model.ts +0 -44
- package/src/core/effects.ts +0 -323
- package/src/core/events.ts +0 -356
- package/src/core/middleware.ts +0 -415
- package/src/core/policies.ts +0 -316
- package/src/core/traits.ts +0 -164
- package/src/fx/collectors.ts +0 -500
- package/src/fx/iter.ts +0 -536
- package/src/impl/array-deque.ts +0 -463
- package/src/impl/array-list.ts +0 -490
- package/src/impl/hash-map.ts +0 -623
- package/src/impl/hash-set.ts +0 -342
- package/src/impl/linked-hash-map.ts +0 -572
- package/src/impl/linked-list.ts +0 -606
- package/src/impl/priority-queue.ts +0 -448
- package/src/impl/summary-tree.ts +0 -586
- package/src/impl/tree-map.ts +0 -912
- package/src/impl/tree-set.ts +0 -469
- package/src/interfaces/collection.ts +0 -141
- package/src/interfaces/list.ts +0 -281
- package/src/interfaces/map.ts +0 -405
- package/src/interfaces/queue.ts +0 -353
- package/src/interfaces/set.ts +0 -158
- package/src/order/index.ts +0 -6
- package/src/order/rank.ts +0 -271
- package/src/storage/listeners.ts +0 -72
- package/src/testing/check.ts +0 -81
- package/src/utils/defaults.ts +0 -345
- package/src/utils/factories.ts +0 -357
- package/src/utils/helpers.ts +0 -248
package/src/impl/tree-set.ts
DELETED
|
@@ -1,469 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TreeSet - Red-black tree based sorted set implementation.
|
|
3
|
-
*
|
|
4
|
-
* Implements a sorted set using TreeMap as the backing structure.
|
|
5
|
-
* Mirrors java.util.TreeSet.
|
|
6
|
-
*
|
|
7
|
-
* Key features:
|
|
8
|
-
* - O(log n) add, remove, contains
|
|
9
|
-
* - Elements maintained in sorted order (natural or custom comparator)
|
|
10
|
-
* - Navigation methods (floor, ceiling, lower, higher)
|
|
11
|
-
* - Range view operations (subSet, headSet, tailSet)
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* const set = treeSet<string>()
|
|
15
|
-
* set.add('c')
|
|
16
|
-
* set.add('a')
|
|
17
|
-
* set.add('b')
|
|
18
|
-
* // Iteration order: a, b, c (sorted)
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* // Custom comparator for descending order
|
|
22
|
-
* const set = treeSet<number>({
|
|
23
|
-
* compare: (a, b) => b - a
|
|
24
|
-
* })
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import type { SortedSet, NavigableSet } from "../interfaces/set.js";
|
|
28
|
-
import type { Eq, Compare } from "../core/traits.js";
|
|
29
|
-
import { TreeMap } from "./tree-map.js";
|
|
30
|
-
import { defaultEq, defaultCompare } from "../utils/defaults.js";
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Dummy value used for TreeMap backing.
|
|
34
|
-
*/
|
|
35
|
-
const PRESENT = true;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Options for creating a TreeSet.
|
|
39
|
-
*/
|
|
40
|
-
export interface TreeSetOptions<T> {
|
|
41
|
-
/**
|
|
42
|
-
* Comparison function for elements.
|
|
43
|
-
* @default defaultCompare
|
|
44
|
-
*/
|
|
45
|
-
compare?: Compare<T>;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Equality function for elements.
|
|
49
|
-
* @default defaultEq
|
|
50
|
-
*/
|
|
51
|
-
eq?: Eq<T>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* TreeSet<T> - Red-black tree based sorted set.
|
|
56
|
-
*
|
|
57
|
-
* Maintains elements in sorted order using a TreeMap as backing structure.
|
|
58
|
-
* Provides O(log n) operations and navigation methods.
|
|
59
|
-
*/
|
|
60
|
-
export class TreeSet<T> implements NavigableSet<T> {
|
|
61
|
-
private readonly _map: TreeMap<T, boolean>;
|
|
62
|
-
private readonly _compare: Compare<T>;
|
|
63
|
-
private readonly _eq: Eq<T>;
|
|
64
|
-
|
|
65
|
-
constructor(options: TreeSetOptions<T> = {}) {
|
|
66
|
-
const {
|
|
67
|
-
compare = defaultCompare,
|
|
68
|
-
eq = defaultEq,
|
|
69
|
-
} = options;
|
|
70
|
-
|
|
71
|
-
this._compare = compare;
|
|
72
|
-
this._eq = eq;
|
|
73
|
-
this._map = new TreeMap<T, boolean>({
|
|
74
|
-
compare,
|
|
75
|
-
eq,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// ========================================================================
|
|
80
|
-
// Size and state
|
|
81
|
-
// ========================================================================
|
|
82
|
-
|
|
83
|
-
get size(): number {
|
|
84
|
-
return this._map.size;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
get isEmpty(): boolean {
|
|
88
|
-
return this._map.isEmpty;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
get comparator(): Compare<T> {
|
|
92
|
-
return this._compare;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
get eq(): Eq<T> {
|
|
96
|
-
return this._eq;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// ========================================================================
|
|
100
|
-
// Membership operations
|
|
101
|
-
// ========================================================================
|
|
102
|
-
|
|
103
|
-
has(element: T): boolean {
|
|
104
|
-
return this._map.has(element);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
contains(element: T): boolean {
|
|
108
|
-
return this.has(element);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
containsAll(other: Iterable<T>): boolean {
|
|
112
|
-
for (const element of other) {
|
|
113
|
-
if (!this.has(element)) {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// ========================================================================
|
|
121
|
-
// Modification operations
|
|
122
|
-
// ========================================================================
|
|
123
|
-
|
|
124
|
-
add(element: T): boolean {
|
|
125
|
-
const oldValue = this._map.set(element, PRESENT);
|
|
126
|
-
return oldValue === undefined; // Returns true if element was added (not already present)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
remove(element: T): boolean {
|
|
130
|
-
const removed = this._map.delete(element);
|
|
131
|
-
return removed !== undefined;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
clear(): void {
|
|
135
|
-
this._map.clear();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// ========================================================================
|
|
139
|
-
// Bulk operations
|
|
140
|
-
// ========================================================================
|
|
141
|
-
|
|
142
|
-
addAll(elements: Iterable<T>): boolean {
|
|
143
|
-
let modified = false;
|
|
144
|
-
for (const element of elements) {
|
|
145
|
-
if (this.add(element)) {
|
|
146
|
-
modified = true;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return modified;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
removeAll(elements: Iterable<T>): boolean {
|
|
153
|
-
let modified = false;
|
|
154
|
-
for (const element of elements) {
|
|
155
|
-
if (this.remove(element)) {
|
|
156
|
-
modified = true;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return modified;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
retainAll(elements: Iterable<T>): boolean {
|
|
163
|
-
const toRetain = new Set<T>();
|
|
164
|
-
for (const element of elements) {
|
|
165
|
-
toRetain.add(element);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
let modified = false;
|
|
169
|
-
for (const element of Array.from(this)) {
|
|
170
|
-
if (!toRetain.has(element)) {
|
|
171
|
-
this.remove(element);
|
|
172
|
-
modified = true;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return modified;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// ========================================================================
|
|
179
|
-
// Set operations
|
|
180
|
-
// ========================================================================
|
|
181
|
-
|
|
182
|
-
union(other: Iterable<T>): TreeSet<T> {
|
|
183
|
-
const result = new TreeSet<T>({
|
|
184
|
-
compare: this._compare,
|
|
185
|
-
eq: this._eq,
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
for (const element of this) {
|
|
189
|
-
result.add(element);
|
|
190
|
-
}
|
|
191
|
-
for (const element of other) {
|
|
192
|
-
result.add(element);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return result;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
intersection(other: Iterable<T>): TreeSet<T> {
|
|
199
|
-
const result = new TreeSet<T>({
|
|
200
|
-
compare: this._compare,
|
|
201
|
-
eq: this._eq,
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
const otherSet = new Set<T>();
|
|
205
|
-
for (const element of other) {
|
|
206
|
-
otherSet.add(element);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
for (const element of this) {
|
|
210
|
-
if (otherSet.has(element)) {
|
|
211
|
-
result.add(element);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return result;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
difference(other: Iterable<T>): TreeSet<T> {
|
|
219
|
-
const result = new TreeSet<T>({
|
|
220
|
-
compare: this._compare,
|
|
221
|
-
eq: this._eq,
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
const otherSet = new Set<T>();
|
|
225
|
-
for (const element of other) {
|
|
226
|
-
otherSet.add(element);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
for (const element of this) {
|
|
230
|
-
if (!otherSet.has(element)) {
|
|
231
|
-
result.add(element);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return result;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
symmetricDifference(other: Iterable<T>): TreeSet<T> {
|
|
239
|
-
const result = new TreeSet<T>({
|
|
240
|
-
compare: this._compare,
|
|
241
|
-
eq: this._eq,
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
// Add all elements from this set
|
|
245
|
-
for (const element of this) {
|
|
246
|
-
result.add(element);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// Toggle elements from other set
|
|
250
|
-
for (const element of other) {
|
|
251
|
-
if (result.has(element)) {
|
|
252
|
-
result.remove(element);
|
|
253
|
-
} else {
|
|
254
|
-
result.add(element);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
isSubsetOf(other: Iterable<T>): boolean {
|
|
262
|
-
const otherSet = new Set<T>();
|
|
263
|
-
for (const element of other) {
|
|
264
|
-
otherSet.add(element);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
for (const element of this) {
|
|
268
|
-
if (!otherSet.has(element)) {
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
isSupersetOf(other: Iterable<T>): boolean {
|
|
276
|
-
for (const element of other) {
|
|
277
|
-
if (!this.has(element)) {
|
|
278
|
-
return false;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return true;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
isDisjointFrom(other: Iterable<T>): boolean {
|
|
285
|
-
for (const element of other) {
|
|
286
|
-
if (this.has(element)) {
|
|
287
|
-
return false;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return true;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// ========================================================================
|
|
294
|
-
// SortedSet operations
|
|
295
|
-
// ========================================================================
|
|
296
|
-
|
|
297
|
-
first(): T {
|
|
298
|
-
const key = this._map.firstKey();
|
|
299
|
-
return key;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
last(): T {
|
|
303
|
-
const key = this._map.lastKey();
|
|
304
|
-
return key;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
headSet(toElement: T): SortedSet<T> {
|
|
308
|
-
const result = new TreeSet<T>({
|
|
309
|
-
compare: this._compare,
|
|
310
|
-
eq: this._eq,
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
for (const element of this) {
|
|
314
|
-
if (this._compare(element, toElement) < 0) {
|
|
315
|
-
result.add(element);
|
|
316
|
-
} else {
|
|
317
|
-
break; // Elements are sorted
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
return result;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
tailSet(fromElement: T): SortedSet<T> {
|
|
325
|
-
const result = new TreeSet<T>({
|
|
326
|
-
compare: this._compare,
|
|
327
|
-
eq: this._eq,
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
let started = false;
|
|
331
|
-
for (const element of this) {
|
|
332
|
-
if (!started && this._compare(element, fromElement) < 0) {
|
|
333
|
-
continue;
|
|
334
|
-
}
|
|
335
|
-
started = true;
|
|
336
|
-
result.add(element);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
return result;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
subSet(fromElement: T, toElement: T): SortedSet<T> {
|
|
343
|
-
const result = new TreeSet<T>({
|
|
344
|
-
compare: this._compare,
|
|
345
|
-
eq: this._eq,
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
for (const element of this) {
|
|
349
|
-
const cmpFrom = this._compare(element, fromElement);
|
|
350
|
-
const cmpTo = this._compare(element, toElement);
|
|
351
|
-
|
|
352
|
-
if (cmpFrom >= 0 && cmpTo < 0) {
|
|
353
|
-
result.add(element);
|
|
354
|
-
} else if (cmpTo >= 0) {
|
|
355
|
-
break; // Past the range
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
return result;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// ========================================================================
|
|
363
|
-
// NavigableSet operations
|
|
364
|
-
// ========================================================================
|
|
365
|
-
|
|
366
|
-
floor(element: T): T | undefined {
|
|
367
|
-
return this._map.floorKey(element);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
ceiling(element: T): T | undefined {
|
|
371
|
-
return this._map.ceilingKey(element);
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
lower(element: T): T | undefined {
|
|
375
|
-
return this._map.lowerKey(element);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
higher(element: T): T | undefined {
|
|
379
|
-
return this._map.higherKey(element);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
pollFirst(): T | undefined {
|
|
383
|
-
const entry = this._map.pollFirstEntry();
|
|
384
|
-
return entry?.key;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
pollLast(): T | undefined {
|
|
388
|
-
const entry = this._map.pollLastEntry();
|
|
389
|
-
return entry?.key;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
descendingSet(): NavigableSet<T> {
|
|
393
|
-
const reversed = new TreeSet<T>({
|
|
394
|
-
compare: (a, b) => this._compare(b, a),
|
|
395
|
-
eq: this._eq,
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
for (const element of this) {
|
|
399
|
-
reversed.add(element);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
return reversed;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
*descendingIterator(): IterableIterator<T> {
|
|
406
|
-
yield* this._map.descendingKeys();
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
// ========================================================================
|
|
410
|
-
// View operations
|
|
411
|
-
// ========================================================================
|
|
412
|
-
|
|
413
|
-
*[Symbol.iterator](): Iterator<T> {
|
|
414
|
-
yield* this._map.keys();
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
forEach(action: (element: T, index: number) => void): void {
|
|
418
|
-
let index = 0;
|
|
419
|
-
for (const element of this) {
|
|
420
|
-
action(element, index++);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
toArray(): T[] {
|
|
425
|
-
return Array.from(this);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
// ========================================================================
|
|
429
|
-
// Utility methods
|
|
430
|
-
// ========================================================================
|
|
431
|
-
|
|
432
|
-
toString(): string {
|
|
433
|
-
const elements = Array.from(this).join(', ');
|
|
434
|
-
return `TreeSet{${elements}}`;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* Factory function to create a TreeSet.
|
|
440
|
-
*
|
|
441
|
-
* @example
|
|
442
|
-
* const set = treeSet<string>()
|
|
443
|
-
* set.add('c')
|
|
444
|
-
* set.add('a')
|
|
445
|
-
* set.add('b')
|
|
446
|
-
* // Iteration order: a, b, c
|
|
447
|
-
*
|
|
448
|
-
* @example
|
|
449
|
-
* // With initial elements
|
|
450
|
-
* const set = treeSet(['c', 'a', 'b'])
|
|
451
|
-
* // Iteration order: a, b, c
|
|
452
|
-
*
|
|
453
|
-
* @example
|
|
454
|
-
* // Custom comparator for descending order
|
|
455
|
-
* const set = treeSet<number>({ compare: (a, b) => b - a })
|
|
456
|
-
*/
|
|
457
|
-
export function treeSet<T>(
|
|
458
|
-
optionsOrElements?: TreeSetOptions<T> | Iterable<T>
|
|
459
|
-
): TreeSet<T> {
|
|
460
|
-
if (optionsOrElements && Symbol.iterator in optionsOrElements) {
|
|
461
|
-
const set = new TreeSet<T>();
|
|
462
|
-
for (const element of optionsOrElements as Iterable<T>) {
|
|
463
|
-
set.add(element);
|
|
464
|
-
}
|
|
465
|
-
return set;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
return new TreeSet<T>(optionsOrElements as TreeSetOptions<T>);
|
|
469
|
-
}
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base collection interfaces that mirror Java Collections Framework.
|
|
3
|
-
*
|
|
4
|
-
* These form the root of the collection hierarchy and define the
|
|
5
|
-
* fundamental operations common to all collections.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Eq } from "../core/traits.js";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* ReadonlyCollection<T> - The root interface for readonly collections.
|
|
12
|
-
*
|
|
13
|
-
* Mirrors java.util.Collection but without mutation operations.
|
|
14
|
-
* All collections should extend this or a more specific readonly interface.
|
|
15
|
-
*/
|
|
16
|
-
export interface ReadonlyCollection<T> extends Iterable<T> {
|
|
17
|
-
/**
|
|
18
|
-
* Returns the number of elements in this collection.
|
|
19
|
-
*/
|
|
20
|
-
readonly size: number;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Returns true if this collection contains no elements.
|
|
24
|
-
*/
|
|
25
|
-
readonly isEmpty: boolean;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Returns true if this collection contains the specified element.
|
|
29
|
-
* Uses the collection's equality function to compare elements.
|
|
30
|
-
*
|
|
31
|
-
* @param element The element to check for
|
|
32
|
-
*/
|
|
33
|
-
contains(element: T): boolean;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Returns true if this collection contains all elements in the specified collection.
|
|
37
|
-
*
|
|
38
|
-
* @param other The collection whose elements to check for
|
|
39
|
-
*/
|
|
40
|
-
containsAll(other: Iterable<T>): boolean;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Returns an array containing all elements in this collection.
|
|
44
|
-
* The returned array is safe to modify (it's a copy).
|
|
45
|
-
*/
|
|
46
|
-
toArray(): T[];
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Performs the given action for each element.
|
|
50
|
-
*
|
|
51
|
-
* @param action The action to perform for each element
|
|
52
|
-
*/
|
|
53
|
-
forEach(action: (element: T, index: number) => void): void;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Returns the equality function used by this collection.
|
|
57
|
-
*/
|
|
58
|
-
readonly eq: Eq<T>;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Collection<T> - The root interface for mutable collections.
|
|
63
|
-
*
|
|
64
|
-
* Mirrors java.util.Collection with mutation operations.
|
|
65
|
-
* All mutable collections should extend this or a more specific interface.
|
|
66
|
-
*/
|
|
67
|
-
export interface Collection<T> extends ReadonlyCollection<T> {
|
|
68
|
-
/**
|
|
69
|
-
* Adds an element to this collection.
|
|
70
|
-
*
|
|
71
|
-
* Returns true if the collection was modified as a result of this call.
|
|
72
|
-
* Some collections (like Set) may return false if the element already exists.
|
|
73
|
-
*
|
|
74
|
-
* @param element The element to add
|
|
75
|
-
* @returns true if the collection changed
|
|
76
|
-
* @throws Error if collection is full (bounded) and policy is 'throw'
|
|
77
|
-
*/
|
|
78
|
-
add(element: T): boolean;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Adds all elements from the specified collection to this collection.
|
|
82
|
-
*
|
|
83
|
-
* @param other The collection whose elements to add
|
|
84
|
-
* @returns true if the collection changed
|
|
85
|
-
*/
|
|
86
|
-
addAll(other: Iterable<T>): boolean;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Removes a single instance of the specified element from this collection.
|
|
90
|
-
*
|
|
91
|
-
* Returns true if an element was removed as a result of this call.
|
|
92
|
-
*
|
|
93
|
-
* @param element The element to remove
|
|
94
|
-
* @returns true if an element was removed
|
|
95
|
-
*/
|
|
96
|
-
remove(element: T): boolean;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Removes all elements from this collection that are also in the specified collection.
|
|
100
|
-
*
|
|
101
|
-
* @param other The collection whose elements to remove
|
|
102
|
-
* @returns true if the collection changed
|
|
103
|
-
*/
|
|
104
|
-
removeAll(other: Iterable<T>): boolean;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Retains only the elements in this collection that are in the specified collection.
|
|
108
|
-
* In other words, removes all elements not in the specified collection.
|
|
109
|
-
*
|
|
110
|
-
* @param other The collection of elements to retain
|
|
111
|
-
* @returns true if the collection changed
|
|
112
|
-
*/
|
|
113
|
-
retainAll(other: Iterable<T>): boolean;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Removes all elements from this collection.
|
|
117
|
-
* The collection will be empty after this call returns.
|
|
118
|
-
*/
|
|
119
|
-
clear(): void;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Utility type to check if a collection is readonly.
|
|
124
|
-
*/
|
|
125
|
-
export type IsReadonly<C> = C extends ReadonlyCollection<any>
|
|
126
|
-
? C extends Collection<any>
|
|
127
|
-
? false
|
|
128
|
-
: true
|
|
129
|
-
: false;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Extract element type from a collection.
|
|
133
|
-
*/
|
|
134
|
-
export type ElementOf<C> = C extends ReadonlyCollection<infer T> ? T : never;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Make a collection readonly by removing mutation methods.
|
|
138
|
-
*/
|
|
139
|
-
export type Readonly<C extends Collection<any>> = C extends Collection<infer T>
|
|
140
|
-
? ReadonlyCollection<T>
|
|
141
|
-
: never;
|