@x-oasis/integer-buffer-set 0.1.19 → 0.1.20
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 +23 -1
- package/dist/index.d.ts +59 -23
- package/dist/integer-buffer-set.cjs.development.js +442 -79
- package/dist/integer-buffer-set.cjs.development.js.map +1 -1
- package/dist/integer-buffer-set.cjs.production.min.js +1 -1
- package/dist/integer-buffer-set.cjs.production.min.js.map +1 -1
- package/dist/integer-buffer-set.esm.js +442 -79
- package/dist/integer-buffer-set.esm.js.map +1 -1
- package/dist/types.d.ts +24 -0
- package/package.json +6 -2
- package/src/index.ts +665 -131
- package/src/types.ts +36 -0
|
@@ -5,6 +5,9 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
6
|
|
|
7
7
|
var Heap = _interopDefault(require('@x-oasis/heap'));
|
|
8
|
+
var isClamped = _interopDefault(require('@x-oasis/is-clamped'));
|
|
9
|
+
var invariant = _interopDefault(require('@x-oasis/invariant'));
|
|
10
|
+
var returnHook = _interopDefault(require('@x-oasis/return-hook'));
|
|
8
11
|
|
|
9
12
|
function _defineProperties(target, props) {
|
|
10
13
|
for (var i = 0; i < props.length; i++) {
|
|
@@ -23,6 +26,37 @@ function _createClass(Constructor, protoProps, staticProps) {
|
|
|
23
26
|
});
|
|
24
27
|
return Constructor;
|
|
25
28
|
}
|
|
29
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
30
|
+
if (!o) return;
|
|
31
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
32
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
33
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
34
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
35
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
36
|
+
}
|
|
37
|
+
function _arrayLikeToArray(arr, len) {
|
|
38
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
39
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
40
|
+
return arr2;
|
|
41
|
+
}
|
|
42
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
43
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
44
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
45
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
46
|
+
if (it) o = it;
|
|
47
|
+
var i = 0;
|
|
48
|
+
return function () {
|
|
49
|
+
if (i >= o.length) return {
|
|
50
|
+
done: true
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
done: false,
|
|
54
|
+
value: o[i++]
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
59
|
+
}
|
|
26
60
|
function _toPrimitive(input, hint) {
|
|
27
61
|
if (typeof input !== "object" || input === null) return input;
|
|
28
62
|
var prim = input[Symbol.toPrimitive];
|
|
@@ -38,45 +72,107 @@ function _toPropertyKey(arg) {
|
|
|
38
72
|
return typeof key === "symbol" ? key : String(key);
|
|
39
73
|
}
|
|
40
74
|
|
|
41
|
-
var
|
|
42
|
-
|
|
43
|
-
bufferSetRange = options.bufferSetRange;
|
|
44
|
-
var lowValue = safeRange.lowValue,
|
|
45
|
-
highValue = safeRange.highValue;
|
|
46
|
-
var maxValue = bufferSetRange.maxValue,
|
|
47
|
-
minValue = bufferSetRange.minValue;
|
|
48
|
-
return lowValue - minValue > maxValue - highValue;
|
|
75
|
+
var defaultMetaExtractor = function defaultMetaExtractor(value) {
|
|
76
|
+
return value;
|
|
49
77
|
};
|
|
78
|
+
var defaultBufferSize = 10;
|
|
50
79
|
var IntegerBufferSet = /*#__PURE__*/function () {
|
|
51
|
-
function IntegerBufferSet() {
|
|
52
|
-
|
|
80
|
+
function IntegerBufferSet(props) {
|
|
81
|
+
if (props === void 0) {
|
|
82
|
+
props = {};
|
|
83
|
+
}
|
|
84
|
+
var _props = props,
|
|
85
|
+
_props$name = _props.name,
|
|
86
|
+
name = _props$name === void 0 ? 'default_buffer' : _props$name,
|
|
87
|
+
indexExtractor = _props.indexExtractor,
|
|
88
|
+
_props$bufferSize = _props.bufferSize,
|
|
89
|
+
bufferSize = _props$bufferSize === void 0 ? defaultBufferSize : _props$bufferSize,
|
|
90
|
+
_props$metaExtractor = _props.metaExtractor,
|
|
91
|
+
metaExtractor = _props$metaExtractor === void 0 ? defaultMetaExtractor : _props$metaExtractor;
|
|
92
|
+
this._metaExtractor = metaExtractor;
|
|
93
|
+
this._indexExtractor = indexExtractor;
|
|
94
|
+
this._name = name;
|
|
95
|
+
this._indexToMetaMap = new Map();
|
|
96
|
+
this._metaToPositionMap = new Map();
|
|
97
|
+
this._positionToMetaList = [];
|
|
98
|
+
this._metaToIndexMap = new Map();
|
|
99
|
+
this._onTheFlyIndices = [];
|
|
53
100
|
this._size = 0;
|
|
101
|
+
this._bufferSize = bufferSize;
|
|
54
102
|
this._smallValues = new Heap([], this._smallerComparator);
|
|
55
103
|
this._largeValues = new Heap([], this._greaterComparator);
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
104
|
+
this.getNewPositionForIndex = this.getNewPositionForIndex.bind(this);
|
|
105
|
+
this.getIndexPosition = this.getIndexPosition.bind(this);
|
|
58
106
|
this.getSize = this.getSize.bind(this);
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
107
|
+
this.replacePositionInFliedIndices = this.replacePositionInFliedIndices.bind(this);
|
|
108
|
+
this.replaceFurthestIndexPosition = this.replaceFurthestIndexPosition.bind(this);
|
|
109
|
+
this._isOnTheFlyFullReturnHook = returnHook(this.setIsOnTheFlyFull.bind(this));
|
|
110
|
+
this._loopMS = Date.now();
|
|
111
|
+
this._lastUpdatedMS = this._loopMS;
|
|
61
112
|
}
|
|
62
113
|
var _proto = IntegerBufferSet.prototype;
|
|
63
114
|
_proto.getSize = function getSize() {
|
|
64
115
|
return this._size;
|
|
65
116
|
};
|
|
66
|
-
_proto.
|
|
67
|
-
if (
|
|
68
|
-
|
|
117
|
+
_proto.setIsOnTheFlyFull = function setIsOnTheFlyFull(val) {
|
|
118
|
+
if (val != null) {
|
|
119
|
+
var data = this._onTheFlyIndices.filter(function (v) {
|
|
120
|
+
return v;
|
|
121
|
+
});
|
|
122
|
+
this._isOnTheFlyFull = data.length === this._bufferSize;
|
|
69
123
|
}
|
|
70
|
-
return this._valueToPositionMap[value];
|
|
71
124
|
};
|
|
72
|
-
_proto.
|
|
73
|
-
|
|
74
|
-
|
|
125
|
+
_proto.getOnTheFlyUncriticalPosition = function getOnTheFlyUncriticalPosition(safeRange) {
|
|
126
|
+
var startIndex = safeRange.startIndex,
|
|
127
|
+
endIndex = safeRange.endIndex;
|
|
128
|
+
for (var idx = 0; idx < this._onTheFlyIndices.length; idx++) {
|
|
129
|
+
var meta = this._onTheFlyIndices[idx];
|
|
130
|
+
var metaIndex = this._metaToIndexMap.get(meta);
|
|
131
|
+
if (!isClamped(startIndex, metaIndex, endIndex)) {
|
|
132
|
+
return idx;
|
|
133
|
+
}
|
|
75
134
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
135
|
+
return null;
|
|
136
|
+
};
|
|
137
|
+
_proto.initialize = function initialize() {
|
|
138
|
+
return {
|
|
139
|
+
smallValues: new Heap([], this._smallerComparator),
|
|
140
|
+
largeValues: new Heap([], this._greaterComparator),
|
|
141
|
+
valueToPositionObject: {}
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
_proto.getIndexMeta = function getIndexMeta(index) {
|
|
145
|
+
return this._metaExtractor(index);
|
|
146
|
+
};
|
|
147
|
+
_proto.getMetaIndex = function getMetaIndex(meta) {
|
|
148
|
+
if (this._indexExtractor) return this._indexExtractor(meta);
|
|
149
|
+
return this._metaToIndexMap.get(meta);
|
|
150
|
+
};
|
|
151
|
+
_proto.setMetaIndex = function setMetaIndex(meta, index) {
|
|
152
|
+
if (!this._indexExtractor) {
|
|
153
|
+
return this._metaToIndexMap.set(meta, index);
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
_proto.deleteMetaIndex = function deleteMetaIndex(meta) {
|
|
158
|
+
return this._metaToIndexMap["delete"](meta);
|
|
159
|
+
};
|
|
160
|
+
_proto.replaceMetaToIndexMap = function replaceMetaToIndexMap(newMetaToIndexMap) {
|
|
161
|
+
if (!this._indexExtractor) {
|
|
162
|
+
return this._metaToIndexMap = newMetaToIndexMap;
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
};
|
|
166
|
+
_proto.getIndexPosition = function getIndexPosition(index) {
|
|
167
|
+
return this.getMetaIndex(this.getIndexMeta(index));
|
|
168
|
+
};
|
|
169
|
+
_proto.getNewPositionForIndex = function getNewPositionForIndex(index) {
|
|
170
|
+
var meta = this.getIndexMeta(index);
|
|
171
|
+
!(this._metaToPositionMap.get(meta) === undefined) ? invariant(false, "Shouldn't try to find new position for value already stored in BufferSet") : void 0;
|
|
172
|
+
var newPosition = this._positionToMetaList.length;
|
|
173
|
+
this._pushToHeaps(newPosition, index);
|
|
174
|
+
this._setMetaIndex(meta, index);
|
|
175
|
+
this._setMetaPosition(meta, newPosition);
|
|
80
176
|
return newPosition;
|
|
81
177
|
};
|
|
82
178
|
_proto.getMinValue = function getMinValue() {
|
|
@@ -87,67 +183,273 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
87
183
|
var _this$_largeValues$pe;
|
|
88
184
|
return (_this$_largeValues$pe = this._largeValues.peek()) == null ? void 0 : _this$_largeValues$pe.value;
|
|
89
185
|
};
|
|
90
|
-
_proto.
|
|
91
|
-
|
|
186
|
+
_proto.setValuePosition = function setValuePosition(value, position) {};
|
|
187
|
+
_proto.findPositionMeta = function findPositionMeta(position) {
|
|
188
|
+
for (var _iterator = _createForOfIteratorHelperLoose(this._metaToPositionMap), _step; !(_step = _iterator()).done;) {
|
|
189
|
+
var _step$value = _step.value,
|
|
190
|
+
meta = _step$value[0],
|
|
191
|
+
pos = _step$value[1];
|
|
192
|
+
if (pos === position) return meta;
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
};
|
|
196
|
+
_proto.rebuildHeapsWithMeta = function rebuildHeapsWithMeta(metaToPositionMap) {
|
|
197
|
+
var _this$initialize = this.initialize(),
|
|
198
|
+
smallValues = _this$initialize.smallValues,
|
|
199
|
+
largeValues = _this$initialize.largeValues;
|
|
200
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(metaToPositionMap), _step2; !(_step2 = _iterator2()).done;) {
|
|
201
|
+
var _step2$value = _step2.value,
|
|
202
|
+
meta = _step2$value[0],
|
|
203
|
+
position = _step2$value[1];
|
|
204
|
+
var index = this.getMetaIndex(meta);
|
|
205
|
+
var token = {
|
|
206
|
+
index: index,
|
|
207
|
+
position: position
|
|
208
|
+
};
|
|
209
|
+
smallValues.push(token);
|
|
210
|
+
largeValues.push(token);
|
|
211
|
+
}
|
|
212
|
+
this._smallValues = smallValues;
|
|
213
|
+
this._largeValues = largeValues;
|
|
214
|
+
};
|
|
215
|
+
_proto.setPositionIndex = function setPositionIndex(position, index) {
|
|
216
|
+
var meta = this._metaExtractor(index);
|
|
217
|
+
var originalPosition = this._metaToPositionMap.get(meta);
|
|
92
218
|
if (originalPosition !== undefined) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
});
|
|
96
|
-
if (index === -1) this._vacantPositions.push(originalPosition);
|
|
97
|
-
delete this._valueToPositionMap[value];
|
|
98
|
-
this._valueToPositionMap[value] = position;
|
|
219
|
+
if (originalPosition === position) return true;
|
|
220
|
+
this.deleteMetaIndex(meta);
|
|
99
221
|
}
|
|
222
|
+
var metaToReplace = this.findPositionMeta(position);
|
|
223
|
+
if (metaToReplace) this._metaToPositionMap["delete"](metaToReplace);
|
|
224
|
+
this._metaToPositionMap.set(meta, position);
|
|
225
|
+
this.rebuildHeapsWithMeta(this._metaToPositionMap);
|
|
226
|
+
return true;
|
|
227
|
+
};
|
|
228
|
+
_proto.getMetaPosition = function getMetaPosition(meta) {
|
|
229
|
+
return this._metaToPositionMap.get(meta);
|
|
100
230
|
};
|
|
101
|
-
_proto.
|
|
102
|
-
|
|
103
|
-
|
|
231
|
+
_proto.replacePositionInFliedIndices = function replacePositionInFliedIndices(newIndex, safeRange) {
|
|
232
|
+
var startIndex = safeRange.startIndex,
|
|
233
|
+
endIndex = safeRange.endIndex;
|
|
234
|
+
if (this._isOnTheFlyFull) {
|
|
235
|
+
if (!isClamped(startIndex, newIndex, endIndex)) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
var pos = this.getOnTheFlyUncriticalPosition(safeRange);
|
|
239
|
+
if (pos != null) return pos;
|
|
104
240
|
}
|
|
105
|
-
|
|
106
|
-
|
|
241
|
+
return null;
|
|
242
|
+
};
|
|
243
|
+
_proto.getFliedPosition = function getFliedPosition(newIndex, safeRange) {
|
|
244
|
+
var pos = this.replacePositionInFliedIndices(newIndex, safeRange);
|
|
245
|
+
if (pos != null) {
|
|
246
|
+
var meta = this.getIndexMeta(newIndex);
|
|
247
|
+
this._onTheFlyIndices[pos] = meta;
|
|
248
|
+
this._setMetaIndex(meta, newIndex);
|
|
249
|
+
return this._isOnTheFlyFullReturnHook(pos);
|
|
107
250
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
251
|
+
return null;
|
|
252
|
+
};
|
|
253
|
+
_proto.getPosition = function getPosition(newIndex, safeRange) {
|
|
254
|
+
this.prepare();
|
|
255
|
+
var meta = this.getIndexMeta(newIndex);
|
|
256
|
+
var prevMetaPosition = this._metaToPositionMap.get(meta);
|
|
257
|
+
if (prevMetaPosition !== undefined) {
|
|
258
|
+
var onTheFlyPositionMeta = this._onTheFlyIndices[prevMetaPosition];
|
|
259
|
+
if (onTheFlyPositionMeta) {
|
|
260
|
+
if (onTheFlyPositionMeta === meta) {
|
|
261
|
+
return prevMetaPosition;
|
|
262
|
+
}
|
|
263
|
+
var _positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
|
|
264
|
+
if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
|
|
265
|
+
while (this._onTheFlyIndices[_positionToReplace]) {
|
|
266
|
+
_positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
|
|
267
|
+
}
|
|
268
|
+
if (_positionToReplace != null) {
|
|
269
|
+
this._setMetaIndex(meta, newIndex);
|
|
270
|
+
this._onTheFlyIndices[_positionToReplace] = onTheFlyPositionMeta;
|
|
271
|
+
return this._isOnTheFlyFullReturnHook(_positionToReplace);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
this._onTheFlyIndices[prevMetaPosition] = meta;
|
|
275
|
+
return this._isOnTheFlyFullReturnHook(prevMetaPosition);
|
|
111
276
|
}
|
|
112
|
-
if (this.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
277
|
+
if (!this.isBufferFull) return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
|
|
278
|
+
if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
|
|
279
|
+
var positionToReplace;
|
|
280
|
+
var prevIndexMeta = this._indexToMetaMap.get(newIndex);
|
|
281
|
+
if (!prevIndexMeta) {
|
|
282
|
+
this._cleanHeaps();
|
|
283
|
+
positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
|
|
284
|
+
} else {
|
|
285
|
+
positionToReplace = this._metaToPositionMap.get(prevIndexMeta);
|
|
286
|
+
}
|
|
287
|
+
this._onTheFlyIndices[positionToReplace] = meta;
|
|
288
|
+
this._setMetaIndex(meta, newIndex);
|
|
289
|
+
this._setMetaPosition(meta, positionToReplace);
|
|
290
|
+
return this._isOnTheFlyFullReturnHook(positionToReplace);
|
|
291
|
+
};
|
|
292
|
+
_proto.replaceFurthestIndexPosition = function replaceFurthestIndexPosition(newIndex, safeRange) {
|
|
293
|
+
if (!this.isBufferFull) {
|
|
294
|
+
return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
|
|
295
|
+
}
|
|
296
|
+
return this._replaceFurthestIndexPosition(newIndex, safeRange);
|
|
297
|
+
};
|
|
298
|
+
_proto._replaceFurthestIndexPosition = function _replaceFurthestIndexPosition(newIndex, safeRange) {
|
|
299
|
+
if (this._largeValues.empty() || this._smallValues.empty()) {
|
|
300
|
+
return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
|
|
117
301
|
}
|
|
118
302
|
var minValue = this._smallValues.peek().value;
|
|
119
303
|
var maxValue = this._largeValues.peek().value;
|
|
120
|
-
|
|
121
|
-
|
|
304
|
+
var indexToReplace;
|
|
305
|
+
if (!safeRange) {
|
|
306
|
+
if (Math.abs(newIndex - minValue) > Math.abs(newIndex - maxValue)) {
|
|
307
|
+
indexToReplace = minValue;
|
|
308
|
+
this._smallValues.pop();
|
|
309
|
+
} else {
|
|
310
|
+
indexToReplace = maxValue;
|
|
311
|
+
this._largeValues.pop();
|
|
312
|
+
}
|
|
313
|
+
var _replacedMeta = this._indexToMetaMap.get(indexToReplace);
|
|
314
|
+
var _position = this._metaToPositionMap.get(_replacedMeta);
|
|
315
|
+
return _position;
|
|
122
316
|
}
|
|
123
|
-
var
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
valueToReplace = minValue;
|
|
317
|
+
var lowValue = safeRange.startIndex,
|
|
318
|
+
highValue = safeRange.endIndex;
|
|
319
|
+
if (isClamped(lowValue, minValue, highValue) && isClamped(lowValue, maxValue, highValue)) {
|
|
320
|
+
return null;
|
|
321
|
+
} else if (isClamped(lowValue, minValue, highValue) && !isClamped(lowValue, maxValue, highValue)) {
|
|
322
|
+
indexToReplace = maxValue;
|
|
323
|
+
this._largeValues.pop();
|
|
324
|
+
} else if (!isClamped(lowValue, minValue, highValue) && isClamped(lowValue, maxValue, highValue)) {
|
|
325
|
+
indexToReplace = minValue;
|
|
326
|
+
this._smallValues.pop();
|
|
327
|
+
} else if (lowValue - minValue > maxValue - highValue) {
|
|
328
|
+
indexToReplace = minValue;
|
|
136
329
|
this._smallValues.pop();
|
|
137
330
|
} else {
|
|
138
|
-
|
|
331
|
+
indexToReplace = maxValue;
|
|
139
332
|
this._largeValues.pop();
|
|
140
333
|
}
|
|
141
|
-
var
|
|
142
|
-
|
|
143
|
-
this._valueToPositionMap[newValue] = position;
|
|
144
|
-
this._pushToHeaps(position, newValue);
|
|
145
|
-
var _i = this._vacantPositions.findIndex(function (v) {
|
|
146
|
-
return v === position;
|
|
147
|
-
});
|
|
148
|
-
if (_i !== -1) this._vacantPositions.splice(_i, 1);
|
|
334
|
+
var replacedMeta = this._indexToMetaMap.get(indexToReplace);
|
|
335
|
+
var position = this._metaToPositionMap.get(replacedMeta);
|
|
149
336
|
return position;
|
|
150
337
|
};
|
|
338
|
+
_proto.shuffle = function shuffle() {
|
|
339
|
+
var _this = this;
|
|
340
|
+
var indices = new Array(this.bufferSize);
|
|
341
|
+
for (var idx = 0; idx < indices.length; idx++) {
|
|
342
|
+
var meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
|
|
343
|
+
var targetIndex = this.getMetaIndex(meta);
|
|
344
|
+
indices[idx] = targetIndex;
|
|
345
|
+
}
|
|
346
|
+
var _arr = new Array(indices.length);
|
|
347
|
+
var _available = [];
|
|
348
|
+
var indexToMetaMap = new Map();
|
|
349
|
+
var metaToIndexMap = new Map();
|
|
350
|
+
var metaToPositionMap = new Map();
|
|
351
|
+
var _loop = function _loop() {
|
|
352
|
+
var currentIndex = indices[_idx];
|
|
353
|
+
var currentMeta = _this._metaExtractor(currentIndex);
|
|
354
|
+
if (currentMeta == null) return "continue";
|
|
355
|
+
indexToMetaMap.set(currentIndex, currentMeta);
|
|
356
|
+
metaToIndexMap.set(currentMeta, currentIndex);
|
|
357
|
+
if (currentMeta === _this._positionToMetaList[_idx]) {
|
|
358
|
+
_arr[_idx] = currentMeta;
|
|
359
|
+
return "continue";
|
|
360
|
+
}
|
|
361
|
+
var _i = _this._positionToMetaList.findIndex(function (v) {
|
|
362
|
+
return v === currentMeta;
|
|
363
|
+
});
|
|
364
|
+
if (_i !== -1) {
|
|
365
|
+
_arr[_i] = currentMeta;
|
|
366
|
+
return "continue";
|
|
367
|
+
}
|
|
368
|
+
_available.push(currentMeta);
|
|
369
|
+
};
|
|
370
|
+
for (var _idx = 0; _idx < indices.length; _idx++) {
|
|
371
|
+
var _ret = _loop();
|
|
372
|
+
if (_ret === "continue") continue;
|
|
373
|
+
}
|
|
374
|
+
var _this$initialize2 = this.initialize(),
|
|
375
|
+
smallValues = _this$initialize2.smallValues,
|
|
376
|
+
largeValues = _this$initialize2.largeValues;
|
|
377
|
+
var positionToMetaList = [];
|
|
378
|
+
for (var position = 0; position < indices.length; position++) {
|
|
379
|
+
var value = indices[position];
|
|
380
|
+
if (_arr[position] != null) {
|
|
381
|
+
positionToMetaList[position] = _arr[position];
|
|
382
|
+
metaToPositionMap.set(_arr[position], position);
|
|
383
|
+
var element = {
|
|
384
|
+
position: position,
|
|
385
|
+
value: value
|
|
386
|
+
};
|
|
387
|
+
smallValues.push(element);
|
|
388
|
+
largeValues.push(element);
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
var _meta = _available.shift();
|
|
392
|
+
if (_meta != null) {
|
|
393
|
+
positionToMetaList[position] = _meta;
|
|
394
|
+
metaToPositionMap.set(_meta, position);
|
|
395
|
+
var _element = {
|
|
396
|
+
position: position,
|
|
397
|
+
value: value
|
|
398
|
+
};
|
|
399
|
+
smallValues.push(_element);
|
|
400
|
+
largeValues.push(_element);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
this._positionToMetaList = positionToMetaList;
|
|
404
|
+
this._smallValues = smallValues;
|
|
405
|
+
this._largeValues = largeValues;
|
|
406
|
+
this._indexToMetaMap = indexToMetaMap;
|
|
407
|
+
this.replaceMetaToIndexMap(metaToIndexMap);
|
|
408
|
+
this._metaToPositionMap = metaToPositionMap;
|
|
409
|
+
this._onTheFlyIndices = [];
|
|
410
|
+
try {
|
|
411
|
+
var _indices = new Array(this.bufferSize);
|
|
412
|
+
for (var _idx2 = 0; _idx2 < _indices.length; _idx2++) {
|
|
413
|
+
var _meta2 = this._onTheFlyIndices[_idx2] || this._positionToMetaList[_idx2];
|
|
414
|
+
var _targetIndex = this.getMetaIndex(_meta2);
|
|
415
|
+
if (_meta2 != null) {
|
|
416
|
+
_indices[_idx2] = {
|
|
417
|
+
meta: _meta2,
|
|
418
|
+
targetIndex: _targetIndex,
|
|
419
|
+
recyclerKey: this._name + "_" + _idx2
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return _indices;
|
|
424
|
+
} catch (err) {
|
|
425
|
+
this.readyToStartNextLoop();
|
|
426
|
+
return this._positionToMetaList;
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
_proto.getIndices = function getIndices() {
|
|
430
|
+
try {
|
|
431
|
+
var indices = new Array(this.bufferSize);
|
|
432
|
+
for (var idx = 0; idx < indices.length; idx++) {
|
|
433
|
+
var meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
|
|
434
|
+
var targetIndex = this.getMetaIndex(meta);
|
|
435
|
+
if (meta !== this.getIndexMeta(targetIndex)) {
|
|
436
|
+
return this.shuffle();
|
|
437
|
+
}
|
|
438
|
+
if (meta != null) {
|
|
439
|
+
indices[idx] = {
|
|
440
|
+
meta: meta,
|
|
441
|
+
targetIndex: targetIndex,
|
|
442
|
+
recyclerKey: this._name + "_" + idx
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
this._onTheFlyIndices = [];
|
|
447
|
+
return indices;
|
|
448
|
+
} catch (err) {
|
|
449
|
+
this.readyToStartNextLoop();
|
|
450
|
+
return this._positionToMetaList;
|
|
451
|
+
}
|
|
452
|
+
};
|
|
151
453
|
_proto._pushToHeaps = function _pushToHeaps(position, value) {
|
|
152
454
|
var element = {
|
|
153
455
|
position: position,
|
|
@@ -156,6 +458,30 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
156
458
|
this._smallValues.push(element);
|
|
157
459
|
this._largeValues.push(element);
|
|
158
460
|
};
|
|
461
|
+
_proto._setMetaPosition = function _setMetaPosition(meta, position) {
|
|
462
|
+
var prevMetaOnPosition = this._positionToMetaList[position];
|
|
463
|
+
if (prevMetaOnPosition) this._metaToPositionMap["delete"](prevMetaOnPosition);
|
|
464
|
+
this._positionToMetaList[position] = meta;
|
|
465
|
+
this._metaToPositionMap.set(meta, position);
|
|
466
|
+
};
|
|
467
|
+
_proto._setMetaIndex = function _setMetaIndex(meta, index) {
|
|
468
|
+
var prevMetaIndex = this.getMetaIndex(meta);
|
|
469
|
+
if (prevMetaIndex !== undefined) {
|
|
470
|
+
this._indexToMetaMap["delete"](prevMetaIndex);
|
|
471
|
+
}
|
|
472
|
+
this.setMetaIndex(meta, index);
|
|
473
|
+
this._indexToMetaMap.set(index, meta);
|
|
474
|
+
return false;
|
|
475
|
+
};
|
|
476
|
+
_proto.readyToStartNextLoop = function readyToStartNextLoop() {
|
|
477
|
+
this._lastUpdatedMS = Date.now();
|
|
478
|
+
};
|
|
479
|
+
_proto.prepare = function prepare() {
|
|
480
|
+
if (this._loopMS === this._lastUpdatedMS) return;
|
|
481
|
+
this._loopMS = this._lastUpdatedMS;
|
|
482
|
+
this._onTheFlyIndices = [];
|
|
483
|
+
this._isOnTheFlyFull = false;
|
|
484
|
+
};
|
|
159
485
|
_proto._cleanHeaps = function _cleanHeaps() {
|
|
160
486
|
this._cleanHeap(this._smallValues);
|
|
161
487
|
this._cleanHeap(this._largeValues);
|
|
@@ -165,13 +491,49 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
165
491
|
this._recreateHeaps();
|
|
166
492
|
}
|
|
167
493
|
};
|
|
494
|
+
_proto.rebuildHeapsWithValues = function rebuildHeapsWithValues(arr) {
|
|
495
|
+
var valueToPositionObject = {};
|
|
496
|
+
var newSmallValues = new Heap([], this._smallerComparator);
|
|
497
|
+
var newLargeValues = new Heap([], this._greaterComparator);
|
|
498
|
+
arr.forEach(function (element) {
|
|
499
|
+
var position = element.position,
|
|
500
|
+
value = element.value;
|
|
501
|
+
if (value !== undefined) {
|
|
502
|
+
var _element2 = {
|
|
503
|
+
position: position,
|
|
504
|
+
value: value
|
|
505
|
+
};
|
|
506
|
+
newSmallValues.push(_element2);
|
|
507
|
+
newLargeValues.push(_element2);
|
|
508
|
+
valueToPositionObject[value] = position;
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
var _arr = new Array(this._bufferSize).fill(2);
|
|
512
|
+
Object.keys(valueToPositionObject).map(function (key) {
|
|
513
|
+
return _arr[valueToPositionObject[key]] = 1;
|
|
514
|
+
});
|
|
515
|
+
_arr.forEach(function (_i, position) {
|
|
516
|
+
if (_i === 2) {
|
|
517
|
+
var value = Number.MAX_SAFE_INTEGER - position;
|
|
518
|
+
var element = {
|
|
519
|
+
position: position,
|
|
520
|
+
value: value
|
|
521
|
+
};
|
|
522
|
+
newSmallValues.push(element);
|
|
523
|
+
newLargeValues.push(element);
|
|
524
|
+
valueToPositionObject[value] = position;
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
this._smallValues = newSmallValues;
|
|
528
|
+
this._largeValues = newLargeValues;
|
|
529
|
+
};
|
|
168
530
|
_proto._recreateHeaps = function _recreateHeaps() {
|
|
169
531
|
var sourceHeap = this._smallValues.size() < this._largeValues.size() ? this._smallValues : this._largeValues;
|
|
170
532
|
var newSmallValues = new Heap([], this._smallerComparator);
|
|
171
533
|
var newLargeValues = new Heap([], this._greaterComparator);
|
|
172
534
|
while (!sourceHeap.empty()) {
|
|
173
535
|
var element = sourceHeap.pop();
|
|
174
|
-
if (this.
|
|
536
|
+
if (this._metaToPositionMap.get(this._indexToMetaMap.get(element.value)) != null) {
|
|
175
537
|
newSmallValues.push(element);
|
|
176
538
|
newLargeValues.push(element);
|
|
177
539
|
}
|
|
@@ -180,7 +542,7 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
180
542
|
this._largeValues = newLargeValues;
|
|
181
543
|
};
|
|
182
544
|
_proto._cleanHeap = function _cleanHeap(heap) {
|
|
183
|
-
while (!heap.empty() && this.
|
|
545
|
+
while (!heap.empty() && this._metaToPositionMap.get(this._indexToMetaMap.get(heap.peek().value)) == null) {
|
|
184
546
|
heap.pop();
|
|
185
547
|
}
|
|
186
548
|
};
|
|
@@ -191,18 +553,19 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
191
553
|
return lhs.value > rhs.value;
|
|
192
554
|
};
|
|
193
555
|
_createClass(IntegerBufferSet, [{
|
|
194
|
-
key: "
|
|
556
|
+
key: "bufferSize",
|
|
195
557
|
get: function get() {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return
|
|
558
|
+
return this._bufferSize;
|
|
559
|
+
}
|
|
560
|
+
}, {
|
|
561
|
+
key: "isBufferFull",
|
|
562
|
+
get: function get() {
|
|
563
|
+
return this._positionToMetaList.length >= this._bufferSize;
|
|
202
564
|
}
|
|
203
565
|
}]);
|
|
204
566
|
return IntegerBufferSet;
|
|
205
567
|
}();
|
|
206
568
|
|
|
207
569
|
exports.default = IntegerBufferSet;
|
|
570
|
+
exports.defaultBufferSize = defaultBufferSize;
|
|
208
571
|
//# sourceMappingURL=integer-buffer-set.cjs.development.js.map
|