data-structure-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.
Files changed (71) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +15 -5
  3. package/dist/cjs/index.cjs +10240 -2079
  4. package/dist/cjs-legacy/index.cjs +10305 -2135
  5. package/dist/esm/index.mjs +10241 -2078
  6. package/dist/esm-legacy/index.mjs +10306 -2134
  7. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  12. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
  13. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  14. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  16. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  17. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
  19. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  20. package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
  21. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  22. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  23. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  24. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  28. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  29. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  30. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  31. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  32. package/dist/types/data-structures/queue/deque.d.ts +272 -65
  33. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  34. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  35. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  36. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  37. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  38. package/dist/umd/data-structure-typed.js +10308 -2133
  39. package/dist/umd/data-structure-typed.min.js +4 -4
  40. package/package.json +5 -4
  41. package/src/data-structures/base/iterable-element-base.ts +4 -5
  42. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  43. package/src/data-structures/binary-tree/binary-indexed-tree.ts +316 -247
  44. package/src/data-structures/binary-tree/binary-tree.ts +454 -79
  45. package/src/data-structures/binary-tree/bst.ts +359 -34
  46. package/src/data-structures/binary-tree/red-black-tree.ts +309 -97
  47. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  48. package/src/data-structures/binary-tree/tree-map.ts +1403 -6
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +1214 -211
  50. package/src/data-structures/binary-tree/tree-multi-set.ts +954 -65
  51. package/src/data-structures/binary-tree/tree-set.ts +1250 -9
  52. package/src/data-structures/graph/directed-graph.ts +229 -47
  53. package/src/data-structures/graph/map-graph.ts +59 -1
  54. package/src/data-structures/graph/undirected-graph.ts +213 -59
  55. package/src/data-structures/hash/hash-map.ts +241 -77
  56. package/src/data-structures/heap/heap.ts +301 -99
  57. package/src/data-structures/heap/max-heap.ts +46 -0
  58. package/src/data-structures/heap/min-heap.ts +59 -0
  59. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  60. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  61. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  62. package/src/data-structures/matrix/matrix.ts +424 -12
  63. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  64. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  65. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  66. package/src/data-structures/queue/deque.ts +287 -65
  67. package/src/data-structures/queue/queue.ts +223 -42
  68. package/src/data-structures/stack/stack.ts +184 -32
  69. package/src/data-structures/trie/trie.ts +225 -43
  70. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  71. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
@@ -84,6 +84,15 @@ 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
+
90
+ * @example
91
+ * // Unique key count
92
+ * const ms = new TreeMultiSet<number>();
93
+ * ms.add(1, 3);
94
+ * ms.add(2, 2);
95
+ * console.log(ms.distinctSize); // 2;
87
96
  */
