forked from educastellano/bidirectional-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
63 lines (59 loc) · 1.48 KB
/
index.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
export default class BidirectionalMap<T, U> {
_map = new Map<T, U>()
_reverse = new Map<U, T>()
constructor(arr: Array<[T, U]> = []) {
for (let el of arr) {
this.set(el[0], el[1])
}
}
get size () {
return this._map.size
}
set(key: T, value: U) {
let _value = this._map.get(key)
if(_value !== undefined) this._reverse.delete(_value)
let _key = this._reverse.get(value)
if(_key !== undefined) this._map.delete(_key)
this._map.set(key, value)
this._reverse.set(value, key)
}
get(key: T) {
return this._map.get(key)
}
getKey(value: U): T | undefined {
return this._reverse.get(value)
}
clear() {
this._map.clear()
this._reverse.clear()
}
delete(key: T) {
let value = this._map.get(key)
if(value === undefined) return false
this._map.delete(key)
this._reverse.delete(value)
return true
}
deleteValue(value: U) {
let key = this._reverse.get(value)
if(key === undefined) return false
this._map.delete(key)
this._reverse.delete(value)
return true
}
entries() {
return this._map.entries()
}
has(key: T) {
return this._map.has(key)
}
hasValue(value: U) {
return this._reverse.has(value)
}
keys() {
return this._map.keys()
}
values() {
return this._map.values()
}
}