@x-oasis/prefix-interval-tree 0.1.35 → 0.2.1

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/README.md CHANGED
@@ -16,4 +16,66 @@ import PrefixIntervalTree from '@x-oasis/prefix-interval-tree'
16
16
 
17
17
  ```bash
18
18
  $ pnpm test
19
+ ```
20
+
21
+ # API
22
+
23
+ ## PrefixIntervalTree Constructor
24
+
25
+ ```ts
26
+ const intervalTree = new PrefixIntervalTree([2,5,7])
27
+
28
+ const intervalTree = new PrefixIntervalTree(10)
29
+ ```
30
+
31
+ Total interval tree array length is power of `2`, such as 8, 16, 32; and the input length value means the half size, which means `10` will result in `2^4 = 16` first, then patch on interval tree, it will be total `2 * 16 = 32`.
32
+
33
+ ## getHeap
34
+
35
+ ```ts
36
+ getHeap(): number[]
37
+ ```
38
+
39
+ ## getActualSize
40
+
41
+ Basically, interval tree's size is this._half, they all have default `0` value. when you want to get the actual size which has been set with value, then call this method.
42
+
43
+ ## get(index: number)
44
+
45
+ ```ts
46
+ get(index: number): number
47
+ ```
48
+
49
+ get the index value
50
+
51
+ ## set(index: number)
52
+
53
+ ```ts
54
+ set (index: number): boolean
55
+ ```
56
+
57
+ To update the index value in interval tree, its parent will be updated as accordingly.
58
+
59
+
60
+ ## computeRange(minValue: number, maxValue: number)
61
+
62
+ ```ts
63
+ computeRange(minValue: number, maxValue: number): {
64
+ startIndex: number
65
+ endIndex: number
66
+ }
67
+ ```
68
+
69
+ - `startIndex`: the biggest index less than or equal minValue;
70
+ - `endIndex`: the smallest index greater than maxValue;
71
+
72
+ when using the return value, endIndex item should not be included.
73
+
74
+ ```ts
75
+ const arr = []
76
+ const intervalTree = new PrefixIntervalTree(arr)
77
+
78
+ const { startIndex, endIndex } = intervalTree.computeRange(100, 200);
79
+
80
+ const itemsInViewport = arr.slice(startIndex, endIndex)
19
81
  ```
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ declare class PrefixIntervalTree {
2
2
  private _size;
3
3
  private _half;
4
4
  private _heap;
5
- private _maxUsefulLength;
5
+ private _actualSize;
6
6
  private _onUpdateItemLayout;
7
7
  private _onUpdateIntervalTree;
8
8
  constructor(xs: number[] | number, opts?: {
@@ -14,9 +14,14 @@ declare class PrefixIntervalTree {
14
14
  static uniform(size: number, initialValue: number): PrefixIntervalTree;
15
15
  static empty(size: number): PrefixIntervalTree;
16
16
  stretch(): void;
17
+ isValidIndex(index: number): boolean;
18
+ reflowHeap(startIndex: number, endIndex?: number): void;
17
19
  remove(index: number): void;
18
- set(index: number, value: number): void;
20
+ batchRemove(indices: number[]): void;
21
+ removeV0(index: number): void;
22
+ set(index: number, value: number): boolean;
19
23
  getMaxUsefulLength(): number;
24
+ getActualSize(): number;
20
25
  get(index: number): number;
21
26
  getSize(): number;
22
27
  getHalf(): number;
@@ -34,7 +34,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
34
34
  this._half = ceilLog2(length);
35
35
  this._size = this._half;
36
36
  this._heap = createArray(2 * this._half);
37
- this._maxUsefulLength = 0;
37
+ this._actualSize = 0;
38
38
  };
39
39
  _proto.initWithArray = function initWithArray(arr) {
40
40
  this._half = ceilLog2(arr.length);
@@ -47,6 +47,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
47
47
  for (i = this._half - 1; i > 0; --i) {
48
48
  this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];
49
49
  }
50
+ this._actualSize = arr.length;
50
51
  };
51
52
  PrefixIntervalTree.uniform = function uniform(size, initialValue) {
52
53
  var xs = [];
@@ -71,8 +72,66 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
71
72
  this._size = nextHeapHalf;
72
73
  this._heap = nextHeap;
73
74
  };
75
+ _proto.isValidIndex = function isValidIndex(index) {
76
+ return typeof index === 'number' && index >= 0 && index < this._actualSize;
77
+ };
78
+ _proto.reflowHeap = function reflowHeap(startIndex, endIndex) {
79
+ var _this = this;
80
+ if (endIndex === void 0) {
81
+ endIndex = this._half * 2 - 2;
82
+ }
83
+ var len = Math.log2(this._size);
84
+ Array.from({
85
+ length: len
86
+ }, function (v, i) {
87
+ return i;
88
+ }).reduce(function (acc) {
89
+ var startIndex = acc.startIndex,
90
+ endIndex = acc.endIndex;
91
+ var _nextStart = parent(startIndex);
92
+ var _nextEnd = parent(endIndex);
93
+ for (var idx = _nextStart; idx <= _nextEnd; idx++) {
94
+ _this._heap[idx] = _this._heap[2 * idx] + _this._heap[2 * idx + 1];
95
+ }
96
+ return {
97
+ startIndex: _nextStart,
98
+ endIndex: _nextEnd
99
+ };
100
+ }, {
101
+ startIndex: startIndex,
102
+ endIndex: endIndex
103
+ });
104
+ };
74
105
  _proto.remove = function remove(index) {
75
- if (typeof index !== 'number' || index >= this._maxUsefulLength) return;
106
+ this.batchRemove([index]);
107
+ };
108
+ _proto.batchRemove = function batchRemove(indices) {
109
+ var _this2 = this;
110
+ indices.sort(function (a, b) {
111
+ return a - b;
112
+ });
113
+ indices.forEach(function (index) {
114
+ if (!_this2.isValidIndex(index)) return;
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;
122
+ });
123
+ this.reflowHeap(indices[0] + this._half);
124
+ if (typeof this._onUpdateIntervalTree === 'function') {
125
+ this._onUpdateIntervalTree(this._heap);
126
+ }
127
+ if (typeof this._onUpdateItemLayout === 'function') {
128
+ for (var idx = indices[0]; idx < this._half; idx++) {
129
+ this._onUpdateItemLayout(idx, this.get(idx));
130
+ }
131
+ }
132
+ };
133
+ _proto.removeV0 = function removeV0(index) {
134
+ if (!this.isValidIndex(index)) return;
76
135
  if (isNaN(index)) {
77
136
  console.warn('Passing a NaN value as interval tree index');
78
137
  return;
@@ -86,14 +145,14 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
86
145
  for (var _index2 = this._half - 1; _index2 > 0; _index2--) {
87
146
  nextHeap[_index2] = nextHeap[2 * _index2] + nextHeap[2 * _index2 + 1];
88
147
  }
89
- this._maxUsefulLength = this._maxUsefulLength - 1;
148
+ this._actualSize = this._actualSize - 1;
90
149
  this._heap = nextHeap;
91
150
  };
92
151
  _proto.set = function set(index, value) {
93
- if (typeof index !== 'number') return;
152
+ if (typeof index !== 'number' || index < 0) return false;
94
153
  if (isNaN(index)) {
95
154
  console.warn('Passing a NaN value as interval tree index');
96
- return;
155
+ return false;
97
156
  }
98
157
  while (index >= this._half) {
99
158
  this.stretch();
@@ -104,8 +163,8 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
104
163
  for (; node !== 0; node = parent(node)) {
105
164
  this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
106
165
  }
107
- if (index + 1 > this._maxUsefulLength) {
108
- this._maxUsefulLength = index + 1;
166
+ if (index + 1 > this._actualSize) {
167
+ this._actualSize = index + 1;
109
168
  }
110
169
  if (typeof this._onUpdateIntervalTree === 'function') {
111
170
  this._onUpdateIntervalTree(this._heap);
@@ -113,9 +172,13 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
113
172
  if (typeof this._onUpdateItemLayout === 'function') {
114
173
  this._onUpdateItemLayout(index, value);
115
174
  }
175
+ return true;
116
176
  };
117
177
  _proto.getMaxUsefulLength = function getMaxUsefulLength() {
118
- return this._maxUsefulLength;
178
+ return this.getActualSize();
179
+ };
180
+ _proto.getActualSize = function getActualSize() {
181
+ return this._actualSize;
119
182
  };
120
183
  _proto.get = function get(index) {
121
184
  var node = this._half + index;
@@ -158,7 +221,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
158
221
  }
159
222
  var node = 1;
160
223
  if (this._heap[node] < t) {
161
- return Math.max(this._maxUsefulLength - 1, 0);
224
+ return Math.max(this._actualSize - 1, 0);
162
225
  }
163
226
  while (node < this._half) {
164
227
  var leftSum = this._heap[2 * node];
@@ -169,7 +232,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
169
232
  t -= leftSum;
170
233
  }
171
234
  }
172
- return Math.min(node - this._half, this._maxUsefulLength - 1);
235
+ return Math.min(node - this._half, this._actualSize - 1);
173
236
  };
174
237
  _proto.greatestStrictLowerBound = function greatestStrictLowerBound(t) {
175
238
  if (t <= 0) {
@@ -177,7 +240,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
177
240
  }
178
241
  var node = 1;
179
242
  if (this._heap[node] < t) {
180
- return Math.max(this._maxUsefulLength - 1, 0);
243
+ return Math.max(this._actualSize - 1, 0);
181
244
  }
182
245
  while (node < this._half) {
183
246
  var leftSum = this._heap[2 * node];
@@ -188,27 +251,25 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
188
251
  t -= leftSum;
189
252
  }
190
253
  }
191
- return Math.min(node - this._half, this._maxUsefulLength - 1);
254
+ return Math.min(node - this._half, this._actualSize - 1);
192
255
  };
193
256
  _proto.computeRange = function computeRange(minOffset, maxOffset) {
194
257
  if (this.getHeap()[1] < minOffset) {
195
258
  return {
196
- startIndex: this._maxUsefulLength - 1,
197
- endIndex: this._maxUsefulLength - 1
259
+ startIndex: this._actualSize,
260
+ endIndex: this._actualSize
198
261
  };
199
262
  }
200
- var startIndex = this.leastStrictUpperBound(minOffset);
201
- var endIndex = Math.min(this.leastStrictUpperBound(maxOffset), Math.max(this._maxUsefulLength - 1, 0));
202
263
  return {
203
- startIndex: endIndex >= 0 ? Math.max(startIndex, 0) : -1,
204
- endIndex: endIndex
264
+ startIndex: this.greatestLowerBound(minOffset),
265
+ endIndex: this.leastStrictUpperBound(maxOffset)
205
266
  };
206
267
  };
207
268
  _proto.leastUpperBound = function leastUpperBound(t) {
208
- return this.greatestLowerBound(t) + 1;
269
+ return this.greatestStrictLowerBound(t) + 1;
209
270
  };
210
271
  _proto.leastStrictUpperBound = function leastStrictUpperBound(t) {
211
- return this.greatestStrictLowerBound(t);
272
+ return this.greatestLowerBound(t) + 1;
212
273
  };
213
274
  return PrefixIntervalTree;
214
275
  }();
@@ -1 +1 @@
1
- {"version":3,"file":"prefix-interval-tree.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\n\nconst 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 _maxUsefulLength: 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._maxUsefulLength = 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 // this._maxUsefulLength = arr.length;\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 }\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 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 remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (typeof index !== 'number' || index >= this._maxUsefulLength) 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 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._maxUsefulLength = this._maxUsefulLength - 1;\n this._heap = nextHeap;\n }\n\n set(index: number, value: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (typeof index !== 'number') return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\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._maxUsefulLength) {\n this._maxUsefulLength = 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 }\n\n getMaxUsefulLength() {\n return this._maxUsefulLength;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n\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 */\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 * Returns the smallest i such that 0 <= i <= size and sumUntil(i) <= t, or\n * -1 if no such i exists.\n */\n greatestLowerBound(t: number) {\n if (t < 0) {\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 Math.max(this._maxUsefulLength - 1, 0);\n }\n\n // 这种写法的结果就是,如果中间一个item的length为0的话,那么它会一直往右边查;\n // 比如初始化的时候是[0, 0, 0, 0, 0, 0, 0, 0];\n // 你会发现node最后会是7,this._half为4;最终即使data没有数据,那么它的index算出来的也是\n // 7 - 4 = 3;所以,考虑到会存在一些item length为0的情况,所以,这个其实是比较合理的方式,\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 return Math.min(node - this._half, this._maxUsefulLength - 1);\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and sumUntil(i) < t, or\n * -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return Math.max(this._maxUsefulLength - 1, 0);\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._maxUsefulLength - 1);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 100, 100, 100, 100].\n * this.leastStrictUpperBound(330) = 3\n * this.leastStrictUpperBound(400) = 3 (should be 4....)\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._maxUsefulLength - 1,\n endIndex: this._maxUsefulLength - 1,\n };\n }\n const startIndex = this.leastStrictUpperBound(minOffset);\n\n // end的话,需要把index + 1,这样才能够把自个也加进去\n const endIndex = Math.min(\n this.leastStrictUpperBound(maxOffset),\n Math.max(this._maxUsefulLength - 1, 0)\n );\n\n return {\n startIndex: endIndex >= 0 ? Math.max(startIndex, 0) : -1,\n endIndex,\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 this.greatestLowerBound(t) + 1;\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 leastStrictUpperBound(t: number) {\n return this.greatestStrictLowerBound(t);\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","_maxUsefulLength","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","remove","index","isNaN","console","warn","copy","slice","splice","set","value","getMaxUsefulLength","get","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","max","leftSum","min","greatestStrictLowerBound","computeRange","minOffset","maxOffset","startIndex","endIndex","leastStrictUpperBound","leastUpperBound"],"mappings":";;;;AAEA,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,I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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) {\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 Math.max(this._actualSize - 1, 0);\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 - 1);\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) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return Math.max(this._actualSize - 1, 0);\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 - 1);\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 this.greatestStrictLowerBound(t) + 1;\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 this.greatestLowerBound(t) + 1;\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","max","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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,wBAAwB,CAACJ,CAAC,CAAC,GAAG,CAAC;GAC5C;EAAA7D,MAAA,CAMDqE,qBAAqB,GAArB,SAAAA,sBAAsBR,CAAS;IAC7B,OAAO,IAAI,CAACD,kBAAkB,CAACC,CAAC,CAAC,GAAG,CAAC;GACtC;EAAA,OAAAxE,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 h(t,e){"number"==typeof t&&this.initWithNumber(t),Array.isArray(t)&&this.initWithArray(t);var i=e||{},h=i.onUpdateItemLayout;this._onUpdateIntervalTree=i.onUpdateIntervalTree,this._onUpdateItemLayout=h}var n=h.prototype;return n.initWithNumber=function(t){this._half=i(t),this._size=this._half,this._heap=e(2*this._half),this._maxUsefulLength=0},n.initWithArray=function(t){var h;for(this._half=i(t.length),this._size=this._half,this._heap=e(2*this._half),h=0;h<this._size;++h)this._heap[this._half+h]=t[h];for(h=this._half-1;h>0;--h)this._heap[h]=this._heap[2*h]+this._heap[2*h+1]},h.uniform=function(t,e){for(var i=[],n=t-1;n>=0;--n)i[n]=e;return new h(i)},h.empty=function(t){return h.uniform(t,0)},n.stretch=function(){for(var t=e(2*this._half*2),i=2*this._half,h=0;h<this._size;h++)t[i+h]=this._heap[this._half+h]||0;for(var n=i-1;n>0;n--)t[n]=t[2*n]+t[2*n+1];this._half=i,this._size=i,this._heap=t},n.remove=function(t){if(!("number"!=typeof t||t>=this._maxUsefulLength))if(isNaN(t))console.warn("Passing a NaN value as interval tree index");else{var i=e(2*this._half),h=this._heap.slice(this._half);h.splice(t,1);for(var n=this._half;n<2*this._half;n++)i[n]=h[n-this._half]||0;for(var a=this._half-1;a>0;a--)i[a]=i[2*a]+i[2*a+1];this._maxUsefulLength=this._maxUsefulLength-1,this._heap=i}},n.set=function(e,i){if("number"==typeof e)if(isNaN(e))console.warn("Passing a NaN value as interval tree index");else{for(;e>=this._half;)this.stretch();var h=this._half+e;for(this._heap[h]=i,h=t(h);0!==h;h=t(h))this._heap[h]=this._heap[2*h]+this._heap[2*h+1];e+1>this._maxUsefulLength&&(this._maxUsefulLength=e+1),"function"==typeof this._onUpdateIntervalTree&&this._onUpdateIntervalTree(this._heap),"function"==typeof this._onUpdateItemLayout&&this._onUpdateItemLayout(e,i)}},n.getMaxUsefulLength=function(){return this._maxUsefulLength},n.get=function(t){return this._heap[this._half+t]},n.getSize=function(){return this._size},n.getHalf=function(){return this._half},n.getHeap=function(){return this._heap},n.getMaxValue=function(){return this._heap[1]},n.sumUntil=function(e){if(e<=0)return 0;for(var i=this._half+e-1,h=this._heap[i];1!==i;i=t(i))i%2==1&&(h+=this._heap[i-1]);return h},n.sumTo=function(t){return this.sumUntil(t+1)},n.sum=function(t,e){return this.sumUntil(e)-this.sumUntil(t)},n.greatestLowerBound=function(t){if(t<0)return-1;var e=1;if(this._heap[e]<t)return Math.max(this._maxUsefulLength-1,0);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._maxUsefulLength-1)},n.greatestStrictLowerBound=function(t){if(t<=0)return-1;var e=1;if(this._heap[e]<t)return Math.max(this._maxUsefulLength-1,0);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._maxUsefulLength-1)},n.computeRange=function(t,e){if(this.getHeap()[1]<t)return{startIndex:this._maxUsefulLength-1,endIndex:this._maxUsefulLength-1};var i=this.leastStrictUpperBound(t),h=Math.min(this.leastStrictUpperBound(e),Math.max(this._maxUsefulLength-1,0));return{startIndex:h>=0?Math.max(i,0):-1,endIndex:h}},n.leastUpperBound=function(t){return this.greatestLowerBound(t)+1},n.leastStrictUpperBound=function(t){return this.greatestStrictLowerBound(t)},h}();
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 a(t,e){"number"==typeof t&&this.initWithNumber(t),Array.isArray(t)&&this.initWithArray(t);var i=e||{},a=i.onUpdateItemLayout;this._onUpdateIntervalTree=i.onUpdateIntervalTree,this._onUpdateItemLayout=a}var n=a.prototype;return n.initWithNumber=function(t){this._half=i(t),this._size=this._half,this._heap=e(2*this._half),this._actualSize=0},n.initWithArray=function(t){var a;for(this._half=i(t.length),this._size=this._half,this._heap=e(2*this._half),a=0;a<this._size;++a)this._heap[this._half+a]=t[a];for(a=this._half-1;a>0;--a)this._heap[a]=this._heap[2*a]+this._heap[2*a+1];this._actualSize=t.length},a.uniform=function(t,e){for(var i=[],n=t-1;n>=0;--n)i[n]=e;return new a(i)},a.empty=function(t){return a.uniform(t,0)},n.stretch=function(){for(var t=e(2*this._half*2),i=2*this._half,a=0;a<this._size;a++)t[i+a]=this._heap[this._half+a]||0;for(var n=i-1;n>0;n--)t[n]=t[2*n]+t[2*n+1];this._half=i,this._size=i,this._heap=t},n.isValidIndex=function(t){return"number"==typeof t&&t>=0&&t<this._actualSize},n.reflowHeap=function(e,i){var a=this;void 0===i&&(i=2*this._half-2);var n=Math.log2(this._size);Array.from({length:n},(function(t,e){return e})).reduce((function(e){for(var i=e.endIndex,n=t(e.startIndex),h=t(i),r=n;r<=h;r++)a._heap[r]=a._heap[2*r]+a._heap[2*r+1];return{startIndex:n,endIndex:h}}),{startIndex:e,endIndex:i})},n.remove=function(t){this.batchRemove([t])},n.batchRemove=function(t){var e=this;if(t.sort((function(t,e){return t-e})),t.forEach((function(t){e.isValidIndex(t)&&(isNaN(t)?console.warn("Passing a NaN value as interval tree index"):(e._heap.splice(e._half+t,1),e._heap.push(0),e._actualSize=e._actualSize-1))})),this.reflowHeap(t[0]+this._half),"function"==typeof this._onUpdateIntervalTree&&this._onUpdateIntervalTree(this._heap),"function"==typeof this._onUpdateItemLayout)for(var i=t[0];i<this._half;i++)this._onUpdateItemLayout(i,this.get(i))},n.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),a=this._heap.slice(this._half);a.splice(t,1);for(var n=this._half;n<2*this._half;n++)i[n]=a[n-this._half]||0;for(var h=this._half-1;h>0;h--)i[h]=i[2*h]+i[2*h+1];this._actualSize=this._actualSize-1,this._heap=i}},n.set=function(e,i){if("number"!=typeof e||e<0)return!1;if(isNaN(e))return console.warn("Passing a NaN value as interval tree index"),!1;for(;e>=this._half;)this.stretch();var a=this._half+e;for(this._heap[a]=i,a=t(a);0!==a;a=t(a))this._heap[a]=this._heap[2*a]+this._heap[2*a+1];return e+1>this._actualSize&&(this._actualSize=e+1),"function"==typeof this._onUpdateIntervalTree&&this._onUpdateIntervalTree(this._heap),"function"==typeof this._onUpdateItemLayout&&this._onUpdateItemLayout(e,i),!0},n.getMaxUsefulLength=function(){return this.getActualSize()},n.getActualSize=function(){return this._actualSize},n.get=function(t){return this._heap[this._half+t]},n.getSize=function(){return this._size},n.getHalf=function(){return this._half},n.getHeap=function(){return this._heap},n.getMaxValue=function(){return this._heap[1]},n.sumUntil=function(e){if(e<=0)return 0;for(var i=this._half+e-1,a=this._heap[i];1!==i;i=t(i))i%2==1&&(a+=this._heap[i-1]);return a},n.sumTo=function(t){return this.sumUntil(t+1)},n.sum=function(t,e){return this.sumUntil(e)-this.sumUntil(t)},n.greatestLowerBound=function(t){if(t<0)return-1;var e=1;if(this._heap[e]<t)return Math.max(this._actualSize-1,0);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-1)},n.greatestStrictLowerBound=function(t){if(t<=0)return-1;var e=1;if(this._heap[e]<t)return Math.max(this._actualSize-1,0);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-1)},n.computeRange=function(t,e){return this.getHeap()[1]<t?{startIndex:this._actualSize,endIndex:this._actualSize}:{startIndex:this.greatestLowerBound(t),endIndex:this.leastStrictUpperBound(e)}},n.leastUpperBound=function(t){return this.greatestStrictLowerBound(t)+1},n.leastStrictUpperBound=function(t){return this.greatestLowerBound(t)+1},a}();
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":["// import invariant from 'invariant';\n\nconst 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 _maxUsefulLength: 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._maxUsefulLength = 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 // this._maxUsefulLength = arr.length;\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 }\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 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 remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (typeof index !== 'number' || index >= this._maxUsefulLength) 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 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._maxUsefulLength = this._maxUsefulLength - 1;\n this._heap = nextHeap;\n }\n\n set(index: number, value: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (typeof index !== 'number') return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\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._maxUsefulLength) {\n this._maxUsefulLength = 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 }\n\n getMaxUsefulLength() {\n return this._maxUsefulLength;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n\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 */\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 * Returns the smallest i such that 0 <= i <= size and sumUntil(i) <= t, or\n * -1 if no such i exists.\n */\n greatestLowerBound(t: number) {\n if (t < 0) {\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 Math.max(this._maxUsefulLength - 1, 0);\n }\n\n // 这种写法的结果就是,如果中间一个item的length为0的话,那么它会一直往右边查;\n // 比如初始化的时候是[0, 0, 0, 0, 0, 0, 0, 0];\n // 你会发现node最后会是7,this._half为4;最终即使data没有数据,那么它的index算出来的也是\n // 7 - 4 = 3;所以,考虑到会存在一些item length为0的情况,所以,这个其实是比较合理的方式,\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 return Math.min(node - this._half, this._maxUsefulLength - 1);\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and sumUntil(i) < t, or\n * -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return Math.max(this._maxUsefulLength - 1, 0);\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._maxUsefulLength - 1);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 100, 100, 100, 100].\n * this.leastStrictUpperBound(330) = 3\n * this.leastStrictUpperBound(400) = 3 (should be 4....)\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._maxUsefulLength - 1,\n endIndex: this._maxUsefulLength - 1,\n };\n }\n const startIndex = this.leastStrictUpperBound(minOffset);\n\n // end的话,需要把index + 1,这样才能够把自个也加进去\n const endIndex = Math.min(\n this.leastStrictUpperBound(maxOffset),\n Math.max(this._maxUsefulLength - 1, 0)\n );\n\n return {\n startIndex: endIndex >= 0 ? Math.max(startIndex, 0) : -1,\n endIndex,\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 this.greatestLowerBound(t) + 1;\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 leastStrictUpperBound(t: number) {\n return this.greatestStrictLowerBound(t);\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","_maxUsefulLength","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","remove","index","isNaN","console","warn","copy","slice","splice","set","value","getMaxUsefulLength","get","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","max","leftSum","min","greatestStrictLowerBound","computeRange","minOffset","maxOffset","startIndex","endIndex","leastStrictUpperBound","leastUpperBound"],"mappings":"oEAEA,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,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AAM,EAAI3B,GAC3B2D,GAAKE,EACP7D,GAAO,GAEPA,EAAO,EAAIA,EAAO,EAClB2D,GAAKE,GAIT,OAAO5D,KAAK6D,IAAI9D,EAAOY,KAAKa,MAAOb,KAAKgB,iBAAmB,IAC5DN,EAaD0C,aAAA,SAAaC,EAAmBC,GAC9B,GAAItD,KAAKsC,UAAU,GAAKe,EACtB,MAAO,CACLE,WAAYvD,KAAKgB,iBAAmB,EACpCwC,SAAUxD,KAAKgB,iBAAmB,GAGtC,IAAMuC,EAAavD,KAAKyD,sBAAsBJ,GAGxCG,EAAWnE,KAAK6D,IACpBlD,KAAKyD,sBAAsBH,GAC3BjE,KAAK2D,IAAIhD,KAAKgB,iBAAmB,EAAG,IAGtC,MAAO,CACLuC,WAAYC,GAAY,EAAInE,KAAK2D,IAAIO,EAAY,IAAM,EACvDC,SAAAA,IAEH9C,EAMDgD,gBAAA,SAAgBX,GACd,OAAO/C,KAAK8C,mBAAmBC,GAAK,GACrCrC,EAMD+C,sBAAA,SAAsBV,GACpB,OAAO/C,KAAKmD,yBAAyBJ,IACtCjD"}
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) {\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 Math.max(this._actualSize - 1, 0);\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 - 1);\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) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return Math.max(this._actualSize - 1, 0);\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 - 1);\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 this.greatestStrictLowerBound(t) + 1;\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 this.greatestLowerBound(t) + 1;\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","max","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,cA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DN,EAgBDkE,aAAA,SAAaC,EAAmBC,GAC9B,OAAI9E,KAAK8D,UAAU,GAAKe,EACf,CACLlD,WAAY3B,KAAKgB,YACjBY,SAAU5B,KAAKgB,aAIZ,CAELW,WAAY3B,KAAKsE,mBAAmBO,GAGpCjD,SAAU5B,KAAK+E,sBAAsBD,KAExCpE,EAMDsE,gBAAA,SAAgBT,GACd,OAAOvE,KAAK2E,yBAAyBJ,GAAK,GAC3C7D,EAMDqE,sBAAA,SAAsBR,GACpB,OAAOvE,KAAKsE,mBAAmBC,GAAK,GACrCzE"}
@@ -30,7 +30,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
30
30
  this._half = ceilLog2(length);
