data-structure-typed 2.4.4 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -1
- package/README.md +34 -1
- package/dist/cjs/index.cjs +10639 -2151
- package/dist/cjs-legacy/index.cjs +10694 -2195
- package/dist/esm/index.mjs +10639 -2150
- package/dist/esm-legacy/index.mjs +10694 -2194
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/data-structure-typed.js +10725 -2221
- package/dist/umd/data-structure-typed.min.js +4 -2
- package/package.json +5 -4
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
- package/src/data-structures/binary-tree/binary-tree.ts +567 -121
- package/src/data-structures/binary-tree/bst.ts +370 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1411 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
- package/src/data-structures/binary-tree/tree-set.ts +1257 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +233 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +308 -59
- package/src/data-structures/hash/hash-map.ts +254 -79
- package/src/data-structures/heap/heap.ts +305 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +433 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +358 -68
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +227 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -5,327 +5,397 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { ERR } from '../../common';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
+
* Binary Indexed Tree (Fenwick Tree).
|
|
11
12
|
*
|
|
13
|
+
* Efficient prefix sums and point updates in O(log n).
|
|
14
|
+
* Standard array-based implementation per C++ competitive programming conventions.
|
|
15
|
+
*
|
|
16
|
+
* All indices are 0-based externally; internally converted to 1-based for BIT arithmetic.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const bit = new BinaryIndexedTree(6);
|
|
21
|
+
* bit.update(0, 3); // index 0 += 3
|
|
22
|
+
* bit.update(1, 2); // index 1 += 2
|
|
23
|
+
* bit.update(2, 7); // index 2 += 7
|
|
24
|
+
*
|
|
25
|
+
* bit.query(2); // prefix sum [0..2] = 12
|
|
26
|
+
* bit.queryRange(1, 2); // range sum [1..2] = 9
|
|
27
|
+
* bit.get(1); // point value at index 1 = 2
|
|
28
|
+
* ```
|
|
12
29
|
*/
|
|
13
|
-
export class BinaryIndexedTree {
|
|
14
|
-
protected readonly
|
|
15
|
-
protected
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The constructor initializes the properties of an object, including default frequency, maximum
|
|
19
|
-
* value, a freqMap data structure, the most significant bit, and the count of negative frequencies.
|
|
20
|
-
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
21
|
-
* value of 0.
|
|
22
|
-
*/
|
|
23
|
-
constructor({ frequency = 0, max }: { frequency?: number; max: number }) {
|
|
24
|
-
this._freq = frequency;
|
|
25
|
-
this._max = max;
|
|
26
|
-
this._freqMap = { 0: 0 };
|
|
27
|
-
this._msb = getMSB(max);
|
|
28
|
-
this._negativeCount = frequency < 0 ? max : 0;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
protected _freqMap: Record<number, number>;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The function returns the frequency map of numbers.
|
|
35
|
-
* @returns The `_freqMap` property, which is a record with number keys and number values, is being
|
|
36
|
-
* returned.
|
|
37
|
-
*/
|
|
38
|
-
get freqMap(): Record<number, number> {
|
|
39
|
-
return this._freqMap;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
protected _msb: number;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The function returns the value of the _msb property.
|
|
46
|
-
* @returns The `_msb` property of the object.
|
|
47
|
-
*/
|
|
48
|
-
get msb(): number {
|
|
49
|
-
return this._msb;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
protected _negativeCount: number;
|
|
30
|
+
export class BinaryIndexedTree implements Iterable<number> {
|
|
31
|
+
protected readonly _size: number;
|
|
32
|
+
protected _tree: number[]; // 1-indexed BIT array
|
|
53
33
|
|
|
54
34
|
/**
|
|
55
|
-
*
|
|
56
|
-
* @
|
|
57
|
-
* `number`.
|
|
35
|
+
* Construct a BIT of given size (all zeros), or from an initial values array.
|
|
36
|
+
* @param sizeOrElements - number of elements, or an array of initial values
|
|
58
37
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
38
|
+
constructor(sizeOrElements: number | number[]) {
|
|
39
|
+
if (Array.isArray(sizeOrElements)) {
|
|
40
|
+
this._size = sizeOrElements.length;
|
|
41
|
+
this._tree = new Array(this._size + 1).fill(0);
|
|
42
|
+
for (let i = 0; i < this._size; i++) {
|
|
43
|
+
this._pointUpdate(i + 1, sizeOrElements[i]);
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
if (!Number.isInteger(sizeOrElements) || sizeOrElements < 0) {
|
|
47
|
+
throw new RangeError(ERR.invalidArgument('size must be a non-negative integer', 'BinaryIndexedTree'));
|
|
48
|
+
}
|
|
49
|
+
this._size = sizeOrElements;
|
|
50
|
+
this._tree = new Array(this._size + 1).fill(0);
|
|
51
|
+
}
|
|
61
52
|
}
|
|
62
53
|
|
|
63
|
-
|
|
64
|
-
* The above function returns the value of the protected variable `_freq`.
|
|
65
|
-
* @returns The frequency value stored in the protected variable `_freq`.
|
|
66
|
-
*/
|
|
67
|
-
get freq(): number {
|
|
68
|
-
return this._freq;
|
|
69
|
-
}
|
|
54
|
+
// ─── Core operations ──────────────────────────────────────────
|
|
70
55
|
|
|
71
56
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
57
|
+
* Point update: add delta to the value at index (0-based).
|
|
58
|
+
* Time: O(log n)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
* @example
|
|
76
|
+
* // Add delta at index
|
|
77
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
|
|
78
|
+
* bit.update(2, 7);
|
|
79
|
+
* console.log(bit.get(2)); // 10;
|
|
74
80
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
update(index: number, delta: number): void {
|
|
82
|
+
this._checkIndex(index);
|
|
83
|
+
this._pointUpdate(index + 1, delta);
|
|
77
84
|
}
|
|
78
85
|
|
|
79
86
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
* Point set: set the value at index to an absolute value (0-based).
|
|
88
|
+
* Time: O(log n)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
* @example
|
|
107
|
+
* // Set value at index
|
|
108
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
109
|
+
* bit.set(1, 10);
|
|
110
|
+
* console.log(bit.get(1)); // 10;
|
|
84
111
|
*/
|
|
85
|
-
|
|
112
|
+
set(index: number, value: number): void {
|
|
86
113
|
this._checkIndex(index);
|
|
87
|
-
|
|
114
|
+
const current = this.get(index);
|
|
115
|
+
this._pointUpdate(index + 1, value - current);
|
|
88
116
|
}
|
|
89
117
|
|
|
90
118
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
119
|
+
* Get the point value at index (0-based).
|
|
120
|
+
* Time: O(log n)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
* @example
|
|
138
|
+
* // Get value at index
|
|
139
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
140
|
+
* console.log(bit.get(0)); // 1;
|
|
141
|
+
* console.log(bit.get(2)); // 3;
|
|
97
142
|
*/
|
|
98
|
-
|
|
99
|
-
this._checkIndex(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this._update(position, change);
|
|
103
|
-
this._updateNegativeCount(freqCur, freqCur + change);
|
|
143
|
+
get(index: number): number {
|
|
144
|
+
this._checkIndex(index);
|
|
145
|
+
return this._pointQuery(index + 1);
|
|
104
146
|
}
|
|
105
147
|
|
|
106
148
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
149
|
+
* Prefix sum query: returns sum of elements [0..index] (inclusive, 0-based).
|
|
150
|
+
* Time: O(log n)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
* @example
|
|
169
|
+
* // Prefix sum
|
|
170
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
171
|
+
* console.log(bit.query(2)); // 6;
|
|
112
172
|
*/
|
|
113
|
-
|
|
173
|
+
query(index: number): number {
|
|
114
174
|
this._checkIndex(index);
|
|
115
|
-
this.
|
|
175
|
+
return this._prefixSum(index + 1);
|
|
116
176
|
}
|
|
117
177
|
|
|
118
178
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
179
|
+
* Range sum query: returns sum of elements [start..end] (inclusive, 0-based).
|
|
180
|
+
* Time: O(log n)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
* @example
|
|
198
|
+
* // Range sum
|
|
199
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
200
|
+
* console.log(bit.queryRange(1, 2)); // 5;
|
|
124
201
|
*/
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
return this.
|
|
202
|
+
queryRange(start: number, end: number): number {
|
|
203
|
+
this._checkIndex(start);
|
|
204
|
+
this._checkIndex(end);
|
|
205
|
+
if (start > end) return 0;
|
|
206
|
+
if (start === 0) return this._prefixSum(end + 1);
|
|
207
|
+
return this._prefixSum(end + 1) - this._prefixSum(start);
|
|
130
208
|
}
|
|
131
209
|
|
|
210
|
+
// ─── Binary search ───────────────────────────────────────────
|
|
211
|
+
|
|
132
212
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
213
|
+
* Find the smallest index i such that prefix sum [0..i] >= sum.
|
|
214
|
+
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
215
|
+
* Returns size if no such index exists.
|
|
216
|
+
* Time: O(log n)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
* @example
|
|
235
|
+
* // Find index with prefix sum ≥ target
|
|
236
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
237
|
+
* const idx = bit.lowerBound(4);
|
|
238
|
+
* console.log(idx); // >= 0;
|
|
137
239
|
*/
|
|
138
240
|
lowerBound(sum: number): number {
|
|
139
|
-
|
|
140
|
-
|
|
241
|
+
let pos = 0;
|
|
242
|
+
let bitMask = this._highBit(this._size);
|
|
243
|
+
|
|
244
|
+
while (bitMask > 0) {
|
|
245
|
+
const next = pos + bitMask;
|
|
246
|
+
if (next <= this._size && this._tree[next] < sum) {
|
|
247
|
+
sum -= this._tree[next];
|
|
248
|
+
pos = next;
|
|
249
|
+
}
|
|
250
|
+
bitMask >>= 1;
|
|
141
251
|
}
|
|
142
|
-
return this._binarySearch(sum, (x, y) => x < y);
|
|
143
|
-
}
|
|
144
252
|
|
|
145
|
-
|
|
146
|
-
* The upperBound function returns the index of the first element in a sequence that is greater than
|
|
147
|
-
* or equal to a given sum.
|
|
148
|
-
* @param {number} sum - The "sum" parameter is a number that represents the target sum that we want
|
|
149
|
-
* to find in the sequence.
|
|
150
|
-
* @returns The upperBound function is returning a number.
|
|
151
|
-
*/
|
|
152
|
-
upperBound(sum: number): number {
|
|
153
|
-
if (this.negativeCount > 0) {
|
|
154
|
-
throw new Error('Must not be descending');
|
|
155
|
-
}
|
|
156
|
-
return this._binarySearch(sum, (x, y) => x <= y);
|
|
253
|
+
return pos; // 0-based
|
|
157
254
|
}
|
|
158
255
|
|
|
159
256
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
|
|
257
|
+
* Find the smallest index i such that prefix sum [0..i] > sum.
|
|
258
|
+
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
259
|
+
* Returns size if no such index exists.
|
|
260
|
+
* Time: O(log n)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
* @example
|
|
265
|
+
* // Find index with prefix sum > target
|
|
266
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
267
|
+
* const idx = bit.upperBound(4);
|
|
268
|
+
* console.log(idx); // >= 0;
|
|
165
269
|
*/
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
270
|
+
upperBound(sum: number): number {
|
|
271
|
+
let pos = 0;
|
|
272
|
+
let bitMask = this._highBit(this._size);
|
|
273
|
+
|
|
274
|
+
while (bitMask > 0) {
|
|
275
|
+
const next = pos + bitMask;
|
|
276
|
+
if (next <= this._size && this._tree[next] <= sum) {
|
|
277
|
+
sum -= this._tree[next];
|
|
278
|
+
pos = next;
|
|
279
|
+
}
|
|
280
|
+
bitMask >>= 1;
|
|
174
281
|
}
|
|
175
282
|
|
|
176
|
-
return
|
|
283
|
+
return pos; // 0-based
|
|
177
284
|
}
|
|
178
285
|
|
|
179
|
-
|
|
180
|
-
* The function returns the value of a specific index in a freqMap data structure, or a default value if
|
|
181
|
-
* the index is not found.
|
|
182
|
-
* @param {number} index - The `index` parameter is a number that represents the index of a node in a
|
|
183
|
-
* freqMap data structure.
|
|
184
|
-
* @returns a number.
|
|
185
|
-
*/
|
|
186
|
-
protected _getFrequency(index: number): number {
|
|
187
|
-
if (index in this.freqMap) {
|
|
188
|
-
return this.freqMap[index];
|
|
189
|
-
}
|
|
286
|
+
// ─── Standard interface ──────────────────────────────────────
|
|
190
287
|
|
|
191
|
-
|
|
288
|
+
get size(): number {
|
|
289
|
+
return this._size;
|
|
192
290
|
}
|
|
193
291
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
* @param {number} index - The index parameter is a number that represents the index of the freqMap
|
|
197
|
-
* element that needs to be updated.
|
|
198
|
-
* @param {number} delta - The `delta` parameter represents the change in value that needs to be
|
|
199
|
-
* added to the freqMap at the specified `index`.
|
|
200
|
-
*/
|
|
201
|
-
protected _updateFrequency(index: number, delta: number): void {
|
|
202
|
-
this.freqMap[index] = this._getFrequency(index) + delta;
|
|
292
|
+
isEmpty(): boolean {
|
|
293
|
+
return this._size === 0;
|
|
203
294
|
}
|
|
204
295
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
* @param {number} index - The parameter "index" is of type number and represents the index value
|
|
208
|
-
* that needs to be checked.
|
|
209
|
-
*/
|
|
210
|
-
protected _checkIndex(index: number): void {
|
|
211
|
-
if (!Number.isInteger(index)) {
|
|
212
|
-
throw new Error('Invalid index: Index must be an integer.');
|
|
213
|
-
}
|
|
214
|
-
if (index < 0 || index >= this.max) {
|
|
215
|
-
throw new Error('Index out of range: Index must be within the range [0, this.max).');
|
|
216
|
-
}
|
|
296
|
+
clear(): void {
|
|
297
|
+
this._tree.fill(0);
|
|
217
298
|
}
|
|
218
299
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
* freqMap.
|
|
222
|
-
* @param {number} index - The `index` parameter is a number that represents the index of an element in a
|
|
223
|
-
* data structure.
|
|
224
|
-
* @returns a number.
|
|
225
|
-
*/
|
|
226
|
-
protected _readSingle(index: number): number {
|
|
227
|
-
index = index + 1;
|
|
228
|
-
let sum = this._getFrequency(index);
|
|
229
|
-
const z = index - (index & -index);
|
|
230
|
-
|
|
231
|
-
index--;
|
|
232
|
-
|
|
233
|
-
while (index !== z) {
|
|
234
|
-
sum -= this._getFrequency(index);
|
|
235
|
-
index -= index & -index;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return sum;
|
|
300
|
+
clone(): BinaryIndexedTree {
|
|
301
|
+
return new BinaryIndexedTree(this.toArray());
|
|
239
302
|
}
|
|
240
303
|
|
|
241
304
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
|
|
305
|
+
* Returns the point values as a plain array.
|
|
306
|
+
* Time: O(n log n)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
* @example
|
|
311
|
+
* // Convert to array
|
|
312
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
313
|
+
* console.log(bit.toArray()); // [1, 2, 3];
|
|
245
314
|
*/
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
this._negativeCount++;
|
|
315
|
+
toArray(): number[] {
|
|
316
|
+
const result: number[] = [];
|
|
317
|
+
for (let i = 0; i < this._size; i++) {
|
|
318
|
+
result.push(this._pointQuery(i + 1));
|
|
251
319
|
}
|
|
320
|
+
return result;
|
|
252
321
|
}
|
|
253
322
|
|
|
254
323
|
/**
|
|
255
|
-
*
|
|
256
|
-
* propagating the changes to its parent nodes.
|
|
257
|
-
* @param {number} index - The `index` parameter is a number that represents the index of the element in
|
|
258
|
-
* the data structure that needs to be updated.
|
|
259
|
-
* @param {number} delta - The `delta` parameter represents the change in value that needs to be
|
|
260
|
-
* applied to the elements in the data structure.
|
|
324
|
+
* Iterate over point values in index order.
|
|
261
325
|
*/
|
|
262
|
-
|
|
263
|
-
|
|
326
|
+
[Symbol.iterator](): IterableIterator<number> {
|
|
327
|
+
const size = this._size;
|
|
328
|
+
let i = 0;
|
|
329
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
330
|
+
const self = this;
|
|
331
|
+
return {
|
|
332
|
+
[Symbol.iterator]() {
|
|
333
|
+
return this;
|
|
334
|
+
},
|
|
335
|
+
next(): IteratorResult<number> {
|
|
336
|
+
if (i < size) {
|
|
337
|
+
return { value: self._pointQuery(i++ + 1), done: false };
|
|
338
|
+
}
|
|
339
|
+
return { value: undefined as any, done: true };
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
264
343
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
344
|
+
forEach(callback: (value: number, index: number) => void): void {
|
|
345
|
+
for (let i = 0; i < this._size; i++) {
|
|
346
|
+
callback(this._pointQuery(i + 1), i);
|
|
268
347
|
}
|
|
269
348
|
}
|
|
270
349
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
* the frequency has changed.
|
|
274
|
-
* @param {number} index - The `index` parameter is a number that represents the index of the element
|
|
275
|
-
* being modified or accessed.
|
|
276
|
-
* @param {number} freq - The `freq` parameter represents the new frequency value that needs to be
|
|
277
|
-
* written to the specified index `index`.
|
|
278
|
-
*/
|
|
279
|
-
protected _writeSingle(index: number, freq: number): void {
|
|
280
|
-
const freqCur = this._readSingle(index);
|
|
281
|
-
|
|
282
|
-
this._update(index, freq - freqCur);
|
|
283
|
-
this._updateNegativeCount(freqCur, freq);
|
|
350
|
+
print(): void {
|
|
351
|
+
console.log(this.toArray());
|
|
284
352
|
}
|
|
285
353
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
* @returns the sum of the values obtained from calling the `_getFrequency` method for each index in the
|
|
291
|
-
* range from `count` to 1.
|
|
292
|
-
*/
|
|
293
|
-
protected _read(count: number): number {
|
|
294
|
-
let index = count;
|
|
354
|
+
// ─── Internal helpers ─────────────────────────────────────────
|
|
355
|
+
|
|
356
|
+
/** 1-based prefix sum up to pos (inclusive). */
|
|
357
|
+
protected _prefixSum(pos: number): number {
|
|
295
358
|
let sum = 0;
|
|
296
|
-
while (
|
|
297
|
-
sum += this.
|
|
298
|
-
|
|
359
|
+
while (pos > 0) {
|
|
360
|
+
sum += this._tree[pos];
|
|
361
|
+
pos -= pos & -pos;
|
|
299
362
|
}
|
|
300
|
-
|
|
301
363
|
return sum;
|
|
302
364
|
}
|
|
303
365
|
|
|
304
|
-
/**
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
* @returns the value of the variable "left".
|
|
312
|
-
*/
|
|
313
|
-
protected _binarySearch(sum: number, before: (x: number, y: number) => boolean): number {
|
|
314
|
-
let left = 0;
|
|
315
|
-
let right = this.msb << 1;
|
|
316
|
-
let sumT = sum;
|
|
366
|
+
/** 1-based point update: add delta to position pos. */
|
|
367
|
+
protected _pointUpdate(pos: number, delta: number): void {
|
|
368
|
+
while (pos <= this._size) {
|
|
369
|
+
this._tree[pos] += delta;
|
|
370
|
+
pos += pos & -pos;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
317
373
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
374
|
+
/** 1-based point query: get exact value at pos. */
|
|
375
|
+
protected _pointQuery(pos: number): number {
|
|
376
|
+
let val = this._tree[pos];
|
|
377
|
+
const lca = pos - (pos & -pos); // parent in prefix-sum sense
|
|
378
|
+
pos--;
|
|
379
|
+
while (pos > lca) {
|
|
380
|
+
val -= this._tree[pos];
|
|
381
|
+
pos -= pos & -pos;
|
|
382
|
+
}
|
|
383
|
+
return val;
|
|
384
|
+
}
|
|
321
385
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
386
|
+
protected _checkIndex(index: number): void {
|
|
387
|
+
if (!Number.isInteger(index)) {
|
|
388
|
+
throw new TypeError(ERR.invalidIndex('BinaryIndexedTree'));
|
|
389
|
+
}
|
|
390
|
+
if (index < 0 || index >= this._size) {
|
|
391
|
+
throw new RangeError(ERR.indexOutOfRange(index, 0, this._size - 1, 'BinaryIndexedTree'));
|
|
328
392
|
}
|
|
329
|
-
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Returns highest power of 2 <= n. */
|
|
396
|
+
protected _highBit(n: number): number {
|
|
397
|
+
let bit = 1;
|
|
398
|
+
while (bit <= n) bit <<= 1;
|
|
399
|
+
return bit >> 1;
|
|
330
400
|
}
|
|
331
401
|
}
|