-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (57 loc) · 1.5 KB
/
index.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
class BiMap extends Map {
/**
* Creates a bi-directional map, with unique keys to unique values.
* @param {Map?} iter predefined entries (iterable)
* @param {nop?} inv please leave this empty
*/
constructor(iter, inv=null) {
super(iter||[]);
inv = inv||new BiMap(flipEntries(iter||[]), this);
Object.defineProperty(this, 'inverse', {get: () => inv});
}
/**
* Gives reversed bi-directional map.
*/
get inverse() {
return null;
}
/**
* Removes all elements from map.
*/
clear() {
super.clear();
this.inverse.clear();
}
/**
* Removes the specified element from map.
* @param {*} key key of the element to remove
* @returns {boolean} true if element was removed
*/
delete(key) {
var val = super.get(key);
if(val===undefined) return false;
super.delete(key);
this.inverse.delete(val);
return true;
}
/**
* Adds or updates an element with a specified key and a value to map.
* @param {*} key key of the element to add
* @param {*} val value of the element to add
* @returns {BiMap} map
*/
set(key, val) {
var valOld = super.get(key);
if(val===valOld) return this;
if(this.inverse.has(val) && this.inverse.get(val)!==key) throw new Error('BiMap pair is not unique');
if(valOld!==undefined) this.inverse.delete(valOld);
super.set(key, val);
this.inverse.set(val, key);
return this;
}
}
function* flipEntries(entries) {
for([k, v] of entries)
yield [v, k];
}
module.exports = BiMap;