88
97
  get distinctSize(): number {
89
98
  return this.#core.size;
@@ -92,6 +101,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
92
101
  /**
93
102
  * Whether the multiset is empty.
94
103
  * @remarks Time O(1), Space O(1)
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
+
142
+
143
+
144
+
145
+
146
+
147
+ * @example
148
+ * // Check empty
149
+ * console.log(new TreeMultiSet().isEmpty()); // true;
95
150
  */
96
151
  isEmpty(): boolean {
97
152
  return this.size === 0;
@@ -100,6 +155,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
100
155
  /**
101
156
  * Whether the multiset contains the given key.
102
157
  * @remarks Time O(log n), Space O(1)
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
+ * @example
205
+ * // Check existence
206
+ * const ms = new TreeMultiSet<number>();
207
+ * ms.add(1);
208
+ * console.log(ms.has(1)); // true;
209
+ * console.log(ms.has(2)); // false;
103
210
  */
104
211
  has(key: K): boolean {
105
212
  this._validateKey(key);
@@ -109,6 +216,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
109
216
  /**
110
217
  * Returns the count of occurrences for the given key.
111
218
  * @remarks Time O(log n), Space O(1)
219
+
220
+
221
+
222
+ * @example
223
+ * // Get occurrence count
224
+ * const ms = new TreeMultiSet<number>();
225
+ * ms.add(1, 5);
226
+ * console.log(ms.count(1)); // 5;
112
227
  */
113
228
  count(key: K): number {
114
229
  this._validateKey(key);
@@ -119,6 +234,51 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
119
234
  * Add `n` occurrences of `key`.
120
235
  * @returns True if the multiset changed.
121
236
  * @remarks Time O(log n), Space O(1)
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
+ * @example
275
+ * // Add elements
276
+ * const ms = new TreeMultiSet<number>();
277
+ * ms.add(1);
278
+ * ms.add(1);
279
+ * ms.add(2);
280
+ * console.log(ms.count(1)); // 2;
281
+ * console.log(ms.size); // 3;
122
282
  */
123
283
  add(key: K, n = 1): boolean {
124
284
  this._validateKey(key);
@@ -136,6 +296,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
136
296
  * Set count for `key` to exactly `n`.
137
297
  * @returns True if changed.
138
298
  * @remarks Time O(log n), Space O(1)
299
+
300
+
301
+
302
+ * @example
303
+ * // Set occurrence count
304
+ * const ms = new TreeMultiSet<number>();
305
+ * ms.setCount(1, 3);
306
+ * console.log(ms.count(1)); // 3;
139
307
  */
140
308
  setCount(key: K, n: number): boolean {
141
309
  this._validateKey(key);
@@ -158,6 +326,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
158
326
  * Delete `n` occurrences of `key` (default 1).
159
327
  * @returns True if any occurrence was removed.
160
328
  * @remarks Time O(log n), Space O(1)
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+
357
+
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+ * @example
376
+ * // Remove one occurrence
377
+ * const ms = new TreeMultiSet<number>();
378
+ * ms.add(1, 3);
379
+ * ms.delete(1);
380
+ * console.log(ms.count(1)); // 2;
161
381
  */
162
382
  delete(key: K, n = 1): boolean {
163
383
  this._validateKey(key);
@@ -181,6 +401,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
181
401
  * Delete all occurrences of the given key.
182
402
  * @returns True if any occurrence was removed.
183
403
  * @remarks Time O(log n), Space O(1)
404
+
405
+
406
+
407
+ * @example
408
+ * // Remove all occurrences
409
+ * const ms = new TreeMultiSet<number>();
410
+ * ms.add(1, 3);
411
+ * ms.deleteAll(1);
412
+ * console.log(ms.has(1)); // false;
184
413
  */
185
414
  deleteAll(key: K): boolean {
186
415
  this._validateKey(key);
@@ -194,6 +423,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
194
423
  /**
195
424
  * Iterates over distinct keys (each key yielded once).
196
425
  * @remarks Time O(n), Space O(1)
426
+
427
+
428
+
429
+ * @example
430
+ * // Iterate unique keys
431
+ * const ms = new TreeMultiSet<number>();
432
+ * ms.add(1, 2);
433
+ * ms.add(2);
434
+ * console.log([...ms.keysDistinct()]); // [1, 2];
197
435
  */
198
436
  *keysDistinct(): IterableIterator<K> {
199
437
  yield* this.#core.keys();
@@ -202,6 +440,54 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
202
440
  /**
203
441
  * Iterates over entries as [key, count] pairs.
204
442
  * @remarks Time O(n), Space O(1)
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
+ * @example
487
+ * // Iterate entries
488
+ * const ms = new TreeMultiSet<number>();
489
+ * ms.add(1, 2);
490
+ * console.log([...ms.entries()].length); // > 0;
205
491
  */
206
492
  *entries(): IterableIterator<[K, number]> {
207
493
  for (const [k, v] of this.#core) {
@@ -222,6 +508,55 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
222
508
  /**
223
509
  * Returns an array with all elements (expanded).
224
510
  * @remarks Time O(size), Space O(size)
511
+
512
+
513
+
514
+
515
+
516
+
517
+
518
+
519
+
520
+
521
+
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
544
+
545
+
546
+
547
+
548
+
549
+
550
+
551
+
552
+
553
+
554
+ * @example
555
+ * // All elements (with duplicates)
556
+ * const ms = new TreeMultiSet<number>();
557
+ * ms.add(1, 2);
558
+ * ms.add(2);
559
+ * console.log(ms.toArray()); // [1, 1, 2];
225
560
  */
226
561
  toArray(): K[] {
227
562
  return [...this];
@@ -230,6 +565,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
230
565
  /**
231
566
  * Returns an array with distinct keys only.
232
567
  * @remarks Time O(n), Space O(n)
568
+
569
+
570
+
571
+ * @example
572
+ * // Unique keys only
573
+ * const ms = new TreeMultiSet<number>();
574
+ * ms.add(1, 3);
575
+ * ms.add(2);
576
+ * console.log(ms.toDistinctArray()); // [1, 2];
233
577
  */
234
578
  toDistinctArray(): K[] {
235
579
  return [...this.keysDistinct()];
@@ -238,6 +582,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
238
582
  /**
239
583
  * Returns an array of [key, count] entries.
240
584
  * @remarks Time O(n), Space O(n)
585
+
586
+
587
+
588
+ * @example
589
+ * // Key-count pairs
590
+ * const ms = new TreeMultiSet<number>();
591
+ * ms.add(1, 2);
592
+ * ms.add(3);
593
+ * console.log(ms.toEntries()); // [[1, 2], [3, 1]];
241
594
  */
242
595
  toEntries(): Array<[K, number]> {
243
596
  return [...this.entries()];
@@ -256,10 +609,56 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
256
609
  /**
257
610
  * Remove all elements from the multiset.
258
611
  * @remarks Time O(1), Space O(1)
259
- * @example
260
- * const ms = new TreeMultiSet([1, 2, 2, 3]);
261
- * ms.clear();
262
- * ms.size; // 0
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
+ * @example
657
+ * // Remove all
658
+ * const ms = new TreeMultiSet<number>();
659
+ * ms.add(1);
660
+ * ms.clear();
661
+ * console.log(ms.isEmpty()); // true;
263
662
  */
264
663
  clear(): void {
265
664
  this.#core.clear();
@@ -271,9 +670,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
271
670
  /**
272
671
  * Returns the smallest key, or undefined if empty.
273
672
  * @remarks Time O(log n), Space O(1)
274
- * @example
275
- * const ms = new TreeMultiSet([3, 1, 4]);
276
- * ms.first(); // 1
673
+
674
+
675
+
676
+
677
+ * @example
678
+ * // Smallest element
679
+ * const ms = new TreeMultiSet<number>();
680
+ * ms.add(3);
681
+ * ms.add(1);
682
+ * console.log(ms.first()); // 1;
277
683
  */
278
684
  first(): K | undefined {
279
685
  return this.#core.getLeftMost();
@@ -282,9 +688,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
282
688
  /**
283
689
  * Returns the largest key, or undefined if empty.
284
690
  * @remarks Time O(log n), Space O(1)
285
- * @example
286
- * const ms = new TreeMultiSet([3, 1, 4]);
287
- * ms.last(); // 4
691
+
692
+
693
+
694
+
695
+ * @example
696
+ * // Largest element
697
+ * const ms = new TreeMultiSet<number>();
698
+ * ms.add(1);
699
+ * ms.add(3);
700
+ * console.log(ms.last()); // 3;
288
701
  */
289
702
  last(): K | undefined {
290
703
  return this.#core.getRightMost();
@@ -293,10 +706,17 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
293
706
  /**
294
707
  * Removes all occurrences of the smallest key and returns it.
295
708
  * @remarks Time O(log n), Space O(1)
296
- * @example
297
- * const ms = new TreeMultiSet([1, 1, 2, 3]);
298
- * ms.pollFirst(); // 1
299
- * ms.has(1); // false
709
+
710
+
711
+
712
+
713
+ * @example
714
+ * // Remove and return smallest
715
+ * const ms = new TreeMultiSet<number>();
716
+ * ms.add(2);
717
+ * ms.add(1);
718
+ * console.log(ms.pollFirst()); // 1;
719
+ * console.log(ms.has(1)); // false;
300
720
  */
301
721
  pollFirst(): K | undefined {
302
722
  const key = this.first();
@@ -308,10 +728,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
308
728
  /**
309
729
  * Removes all occurrences of the largest key and returns it.
310
730
  * @remarks Time O(log n), Space O(1)
311
- * @example
312
- * const ms = new TreeMultiSet([1, 2, 3, 3]);
313
- * ms.pollLast(); // 3
314
- * ms.has(3); // false
731
+
732
+
733
+
734
+
735
+ * @example
736
+ * // Remove and return largest
737
+ * const ms = new TreeMultiSet<number>();
738
+ * ms.add(1);
739
+ * ms.add(3);
740
+ * console.log(ms.pollLast()); // 3;
315
741
  */
316
742
  pollLast(): K | undefined {
317
743
  const key = this.last();
@@ -323,10 +749,47 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
323
749
  /**
324
750
  * Returns the smallest key >= given key, or undefined.
325
751
  * @remarks Time O(log n), Space O(1)
326
- * @example
327
- * const ms = new TreeMultiSet([10, 20, 30]);
328
- * ms.ceiling(15); // 20
329
- * ms.ceiling(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
+
783
+
784
+
785
+
786
+ * @example
787
+ * // Least key ≥ target
788
+ * const ms = new TreeMultiSet<number>();
789
+ * ms.add(10);
790
+ * ms.add(20);
791
+ * ms.add(30);
792
+ * console.log(ms.ceiling(15)); // 20;
330
793
  */
331
794
  ceiling(key: K): K | undefined {
332
795
  this._validateKey(key);
@@ -336,10 +799,47 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
336
799
  /**
337
800
  * Returns the largest key <= given key, or undefined.
338
801
  * @remarks Time O(log n), Space O(1)
339
- * @example
340
- * const ms = new TreeMultiSet([10, 20, 30]);
341
- * ms.floor(25); // 20
342
- * ms.floor(20); // 20
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
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+ * @example
837
+ * // Greatest key ≤ target
838
+ * const ms = new TreeMultiSet<number>();
839
+ * ms.add(10);
840
+ * ms.add(20);
841
+ * ms.add(30);
842
+ * console.log(ms.floor(25)); // 20;
343
843
  */
344
844
  floor(key: K): K | undefined {
345
845
  this._validateKey(key);
@@ -349,10 +849,46 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
349
849
  /**
350
850
  * Returns the smallest key > given key, or undefined.
351
851
  * @remarks Time O(log n), Space O(1)
352
- * @example
353
- * const ms = new TreeMultiSet([10, 20, 30]);
354
- * ms.higher(10); // 20
355
- * ms.higher(15); // 20
852
+
853
+
854
+
855
+
856
+
857
+
858
+
859
+
860
+
861
+
862
+
863
+
864
+
865
+
866
+
867
+
868
+
869
+
870
+
871
+
872
+
873
+
874
+
875
+
876
+
877
+
878
+
879
+
880
+
881
+
882
+
883
+
884
+
885
+
886
+ * @example
887
+ * // Least key > target
888
+ * const ms = new TreeMultiSet<number>();
889
+ * ms.add(10);
890
+ * ms.add(20);
891
+ * console.log(ms.higher(10)); // 20;
356
892
  */
357
893
  higher(key: K): K | undefined {
358
894
  this._validateKey(key);
@@ -362,10 +898,46 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
362
898
  /**
363
899
  * Returns the largest key < given key, or undefined.
364
900
  * @remarks Time O(log n), Space O(1)
365
- * @example
366
- * const ms = new TreeMultiSet([10, 20, 30]);
367
- * ms.lower(20); // 10
368
- * ms.lower(15); // 10
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
+ * @example
936
+ * // Greatest key < target
937
+ * const ms = new TreeMultiSet<number>();
938
+ * ms.add(10);
939
+ * ms.add(20);
940
+ * console.log(ms.lower(20)); // 10;
369
941
  */
370
942
  lower(key: K): K | undefined {
371
943
  this._validateKey(key);
@@ -377,10 +949,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
377
949
  /**
378
950
  * Iterates over distinct keys with their counts.
379
951
  * @remarks Time O(n), Space O(1)
380
- * @example
381
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
382
- * ms.forEach((key, count) => console.log(`${key}: ${count}`));
383
- * // 1: 2, 2: 1, 3: 3
952
+
953
+
954
+
955
+
956
+
957
+
958
+
959
+
960
+
961
+
962
+
963
+
964
+
965
+
966
+
967
+
968
+
969
+
970
+
971
+
972
+
973
+
974
+
975
+
976
+
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+
989
+
990
+
991
+
992
+
993
+
994
+
995
+
996
+ * @example
997
+ * // Iterate
998
+ * const ms = new TreeMultiSet<number>();
999
+ * ms.add(1, 2);
1000
+ * ms.add(2);
1001
+ * const pairs: [number, number][] = [];
1002
+ * ms.forEach((k, c) => pairs.push([k, c]));
1003
+ * console.log(pairs); // [[1, 2], [2, 1]];
384
1004
  */
385
1005
  forEach(callback: (key: K, count: number) => void): void {
386
1006
  for (const [k, c] of this.entries()) {
@@ -391,10 +1011,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
391
1011
  /**
392
1012
  * Creates a new TreeMultiSet with entries that match the predicate.
393
1013
  * @remarks Time O(n log n), Space O(n)
394
- * @example
395
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
396
- * const filtered = ms.filter((key, count) => count >= 2);
397
- * // TreeMultiSet { 1: 2, 3: 3 }
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
+ * @example
1059
+ * // Filter
1060
+ * const ms = new TreeMultiSet<number>();
1061
+ * ms.add(1, 3);
1062
+ * ms.add(2, 1);
1063
+ * ms.add(3, 2);
1064
+ * const filtered = ms.filter((k, c) => c > 1);
1065
+ * console.log([...filtered.keysDistinct()]); // [1, 3];
398
1066
  */
399
1067
  filter(predicate: (key: K, count: number) => boolean): TreeMultiSet<K> {
400
1068
  const result = new TreeMultiSet<K>([], {
@@ -412,9 +1080,57 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
412
1080
  /**
413
1081
  * Reduces the multiset to a single value.
414
1082
  * @remarks Time O(n), Space O(1)
415
- * @example
416
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
417
- * const total = ms.reduce((acc, key, count) => acc + count, 0); // 6
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
+
1110
+
1111
+
1112
+
1113
+
1114
+
1115
+
1116
+
1117
+
1118
+
1119
+
1120
+
1121
+
1122
+
1123
+
1124
+
1125
+
1126
+
1127
+ * @example
1128
+ * // Aggregate
1129
+ * const ms = new TreeMultiSet<number>();
1130
+ * ms.add(1, 2);
1131
+ * ms.add(2, 3);
1132
+ * const sum = ms.reduce((acc, k, c) => acc + k * c, 0);
1133
+ * console.log(sum); // 8;
418
1134
  */
419
1135
  reduce<U>(callback: (accumulator: U, key: K, count: number) => U, initialValue: U): U {
420
1136
  let acc = initialValue;
@@ -428,15 +1144,57 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
428
1144
  * Maps keys and counts to a new TreeMultiSet.
429
1145
  * When multiple keys map to the same new key, counts are merged (added).
430
1146
  * @remarks Time O(n log n), Space O(n)
431
- * @example
432
- * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
433
- * const mapped = ms.map((key, count) => [key * 10, count]);
434
- * // TreeMultiSet { 10: 2, 20: 1, 30: 3 }
435
- * @example
436
- * // Collision: counts merge
437
- * const ms = new TreeMultiSet([1, 2, 3]);
438
- * const merged = ms.map((key, count) => [key % 2, count]);
439
- * // { 0: 1, 1: 2 } (1 and 3 both map to 1, counts add)
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+
1166
+
1167
+
1168
+
1169
+
1170
+
1171
+
1172
+
1173
+
1174
+
1175
+
1176
+
1177
+
1178
+
1179
+
1180
+
1181
+
1182
+
1183
+
1184
+
1185
+
1186
+
1187
+
1188
+
1189
+
1190
+
1191
+ * @example
1192
+ * // Transform
1193
+ * const ms = new TreeMultiSet<number>();
1194
+ * ms.add(1, 2);
1195
+ * ms.add(2, 3);
1196
+ * const doubled = ms.map((k, c) => [k * 10, c] as [number, number]);
1197
+ * console.log([...doubled.keysDistinct()]); // [10, 20];
440
1198
  */
441
1199
  map<K2>(
442
1200
  mapper: (key: K, count: number) => [K2, number],
@@ -456,11 +1214,57 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
456
1214
  /**
457
1215
  * Creates an independent copy of this multiset.
458
1216
  * @remarks Time O(n log n), Space O(n)
459
- * @example
460
- * const ms = new TreeMultiSet([1, 1, 2]);
461
- * const copy = ms.clone();
462
- * copy.add(3);
463
- * ms.has(3); // false (original unchanged)
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
+ * @example
1262
+ * // Deep clone
1263
+ * const ms = new TreeMultiSet<number>();
1264
+ * ms.add(1, 3);
1265
+ * const copy = ms.clone();
1266
+ * copy.deleteAll(1);
1267
+ * console.log(ms.has(1)); // true;
464
1268
  */
465
1269
  clone(): TreeMultiSet<K> {
466
1270
  const result = new TreeMultiSet<K>([], {
@@ -478,9 +1282,48 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
478
1282
  /**
479
1283
  * Returns keys within the given range.
480
1284
  * @remarks Time O(log n + k), Space O(k) where k is result size
481
- * @example
482
- * const ms = new TreeMultiSet([10, 20, 30, 40, 50]);
483
- * ms.rangeSearch([15, 45]); // [20, 30, 40]
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
+ * @example
1320
+ * // Find in range
1321
+ * const ms = new TreeMultiSet<number>();
1322
+ * ms.add(10);
1323
+ * ms.add(20);
1324
+ * ms.add(30);
1325
+ * const result = ms.rangeSearch([15, 25]);
1326
+ * console.log(result.length); // 1;
484
1327
  */
485
1328
  rangeSearch<C extends (key: K) => any>(
486
1329
  range: [K, K],
@@ -493,9 +1336,55 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
493
1336
  /**
494
1337
  * Prints the internal tree structure (for debugging).
495
1338
  * @remarks Time O(n), Space O(n)
496
- * @example
497
- * const ms = new TreeMultiSet([1, 2, 3]);
498
- * ms.print();
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
+ * @example
1384
+ * // Display
1385
+ * const ms = new TreeMultiSet<number>();
1386
+ * ms.add(1);
1387
+ * expect(() => ms.print()).not.toThrow();
499
1388
  */
500
1389
  print(): void {
501
1390
  this.#core.print();