-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathEnumMap.js
More file actions
78 lines (57 loc) · 1.47 KB
/
EnumMap.js
File metadata and controls
78 lines (57 loc) · 1.47 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
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
class BiMap {
constructor(map, inverseMap) {
this.map = map;
this.inverseMap = inverseMap;
}
static create() {
return new BiMap(new Map(), new Map());
}
get size() {
return this.map.size;
}
set(key, value) {
const oldValue = this.map.get(key);
this.inverseMap.delete(oldValue);
this.map.set(key, value);
this.inverseMap.set(value, key);
return this;
}
clear() {
this.map.clear();
this.inverseMap.clear();
}
delete(key) {
const value = this.map.get(key);
const deleted = this.map.delete(key);
const inverseDeleted = this.inverseMap.delete(value);
return deleted || inverseDeleted;
}
entries() {
return this.map.entries();
}
forEach(callbackFn, thisArg) {
return this.map.forEach(callbackFn, thisArg);
}
get(key) {
return this.map.get(key);
}
has(key) {
return this.map.has(key);
}
keys() {
return this.map.keys();
}
inverse() {
return new BiMap(this.inverseMap, this.map);
}
values() {
return this.inverseMap.keys();
}
*[Symbol.iterator]() {
yield* this.map;
}
}
const TYPE_MAP = BiMap.create()
TYPE_MAP.set(0, '衣服')
console.log('TYPE_MAP正向:', TYPE_MAP.get(0)); // TYPE_MAP正向: 衣服
console.log('TYPE_MAP逆向: ', TYPE_MAP.inverse().get('衣服')); // TYPE_MAP逆向: 0