-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterable_weak_set.ts
98 lines (86 loc) · 2.23 KB
/
iterable_weak_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
export class IterableWeakSet<T extends object> implements
WeakSet<T>,
Omit<
Set<T>,
| "add" // override to return this
| "forEach" // override to call callbackfn with IterableWeakSet
| "union" // not implemented
| "intersection" // not implemented
| "difference" // not implemented
| "symmetricDifference" // not implemented
| "isSubsetOf" // not implemented
| "isSupersetOf" // not implemented
| "isDisjointFrom" // not implemented
> {
#weakMap = new WeakMap<T, WeakRef<T>>();
#set = new Set<WeakRef<T>>();
#registry = new FinalizationRegistry<WeakRef<T>>(
this.#set.delete.bind(this.#set),
);
constructor(values?: readonly T[] | null);
constructor(iterable: Iterable<T>);
constructor(iterable: Iterable<T> | null = null) {
for (const value of iterable ?? []) {
this.add(value);
}
}
get size(): number {
return this.#set.size;
}
add(value: T): this {
if (this.has(value)) {
return this;
}
const ref = new WeakRef(value);
this.#weakMap.set(value, ref);
this.#set.add(ref);
this.#registry.register(value, ref, ref);
return this;
}
clear(): void {
for (const value of this.#set) {
this.delete(value.deref()!);
}
}
delete(value: T): boolean {
const ref = this.#weakMap.get(value);
if (ref) {
this.#set.delete(ref);
this.#weakMap.delete(value);
this.#registry.unregister(ref);
return true;
}
return false;
}
has(value: T): boolean {
return this.#weakMap.has(value);
}
forEach(
callbackfn: (value: T, value2: T, set: IterableWeakSet<T>) => void,
thisArg?: unknown,
): void {
for (const tuple of this.entries()) {
callbackfn.call(thisArg, tuple[0], tuple[1], this);
}
}
get [Symbol.toStringTag](): string {
return "IterableWeakSet";
}
*[Symbol.iterator](): IterableIterator<T> {
for (const ref of this.#set) {
yield ref.deref()!;
}
}
*entries(): IterableIterator<[T, T]> {
for (const ref of this.#set) {
const value = ref.deref()!;
yield [value, value];
}
}
keys(): IterableIterator<T> {
return this[Symbol.iterator]();
}
values(): IterableIterator<T> {
return this[Symbol.iterator]();
}
}