@x-oasis/integer-buffer-set 0.1.27 → 0.1.29

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 +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;\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) != null\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\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 const indexToMetaMap = new Map();\n const metaToIndexMap = 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 indexToMetaMap.set(targetIndex, meta);\n metaToIndexMap.set(meta, targetIndex);\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 this._indexToMetaMap = indexToMetaMap;\n this.replaceMetaToIndexMap(metaToIndexMap);\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,EAAoBY,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,6BACHnB,KAAKmB,6BAA6BF,KAAKjB,MACzCA,KAAKoB,0BAA4BC,EAC/BrB,KAAKsB,kBAAkBL,KAAKjB,OAG9BA,KAAKuB,QAAUC,KAAKC,MACpBzB,KAAK0B,eAAiB1B,KAAKuB,QAC5B,QAAAI,EAAApC,EAAAqC,UA0BA,OA1BAD,EAMDE,gBAAA,SAAgBC,GACd,MAAoB,iBAATA,GAAqBA,EAAO1C,GAExCuC,EAEDL,kBAAA,SAAkBS,GAChB,GAAW,MAAPA,EAAa,CACf,IAAMC,EAAOhC,KAAKS,iBAAiBwB,QAAO,SAACC,G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cnC,KAAKoC,IACvBzH,KAAKW,aAAa+G,OAClB1H,KAAKc,aAAa4G,QAEArC,KAAKsC,IACvB3H,KAAKW,aAAa+G,OAClB1H,KAAKc,aAAa4G,QAEF,GAAKF,GAGrBxH,KAAKuH,kBAER5F,EACD4F,eAAA,WAEE,IADA,IAAAK,EAAqC5H,KAAK8C,aAAlCC,EAAW6E,EAAX7E,YAAaC,EAAW4E,EAAX5E,YAEfwB,EAAW,EACfA,EAAWxE,KAAKO,oBAAoB6B,OACpCoC,IACA,CACA,IAAM1C,EAAO9B,KAAKO,oBAAoBiE,GAClCrF,EAAQa,KAAK4C,aAAad,GAEzBA,IAAmB,IAAX3C,GAAyB,MAATA,IAC3BA,EAAQE,OAAOC,iBAAmBkF,GAGpC,IAAMqC,EAAU,CAAErC,SAAAA,EAAUrF,MAAAA,GAC5B4D,EAAYwD,KAAKM,GACjB7D,EAAYuD,KAAKM,GACb1H,EAAQC,IAEVY,KAAK8D,iBAAiB3E,EAAOqF,GAE7BxE,KAAK6D,cAAc1E,EAAOA,IAM9Ba,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,IAljBD,WACE,OAAOnD,KAAKU,eACbqH,mBAAA5E,IAoBD,WACE,OAAOnD,KAAKO,oBAAoB6B,QAAUpC,KAAKU,8gBAChDnB,+BAxG8B"}
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;\nconst assertThresholdNumber = (val: any) =>\n typeof val === 'number' && val > thresholdNumber;\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 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 try {\n if (index == null || index < 0) return null;\n return this._metaExtractor(index);\n } catch (err) {\n return null;\n }\n }\n\n getMetaIndex(meta: Meta) {\n try {\n if (meta == null) return -1;\n if (assertThresholdNumber(meta)) return -1;\n if (this._indexExtractor) return this._indexExtractor(meta);\n return this._metaToIndexMap.get(meta);\n } catch (err) {\n return -1;\n }\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 _peek(heap: Heap) {\n return heap.peek();\n }\n\n _getMaxItem() {\n return this._peek(this._largeValues);\n }\n\n _getMinItem() {\n return this._peek(this._smallValues);\n }\n\n _getMinValue() {\n return this._peek(this._smallValues)?.value;\n }\n\n _getMaxValue() {\n return this._peek(this._largeValues)?.value;\n }\n\n // should omit thresholdNumber\n getMaxValue() {\n const stack = [];\n let item;\n\n while ((item = this._getMaxItem()) && assertThresholdNumber(item?.value)) {\n stack.push(item);\n this._largeValues.pop();\n }\n\n let stackItem;\n while ((stackItem = stack.pop())) {\n this._largeValues.push(stackItem);\n }\n\n return item?.value;\n }\n\n getMinValue() {\n const stack = [];\n let item;\n\n while ((item = this._getMinItem()) && assertThresholdNumber(item?.value)) {\n stack.push(item);\n this._smallValues.pop();\n }\n\n let stackItem;\n while ((stackItem = stack.pop())) {\n this._smallValues.push(stackItem);\n }\n\n return item?.value;\n }\n\n _push(heap: Heap, item: HeapItem) {\n heap.push(item);\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) != null\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 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._getMinValue();\n const maxValue = this._getMaxValue();\n\n if (assertThresholdNumber(maxValue)) {\n indexToReplace = maxValue;\n this._largeValues.pop();\n const replacedMeta = this._indexToMetaMap.get(indexToReplace);\n\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(options: { retry: boolean; fallback: boolean }) {\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 ...options,\n retry: true,\n });\n }\n\n // key point: `meta` should be preserved..\n getIndices(\n options = {\n retry: false,\n fallback: false,\n }\n ) {\n try {\n const { retry, fallback } = options;\n const { smallValues, largeValues } = this.initialize();\n const indices = new Array(this._positionToMetaList.length);\n const metaToPositionMap = new Map();\n const indexToMetaMap = new Map();\n const metaToIndexMap = new Map();\n for (let idx = 0; idx < indices.length; idx++) {\n const meta = fallback\n ? this._onTheFlyIndices[idx]\n : this._onTheFlyIndices[idx] || this._positionToMetaList[idx];\n const targetIndex = this.getMetaIndex(meta);\n // which means source data has changed. such as one item has been deleted\n if (\n !assertThresholdNumber(meta) &&\n meta != this.getIndexMeta(targetIndex) &&\n !retry\n ) {\n return this.shuffle(options);\n }\n if (meta != null && !assertThresholdNumber(meta)) {\n const item = { position: idx, value: targetIndex };\n this._push(smallValues, item);\n this._push(largeValues, item);\n metaToPositionMap.set(meta, idx);\n indexToMetaMap.set(targetIndex, meta);\n metaToIndexMap.set(meta, targetIndex);\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 this._indexToMetaMap = indexToMetaMap;\n this.replaceMetaToIndexMap(metaToIndexMap);\n\n return indices;\n } catch (err) {\n console.log('err ', err);\n return this.getIndices({\n ...options,\n fallback: true,\n });\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 item = { position, value };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._push(this._smallValues, item);\n this._push(this._largeValues, item);\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 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 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 // 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 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 if (meta == null || value === -1 || value == null) {\n value = Number.MAX_SAFE_INTEGER - position;\n }\n\n const item = { position, value };\n this._push(smallValues, item);\n this._push(largeValues, item);\n if (value > thresholdNumber) {\n // @ts-ignore\n this._setMetaPosition(value, position);\n // @ts-ignore\n this._setMetaIndex(value, value);\n }\n }\n this._smallValues = smallValues;\n this._largeValues = largeValues;\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","assertThresholdNumber","val","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","data","filter","v","_isOnTheFlyFull","length","resetOnTheFlies","getOnTheFlyUncriticalPosition","safeRange","startIndex","endIndex","idx","metaIndex","getMetaIndex","isClamped","initialize","smallValues","largeValues","getIndexMeta","index","err","meta","get","setMetaIndex","set","deleteMetaIndex","replaceMetaToIndexMap","newMetaToIndexMap","undefined","invariant","newPosition","_pushToHeaps","_setMetaIndex","_setMetaPosition","_peek","heap","peek","_getMaxItem","_getMinItem","_getMinValue","_this$_peek","_getMaxValue","_this$_peek2","getMaxValue","item","stackItem","stack","_item","push","pop","_item2","getMinValue","_item3","_item4","_push","getFliedPosition","newIndex","getPosition","prepare","position","indexMeta","metaPosition","commitPosition","isBufferFull","_cleanHeaps","_replaceFurthestIndexPosition","empty","indexToReplace","minValue","maxValue","replacedMeta","Math","abs","lowValue","highValue","shuffle","options","indices","Array","targetIndex","_arr","_available","indexToMetaMap","metaToIndexMap","_loop","currentIndex","currentMeta","_this","_i","findIndex","positionToMetaList","shift","getIndices","_extends","retry","fallback","_this$initialize","metaToPositionMap","recyclerKey","map","console","log","readyToStartNextLoop","onTheFlyPositionMeta","positionToReplace","prevMetaIndex","_recreateHeaps","minHeapSize","min","size","max","_this$initialize2","lhs","rhs","key"],"mappings":"2fAeA,IAAMA,EAAuB,SAACC,GAAK,OAAKA,GAElCC,EAAkBC,OAAOC,iBAAmB,IAC5CC,EAAwB,SAACC,GAAQ,MACtB,iBAARA,GAAoBA,EAAMJ,8BAyCjC,SAAAK,EAAYC,YAAAA,IAAAA,EAAqC,IAC/C,IAKSC,EAALD,EAJFE,KAAAA,WAAID,EAAG,iBAAgBA,EACvBE,EAGEH,EAHFG,eAAcC,EAGZJ,EAFFK,WAAAA,WAAUD,EAhDiB,GAgDGA,EAAAE,EAE5BN,EADFO,cAEFC,KAAKC,wBAFUH,EAAGd,EAAoBc,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,6BACHnB,KAAKmB,6BAA6BF,KAAKjB,MACzCA,KAAKoB,0BAA4BC,EAC/BrB,KAAKsB,kBAAkBL,KAAKjB,OAG9BA,KAAKuB,QAAUC,KAAKC,MACpBzB,KAAK0B,eAAiB1B,KAAKuB,QAC5B,QAAAI,EAAApC,EAAAqC,UAqBA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AKuC,IACvB1I,KAAKW,aAAa8H,OAClBzI,KAAKc,aAAa2H,QAIF,GAAKF,GAGrBvI,KAAKsI,kBAER3G,EACD2G,eAAA,WAEE,IADA,IAAAK,EAAqC3I,KAAK2C,aAAlCC,EAAW+F,EAAX/F,YAAaC,EAAW8F,EAAX9F,YAEf0C,EAAW,EACfA,EAAWvF,KAAKO,oBAAoB0B,OACpCsD,IACA,CACA,IAAMtC,EAAOjD,KAAKO,oBAAoBgF,GAClCtG,EAAQe,KAAKyC,aAAaQ,GAClB,MAARA,IAA2B,IAAXhE,GAAyB,MAATA,IAClCA,EAAQE,OAAOC,iBAAmBmG,GAGpC,IAAMf,EAAO,CAAEe,SAAAA,EAAUtG,MAAAA,GACzBe,KAAKkF,MAAMtC,EAAa4B,GACxBxE,KAAKkF,MAAMrC,EAAa2B,GACpBvF,EAAQC,IAEVc,KAAK6D,iBAAiB5E,EAAOsG,GAE7BvF,KAAK4D,cAAc3E,EAAOA,IAG9Be,KAAKW,aAAeiC,EACpB5C,KAAKc,aAAe+B,GACrBlB,EAEDd,mBAAA,SAAmB+H,EAAeC,GAChC,OAAOD,EAAI3J,MAAQ4J,EAAI5J,OACxB0C,EAEDZ,mBAAA,SAAmB6H,EAAeC,GAChC,OAAOD,EAAI3J,MAAQ4J,EAAI5J,SACxBM,OAAAuJ,iBAAA5F,IA9lBD,WACE,OAAOlD,KAAKU,eACboI,mBAAA5F,IAeD,WACE,OAAOlD,KAAKO,oBAAoB0B,QAAUjC,KAAKU,8gBAChDnB,+BArG8B"}
@@ -20,6 +20,20 @@ function _createClass(Constructor, protoProps, staticProps) {
20
20
  });
