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