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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- "use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("@x-oasis/heap")),i=e(require("@x-oasis/is-clamped")),s=e(require("@x-oasis/invariant")),n=e(require("@x-oasis/return-hook")),a=function(e){return e};exports.default=function(){function e(e){void 0===e&&(e={});var i=e.name,s=void 0===i?"default_buffer":i,r=e.indexExtractor,o=e.bufferSize,l=void 0===o?10:o,h=e.metaExtractor;this._metaExtractor=void 0===h?a:h,this._indexExtractor=r,this._name=s,this._indexToMetaMap=new Map,this._metaToPositionMap=new Map,this._positionToMetaList=[],this._metaToIndexMap=new Map,this._onTheFlyIndices=[],this._bufferSize=l,this._smallValues=new t([],this._smallerComparator),this._largeValues=new t([],this._greaterComparator),this.getNewPositionForIndex=this.getNewPositionForIndex.bind(this),this.getIndexPosition=this.getIndexPosition.bind(this),this.replacePositionInFliedIndices=this.replacePositionInFliedIndices.bind(this),this.replaceFurthestIndexPosition=this.replaceFurthestIndexPosition.bind(this),this._isOnTheFlyFullReturnHook=n(this.setIsOnTheFlyFull.bind(this)),this._loopMS=Date.now(),this._lastUpdatedMS=this._loopMS}var r,o,l=e.prototype;return l.setIsOnTheFlyFull=function(e){if(null!=e){var t=this._onTheFlyIndices.filter((function(e){return e}));this._isOnTheFlyFull=t.length===this._bufferSize}},l.getOnTheFlyUncriticalPosition=function(e){for(var t=e.startIndex,s=e.endIndex,n=0;n<this._onTheFlyIndices.length;n++){var a=this._metaToIndexMap.get(this._onTheFlyIndices[n]);if(!i(t,a,s))return n}return null},l.initialize=function(){return{smallValues:new t([],this._smallerComparator),largeValues:new t([],this._greaterComparator),valueToPositionObject:{}}},l.getIndexMeta=function(e){return this._metaExtractor(e)},l.getMetaIndex=function(e){return this._indexExtractor?this._indexExtractor(e):this._metaToIndexMap.get(e)},l.setMetaIndex=function(e,t){return!this._indexExtractor&&this._metaToIndexMap.set(e,t)},l.deleteMetaIndex=function(e){return this._metaToIndexMap.delete(e)},l.replaceMetaToIndexMap=function(e){return!this._indexExtractor&&(this._metaToIndexMap=e)},l.getIndexPosition=function(e){return this.getMetaIndex(this.getIndexMeta(e))},l.getNewPositionForIndex=function(e){var t=this.getIndexMeta(e);void 0!==this._metaToPositionMap.get(t)&&s(!1);var i=this._positionToMetaList.length;return this._pushToHeaps(i,e),this._setMetaIndex(t,e),this._setMetaPosition(t,i),i},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.replacePositionInFliedIndices=function(e,t){if(this._isOnTheFlyFull){if(!i(t.startIndex,e,t.endIndex))return null;var s=this.getOnTheFlyUncriticalPosition(t);if(null!=s)return s}return null},l.getFliedPosition=function(e,t){var i=this.replacePositionInFliedIndices(e,t);if(null!=i){var s=this.getIndexMeta(e);return this._onTheFlyIndices[i]=s,this._setMetaIndex(s,e),this._isOnTheFlyFullReturnHook(i)}return null},l.getPosition=function(e,t){this.prepare();var i,s=this.getIndexMeta(e),n=this._metaToPositionMap.get(s);if(void 0!==n){var a=this._onTheFlyIndices[n];if(a){if(a===s)return n;var r=this._replaceFurthestIndexPosition(e,t);if(this._isOnTheFlyFull)return this.getFliedPosition(e,t);for(;this._onTheFlyIndices[r];)r=this._replaceFurthestIndexPosition(e,t);if(null!=r)return this._setMetaIndex(s,e),this._onTheFlyIndices[r]=a,this._isOnTheFlyFullReturnHook(r)}return this._onTheFlyIndices[n]=s,this._isOnTheFlyFullReturnHook(n)}if(!this.isBufferFull)return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(e));if(this._isOnTheFlyFull)return this.getFliedPosition(e,t);var o=this._indexToMetaMap.get(e);return o?i=this._metaToPositionMap.get(o):(this._cleanHeaps(),i=this._replaceFurthestIndexPosition(e,t)),this._onTheFlyIndices[i]=s,this._setMetaIndex(s,e),this._setMetaPosition(s,i),this._isOnTheFlyFullReturnHook(i)},l.replaceFurthestIndexPosition=function(e,t){return this.isBufferFull?this._replaceFurthestIndexPosition(e,t):this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(e))},l._replaceFurthestIndexPosition=function(e,t){if(this._largeValues.empty()||this._smallValues.empty())return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(e));var s,n=this._smallValues.peek().value,a=this._largeValues.peek().value;if(!t){Math.abs(e-n)>Math.abs(e-a)?(s=n,this._smallValues.pop()):(s=a,this._largeValues.pop());var r=this._indexToMetaMap.get(s);return this._metaToPositionMap.get(r)}var o=t.startIndex,l=t.endIndex;if(i(o,n,l)&&i(o,a,l))return null;i(o,n,l)&&!i(o,a,l)?(s=a,this._largeValues.pop()):!i(o,n,l)&&i(o,a,l)||o-n>a-l?(s=n,this._smallValues.pop()):(s=a,this._largeValues.pop());var h=this._indexToMetaMap.get(s);return this._metaToPositionMap.get(h)},l.shuffle=function(){for(var e=this,t=new Array(this.bufferSize),i=0;i<t.length;i++){var s=this.getMetaIndex(this._onTheFlyIndices[i]||this._positionToMetaList[i]);t[i]=s}for(var n=new Array(t.length),a=[],r=new Map,o=new Map,l=new Map,h=function(){var i=t[u],s=e._metaExtractor(i);if(null==s)return"continue";if(r.set(i,s),o.set(s,i),s===e._positionToMetaList[u])return n[u]=s,"continue";var l=e._positionToMetaList.findIndex((function(e){return e===s}));if(-1!==l)return n[l]=s,"continue";a.push(s)},u=0;u<t.length;u++)h();for(var _=this.initialize(),p=_.smallValues,d=_.largeValues,f=[],c=0;c<t.length;c++){var M=t[c];if(null==n[c]){var x=a.shift();if(null!=x){f[c]=x,l.set(x,c);var g={position:c,value:M};p.push(g),d.push(g)}}else{f[c]=n[c],l.set(n[c],c);var I={position:c,value:M};p.push(I),d.push(I)}}this._positionToMetaList=f,this._smallValues=p,this._largeValues=d,this._indexToMetaMap=r,this.replaceMetaToIndexMap(o),this._metaToPositionMap=l,this._onTheFlyIndices=[];try{for(var T=new Array(this.bufferSize),v=0;v<T.length;v++){var F=this._onTheFlyIndices[v]||this._positionToMetaList[v],m=this.getMetaIndex(F);null!=F&&(T[v]={meta:F,targetIndex:m,recyclerKey:this._name+"_"+v})}return T}catch(e){return this.readyToStartNextLoop(),this._positionToMetaList}},l.getIndices=function(){try{for(var e=new Array(this.bufferSize),t=0;t<e.length;t++){var i=this._onTheFlyIndices[t]||this._positionToMetaList[t],s=this.getMetaIndex(i);if(i!==this.getIndexMeta(s))return this.shuffle();null!=i&&(e[t]={meta:i,targetIndex:s,recyclerKey:this._name+"_"+t})}return this._onTheFlyIndices=[],e}catch(e){return this.readyToStartNextLoop(),this._positionToMetaList}},l._pushToHeaps=function(e,t){var i={position:e,value:t};this._smallValues.push(i),this._largeValues.push(i)},l._setMetaPosition=function(e,t){var i=this._positionToMetaList[t];i&&this._metaToPositionMap.delete(i),this._positionToMetaList[t]=e,this._metaToPositionMap.set(e,t)},l._setMetaIndex=function(e,t){var i=this.getMetaIndex(e);return void 0!==i&&this._indexToMetaMap.delete(i),this.setMetaIndex(e,t),this._indexToMetaMap.set(t,e),!1},l.readyToStartNextLoop=function(){this._lastUpdatedMS=Date.now()},l.prepare=function(){this._loopMS!==this._lastUpdatedMS&&(this._loopMS=this._lastUpdatedMS,this._onTheFlyIndices=[],this._isOnTheFlyFull=!1)},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,i=new t([],this._smallerComparator),s=new t([],this._greaterComparator);!e.empty();){var n=e.pop();null!=this._metaToPositionMap.get(this._indexToMetaMap.get(n.value))&&(i.push(n),s.push(n))}this._smallValues=i,this._largeValues=s},l._cleanHeap=function(e){for(;!e.empty()&&null==this._metaToPositionMap.get(this._indexToMetaMap.get(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},r=e,(o=[{key:"bufferSize",get:function(){return this._bufferSize}},{key:"isBufferFull",get:function(){return this._positionToMetaList.length>=this._bufferSize}}])&&function(e,t){for(var i=0;i<t.length;i++){var s=t[i];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(e,"symbol"==typeof(n=function(e,t){if("object"!=typeof e||null===e)return e;var i=e[Symbol.toPrimitive];if(void 0!==i){var s=i.call(e,"string");if("object"!=typeof s)return s;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(s.key))?n:String(n),s)}var n}(r.prototype,o),Object.defineProperty(r,"prototype",{writable:!1}),e}(),exports.defaultBufferSize=10;
1
+ "use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=t(require("@x-oasis/heap")),i=t(require("@x-oasis/is-clamped")),s=t(require("@x-oasis/invariant")),n=t(require("@x-oasis/return-hook")),o=function(t){return t},a=Number.MAX_SAFE_INTEGER-1e5;exports.default=function(){function t(t){void 0===t&&(t={});var i=t.name,s=void 0===i?"default_buffer":i,a=t.indexExtractor,r=t.bufferSize,l=void 0===r?10:r,h=t.metaExtractor;this._metaExtractor=void 0===h?o:h,this._indexExtractor=a,this._name=s,this._indexToMetaMap=new Map,this._metaToPositionMap=new Map,this._positionToMetaList=[],this._metaToIndexMap=new Map,this._onTheFlyIndices=[],this._bufferSize=l,this._smallValues=new e([],this._smallerComparator),this._largeValues=new e([],this._greaterComparator),this.getNewPositionForIndex=this.getNewPositionForIndex.bind(this),this.getIndexPosition=this.getIndexPosition.bind(this),this.replaceFurthestIndexPosition=this.replaceFurthestIndexPosition.bind(this),this._isOnTheFlyFullReturnHook=n(this.setIsOnTheFlyFull.bind(this)),this._loopMS=Date.now(),this._lastUpdatedMS=this._loopMS}var r,l,h=t.prototype;return h.isThresholdMeta=function(t){return"number"==typeof t&&t>a},h.setIsOnTheFlyFull=function(t){if(null!=t){var e=this._onTheFlyIndices.filter((function(t){return null!=t}));this._isOnTheFlyFull=e.length===this._bufferSize}},h.resetOnTheFlies=function(){this._isOnTheFlyFull=!1,this._onTheFlyIndices=[]},h.getOnTheFlyUncriticalPosition=function(t){for(var e=t.startIndex,s=t.endIndex,n=0;n<this._onTheFlyIndices.length;n++){var o=this.getMetaIndex(this._onTheFlyIndices[n]);if(!i(e,o,s))return n}return null},h.initialize=function(){return{smallValues:new e([],this._smallerComparator),largeValues:new e([],this._greaterComparator)}},h.getIndexMeta=function(t){return null==t||t<0?null:this._metaExtractor(t)},h.getMetaIndex=function(t){return null==t||this.isThresholdMeta(t)?-1:this._indexExtractor?this._indexExtractor(t):this._metaToIndexMap.get(t)},h.setMetaIndex=function(t,e){return!this._indexExtractor&&this._metaToIndexMap.set(t,e)},h.deleteMetaIndex=function(t){return this._metaToIndexMap.delete(t)},h.replaceMetaToIndexMap=function(t){return!this._indexExtractor&&(this._metaToIndexMap=t)},h.getIndexPosition=function(t){return this.getMetaIndex(this.getIndexMeta(t))},h.getNewPositionForIndex=function(t){var e=this.getIndexMeta(t);void 0!==this._metaToPositionMap.get(e)&&s(!1);var i=this._positionToMetaList.length;return this._pushToHeaps(i,t),this._setMetaIndex(e,t),this._setMetaPosition(e,i),i},h.getMinValue=function(){var t;return null==(t=this._smallValues.peek())?void 0:t.value},h.getMaxValue=function(){var t;return null==(t=this._largeValues.peek())?void 0:t.value},h.getFliedPosition=function(t,e){return this._isOnTheFlyFull&&e&&i(e.startIndex,t,e.endIndex)?this.getOnTheFlyUncriticalPosition(e):null},h.getPosition=function(t,e){this.prepare();var i,s,n=this.getIndexMeta(t),o=this._metaToPositionMap.get(n);return void 0!==o?i=this.commitPosition({newIndex:t,meta:n,safeRange:e,position:o}):this.isBufferFull?this._isOnTheFlyFull?i=this.getFliedPosition(t,e):(s=this._indexToMetaMap.get(t))&&this._metaToPositionMap.get(s)?i=this.commitPosition({newIndex:t,meta:n,safeRange:e,position:this._metaToPositionMap.get(s)}):(this._cleanHeaps(),i=this.commitPosition({newIndex:t,meta:n,safeRange:e,position:this._replaceFurthestIndexPosition(t,e)})):i=this.getNewPositionForIndex(t),null!=i?(this._onTheFlyIndices[i]=n,this._setMetaIndex(n,t),this._metaToPositionMap.set(n,i),this._isOnTheFlyFullReturnHook(i)):null},h.replaceFurthestIndexPosition=function(t,e){return this.isBufferFull?this._replaceFurthestIndexPosition(t,e):this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(t))},h._replaceFurthestIndexPosition=function(t,e){if(this._largeValues.empty()||this._smallValues.empty())return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(t));var s,n=this._smallValues.peek().value,o=this._largeValues.peek().value;if(o>a){s=o,this._largeValues.pop();var r=this._indexToMetaMap.get(s);return this._metaToPositionMap.get(r)}if(!e){Math.abs(t-n)>Math.abs(t-o)?(s=n,this._smallValues.pop()):(s=o,this._largeValues.pop());var l=this._indexToMetaMap.get(s);return this._metaToPositionMap.get(l)}var h=e.startIndex,u=e.endIndex;if(i(h,n,u)&&i(h,o,u))return null;i(h,n,u)&&!i(h,o,u)?(s=o,this._largeValues.pop()):!i(h,n,u)&&i(h,o,u)||h-n>o-u?(s=n,this._smallValues.pop()):(s=o,this._largeValues.pop());var p=this._indexToMetaMap.get(s);return this._metaToPositionMap.get(p)},h.shuffle=function(){for(var t=this,e=new Array(this.bufferSize),i=0;i<e.length;i++){var s=this.getMetaIndex(this._onTheFlyIndices[i]||this._positionToMetaList[i]);e[i]=s}for(var n=new Array(e.length),o=[],a=new Map,r=new Map,l=function(){var i=e[h],s=t._metaExtractor(i);if(null==s)return"continue";if(a.set(i,s),r.set(s,i),s===t._positionToMetaList[h])return n[h]=s,"continue";var l=t._positionToMetaList.findIndex((function(t){return t===s}));if(-1!==l)return n[l]=s,"continue";o.push(s)},h=0;h<e.length;h++)l();var u=[];this._indexToMetaMap=a,this.replaceMetaToIndexMap(r);for(var p=0;p<e.length;p++)if(null==n[p]){var _=o.shift();null!=_&&(u[p]=_)}else u[p]=n[p];return this._positionToMetaList=u,this.getIndices()},h.getIndices=function(){var t=this.initialize(),e=t.smallValues,i=t.largeValues;try{for(var s=new Array(this._positionToMetaList.length),n=new Map,o=0;o<s.length;o++){var a=this._onTheFlyIndices[o]||this._positionToMetaList[o],r=this.getMetaIndex(a);if(!this.isThresholdMeta(a)&&a!=this.getIndexMeta(r))return this.shuffle();if(null!=a&&!this.isThresholdMeta(a)){var l={position:o,value:r};e.push(l),i.push(l),n.set(a,o),s[o]={meta:a,targetIndex:r,recyclerKey:this._name+"_"+o}}}return this._smallValues=e,this._largeValues=i,this._metaToPositionMap=n,this._positionToMetaList=s.map((function(t){return null==t?void 0:t.meta})),this.resetOnTheFlies(),s}catch(t){return console.log("err ",t),this._positionToMetaList}finally{this.readyToStartNextLoop()}},h._pushToHeaps=function(t,e){var i={position:t,value:e};this._smallValues.push(i),this._largeValues.push(i)},h._setMetaPosition=function(t,e){this._positionToMetaList[e]=t,this._metaToPositionMap.set(t,e)},h.commitPosition=function(t){var e=t.newIndex,i=t.safeRange,s=t.position,n=this._onTheFlyIndices[s],o=s;if(n){if(n===t.meta)return s;if(this._isOnTheFlyFull)return this.getFliedPosition(e,i);for(o=this._replaceFurthestIndexPosition(e,i);this._onTheFlyIndices[o];)o=this._replaceFurthestIndexPosition(e,i)}return o},h._setMetaIndex=function(t,e){var i=this.getMetaIndex(t);return void 0!==i&&this._indexToMetaMap.delete(i),this.setMetaIndex(t,e),this._indexToMetaMap.set(e,t),!1},h.readyToStartNextLoop=function(){this._lastUpdatedMS=Date.now()},h.prepare=function(){this._loopMS!==this._lastUpdatedMS&&(this._loopMS=this._lastUpdatedMS,this._onTheFlyIndices=[],this._isOnTheFlyFull=!1)},h._cleanHeaps=function(){for(var t=0;t<this._positionToMetaList.length;t++)if(null==this._positionToMetaList[t])return void this._recreateHeaps();var e=Math.min(this._smallValues.size(),this._largeValues.size());Math.max(this._smallValues.size(),this._largeValues.size())>10*e&&this._recreateHeaps()},h._recreateHeaps=function(){for(var t=this.initialize(),e=t.smallValues,i=t.largeValues,s=0;s<this._positionToMetaList.length;s++){var n=this._positionToMetaList[s],o=this.getMetaIndex(n);n&&-1!==o&&null!=o||(o=Number.MAX_SAFE_INTEGER-s);var r={position:s,value:o};e.push(r),i.push(r),o>a&&(this._setMetaPosition(o,s),this._setMetaIndex(o,o))}this._largeValues.peek(),this._smallValues=e,this._largeValues=i},h._smallerComparator=function(t,e){return t.value<e.value},h._greaterComparator=function(t,e){return t.value>e.value},r=t,(l=[{key:"bufferSize",get:function(){return this._bufferSize}},{key:"isBufferFull",get:function(){return this._positionToMetaList.length>=this._bufferSize}}])&&function(t,e){for(var i=0;i<e.length;i++){var s=e[i];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,"symbol"==typeof(n=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var s=i.call(t,"string");if("object"!=typeof s)return s;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(s.key))?n:String(n),s)}var n}(r.prototype,l),Object.defineProperty(r,"prototype",{writable:!1}),t}(),exports.defaultBufferSize=10;
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 Heap from '@x-oasis/heap';\nimport isClamped from '@x-oasis/is-clamped';\nimport invariant from '@x-oasis/invariant';\nimport returnHook, { ReturnHook } from '@x-oasis/return-hook';\nimport {\n HeapItem,\n SafeRange,\n MetaExtractor,\n IndexExtractor,\n IntegerBufferSetProps,\n MetaToIndexMap,\n MetaToPositionMap,\n IndexToMetaMap,\n} from './types';\n\nconst defaultMetaExtractor = (value) => value;\nexport const defaultBufferSize = 10;\n\n// !!!!! should do meta validation...meta should has an index...\n// value: original data `index` value\n// value(index) => meta => position\n// `index to getIndices, meta to find index`\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.\n// feature: add / delete / update item will also in consider..\nclass IntegerBufferSet<Meta = any> {\n private _name: string;\n private _bufferSize: number;\n\n private _indexToMetaMap: IndexToMetaMap<Meta>;\n private _metaToPositionMap: MetaToPositionMap<Meta>;\n private _positionToMetaList: Array<Meta>;\n private _metaToIndexMap: MetaToIndexMap<Meta>;\n\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n private _metaExtractor: MetaExtractor<Meta>;\n private _indexExtractor: IndexExtractor<Meta>;\n\n private _onTheFlyIndices: Array<Meta>;\n\n private _isOnTheFlyFull: boolean;\n private _isOnTheFlyFullReturnHook: ReturnHook;\n\n private _loopMS: number;\n private _lastUpdatedMS: number;\n\n constructor(props: IntegerBufferSetProps<Meta> = {}) {\n const {\n name = 'default_buffer',\n indexExtractor,\n bufferSize = defaultBufferSize,\n metaExtractor = defaultMetaExtractor,\n } = props;\n this._metaExtractor = metaExtractor;\n this._indexExtractor = indexExtractor;\n\n this._name = name;\n\n /**\n * this._indexToMetaMap is used to find the prev meta when finding a position for index.\n */\n this._indexToMetaMap = new Map();\n this._metaToPositionMap = new Map();\n this._positionToMetaList = [];\n this._metaToIndexMap = new Map();\n this._onTheFlyIndices = [];\n\n this._bufferSize = bufferSize;\n\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForIndex = this.getNewPositionForIndex.bind(this);\n this.getIndexPosition = this.getIndexPosition.bind(this);\n this.replacePositionInFliedIndices =\n this.replacePositionInFliedIndices.bind(this);\n this.replaceFurthestIndexPosition =\n this.replaceFurthestIndexPosition.bind(this);\n this._isOnTheFlyFullReturnHook = returnHook(\n this.setIsOnTheFlyFull.bind(this)\n );\n\n this._loopMS = Date.now();\n this._lastUpdatedMS = this._loopMS;\n }\n\n get bufferSize() {\n return this._bufferSize;\n }\n\n setIsOnTheFlyFull(val: any) {\n if (val != null) {\n const data = this._onTheFlyIndices.filter((v) => v);\n this._isOnTheFlyFull = data.length === this._bufferSize;\n }\n }\n\n get isBufferFull() {\n return this._positionToMetaList.length >= this._bufferSize;\n }\n\n getOnTheFlyUncriticalPosition(safeRange: SafeRange) {\n const { startIndex, endIndex } = safeRange;\n for (let idx = 0; idx < this._onTheFlyIndices.length; idx++) {\n const meta = this._onTheFlyIndices[idx];\n const metaIndex = this._metaToIndexMap.get(meta);\n if (!isClamped(startIndex, metaIndex, endIndex)) {\n return idx;\n }\n }\n return null;\n }\n\n initialize() {\n return {\n smallValues: new Heap([], this._smallerComparator),\n largeValues: new Heap([], this._greaterComparator),\n valueToPositionObject: {},\n };\n }\n\n getIndexMeta(index: number) {\n return this._metaExtractor(index);\n }\n\n getMetaIndex(meta: Meta) {\n if (this._indexExtractor) return this._indexExtractor(meta);\n return this._metaToIndexMap.get(meta);\n }\n\n setMetaIndex(meta: Meta, index: number) {\n if (!this._indexExtractor) {\n return this._metaToIndexMap.set(meta, index);\n }\n return false;\n }\n\n deleteMetaIndex(meta: Meta) {\n return this._metaToIndexMap.delete(meta);\n }\n\n replaceMetaToIndexMap(newMetaToIndexMap: MetaToIndexMap<Meta>) {\n if (!this._indexExtractor) {\n return (this._metaToIndexMap = newMetaToIndexMap);\n }\n return false;\n }\n\n getIndexPosition(index: number): undefined | number {\n return this.getMetaIndex(this.getIndexMeta(index));\n }\n\n getNewPositionForIndex(index: number) {\n const meta = this.getIndexMeta(index);\n invariant(\n this._metaToPositionMap.get(meta) === undefined,\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n const newPosition = this._positionToMetaList.length;\n\n this._pushToHeaps(newPosition, index);\n this._setMetaIndex(meta, index);\n this._setMetaPosition(meta, newPosition);\n\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 replacePositionInFliedIndices(newIndex: number, safeRange: SafeRange) {\n const { startIndex, endIndex } = safeRange;\n\n if (this._isOnTheFlyFull) {\n // newIndex is not critical index, do nothing\n if (!isClamped(startIndex, newIndex, endIndex)) {\n return null;\n }\n // if `newIndex` is critical index, replace an un-committed\n // index value from _onTheFlyIndices.\n const pos = this.getOnTheFlyUncriticalPosition(safeRange);\n if (pos != null) return pos;\n }\n return null;\n }\n\n getFliedPosition(newIndex: number, safeRange: SafeRange) {\n const pos = this.replacePositionInFliedIndices(newIndex, safeRange);\n if (pos != null) {\n const meta = this.getIndexMeta(newIndex);\n this._onTheFlyIndices[pos] = meta;\n this._setMetaIndex(meta, newIndex);\n return this._isOnTheFlyFullReturnHook(pos);\n }\n return null;\n }\n\n /**\n *\n * @param newIndex\n * @param safeRange\n * @returns\n *\n *\n * _positionToMetaList maybe undefined on next loop\n */\n getPosition(newIndex: number, safeRange?: SafeRange) {\n this.prepare();\n const meta = this.getIndexMeta(newIndex);\n const prevMetaPosition = this._metaToPositionMap.get(meta);\n\n if (prevMetaPosition !== undefined) {\n const onTheFlyPositionMeta = this._onTheFlyIndices[prevMetaPosition];\n // the occupied meta should change position\n if (onTheFlyPositionMeta) {\n // such as place item 11 twice...\n if (onTheFlyPositionMeta === meta) {\n return prevMetaPosition;\n }\n let positionToReplace = this._replaceFurthestIndexPosition(\n newIndex,\n safeRange\n );\n if (this._isOnTheFlyFull)\n return this.getFliedPosition(newIndex, safeRange);\n\n while (this._onTheFlyIndices[positionToReplace]) {\n positionToReplace = this._replaceFurthestIndexPosition(\n newIndex,\n safeRange\n );\n }\n\n if (positionToReplace != null) {\n this._setMetaIndex(meta, newIndex);\n this._onTheFlyIndices[positionToReplace] = onTheFlyPositionMeta;\n return this._isOnTheFlyFullReturnHook(positionToReplace);\n }\n }\n this._onTheFlyIndices[prevMetaPosition] = meta;\n return this._isOnTheFlyFullReturnHook(prevMetaPosition);\n }\n\n // placed on new buffered position\n if (!this.isBufferFull)\n return this._isOnTheFlyFullReturnHook(\n this.getNewPositionForIndex(newIndex)\n );\n\n if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);\n\n let positionToReplace;\n const prevIndexMeta = this._indexToMetaMap.get(newIndex);\n\n // Index has already been stored, but we cant use its old position directly...\n // 1:index -> meta, meta may be reused later\n\n // 2: temp use index -> meta -> position, this issue should exist for follows...\n if (!prevIndexMeta) {\n this._cleanHeaps();\n positionToReplace = this._replaceFurthestIndexPosition(\n newIndex,\n safeRange\n );\n } else {\n positionToReplace = this._metaToPositionMap.get(prevIndexMeta);\n }\n\n this._onTheFlyIndices[positionToReplace] = meta;\n this._setMetaIndex(meta, newIndex);\n this._setMetaPosition(meta, positionToReplace);\n // should not push to heap, pop only\n // this._pushToHeaps(positionToReplace, newIndex)\n\n return this._isOnTheFlyFullReturnHook(positionToReplace);\n }\n\n replaceFurthestIndexPosition(\n newIndex: number,\n safeRange?: {\n startIndex: number;\n endIndex: number;\n }\n ) {\n if (!this.isBufferFull) {\n return this._isOnTheFlyFullReturnHook(\n this.getNewPositionForIndex(newIndex)\n );\n }\n\n return this._replaceFurthestIndexPosition(newIndex, safeRange);\n }\n\n _replaceFurthestIndexPosition(\n newIndex: number,\n safeRange?: {\n startIndex: number;\n endIndex: number;\n }\n ) {\n if (this._largeValues.empty() || this._smallValues.empty()) {\n return this._isOnTheFlyFullReturnHook(\n this.getNewPositionForIndex(newIndex)\n );\n }\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n\n let indexToReplace;\n\n if (!safeRange) {\n // far from min\n if (Math.abs(newIndex - minValue) > Math.abs(newIndex - maxValue)) {\n indexToReplace = minValue;\n this._smallValues.pop();\n } else {\n indexToReplace = maxValue;\n this._largeValues.pop();\n }\n const replacedMeta = this._indexToMetaMap.get(indexToReplace);\n const position = this._metaToPositionMap.get(replacedMeta);\n\n return position;\n }\n\n const { startIndex: lowValue, endIndex: highValue } = safeRange;\n\n // All values currently stored are necessary, we can't reuse any of them.\n if (\n isClamped(lowValue, minValue, highValue) &&\n isClamped(lowValue, maxValue, highValue)\n ) {\n return null;\n } else if (\n isClamped(lowValue, minValue, highValue) &&\n !isClamped(lowValue, maxValue, highValue)\n ) {\n indexToReplace = maxValue;\n this._largeValues.pop();\n } else if (\n !isClamped(lowValue, minValue, highValue) &&\n isClamped(lowValue, maxValue, highValue)\n ) {\n indexToReplace = minValue;\n this._smallValues.pop();\n } else if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n indexToReplace = minValue;\n this._smallValues.pop();\n } else {\n indexToReplace = maxValue;\n this._largeValues.pop();\n }\n\n const replacedMeta = this._indexToMetaMap.get(indexToReplace);\n const position = this._metaToPositionMap.get(replacedMeta);\n\n return position;\n }\n\n shuffle() {\n const indices = new Array(this.bufferSize);\n for (let idx = 0; idx < indices.length; idx++) {\n const meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];\n const targetIndex = this.getMetaIndex(meta);\n indices[idx] = targetIndex;\n }\n\n const _arr = new Array(indices.length);\n const _available = [];\n const indexToMetaMap = new Map();\n const metaToIndexMap = new Map();\n const metaToPositionMap = new Map();\n for (let idx = 0; idx < indices.length; idx++) {\n const currentIndex = indices[idx];\n const currentMeta = this._metaExtractor(currentIndex);\n if (currentMeta == null) continue;\n\n indexToMetaMap.set(currentIndex, currentMeta);\n metaToIndexMap.set(currentMeta, currentIndex);\n\n if (currentMeta === this._positionToMetaList[idx]) {\n _arr[idx] = currentMeta;\n continue;\n }\n const _i = this._positionToMetaList.findIndex((v) => v === currentMeta);\n if (_i !== -1) {\n _arr[_i] = currentMeta;\n continue;\n }\n\n _available.push(currentMeta);\n }\n\n const { smallValues, largeValues } = this.initialize();\n const positionToMetaList = [];\n\n for (let position = 0; position < indices.length; position++) {\n const value = indices[position];\n if (_arr[position] != null) {\n positionToMetaList[position] = _arr[position];\n metaToPositionMap.set(_arr[position], position);\n const element = { position, value };\n smallValues.push(element);\n largeValues.push(element);\n continue;\n }\n const meta = _available.shift();\n if (meta != null) {\n positionToMetaList[position] = meta;\n metaToPositionMap.set(meta, position);\n\n const element = { position, value };\n smallValues.push(element);\n largeValues.push(element);\n }\n }\n\n this._positionToMetaList = positionToMetaList;\n this._smallValues = smallValues;\n this._largeValues = largeValues;\n this._indexToMetaMap = indexToMetaMap;\n this.replaceMetaToIndexMap(metaToIndexMap);\n this._metaToPositionMap = metaToPositionMap;\n this._onTheFlyIndices = [];\n\n try {\n const indices = new Array(this.bufferSize);\n for (let idx = 0; idx < indices.length; idx++) {\n const meta =\n this._onTheFlyIndices[idx] || this._positionToMetaList[idx];\n const targetIndex = this.getMetaIndex(meta);\n if (meta != null) {\n indices[idx] = {\n meta,\n targetIndex,\n recyclerKey: `${this._name}_${idx}`,\n };\n }\n }\n return indices;\n } catch (err) {\n this.readyToStartNextLoop();\n return this._positionToMetaList;\n }\n }\n\n // key point: `meta` should be preserved..\n getIndices() {\n try {\n const indices = new Array(this.bufferSize);\n for (let idx = 0; idx < indices.length; idx++) {\n const meta =\n this._onTheFlyIndices[idx] || this._positionToMetaList[idx];\n const targetIndex = this.getMetaIndex(meta);\n // which means source data has changed. such as one element has been deleted\n if (meta !== this.getIndexMeta(targetIndex)) {\n return this.shuffle();\n }\n if (meta != null) {\n indices[idx] = {\n meta,\n targetIndex,\n recyclerKey: `${this._name}_${idx}`,\n };\n }\n }\n // clear on the fly indices after return indices.\n this._onTheFlyIndices = [];\n\n return indices;\n } catch (err) {\n this.readyToStartNextLoop();\n return this._positionToMetaList;\n }\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = { position, value };\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 _setMetaPosition(meta: Meta, position: number) {\n const prevMetaOnPosition = this._positionToMetaList[position];\n if (prevMetaOnPosition) this._metaToPositionMap.delete(prevMetaOnPosition);\n this._positionToMetaList[position] = meta;\n this._metaToPositionMap.set(meta, position);\n }\n\n /**\n *\n * @param meta\n * @param index\n * @returns true means index not changed\n */\n _setMetaIndex(meta: Meta, index: number) {\n const prevMetaIndex = this.getMetaIndex(meta);\n if (prevMetaIndex !== undefined) {\n // no need to set\n // if (prevMetaIndex === index) return true;\n this._indexToMetaMap.delete(prevMetaIndex);\n }\n this.setMetaIndex(meta, index);\n this._indexToMetaMap.set(index, meta);\n return false;\n }\n\n readyToStartNextLoop() {\n this._lastUpdatedMS = Date.now();\n }\n\n prepare() {\n if (this._loopMS === this._lastUpdatedMS) return;\n this._loopMS = this._lastUpdatedMS;\n\n this._onTheFlyIndices = [];\n this._isOnTheFlyFull = false;\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 _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 still valid elements to new heaps\n if (\n this._metaToPositionMap.get(this._indexToMetaMap.get(element.value)) !=\n null\n ) {\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._metaToPositionMap.get(\n this._indexToMetaMap.get(heap.peek()!.value)\n ) == null\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":["defaultMetaExtractor","value","IntegerBufferSet","props","_props$name","name","indexExtractor","_props$bufferSize","bufferSize","_props$metaExtractor","metaExtractor","this","_metaExtractor","_indexExtractor","_name","_indexToMetaMap","Map","_metaToPositionMap","_positionToMetaList","_metaToIndexMap","_onTheFlyIndices","_bufferSize","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForIndex","bind","getIndexPosition","replacePositionInFliedIndices","replaceFurthestIndexPosition","_isOnTheFlyFullReturnHook","returnHook","setIsOnTheFlyFull","_loopMS","Date","now","_lastUpdatedMS","_proto","prototype","val","data","filter","v","_isOnTheFlyFull","length","getOnTheFlyUncriticalPosition","safeRange","startIndex","endIndex","idx","metaIndex","get","isClamped","initialize","smallValues","largeValues","valueToPositionObject","getIndexMeta","index","getMetaIndex","meta","setMetaIndex","set","deleteMetaIndex","replaceMetaToIndexMap","newMetaToIndexMap","undefined","invariant","newPosition","_pushToHeaps","_setMetaIndex","_setMetaPosition","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","newIndex","pos","getFliedPosition","getPosition","prepare","positionToReplace","prevMetaPosition","onTheFlyPositionMeta","_replaceFurthestIndexPosition","isBufferFull","prevIndexMeta","_cleanHeaps","empty","indexToReplace","minValue","maxValue","Math","abs","pop","replacedMeta","lowValue","highValue","shuffle","indices","Array","targetIndex","_arr","_available","indexToMetaMap","metaToIndexMap","metaToPositionMap","_loop","currentIndex","currentMeta","_this","_i","findIndex","push","_this$initialize","positionToMetaList","position","shift","element","recyclerKey","err","readyToStartNextLoop","getIndices","prevMetaOnPosition","prevMetaIndex","_cleanHeap","minHeapSize","min","size","max","_recreateHeaps","sourceHeap","newSmallValues","newLargeValues","heap","lhs","rhs","key"],"mappings":"wRAeMA,EAAuB,SAACC,GAAK,OAAKA,8BA0CtC,SAAAC,EAAYC,YAAAA,IAAAA,EAAqC,IAC/C,IAKSC,EAALD,EAJFE,KAAAA,WAAID,EAAG,iBAAgBA,EACvBE,EAGEH,EAHFG,eAAcC,EAGZJ,EAFFK,WAAAA,WAAUD,EA7CiB,GA6CGA,EAAAE,EAE5BN,EADFO,cAEFC,KAAKC,wBAFUH,EAAGT,EAAoBS,EAGtCE,KAAKE,gBAAkBP,EAEvBK,KAAKG,MAAQT,EAKbM,KAAKI,gBAAkB,IAAIC,IAC3BL,KAAKM,mBAAqB,IAAID,IAC9BL,KAAKO,oBAAsB,GAC3BP,KAAKQ,gBAAkB,IAAIH,IAC3BL,KAAKS,iBAAmB,GAExBT,KAAKU,YAAcb,EAEnBG,KAAKW,aAAe,IAAIC,EAAK,GAAIZ,KAAKa,oBACtCb,KAAKc,aAAe,IAAIF,EAAK,GAAIZ,KAAKe,oBAEtCf,KAAKgB,uBAAyBhB,KAAKgB,uBAAuBC,KAAKjB,MAC/DA,KAAKkB,iBAAmBlB,KAAKkB,iBAAiBD,KAAKjB,MACnDA,KAAKmB,8BACHnB,KAAKmB,8BAA8BF,KAAKjB,MAC1CA,KAAKoB,6BACHpB,KAAKoB,6BAA6BH,KAAKjB,MACzCA,KAAKqB,0BAA4BC,EAC/BtB,KAAKuB,kBAAkBN,KAAKjB,OAG9BA,KAAKwB,QAAUC,KAAKC,MACpB1B,KAAK2B,eAAiB3B,KAAKwB,QAC5B,QAAAI,EAAArC,EAAAsC,UAeA,OAfAD,EAMDL,kBAAA,SAAkBO,GAChB,GAAW,MAAPA,EAAa,CACf,IAAMC,EAAO/B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WAAWrH,KAAKW,cACrBX,KAAKqH,WAAWrH,KAAKc,cACrB,IAAMwG,EAAclC,KAAKmC,IACvBvH,KAAKW,aAAa6G,OAClBxH,KAAKc,aAAa0G,QAEApC,KAAKqC,IACvBzH,KAAKW,aAAa6G,OAClBxH,KAAKc,aAAa0G,QAEF,GAAKF,GAGrBtH,KAAK0H,kBAER9F,EACD8F,eAAA,WAaE,IAZA,IAAMC,EACJ3H,KAAKW,aAAa6G,OAASxH,KAAKc,aAAa0G,OACzCxH,KAAKW,aACLX,KAAKc,aACL8G,EAAiB,IAAIhH,EACzB,GACAZ,KAAKa,oBAEDgH,EAAiB,IAAIjH,EACzB,GACAZ,KAAKe,qBAEC4G,EAAW3C,SAAS,CAC1B,IAAM8B,EAAUa,EAAWrC,MAIzB,MADAtF,KAAKM,mBAAmBoC,IAAI1C,KAAKI,gBAAgBsC,IAAIoE,EAAQxH,UAG7DsI,EAAenB,KAAKK,GACpBe,EAAepB,KAAKK,IAGxB9G,KAAKW,aAAeiH,EACpB5H,KAAKc,aAAe+G,GACrBjG,EAEDyF,WAAA,SAAWS,GACT,MACGA,EAAK9C,SAGD,MAFLhF,KAAKM,mBAAmBoC,IACtB1C,KAAKI,gBAAgBsC,IAAIoF,EAAK7D,OAAQ3E,SAGxCwI,EAAKxC,OAER1D,EAEDf,mBAAA,SAAmBkH,EAAeC,GAChC,OAAOD,EAAIzI,MAAQ0I,EAAI1I,OACxBsC,EAEDb,mBAAA,SAAmBgH,EAAeC,GAChC,OAAOD,EAAIzI,MAAQ0I,EAAI1I,SACxBC,OAAA0I,iBAAAvF,IAzfD,WACE,OAAO1C,KAAKU,eACbuH,mBAAAvF,IASD,WACE,OAAO1C,KAAKO,oBAAoB4B,QAAUnC,KAAKU,8gBAChDnB,+BA9F8B"}
1
+ {"version":3,"file":"integer-buffer-set.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["import Heap from '@x-oasis/heap';\nimport isClamped from '@x-oasis/is-clamped';\nimport invariant from '@x-oasis/invariant';\nimport returnHook, { ReturnHook } from '@x-oasis/return-hook';\nimport {\n HeapItem,\n SafeRange,\n MetaExtractor,\n IndexExtractor,\n IntegerBufferSetProps,\n MetaToIndexMap,\n MetaToPositionMap,\n IndexToMetaMap,\n} from './types';\n\nconst defaultMetaExtractor = (value) => value;\nexport const defaultBufferSize = 10;\nconst thresholdNumber = Number.MAX_SAFE_INTEGER - 100000;\n\n// !!!!! should do meta validation...meta should has an index...\n// value: original data `index` value\n// value(index) => meta => position\n// `index to getIndices, meta to find index`\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.\n// feature: add / delete / update item will also in consider..\nclass IntegerBufferSet<Meta = any> {\n private _name: string;\n private _bufferSize: number;\n\n private _indexToMetaMap: IndexToMetaMap<Meta>;\n private _metaToPositionMap: MetaToPositionMap<Meta>;\n private _positionToMetaList: Array<Meta>;\n private _metaToIndexMap: MetaToIndexMap<Meta>;\n\n private _smallValues: Heap<HeapItem>;\n private _largeValues: Heap<HeapItem>;\n private _metaExtractor: MetaExtractor<Meta>;\n private _indexExtractor: IndexExtractor<Meta>;\n\n private _onTheFlyIndices: Array<Meta>;\n\n private _isOnTheFlyFull: boolean;\n private _isOnTheFlyFullReturnHook: ReturnHook;\n\n private _loopMS: number;\n private _lastUpdatedMS: number;\n\n constructor(props: IntegerBufferSetProps<Meta> = {}) {\n const {\n name = 'default_buffer',\n indexExtractor,\n bufferSize = defaultBufferSize,\n metaExtractor = defaultMetaExtractor,\n } = props;\n this._metaExtractor = metaExtractor;\n this._indexExtractor = indexExtractor;\n\n this._name = name;\n\n /**\n * this._indexToMetaMap is used to find the prev meta when finding a position for index.\n */\n this._indexToMetaMap = new Map();\n this._metaToPositionMap = new Map();\n this._positionToMetaList = [];\n this._metaToIndexMap = new Map();\n this._onTheFlyIndices = [];\n\n this._bufferSize = bufferSize;\n\n this._smallValues = new Heap([], this._smallerComparator);\n this._largeValues = new Heap([], this._greaterComparator);\n\n this.getNewPositionForIndex = this.getNewPositionForIndex.bind(this);\n this.getIndexPosition = this.getIndexPosition.bind(this);\n this.replaceFurthestIndexPosition =\n this.replaceFurthestIndexPosition.bind(this);\n this._isOnTheFlyFullReturnHook = returnHook(\n this.setIsOnTheFlyFull.bind(this)\n );\n\n this._loopMS = Date.now();\n this._lastUpdatedMS = this._loopMS;\n }\n\n get bufferSize() {\n return this._bufferSize;\n }\n\n isThresholdMeta(meta) {\n if (typeof meta === 'number' && meta > thresholdNumber) return true;\n return false;\n }\n\n setIsOnTheFlyFull(val: any) {\n if (val != null) {\n const data = this._onTheFlyIndices.filter((v) => v != null);\n this._isOnTheFlyFull = data.length === this._bufferSize;\n // console.log('fly ', this._isOnTheFlyFull, data.length, this._bufferSize);\n }\n }\n\n resetOnTheFlies() {\n this._isOnTheFlyFull = false;\n this._onTheFlyIndices = [];\n }\n\n get isBufferFull() {\n return this._positionToMetaList.length >= this._bufferSize;\n }\n\n getOnTheFlyUncriticalPosition(safeRange: SafeRange) {\n const { startIndex, endIndex } = safeRange;\n for (let idx = 0; idx < this._onTheFlyIndices.length; idx++) {\n const meta = this._onTheFlyIndices[idx];\n const metaIndex = this.getMetaIndex(meta);\n if (!isClamped(startIndex, metaIndex, endIndex)) {\n return idx;\n }\n }\n return null;\n }\n\n initialize() {\n return {\n smallValues: new Heap([], this._smallerComparator),\n largeValues: new Heap([], this._greaterComparator),\n };\n }\n\n getIndexMeta(index: number) {\n if (index == null || index < 0) return null;\n return this._metaExtractor(index);\n }\n\n getMetaIndex(meta: Meta) {\n if (meta == null) return -1;\n if (this.isThresholdMeta(meta)) return -1;\n if (this._indexExtractor) return this._indexExtractor(meta);\n return this._metaToIndexMap.get(meta);\n }\n\n setMetaIndex(meta: Meta, index: number) {\n if (!this._indexExtractor) {\n return this._metaToIndexMap.set(meta, index);\n }\n return false;\n }\n\n deleteMetaIndex(meta: Meta) {\n return this._metaToIndexMap.delete(meta);\n }\n\n replaceMetaToIndexMap(newMetaToIndexMap: MetaToIndexMap<Meta>) {\n if (!this._indexExtractor) {\n return (this._metaToIndexMap = newMetaToIndexMap);\n }\n return false;\n }\n\n getIndexPosition(index: number): undefined | number {\n return this.getMetaIndex(this.getIndexMeta(index));\n }\n\n getNewPositionForIndex(index: number) {\n const meta = this.getIndexMeta(index);\n invariant(\n this._metaToPositionMap.get(meta) === undefined,\n \"Shouldn't try to find new position for value already stored in BufferSet\"\n );\n const newPosition = this._positionToMetaList.length;\n\n this._pushToHeaps(newPosition, index);\n this._setMetaIndex(meta, index);\n this._setMetaPosition(meta, newPosition);\n\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 getFliedPosition(newIndex: number, safeRange: SafeRange) {\n if (this._isOnTheFlyFull) {\n // newIndex is not critical index, do nothing\n if (\n safeRange &&\n isClamped(safeRange.startIndex, newIndex, safeRange.endIndex)\n ) {\n return this.getOnTheFlyUncriticalPosition(safeRange);\n }\n // if `newIndex` is critical index, replace an un-committed\n // index value from _onTheFlyIndices.\n // const pos = this.getOnTheFlyUncriticalPosition(safeRange);\n // if (pos != null) return pos;\n }\n return null;\n }\n\n /**\n *\n * @param newIndex\n * @param safeRange\n * @returns\n *\n *\n * _positionToMetaList maybe undefined on next loop\n */\n getPosition(newIndex: number, safeRange?: SafeRange) {\n this.prepare();\n const meta = this.getIndexMeta(newIndex);\n const metaPosition = this._metaToPositionMap.get(meta);\n let position, indexMeta;\n\n // if (this._name === 'normal_goods')\n // console.log(\n // 'getPosition ',\n // newIndex,\n // !this.isBufferFull,\n // this._isOnTheFlyFull,\n // this._onTheFlyIndices.slice(),\n // this._indexToMetaMap.get(newIndex),\n // this._metaToPositionMap.get(this._indexToMetaMap.get(newIndex))\n // );\n\n if (metaPosition !== undefined) {\n position = this.commitPosition({\n newIndex,\n meta,\n safeRange,\n position: metaPosition,\n });\n } else if (!this.isBufferFull) {\n /** placed on new buffered position */\n position = this.getNewPositionForIndex(newIndex);\n } else if (this._isOnTheFlyFull) {\n position = this.getFliedPosition(newIndex, safeRange);\n } else if (\n (indexMeta = this._indexToMetaMap.get(newIndex)) &&\n this._metaToPositionMap.get(indexMeta)\n ) {\n /**\n Index has already been stored, but we cant use its old position directly...\n 1:index -> meta, meta may be reused later\n 2: temp use index -> meta -> position, this issue should exist for follows...\n */\n position = this.commitPosition({\n newIndex,\n meta,\n safeRange,\n position: this._metaToPositionMap.get(indexMeta),\n });\n } else {\n this._cleanHeaps();\n // console.log('commeit ---')\n position = this.commitPosition({\n newIndex,\n meta,\n safeRange,\n position: this._replaceFurthestIndexPosition(newIndex, safeRange),\n });\n }\n\n // console.log('position ', position)\n\n if (position != null) {\n this._onTheFlyIndices[position] = meta;\n this._setMetaIndex(meta, newIndex);\n this._metaToPositionMap.set(meta, position);\n\n // this._setMetaPosition(meta, position);\n // should not push to heap, pop only\n // this._pushToHeaps(position, newIndex)\n\n return this._isOnTheFlyFullReturnHook(position);\n }\n\n return null;\n }\n\n replaceFurthestIndexPosition(\n newIndex: number,\n safeRange?: {\n startIndex: number;\n endIndex: number;\n }\n ) {\n if (!this.isBufferFull) {\n return this._isOnTheFlyFullReturnHook(\n this.getNewPositionForIndex(newIndex)\n );\n }\n\n return this._replaceFurthestIndexPosition(newIndex, safeRange);\n }\n\n _replaceFurthestIndexPosition(\n newIndex: number,\n safeRange?: {\n startIndex: number;\n endIndex: number;\n }\n ) {\n if (this._largeValues.empty() || this._smallValues.empty()) {\n return this._isOnTheFlyFullReturnHook(\n this.getNewPositionForIndex(newIndex)\n );\n }\n\n let indexToReplace;\n\n const minValue = this._smallValues.peek()!.value;\n const maxValue = this._largeValues.peek()!.value;\n\n // console.log('mathc ', maxValue, maxValue > thresholdNumber)\n if (maxValue > thresholdNumber) {\n indexToReplace = maxValue;\n this._largeValues.pop();\n const replacedMeta = this._indexToMetaMap.get(indexToReplace);\n const position = this._metaToPositionMap.get(replacedMeta);\n return position;\n }\n\n if (!safeRange) {\n // far from min\n if (Math.abs(newIndex - minValue) > Math.abs(newIndex - maxValue)) {\n indexToReplace = minValue;\n this._smallValues.pop();\n } else {\n indexToReplace = maxValue;\n this._largeValues.pop();\n }\n const replacedMeta = this._indexToMetaMap.get(indexToReplace);\n const position = this._metaToPositionMap.get(replacedMeta);\n\n return position;\n }\n\n const { startIndex: lowValue, endIndex: highValue } = safeRange;\n\n // All values currently stored are necessary, we can't reuse any of them.\n if (\n isClamped(lowValue, minValue, highValue) &&\n isClamped(lowValue, maxValue, highValue)\n ) {\n return null;\n } else if (\n isClamped(lowValue, minValue, highValue) &&\n !isClamped(lowValue, maxValue, highValue)\n ) {\n indexToReplace = maxValue;\n this._largeValues.pop();\n } else if (\n !isClamped(lowValue, minValue, highValue) &&\n isClamped(lowValue, maxValue, highValue)\n ) {\n indexToReplace = minValue;\n this._smallValues.pop();\n } else if (lowValue - minValue > maxValue - highValue) {\n // minValue is further from provided range. We will reuse it's position.\n indexToReplace = minValue;\n this._smallValues.pop();\n } else {\n indexToReplace = maxValue;\n this._largeValues.pop();\n }\n\n const replacedMeta = this._indexToMetaMap.get(indexToReplace);\n const position = this._metaToPositionMap.get(replacedMeta);\n\n // console.log('index ', indexToReplace, replacedMeta, position)\n\n return position;\n }\n\n shuffle() {\n const indices = new Array(this.bufferSize);\n for (let idx = 0; idx < indices.length; idx++) {\n const meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];\n // console.log('ix ', idx,this.getMetaIndex(meta) )\n const targetIndex = this.getMetaIndex(meta);\n indices[idx] = targetIndex;\n }\n\n // console.log(\n // 'indices ',\n // this._positionToMetaList,\n // this._onTheFlyIndices.slice(),\n // indices\n // );\n\n const _arr = new Array(indices.length);\n const _available = [];\n const indexToMetaMap = new Map();\n const metaToIndexMap = new Map();\n\n for (let idx = 0; idx < indices.length; idx++) {\n const currentIndex = indices[idx];\n const currentMeta = this._metaExtractor(currentIndex);\n // console.log(\"current \", currentIndex, currentMeta)\n if (currentMeta == null) continue;\n indexToMetaMap.set(currentIndex, currentMeta);\n metaToIndexMap.set(currentMeta, currentIndex);\n if (currentMeta === this._positionToMetaList[idx]) {\n _arr[idx] = currentMeta;\n continue;\n }\n const _i = this._positionToMetaList.findIndex((v) => v === currentMeta);\n if (_i !== -1) {\n _arr[_i] = currentMeta;\n continue;\n }\n\n _available.push(currentMeta);\n }\n\n const positionToMetaList = [];\n this._indexToMetaMap = indexToMetaMap;\n this.replaceMetaToIndexMap(metaToIndexMap);\n\n for (let position = 0; position < indices.length; position++) {\n if (_arr[position] != null) {\n positionToMetaList[position] = _arr[position];\n continue;\n }\n const meta = _available.shift();\n if (meta != null) {\n positionToMetaList[position] = meta;\n }\n }\n\n this._positionToMetaList = positionToMetaList;\n\n return this.getIndices();\n }\n\n // key point: `meta` should be preserved..\n getIndices() {\n const { smallValues, largeValues } = this.initialize();\n\n try {\n const indices = new Array(this._positionToMetaList.length);\n const metaToPositionMap = new Map();\n for (let idx = 0; idx < indices.length; idx++) {\n const meta =\n this._onTheFlyIndices[idx] || this._positionToMetaList[idx];\n const targetIndex = this.getMetaIndex(meta);\n // which means source data has changed. such as one element has been deleted\n if (\n !this.isThresholdMeta(meta) &&\n meta != this.getIndexMeta(targetIndex)\n ) {\n return this.shuffle();\n }\n if (meta != null && !this.isThresholdMeta(meta)) {\n const element = { position: idx, value: targetIndex };\n smallValues.push(element);\n largeValues.push(element);\n metaToPositionMap.set(meta, idx);\n indices[idx] = {\n meta,\n targetIndex,\n recyclerKey: `${this._name}_${idx}`,\n };\n }\n }\n this._smallValues = smallValues;\n this._largeValues = largeValues;\n this._metaToPositionMap = metaToPositionMap;\n this._positionToMetaList = indices.map((v) => v?.meta);\n this.resetOnTheFlies();\n\n return indices;\n } catch (err) {\n console.log('err ', err);\n return this._positionToMetaList;\n } finally {\n this.readyToStartNextLoop();\n // clear on the fly indices after return indices.\n }\n }\n\n _pushToHeaps(position: number, value: number) {\n const element = { position, value };\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 _setMetaPosition(meta: Meta, position: number) {\n // do not delete meta2position; because getPosition will get by meta first...\n // const prevMetaOnPosition = this._positionToMetaList[position];\n // if (prevMetaOnPosition) this._metaToPositionMap.delete(prevMetaOnPosition);\n this._positionToMetaList[position] = meta;\n this._metaToPositionMap.set(meta, position);\n }\n\n commitPosition(props: {\n newIndex: number;\n position: number;\n meta: Meta;\n safeRange: SafeRange;\n }) {\n const { newIndex, safeRange, position, meta } = props;\n const onTheFlyPositionMeta = this._onTheFlyIndices[position];\n let positionToReplace = position;\n\n // console.log('position ', newIndex, position);\n\n if (onTheFlyPositionMeta) {\n // such as place item 11 twice...\n if (onTheFlyPositionMeta === meta) return position;\n if (this._isOnTheFlyFull)\n return this.getFliedPosition(newIndex, safeRange);\n positionToReplace = this._replaceFurthestIndexPosition(\n newIndex,\n safeRange\n );\n\n while (this._onTheFlyIndices[positionToReplace]) {\n positionToReplace = this._replaceFurthestIndexPosition(\n newIndex,\n safeRange\n );\n }\n }\n return positionToReplace;\n }\n\n /**\n *\n * @param meta\n * @param index\n * @returns true means index not changed\n */\n _setMetaIndex(meta: Meta, index: number) {\n const prevMetaIndex = this.getMetaIndex(meta);\n if (prevMetaIndex !== undefined) {\n // no need to set\n // if (prevMetaIndex === index) return true;\n this._indexToMetaMap.delete(prevMetaIndex);\n }\n this.setMetaIndex(meta, index);\n this._indexToMetaMap.set(index, meta);\n return false;\n }\n\n readyToStartNextLoop() {\n this._lastUpdatedMS = Date.now();\n }\n\n prepare() {\n if (this._loopMS === this._lastUpdatedMS) return;\n this._loopMS = this._lastUpdatedMS;\n\n this._onTheFlyIndices = [];\n this._isOnTheFlyFull = false;\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\n for (let idx = 0; idx < this._positionToMetaList.length; idx++) {\n if (this._positionToMetaList[idx] == null) {\n this._recreateHeaps();\n return;\n }\n }\n\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 _recreateHeaps() {\n const { smallValues, largeValues } = this.initialize();\n for (\n let position = 0;\n position < this._positionToMetaList.length;\n position++\n ) {\n const meta = this._positionToMetaList[position];\n let value = this.getMetaIndex(meta);\n\n if (!meta || value === -1 || value == null) {\n value = Number.MAX_SAFE_INTEGER - position;\n }\n\n const element = { position, value };\n smallValues.push(element);\n largeValues.push(element);\n if (value > thresholdNumber) {\n // @ts-ignore\n this._setMetaPosition(value, position);\n // @ts-ignore\n this._setMetaIndex(value, value);\n }\n }\n\n this._largeValues.peek().value;\n\n this._smallValues = smallValues;\n this._largeValues = largeValues;\n }\n\n // _cleanHeap(heap: Heap<HeapItem>) {\n // while (\n // !heap.empty() &&\n // this._metaToPositionMap.get(\n // this._indexToMetaMap.get(heap.peek()!.value)\n // ) == null\n // ) {\n // console.log('pop ---', heap.peek()!.value);\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":["defaultMetaExtractor","value","thresholdNumber","Number","MAX_SAFE_INTEGER","IntegerBufferSet","props","_props$name","name","indexExtractor","_props$bufferSize","bufferSize","_props$metaExtractor","metaExtractor","this","_metaExtractor","_indexExtractor","_name","_indexToMetaMap","Map","_metaToPositionMap","_positionToMetaList","_metaToIndexMap","_onTheFlyIndices","_bufferSize","_smallValues","Heap","_smallerComparator","_largeValues","_greaterComparator","getNewPositionForIndex","bind","getIndexPosition","replaceFurthestIndexPosition","_isOnTheFlyFullReturnHook","returnHook","setIsOnTheFlyFull","_loopMS","Date","now","_lastUpdatedMS","_proto","prototype","isThresholdMeta","meta","val","data","filter","v","_isOnTheFlyFull","length","resetOnTheFlies","getOnTheFlyUncriticalPosition","safeRange","startIndex","endIndex","idx","metaIndex","getMetaIndex","isClamped","initialize","smallValues","largeValues","getIndexMeta","index","get","setMetaIndex","set","deleteMetaIndex","replaceMetaToIndexMap","newMetaToIndexMap","undefined","invariant","newPosition","_pushToHeaps","_setMetaIndex","_setMetaPosition","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","getFliedPosition","newIndex","getPosition","prepare","position","indexMeta","metaPosition","commitPosition","isBufferFull","_cleanHeaps","_replaceFurthestIndexPosition","empty","indexToReplace","minValue","maxValue","pop","replacedMeta","Math","abs","lowValue","highValue","shuffle","indices","Array","targetIndex","_arr","_available","indexToMetaMap","metaToIndexMap","_loop","currentIndex","currentMeta","_this","_i","findIndex","push","positionToMetaList","shift","getIndices","_this$initialize","metaToPositionMap","element","recyclerKey","map","err","console","log","readyToStartNextLoop","onTheFlyPositionMeta","positionToReplace","prevMetaIndex","_recreateHeaps","minHeapSize","min","size","max","_this$initialize2","lhs","rhs","key"],"mappings":"wRAeMA,EAAuB,SAACC,GAAK,OAAKA,GAElCC,EAAkBC,OAAOC,iBAAmB,+BAyChD,SAAAC,EAAYC,YAAAA,IAAAA,EAAqC,IAC/C,IAKSC,EAALD,EAJFE,KAAAA,WAAID,EAAG,iBAAgBA,EACvBE,EAGEH,EAHFG,eAAcC,EAGZJ,EAFFK,WAAAA,WAAUD,EA9CiB,GA8CGA,EAAAE,EAE5BN,EADFO,cAEFC,KAAKC,wBAFUH,EAAGZ,E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Y,KAAK8D,iBAAiB3E,EAAOqF,GAE7BxE,KAAK6D,cAAc1E,EAAOA,IAI9Ba,KAAKc,aAAamD,OAElBjE,KAAKW,aAAeoC,EACpB/C,KAAKc,aAAekC,GACrBrB,EAcDd,mBAAA,SAAmBgH,EAAeC,GAChC,OAAOD,EAAI1I,MAAQ2I,EAAI3I,OACxBwC,EAEDZ,mBAAA,SAAmB8G,EAAeC,GAChC,OAAOD,EAAI1I,MAAQ2I,EAAI3I,SACxBI,OAAAwI,iBAAA5E,IA3iBD,WACE,OAAOnD,KAAKU,eACbqH,mBAAA5E,IAoBD,WACE,OAAOnD,KAAKO,oBAAoB6B,QAAUpC,KAAKU,8gBAChDnB,+BAxG8B"}
@@ -39,6 +39,7 @@ var defaultMetaExtractor = function defaultMetaExtractor(value) {
39
39
  return value;
40
40
  };
41
41
  var defaultBufferSize = 10;
42
+ var thresholdNumber = Number.MAX_SAFE_INTEGER - 100000;
42
43
  var IntegerBufferSet = /*#__PURE__*/function () {
43
44
  function IntegerBufferSet(props) {
44
45
  if (props === void 0) {
@@ -65,27 +66,34 @@ var IntegerBufferSet = /*#__PURE__*/function () {
65
66
  this._largeValues = new Heap([], this._greaterComparator);
66
67
  this.getNewPositionForIndex = this.getNewPositionForIndex.bind(this);
67
68
  this.getIndexPosition = this.getIndexPosition.bind(this);
68
- this.replacePositionInFliedIndices = this.replacePositionInFliedIndices.bind(this);
69
69
  this.replaceFurthestIndexPosition = this.replaceFurthestIndexPosition.bind(this);
70
70
  this._isOnTheFlyFullReturnHook = returnHook(this.setIsOnTheFlyFull.bind(this));
71
71
  this._loopMS = Date.now();
72
72
  this._lastUpdatedMS = this._loopMS;
73
73
  }
74
74
  var _proto = IntegerBufferSet.prototype;
75
+ _proto.isThresholdMeta = function isThresholdMeta(meta) {
76
+ if (typeof meta === 'number' && meta > thresholdNumber) return true;
77
+ return false;
78
+ };
75
79
  _proto.setIsOnTheFlyFull = function setIsOnTheFlyFull(val) {
76
80
  if (val != null) {
77
81
  var data = this._onTheFlyIndices.filter(function (v) {
78
- return v;
82
+ return v != null;
79
83
  });
80
84
  this._isOnTheFlyFull = data.length === this._bufferSize;
81
85
  }
82
86
  };
87
+ _proto.resetOnTheFlies = function resetOnTheFlies() {
88
+ this._isOnTheFlyFull = false;
89
+ this._onTheFlyIndices = [];
90
+ };
83
91
  _proto.getOnTheFlyUncriticalPosition = function getOnTheFlyUncriticalPosition(safeRange) {
84
92
  var startIndex = safeRange.startIndex,
85
93
  endIndex = safeRange.endIndex;
86
94
  for (var idx = 0; idx < this._onTheFlyIndices.length; idx++) {
87
95
  var meta = this._onTheFlyIndices[idx];
88
- var metaIndex = this._metaToIndexMap.get(meta);
96
+ var metaIndex = this.getMetaIndex(meta);
89
97
  if (!isClamped(startIndex, metaIndex, endIndex)) {
90
98
  return idx;
91
99
  }
@@ -95,14 +103,16 @@ var IntegerBufferSet = /*#__PURE__*/function () {
95
103
  _proto.initialize = function initialize() {
96
104
  return {
97
105
  smallValues: new Heap([], this._smallerComparator),
98
- largeValues: new Heap([], this._greaterComparator),
99
- valueToPositionObject: {}
106
+ largeValues: new Heap([], this._greaterComparator)
100
107
  };
101
108
  };
102
109
  _proto.getIndexMeta = function getIndexMeta(index) {
110
+ if (index == null || index < 0) return null;
103
111
  return this._metaExtractor(index);
104
112
  };
105
113
  _proto.getMetaIndex = function getMetaIndex(meta) {
114
+ if (meta == null) return -1;
115
+ if (this.isThresholdMeta(meta)) return -1;
106
116
  if (this._indexExtractor) return this._indexExtractor(meta);
107
117
  return this._metaToIndexMap.get(meta);
108
118
  };
@@ -141,66 +151,53 @@ var IntegerBufferSet = /*#__PURE__*/function () {
141
151
  var _this$_largeValues$pe;
142
152
  return (_this$_largeValues$pe = this._largeValues.peek()) == null ? void 0 : _this$_largeValues$pe.value;
143
153
  };
144
- _proto.replacePositionInFliedIndices = function replacePositionInFliedIndices(newIndex, safeRange) {
145
- var startIndex = safeRange.startIndex,
146
- endIndex = safeRange.endIndex;
154
+ _proto.getFliedPosition = function getFliedPosition(newIndex, safeRange) {
147
155
  if (this._isOnTheFlyFull) {
148
- if (!isClamped(startIndex, newIndex, endIndex)) {
149
- return null;
156
+ if (safeRange && isClamped(safeRange.startIndex, newIndex, safeRange.endIndex)) {
157
+ return this.getOnTheFlyUncriticalPosition(safeRange);
150
158
  }
151
- var pos = this.getOnTheFlyUncriticalPosition(safeRange);
152
- if (pos != null) return pos;
153
- }
154
- return null;
155
- };
156
- _proto.getFliedPosition = function getFliedPosition(newIndex, safeRange) {
157
- var pos = this.replacePositionInFliedIndices(newIndex, safeRange);
158
- if (pos != null) {
159
- var meta = this.getIndexMeta(newIndex);
160
- this._onTheFlyIndices[pos] = meta;
161
- this._setMetaIndex(meta, newIndex);
162
- return this._isOnTheFlyFullReturnHook(pos);
163
159
  }
164
160
  return null;
165
161
  };
166
162
  _proto.getPosition = function getPosition(newIndex, safeRange) {
167
163
  this.prepare();
168
164
  var meta = this.getIndexMeta(newIndex);
169
- var prevMetaPosition = this._metaToPositionMap.get(meta);
170
- if (prevMetaPosition !== undefined) {
171
- var onTheFlyPositionMeta = this._onTheFlyIndices[prevMetaPosition];
172
- if (onTheFlyPositionMeta) {
173
- if (onTheFlyPositionMeta === meta) {
174
- return prevMetaPosition;
175
- }
176
- var _positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
177
- if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
178
- while (this._onTheFlyIndices[_positionToReplace]) {
179
- _positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
180
- }
181
- if (_positionToReplace != null) {
182
- this._setMetaIndex(meta, newIndex);
183
- this._onTheFlyIndices[_positionToReplace] = onTheFlyPositionMeta;
184
- return this._isOnTheFlyFullReturnHook(_positionToReplace);
185
- }
186
- }
187
- this._onTheFlyIndices[prevMetaPosition] = meta;
188
- return this._isOnTheFlyFullReturnHook(prevMetaPosition);
189
- }
190
- if (!this.isBufferFull) return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
191
- if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
192
- var positionToReplace;
193
- var prevIndexMeta = this._indexToMetaMap.get(newIndex);
194
- if (!prevIndexMeta) {
195
- this._cleanHeaps();
196
- positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
165
+ var metaPosition = this._metaToPositionMap.get(meta);
166
+ var position, indexMeta;
167
+ if (metaPosition !== undefined) {
168
+ position = this.commitPosition({
169
+ newIndex: newIndex,
170
+ meta: meta,
171
+ safeRange: safeRange,
172
+ position: metaPosition
173
+ });
174
+ } else if (!this.isBufferFull) {
175
+ position = this.getNewPositionForIndex(newIndex);
176
+ } else if (this._isOnTheFlyFull) {
177
+ position = this.getFliedPosition(newIndex, safeRange);
178
+ } else if ((indexMeta = this._indexToMetaMap.get(newIndex)) && this._metaToPositionMap.get(indexMeta)) {
179
+ position = this.commitPosition({
180
+ newIndex: newIndex,
181
+ meta: meta,
182
+ safeRange: safeRange,
183
+ position: this._metaToPositionMap.get(indexMeta)
184
+ });
197
185
  } else {
198
- positionToReplace = this._metaToPositionMap.get(prevIndexMeta);
186
+ this._cleanHeaps();
187
+ position = this.commitPosition({
188
+ newIndex: newIndex,
189
+ meta: meta,
190
+ safeRange: safeRange,
191
+ position: this._replaceFurthestIndexPosition(newIndex, safeRange)
192
+ });
199
193
  }
200
- this._onTheFlyIndices[positionToReplace] = meta;
201
- this._setMetaIndex(meta, newIndex);
202
- this._setMetaPosition(meta, positionToReplace);
203
- return this._isOnTheFlyFullReturnHook(positionToReplace);
194
+ if (position != null) {
195
+ this._onTheFlyIndices[position] = meta;
196
+ this._setMetaIndex(meta, newIndex);
197
+ this._metaToPositionMap.set(meta, position);
198
+ return this._isOnTheFlyFullReturnHook(position);
199
+ }
200
+ return null;
204
201
  };
205
202
  _proto.replaceFurthestIndexPosition = function replaceFurthestIndexPosition(newIndex, safeRange) {
206
203
  if (!this.isBufferFull) {
@@ -212,9 +209,16 @@ var IntegerBufferSet = /*#__PURE__*/function () {
212
209
  if (this._largeValues.empty() || this._smallValues.empty()) {
213
210
  return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
214
211
  }
212
+ var indexToReplace;
215
213
  var minValue = this._smallValues.peek().value;
216
214
  var maxValue = this._largeValues.peek().value;
217
- var indexToReplace;
215
+ if (maxValue > thresholdNumber) {
216
+ indexToReplace = maxValue;
217
+ this._largeValues.pop();
218
+ var _replacedMeta = this._indexToMetaMap.get(indexToReplace);
219
+ var _position = this._metaToPositionMap.get(_replacedMeta);
220
+ return _position;
221
+ }
218
222
  if (!safeRange) {
219
223
  if (Math.abs(newIndex - minValue) > Math.abs(newIndex - maxValue)) {
220
224
  indexToReplace = minValue;
@@ -223,9 +227,9 @@ var IntegerBufferSet = /*#__PURE__*/function () {
223
227
  indexToReplace = maxValue;
224
228
  this._largeValues.pop();
225
229
  }
226
- var _replacedMeta = this._indexToMetaMap.get(indexToReplace);
227
- var _position = this._metaToPositionMap.get(_replacedMeta);
228
- return _position;
230
+ var _replacedMeta2 = this._indexToMetaMap.get(indexToReplace);
231
+ var _position2 = this._metaToPositionMap.get(_replacedMeta2);
232
+ return _position2;
229
233
  }
230
234
  var lowValue = safeRange.startIndex,
231
235
  highValue = safeRange.endIndex;
@@ -260,7 +264,6 @@ var IntegerBufferSet = /*#__PURE__*/function () {
260
264
  var _available = [];
261
265
  var indexToMetaMap = new Map();
262
266
  var metaToIndexMap = new Map();
263
- var metaToPositionMap = new Map();
264
267
  var _loop = function _loop() {
265
268
  var currentIndex = indices[_idx];
266
269
  var currentMeta = _this._metaExtractor(currentIndex);
@@ -284,71 +287,43 @@ var IntegerBufferSet = /*#__PURE__*/function () {
284
287
  var _ret = _loop();
285
288
  if (_ret === "continue") continue;
286
289
  }
287
- var _this$initialize = this.initialize(),
288
- smallValues = _this$initialize.smallValues,
289
- largeValues = _this$initialize.largeValues;
290
290
  var positionToMetaList = [];
291
+ this._indexToMetaMap = indexToMetaMap;
292
+ this.replaceMetaToIndexMap(metaToIndexMap);
291
293
  for (var position = 0; position < indices.length; position++) {
292
- var value = indices[position];
293
294
  if (_arr[position] != null) {
294
295
  positionToMetaList[position] = _arr[position];
295
- metaToPositionMap.set(_arr[position], position);
296
- var element = {
297
- position: position,
298
- value: value
299
- };
300
- smallValues.push(element);
301
- largeValues.push(element);
302
296
  continue;
303
297
  }
304
298
  var _meta = _available.shift();
305
299
  if (_meta != null) {
306
300
  positionToMetaList[position] = _meta;
307
- metaToPositionMap.set(_meta, position);
308
- var _element = {
309
- position: position,
310
- value: value
311
- };
312
- smallValues.push(_element);
313
- largeValues.push(_element);
314
301
  }
315
302
  }
316
303
  this._positionToMetaList = positionToMetaList;
317
- this._smallValues = smallValues;
318
- this._largeValues = largeValues;
319
- this._indexToMetaMap = indexToMetaMap;
320
- this.replaceMetaToIndexMap(metaToIndexMap);
321
- this._metaToPositionMap = metaToPositionMap;
322
- this._onTheFlyIndices = [];
323
- try {
324
- var _indices = new Array(this.bufferSize);
325
- for (var _idx2 = 0; _idx2 < _indices.length; _idx2++) {
326
- var _meta2 = this._onTheFlyIndices[_idx2] || this._positionToMetaList[_idx2];
327
- var _targetIndex = this.getMetaIndex(_meta2);
328
- if (_meta2 != null) {
329
- _indices[_idx2] = {
330
- meta: _meta2,
331
- targetIndex: _targetIndex,
332
- recyclerKey: this._name + "_" + _idx2
333
- };
334
- }
335
- }
336
- return _indices;
337
- } catch (err) {
338
- this.readyToStartNextLoop();
339
- return this._positionToMetaList;
340
- }
304
+ return this.getIndices();
341
305
  };
342
306
  _proto.getIndices = function getIndices() {
307
+ var _this$initialize = this.initialize(),
308
+ smallValues = _this$initialize.smallValues,
309
+ largeValues = _this$initialize.largeValues;
343
310
  try {
344
- var indices = new Array(this.bufferSize);
311
+ var indices = new Array(this._positionToMetaList.length);
312
+ var metaToPositionMap = new Map();
345
313
  for (var idx = 0; idx < indices.length; idx++) {
346
314
  var meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
347
315
  var targetIndex = this.getMetaIndex(meta);
348
- if (meta !== this.getIndexMeta(targetIndex)) {
316
+ if (!this.isThresholdMeta(meta) && meta != this.getIndexMeta(targetIndex)) {
349
317
  return this.shuffle();
350
318
  }
351
- if (meta != null) {
319
+ if (meta != null && !this.isThresholdMeta(meta)) {
320
+ var element = {
321
+ position: idx,
322
+ value: targetIndex
323
+ };
324
+ smallValues.push(element);
325
+ largeValues.push(element);
326
+ metaToPositionMap.set(meta, idx);
352
327
  indices[idx] = {
353
328
  meta: meta,
354
329
  targetIndex: targetIndex,
@@ -356,11 +331,19 @@ var IntegerBufferSet = /*#__PURE__*/function () {
356
331
  };
357
332
  }
358
333
  }
359
- this._onTheFlyIndices = [];
334
+ this._smallValues = smallValues;
335
+ this._largeValues = largeValues;
336
+ this._metaToPositionMap = metaToPositionMap;
337
+ this._positionToMetaList = indices.map(function (v) {
338
+ return v == null ? void 0 : v.meta;
339
+ });
340
+ this.resetOnTheFlies();
360
341
  return indices;
361
342
  } catch (err) {
362
- this.readyToStartNextLoop();
343
+ console.log('err ', err);
363
344
  return this._positionToMetaList;
345
+ } finally {
346
+ this.readyToStartNextLoop();
364
347
  }
365
348
  };
366
349
  _proto._pushToHeaps = function _pushToHeaps(position, value) {
@@ -372,11 +355,26 @@ var IntegerBufferSet = /*#__PURE__*/function () {
372
355
  this._largeValues.push(element);
373
356
  };
374
357
  _proto._setMetaPosition = function _setMetaPosition(meta, position) {
375
- var prevMetaOnPosition = this._positionToMetaList[position];
376
- if (prevMetaOnPosition) this._metaToPositionMap["delete"](prevMetaOnPosition);
377
358
  this._positionToMetaList[position] = meta;
378
359
  this._metaToPositionMap.set(meta, position);
379
360
  };
361
+ _proto.commitPosition = function commitPosition(props) {
362
+ var newIndex = props.newIndex,
363
+ safeRange = props.safeRange,
364
+ position = props.position,
365
+ meta = props.meta;
366
+ var onTheFlyPositionMeta = this._onTheFlyIndices[position];
367
+ var positionToReplace = position;
368
+ if (onTheFlyPositionMeta) {
369
+ if (onTheFlyPositionMeta === meta) return position;
370
+ if (this._isOnTheFlyFull) return this.getFliedPosition(newIndex, safeRange);
371
+ positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
372
+ while (this._onTheFlyIndices[positionToReplace]) {
373
+ positionToReplace = this._replaceFurthestIndexPosition(newIndex, safeRange);
374
+ }
375
+ }
376
+ return positionToReplace;
377
+ };
380
378
  _proto._setMetaIndex = function _setMetaIndex(meta, index) {
381
379
  var prevMetaIndex = this.getMetaIndex(meta);
382
380
  if (prevMetaIndex !== undefined) {
@@ -396,8 +394,12 @@ var IntegerBufferSet = /*#__PURE__*/function () {
396
394
  this._isOnTheFlyFull = false;
397
395
  };
398
396
  _proto._cleanHeaps = function _cleanHeaps() {
399
- this._cleanHeap(this._smallValues);
400
- this._cleanHeap(this._largeValues);
397
+ for (var idx = 0; idx < this._positionToMetaList.length; idx++) {
398
+ if (this._positionToMetaList[idx] == null) {
399
+ this._recreateHeaps();
400
+ return;
401
+ }
402
+ }
401
403
  var minHeapSize = Math.min(this._smallValues.size(), this._largeValues.size());
402
404
  var maxHeapSize = Math.max(this._smallValues.size(), this._largeValues.size());
403
405
  if (maxHeapSize > 10 * minHeapSize) {
@@ -405,23 +407,29 @@ var IntegerBufferSet = /*#__PURE__*/function () {
405
407
  }
406
408
  };
407
409
  _proto._recreateHeaps = function _recreateHeaps() {
408
- var sourceHeap = this._smallValues.size() < this._largeValues.size() ? this._smallValues : this._largeValues;
409
- var newSmallValues = new Heap([], this._smallerComparator);
410
- var newLargeValues = new Heap([], this._greaterComparator);
411
- while (!sourceHeap.empty()) {
412
- var element = sourceHeap.pop();
413
- if (this._metaToPositionMap.get(this._indexToMetaMap.get(element.value)) != null) {
414
- newSmallValues.push(element);
415
- newLargeValues.push(element);
410
+ var _this$initialize2 = this.initialize(),
411
+ smallValues = _this$initialize2.smallValues,
412
+ largeValues = _this$initialize2.largeValues;
413
+ for (var position = 0; position < this._positionToMetaList.length; position++) {
414
+ var meta = this._positionToMetaList[position];
415
+ var value = this.getMetaIndex(meta);
416
+ if (!meta || value === -1 || value == null) {
417
+ value = Number.MAX_SAFE_INTEGER - position;
418
+ }
419
+ var element = {
420
+ position: position,
421
+ value: value
422
+ };
423
+ smallValues.push(element);
424
+ largeValues.push(element);
425
+ if (value > thresholdNumber) {
426
+ this._setMetaPosition(value, position);
427
+ this._setMetaIndex(value, value);
416
428
  }
417
429
  }
418
- this._smallValues = newSmallValues;
419
- this._largeValues = newLargeValues;
420
- };
421
- _proto._cleanHeap = function _cleanHeap(heap) {
422
- while (!heap.empty() && this._metaToPositionMap.get(this._indexToMetaMap.get(heap.peek().value)) == null) {
423
- heap.pop();
424
- }
430
+ this._largeValues.peek().value;
431
+ this._smallValues = smallValues;
432
+ this._largeValues = largeValues;
425
433
  };
426
434
  _proto._smallerComparator = function _smallerComparator(lhs, rhs) {
427
435
  return lhs.value < rhs.value;