data-structure-typed 2.5.3 → 2.6.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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +1 -1
- package/MIGRATION.md +48 -0
- package/README.md +20 -2
- package/README_CN.md +20 -2
- package/SPECIFICATION.md +24 -0
- package/SPECIFICATION.zh-CN.md +24 -0
- package/dist/cjs/binary-tree.cjs +1897 -19
- package/dist/cjs/graph.cjs +174 -0
- package/dist/cjs/hash.cjs +33 -0
- package/dist/cjs/heap.cjs +71 -0
- package/dist/cjs/index.cjs +2383 -3
- package/dist/cjs/linked-list.cjs +224 -2
- package/dist/cjs/matrix.cjs +24 -0
- package/dist/cjs/priority-queue.cjs +71 -0
- package/dist/cjs/queue.cjs +221 -1
- package/dist/cjs/stack.cjs +59 -0
- package/dist/cjs/trie.cjs +62 -0
- package/dist/cjs-legacy/binary-tree.cjs +1897 -19
- package/dist/cjs-legacy/graph.cjs +174 -0
- package/dist/cjs-legacy/hash.cjs +33 -0
- package/dist/cjs-legacy/heap.cjs +71 -0
- package/dist/cjs-legacy/index.cjs +2383 -3
- package/dist/cjs-legacy/linked-list.cjs +224 -2
- package/dist/cjs-legacy/matrix.cjs +24 -0
- package/dist/cjs-legacy/priority-queue.cjs +71 -0
- package/dist/cjs-legacy/queue.cjs +221 -1
- package/dist/cjs-legacy/stack.cjs +59 -0
- package/dist/cjs-legacy/trie.cjs +62 -0
- package/dist/esm/binary-tree.mjs +1897 -19
- package/dist/esm/graph.mjs +174 -0
- package/dist/esm/hash.mjs +33 -0
- package/dist/esm/heap.mjs +71 -0
- package/dist/esm/index.mjs +2383 -3
- package/dist/esm/linked-list.mjs +224 -2
- package/dist/esm/matrix.mjs +24 -0
- package/dist/esm/priority-queue.mjs +71 -0
- package/dist/esm/queue.mjs +221 -1
- package/dist/esm/stack.mjs +59 -0
- package/dist/esm/trie.mjs +62 -0
- package/dist/esm-legacy/binary-tree.mjs +1897 -19
- package/dist/esm-legacy/graph.mjs +174 -0
- package/dist/esm-legacy/hash.mjs +33 -0
- package/dist/esm-legacy/heap.mjs +71 -0
- package/dist/esm-legacy/index.mjs +2383 -3
- package/dist/esm-legacy/linked-list.mjs +224 -2
- package/dist/esm-legacy/matrix.mjs +24 -0
- package/dist/esm-legacy/priority-queue.mjs +71 -0
- package/dist/esm-legacy/queue.mjs +221 -1
- package/dist/esm-legacy/stack.mjs +59 -0
- package/dist/esm-legacy/trie.mjs +62 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +90 -1
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/umd/data-structure-typed.js +2383 -3
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/jest.integration.config.js +1 -2
- package/package.json +51 -50
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +32 -3
- package/src/data-structures/base/linear-base.ts +13 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -493
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
- package/src/data-structures/binary-tree/bst.ts +158 -1010
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
- package/src/data-structures/binary-tree/segment-tree.ts +73 -333
- package/src/data-structures/binary-tree/tree-map.ts +462 -4749
- package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
- package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
- package/src/data-structures/binary-tree/tree-set.ts +437 -4443
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -532
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -484
- package/src/data-structures/hash/hash-map.ts +154 -549
- package/src/data-structures/heap/heap.ts +200 -753
- package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
- package/src/data-structures/matrix/matrix.ts +179 -494
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +241 -807
- package/src/data-structures/queue/queue.ts +102 -589
- package/src/data-structures/stack/stack.ts +76 -475
- package/src/data-structures/trie/trie.ts +98 -592
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
* - Default iteration is expanded (each element yielded `count(x)` times)
|
|
8
8
|
* - `delete(x)` removes one occurrence by default
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
10
|
import type { Comparator, TreeMultiSetOptions } from '../../types';
|
|
12
11
|
import { ERR, raise } from '../../common';
|
|
13
12
|
import { RedBlackTree } from './red-black-tree';
|
|
@@ -16,7 +15,6 @@ import { TreeSet } from './tree-set';
|
|
|
16
15
|
export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
17
16
|
readonly #core: RedBlackTree<K, number>;
|
|
18
17
|
readonly #isDefaultComparator: boolean;
|
|
19
|
-
private _size = 0; // total occurrences (sumCounts)
|
|
20
18
|
|
|
21
19
|
/**
|
|
22
20
|
* Creates a new TreeMultiSet.
|
|
@@ -35,43 +33,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
35
33
|
const toElementFn = options.toElementFn;
|
|
36
34
|
const comparator = options.comparator ?? TreeSet.createDefaultComparator<K>();
|
|
37
35
|
this.#isDefaultComparator = options.comparator === undefined;
|
|
38
|
-
this.#core = new RedBlackTree<K, number>([], {
|
|
39
|
-
|
|
36
|
+
this.#core = new RedBlackTree<K, number>([], {
|
|
37
|
+
comparator,
|
|
38
|
+
isMapMode: options.isMapMode,
|
|
39
|
+
enableOrderStatistic: options.enableOrderStatistic
|
|
40
|
+
});
|
|
40
41
|
for (const item of elements) {
|
|
41
42
|
const k = toElementFn ? toElementFn(item as R) : (item as K);
|
|
42
43
|
this.add(k);
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
* Validates the key against the default comparator rules.
|
|
48
|
-
* @remarks Time O(1), Space O(1)
|
|
49
|
-
*/
|
|
50
|
-
private _validateKey(key: K): void {
|
|
51
|
-
if (!this.#isDefaultComparator) return;
|
|
52
|
-
|
|
53
|
-
if (typeof key === 'number') {
|
|
54
|
-
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN('TreeMultiSet'));
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (typeof key === 'string') return;
|
|
59
|
-
|
|
60
|
-
if (key instanceof Date) {
|
|
61
|
-
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeMultiSet'));
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
raise(TypeError, ERR.comparatorRequired('TreeMultiSet'));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Validates that count is a non-negative safe integer.
|
|
70
|
-
* @remarks Time O(1), Space O(1)
|
|
71
|
-
*/
|
|
72
|
-
private _validateCount(n: number): void {
|
|
73
|
-
if (!Number.isSafeInteger(n) || n < 0) raise(RangeError, ERR.invalidArgument('count must be a safe integer >= 0.', 'TreeMultiSet'));
|
|
74
|
-
}
|
|
47
|
+
private _size = 0; // total occurrences (sumCounts)
|
|
75
48
|
|
|
76
49
|
/**
|
|
77
50
|
* Total occurrences (sumCounts).
|
|
@@ -84,231 +57,31 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
84
57
|
/**
|
|
85
58
|
* Number of distinct keys.
|
|
86
59
|
* @remarks Time O(1), Space O(1)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
* @example
|
|
118
|
-
* // Unique key count
|
|
119
|
-
* const ms = new TreeMultiSet<number>();
|
|
120
|
-
* ms.add(1, 3);
|
|
121
|
-
* ms.add(2, 2);
|
|
122
|
-
* console.log(ms.distinctSize); // 2;
|
|
60
|
+
* @example
|
|
61
|
+
* // Unique key count
|
|
62
|
+
* const ms = new TreeMultiSet<number>();
|
|
63
|
+
* ms.add(1, 3);
|
|
64
|
+
* ms.add(2, 2);
|
|
65
|
+
* console.log(ms.distinctSize); // 2;
|
|
123
66
|
*/
|
|
124
67
|
get distinctSize(): number {
|
|
125
68
|
return this.#core.size;
|
|
126
69
|
}
|
|
127
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Expose comparator for advanced usage/testing (read-only).
|
|
73
|
+
* @remarks Time O(1), Space O(1)
|
|
74
|
+
*/
|
|
75
|
+
get comparator(): Comparator<K> {
|
|
76
|
+
return this.#core.comparator;
|
|
77
|
+
}
|
|
78
|
+
|
|
128
79
|
/**
|
|
129
80
|
* Whether the multiset is empty.
|
|
130
81
|
* @remarks Time O(1), Space O(1)
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
* @example
|
|
310
|
-
* // Check empty
|
|
311
|
-
* console.log(new TreeMultiSet().isEmpty()); // true;
|
|
82
|
+
* @example
|
|
83
|
+
* // Check empty
|
|
84
|
+
* console.log(new TreeMultiSet().isEmpty()); // true;
|
|
312
85
|
*/
|
|
313
86
|
isEmpty(): boolean {
|
|
314
87
|
return this.size === 0;
|
|
@@ -317,193 +90,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
317
90
|
/**
|
|
318
91
|
* Whether the multiset contains the given key.
|
|
319
92
|
* @remarks Time O(log n), Space O(1)
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
* @example
|
|
502
|
-
* // Check existence
|
|
503
|
-
* const ms = new TreeMultiSet<number>();
|
|
504
|
-
* ms.add(1);
|
|
505
|
-
* console.log(ms.has(1)); // true;
|
|
506
|
-
* console.log(ms.has(2)); // false;
|
|
93
|
+
* @example
|
|
94
|
+
* // Check existence
|
|
95
|
+
* const ms = new TreeMultiSet<number>();
|
|
96
|
+
* ms.add(1);
|
|
97
|
+
* console.log(ms.has(1)); // true;
|
|
98
|
+
* console.log(ms.has(2)); // false;
|
|
507
99
|
*/
|
|
508
100
|
has(key: K): boolean {
|
|
509
101
|
this._validateKey(key);
|
|
@@ -513,41 +105,11 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
513
105
|
/**
|
|
514
106
|
* Returns the count of occurrences for the given key.
|
|
515
107
|
* @remarks Time O(log n), Space O(1)
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
* @example
|
|
547
|
-
* // Get occurrence count
|
|
548
|
-
* const ms = new TreeMultiSet<number>();
|
|
549
|
-
* ms.add(1, 5);
|
|
550
|
-
* console.log(ms.count(1)); // 5;
|
|
108
|
+
* @example
|
|
109
|
+
* // Get occurrence count
|
|
110
|
+
* const ms = new TreeMultiSet<number>();
|
|
111
|
+
* ms.add(1, 5);
|
|
112
|
+
* console.log(ms.count(1)); // 5;
|
|
551
113
|
*/
|
|
552
114
|
count(key: K): number {
|
|
553
115
|
this._validateKey(key);
|
|
@@ -558,192 +120,19 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
558
120
|
* Add `n` occurrences of `key`.
|
|
559
121
|
* @returns True if the multiset changed.
|
|
560
122
|
* @remarks Time O(log n), Space O(1)
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
* @example
|
|
734
|
-
* // Add elements
|
|
735
|
-
* const ms = new TreeMultiSet<number>();
|
|
736
|
-
* ms.add(1);
|
|
737
|
-
* ms.add(1);
|
|
738
|
-
* ms.add(2);
|
|
739
|
-
* console.log(ms.count(1)); // 2;
|
|
740
|
-
* console.log(ms.size); // 3;
|
|
123
|
+
* @example
|
|
124
|
+
* // Add elements
|
|
125
|
+
* const ms = new TreeMultiSet<number>();
|
|
126
|
+
* ms.add(1);
|
|
127
|
+
* ms.add(1);
|
|
128
|
+
* ms.add(2);
|
|
129
|
+
* console.log(ms.count(1)); // 2;
|
|
130
|
+
* console.log(ms.size); // 3;
|
|
741
131
|
*/
|
|
742
132
|
add(key: K, n = 1): boolean {
|
|
743
133
|
this._validateKey(key);
|
|
744
134
|
this._validateCount(n);
|
|
745
135
|
if (n === 0) return false;
|
|
746
|
-
|
|
747
136
|
const old = this.#core.get(key) ?? 0;
|
|
748
137
|
const next = old + n;
|
|
749
138
|
this.#core.set(key, next);
|
|
@@ -755,55 +144,22 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
755
144
|
* Set count for `key` to exactly `n`.
|
|
756
145
|
* @returns True if changed.
|
|
757
146
|
* @remarks Time O(log n), Space O(1)
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
* @example
|
|
789
|
-
* // Set occurrence count
|
|
790
|
-
* const ms = new TreeMultiSet<number>();
|
|
791
|
-
* ms.setCount(1, 3);
|
|
792
|
-
* console.log(ms.count(1)); // 3;
|
|
147
|
+
* @example
|
|
148
|
+
* // Set occurrence count
|
|
149
|
+
* const ms = new TreeMultiSet<number>();
|
|
150
|
+
* ms.setCount(1, 3);
|
|
151
|
+
* console.log(ms.count(1)); // 3;
|
|
793
152
|
*/
|
|
794
153
|
setCount(key: K, n: number): boolean {
|
|
795
154
|
this._validateKey(key);
|
|
796
155
|
this._validateCount(n);
|
|
797
|
-
|
|
798
156
|
const old = this.#core.get(key) ?? 0;
|
|
799
157
|
if (old === n) return false;
|
|
800
|
-
|
|
801
158
|
if (n === 0) {
|
|
802
159
|
if (old !== 0) this.#core.delete(key);
|
|
803
160
|
} else {
|
|
804
161
|
this.#core.set(key, n);
|
|
805
162
|
}
|
|
806
|
-
|
|
807
163
|
this._size += n - old;
|
|
808
164
|
return true;
|
|
809
165
|
}
|
|
@@ -812,208 +168,23 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
812
168
|
* Delete `n` occurrences of `key` (default 1).
|
|
813
169
|
* @returns True if any occurrence was removed.
|
|
814
170
|
* @remarks Time O(log n), Space O(1)
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
* @example
|
|
997
|
-
* // Remove one occurrence
|
|
998
|
-
* const ms = new TreeMultiSet<number>();
|
|
999
|
-
* ms.add(1, 3);
|
|
1000
|
-
* ms.delete(1);
|
|
1001
|
-
* console.log(ms.count(1)); // 2;
|
|
171
|
+
* @example
|
|
172
|
+
* // Remove one occurrence
|
|
173
|
+
* const ms = new TreeMultiSet<number>();
|
|
174
|
+
* ms.add(1, 3);
|
|
175
|
+
* ms.delete(1);
|
|
176
|
+
* console.log(ms.count(1)); // 2;
|
|
1002
177
|
*/
|
|
1003
178
|
delete(key: K, n = 1): boolean {
|
|
1004
179
|
this._validateKey(key);
|
|
1005
180
|
this._validateCount(n);
|
|
1006
181
|
if (n === 0) return false;
|
|
1007
|
-
|
|
1008
182
|
const old = this.#core.get(key) ?? 0;
|
|
1009
183
|
if (old === 0) return false;
|
|
1010
|
-
|
|
1011
184
|
const removed = Math.min(old, n);
|
|
1012
185
|
const next = old - removed;
|
|
1013
|
-
|
|
1014
186
|
if (next === 0) this.#core.delete(key);
|
|
1015
187
|
else this.#core.set(key, next);
|
|
1016
|
-
|
|
1017
188
|
this._size -= removed;
|
|
1018
189
|
return true;
|
|
1019
190
|
}
|
|
@@ -1022,42 +193,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1022
193
|
* Delete all occurrences of the given key.
|
|
1023
194
|
* @returns True if any occurrence was removed.
|
|
1024
195
|
* @remarks Time O(log n), Space O(1)
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
* @example
|
|
1056
|
-
* // Remove all occurrences
|
|
1057
|
-
* const ms = new TreeMultiSet<number>();
|
|
1058
|
-
* ms.add(1, 3);
|
|
1059
|
-
* ms.deleteAll(1);
|
|
1060
|
-
* console.log(ms.has(1)); // false;
|
|
196
|
+
* @example
|
|
197
|
+
* // Remove all occurrences
|
|
198
|
+
* const ms = new TreeMultiSet<number>();
|
|
199
|
+
* ms.add(1, 3);
|
|
200
|
+
* ms.deleteAll(1);
|
|
201
|
+
* console.log(ms.has(1)); // false;
|
|
1061
202
|
*/
|
|
1062
203
|
deleteAll(key: K): boolean {
|
|
1063
204
|
this._validateKey(key);
|
|
@@ -1071,42 +212,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1071
212
|
/**
|
|
1072
213
|
* Iterates over distinct keys (each key yielded once).
|
|
1073
214
|
* @remarks Time O(n), Space O(1)
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
* @example
|
|
1105
|
-
* // Iterate unique keys
|
|
1106
|
-
* const ms = new TreeMultiSet<number>();
|
|
1107
|
-
* ms.add(1, 2);
|
|
1108
|
-
* ms.add(2);
|
|
1109
|
-
* console.log([...ms.keysDistinct()]); // [1, 2];
|
|
215
|
+
* @example
|
|
216
|
+
* // Iterate unique keys
|
|
217
|
+
* const ms = new TreeMultiSet<number>();
|
|
218
|
+
* ms.add(1, 2);
|
|
219
|
+
* ms.add(2);
|
|
220
|
+
* console.log([...ms.keysDistinct()]); // [1, 2];
|
|
1110
221
|
*/
|
|
1111
222
|
*keysDistinct(): IterableIterator<K> {
|
|
1112
223
|
yield* this.#core.keys();
|
|
@@ -1115,189 +226,11 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1115
226
|
/**
|
|
1116
227
|
* Iterates over entries as [key, count] pairs.
|
|
1117
228
|
* @remarks Time O(n), Space O(1)
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
* @example
|
|
1297
|
-
* // Iterate entries
|
|
1298
|
-
* const ms = new TreeMultiSet<number>();
|
|
1299
|
-
* ms.add(1, 2);
|
|
1300
|
-
* console.log([...ms.entries()].length); // > 0;
|
|
229
|
+
* @example
|
|
230
|
+
* // Iterate entries
|
|
231
|
+
* const ms = new TreeMultiSet<number>();
|
|
232
|
+
* ms.add(1, 2);
|
|
233
|
+
* console.log([...ms.entries()].length); // > 0;
|
|
1301
234
|
*/
|
|
1302
235
|
*entries(): IterableIterator<[K, number]> {
|
|
1303
236
|
for (const [k, v] of this.#core) {
|
|
@@ -1315,193 +248,48 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1315
248
|
}
|
|
1316
249
|
}
|
|
1317
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Iterate over all elements with multiplicity (Set-compatible, alias for `[Symbol.iterator]`).
|
|
253
|
+
* @remarks Each key is yielded `count(key)` times. Time O(size), Space O(1) per step.
|
|
254
|
+
* @example
|
|
255
|
+
* // Iterate with multiplicity
|
|
256
|
+
* const ms = new TreeMultiSet<number>();
|
|
257
|
+
* ms.add(1);
|
|
258
|
+
* ms.add(1);
|
|
259
|
+
* ms.add(2);
|
|
260
|
+
* ms.add(3);
|
|
261
|
+
* ms.add(3);
|
|
262
|
+
* ms.add(3);
|
|
263
|
+
* console.log([...ms.keys()]); // [1, 1, 2, 3, 3, 3];
|
|
264
|
+
*/
|
|
265
|
+
*keys(): IterableIterator<K> {
|
|
266
|
+
yield* this;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Iterate over all elements with multiplicity (Set-compatible, alias for `[Symbol.iterator]`).
|
|
271
|
+
* @remarks Each key is yielded `count(key)` times. Time O(size), Space O(1) per step.
|
|
272
|
+
* @example
|
|
273
|
+
* // Iterate with multiplicity
|
|
274
|
+
* const ms = new TreeMultiSet<number>();
|
|
275
|
+
* ms.add(5);
|
|
276
|
+
* ms.add(5);
|
|
277
|
+
* ms.add(10);
|
|
278
|
+
* console.log([...ms.values()]); // [5, 5, 10];
|
|
279
|
+
*/
|
|
280
|
+
*values(): IterableIterator<K> {
|
|
281
|
+
yield* this;
|
|
282
|
+
}
|
|
283
|
+
|
|
1318
284
|
/**
|
|
1319
285
|
* Returns an array with all elements (expanded).
|
|
1320
286
|
* @remarks Time O(size), Space O(size)
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
* @example
|
|
1500
|
-
* // All elements (with duplicates)
|
|
1501
|
-
* const ms = new TreeMultiSet<number>();
|
|
1502
|
-
* ms.add(1, 2);
|
|
1503
|
-
* ms.add(2);
|
|
1504
|
-
* console.log(ms.toArray()); // [1, 1, 2];
|
|
287
|
+
* @example
|
|
288
|
+
* // All elements (with duplicates)
|
|
289
|
+
* const ms = new TreeMultiSet<number>();
|
|
290
|
+
* ms.add(1, 2);
|
|
291
|
+
* ms.add(2);
|
|
292
|
+
* console.log(ms.toArray()); // [1, 1, 2];
|
|
1505
293
|
*/
|
|
1506
294
|
toArray(): K[] {
|
|
1507
295
|
return [...this];
|
|
@@ -1510,42 +298,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1510
298
|
/**
|
|
1511
299
|
* Returns an array with distinct keys only.
|
|
1512
300
|
* @remarks Time O(n), Space O(n)
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
* @example
|
|
1544
|
-
* // Unique keys only
|
|
1545
|
-
* const ms = new TreeMultiSet<number>();
|
|
1546
|
-
* ms.add(1, 3);
|
|
1547
|
-
* ms.add(2);
|
|
1548
|
-
* console.log(ms.toDistinctArray()); // [1, 2];
|
|
301
|
+
* @example
|
|
302
|
+
* // Unique keys only
|
|
303
|
+
* const ms = new TreeMultiSet<number>();
|
|
304
|
+
* ms.add(1, 3);
|
|
305
|
+
* ms.add(2);
|
|
306
|
+
* console.log(ms.toDistinctArray()); // [1, 2];
|
|
1549
307
|
*/
|
|
1550
308
|
toDistinctArray(): K[] {
|
|
1551
309
|
return [...this.keysDistinct()];
|
|
@@ -1554,384 +312,77 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1554
312
|
/**
|
|
1555
313
|
* Returns an array of [key, count] entries.
|
|
1556
314
|
* @remarks Time O(n), Space O(n)
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
* @example
|
|
1588
|
-
* // Key-count pairs
|
|
1589
|
-
* const ms = new TreeMultiSet<number>();
|
|
1590
|
-
* ms.add(1, 2);
|
|
1591
|
-
* ms.add(3);
|
|
1592
|
-
* console.log(ms.toEntries()); // [[1, 2], [3, 1]];
|
|
315
|
+
* @example
|
|
316
|
+
* // Key-count pairs
|
|
317
|
+
* const ms = new TreeMultiSet<number>();
|
|
318
|
+
* ms.add(1, 2);
|
|
319
|
+
* ms.add(3);
|
|
320
|
+
* console.log(ms.toEntries()); // [
|
|
321
|
+
* // [1, 2],
|
|
322
|
+
* // [3, 1]
|
|
323
|
+
* // ];
|
|
1593
324
|
*/
|
|
1594
325
|
toEntries(): Array<[K, number]> {
|
|
1595
326
|
return [...this.entries()];
|
|
1596
327
|
}
|
|
1597
328
|
|
|
1598
|
-
/**
|
|
1599
|
-
* Expose comparator for advanced usage/testing (read-only).
|
|
1600
|
-
* @remarks Time O(1), Space O(1)
|
|
1601
|
-
*/
|
|
1602
|
-
get comparator(): Comparator<K> {
|
|
1603
|
-
return this.#core.comparator;
|
|
1604
|
-
}
|
|
1605
|
-
|
|
1606
|
-
// ━━━ clear ━━━
|
|
1607
|
-
|
|
1608
329
|
/**
|
|
1609
330
|
* Remove all elements from the multiset.
|
|
1610
331
|
* @remarks Time O(1), Space O(1)
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
* @example
|
|
1791
|
-
* // Remove all
|
|
1792
|
-
* const ms = new TreeMultiSet<number>();
|
|
1793
|
-
* ms.add(1);
|
|
1794
|
-
* ms.clear();
|
|
1795
|
-
* console.log(ms.isEmpty()); // true;
|
|
332
|
+
* @example
|
|
333
|
+
* // Remove all
|
|
334
|
+
* const ms = new TreeMultiSet<number>();
|
|
335
|
+
* ms.add(1);
|
|
336
|
+
* ms.clear();
|
|
337
|
+
* console.log(ms.isEmpty()); // true;
|
|
1796
338
|
*/
|
|
1797
339
|
clear(): void {
|
|
1798
340
|
this.#core.clear();
|
|
1799
341
|
this._size = 0;
|
|
1800
342
|
}
|
|
1801
343
|
|
|
1802
|
-
// ━━━ Navigable methods ━━━
|
|
1803
|
-
|
|
1804
344
|
/**
|
|
1805
345
|
* Returns the smallest key, or undefined if empty.
|
|
1806
346
|
* @remarks Time O(log n), Space O(1)
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
* @example
|
|
1839
|
-
* // Smallest element
|
|
1840
|
-
* const ms = new TreeMultiSet<number>();
|
|
1841
|
-
* ms.add(3);
|
|
1842
|
-
* ms.add(1);
|
|
1843
|
-
* console.log(ms.first()); // 1;
|
|
347
|
+
* @example
|
|
348
|
+
* // Smallest element
|
|
349
|
+
* const ms = new TreeMultiSet<number>();
|
|
350
|
+
* ms.add(3);
|
|
351
|
+
* ms.add(1);
|
|
352
|
+
* console.log(ms.first()); // 1;
|
|
1844
353
|
*/
|
|
1845
354
|
first(): K | undefined {
|
|
1846
355
|
return this.#core.getLeftMost();
|
|
1847
356
|
}
|
|
1848
357
|
|
|
358
|
+
// ━━━ clear ━━━
|
|
359
|
+
|
|
1849
360
|
/**
|
|
1850
361
|
* Returns the largest key, or undefined if empty.
|
|
1851
362
|
* @remarks Time O(log n), Space O(1)
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
* @example
|
|
1884
|
-
* // Largest element
|
|
1885
|
-
* const ms = new TreeMultiSet<number>();
|
|
1886
|
-
* ms.add(1);
|
|
1887
|
-
* ms.add(3);
|
|
1888
|
-
* console.log(ms.last()); // 3;
|
|
363
|
+
* @example
|
|
364
|
+
* // Largest element
|
|
365
|
+
* const ms = new TreeMultiSet<number>();
|
|
366
|
+
* ms.add(1);
|
|
367
|
+
* ms.add(3);
|
|
368
|
+
* console.log(ms.last()); // 3;
|
|
1889
369
|
*/
|
|
1890
370
|
last(): K | undefined {
|
|
1891
371
|
return this.#core.getRightMost();
|
|
1892
372
|
}
|
|
1893
373
|
|
|
374
|
+
// ━━━ Navigable methods ━━━
|
|
375
|
+
|
|
1894
376
|
/**
|
|
1895
377
|
* Removes all occurrences of the smallest key and returns it.
|
|
1896
378
|
* @remarks Time O(log n), Space O(1)
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
* @example
|
|
1929
|
-
* // Remove and return smallest
|
|
1930
|
-
* const ms = new TreeMultiSet<number>();
|
|
1931
|
-
* ms.add(2);
|
|
1932
|
-
* ms.add(1);
|
|
1933
|
-
* console.log(ms.pollFirst()); // 1;
|
|
1934
|
-
* console.log(ms.has(1)); // false;
|
|
379
|
+
* @example
|
|
380
|
+
* // Remove and return smallest
|
|
381
|
+
* const ms = new TreeMultiSet<number>();
|
|
382
|
+
* ms.add(2);
|
|
383
|
+
* ms.add(1);
|
|
384
|
+
* console.log(ms.pollFirst()); // 1;
|
|
385
|
+
* console.log(ms.has(1)); // false;
|
|
1935
386
|
*/
|
|
1936
387
|
pollFirst(): K | undefined {
|
|
1937
388
|
const key = this.first();
|
|
@@ -1943,43 +394,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1943
394
|
/**
|
|
1944
395
|
* Removes all occurrences of the largest key and returns it.
|
|
1945
396
|
* @remarks Time O(log n), Space O(1)
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
* @example
|
|
1978
|
-
* // Remove and return largest
|
|
1979
|
-
* const ms = new TreeMultiSet<number>();
|
|
1980
|
-
* ms.add(1);
|
|
1981
|
-
* ms.add(3);
|
|
1982
|
-
* console.log(ms.pollLast()); // 3;
|
|
397
|
+
* @example
|
|
398
|
+
* // Remove and return largest
|
|
399
|
+
* const ms = new TreeMultiSet<number>();
|
|
400
|
+
* ms.add(1);
|
|
401
|
+
* ms.add(3);
|
|
402
|
+
* console.log(ms.pollLast()); // 3;
|
|
1983
403
|
*/
|
|
1984
404
|
pollLast(): K | undefined {
|
|
1985
405
|
const key = this.last();
|
|
@@ -1991,155 +411,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
1991
411
|
/**
|
|
1992
412
|
* Returns the smallest key >= given key, or undefined.
|
|
1993
413
|
* @remarks Time O(log n), Space O(1)
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
* @example
|
|
2137
|
-
* // Least key ≥ target
|
|
2138
|
-
* const ms = new TreeMultiSet<number>();
|
|
2139
|
-
* ms.add(10);
|
|
2140
|
-
* ms.add(20);
|
|
2141
|
-
* ms.add(30);
|
|
2142
|
-
* console.log(ms.ceiling(15)); // 20;
|
|
414
|
+
* @example
|
|
415
|
+
* // Least key ≥ target
|
|
416
|
+
* const ms = new TreeMultiSet<number>();
|
|
417
|
+
* ms.add(10);
|
|
418
|
+
* ms.add(20);
|
|
419
|
+
* ms.add(30);
|
|
420
|
+
* console.log(ms.ceiling(15)); // 20;
|
|
2143
421
|
*/
|
|
2144
422
|
ceiling(key: K): K | undefined {
|
|
2145
423
|
this._validateKey(key);
|
|
@@ -2149,155 +427,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
2149
427
|
/**
|
|
2150
428
|
* Returns the largest key <= given key, or undefined.
|
|
2151
429
|
* @remarks Time O(log n), Space O(1)
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
* @example
|
|
2295
|
-
* // Greatest key ≤ target
|
|
2296
|
-
* const ms = new TreeMultiSet<number>();
|
|
2297
|
-
* ms.add(10);
|
|
2298
|
-
* ms.add(20);
|
|
2299
|
-
* ms.add(30);
|
|
2300
|
-
* console.log(ms.floor(25)); // 20;
|
|
430
|
+
* @example
|
|
431
|
+
* // Greatest key ≤ target
|
|
432
|
+
* const ms = new TreeMultiSet<number>();
|
|
433
|
+
* ms.add(10);
|
|
434
|
+
* ms.add(20);
|
|
435
|
+
* ms.add(30);
|
|
436
|
+
* console.log(ms.floor(25)); // 20;
|
|
2301
437
|
*/
|
|
2302
438
|
floor(key: K): K | undefined {
|
|
2303
439
|
this._validateKey(key);
|
|
@@ -2307,154 +443,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
2307
443
|
/**
|
|
2308
444
|
* Returns the smallest key > given key, or undefined.
|
|
2309
445
|
* @remarks Time O(log n), Space O(1)
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
* @example
|
|
2453
|
-
* // Least key > target
|
|
2454
|
-
* const ms = new TreeMultiSet<number>();
|
|
2455
|
-
* ms.add(10);
|
|
2456
|
-
* ms.add(20);
|
|
2457
|
-
* console.log(ms.higher(10)); // 20;
|
|
446
|
+
* @example
|
|
447
|
+
* // Least key > target
|
|
448
|
+
* const ms = new TreeMultiSet<number>();
|
|
449
|
+
* ms.add(10);
|
|
450
|
+
* ms.add(20);
|
|
451
|
+
* console.log(ms.higher(10)); // 20;
|
|
2458
452
|
*/
|
|
2459
453
|
higher(key: K): K | undefined {
|
|
2460
454
|
this._validateKey(key);
|
|
@@ -2464,352 +458,32 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
2464
458
|
/**
|
|
2465
459
|
* Returns the largest key < given key, or undefined.
|
|
2466
460
|
* @remarks Time O(log n), Space O(1)
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
* @example
|
|
2610
|
-
* // Greatest key < target
|
|
2611
|
-
* const ms = new TreeMultiSet<number>();
|
|
2612
|
-
* ms.add(10);
|
|
2613
|
-
* ms.add(20);
|
|
2614
|
-
* console.log(ms.lower(20)); // 10;
|
|
461
|
+
* @example
|
|
462
|
+
* // Greatest key < target
|
|
463
|
+
* const ms = new TreeMultiSet<number>();
|
|
464
|
+
* ms.add(10);
|
|
465
|
+
* ms.add(20);
|
|
466
|
+
* console.log(ms.lower(20)); // 10;
|
|
2615
467
|
*/
|
|
2616
468
|
lower(key: K): K | undefined {
|
|
2617
469
|
this._validateKey(key);
|
|
2618
470
|
return this.#core.lower(key);
|
|
2619
471
|
}
|
|
2620
472
|
|
|
2621
|
-
// ━━━ Functional methods ━━━
|
|
2622
|
-
|
|
2623
473
|
/**
|
|
2624
474
|
* Iterates over distinct keys with their counts.
|
|
2625
475
|
* @remarks Time O(n), Space O(1)
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
* @example
|
|
2806
|
-
* // Iterate
|
|
2807
|
-
* const ms = new TreeMultiSet<number>();
|
|
2808
|
-
* ms.add(1, 2);
|
|
2809
|
-
* ms.add(2);
|
|
2810
|
-
* const pairs: [number, number][] = [];
|
|
2811
|
-
* ms.forEach((k, c) => pairs.push([k, c]));
|
|
2812
|
-
* console.log(pairs); // [[1, 2], [2, 1]];
|
|
476
|
+
* @example
|
|
477
|
+
* // Iterate
|
|
478
|
+
* const ms = new TreeMultiSet<number>();
|
|
479
|
+
* ms.add(1, 2);
|
|
480
|
+
* ms.add(2);
|
|
481
|
+
* const pairs: [number, number][] = [];
|
|
482
|
+
* ms.forEach((k, c) => pairs.push([k, c]));
|
|
483
|
+
* console.log(pairs); // [
|
|
484
|
+
* // [1, 2],
|
|
485
|
+
* // [2, 1]
|
|
486
|
+
* // ];
|
|
2813
487
|
*/
|
|
2814
488
|
forEach(callback: (key: K, count: number) => void): void {
|
|
2815
489
|
for (const [k, c] of this.entries()) {
|
|
@@ -2820,193 +494,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
2820
494
|
/**
|
|
2821
495
|
* Creates a new TreeMultiSet with entries that match the predicate.
|
|
2822
496
|
* @remarks Time O(n log n), Space O(n)
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
* @example
|
|
3003
|
-
* // Filter
|
|
3004
|
-
* const ms = new TreeMultiSet<number>();
|
|
3005
|
-
* ms.add(1, 3);
|
|
3006
|
-
* ms.add(2, 1);
|
|
3007
|
-
* ms.add(3, 2);
|
|
3008
|
-
* const filtered = ms.filter((k, c) => c > 1);
|
|
3009
|
-
* console.log([...filtered.keysDistinct()]); // [1, 3];
|
|
497
|
+
* @example
|
|
498
|
+
* // Filter
|
|
499
|
+
* const ms = new TreeMultiSet<number>();
|
|
500
|
+
* ms.add(1, 3);
|
|
501
|
+
* ms.add(2, 1);
|
|
502
|
+
* ms.add(3, 2);
|
|
503
|
+
* const filtered = ms.filter((k, c) => c > 1);
|
|
504
|
+
* console.log([...filtered.keysDistinct()]); // [1, 3];
|
|
3010
505
|
*/
|
|
3011
506
|
filter(predicate: (key: K, count: number) => boolean): TreeMultiSet<K> {
|
|
3012
507
|
const result = new TreeMultiSet<K>([], {
|
|
@@ -3021,195 +516,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
3021
516
|
return result;
|
|
3022
517
|
}
|
|
3023
518
|
|
|
519
|
+
// ━━━ Functional methods ━━━
|
|
520
|
+
|
|
3024
521
|
/**
|
|
3025
522
|
* Reduces the multiset to a single value.
|
|
3026
523
|
* @remarks Time O(n), Space O(1)
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
* @example
|
|
3207
|
-
* // Aggregate
|
|
3208
|
-
* const ms = new TreeMultiSet<number>();
|
|
3209
|
-
* ms.add(1, 2);
|
|
3210
|
-
* ms.add(2, 3);
|
|
3211
|
-
* const sum = ms.reduce((acc, k, c) => acc + k * c, 0);
|
|
3212
|
-
* console.log(sum); // 8;
|
|
524
|
+
* @example
|
|
525
|
+
* // Aggregate
|
|
526
|
+
* const ms = new TreeMultiSet<number>();
|
|
527
|
+
* ms.add(1, 2);
|
|
528
|
+
* ms.add(2, 3);
|
|
529
|
+
* const sum = ms.reduce((acc, k, c) => acc + k * c, 0);
|
|
530
|
+
* console.log(sum); // 8;
|
|
3213
531
|
*/
|
|
3214
532
|
reduce<U>(callback: (accumulator: U, key: K, count: number) => U, initialValue: U): U {
|
|
3215
533
|
let acc = initialValue;
|
|
@@ -3223,192 +541,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
3223
541
|
* Maps keys and counts to a new TreeMultiSet.
|
|
3224
542
|
* When multiple keys map to the same new key, counts are merged (added).
|
|
3225
543
|
* @remarks Time O(n log n), Space O(n)
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
* @example
|
|
3406
|
-
* // Transform
|
|
3407
|
-
* const ms = new TreeMultiSet<number>();
|
|
3408
|
-
* ms.add(1, 2);
|
|
3409
|
-
* ms.add(2, 3);
|
|
3410
|
-
* const doubled = ms.map((k, c) => [k * 10, c] as [number, number]);
|
|
3411
|
-
* console.log([...doubled.keysDistinct()]); // [10, 20];
|
|
544
|
+
* @example
|
|
545
|
+
* // Transform
|
|
546
|
+
* const ms = new TreeMultiSet<number>();
|
|
547
|
+
* ms.add(1, 2);
|
|
548
|
+
* ms.add(2, 3);
|
|
549
|
+
* const doubled = ms.map((k, c) => [k * 10, c] as [number, number]);
|
|
550
|
+
* console.log([...doubled.keysDistinct()]); // [10, 20];
|
|
3412
551
|
*/
|
|
3413
552
|
map<K2>(
|
|
3414
553
|
mapper: (key: K, count: number) => [K2, number],
|
|
@@ -3428,256 +567,62 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
3428
567
|
/**
|
|
3429
568
|
* Creates an independent copy of this multiset.
|
|
3430
569
|
* @remarks Time O(n log n), Space O(n)
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3508
|
-
|
|
3509
|
-
|
|
3510
|
-
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
|
|
3521
|
-
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
* @example
|
|
3589
|
-
* // Order-statistic on BST
|
|
3590
|
-
* const tree = new TreeMultiSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
3591
|
-
* console.log(tree.getByRank(0)); // 10;
|
|
3592
|
-
* console.log(tree.getByRank(4)); // 50;
|
|
3593
|
-
* console.log(tree.getRank(30)); // 2;
|
|
570
|
+
* @example
|
|
571
|
+
* // Order-statistic on BST
|
|
572
|
+
* const tree = new TreeMultiSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
573
|
+
* console.log(tree.getByRank(0)); // 10;
|
|
574
|
+
* console.log(tree.getByRank(4)); // 50;
|
|
575
|
+
* console.log(tree.getRank(30)); // 2;
|
|
3594
576
|
*/
|
|
3595
577
|
// ─── Order-Statistic Methods ───────────────────────────
|
|
3596
|
-
|
|
3597
578
|
getByRank(k: number): K | undefined {
|
|
3598
579
|
return this.#core.getByRank(k);
|
|
3599
580
|
}
|
|
3600
581
|
|
|
3601
|
-
|
|
582
|
+
/**
|
|
3602
583
|
* Get the rank of a key in sorted order
|
|
3603
584
|
* @example
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
585
|
+
* // Get the rank of a key in sorted order
|
|
586
|
+
* const tree = new TreeMultiSet<number>(
|
|
587
|
+
* [10, 20, 30, 40, 50],
|
|
588
|
+
* { enableOrderStatistic: true }
|
|
589
|
+
* );
|
|
590
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
591
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
592
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
593
|
+
* console.log(tree.getRank(25)); // 2;
|
|
3613
594
|
*/
|
|
3614
595
|
getRank(key: K): number {
|
|
3615
596
|
return this.#core.getRank(key);
|
|
3616
597
|
}
|
|
3617
598
|
|
|
3618
|
-
|
|
599
|
+
/**
|
|
3619
600
|
* Get elements by rank range
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
*
|
|
3631
|
-
* const tree = new TreeMultiSet<number>(
|
|
3632
|
-
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
3633
|
-
* { enableOrderStatistic: true }
|
|
3634
|
-
* );
|
|
3635
|
-
* const pageSize = 3;
|
|
3636
|
-
*
|
|
3637
|
-
* // Page 1
|
|
3638
|
-
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
3639
|
-
* // Page 2
|
|
3640
|
-
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
3641
|
-
* // Page 3
|
|
3642
|
-
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
601
|
+
* @example
|
|
602
|
+
* // Pagination by position in tree order
|
|
603
|
+
* const tree = new TreeMultiSet<number>([10, 20, 30, 40, 50, 60, 70, 80, 90], { enableOrderStatistic: true });
|
|
604
|
+
* const pageSize = 3;
|
|
605
|
+
*
|
|
606
|
+
* // Page 1
|
|
607
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
608
|
+
* // Page 2
|
|
609
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
610
|
+
* // Page 3
|
|
611
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
3643
612
|
*/
|
|
3644
613
|
rangeByRank(start: number, end: number): K[] {
|
|
3645
614
|
return this.#core.rangeByRank(start, end).filter((k): k is K => k !== undefined);
|
|
3646
615
|
}
|
|
3647
616
|
|
|
3648
|
-
|
|
617
|
+
/**
|
|
3649
618
|
* Deep copy
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
|
|
3666
|
-
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
* @example
|
|
3675
|
-
* // Deep clone
|
|
3676
|
-
* const ms = new TreeMultiSet<number>();
|
|
3677
|
-
* ms.add(1, 3);
|
|
3678
|
-
* const copy = ms.clone();
|
|
3679
|
-
* copy.deleteAll(1);
|
|
3680
|
-
* console.log(ms.has(1)); // true;
|
|
619
|
+
* @example
|
|
620
|
+
* // Deep clone
|
|
621
|
+
* const ms = new TreeMultiSet<number>();
|
|
622
|
+
* ms.add(1, 3);
|
|
623
|
+
* const copy = ms.clone();
|
|
624
|
+
* copy.deleteAll(1);
|
|
625
|
+
* console.log(ms.has(1)); // true;
|
|
3681
626
|
*/
|
|
3682
627
|
clone(): TreeMultiSet<K> {
|
|
3683
628
|
const result = new TreeMultiSet<K>([], {
|
|
@@ -3690,166 +635,19 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
3690
635
|
return result;
|
|
3691
636
|
}
|
|
3692
637
|
|
|
3693
|
-
// ━━━ Tree utilities ━━━
|
|
3694
|
-
|
|
3695
638
|
/**
|
|
3696
639
|
* Returns keys within the given range.
|
|
3697
640
|
* @remarks Time O(log n + k), Space O(k) where k is result size
|
|
3698
|
-
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
|
|
3792
|
-
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
|
|
3799
|
-
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
-
|
|
3840
|
-
* @example
|
|
3841
|
-
* // Find in range
|
|
3842
|
-
* const ms = new TreeMultiSet<number>();
|
|
3843
|
-
* ms.add(10);
|
|
3844
|
-
* ms.add(20);
|
|
3845
|
-
* ms.add(30);
|
|
3846
|
-
* const result = ms.rangeSearch([15, 25]);
|
|
3847
|
-
* console.log(result.length); // 1;
|
|
3848
|
-
*/
|
|
3849
|
-
rangeSearch<C extends (key: K) => any>(
|
|
3850
|
-
range: [K, K],
|
|
3851
|
-
callback?: C
|
|
3852
|
-
): (C extends undefined ? K : ReturnType<C>)[] {
|
|
641
|
+
* @example
|
|
642
|
+
* // Find in range
|
|
643
|
+
* const ms = new TreeMultiSet<number>();
|
|
644
|
+
* ms.add(10);
|
|
645
|
+
* ms.add(20);
|
|
646
|
+
* ms.add(30);
|
|
647
|
+
* const result = ms.rangeSearch([15, 25]);
|
|
648
|
+
* console.log(result.length); // 1;
|
|
649
|
+
*/
|
|
650
|
+
rangeSearch<C extends (key: K) => any>(range: [K, K], callback?: C): (C extends undefined ? K : ReturnType<C>)[] {
|
|
3853
651
|
const cb = callback ?? ((k: K) => k);
|
|
3854
652
|
return this.#core.rangeSearch(range, node => cb(node.key));
|
|
3855
653
|
}
|
|
@@ -3857,192 +655,42 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
3857
655
|
/**
|
|
3858
656
|
* Prints the internal tree structure (for debugging).
|
|
3859
657
|
* @remarks Time O(n), Space O(n)
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
|
|
3903
|
-
|
|
3904
|
-
|
|
3905
|
-
|
|
3906
|
-
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
|
|
3989
|
-
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
|
|
4007
|
-
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
* @example
|
|
4040
|
-
* // Display
|
|
4041
|
-
* const ms = new TreeMultiSet<number>();
|
|
4042
|
-
* ms.add(1);
|
|
4043
|
-
* expect(() => ms.print()).not.toThrow();
|
|
658
|
+
* @example
|
|
659
|
+
* // Display
|
|
660
|
+
* const ms = new TreeMultiSet<number>();
|
|
661
|
+
* ms.add(1);
|
|
662
|
+
* expect(() => ms.print()).not.toThrow();
|
|
4044
663
|
*/
|
|
4045
664
|
print(): void {
|
|
4046
665
|
this.#core.print();
|
|
4047
666
|
}
|
|
667
|
+
|
|
668
|
+
// ━━━ Tree utilities ━━━
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Validates the key against the default comparator rules.
|
|
672
|
+
* @remarks Time O(1), Space O(1)
|
|
673
|
+
*/
|
|
674
|
+
private _validateKey(key: K): void {
|
|
675
|
+
if (!this.#isDefaultComparator) return;
|
|
676
|
+
if (typeof key === 'number') {
|
|
677
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN('TreeMultiSet'));
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if (typeof key === 'string') return;
|
|
681
|
+
if (key instanceof Date) {
|
|
682
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeMultiSet'));
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
raise(TypeError, ERR.comparatorRequired('TreeMultiSet'));
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Validates that count is a non-negative safe integer.
|
|
690
|
+
* @remarks Time O(1), Space O(1)
|
|
691
|
+
*/
|
|
692
|
+
private _validateCount(n: number): void {
|
|
693
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
694
|
+
raise(RangeError, ERR.invalidArgument('count must be a safe integer >= 0.', 'TreeMultiSet'));
|
|
695
|
+
}
|
|
4048
696
|
}
|