@x-oasis/integer-buffer-set 0.1.20 → 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/src/index.ts CHANGED
@@ -34,10 +34,8 @@ export const defaultBufferSize = 10;
34
34
  // the set.
35
35
  // feature: add / delete / update item will also in consider..
36
36
  class IntegerBufferSet<Meta = any> {
37
- private _size: number;
38
37
  private _name: string;
39
38
  private _bufferSize: number;
40
- // private _positionToValueObject: ValueToPositionObject;
41
39
 
42
40
  private _indexToMetaMap: IndexToMetaMap<Meta>;
43
41
  private _metaToPositionMap: MetaToPositionMap<Meta>;
@@ -68,7 +66,6 @@ class IntegerBufferSet<Meta = any> {
68
66
  this._indexExtractor = indexExtractor;
69
67
 
70
68
  this._name = name;
71
- // this._positionToValueObject = {};
72
69
 
73
70
  /**
74
71
  * this._indexToMetaMap is used to find the prev meta when finding a position for index.
@@ -79,7 +76,6 @@ class IntegerBufferSet<Meta = any> {
79
76
  this._metaToIndexMap = new Map();
80
77
  this._onTheFlyIndices = [];
81
78
 
82
- this._size = 0;
83
79
  this._bufferSize = bufferSize;
84
80
 
85
81
  this._smallValues = new Heap([], this._smallerComparator);
@@ -87,7 +83,6 @@ class IntegerBufferSet<Meta = any> {
87
83
 
88
84
  this.getNewPositionForIndex = this.getNewPositionForIndex.bind(this);
89
85
  this.getIndexPosition = this.getIndexPosition.bind(this);
90
- this.getSize = this.getSize.bind(this);
91
86
  this.replacePositionInFliedIndices =
92
87
  this.replacePositionInFliedIndices.bind(this);
93
88
  this.replaceFurthestIndexPosition =
@@ -100,10 +95,6 @@ class IntegerBufferSet<Meta = any> {
100
95
  this._lastUpdatedMS = this._loopMS;
101
96
  }
102
97
 
103
- getSize() {
104
- return this._size;
105
- }
106
-
107
98
  get bufferSize() {
108
99
  return this._bufferSize;
109
100
  }
@@ -193,117 +184,6 @@ class IntegerBufferSet<Meta = any> {
193
184
  return this._largeValues.peek()?.value;
194
185
  }
195
186
 
196
- /**
197
- * values actually is the position of original data.
198
- */
199
- setValuePosition(value: number, position: number) {}
200
-
201
- findPositionMeta(position: number) {
202
- for (const [meta, pos] of this._metaToPositionMap) {
203
- if (pos === position) return meta;
204
- }
205
- return null;
206
- }
207
-
208
- rebuildHeapsWithMeta(metaToPositionMap: MetaToPositionMap<Meta>) {
209
- const { smallValues, largeValues } = this.initialize();
210
-
211
- for (const [meta, position] of metaToPositionMap) {
212
- const index = this.getMetaIndex(meta);
213
- const token = { index, position };
214
- smallValues.push(token);
215
- largeValues.push(token);
216
- }
217
-
218
- this._smallValues = smallValues;
219
- this._largeValues = largeValues;
220
- }
221
-
222
- /**
223
- *
224
- * @param position
225
- * @param value
226
- *
227
- *
228
- */
229
- setPositionIndex(position: number, index: number) {
230
- const meta = this._metaExtractor(index);
231
- const originalPosition = this._metaToPositionMap.get(meta);
232
-
233
- // current index has a position
234
- if (originalPosition !== undefined) {
235
- if (originalPosition === position) return true;
236
- this.deleteMetaIndex(meta);
237
- }
238
-
239
- const metaToReplace = this.findPositionMeta(position);
240
- if (metaToReplace) this._metaToPositionMap.delete(metaToReplace);
241
- this._metaToPositionMap.set(meta, position);
242
-
243
- this.rebuildHeapsWithMeta(this._metaToPositionMap);
244
- return true;
245
- }
246
-
247
- getMetaPosition(meta: Meta) {
248
- return this._metaToPositionMap.get(meta);
249
- }
250
-
251
- // performRangeUpdate(
252
- // startIndex: number,
253
- // endIndex: number,
254
- // safeRange: {
255
- // startIndex: number;
256
- // endIndex: number;
257
- // }
258
- // ) {
259
- // const _start = Math.max(startIndex, safeRange.startIndex);
260
- // const _end = Math.min(endIndex, safeRange.endIndex);
261
- // const primaryMetaList = [];
262
- // const secondaryMetaList = [];
263
- // const locationStartIndex = startIndex;
264
- // const targetIndices = new Array(this._bufferSize);
265
-
266
- // const _valueToPositionObject = {};
267
- // const _positionToValueObject = {};
268
-
269
- // const _valueToMetaObject = {};
270
- // const _metaToIndexMap = new Map();
271
-
272
- // for (let value = startIndex; value <= endIndex; value++) {
273
- // const meta = this._metaExtractor(value);
274
- // if (meta) {
275
- // const _i = value - locationStartIndex;
276
- // if (isClamped(value, safeRange.startIndex, safeRange.endIndex)) {
277
- // primaryMetaList[_i] = meta;
278
- // const targetIndex = this.getMetaPosition(meta);
279
- // if (isNumber(targetIndex)) {
280
- // targetIndices[targetIndex] = value;
281
- // _valueToPositionObject[value] = targetIndex;
282
- // _valueToMetaObject[value] = meta;
283
- // _metaToIndexMap.set(meta, value);
284
- // _positionToValueObject[targetIndex] = value;
285
- // }
286
- // } else {
287
- // secondaryMetaList[_i] = meta;
288
- // }
289
- // }
290
- // }
291
-
292
- // for (let idx = _start; idx <= _end; idx++) {
293
- // const meta = this._metaExtractor(idx);
294
- // if (_metaToIndexMap.get(meta) !== undefined) continue;
295
- // let p;
296
- // while (
297
- // (p =
298
- // targetIndices[
299
- // this.resolvePosition(safeRange.startIndex, safeRange.endIndex, idx)
300
- // ]) === undefined
301
- // ) {
302
- // targetIndices[p] = idx;
303
- // }
304
- // }
305
- // }
306
-
307
187
  replacePositionInFliedIndices(newIndex: number, safeRange: SafeRange) {
308
188
  const { startIndex, endIndex } = safeRange;
309
189
 
@@ -383,12 +263,10 @@ class IntegerBufferSet<Meta = any> {
383
263
  this.getNewPositionForIndex(newIndex)
384
264
  );
385
265
 
386
- // console.log('this. fly ', this._isOnTheFlyFull)
387
266
  if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
388
267
 
389
268
  let positionToReplace;
390
269
  const prevIndexMeta = this._indexToMetaMap.get(newIndex);
391
- // console.log('this. is ', this.isBufferFull, prevIndexMeta);
392
270
 
393
271
  // Index has already been stored, but we cant use its old position directly...
394
272
  // 1:index -> meta, meta may be reused later
@@ -445,7 +323,6 @@ class IntegerBufferSet<Meta = any> {
445
323
  const minValue = this._smallValues.peek()!.value;
446
324
  const maxValue = this._largeValues.peek()!.value;
447
325
 
448
- // console.log('mxa ', maxValue, minValue);
449
326
  let indexToReplace;
450
327
 
451
328
  if (!safeRange) {
@@ -506,12 +383,6 @@ class IntegerBufferSet<Meta = any> {
506
383
  indices[idx] = targetIndex;
507
384
  }
508
385
 
509
- // console.log(
510
- // 'position xxx ',
511
- // this._positionToMetaList,
512
- // this._onTheFlyIndices
513
- // );
514
-
515
386
  const _arr = new Array(indices.length);
516
387
  const _available = [];
517
388
  const indexToMetaMap = new Map();
@@ -562,8 +433,6 @@ class IntegerBufferSet<Meta = any> {
562
433
  }
563
434
  }
564
435
 
565
- // console.log('position ', positionToMetaList, largeValues.peek().value);
566
-
567
436
  this._positionToMetaList = positionToMetaList;
568
437
  this._smallValues = smallValues;
569
438
  this._largeValues = largeValues;
@@ -665,9 +534,6 @@ class IntegerBufferSet<Meta = any> {
665
534
 
666
535
  this._onTheFlyIndices = [];
667
536
  this._isOnTheFlyFull = false;
668
- const len = this._positionToMetaList.length;
669
-
670
- for (let index = 0; index < len; index++) {}
671
537
  }
672
538
 
673
539
  _cleanHeaps() {
@@ -689,73 +555,6 @@ class IntegerBufferSet<Meta = any> {
689
555
  this._recreateHeaps();
690
556
  }
691
557
  }
692
-
693
- rebuildHeapsWithValues(
694
- arr: Array<{
695
- position: number;
696
- value: number;
697
- }>
698
- ) {
699
- const valueToPositionObject = {};
700
- const newSmallValues = new Heap<HeapItem>([], this._smallerComparator);
701
- const newLargeValues = new Heap<HeapItem>([], this._greaterComparator);
702
-
703
- arr.forEach((element) => {
704
- const { position, value } = element;
705
- if (value !== undefined) {
706
- const element = {
707
- position,
708
- value,
709
- };
710
- newSmallValues.push(element);
711
- newLargeValues.push(element);
712
- valueToPositionObject[value] = position;
713
- }
714
- });
715
- const _arr = new Array(this._bufferSize).fill(2);
716
- Object.keys(valueToPositionObject).map(
717
- (key) => (_arr[valueToPositionObject[key]] = 1)
718
- );
719
- _arr.forEach((_i, position) => {
720
- if (_i === 2) {
721
- const value = Number.MAX_SAFE_INTEGER - position;
722
- const element = {
723
- position,
724
- value,
725
- };
726
-
727
- newSmallValues.push(element);
728
- newLargeValues.push(element);
729
- valueToPositionObject[value] = position;
730
- }
731
- });
732
- this._smallValues = newSmallValues;
733
- this._largeValues = newLargeValues;
734
- }
735
-
736
- // rebuildHeaps() {
737
- // const valueToPositionObject = {};
738
- // const newSmallValues = new Heap<HeapItem>([], this._smallerComparator);
739
- // const newLargeValues = new Heap<HeapItem>([], this._greaterComparator);
740
-
741
- // const keys = Object.keys(this._positionToValueObject);
742
- // for (let position = 0; position < keys.length; position++) {
743
- // const value = this._positionToValueObject[position];
744
- // if (value !== undefined) {
745
- // const element = {
746
- // position,
747
- // value,
748
- // };
749
- // valueToPositionObject[value] = position;
750
- // newSmallValues.push(element);
751
- // newLargeValues.push(element);
752
- // }
753
- // }
754
-
755
- // this._smallValues = newSmallValues;
756
- // this._largeValues = newLargeValues;
757
- // }
758
-
759
558
  _recreateHeaps() {
760
559
  const sourceHeap =
761
560
  this._smallValues.size() < this._largeValues.size()