-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.ts
345 lines (320 loc) · 7.12 KB
/
set.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* @module
*
* MeekSet data collection.
*/
/**
* Private data.
*/
interface Pri<T extends WeakKey = WeakKey> {
/**
* Finalization registry.
*/
readonly fr: FinalizationRegistry<WeakRef<T>>;
/**
* Map of values to weak references.
*/
vwv: WeakMap<T, WeakRef<T>>;
/**
* Set of weak references to values.
*/
readonly wv: Set<WeakRef<T>>;
}
let pri: WeakMap<MeekSet, Pri>;
/**
* Like WeakSet.
*/
export class MeekSet<T extends WeakKey = WeakKey> {
/**
* Type string.
*/
declare public readonly [Symbol.toStringTag]: string;
/**
* Create a new MeekSet.
*
* @param iterable Initial values.
*/
constructor(iterable?: Iterable<T> | null) {
const vwv = new WeakMap<T, WeakRef<T>>();
const wv = new Set<WeakRef<T>>();
const fr = new FinalizationRegistry(wv.delete.bind(wv));
for (const value of iterable ?? []) {
if (!vwv.has(value)) {
const ref = new WeakRef(value);
fr.register(value, ref, value);
vwv.set(value, ref);
wv.add(ref);
}
}
(pri ??= new WeakMap()).set(this, { fr, vwv, wv });
}
/**
* Iterator for values in this set.
*
* @returns Set iterator.
*/
public *[Symbol.iterator](): Generator<T, undefined, unknown> {
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value) {
yield value;
}
}
}
/**
* Add a value to this set.
*
* @param value Value to add.
* @returns This set.
*/
public add(value: T): this {
const { fr, vwv, wv } = pri.get(this) as Pri<T>;
let ref = vwv.get(value);
if (!ref) {
ref = new WeakRef(value);
fr.register(value, ref, value);
vwv.set(value, ref);
wv.add(ref);
}
return this;
}
/**
* Clear this set.
*/
public clear(): void {
const p = pri.get(this) as Pri<T>;
const map = new WeakMap();
p.wv.clear();
p.vwv = map;
}
/**
* Delete a value from this set.
*
* @param value Value to delete.
* @returns Whether the value was deleted.
*/
public delete(value: T): boolean {
const { fr, vwv, wv } = pri.get(this) as Pri<T>;
const ref = vwv.get(value);
if (ref) {
fr.unregister(value);
vwv.delete(value);
return wv.delete(ref);
}
return false;
}
/**
* New MeekSet containing the values in this set not in other set.
*
* @param other Other set.
* @returns New MeekSet.
*/
public difference<U>(other: ReadonlySetLike<U>): MeekSet<T> {
const set = new MeekSet<T>();
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value && !other.has(value as unknown as U)) {
set.add(value);
}
}
return set;
}
/**
* Iterator for key-value pairs in this set.
*
* @returns Key-value iterator.
*/
public *entries(): Generator<[T, T], undefined, unknown> {
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value) {
yield [value, value];
}
}
}
/**
* Call a function for each value in this set.
*
* @param callbackfn Callback function.
* @param thisArg This argument.
*/
public forEach(
callbackfn: (value: T, value2: T, set: MeekSet<T>) => void,
thisArg?: any,
): void {
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value) {
callbackfn.call(thisArg, value, value, this);
}
}
}
/**
* Has a value in this set.
*
* @param value Value to check.
* @returns Whether the value is in this set.
*/
public has(value: T): boolean {
return (pri.get(this) as Pri<T>).vwv.has(value);
}
/**
* New MeekSet containing the values in both sets.
*
* @param other Other set.
* @returns New MeekSet.
*/
public intersection<U extends WeakKey>(
other: ReadonlySetLike<U>,
): MeekSet<T & U> {
const set = new MeekSet<T & U>();
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref() as T & U;
if (value) {
if (other.has(value)) {
set.add(value);
}
}
}
return set;
}
/**
* Is every value in this set not in other set.
*
* @param other Other set.
* @returns Whether every value in this set is not in other set.
*/
public isDisjointFrom(other: ReadonlySetLike<unknown>): boolean {
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value && other.has(value)) {
return false;
}
}
return true;
}
/**
* Is every value in this set in other set.
*
* @param other Other set.
* @returns Whether every value in this set is in other set.
*/
public isSubsetOf(other: ReadonlySetLike<unknown>): boolean {
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value && !other.has(value)) {
return false;
}
}
return true;
}
/**
* Is every value in other set in this set.
*
* @param other Other set.
* @returns Whether every value in other set is in this set.
*/
public isSupersetOf(other: ReadonlySetLike<unknown>): boolean {
const p = pri.get(this) as Pri<T>;
const it = other.keys();
for (let result = it.next(); !result.done; result = it.next()) {
const { value } = result as { value: T };
if (!p.vwv.has(value)) {
return false;
}
}
return true;
}
/**
* Iterator for keys in this set.
*
* @returns Key iterator.
*/
public *keys(): Generator<T, undefined, unknown> {
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value) {
yield value;
}
}
}
/**
* The number of values in this set.
* Can be greater than number of active keys.
*/
public get size(): number {
return (pri.get(this) as Pri<T>).wv.size;
}
/**
* New MeekSet containing the values in either set but not both.
*
* @param other Other set.
* @returns New MeekSet.
*/
public symmetricDifference<U extends WeakKey>(
other: ReadonlySetLike<U>,
): MeekSet<T | U> {
const p = pri.get(this) as Pri<T>;
const set = new MeekSet<T | U>();
for (const ref of p.wv) {
const value = ref.deref();
if (value && !other.has(value as unknown as U)) {
set.add(value);
}
}
const it = other.keys();
for (let result = it.next(); !result.done; result = it.next()) {
const { value } = result as { value: T & U };
if (!p.vwv.has(value)) {
set.add(value);
}
}
return set;
}
/**
* New MeekSet containing all values from both sets.
*
* @param other Other set.
* @returns New MeekSet.
*/
public union<U extends WeakKey>(other: ReadonlySetLike<U>): MeekSet<T | U> {
const set = new MeekSet<T | U>();
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value) {
set.add(value);
}
}
const it = other.keys();
for (let result = it.next(); !result.done; result = it.next()) {
set.add(result.value);
}
return set;
}
/**
* Iterator for values in this set.
*
* @returns Value iterator.
*/
public *values(): Generator<T, undefined, unknown> {
for (const ref of (pri.get(this) as Pri<T>).wv) {
const value = ref.deref();
if (value) {
yield value;
}
}
}
static {
Object.defineProperty(this.prototype, Symbol.toStringTag, {
value: 'MeekSet',
configurable: true,
});
}
}
/**
* Readonly MeekSet.
*/
export type ReadonlyMeekSet<T extends WeakKey> = Omit<
MeekSet<T>,
'add' | 'clear' | 'delete'
>;