-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.ts
244 lines (224 loc) · 4.71 KB
/
map.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
/**
* @module
*
* MeekMap data collection.
*/
/**
* Private data.
*/
interface Pri<K extends WeakKey = WeakKey, V = any> {
/**
* Finalization registry.
*/
readonly fr: FinalizationRegistry<WeakRef<K>>;
/**
* Map of keys to weak references to keys.
*/
kwk: WeakMap<K, WeakRef<K>>;
/**
* Set of weak references to keys.
*/
readonly wk: Set<WeakRef<K>>;
/**
* Map of keys to values.
*/
kv: WeakMap<K, V>;
}
let pri: WeakMap<MeekMap, Pri>;
/**
* Like WeakMap.
*/
export class MeekMap<K extends WeakKey = WeakKey, V = any> {
/**
* Type string.
*/
declare public readonly [Symbol.toStringTag]: string;
/**
* Create a new MeekMap.
*
* @param iterable Initial pairs.
*/
constructor(iterable?: Iterable<readonly [K, V]> | null) {
const kwk = new WeakMap<K, WeakRef<K>>();
const wk = new Set<WeakRef<K>>();
const fr = new FinalizationRegistry(wk.delete.bind(wk));
const kv = new WeakMap<K, V>();
for (const [key, value] of iterable ?? []) {
let ref = kwk.get(key);
if (!ref) {
ref = new WeakRef(key);
fr.register(key, ref, key);
kwk.set(key, ref);
wk.add(ref);
}
kv.set(key, value);
}
(pri ??= new WeakMap()).set(this, { fr, kwk, wk, kv });
}
/**
* Iterator for key-value pairs in this map.
*
* @returns Key-value iterator.
*/
public *[Symbol.iterator](): Generator<[K, V], undefined, unknown> {
const p = pri.get(this) as Pri<K, V>;
for (const ref of p.wk) {
const key = ref.deref();
if (key) {
yield [key, p.kv.get(key) as V];
}
}
}
/**
* Clear this map.
*/
public clear(): void {
const p = pri.get(this) as Pri<K, V>;
const map = new WeakMap();
const values = new WeakMap();
p.wk.clear();
p.kwk = map;
p.kv = values;
}
/**
* Delete a key from this map.
*
* @param key Key to delete.
* @returns Whether the key was deleted.
*/
public delete(key: K): boolean {
const { fr, kv, kwk, wk } = pri.get(this) as Pri<K, V>;
const ref = kwk.get(key);
if (ref) {
fr.unregister(key);
kwk.delete(key);
kv.delete(key);
return wk.delete(ref);
}
return false;
}
/**
* Iterator for key-value pairs in this map.
*
* @returns Key-value iterator.
*/
public *entries(): Generator<[K, V], undefined, unknown> {
const p = pri.get(this) as Pri<K, V>;
for (const ref of p.wk) {
const key = ref.deref();
if (key) {
yield [key, p.kv.get(key) as V];
}
}
}
/**
* Call a function for each pair in this map.
*
* @param callbackfn Callback function.
* @param thisArg This argument.
*/
public forEach(
callbackfn: (value: V, key: K, map: MeekMap<K, V>) => void,
thisArg?: any,
): void {
const p = pri.get(this) as Pri<K, V>;
for (const ref of p.wk) {
const key = ref.deref();
if (key) {
callbackfn.call(thisArg, p.kv.get(key) as V, key, this);
}
}
}
/**
* Get the value for a key from this map.
*
* @param key Key to get.
* @returns Value for the key.
*/
public get(key: K): V | undefined {
const { kv, kwk } = pri.get(this) as Pri<K, V>;
const ref = kwk.get(key);
if (ref) {
const key = ref.deref();
if (key) {
return kv.get(key);
}
}
}
/**
* Has a key in this map.
*
* @param key Key to check.
* @returns Whether the key is in this map.
*/
public has(key: K): boolean {
return !!(pri.get(this) as Pri<K, V>).kwk.get(key)?.deref();
}
/**
* Iterator for keys in this map.
*
* @returns Key iterator.
*/
public *keys(): Generator<K, undefined, unknown> {
const { wk } = pri.get(this) as Pri<K, V>;
for (const ref of wk) {
const key = ref.deref();
if (key) {
yield key;
}
}
}
/**
* Set a value for a key in this map.
*
* @param key Key to set.
* @param value Value to set.
* @returns This map.
*/
public set(key: K, value: V): this {
const { fr, kv, kwk, wk } = pri.get(this) as Pri<K, V>;
let ref = kwk.get(key);
if (!ref) {
ref = new WeakRef(key);
fr.register(key, ref, key);
kwk.set(key, ref);
}
wk.add(ref);
kv.set(key, value);
return this;
}
/**
* The number of keys in this map.
* Can be greater than number of active keys.
*/
public get size(): number {
return (pri.get(this) as Pri<K, V>).wk.size;
}
/**
* Iterator for values in this map.
*
* @returns Value iterator.
*/
public *values(): Generator<V, undefined, unknown> {
const p = pri.get(this) as Pri<K, V>;
for (const ref of p.wk) {
const key = ref.deref();
if (key) {
yield p.kv.get(key) as V;
}
}
}
static {
Object.defineProperty(this.prototype, Symbol.toStringTag, {
value: 'MeekMap',
configurable: true,
});
}
}
/**
* Readonly MeekMap.
*/
export type ReadonlyMeekMap<K extends WeakKey = WeakKey, V = any> = Omit<
MeekMap<K, V>,
'clear' | 'delete' | 'set'
>;