@x-oasis/integer-buffer-set 0.1.20 → 0.1.24

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/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import Heap from '@x-oasis/heap';
2
- import { HeapItem, SafeRange, IntegerBufferSetProps, MetaToIndexMap, MetaToPositionMap } from './types';
2
+ import { HeapItem, SafeRange, IntegerBufferSetProps, MetaToIndexMap } from './types';
3
3
  export declare const defaultBufferSize = 10;
4
4
  declare class IntegerBufferSet<Meta = any> {
5
- private _size;
6
5
  private _name;
7
6
  private _bufferSize;
8
7
  private _indexToMetaMap;
@@ -19,15 +18,15 @@ declare class IntegerBufferSet<Meta = any> {
19
18
  private _loopMS;
20
19
  private _lastUpdatedMS;
21
20
  constructor(props?: IntegerBufferSetProps<Meta>);
22
- getSize(): number;
23
21
  get bufferSize(): number;
22
+ isThresholdMeta(meta: any): boolean;
24
23
  setIsOnTheFlyFull(val: any): void;
24
+ resetOnTheFlies(): void;
25
25
  get isBufferFull(): boolean;
26
26
  getOnTheFlyUncriticalPosition(safeRange: SafeRange): number;
27
27
  initialize(): {
28
28
  smallValues: Heap<any>;
29
29
  largeValues: Heap<any>;
30
- valueToPositionObject: {};
31
30
  };
32
31
  getIndexMeta(index: number): Meta;
33
32
  getMetaIndex(meta: Meta): number;
@@ -38,13 +37,7 @@ declare class IntegerBufferSet<Meta = any> {
38
37
  getNewPositionForIndex(index: number): number;
39
38
  getMinValue(): number;
40
39
  getMaxValue(): number;
41
- setValuePosition(value: number, position: number): void;
42
- findPositionMeta(position: number): Meta;
43
- rebuildHeapsWithMeta(metaToPositionMap: MetaToPositionMap<Meta>): void;
44
- setPositionIndex(position: number, index: number): boolean;
45
- getMetaPosition(meta: Meta): number;
46
- replacePositionInFliedIndices(newIndex: number, safeRange: SafeRange): number;
47
- getFliedPosition(newIndex: number, safeRange: SafeRange): any;
40
+ getFliedPosition(newIndex: number, safeRange: SafeRange): number;
48
41
  getPosition(newIndex: number, safeRange?: SafeRange): any;
49
42
  replaceFurthestIndexPosition(newIndex: number, safeRange?: {
50
43
  startIndex: number;
@@ -54,20 +47,21 @@ declare class IntegerBufferSet<Meta = any> {
54
47
  startIndex: number;
55
48
  endIndex: number;
56
49
  }): any;
57
- shuffle(): any[];
58
- getIndices(): any[];
50
+ shuffle(): any;
51
+ getIndices(): any;
59
52
  _pushToHeaps(position: number, value: number): void;
60
53
  _setMetaPosition(meta: Meta, position: number): void;
54
+ commitPosition(props: {
55
+ newIndex: number;
56
+ position: number;
57
+ meta: Meta;
58
+ safeRange: SafeRange;
59
+ }): number;
61
60
  _setMetaIndex(meta: Meta, index: number): boolean;
62
61
  readyToStartNextLoop(): void;
63
62
  prepare(): void;
64
63
  _cleanHeaps(): void;
65
- rebuildHeapsWithValues(arr: Array<{
66
- position: number;
67
- value: number;
68
- }>): void;
69
64
  _recreateHeaps(): void;
70
- _cleanHeap(heap: Heap<HeapItem>): void;
71
65
  _smallerComparator(lhs: HeapItem, rhs: HeapItem): boolean;
72
66
  _greaterComparator(lhs: HeapItem, rhs: HeapItem): boolean;
73
67
  }
@@ -26,37 +26,6 @@ function _createClass(Constructor, protoProps, staticProps) {
26
26
  });
27
27
  return Constructor;
28
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
- }
60
29
  function _toPrimitive(input, hint) {
61
30
  if (typeof input !== "object" || input === null) return input;
62
31
  var prim = input[Symbol.toPrimitive];
@@ -76,6 +45,7 @@ var defaultMetaExtractor = function defaultMetaExtractor(value) {
76
45
  return value;
77
46
  };
78
47
  var defaultBufferSize = 10;
48
+ var thresholdNumber = Number.MAX_SAFE_INTEGER - 100000;
79
49
  var IntegerBufferSet = /*#__PURE__*/function () {
80
50
  function IntegerBufferSet(props) {
81
51
  if (props === void 0) {
@@ -97,37 +67,39 @@ var IntegerBufferSet = /*#__PURE__*/function () {
97
67
  this._positionToMetaList = [];
98
68
  this._metaToIndexMap = new Map();
99
69
  this._onTheFlyIndices = [];
100
- this._size = 0;
101
70
  this._bufferSize = bufferSize;
102
71
  this._smallValues = new Heap([], this._smallerComparator);
103
72
  this._largeValues = new Heap([], this._greaterComparator);
104
73
  this.getNewPositionForIndex = this.getNewPositionForIndex.bind(this);
105
74
  this.getIndexPosition = this.getIndexPosition.bind(this);
106
- this.getSize = this.getSize.bind(this);
107
- this.replacePositionInFliedIndices = this.replacePositionInFliedIndices.bind(this);
108
75
  this.replaceFurthestIndexPosition = this.replaceFurthestIndexPosition.bind(this);
109
76
  this._isOnTheFlyFullReturnHook = returnHook(this.setIsOnTheFlyFull.bind(this));
110
77
  this._loopMS = Date.now();
111
78
  this._lastUpdatedMS = this._loopMS;
112
79
  }
113
80
  var _proto = IntegerBufferSet.prototype;
114
- _proto.getSize = function getSize() {
115
- return this._size;
81
+ _proto.isThresholdMeta = function isThresholdMeta(meta) {
82
+ if (typeof meta === 'number' && meta > thresholdNumber) return true;
83
+ return false;
116
84
  };
117
85
  _proto.setIsOnTheFlyFull = function setIsOnTheFlyFull(val) {
118
86
  if (val != null) {
119
87
  var data = this._onTheFlyIndices.filter(function (v) {
120
- return v;
88
+ return v != null;
121
89
  });
122
90
  this._isOnTheFlyFull = data.length === this._bufferSize;
123
91
  }
124
92
  };
93
+ _proto.resetOnTheFlies = function resetOnTheFlies() {
94
+ this._isOnTheFlyFull = false;
95
+ this._onTheFlyIndices = [];
96
+ };
125
97
  _proto.getOnTheFlyUncriticalPosition = function getOnTheFlyUncriticalPosition(safeRange) {
126
98
  var startIndex = safeRange.startIndex,
127
99
  endIndex = safeRange.endIndex;
128
100
  for (var idx = 0; idx < this._onTheFlyIndices.length; idx++) {
129
101
  var meta = this._onTheFlyIndices[idx];
130
- var metaIndex = this._metaToIndexMap.get(meta);
102
+ var metaIndex = this.getMetaIndex(meta);
131
103
  if (!isClamped(startIndex, metaIndex, endIndex)) {
132
104
  return idx;
133
105
  }
@@ -137,14 +109,16 @@ var IntegerBufferSet = /*#__PURE__*/function () {
137
109
  _proto.initialize = function initialize() {
138
110
  return {
139
111
  smallValues: new Heap([], this._smallerComparator),
140
- largeValues: new Heap([], this._greaterComparator),
141
- valueToPositionObject: {}
112
+ largeValues: new Heap([], this._greaterComparator)
142
113
  };
143
114
  };
144
115
  _proto.getIndexMeta = function getIndexMeta(index) {
116
+ if (index == null || index < 0) return null;
145
117
  return this._metaExtractor(index);
146
118
  };
147
119
  _proto.getMetaIndex = function getMetaIndex(meta) {
120
+ if (meta == null) return -1;
121
+ if (this.isThresholdMeta(meta)) return -1;
148
122
  if (this._indexExtractor) return this._indexExtractor(meta);
149
123
  return this._metaToIndexMap.get(meta);
150
124
  };
@@ -183,111 +157,53 @@ var IntegerBufferSet = /*#__PURE__*/function () {
183
157
  var _this$_largeValues$pe;
184
158
  return (_this$_largeValues$pe = this._largeValues.peek()) == null ? void 0 : _this$_largeValues$pe.value;
185
159
  };
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);
218
- if (originalPosition !== undefined) {
219
- if (originalPosition === position) return true;
220
- this.deleteMetaIndex(meta);
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);
230
- };
231
- _proto.replacePositionInFliedIndices = function replacePositionInFliedIndices(newIndex, safeRange) {
232
- var startIndex = safeRange.startIndex,
233
- endIndex = safeRange.endIndex;
160
+ _proto.getFliedPosition = function getFliedPosition(newIndex, safeRange) {
234
161
  if (this._isOnTheFlyFull) {
235
- if (!isClamped(startIndex, newIndex, endIndex)) {
236
- return null;
162
+ if (safeRange && isClamped(safeRange.startIndex, newIndex, safeRange.endIndex)) {
163
+ return this.getOnTheFlyUncriticalPosition(safeRange);
237
164
  }
238
- var pos = this.getOnTheFlyUncriticalPosition(safeRange);
239
- if (pos != null) return pos;
240
- }
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);
250
165
  }
251
166
  return null;
252
167
  };
253
168
  _proto.getPosition = function getPosition(newIndex, safeRange) {
254
169
  this.prepare();
255
170
  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);
276
- }
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);
171
+ var metaPosition = this._metaToPositionMap.get(meta);
172
+ var position, indexMeta;
173
+ if (metaPosition !== undefined) {
174
+ position = this.commitPosition({
175
+ newIndex: newIndex,
176
+ meta: meta,
177
+ safeRange: safeRange,
178
+ position: metaPosition
179
+ });
180
+ } else if (!this.isBufferFull) {
181
+ position = this.getNewPositionForIndex(newIndex);
182
+ } else if (this._isOnTheFlyFull) {
183
+ position = this.getFliedPosition(newIndex, safeRange);
184
+ } else if ((indexMeta = this._indexToMetaMap.get(newIndex)) && this._metaToPositionMap.get(indexMeta)) {
185
+ position = this.commitPosition({
186
+ newIndex: newIndex,
187
+ meta: meta,
188
+ safeRange: safeRange,
189
+ position: this._metaToPositionMap.get(indexMeta)
190
+ });
284
191
  } else {
285
- positionToReplace = this._metaToPositionMap.get(prevIndexMeta);
192
+ this._cleanHeaps();
193
+ position = this.commitPosition({
194
+ newIndex: newIndex,
195
+ meta: meta,
196
+ safeRange: safeRange,
197
+ position: this._replaceFurthestIndexPosition(newIndex, safeRange)
198
+ });
286
199
  }
287
- this._onTheFlyIndices[positionToReplace] = meta;
288
- this._setMetaIndex(meta, newIndex);
289
- this._setMetaPosition(meta, positionToReplace);
290
- return this._isOnTheFlyFullReturnHook(positionToReplace);
200
+ if (position != null) {
201
+ this._onTheFlyIndices[position] = meta;
202
+ this._setMetaIndex(meta, newIndex);
203
+ this._metaToPositionMap.set(meta, position);
204
+ return this._isOnTheFlyFullReturnHook(position);
205
+ }
206
+ return null;
291
207
  };
292
208
  _proto.replaceFurthestIndexPosition = function replaceFurthestIndexPosition(newIndex, safeRange) {
293
209
  if (!this.isBufferFull) {
@@ -299,9 +215,16 @@ var IntegerBufferSet = /*#__PURE__*/function () {
299
215
  if (this._largeValues.empty() || this._smallValues.empty()) {
300
216
  return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
301
217
  }
218
+ var indexToReplace;
302
219
  var minValue = this._smallValues.peek().value;
303
220
  var maxValue = this._largeValues.peek().value;
304
- var indexToReplace;
221
+ if (maxValue > thresholdNumber) {
222
+ indexToReplace = maxValue;
223
+ this._largeValues.pop();
224
+ var _replacedMeta = this._indexToMetaMap.get(indexToReplace);
225
+ var _position = this._metaToPositionMap.get(_replacedMeta);
226
+ return _position;
227
+ }
305
228
  if (!safeRange) {
306
229
  if (Math.abs(newIndex - minValue) > Math.abs(newIndex - maxValue)) {
307
230
  indexToReplace = minValue;
@@ -310,9 +233,9 @@ var IntegerBufferSet = /*#__PURE__*/function () {
310
233
  indexToReplace = maxValue;
311
234
  this._largeValues.pop();
312
235
  }
313
- var _replacedMeta = this._indexToMetaMap.get(indexToReplace);
314
- var _position = this._metaToPositionMap.get(_replacedMeta);
315
- return _position;
236
+ var _replacedMeta2 = this._indexToMetaMap.get(indexToReplace);
237
+ var _position2 = this._metaToPositionMap.get(_replacedMeta2);
238
+ return _position2;
316
239
  }
317
240
  var lowValue = safeRange.startIndex,
318
241
  highValue = safeRange.endIndex;
@@ -347,7 +270,6 @@ var IntegerBufferSet = /*#__PURE__*/function () {
347
270
  var _available = [];
348
271
  var indexToMetaMap = new Map();
349
272
  var metaToIndexMap = new Map();
350
- var metaToPositionMap = new Map();
351
273
  var _loop = function _loop() {
352
274
  var currentIndex = indices[_idx];
353
275
  var currentMeta = _this._metaExtractor(currentIndex);
@@ -371,71 +293,43 @@ var IntegerBufferSet = /*#__PURE__*/function () {
371
293
  var _ret = _loop();
372
294
  if (_ret === "continue") continue;
373
295
  }
374
- var _this$initialize2 = this.initialize(),
375
- smallValues = _this$initialize2.smallValues,
376
- largeValues = _this$initialize2.largeValues;
377
296
  var positionToMetaList = [];
297
+ this._indexToMetaMap = indexToMetaMap;
298
+ this.replaceMetaToIndexMap(metaToIndexMap);
378
299
  for (var position = 0; position < indices.length; position++) {
379
- var value = indices[position];
380
300
  if (_arr[position] != null) {
381
301
  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
302
  continue;
390
303
  }
391
304
  var _meta = _available.shift();
392
305
  if (_meta != null) {
393
306
  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
307
  }
402
308
  }
403
309
  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
- }
310
+ return this.getIndices();
428
311
  };
429
312
  _proto.getIndices = function getIndices() {
313
+ var _this$initialize = this.initialize(),
314
+ smallValues = _this$initialize.smallValues,
315
+ largeValues = _this$initialize.largeValues;
430
316
  try {
431
- var indices = new Array(this.bufferSize);
317
+ var indices = new Array(this._positionToMetaList.length);
318
+ var metaToPositionMap = new Map();
432
319
  for (var idx = 0; idx < indices.length; idx++) {
433
320
  var meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
434
321
  var targetIndex = this.getMetaIndex(meta);
435
- if (meta !== this.getIndexMeta(targetIndex)) {
322
+ if (!this.isThresholdMeta(meta) && meta != this.getIndexMeta(targetIndex)) {
436
323
  return this.shuffle();
437
324
  }
438
- if (meta != null) {
325
+ if (meta != null && !this.isThresholdMeta(meta)) {
326
+ var element = {
327
+ position: idx,
328
+ value: targetIndex
329
+ };
330
+ smallValues.push(element);
331
+ largeValues.push(element);
332
+ metaToPositionMap.set(meta, idx);
439
333
  indices[idx] = {
440
334
  meta: meta,
441
335
  targetIndex: targetIndex,
@@ -443,11 +337,19 @@ var IntegerBufferSet = /*#__PURE__*/function () {
443
337
  };
444
338
  }
445
339
  }
446
- this._onTheFlyIndices = [];
340
+ this._smallValues = smallValues;
341
+ this._largeValues = largeValues;
342
+ this._metaToPositionMap = metaToPositionMap;
343
+ this._positionToMetaList = indices.map(function (v) {
344
+ return v == null ? void 0 : v.meta;
345
+ });
346
+ this.resetOnTheFlies();
447
347
  return indices;
448
348
  } catch (err) {
449
- this.readyToStartNextLoop();
349
+ console.log('err ', err);
450
350
  return this._positionToMetaList;
351
+ } finally {
352
+ this.readyToStartNextLoop();
451
353
  }
452
354
  };
453
355
  _proto._pushToHeaps = function _pushToHeaps(position, value) {
@@ -459,11 +361,26 @@ var IntegerBufferSet = /*#__PURE__*/function () {
459
361
  this._largeValues.push(element);
460
362
  };
461
363
  _proto._setMetaPosition = function _setMetaPosition(meta, position) {
462
- var prevMetaOnPosition = this._positionToMetaList[position];
463
- if (prevMetaOnPosition) this._metaToPositionMap["delete"](prevMetaOnPosition);
464
364
  this._positionToMetaList[position] = meta;
465
365
  this._metaToPositionMap.set(meta, position);
466
366
  };
367
+ _proto.commitPosition = function commitPosition(props) {
368
+ var newIndex = props.newIndex,
369
+ safeRange = props.safeRange,
370
+ position = props.position,
371
+ meta = props.meta;
372
+ var onTheFlyPositionMeta = this._onTheFlyIndices[position];
373
+ var positionToReplace = position;
374
+ if (onTheFlyPositionMeta) {
375
+ if (onTheFlyPositionMeta === meta) return position;
376
+ if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
377
+ positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
378
+ while (this._onTheFlyIndices[positionToReplace]) {
379
+ positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
380
+ }
381
+ }
382
+ return positionToReplace;
383
+ };
467
384
  _proto._setMetaIndex = function _setMetaIndex(meta, index) {
468
385
  var prevMetaIndex = this.getMetaIndex(meta);
469
386
  if (prevMetaIndex !== undefined) {
@@ -483,68 +400,42 @@ var IntegerBufferSet = /*#__PURE__*/function () {
483
400
  this._isOnTheFlyFull = false;
484
401
  };
485
402
  _proto._cleanHeaps = function _cleanHeaps() {
486
- this._cleanHeap(this._smallValues);
487
- this._cleanHeap(this._largeValues);
403
+ for (var idx = 0; idx < this._positionToMetaList.length; idx++) {
404
+ if (this._positionToMetaList[idx] == null) {
405
+ this._recreateHeaps();
406
+ return;
407
+ }
408
+ }
488
409
  var minHeapSize = Math.min(this._smallValues.size(), this._largeValues.size());
489
410
  var maxHeapSize = Math.max(this._smallValues.size(), this._largeValues.size());
490
411
  if (maxHeapSize > 10 * minHeapSize) {
491
412
  this._recreateHeaps();
492
413
  }
493
414
  };
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
- };
530
415
  _proto._recreateHeaps = function _recreateHeaps() {
531
- var sourceHeap = this._smallValues.size() < this._largeValues.size() ? this._smallValues : this._largeValues;
532
- var newSmallValues = new Heap([], this._smallerComparator);
533
- var newLargeValues = new Heap([], this._greaterComparator);
534
- while (!sourceHeap.empty()) {
535
- var element = sourceHeap.pop();
536
- if (this._metaToPositionMap.get(this._indexToMetaMap.get(element.value)) != null) {
537
- newSmallValues.push(element);
538
- newLargeValues.push(element);
416
+ var _this$initialize2 = this.initialize(),
417
+ smallValues = _this$initialize2.smallValues,
418
+ largeValues = _this$initialize2.largeValues;
419
+ for (var position = 0; position < this._positionToMetaList.length; position++) {
420
+ var meta = this._positionToMetaList[position];
421
+ var value = this.getMetaIndex(meta);
422
+ if (!meta || value === -1 || value == null) {
423
+ value = Number.MAX_SAFE_INTEGER - position;
424
+ }
425
+ var element = {
426
+ position: position,
427
+ value: value
428
+ };
429
+ smallValues.push(element);
430
+ largeValues.push(element);
431
+ if (value > thresholdNumber) {
432
+ this._setMetaPosition(value, position);
433
+ this._setMetaIndex(value, value);
539
434
  }
540
435
  }
541
- this._smallValues = newSmallValues;
542
- this._largeValues = newLargeValues;
543
- };
544
- _proto._cleanHeap = function _cleanHeap(heap) {
545
- while (!heap.empty() && this._metaToPositionMap.get(this._indexToMetaMap.get(heap.peek().value)) == null) {
546
- heap.pop();
547
- }
436
+ this._largeValues.peek().value;
437
+ this._smallValues = smallValues;
438
+ this._largeValues = largeValues;
548
439
  };
549
440
  _proto._smallerComparator = function _smallerComparator(lhs, rhs) {
550
441
  return lhs.value < rhs.value;