max-priority-queue-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/README.md +63 -0
- package/dist/cjs/index.cjs +403 -98
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +402 -97
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +403 -99
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +402 -98
- package/dist/esm-legacy/index.mjs.map +1 -1
- 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/max-priority-queue-typed.js +400 -95
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- 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 +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -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 +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +425 -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 +343 -68
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +215 -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
|
@@ -1,174 +1,220 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Binary Indexed Tree (Fenwick Tree).
|
|
2
3
|
*
|
|
4
|
+
* Efficient prefix sums and point updates in O(log n).
|
|
5
|
+
* Standard array-based implementation per C++ competitive programming conventions.
|
|
6
|
+
*
|
|
7
|
+
* All indices are 0-based externally; internally converted to 1-based for BIT arithmetic.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const bit = new BinaryIndexedTree(6);
|
|
12
|
+
* bit.update(0, 3); // index 0 += 3
|
|
13
|
+
* bit.update(1, 2); // index 1 += 2
|
|
14
|
+
* bit.update(2, 7); // index 2 += 7
|
|
15
|
+
*
|
|
16
|
+
* bit.query(2); // prefix sum [0..2] = 12
|
|
17
|
+
* bit.queryRange(1, 2); // range sum [1..2] = 9
|
|
18
|
+
* bit.get(1); // point value at index 1 = 2
|
|
19
|
+
* ```
|
|
3
20
|
*/
|
|
4
|
-
export declare class BinaryIndexedTree {
|
|
5
|
-
protected readonly
|
|
6
|
-
protected
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
21
|
+
export declare class BinaryIndexedTree implements Iterable<number> {
|
|
22
|
+
protected readonly _size: number;
|
|
23
|
+
protected _tree: number[];
|
|
24
|
+
/**
|
|
25
|
+
* Construct a BIT of given size (all zeros), or from an initial values array.
|
|
26
|
+
* @param sizeOrElements - number of elements, or an array of initial values
|
|
27
|
+
*/
|
|
28
|
+
constructor(sizeOrElements: number | number[]);
|
|
29
|
+
/**
|
|
30
|
+
* Point update: add delta to the value at index (0-based).
|
|
31
|
+
* Time: O(log n)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
* @example
|
|
47
|
+
* // Add delta at index
|
|
48
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
|
|
49
|
+
* bit.update(2, 7);
|
|
50
|
+
* console.log(bit.get(2)); // 10;
|
|
51
|
+
*/
|
|
52
|
+
update(index: number, delta: number): void;
|
|
53
|
+
/**
|
|
54
|
+
* Point set: set the value at index to an absolute value (0-based).
|
|
55
|
+
* Time: O(log n)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
* @example
|
|
72
|
+
* // Set value at index
|
|
73
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
74
|
+
* bit.set(1, 10);
|
|
75
|
+
* console.log(bit.get(1)); // 10;
|
|
76
|
+
*/
|
|
77
|
+
set(index: number, value: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Get the point value at index (0-based).
|
|
80
|
+
* Time: O(log n)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
* @example
|
|
96
|
+
* // Get value at index
|
|
97
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
98
|
+
* console.log(bit.get(0)); // 1;
|
|
99
|
+
* console.log(bit.get(2)); // 3;
|
|
100
|
+
*/
|
|
101
|
+
get(index: number): number;
|
|
102
|
+
/**
|
|
103
|
+
* Prefix sum query: returns sum of elements [0..index] (inclusive, 0-based).
|
|
104
|
+
* Time: O(log n)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
* @example
|
|
121
|
+
* // Prefix sum
|
|
122
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
123
|
+
* console.log(bit.query(2)); // 6;
|
|
124
|
+
*/
|
|
125
|
+
query(index: number): number;
|
|
126
|
+
/**
|
|
127
|
+
* Range sum query: returns sum of elements [start..end] (inclusive, 0-based).
|
|
128
|
+
* Time: O(log n)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
* @example
|
|
144
|
+
* // Range sum
|
|
145
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
146
|
+
* console.log(bit.queryRange(1, 2)); // 5;
|
|
147
|
+
*/
|
|
148
|
+
queryRange(start: number, end: number): number;
|
|
149
|
+
/**
|
|
150
|
+
* Find the smallest index i such that prefix sum [0..i] >= sum.
|
|
151
|
+
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
152
|
+
* Returns size if no such index exists.
|
|
153
|
+
* Time: O(log n)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
* @example
|
|
170
|
+
* // Find index with prefix sum ≥ target
|
|
171
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
172
|
+
* const idx = bit.lowerBound(4);
|
|
173
|
+
* console.log(idx); // >= 0;
|
|
84
174
|
*/
|
|
85
175
|
lowerBound(sum: number): number;
|
|
86
176
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
|
|
177
|
+
* Find the smallest index i such that prefix sum [0..i] > sum.
|
|
178
|
+
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
179
|
+
* Returns size if no such index exists.
|
|
180
|
+
* Time: O(log n)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
* @example
|
|
184
|
+
* // Find index with prefix sum > target
|
|
185
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
186
|
+
* const idx = bit.upperBound(4);
|
|
187
|
+
* console.log(idx); // >= 0;
|
|
92
188
|
*/
|
|
93
189
|
upperBound(sum: number): number;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
* that needs to be checked.
|
|
122
|
-
*/
|
|
190
|
+
get size(): number;
|
|
191
|
+
isEmpty(): boolean;
|
|
192
|
+
clear(): void;
|
|
193
|
+
clone(): BinaryIndexedTree;
|
|
194
|
+
/**
|
|
195
|
+
* Returns the point values as a plain array.
|
|
196
|
+
* Time: O(n log n)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
* @example
|
|
200
|
+
* // Convert to array
|
|
201
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
202
|
+
* console.log(bit.toArray()); // [1, 2, 3];
|
|
203
|
+
*/
|
|
204
|
+
toArray(): number[];
|
|
205
|
+
/**
|
|
206
|
+
* Iterate over point values in index order.
|
|
207
|
+
*/
|
|
208
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
209
|
+
forEach(callback: (value: number, index: number) => void): void;
|
|
210
|
+
print(): void;
|
|
211
|
+
/** 1-based prefix sum up to pos (inclusive). */
|
|
212
|
+
protected _prefixSum(pos: number): number;
|
|
213
|
+
/** 1-based point update: add delta to position pos. */
|
|
214
|
+
protected _pointUpdate(pos: number, delta: number): void;
|
|
215
|
+
/** 1-based point query: get exact value at pos. */
|
|
216
|
+
protected _pointQuery(pos: number): number;
|
|
123
217
|
protected _checkIndex(index: number): void;
|
|
124
|
-
/**
|
|
125
|
-
|
|
126
|
-
* freqMap.
|
|
127
|
-
* @param {number} index - The `index` parameter is a number that represents the index of an element in a
|
|
128
|
-
* data structure.
|
|
129
|
-
* @returns a number.
|
|
130
|
-
*/
|
|
131
|
-
protected _readSingle(index: number): number;
|
|
132
|
-
/**
|
|
133
|
-
* The function `_updateNegativeCount` updates a counter based on changes in frequency values.
|
|
134
|
-
* @param {number} freqCur - The current frequency value.
|
|
135
|
-
* @param {number} freqNew - The freqNew parameter represents the new frequency value.
|
|
136
|
-
*/
|
|
137
|
-
protected _updateNegativeCount(freqCur: number, freqNew: number): void;
|
|
138
|
-
/**
|
|
139
|
-
* The `_update` function updates the values in a binary indexed freqMap starting from a given index and
|
|
140
|
-
* propagating the changes to its parent nodes.
|
|
141
|
-
* @param {number} index - The `index` parameter is a number that represents the index of the element in
|
|
142
|
-
* the data structure that needs to be updated.
|
|
143
|
-
* @param {number} delta - The `delta` parameter represents the change in value that needs to be
|
|
144
|
-
* applied to the elements in the data structure.
|
|
145
|
-
*/
|
|
146
|
-
protected _update(index: number, delta: number): void;
|
|
147
|
-
/**
|
|
148
|
-
* The `_writeSingle` function updates the frequency at a specific index and triggers a callback if
|
|
149
|
-
* the frequency has changed.
|
|
150
|
-
* @param {number} index - The `index` parameter is a number that represents the index of the element
|
|
151
|
-
* being modified or accessed.
|
|
152
|
-
* @param {number} freq - The `freq` parameter represents the new frequency value that needs to be
|
|
153
|
-
* written to the specified index `index`.
|
|
154
|
-
*/
|
|
155
|
-
protected _writeSingle(index: number, freq: number): void;
|
|
156
|
-
/**
|
|
157
|
-
* The `_read` function calculates the sum of values in a binary freqMap up to a given count.
|
|
158
|
-
* @param {number} count - The `count` parameter is a number that represents the number of elements
|
|
159
|
-
* to read from the freqMap.
|
|
160
|
-
* @returns the sum of the values obtained from calling the `_getFrequency` method for each index in the
|
|
161
|
-
* range from `count` to 1.
|
|
162
|
-
*/
|
|
163
|
-
protected _read(count: number): number;
|
|
164
|
-
/**
|
|
165
|
-
* The function `_binarySearch` performs a binary search to find the largest number that satisfies a given
|
|
166
|
-
* condition.
|
|
167
|
-
* @param {number} sum - The sum parameter is a number that represents the target sum value.
|
|
168
|
-
* @param before - The `before` parameter is a function that takes two numbers `x` and `y` as
|
|
169
|
-
* arguments and returns a boolean value. It is used to determine if `x` is less than or equal to
|
|
170
|
-
* `y`. The purpose of this function is to compare two numbers and determine their order.
|
|
171
|
-
* @returns the value of the variable "left".
|
|
172
|
-
*/
|
|
173
|
-
protected _binarySearch(sum: number, before: (x: number, y: number) => boolean): number;
|
|
218
|
+
/** Returns highest power of 2 <= n. */
|
|
219
|
+
protected _highBit(n: number): number;
|
|
174
220
|
}
|