31
31
  this._size = this._half;
32
32
  this._heap = createArray(2 * this._half);
33
- this._maxUsefulLength = 0;
33
+ this._actualSize = 0;
34
34
  };
35
35
  _proto.initWithArray = function initWithArray(arr) {
36
36
  this._half = ceilLog2(arr.length);
@@ -43,6 +43,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
43
43
  for (i = this._half - 1; i > 0; --i) {
44
44
  this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];
45
45
  }
46
+ this._actualSize = arr.length;
46
47
  };
47
48
  PrefixIntervalTree.uniform = function uniform(size, initialValue) {
48
49
  var xs = [];
@@ -67,8 +68,66 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
67
68
  this._size = nextHeapHalf;
68
69
  this._heap = nextHeap;
69
70
  };
71
+ _proto.isValidIndex = function isValidIndex(index) {
72
+ return typeof index === 'number' && index >= 0 && index < this._actualSize;
73
+ };
74
+ _proto.reflowHeap = function reflowHeap(startIndex, endIndex) {
75
+ var _this = this;
76
+ if (endIndex === void 0) {
77
+ endIndex = this._half * 2 - 2;
78
+ }
79
+ var len = Math.log2(this._size);
80
+ Array.from({
81
+ length: len
82
+ }, function (v, i) {
83
+ return i;
84
+ }).reduce(function (acc) {
85
+ var startIndex = acc.startIndex,
86
+ endIndex = acc.endIndex;
87
+ var _nextStart = parent(startIndex);
88
+ var _nextEnd = parent(endIndex);
89
+ for (var idx = _nextStart; idx <= _nextEnd; idx++) {
90
+ _this._heap[idx] = _this._heap[2 * idx] + _this._heap[2 * idx + 1];
91
+ }
92
+ return {
93
+ startIndex: _nextStart,
94
+ endIndex: _nextEnd
95
+ };
96
+ }, {
97
+ startIndex: startIndex,
98
+ endIndex: endIndex
99
+ });
100
+ };
70
101
  _proto.remove = function remove(index) {
71
- if (typeof index !== 'number' || index >= this._maxUsefulLength) return;
102
+ this.batchRemove([index]);
103
+ };
104
+ _proto.batchRemove = function batchRemove(indices) {
105
+ var _this2 = this;
106
+ indices.sort(function (a, b) {
107
+ return a - b;
108
+ });
109
+ indices.forEach(function (index) {
110
+ if (!_this2.isValidIndex(index)) return;
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;
118
+ });
119
+ this.reflowHeap(indices[0] + this._half);
120
+ if (typeof this._onUpdateIntervalTree === 'function') {
121
+ this._onUpdateIntervalTree(this._heap);
122
+ }
123
+ if (typeof this._onUpdateItemLayout === 'function') {
124
+ for (var idx = indices[0]; idx < this._half; idx++) {
125
+ this._onUpdateItemLayout(idx, this.get(idx));
126
+ }
127
+ }
128
+ };
129
+ _proto.removeV0 = function removeV0(index) {
130
+ if (!this.isValidIndex(index)) return;
72
131
  if (isNaN(index)) {
73
132
  console.warn('Passing a NaN value as interval tree index');
74
133
  return;
@@ -82,14 +141,14 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
82
141
  for (var _index2 = this._half - 1; _index2 > 0; _index2--) {
83
142
  nextHeap[_index2] = nextHeap[2 * _index2] + nextHeap[2 * _index2 + 1];
84
143
  }
85
- this._maxUsefulLength = this._maxUsefulLength - 1;
144
+ this._actualSize = this._actualSize - 1;
86
145
  this._heap = nextHeap;
87
146
  };
88
147
  _proto.set = function set(index, value) {
89
- if (typeof index !== 'number') return;
148
+ if (typeof index !== 'number' || index < 0) return false;
90
149
  if (isNaN(index)) {
91
150
  console.warn('Passing a NaN value as interval tree index');
92
- return;
151
+ return false;
93
152
  }
94
153
  while (index >= this._half) {
95
154
  this.stretch();
@@ -100,8 +159,8 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
100
159
  for (; node !== 0; node = parent(node)) {
101
160
  this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
102
161
  }
103
- if (index + 1 > this._maxUsefulLength) {
104
- this._maxUsefulLength = index + 1;
162
+ if (index + 1 > this._actualSize) {
163
+ this._actualSize = index + 1;
105
164
  }
106
165
  if (typeof this._onUpdateIntervalTree === 'function') {
107
166
  this._onUpdateIntervalTree(this._heap);
@@ -109,9 +168,13 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
109
168
  if (typeof this._onUpdateItemLayout === 'function') {
110
169
  this._onUpdateItemLayout(index, value);
111
170
  }
171
+ return true;
112
172
  };
113
173
  _proto.getMaxUsefulLength = function getMaxUsefulLength() {
114
- return this._maxUsefulLength;
174
+ return this.getActualSize();
175
+ };
176
+ _proto.getActualSize = function getActualSize() {
177
+ return this._actualSize;
115
178
  };
116
179
  _proto.get = function get(index) {
117
180
  var node = this._half + index;
@@ -154,7 +217,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
154
217
  }
155
218
  var node = 1;
156
219
  if (this._heap[node] < t) {
157
- return Math.max(this._maxUsefulLength - 1, 0);
220
+ return Math.max(this._actualSize - 1, 0);
158
221
  }
159
222
  while (node < this._half) {
160
223
  var leftSum = this._heap[2 * node];
@@ -165,7 +228,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
165
228
  t -= leftSum;
166
229
  }
167
230
  }
168
- return Math.min(node - this._half, this._maxUsefulLength - 1);
231
+ return Math.min(node - this._half, this._actualSize - 1);
169
232
  };
170
233
  _proto.greatestStrictLowerBound = function greatestStrictLowerBound(t) {
171
234
  if (t <= 0) {
@@ -173,7 +236,7 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
173
236
  }
174
237
  var node = 1;
175
238
  if (this._heap[node] < t) {
176
- return Math.max(this._maxUsefulLength - 1, 0);
239
+ return Math.max(this._actualSize - 1, 0);
177
240
  }
178
241
  while (node < this._half) {
179
242
  var leftSum = this._heap[2 * node];
@@ -184,27 +247,25 @@ var PrefixIntervalTree = /*#__PURE__*/function () {
184
247
  t -= leftSum;
185
248
  }
186
249
  }
187
- return Math.min(node - this._half, this._maxUsefulLength - 1);
250
+ return Math.min(node - this._half, this._actualSize - 1);
188
251
  };
189
252
  _proto.computeRange = function computeRange(minOffset, maxOffset) {
190
253
  if (this.getHeap()[1] < minOffset) {
191
254
  return {
192
- startIndex: this._maxUsefulLength - 1,
193
- endIndex: this._maxUsefulLength - 1
255
+ startIndex: this._actualSize,
256
+ endIndex: this._actualSize
194
257
  };
195
258
  }
196
- var startIndex = this.leastStrictUpperBound(minOffset);
197
- var endIndex = Math.min(this.leastStrictUpperBound(maxOffset), Math.max(this._maxUsefulLength - 1, 0));
198
259
  return {
199
- startIndex: endIndex >= 0 ? Math.max(startIndex, 0) : -1,
200
- endIndex: endIndex
260
+ startIndex: this.greatestLowerBound(minOffset),
261
+ endIndex: this.leastStrictUpperBound(maxOffset)
201
262
  };
202
263
  };
203
264
  _proto.leastUpperBound = function leastUpperBound(t) {
204
- return this.greatestLowerBound(t) + 1;
265
+ return this.greatestStrictLowerBound(t) + 1;
205
266
  };
206
267
  _proto.leastStrictUpperBound = function leastStrictUpperBound(t) {
207
- return this.greatestStrictLowerBound(t);
268
+ return this.greatestLowerBound(t) + 1;
208
269
  };
209
270
  return PrefixIntervalTree;
210
271
  }();
@@ -1 +1 @@
1
- {"version":3,"file":"prefix-interval-tree.esm.js","sources":["../src/index.ts"],"sourcesContent":["// import invariant from 'invariant';\n\nconst 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 _maxUsefulLength: 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._maxUsefulLength = 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 // this._maxUsefulLength = arr.length;\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 }\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 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 remove(index: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (typeof index !== 'number' || index >= this._maxUsefulLength) 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 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._maxUsefulLength = this._maxUsefulLength - 1;\n this._heap = nextHeap;\n }\n\n set(index: number, value: number) {\n // if typeof index === 'undefined', then it will go into looooooooop\n if (typeof index !== 'number') return;\n if (isNaN(index)) {\n console.warn('Passing a NaN value as interval tree index');\n return;\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._maxUsefulLength) {\n this._maxUsefulLength = 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 }\n\n getMaxUsefulLength() {\n return this._maxUsefulLength;\n }\n\n get(index: number) {\n // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);\n\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 */\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 * Returns the smallest i such that 0 <= i <= size and sumUntil(i) <= t, or\n * -1 if no such i exists.\n */\n greatestLowerBound(t: number) {\n if (t < 0) {\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 Math.max(this._maxUsefulLength - 1, 0);\n }\n\n // 这种写法的结果就是,如果中间一个item的length为0的话,那么它会一直往右边查;\n // 比如初始化的时候是[0, 0, 0, 0, 0, 0, 0, 0];\n // 你会发现node最后会是7,this._half为4;最终即使data没有数据,那么它的index算出来的也是\n // 7 - 4 = 3;所以,考虑到会存在一些item length为0的情况,所以,这个其实是比较合理的方式,\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 return Math.min(node - this._half, this._maxUsefulLength - 1);\n }\n\n /**\n * Returns the smallest i such that 0 <= i <= size and sumUntil(i) < t, or\n * -1 if no such i exists.\n */\n greatestStrictLowerBound(t: number) {\n if (t <= 0) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return Math.max(this._maxUsefulLength - 1, 0);\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._maxUsefulLength - 1);\n }\n\n /**\n *\n * @param minOffset\n * @param maxOffset\n * @returns\n *\n * pending issue:\n * when item with length list [100, 100, 100, 100, 100].\n * this.leastStrictUpperBound(330) = 3\n * this.leastStrictUpperBound(400) = 3 (should be 4....)\n */\n computeRange(minOffset: number, maxOffset: number) {\n if (this.getHeap()[1] < minOffset) {\n return {\n startIndex: this._maxUsefulLength - 1,\n endIndex: this._maxUsefulLength - 1,\n };\n }\n const startIndex = this.leastStrictUpperBound(minOffset);\n\n // end的话,需要把index + 1,这样才能够把自个也加进去\n const endIndex = Math.min(\n this.leastStrictUpperBound(maxOffset),\n Math.max(this._maxUsefulLength - 1, 0)\n );\n\n return {\n startIndex: endIndex >= 0 ? Math.max(startIndex, 0) : -1,\n endIndex,\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 this.greatestLowerBound(t) + 1;\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 leastStrictUpperBound(t: number) {\n return this.greatestStrictLowerBound(t);\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","_maxUsefulLength","arr","uniform","initialValue","empty","stretch","nextHeap","nextHeapHalf","remove","index","isNaN","console","warn","copy","slice","splice","set","value","getMaxUsefulLength","get","getSize","getHalf","getHeap","getMaxValue","sumUntil","end","sum","sumTo","inclusiveEnd","begin","greatestLowerBound","t","max","leftSum","min","greatestStrictLowerBound","computeRange","minOffset","maxOffset","startIndex","endIndex","leastStrictUpperBound","leastUpperBound"],"mappings":"AAEA,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,IAAAW,IAAA,GAAqDL,IAAI,IAAI,EAAE;MAAvDM,kBAAkB,GAAAD,IAAA,CAAlBC,kBAAkB;MAAEC,oBAAoB,GAAAF,IAAA,CAApBE,oBAAoB;IAChD,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GAAG,CAAC,CAAC;GAC9D;EAAAN,MAAA,CAaD0C,YAAY,GAAZ,SAAAA,aAAaC,SAAiB,EAAEC,SAAiB;IAC/C,IAAI,IAAI,CAAChB,OAAO,EAAE,CAAC,CAAC,CAAC,GAAGe,SAAS,EAAE;MACjC,OAAO;QACLE,UAAU,EAAE,IAAI,CAACvC,gBAAgB,GAAG,CAAC;QACrCwC,QAAQ,EAAE,IAAI,CAACxC,gBAAgB,GAAG;OACnC;;IAEH,IAAMuC,UAAU,GAAG,IAAI,CAACE,qBAAqB,CAACJ,SAAS,CAAC;IAGxD,IAAMG,QAAQ,GAAGlE,IAAI,CAAC4D,GAAG,CACvB,IAAI,CAACO,qBAAqB,CAACH,SAAS,CAAC,EACrChE,IAAI,CAAC0D,GAAG,CAAC,IAAI,CAAChC,gBAAgB,GAAG,CAAC,EAAE,CAAC,CAAC,CACvC;IAED,OAAO;MACLuC,UAAU,EAAEC,QAAQ,IAAI,CAAC,GAAGlE,IAAI,CAAC0D,GAAG,CAACO,UAAU,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;MACxDC,QAAQ,EAARA;KACD;GACF;EAAA9C,MAAA,CAMDgD,eAAe,GAAf,SAAAA,gBAAgBX,CAAS;IACvB,OAAO,IAAI,CAACD,kBAAkB,CAACC,CAAC,CAAC,GAAG,CAAC;GACtC;EAAArC,MAAA,CAMD+C,qBAAqB,GAArB,SAAAA,sBAAsBV,CAAS;IAC7B,OAAO,IAAI,CAACI,wBAAwB,CAACJ,CAAC,CAAC;GACxC;EAAA,OAAAhD,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\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) {\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 Math.max(this._actualSize - 1, 0);\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 - 1);\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) {\n return -1;\n }\n\n let node = 1;\n if (this._heap[node] < t) {\n return Math.max(this._actualSize - 1, 0);\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 - 1);\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 this.greatestStrictLowerBound(t) + 1;\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 this.greatestLowerBound(t) + 1;\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","max","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,CA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AAxE,kBAAA;AAAA;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-oasis/prefix-interval-tree",
3
- "version": "0.1.35",
3
+ "version": "0.2.1",
4
4
  "description": "prefix interval tree function",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -10,6 +10,9 @@
10
10
  "index.ts",
11
11
  "src"
12
12
  ],
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
13
16
  "author": "",
14
17
  "license": "ISC",
15
18
  "devDependencies": {
package/src/index.ts CHANGED
@@ -1,5 +1,3 @@
1
- // import invariant from 'invariant';
2
-
3
1
  const parent = (node: number) => Math.floor(node / 2);
4
2
 
5
3
  const createArray = function (size: number) {
@@ -45,7 +43,7 @@ class PrefixIntervalTree {
45
43
  private _half: number;
46
44
  private _heap: number[];
47
45
 
48
- private _maxUsefulLength: number;
46
+ private _actualSize: number;
49
47
 
50
48
  private _onUpdateItemLayout: Function;
51
49
  private _onUpdateIntervalTree: Function;
@@ -69,14 +67,13 @@ class PrefixIntervalTree {
69
67
  this._half = ceilLog2(length);
70
68
  this._size = this._half;
71
69
  this._heap = createArray(2 * this._half);
72
- this._maxUsefulLength = 0;
70
+ this._actualSize = 0;
73
71
  }
74
72
 
75
73
  initWithArray(arr: number[]) {
76
74
  this._half = ceilLog2(arr.length);
77
75
  this._size = this._half;
78
76
  this._heap = createArray(2 * this._half);
79
- // this._maxUsefulLength = arr.length;
80
77
  let i;
81
78
  for (i = 0; i < this._size; ++i) {
82
79
  this._heap[this._half + i] = arr[i];
@@ -85,6 +82,7 @@ class PrefixIntervalTree {
85
82
  for (i = this._half - 1; i > 0; --i) {
86
83
  this._heap[i] = this._heap[2 * i] + this._heap[2 * i + 1];
87
84
  }
85
+ this._actualSize = arr.length;
88
86
  }
89
87
 
90
88
  static uniform(size: number, initialValue: number) {
@@ -100,6 +98,9 @@ class PrefixIntervalTree {
100
98
  return PrefixIntervalTree.uniform(size, 0);
101
99
  }
102
100
 
101
+ /**
102
+ * the length should be 2
103
+ */
103
104
  stretch() {
104
105
  const nextHeap = createArray(2 * this._half * 2);
105
106
  const nextHeapHalf = this._half * 2;
@@ -119,9 +120,71 @@ class PrefixIntervalTree {
119
120
  this._heap = nextHeap;
120
121
  }
121
122
 
123
+ isValidIndex(index: number) {
124
+ return typeof index === 'number' && index >= 0 && index < this._actualSize;
125
+ }
126
+
127
+ reflowHeap(startIndex: number, endIndex = this._half * 2 - 2) {
128
+ const len = Math.log2(this._size);
129
+
130
+ Array.from({ length: len }, (v, i) => i).reduce(
131
+ (acc) => {
132
+ const { startIndex, endIndex } = acc;
133
+ const _nextStart = parent(startIndex);
134
+ const _nextEnd = parent(endIndex);
135
+
136
+ for (let idx = _nextStart; idx <= _nextEnd; idx++) {
137
+ this._heap[idx] = this._heap[2 * idx] + this._heap[2 * idx + 1];
138
+ }
139
+
140
+ return {
141
+ startIndex: _nextStart,
142
+ endIndex: _nextEnd,
143
+ };
144
+ },
145
+ {
146
+ startIndex,
147
+ endIndex,
148
+ }
149
+ );
150
+ }
151
+
122
152
  remove(index: number) {
123
153
  // if typeof index === 'undefined', then it will go into looooooooop
124
- if (typeof index !== 'number' || index >= this._maxUsefulLength) return;
154
+
155
+ this.batchRemove([index]);
156
+ }
157
+
158
+ batchRemove(indices: number[]) {
159
+ indices.sort((a, b) => a - b);
160
+
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
+ }
167
+
168
+ this._heap.splice(this._half + index, 1);
169
+ this._heap.push(0);
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
+ }
177
+
178
+ if (typeof this._onUpdateItemLayout === 'function') {
179
+ for (let idx = indices[0]; idx < this._half; idx++) {
180
+ this._onUpdateItemLayout(idx, this.get(idx));
181
+ }
182
+ }
183
+ }
184
+
185
+ removeV0(index: number) {
186
+ // if typeof index === 'undefined', then it will go into looooooooop
187
+ if (!this.isValidIndex(index)) return;
125
188
  if (isNaN(index)) {
126
189
  console.warn('Passing a NaN value as interval tree index');
127
190
  return;
@@ -130,6 +193,7 @@ class PrefixIntervalTree {
130
193
  const nextHeap = createArray(this._half * 2);
131
194
  const copy = this._heap.slice(this._half);
132
195
  copy.splice(index, 1);
196
+
133
197
  for (let index = this._half; index < this._half * 2; index++) {
134
198
  nextHeap[index] = copy[index - this._half] || 0;
135
199
  }
@@ -138,16 +202,15 @@ class PrefixIntervalTree {
138
202
  nextHeap[index] = nextHeap[2 * index] + nextHeap[2 * index + 1];
139
203
  }
140
204
 
141
- this._maxUsefulLength = this._maxUsefulLength - 1;
205
+ this._actualSize = this._actualSize - 1;
142
206
  this._heap = nextHeap;
143
207
  }
144
208
 
145
209
  set(index: number, value: number) {
146
- // if typeof index === 'undefined', then it will go into looooooooop
147
- if (typeof index !== 'number') return;
210
+ if (typeof index !== 'number' || index < 0) return false;
148
211
  if (isNaN(index)) {
149
212
  console.warn('Passing a NaN value as interval tree index');
150
- return;
213
+ return false;
151
214
  }
152
215
 
153
216
  while (index >= this._half) {
@@ -162,8 +225,8 @@ class PrefixIntervalTree {
162
225
  this._heap[node] = this._heap[2 * node] + this._heap[2 * node + 1];
163
226
  }
164
227
 
165
- if (index + 1 > this._maxUsefulLength) {
166
- this._maxUsefulLength = index + 1;
228
+ if (index + 1 > this._actualSize) {
229
+ this._actualSize = index + 1;
167
230
  }
168
231
 
169
232
  if (typeof this._onUpdateIntervalTree === 'function') {
@@ -173,15 +236,19 @@ class PrefixIntervalTree {
173
236
  if (typeof this._onUpdateItemLayout === 'function') {
174
237
  this._onUpdateItemLayout(index, value);
175
238
  }
239
+ return true;
176
240
  }
177
241
 
178
242
  getMaxUsefulLength() {
179
- return this._maxUsefulLength;
243
+ return this.getActualSize();
244
+ }
245
+
246
+ getActualSize() {
247
+ return this._actualSize;
180
248
  }
181
249
 
182
250
  get(index: number) {
183
251
  // invariant(index >= 0 && index < this._size, 'Index out of range %s', index);
184
-
185
252
  const node = this._half + index;
186
253
  return this._heap[node];
187
254
  }
@@ -239,6 +306,7 @@ class PrefixIntervalTree {
239
306
 
240
307
  /**
241
308
  * Returns the sum get(begin) + get(begin + 1) + ... + get(end - 1).
309
+ * end length is not included
242
310
  */
243
311
  sum(begin: number, end: number) {
244
312
  // invariant(begin <= end, 'Begin must precede end');
@@ -246,8 +314,8 @@ class PrefixIntervalTree {
246
314
  }
247
315
 
248
316
  /**
249
- * Returns the smallest i such that 0 <= i <= size and sumUntil(i) <= t, or
250
- * -1 if no such i exists.
317
+ * return the biggest i, sumUntil(i) === t
318
+ * return the biggest i, subUntil(i) < t
251
319
  */
252
320
  greatestLowerBound(t: number) {
253
321
  if (t < 0) {
@@ -257,14 +325,9 @@ class PrefixIntervalTree {
257
325
  let node = 1;
258
326
  if (this._heap[node] < t) {
259
327
  // not use this._size;this._size always be a big value
260
- return Math.max(this._maxUsefulLength - 1, 0);
328
+ return Math.max(this._actualSize - 1, 0);
261
329
  }
262
330
 
263
- // 这种写法的结果就是,如果中间一个item的length为0的话,那么它会一直往右边查;
264
- // 比如初始化的时候是[0, 0, 0, 0, 0, 0, 0, 0];
265
- // 你会发现node最后会是7,this._half为4;最终即使data没有数据,那么它的index算出来的也是
266
- // 7 - 4 = 3;所以,考虑到会存在一些item length为0的情况,所以,这个其实是比较合理的方式,
267
- // 拿右边的
268
331
  while (node < this._half) {
269
332
  const leftSum = this._heap[2 * node];
270
333
  if (t < leftSum) {
@@ -274,12 +337,13 @@ class PrefixIntervalTree {
274
337
  t -= leftSum;
275
338
  }
276
339
  }
277
- return Math.min(node - this._half, this._maxUsefulLength - 1);
340
+
341
+ return Math.min(node - this._half, this._actualSize - 1);
278
342
  }
279
343
 
280
344
  /**
281
- * Returns the smallest i such that 0 <= i <= size and sumUntil(i) < t, or
282
- * -1 if no such i exists.
345
+ * Return the biggest i, subUntil(i) < t
346
+ * or -1 if no such i exists.
283
347
  */
284
348
  greatestStrictLowerBound(t: number) {
285
349
  if (t <= 0) {
@@ -288,7 +352,7 @@ class PrefixIntervalTree {
288
352
 
289
353
  let node = 1;
290
354
  if (this._heap[node] < t) {
291
- return Math.max(this._maxUsefulLength - 1, 0);
355
+ return Math.max(this._actualSize - 1, 0);
292
356
  }
293
357
 
294
358
  while (node < this._half) {
@@ -301,7 +365,7 @@ class PrefixIntervalTree {
301
365
  }
302
366
  }
303
367
 
304
- return Math.min(node - this._half, this._maxUsefulLength - 1);
368
+ return Math.min(node - this._half, this._actualSize - 1);
305
369
  }
306
370
 
307
371
  /**
@@ -311,28 +375,27 @@ class PrefixIntervalTree {
311
375
  * @returns
312
376
  *
313
377
  * pending issue:
314
- * when item with length list [100, 100, 100, 100, 100].
315
- * this.leastStrictUpperBound(330) = 3
316
- * this.leastStrictUpperBound(400) = 3 (should be 4....)
378
+ * when item with length list [100, 0, 100, 0, 0, 100].
379
+ * then computeRange(100, 200) => { startIndex: 2, endIndex: 6 }
380
+ *
381
+ * item index in viewport will be [2, 3, 4, 5], index 6 is not
382
+ * included just like Array.slice(start, end)
383
+ *
317
384
  */
318
385
  computeRange(minOffset: number, maxOffset: number) {
319
386
  if (this.getHeap()[1] < minOffset) {
320
387
  return {
321
- startIndex: this._maxUsefulLength - 1,
322
- endIndex: this._maxUsefulLength - 1,
388
+ startIndex: this._actualSize,
389
+ endIndex: this._actualSize,
323
390
  };
324
391
  }
325
- const startIndex = this.leastStrictUpperBound(minOffset);
326
-
327
- // end的话,需要把index + 1,这样才能够把自个也加进去
328
- const endIndex = Math.min(
329
- this.leastStrictUpperBound(maxOffset),
330
- Math.max(this._maxUsefulLength - 1, 0)
331
- );
332
392
 
333
393
  return {
334
- startIndex: endIndex >= 0 ? Math.max(startIndex, 0) : -1,
335
- endIndex,
394
+ // the biggest item, value <= minOffset
395
+ startIndex: this.greatestLowerBound(minOffset),
396
+
397
+ // the smallest item, value > maxOffset
398
+ endIndex: this.leastStrictUpperBound(maxOffset),
336
399
  };
337
400
  }
338
401
 
@@ -341,15 +404,15 @@ class PrefixIntervalTree {
341
404
  * size + 1 if no such i exists.
342
405
  */
343
406
  leastUpperBound(t: number) {
344
- return this.greatestLowerBound(t) + 1;
407
+ return this.greatestStrictLowerBound(t) + 1;
345
408
  }
346
409
 
347
410
  /**
348
- * Returns the smallest i such that 0 <= i <= size and t < sumUntil(i), or
411
+ * Returns the smallest i, t < sumUntil(i), it should be used as range end
349
412
  * size + 1 if no such i exists.
350
413
  */
351
414
  leastStrictUpperBound(t: number) {
352
- return this.greatestStrictLowerBound(t);
415
+ return this.greatestLowerBound(t) + 1;
353
416
  }
354
417
  }
355
418