max-priority-queue-typed 2.4.4 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/README.md +63 -0
  2. package/dist/cjs/index.cjs +403 -98
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +402 -97
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +403 -99
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +402 -98
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/max-priority-queue-typed.js +400 -95
  46. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  47. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  48. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -9,6 +9,7 @@
9
9
  */
10
10
 
11
11
  import type { Comparator, TreeMultiSetOptions } from '../../types';
12
+ import { ERR } from '../../common';
12
13
  import { RedBlackTree } from './red-black-tree';
13
14
  import { TreeSet } from './tree-set';
14
15
 
@@ -50,18 +51,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
50
51
  if (!this.#isDefaultComparator) return;
51
52
 
52
53
  if (typeof key === 'number') {
53
- if (Number.isNaN(key)) throw new TypeError('TreeMultiSet: NaN is not a valid key');
54
+ if (Number.isNaN(key)) throw new TypeError(ERR.invalidNaN('TreeMultiSet'));
54
55
  return;
55
56
  }
56
57
 
57
58
  if (typeof key === 'string') return;
58
59
 
59
60
  if (key instanceof Date) {
60
- if (Number.isNaN(key.getTime())) throw new TypeError('TreeMultiSet: invalid Date key');
61
+ if (Number.isNaN(key.getTime())) throw new TypeError(ERR.invalidDate('TreeMultiSet'));
61
62
  return;
62
63
  }
63
64
 
64
- throw new TypeError('TreeMultiSet: comparator is required for non-number/non-string/non-Date keys');
65
+ throw new TypeError(ERR.comparatorRequired('TreeMultiSet'));
65
66
  }
66
67
 
67
68
  /**
@@ -69,7 +70,7 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
69
70
  * @remarks Time O(1), Space O(1)
70
71
  */
71
72
  private _validateCount(n: number): void {
72
- if (!Number.isSafeInteger(n) || n < 0) throw new RangeError('TreeMultiSet: count must be a safe integer >= 0');
73
+ if (!Number.isSafeInteger(n) || n < 0) throw new RangeError(ERR.invalidArgument('count must be a safe integer >= 0.', 'TreeMultiSet'));
73
74
  }
74
75
 
75
76
  /**
@@ -83,6 +84,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
83
84
  /**
84
85
  * Number of distinct keys.
85
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;
86
95
  */
