-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathequivalent-key-map.js
238 lines (201 loc) · 6.22 KB
/
equivalent-key-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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Given an instance of EquivalentKeyMap, returns its internal value pair tuple
* for a key, if one exists. The tuple members consist of the last reference
* value for the key (used in efficient subsequent lookups) and the value
* assigned for the key at the leaf node.
*
* @param {EquivalentKeyMap} instance EquivalentKeyMap instance.
* @param {*} key The key for which to return value pair.
*
* @return {?Array} Value pair, if exists.
*/
function getValuePair( instance, key ) {
const { _map, _arrayTreeMap, _objectTreeMap } = instance;
// Map keeps a reference to the last object-like key used to set the
// value, which can be used to shortcut immediately to the value.
if ( _map.has( key ) ) {
return _map.get( key );
}
// Sort keys to ensure stable retrieval from tree.
const properties = Object.keys( key ).sort();
// Tree by type to avoid conflicts on numeric object keys, empty value.
let map = Array.isArray( key ) ? _arrayTreeMap : _objectTreeMap;
for ( let i = 0; i < properties.length; i++ ) {
const property = properties[ i ];
map = map.get( property );
if ( map === undefined ) {
return;
}
const propertyValue = key[ property ];
map = map.get( propertyValue );
if ( map === undefined ) {
return;
}
}
const valuePair = map.get( '_ekm_value' );
if ( ! valuePair ) {
return;
}
// If reached, it implies that an object-like key was set with another
// reference, so delete the reference and replace with the current.
_map.delete( valuePair[ 0 ] );
valuePair[ 0 ] = key;
map.set( '_ekm_value', valuePair );
_map.set( key, valuePair );
return valuePair;
}
/**
* Variant of a Map object which enables lookup by equivalent (deeply equal)
* object and array keys.
*/
class EquivalentKeyMap {
/**
* Constructs a new instance of EquivalentKeyMap.
*
* @param {Iterable.<*>} iterable Initial pair of key, value for map.
*/
constructor( iterable ) {
this.clear();
if ( iterable instanceof EquivalentKeyMap ) {
// Map#forEach is only means of iterating with support for IE11.
const iterablePairs = [];
iterable.forEach( ( value, key ) => {
iterablePairs.push( [ key, value ] );
} );
iterable = iterablePairs;
}
if ( iterable != null ) {
for ( let i = 0; i < iterable.length; i++ ) {
this.set( iterable[ i ][ 0 ], iterable[ i ][ 1 ] );
}
}
}
/**
* Accessor property returning the number of elements.
*
* @return {number} Number of elements.
*/
get size() {
return this._map.size;
}
/**
* Add or update an element with a specified key and value.
*
* @param {*} key The key of the element to add.
* @param {*} value The value of the element to add.
*
* @return {EquivalentKeyMap} Map instance.
*/
set( key, value ) {
// Shortcut non-object-like to set on internal Map.
if ( key === null || typeof key !== 'object' ) {
this._map.set( key, value );
return this;
}
// Sort keys to ensure stable assignment into tree.
const properties = Object.keys( key ).sort();
const valuePair = [ key, value ];
// Tree by type to avoid conflicts on numeric object keys, empty value.
let map = Array.isArray( key ) ? this._arrayTreeMap : this._objectTreeMap;
for ( let i = 0; i < properties.length; i++ ) {
const property = properties[ i ];
if ( ! map.has( property ) ) {
map.set( property, new EquivalentKeyMap );
}
map = map.get( property );
const propertyValue = key[ property ];
if ( ! map.has( propertyValue ) ) {
map.set( propertyValue, new EquivalentKeyMap );
}
map = map.get( propertyValue );
}
// If an _ekm_value exists, there was already an equivalent key. Before
// overriding, ensure that the old key reference is removed from map to
// avoid memory leak of accumulating equivalent keys. This is, in a
// sense, a poor man's WeakMap, while still enabling iterability.
const previousValuePair = map.get( '_ekm_value' );
if ( previousValuePair ) {
this._map.delete( previousValuePair[ 0 ] );
}
map.set( '_ekm_value', valuePair );
this._map.set( key, valuePair );
return this;
}
/**
* Returns a specified element.
*
* @param {*} key The key of the element to return.
*
* @return {?*} The element associated with the specified key or undefined
* if the key can't be found.
*/
get( key ) {
// Shortcut non-object-like to get from internal Map.
if ( key === null || typeof key !== 'object' ) {
return this._map.get( key );
}
const valuePair = getValuePair( this, key );
if ( valuePair ) {
return valuePair[ 1 ];
}
}
/**
* Returns a boolean indicating whether an element with the specified key
* exists or not.
*
* @param {*} key The key of the element to test for presence.
*
* @return {boolean} Whether an element with the specified key exists.
*/
has( key ) {
if ( key === null || typeof key !== 'object' ) {
return this._map.has( key );
}
// Test on the _presence_ of the pair, not its value, as even undefined
// can be a valid member value for a key.
return getValuePair( this, key ) !== undefined;
}
/**
* Removes the specified element.
*
* @param {*} key The key of the element to remove.
*
* @return {boolean} Returns true if an element existed and has been
* removed, or false if the element does not exist.
*/
delete( key ) {
if ( ! this.has( key ) ) {
return false;
}
// This naive implementation will leave orphaned child trees. A better
// implementation should traverse and remove orphans.
this.set( key, undefined );
return true;
}
/**
* Executes a provided function once per each key/value pair, in insertion
* order.
*
* @param {Function} callback Function to execute for each element.
* @param {*} thisArg Value to use as `this` when executing
* `callback`.
*/
forEach( callback, thisArg = this ) {
this._map.forEach( ( value, key ) => {
// Unwrap value from object-like value pair.
if ( key !== null && typeof key === 'object' ) {
value = value[ 1 ];
}
callback.call( thisArg, value, key, this );
} );
}
/**
* Removes all elements.
*/
clear() {
this._map = new Map;
this._arrayTreeMap = new Map;
this._objectTreeMap = new Map;
}
}
export default EquivalentKeyMap;