21
21
  return Constructor;
22
22
  }
23
+ function _extends() {
24
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
25
+ for (var i = 1; i < arguments.length; i++) {
26
+ var source = arguments[i];
27
+ for (var key in source) {
28
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
29
+ target[key] = source[key];
30
+ }
31
+ }
32
+ }
33
+ return target;
34
+ };
35
+ return _extends.apply(this, arguments);
36
+ }
23
37
  function _toPrimitive(input, hint) {
24
38
  if (typeof input !== "object" || input === null) return input;
25
39
  var prim = input[Symbol.toPrimitive];
@@ -40,6 +54,9 @@ var defaultMetaExtractor = function defaultMetaExtractor(value) {
40
54
  };
41
55
  var defaultBufferSize = 10;
42
56
  var thresholdNumber = Number.MAX_SAFE_INTEGER - 100000;
57
+ var assertThresholdNumber = function assertThresholdNumber(val) {
58
+ return typeof val === 'number' && val > thresholdNumber;
59
+ };
43
60
  var IntegerBufferSet = /*#__PURE__*/function () {
44
61
  function IntegerBufferSet(props) {
45
62
  if (props === void 0) {
@@ -72,10 +89,6 @@ var IntegerBufferSet = /*#__PURE__*/function () {
72
89
  this._lastUpdatedMS = this._loopMS;
73
90
  }
74
91
  var _proto = IntegerBufferSet.prototype;
75
- _proto.isThresholdMeta = function isThresholdMeta(meta) {
76
- if (typeof meta === 'number' && meta > thresholdNumber) return true;
77
- return false;
78
- };
79
92
  _proto.setIsOnTheFlyFull = function setIsOnTheFlyFull(val) {
80
93
  if (val != null) {
81
94
  var data = this._onTheFlyIndices.filter(function (v) {
@@ -107,14 +120,22 @@ var IntegerBufferSet = /*#__PURE__*/function () {
107
120
  };
108
121
  };
109
122
  _proto.getIndexMeta = function getIndexMeta(index) {
110
- if (index == null || index < 0) return null;
111
- return this._metaExtractor(index);
123
+ try {
124
+ if (index == null || index < 0) return null;
125
+ return this._metaExtractor(index);
126
+ } catch (err) {
127
+ return null;
128
+ }
112
129
  };
113
130
  _proto.getMetaIndex = function getMetaIndex(meta) {
114
- if (meta == null) return -1;
115
- if (this.isThresholdMeta(meta)) return -1;
116
- if (this._indexExtractor) return this._indexExtractor(meta);
117
- return this._metaToIndexMap.get(meta);
131
+ try {
132
+ if (meta == null) return -1;
133
+ if (assertThresholdNumber(meta)) return -1;
134
+ if (this._indexExtractor) return this._indexExtractor(meta);
135
+ return this._metaToIndexMap.get(meta);
136
+ } catch (err) {
137
+ return -1;
138
+ }
118
139
  };
119
140
  _proto.setMetaIndex = function setMetaIndex(meta, index) {
120
141
  if (!this._indexExtractor) {
@@ -143,13 +164,55 @@ var IntegerBufferSet = /*#__PURE__*/function () {
143
164
  this._setMetaPosition(meta, newPosition);
144
165
  return newPosition;
145
166
  };
146
- _proto.getMinValue = function getMinValue() {
147
- var _this$_smallValues$pe;
148
- return (_this$_smallValues$pe = this._smallValues.peek()) == null ? void 0 : _this$_smallValues$pe.value;
167
+ _proto._peek = function _peek(heap) {
168
+ return heap.peek();
169
+ };
170
+ _proto._getMaxItem = function _getMaxItem() {
171
+ return this._peek(this._largeValues);
172
+ };
173
+ _proto._getMinItem = function _getMinItem() {
174
+ return this._peek(this._smallValues);
175
+ };
176
+ _proto._getMinValue = function _getMinValue() {
177
+ var _this$_peek;
178
+ return (_this$_peek = this._peek(this._smallValues)) == null ? void 0 : _this$_peek.value;
179
+ };
180
+ _proto._getMaxValue = function _getMaxValue() {
181
+ var _this$_peek2;
182
+ return (_this$_peek2 = this._peek(this._largeValues)) == null ? void 0 : _this$_peek2.value;
149
183
  };
150
184
  _proto.getMaxValue = function getMaxValue() {
151
- var _this$_largeValues$pe;
152
- return (_this$_largeValues$pe = this._largeValues.peek()) == null ? void 0 : _this$_largeValues$pe.value;
185
+ var _item2;
186
+ var stack = [];
187
+ var item;
188
+ while ((item = this._getMaxItem()) && assertThresholdNumber((_item = item) == null ? void 0 : _item.value)) {
189
+ var _item;
190
+ stack.push(item);
191
+ this._largeValues.pop();
192
+ }
193
+ var stackItem;
194
+ while (stackItem = stack.pop()) {
195
+ this._largeValues.push(stackItem);
196
+ }
197
+ return (_item2 = item) == null ? void 0 : _item2.value;
198
+ };
199
+ _proto.getMinValue = function getMinValue() {
200
+ var _item4;
201
+ var stack = [];
202
+ var item;
203
+ while ((item = this._getMinItem()) && assertThresholdNumber((_item3 = item) == null ? void 0 : _item3.value)) {
204
+ var _item3;
205
+ stack.push(item);
206
+ this._smallValues.pop();
207
+ }
208
+ var stackItem;
209
+ while (stackItem = stack.pop()) {
210
+ this._smallValues.push(stackItem);
211
+ }
212
+ return (_item4 = item) == null ? void 0 : _item4.value;
213
+ };
214
+ _proto._push = function _push(heap, item) {
215
+ heap.push(item);
153
216
  };
154
217
  _proto.getFliedPosition = function getFliedPosition(newIndex, safeRange) {
155
218
  if (this._isOnTheFlyFull) {
@@ -210,9 +273,9 @@ var IntegerBufferSet = /*#__PURE__*/function () {
210
273
  return this._isOnTheFlyFullReturnHook(this.getNewPositionForIndex(newIndex));
211
274
  }
212
275
  var indexToReplace;
213
- var minValue = this._smallValues.peek().value;
214
- var maxValue = this._largeValues.peek().value;
215
- if (maxValue > thresholdNumber) {
276
+ var minValue = this._getMinValue();
277
+ var maxValue = this._getMaxValue();
278
+ if (assertThresholdNumber(maxValue)) {
216
279
  indexToReplace = maxValue;
217
280
  this._largeValues.pop();
218
281
  var _replacedMeta = this._indexToMetaMap.get(indexToReplace);
@@ -252,7 +315,7 @@ var IntegerBufferSet = /*#__PURE__*/function () {
252
315
  var position = this._metaToPositionMap.get(replacedMeta);
253
316
  return position;
254
317
  };
255
- _proto.shuffle = function shuffle() {
318
+ _proto.shuffle = function shuffle(options) {
256
319
  var _this = this;
257
320
  var indices = new Array(this.bufferSize);
258
321
  for (var idx = 0; idx < indices.length; idx++) {
@@ -301,30 +364,41 @@ var IntegerBufferSet = /*#__PURE__*/function () {
301
364
  }
302
365
  }
303
366
  this._positionToMetaList = positionToMetaList;
304
- return this.getIndices();
367
+ return this.getIndices(_extends({}, options, {
368
+ retry: true
369
+ }));
305
370
  };
306
- _proto.getIndices = function getIndices() {
307
- var _this$initialize = this.initialize(),
308
- smallValues = _this$initialize.smallValues,
309
- largeValues = _this$initialize.largeValues;
371
+ _proto.getIndices = function getIndices(options) {
372
+ if (options === void 0) {
373
+ options = {
374
+ retry: false,
375
+ fallback: false
376
+ };
377
+ }
310
378
  try {
379
+ var _options = options,
380
+ retry = _options.retry,
381
+ fallback = _options.fallback;
382
+ var _this$initialize = this.initialize(),
383
+ smallValues = _this$initialize.smallValues,
384
+ largeValues = _this$initialize.largeValues;
311
385
  var indices = new Array(this._positionToMetaList.length);
312
386
  var metaToPositionMap = new Map();
313
387
  var indexToMetaMap = new Map();
314
388
  var metaToIndexMap = new Map();
315
389
  for (var idx = 0; idx < indices.length; idx++) {
316
- var meta = this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
390
+ var meta = fallback ? this._onTheFlyIndices[idx] : this._onTheFlyIndices[idx] || this._positionToMetaList[idx];
317
391
  var targetIndex = this.getMetaIndex(meta);
318
- if (!this.isThresholdMeta(meta) && meta != this.getIndexMeta(targetIndex)) {
319
- return this.shuffle();
392
+ if (!assertThresholdNumber(meta) && meta != this.getIndexMeta(targetIndex) && !retry) {
393
+ return this.shuffle(options);
320
394
  }
321
- if (meta != null && !this.isThresholdMeta(meta)) {
322
- var element = {
395
+ if (meta != null && !assertThresholdNumber(meta)) {
396
+ var item = {
323
397
  position: idx,
324
398
  value: targetIndex
325
399
  };
326
- smallValues.push(element);
327
- largeValues.push(element);
400
+ this._push(smallValues, item);
401
+ this._push(largeValues, item);
328
402
  metaToPositionMap.set(meta, idx);
329
403
  indexToMetaMap.set(targetIndex, meta);
330
404
  metaToIndexMap.set(meta, targetIndex);
@@ -347,18 +421,20 @@ var IntegerBufferSet = /*#__PURE__*/function () {
347
421
  return indices;
348
422
  } catch (err) {
349
423
  console.log('err ', err);
350
- return this._positionToMetaList;
424
+ return this.getIndices(_extends({}, options, {
425
+ fallback: true
426
+ }));
351
427
  } finally {
352
428
  this.readyToStartNextLoop();
353
429
  }
354
430
  };
355
431
  _proto._pushToHeaps = function _pushToHeaps(position, value) {
356
- var element = {
432
+ var item = {
357
433
  position: position,
358
434
  value: value
359
435
  };
360
- this._smallValues.push(element);
361
- this._largeValues.push(element);
436
+ this._push(this._smallValues, item);
437
+ this._push(this._largeValues, item);
362
438
  };
363
439
  _proto._setMetaPosition = function _setMetaPosition(meta, position) {
364
440
  this._positionToMetaList[position] = meta;
@@ -419,15 +495,15 @@ var IntegerBufferSet = /*#__PURE__*/function () {
419
495
  for (var position = 0; position < this._positionToMetaList.length; position++) {
420
496
  var meta = this._positionToMetaList[position];
421
497
  var value = this.getMetaIndex(meta);
422
- if (!meta || value === -1 || value == null) {
498
+ if (meta == null || value === -1 || value == null) {
423
499
  value = Number.MAX_SAFE_INTEGER - position;
424
500
  }
425
- var element = {
501
+ var item = {
426
502
  position: position,
427
503
  value: value
428
504
  };
429
- smallValues.push(element);
430
- largeValues.push(element);
505
+ this._push(smallValues, item);
506
+ this._push(largeValues, item);
431
507
  if (value > thresholdNumber) {
432
508
  this._setMetaPosition(value, position);
433
509
  this._setMetaIndex(value, value);
@@ -1 +1 @@
1
- {"version":3,"file":"integer-buffer-set.esm.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) != null\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\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 const indexToMetaMap = new Map();\n const metaToIndexMap = 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 indexToMetaMap.set(targetIndex, meta);\n metaToIndexMap.set(meta, targetIndex);\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 this._indexToMetaMap = indexToMetaMap;\n this.replaceMetaToIndexMap(metaToIndexMap);\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","defaultBufferSize","thresholdNumber","Number","MAX_SAFE_INTEGER","IntegerBufferSet","props","_props","_props$name","name","indexExtractor","_props$bufferSize","bufferSize","_props$metaExtractor","metaExtractor","_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","process","env","NODE_ENV","invariant","newPosition","_pushToHeaps","_setMetaIndex","_setMetaPosition","getMinValue","_this$_smallValues$pe","peek","getMaxValue","_this$_largeValues$pe","getFliedPosition","newIndex","getPosition","prepare","metaPosition","position","indexMeta","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","_ret","positionToMetaList","shift","getIndices","_this$initialize","metaToPositionMap","element","recyclerKey","map","err","console","log","readyToStartNextLoop","onTheFlyPositionMeta","positionToReplace","prevMetaIndex","_recreateHeaps","minHeapSize","min","size","maxHeapSize","max","_this$initialize2","lhs","rhs","_createClass","key"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,IAAMA,oBAAoB,GAAG,SAAvBA,oBAAoBA,CAAIC,KAAK;EAAA,OAAKA,KAAK;AAAA;IAChCC,iBAAiB,GAAG;AACjC,IAAMC,eAAe,GAAGC,MAAM,CAACC,gBAAgB,GAAG,MAAM;AAAC,IAmBnDC,gBAAgB;EAsBpB,SAAAA,iBAAYC;QAAAA;MAAAA,QAAqC,EAAE;;IACjD,IAAAC,MAAA,GAKID,KAAK;MAAAE,WAAA,GAAAD,MAAA,CAJPE,IAAI;MAAJA,IAAI,GAAAD,WAAA,cAAG,gBAAgB,GAAAA,WAAA;MACvBE,cAAc,GAAAH,MAAA,CAAdG,cAAc;MAAAC,iBAAA,GAAAJ,MAAA,CACdK,UAAU;MAAVA,UAAU,GAAAD,iBAAA,cAAGV,iBAAiB,GAAAU,iBAAA;MAAAE,oBAAA,GAAAN,MAAA,CAC9BO,aAAa;MAAbA,aAAa,GAAAD,oBAAA,cAAGd,oBAAoB,GAAAc,oBAAA;IAEtC,IAAI,CAACE,cAAc,GAAGD,aAAa;IACnC,IAAI,CAACE,eAAe,GAAGN,cAAc;IAErC,IAAI,CAACO,KAAK,GAAGR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,CAAC6B,MAAM,IAAI,IAAI,CAAC1B,WAAW;;;EAC3D,OAAAnB,gBAAA;AAAA;;;;;"}
1
+ {"version":3,"file":"integer-buffer-set.esm.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;\nconst assertThresholdNumber = (val: any) =>\n typeof val === 'number' && val > thresholdNumber;\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 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 try {\n if (index == null || index < 0) return null;\n return this._metaExtractor(index);\n } catch (err) {\n return null;\n }\n }\n\n getMetaIndex(meta: Meta) {\n try {\n if (meta == null) return -1;\n if (assertThresholdNumber(meta)) return -1;\n if (this._indexExtractor) return this._indexExtractor(meta);\n return this._metaToIndexMap.get(meta);\n } catch (err) {\n return -1;\n }\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 _peek(heap: Heap) {\n return heap.peek();\n }\n\n _getMaxItem() {\n return this._peek(this._largeValues);\n }\n\n _getMinItem() {\n return this._peek(this._smallValues);\n }\n\n _getMinValue() {\n return this._peek(this._smallValues)?.value;\n }\n\n _getMaxValue() {\n return this._peek(this._largeValues)?.value;\n }\n\n // should omit thresholdNumber\n getMaxValue() {\n const stack = [];\n let item;\n\n while ((item = this._getMaxItem()) && assertThresholdNumber(item?.value)) {\n stack.push(item);\n this._largeValues.pop();\n }\n\n let stackItem;\n while ((stackItem = stack.pop())) {\n this._largeValues.push(stackItem);\n }\n\n return item?.value;\n }\n\n getMinValue() {\n const stack = [];\n let item;\n\n while ((item = this._getMinItem()) && assertThresholdNumber(item?.value)) {\n stack.push(item);\n this._smallValues.pop();\n }\n\n let stackItem;\n while ((stackItem = stack.pop())) {\n this._smallValues.push(stackItem);\n }\n\n return item?.value;\n }\n\n _push(heap: Heap, item: HeapItem) {\n heap.push(item);\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) != null\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 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._getMinValue();\n const maxValue = this._getMaxValue();\n\n if (assertThresholdNumber(maxValue)) {\n indexToReplace = maxValue;\n this._largeValues.pop();\n const replacedMeta = this._indexToMetaMap.get(indexToReplace);\n\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(options: { retry: boolean; fallback: boolean }) {\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 ...options,\n retry: true,\n });\n }\n\n // key point: `meta` should be preserved..\n getIndices(\n options = {\n retry: false,\n fallback: false,\n }\n ) {\n try {\n const { retry, fallback } = options;\n const { smallValues, largeValues } = this.initialize();\n const indices = new Array(this._positionToMetaList.length);\n const metaToPositionMap = new Map();\n const indexToMetaMap = new Map();\n const metaToIndexMap = new Map();\n for (let idx = 0; idx < indices.length; idx++) {\n const meta = fallback\n ? this._onTheFlyIndices[idx]\n : this._onTheFlyIndices[idx] || this._positionToMetaList[idx];\n const targetIndex = this.getMetaIndex(meta);\n // which means source data has changed. such as one item has been deleted\n if (\n !assertThresholdNumber(meta) &&\n meta != this.getIndexMeta(targetIndex) &&\n !retry\n ) {\n return this.shuffle(options);\n }\n if (meta != null && !assertThresholdNumber(meta)) {\n const item = { position: idx, value: targetIndex };\n this._push(smallValues, item);\n this._push(largeValues, item);\n metaToPositionMap.set(meta, idx);\n indexToMetaMap.set(targetIndex, meta);\n metaToIndexMap.set(meta, targetIndex);\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 this._indexToMetaMap = indexToMetaMap;\n this.replaceMetaToIndexMap(metaToIndexMap);\n\n return indices;\n } catch (err) {\n console.log('err ', err);\n return this.getIndices({\n ...options,\n fallback: true,\n });\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 item = { position, value };\n // We can reuse the same object in both heaps, because we don't mutate them\n this._push(this._smallValues, item);\n this._push(this._largeValues, item);\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 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 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 // 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 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 if (meta == null || value === -1 || value == null) {\n value = Number.MAX_SAFE_INTEGER - position;\n }\n\n const item = { position, value };\n this._push(smallValues, item);\n this._push(largeValues, item);\n if (value > thresholdNumber) {\n // @ts-ignore\n this._setMetaPosition(value, position);\n // @ts-ignore\n this._setMetaIndex(value, value);\n }\n }\n this._smallValues = smallValues;\n this._largeValues = largeValues;\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","defaultBufferSize","thresholdNumber","Number","MAX_SAFE_INTEGER","assertThresholdNumber","val","IntegerBufferSet","props","_props","_props$name","name","indexExtractor","_props$bufferSize","bufferSize","_props$metaExtractor","metaExtractor","_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","data","filter","v","_isOnTheFlyFull","length","resetOnTheFlies","getOnTheFlyUncriticalPosition","safeRange","startIndex","endIndex","idx","meta","metaIndex","getMetaIndex","isClamped","initialize","smallValues","largeValues","getIndexMeta","index","err","get","setMetaIndex","set","deleteMetaIndex","replaceMetaToIndexMap","newMetaToIndexMap","undefined","process","env","NODE_ENV","invariant","newPosition","_pushToHeaps","_setMetaIndex","_setMetaPosition","_peek","heap","peek","_getMaxItem","_getMinItem","_getMinValue","_this$_peek","_getMaxValue","_this$_peek2","getMaxValue","stack","item","_item","push","pop","stackItem","_item2","getMinValue","_item3","_item4","_push","getFliedPosition","newIndex","getPosition","prepare","metaPosition","position","indexMeta","commitPosition","isBufferFull","_cleanHeaps","_replaceFurthestIndexPosition","empty","indexToReplace","minValue","maxValue","replacedMeta","Math","abs","lowValue","highValue","shuffle","options","indices","Array","targetIndex","_arr","_available","indexToMetaMap","metaToIndexMap","_loop","currentIndex","currentMeta","_this","_i","findIndex","_ret","positionToMetaList","shift","getIndices","_extends","retry","fallback","_options","_this$initialize","metaToPositionMap","recyclerKey","map","console","log","readyToStartNextLoop","onTheFlyPositionMeta","positionToReplace","prevMetaIndex","_recreateHeaps","minHeapSize","min","size","maxHeapSize","max","_this$initialize2","lhs","rhs","_createClass","key"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,IAAMA,oBAAoB,GAAG,SAAvBA,oBAAoBA,CAAIC,KAAK;EAAA,OAAKA,KAAK;AAAA;IAChCC,iBAAiB,GAAG;AACjC,IAAMC,eAAe,GAAGC,MAAM,CAACC,gBAAgB,GAAG,MAAM;AACxD,IAAMC,qBAAqB,GAAG,SAAxBA,qBAAqBA,CAAIC,GAAQ;EAAA,OACrC,OAAOA,GAAG,KAAK,QAAQ,IAAIA,GAAG,GAAGJ,eAAe;AAAA;AAAC,IAmB7CK,gBAAgB;EAsBpB,SAAAA,iBAAYC;QAAAA;MAAAA,QAAqC,EAAE;;IACjD,IAAAC,MAAA,GAKID,KAAK;MAAAE,WAAA,GAAAD,MAAA,CAJPE,IAAI;MAAJA,IAAI,GAAAD,WAAA,cAAG,gBAAgB,GAAAA,WAAA;MACvBE,cAAc,GAAAH,MAAA,CAAdG,cAAc;MAAAC,iBAAA,GAAAJ,MAAA,CACdK,UAAU;MAAVA,UA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,KAAK,GAAGG,MAAM,CAACC,gBAAgB,GAAGuG,QAAQ;;MAG5C,IAAMf,IAAI,GAAG;QAAEe,QAAQ,EAARA,QAAQ;QAAE3G,KAAK,EAALA;OAAO;MAChC,IAAI,CAACqG,KAAK,CAACxC,WAAW,EAAE+B,IAAI,CAAC;MAC7B,IAAI,CAACS,KAAK,CAACvC,WAAW,EAAE8B,IAAI,CAAC;MAC7B,IAAI5F,KAAK,GAAGE,eAAe,EAAE;QAE3B,IAAI,CAAC8E,gBAAgB,CAAChF,KAAK,EAAE2G,QAAQ,CAAC;QAEtC,IAAI,CAAC5B,aAAa,CAAC/E,KAAK,EAAEA,KAAK,CAAC;;;IAGpC,IAAI,CAAC2B,YAAY,GAAGkC,WAAW;IAC/B,IAAI,CAAC/B,YAAY,GAAGgC,WAAW;GAChC;EAAAnB,MAAA,CAEDd,kBAAkB,GAAlB,SAAAA,mBAAmBqI,GAAa,EAAEC,GAAa;IAC7C,OAAOD,GAAG,CAAClK,KAAK,GAAGmK,GAAG,CAACnK,KAAK;GAC7B;EAAA2C,MAAA,CAEDZ,kBAAkB,GAAlB,SAAAA,mBAAmBmI,GAAa,EAAEC,GAAa;IAC7C,OAAOD,GAAG,CAAClK,KAAK,GAAGmK,GAAG,CAACnK,KAAK;GAC7B;EAAAoK,YAAA,CAAA7J,gBAAA;IAAA8J,GAAA;IAAAnG,GAAA,EA9lBD,SAAAA;MACE,OAAO,IAAI,CAACxC,WAAW;;;IACxB2I,GAAA;IAAAnG,GAAA,EAeD,SAAAA;MACE,OAAO,IAAI,CAAC3C,mBAAmB,CAAC0B,MAAM,IAAI,IAAI,CAACvB,WAAW;;;EAC3D,OAAAnB,gBAAA;AAAA;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-oasis/integer-buffer-set",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
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.27",
19
+ "@x-oasis/heap": "0.1.29",
20
20
  "@x-oasis/invariant": "^0.1.19",
21
21
  "@x-oasis/is-clamped": "^0.1.19",
22
22
  "@x-oasis/return-hook": "^0.1.19"