87
96
  get distinctSize(): number {
88
97
  return this.#core.size;
@@ -91,6 +100,47 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
91
100
  /**
92
101
  * Whether the multiset is empty.
93
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;
94
144
  */
95
145
  isEmpty(): boolean {
96
146
  return this.size === 0;
@@ -99,6 +149,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
99
149
  /**
100
150
  * Whether the multiset contains the given key.
101
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;
102
199
  */
103
200
  has(key: K): boolean {
104
201
  this._validateKey(key);
@@ -108,6 +205,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
108
205
  /**
109
206
  * Returns the count of occurrences for the given key.
110
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;
111
215
  */
112
216
  count(key: K): number {
113
217
  this._validateKey(key);
@@ -118,6 +222,46 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
118
222
  * Add `n` occurrences of `key`.
119
223
  * @returns True if the multiset changed.
120
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;
121
265
  */
122
266
  add(key: K, n = 1): boolean {
123
267
  this._validateKey(key);
@@ -135,6 +279,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
135
279
  * Set count for `key` to exactly `n`.
136
280
  * @returns True if changed.
137
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;
138
289
  */
139
290
  setCount(key: K, n: number): boolean {
140
291
  this._validateKey(key);
@@ -157,6 +308,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
157
308
  * Delete `n` occurrences of `key` (default 1).
158
309
  * @returns True if any occurrence was removed.
159
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;
160
358
  */
161
359
  delete(key: K, n = 1): boolean {
162
360
  this._validateKey(key);
@@ -180,6 +378,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
180
378
  * Delete all occurrences of the given key.
181
379
  * @returns True if any occurrence was removed.
182
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;
183
389
  */
184
390
  deleteAll(key: K): boolean {
185
391
  this._validateKey(key);
@@ -193,6 +399,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
193
399
  /**
194
400
  * Iterates over distinct keys (each key yielded once).
195
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];
196
410
  */
197
411
  *keysDistinct(): IterableIterator<K> {
198
412
  yield* this.#core.keys();
@@ -201,6 +415,49 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
201
415
  /**
202
416
  * Iterates over entries as [key, count] pairs.
203
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;
204
461
  */
205
462
  *entries(): IterableIterator<[K, number]> {
206
463
  for (const [k, v] of this.#core) {
@@ -221,6 +478,50 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
221
478
  /**
222
479
  * Returns an array with all elements (expanded).
223
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];
224
525
  */
225
526
  toArray(): K[] {
226
527
  return [...this];
@@ -229,6 +530,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
229
530
  /**
230
531
  * Returns an array with distinct keys only.
231
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];
232
541
  */
233
542
  toDistinctArray(): K[] {
234
543
  return [...this.keysDistinct()];
@@ -237,6 +546,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
237
546
  /**
238
547
  * Returns an array of [key, count] entries.
239
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]];
240
557
  */
241
558
  toEntries(): Array<[K, number]> {
242
559
  return [...this.entries()];
@@ -255,10 +572,51 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
255
572
  /**
256
573
  * Remove all elements from the multiset.
257
574
  * @remarks Time O(1), Space O(1)
258
- * @example
259
- * const ms = new TreeMultiSet([1, 2, 2, 3]);
260
- * ms.clear();
261
- * ms.size; // 0
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;
262
620
  */
263
621
  clear(): void {
264
622
  this.#core.clear();
@@ -270,9 +628,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
270
628
  /**
271
629
  * Returns the smallest key, or undefined if empty.
272
630
  * @remarks Time O(log n), Space O(1)
273
- * @example
274
- * const ms = new TreeMultiSet([3, 1, 4]);
275
- * ms.first(); // 1
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;
276
640
  */
277
641
  first(): K | undefined {
278
642
  return this.#core.getLeftMost();
@@ -281,9 +645,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
281
645
  /**
282
646
  * Returns the largest key, or undefined if empty.
283
647
  * @remarks Time O(log n), Space O(1)
284
- * @example
285
- * const ms = new TreeMultiSet([3, 1, 4]);
286
- * ms.last(); // 4
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;
287
657
  */
288
658
  last(): K | undefined {
289
659
  return this.#core.getRightMost();
@@ -292,10 +662,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
292
662
  /**
293
663
  * Removes all occurrences of the smallest key and returns it.
294
664
  * @remarks Time O(log n), Space O(1)
295
- * @example
296
- * const ms = new TreeMultiSet([1, 1, 2, 3]);
297
- * ms.pollFirst(); // 1
298
- * ms.has(1); // false
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;
299
675
  */
300
676
  pollFirst(): K | undefined {
301
677
  const key = this.first();
@@ -307,10 +683,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
307
683
  /**
308
684
  * Removes all occurrences of the largest key and returns it.
309
685
  * @remarks Time O(log n), Space O(1)
310
- * @example
311
- * const ms = new TreeMultiSet([1, 2, 3, 3]);
312
- * ms.pollLast(); // 3
313
- * ms.has(3); // false
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;
314
695
  */
315
696
  pollLast(): K | undefined {
316
697
  const key = this.last();
@@ -322,10 +703,43 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
322
703
  /**
323
704
  * Returns the smallest key >= given key, or undefined.
324
705
  * @remarks Time O(log n), Space O(1)
325
- * @example
326
- * const ms = new TreeMultiSet([10, 20, 30]);
327
- * ms.ceiling(15); // 20
328
- * ms.ceiling(20); // 20
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;
329
743
  */
330
744
  ceiling(key: K): K | undefined {
331
745
  this._validateKey(key);
@@ -335,10 +749,43 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
335
749
  /**
336
750
  * Returns the largest key <= given key, or undefined.
337
751
  * @remarks Time O(log n), Space O(1)
338
- * @example
339
- * const ms = new TreeMultiSet([10, 20, 30]);
340
- * ms.floor(25); // 20
341
- * ms.floor(20); // 20
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;
342
789
  */
343
790
  floor(key: K): K | undefined {
344
791
  this._validateKey(key);
@@ -348,10 +795,42 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
348
795
  /**
349
796
  * Returns the smallest key > given key, or undefined.
350
797
  * @remarks Time O(log n), Space O(1)
351
- * @example
352
- * const ms = new TreeMultiSet([10, 20, 30]);
353
- * ms.higher(10); // 20
354
- * ms.higher(15); // 20
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;
355
834
  */
356
835
  higher(key: K): K | undefined {
357
836
  this._validateKey(key);
@@ -361,10 +840,42 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
361
840
  /**
362
841
  * Returns the largest key < given key, or undefined.
363
842
  * @remarks Time O(log n), Space O(1)
364
- * @example
365
- * const ms = new TreeMultiSet([10, 20, 30]);
366
- * ms.lower(20); // 10
367
- * ms.lower(15); // 10
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;
368
879
  */
369
880
  lower(key: K): K | undefined {
370
881
  this._validateKey(key);
@@ -376,10 +887,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
376
887
  /**
377
888
  * Iterates over distinct keys with their counts.
378
889
  * @remarks Time O(n), Space O(1)
379
- * @example
380
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
381
- * ms.forEach((key, count) => console.log(`${key}: ${count}`));
382
- * // 1: 2, 2: 1, 3: 3
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]];
383
937
  */
384
938
  forEach(callback: (key: K, count: number) => void): void {
385
939
  for (const [k, c] of this.entries()) {
@@ -390,10 +944,53 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
390
944
  /**
391
945
  * Creates a new TreeMultiSet with entries that match the predicate.
392
946
  * @remarks Time O(n log n), Space O(n)
393
- * @example
394
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
395
- * const filtered = ms.filter((key, count) => count >= 2);
396
- * // TreeMultiSet { 1: 2, 3: 3 }
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];
397
994
  */
398
995
  filter(predicate: (key: K, count: number) => boolean): TreeMultiSet<K> {
399
996
  const result = new TreeMultiSet<K>([], {
@@ -411,9 +1008,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
411
1008
  /**
412
1009
  * Reduces the multiset to a single value.
413
1010
  * @remarks Time O(n), Space O(1)
414
- * @example
415
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
416
- * const total = ms.reduce((acc, key, count) => acc + count, 0); // 6
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;
417
1057
  */
418
1058
  reduce<U>(callback: (accumulator: U, key: K, count: number) => U, initialValue: U): U {
419
1059
  let acc = initialValue;
@@ -427,15 +1067,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
427
1067
  * Maps keys and counts to a new TreeMultiSet.
428
1068
  * When multiple keys map to the same new key, counts are merged (added).
429
1069
  * @remarks Time O(n log n), Space O(n)
430
- * @example
431
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
432
- * const mapped = ms.map((key, count) => [key * 10, count]);
433
- * // TreeMultiSet { 10: 2, 20: 1, 30: 3 }
434
- * @example
435
- * // Collision: counts merge
436
- * const ms = new TreeMultiSet([1, 2, 3]);
437
- * const merged = ms.map((key, count) => [key % 2, count]);
438
- * // { 0: 1, 1: 2 } (1 and 3 both map to 1, counts add)
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];
439
1116
  */
440
1117
  map<K2>(
441
1118
  mapper: (key: K, count: number) => [K2, number],
@@ -455,11 +1132,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
455
1132
  /**
456
1133
  * Creates an independent copy of this multiset.
457
1134
  * @remarks Time O(n log n), Space O(n)
458
- * @example
459
- * const ms = new TreeMultiSet([1, 1, 2]);
460
- * const copy = ms.clone();
461
- * copy.add(3);
462
- * ms.has(3); // false (original unchanged)
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;
463
1181
  */
464
1182
  clone(): TreeMultiSet<K> {
465
1183
  const result = new TreeMultiSet<K>([], {
@@ -477,9 +1195,44 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
477
1195
  /**
478
1196
  * Returns keys within the given range.
479
1197
  * @remarks Time O(log n + k), Space O(k) where k is result size
480
- * @example
481
- * const ms = new TreeMultiSet([10, 20, 30, 40, 50]);
482
- * ms.rangeSearch([15, 45]); // [20, 30, 40]
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;
483
1236
  */
484
1237
  rangeSearch<C extends (key: K) => any>(
485
1238
  range: [K, K],
@@ -492,9 +1245,50 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
492
1245
  /**
493
1246
  * Prints the internal tree structure (for debugging).
494
1247
  * @remarks Time O(n), Space O(n)
495
- * @example
496
- * const ms = new TreeMultiSet([1, 2, 3]);
497
- * ms.print();
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();
498
1292
  */
499
1293
  print(): void {
500
1294
  this.#core.print();