data-structure-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 (80) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.md +34 -1
  3. package/dist/cjs/index.cjs +10639 -2151
  4. package/dist/cjs-legacy/index.cjs +10694 -2195
  5. package/dist/esm/index.mjs +10639 -2150
  6. package/dist/esm-legacy/index.mjs +10694 -2194
  7. package/dist/types/common/error.d.ts +23 -0
  8. package/dist/types/common/index.d.ts +1 -0
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  11. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  15. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  16. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  18. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  19. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  20. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  42. package/dist/umd/data-structure-typed.js +10725 -2221
  43. package/dist/umd/data-structure-typed.min.js +4 -2
  44. package/package.json +5 -4
  45. package/src/common/error.ts +60 -0
  46. package/src/common/index.ts +2 -0
  47. package/src/data-structures/base/iterable-element-base.ts +2 -2
  48. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  49. package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
  50. package/src/data-structures/binary-tree/binary-tree.ts +567 -121
  51. package/src/data-structures/binary-tree/bst.ts +370 -37
  52. package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
  53. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  54. package/src/data-structures/binary-tree/tree-map.ts +1411 -13
  55. package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
  56. package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
  57. package/src/data-structures/binary-tree/tree-set.ts +1257 -15
  58. package/src/data-structures/graph/abstract-graph.ts +106 -1
  59. package/src/data-structures/graph/directed-graph.ts +233 -47
  60. package/src/data-structures/graph/map-graph.ts +59 -1
  61. package/src/data-structures/graph/undirected-graph.ts +308 -59
  62. package/src/data-structures/hash/hash-map.ts +254 -79
  63. package/src/data-structures/heap/heap.ts +305 -102
  64. package/src/data-structures/heap/max-heap.ts +48 -3
  65. package/src/data-structures/heap/min-heap.ts +59 -0
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  67. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  68. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  69. package/src/data-structures/matrix/matrix.ts +433 -22
  70. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  71. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  72. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  73. package/src/data-structures/queue/deque.ts +358 -68
  74. package/src/data-structures/queue/queue.ts +223 -42
  75. package/src/data-structures/stack/stack.ts +184 -32
  76. package/src/data-structures/trie/trie.ts +227 -44
  77. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  78. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  79. package/src/types/data-structures/queue/deque.ts +7 -0
  80. 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,15 @@ 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
+
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;
86
96
  */
