-
Notifications
You must be signed in to change notification settings - Fork 31
/
DefaultDict.ts
38 lines (32 loc) · 999 Bytes
/
DefaultDict.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
/* eslint-disable max-len */
/**
* 带有默认值的字典.
* 模拟python的`collections.defaultdict`.
*/
class DefaultDict<K, V> extends Map<K, V> {
private readonly _defaultFactory: (self: DefaultDict<K, V>) => V
constructor(defaultFactory: (self: DefaultDict<K, V>) => V, iterable?: Iterable<readonly [K, V]> | null) {
super(iterable)
this._defaultFactory = defaultFactory
}
setDefault(key: K, value: V): V {
const old = super.get(key)
if (old !== void 0) return old // has key
super.set(key, value)
return value
}
override get(key: K): V {
const old = super.get(key)
if (old !== void 0) return old // has key
const value = this._defaultFactory(this)
super.set(key, value)
return value
}
}
export { DefaultDict }
if (require.main === module) {
const mp = new DefaultDict<string, number>(d => d.size)
console.log(mp.get('12'))
console.log(mp.get('121'))
console.log(mp.get('12'))
}