@x-oasis/integer-buffer-set 0.1.19 → 0.1.23

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 CHANGED
@@ -16,4 +16,26 @@ import IntegerBufferSet from '@x-oasis/integer-buffer-set'
16
16
 
17
17
  ```bash
18
18
  $ pnpm test
19
- ```
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ## Philosophy
24
+
25
+ Basically, we only compare index value, such as
26
+
27
+ create a `onTheFlyIndices` as a slave buffer. theoretically, at most buffer size's new
28
+ item could occupy a position.
29
+
30
+ `IndexExtractor` is the key point of design.
31
+ if you do not delete/reorder an an array, `indexExtractor` is useless.
32
+
33
+ ## bad case
34
+
35
+ when getIndices..
36
+
37
+ `positionToMetaIndices` may used as a supplement. but when has multiple buffer. index refer to meta may changed to other buffer..
38
+
39
+ const data = [0, 1, 2, 3, 4, 5] only reuse `m % 3 === 0`, but when an item is deleted..
40
+ all these index after delete index value will be invalid, because `m % 2 === 0` could not be reused.
41
+
package/dist/index.d.ts CHANGED
@@ -1,34 +1,59 @@
1
1
  import Heap from '@x-oasis/heap';
2
- declare type HeapItem = {
3
- position: number;
4
- value: number;
5
- };
6
- declare class IntegerBufferSet {
7
- private _size;
8
- private _valueToPositionMap;
2
+ import { HeapItem, SafeRange, IntegerBufferSetProps, MetaToIndexMap } from './types';
3
+ export declare const defaultBufferSize = 10;
4
+ declare class IntegerBufferSet<Meta = any> {
5
+ private _name;
6
+ private _bufferSize;
7
+ private _indexToMetaMap;
8
+ private _metaToPositionMap;
9
+ private _positionToMetaList;
10
+ private _metaToIndexMap;
9
11
  private _smallValues;
10
12
  private _largeValues;
11
- private _vacantPositions;
12
- constructor();
13
- getSize(): number;
14
- get indices(): any[];
15
- getValuePosition(value: number): null | number;
16
- getNewPositionForValue(value: number): number;
13
+ private _metaExtractor;
14
+ private _indexExtractor;
15
+ private _onTheFlyIndices;
16
+ private _isOnTheFlyFull;
17
+ private _isOnTheFlyFullReturnHook;
18
+ private _loopMS;
19
+ private _lastUpdatedMS;
20
+ constructor(props?: IntegerBufferSetProps<Meta>);
21
+ get bufferSize(): number;
22
+ setIsOnTheFlyFull(val: any): void;
23
+ get isBufferFull(): boolean;
24
+ getOnTheFlyUncriticalPosition(safeRange: SafeRange): number;
25
+ initialize(): {
26
+ smallValues: Heap<any>;
27
+ largeValues: Heap<any>;
28
+ valueToPositionObject: {};
29
+ };
30
+ getIndexMeta(index: number): Meta;
31
+ getMetaIndex(meta: Meta): number;
32
+ setMetaIndex(meta: Meta, index: number): false | MetaToIndexMap<Meta>;
33
+ deleteMetaIndex(meta: Meta): boolean;
34
+ replaceMetaToIndexMap(newMetaToIndexMap: MetaToIndexMap<Meta>): false | MetaToIndexMap<Meta>;
35
+ getIndexPosition(index: number): undefined | number;
36
+ getNewPositionForIndex(index: number): number;
17
37
  getMinValue(): number;
18
38
  getMaxValue(): number;
19
- setPositionValue(position: number, value: number): void;
20
- replaceFurthestValuePosition(lowValue: number, highValue: number, newValue: number, useMinValueFn?: (options: {
21
- safeRange: {
22
- lowValue: number;
23
- highValue: number;
24
- };
25
- bufferSetRange: {
26
- maxValue: number;
27
- minValue: number;
28
- };
29
- currentIndex: number;
30
- }) => boolean): null | number;
39
+ replacePositionInFliedIndices(newIndex: number, safeRange: SafeRange): number;
40
+ getFliedPosition(newIndex: number, safeRange: SafeRange): any;
41
+ getPosition(newIndex: number, safeRange?: SafeRange): any;
42
+ replaceFurthestIndexPosition(newIndex: number, safeRange?: {
43
+ startIndex: number;
44
+ endIndex: number;
45
+ }): any;
46
+ _replaceFurthestIndexPosition(newIndex: number, safeRange?: {
47
+ startIndex: number;
48
+ endIndex: number;
49
+ }): any;
50
+ shuffle(): any[];
51
+ getIndices(): any[];
31
52
  _pushToHeaps(position: number, value: number): void;
53
+ _setMetaPosition(meta: Meta, position: number): void;
54
+ _setMetaIndex(meta: Meta, index: number): boolean;
55
+ readyToStartNextLoop(): void;
56
+ prepare(): void;
32
57
  _cleanHeaps(): void;
33
58
  _recreateHeaps(): void;
34
59
  _cleanHeap(heap: Heap<HeapItem>): void;
@@ -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++) {
@@ -38,45 +41,102 @@ function _toPropertyKey(arg) {
38
41
  return typeof key === "symbol" ? key : String(key);
39
42
  }
40
43
 
41
- var defaultUseMinValueFn = function defaultUseMinValueFn(options) {
42
- var safeRange = options.safeRange,
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;
44
+ var defaultMetaExtractor = function defaultMetaExtractor(value) {
45
+ return value;
49
46
  };
47
+ var defaultBufferSize = 10;
50
48
  var IntegerBufferSet = /*#__PURE__*/function () {
51
- function IntegerBufferSet() {
52
- this._valueToPositionMap = {};
53
- this._size = 0;
49
+ function IntegerBufferSet(props) {
50
+ if (props === void 0) {
51
+ props = {};
52
+ }
53
+ var _props = props,
54
+ _props$name = _props.name,
55
+ name = _props$name === void 0 ? 'default_buffer' : _props$name,
56
+ indexExtractor = _props.indexExtractor,
57
+ _props$bufferSize = _props.bufferSize,
58
+ bufferSize = _props$bufferSize === void 0 ? defaultBufferSize : _props$bufferSize,
59
+ _props$metaExtractor = _props.metaExtractor,
60
+ metaExtractor = _props$metaExtractor === void 0 ? defaultMetaExtractor : _props$metaExtractor;
61
+ this._metaExtractor = metaExtractor;
62
+ this._indexExtractor = indexExtractor;
63
+ this._name = name;
64
+ this._indexToMetaMap = new Map();
65
+ this._metaToPositionMap = new Map();
66
+ this._positionToMetaList = [];
67
+ this._metaToIndexMap = new Map();
68
+ this._onTheFlyIndices = [];
69
+ this._bufferSize = bufferSize;
54
70
  this._smallValues = new Heap([], this._smallerComparator);
55
71
  this._largeValues = new Heap([], this._greaterComparator);
56
- this.getNewPositionForValue = this.getNewPositionForValue.bind(this);
57
- this.getValuePosition = this.getValuePosition.bind(this);
58
- this.getSize = this.getSize.bind(this);
59
- this.replaceFurthestValuePosition = this.replaceFurthestValuePosition.bind(this);
60
- this._vacantPositions = [];
72
+ this.getNewPositionForIndex = this.getNewPositionForIndex.bind(this);
73
+ this.getIndexPosition = this.getIndexPosition.bind(this);
74
+ this.replacePositionInFliedIndices = this.replacePositionInFliedIndices.bind(this);
75
+ this.replaceFurthestIndexPosition = this.replaceFurthestIndexPosition.bind(this);
76
+ this._isOnTheFlyFullReturnHook = returnHook(this.setIsOnTheFlyFull.bind(this));
77
+ this._loopMS = Date.now();
78
+ this._lastUpdatedMS = this._loopMS;
61
79
  }
62
80
  var _proto = IntegerBufferSet.prototype;
63
- _proto.getSize = function getSize() {
64
- return this._size;
81
+ _proto.setIsOnTheFlyFull = function setIsOnTheFlyFull(val) {
82
+ if (val != null) {
83
+ var data = this._onTheFlyIndices.filter(function (v) {
84
+ return v;
85
+ });
86
+ this._isOnTheFlyFull = data.length === this._bufferSize;
87
+ }
65
88
  };
66
- _proto.getValuePosition = function getValuePosition(value) {
67
- if (this._valueToPositionMap[value] === undefined) {
68
- return null;
89
+ _proto.getOnTheFlyUncriticalPosition = function getOnTheFlyUncriticalPosition(safeRange) {
90
+ var startIndex = safeRange.startIndex,
91
+ endIndex = safeRange.endIndex;
92
+ for (var idx = 0; idx < this._onTheFlyIndices.length; idx++) {
93
+ var meta = this._onTheFlyIndices[idx];
94
+ var metaIndex = this._metaToIndexMap.get(meta);
95
+ if (!isClamped(startIndex, metaIndex, endIndex)) {
96
+ return idx;
97
+ }
98
+ }
99
+ return null;
100
+ };
101
+ _proto.initialize = function initialize() {
102
+ return {
103
+ smallValues: new Heap([], this._smallerComparator),
104
+ largeValues: new Heap([], this._greaterComparator),
105
+ valueToPositionObject: {}
106
+ };
107
+ };
108
+ _proto.getIndexMeta = function getIndexMeta(index) {
109
+ return this._metaExtractor(index);
110
+ };
111
+ _proto.getMetaIndex = function getMetaIndex(meta) {
112
+ if (this._indexExtractor) return this._indexExtractor(meta);
113
+ return this._metaToIndexMap.get(meta);
114
+ };
115
+ _proto.setMetaIndex = function setMetaIndex(meta, index) {
116
+ if (!this._indexExtractor) {
117
+ return this._metaToIndexMap.set(meta, index);
69
118
  }
70
- return this._valueToPositionMap[value];
119
+ return false;
71
120
  };
72
- _proto.getNewPositionForValue = function getNewPositionForValue(value) {
73
- if (this._valueToPositionMap[value] !== undefined) {
74
- console.warn("Shouldn't try to find new position for value already stored in BufferSet");
121
+ _proto.deleteMetaIndex = function deleteMetaIndex(meta) {
122
+ return this._metaToIndexMap["delete"](meta);
123
+ };
124
+ _proto.replaceMetaToIndexMap = function replaceMetaToIndexMap(newMetaToIndexMap) {
125
+ if (!this._indexExtractor) {
126
+ return this._metaToIndexMap = newMetaToIndexMap;
75
127
  }
76
- var newPosition = this._size;
77
- this._size++;
78
- this._pushToHeaps(newPosition, value);
79
- this._valueToPositionMap[value] = newPosition;
128
+ return false;
129
+ };
130
+ _proto.getIndexPosition = function getIndexPosition(index) {
131
+ return this.getMetaIndex(this.getIndexMeta(index));
132
+ };
133
+ _proto.getNewPositionForIndex = function getNewPositionForIndex(index) {
134
+ var meta = this.getIndexMeta(index);
135
+ !(this._metaToPositionMap.get(meta) === undefined) ? invariant(false, "Shouldn't try to find new position for value already stored in BufferSet") : void 0;
136
+ var newPosition = this._positionToMetaList.length;
137
+ this._pushToHeaps(newPosition, index);
138
+ this._setMetaIndex(meta, index);
139
+ this._setMetaPosition(meta, newPosition);
80
140
  return newPosition;
81
141
  };
82
142
  _proto.getMinValue = function getMinValue() {
@@ -87,67 +147,228 @@ var IntegerBufferSet = /*#__PURE__*/function () {
87
147
  var _this$_largeValues$pe;
88
148
  return (_this$_largeValues$pe = this._largeValues.peek()) == null ? void 0 : _this$_largeValues$pe.value;
89
149
  };
90
- _proto.setPositionValue = function setPositionValue(position, value) {
91
- var originalPosition = this._valueToPositionMap[value];
92
- if (originalPosition !== undefined) {
93
- var index = this._vacantPositions.findIndex(function (v) {
94
- return v === originalPosition;
95
- });
96
- if (index === -1) this._vacantPositions.push(originalPosition);
97
- delete this._valueToPositionMap[value];
98
- this._valueToPositionMap[value] = position;
150
+ _proto.replacePositionInFliedIndices = function replacePositionInFliedIndices(newIndex, safeRange) {
151
+ var startIndex = safeRange.startIndex,
152
+ endIndex = safeRange.endIndex;
153
+ if (this._isOnTheFlyFull) {
154
+ if (!isClamped(startIndex, newIndex, endIndex)) {
155
+ return null;
156
+ }
157
+ var pos = this.getOnTheFlyUncriticalPosition(safeRange);
158
+ if (pos != null) return pos;
99
159
  }
160
+ return null;
100
161
  };
101
- _proto.replaceFurthestValuePosition = function replaceFurthestValuePosition(lowValue, highValue, newValue, useMinValueFn) {
102
- if (useMinValueFn === void 0) {
103
- useMinValueFn = defaultUseMinValueFn;
162
+ _proto.getFliedPosition = function getFliedPosition(newIndex, safeRange) {
163
+ var pos = this.replacePositionInFliedIndices(newIndex, safeRange);
164
+ if (pos != null) {
165
+ var meta = this.getIndexMeta(newIndex);
166
+ this._onTheFlyIndices[pos] = meta;
167
+ this._setMetaIndex(meta, newIndex);
168
+ return this._isOnTheFlyFullReturnHook(pos);
104
169
  }
105
- if (this._valueToPositionMap[newValue] !== undefined) {
106
- console.warn("Shouldn't try to replace values with value already stored value in " + 'BufferSet');
170
+ return null;
171
+ };
172
+ _proto.getPosition = function getPosition(newIndex, safeRange) {
173
+ this.prepare();
174
+ var meta = this.getIndexMeta(newIndex);
175
+ var prevMetaPosition = this._metaToPositionMap.get(meta);
176
+ if (prevMetaPosition !== undefined) {
177
+ var onTheFlyPositionMeta = this._onTheFlyIndices[prevMetaPosition];
178
+ if (onTheFlyPositionMeta) {
179
+ if (onTheFlyPositionMeta === meta) {
180
+ return prevMetaPosition;
181
+ }
182
+ var _positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
183
+ if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
184
+ while (this._onTheFlyIndices[_positionToReplace]) {
185
+ _positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
186
+ }
187
+ if (_positionToReplace != null) {
188
+ this._setMetaIndex(meta, newIndex);
189
+ this._onTheFlyIndices[_positionToReplace] = onTheFlyPositionMeta;
190
+ return this._isOnTheFlyFullReturnHook(_positionToReplace);
191
+ }
192
+ }
193
+ this._onTheFlyIndices[prevMetaPosition] = meta;
194
+ return this._isOnTheFlyFullReturnHook(prevMetaPosition);
107
195
  }
108
- this._cleanHeaps();
109
- if (this._smallValues.empty() || this._largeValues.empty()) {
110
- return null;
196
+ if (!this.isBufferFull) return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
197
+ if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
198
+ var positionToReplace;
199
+ var prevIndexMeta = this._indexToMetaMap.get(newIndex);
200
+ if (!prevIndexMeta) {
201
+ this._cleanHeaps();
202
+ positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
203
+ } else {
204
+ positionToReplace = this._metaToPositionMap.get(prevIndexMeta);
111
205
  }
112
- if (this._vacantPositions.length) {
113
- var _position = this._vacantPositions.pop();
114
- this._valueToPositionMap[newValue] = _position;
115
- this._pushToHeaps(_position, newValue);
116
- return _position;
206
+ this._onTheFlyIndices[positionToReplace] = meta;
207
+ this._setMetaIndex(meta, newIndex);
208
+ this._setMetaPosition(meta, positionToReplace);
209
+ return this._isOnTheFlyFullReturnHook(positionToReplace);
210
+ };
211
+ _proto.replaceFurthestIndexPosition = function replaceFurthestIndexPosition(newIndex, safeRange) {
212
+ if (!this.isBufferFull) {
213
+ return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
214
+ }
215
+ return this._replaceFurthestIndexPosition(newIndex, safeRange);
216
+ };
217
+ _proto._replaceFurthestIndexPosition = function _replaceFurthestIndexPosition(newIndex, safeRange) {
218
+ if (this._largeValues.empty() || this._smallValues.empty()) {
219
+ return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
117
220
  }
118
221
  var minValue = this._smallValues.peek().value;
119
222
  var maxValue = this._largeValues.peek().value;
120
- if (minValue >= lowValue && maxValue <= highValue) {
121
- return null;
223
+ var indexToReplace;
224
+ if (!safeRange) {
225
+ if (Math.abs(newIndex - minValue) > Math.abs(newIndex - maxValue)) {
226
+ indexToReplace = minValue;
227
+ this._smallValues.pop();
228
+ } else {
229
+ indexToReplace = maxValue;
230
+ this._largeValues.pop();
231
+ }
232
+ var _replacedMeta = this._indexToMetaMap.get(indexToReplace);
233
+ var _position = this._metaToPositionMap.get(_replacedMeta);
234
+ return _position;
122
235
  }
123
- var valueToReplace;
124
- if (useMinValueFn({
125
- safeRange: {
126
- lowValue: lowValue,
127
- highValue: highValue
128
- },
129
- bufferSetRange: {
130
- minValue: minValue,
131
- maxValue: maxValue
132
- },
133
- currentIndex: newValue
134
- })) {
135
- valueToReplace = minValue;
236
+ var lowValue = safeRange.startIndex,
237
+ highValue = safeRange.endIndex;
238
+ if (isClamped(lowValue, minValue, highValue) && isClamped(lowValue, maxValue, highValue)) {
239
+ return null;
240
+ } else if (isClamped(lowValue, minValue, highValue) && !isClamped(lowValue, maxValue, highValue)) {
241
+ indexToReplace = maxValue;
242
+ this._largeValues.pop();
243
+ } else if (!isClamped(lowValue, minValue, highValue) && isClamped(lowValue, maxValue, highValue)) {
244
+ indexToReplace = minValue;
245
+ this._smallValues.pop();
246
+ } else if (lowValue - minValue > maxValue - highValue) {
247
+ indexToReplace = minValue;
136
248
  this._smallValues.pop();
137
249
  } else {
138
- valueToReplace = maxValue;
250
+ indexToReplace = maxValue;
139
251
  this._largeValues.pop();
140
252
  }
141
- var position = this._valueToPositionMap[valueToReplace];
142
- delete this._valueToPositionMap[valueToReplace];
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);
253
+ var replacedMeta = this._indexToMetaMap.get(indexToReplace);
254
+ var position = this._metaToPositionMap.get(replacedMeta);
149
255
  return position;
150
256
  };
257
+ _proto.shuffle = function shuffle() {
258
+ var _this = this;
259
+ var indices = new Array(this.bufferSize);
260
+ for (var idx = 0; idx < indices.length; idx++) {
261
+ var meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
262
+ var targetIndex = this.getMetaIndex(meta);
263
+ indices[idx] = targetIndex;
264
+ }
265
+ var _arr = new Array(indices.length);
266
+ var _available = [];
267
+ var indexToMetaMap = new Map();
268
+ var metaToIndexMap = new Map();
269
+ var metaToPositionMap = new Map();
270
+ var _loop = function _loop() {
271
+ var currentIndex = indices[_idx];
272
+ var currentMeta = _this._metaExtractor(currentIndex);
273
+ if (currentMeta == null) return "continue";
274
+ indexToMetaMap.set(currentIndex, currentMeta);
275
+ metaToIndexMap.set(currentMeta, currentIndex);
276
+ if (currentMeta === _this._positionToMetaList[_idx]) {
277
+ _arr[_idx] = currentMeta;
278
+ return "continue";
279
+ }
280
+ var _i = _this._positionToMetaList.findIndex(function (v) {
281
+ return v === currentMeta;
282
+ });
283
+ if (_i !== -1) {
284
+ _arr[_i] = currentMeta;
285
+ return "continue";
286
+ }
287
+ _available.push(currentMeta);
288
+ };
289
+ for (var _idx = 0; _idx < indices.length; _idx++) {
290
+ var _ret = _loop();
291
+ if (_ret === "continue") continue;
292
+ }
293
+ var _this$initialize = this.initialize(),
294
+ smallValues = _this$initialize.smallValues,
295
+ largeValues = _this$initialize.largeValues;
296
+ var positionToMetaList = [];
297
+ for (var position = 0; position < indices.length; position++) {
298
+ var value = indices[position];
299
+ if (_arr[position] != null) {
300
+ positionToMetaList[position] = _arr[position];
301
+ metaToPositionMap.set(_arr[position], position);
302
+ var element = {
303
+ position: position,
304
+ value: value
305
+ };
306
+ smallValues.push(element);
307
+ largeValues.push(element);
308
+ continue;
309
+ }
310
+ var _meta = _available.shift();
311
+ if (_meta != null) {
312
+ positionToMetaList[position] = _meta;
313
+ metaToPositionMap.set(_meta, position);
314
+ var _element = {
315
+ position: position,
316
+ value: value
317
+ };
318
+ smallValues.push(_element);
319
+ largeValues.push(_element);
320
+ }
321
+ }
322
+ this._positionToMetaList = positionToMetaList;
323
+ this._smallValues = smallValues;
324
+ this._largeValues = largeValues;
325
+ this._indexToMetaMap = indexToMetaMap;
326
+ this.replaceMetaToIndexMap(metaToIndexMap);
327
+ this._metaToPositionMap = metaToPositionMap;
328
+ this._onTheFlyIndices = [];
329
+ try {
330
+ var _indices = new Array(this.bufferSize);
331
+ for (var _idx2 = 0; _idx2 < _indices.length; _idx2++) {
332
+ var _meta2 = this._onTheFlyIndices[_idx2] || this._positionToMetaList[_idx2];
333
+ var _targetIndex = this.getMetaIndex(_meta2);
334
+ if (_meta2 != null) {
335
+ _indices[_idx2] = {
336
+ meta: _meta2,
337
+ targetIndex: _targetIndex,
338
+ recyclerKey: this._name + "_" + _idx2
339
+ };
340
+ }
341
+ }
342
+ return _indices;
343
+ } catch (err) {
344
+ this.readyToStartNextLoop();
345
+ return this._positionToMetaList;
346
+ }
347
+ };
348
+ _proto.getIndices = function getIndices() {
349
+ try {
350
+ var indices = new Array(this.bufferSize);
351
+ for (var idx = 0; idx < indices.length; idx++) {
352
+ var meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
353
+ var targetIndex = this.getMetaIndex(meta);
354
+ if (meta !== this.getIndexMeta(targetIndex)) {
355
+ return this.shuffle();
356
+ }
357
+ if (meta != null) {
358
+ indices[idx] = {
359
+ meta: meta,
360
+ targetIndex: targetIndex,
361
+ recyclerKey: this._name + "_" + idx
362
+ };
363
+ }
364
+ }
365
+ this._onTheFlyIndices = [];
366
+ return indices;
367
+ } catch (err) {
368
+ this.readyToStartNextLoop();
369
+ return this._positionToMetaList;
370
+ }
371
+ };
151
372
  _proto._pushToHeaps = function _pushToHeaps(position, value) {
152
373
  var element = {
153
374
  position: position,
@@ -156,6 +377,30 @@ var IntegerBufferSet = /*#__PURE__*/function () {
156
377
  this._smallValues.push(element);
157
378
  this._largeValues.push(element);
158
379
  };
380
+ _proto._setMetaPosition = function _setMetaPosition(meta, position) {
381
+ var prevMetaOnPosition = this._positionToMetaList[position];
382
+ if (prevMetaOnPosition) this._metaToPositionMap["delete"](prevMetaOnPosition);
383
+ this._positionToMetaList[position] = meta;
384
+ this._metaToPositionMap.set(meta, position);
385
+ };
386
+ _proto._setMetaIndex = function _setMetaIndex(meta, index) {
387
+ var prevMetaIndex = this.getMetaIndex(meta);
388
+ if (prevMetaIndex !== undefined) {
389
+ this._indexToMetaMap["delete"](prevMetaIndex);
390
+ }
391
+ this.setMetaIndex(meta, index);
392
+ this._indexToMetaMap.set(index, meta);
393
+ return false;
394
+ };
395
+ _proto.readyToStartNextLoop = function readyToStartNextLoop() {
396
+ this._lastUpdatedMS = Date.now();
397
+ };
398
+ _proto.prepare = function prepare() {
399
+ if (this._loopMS === this._lastUpdatedMS) return;
400
+ this._loopMS = this._lastUpdatedMS;
401
+ this._onTheFlyIndices = [];
402
+ this._isOnTheFlyFull = false;
403
+ };
159
404
  _proto._cleanHeaps = function _cleanHeaps() {
160
405
  this._cleanHeap(this._smallValues);
161
406
  this._cleanHeap(this._largeValues);
@@ -171,7 +416,7 @@ var IntegerBufferSet = /*#__PURE__*/function () {
171
416
  var newLargeValues = new Heap([], this._greaterComparator);
172
417
  while (!sourceHeap.empty()) {
173
418
  var element = sourceHeap.pop();
174
- if (this._valueToPositionMap[element.value] !== undefined) {
419
+ if (this._metaToPositionMap.get(this._indexToMetaMap.get(element.value)) != null) {
175
420
  newSmallValues.push(element);
176
421
  newLargeValues.push(element);
177
422
  }
@@ -180,7 +425,7 @@ var IntegerBufferSet = /*#__PURE__*/function () {
180
425
  this._largeValues = newLargeValues;
181
426
  };
182
427
  _proto._cleanHeap = function _cleanHeap(heap) {
183
- while (!heap.empty() && this._valueToPositionMap[heap.peek().value] === undefined) {
428
+ while (!heap.empty() && this._metaToPositionMap.get(this._indexToMetaMap.get(heap.peek().value)) == null) {
184
429
  heap.pop();
185
430
  }
186
431
  };
@@ -191,18 +436,19 @@ var IntegerBufferSet = /*#__PURE__*/function () {
191
436
  return lhs.value > rhs.value;
192
437
  };
193
438
  _createClass(IntegerBufferSet, [{
194
- key: "indices",
439
+ key: "bufferSize",
195
440
  get: function get() {
196
- var indices = [];
197
- for (var key in this._valueToPositionMap) {
198
- var value = this._valueToPositionMap[key];
199
- indices[value] = key;
200
- }
201
- return indices;
441
+ return this._bufferSize;
442
+ }
443
+ }, {
444
+ key: "isBufferFull",
445
+ get: function get() {
446
+ return this._positionToMetaList.length >= this._bufferSize;
202
447
  }
203
448
  }]);
204
449
  return IntegerBufferSet;
205
450
  }();
206
451
 
207
452
  exports.default = IntegerBufferSet;
453
+ exports.defaultBufferSize = defaultBufferSize;
208
454
  //# sourceMappingURL=integer-buffer-set.cjs.development.js.map