-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.ts
More file actions
289 lines (267 loc) · 5.77 KB
/
map.ts
File metadata and controls
289 lines (267 loc) · 5.77 KB
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
/**
* @module
*
* MeekMap data collection.
*/
/**
* Private data.
*
* @template K Key type.
* @template V Value type.
*/
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>;
}
const pri = new WeakMap<MeekMap, Pri>();
/**
* Like WeakMap.
*
* @template K Key type.
* @template V Value type.
*/
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.set(this, { fr, kwk, wk, kv });
}
/**
* Iterator for key-value pairs in this map.
*
* @returns Key-value iterator.
*/
public *[Symbol.iterator](): MapIterator<[K, V]> {
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<K, WeakRef<K>>();
const values = new WeakMap<K, V>();
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(): MapIterator<[K, V]> {
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 {
return (pri.get(this) as Pri<K, V>).kv.get(key);
}
/**
* Get the value for a key from this map or insert the default value.
*
* @param key Key to get.
* @param defaultValue Default value.
* @returns Value for the key.
*/
public getOrInsert(key: K, defaultValue: V): V {
const { fr, kv, kwk, wk } = pri.get(this) as Pri<K, V>;
if (kv.has(key)) {
return kv.get(key)!;
}
const ref = new WeakRef(key);
fr.register(key, ref, key);
kwk.set(key, ref);
wk.add(ref);
kv.set(key, defaultValue);
return defaultValue;
}
/**
* Get the value for a key from this map or insert computed default value.
*
* @param key Key to get.
* @param callback Compute the default value.
* @returns Value for the key.
*/
public getOrInsertComputed(key: K, callback: (key: K) => V): V {
const { fr, kv, kwk, wk } = pri.get(this) as Pri<K, V>;
if (kv.has(key)) {
return kv.get(key)!;
}
const value = callback(key);
const ref = new WeakRef(key);
fr.register(key, ref, key);
kwk.set(key, ref);
wk.add(ref);
kv.set(key, value);
return value;
}
/**
* 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>).kv.has(key);
}
/**
* Iterator for keys in this map.
*
* @returns Key iterator.
*/
public *keys(): MapIterator<K> {
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(): MapIterator<V> {
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,
enumerable: false,
writable: false,
});
}
}
/**
* Readonly MeekMap.
*
* @template K Key type.
* @template V Value type.
*/
export type ReadonlyMeekMap<K extends WeakKey = WeakKey, V = any> = Omit<
MeekMap<K, V>,
'clear' | 'delete' | 'getOrInsert' | 'getOrInsertComputed' | 'set'
>;