@x-oasis/integer-buffer-set 0.1.12 → 0.1.13
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 +11 -1
- package/dist/integer-buffer-set.cjs.development.js +24 -2
- 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 +24 -2
- package/dist/integer-buffer-set.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +43 -2
package/dist/index.d.ts
CHANGED
|
@@ -15,7 +15,17 @@ declare class IntegerBufferSet {
|
|
|
15
15
|
getNewPositionForValue(value: number): number;
|
|
16
16
|
getMinValue(): number;
|
|
17
17
|
getMaxValue(): number;
|
|
18
|
-
replaceFurthestValuePosition(lowValue: number, highValue: number, newValue: number
|
|
18
|
+
replaceFurthestValuePosition(lowValue: number, highValue: number, newValue: number, useMinValueFn?: (options: {
|
|
19
|
+
safeRange: {
|
|
20
|
+
lowValue: number;
|
|
21
|
+
highValue: number;
|
|
22
|
+
};
|
|
23
|
+
bufferSetRange: {
|
|
24
|
+
maxValue: number;
|
|
25
|
+
minValue: number;
|
|
26
|
+
};
|
|
27
|
+
currentIndex: number;
|
|
28
|
+
}) => boolean): null | number;
|
|
19
29
|
_pushToHeaps(position: number, value: number): void;
|
|
20
30
|
_cleanHeaps(): void;
|
|
21
31
|
_recreateHeaps(): void;
|
|
@@ -38,6 +38,15 @@ function _toPropertyKey(arg) {
|
|
|
38
38
|
return typeof key === "symbol" ? key : String(key);
|
|
39
39
|
}
|
|
40
40
|
|
|
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;
|
|
49
|
+
};
|
|
41
50
|
var IntegerBufferSet = /*#__PURE__*/function () {
|
|
42
51
|
function IntegerBufferSet() {
|
|
43
52
|
this._valueToPositionMap = {};
|
|
@@ -77,7 +86,10 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
77
86
|
var _this$_largeValues$pe;
|
|
78
87
|
return (_this$_largeValues$pe = this._largeValues.peek()) == null ? void 0 : _this$_largeValues$pe.value;
|
|
79
88
|
};
|
|
80
|
-
_proto.replaceFurthestValuePosition = function replaceFurthestValuePosition(lowValue, highValue, newValue) {
|
|
89
|
+
_proto.replaceFurthestValuePosition = function replaceFurthestValuePosition(lowValue, highValue, newValue, useMinValueFn) {
|
|
90
|
+
if (useMinValueFn === void 0) {
|
|
91
|
+
useMinValueFn = defaultUseMinValueFn;
|
|
92
|
+
}
|
|
81
93
|
if (this._valueToPositionMap[newValue] !== undefined) {
|
|
82
94
|
console.warn("Shouldn't try to replace values with value already stored value in " + 'BufferSet');
|
|
83
95
|
}
|
|
@@ -91,7 +103,17 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
91
103
|
return null;
|
|
92
104
|
}
|
|
93
105
|
var valueToReplace;
|
|
94
|
-
if (
|
|
106
|
+
if (useMinValueFn({
|
|
107
|
+
safeRange: {
|
|
108
|
+
lowValue: lowValue,
|
|
109
|
+
highValue: highValue
|
|
110
|
+
},
|
|
111
|
+
bufferSetRange: {
|
|
112
|
+
minValue: minValue,
|
|
113
|
+
maxValue: maxValue
|
|
114
|
+
},
|
|
115
|
+
currentIndex: newValue
|
|
116
|
+
})) {
|
|
95
117
|
valueToReplace = minValue;
|
|
96
118
|
this._smallValues.pop();
|
|
97
119
|
} else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integer-buffer-set.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\nimport Heap from '@x-oasis/heap';\n\ntype HeapItem = {\n position: number;\n value: number;\n};\n\n// Data structure that allows to store values and assign positions to them\n// in a way to minimize changing positions of stored values when new ones are\n// added or when some values are replaced. Stored elements are alwasy assigned\n// a consecutive set of positoins startin from 0 up to count of elements less 1\n// Following actions can be executed\n// * get position assigned to given value (null if value is not stored)\n// * create new entry for new value and get assigned position back\n// * replace value that is furthest from specified value range with new value\n// and get it's position back\n// All operations take amortized log(n) time where n is number of elements in\n// the set.\nclass IntegerBufferSet {\n private _size: number;\n private _valueToPositionMap: {\n [key: string]: number;\n };\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n\n constructor() {\n this._valueToPositionMap = {};\n this._size = 0;\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForValue = this.getNewPositionForValue.bind(this);\n this.getValuePosition = this.getValuePosition.bind(this);\n this.getSize = this.getSize.bind(this);\n this.replaceFurthestValuePosition =\n this.replaceFurthestValuePosition.bind(this);\n }\n\n getSize() {\n return this._size;\n }\n\n get indices() {\n const indices = [];\n for (const key in this._valueToPositionMap) {\n const value = this._valueToPositionMap[key];\n indices[value] = key;\n }\n return indices;\n }\n\n getValuePosition(value: number): null | number {\n if (this._valueToPositionMap[value] === undefined) {\n return null;\n }\n return this._valueToPositionMap[value];\n }\n\n getNewPositionForValue(value: number) {\n if (this._valueToPositionMap[value] !== undefined) {\n console.warn(\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n }\n // invariant(\n // this._valueToPositionMap[value] === undefined,\n // \"Shouldn't try to find new position for value already stored in BufferSet\"\n // );\n const newPosition = this._size;\n this._size++;\n this._pushToHeaps(newPosition, value);\n this._valueToPositionMap[value] = newPosition;\n return newPosition;\n }\n\n getMinValue() {\n return this._smallValues.peek()?.value;\n }\n\n getMaxValue() {\n return this._largeValues.peek()?.value;\n }\n\n replaceFurthestValuePosition(\n lowValue: number,\n highValue: number,\n newValue: number\n ): null | number {\n if (this._valueToPositionMap[newValue] !== undefined) {\n console.warn(\n \"Shouldn't try to replace values with value already stored value in \" +\n 'BufferSet'\n );\n }\n // invariant(\n // this._valueToPositionMap[newValue] === undefined,\n // \"Shouldn't try to replace values with value already stored value in \" +\n // 'BufferSet'\n // );\n\n this._cleanHeaps();\n if (this._smallValues.empty() || this._largeValues.empty()) {\n // Threre are currently no values stored. We will have to create new\n // position for this value.\n return null;\n }\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n if (minValue >= lowValue && maxValue <= highValue) {\n // All values currently stored are necessary, we can't reuse any of them.\n return null;\n }\n\n let valueToReplace;\n if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n valueToReplace = minValue;\n this._smallValues.pop();\n } else {\n valueToReplace = maxValue;\n this._largeValues.pop();\n }\n const position = this._valueToPositionMap[valueToReplace];\n delete this._valueToPositionMap[valueToReplace];\n this._valueToPositionMap[newValue] = position;\n this._pushToHeaps(position, newValue);\n\n return position;\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = {\n position,\n value,\n };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._smallValues.push(element);\n this._largeValues.push(element);\n }\n\n _cleanHeaps() {\n // We not usually only remove object from one heap while moving value.\n // Here we make sure that there is no stale data on top of heaps.\n this._cleanHeap(this._smallValues);\n this._cleanHeap(this._largeValues);\n const minHeapSize = Math.min(\n this._smallValues.size(),\n this._largeValues.size()\n );\n const maxHeapSize = Math.max(\n this._smallValues.size(),\n this._largeValues.size()\n );\n if (maxHeapSize > 10 * minHeapSize) {\n // There are many old values in one of heaps. We need to get rid of them\n // to not use too avoid memory leaks\n this._recreateHeaps();\n }\n }\n\n _recreateHeaps() {\n const sourceHeap =\n this._smallValues.size() < this._largeValues.size()\n ? this._smallValues\n : this._largeValues;\n const newSmallValues = new Heap<HeapItem>(\n [], // Initial data in the heap\n this._smallerComparator\n );\n const newLargeValues = new Heap<HeapItem>(\n [], // Initial datat in the heap\n this._greaterComparator\n );\n while (!sourceHeap.empty()) {\n const element = sourceHeap.pop()!;\n // Push all stil valid elements to new heaps\n if (this._valueToPositionMap[element.value] !== undefined) {\n newSmallValues.push(element);\n newLargeValues.push(element);\n }\n }\n this._smallValues = newSmallValues;\n this._largeValues = newLargeValues;\n }\n\n _cleanHeap(heap: Heap<HeapItem>) {\n while (\n !heap.empty() &&\n this._valueToPositionMap[heap.peek()!.value] === undefined\n ) {\n heap.pop();\n }\n }\n\n _smallerComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value < rhs.value;\n }\n\n _greaterComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value > rhs.value;\n }\n}\n\nexport default IntegerBufferSet;\n"],"names":["IntegerBufferSet","_valueToPositionMap","_size","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForValue","bind","getValuePosition","getSize","replaceFurthestValuePosition","_proto","prototype","value","undefined","console","warn","newPosition","_pushToHeaps","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","lowValue","highValue","newValue","_cleanHeaps","empty","minValue","maxValue","valueToReplace","pop","position","element","push","_cleanHeap","minHeapSize","Math","min","size","maxHeapSize","max","_recreateHeaps","sourceHeap","newSmallValues","newLargeValues","heap","lhs","rhs","_createClass","key","get","indices"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACiC,IAkB3BA,gBAAgB;EAQpB,SAAAA;IACE,IAAI,CAACC,mBAAmB,GAAG,EAAE;IAC7B,IAAI,CAACC,KAAK,GAAG,CAAC;IACd,IAAI,CAACC,YAAY,GAAG,IAAIC,IAAI,CAAC,EAAE,EAAE,IAAI,CAACC,kBAAkB,CAAC;IACzD,IAAI,CAACC,YAAY,GAAG,IAAIF,IAAI,CAAC,EAAE,EAAE,IAAI,CAACG,kBAAkB,CAAC;IAEzD,IAAI,CAACC,sBAAsB,GAAG,IAAI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,GAAG,EAAG;MAEjC,IAAI,IAAI,CAACjC,mBAAmB,CAACmC,OAAO,CAACrB,KAAK,CAAC,KAAKC,SAAS,EAAE;QACzD+B,cAAc,CAACV,IAAI,CAACD,OAAO,CAAC;QAC5BY,cAAc,CAACX,IAAI,CAACD,OAAO,CAAC;;;IAGhC,IAAI,CAACjC,YAAY,GAAG4C,cAAc;IAClC,IAAI,CAACzC,YAAY,GAAG0C,cAAc;GACnC;EAAAnC,MAAA,CAEDyB,UAAU,GAAV,SAAAA,WAAWW,IAAoB;IAC7B,OACE,CAACA,IAAI,CAACnB,KAAK,EAAE,IACb,IAAI,CAAC7B,mBAAmB,CAACgD,IAAI,CAAC1B,IAAI,EAAG,CAACR,KAAK,CAAC,KAAKC,SAAS,EAC1D;MACAiC,IAAI,CAACf,GAAG,EAAE;;GAEb;EAAArB,MAAA,CAEDR,kBAAkB,GAAlB,SAAAA,mBAAmB6C,GAAa,EAAEC,GAAa;IAC7C,OAAOD,GAAG,CAACnC,KAAK,GAAGoC,GAAG,CAACpC,KAAK;GAC7B;EAAAF,MAAA,CAEDN,kBAAkB,GAAlB,SAAAA,mBAAmB2C,GAAa,EAAEC,GAAa;IAC7C,OAAOD,GAAG,CAACnC,KAAK,GAAGoC,GAAG,CAACpC,KAAK;GAC7B;EAAAqC,YAAA,CAAApD,gBAAA;IAAAqD,GAAA;IAAAC,GAAA,EA/JD,SAAAA;MACE,IAAMC,OAAO,GAAG,EAAE;MAClB,KAAK,IAAMF,GAAG,IAAI,IAAI,CAACpD,mBAAmB,EAAE;QAC1C,IAAMc,KAAK,GAAG,IAAI,CAACd,mBAAmB,CAACoD,GAAG,CAAC;QAC3CE,OAAO,CAACxC,KAAK,CAAC,GAAGsC,GAAG;;MAEtB,OAAOE,OAAO;;;EACf,OAAAvD,gBAAA;AAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"integer-buffer-set.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\nimport Heap from '@x-oasis/heap';\n\ntype HeapItem = {\n position: number;\n value: number;\n};\n\nconst defaultUseMinValueFn = (options: {\n safeRange: {\n lowValue: number;\n highValue: number;\n };\n bufferSetRange: {\n maxValue: number;\n minValue: number;\n };\n currentIndex: number;\n}) => {\n const { safeRange, bufferSetRange } = options;\n const { lowValue, highValue } = safeRange;\n const { maxValue, minValue } = bufferSetRange;\n return lowValue - minValue > maxValue - highValue;\n};\n\n// Data structure that allows to store values and assign positions to them\n// in a way to minimize changing positions of stored values when new ones are\n// added or when some values are replaced. Stored elements are alwasy assigned\n// a consecutive set of positoins startin from 0 up to count of elements less 1\n// Following actions can be executed\n// * get position assigned to given value (null if value is not stored)\n// * create new entry for new value and get assigned position back\n// * replace value that is furthest from specified value range with new value\n// and get it's position back\n// All operations take amortized log(n) time where n is number of elements in\n// the set.\nclass IntegerBufferSet {\n private _size: number;\n private _valueToPositionMap: {\n [key: string]: number;\n };\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n\n constructor() {\n this._valueToPositionMap = {};\n this._size = 0;\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForValue = this.getNewPositionForValue.bind(this);\n this.getValuePosition = this.getValuePosition.bind(this);\n this.getSize = this.getSize.bind(this);\n this.replaceFurthestValuePosition =\n this.replaceFurthestValuePosition.bind(this);\n }\n\n getSize() {\n return this._size;\n }\n\n get indices() {\n const indices = [];\n for (const key in this._valueToPositionMap) {\n const value = this._valueToPositionMap[key];\n indices[value] = key;\n }\n return indices;\n }\n\n getValuePosition(value: number): null | number {\n if (this._valueToPositionMap[value] === undefined) {\n return null;\n }\n return this._valueToPositionMap[value];\n }\n\n getNewPositionForValue(value: number) {\n if (this._valueToPositionMap[value] !== undefined) {\n console.warn(\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n }\n // invariant(\n // this._valueToPositionMap[value] === undefined,\n // \"Shouldn't try to find new position for value already stored in BufferSet\"\n // );\n const newPosition = this._size;\n this._size++;\n this._pushToHeaps(newPosition, value);\n this._valueToPositionMap[value] = newPosition;\n return newPosition;\n }\n\n getMinValue() {\n return this._smallValues.peek()?.value;\n }\n\n getMaxValue() {\n return this._largeValues.peek()?.value;\n }\n\n replaceFurthestValuePosition(\n lowValue: number,\n highValue: number,\n newValue: number,\n useMinValueFn: (options: {\n safeRange: {\n lowValue: number;\n highValue: number;\n };\n bufferSetRange: {\n maxValue: number;\n minValue: number;\n };\n currentIndex: number;\n }) => boolean = defaultUseMinValueFn\n ): null | number {\n if (this._valueToPositionMap[newValue] !== undefined) {\n console.warn(\n \"Shouldn't try to replace values with value already stored value in \" +\n 'BufferSet'\n );\n }\n // invariant(\n // this._valueToPositionMap[newValue] === undefined,\n // \"Shouldn't try to replace values with value already stored value in \" +\n // 'BufferSet'\n // );\n\n this._cleanHeaps();\n if (this._smallValues.empty() || this._largeValues.empty()) {\n // Threre are currently no values stored. We will have to create new\n // position for this value.\n return null;\n }\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n if (minValue >= lowValue && maxValue <= highValue) {\n // All values currently stored are necessary, we can't reuse any of them.\n return null;\n }\n\n let valueToReplace;\n if (\n useMinValueFn({\n safeRange: {\n lowValue,\n highValue,\n },\n bufferSetRange: {\n minValue,\n maxValue,\n },\n currentIndex: newValue,\n })\n ) {\n // if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n valueToReplace = minValue;\n this._smallValues.pop();\n } else {\n valueToReplace = maxValue;\n this._largeValues.pop();\n }\n const position = this._valueToPositionMap[valueToReplace];\n delete this._valueToPositionMap[valueToReplace];\n this._valueToPositionMap[newValue] = position;\n this._pushToHeaps(position, newValue);\n\n return position;\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = {\n position,\n value,\n };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._smallValues.push(element);\n this._largeValues.push(element);\n }\n\n _cleanHeaps() {\n // We not usually only remove object from one heap while moving value.\n // Here we make sure that there is no stale data on top of heaps.\n this._cleanHeap(this._smallValues);\n this._cleanHeap(this._largeValues);\n const minHeapSize = Math.min(\n this._smallValues.size(),\n this._largeValues.size()\n );\n const maxHeapSize = Math.max(\n this._smallValues.size(),\n this._largeValues.size()\n );\n if (maxHeapSize > 10 * minHeapSize) {\n // There are many old values in one of heaps. We need to get rid of them\n // to not use too avoid memory leaks\n this._recreateHeaps();\n }\n }\n\n _recreateHeaps() {\n const sourceHeap =\n this._smallValues.size() < this._largeValues.size()\n ? this._smallValues\n : this._largeValues;\n const newSmallValues = new Heap<HeapItem>(\n [], // Initial data in the heap\n this._smallerComparator\n );\n const newLargeValues = new Heap<HeapItem>(\n [], // Initial datat in the heap\n this._greaterComparator\n );\n while (!sourceHeap.empty()) {\n const element = sourceHeap.pop()!;\n // Push all stil valid elements to new heaps\n if (this._valueToPositionMap[element.value] !== undefined) {\n newSmallValues.push(element);\n newLargeValues.push(element);\n }\n }\n this._smallValues = newSmallValues;\n this._largeValues = newLargeValues;\n }\n\n _cleanHeap(heap: Heap<HeapItem>) {\n while (\n !heap.empty() &&\n this._valueToPositionMap[heap.peek()!.value] === undefined\n ) {\n heap.pop();\n }\n }\n\n _smallerComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value < rhs.value;\n }\n\n _greaterComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value > rhs.value;\n }\n}\n\nexport default IntegerBufferSet;\n"],"names":["defaultUseMinValueFn","options","safeRange","bufferSetRange","lowValue","highValue","maxValue","minValue","IntegerBufferSet","_valueToPositionMap","_size","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForValue","bind","getValuePosition","getSize","replaceFurthestValuePosition","_proto","prototype","value","undefined","console","warn","newPosition","_pushToHeaps","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","newValue","useMinValueFn","_cleanHeaps","empty","valueToReplace","currentIndex","pop","position","element","push","_cleanHeap","minHeapSize","Math","min","size","maxHeapSize","max","_recreateHeaps","sourceHeap","newSmallValues","newLargeValues","heap","lhs","rhs","_createClass","key","get","indices"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,IAAMA,oBAAoB,GAAG,SAAvBA,oBAAoBA,CAAIC,OAU7B;EACC,IAAQC,SAAS,GAAqBD,OAAO,CAArCC,SAAS;IAAEC,cAAc,GAAKF,OAAO,CAA1BE,cAAc;EACjC,IAAQC,QAAQ,GAAgBF,SAAS,CAAjCE,QAAQ;IAAEC,SAAS,GAAKH,SAAS,CAAvBG,SAAS;EAC3B,IAAQC,QAAQ,GAAeH,cAAc,CAArCG,QAAQ;IAAEC,QAAQ,GAAKJ,cAAc,CAA3BI,QAAQ;EAC1B,OAAOH,QAAQ,GAAGG,QAAQ,GAAGD,QAAQ,GAAGD,SAAS;AACnD,CAAC;AAAC,IAaIG,gBAAgB;EAQpB,SAAAA;IACE,IAAI,CAACC,mBAAmB,GAAG,EAAE;IAC7B,IAAI,CAACC,KAAK,GAAG,CAAC;IACd,IAAI,CAACC,YAAY,GAAG,IAAIC,IAAI,CAAC,EAAE,EAAE,IAAI,CAACC,kBAAkB,CAAC;IACzD,IAAI,CAACC,YAAY,GAAG,IAAIF,IAAI,CAAC,EAAE,EAAE,IAAI,CAACG,kBAAkB,CAAC;IAEzD,IAAI,CAACC,sBAAsB,GAAG,IAAI,CAACA,sBAAsB,CAACC,IAAI,CAAC,IAAI,CAAC;IACpE,IAAI,CAACC,gBAAgB,GAAG,IAAI,CAACA,gBAAgB,CAACD,IAAI,CAAC,IAAI,CAAC;IACxD,IAAI,CAACE,OAAO,GAAG,IAAI,CAACA,OAAO,CAACF,IAAI,CAAC,IAAI,CAAC;IACtC,IAAI,CAACG,4BAA4B,GAC/B,IAAI,CAACA,4BAA4B,CAACH,IAAI,CAAC,IAAI,CAAC;;EAC/C,IAAAI,MAAA,GAAAb,gBAAA,CAAAc,SAAA;EAAAD,MAAA,CAEDF,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,IAAI,CAAC/B,YAAY,GAAG0C,cAAc;IAClC,IAAI,CAACvC,YAAY,GAAGwC,cAAc;GACnC;EAAAjC,MAAA,CAEDuB,UAAU,GAAV,SAAAA,WAAWW,IAAoB;IAC7B,OACE,CAACA,IAAI,CAAClB,KAAK,EAAE,IACb,IAAI,CAAC5B,mBAAmB,CAAC8C,IAAI,CAACxB,IAAI,EAAG,CAACR,KAAK,CAAC,KAAKC,SAAS,EAC1D;MACA+B,IAAI,CAACf,GAAG,EAAE;;GAEb;EAAAnB,MAAA,CAEDR,kBAAkB,GAAlB,SAAAA,mBAAmB2C,GAAa,EAAEC,GAAa;IAC7C,OAAOD,GAAG,CAACjC,KAAK,GAAGkC,GAAG,CAAClC,KAAK;GAC7B;EAAAF,MAAA,CAEDN,kBAAkB,GAAlB,SAAAA,mBAAmByC,GAAa,EAAEC,GAAa;IAC7C,OAAOD,GAAG,CAACjC,KAAK,GAAGkC,GAAG,CAAClC,KAAK;GAC7B;EAAAmC,YAAA,CAAAlD,gBAAA;IAAAmD,GAAA;IAAAC,GAAA,EAvLD,SAAAA;MACE,IAAMC,OAAO,GAAG,EAAE;MAClB,KAAK,IAAMF,GAAG,IAAI,IAAI,CAAClD,mBAAmB,EAAE;QAC1C,IAAMc,KAAK,GAAG,IAAI,CAACd,mBAAmB,CAACkD,GAAG,CAAC;QAC3CE,OAAO,CAACtC,KAAK,CAAC,GAAGoC,GAAG;;MAEtB,OAAOE,OAAO;;;EACf,OAAArD,gBAAA;AAAA;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("@x-oasis/heap"))&&"object"==typeof e&&"default"in e?e.default:e;exports.default=function(){function e(){this._valueToPositionMap={},this._size=0,this._smallValues=new t([],this._smallerComparator),this._largeValues=new t([],this._greaterComparator),this.getNewPositionForValue=this.getNewPositionForValue.bind(this),this.getValuePosition=this.getValuePosition.bind(this),this.getSize=this.getSize.bind(this),this.replaceFurthestValuePosition=this.replaceFurthestValuePosition.bind(this)}var i,
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("@x-oasis/heap"))&&"object"==typeof e&&"default"in e?e.default:e,a=function(e){var t=e.safeRange,a=e.bufferSetRange;return t.lowValue-a.minValue>a.maxValue-t.highValue};exports.default=function(){function e(){this._valueToPositionMap={},this._size=0,this._smallValues=new t([],this._smallerComparator),this._largeValues=new t([],this._greaterComparator),this.getNewPositionForValue=this.getNewPositionForValue.bind(this),this.getValuePosition=this.getValuePosition.bind(this),this.getSize=this.getSize.bind(this),this.replaceFurthestValuePosition=this.replaceFurthestValuePosition.bind(this)}var i,s,l=e.prototype;return l.getSize=function(){return this._size},l.getValuePosition=function(e){return void 0===this._valueToPositionMap[e]?null:this._valueToPositionMap[e]},l.getNewPositionForValue=function(e){void 0!==this._valueToPositionMap[e]&&console.warn("Shouldn't try to find new position for value already stored in BufferSet");var t=this._size;return this._size++,this._pushToHeaps(t,e),this._valueToPositionMap[e]=t,t},l.getMinValue=function(){var e;return null==(e=this._smallValues.peek())?void 0:e.value},l.getMaxValue=function(){var e;return null==(e=this._largeValues.peek())?void 0:e.value},l.replaceFurthestValuePosition=function(e,t,i,s){if(void 0===s&&(s=a),void 0!==this._valueToPositionMap[i]&&console.warn("Shouldn't try to replace values with value already stored value in BufferSet"),this._cleanHeaps(),this._smallValues.empty()||this._largeValues.empty())return null;var l,o=this._smallValues.peek().value,r=this._largeValues.peek().value;if(o>=e&&r<=t)return null;s({safeRange:{lowValue:e,highValue:t},bufferSetRange:{minValue:o,maxValue:r},currentIndex:i})?(l=o,this._smallValues.pop()):(l=r,this._largeValues.pop());var u=this._valueToPositionMap[l];return delete this._valueToPositionMap[l],this._valueToPositionMap[i]=u,this._pushToHeaps(u,i),u},l._pushToHeaps=function(e,t){var a={position:e,value:t};this._smallValues.push(a),this._largeValues.push(a)},l._cleanHeaps=function(){this._cleanHeap(this._smallValues),this._cleanHeap(this._largeValues);var e=Math.min(this._smallValues.size(),this._largeValues.size());Math.max(this._smallValues.size(),this._largeValues.size())>10*e&&this._recreateHeaps()},l._recreateHeaps=function(){for(var e=this._smallValues.size()<this._largeValues.size()?this._smallValues:this._largeValues,a=new t([],this._smallerComparator),i=new t([],this._greaterComparator);!e.empty();){var s=e.pop();void 0!==this._valueToPositionMap[s.value]&&(a.push(s),i.push(s))}this._smallValues=a,this._largeValues=i},l._cleanHeap=function(e){for(;!e.empty()&&void 0===this._valueToPositionMap[e.peek().value];)e.pop()},l._smallerComparator=function(e,t){return e.value<t.value},l._greaterComparator=function(e,t){return e.value>t.value},i=e,(s=[{key:"indices",get:function(){var e=[];for(var t in this._valueToPositionMap)e[this._valueToPositionMap[t]]=t;return e}}])&&function(e,t){for(var a=0;a<t.length;a++){var i=t[a];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,"symbol"==typeof(s=function(e,t){if("object"!=typeof e||null===e)return e;var a=e[Symbol.toPrimitive];if(void 0!==a){var i=a.call(e,"string");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(i.key))?s:String(s),i)}var s}(i.prototype,s),Object.defineProperty(i,"prototype",{writable:!1}),e}();
|
|
2
2
|
//# sourceMappingURL=integer-buffer-set.cjs.production.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integer-buffer-set.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\nimport Heap from '@x-oasis/heap';\n\ntype HeapItem = {\n position: number;\n value: number;\n};\n\n// Data structure that allows to store values and assign positions to them\n// in a way to minimize changing positions of stored values when new ones are\n// added or when some values are replaced. Stored elements are alwasy assigned\n// a consecutive set of positoins startin from 0 up to count of elements less 1\n// Following actions can be executed\n// * get position assigned to given value (null if value is not stored)\n// * create new entry for new value and get assigned position back\n// * replace value that is furthest from specified value range with new value\n// and get it's position back\n// All operations take amortized log(n) time where n is number of elements in\n// the set.\nclass IntegerBufferSet {\n private _size: number;\n private _valueToPositionMap: {\n [key: string]: number;\n };\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n\n constructor() {\n this._valueToPositionMap = {};\n this._size = 0;\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForValue = this.getNewPositionForValue.bind(this);\n this.getValuePosition = this.getValuePosition.bind(this);\n this.getSize = this.getSize.bind(this);\n this.replaceFurthestValuePosition =\n this.replaceFurthestValuePosition.bind(this);\n }\n\n getSize() {\n return this._size;\n }\n\n get indices() {\n const indices = [];\n for (const key in this._valueToPositionMap) {\n const value = this._valueToPositionMap[key];\n indices[value] = key;\n }\n return indices;\n }\n\n getValuePosition(value: number): null | number {\n if (this._valueToPositionMap[value] === undefined) {\n return null;\n }\n return this._valueToPositionMap[value];\n }\n\n getNewPositionForValue(value: number) {\n if (this._valueToPositionMap[value] !== undefined) {\n console.warn(\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n }\n // invariant(\n // this._valueToPositionMap[value] === undefined,\n // \"Shouldn't try to find new position for value already stored in BufferSet\"\n // );\n const newPosition = this._size;\n this._size++;\n this._pushToHeaps(newPosition, value);\n this._valueToPositionMap[value] = newPosition;\n return newPosition;\n }\n\n getMinValue() {\n return this._smallValues.peek()?.value;\n }\n\n getMaxValue() {\n return this._largeValues.peek()?.value;\n }\n\n replaceFurthestValuePosition(\n lowValue: number,\n highValue: number,\n newValue: number\n ): null | number {\n if (this._valueToPositionMap[newValue] !== undefined) {\n console.warn(\n \"Shouldn't try to replace values with value already stored value in \" +\n 'BufferSet'\n );\n }\n // invariant(\n // this._valueToPositionMap[newValue] === undefined,\n // \"Shouldn't try to replace values with value already stored value in \" +\n // 'BufferSet'\n // );\n\n this._cleanHeaps();\n if (this._smallValues.empty() || this._largeValues.empty()) {\n // Threre are currently no values stored. We will have to create new\n // position for this value.\n return null;\n }\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n if (minValue >= lowValue && maxValue <= highValue) {\n // All values currently stored are necessary, we can't reuse any of them.\n return null;\n }\n\n let valueToReplace;\n if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n valueToReplace = minValue;\n this._smallValues.pop();\n } else {\n valueToReplace = maxValue;\n this._largeValues.pop();\n }\n const position = this._valueToPositionMap[valueToReplace];\n delete this._valueToPositionMap[valueToReplace];\n this._valueToPositionMap[newValue] = position;\n this._pushToHeaps(position, newValue);\n\n return position;\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = {\n position,\n value,\n };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._smallValues.push(element);\n this._largeValues.push(element);\n }\n\n _cleanHeaps() {\n // We not usually only remove object from one heap while moving value.\n // Here we make sure that there is no stale data on top of heaps.\n this._cleanHeap(this._smallValues);\n this._cleanHeap(this._largeValues);\n const minHeapSize = Math.min(\n this._smallValues.size(),\n this._largeValues.size()\n );\n const maxHeapSize = Math.max(\n this._smallValues.size(),\n this._largeValues.size()\n );\n if (maxHeapSize > 10 * minHeapSize) {\n // There are many old values in one of heaps. We need to get rid of them\n // to not use too avoid memory leaks\n this._recreateHeaps();\n }\n }\n\n _recreateHeaps() {\n const sourceHeap =\n this._smallValues.size() < this._largeValues.size()\n ? this._smallValues\n : this._largeValues;\n const newSmallValues = new Heap<HeapItem>(\n [], // Initial data in the heap\n this._smallerComparator\n );\n const newLargeValues = new Heap<HeapItem>(\n [], // Initial datat in the heap\n this._greaterComparator\n );\n while (!sourceHeap.empty()) {\n const element = sourceHeap.pop()!;\n // Push all stil valid elements to new heaps\n if (this._valueToPositionMap[element.value] !== undefined) {\n newSmallValues.push(element);\n newLargeValues.push(element);\n }\n }\n this._smallValues = newSmallValues;\n this._largeValues = newLargeValues;\n }\n\n _cleanHeap(heap: Heap<HeapItem>) {\n while (\n !heap.empty() &&\n this._valueToPositionMap[heap.peek()!.value] === undefined\n ) {\n heap.pop();\n }\n }\n\n _smallerComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value < rhs.value;\n }\n\n _greaterComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value > rhs.value;\n }\n}\n\nexport default IntegerBufferSet;\n"],"names":["IntegerBufferSet","this","_valueToPositionMap","_size","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForValue","bind","getValuePosition","getSize","replaceFurthestValuePosition","_proto","prototype","value","undefined","console","warn","newPosition","_pushToHeaps","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","lowValue","highValue","newValue","_cleanHeaps","empty","valueToReplace","minValue","maxValue","pop","position","element","push","_cleanHeap","minHeapSize","Math","min","size","max","_recreateHeaps","sourceHeap","newSmallValues","newLargeValues","heap","lhs","rhs","key","get","indices"],"mappings":"mLA2BE,SAAAA,IACEC,KAAKC,oBAAsB,GAC3BD,KAAKE,MAAQ,EACbF,KAAKG,aAAe,IAAIC,EAAK,GAAIJ,KAAKK,oBACtCL,KAAKM,aAAe,IAAIF,EAAK,GAAIJ,KAAKO,oBAEtCP,KAAKQ,uBAAyBR,KAAKQ,uBAAuBC,KAAKT,MAC/DA,KAAKU,iBAAmBV,KAAKU,iBAAiBD,KAAKT,MACnDA,KAAKW,QAAUX,KAAKW,QAAQF,KAAKT,MACjCA,KAAKY,6BACHZ,KAAKY,6BAA6BH,KAAKT,MAC1C,QAAAa,EAAAd,EAAAe,UAaA,OAbAD,EAEDF,QAAA,WACE,OAAOX,KAAKE,OACbW,EAWDH,iBAAA,SAAiBK,GACf,YAAwCC,IAApChB,KAAKC,oBAAoBc,GACpB,KAEFf,KAAKC,oBAAoBc,IACjCF,EAEDL,uBAAA,SAAuBO,QACmBC,IAApChB,KAAKC,oBAAoBc,IAC3BE,QAAQC,KACN,4EAOJ,IAAMC,EAAcnB,KAAKE,MAIzB,OAHAF,KAAKE,QACLF,KAAKoB,aAAaD,EAAaJ,GAC/B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AKM,aAAaoC,OACzC1C,KAAKG,aACLH,KAAKM,aACLwC,EAAiB,IAAI1C,EACzB,GACAJ,KAAKK,oBAED0C,EAAiB,IAAI3C,EACzB,GACAJ,KAAKO,qBAECsC,EAAWf,SAAS,CAC1B,IAAMM,EAAUS,EAAWX,WAEqBlB,IAA5ChB,KAAKC,oBAAoBmC,EAAQrB,SACnC+B,EAAeT,KAAKD,GACpBW,EAAeV,KAAKD,IAGxBpC,KAAKG,aAAe2C,EACpB9C,KAAKM,aAAeyC,GACrBlC,EAEDyB,WAAA,SAAWU,GACT,MACGA,EAAKlB,cAC2Cd,IAAjDhB,KAAKC,oBAAoB+C,EAAKzB,OAAQR,QAEtCiC,EAAKd,OAERrB,EAEDR,mBAAA,SAAmB4C,EAAeC,GAChC,OAAOD,EAAIlC,MAAQmC,EAAInC,OACxBF,EAEDN,mBAAA,SAAmB0C,EAAeC,GAChC,OAAOD,EAAIlC,MAAQmC,EAAInC,SACxBhB,OAAAoD,cAAAC,IA/JD,WACE,IAAMC,EAAU,GAChB,IAAK,IAAMF,KAAOnD,KAAKC,oBAErBoD,EADcrD,KAAKC,oBAAoBkD,IACtBA,EAEnB,OAAOE,ogBACRtD"}
|
|
1
|
+
{"version":3,"file":"integer-buffer-set.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\nimport Heap from '@x-oasis/heap';\n\ntype HeapItem = {\n position: number;\n value: number;\n};\n\nconst defaultUseMinValueFn = (options: {\n safeRange: {\n lowValue: number;\n highValue: number;\n };\n bufferSetRange: {\n maxValue: number;\n minValue: number;\n };\n currentIndex: number;\n}) => {\n const { safeRange, bufferSetRange } = options;\n const { lowValue, highValue } = safeRange;\n const { maxValue, minValue } = bufferSetRange;\n return lowValue - minValue > maxValue - highValue;\n};\n\n// Data structure that allows to store values and assign positions to them\n// in a way to minimize changing positions of stored values when new ones are\n// added or when some values are replaced. Stored elements are alwasy assigned\n// a consecutive set of positoins startin from 0 up to count of elements less 1\n// Following actions can be executed\n// * get position assigned to given value (null if value is not stored)\n// * create new entry for new value and get assigned position back\n// * replace value that is furthest from specified value range with new value\n// and get it's position back\n// All operations take amortized log(n) time where n is number of elements in\n// the set.\nclass IntegerBufferSet {\n private _size: number;\n private _valueToPositionMap: {\n [key: string]: number;\n };\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n\n constructor() {\n this._valueToPositionMap = {};\n this._size = 0;\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForValue = this.getNewPositionForValue.bind(this);\n this.getValuePosition = this.getValuePosition.bind(this);\n this.getSize = this.getSize.bind(this);\n this.replaceFurthestValuePosition =\n this.replaceFurthestValuePosition.bind(this);\n }\n\n getSize() {\n return this._size;\n }\n\n get indices() {\n const indices = [];\n for (const key in this._valueToPositionMap) {\n const value = this._valueToPositionMap[key];\n indices[value] = key;\n }\n return indices;\n }\n\n getValuePosition(value: number): null | number {\n if (this._valueToPositionMap[value] === undefined) {\n return null;\n }\n return this._valueToPositionMap[value];\n }\n\n getNewPositionForValue(value: number) {\n if (this._valueToPositionMap[value] !== undefined) {\n console.warn(\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n }\n // invariant(\n // this._valueToPositionMap[value] === undefined,\n // \"Shouldn't try to find new position for value already stored in BufferSet\"\n // );\n const newPosition = this._size;\n this._size++;\n this._pushToHeaps(newPosition, value);\n this._valueToPositionMap[value] = newPosition;\n return newPosition;\n }\n\n getMinValue() {\n return this._smallValues.peek()?.value;\n }\n\n getMaxValue() {\n return this._largeValues.peek()?.value;\n }\n\n replaceFurthestValuePosition(\n lowValue: number,\n highValue: number,\n newValue: number,\n useMinValueFn: (options: {\n safeRange: {\n lowValue: number;\n highValue: number;\n };\n bufferSetRange: {\n maxValue: number;\n minValue: number;\n };\n currentIndex: number;\n }) => boolean = defaultUseMinValueFn\n ): null | number {\n if (this._valueToPositionMap[newValue] !== undefined) {\n console.warn(\n \"Shouldn't try to replace values with value already stored value in \" +\n 'BufferSet'\n );\n }\n // invariant(\n // this._valueToPositionMap[newValue] === undefined,\n // \"Shouldn't try to replace values with value already stored value in \" +\n // 'BufferSet'\n // );\n\n this._cleanHeaps();\n if (this._smallValues.empty() || this._largeValues.empty()) {\n // Threre are currently no values stored. We will have to create new\n // position for this value.\n return null;\n }\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n if (minValue >= lowValue && maxValue <= highValue) {\n // All values currently stored are necessary, we can't reuse any of them.\n return null;\n }\n\n let valueToReplace;\n if (\n useMinValueFn({\n safeRange: {\n lowValue,\n highValue,\n },\n bufferSetRange: {\n minValue,\n maxValue,\n },\n currentIndex: newValue,\n })\n ) {\n // if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n valueToReplace = minValue;\n this._smallValues.pop();\n } else {\n valueToReplace = maxValue;\n this._largeValues.pop();\n }\n const position = this._valueToPositionMap[valueToReplace];\n delete this._valueToPositionMap[valueToReplace];\n this._valueToPositionMap[newValue] = position;\n this._pushToHeaps(position, newValue);\n\n return position;\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = {\n position,\n value,\n };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._smallValues.push(element);\n this._largeValues.push(element);\n }\n\n _cleanHeaps() {\n // We not usually only remove object from one heap while moving value.\n // Here we make sure that there is no stale data on top of heaps.\n this._cleanHeap(this._smallValues);\n this._cleanHeap(this._largeValues);\n const minHeapSize = Math.min(\n this._smallValues.size(),\n this._largeValues.size()\n );\n const maxHeapSize = Math.max(\n this._smallValues.size(),\n this._largeValues.size()\n );\n if (maxHeapSize > 10 * minHeapSize) {\n // There are many old values in one of heaps. We need to get rid of them\n // to not use too avoid memory leaks\n this._recreateHeaps();\n }\n }\n\n _recreateHeaps() {\n const sourceHeap =\n this._smallValues.size() < this._largeValues.size()\n ? this._smallValues\n : this._largeValues;\n const newSmallValues = new Heap<HeapItem>(\n [], // Initial data in the heap\n this._smallerComparator\n );\n const newLargeValues = new Heap<HeapItem>(\n [], // Initial datat in the heap\n this._greaterComparator\n );\n while (!sourceHeap.empty()) {\n const element = sourceHeap.pop()!;\n // Push all stil valid elements to new heaps\n if (this._valueToPositionMap[element.value] !== undefined) {\n newSmallValues.push(element);\n newLargeValues.push(element);\n }\n }\n this._smallValues = newSmallValues;\n this._largeValues = newLargeValues;\n }\n\n _cleanHeap(heap: Heap<HeapItem>) {\n while (\n !heap.empty() &&\n this._valueToPositionMap[heap.peek()!.value] === undefined\n ) {\n heap.pop();\n }\n }\n\n _smallerComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value < rhs.value;\n }\n\n _greaterComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value > rhs.value;\n }\n}\n\nexport default IntegerBufferSet;\n"],"names":["defaultUseMinValueFn","options","safeRange","bufferSetRange","lowValue","minValue","maxValue","highValue","IntegerBufferSet","this","_valueToPositionMap","_size","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForValue","bind","getValuePosition","getSize","replaceFurthestValuePosition","_proto","prototype","value","undefined","console","warn","newPosition","_pushToHeaps","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","newValue","useMinValueFn","_cleanHeaps","empty","valueToReplace","currentIndex","pop","position","element","push","_cleanHeap","minHeapSize","Math","min","size","max","_recreateHeaps","sourceHeap","newSmallValues","newLargeValues","heap","lhs","rhs","key","get","indices"],"mappings":"wJAQMA,EAAuB,SAACC,GAW5B,IAAQC,EAA8BD,EAA9BC,UAAWC,EAAmBF,EAAnBE,eAGnB,OAFgCD,EAAxBE,SACuBD,EAAbE,SAAaF,EAAvBG,SADwBJ,EAAdK,sCAwBlB,SAAAC,IACEC,KAAKC,oBAAsB,GAC3BD,KAAKE,MAAQ,EACbF,KAAKG,aAAe,IAAIC,EAAK,GAAIJ,KAAKK,oBACtCL,KAAKM,aAAe,IAAIF,EAAK,GAAIJ,KAAKO,o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,EAAIjC,SACxBhB,OAAAkD,cAAAC,IAvLD,WACE,IAAMC,EAAU,GAChB,IAAK,IAAMF,KAAOjD,KAAKC,oBAErBkD,EADcnD,KAAKC,oBAAoBgD,IACtBA,EAEnB,OAAOE,ogBACRpD"}
|
|
@@ -32,6 +32,15 @@ function _toPropertyKey(arg) {
|
|
|
32
32
|
return typeof key === "symbol" ? key : String(key);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
var defaultUseMinValueFn = function defaultUseMinValueFn(options) {
|
|
36
|
+
var safeRange = options.safeRange,
|
|
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;
|
|
43
|
+
};
|
|
35
44
|
var IntegerBufferSet = /*#__PURE__*/function () {
|
|
36
45
|
function IntegerBufferSet() {
|
|
37
46
|
this._valueToPositionMap = {};
|
|
@@ -71,7 +80,10 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
71
80
|
var _this$_largeValues$pe;
|
|
72
81
|
return (_this$_largeValues$pe = this._largeValues.peek()) == null ? void 0 : _this$_largeValues$pe.value;
|
|
73
82
|
};
|
|
74
|
-
_proto.replaceFurthestValuePosition = function replaceFurthestValuePosition(lowValue, highValue, newValue) {
|
|
83
|
+
_proto.replaceFurthestValuePosition = function replaceFurthestValuePosition(lowValue, highValue, newValue, useMinValueFn) {
|
|
84
|
+
if (useMinValueFn === void 0) {
|
|
85
|
+
useMinValueFn = defaultUseMinValueFn;
|
|
86
|
+
}
|
|
75
87
|
if (this._valueToPositionMap[newValue] !== undefined) {
|
|
76
88
|
console.warn("Shouldn't try to replace values with value already stored value in " + 'BufferSet');
|
|
77
89
|
}
|
|
@@ -85,7 +97,17 @@ var IntegerBufferSet = /*#__PURE__*/function () {
|
|
|
85
97
|
return null;
|
|
86
98
|
}
|
|
87
99
|
var valueToReplace;
|
|
88
|
-
if (
|
|
100
|
+
if (useMinValueFn({
|
|
101
|
+
safeRange: {
|
|
102
|
+
lowValue: lowValue,
|
|
103
|
+
highValue: highValue
|
|
104
|
+
},
|
|
105
|
+
bufferSetRange: {
|
|
106
|
+
minValue: minValue,
|
|
107
|
+
maxValue: maxValue
|
|
108
|
+
},
|
|
109
|
+
currentIndex: newValue
|
|
110
|
+
})) {
|
|
89
111
|
valueToReplace = minValue;
|
|
90
112
|
this._smallValues.pop();
|
|
91
113
|
} else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integer-buffer-set.esm.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\nimport Heap from '@x-oasis/heap';\n\ntype HeapItem = {\n position: number;\n value: number;\n};\n\n// Data structure that allows to store values and assign positions to them\n// in a way to minimize changing positions of stored values when new ones are\n// added or when some values are replaced. Stored elements are alwasy assigned\n// a consecutive set of positoins startin from 0 up to count of elements less 1\n// Following actions can be executed\n// * get position assigned to given value (null if value is not stored)\n// * create new entry for new value and get assigned position back\n// * replace value that is furthest from specified value range with new value\n// and get it's position back\n// All operations take amortized log(n) time where n is number of elements in\n// the set.\nclass IntegerBufferSet {\n private _size: number;\n private _valueToPositionMap: {\n [key: string]: number;\n };\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n\n constructor() {\n this._valueToPositionMap = {};\n this._size = 0;\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForValue = this.getNewPositionForValue.bind(this);\n this.getValuePosition = this.getValuePosition.bind(this);\n this.getSize = this.getSize.bind(this);\n this.replaceFurthestValuePosition =\n this.replaceFurthestValuePosition.bind(this);\n }\n\n getSize() {\n return this._size;\n }\n\n get indices() {\n const indices = [];\n for (const key in this._valueToPositionMap) {\n const value = this._valueToPositionMap[key];\n indices[value] = key;\n }\n return indices;\n }\n\n getValuePosition(value: number): null | number {\n if (this._valueToPositionMap[value] === undefined) {\n return null;\n }\n return this._valueToPositionMap[value];\n }\n\n getNewPositionForValue(value: number) {\n if (this._valueToPositionMap[value] !== undefined) {\n console.warn(\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n }\n // invariant(\n // this._valueToPositionMap[value] === undefined,\n // \"Shouldn't try to find new position for value already stored in BufferSet\"\n // );\n const newPosition = this._size;\n this._size++;\n this._pushToHeaps(newPosition, value);\n this._valueToPositionMap[value] = newPosition;\n return newPosition;\n }\n\n getMinValue() {\n return this._smallValues.peek()?.value;\n }\n\n getMaxValue() {\n return this._largeValues.peek()?.value;\n }\n\n replaceFurthestValuePosition(\n lowValue: number,\n highValue: number,\n newValue: number\n ): null | number {\n if (this._valueToPositionMap[newValue] !== undefined) {\n console.warn(\n \"Shouldn't try to replace values with value already stored value in \" +\n 'BufferSet'\n );\n }\n // invariant(\n // this._valueToPositionMap[newValue] === undefined,\n // \"Shouldn't try to replace values with value already stored value in \" +\n // 'BufferSet'\n // );\n\n this._cleanHeaps();\n if (this._smallValues.empty() || this._largeValues.empty()) {\n // Threre are currently no values stored. We will have to create new\n // position for this value.\n return null;\n }\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n if (minValue >= lowValue && maxValue <= highValue) {\n // All values currently stored are necessary, we can't reuse any of them.\n return null;\n }\n\n let valueToReplace;\n if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n valueToReplace = minValue;\n this._smallValues.pop();\n } else {\n valueToReplace = maxValue;\n this._largeValues.pop();\n }\n const position = this._valueToPositionMap[valueToReplace];\n delete this._valueToPositionMap[valueToReplace];\n this._valueToPositionMap[newValue] = position;\n this._pushToHeaps(position, newValue);\n\n return position;\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = {\n position,\n value,\n };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._smallValues.push(element);\n this._largeValues.push(element);\n }\n\n _cleanHeaps() {\n // We not usually only remove object from one heap while moving value.\n // Here we make sure that there is no stale data on top of heaps.\n this._cleanHeap(this._smallValues);\n this._cleanHeap(this._largeValues);\n const minHeapSize = Math.min(\n this._smallValues.size(),\n this._largeValues.size()\n );\n const maxHeapSize = Math.max(\n this._smallValues.size(),\n this._largeValues.size()\n );\n if (maxHeapSize > 10 * minHeapSize) {\n // There are many old values in one of heaps. We need to get rid of them\n // to not use too avoid memory leaks\n this._recreateHeaps();\n }\n }\n\n _recreateHeaps() {\n const sourceHeap =\n this._smallValues.size() < this._largeValues.size()\n ? this._smallValues\n : this._largeValues;\n const newSmallValues = new Heap<HeapItem>(\n [], // Initial data in the heap\n this._smallerComparator\n );\n const newLargeValues = new Heap<HeapItem>(\n [], // Initial datat in the heap\n this._greaterComparator\n );\n while (!sourceHeap.empty()) {\n const element = sourceHeap.pop()!;\n // Push all stil valid elements to new heaps\n if (this._valueToPositionMap[element.value] !== undefined) {\n newSmallValues.push(element);\n newLargeValues.push(element);\n }\n }\n this._smallValues = newSmallValues;\n this._largeValues = newLargeValues;\n }\n\n _cleanHeap(heap: Heap<HeapItem>) {\n while (\n !heap.empty() &&\n this._valueToPositionMap[heap.peek()!.value] === undefined\n ) {\n heap.pop();\n }\n }\n\n _smallerComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value < rhs.value;\n }\n\n _greaterComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value > rhs.value;\n }\n}\n\nexport default IntegerBufferSet;\n"],"names":["IntegerBufferSet","_valueToPositionMap","_size","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForValue","bind","getValuePosition","getSize","replaceFurthestValuePosition","_proto","prototype","value","undefined","console","warn","newPosition","_pushToHeaps","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","lowValue","highValue","newValue","_cleanHeaps","empty","minValue","maxValue","valueToReplace","pop","position","element","push","_cleanHeap","minHeapSize","Math","min","size","maxHeapSize","max","_recreateHeaps","sourceHeap","newSmallValues","newLargeValues","heap","lhs","rhs","_createClass","key","get","indices"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACiC,IAkB3BA,gBAAgB;EAQpB,SAAAA;IACE,IAAI,CAACC,mBAAmB,GAAG,EAAE;IAC7B,IAAI,CAACC,KAAK,GAAG,CAAC;IACd,IAAI,CAACC,YAAY,GAAG,IAAIC,IAAI,CAAC,EAAE,EAAE,IAAI,CAACC,kBAAkB,CAAC;IACzD,IAAI,CAACC,YAAY,GAAG,IAAIF,IAAI,CAAC,EAAE,EAAE,IAAI,CAACG,kBAAkB,CAAC;IAEzD,IAAI,CAACC,sBAAsB,GAAG,IAAI,CAACA,sBAAsB,CAACC,IAAI,CAAC,IAAI,CAAC;IACpE,IAAI,CAACC,gBAAgB,GAAG,IAAI,CAACA,gBAAgB,CAACD,IAAI,CAAC,IAAI,CAAC;IACxD,IAAI,CAACE,OAAO,GAAG,IAAI,CAACA,OAAO,CAACF,IAAI,CAAC,IAAI,CAAC;IACtC,IAAI,CAACG,4BAA4B,GAC/B,IAAI,CAACA,4BAA4B,CAACH,IAAI,CAAC,IAAI,CAAC;;EAC/C,IAAAI,MAAA,GAAAb,gBAAA,CAAAc,SAAA;EAAAD,MAAA,CAEDF,OAAO,GAAP,SAAAA;IACE,OAAO,IAAI,CAACT,KAAK;GAClB;EAAAW,MAAA,CAWDH,gBAAgB,GAAhB,SAAAA,iBAAiBK,KAAa;IAC5B,IAAI,IAAI,CAACd,mBAAmB,CAACc,KAAK,CAAC,KAAKC,SAAS,EAAE;MACjD,OAAO,IAAI;;IAEb,OAAO,IAAI,CAACf,mBAAmB,CAACc,KAAK,CAAC;GACvC;EAAAF,MAAA,CAEDL,sBAAsB,GAAtB,SAAAA,uBAAuBO,KAAa;IAClC,IAAI,IAAI,CAACd,mBAAmB,CAACc,KAAK,CAAC,KAAKC,SAAS,EAAE;MACjDC,OAAO,CAACC,IAAI,CACV,0EAA0E,C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AAK;GAC7B;EAAAqC,YAAA,CAAApD,gBAAA;IAAAqD,GAAA;IAAAC,GAAA,EA/JD,SAAAA;MACE,IAAMC,OAAO,GAAG,EAAE;MAClB,KAAK,IAAMF,GAAG,IAAI,IAAI,CAACpD,mBAAmB,EAAE;QAC1C,IAAMc,KAAK,GAAG,IAAI,CAACd,mBAAmB,CAACoD,GAAG,CAAC;QAC3CE,OAAO,CAACxC,KAAK,CAAC,GAAGsC,GAAG;;MAEtB,OAAOE,OAAO;;;EACf,OAAAvD,gBAAA;AAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"integer-buffer-set.esm.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\nimport Heap from '@x-oasis/heap';\n\ntype HeapItem = {\n position: number;\n value: number;\n};\n\nconst defaultUseMinValueFn = (options: {\n safeRange: {\n lowValue: number;\n highValue: number;\n };\n bufferSetRange: {\n maxValue: number;\n minValue: number;\n };\n currentIndex: number;\n}) => {\n const { safeRange, bufferSetRange } = options;\n const { lowValue, highValue } = safeRange;\n const { maxValue, minValue } = bufferSetRange;\n return lowValue - minValue > maxValue - highValue;\n};\n\n// Data structure that allows to store values and assign positions to them\n// in a way to minimize changing positions of stored values when new ones are\n// added or when some values are replaced. Stored elements are alwasy assigned\n// a consecutive set of positoins startin from 0 up to count of elements less 1\n// Following actions can be executed\n// * get position assigned to given value (null if value is not stored)\n// * create new entry for new value and get assigned position back\n// * replace value that is furthest from specified value range with new value\n// and get it's position back\n// All operations take amortized log(n) time where n is number of elements in\n// the set.\nclass IntegerBufferSet {\n private _size: number;\n private _valueToPositionMap: {\n [key: string]: number;\n };\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n\n constructor() {\n this._valueToPositionMap = {};\n this._size = 0;\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForValue = this.getNewPositionForValue.bind(this);\n this.getValuePosition = this.getValuePosition.bind(this);\n this.getSize = this.getSize.bind(this);\n this.replaceFurthestValuePosition =\n this.replaceFurthestValuePosition.bind(this);\n }\n\n getSize() {\n return this._size;\n }\n\n get indices() {\n const indices = [];\n for (const key in this._valueToPositionMap) {\n const value = this._valueToPositionMap[key];\n indices[value] = key;\n }\n return indices;\n }\n\n getValuePosition(value: number): null | number {\n if (this._valueToPositionMap[value] === undefined) {\n return null;\n }\n return this._valueToPositionMap[value];\n }\n\n getNewPositionForValue(value: number) {\n if (this._valueToPositionMap[value] !== undefined) {\n console.warn(\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n }\n // invariant(\n // this._valueToPositionMap[value] === undefined,\n // \"Shouldn't try to find new position for value already stored in BufferSet\"\n // );\n const newPosition = this._size;\n this._size++;\n this._pushToHeaps(newPosition, value);\n this._valueToPositionMap[value] = newPosition;\n return newPosition;\n }\n\n getMinValue() {\n return this._smallValues.peek()?.value;\n }\n\n getMaxValue() {\n return this._largeValues.peek()?.value;\n }\n\n replaceFurthestValuePosition(\n lowValue: number,\n highValue: number,\n newValue: number,\n useMinValueFn: (options: {\n safeRange: {\n lowValue: number;\n highValue: number;\n };\n bufferSetRange: {\n maxValue: number;\n minValue: number;\n };\n currentIndex: number;\n }) => boolean = defaultUseMinValueFn\n ): null | number {\n if (this._valueToPositionMap[newValue] !== undefined) {\n console.warn(\n \"Shouldn't try to replace values with value already stored value in \" +\n 'BufferSet'\n );\n }\n // invariant(\n // this._valueToPositionMap[newValue] === undefined,\n // \"Shouldn't try to replace values with value already stored value in \" +\n // 'BufferSet'\n // );\n\n this._cleanHeaps();\n if (this._smallValues.empty() || this._largeValues.empty()) {\n // Threre are currently no values stored. We will have to create new\n // position for this value.\n return null;\n }\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n if (minValue >= lowValue && maxValue <= highValue) {\n // All values currently stored are necessary, we can't reuse any of them.\n return null;\n }\n\n let valueToReplace;\n if (\n useMinValueFn({\n safeRange: {\n lowValue,\n highValue,\n },\n bufferSetRange: {\n minValue,\n maxValue,\n },\n currentIndex: newValue,\n })\n ) {\n // if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n valueToReplace = minValue;\n this._smallValues.pop();\n } else {\n valueToReplace = maxValue;\n this._largeValues.pop();\n }\n const position = this._valueToPositionMap[valueToReplace];\n delete this._valueToPositionMap[valueToReplace];\n this._valueToPositionMap[newValue] = position;\n this._pushToHeaps(position, newValue);\n\n return position;\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = {\n position,\n value,\n };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._smallValues.push(element);\n this._largeValues.push(element);\n }\n\n _cleanHeaps() {\n // We not usually only remove object from one heap while moving value.\n // Here we make sure that there is no stale data on top of heaps.\n this._cleanHeap(this._smallValues);\n this._cleanHeap(this._largeValues);\n const minHeapSize = Math.min(\n this._smallValues.size(),\n this._largeValues.size()\n );\n const maxHeapSize = Math.max(\n this._smallValues.size(),\n this._largeValues.size()\n );\n if (maxHeapSize > 10 * minHeapSize) {\n // There are many old values in one of heaps. We need to get rid of them\n // to not use too avoid memory leaks\n this._recreateHeaps();\n }\n }\n\n _recreateHeaps() {\n const sourceHeap =\n this._smallValues.size() < this._largeValues.size()\n ? this._smallValues\n : this._largeValues;\n const newSmallValues = new Heap<HeapItem>(\n [], // Initial data in the heap\n this._smallerComparator\n );\n const newLargeValues = new Heap<HeapItem>(\n [], // Initial datat in the heap\n this._greaterComparator\n );\n while (!sourceHeap.empty()) {\n const element = sourceHeap.pop()!;\n // Push all stil valid elements to new heaps\n if (this._valueToPositionMap[element.value] !== undefined) {\n newSmallValues.push(element);\n newLargeValues.push(element);\n }\n }\n this._smallValues = newSmallValues;\n this._largeValues = newLargeValues;\n }\n\n _cleanHeap(heap: Heap<HeapItem>) {\n while (\n !heap.empty() &&\n this._valueToPositionMap[heap.peek()!.value] === undefined\n ) {\n heap.pop();\n }\n }\n\n _smallerComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value < rhs.value;\n }\n\n _greaterComparator(lhs: HeapItem, rhs: HeapItem) {\n return lhs.value > rhs.value;\n }\n}\n\nexport default IntegerBufferSet;\n"],"names":["defaultUseMinValueFn","options","safeRange","bufferSetRange","lowValue","highValue","maxValue","minValue","IntegerBufferSet","_valueToPositionMap","_size","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForValue","bind","getValuePosition","getSize","replaceFurthestValuePosition","_proto","prototype","value","undefined","console","warn","newPosition","_pushToHeaps","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","newValue","useMinValueFn","_cleanHeaps","empty","valueToReplace","currentIndex","pop","position","element","push","_cleanHeap","minHeapSize","Math","min","size","maxHeapSize","max","_recreateHeaps","sourceHeap","newSmallValues","newLargeValues","heap","lhs","rhs","_createClass","key","get","indices"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,IAAMA,oBAAoB,GAAG,SAAvBA,oBAAoBA,CAAIC,OAU7B;EACC,IAAQC,SAAS,GAAqBD,OAAO,CAArCC,SAAS;IAAEC,cAAc,GAAKF,OAAO,CAA1BE,cAAc;EACjC,IAAQC,QAAQ,GAAgBF,SAAS,CAAjCE,QAAQ;IAAEC,SAAS,GAAKH,SAAS,CAAvBG,SAAS;EAC3B,IAAQC,QAAQ,GAAeH,cAAc,CAArCG,QAAQ;IAAEC,QAAQ,GAAKJ,cAAc,CAA3BI,QAAQ;EAC1B,OAAOH,QAAQ,GAAGG,QAAQ,GAAGD,QAAQ,GAAGD,SAAS;AACnD,CAAC;AAAC,IAaIG,gBAAgB;EAQpB,SAAAA;IACE,IAAI,CAACC,mBAAmB,GAAG,EAAE;IAC7B,IAAI,CAACC,KAAK,GAAG,CAAC;IACd,IAAI,CAACC,YAAY,GAAG,IAAIC,IAAI,CAAC,EAAE,EAAE,IAAI,CAACC,kBAAkB,CAAC;IACzD,IAAI,CAACC,YAAY,GAAG,IAAIF,IAAI,CAAC,EAAE,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ACd,mBAAmB,CAACkD,GAAG,CAAC;QAC3CE,OAAO,CAACtC,KAAK,CAAC,GAAGoC,GAAG;;MAEtB,OAAOE,OAAO;;;EACf,OAAArD,gBAAA;AAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-oasis/integer-buffer-set",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "IntegerBufferSet function",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"tsdx": "^0.14.1"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@x-oasis/heap": "0.1.
|
|
19
|
+
"@x-oasis/heap": "0.1.13"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsdx build --tsconfig tsconfig.build.json",
|
package/src/index.ts
CHANGED
|
@@ -6,6 +6,23 @@ type HeapItem = {
|
|
|
6
6
|
value: number;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
const defaultUseMinValueFn = (options: {
|
|
10
|
+
safeRange: {
|
|
11
|
+
lowValue: number;
|
|
12
|
+
highValue: number;
|
|
13
|
+
};
|
|
14
|
+
bufferSetRange: {
|
|
15
|
+
maxValue: number;
|
|
16
|
+
minValue: number;
|
|
17
|
+
};
|
|
18
|
+
currentIndex: number;
|
|
19
|
+
}) => {
|
|
20
|
+
const { safeRange, bufferSetRange } = options;
|
|
21
|
+
const { lowValue, highValue } = safeRange;
|
|
22
|
+
const { maxValue, minValue } = bufferSetRange;
|
|
23
|
+
return lowValue - minValue > maxValue - highValue;
|
|
24
|
+
};
|
|
25
|
+
|
|
9
26
|
// Data structure that allows to store values and assign positions to them
|
|
10
27
|
// in a way to minimize changing positions of stored values when new ones are
|
|
11
28
|
// added or when some values are replaced. Stored elements are alwasy assigned
|
|
@@ -86,7 +103,18 @@ class IntegerBufferSet {
|
|
|
86
103
|
replaceFurthestValuePosition(
|
|
87
104
|
lowValue: number,
|
|
88
105
|
highValue: number,
|
|
89
|
-
newValue: number
|
|
106
|
+
newValue: number,
|
|
107
|
+
useMinValueFn: (options: {
|
|
108
|
+
safeRange: {
|
|
109
|
+
lowValue: number;
|
|
110
|
+
highValue: number;
|
|
111
|
+
};
|
|
112
|
+
bufferSetRange: {
|
|
113
|
+
maxValue: number;
|
|
114
|
+
minValue: number;
|
|
115
|
+
};
|
|
116
|
+
currentIndex: number;
|
|
117
|
+
}) => boolean = defaultUseMinValueFn
|
|
90
118
|
): null | number {
|
|
91
119
|
if (this._valueToPositionMap[newValue] !== undefined) {
|
|
92
120
|
console.warn(
|
|
@@ -115,7 +143,20 @@ class IntegerBufferSet {
|
|
|
115
143
|
}
|
|
116
144
|
|
|
117
145
|
let valueToReplace;
|
|
118
|
-
if (
|
|
146
|
+
if (
|
|
147
|
+
useMinValueFn({
|
|
148
|
+
safeRange: {
|
|
149
|
+
lowValue,
|
|
150
|
+
highValue,
|
|
151
|
+
},
|
|
152
|
+
bufferSetRange: {
|
|
153
|
+
minValue,
|
|
154
|
+
maxValue,
|
|
155
|
+
},
|
|
156
|
+
currentIndex: newValue,
|
|
157
|
+
})
|
|
158
|
+
) {
|
|
159
|
+
// if (lowValue - minValue > maxValue - highValue) {
|
|
119
160
|
// minValue is further from provided range. We will reuse it's position.
|
|
120
161
|
valueToReplace = minValue;
|
|
121
162
|
this._smallValues.pop();
|