immutable 5.0.0-beta.3 → 5.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/es/Collection.js +75 -0
- package/dist/es/CollectionImpl.js +781 -0
- package/dist/es/Hash.js +260 -0
- package/dist/es/Immutable.js +73 -0
- package/dist/es/Iterator.js +106 -0
- package/dist/es/List.js +692 -0
- package/dist/es/Map.js +833 -0
- package/dist/es/Math.js +45 -0
- package/dist/es/Operations.js +953 -0
- package/dist/es/OrderedMap.js +195 -0
- package/dist/es/OrderedSet.js +91 -0
- package/dist/es/PairSorting.js +30 -0
- package/dist/es/Range.js +175 -0
- package/dist/es/Record.js +291 -0
- package/dist/es/Repeat.js +130 -0
- package/dist/es/Seq.js +397 -0
- package/dist/es/Set.js +278 -0
- package/dist/es/Stack.js +260 -0
- package/dist/es/TrieUtils.js +117 -0
- package/dist/es/fromJS.js +74 -0
- package/dist/es/functional/get.js +38 -0
- package/dist/es/functional/getIn.js +41 -0
- package/dist/es/functional/has.js +35 -0
- package/dist/es/functional/hasIn.js +32 -0
- package/dist/es/functional/merge.js +137 -0
- package/dist/es/functional/remove.js +56 -0
- package/dist/es/functional/removeIn.js +32 -0
- package/dist/es/functional/set.js +52 -0
- package/dist/es/functional/setIn.js +32 -0
- package/dist/es/functional/update.js +31 -0
- package/dist/es/functional/updateIn.js +94 -0
- package/dist/es/is.js +108 -0
- package/dist/es/methods/asImmutable.js +29 -0
- package/dist/es/methods/asMutable.js +31 -0
- package/dist/es/methods/deleteIn.js +31 -0
- package/dist/es/methods/getIn.js +31 -0
- package/dist/es/methods/hasIn.js +31 -0
- package/dist/es/methods/merge.js +79 -0
- package/dist/es/methods/mergeDeep.js +41 -0
- package/dist/es/methods/mergeDeepIn.js +37 -0
- package/dist/es/methods/mergeIn.js +36 -0
- package/dist/es/methods/setIn.js +31 -0
- package/dist/es/methods/toObject.js +36 -0
- package/dist/es/methods/update.js +33 -0
- package/dist/es/methods/updateIn.js +31 -0
- package/dist/es/methods/wasAltered.js +29 -0
- package/dist/es/methods/withMutations.js +31 -0
- package/dist/es/package.json.js +27 -0
- package/dist/es/predicates/isAssociative.js +32 -0
- package/dist/es/predicates/isCollection.js +32 -0
- package/dist/es/predicates/isImmutable.js +32 -0
- package/dist/es/predicates/isIndexed.js +31 -0
- package/dist/es/predicates/isKeyed.js +31 -0
- package/dist/es/predicates/isList.js +31 -0
- package/dist/es/predicates/isMap.js +31 -0
- package/dist/es/predicates/isOrdered.js +31 -0
- package/dist/es/predicates/isOrderedMap.js +32 -0
- package/dist/es/predicates/isOrderedSet.js +32 -0
- package/dist/es/predicates/isRecord.js +31 -0
- package/dist/es/predicates/isSeq.js +31 -0
- package/dist/es/predicates/isSet.js +31 -0
- package/dist/es/predicates/isStack.js +31 -0
- package/dist/es/predicates/isValueObject.js +33 -0
- package/dist/es/toJS.js +54 -0
- package/dist/es/utils/arrCopy.js +36 -0
- package/dist/es/utils/assertNotInfinite.js +34 -0
- package/dist/es/utils/coerceKeyPath.js +40 -0
- package/dist/es/utils/deepEqual.js +99 -0
- package/dist/es/utils/hasOwnProperty.js +27 -0
- package/dist/es/utils/invariant.js +29 -0
- package/dist/es/utils/isArrayLike.js +44 -0
- package/dist/es/utils/isDataStructure.js +39 -0
- package/dist/es/utils/isPlainObj.js +52 -0
- package/dist/es/utils/mixin.js +38 -0
- package/dist/es/utils/quoteString.js +36 -0
- package/dist/es/utils/shallowCopy.js +41 -0
- package/dist/immutable.d.ts +137 -16
- package/dist/immutable.js +25 -68
- package/dist/immutable.min.js +2 -32
- package/package.json +2 -3
- package/dist/immutable.es.js +0 -5965
package/dist/es/Map.js
ADDED
|
@@ -0,0 +1,833 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2014-present, Lee Byron and other contributors.
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
import { is } from './is.js';
|
|
26
|
+
import { Collection, KeyedCollection } from './Collection.js';
|
|
27
|
+
import { isMap, IS_MAP_SYMBOL } from './predicates/isMap.js';
|
|
28
|
+
import { isOrdered } from './predicates/isOrdered.js';
|
|
29
|
+
import { DELETE, NOT_SET, SetRef, MakeRef, MASK, SHIFT, SIZE, OwnerID } from './TrieUtils.js';
|
|
30
|
+
import { hash } from './Hash.js';
|
|
31
|
+
import { iteratorDone, Iterator, iteratorValue } from './Iterator.js';
|
|
32
|
+
import { sortFactory } from './Operations.js';
|
|
33
|
+
import arrCopy from './utils/arrCopy.js';
|
|
34
|
+
import assertNotInfinite from './utils/assertNotInfinite.js';
|
|
35
|
+
import { setIn } from './methods/setIn.js';
|
|
36
|
+
import { deleteIn } from './methods/deleteIn.js';
|
|
37
|
+
import { update } from './methods/update.js';
|
|
38
|
+
import { updateIn } from './methods/updateIn.js';
|
|
39
|
+
import { merge, mergeWith } from './methods/merge.js';
|
|
40
|
+
import { mergeDeep, mergeDeepWith } from './methods/mergeDeep.js';
|
|
41
|
+
import { mergeIn } from './methods/mergeIn.js';
|
|
42
|
+
import { mergeDeepIn } from './methods/mergeDeepIn.js';
|
|
43
|
+
import { withMutations } from './methods/withMutations.js';
|
|
44
|
+
import { asMutable } from './methods/asMutable.js';
|
|
45
|
+
import { asImmutable } from './methods/asImmutable.js';
|
|
46
|
+
import { wasAltered } from './methods/wasAltered.js';
|
|
47
|
+
import { OrderedMap } from './OrderedMap.js';
|
|
48
|
+
|
|
49
|
+
var Map = /*@__PURE__*/(function (KeyedCollection) {
|
|
50
|
+
function Map(value) {
|
|
51
|
+
return value === undefined || value === null
|
|
52
|
+
? emptyMap()
|
|
53
|
+
: isMap(value) && !isOrdered(value)
|
|
54
|
+
? value
|
|
55
|
+
: emptyMap().withMutations(function (map) {
|
|
56
|
+
var iter = KeyedCollection(value);
|
|
57
|
+
assertNotInfinite(iter.size);
|
|
58
|
+
iter.forEach(function (v, k) { return map.set(k, v); });
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if ( KeyedCollection ) Map.__proto__ = KeyedCollection;
|
|
63
|
+
Map.prototype = Object.create( KeyedCollection && KeyedCollection.prototype );
|
|
64
|
+
Map.prototype.constructor = Map;
|
|
65
|
+
|
|
66
|
+
Map.of = function of () {
|
|
67
|
+
var keyValues = [], len = arguments.length;
|
|
68
|
+
while ( len-- ) keyValues[ len ] = arguments[ len ];
|
|
69
|
+
|
|
70
|
+
return emptyMap().withMutations(function (map) {
|
|
71
|
+
for (var i = 0; i < keyValues.length; i += 2) {
|
|
72
|
+
if (i + 1 >= keyValues.length) {
|
|
73
|
+
throw new Error('Missing value for key: ' + keyValues[i]);
|
|
74
|
+
}
|
|
75
|
+
map.set(keyValues[i], keyValues[i + 1]);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
Map.prototype.toString = function toString () {
|
|
81
|
+
return this.__toString('Map {', '}');
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// @pragma Access
|
|
85
|
+
|
|
86
|
+
Map.prototype.get = function get (k, notSetValue) {
|
|
87
|
+
return this._root
|
|
88
|
+
? this._root.get(0, undefined, k, notSetValue)
|
|
89
|
+
: notSetValue;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// @pragma Modification
|
|
93
|
+
|
|
94
|
+
Map.prototype.set = function set (k, v) {
|
|
95
|
+
return updateMap(this, k, v);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
Map.prototype.remove = function remove (k) {
|
|
99
|
+
return updateMap(this, k, NOT_SET);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
Map.prototype.deleteAll = function deleteAll (keys) {
|
|
103
|
+
var collection = Collection(keys);
|
|
104
|
+
|
|
105
|
+
if (collection.size === 0) {
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return this.withMutations(function (map) {
|
|
110
|
+
collection.forEach(function (key) { return map.remove(key); });
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
Map.prototype.clear = function clear () {
|
|
115
|
+
if (this.size === 0) {
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
if (this.__ownerID) {
|
|
119
|
+
this.size = 0;
|
|
120
|
+
this._root = null;
|
|
121
|
+
this.__hash = undefined;
|
|
122
|
+
this.__altered = true;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
return emptyMap();
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// @pragma Composition
|
|
129
|
+
|
|
130
|
+
Map.prototype.sort = function sort (comparator) {
|
|
131
|
+
// Late binding
|
|
132
|
+
return OrderedMap(sortFactory(this, comparator));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
Map.prototype.sortBy = function sortBy (mapper, comparator) {
|
|
136
|
+
// Late binding
|
|
137
|
+
return OrderedMap(sortFactory(this, comparator, mapper));
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
Map.prototype.map = function map (mapper, context) {
|
|
141
|
+
var this$1$1 = this;
|
|
142
|
+
|
|
143
|
+
return this.withMutations(function (map) {
|
|
144
|
+
map.forEach(function (value, key) {
|
|
145
|
+
map.set(key, mapper.call(context, value, key, this$1$1));
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// @pragma Mutability
|
|
151
|
+
|
|
152
|
+
Map.prototype.__iterator = function __iterator (type, reverse) {
|
|
153
|
+
return new MapIterator(this, type, reverse);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
Map.prototype.__iterate = function __iterate (fn, reverse) {
|
|
157
|
+
var this$1$1 = this;
|
|
158
|
+
|
|
159
|
+
var iterations = 0;
|
|
160
|
+
this._root &&
|
|
161
|
+
this._root.iterate(function (entry) {
|
|
162
|
+
iterations++;
|
|
163
|
+
return fn(entry[1], entry[0], this$1$1);
|
|
164
|
+
}, reverse);
|
|
165
|
+
return iterations;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
Map.prototype.__ensureOwner = function __ensureOwner (ownerID) {
|
|
169
|
+
if (ownerID === this.__ownerID) {
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
if (!ownerID) {
|
|
173
|
+
if (this.size === 0) {
|
|
174
|
+
return emptyMap();
|
|
175
|
+
}
|
|
176
|
+
this.__ownerID = ownerID;
|
|
177
|
+
this.__altered = false;
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
return makeMap(this.size, this._root, ownerID, this.__hash);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
return Map;
|
|
184
|
+
}(KeyedCollection));
|
|
185
|
+
|
|
186
|
+
Map.isMap = isMap;
|
|
187
|
+
|
|
188
|
+
var MapPrototype = Map.prototype;
|
|
189
|
+
MapPrototype[IS_MAP_SYMBOL] = true;
|
|
190
|
+
MapPrototype[DELETE] = MapPrototype.remove;
|
|
191
|
+
MapPrototype.removeAll = MapPrototype.deleteAll;
|
|
192
|
+
MapPrototype.setIn = setIn;
|
|
193
|
+
MapPrototype.removeIn = MapPrototype.deleteIn = deleteIn;
|
|
194
|
+
MapPrototype.update = update;
|
|
195
|
+
MapPrototype.updateIn = updateIn;
|
|
196
|
+
MapPrototype.merge = MapPrototype.concat = merge;
|
|
197
|
+
MapPrototype.mergeWith = mergeWith;
|
|
198
|
+
MapPrototype.mergeDeep = mergeDeep;
|
|
199
|
+
MapPrototype.mergeDeepWith = mergeDeepWith;
|
|
200
|
+
MapPrototype.mergeIn = mergeIn;
|
|
201
|
+
MapPrototype.mergeDeepIn = mergeDeepIn;
|
|
202
|
+
MapPrototype.withMutations = withMutations;
|
|
203
|
+
MapPrototype.wasAltered = wasAltered;
|
|
204
|
+
MapPrototype.asImmutable = asImmutable;
|
|
205
|
+
MapPrototype['@@transducer/init'] = MapPrototype.asMutable = asMutable;
|
|
206
|
+
MapPrototype['@@transducer/step'] = function (result, arr) {
|
|
207
|
+
return result.set(arr[0], arr[1]);
|
|
208
|
+
};
|
|
209
|
+
MapPrototype['@@transducer/result'] = function (obj) {
|
|
210
|
+
return obj.asImmutable();
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// #pragma Trie Nodes
|
|
214
|
+
|
|
215
|
+
var ArrayMapNode = function ArrayMapNode(ownerID, entries) {
|
|
216
|
+
this.ownerID = ownerID;
|
|
217
|
+
this.entries = entries;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
ArrayMapNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
|
|
221
|
+
var entries = this.entries;
|
|
222
|
+
for (var ii = 0, len = entries.length; ii < len; ii++) {
|
|
223
|
+
if (is(key, entries[ii][0])) {
|
|
224
|
+
return entries[ii][1];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return notSetValue;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
ArrayMapNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
|
|
231
|
+
var removed = value === NOT_SET;
|
|
232
|
+
|
|
233
|
+
var entries = this.entries;
|
|
234
|
+
var idx = 0;
|
|
235
|
+
var len = entries.length;
|
|
236
|
+
for (; idx < len; idx++) {
|
|
237
|
+
if (is(key, entries[idx][0])) {
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
var exists = idx < len;
|
|
242
|
+
|
|
243
|
+
if (exists ? entries[idx][1] === value : removed) {
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
SetRef(didAlter);
|
|
248
|
+
(removed || !exists) && SetRef(didChangeSize);
|
|
249
|
+
|
|
250
|
+
if (removed && entries.length === 1) {
|
|
251
|
+
return; // undefined
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!exists && !removed && entries.length >= MAX_ARRAY_MAP_SIZE) {
|
|
255
|
+
return createNodes(ownerID, entries, key, value);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
var isEditable = ownerID && ownerID === this.ownerID;
|
|
259
|
+
var newEntries = isEditable ? entries : arrCopy(entries);
|
|
260
|
+
|
|
261
|
+
if (exists) {
|
|
262
|
+
if (removed) {
|
|
263
|
+
idx === len - 1
|
|
264
|
+
? newEntries.pop()
|
|
265
|
+
: (newEntries[idx] = newEntries.pop());
|
|
266
|
+
} else {
|
|
267
|
+
newEntries[idx] = [key, value];
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
newEntries.push([key, value]);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (isEditable) {
|
|
274
|
+
this.entries = newEntries;
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return new ArrayMapNode(ownerID, newEntries);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
var BitmapIndexedNode = function BitmapIndexedNode(ownerID, bitmap, nodes) {
|
|
282
|
+
this.ownerID = ownerID;
|
|
283
|
+
this.bitmap = bitmap;
|
|
284
|
+
this.nodes = nodes;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
BitmapIndexedNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
|
|
288
|
+
if (keyHash === undefined) {
|
|
289
|
+
keyHash = hash(key);
|
|
290
|
+
}
|
|
291
|
+
var bit = 1 << ((shift === 0 ? keyHash : keyHash >>> shift) & MASK);
|
|
292
|
+
var bitmap = this.bitmap;
|
|
293
|
+
return (bitmap & bit) === 0
|
|
294
|
+
? notSetValue
|
|
295
|
+
: this.nodes[popCount(bitmap & (bit - 1))].get(
|
|
296
|
+
shift + SHIFT,
|
|
297
|
+
keyHash,
|
|
298
|
+
key,
|
|
299
|
+
notSetValue
|
|
300
|
+
);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
BitmapIndexedNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
|
|
304
|
+
if (keyHash === undefined) {
|
|
305
|
+
keyHash = hash(key);
|
|
306
|
+
}
|
|
307
|
+
var keyHashFrag = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
|
|
308
|
+
var bit = 1 << keyHashFrag;
|
|
309
|
+
var bitmap = this.bitmap;
|
|
310
|
+
var exists = (bitmap & bit) !== 0;
|
|
311
|
+
|
|
312
|
+
if (!exists && value === NOT_SET) {
|
|
313
|
+
return this;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
var idx = popCount(bitmap & (bit - 1));
|
|
317
|
+
var nodes = this.nodes;
|
|
318
|
+
var node = exists ? nodes[idx] : undefined;
|
|
319
|
+
var newNode = updateNode(
|
|
320
|
+
node,
|
|
321
|
+
ownerID,
|
|
322
|
+
shift + SHIFT,
|
|
323
|
+
keyHash,
|
|
324
|
+
key,
|
|
325
|
+
value,
|
|
326
|
+
didChangeSize,
|
|
327
|
+
didAlter
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
if (newNode === node) {
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (!exists && newNode && nodes.length >= MAX_BITMAP_INDEXED_SIZE) {
|
|
335
|
+
return expandNodes(ownerID, nodes, bitmap, keyHashFrag, newNode);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (
|
|
339
|
+
exists &&
|
|
340
|
+
!newNode &&
|
|
341
|
+
nodes.length === 2 &&
|
|
342
|
+
isLeafNode(nodes[idx ^ 1])
|
|
343
|
+
) {
|
|
344
|
+
return nodes[idx ^ 1];
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (exists && newNode && nodes.length === 1 && isLeafNode(newNode)) {
|
|
348
|
+
return newNode;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
var isEditable = ownerID && ownerID === this.ownerID;
|
|
352
|
+
var newBitmap = exists ? (newNode ? bitmap : bitmap ^ bit) : bitmap | bit;
|
|
353
|
+
var newNodes = exists
|
|
354
|
+
? newNode
|
|
355
|
+
? setAt(nodes, idx, newNode, isEditable)
|
|
356
|
+
: spliceOut(nodes, idx, isEditable)
|
|
357
|
+
: spliceIn(nodes, idx, newNode, isEditable);
|
|
358
|
+
|
|
359
|
+
if (isEditable) {
|
|
360
|
+
this.bitmap = newBitmap;
|
|
361
|
+
this.nodes = newNodes;
|
|
362
|
+
return this;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return new BitmapIndexedNode(ownerID, newBitmap, newNodes);
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
var HashArrayMapNode = function HashArrayMapNode(ownerID, count, nodes) {
|
|
369
|
+
this.ownerID = ownerID;
|
|
370
|
+
this.count = count;
|
|
371
|
+
this.nodes = nodes;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
HashArrayMapNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
|
|
375
|
+
if (keyHash === undefined) {
|
|
376
|
+
keyHash = hash(key);
|
|
377
|
+
}
|
|
378
|
+
var idx = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
|
|
379
|
+
var node = this.nodes[idx];
|
|
380
|
+
return node
|
|
381
|
+
? node.get(shift + SHIFT, keyHash, key, notSetValue)
|
|
382
|
+
: notSetValue;
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
HashArrayMapNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
|
|
386
|
+
if (keyHash === undefined) {
|
|
387
|
+
keyHash = hash(key);
|
|
388
|
+
}
|
|
389
|
+
var idx = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
|
|
390
|
+
var removed = value === NOT_SET;
|
|
391
|
+
var nodes = this.nodes;
|
|
392
|
+
var node = nodes[idx];
|
|
393
|
+
|
|
394
|
+
if (removed && !node) {
|
|
395
|
+
return this;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
var newNode = updateNode(
|
|
399
|
+
node,
|
|
400
|
+
ownerID,
|
|
401
|
+
shift + SHIFT,
|
|
402
|
+
keyHash,
|
|
403
|
+
key,
|
|
404
|
+
value,
|
|
405
|
+
didChangeSize,
|
|
406
|
+
didAlter
|
|
407
|
+
);
|
|
408
|
+
if (newNode === node) {
|
|
409
|
+
return this;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
var newCount = this.count;
|
|
413
|
+
if (!node) {
|
|
414
|
+
newCount++;
|
|
415
|
+
} else if (!newNode) {
|
|
416
|
+
newCount--;
|
|
417
|
+
if (newCount < MIN_HASH_ARRAY_MAP_SIZE) {
|
|
418
|
+
return packNodes(ownerID, nodes, newCount, idx);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
var isEditable = ownerID && ownerID === this.ownerID;
|
|
423
|
+
var newNodes = setAt(nodes, idx, newNode, isEditable);
|
|
424
|
+
|
|
425
|
+
if (isEditable) {
|
|
426
|
+
this.count = newCount;
|
|
427
|
+
this.nodes = newNodes;
|
|
428
|
+
return this;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return new HashArrayMapNode(ownerID, newCount, newNodes);
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
var HashCollisionNode = function HashCollisionNode(ownerID, keyHash, entries) {
|
|
435
|
+
this.ownerID = ownerID;
|
|
436
|
+
this.keyHash = keyHash;
|
|
437
|
+
this.entries = entries;
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
HashCollisionNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
|
|
441
|
+
var entries = this.entries;
|
|
442
|
+
for (var ii = 0, len = entries.length; ii < len; ii++) {
|
|
443
|
+
if (is(key, entries[ii][0])) {
|
|
444
|
+
return entries[ii][1];
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return notSetValue;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
HashCollisionNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
|
|
451
|
+
if (keyHash === undefined) {
|
|
452
|
+
keyHash = hash(key);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
var removed = value === NOT_SET;
|
|
456
|
+
|
|
457
|
+
if (keyHash !== this.keyHash) {
|
|
458
|
+
if (removed) {
|
|
459
|
+
return this;
|
|
460
|
+
}
|
|
461
|
+
SetRef(didAlter);
|
|
462
|
+
SetRef(didChangeSize);
|
|
463
|
+
return mergeIntoNode(this, ownerID, shift, keyHash, [key, value]);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
var entries = this.entries;
|
|
467
|
+
var idx = 0;
|
|
468
|
+
var len = entries.length;
|
|
469
|
+
for (; idx < len; idx++) {
|
|
470
|
+
if (is(key, entries[idx][0])) {
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
var exists = idx < len;
|
|
475
|
+
|
|
476
|
+
if (exists ? entries[idx][1] === value : removed) {
|
|
477
|
+
return this;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
SetRef(didAlter);
|
|
481
|
+
(removed || !exists) && SetRef(didChangeSize);
|
|
482
|
+
|
|
483
|
+
if (removed && len === 2) {
|
|
484
|
+
return new ValueNode(ownerID, this.keyHash, entries[idx ^ 1]);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
var isEditable = ownerID && ownerID === this.ownerID;
|
|
488
|
+
var newEntries = isEditable ? entries : arrCopy(entries);
|
|
489
|
+
|
|
490
|
+
if (exists) {
|
|
491
|
+
if (removed) {
|
|
492
|
+
idx === len - 1
|
|
493
|
+
? newEntries.pop()
|
|
494
|
+
: (newEntries[idx] = newEntries.pop());
|
|
495
|
+
} else {
|
|
496
|
+
newEntries[idx] = [key, value];
|
|
497
|
+
}
|
|
498
|
+
} else {
|
|
499
|
+
newEntries.push([key, value]);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (isEditable) {
|
|
503
|
+
this.entries = newEntries;
|
|
504
|
+
return this;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return new HashCollisionNode(ownerID, this.keyHash, newEntries);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
var ValueNode = function ValueNode(ownerID, keyHash, entry) {
|
|
511
|
+
this.ownerID = ownerID;
|
|
512
|
+
this.keyHash = keyHash;
|
|
513
|
+
this.entry = entry;
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
ValueNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
|
|
517
|
+
return is(key, this.entry[0]) ? this.entry[1] : notSetValue;
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
ValueNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
|
|
521
|
+
var removed = value === NOT_SET;
|
|
522
|
+
var keyMatch = is(key, this.entry[0]);
|
|
523
|
+
if (keyMatch ? value === this.entry[1] : removed) {
|
|
524
|
+
return this;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
SetRef(didAlter);
|
|
528
|
+
|
|
529
|
+
if (removed) {
|
|
530
|
+
SetRef(didChangeSize);
|
|
531
|
+
return; // undefined
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (keyMatch) {
|
|
535
|
+
if (ownerID && ownerID === this.ownerID) {
|
|
536
|
+
this.entry[1] = value;
|
|
537
|
+
return this;
|
|
538
|
+
}
|
|
539
|
+
return new ValueNode(ownerID, this.keyHash, [key, value]);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
SetRef(didChangeSize);
|
|
543
|
+
return mergeIntoNode(this, ownerID, shift, hash(key), [key, value]);
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// #pragma Iterators
|
|
547
|
+
|
|
548
|
+
ArrayMapNode.prototype.iterate = HashCollisionNode.prototype.iterate =
|
|
549
|
+
function (fn, reverse) {
|
|
550
|
+
var entries = this.entries;
|
|
551
|
+
for (var ii = 0, maxIndex = entries.length - 1; ii <= maxIndex; ii++) {
|
|
552
|
+
if (fn(entries[reverse ? maxIndex - ii : ii]) === false) {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
BitmapIndexedNode.prototype.iterate = HashArrayMapNode.prototype.iterate =
|
|
559
|
+
function (fn, reverse) {
|
|
560
|
+
var nodes = this.nodes;
|
|
561
|
+
for (var ii = 0, maxIndex = nodes.length - 1; ii <= maxIndex; ii++) {
|
|
562
|
+
var node = nodes[reverse ? maxIndex - ii : ii];
|
|
563
|
+
if (node && node.iterate(fn, reverse) === false) {
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// eslint-disable-next-line no-unused-vars
|
|
570
|
+
ValueNode.prototype.iterate = function (fn, reverse) {
|
|
571
|
+
return fn(this.entry);
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
var MapIterator = /*@__PURE__*/(function (Iterator) {
|
|
575
|
+
function MapIterator(map, type, reverse) {
|
|
576
|
+
this._type = type;
|
|
577
|
+
this._reverse = reverse;
|
|
578
|
+
this._stack = map._root && mapIteratorFrame(map._root);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if ( Iterator ) MapIterator.__proto__ = Iterator;
|
|
582
|
+
MapIterator.prototype = Object.create( Iterator && Iterator.prototype );
|
|
583
|
+
MapIterator.prototype.constructor = MapIterator;
|
|
584
|
+
|
|
585
|
+
MapIterator.prototype.next = function next () {
|
|
586
|
+
var type = this._type;
|
|
587
|
+
var stack = this._stack;
|
|
588
|
+
while (stack) {
|
|
589
|
+
var node = stack.node;
|
|
590
|
+
var index = stack.index++;
|
|
591
|
+
var maxIndex = (void 0);
|
|
592
|
+
if (node.entry) {
|
|
593
|
+
if (index === 0) {
|
|
594
|
+
return mapIteratorValue(type, node.entry);
|
|
595
|
+
}
|
|
596
|
+
} else if (node.entries) {
|
|
597
|
+
maxIndex = node.entries.length - 1;
|
|
598
|
+
if (index <= maxIndex) {
|
|
599
|
+
return mapIteratorValue(
|
|
600
|
+
type,
|
|
601
|
+
node.entries[this._reverse ? maxIndex - index : index]
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
} else {
|
|
605
|
+
maxIndex = node.nodes.length - 1;
|
|
606
|
+
if (index <= maxIndex) {
|
|
607
|
+
var subNode = node.nodes[this._reverse ? maxIndex - index : index];
|
|
608
|
+
if (subNode) {
|
|
609
|
+
if (subNode.entry) {
|
|
610
|
+
return mapIteratorValue(type, subNode.entry);
|
|
611
|
+
}
|
|
612
|
+
stack = this._stack = mapIteratorFrame(subNode, stack);
|
|
613
|
+
}
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
stack = this._stack = this._stack.__prev;
|
|
618
|
+
}
|
|
619
|
+
return iteratorDone();
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
return MapIterator;
|
|
623
|
+
}(Iterator));
|
|
624
|
+
|
|
625
|
+
function mapIteratorValue(type, entry) {
|
|
626
|
+
return iteratorValue(type, entry[0], entry[1]);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function mapIteratorFrame(node, prev) {
|
|
630
|
+
return {
|
|
631
|
+
node: node,
|
|
632
|
+
index: 0,
|
|
633
|
+
__prev: prev,
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function makeMap(size, root, ownerID, hash) {
|
|
638
|
+
var map = Object.create(MapPrototype);
|
|
639
|
+
map.size = size;
|
|
640
|
+
map._root = root;
|
|
641
|
+
map.__ownerID = ownerID;
|
|
642
|
+
map.__hash = hash;
|
|
643
|
+
map.__altered = false;
|
|
644
|
+
return map;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
var EMPTY_MAP;
|
|
648
|
+
function emptyMap() {
|
|
649
|
+
return EMPTY_MAP || (EMPTY_MAP = makeMap(0));
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function updateMap(map, k, v) {
|
|
653
|
+
var newRoot;
|
|
654
|
+
var newSize;
|
|
655
|
+
if (!map._root) {
|
|
656
|
+
if (v === NOT_SET) {
|
|
657
|
+
return map;
|
|
658
|
+
}
|
|
659
|
+
newSize = 1;
|
|
660
|
+
newRoot = new ArrayMapNode(map.__ownerID, [[k, v]]);
|
|
661
|
+
} else {
|
|
662
|
+
var didChangeSize = MakeRef();
|
|
663
|
+
var didAlter = MakeRef();
|
|
664
|
+
newRoot = updateNode(
|
|
665
|
+
map._root,
|
|
666
|
+
map.__ownerID,
|
|
667
|
+
0,
|
|
668
|
+
undefined,
|
|
669
|
+
k,
|
|
670
|
+
v,
|
|
671
|
+
didChangeSize,
|
|
672
|
+
didAlter
|
|
673
|
+
);
|
|
674
|
+
if (!didAlter.value) {
|
|
675
|
+
return map;
|
|
676
|
+
}
|
|
677
|
+
newSize = map.size + (didChangeSize.value ? (v === NOT_SET ? -1 : 1) : 0);
|
|
678
|
+
}
|
|
679
|
+
if (map.__ownerID) {
|
|
680
|
+
map.size = newSize;
|
|
681
|
+
map._root = newRoot;
|
|
682
|
+
map.__hash = undefined;
|
|
683
|
+
map.__altered = true;
|
|
684
|
+
return map;
|
|
685
|
+
}
|
|
686
|
+
return newRoot ? makeMap(newSize, newRoot) : emptyMap();
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function updateNode(
|
|
690
|
+
node,
|
|
691
|
+
ownerID,
|
|
692
|
+
shift,
|
|
693
|
+
keyHash,
|
|
694
|
+
key,
|
|
695
|
+
value,
|
|
696
|
+
didChangeSize,
|
|
697
|
+
didAlter
|
|
698
|
+
) {
|
|
699
|
+
if (!node) {
|
|
700
|
+
if (value === NOT_SET) {
|
|
701
|
+
return node;
|
|
702
|
+
}
|
|
703
|
+
SetRef(didAlter);
|
|
704
|
+
SetRef(didChangeSize);
|
|
705
|
+
return new ValueNode(ownerID, keyHash, [key, value]);
|
|
706
|
+
}
|
|
707
|
+
return node.update(
|
|
708
|
+
ownerID,
|
|
709
|
+
shift,
|
|
710
|
+
keyHash,
|
|
711
|
+
key,
|
|
712
|
+
value,
|
|
713
|
+
didChangeSize,
|
|
714
|
+
didAlter
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function isLeafNode(node) {
|
|
719
|
+
return (
|
|
720
|
+
node.constructor === ValueNode || node.constructor === HashCollisionNode
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function mergeIntoNode(node, ownerID, shift, keyHash, entry) {
|
|
725
|
+
if (node.keyHash === keyHash) {
|
|
726
|
+
return new HashCollisionNode(ownerID, keyHash, [node.entry, entry]);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
var idx1 = (shift === 0 ? node.keyHash : node.keyHash >>> shift) & MASK;
|
|
730
|
+
var idx2 = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
|
|
731
|
+
|
|
732
|
+
var newNode;
|
|
733
|
+
var nodes =
|
|
734
|
+
idx1 === idx2
|
|
735
|
+
? [mergeIntoNode(node, ownerID, shift + SHIFT, keyHash, entry)]
|
|
736
|
+
: ((newNode = new ValueNode(ownerID, keyHash, entry)),
|
|
737
|
+
idx1 < idx2 ? [node, newNode] : [newNode, node]);
|
|
738
|
+
|
|
739
|
+
return new BitmapIndexedNode(ownerID, (1 << idx1) | (1 << idx2), nodes);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function createNodes(ownerID, entries, key, value) {
|
|
743
|
+
if (!ownerID) {
|
|
744
|
+
ownerID = new OwnerID();
|
|
745
|
+
}
|
|
746
|
+
var node = new ValueNode(ownerID, hash(key), [key, value]);
|
|
747
|
+
for (var ii = 0; ii < entries.length; ii++) {
|
|
748
|
+
var entry = entries[ii];
|
|
749
|
+
node = node.update(ownerID, 0, undefined, entry[0], entry[1]);
|
|
750
|
+
}
|
|
751
|
+
return node;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function packNodes(ownerID, nodes, count, excluding) {
|
|
755
|
+
var bitmap = 0;
|
|
756
|
+
var packedII = 0;
|
|
757
|
+
var packedNodes = new Array(count);
|
|
758
|
+
for (var ii = 0, bit = 1, len = nodes.length; ii < len; ii++, bit <<= 1) {
|
|
759
|
+
var node = nodes[ii];
|
|
760
|
+
if (node !== undefined && ii !== excluding) {
|
|
761
|
+
bitmap |= bit;
|
|
762
|
+
packedNodes[packedII++] = node;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
return new BitmapIndexedNode(ownerID, bitmap, packedNodes);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
function expandNodes(ownerID, nodes, bitmap, including, node) {
|
|
769
|
+
var count = 0;
|
|
770
|
+
var expandedNodes = new Array(SIZE);
|
|
771
|
+
for (var ii = 0; bitmap !== 0; ii++, bitmap >>>= 1) {
|
|
772
|
+
expandedNodes[ii] = bitmap & 1 ? nodes[count++] : undefined;
|
|
773
|
+
}
|
|
774
|
+
expandedNodes[including] = node;
|
|
775
|
+
return new HashArrayMapNode(ownerID, count + 1, expandedNodes);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
function popCount(x) {
|
|
779
|
+
x -= (x >> 1) & 0x55555555;
|
|
780
|
+
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
781
|
+
x = (x + (x >> 4)) & 0x0f0f0f0f;
|
|
782
|
+
x += x >> 8;
|
|
783
|
+
x += x >> 16;
|
|
784
|
+
return x & 0x7f;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
function setAt(array, idx, val, canEdit) {
|
|
788
|
+
var newArray = canEdit ? array : arrCopy(array);
|
|
789
|
+
newArray[idx] = val;
|
|
790
|
+
return newArray;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
function spliceIn(array, idx, val, canEdit) {
|
|
794
|
+
var newLen = array.length + 1;
|
|
795
|
+
if (canEdit && idx + 1 === newLen) {
|
|
796
|
+
array[idx] = val;
|
|
797
|
+
return array;
|
|
798
|
+
}
|
|
799
|
+
var newArray = new Array(newLen);
|
|
800
|
+
var after = 0;
|
|
801
|
+
for (var ii = 0; ii < newLen; ii++) {
|
|
802
|
+
if (ii === idx) {
|
|
803
|
+
newArray[ii] = val;
|
|
804
|
+
after = -1;
|
|
805
|
+
} else {
|
|
806
|
+
newArray[ii] = array[ii + after];
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
return newArray;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function spliceOut(array, idx, canEdit) {
|
|
813
|
+
var newLen = array.length - 1;
|
|
814
|
+
if (canEdit && idx === newLen) {
|
|
815
|
+
array.pop();
|
|
816
|
+
return array;
|
|
817
|
+
}
|
|
818
|
+
var newArray = new Array(newLen);
|
|
819
|
+
var after = 0;
|
|
820
|
+
for (var ii = 0; ii < newLen; ii++) {
|
|
821
|
+
if (ii === idx) {
|
|
822
|
+
after = 1;
|
|
823
|
+
}
|
|
824
|
+
newArray[ii] = array[ii + after];
|
|
825
|
+
}
|
|
826
|
+
return newArray;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
var MAX_ARRAY_MAP_SIZE = SIZE / 4;
|
|
830
|
+
var MAX_BITMAP_INDEXED_SIZE = SIZE / 2;
|
|
831
|
+
var MIN_HASH_ARRAY_MAP_SIZE = SIZE / 4;
|
|
832
|
+
|
|
833
|
+
export { Map, emptyMap };
|