forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bi-map.js
195 lines (158 loc) · 3.78 KB
/
bi-map.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Mnemonist BiMap
* ================
*
* JavaScript implementation of a BiMap.
*/
var forEach = require('obliterator/foreach');
/**
* Inverse Map.
*
* @constructor
*/
function InverseMap(original) {
this.size = 0;
this.items = new Map();
this.inverse = original;
}
/**
* BiMap.
*
* @constructor
*/
function BiMap() {
this.size = 0;
this.items = new Map();
this.inverse = new InverseMap(this);
}
/**
* Method used to clear the map.
*
* @return {undefined}
*/
function clear() {
this.size = 0;
this.items.clear();
this.inverse.items.clear();
}
BiMap.prototype.clear = clear;
InverseMap.prototype.clear = clear;
/**
* Method used to set a relation.
*
* @param {any} key - Key.
* @param {any} value - Value.
* @return {BiMap|InverseMap}
*/
function set(key, value) {
// First we need to attempt to see if the relation is not flawed
if (this.items.has(key)) {
var currentValue = this.items.get(key);
// The relation already exists, we do nothing
if (currentValue === value)
return this;
else
this.inverse.items.delete(currentValue);
}
if (this.inverse.items.has(value)) {
var currentKey = this.inverse.items.get(value);
if (currentKey === key)
return this;
else
this.items.delete(currentKey);
}
// Here we actually add the relation
this.items.set(key, value);
this.inverse.items.set(value, key);
// Size
this.size = this.items.size;
this.inverse.size = this.inverse.items.size;
return this;
}
BiMap.prototype.set = set;
InverseMap.prototype.set = set;
/**
* Method used to delete a relation.
*
* @param {any} key - Key.
* @return {boolean}
*/
function del(key) {
if (this.items.has(key)) {
var currentValue = this.items.get(key);
this.items.delete(key);
this.inverse.items.delete(currentValue);
// Size
this.size = this.items.size;
this.inverse.size = this.inverse.items.size;
return true;
}
return false;
}
BiMap.prototype.delete = del;
InverseMap.prototype.delete = del;
/**
* Mapping some Map prototype function unto our two classes.
*/
var METHODS = ['has', 'get', 'forEach', 'keys', 'values', 'entries'];
METHODS.forEach(function(name) {
BiMap.prototype[name] = InverseMap.prototype[name] = function() {
return Map.prototype[name].apply(this.items, arguments);
};
});
/**
* Attaching the #.values method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined') {
BiMap.prototype[Symbol.iterator] = BiMap.prototype.entries;
InverseMap.prototype[Symbol.iterator] = InverseMap.prototype.entries;
}
/**
* Convenience known methods.
*/
BiMap.prototype.inspect = function() {
var dummy = {
left: this.items,
right: this.inverse.items
};
// Trick so that node displays the name of the constructor
Object.defineProperty(dummy, 'constructor', {
value: BiMap,
enumerable: false
});
return dummy;
};
if (typeof Symbol !== 'undefined')
BiMap.prototype[Symbol.for('nodejs.util.inspect.custom')] = BiMap.prototype.inspect;
InverseMap.prototype.inspect = function() {
var dummy = {
left: this.inverse.items,
right: this.items
};
// Trick so that node displays the name of the constructor
Object.defineProperty(dummy, 'constructor', {
value: InverseMap,
enumerable: false
});
return dummy;
};
if (typeof Symbol !== 'undefined')
InverseMap.prototype[Symbol.for('nodejs.util.inspect.custom')] = InverseMap.prototype.inspect;
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a bimap.
*
* @param {Iterable} iterable - Target iterable.
* @return {BiMap}
*/
BiMap.from = function(iterable) {
var bimap = new BiMap();
forEach(iterable, function(value, key) {
bimap.set(key, value);
});
return bimap;
};
/**
* Exporting.
*/
module.exports = BiMap;