-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDefaultDict.ts
More file actions
45 lines (38 loc) · 1.11 KB
/
DefaultDict.ts
File metadata and controls
45 lines (38 loc) · 1.11 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
/* 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
}
override get(key: K): V {
if (super.has(key)) return super.get(key)!
const value = this._defaultFactory(this)
super.set(key, value)
return value
}
setDefault(key: K, value: V): V {
if (super.has(key)) return super.get(key)!
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'))
const idPool = new DefaultDict<number, number>(d => d.size)
console.log(idPool.get(1))
console.log(idPool.get(2))
console.log(idPool.get(1))
console.log(idPool.get(3))
}