max-priority-queue-typed 2.4.5 → 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 +400 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +399 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +400 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +399 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- 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 +429 -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 +212 -32
- 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/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -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 +272 -65
- 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/umd/max-priority-queue-typed.js +397 -116
- 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/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- 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 +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- 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 +272 -65
- 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 +213 -43
- 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
|
@@ -84,6 +84,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
84
84
|
/**
|
|
85
85
|
* Number of distinct keys.
|
|
86
86
|
* @remarks Time O(1), Space O(1)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
* @example
|
|
90
|
+
* // Unique key count
|
|
91
|
+
* const ms = new TreeMultiSet<number>();
|
|
92
|
+
* ms.add(1, 3);
|
|
93
|
+
* ms.add(2, 2);
|
|
94
|
+
* console.log(ms.distinctSize); // 2;
|
|
87
95
|
*/
|
|
88
96
|
get distinctSize(): number {
|
|
89
97
|
return this.#core.size;
|
|
@@ -92,6 +100,47 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
92
100
|
/**
|
|
93
101
|
* Whether the multiset is empty.
|
|
94
102
|
* @remarks Time O(1), Space O(1)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
* @example
|
|
142
|
+
* // Check empty
|
|
143
|
+
* console.log(new TreeMultiSet().isEmpty()); // true;
|
|
95
144
|
*/
|
|
96
145
|
isEmpty(): boolean {
|
|
97
146
|
return this.size === 0;
|
|
@@ -100,6 +149,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
100
149
|
/**
|
|
101
150
|
* Whether the multiset contains the given key.
|
|
102
151
|
* @remarks Time O(log n), Space O(1)
|
|
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
|
+
* @example
|
|
194
|
+
* // Check existence
|
|
195
|
+
* const ms = new TreeMultiSet<number>();
|
|
196
|
+
* ms.add(1);
|
|
197
|
+
* console.log(ms.has(1)); // true;
|
|
198
|
+
* console.log(ms.has(2)); // false;
|
|
103
199
|
*/
|
|
104
200
|
has(key: K): boolean {
|
|
105
201
|
this._validateKey(key);
|
|
@@ -109,6 +205,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
109
205
|
/**
|
|
110
206
|
* Returns the count of occurrences for the given key.
|
|
111
207
|
* @remarks Time O(log n), Space O(1)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
* @example
|
|
211
|
+
* // Get occurrence count
|
|
212
|
+
* const ms = new TreeMultiSet<number>();
|
|
213
|
+
* ms.add(1, 5);
|
|
214
|
+
* console.log(ms.count(1)); // 5;
|
|
112
215
|
*/
|
|
113
216
|
count(key: K): number {
|
|
114
217
|
this._validateKey(key);
|
|
@@ -119,6 +222,46 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
119
222
|
* Add `n` occurrences of `key`.
|
|
120
223
|
* @returns True if the multiset changed.
|
|
121
224
|
* @remarks Time O(log n), Space O(1)
|
|
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
|
+
* @example
|
|
258
|
+
* // Add elements
|
|
259
|
+
* const ms = new TreeMultiSet<number>();
|
|
260
|
+
* ms.add(1);
|
|
261
|
+
* ms.add(1);
|
|
262
|
+
* ms.add(2);
|
|
263
|
+
* console.log(ms.count(1)); // 2;
|
|
264
|
+
* console.log(ms.size); // 3;
|
|
122
265
|
*/
|
|
123
266
|
add(key: K, n = 1): boolean {
|
|
124
267
|
this._validateKey(key);
|
|
@@ -136,6 +279,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
136
279
|
* Set count for `key` to exactly `n`.
|
|
137
280
|
* @returns True if changed.
|
|
138
281
|
* @remarks Time O(log n), Space O(1)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
* @example
|
|
285
|
+
* // Set occurrence count
|
|
286
|
+
* const ms = new TreeMultiSet<number>();
|
|
287
|
+
* ms.setCount(1, 3);
|
|
288
|
+
* console.log(ms.count(1)); // 3;
|
|
139
289
|
*/
|
|
140
290
|
setCount(key: K, n: number): boolean {
|
|
141
291
|
this._validateKey(key);
|
|
@@ -158,6 +308,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
158
308
|
* Delete `n` occurrences of `key` (default 1).
|
|
159
309
|
* @returns True if any occurrence was removed.
|
|
160
310
|
* @remarks Time O(log n), Space O(1)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
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
|
+
* @example
|
|
353
|
+
* // Remove one occurrence
|
|
354
|
+
* const ms = new TreeMultiSet<number>();
|
|
355
|
+
* ms.add(1, 3);
|
|
356
|
+
* ms.delete(1);
|
|
357
|
+
* console.log(ms.count(1)); // 2;
|
|
161
358
|
*/
|
|
162
359
|
delete(key: K, n = 1): boolean {
|
|
163
360
|
this._validateKey(key);
|
|
@@ -181,6 +378,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
181
378
|
* Delete all occurrences of the given key.
|
|
182
379
|
* @returns True if any occurrence was removed.
|
|
183
380
|
* @remarks Time O(log n), Space O(1)
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
* @example
|
|
384
|
+
* // Remove all occurrences
|
|
385
|
+
* const ms = new TreeMultiSet<number>();
|
|
386
|
+
* ms.add(1, 3);
|
|
387
|
+
* ms.deleteAll(1);
|
|
388
|
+
* console.log(ms.has(1)); // false;
|
|
184
389
|
*/
|
|
185
390
|
deleteAll(key: K): boolean {
|
|
186
391
|
this._validateKey(key);
|
|
@@ -194,6 +399,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
194
399
|
/**
|
|
195
400
|
* Iterates over distinct keys (each key yielded once).
|
|
196
401
|
* @remarks Time O(n), Space O(1)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
* @example
|
|
405
|
+
* // Iterate unique keys
|
|
406
|
+
* const ms = new TreeMultiSet<number>();
|
|
407
|
+
* ms.add(1, 2);
|
|
408
|
+
* ms.add(2);
|
|
409
|
+
* console.log([...ms.keysDistinct()]); // [1, 2];
|
|
197
410
|
*/
|
|
198
411
|
*keysDistinct(): IterableIterator<K> {
|
|
199
412
|
yield* this.#core.keys();
|
|
@@ -202,6 +415,49 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
202
415
|
/**
|
|
203
416
|
* Iterates over entries as [key, count] pairs.
|
|
204
417
|
* @remarks Time O(n), Space O(1)
|
|
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
|
+
* @example
|
|
457
|
+
* // Iterate entries
|
|
458
|
+
* const ms = new TreeMultiSet<number>();
|
|
459
|
+
* ms.add(1, 2);
|
|
460
|
+
* console.log([...ms.entries()].length); // > 0;
|
|
205
461
|
*/
|
|
206
462
|
*entries(): IterableIterator<[K, number]> {
|
|
207
463
|
for (const [k, v] of this.#core) {
|
|
@@ -222,6 +478,50 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
222
478
|
/**
|
|
223
479
|
* Returns an array with all elements (expanded).
|
|
224
480
|
* @remarks Time O(size), Space O(size)
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
* @example
|
|
520
|
+
* // All elements (with duplicates)
|
|
521
|
+
* const ms = new TreeMultiSet<number>();
|
|
522
|
+
* ms.add(1, 2);
|
|
523
|
+
* ms.add(2);
|
|
524
|
+
* console.log(ms.toArray()); // [1, 1, 2];
|
|
225
525
|
*/
|
|
226
526
|
toArray(): K[] {
|
|
227
527
|
return [...this];
|
|
@@ -230,6 +530,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
230
530
|
/**
|
|
231
531
|
* Returns an array with distinct keys only.
|
|
232
532
|
* @remarks Time O(n), Space O(n)
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
* @example
|
|
536
|
+
* // Unique keys only
|
|
537
|
+
* const ms = new TreeMultiSet<number>();
|
|
538
|
+
* ms.add(1, 3);
|
|
539
|
+
* ms.add(2);
|
|
540
|
+
* console.log(ms.toDistinctArray()); // [1, 2];
|
|
233
541
|
*/
|
|
234
542
|
toDistinctArray(): K[] {
|
|
235
543
|
return [...this.keysDistinct()];
|
|
@@ -238,6 +546,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
238
546
|
/**
|
|
239
547
|
* Returns an array of [key, count] entries.
|
|
240
548
|
* @remarks Time O(n), Space O(n)
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
* @example
|
|
552
|
+
* // Key-count pairs
|
|
553
|
+
* const ms = new TreeMultiSet<number>();
|
|
554
|
+
* ms.add(1, 2);
|
|
555
|
+
* ms.add(3);
|
|
556
|
+
* console.log(ms.toEntries()); // [[1, 2], [3, 1]];
|
|
241
557
|
*/
|
|
242
558
|
toEntries(): Array<[K, number]> {
|
|
243
559
|
return [...this.entries()];
|
|
@@ -256,10 +572,51 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
256
572
|
/**
|
|
257
573
|
* Remove all elements from the multiset.
|
|
258
574
|
* @remarks Time O(1), Space O(1)
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
+
* @example
|
|
615
|
+
* // Remove all
|
|
616
|
+
* const ms = new TreeMultiSet<number>();
|
|
617
|
+
* ms.add(1);
|
|
618
|
+
* ms.clear();
|
|
619
|
+
* console.log(ms.isEmpty()); // true;
|
|
263
620
|
*/
|
|
264
621
|
clear(): void {
|
|
265
622
|
this.#core.clear();
|
|
@@ -271,9 +628,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
271
628
|
/**
|
|
272
629
|
* Returns the smallest key, or undefined if empty.
|
|
273
630
|
* @remarks Time O(log n), Space O(1)
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
* @example
|
|
635
|
+
* // Smallest element
|
|
636
|
+
* const ms = new TreeMultiSet<number>();
|
|
637
|
+
* ms.add(3);
|
|
638
|
+
* ms.add(1);
|
|
639
|
+
* console.log(ms.first()); // 1;
|
|
277
640
|
*/
|
|
278
641
|
first(): K | undefined {
|
|
279
642
|
return this.#core.getLeftMost();
|
|
@@ -282,9 +645,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
282
645
|
/**
|
|
283
646
|
* Returns the largest key, or undefined if empty.
|
|
284
647
|
* @remarks Time O(log n), Space O(1)
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
* @example
|
|
652
|
+
* // Largest element
|
|
653
|
+
* const ms = new TreeMultiSet<number>();
|
|
654
|
+
* ms.add(1);
|
|
655
|
+
* ms.add(3);
|
|
656
|
+
* console.log(ms.last()); // 3;
|
|
288
657
|
*/
|
|
289
658
|
last(): K | undefined {
|
|
290
659
|
return this.#core.getRightMost();
|
|
@@ -293,10 +662,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
293
662
|
/**
|
|
294
663
|
* Removes all occurrences of the smallest key and returns it.
|
|
295
664
|
* @remarks Time O(log n), Space O(1)
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
* @example
|
|
669
|
+
* // Remove and return smallest
|
|
670
|
+
* const ms = new TreeMultiSet<number>();
|
|
671
|
+
* ms.add(2);
|
|
672
|
+
* ms.add(1);
|
|
673
|
+
* console.log(ms.pollFirst()); // 1;
|
|
674
|
+
* console.log(ms.has(1)); // false;
|
|
300
675
|
*/
|
|
301
676
|
pollFirst(): K | undefined {
|
|
302
677
|
const key = this.first();
|
|
@@ -308,10 +683,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
308
683
|
/**
|
|
309
684
|
* Removes all occurrences of the largest key and returns it.
|
|
310
685
|
* @remarks Time O(log n), Space O(1)
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
* @example
|
|
690
|
+
* // Remove and return largest
|
|
691
|
+
* const ms = new TreeMultiSet<number>();
|
|
692
|
+
* ms.add(1);
|
|
693
|
+
* ms.add(3);
|
|
694
|
+
* console.log(ms.pollLast()); // 3;
|
|
315
695
|
*/
|
|
316
696
|
pollLast(): K | undefined {
|
|
317
697
|
const key = this.last();
|
|
@@ -323,10 +703,43 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
323
703
|
/**
|
|
324
704
|
* Returns the smallest key >= given key, or undefined.
|
|
325
705
|
* @remarks Time O(log n), Space O(1)
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
* @example
|
|
737
|
+
* // Least key ≥ target
|
|
738
|
+
* const ms = new TreeMultiSet<number>();
|
|
739
|
+
* ms.add(10);
|
|
740
|
+
* ms.add(20);
|
|
741
|
+
* ms.add(30);
|
|
742
|
+
* console.log(ms.ceiling(15)); // 20;
|
|
330
743
|
*/
|
|
331
744
|
ceiling(key: K): K | undefined {
|
|
332
745
|
this._validateKey(key);
|
|
@@ -336,10 +749,43 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
336
749
|
/**
|
|
337
750
|
* Returns the largest key <= given key, or undefined.
|
|
338
751
|
* @remarks Time O(log n), Space O(1)
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
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
|
+
* @example
|
|
783
|
+
* // Greatest key ≤ target
|
|
784
|
+
* const ms = new TreeMultiSet<number>();
|
|
785
|
+
* ms.add(10);
|
|
786
|
+
* ms.add(20);
|
|
787
|
+
* ms.add(30);
|
|
788
|
+
* console.log(ms.floor(25)); // 20;
|
|
343
789
|
*/
|
|
344
790
|
floor(key: K): K | undefined {
|
|
345
791
|
this._validateKey(key);
|
|
@@ -349,10 +795,42 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
349
795
|
/**
|
|
350
796
|
* Returns the smallest key > given key, or undefined.
|
|
351
797
|
* @remarks Time O(log n), Space O(1)
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
* @example
|
|
829
|
+
* // Least key > target
|
|
830
|
+
* const ms = new TreeMultiSet<number>();
|
|
831
|
+
* ms.add(10);
|
|
832
|
+
* ms.add(20);
|
|
833
|
+
* console.log(ms.higher(10)); // 20;
|
|
356
834
|
*/
|
|
357
835
|
higher(key: K): K | undefined {
|
|
358
836
|
this._validateKey(key);
|
|
@@ -362,10 +840,42 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
362
840
|
/**
|
|
363
841
|
* Returns the largest key < given key, or undefined.
|
|
364
842
|
* @remarks Time O(log n), Space O(1)
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
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
|
+
* @example
|
|
874
|
+
* // Greatest key < target
|
|
875
|
+
* const ms = new TreeMultiSet<number>();
|
|
876
|
+
* ms.add(10);
|
|
877
|
+
* ms.add(20);
|
|
878
|
+
* console.log(ms.lower(20)); // 10;
|
|
369
879
|
*/
|
|
370
880
|
lower(key: K): K | undefined {
|
|
371
881
|
this._validateKey(key);
|
|
@@ -377,10 +887,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
377
887
|
/**
|
|
378
888
|
* Iterates over distinct keys with their counts.
|
|
379
889
|
* @remarks Time O(n), Space O(1)
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
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
|
+
* @example
|
|
930
|
+
* // Iterate
|
|
931
|
+
* const ms = new TreeMultiSet<number>();
|
|
932
|
+
* ms.add(1, 2);
|
|
933
|
+
* ms.add(2);
|
|
934
|
+
* const pairs: [number, number][] = [];
|
|
935
|
+
* ms.forEach((k, c) => pairs.push([k, c]));
|
|
936
|
+
* console.log(pairs); // [[1, 2], [2, 1]];
|
|
384
937
|
*/
|
|
385
938
|
forEach(callback: (key: K, count: number) => void): void {
|
|
386
939
|
for (const [k, c] of this.entries()) {
|
|
@@ -391,10 +944,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
391
944
|
/**
|
|
392
945
|
* Creates a new TreeMultiSet with entries that match the predicate.
|
|
393
946
|
* @remarks Time O(n log n), Space O(n)
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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
|
+
* @example
|
|
987
|
+
* // Filter
|
|
988
|
+
* const ms = new TreeMultiSet<number>();
|
|
989
|
+
* ms.add(1, 3);
|
|
990
|
+
* ms.add(2, 1);
|
|
991
|
+
* ms.add(3, 2);
|
|
992
|
+
* const filtered = ms.filter((k, c) => c > 1);
|
|
993
|
+
* console.log([...filtered.keysDistinct()]); // [1, 3];
|
|
398
994
|
*/
|
|
399
995
|
filter(predicate: (key: K, count: number) => boolean): TreeMultiSet<K> {
|
|
400
996
|
const result = new TreeMultiSet<K>([], {
|
|
@@ -412,9 +1008,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
412
1008
|
/**
|
|
413
1009
|
* Reduces the multiset to a single value.
|
|
414
1010
|
* @remarks Time O(n), Space O(1)
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
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
|
+
* @example
|
|
1051
|
+
* // Aggregate
|
|
1052
|
+
* const ms = new TreeMultiSet<number>();
|
|
1053
|
+
* ms.add(1, 2);
|
|
1054
|
+
* ms.add(2, 3);
|
|
1055
|
+
* const sum = ms.reduce((acc, k, c) => acc + k * c, 0);
|
|
1056
|
+
* console.log(sum); // 8;
|
|
418
1057
|
*/
|
|
419
1058
|
reduce<U>(callback: (accumulator: U, key: K, count: number) => U, initialValue: U): U {
|
|
420
1059
|
let acc = initialValue;
|
|
@@ -428,15 +1067,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
428
1067
|
* Maps keys and counts to a new TreeMultiSet.
|
|
429
1068
|
* When multiple keys map to the same new key, counts are merged (added).
|
|
430
1069
|
* @remarks Time O(n log n), Space O(n)
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
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
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
* @example
|
|
1110
|
+
* // Transform
|
|
1111
|
+
* const ms = new TreeMultiSet<number>();
|
|
1112
|
+
* ms.add(1, 2);
|
|
1113
|
+
* ms.add(2, 3);
|
|
1114
|
+
* const doubled = ms.map((k, c) => [k * 10, c] as [number, number]);
|
|
1115
|
+
* console.log([...doubled.keysDistinct()]); // [10, 20];
|
|
440
1116
|
*/
|
|
441
1117
|
map<K2>(
|
|
442
1118
|
mapper: (key: K, count: number) => [K2, number],
|
|
@@ -456,11 +1132,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
456
1132
|
/**
|
|
457
1133
|
* Creates an independent copy of this multiset.
|
|
458
1134
|
* @remarks Time O(n log n), Space O(n)
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
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
|
+
* @example
|
|
1175
|
+
* // Deep clone
|
|
1176
|
+
* const ms = new TreeMultiSet<number>();
|
|
1177
|
+
* ms.add(1, 3);
|
|
1178
|
+
* const copy = ms.clone();
|
|
1179
|
+
* copy.deleteAll(1);
|
|
1180
|
+
* console.log(ms.has(1)); // true;
|
|
464
1181
|
*/
|
|
465
1182
|
clone(): TreeMultiSet<K> {
|
|
466
1183
|
const result = new TreeMultiSet<K>([], {
|
|
@@ -478,9 +1195,44 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
478
1195
|
/**
|
|
479
1196
|
* Returns keys within the given range.
|
|
480
1197
|
* @remarks Time O(log n + k), Space O(k) where k is result size
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
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
|
+
* @example
|
|
1229
|
+
* // Find in range
|
|
1230
|
+
* const ms = new TreeMultiSet<number>();
|
|
1231
|
+
* ms.add(10);
|
|
1232
|
+
* ms.add(20);
|
|
1233
|
+
* ms.add(30);
|
|
1234
|
+
* const result = ms.rangeSearch([15, 25]);
|
|
1235
|
+
* console.log(result.length); // 1;
|
|
484
1236
|
*/
|
|
485
1237
|
rangeSearch<C extends (key: K) => any>(
|
|
486
1238
|
range: [K, K],
|
|
@@ -493,9 +1245,50 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
|
|
|
493
1245
|
/**
|
|
494
1246
|
* Prints the internal tree structure (for debugging).
|
|
495
1247
|
* @remarks Time O(n), Space O(n)
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
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
|
+
* @example
|
|
1288
|
+
* // Display
|
|
1289
|
+
* const ms = new TreeMultiSet<number>();
|
|
1290
|
+
* ms.add(1);
|
|
1291
|
+
* expect(() => ms.print()).not.toThrow();
|
|
499
1292
|
*/
|
|
500
1293
|
print(): void {
|
|
501
1294
|
this.#core.print();
|