@x-oasis/prefix-interval-tree 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +16 -3
- package/dist/prefix-interval-tree.cjs.development.js +93 -25
- package/dist/prefix-interval-tree.cjs.development.js.map +1 -1
- package/dist/prefix-interval-tree.cjs.production.min.js +1 -1
- package/dist/prefix-interval-tree.cjs.production.min.js.map +1 -1
- package/dist/prefix-interval-tree.esm.js +93 -25
- package/dist/prefix-interval-tree.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +135 -31
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
declare type OperationType = 'set' | 'remove';
|
|
1
2
|
declare class PrefixIntervalTree {
|
|
2
3
|
private _size;
|
|
3
4
|
private _half;
|
|
@@ -5,7 +6,10 @@ declare class PrefixIntervalTree {
|
|
|
5
6
|
private _actualSize;
|
|
6
7
|
private _onUpdateItemLayout;
|
|
7
8
|
private _onUpdateIntervalTree;
|
|
8
|
-
|
|
9
|
+
private _affectedIndicesMap;
|
|
10
|
+
private _affectedMinimalIndex;
|
|
11
|
+
private _cachedSumUntilValues;
|
|
12
|
+
constructor(xs?: number[] | number, opts?: {
|
|
9
13
|
onUpdateItemLayout?: Function;
|
|
10
14
|
onUpdateIntervalTree?: Function;
|
|
11
15
|
});
|
|
@@ -14,12 +18,21 @@ declare class PrefixIntervalTree {
|
|
|
14
18
|
static uniform(size: number, initialValue: number): PrefixIntervalTree;
|
|
15
19
|
static empty(size: number): PrefixIntervalTree;
|
|
16
20
|
stretch(): void;
|
|
21
|
+
indexKey(index: number): string;
|
|
22
|
+
resolveKeyIndex(key: string): number;
|
|
17
23
|
isValidIndex(index: number): boolean;
|
|
18
|
-
|
|
24
|
+
reflowDirectParent(index: number): void;
|
|
25
|
+
reflowHeap(_startIndex: number, endIndex?: number): void;
|
|
26
|
+
dryRemove(index: number): void;
|
|
19
27
|
remove(index: number): void;
|
|
20
|
-
|
|
28
|
+
removeIndices(indices: number[]): void;
|
|
21
29
|
removeV0(index: number): void;
|
|
30
|
+
resetAffected(): void;
|
|
31
|
+
updateAffectedIndices(index: number, type: OperationType, value?: number): void;
|
|
32
|
+
onUpdateIntervalTree(): void;
|
|
33
|
+
drySet(index: number, value: number): boolean;
|
|
22
34
|
set(index: number, value: number): boolean;
|
|
35
|
+
applyUpdate(): void;
|
|
23
36
|
getMaxUsefulLength(): number;
|
|
24
37
|
getActualSize(): number;
|
|
25
38
|
get(index: number): number;
|
|
@@ -21,6 +21,12 @@ function ceilLog2(x) {
|
|
|
21
21
|
}
|
|
22
22
|
var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
23
23
|
function PrefixIntervalTree(xs, opts) {
|
|
24
|
+
if (xs === void 0) {
|
|
25
|
+
xs = 10;
|
|
26
|
+
}
|
|
27
|
+
this._affectedIndicesMap = {};
|
|
28
|
+
this._affectedMinimalIndex = undefined;
|
|
29
|
+
this._cachedSumUntilValues = {};
|
|
24
30
|
if (typeof xs === 'number') this.initWithNumber(xs);
|
|
25
31
|
if (Array.isArray(xs)) this.initWithArray(xs);
|
|
26
32
|
var _ref = opts || {},
|
|
@@ -72,17 +78,33 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
72
78
|
this._size = nextHeapHalf;
|
|
73
79
|
this._heap = nextHeap;
|
|
74
80
|
};
|
|
81
|
+
_proto.indexKey = function indexKey(index) {
|
|
82
|
+
return "" + index;
|
|
83
|
+
};
|
|
84
|
+
_proto.resolveKeyIndex = function resolveKeyIndex(key) {
|
|
85
|
+
return parseInt(key, 10);
|
|
86
|
+
};
|
|
75
87
|
_proto.isValidIndex = function isValidIndex(index) {
|
|
76
88
|
return typeof index === 'number' && index >= 0 && index < this._actualSize;
|
|
77
89
|
};
|
|
78
|
-
_proto.
|
|
90
|
+
_proto.reflowDirectParent = function reflowDirectParent(index) {
|
|
91
|
+
this.resetAffected();
|
|
92
|
+
var node = this._half + index;
|
|
93
|
+
node = parent(node);
|
|
94
|
+
for (; node !== 0; node = parent(node)) {
|
|
95
|
+
this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
_proto.reflowHeap = function reflowHeap(_startIndex, endIndex) {
|
|
79
99
|
var _this = this;
|
|
80
100
|
if (endIndex === void 0) {
|
|
81
101
|
endIndex = this._half * 2 - 2;
|
|
82
102
|
}
|
|
83
|
-
|
|
103
|
+
this.resetAffected();
|
|
104
|
+
var depth = Math.log2(this._size);
|
|
105
|
+
var startIndex = _startIndex + this._half;
|
|
84
106
|
Array.from({
|
|
85
|
-
length:
|
|
107
|
+
length: depth
|
|
86
108
|
}, function (v, i) {
|
|
87
109
|
return i;
|
|
88
110
|
}).reduce(function (acc) {
|
|
@@ -102,28 +124,30 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
102
124
|
endIndex: endIndex
|
|
103
125
|
});
|
|
104
126
|
};
|
|
127
|
+
_proto.dryRemove = function dryRemove(index) {
|
|
128
|
+
if (!this.isValidIndex(index)) return;
|
|
129
|
+
if (isNaN(index)) {
|
|
130
|
+
console.warn('Passing a NaN value as interval tree index');
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
this._heap.splice(this._half + index, 1);
|
|
134
|
+
this._heap.push(0);
|
|
135
|
+
this._actualSize = this._actualSize - 1;
|
|
136
|
+
this.updateAffectedIndices(index, 'remove');
|
|
137
|
+
};
|
|
105
138
|
_proto.remove = function remove(index) {
|
|
106
|
-
this.
|
|
139
|
+
this.removeIndices([index]);
|
|
107
140
|
};
|
|
108
|
-
_proto.
|
|
141
|
+
_proto.removeIndices = function removeIndices(indices) {
|
|
109
142
|
var _this2 = this;
|
|
110
143
|
indices.sort(function (a, b) {
|
|
111
144
|
return a - b;
|
|
112
145
|
});
|
|
113
146
|
indices.forEach(function (index) {
|
|
114
|
-
|
|
115
|
-
if (isNaN(index)) {
|
|
116
|
-
console.warn('Passing a NaN value as interval tree index');
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
_this2._heap.splice(_this2._half + index, 1);
|
|
120
|
-
_this2._heap.push(0);
|
|
121
|
-
_this2._actualSize = _this2._actualSize - 1;
|
|
147
|
+
return _this2.dryRemove(index);
|
|
122
148
|
});
|
|
123
|
-
this.reflowHeap(indices[0]
|
|
124
|
-
|
|
125
|
-
this._onUpdateIntervalTree(this._heap);
|
|
126
|
-
}
|
|
149
|
+
this.reflowHeap(indices[0]);
|
|
150
|
+
this.onUpdateIntervalTree();
|
|
127
151
|
if (typeof this._onUpdateItemLayout === 'function') {
|
|
128
152
|
for (var idx = indices[0]; idx < this._half; idx++) {
|
|
129
153
|
this._onUpdateItemLayout(idx, this.get(idx));
|
|
@@ -148,7 +172,26 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
148
172
|
this._actualSize = this._actualSize - 1;
|
|
149
173
|
this._heap = nextHeap;
|
|
150
174
|
};
|
|
151
|
-
_proto.
|
|
175
|
+
_proto.resetAffected = function resetAffected() {
|
|
176
|
+
this._affectedIndicesMap = {};
|
|
177
|
+
this._affectedMinimalIndex = undefined;
|
|
178
|
+
};
|
|
179
|
+
_proto.updateAffectedIndices = function updateAffectedIndices(index, type, value) {
|
|
180
|
+
if (typeof index === 'number') {
|
|
181
|
+
this._affectedMinimalIndex = typeof this._affectedMinimalIndex === 'number' ? Math.min(this._affectedMinimalIndex, index) : index;
|
|
182
|
+
this._affectedIndicesMap[this.indexKey(index)] = {
|
|
183
|
+
value: value,
|
|
184
|
+
type: type
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
_proto.onUpdateIntervalTree = function onUpdateIntervalTree() {
|
|
189
|
+
this._cachedSumUntilValues = {};
|
|
190
|
+
if (typeof this._onUpdateIntervalTree === 'function') {
|
|
191
|
+
this._onUpdateIntervalTree(this._heap);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
_proto.drySet = function drySet(index, value) {
|
|
152
195
|
if (typeof index !== 'number' || index < 0) return false;
|
|
153
196
|
if (isNaN(index)) {
|
|
154
197
|
console.warn('Passing a NaN value as interval tree index');
|
|
@@ -159,21 +202,45 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
159
202
|
}
|
|
160
203
|
var node = this._half + index;
|
|
161
204
|
this._heap[node] = value;
|
|
162
|
-
|
|
163
|
-
for (; node !== 0; node = parent(node)) {
|
|
164
|
-
this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
|
|
165
|
-
}
|
|
205
|
+
this.updateAffectedIndices(index, 'set', value);
|
|
166
206
|
if (index + 1 > this._actualSize) {
|
|
167
207
|
this._actualSize = index + 1;
|
|
168
208
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
209
|
+
return true;
|
|
210
|
+
};
|
|
211
|
+
_proto.set = function set(index, value) {
|
|
212
|
+
this.drySet(index, value);
|
|
213
|
+
this.reflowDirectParent(index);
|
|
214
|
+
this.onUpdateIntervalTree();
|
|
172
215
|
if (typeof this._onUpdateItemLayout === 'function') {
|
|
173
216
|
this._onUpdateItemLayout(index, value);
|
|
174
217
|
}
|
|
175
218
|
return true;
|
|
176
219
|
};
|
|
220
|
+
_proto.applyUpdate = function applyUpdate() {
|
|
221
|
+
var _this3 = this;
|
|
222
|
+
if (typeof this._affectedMinimalIndex === 'number') {
|
|
223
|
+
var index = this._affectedMinimalIndex;
|
|
224
|
+
var keys = Object.keys(this._affectedIndicesMap);
|
|
225
|
+
var hasRemoveItem = keys.some(function (key) {
|
|
226
|
+
return _this3._affectedIndicesMap[key].type === 'remove';
|
|
227
|
+
});
|
|
228
|
+
if (hasRemoveItem) {
|
|
229
|
+
return this.reflowHeap(index);
|
|
230
|
+
}
|
|
231
|
+
var depth = Math.log2(this._size);
|
|
232
|
+
var itemLength = keys.length;
|
|
233
|
+
var reflowTimes = this._half - index - 1;
|
|
234
|
+
if (depth * itemLength > reflowTimes) {
|
|
235
|
+
return this.reflowHeap(index);
|
|
236
|
+
}
|
|
237
|
+
keys.forEach(function (key) {
|
|
238
|
+
return _this3.reflowDirectParent(_this3.resolveKeyIndex(key));
|
|
239
|
+
});
|
|
240
|
+
this.onUpdateIntervalTree();
|
|
241
|
+
this.resetAffected();
|
|
242
|
+
}
|
|
243
|
+
};
|
|
177
244
|
_proto.getMaxUsefulLength = function getMaxUsefulLength() {
|
|
178
245
|
return this.getActualSize();
|
|
179
246
|
};
|
|
@@ -197,6 +264,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
197
264
|
return this._heap[1];
|
|
198
265
|
};
|
|
199
266
|
_proto.sumUntil = function sumUntil(end) {
|
|
267
|
+
if (this._cachedSumUntilValues["" + end] != null) return this._cachedSumUntilValues["" + end];
|
|
200
268
|
if (end <= 0) {
|
|
201
269
|
return 0;
|
|
202
270
|
}
|
|
@@ -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\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 constructor(\n xs: number[] | number,\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 */\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 isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowHeap(startIndex: number, endIndex = this._half * 2 - 2) {\n const len = Math.log2(this._size);\n\n Array.from({ length: len }, (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 remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.batchRemove([index]);\n }\n\n batchRemove(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => {\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\n this.reflowHeap(indices[0] + this._half);\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\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 set(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 let node = this._half + index;\n this._heap[node] = value;\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 if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\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 (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","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","onUpdateIntervalTree","_onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","isValidIndex","index","reflowHeap","startIndex","endIndex","len","log2","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","remove","batchRemove","indices","sort","a","b","forEach","_this2","isNaN","console","warn","splice","push","get","removeV0","copy","slice","set","value","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","min","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,IAgBKC,kBAAkB;EActB,SAAAA,mBACEL,EAAqB,EACrBM,IAGC;IAED,IAAI,OAAON,EAAE,KAAK,QAAQ,EAAE,IAAI,CAACO,cAAc,CAACP,EAAE,CAAC;IACnD,IAAIQ,KAAK,CAACC,OAAO,CAACT,EAAE,CAAC,EAAE,IAAI,CAACU,aAAa,CAACV,EAAE,CAAC;IAE7C,IAAA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ACnBkF,CAAC,IAAIC,OAAO;;;IAIhB,OAAOlF,IAAI,CAACmF,GAAG,CAACpF,IAAI,GAAG,IAAI,CAACwB,KAAK,EAAE,IAAI,CAACG,WAAW,CAAC;GACrD;EAAAN,MAAA,CAgBDiE,YAAY,GAAZ,SAAAA,aAAaC,SAAiB,EAAEC,SAAiB;IAC/C,IAAI,IAAI,CAACf,OAAO,EAAE,CAAC,CAAC,CAAC,GAAGc,SAAS,EAAE;MACjC,OAAO;QACLjD,UAAU,EAAE,IAAI,CAACX,WAAW;QAC5BY,QAAQ,EAAE,IAAI,CAACZ;OAChB;;IAGH,OAAO;MAELW,UAAU,EAAE,IAAI,CAAC2C,kBAAkB,CAACM,SAAS,CAAC;MAG9ChD,QAAQ,EAAE,IAAI,CAACkD,qBAAqB,CAACD,SAAS;KAC/C;GACF;EAAAnE,MAAA,CAMDqE,eAAe,GAAf,SAAAA,gBAAgBR,CAAS;IACvB,OAAOjF,IAAI,CAACmF,GAAG,CAAC,IAAI,CAACC,wBAAwB,CAACH,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAACvD,WAAW,CAAC;GACxE;EAAAN,MAAA,CAMDoE,qBAAqB,GAArB,SAAAA,sBAAsBP,CAAS;IAC7B,OAAOjF,IAAI,CAACmF,GAAG,CAAC,IAAI,CAACH,kBAAkB,CAACC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAACvD,WAAW,CAAC;GAClE;EAAA,OAAAjB,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: {\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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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,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(t){return Math.floor(t/2)},e=function(t){for(var e=[],i=t-1;i>=0;--i)e[i]=0;return e};function i(t){for(var e=1;e<t;)e*=2;return e}exports.default=function(){function
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(t){return Math.floor(t/2)},e=function(t){for(var e=[],i=t-1;i>=0;--i)e[i]=0;return e};function i(t){for(var e=1;e<t;)e*=2;return e}exports.default=function(){function n(t,e){void 0===t&&(t=10),this._affectedIndicesMap={},this._affectedMinimalIndex=void 0,this._cachedSumUntilValues={},"number"==typeof t&&this.initWithNumber(t),Array.isArray(t)&&this.initWithArray(t);var i=e||{},n=i.onUpdateItemLayout;this._onUpdateIntervalTree=i.onUpdateIntervalTree,this._onUpdateItemLayout=n}var a=n.prototype;return a.initWithNumber=function(t){this._half=i(t),this._size=this._half,this._heap=e(2*this._half),this._actualSize=0},a.initWithArray=function(t){var n;for(this._half=i(t.length),this._size=this._half,this._heap=e(2*this._half),n=0;n<this._size;++n)this._heap[this._half+n]=t[n];for(n=this._half-1;n>0;--n)this._heap[n]=this._heap[2*n]+this._heap[2*n+1];this._actualSize=t.length},n.uniform=function(t,e){for(var i=[],a=t-1;a>=0;--a)i[a]=e;return new n(i)},n.empty=function(t){return n.uniform(t,0)},a.stretch=function(){for(var t=e(2*this._half*2),i=2*this._half,n=0;n<this._size;n++)t[i+n]=this._heap[this._half+n]||0;for(var a=i-1;a>0;a--)t[a]=t[2*a]+t[2*a+1];this._half=i,this._size=i,this._heap=t},a.indexKey=function(t){return""+t},a.resolveKeyIndex=function(t){return parseInt(t,10)},a.isValidIndex=function(t){return"number"==typeof t&&t>=0&&t<this._actualSize},a.reflowDirectParent=function(e){this.resetAffected();var i=this._half+e;for(i=t(i);0!==i;i=t(i))this._heap[i]=this._heap[2*i]+this._heap[2*i+1]},a.reflowHeap=function(e,i){var n=this;void 0===i&&(i=2*this._half-2),this.resetAffected();var a=Math.log2(this._size),r=e+this._half;Array.from({length:a},(function(t,e){return e})).reduce((function(e){for(var i=e.endIndex,a=t(e.startIndex),r=t(i),h=a;h<=r;h++)n._heap[h]=n._heap[2*h]+n._heap[2*h+1];return{startIndex:a,endIndex:r}}),{startIndex:r,endIndex:i})},a.dryRemove=function(t){this.isValidIndex(t)&&(isNaN(t)?console.warn("Passing a NaN value as interval tree index"):(this._heap.splice(this._half+t,1),this._heap.push(0),this._actualSize=this._actualSize-1,this.updateAffectedIndices(t,"remove")))},a.remove=function(t){this.removeIndices([t])},a.removeIndices=function(t){var e=this;if(t.sort((function(t,e){return t-e})),t.forEach((function(t){return e.dryRemove(t)})),this.reflowHeap(t[0]),this.onUpdateIntervalTree(),"function"==typeof this._onUpdateItemLayout)for(var i=t[0];i<this._half;i++)this._onUpdateItemLayout(i,this.get(i))},a.removeV0=function(t){if(this.isValidIndex(t))if(isNaN(t))console.warn("Passing a NaN value as interval tree index");else{var i=e(2*this._half),n=this._heap.slice(this._half);n.splice(t,1);for(var a=this._half;a<2*this._half;a++)i[a]=n[a-this._half]||0;for(var r=this._half-1;r>0;r--)i[r]=i[2*r]+i[2*r+1];this._actualSize=this._actualSize-1,this._heap=i}},a.resetAffected=function(){this._affectedIndicesMap={},this._affectedMinimalIndex=void 0},a.updateAffectedIndices=function(t,e,i){"number"==typeof t&&(this._affectedMinimalIndex="number"==typeof this._affectedMinimalIndex?Math.min(this._affectedMinimalIndex,t):t,this._affectedIndicesMap[this.indexKey(t)]={value:i,type:e})},a.onUpdateIntervalTree=function(){this._cachedSumUntilValues={},"function"==typeof this._onUpdateIntervalTree&&this._onUpdateIntervalTree(this._heap)},a.drySet=function(t,e){if("number"!=typeof t||t<0)return!1;if(isNaN(t))return console.warn("Passing a NaN value as interval tree index"),!1;for(;t>=this._half;)this.stretch();return this._heap[this._half+t]=e,this.updateAffectedIndices(t,"set",e),t+1>this._actualSize&&(this._actualSize=t+1),!0},a.set=function(t,e){return this.drySet(t,e),this.reflowDirectParent(t),this.onUpdateIntervalTree(),"function"==typeof this._onUpdateItemLayout&&this._onUpdateItemLayout(t,e),!0},a.applyUpdate=function(){var t=this;if("number"==typeof this._affectedMinimalIndex){var e=this._affectedMinimalIndex,i=Object.keys(this._affectedIndicesMap);if(i.some((function(e){return"remove"===t._affectedIndicesMap[e].type})))return this.reflowHeap(e);if(Math.log2(this._size)*i.length>this._half-e-1)return this.reflowHeap(e);i.forEach((function(e){return t.reflowDirectParent(t.resolveKeyIndex(e))})),this.onUpdateIntervalTree(),this.resetAffected()}},a.getMaxUsefulLength=function(){return this.getActualSize()},a.getActualSize=function(){return this._actualSize},a.get=function(t){return this._heap[this._half+t]},a.getSize=function(){return this._size},a.getHalf=function(){return this._half},a.getHeap=function(){return this._heap},a.getMaxValue=function(){return this._heap[1]},a.sumUntil=function(e){if(null!=this._cachedSumUntilValues[""+e])return this._cachedSumUntilValues[""+e];if(e<=0)return 0;for(var i=this._half+e-1,n=this._heap[i];1!==i;i=t(i))i%2==1&&(n+=this._heap[i-1]);return n},a.sumTo=function(t){return this.sumUntil(t+1)},a.sum=function(t,e){return this.sumUntil(e)-this.sumUntil(t)},a.greatestLowerBound=function(t){if(t<0||!this._actualSize)return-1;var e=1;if(this._heap[e]<t)return this._actualSize;for(;e<this._half;){var i=this._heap[2*e];t<i?e*=2:(e=2*e+1,t-=i)}return Math.min(e-this._half,this._actualSize)},a.greatestStrictLowerBound=function(t){if(t<=0||!this._actualSize)return-1;var e=1;if(this._heap[e]<t)return this._actualSize;for(;e<this._half;){var i=this._heap[2*e];t<=i?e*=2:(e=2*e+1,t-=i)}return Math.min(e-this._half,this._actualSize)},a.computeRange=function(t,e){return this.getHeap()[1]<t?{startIndex:this._actualSize,endIndex:this._actualSize}:{startIndex:this.greatestLowerBound(t),endIndex:this.leastStrictUpperBound(e)}},a.leastUpperBound=function(t){return Math.min(this.greatestStrictLowerBound(t)+1,this._actualSize)},a.leastStrictUpperBound=function(t){return Math.min(this.greatestLowerBound(t)+1,this._actualSize)},n}();
|
|
2
2
|
//# sourceMappingURL=prefix-interval-tree.cjs.production.min.js.map
|
|
@@ -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\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 constructor(\n xs: number[] | number,\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 */\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 isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowHeap(startIndex: number, endIndex = this._half * 2 - 2) {\n const len = Math.log2(this._size);\n\n Array.from({ length: len }, (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 remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.batchRemove([index]);\n }\n\n batchRemove(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => {\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\n this.reflowHeap(indices[0] + this._half);\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\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 set(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 let node = this._half + index;\n this._heap[node] = value;\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 if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\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 (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","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","_onUpdateIntervalTree","onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","isValidIndex","index","reflowHeap","startIndex","endIndex","len","log2","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","remove","batchRemove","indices","sort","a","b","forEach","_this2","isNaN","console","warn","splice","push","get","removeV0","copy","slice","set","value","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","min","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,6BA+BP,SAAAC,EACEL,EACAM,GAKkB,iBAAPN,GAAiBO,KAAKC,eAAeR,GAC5CS,MAAMC,QAAQV,IAAKO,KAAKI,cAAcX,GAE1C,IAAAY,EAAqDN,GAAQ,GAArDO,EAAkBD,EAAlBC,mBACRN,KAAKO,sBAD2CF,EAApBG,qBAE5BR,KAAKS,oBAAsBH,EAC5B,IAAAI,EAAAZ,EAAAa,UAgWA,OAhWAD,EAEDT,eAAA,SAAeW,GACbZ,KAAKa,MAAQlB,EAASiB,GACtBZ,KAAKc,MAAQd,KAAKa,MAClBb,KAAKe,MAAQxB,EAAY,EAAIS,KAAKa,OAClCb,KAAKgB,YAAc,GACpBN,EAEDN,cAAA,SAAca,GAIZ,IAAIvB,EACJ,IAJAM,KAA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f,CACLjD,WAAY3B,KAAKgB,YACjBY,SAAU5B,KAAKgB,aAIZ,CAELW,WAAY3B,KAAKsE,mBAAmBM,GAGpChD,SAAU5B,KAAK8E,sBAAsBD,KAExCnE,EAMDqE,gBAAA,SAAgBR,GACd,OAAOlF,KAAKoF,IAAIzE,KAAK0E,yBAAyBH,GAAK,EAAGvE,KAAKgB,cAC5DN,EAMDoE,sBAAA,SAAsBP,GACpB,OAAOlF,KAAKoF,IAAIzE,KAAKsE,mBAAmBC,GAAK,EAAGvE,KAAKgB,cACtDlB"}
|
|
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,eAAA,SAAeW,GACbb,KAAKc,MAAQnB,EAASkB,GACtBb,KAAKe,MAAQf,KAAKc,MAClBd,KAAKgB,MAAQzB,EAAY,EAAIS,KAAKc,OAClCd,KAAKiB,YAAc,GACpBN,EAEDN,cAAA,SAAca,GAIZ,IAAIxB,EACJ,IAJ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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"}
|
|
@@ -17,6 +17,12 @@ function ceilLog2(x) {
|
|
|
17
17
|
}
|
|
18
18
|
var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
19
19
|
function PrefixIntervalTree(xs, opts) {
|
|
20
|
+
if (xs === void 0) {
|
|
21
|
+
xs = 10;
|
|
22
|
+
}
|
|
23
|
+
this._affectedIndicesMap = {};
|
|
24
|
+
this._affectedMinimalIndex = undefined;
|
|
25
|
+
this._cachedSumUntilValues = {};
|
|
20
26
|
if (typeof xs === 'number') this.initWithNumber(xs);
|
|
21
27
|
if (Array.isArray(xs)) this.initWithArray(xs);
|
|
22
28
|
var _ref = opts || {},
|
|
@@ -68,17 +74,33 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
68
74
|
this._size = nextHeapHalf;
|
|
69
75
|
this._heap = nextHeap;
|
|
70
76
|
};
|
|
77
|
+
_proto.indexKey = function indexKey(index) {
|
|
78
|
+
return "" + index;
|
|
79
|
+
};
|
|
80
|
+
_proto.resolveKeyIndex = function resolveKeyIndex(key) {
|
|
81
|
+
return parseInt(key, 10);
|
|
82
|
+
};
|
|
71
83
|
_proto.isValidIndex = function isValidIndex(index) {
|
|
72
84
|
return typeof index === 'number' && index >= 0 && index < this._actualSize;
|
|
73
85
|
};
|
|
74
|
-
_proto.
|
|
86
|
+
_proto.reflowDirectParent = function reflowDirectParent(index) {
|
|
87
|
+
this.resetAffected();
|
|
88
|
+
var node = this._half + index;
|
|
89
|
+
node = parent(node);
|
|
90
|
+
for (; node !== 0; node = parent(node)) {
|
|
91
|
+
this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
_proto.reflowHeap = function reflowHeap(_startIndex, endIndex) {
|
|
75
95
|
var _this = this;
|
|
76
96
|
if (endIndex === void 0) {
|
|
77
97
|
endIndex = this._half * 2 - 2;
|
|
78
98
|
}
|
|
79
|
-
|
|
99
|
+
this.resetAffected();
|
|
100
|
+
var depth = Math.log2(this._size);
|
|
101
|
+
var startIndex = _startIndex + this._half;
|
|
80
102
|
Array.from({
|
|
81
|
-
length:
|
|
103
|
+
length: depth
|
|
82
104
|
}, function (v, i) {
|
|
83
105
|
return i;
|
|
84
106
|
}).reduce(function (acc) {
|
|
@@ -98,28 +120,30 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
98
120
|
endIndex: endIndex
|
|
99
121
|
});
|
|
100
122
|
};
|
|
123
|
+
_proto.dryRemove = function dryRemove(index) {
|
|
124
|
+
if (!this.isValidIndex(index)) return;
|
|
125
|
+
if (isNaN(index)) {
|
|
126
|
+
console.warn('Passing a NaN value as interval tree index');
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this._heap.splice(this._half + index, 1);
|
|
130
|
+
this._heap.push(0);
|
|
131
|
+
this._actualSize = this._actualSize - 1;
|
|
132
|
+
this.updateAffectedIndices(index, 'remove');
|
|
133
|
+
};
|
|
101
134
|
_proto.remove = function remove(index) {
|
|
102
|
-
this.
|
|
135
|
+
this.removeIndices([index]);
|
|
103
136
|
};
|
|
104
|
-
_proto.
|
|
137
|
+
_proto.removeIndices = function removeIndices(indices) {
|
|
105
138
|
var _this2 = this;
|
|
106
139
|
indices.sort(function (a, b) {
|
|
107
140
|
return a - b;
|
|
108
141
|
});
|
|
109
142
|
indices.forEach(function (index) {
|
|
110
|
-
|
|
111
|
-
if (isNaN(index)) {
|
|
112
|
-
console.warn('Passing a NaN value as interval tree index');
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
_this2._heap.splice(_this2._half + index, 1);
|
|
116
|
-
_this2._heap.push(0);
|
|
117
|
-
_this2._actualSize = _this2._actualSize - 1;
|
|
143
|
+
return _this2.dryRemove(index);
|
|
118
144
|
});
|
|
119
|
-
this.reflowHeap(indices[0]
|
|
120
|
-
|
|
121
|
-
this._onUpdateIntervalTree(this._heap);
|
|
122
|
-
}
|
|
145
|
+
this.reflowHeap(indices[0]);
|
|
146
|
+
this.onUpdateIntervalTree();
|
|
123
147
|
if (typeof this._onUpdateItemLayout === 'function') {
|
|
124
148
|
for (var idx = indices[0]; idx < this._half; idx++) {
|
|
125
149
|
this._onUpdateItemLayout(idx, this.get(idx));
|
|
@@ -144,7 +168,26 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
144
168
|
this._actualSize = this._actualSize - 1;
|
|
145
169
|
this._heap = nextHeap;
|
|
146
170
|
};
|
|
147
|
-
_proto.
|
|
171
|
+
_proto.resetAffected = function resetAffected() {
|
|
172
|
+
this._affectedIndicesMap = {};
|
|
173
|
+
this._affectedMinimalIndex = undefined;
|
|
174
|
+
};
|
|
175
|
+
_proto.updateAffectedIndices = function updateAffectedIndices(index, type, value) {
|
|
176
|
+
if (typeof index === 'number') {
|
|
177
|
+
this._affectedMinimalIndex = typeof this._affectedMinimalIndex === 'number' ? Math.min(this._affectedMinimalIndex, index) : index;
|
|
178
|
+
this._affectedIndicesMap[this.indexKey(index)] = {
|
|
179
|
+
value: value,
|
|
180
|
+
type: type
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
_proto.onUpdateIntervalTree = function onUpdateIntervalTree() {
|
|
185
|
+
this._cachedSumUntilValues = {};
|
|
186
|
+
if (typeof this._onUpdateIntervalTree === 'function') {
|
|
187
|
+
this._onUpdateIntervalTree(this._heap);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
_proto.drySet = function drySet(index, value) {
|
|
148
191
|
if (typeof index !== 'number' || index < 0) return false;
|
|
149
192
|
if (isNaN(index)) {
|
|
150
193
|
console.warn('Passing a NaN value as interval tree index');
|
|
@@ -155,21 +198,45 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
155
198
|
}
|
|
156
199
|
var node = this._half + index;
|
|
157
200
|
this._heap[node] = value;
|
|
158
|
-
|
|
159
|
-
for (; node !== 0; node = parent(node)) {
|
|
160
|
-
this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
|
|
161
|
-
}
|
|
201
|
+
this.updateAffectedIndices(index, 'set', value);
|
|
162
202
|
if (index + 1 > this._actualSize) {
|
|
163
203
|
this._actualSize = index + 1;
|
|
164
204
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
205
|
+
return true;
|
|
206
|
+
};
|
|
207
|
+
_proto.set = function set(index, value) {
|
|
208
|
+
this.drySet(index, value);
|
|
209
|
+
this.reflowDirectParent(index);
|
|
210
|
+
this.onUpdateIntervalTree();
|
|
168
211
|
if (typeof this._onUpdateItemLayout === 'function') {
|
|
169
212
|
this._onUpdateItemLayout(index, value);
|
|
170
213
|
}
|
|
171
214
|
return true;
|
|
172
215
|
};
|
|
216
|
+
_proto.applyUpdate = function applyUpdate() {
|
|
217
|
+
var _this3 = this;
|
|
218
|
+
if (typeof this._affectedMinimalIndex === 'number') {
|
|
219
|
+
var index = this._affectedMinimalIndex;
|
|
220
|
+
var keys = Object.keys(this._affectedIndicesMap);
|
|
221
|
+
var hasRemoveItem = keys.some(function (key) {
|
|
222
|
+
return _this3._affectedIndicesMap[key].type === 'remove';
|
|
223
|
+
});
|
|
224
|
+
if (hasRemoveItem) {
|
|
225
|
+
return this.reflowHeap(index);
|
|
226
|
+
}
|
|
227
|
+
var depth = Math.log2(this._size);
|
|
228
|
+
var itemLength = keys.length;
|
|
229
|
+
var reflowTimes = this._half - index - 1;
|
|
230
|
+
if (depth * itemLength > reflowTimes) {
|
|
231
|
+
return this.reflowHeap(index);
|
|
232
|
+
}
|
|
233
|
+
keys.forEach(function (key) {
|
|
234
|
+
return _this3.reflowDirectParent(_this3.resolveKeyIndex(key));
|
|
235
|
+
});
|
|
236
|
+
this.onUpdateIntervalTree();
|
|
237
|
+
this.resetAffected();
|
|
238
|
+
}
|
|
239
|
+
};
|
|
173
240
|
_proto.getMaxUsefulLength = function getMaxUsefulLength() {
|
|
174
241
|
return this.getActualSize();
|
|
175
242
|
};
|
|
@@ -193,6 +260,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
|
|
|
193
260
|
return this._heap[1];
|
|
194
261
|
};
|
|
195
262
|
_proto.sumUntil = function sumUntil(end) {
|
|
263
|
+
if (this._cachedSumUntilValues["" + end] != null) return this._cachedSumUntilValues["" + end];
|
|
196
264
|
if (end <= 0) {
|
|
197
265
|
return 0;
|
|
198
266
|
}
|
|
@@ -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\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 constructor(\n xs: number[] | number,\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 */\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 isValidIndex(index: number) {\n return typeof index === 'number' && index >= 0 && index < this._actualSize;\n }\n\n reflowHeap(startIndex: number, endIndex = this._half * 2 - 2) {\n const len = Math.log2(this._size);\n\n Array.from({ length: len }, (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 remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n\n this.batchRemove([index]);\n }\n\n batchRemove(indices: number[]) {\n indices.sort((a, b) => a - b);\n\n indices.forEach((index) => {\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\n this.reflowHeap(indices[0] + this._half);\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\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 set(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 let node = this._half + index;\n this._heap[node] = value;\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 if (index + 1 > this._actualSize) {\n this._actualSize = index + 1;\n }\n\n if (typeof this._onUpdateIntervalTree === 'function') {\n this._onUpdateIntervalTree(this._heap);\n }\n\n if (typeof this._onUpdateItemLayout === 'function') {\n this._onUpdateItemLayout(index, value);\n }\n return true;\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 (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","initWithNumber","Array","isArray","initWithArray","_ref","onUpdateItemLayout","onUpdateIntervalTree","_onUpdateIntervalTree","_onUpdateItemLayout","_proto","prototype","length","_half","_size","_heap","_actualSize","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","isValidIndex","index","reflowHeap","startIndex","endIndex","len","log2","from","v","reduce","acc","_nextStart","_nextEnd","idx","_this","remove","batchRemove","indices","sort","a","b","forEach","_this2","isNaN","console","warn","splice","push","get","removeV0","copy","slice","set","value","getMaxUsefulLength","getActualSize","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","leftSum","min","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,IAgBKC,kBAAkB;EActB,SAAAA,mBACEL,EAAqB,EACrBM,IAGC;IAED,IAAI,OAAON,EAAE,KAAK,QAAQ,EAAE,IAAI,CAAC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kF,CAAC,IAAIC,OAAO,EAAE;QAChBnF,IAAI,GAAG,CAAC,GAAGA,IAAI;OAChB,MAAM;QACLA,IAAI,GAAG,CAAC,GAAGA,IAAI,GAAG,CAAC;QACnBkF,CAAC,IAAIC,OAAO;;;IAIhB,OAAOlF,IAAI,CAACmF,GAAG,CAACpF,IAAI,GAAG,IAAI,CAACwB,KAAK,EAAE,IAAI,CAACG,WAAW,CAAC;GACrD;EAAAN,MAAA,CAgBDiE,YAAY,GAAZ,SAAAA,aAAaC,SAAiB,EAAEC,SAAiB;IAC/C,IAAI,IAAI,CAACf,OAAO,EAAE,CAAC,CAAC,CAAC,GAAGc,SAAS,EAAE;MACjC,OAAO;QACLjD,UAAU,EAAE,IAAI,CAACX,WAAW;QAC5BY,QAAQ,EAAE,IAAI,CAACZ;OAChB;;IAGH,OAAO;MAELW,UAAU,EAAE,IAAI,CAAC2C,kBAAkB,CAACM,SAAS,CAAC;MAG9ChD,QAAQ,EAAE,IAAI,CAACkD,qBAAqB,CAACD,SAAS;KAC/C;GACF;EAAAnE,MAAA,CAMDqE,eAAe,GAAf,SAAAA,gBAAgBR,CAAS;IACvB,OAAOjF,IAAI,CAACmF,GAAG,CAAC,IAAI,CAACC,wBAAwB,CAACH,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAACvD,WAAW,CAAC;GACxE;EAAAN,MAAA,CAMDoE,qBAAqB,GAArB,SAAAA,sBAAsBP,CAAS;IAC7B,OAAOjF,IAAI,CAACmF,GAAG,CAAC,IAAI,CAACH,kBAAkB,CAACC,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAACvD,WAAW,CAAC;GAClE;EAAA,OAAAjB,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: {\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,I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U,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;;;;"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -20,6 +20,13 @@ function ceilLog2(x: number) {
|
|
|
20
20
|
return y;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
type OperationType = 'set' | 'remove';
|
|
24
|
+
|
|
25
|
+
type Operation = {
|
|
26
|
+
type: OperationType;
|
|
27
|
+
value: any;
|
|
28
|
+
};
|
|
29
|
+
|
|
23
30
|
/**
|
|
24
31
|
* A prefix interval tree stores an numeric array and the partial sums of that
|
|
25
32
|
* array. It is optimized for updating the values of the array without
|
|
@@ -48,8 +55,16 @@ class PrefixIntervalTree {
|
|
|
48
55
|
private _onUpdateItemLayout: Function;
|
|
49
56
|
private _onUpdateIntervalTree: Function;
|
|
50
57
|
|
|
58
|
+
private _affectedIndicesMap: {
|
|
59
|
+
[key: string]: Operation;
|
|
60
|
+
} = {};
|
|
61
|
+
private _affectedMinimalIndex: number = undefined;
|
|
62
|
+
private _cachedSumUntilValues: {
|
|
63
|
+
[key: string]: number;
|
|
64
|
+
} = {};
|
|
65
|
+
|
|
51
66
|
constructor(
|
|
52
|
-
xs: number[] | number,
|
|
67
|
+
xs: number[] | number = 10,
|
|
53
68
|
opts?: {
|
|
54
69
|
onUpdateItemLayout?: Function;
|
|
55
70
|
onUpdateIntervalTree?: Function;
|
|
@@ -100,6 +115,7 @@ class PrefixIntervalTree {
|
|
|
100
115
|
|
|
101
116
|
/**
|
|
102
117
|
* the length should be 2
|
|
118
|
+
* it will cause calculation of
|
|
103
119
|
*/
|
|
104
120
|
stretch() {
|
|
105
121
|
const nextHeap = createArray(2 * this._half * 2);
|
|
@@ -120,14 +136,35 @@ class PrefixIntervalTree {
|
|
|
120
136
|
this._heap = nextHeap;
|
|
121
137
|
}
|
|
122
138
|
|
|
139
|
+
indexKey(index: number) {
|
|
140
|
+
return `${index}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
resolveKeyIndex(key: string) {
|
|
144
|
+
return parseInt(key, 10);
|
|
145
|
+
}
|
|
146
|
+
|
|
123
147
|
isValidIndex(index: number) {
|
|
124
148
|
return typeof index === 'number' && index >= 0 && index < this._actualSize;
|
|
125
149
|
}
|
|
126
150
|
|
|
127
|
-
|
|
128
|
-
|
|
151
|
+
reflowDirectParent(index: number) {
|
|
152
|
+
this.resetAffected();
|
|
153
|
+
let node = this._half + index;
|
|
129
154
|
|
|
130
|
-
|
|
155
|
+
node = parent(node);
|
|
156
|
+
for (; node !== 0; node = parent(node)) {
|
|
157
|
+
this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// recalculate parent value from startIndex to endIndex
|
|
162
|
+
reflowHeap(_startIndex: number, endIndex = this._half * 2 - 2) {
|
|
163
|
+
this.resetAffected();
|
|
164
|
+
const depth = Math.log2(this._size);
|
|
165
|
+
const startIndex = _startIndex + this._half;
|
|
166
|
+
|
|
167
|
+
Array.from({ length: depth }, (v, i) => i).reduce(
|
|
131
168
|
(acc) => {
|
|
132
169
|
const { startIndex, endIndex } = acc;
|
|
133
170
|
const _nextStart = parent(startIndex);
|
|
@@ -149,31 +186,40 @@ class PrefixIntervalTree {
|
|
|
149
186
|
);
|
|
150
187
|
}
|
|
151
188
|
|
|
189
|
+
/**
|
|
190
|
+
*
|
|
191
|
+
* @param index index to remove
|
|
192
|
+
* @returns
|
|
193
|
+
*
|
|
194
|
+
* update heap only, but not recalculate interval tree value
|
|
195
|
+
*/
|
|
196
|
+
dryRemove(index: number) {
|
|
197
|
+
if (!this.isValidIndex(index)) return;
|
|
198
|
+
if (isNaN(index)) {
|
|
199
|
+
console.warn('Passing a NaN value as interval tree index');
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this._heap.splice(this._half + index, 1);
|
|
204
|
+
this._heap.push(0);
|
|
205
|
+
this._actualSize = this._actualSize - 1;
|
|
206
|
+
|
|
207
|
+
this.updateAffectedIndices(index, 'remove');
|
|
208
|
+
}
|
|
209
|
+
|
|
152
210
|
remove(index: number) {
|
|
153
211
|
// if typeof index === 'undefined', then it will go into looooooooop
|
|
154
212
|
|
|
155
|
-
this.
|
|
213
|
+
this.removeIndices([index]);
|
|
156
214
|
}
|
|
157
215
|
|
|
158
|
-
|
|
216
|
+
removeIndices(indices: number[]) {
|
|
159
217
|
indices.sort((a, b) => a - b);
|
|
160
218
|
|
|
161
|
-
indices.forEach((index) =>
|
|
162
|
-
if (!this.isValidIndex(index)) return;
|
|
163
|
-
if (isNaN(index)) {
|
|
164
|
-
console.warn('Passing a NaN value as interval tree index');
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
219
|
+
indices.forEach((index) => this.dryRemove(index));
|
|
167
220
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
this._actualSize = this._actualSize - 1;
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
this.reflowHeap(indices[0] + this._half);
|
|
174
|
-
if (typeof this._onUpdateIntervalTree === 'function') {
|
|
175
|
-
this._onUpdateIntervalTree(this._heap);
|
|
176
|
-
}
|
|
221
|
+
this.reflowHeap(indices[0]);
|
|
222
|
+
this.onUpdateIntervalTree();
|
|
177
223
|
|
|
178
224
|
if (typeof this._onUpdateItemLayout === 'function') {
|
|
179
225
|
for (let idx = indices[0]; idx < this._half; idx++) {
|
|
@@ -206,7 +252,34 @@ class PrefixIntervalTree {
|
|
|
206
252
|
this._heap = nextHeap;
|
|
207
253
|
}
|
|
208
254
|
|
|
209
|
-
|
|
255
|
+
resetAffected() {
|
|
256
|
+
this._affectedIndicesMap = {};
|
|
257
|
+
this._affectedMinimalIndex = undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
updateAffectedIndices(index: number, type: OperationType, value?: number) {
|
|
261
|
+
if (typeof index === 'number') {
|
|
262
|
+
this._affectedMinimalIndex =
|
|
263
|
+
typeof this._affectedMinimalIndex === 'number'
|
|
264
|
+
? Math.min(this._affectedMinimalIndex, index)
|
|
265
|
+
: index;
|
|
266
|
+
this._affectedIndicesMap[this.indexKey(index)] = {
|
|
267
|
+
value,
|
|
268
|
+
type,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
onUpdateIntervalTree() {
|
|
274
|
+
// reset cached value
|
|
275
|
+
this._cachedSumUntilValues = {};
|
|
276
|
+
|
|
277
|
+
if (typeof this._onUpdateIntervalTree === 'function') {
|
|
278
|
+
this._onUpdateIntervalTree(this._heap);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
drySet(index: number, value: number) {
|
|
210
283
|
if (typeof index !== 'number' || index < 0) return false;
|
|
211
284
|
if (isNaN(index)) {
|
|
212
285
|
console.warn('Passing a NaN value as interval tree index');
|
|
@@ -217,28 +290,57 @@ class PrefixIntervalTree {
|
|
|
217
290
|
this.stretch();
|
|
218
291
|
}
|
|
219
292
|
|
|
220
|
-
|
|
293
|
+
const node = this._half + index;
|
|
221
294
|
this._heap[node] = value;
|
|
222
295
|
|
|
223
|
-
|
|
224
|
-
for (; node !== 0; node = parent(node)) {
|
|
225
|
-
this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
|
|
226
|
-
}
|
|
296
|
+
this.updateAffectedIndices(index, 'set', value);
|
|
227
297
|
|
|
228
298
|
if (index + 1 > this._actualSize) {
|
|
229
299
|
this._actualSize = index + 1;
|
|
230
300
|
}
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
231
303
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
304
|
+
set(index: number, value: number) {
|
|
305
|
+
this.drySet(index, value);
|
|
306
|
+
this.reflowDirectParent(index);
|
|
307
|
+
this.onUpdateIntervalTree();
|
|
236
308
|
if (typeof this._onUpdateItemLayout === 'function') {
|
|
237
309
|
this._onUpdateItemLayout(index, value);
|
|
238
310
|
}
|
|
239
311
|
return true;
|
|
240
312
|
}
|
|
241
313
|
|
|
314
|
+
applyUpdate() {
|
|
315
|
+
if (typeof this._affectedMinimalIndex === 'number') {
|
|
316
|
+
const index = this._affectedMinimalIndex;
|
|
317
|
+
|
|
318
|
+
const keys = Object.keys(this._affectedIndicesMap);
|
|
319
|
+
const hasRemoveItem = keys.some(
|
|
320
|
+
(key) => this._affectedIndicesMap[key].type === 'remove'
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
if (hasRemoveItem) {
|
|
324
|
+
return this.reflowHeap(index);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const depth = Math.log2(this._size);
|
|
328
|
+
const itemLength = keys.length;
|
|
329
|
+
|
|
330
|
+
const reflowTimes = this._half - index - 1;
|
|
331
|
+
|
|
332
|
+
if (depth * itemLength > reflowTimes) {
|
|
333
|
+
return this.reflowHeap(index);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
keys.forEach((key) => this.reflowDirectParent(this.resolveKeyIndex(key)));
|
|
337
|
+
|
|
338
|
+
this.onUpdateIntervalTree();
|
|
339
|
+
|
|
340
|
+
this.resetAffected();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
242
344
|
getMaxUsefulLength() {
|
|
243
345
|
return this.getActualSize();
|
|
244
346
|
}
|
|
@@ -276,6 +378,8 @@ class PrefixIntervalTree {
|
|
|
276
378
|
sumUntil(end: number) {
|
|
277
379
|
// invariant(end >= 0 && end < this._size + 1, 'Index out of range %s', end);
|
|
278
380
|
|
|
381
|
+
if (this._cachedSumUntilValues[`${end}`] != null)
|
|
382
|
+
return this._cachedSumUntilValues[`${end}`];
|
|
279
383
|
if (end <= 0) {
|
|
280
384
|
return 0;
|
|
281
385
|
}
|