-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbiDirectional.js
66 lines (54 loc) · 1.35 KB
/
biDirectional.js
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
module.exports = class BiDirectionalMap {
constructor(iterable) {
this._forward = new Map(iterable)
this._reverse = new Map()
this.size = 0
for (let entry of this._forward.entries()) {
this._reverse.set(entry[1], entry[0])
}
}
deleteWithKey(key) {
let value = this._forward.get(key)
let removed = this._forward.delete(key)
this._reverse.delete(value)
this.size--
return removed
}
deleteWithValue(value) {
let key = this._reverse.get(value)
let removed = this._reverse.delete(value)
this._forward.delete(key)
this.size--
return removed
}
forwardEntries() {
return this._forward.entries()
}
reverseEntries() {
return this._reverse.entries()
}
forEachForward(callback) {
this._forward.forEach(callback)
}
forEachReverse(callback) {
this._forward.forEach(callback)
}
getForward(key) {
return this._forward.get(key)
}
getReverse(value) {
return this._reverse.get(value)
}
keys() {
return this._forward.keys()
}
set(key, value) {
this._forward.set(key, value)
this._reverse.set(value, key)
this.size++
return this
}
values() {
this._forward.values()
}
}