@x-oasis/prefix-interval-tree 0.5.0 → 0.7.0
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":"prefix-interval-tree.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["const parent = (node: number) => Math.floor(node / 2);\n\nconst createArray = function (size: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = 0;\n }\n return xs;\n};\n\n/**\n * Computes the next power of 2 after or equal to x.\n */\nfunction ceilLog2(x: number) {\n let y = 1;\n while (y < x) {\n y *= 2;\n }\n\n return y;\n}\n\ntype OperationType = 'set' | 'remove';\n\ntype Operation = {\n type: OperationType;\n value: any;\n};\n\n/**\n * A prefix interval tree stores an numeric array and the partial sums of that\n * array. It is optimized for updating the values of the array without\n * recomputing all of the partial sums.\n *\n * - O(ln n) update\n * - O(1) lookup\n * - O(ln n) compute a partial sum\n * - O(n) space\n *\n * Note that the sequence of partial sums is one longer than the array, so that\n * the first partial sum is always 0, and the last partial sum is the sum of the\n * entire array.\n */\nclass PrefixIntervalTree {\n private _size: number;\n /**\n * Half the size of the heap. It is also the number of non-leaf nodes, and the\n * index of the first element in the heap. Always a power of 2.\n */\n private _half: number;\n private _heap: number[];\n\n private _actualSize: number;\n\n private _onUpdateItemLayout: Function;\n private _onUpdateIntervalTree: Function;\n\n private _affectedIndicesMap: {\n [key: string]: Operation;\n } = {};\n private _affectedMinimalIndex: number = undefined;\n private _cachedSumUntilValues: {\n [key: string]: number;\n } = {};\n\n constructor(\n xs: number[] | number = 10,\n opts?: {\n onUpdateItemLayout?: Function;\n onUpdateIntervalTree?: Function;\n }\n ) {\n if (typeof xs === 'number') this.initWithNumber(xs);\n if (Array.isArray(xs)) this.initWithArray(xs);\n\n const { onUpdateItemLayout, onUpdateIntervalTree } = opts || {};\n this._onUpdateIntervalTree = onUpdateIntervalTree;\n this._onUpdateItemLayout = onUpdateItemLayout;\n }\n\n initWithNumber(length: number) {\n this._half = ceilLog2(length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n this._actualSize = 0;\n }\n\n initWithArray(arr: number[]) {\n this._half = ceilLog2(arr.length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n let i;\n for (i = 0; i < this._size; ++i) {\n this._heap[this._half + i] = arr[i];\n }\n\n for (i = this._half - 1; i > 0; --i) {\n this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];\n }\n this._actualSize = arr.length;\n }\n\n static uniform(size: number, initialValue: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = initialValue;\n }\n\n return new PrefixIntervalTree(xs);\n }\n\n static empty(size: number) {\n return PrefixIntervalTree.uniform(size, 0);\n }\n\n /**\n * the length should be 2\n * it will cause calculation of\n */\n stretch() {\n const nextHeap = createArray(2 * this._half * 2);\n const nextHeapHalf = this._half * 2;\n\n // copy old value to new one\n for (let i = 0; i < this._size; i++) {\n nextHeap[nextHeapHalf + i] = this._heap[this._half + i] || 0;\n }\n\n // sum old value to create new sum value\n for (let i = nextHeapHalf - 1; i > 0; i--) {\n nextHeap[i] = nextHeap[2 * i] + nextHeap[2 * i + 1];\n }\n\n this._half = nextHeapHalf;\n this._size = nextHeapHalf;\n this._heap = nextHeap;\n }\n\n indexKey(index: number) {\n return `${index}`;\n }\n\n resolveKeyIndex(key: string) {\n return parseInt(key, 10);\n }\n\n isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowDirectParent(index: number) {\n this.resetAffected();\n let node = this._half + index;\n\n node = parent(node);\n for (; node !== 0; node = parent(node)) {\n this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];\n }\n }\n\n // recalculate parent value from startIndex to endIndex\n reflowHeap(_startIndex: number, endIndex = this._half * 2 - 2) {\n this.resetAffected();\n const depth = Math.log2(this._size);\n const startIndex = _startIndex + this._half;\n\n Array.from({ length: depth }, (v, i) => i).reduce(\n (acc) => {\n const { startIndex, endIndex } = acc;\n const _nextStart = parent(startIndex);\n const _nextEnd = parent(endIndex);\n\n for (let idx = _nextStart; idx <= _nextEnd; idx++) {\n this._heap[idx] = this._heap[2 * idx] + this._heap[2 * idx + 1];\n }\n\n return {\n startIndex: _nextStart,\n endIndex: _nextEnd,\n };\n },\n {\n startIndex,\n endIndex,\n }\n );\n }\n\n /**\n *\n * @param index index to remove\n * @returns\n *\n * update heap only, but not recalculate interval tree value\n */\n dryRemove(index: number) {\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n this._heap.splice(this._half + index, 1);\n this._heap.push(0);\n this._actualSize = this._actualSize - 1;\n\n this.updateAffectedIndices(index, 'remove');\n }\n\n remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.removeIndices([index]);\n }\n\n removeIndices(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => this.dryRemove(index));\n\n this.reflowHeap(indices[0]);\n this.onUpdateIntervalTree();\n\n if (typeof this._onUpdateItemLayout === 'function') {\n for (let idx = indices[0]; idx < this._half; idx++) {\n this._onUpdateItemLayout(idx, this.get(idx));\n }\n }\n }\n\n removeV0(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n const nextHeap = createArray(this._half * 2);\n const copy = this._heap.slice(this._half);\n copy.splice(index, 1);\n\n for (let index = this._half; index < this._half * 2; index++) {\n nextHeap[index] = copy[index - this._half] || 0;\n }\n\n for (let index = this._half - 1; index > 0; index--) {\n nextHeap[index] = nextHeap[2 * index] + nextHeap[2 * index + 1];\n }\n\n this._actualSize = this._actualSize - 1;\n this._heap = nextHeap;\n }\n\n resetAffected() {\n this._affectedIndicesMap = {};\n this._affectedMinimalIndex = undefined;\n }\n\n updateAffectedIndices(index: number, type: OperationType, value?: number) {\n if (typeof index === 'number') {\n this._affectedMinimalIndex =\n typeof this._affectedMinimalIndex === 'number'\n ? Math.min(this._affectedMinimalIndex, index)\n : index;\n this._affectedIndicesMap[this.indexKey(index)] = {\n value,\n type,\n };\n }\n }\n\n onUpdateIntervalTree() {\n // reset cached value\n this._cachedSumUntilValues = {};\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n }\n\n drySet(index: number, value: number) {\n if (typeof index !== 'number' || index < 0) return false;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return false;\n }\n\n while (index >= this._half) {\n this.stretch();\n }\n\n const node = this._half + index;\n this._heap[node] = value;\n\n this.updateAffectedIndices(index, 'set', value);\n\n if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n return true;\n }\n\n set(index: number, value: number) {\n this.drySet(index, value);\n this.reflowDirectParent(index);\n this.onUpdateIntervalTree();\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\n }\n\n applyUpdate() {\n if (typeof this._affectedMinimalIndex === 'number') {\n const index = this._affectedMinimalIndex;\n\n const keys = Object.keys(this._affectedIndicesMap);\n const hasRemoveItem = keys.some(\n (key) => this._affectedIndicesMap[key].type === 'remove'\n );\n\n if (hasRemoveItem) {\n return this.reflowHeap(index);\n }\n\n const depth = Math.log2(this._size);\n const itemLength = keys.length;\n\n const reflowTimes = this._half - index - 1;\n\n if (depth * itemLength > reflowTimes) {\n return this.reflowHeap(index);\n }\n\n keys.forEach((key) => this.reflowDirectParent(this.resolveKeyIndex(key)));\n\n this.onUpdateIntervalTree();\n\n this.resetAffected();\n }\n }\n\n getMaxUsefulLength() {\n return this.getActualSize();\n }\n\n getActualSize() {\n return this._actualSize;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n const node = this._half + index;\n return this._heap[node];\n }\n\n getSize() {\n return this._size;\n }\n\n getHalf() {\n return this._half;\n }\n\n getHeap() {\n return this._heap;\n }\n\n getMaxValue() {\n return this._heap[1];\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(end - 1).\n * End is not included. if end less than 0, then return 0\n */\n sumUntil(end: number) {\n // invariant(end >= 0 && end < this._size + 1, 'Index out of range %s', end);\n\n if (this._cachedSumUntilValues[`${end}`] != null)\n return this._cachedSumUntilValues[`${end}`];\n if (end <= 0) {\n return 0;\n }\n\n let node = this._half + end - 1;\n let sum = this._heap[node];\n\n for (; node !== 1; node = parent(node)) {\n if (node % 2 === 1) {\n sum += this._heap[node - 1];\n }\n }\n\n return sum;\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(inclusiveEnd).\n */\n sumTo(inclusiveEnd: number) {\n // invariant(\n // inclusiveEnd >= 0 && inclusiveEnd < this._size,\n // 'Index out of range %s',\n // inclusiveEnd\n // );\n return this.sumUntil(inclusiveEnd + 1);\n }\n\n /**\n * Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).\n * end length is not included\n */\n sum(begin: number, end: number) {\n // invariant(begin <= end, 'Begin must precede end');\n return this.sumUntil(end) - this.sumUntil(begin);\n }\n\n /**\n * return the biggest i, sumUntil(i) === t\n * return the biggest i, subUntil(i) < t\n */\n greatestLowerBound(t: number) {\n if (t < 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n // not use this._size;this._size always be a big value\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t < leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n * Return the biggest i, subUntil(i) < t\n * or -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t <= leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 0, 100, 0, 0, 100].\n * then computeRange(100, 200) => { startIndex: 2, endIndex: 6 }\n *\n * item index in viewport will be [2, 3, 4, 5], index 6 is not\n * included just like Array.slice(start, end)\n *\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._actualSize,\n endIndex: this._actualSize,\n };\n }\n\n return {\n // the biggest item, value <= minOffset\n startIndex: this.greatestLowerBound(minOffset),\n\n // the smallest item, value > maxOffset\n endIndex: this.leastStrictUpperBound(maxOffset),\n };\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and t <= sumUntil(i), or\n * size + 1 if no such i exists.\n */\n leastUpperBound(t: number) {\n return Math.min(this.greatestStrictLowerBound(t) + 1, this._actualSize);\n }\n\n /**\n * Returns the smallest i, t < sumUntil(i), it should be used as range end\n * size + 1 if no such i exists.\n */\n leastStrictUpperBound(t: number) {\n return Math.min(this.greatestLowerBound(t) + 1, this._actualSize);\n }\n}\n\nexport default PrefixIntervalTree;\n"],"names":["parent","node","Math","floor","createArray","size","xs","i","ceilLog2","x","y","PrefixIntervalTree","opts","undefined","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","onUpdateIntervalTree","_onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","indexKey","index","resolveKeyIndex","key","parseInt","isValidIndex","reflowDirectParent","resetAffected","reflowHeap","_startIndex","endIndex","depth","log2","startIndex","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","dryRemove","isNaN","console","warn","splice","push","updateAffectedIndices","remove","removeIndices","indices","sort","a","b","forEach","_this2","get","removeV0","copy","slice","_affectedIndicesMap","_affectedMinimalIndex","type","value","min","_cachedSumUntilValues","drySet","set","applyUpdate","keys","Object","hasRemoveItem","some","_this3","itemLength","reflowTimes","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","greatestStrictLowerBound","computeRange","minOffset","maxOffset","leastStrictUpperBound","leastUpperBound"],"mappings":";;;;AAAA,IAAMA,MAAM,GAAG,SAATA,MAAMA,CAAIC,IAAY;EAAA,OAAKC,IAAI,CAACC,KAAK,CAACF,IAAI,GAAG,CAAC,CAAC;AAAA;AAErD,IAAMG,WAAW,GAAG,SAAdA,WAAWA,CAAaC,IAAY;EACxC,IAAMC,EAAE,GAAG,EAAE;EACb,KAAK,IAAIC,CAAC,GAAGF,IAAI,GAAG,CAAC,EAAEE,CAAC,IAAI,CAAC,EAAE,EAAEA,CAAC,EAAE;IAClCD,EAAE,CAACC,CAAC,CAAC,GAAG,CAAC;;EAEX,OAAOD,EAAE;AACX,CAAC;AAKD,SAASE,QAAQA,CAACC,CAAS;EACzB,IAAIC,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGD,CAAC,EAAE;IACZC,CAAC,IAAI,CAAC;;EAGR,OAAOA,CAAC;AACV;AAAC,IAuBKC,kBAAkB;EAsBtB,SAAAA,mBACEL,IACAM,IAGC;QAJDN;MAAAA,KAAwB,EAAE;;IATpB,wBAAmB,GAEvB,EAAE;IACE,0BAAqB,GAAWO,SAAS;IACzC,0BAAqB,GAEzB,EAAE;IASJ,IAAI,OAAOP,EAAE,KAAK,QAAQ,EAAE,IAAI,CAACQ,cAAc,CAACR,EAAE,CAAC;IACnD,IAAIS,KAAK,CAACC,OAAO,CAACV,EAAE,CAAC,EAAE,IAAI,CAACW,aAAa,CAACX,EAAE,CAAC;IAE7C,IAAAY,IAAA,GAAqDN,IAAI,IAAI,EAAE;MAAvDO,kBAAkB,GAAAD,IAAA,CAAlBC,kBAAkB;MAAEC,oBAA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I,GAAG,CAAC;QACnB0G,CAAC,IAAIC,OAAO;;;IAIhB,OAAO1G,IAAI,CAACgF,GAAG,CAACjF,IAAI,GAAG,IAAI,CAACyB,KAAK,EAAE,IAAI,CAACG,WAAW,CAAC;GACrD;EAAAN,MAAA,CAgBDuF,YAAY,GAAZ,SAAAA,aAAaC,SAAiB,EAAEC,SAAiB;IAC/C,IAAI,IAAI,CAACd,OAAO,EAAE,CAAC,CAAC,CAAC,GAAGa,SAAS,EAAE;MACjC,OAAO;QACL7D,UAAU,EAAE,IAAI,CAACrB,WAAW;QAC5BkB,QAAQ,EAAE,IAAI,CAAClB;OAChB;;IAGH,OAAO;MAELqB,UAAU,EAAE,IAAI,CAACwD,kBAAkB,CAACK,SAAS,CAAC;MAG9ChE,QAAQ,EAAE,IAAI,CAACkE,qBAAqB,CAACD,SAAS;KAC/C;GACF;EAAAzF,MAAA,CAMD2F,eAAe,GAAf,SAAAA,gBAAgBP,CAAS;IACvB,OAAOzG,IAAI,CAACgF,GAAG,CAAC,IAAI,CAAC2B,wBAAwB,CAACF,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC9E,WAAW,CAAC;GACxE;EAAAN,MAAA,CAMD0F,qBAAqB,GAArB,SAAAA,sBAAsBN,CAAS;IAC7B,OAAOzG,IAAI,CAACgF,GAAG,CAAC,IAAI,CAACwB,kBAAkB,CAACC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC9E,WAAW,CAAC;GAClE;EAAA,OAAAlB,kBAAA;AAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"prefix-interval-tree.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["const parent = (node: number) => Math.floor(node / 2);\n\nconst createArray = function (size: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = 0;\n }\n return xs;\n};\n\n/**\n * Computes the next power of 2 after or equal to x.\n */\nfunction ceilLog2(x: number) {\n let y = 1;\n while (y < x) {\n y *= 2;\n }\n\n return y;\n}\n\ntype OperationType = 'set' | 'remove';\n\ntype Operation = {\n type: OperationType;\n value: any;\n};\n\n/**\n * A prefix interval tree stores an numeric array and the partial sums of that\n * array. It is optimized for updating the values of the array without\n * recomputing all of the partial sums.\n *\n * - O(ln n) update\n * - O(1) lookup\n * - O(ln n) compute a partial sum\n * - O(n) space\n *\n * Note that the sequence of partial sums is one longer than the array, so that\n * the first partial sum is always 0, and the last partial sum is the sum of the\n * entire array.\n */\nclass PrefixIntervalTree {\n private _size: number;\n /**\n * Half the size of the heap. It is also the number of non-leaf nodes, and the\n * index of the first element in the heap. Always a power of 2.\n */\n private _half: number;\n private _heap: number[];\n\n private _actualSize: number;\n\n private _onUpdateItemLayout: Function;\n private _onUpdateIntervalTree: Function;\n\n private _affectedIndicesMap: Record<string, Operation> = {};\n private _affectedMinimalIndex: number = undefined;\n private _cachedSumUntilValues: Record<string, number> = {};\n\n constructor(\n xs: number[] | number = 10,\n opts?: {\n onUpdateItemLayout?: Function;\n onUpdateIntervalTree?: Function;\n }\n ) {\n if (typeof xs === 'number') this.initWithNumber(xs);\n if (Array.isArray(xs)) this.initWithArray(xs);\n\n const { onUpdateItemLayout, onUpdateIntervalTree } = opts || {};\n this._onUpdateIntervalTree = onUpdateIntervalTree;\n this._onUpdateItemLayout = onUpdateItemLayout;\n }\n\n initWithNumber(length: number) {\n this._half = ceilLog2(length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n this._actualSize = 0;\n }\n\n initWithArray(arr: number[]) {\n this._half = ceilLog2(arr.length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n let i;\n for (i = 0; i < this._size; ++i) {\n this._heap[this._half + i] = arr[i];\n }\n\n for (i = this._half - 1; i > 0; --i) {\n this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];\n }\n this._actualSize = arr.length;\n }\n\n static uniform(size: number, initialValue: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = initialValue;\n }\n\n return new PrefixIntervalTree(xs);\n }\n\n static empty(size: number) {\n return PrefixIntervalTree.uniform(size, 0);\n }\n\n /**\n * the length should be 2\n * it will cause calculation of\n */\n stretch() {\n const nextHeap = createArray(2 * this._half * 2);\n const nextHeapHalf = this._half * 2;\n\n // copy old value to new one\n for (let i = 0; i < this._size; i++) {\n nextHeap[nextHeapHalf + i] = this._heap[this._half + i] || 0;\n }\n\n // sum old value to create new sum value\n for (let i = nextHeapHalf - 1; i > 0; i--) {\n nextHeap[i] = nextHeap[2 * i] + nextHeap[2 * i + 1];\n }\n\n this._half = nextHeapHalf;\n this._size = nextHeapHalf;\n this._heap = nextHeap;\n }\n\n indexKey(index: number) {\n return `${index}`;\n }\n\n resolveKeyIndex(key: string) {\n return parseInt(key, 10);\n }\n\n isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowDirectParent(index: number) {\n this.resetAffected();\n let node = this._half + index;\n\n node = parent(node);\n for (; node !== 0; node = parent(node)) {\n this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];\n }\n }\n\n // recalculate parent value from startIndex to endIndex\n reflowHeap(_startIndex: number, endIndex = this._half * 2 - 2) {\n this.resetAffected();\n const depth = Math.log2(this._size);\n const startIndex = _startIndex + this._half;\n\n Array.from({ length: depth }, (v, i) => i).reduce(\n (acc) => {\n const { startIndex, endIndex } = acc;\n const _nextStart = parent(startIndex);\n const _nextEnd = parent(endIndex);\n\n for (let idx = _nextStart; idx <= _nextEnd; idx++) {\n this._heap[idx] = this._heap[2 * idx] + this._heap[2 * idx + 1];\n }\n\n return {\n startIndex: _nextStart,\n endIndex: _nextEnd,\n };\n },\n {\n startIndex,\n endIndex,\n }\n );\n }\n\n /**\n *\n * @param index index to remove\n * @returns\n *\n * update heap only, but not recalculate interval tree value\n */\n dryRemove(index: number) {\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n this._heap.splice(this._half + index, 1);\n this._heap.push(0);\n this._actualSize = this._actualSize - 1;\n\n this.updateAffectedIndices(index, 'remove');\n }\n\n remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.removeIndices([index]);\n }\n\n removeIndices(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => this.dryRemove(index));\n\n this.reflowHeap(indices[0]);\n this.onUpdateIntervalTree();\n\n if (typeof this._onUpdateItemLayout === 'function') {\n for (let idx = indices[0]; idx < this._half; idx++) {\n this._onUpdateItemLayout(idx, this.get(idx));\n }\n }\n }\n\n removeV0(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n const nextHeap = createArray(this._half * 2);\n const copy = this._heap.slice(this._half);\n copy.splice(index, 1);\n\n for (let index = this._half; index < this._half * 2; index++) {\n nextHeap[index] = copy[index - this._half] || 0;\n }\n\n for (let index = this._half - 1; index > 0; index--) {\n nextHeap[index] = nextHeap[2 * index] + nextHeap[2 * index + 1];\n }\n\n this._actualSize = this._actualSize - 1;\n this._heap = nextHeap;\n }\n\n resetAffected() {\n this._affectedIndicesMap = {};\n this._affectedMinimalIndex = undefined;\n }\n\n updateAffectedIndices(index: number, type: OperationType, value?: number) {\n if (typeof index === 'number') {\n this._affectedMinimalIndex =\n typeof this._affectedMinimalIndex === 'number'\n ? Math.min(this._affectedMinimalIndex, index)\n : index;\n this._affectedIndicesMap[this.indexKey(index)] = {\n value,\n type,\n };\n }\n }\n\n onUpdateIntervalTree() {\n // reset cached value\n this._cachedSumUntilValues = {};\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n }\n\n drySet(index: number, value: number) {\n if (typeof index !== 'number' || index < 0) return false;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return false;\n }\n\n while (index >= this._half) {\n this.stretch();\n }\n\n const node = this._half + index;\n this._heap[node] = value;\n\n this.updateAffectedIndices(index, 'set', value);\n\n if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n return true;\n }\n\n set(index: number, value: number) {\n this.drySet(index, value);\n this.reflowDirectParent(index);\n this.onUpdateIntervalTree();\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\n }\n\n applyUpdate() {\n if (typeof this._affectedMinimalIndex === 'number') {\n const index = this._affectedMinimalIndex;\n\n const keys = Object.keys(this._affectedIndicesMap);\n const hasRemoveItem = keys.some(\n (key) => this._affectedIndicesMap[key].type === 'remove'\n );\n\n if (hasRemoveItem) {\n return this.reflowHeap(index);\n }\n\n const depth = Math.log2(this._size);\n const itemLength = keys.length;\n\n const reflowTimes = this._half - index - 1;\n\n if (depth * itemLength > reflowTimes) {\n return this.reflowHeap(index);\n }\n\n keys.forEach((key) => this.reflowDirectParent(this.resolveKeyIndex(key)));\n\n this.onUpdateIntervalTree();\n\n this.resetAffected();\n }\n }\n\n getMaxUsefulLength() {\n return this.getActualSize();\n }\n\n getActualSize() {\n return this._actualSize;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n const node = this._half + index;\n return this._heap[node];\n }\n\n getSize() {\n return this._size;\n }\n\n getHalf() {\n return this._half;\n }\n\n getHeap() {\n return this._heap;\n }\n\n getMaxValue() {\n return this._heap[1];\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(end - 1).\n * End is not included. if end less than 0, then return 0\n */\n sumUntil(end: number) {\n // invariant(end >= 0 && end < this._size + 1, 'Index out of range %s', end);\n\n if (this._cachedSumUntilValues[`${end}`] != null)\n return this._cachedSumUntilValues[`${end}`];\n if (end <= 0) {\n return 0;\n }\n\n let node = this._half + end - 1;\n let sum = this._heap[node];\n\n for (; node !== 1; node = parent(node)) {\n if (node % 2 === 1) {\n sum += this._heap[node - 1];\n }\n }\n\n return sum;\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(inclusiveEnd).\n */\n sumTo(inclusiveEnd: number) {\n // invariant(\n // inclusiveEnd >= 0 && inclusiveEnd < this._size,\n // 'Index out of range %s',\n // inclusiveEnd\n // );\n return this.sumUntil(inclusiveEnd + 1);\n }\n\n /**\n * Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).\n * end length is not included\n */\n sum(begin: number, end: number) {\n // invariant(begin <= end, 'Begin must precede end');\n return this.sumUntil(end) - this.sumUntil(begin);\n }\n\n /**\n * return the biggest i, sumUntil(i) === t\n * return the biggest i, subUntil(i) < t\n */\n greatestLowerBound(t: number) {\n if (t < 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n // not use this._size;this._size always be a big value\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t < leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n * Return the biggest i, subUntil(i) < t\n * or -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t <= leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 0, 100, 0, 0, 100].\n * then computeRange(100, 200) => { startIndex: 2, endIndex: 6 }\n *\n * item index in viewport will be [2, 3, 4, 5], index 6 is not\n * included just like Array.slice(start, end)\n *\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._actualSize,\n endIndex: this._actualSize,\n };\n }\n\n return {\n // the biggest item, value <= minOffset\n startIndex: this.greatestLowerBound(minOffset),\n\n // the smallest item, value > maxOffset\n endIndex: this.leastStrictUpperBound(maxOffset),\n };\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and t <= sumUntil(i), or\n * size + 1 if no such i exists.\n */\n leastUpperBound(t: number) {\n return Math.min(this.greatestStrictLowerBound(t) + 1, this._actualSize);\n }\n\n /**\n * Returns the smallest i, t < sumUntil(i), it should be used as range end\n * size + 1 if no such i exists.\n */\n leastStrictUpperBound(t: number) {\n return Math.min(this.greatestLowerBound(t) + 1, this._actualSize);\n }\n}\n\nexport default PrefixIntervalTree;\n"],"names":["parent","node","Math","floor","createArray","size","xs","i","ceilLog2","x","y","PrefixIntervalTree","opts","undefined","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","onUpdateIntervalTree","_onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","indexKey","index","resolveKeyIndex","key","parseInt","isValidIndex","reflowDirectParent","resetAffected","reflowHeap","_startIndex","endIndex","depth","log2","startIndex","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","dryRemove","isNaN","console","warn","splice","push","updateAffectedIndices","remove","removeIndices","indices","sort","a","b","forEach","_this2","get","removeV0","copy","slice","_affectedIndicesMap","_affectedMinimalIndex","type","value","min","_cachedSumUntilValues","drySet","set","applyUpdate","keys","Object","hasRemoveItem","some","_this3","itemLength","reflowTimes","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","greatestStrictLowerBound","computeRange","minOffset","maxOffset","leastStrictUpperBound","leastUpperBound"],"mappings":";;;;AAAA,IAAMA,MAAM,GAAG,SAATA,MAAMA,CAAIC,IAAY;EAAA,OAAKC,IAAI,CAACC,KAAK,CAACF,IAAI,GAAG,CAAC,CAAC;AAAA;AAErD,IAAMG,WAAW,GAAG,SAAdA,WAAWA,CAAaC,IAAY;EACxC,IAAM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kBAAkB,CAACK,SAAS,CAAC;MAG9ChE,QAAQ,EAAE,IAAI,CAACkE,qBAAqB,CAACD,SAAS;KAC/C;GACF;EAAAzF,MAAA,CAMD2F,eAAe,GAAf,SAAAA,gBAAgBP,CAAS;IACvB,OAAOzG,IAAI,CAACgF,GAAG,CAAC,IAAI,CAAC2B,wBAAwB,CAACF,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC9E,WAAW,CAAC;GACxE;EAAAN,MAAA,CAMD0F,qBAAqB,GAArB,SAAAA,sBAAsBN,CAAS;IAC7B,OAAOzG,IAAI,CAACgF,GAAG,CAAC,IAAI,CAACwB,kBAAkB,CAACC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC9E,WAAW,CAAC;GAClE;EAAA,OAAAlB,kBAAA;AAAA;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefix-interval-tree.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["const parent = (node: number) => Math.floor(node / 2);\n\nconst createArray = function (size: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = 0;\n }\n return xs;\n};\n\n/**\n * Computes the next power of 2 after or equal to x.\n */\nfunction ceilLog2(x: number) {\n let y = 1;\n while (y < x) {\n y *= 2;\n }\n\n return y;\n}\n\ntype OperationType = 'set' | 'remove';\n\ntype Operation = {\n type: OperationType;\n value: any;\n};\n\n/**\n * A prefix interval tree stores an numeric array and the partial sums of that\n * array. It is optimized for updating the values of the array without\n * recomputing all of the partial sums.\n *\n * - O(ln n) update\n * - O(1) lookup\n * - O(ln n) compute a partial sum\n * - O(n) space\n *\n * Note that the sequence of partial sums is one longer than the array, so that\n * the first partial sum is always 0, and the last partial sum is the sum of the\n * entire array.\n */\nclass PrefixIntervalTree {\n private _size: number;\n /**\n * Half the size of the heap. It is also the number of non-leaf nodes, and the\n * index of the first element in the heap. Always a power of 2.\n */\n private _half: number;\n private _heap: number[];\n\n private _actualSize: number;\n\n private _onUpdateItemLayout: Function;\n private _onUpdateIntervalTree: Function;\n\n private _affectedIndicesMap: {\n [key: string]: Operation;\n } = {};\n private _affectedMinimalIndex: number = undefined;\n private _cachedSumUntilValues: {\n [key: string]: number;\n } = {};\n\n constructor(\n xs: number[] | number = 10,\n opts?: {\n onUpdateItemLayout?: Function;\n onUpdateIntervalTree?: Function;\n }\n ) {\n if (typeof xs === 'number') this.initWithNumber(xs);\n if (Array.isArray(xs)) this.initWithArray(xs);\n\n const { onUpdateItemLayout, onUpdateIntervalTree } = opts || {};\n this._onUpdateIntervalTree = onUpdateIntervalTree;\n this._onUpdateItemLayout = onUpdateItemLayout;\n }\n\n initWithNumber(length: number) {\n this._half = ceilLog2(length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n this._actualSize = 0;\n }\n\n initWithArray(arr: number[]) {\n this._half = ceilLog2(arr.length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n let i;\n for (i = 0; i < this._size; ++i) {\n this._heap[this._half + i] = arr[i];\n }\n\n for (i = this._half - 1; i > 0; --i) {\n this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];\n }\n this._actualSize = arr.length;\n }\n\n static uniform(size: number, initialValue: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = initialValue;\n }\n\n return new PrefixIntervalTree(xs);\n }\n\n static empty(size: number) {\n return PrefixIntervalTree.uniform(size, 0);\n }\n\n /**\n * the length should be 2\n * it will cause calculation of\n */\n stretch() {\n const nextHeap = createArray(2 * this._half * 2);\n const nextHeapHalf = this._half * 2;\n\n // copy old value to new one\n for (let i = 0; i < this._size; i++) {\n nextHeap[nextHeapHalf + i] = this._heap[this._half + i] || 0;\n }\n\n // sum old value to create new sum value\n for (let i = nextHeapHalf - 1; i > 0; i--) {\n nextHeap[i] = nextHeap[2 * i] + nextHeap[2 * i + 1];\n }\n\n this._half = nextHeapHalf;\n this._size = nextHeapHalf;\n this._heap = nextHeap;\n }\n\n indexKey(index: number) {\n return `${index}`;\n }\n\n resolveKeyIndex(key: string) {\n return parseInt(key, 10);\n }\n\n isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowDirectParent(index: number) {\n this.resetAffected();\n let node = this._half + index;\n\n node = parent(node);\n for (; node !== 0; node = parent(node)) {\n this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];\n }\n }\n\n // recalculate parent value from startIndex to endIndex\n reflowHeap(_startIndex: number, endIndex = this._half * 2 - 2) {\n this.resetAffected();\n const depth = Math.log2(this._size);\n const startIndex = _startIndex + this._half;\n\n Array.from({ length: depth }, (v, i) => i).reduce(\n (acc) => {\n const { startIndex, endIndex } = acc;\n const _nextStart = parent(startIndex);\n const _nextEnd = parent(endIndex);\n\n for (let idx = _nextStart; idx <= _nextEnd; idx++) {\n this._heap[idx] = this._heap[2 * idx] + this._heap[2 * idx + 1];\n }\n\n return {\n startIndex: _nextStart,\n endIndex: _nextEnd,\n };\n },\n {\n startIndex,\n endIndex,\n }\n );\n }\n\n /**\n *\n * @param index index to remove\n * @returns\n *\n * update heap only, but not recalculate interval tree value\n */\n dryRemove(index: number) {\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n this._heap.splice(this._half + index, 1);\n this._heap.push(0);\n this._actualSize = this._actualSize - 1;\n\n this.updateAffectedIndices(index, 'remove');\n }\n\n remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.removeIndices([index]);\n }\n\n removeIndices(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => this.dryRemove(index));\n\n this.reflowHeap(indices[0]);\n this.onUpdateIntervalTree();\n\n if (typeof this._onUpdateItemLayout === 'function') {\n for (let idx = indices[0]; idx < this._half; idx++) {\n this._onUpdateItemLayout(idx, this.get(idx));\n }\n }\n }\n\n removeV0(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n const nextHeap = createArray(this._half * 2);\n const copy = this._heap.slice(this._half);\n copy.splice(index, 1);\n\n for (let index = this._half; index < this._half * 2; index++) {\n nextHeap[index] = copy[index - this._half] || 0;\n }\n\n for (let index = this._half - 1; index > 0; index--) {\n nextHeap[index] = nextHeap[2 * index] + nextHeap[2 * index + 1];\n }\n\n this._actualSize = this._actualSize - 1;\n this._heap = nextHeap;\n }\n\n resetAffected() {\n this._affectedIndicesMap = {};\n this._affectedMinimalIndex = undefined;\n }\n\n updateAffectedIndices(index: number, type: OperationType, value?: number) {\n if (typeof index === 'number') {\n this._affectedMinimalIndex =\n typeof this._affectedMinimalIndex === 'number'\n ? Math.min(this._affectedMinimalIndex, index)\n : index;\n this._affectedIndicesMap[this.indexKey(index)] = {\n value,\n type,\n };\n }\n }\n\n onUpdateIntervalTree() {\n // reset cached value\n this._cachedSumUntilValues = {};\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n }\n\n drySet(index: number, value: number) {\n if (typeof index !== 'number' || index < 0) return false;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return false;\n }\n\n while (index >= this._half) {\n this.stretch();\n }\n\n const node = this._half + index;\n this._heap[node] = value;\n\n this.updateAffectedIndices(index, 'set', value);\n\n if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n return true;\n }\n\n set(index: number, value: number) {\n this.drySet(index, value);\n this.reflowDirectParent(index);\n this.onUpdateIntervalTree();\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\n }\n\n applyUpdate() {\n if (typeof this._affectedMinimalIndex === 'number') {\n const index = this._affectedMinimalIndex;\n\n const keys = Object.keys(this._affectedIndicesMap);\n const hasRemoveItem = keys.some(\n (key) => this._affectedIndicesMap[key].type === 'remove'\n );\n\n if (hasRemoveItem) {\n return this.reflowHeap(index);\n }\n\n const depth = Math.log2(this._size);\n const itemLength = keys.length;\n\n const reflowTimes = this._half - index - 1;\n\n if (depth * itemLength > reflowTimes) {\n return this.reflowHeap(index);\n }\n\n keys.forEach((key) => this.reflowDirectParent(this.resolveKeyIndex(key)));\n\n this.onUpdateIntervalTree();\n\n this.resetAffected();\n }\n }\n\n getMaxUsefulLength() {\n return this.getActualSize();\n }\n\n getActualSize() {\n return this._actualSize;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n const node = this._half + index;\n return this._heap[node];\n }\n\n getSize() {\n return this._size;\n }\n\n getHalf() {\n return this._half;\n }\n\n getHeap() {\n return this._heap;\n }\n\n getMaxValue() {\n return this._heap[1];\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(end - 1).\n * End is not included. if end less than 0, then return 0\n */\n sumUntil(end: number) {\n // invariant(end >= 0 && end < this._size + 1, 'Index out of range %s', end);\n\n if (this._cachedSumUntilValues[`${end}`] != null)\n return this._cachedSumUntilValues[`${end}`];\n if (end <= 0) {\n return 0;\n }\n\n let node = this._half + end - 1;\n let sum = this._heap[node];\n\n for (; node !== 1; node = parent(node)) {\n if (node % 2 === 1) {\n sum += this._heap[node - 1];\n }\n }\n\n return sum;\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(inclusiveEnd).\n */\n sumTo(inclusiveEnd: number) {\n // invariant(\n // inclusiveEnd >= 0 && inclusiveEnd < this._size,\n // 'Index out of range %s',\n // inclusiveEnd\n // );\n return this.sumUntil(inclusiveEnd + 1);\n }\n\n /**\n * Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).\n * end length is not included\n */\n sum(begin: number, end: number) {\n // invariant(begin <= end, 'Begin must precede end');\n return this.sumUntil(end) - this.sumUntil(begin);\n }\n\n /**\n * return the biggest i, sumUntil(i) === t\n * return the biggest i, subUntil(i) < t\n */\n greatestLowerBound(t: number) {\n if (t < 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n // not use this._size;this._size always be a big value\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t < leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n * Return the biggest i, subUntil(i) < t\n * or -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t <= leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 0, 100, 0, 0, 100].\n * then computeRange(100, 200) => { startIndex: 2, endIndex: 6 }\n *\n * item index in viewport will be [2, 3, 4, 5], index 6 is not\n * included just like Array.slice(start, end)\n *\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._actualSize,\n endIndex: this._actualSize,\n };\n }\n\n return {\n // the biggest item, value <= minOffset\n startIndex: this.greatestLowerBound(minOffset),\n\n // the smallest item, value > maxOffset\n endIndex: this.leastStrictUpperBound(maxOffset),\n };\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and t <= sumUntil(i), or\n * size + 1 if no such i exists.\n */\n leastUpperBound(t: number) {\n return Math.min(this.greatestStrictLowerBound(t) + 1, this._actualSize);\n }\n\n /**\n * Returns the smallest i, t < sumUntil(i), it should be used as range end\n * size + 1 if no such i exists.\n */\n leastStrictUpperBound(t: number) {\n return Math.min(this.greatestLowerBound(t) + 1, this._actualSize);\n }\n}\n\nexport default PrefixIntervalTree;\n"],"names":["parent","node","Math","floor","createArray","size","xs","i","ceilLog2","x","y","PrefixIntervalTree","opts","this","undefined","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","_onUpdateIntervalTree","onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","indexKey","index","resolveKeyIndex","key","parseInt","isValidIndex","reflowDirectParent","resetAffected","reflowHeap","_startIndex","endIndex","depth","log2","startIndex","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","dryRemove","isNaN","console","warn","splice","push","updateAffectedIndices","remove","removeIndices","indices","sort","a","b","forEach","_this2","get","removeV0","copy","slice","_affectedIndicesMap","_affectedMinimalIndex","type","value","min","_cachedSumUntilValues","drySet","set","applyUpdate","keys","Object","some","_this3","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","greatestStrictLowerBound","computeRange","minOffset","maxOffset","leastStrictUpperBound","leastUpperBound"],"mappings":"oEAAA,IAAMA,EAAS,SAACC,GAAY,OAAKC,KAAKC,MAAMF,EAAO,IAE7CG,EAAc,SAAUC,GAE5B,IADA,IAAMC,EAAK,GACFC,EAAIF,EAAO,EAAGE,GAAK,IAAKA,EAC/BD,EAAGC,GAAK,EAEV,OAAOD,GAMT,SAASE,EAASC,GAEhB,IADA,IAAIC,EAAI,EACDA,EAAID,GACTC,GAAK,EAGP,OAAOA,6BA8CP,SAAAC,EACEL,EACAM,YADAN,IAAAA,EAAwB,IATlBO,yBAEJ,GACIA,gCAAgCC,EAChCD,2BAEJ,GASgB,iBAAPP,GAAiBO,KAAKE,eAAeT,GAC5CU,MAAMC,QAAQX,IAAKO,KAAKK,cAAcZ,GAE1C,IAAAa,EAAqDP,GAAQ,GAArDQ,EAAkBD,EAAlBC,mBACRP,KAAKQ,sBAD2CF,EAApBG,qBAE5BT,KAAKU,oBAAsBH,EAC5B,IAAAI,EAAAb,EAAAc,UAybA,OAzbAD,EAEDT,eA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,MAAM5B,GAAQwG,EACrB,OAAO5F,KAAKiB,YAGd,KAAO7B,EAAOY,KAAKc,OAAO,CACxB,IAAM+E,EAAU7F,KAAKgB,MAAM,EAAI5B,GAC3BwG,GAAKC,EACPzG,GAAO,GAEPA,EAAO,EAAIA,EAAO,EAClBwG,GAAKC,GAIT,OAAOxG,KAAKiF,IAAIlF,EAAOY,KAAKc,MAAOd,KAAKiB,cACzCN,EAgBDoF,aAAA,SAAaC,EAAmBC,GAC9B,OAAIjG,KAAKmF,UAAU,GAAKa,EACf,CACL1D,WAAYtC,KAAKiB,YACjBkB,SAAUnC,KAAKiB,aAIZ,CAELqB,WAAYtC,KAAK2F,mBAAmBK,GAGpC7D,SAAUnC,KAAKkG,sBAAsBD,KAExCtF,EAMDwF,gBAAA,SAAgBP,GACd,OAAOvG,KAAKiF,IAAItE,KAAK8F,yBAAyBF,GAAK,EAAG5F,KAAKiB,cAC5DN,EAMDuF,sBAAA,SAAsBN,GACpB,OAAOvG,KAAKiF,IAAItE,KAAK2F,mBAAmBC,GAAK,EAAG5F,KAAKiB,cACtDnB"}
|
|
1
|
+
{"version":3,"file":"prefix-interval-tree.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["const parent = (node: number) => Math.floor(node / 2);\n\nconst createArray = function (size: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = 0;\n }\n return xs;\n};\n\n/**\n * Computes the next power of 2 after or equal to x.\n */\nfunction ceilLog2(x: number) {\n let y = 1;\n while (y < x) {\n y *= 2;\n }\n\n return y;\n}\n\ntype OperationType = 'set' | 'remove';\n\ntype Operation = {\n type: OperationType;\n value: any;\n};\n\n/**\n * A prefix interval tree stores an numeric array and the partial sums of that\n * array. It is optimized for updating the values of the array without\n * recomputing all of the partial sums.\n *\n * - O(ln n) update\n * - O(1) lookup\n * - O(ln n) compute a partial sum\n * - O(n) space\n *\n * Note that the sequence of partial sums is one longer than the array, so that\n * the first partial sum is always 0, and the last partial sum is the sum of the\n * entire array.\n */\nclass PrefixIntervalTree {\n private _size: number;\n /**\n * Half the size of the heap. It is also the number of non-leaf nodes, and the\n * index of the first element in the heap. Always a power of 2.\n */\n private _half: number;\n private _heap: number[];\n\n private _actualSize: number;\n\n private _onUpdateItemLayout: Function;\n private _onUpdateIntervalTree: Function;\n\n private _affectedIndicesMap: Record<string, Operation> = {};\n private _affectedMinimalIndex: number = undefined;\n private _cachedSumUntilValues: Record<string, number> = {};\n\n constructor(\n xs: number[] | number = 10,\n opts?: {\n onUpdateItemLayout?: Function;\n onUpdateIntervalTree?: Function;\n }\n ) {\n if (typeof xs === 'number') this.initWithNumber(xs);\n if (Array.isArray(xs)) this.initWithArray(xs);\n\n const { onUpdateItemLayout, onUpdateIntervalTree } = opts || {};\n this._onUpdateIntervalTree = onUpdateIntervalTree;\n this._onUpdateItemLayout = onUpdateItemLayout;\n }\n\n initWithNumber(length: number) {\n this._half = ceilLog2(length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n this._actualSize = 0;\n }\n\n initWithArray(arr: number[]) {\n this._half = ceilLog2(arr.length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n let i;\n for (i = 0; i < this._size; ++i) {\n this._heap[this._half + i] = arr[i];\n }\n\n for (i = this._half - 1; i > 0; --i) {\n this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];\n }\n this._actualSize = arr.length;\n }\n\n static uniform(size: number, initialValue: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = initialValue;\n }\n\n return new PrefixIntervalTree(xs);\n }\n\n static empty(size: number) {\n return PrefixIntervalTree.uniform(size, 0);\n }\n\n /**\n * the length should be 2\n * it will cause calculation of\n */\n stretch() {\n const nextHeap = createArray(2 * this._half * 2);\n const nextHeapHalf = this._half * 2;\n\n // copy old value to new one\n for (let i = 0; i < this._size; i++) {\n nextHeap[nextHeapHalf + i] = this._heap[this._half + i] || 0;\n }\n\n // sum old value to create new sum value\n for (let i = nextHeapHalf - 1; i > 0; i--) {\n nextHeap[i] = nextHeap[2 * i] + nextHeap[2 * i + 1];\n }\n\n this._half = nextHeapHalf;\n this._size = nextHeapHalf;\n this._heap = nextHeap;\n }\n\n indexKey(index: number) {\n return `${index}`;\n }\n\n resolveKeyIndex(key: string) {\n return parseInt(key, 10);\n }\n\n isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowDirectParent(index: number) {\n this.resetAffected();\n let node = this._half + index;\n\n node = parent(node);\n for (; node !== 0; node = parent(node)) {\n this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];\n }\n }\n\n // recalculate parent value from startIndex to endIndex\n reflowHeap(_startIndex: number, endIndex = this._half * 2 - 2) {\n this.resetAffected();\n const depth = Math.log2(this._size);\n const startIndex = _startIndex + this._half;\n\n Array.from({ length: depth }, (v, i) => i).reduce(\n (acc) => {\n const { startIndex, endIndex } = acc;\n const _nextStart = parent(startIndex);\n const _nextEnd = parent(endIndex);\n\n for (let idx = _nextStart; idx <= _nextEnd; idx++) {\n this._heap[idx] = this._heap[2 * idx] + this._heap[2 * idx + 1];\n }\n\n return {\n startIndex: _nextStart,\n endIndex: _nextEnd,\n };\n },\n {\n startIndex,\n endIndex,\n }\n );\n }\n\n /**\n *\n * @param index index to remove\n * @returns\n *\n * update heap only, but not recalculate interval tree value\n */\n dryRemove(index: number) {\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n this._heap.splice(this._half + index, 1);\n this._heap.push(0);\n this._actualSize = this._actualSize - 1;\n\n this.updateAffectedIndices(index, 'remove');\n }\n\n remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.removeIndices([index]);\n }\n\n removeIndices(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => this.dryRemove(index));\n\n this.reflowHeap(indices[0]);\n this.onUpdateIntervalTree();\n\n if (typeof this._onUpdateItemLayout === 'function') {\n for (let idx = indices[0]; idx < this._half; idx++) {\n this._onUpdateItemLayout(idx, this.get(idx));\n }\n }\n }\n\n removeV0(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n const nextHeap = createArray(this._half * 2);\n const copy = this._heap.slice(this._half);\n copy.splice(index, 1);\n\n for (let index = this._half; index < this._half * 2; index++) {\n nextHeap[index] = copy[index - this._half] || 0;\n }\n\n for (let index = this._half - 1; index > 0; index--) {\n nextHeap[index] = nextHeap[2 * index] + nextHeap[2 * index + 1];\n }\n\n this._actualSize = this._actualSize - 1;\n this._heap = nextHeap;\n }\n\n resetAffected() {\n this._affectedIndicesMap = {};\n this._affectedMinimalIndex = undefined;\n }\n\n updateAffectedIndices(index: number, type: OperationType, value?: number) {\n if (typeof index === 'number') {\n this._affectedMinimalIndex =\n typeof this._affectedMinimalIndex === 'number'\n ? Math.min(this._affectedMinimalIndex, index)\n : index;\n this._affectedIndicesMap[this.indexKey(index)] = {\n value,\n type,\n };\n }\n }\n\n onUpdateIntervalTree() {\n // reset cached value\n this._cachedSumUntilValues = {};\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n }\n\n drySet(index: number, value: number) {\n if (typeof index !== 'number' || index < 0) return false;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return false;\n }\n\n while (index >= this._half) {\n this.stretch();\n }\n\n const node = this._half + index;\n this._heap[node] = value;\n\n this.updateAffectedIndices(index, 'set', value);\n\n if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n return true;\n }\n\n set(index: number, value: number) {\n this.drySet(index, value);\n this.reflowDirectParent(index);\n this.onUpdateIntervalTree();\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\n }\n\n applyUpdate() {\n if (typeof this._affectedMinimalIndex === 'number') {\n const index = this._affectedMinimalIndex;\n\n const keys = Object.keys(this._affectedIndicesMap);\n const hasRemoveItem = keys.some(\n (key) => this._affectedIndicesMap[key].type === 'remove'\n );\n\n if (hasRemoveItem) {\n return this.reflowHeap(index);\n }\n\n const depth = Math.log2(this._size);\n const itemLength = keys.length;\n\n const reflowTimes = this._half - index - 1;\n\n if (depth * itemLength > reflowTimes) {\n return this.reflowHeap(index);\n }\n\n keys.forEach((key) => this.reflowDirectParent(this.resolveKeyIndex(key)));\n\n this.onUpdateIntervalTree();\n\n this.resetAffected();\n }\n }\n\n getMaxUsefulLength() {\n return this.getActualSize();\n }\n\n getActualSize() {\n return this._actualSize;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n const node = this._half + index;\n return this._heap[node];\n }\n\n getSize() {\n return this._size;\n }\n\n getHalf() {\n return this._half;\n }\n\n getHeap() {\n return this._heap;\n }\n\n getMaxValue() {\n return this._heap[1];\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(end - 1).\n * End is not included. if end less than 0, then return 0\n */\n sumUntil(end: number) {\n // invariant(end >= 0 && end < this._size + 1, 'Index out of range %s', end);\n\n if (this._cachedSumUntilValues[`${end}`] != null)\n return this._cachedSumUntilValues[`${end}`];\n if (end <= 0) {\n return 0;\n }\n\n let node = this._half + end - 1;\n let sum = this._heap[node];\n\n for (; node !== 1; node = parent(node)) {\n if (node % 2 === 1) {\n sum += this._heap[node - 1];\n }\n }\n\n return sum;\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(inclusiveEnd).\n */\n sumTo(inclusiveEnd: number) {\n // invariant(\n // inclusiveEnd >= 0 && inclusiveEnd < this._size,\n // 'Index out of range %s',\n // inclusiveEnd\n // );\n return this.sumUntil(inclusiveEnd + 1);\n }\n\n /**\n * Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).\n * end length is not included\n */\n sum(begin: number, end: number) {\n // invariant(begin <= end, 'Begin must precede end');\n return this.sumUntil(end) - this.sumUntil(begin);\n }\n\n /**\n * return the biggest i, sumUntil(i) === t\n * return the biggest i, subUntil(i) < t\n */\n greatestLowerBound(t: number) {\n if (t < 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n // not use this._size;this._size always be a big value\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t < leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n * Return the biggest i, subUntil(i) < t\n * or -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t <= leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 0, 100, 0, 0, 100].\n * then computeRange(100, 200) => { startIndex: 2, endIndex: 6 }\n *\n * item index in viewport will be [2, 3, 4, 5], index 6 is not\n * included just like Array.slice(start, end)\n *\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._actualSize,\n endIndex: this._actualSize,\n };\n }\n\n return {\n // the biggest item, value <= minOffset\n startIndex: this.greatestLowerBound(minOffset),\n\n // the smallest item, value > maxOffset\n endIndex: this.leastStrictUpperBound(maxOffset),\n };\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and t <= sumUntil(i), or\n * size + 1 if no such i exists.\n */\n leastUpperBound(t: number) {\n return Math.min(this.greatestStrictLowerBound(t) + 1, this._actualSize);\n }\n\n /**\n * Returns the smallest i, t < sumUntil(i), it should be used as range end\n * size + 1 if no such i exists.\n */\n leastStrictUpperBound(t: number) {\n return Math.min(this.greatestLowerBound(t) + 1, this._actualSize);\n }\n}\n\nexport default PrefixIntervalTree;\n"],"names":["parent","node","Math","floor","createArray","size","xs","i","ceilLog2","x","y","PrefixIntervalTree","opts","this","undefined","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","_onUpdateIntervalTree","onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","indexKey","index","resolveKeyIndex","key","parseInt","isValidIndex","reflowDirectParent","resetAffected","reflowHeap","_startIndex","endIndex","depth","log2","startIndex","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","dryRemove","isNaN","console","warn","splice","push","updateAffectedIndices","remove","removeIndices","indices","sort","a","b","forEach","_this2","get","removeV0","copy","slice","_affectedIndicesMap","_affectedMinimalIndex","type","value","min","_cachedSumUntilValues","drySet","set","applyUpdate","keys","Object","some","_this3","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","greatestStrictLowerBound","computeRange","minOffset","maxOffset","leastStrictUpperBound","leastUpperBound"],"mappings":"oEAAA,IAAMA,EAAS,SAACC,GAAY,OAAKC,KAAKC,MAAMF,EAAO,IAE7CG,EAAc,SAAUC,GAE5B,IADA,IAAMC,EAAK,GACFC,EAAIF,EAAO,EAAGE,GAAK,IAAKA,EAC/BD,EAAGC,GAAK,EAEV,OAAOD,GAMT,SAASE,EAASC,GAEhB,IADA,IAAIC,EAAI,EACDA,EAAID,GACTC,GAAK,EAGP,OAAOA,6BA0CP,SAAAC,EACEL,EACAM,YADAN,IAAAA,EAAwB,IALlBO,yBAAiD,GACjDA,gCAAgCC,EAChCD,2BAAgD,GASpC,iBAAPP,GAAiBO,KAAKE,eAAeT,GAC5CU,MAAMC,QAAQX,IAAKO,KAAKK,cAAcZ,GAE1C,IAAAa,EAAqDP,GAAQ,GAArD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Od,KAAKiB,cACzCN,EAMDmF,yBAAA,SAAyBF,GACvB,GAAIA,GAAK,IAAM5F,KAAKiB,YAClB,OAAQ,EAGV,IAAI7B,EAAO,EACX,GAAIY,KAAKgB,MAAM5B,GAAQwG,EACrB,OAAO5F,KAAKiB,YAGd,KAAO7B,EAAOY,KAAKc,OAAO,CACxB,IAAM+E,EAAU7F,KAAKgB,MAAM,EAAI5B,GAC3BwG,GAAKC,EACPzG,GAAO,GAEPA,EAAO,EAAIA,EAAO,EAClBwG,GAAKC,GAIT,OAAOxG,KAAKiF,IAAIlF,EAAOY,KAAKc,MAAOd,KAAKiB,cACzCN,EAgBDoF,aAAA,SAAaC,EAAmBC,GAC9B,OAAIjG,KAAKmF,UAAU,GAAKa,EACf,CACL1D,WAAYtC,KAAKiB,YACjBkB,SAAUnC,KAAKiB,aAIZ,CAELqB,WAAYtC,KAAK2F,mBAAmBK,GAGpC7D,SAAUnC,KAAKkG,sBAAsBD,KAExCtF,EAMDwF,gBAAA,SAAgBP,GACd,OAAOvG,KAAKiF,IAAItE,KAAK8F,yBAAyBF,GAAK,EAAG5F,KAAKiB,cAC5DN,EAMDuF,sBAAA,SAAsBN,GACpB,OAAOvG,KAAKiF,IAAItE,KAAK2F,mBAAmBC,GAAK,EAAG5F,KAAKiB,cACtDnB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefix-interval-tree.esm.js","sources":["../src/index.ts"],"sourcesContent":["const parent = (node: number) => Math.floor(node / 2);\n\nconst createArray = function (size: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = 0;\n }\n return xs;\n};\n\n/**\n * Computes the next power of 2 after or equal to x.\n */\nfunction ceilLog2(x: number) {\n let y = 1;\n while (y < x) {\n y *= 2;\n }\n\n return y;\n}\n\ntype OperationType = 'set' | 'remove';\n\ntype Operation = {\n type: OperationType;\n value: any;\n};\n\n/**\n * A prefix interval tree stores an numeric array and the partial sums of that\n * array. It is optimized for updating the values of the array without\n * recomputing all of the partial sums.\n *\n * - O(ln n) update\n * - O(1) lookup\n * - O(ln n) compute a partial sum\n * - O(n) space\n *\n * Note that the sequence of partial sums is one longer than the array, so that\n * the first partial sum is always 0, and the last partial sum is the sum of the\n * entire array.\n */\nclass PrefixIntervalTree {\n private _size: number;\n /**\n * Half the size of the heap. It is also the number of non-leaf nodes, and the\n * index of the first element in the heap. Always a power of 2.\n */\n private _half: number;\n private _heap: number[];\n\n private _actualSize: number;\n\n private _onUpdateItemLayout: Function;\n private _onUpdateIntervalTree: Function;\n\n private _affectedIndicesMap: {\n [key: string]: Operation;\n } = {};\n private _affectedMinimalIndex: number = undefined;\n private _cachedSumUntilValues: {\n [key: string]: number;\n } = {};\n\n constructor(\n xs: number[] | number = 10,\n opts?: {\n onUpdateItemLayout?: Function;\n onUpdateIntervalTree?: Function;\n }\n ) {\n if (typeof xs === 'number') this.initWithNumber(xs);\n if (Array.isArray(xs)) this.initWithArray(xs);\n\n const { onUpdateItemLayout, onUpdateIntervalTree } = opts || {};\n this._onUpdateIntervalTree = onUpdateIntervalTree;\n this._onUpdateItemLayout = onUpdateItemLayout;\n }\n\n initWithNumber(length: number) {\n this._half = ceilLog2(length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n this._actualSize = 0;\n }\n\n initWithArray(arr: number[]) {\n this._half = ceilLog2(arr.length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n let i;\n for (i = 0; i < this._size; ++i) {\n this._heap[this._half + i] = arr[i];\n }\n\n for (i = this._half - 1; i > 0; --i) {\n this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];\n }\n this._actualSize = arr.length;\n }\n\n static uniform(size: number, initialValue: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = initialValue;\n }\n\n return new PrefixIntervalTree(xs);\n }\n\n static empty(size: number) {\n return PrefixIntervalTree.uniform(size, 0);\n }\n\n /**\n * the length should be 2\n * it will cause calculation of\n */\n stretch() {\n const nextHeap = createArray(2 * this._half * 2);\n const nextHeapHalf = this._half * 2;\n\n // copy old value to new one\n for (let i = 0; i < this._size; i++) {\n nextHeap[nextHeapHalf + i] = this._heap[this._half + i] || 0;\n }\n\n // sum old value to create new sum value\n for (let i = nextHeapHalf - 1; i > 0; i--) {\n nextHeap[i] = nextHeap[2 * i] + nextHeap[2 * i + 1];\n }\n\n this._half = nextHeapHalf;\n this._size = nextHeapHalf;\n this._heap = nextHeap;\n }\n\n indexKey(index: number) {\n return `${index}`;\n }\n\n resolveKeyIndex(key: string) {\n return parseInt(key, 10);\n }\n\n isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowDirectParent(index: number) {\n this.resetAffected();\n let node = this._half + index;\n\n node = parent(node);\n for (; node !== 0; node = parent(node)) {\n this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];\n }\n }\n\n // recalculate parent value from startIndex to endIndex\n reflowHeap(_startIndex: number, endIndex = this._half * 2 - 2) {\n this.resetAffected();\n const depth = Math.log2(this._size);\n const startIndex = _startIndex + this._half;\n\n Array.from({ length: depth }, (v, i) => i).reduce(\n (acc) => {\n const { startIndex, endIndex } = acc;\n const _nextStart = parent(startIndex);\n const _nextEnd = parent(endIndex);\n\n for (let idx = _nextStart; idx <= _nextEnd; idx++) {\n this._heap[idx] = this._heap[2 * idx] + this._heap[2 * idx + 1];\n }\n\n return {\n startIndex: _nextStart,\n endIndex: _nextEnd,\n };\n },\n {\n startIndex,\n endIndex,\n }\n );\n }\n\n /**\n *\n * @param index index to remove\n * @returns\n *\n * update heap only, but not recalculate interval tree value\n */\n dryRemove(index: number) {\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n this._heap.splice(this._half + index, 1);\n this._heap.push(0);\n this._actualSize = this._actualSize - 1;\n\n this.updateAffectedIndices(index, 'remove');\n }\n\n remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.removeIndices([index]);\n }\n\n removeIndices(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => this.dryRemove(index));\n\n this.reflowHeap(indices[0]);\n this.onUpdateIntervalTree();\n\n if (typeof this._onUpdateItemLayout === 'function') {\n for (let idx = indices[0]; idx < this._half; idx++) {\n this._onUpdateItemLayout(idx, this.get(idx));\n }\n }\n }\n\n removeV0(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n const nextHeap = createArray(this._half * 2);\n const copy = this._heap.slice(this._half);\n copy.splice(index, 1);\n\n for (let index = this._half; index < this._half * 2; index++) {\n nextHeap[index] = copy[index - this._half] || 0;\n }\n\n for (let index = this._half - 1; index > 0; index--) {\n nextHeap[index] = nextHeap[2 * index] + nextHeap[2 * index + 1];\n }\n\n this._actualSize = this._actualSize - 1;\n this._heap = nextHeap;\n }\n\n resetAffected() {\n this._affectedIndicesMap = {};\n this._affectedMinimalIndex = undefined;\n }\n\n updateAffectedIndices(index: number, type: OperationType, value?: number) {\n if (typeof index === 'number') {\n this._affectedMinimalIndex =\n typeof this._affectedMinimalIndex === 'number'\n ? Math.min(this._affectedMinimalIndex, index)\n : index;\n this._affectedIndicesMap[this.indexKey(index)] = {\n value,\n type,\n };\n }\n }\n\n onUpdateIntervalTree() {\n // reset cached value\n this._cachedSumUntilValues = {};\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n }\n\n drySet(index: number, value: number) {\n if (typeof index !== 'number' || index < 0) return false;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return false;\n }\n\n while (index >= this._half) {\n this.stretch();\n }\n\n const node = this._half + index;\n this._heap[node] = value;\n\n this.updateAffectedIndices(index, 'set', value);\n\n if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n return true;\n }\n\n set(index: number, value: number) {\n this.drySet(index, value);\n this.reflowDirectParent(index);\n this.onUpdateIntervalTree();\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\n }\n\n applyUpdate() {\n if (typeof this._affectedMinimalIndex === 'number') {\n const index = this._affectedMinimalIndex;\n\n const keys = Object.keys(this._affectedIndicesMap);\n const hasRemoveItem = keys.some(\n (key) => this._affectedIndicesMap[key].type === 'remove'\n );\n\n if (hasRemoveItem) {\n return this.reflowHeap(index);\n }\n\n const depth = Math.log2(this._size);\n const itemLength = keys.length;\n\n const reflowTimes = this._half - index - 1;\n\n if (depth * itemLength > reflowTimes) {\n return this.reflowHeap(index);\n }\n\n keys.forEach((key) => this.reflowDirectParent(this.resolveKeyIndex(key)));\n\n this.onUpdateIntervalTree();\n\n this.resetAffected();\n }\n }\n\n getMaxUsefulLength() {\n return this.getActualSize();\n }\n\n getActualSize() {\n return this._actualSize;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n const node = this._half + index;\n return this._heap[node];\n }\n\n getSize() {\n return this._size;\n }\n\n getHalf() {\n return this._half;\n }\n\n getHeap() {\n return this._heap;\n }\n\n getMaxValue() {\n return this._heap[1];\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(end - 1).\n * End is not included. if end less than 0, then return 0\n */\n sumUntil(end: number) {\n // invariant(end >= 0 && end < this._size + 1, 'Index out of range %s', end);\n\n if (this._cachedSumUntilValues[`${end}`] != null)\n return this._cachedSumUntilValues[`${end}`];\n if (end <= 0) {\n return 0;\n }\n\n let node = this._half + end - 1;\n let sum = this._heap[node];\n\n for (; node !== 1; node = parent(node)) {\n if (node % 2 === 1) {\n sum += this._heap[node - 1];\n }\n }\n\n return sum;\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(inclusiveEnd).\n */\n sumTo(inclusiveEnd: number) {\n // invariant(\n // inclusiveEnd >= 0 && inclusiveEnd < this._size,\n // 'Index out of range %s',\n // inclusiveEnd\n // );\n return this.sumUntil(inclusiveEnd + 1);\n }\n\n /**\n * Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).\n * end length is not included\n */\n sum(begin: number, end: number) {\n // invariant(begin <= end, 'Begin must precede end');\n return this.sumUntil(end) - this.sumUntil(begin);\n }\n\n /**\n * return the biggest i, sumUntil(i) === t\n * return the biggest i, subUntil(i) < t\n */\n greatestLowerBound(t: number) {\n if (t < 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n // not use this._size;this._size always be a big value\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t < leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n * Return the biggest i, subUntil(i) < t\n * or -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t <= leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 0, 100, 0, 0, 100].\n * then computeRange(100, 200) => { startIndex: 2, endIndex: 6 }\n *\n * item index in viewport will be [2, 3, 4, 5], index 6 is not\n * included just like Array.slice(start, end)\n *\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._actualSize,\n endIndex: this._actualSize,\n };\n }\n\n return {\n // the biggest item, value <= minOffset\n startIndex: this.greatestLowerBound(minOffset),\n\n // the smallest item, value > maxOffset\n endIndex: this.leastStrictUpperBound(maxOffset),\n };\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and t <= sumUntil(i), or\n * size + 1 if no such i exists.\n */\n leastUpperBound(t: number) {\n return Math.min(this.greatestStrictLowerBound(t) + 1, this._actualSize);\n }\n\n /**\n * Returns the smallest i, t < sumUntil(i), it should be used as range end\n * size + 1 if no such i exists.\n */\n leastStrictUpperBound(t: number) {\n return Math.min(this.greatestLowerBound(t) + 1, this._actualSize);\n }\n}\n\nexport default PrefixIntervalTree;\n"],"names":["parent","node","Math","floor","createArray","size","xs","i","ceilLog2","x","y","PrefixIntervalTree","opts","undefined","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","onUpdateIntervalTree","_onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","indexKey","index","resolveKeyIndex","key","parseInt","isValidIndex","reflowDirectParent","resetAffected","reflowHeap","_startIndex","endIndex","depth","log2","startIndex","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","dryRemove","isNaN","console","warn","splice","push","updateAffectedIndices","remove","removeIndices","indices","sort","a","b","forEach","_this2","get","removeV0","copy","slice","_affectedIndicesMap","_affectedMinimalIndex","type","value","min","_cachedSumUntilValues","drySet","set","applyUpdate","keys","Object","hasRemoveItem","some","_this3","itemLength","reflowTimes","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","greatestStrictLowerBound","computeRange","minOffset","maxOffset","leastStrictUpperBound","leastUpperBound"],"mappings":"AAAA,IAAMA,MAAM,GAAG,SAATA,MAAMA,CAAIC,IAAY;EAAA,OAAKC,IAAI,CAACC,KAAK,CAACF,IAAI,GAAG,CAAC,CAAC;AAAA;AAErD,IAAMG,WAAW,GAAG,SAAdA,WAAWA,CAAaC,IAAY;EACxC,IAAMC,EAAE,GAAG,EAAE;EACb,KAAK,IAAIC,CAAC,GAAGF,IAAI,GAAG,CAAC,EAAEE,CAAC,IAAI,CAAC,EAAE,EAAE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A,CAMD2F,eAAe,GAAf,SAAAA,gBAAgBP,CAAS;IACvB,OAAOzG,IAAI,CAACgF,GAAG,CAAC,IAAI,CAAC2B,wBAAwB,CAACF,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC9E,WAAW,CAAC;GACxE;EAAAN,MAAA,CAMD0F,qBAAqB,GAArB,SAAAA,sBAAsBN,CAAS;IAC7B,OAAOzG,IAAI,CAACgF,GAAG,CAAC,IAAI,CAACwB,kBAAkB,CAACC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC9E,WAAW,CAAC;GAClE;EAAA,OAAAlB,kBAAA;AAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"prefix-interval-tree.esm.js","sources":["../src/index.ts"],"sourcesContent":["const parent = (node: number) => Math.floor(node / 2);\n\nconst createArray = function (size: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = 0;\n }\n return xs;\n};\n\n/**\n * Computes the next power of 2 after or equal to x.\n */\nfunction ceilLog2(x: number) {\n let y = 1;\n while (y < x) {\n y *= 2;\n }\n\n return y;\n}\n\ntype OperationType = 'set' | 'remove';\n\ntype Operation = {\n type: OperationType;\n value: any;\n};\n\n/**\n * A prefix interval tree stores an numeric array and the partial sums of that\n * array. It is optimized for updating the values of the array without\n * recomputing all of the partial sums.\n *\n * - O(ln n) update\n * - O(1) lookup\n * - O(ln n) compute a partial sum\n * - O(n) space\n *\n * Note that the sequence of partial sums is one longer than the array, so that\n * the first partial sum is always 0, and the last partial sum is the sum of the\n * entire array.\n */\nclass PrefixIntervalTree {\n private _size: number;\n /**\n * Half the size of the heap. It is also the number of non-leaf nodes, and the\n * index of the first element in the heap. Always a power of 2.\n */\n private _half: number;\n private _heap: number[];\n\n private _actualSize: number;\n\n private _onUpdateItemLayout: Function;\n private _onUpdateIntervalTree: Function;\n\n private _affectedIndicesMap: Record<string, Operation> = {};\n private _affectedMinimalIndex: number = undefined;\n private _cachedSumUntilValues: Record<string, number> = {};\n\n constructor(\n xs: number[] | number = 10,\n opts?: {\n onUpdateItemLayout?: Function;\n onUpdateIntervalTree?: Function;\n }\n ) {\n if (typeof xs === 'number') this.initWithNumber(xs);\n if (Array.isArray(xs)) this.initWithArray(xs);\n\n const { onUpdateItemLayout, onUpdateIntervalTree } = opts || {};\n this._onUpdateIntervalTree = onUpdateIntervalTree;\n this._onUpdateItemLayout = onUpdateItemLayout;\n }\n\n initWithNumber(length: number) {\n this._half = ceilLog2(length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n this._actualSize = 0;\n }\n\n initWithArray(arr: number[]) {\n this._half = ceilLog2(arr.length);\n this._size = this._half;\n this._heap = createArray(2 * this._half);\n let i;\n for (i = 0; i < this._size; ++i) {\n this._heap[this._half + i] = arr[i];\n }\n\n for (i = this._half - 1; i > 0; --i) {\n this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];\n }\n this._actualSize = arr.length;\n }\n\n static uniform(size: number, initialValue: number) {\n const xs = [];\n for (let i = size - 1; i >= 0; --i) {\n xs[i] = initialValue;\n }\n\n return new PrefixIntervalTree(xs);\n }\n\n static empty(size: number) {\n return PrefixIntervalTree.uniform(size, 0);\n }\n\n /**\n * the length should be 2\n * it will cause calculation of\n */\n stretch() {\n const nextHeap = createArray(2 * this._half * 2);\n const nextHeapHalf = this._half * 2;\n\n // copy old value to new one\n for (let i = 0; i < this._size; i++) {\n nextHeap[nextHeapHalf + i] = this._heap[this._half + i] || 0;\n }\n\n // sum old value to create new sum value\n for (let i = nextHeapHalf - 1; i > 0; i--) {\n nextHeap[i] = nextHeap[2 * i] + nextHeap[2 * i + 1];\n }\n\n this._half = nextHeapHalf;\n this._size = nextHeapHalf;\n this._heap = nextHeap;\n }\n\n indexKey(index: number) {\n return `${index}`;\n }\n\n resolveKeyIndex(key: string) {\n return parseInt(key, 10);\n }\n\n isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowDirectParent(index: number) {\n this.resetAffected();\n let node = this._half + index;\n\n node = parent(node);\n for (; node !== 0; node = parent(node)) {\n this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];\n }\n }\n\n // recalculate parent value from startIndex to endIndex\n reflowHeap(_startIndex: number, endIndex = this._half * 2 - 2) {\n this.resetAffected();\n const depth = Math.log2(this._size);\n const startIndex = _startIndex + this._half;\n\n Array.from({ length: depth }, (v, i) => i).reduce(\n (acc) => {\n const { startIndex, endIndex } = acc;\n const _nextStart = parent(startIndex);\n const _nextEnd = parent(endIndex);\n\n for (let idx = _nextStart; idx <= _nextEnd; idx++) {\n this._heap[idx] = this._heap[2 * idx] + this._heap[2 * idx + 1];\n }\n\n return {\n startIndex: _nextStart,\n endIndex: _nextEnd,\n };\n },\n {\n startIndex,\n endIndex,\n }\n );\n }\n\n /**\n *\n * @param index index to remove\n * @returns\n *\n * update heap only, but not recalculate interval tree value\n */\n dryRemove(index: number) {\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n this._heap.splice(this._half + index, 1);\n this._heap.push(0);\n this._actualSize = this._actualSize - 1;\n\n this.updateAffectedIndices(index, 'remove');\n }\n\n remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.removeIndices([index]);\n }\n\n removeIndices(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => this.dryRemove(index));\n\n this.reflowHeap(indices[0]);\n this.onUpdateIntervalTree();\n\n if (typeof this._onUpdateItemLayout === 'function') {\n for (let idx = indices[0]; idx < this._half; idx++) {\n this._onUpdateItemLayout(idx, this.get(idx));\n }\n }\n }\n\n removeV0(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (!this.isValidIndex(index)) return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\n }\n\n const nextHeap = createArray(this._half * 2);\n const copy = this._heap.slice(this._half);\n copy.splice(index, 1);\n\n for (let index = this._half; index < this._half * 2; index++) {\n nextHeap[index] = copy[index - this._half] || 0;\n }\n\n for (let index = this._half - 1; index > 0; index--) {\n nextHeap[index] = nextHeap[2 * index] + nextHeap[2 * index + 1];\n }\n\n this._actualSize = this._actualSize - 1;\n this._heap = nextHeap;\n }\n\n resetAffected() {\n this._affectedIndicesMap = {};\n this._affectedMinimalIndex = undefined;\n }\n\n updateAffectedIndices(index: number, type: OperationType, value?: number) {\n if (typeof index === 'number') {\n this._affectedMinimalIndex =\n typeof this._affectedMinimalIndex === 'number'\n ? Math.min(this._affectedMinimalIndex, index)\n : index;\n this._affectedIndicesMap[this.indexKey(index)] = {\n value,\n type,\n };\n }\n }\n\n onUpdateIntervalTree() {\n // reset cached value\n this._cachedSumUntilValues = {};\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n }\n\n drySet(index: number, value: number) {\n if (typeof index !== 'number' || index < 0) return false;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return false;\n }\n\n while (index >= this._half) {\n this.stretch();\n }\n\n const node = this._half + index;\n this._heap[node] = value;\n\n this.updateAffectedIndices(index, 'set', value);\n\n if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n return true;\n }\n\n set(index: number, value: number) {\n this.drySet(index, value);\n this.reflowDirectParent(index);\n this.onUpdateIntervalTree();\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\n }\n\n applyUpdate() {\n if (typeof this._affectedMinimalIndex === 'number') {\n const index = this._affectedMinimalIndex;\n\n const keys = Object.keys(this._affectedIndicesMap);\n const hasRemoveItem = keys.some(\n (key) => this._affectedIndicesMap[key].type === 'remove'\n );\n\n if (hasRemoveItem) {\n return this.reflowHeap(index);\n }\n\n const depth = Math.log2(this._size);\n const itemLength = keys.length;\n\n const reflowTimes = this._half - index - 1;\n\n if (depth * itemLength > reflowTimes) {\n return this.reflowHeap(index);\n }\n\n keys.forEach((key) => this.reflowDirectParent(this.resolveKeyIndex(key)));\n\n this.onUpdateIntervalTree();\n\n this.resetAffected();\n }\n }\n\n getMaxUsefulLength() {\n return this.getActualSize();\n }\n\n getActualSize() {\n return this._actualSize;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n const node = this._half + index;\n return this._heap[node];\n }\n\n getSize() {\n return this._size;\n }\n\n getHalf() {\n return this._half;\n }\n\n getHeap() {\n return this._heap;\n }\n\n getMaxValue() {\n return this._heap[1];\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(end - 1).\n * End is not included. if end less than 0, then return 0\n */\n sumUntil(end: number) {\n // invariant(end >= 0 && end < this._size + 1, 'Index out of range %s', end);\n\n if (this._cachedSumUntilValues[`${end}`] != null)\n return this._cachedSumUntilValues[`${end}`];\n if (end <= 0) {\n return 0;\n }\n\n let node = this._half + end - 1;\n let sum = this._heap[node];\n\n for (; node !== 1; node = parent(node)) {\n if (node % 2 === 1) {\n sum += this._heap[node - 1];\n }\n }\n\n return sum;\n }\n\n /**\n * Returns the sum get(0) + get(1) + ... + get(inclusiveEnd).\n */\n sumTo(inclusiveEnd: number) {\n // invariant(\n // inclusiveEnd >= 0 && inclusiveEnd < this._size,\n // 'Index out of range %s',\n // inclusiveEnd\n // );\n return this.sumUntil(inclusiveEnd + 1);\n }\n\n /**\n * Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).\n * end length is not included\n */\n sum(begin: number, end: number) {\n // invariant(begin <= end, 'Begin must precede end');\n return this.sumUntil(end) - this.sumUntil(begin);\n }\n\n /**\n * return the biggest i, sumUntil(i) === t\n * return the biggest i, subUntil(i) < t\n */\n greatestLowerBound(t: number) {\n if (t < 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n // not use this._size;this._size always be a big value\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t < leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n * Return the biggest i, subUntil(i) < t\n * or -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0 || !this._actualSize) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return this._actualSize;\n }\n\n while (node < this._half) {\n const leftSum = this._heap[2 * node];\n if (t <= leftSum) {\n node = 2 * node;\n } else {\n node = 2 * node + 1;\n t -= leftSum;\n }\n }\n\n return Math.min(node - this._half, this._actualSize);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 0, 100, 0, 0, 100].\n * then computeRange(100, 200) => { startIndex: 2, endIndex: 6 }\n *\n * item index in viewport will be [2, 3, 4, 5], index 6 is not\n * included just like Array.slice(start, end)\n *\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._actualSize,\n endIndex: this._actualSize,\n };\n }\n\n return {\n // the biggest item, value <= minOffset\n startIndex: this.greatestLowerBound(minOffset),\n\n // the smallest item, value > maxOffset\n endIndex: this.leastStrictUpperBound(maxOffset),\n };\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and t <= sumUntil(i), or\n * size + 1 if no such i exists.\n */\n leastUpperBound(t: number) {\n return Math.min(this.greatestStrictLowerBound(t) + 1, this._actualSize);\n }\n\n /**\n * Returns the smallest i, t < sumUntil(i), it should be used as range end\n * size + 1 if no such i exists.\n */\n leastStrictUpperBound(t: number) {\n return Math.min(this.greatestLowerBound(t) + 1, this._actualSize);\n }\n}\n\nexport default PrefixIntervalTree;\n"],"names":["parent","node","Math","floor","createArray","size","xs","i","ceilLog2","x","y","PrefixIntervalTree","opts","undefined","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","onUpdateIntervalTree","_onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","indexKey","index","resolveKeyIndex","key","parseInt","isValidIndex","reflowDirectParent","resetAffected","reflowHeap","_startIndex","endIndex","depth","log2","startIndex","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","dryRemove","isNaN","console","warn","splice","push","updateAffectedIndices","remove","removeIndices","indices","sort","a","b","forEach","_this2","get","removeV0","copy","slice","_affectedIndicesMap","_affectedMinimalIndex","type","value","min","_cachedSumUntilValues","drySet","set","applyUpdate","keys","Object","hasRemoveItem","some","_this3","itemLength","reflowTimes","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","greatestStrictLowerBound","computeRange","minOffset","maxOffset","leastStrictUpperBound","leastUpperBound"],"mappings":"AAAA,IAAMA,MAAM,GAAG,SAATA,MAAMA,CAAIC,IAAY;EAAA,OAAKC,IAAI,CAACC,KAAK,CAACF,IAAI,GAAG,CAAC,CAAC;AAAA;AAErD,IAAMG,WAAW,GAAG,SAAdA,WAAWA,CAAaC,IAAY;EACxC,IAAMC,EAAE,GAAG,EAAE;EACb,KAAK,IAAIC,CAAC,GAAGF,IAAI,GAAG,CAAC,EAAEE,CAAC,IAAI,CAAC,EAAE,EAAEA,CAAC,EAAE;IAClCD,EAAE,CAACC,CAAC,CAAC,GAAG,CAAC;;EAEX,OAAOD,EAAE;AACX,CAAC;AAKD,SAASE,QAAQA,CAACC,CAAS;EACzB,IAAIC,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGD,CAAC,EAAE;IACZC,CAAC,IAAI,CAAC;;EAGR,OAAOA,CAAC;AACV;AAAC,IAuBKC,kBAAkB;EAkBtB,SAAAA,mBACEL,IACAM,IAGC;QAJDN;MAAAA,KAAwB,EAAE;;IALpB,wBAAmB,GAA8B,EAAE;IACnD,0BAAqB,GAAWO,SAAS;IACzC,0BAAqB,GAA2B,EAAE;IASxD,IAAI,OAAOP,EAAE,KAAK,QAAQ,E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13 +55,9 @@ class PrefixIntervalTree {
|
|
|
55
55
|
private _onUpdateItemLayout: Function;
|
|
56
56
|
private _onUpdateIntervalTree: Function;
|
|
57
57
|
|
|
58
|
-
private _affectedIndicesMap: {
|
|
59
|
-
[key: string]: Operation;
|
|
60
|
-
} = {};
|
|
58
|
+
private _affectedIndicesMap: Record<string, Operation> = {};
|
|
61
59
|
private _affectedMinimalIndex: number = undefined;
|
|
62
|
-
private _cachedSumUntilValues: {
|
|
63
|
-
[key: string]: number;
|
|
64
|
-
} = {};
|
|
60
|
+
private _cachedSumUntilValues: Record<string, number> = {};
|
|
65
61
|
|
|
66
62
|
constructor(
|
|
67
63
|
xs: number[] | number = 10,
|