data-structure-typed 2.6.0 → 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.
Files changed (80) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
  4. package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
  5. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
  6. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
  7. package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
  8. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
  9. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
  10. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  11. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  12. package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
  13. package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
  14. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
  15. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
  16. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
  17. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
  18. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
  19. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
  20. package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
  21. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
  22. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
  23. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
  24. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
  25. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
  26. package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
  27. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
  28. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
  29. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
  30. package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
  31. package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
  32. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
  33. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
  34. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
  35. package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
  36. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
  37. package/package.json +45 -46
  38. package/src/common/error.ts +15 -32
  39. package/src/common/index.ts +0 -3
  40. package/src/data-structures/base/iterable-element-base.ts +0 -3
  41. package/src/data-structures/base/linear-base.ts +2 -36
  42. package/src/data-structures/binary-tree/avl-tree.ts +31 -529
  43. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
  44. package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
  45. package/src/data-structures/binary-tree/bst.ts +158 -1082
  46. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
  47. package/src/data-structures/binary-tree/segment-tree.ts +73 -351
  48. package/src/data-structures/binary-tree/tree-map.ts +462 -5124
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
  50. package/src/data-structures/binary-tree/tree-multi-set.ts +284 -3972
  51. package/src/data-structures/binary-tree/tree-set.ts +338 -4836
  52. package/src/data-structures/graph/abstract-graph.ts +98 -167
  53. package/src/data-structures/graph/directed-graph.ts +137 -562
  54. package/src/data-structures/graph/map-graph.ts +0 -3
  55. package/src/data-structures/graph/undirected-graph.ts +132 -511
  56. package/src/data-structures/hash/hash-map.ts +154 -582
  57. package/src/data-structures/heap/heap.ts +200 -795
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
  59. package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
  60. package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
  61. package/src/data-structures/matrix/matrix.ts +179 -518
  62. package/src/data-structures/matrix/navigator.ts +0 -1
  63. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  64. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  65. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  66. package/src/data-structures/queue/deque.ts +214 -882
  67. package/src/data-structures/queue/queue.ts +102 -625
  68. package/src/data-structures/stack/stack.ts +76 -505
  69. package/src/data-structures/trie/trie.ts +98 -628
  70. package/src/types/common.ts +0 -10
  71. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  72. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  73. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  74. package/src/types/data-structures/hash/hash-map.ts +0 -3
  75. package/src/types/data-structures/hash/index.ts +0 -1
  76. package/src/types/data-structures/matrix/navigator.ts +0 -2
  77. package/src/types/utils/utils.ts +0 -7
  78. package/src/types/utils/validate-type.ts +0 -7
  79. package/src/utils/number.ts +0 -2
  80. 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>([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
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,249 +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
-
118
-
119
-
120
- * @example
121
- * // Unique key count
122
- * const ms = new TreeMultiSet<number>();
123
- * ms.add(1, 3);
124
- * ms.add(2, 2);
125
- * 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;
126
66
  */
127
67
  get distinctSize(): number {
128
68
  return this.#core.size;
129
69
  }
130
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
+
131
79
  /**
132
80
  * Whether the multiset is empty.
133
81
  * @remarks Time O(1), Space O(1)
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
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
- * @example
328
- * // Check empty
329
- * console.log(new TreeMultiSet().isEmpty()); // true;
82
+ * @example
83
+ * // Check empty
84
+ * console.log(new TreeMultiSet().isEmpty()); // true;
330
85
  */
331
86
  isEmpty(): boolean {
332
87
  return this.size === 0;
@@ -335,208 +90,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
335
90
  /**
336
91
  * Whether the multiset contains the given key.
337
92
  * @remarks Time O(log n), Space O(1)
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
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
- * @example
535
- * // Check existence
536
- * const ms = new TreeMultiSet<number>();
537
- * ms.add(1);
538
- * console.log(ms.has(1)); // true;
539
- * 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;
540
99
  */
541
100
  has(key: K): boolean {
542
101
  this._validateKey(key);
@@ -546,44 +105,11 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
546
105
  /**
547
106
  * Returns the count of occurrences for the given key.
548
107
  * @remarks Time O(log n), Space O(1)
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
- * @example
583
- * // Get occurrence count
584
- * const ms = new TreeMultiSet<number>();
585
- * ms.add(1, 5);
586
- * 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;
587
113
  */
588
114
  count(key: K): number {
589
115
  this._validateKey(key);
@@ -594,207 +120,19 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
594
120
  * Add `n` occurrences of `key`.
595
121
  * @returns True if the multiset changed.
596
122
  * @remarks Time O(log n), Space O(1)
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
-
734
-
735
-
736
-
737
-
738
-
739
-
740
-
741
-
742
-
743
-
744
-
745
-
746
-
747
-
748
-
749
-
750
-
751
-
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
-
783
-
784
- * @example
785
- * // Add elements
786
- * const ms = new TreeMultiSet<number>();
787
- * ms.add(1);
788
- * ms.add(1);
789
- * ms.add(2);
790
- * console.log(ms.count(1)); // 2;
791
- * 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;
792
131
  */
793
132
  add(key: K, n = 1): boolean {
794
133
  this._validateKey(key);
795
134
  this._validateCount(n);
796
135
  if (n === 0) return false;
797
-
798
136
  const old = this.#core.get(key) ?? 0;
799
137
  const next = old + n;
800
138
  this.#core.set(key, next);
@@ -806,58 +144,22 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
806
144
  * Set count for `key` to exactly `n`.
807
145
  * @returns True if changed.
808
146
  * @remarks Time O(log n), Space O(1)
809
-
810
-
811
-
812
-
813
-
814
-
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
- * @example
843
- * // Set occurrence count
844
- * const ms = new TreeMultiSet<number>();
845
- * ms.setCount(1, 3);
846
- * 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;
847
152
  */
848
153
  setCount(key: K, n: number): boolean {
849
154
  this._validateKey(key);
850
155
  this._validateCount(n);
851
-
852
156
  const old = this.#core.get(key) ?? 0;
853
157
  if (old === n) return false;
854
-
855
158
  if (n === 0) {
856
159
  if (old !== 0) this.#core.delete(key);
857
160
  } else {
858
161
  this.#core.set(key, n);
859
162
  }
860
-
861
163
  this._size += n - old;
862
164
  return true;
863
165
  }
@@ -866,223 +168,23 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
866
168
  * Delete `n` occurrences of `key` (default 1).
867
169
  * @returns True if any occurrence was removed.
868
170
  * @remarks Time O(log n), Space O(1)
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
-
997
-
998
-
999
-
1000
-
1001
-
1002
-
1003
-
1004
-
1005
-
1006
-
1007
-
1008
-
1009
-
1010
-
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
-
1051
-
1052
-
1053
-
1054
-
1055
-
1056
-
1057
-
1058
-
1059
-
1060
-
1061
-
1062
-
1063
-
1064
-
1065
- * @example
1066
- * // Remove one occurrence
1067
- * const ms = new TreeMultiSet<number>();
1068
- * ms.add(1, 3);
1069
- * ms.delete(1);
1070
- * 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;
1071
177
  */
1072
178
  delete(key: K, n = 1): boolean {
1073
179
  this._validateKey(key);
1074
180
  this._validateCount(n);
1075
181
  if (n === 0) return false;
1076
-
1077
182
  const old = this.#core.get(key) ?? 0;
1078
183
  if (old === 0) return false;
1079
-
1080
184
  const removed = Math.min(old, n);
1081
185
  const next = old - removed;
1082
-
1083
186
  if (next === 0) this.#core.delete(key);
1084
187
  else this.#core.set(key, next);
1085
-
1086
188
  this._size -= removed;
1087
189
  return true;
1088
190
  }
@@ -1091,45 +193,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1091
193
  * Delete all occurrences of the given key.
1092
194
  * @returns True if any occurrence was removed.
1093
195
  * @remarks Time O(log n), Space O(1)
1094
-
1095
-
1096
-
1097
-
1098
-
1099
-
1100
-
1101
-
1102
-
1103
-
1104
-
1105
-
1106
-
1107
-
1108
-
1109
-
1110
-
1111
-
1112
-
1113
-
1114
-
1115
-
1116
-
1117
-
1118
-
1119
-
1120
-
1121
-
1122
-
1123
-
1124
-
1125
-
1126
-
1127
- * @example
1128
- * // Remove all occurrences
1129
- * const ms = new TreeMultiSet<number>();
1130
- * ms.add(1, 3);
1131
- * ms.deleteAll(1);
1132
- * 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;
1133
202
  */
1134
203
  deleteAll(key: K): boolean {
1135
204
  this._validateKey(key);
@@ -1143,45 +212,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1143
212
  /**
1144
213
  * Iterates over distinct keys (each key yielded once).
1145
214
  * @remarks Time O(n), Space O(1)
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
- * @example
1180
- * // Iterate unique keys
1181
- * const ms = new TreeMultiSet<number>();
1182
- * ms.add(1, 2);
1183
- * ms.add(2);
1184
- * 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];
1185
221
  */
1186
222
  *keysDistinct(): IterableIterator<K> {
1187
223
  yield* this.#core.keys();
@@ -1190,204 +226,11 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1190
226
  /**
1191
227
  * Iterates over entries as [key, count] pairs.
1192
228
  * @remarks Time O(n), Space O(1)
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
-
1297
-
1298
-
1299
-
1300
-
1301
-
1302
-
1303
-
1304
-
1305
-
1306
-
1307
-
1308
-
1309
-
1310
-
1311
-
1312
-
1313
-
1314
-
1315
-
1316
-
1317
-
1318
-
1319
-
1320
-
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
- * @example
1387
- * // Iterate entries
1388
- * const ms = new TreeMultiSet<number>();
1389
- * ms.add(1, 2);
1390
- * 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;
1391
234
  */
1392
235
  *entries(): IterableIterator<[K, number]> {
1393
236
  for (const [k, v] of this.#core) {
@@ -1408,19 +251,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1408
251
  /**
1409
252
  * Iterate over all elements with multiplicity (Set-compatible, alias for `[Symbol.iterator]`).
1410
253
  * @remarks Each key is yielded `count(key)` times. Time O(size), Space O(1) per step.
1411
-
1412
-
1413
-
1414
-
1415
-
1416
-
1417
-
1418
-
1419
- * @example
1420
- * // Iterate with multiplicity
1421
- * const ms = new TreeMultiSet<number>();
1422
- * ms.add(1); ms.add(1); ms.add(2); ms.add(3); ms.add(3); ms.add(3);
1423
- * console.log([...ms.keys()]); // [1, 1, 2, 3, 3, 3];
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];
1424
264
  */
1425
265
  *keys(): IterableIterator<K> {
1426
266
  yield* this;
@@ -1429,19 +269,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1429
269
  /**
1430
270
  * Iterate over all elements with multiplicity (Set-compatible, alias for `[Symbol.iterator]`).
1431
271
  * @remarks Each key is yielded `count(key)` times. Time O(size), Space O(1) per step.
1432
-
1433
-
1434
-
1435
-
1436
-
1437
-
1438
-
1439
-
1440
- * @example
1441
- * // Iterate with multiplicity
1442
- * const ms = new TreeMultiSet<number>();
1443
- * ms.add(5); ms.add(5); ms.add(10);
1444
- * console.log([...ms.values()]); // [5, 5, 10];
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];
1445
279
  */
1446
280
  *values(): IterableIterator<K> {
1447
281
  yield* this;
@@ -1450,205 +284,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1450
284
  /**
1451
285
  * Returns an array with all elements (expanded).
1452
286
  * @remarks Time O(size), Space O(size)
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
-
1500
-
1501
-
1502
-
1503
-
1504
-
1505
-
1506
-
1507
-
1508
-
1509
-
1510
-
1511
-
1512
-
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
-
1544
-
1545
-
1546
-
1547
-
1548
-
1549
-
1550
-
1551
-
1552
-
1553
-
1554
-
1555
-
1556
-
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
-
1588
-
1589
-
1590
-
1591
-
1592
-
1593
-
1594
-
1595
-
1596
-
1597
-
1598
-
1599
-
1600
-
1601
-
1602
-
1603
-
1604
-
1605
-
1606
-
1607
-
1608
-
1609
-
1610
-
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
- * @example
1647
- * // All elements (with duplicates)
1648
- * const ms = new TreeMultiSet<number>();
1649
- * ms.add(1, 2);
1650
- * ms.add(2);
1651
- * 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];
1652
293
  */
1653
294
  toArray(): K[] {
1654
295
  return [...this];
@@ -1657,45 +298,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1657
298
  /**
1658
299
  * Returns an array with distinct keys only.
1659
300
  * @remarks Time O(n), Space O(n)
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
- * @example
1694
- * // Unique keys only
1695
- * const ms = new TreeMultiSet<number>();
1696
- * ms.add(1, 3);
1697
- * ms.add(2);
1698
- * 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];
1699
307
  */
1700
308
  toDistinctArray(): K[] {
1701
309
  return [...this.keysDistinct()];
@@ -1704,411 +312,77 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
1704
312
  /**
1705
313
  * Returns an array of [key, count] entries.
1706
314
  * @remarks Time O(n), Space O(n)
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
- * @example
1741
- * // Key-count pairs
1742
- * const ms = new TreeMultiSet<number>();
1743
- * ms.add(1, 2);
1744
- * ms.add(3);
1745
- * 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
+ * // ];
1746
324
  */
1747
325
  toEntries(): Array<[K, number]> {
1748
326
  return [...this.entries()];
1749
327
  }
1750
328
 
1751
- /**
1752
- * Expose comparator for advanced usage/testing (read-only).
1753
- * @remarks Time O(1), Space O(1)
1754
- */
1755
- get comparator(): Comparator<K> {
1756
- return this.#core.comparator;
1757
- }
1758
-
1759
- // ━━━ clear ━━━
1760
-
1761
329
  /**
1762
330
  * Remove all elements from the multiset.
1763
331
  * @remarks Time O(1), Space O(1)
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
-
1791
-
1792
-
1793
-
1794
-
1795
-
1796
-
1797
-
1798
-
1799
-
1800
-
1801
-
1802
-
1803
-
1804
-
1805
-
1806
-
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
-
1839
-
1840
-
1841
-
1842
-
1843
-
1844
-
1845
-
1846
-
1847
-
1848
-
1849
-
1850
-
1851
-
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
-
1884
-
1885
-
1886
-
1887
-
1888
-
1889
-
1890
-
1891
-
1892
-
1893
-
1894
-
1895
-
1896
-
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
-
1929
-
1930
-
1931
-
1932
-
1933
-
1934
-
1935
-
1936
-
1937
-
1938
-
1939
-
1940
-
1941
-
1942
-
1943
-
1944
-
1945
-
1946
-
1947
-
1948
-
1949
-
1950
-
1951
-
1952
-
1953
-
1954
-
1955
-
1956
-
1957
-
1958
- * @example
1959
- * // Remove all
1960
- * const ms = new TreeMultiSet<number>();
1961
- * ms.add(1);
1962
- * ms.clear();
1963
- * 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;
1964
338
  */
1965
339
  clear(): void {
1966
340
  this.#core.clear();
1967
341
  this._size = 0;
1968
342
  }
1969
343
 
1970
- // ━━━ Navigable methods ━━━
1971
-
1972
344
  /**
1973
345
  * Returns the smallest key, or undefined if empty.
1974
346
  * @remarks Time O(log n), Space O(1)
1975
-
1976
-
1977
-
1978
-
1979
-
1980
-
1981
-
1982
-
1983
-
1984
-
1985
-
1986
-
1987
-
1988
-
1989
-
1990
-
1991
-
1992
-
1993
-
1994
-
1995
-
1996
-
1997
-
1998
-
1999
-
2000
-
2001
-
2002
-
2003
-
2004
-
2005
-
2006
-
2007
-
2008
-
2009
- * @example
2010
- * // Smallest element
2011
- * const ms = new TreeMultiSet<number>();
2012
- * ms.add(3);
2013
- * ms.add(1);
2014
- * 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;
2015
353
  */
2016
354
  first(): K | undefined {
2017
355
  return this.#core.getLeftMost();
2018
356
  }
2019
357
 
358
+ // ━━━ clear ━━━
359
+
2020
360
  /**
2021
361
  * Returns the largest key, or undefined if empty.
2022
362
  * @remarks Time O(log n), Space O(1)
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
- * @example
2058
- * // Largest element
2059
- * const ms = new TreeMultiSet<number>();
2060
- * ms.add(1);
2061
- * ms.add(3);
2062
- * 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;
2063
369
  */
2064
370
  last(): K | undefined {
2065
371
  return this.#core.getRightMost();
2066
372
  }
2067
373
 
374
+ // ━━━ Navigable methods ━━━
375
+
2068
376
  /**
2069
377
  * Removes all occurrences of the smallest key and returns it.
2070
378
  * @remarks Time O(log n), Space O(1)
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
- * @example
2106
- * // Remove and return smallest
2107
- * const ms = new TreeMultiSet<number>();
2108
- * ms.add(2);
2109
- * ms.add(1);
2110
- * console.log(ms.pollFirst()); // 1;
2111
- * 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;
2112
386
  */
2113
387
  pollFirst(): K | undefined {
2114
388
  const key = this.first();
@@ -2120,46 +394,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2120
394
  /**
2121
395
  * Removes all occurrences of the largest key and returns it.
2122
396
  * @remarks Time O(log n), Space O(1)
2123
-
2124
-
2125
-
2126
-
2127
-
2128
-
2129
-
2130
-
2131
-
2132
-
2133
-
2134
-
2135
-
2136
-
2137
-
2138
-
2139
-
2140
-
2141
-
2142
-
2143
-
2144
-
2145
-
2146
-
2147
-
2148
-
2149
-
2150
-
2151
-
2152
-
2153
-
2154
-
2155
-
2156
-
2157
- * @example
2158
- * // Remove and return largest
2159
- * const ms = new TreeMultiSet<number>();
2160
- * ms.add(1);
2161
- * ms.add(3);
2162
- * 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;
2163
403
  */
2164
404
  pollLast(): K | undefined {
2165
405
  const key = this.last();
@@ -2171,167 +411,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2171
411
  /**
2172
412
  * Returns the smallest key >= given key, or undefined.
2173
413
  * @remarks Time O(log n), Space O(1)
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
-
2295
-
2296
-
2297
-
2298
-
2299
-
2300
-
2301
-
2302
-
2303
-
2304
-
2305
-
2306
-
2307
-
2308
-
2309
-
2310
-
2311
-
2312
-
2313
-
2314
-
2315
-
2316
-
2317
-
2318
-
2319
-
2320
-
2321
-
2322
-
2323
-
2324
-
2325
-
2326
-
2327
-
2328
- * @example
2329
- * // Least key ≥ target
2330
- * const ms = new TreeMultiSet<number>();
2331
- * ms.add(10);
2332
- * ms.add(20);
2333
- * ms.add(30);
2334
- * 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;
2335
421
  */
2336
422
  ceiling(key: K): K | undefined {
2337
423
  this._validateKey(key);
@@ -2341,167 +427,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2341
427
  /**
2342
428
  * Returns the largest key <= given key, or undefined.
2343
429
  * @remarks Time O(log n), Space O(1)
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
-
2453
-
2454
-
2455
-
2456
-
2457
-
2458
-
2459
-
2460
-
2461
-
2462
-
2463
-
2464
-
2465
-
2466
-
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
- * @example
2499
- * // Greatest key ≤ target
2500
- * const ms = new TreeMultiSet<number>();
2501
- * ms.add(10);
2502
- * ms.add(20);
2503
- * ms.add(30);
2504
- * 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;
2505
437
  */
2506
438
  floor(key: K): K | undefined {
2507
439
  this._validateKey(key);
@@ -2511,166 +443,12 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2511
443
  /**
2512
444
  * Returns the smallest key > given key, or undefined.
2513
445
  * @remarks Time O(log n), Space O(1)
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
-
2610
-
2611
-
2612
-
2613
-
2614
-
2615
-
2616
-
2617
-
2618
-
2619
-
2620
-
2621
-
2622
-
2623
-
2624
-
2625
-
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
- * @example
2669
- * // Least key > target
2670
- * const ms = new TreeMultiSet<number>();
2671
- * ms.add(10);
2672
- * ms.add(20);
2673
- * 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;
2674
452
  */
2675
453
  higher(key: K): K | undefined {
2676
454
  this._validateKey(key);
@@ -2680,379 +458,32 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
2680
458
  /**
2681
459
  * Returns the largest key < given key, or undefined.
2682
460
  * @remarks Time O(log n), Space O(1)
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
-
2806
-
2807
-
2808
-
2809
-
2810
-
2811
-
2812
-
2813
-
2814
-
2815
-
2816
-
2817
-
2818
-
2819
-
2820
-
2821
-
2822
-
2823
-
2824
-
2825
-
2826
-
2827
-
2828
-
2829
-
2830
-
2831
-
2832
-
2833
-
2834
-
2835
-
2836
-
2837
- * @example
2838
- * // Greatest key < target
2839
- * const ms = new TreeMultiSet<number>();
2840
- * ms.add(10);
2841
- * ms.add(20);
2842
- * 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;
2843
467
  */
2844
468
  lower(key: K): K | undefined {
2845
469
  this._validateKey(key);
2846
470
  return this.#core.lower(key);
2847
471
  }
2848
472
 
2849
- // ━━━ Functional methods ━━━
2850
-
2851
473
  /**
2852
474
  * Iterates over distinct keys with their counts.
2853
475
  * @remarks Time O(n), Space O(1)
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
-
3003
-
3004
-
3005
-
3006
-
3007
-
3008
-
3009
-
3010
-
3011
-
3012
-
3013
-
3014
-
3015
-
3016
-
3017
-
3018
-
3019
-
3020
-
3021
-
3022
-
3023
-
3024
-
3025
-
3026
-
3027
-
3028
-
3029
-
3030
-
3031
-
3032
-
3033
-
3034
-
3035
-
3036
-
3037
-
3038
-
3039
-
3040
-
3041
-
3042
-
3043
-
3044
-
3045
-
3046
-
3047
-
3048
- * @example
3049
- * // Iterate
3050
- * const ms = new TreeMultiSet<number>();
3051
- * ms.add(1, 2);
3052
- * ms.add(2);
3053
- * const pairs: [number, number][] = [];
3054
- * ms.forEach((k, c) => pairs.push([k, c]));
3055
- * 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
+ * // ];
3056
487
  */
3057
488
  forEach(callback: (key: K, count: number) => void): void {
3058
489
  for (const [k, c] of this.entries()) {
@@ -3063,208 +494,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
3063
494
  /**
3064
495
  * Creates a new TreeMultiSet with entries that match the predicate.
3065
496
  * @remarks Time O(n log n), Space O(n)
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
-
3207
-
3208
-
3209
-
3210
-
3211
-
3212
-
3213
-
3214
-
3215
-
3216
-
3217
-
3218
-
3219
-
3220
-
3221
-
3222
-
3223
-
3224
-
3225
-
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
- * @example
3261
- * // Filter
3262
- * const ms = new TreeMultiSet<number>();
3263
- * ms.add(1, 3);
3264
- * ms.add(2, 1);
3265
- * ms.add(3, 2);
3266
- * const filtered = ms.filter((k, c) => c > 1);
3267
- * 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];
3268
505
  */
3269
506
  filter(predicate: (key: K, count: number) => boolean): TreeMultiSet<K> {
3270
507
  const result = new TreeMultiSet<K>([], {
@@ -3279,210 +516,18 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
3279
516
  return result;
3280
517
  }
3281
518
 
519
+ // ━━━ Functional methods ━━━
520
+
3282
521
  /**
3283
522
  * Reduces the multiset to a single value.
3284
523
  * @remarks Time O(n), Space O(1)
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
-
3406
-
3407
-
3408
-
3409
-
3410
-
3411
-
3412
-
3413
-
3414
-
3415
-
3416
-
3417
-
3418
-
3419
-
3420
-
3421
-
3422
-
3423
-
3424
-
3425
-
3426
-
3427
-
3428
-
3429
-
3430
-
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
- * @example
3480
- * // Aggregate
3481
- * const ms = new TreeMultiSet<number>();
3482
- * ms.add(1, 2);
3483
- * ms.add(2, 3);
3484
- * const sum = ms.reduce((acc, k, c) => acc + k * c, 0);
3485
- * 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;
3486
531
  */
3487
532
  reduce<U>(callback: (accumulator: U, key: K, count: number) => U, initialValue: U): U {
3488
533
  let acc = initialValue;
@@ -3496,207 +541,13 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
3496
541
  * Maps keys and counts to a new TreeMultiSet.
3497
542
  * When multiple keys map to the same new key, counts are merged (added).
3498
543
  * @remarks Time O(n log n), Space O(n)
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
-
3589
-
3590
-
3591
-
3592
-
3593
-
3594
-
3595
-
3596
-
3597
-
3598
-
3599
-
3600
-
3601
-
3602
-
3603
-
3604
-
3605
-
3606
-
3607
-
3608
-
3609
-
3610
-
3611
-
3612
-
3613
-
3614
-
3615
-
3616
-
3617
-
3618
-
3619
-
3620
-
3621
-
3622
-
3623
-
3624
-
3625
-
3626
-
3627
-
3628
-
3629
-
3630
-
3631
-
3632
-
3633
-
3634
-
3635
-
3636
-
3637
-
3638
-
3639
-
3640
-
3641
-
3642
-
3643
-
3644
-
3645
-
3646
-
3647
-
3648
-
3649
-
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
-
3675
-
3676
-
3677
-
3678
-
3679
-
3680
-
3681
-
3682
-
3683
-
3684
-
3685
-
3686
-
3687
-
3688
-
3689
-
3690
-
3691
-
3692
-
3693
- * @example
3694
- * // Transform
3695
- * const ms = new TreeMultiSet<number>();
3696
- * ms.add(1, 2);
3697
- * ms.add(2, 3);
3698
- * const doubled = ms.map((k, c) => [k * 10, c] as [number, number]);
3699
- * 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];
3700
551
  */
3701
552
  map<K2>(
3702
553
  mapper: (key: K, count: number) => [K2, number],
@@ -3716,277 +567,62 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
3716
567
  /**
3717
568
  * Creates an independent copy of this multiset.
3718
569
  * @remarks Time O(n log n), Space O(n)
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
-
3841
-
3842
-
3843
-
3844
-
3845
-
3846
-
3847
-
3848
-
3849
-
3850
-
3851
-
3852
-
3853
-
3854
-
3855
-
3856
-
3857
-
3858
-
3859
-
3860
-
3861
-
3862
-
3863
-
3864
-
3865
-
3866
-
3867
-
3868
-
3869
-
3870
-
3871
-
3872
-
3873
-
3874
-
3875
-
3876
- * @example
3877
- * // Order-statistic on BST
3878
- * const tree = new TreeMultiSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
3879
- * console.log(tree.getByRank(0)); // 10;
3880
- * console.log(tree.getByRank(4)); // 50;
3881
- * 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;
3882
576
  */
3883
577
  // ─── Order-Statistic Methods ───────────────────────────
3884
-
3885
578
  getByRank(k: number): K | undefined {
3886
579
  return this.#core.getByRank(k);
3887
580
  }
3888
581
 
3889
- /**
582
+ /**
3890
583
  * Get the rank of a key in sorted order
3891
584
  * @example
3892
- * // Get the rank of a key in sorted order
3893
- * const tree = new TreeMultiSet<number>(
3894
- * [10, 20, 30, 40, 50],
3895
- * { enableOrderStatistic: true }
3896
- * );
3897
- * console.log(tree.getRank(10)); // 0; // smallest → rank 0
3898
- * console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
3899
- * console.log(tree.getRank(50)); // 4; // largest → rank 4
3900
- * console.log(tree.getRank(25)); // 2;
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;
3901
594
  */
3902
595
  getRank(key: K): number {
3903
596
  return this.#core.getRank(key);
3904
597
  }
3905
598
 
3906
- /**
599
+ /**
3907
600
  * Get elements by rank range
3908
-
3909
-
3910
-
3911
-
3912
-
3913
-
3914
-
3915
-
3916
-
3917
-
3918
-
3919
-
3920
-
3921
-
3922
-
3923
- * @example
3924
- * // Pagination by position in tree order
3925
- * const tree = new TreeMultiSet<number>(
3926
- * [10, 20, 30, 40, 50, 60, 70, 80, 90],
3927
- * { enableOrderStatistic: true }
3928
- * );
3929
- * const pageSize = 3;
3930
- *
3931
- * // Page 1
3932
- * console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
3933
- * // Page 2
3934
- * console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
3935
- * // Page 3
3936
- * 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];
3937
612
  */
3938
613
  rangeByRank(start: number, end: number): K[] {
3939
614
  return this.#core.rangeByRank(start, end).filter((k): k is K => k !== undefined);
3940
615
  }
3941
616
 
3942
- /**
617
+ /**
3943
618
  * Deep copy
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
- * @example
3984
- * // Deep clone
3985
- * const ms = new TreeMultiSet<number>();
3986
- * ms.add(1, 3);
3987
- * const copy = ms.clone();
3988
- * copy.deleteAll(1);
3989
- * 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;
3990
626
  */
3991
627
  clone(): TreeMultiSet<K> {
3992
628
  const result = new TreeMultiSet<K>([], {
@@ -3999,178 +635,19 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
3999
635
  return result;
4000
636
  }
4001
637
 
4002
- // ━━━ Tree utilities ━━━
4003
-
4004
638
  /**
4005
639
  * Returns keys within the given range.
4006
640
  * @remarks Time O(log n + k), Space O(k) where k is result size
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
-
4040
-
4041
-
4042
-
4043
-
4044
-
4045
-
4046
-
4047
-
4048
-
4049
-
4050
-
4051
-
4052
-
4053
-
4054
-
4055
-
4056
-
4057
-
4058
-
4059
-
4060
-
4061
-
4062
-
4063
-
4064
-
4065
-
4066
-
4067
-
4068
-
4069
-
4070
-
4071
-
4072
-
4073
-
4074
-
4075
-
4076
-
4077
-
4078
-
4079
-
4080
-
4081
-
4082
-
4083
-
4084
-
4085
-
4086
-
4087
-
4088
-
4089
-
4090
-
4091
-
4092
-
4093
-
4094
-
4095
-
4096
-
4097
-
4098
-
4099
-
4100
-
4101
-
4102
-
4103
-
4104
-
4105
-
4106
-
4107
-
4108
-
4109
-
4110
-
4111
-
4112
-
4113
-
4114
-
4115
-
4116
-
4117
-
4118
-
4119
-
4120
-
4121
-
4122
-
4123
-
4124
-
4125
-
4126
-
4127
-
4128
-
4129
-
4130
-
4131
-
4132
-
4133
-
4134
-
4135
-
4136
-
4137
-
4138
-
4139
-
4140
-
4141
-
4142
-
4143
-
4144
-
4145
-
4146
-
4147
-
4148
-
4149
-
4150
-
4151
-
4152
-
4153
-
4154
-
4155
-
4156
-
4157
-
4158
-
4159
-
4160
-
4161
- * @example
4162
- * // Find in range
4163
- * const ms = new TreeMultiSet<number>();
4164
- * ms.add(10);
4165
- * ms.add(20);
4166
- * ms.add(30);
4167
- * const result = ms.rangeSearch([15, 25]);
4168
- * console.log(result.length); // 1;
4169
- */
4170
- rangeSearch<C extends (key: K) => any>(
4171
- range: [K, K],
4172
- callback?: C
4173
- ): (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>)[] {
4174
651
  const cb = callback ?? ((k: K) => k);
4175
652
  return this.#core.rangeSearch(range, node => cb(node.key));
4176
653
  }
@@ -4178,207 +655,42 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
4178
655
  /**
4179
656
  * Prints the internal tree structure (for debugging).
4180
657
  * @remarks Time O(n), Space O(n)
4181
-
4182
-
4183
-
4184
-
4185
-
4186
-
4187
-
4188
-
4189
-
4190
-
4191
-
4192
-
4193
-
4194
-
4195
-
4196
-
4197
-
4198
-
4199
-
4200
-
4201
-
4202
-
4203
-
4204
-
4205
-
4206
-
4207
-
4208
-
4209
-
4210
-
4211
-
4212
-
4213
-
4214
-
4215
-
4216
-
4217
-
4218
-
4219
-
4220
-
4221
-
4222
-
4223
-
4224
-
4225
-
4226
-
4227
-
4228
-
4229
-
4230
-
4231
-
4232
-
4233
-
4234
-
4235
-
4236
-
4237
-
4238
-
4239
-
4240
-
4241
-
4242
-
4243
-
4244
-
4245
-
4246
-
4247
-
4248
-
4249
-
4250
-
4251
-
4252
-
4253
-
4254
-
4255
-
4256
-
4257
-
4258
-
4259
-
4260
-
4261
-
4262
-
4263
-
4264
-
4265
-
4266
-
4267
-
4268
-
4269
-
4270
-
4271
-
4272
-
4273
-
4274
-
4275
-
4276
-
4277
-
4278
-
4279
-
4280
-
4281
-
4282
-
4283
-
4284
-
4285
-
4286
-
4287
-
4288
-
4289
-
4290
-
4291
-
4292
-
4293
-
4294
-
4295
-
4296
-
4297
-
4298
-
4299
-
4300
-
4301
-
4302
-
4303
-
4304
-
4305
-
4306
-
4307
-
4308
-
4309
-
4310
-
4311
-
4312
-
4313
-
4314
-
4315
-
4316
-
4317
-
4318
-
4319
-
4320
-
4321
-
4322
-
4323
-
4324
-
4325
-
4326
-
4327
-
4328
-
4329
-
4330
-
4331
-
4332
-
4333
-
4334
-
4335
-
4336
-
4337
-
4338
-
4339
-
4340
-
4341
-
4342
-
4343
-
4344
-
4345
-
4346
-
4347
-
4348
-
4349
-
4350
-
4351
-
4352
-
4353
-
4354
-
4355
-
4356
-
4357
-
4358
-
4359
-
4360
-
4361
-
4362
-
4363
-
4364
-
4365
-
4366
-
4367
-
4368
-
4369
-
4370
-
4371
-
4372
-
4373
-
4374
-
4375
- * @example
4376
- * // Display
4377
- * const ms = new TreeMultiSet<number>();
4378
- * ms.add(1);
4379
- * 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();
4380
663
  */
4381
664
  print(): void {
4382
665
  this.#core.print();
4383
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
+ }
4384
696
  }