-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayMap-commonjs.js
166 lines (148 loc) · 4.38 KB
/
ArrayMap-commonjs.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
#!/usr/bin/env babel-node
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function id(x) {
return x;
}
class ArrayMap {
constructor(iterable = null, serialize = id) {
/* Creates an ArrayMap object with optional serialize that will
be called on every single key entered
*/
this.size = 0;
this.subMaps = new Map();
this.value = undefined;
this.hasValue = false;
this.serialize = serialize;
if (iterable != null) {
for (const [arrayKey, value] of iterable) {
this.set(arrayKey, value);
}
}
}
clear() {
/* Removes all elements from the ArrayMap */
this.size = 0;
this.subMaps = new Map();
this.value = undefined;
this.hasValue = false;
}
delete(arrayKey) {
/* Removes a item from the ArrayMap */
return this._delete(this.serialize(arrayKey));
}
/*eslint-disable complexity*/
_delete(arrayKey) {
const [first, ...rest] = arrayKey;
if (arrayKey.length === 0) {
if (this.hasValue) {
this.size -= 1;
this.value = undefined;
this.hasValue = false;
return true;
} else {
return false;
}
} else if (!this.subMaps.has(first)) {
return false;
} else {
const subMap = this.subMaps.get(first);
const result = subMap._delete(rest);
if (result) {
this.size -= 1;
if (subMap.size === 0) {
this.subMaps.delete(first);
}
}
return result;
}
}
/*eslint-enable complexity*/
*entries() {
/* Yields successively values from the ArrayMap in
depth first search order
*/
if (this.hasValue) {
yield [[], this.value];
}
for (const [key, subMap] of this.subMaps) {
for (const [arrayKey, value] of subMap.entries()) {
yield [[key, ...arrayKey], value];
}
}
}
forEach(callback, thisValue = null) {
/* Applies the callback to each value in the ArrayMap */
for (const [key, value] of this.entries()) {
Reflect.apply(callback, thisValue, [value, key, this]);
}
}
get(arrayKey) {
/* Returns the value for a given arrayKey, otherwise undefined */
return this._get(this.serialize(arrayKey));
}
_get(arrayKey) {
const [first, ...rest] = arrayKey;
if (arrayKey.length === 0) {
return this.value;
} else if (!this.subMaps.has(first)) {
return undefined;
} else {
return this.subMaps.get(first)._get(rest);
}
}
has(arrayKey) {
return this._has(this.serialize(arrayKey));
}
_has(arrayKey) {
const [first, ...rest] = arrayKey;
if (arrayKey.length === 0) {
return this.hasValue;
} else if (!this.subMaps.has(first)) {
return false;
} else {
return this.subMaps.get(first)._get(rest);
}
}
*keys() {
if (this.hasValue) {
yield [];
}
for (const [key, subMap] of this.subMaps) {
for (const [arrayKey] of subMap.entries()) {
yield [key, ...arrayKey];
}
}
}
set(arrayKey, value) {
/* Sets the value of an arrayKey to the given value */
return this._set(this.serialize(arrayKey), value);
}
_set(arrayKey, value) {
if (arrayKey.length === 0) {
if (!this.hasValue) {
this.size += 1;
}
this.value = value;
this.hasValue = true;
} else {
const [first, ...rest] = arrayKey;
if (!this.subMaps.has(first)) {
this.subMaps.set(first, new ArrayMap(null));
}
const subMap = this.subMaps.get(first);
const previousSize = subMap.size;
subMap._set(rest, value);
const newSize = subMap.size;
this.size += newSize - previousSize;
}
return this;
}
[Symbol.iterator]() {
return this.entries();
}
}
exports.default = ArrayMap;
module.exports = exports["default"];