87
97
  get distinctSize(): number {
88
98
  return this.#core.size;
@@ -91,6 +101,52 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
91
101
  /**
92
102
  * Whether the multiset is empty.
93
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;
94
150
  */
95
151
  isEmpty(): boolean {
96
152
  return this.size === 0;
@@ -99,6 +155,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
99
155
  /**
100
156
  * Whether the multiset contains the given key.
101
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;
102
210
  */
103
211
  has(key: K): boolean {
104
212
  this._validateKey(key);
@@ -108,6 +216,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
108
216
  /**
109
217
  * Returns the count of occurrences for the given key.
110
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;
111
227
  */
112
228
  count(key: K): number {
113
229
  this._validateKey(key);
@@ -118,6 +234,51 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
118
234
  * Add `n` occurrences of `key`.
119
235
  * @returns True if the multiset changed.
120
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;
121
282
  */
122
283
  add(key: K, n = 1): boolean {
123
284
  this._validateKey(key);
@@ -135,6 +296,14 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
135
296
  * Set count for `key` to exactly `n`.
136
297
  * @returns True if changed.
137
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;
138
307
  */
139
308
  setCount(key: K, n: number): boolean {
140
309
  this._validateKey(key);
@@ -157,6 +326,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
157
326
  * Delete `n` occurrences of `key` (default 1).
158
327
  * @returns True if any occurrence was removed.
159
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;
160
381
  */
161
382
  delete(key: K, n = 1): boolean {
162
383
  this._validateKey(key);
@@ -180,6 +401,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
180
401
  * Delete all occurrences of the given key.
181
402
  * @returns True if any occurrence was removed.
182
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;
183
413
  */
184
414
  deleteAll(key: K): boolean {
185
415
  this._validateKey(key);
@@ -193,6 +423,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
193
423
  /**
194
424
  * Iterates over distinct keys (each key yielded once).
195
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];
196
435
  */
197
436
  *keysDistinct(): IterableIterator<K> {
198
437
  yield* this.#core.keys();
@@ -201,6 +440,54 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
201
440
  /**
202
441
  * Iterates over entries as [key, count] pairs.
203
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;
204
491
  */
205
492
  *entries(): IterableIterator<[K, number]> {
206
493
  for (const [k, v] of this.#core) {
@@ -221,6 +508,55 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
221
508
  /**
222
509
  * Returns an array with all elements (expanded).
223
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];
224
560
  */
225
561
  toArray(): K[] {
226
562
  return [...this];
@@ -229,6 +565,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
229
565
  /**
230
566
  * Returns an array with distinct keys only.
231
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];
232
577
  */
233
578
  toDistinctArray(): K[] {
234
579
  return [...this.keysDistinct()];
@@ -237,6 +582,15 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
237
582
  /**
238
583
  * Returns an array of [key, count] entries.
239
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]];
240
594
  */
241
595
  toEntries(): Array<[K, number]> {
242
596
  return [...this.entries()];
@@ -255,10 +609,56 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
255
609
  /**
256
610
  * Remove all elements from the multiset.
257
611
  * @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
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;
262
662
  */
263
663
  clear(): void {
264
664
  this.#core.clear();
@@ -270,9 +670,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
270
670
  /**
271
671
  * Returns the smallest key, or undefined if empty.
272
672
  * @remarks Time O(log n), Space O(1)
273
- * @example
274
- * const ms = new TreeMultiSet([3, 1, 4]);
275
- * 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;
276
683
  */
277
684
  first(): K | undefined {
278
685
  return this.#core.getLeftMost();
@@ -281,9 +688,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
281
688
  /**
282
689
  * Returns the largest key, or undefined if empty.
283
690
  * @remarks Time O(log n), Space O(1)
284
- * @example
285
- * const ms = new TreeMultiSet([3, 1, 4]);
286
- * 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;
287
701
  */
288
702
  last(): K | undefined {
289
703
  return this.#core.getRightMost();
@@ -292,10 +706,17 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
292
706
  /**
293
707
  * Removes all occurrences of the smallest key and returns it.
294
708
  * @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
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;
299
720
  */
300
721
  pollFirst(): K | undefined {
301
722
  const key = this.first();
@@ -307,10 +728,16 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
307
728
  /**
308
729
  * Removes all occurrences of the largest key and returns it.
309
730
  * @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
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;
314
741
  */
315
742
  pollLast(): K | undefined {
316
743
  const key = this.last();
@@ -322,10 +749,47 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
322
749
  /**
323
750
  * Returns the smallest key >= given key, or undefined.
324
751
  * @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
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;
329
793
  */
330
794
  ceiling(key: K): K | undefined {
331
795
  this._validateKey(key);
@@ -335,10 +799,47 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
335
799
  /**
336
800
  * Returns the largest key <= given key, or undefined.
337
801
  * @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
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;
342
843
  */
343
844
  floor(key: K): K | undefined {
344
845
  this._validateKey(key);
@@ -348,10 +849,46 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
348
849
  /**
349
850
  * Returns the smallest key > given key, or undefined.
350
851
  * @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
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;
355
892
  */
356
893
  higher(key: K): K | undefined {
357
894
  this._validateKey(key);
@@ -361,10 +898,46 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
361
898
  /**
362
899
  * Returns the largest key < given key, or undefined.
363
900
  * @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
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;
368
941
  */
369
942
  lower(key: K): K | undefined {
370
943
  this._validateKey(key);
@@ -376,10 +949,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
376
949
  /**
377
950
  * Iterates over distinct keys with their counts.
378
951
  * @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
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]];
383
1004
  */
384
1005
  forEach(callback: (key: K, count: number) => void): void {
385
1006
  for (const [k, c] of this.entries()) {
@@ -390,10 +1011,58 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
390
1011
  /**
391
1012
  * Creates a new TreeMultiSet with entries that match the predicate.
392
1013
  * @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 }
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];
397
1066
  */
398
1067
  filter(predicate: (key: K, count: number) => boolean): TreeMultiSet<K> {
399
1068
  const result = new TreeMultiSet<K>([], {
@@ -411,9 +1080,57 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
411
1080
  /**
412
1081
  * Reduces the multiset to a single value.
413
1082
  * @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
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;
417
1134
  */
418
1135
  reduce<U>(callback: (accumulator: U, key: K, count: number) => U, initialValue: U): U {
419
1136
  let acc = initialValue;
@@ -427,15 +1144,57 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
427
1144
  * Maps keys and counts to a new TreeMultiSet.
428
1145
  * When multiple keys map to the same new key, counts are merged (added).
429
1146
  * @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)
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];
439
1198
  */
440
1199
  map<K2>(
441
1200
  mapper: (key: K, count: number) => [K2, number],
@@ -455,11 +1214,57 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
455
1214
  /**
456
1215
  * Creates an independent copy of this multiset.
457
1216
  * @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)
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;
463
1268
  */
464
1269
  clone(): TreeMultiSet<K> {
465
1270
  const result = new TreeMultiSet<K>([], {
@@ -477,9 +1282,48 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
477
1282
  /**
478
1283
  * Returns keys within the given range.
479
1284
  * @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]
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;
483
1327
  */
484
1328
  rangeSearch<C extends (key: K) => any>(
485
1329
  range: [K, K],
@@ -492,9 +1336,55 @@ export class TreeMultiSet<K = any, R = K> implements Iterable<K> {
492
1336
  /**
493
1337
  * Prints the internal tree structure (for debugging).
494
1338
  * @remarks Time O(n), Space O(n)
495
- * @example
496
- * const ms = new TreeMultiSet([1, 2, 3]);
497
- * 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();
498
1388
  */
499
1389
  print(): void {
500
1390
  this.#core.print();