binary-tree-typed 2.5.1 → 2.5.3

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 (75) hide show
  1. package/dist/cjs/index.cjs +340 -107
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +339 -106
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +340 -108
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +339 -107
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +9 -0
  10. package/dist/types/common/index.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +189 -13
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +270 -3
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1089 -161
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1243 -350
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +980 -255
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1174 -284
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  24. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +126 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +127 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  32. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  33. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  34. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  37. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  38. package/dist/umd/binary-tree-typed.js +337 -105
  39. package/dist/umd/binary-tree-typed.js.map +1 -1
  40. package/dist/umd/binary-tree-typed.min.js +5 -5
  41. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  42. package/package.json +2 -2
  43. package/src/common/error.ts +19 -1
  44. package/src/common/index.ts +1 -1
  45. package/src/data-structures/base/iterable-element-base.ts +3 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +99 -5
  47. package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
  48. package/src/data-structures/binary-tree/binary-tree.ts +239 -78
  49. package/src/data-structures/binary-tree/bst.ts +542 -13
  50. package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
  51. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  52. package/src/data-structures/binary-tree/tree-map.ts +1223 -261
  53. package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
  54. package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
  55. package/src/data-structures/binary-tree/tree-set.ts +1018 -99
  56. package/src/data-structures/graph/abstract-graph.ts +2 -2
  57. package/src/data-structures/graph/directed-graph.ts +71 -1
  58. package/src/data-structures/graph/undirected-graph.ts +64 -1
  59. package/src/data-structures/hash/hash-map.ts +102 -16
  60. package/src/data-structures/heap/heap.ts +153 -23
  61. package/src/data-structures/heap/max-heap.ts +2 -2
  62. package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
  63. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  64. package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
  65. package/src/data-structures/matrix/matrix.ts +65 -9
  66. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  67. package/src/data-structures/queue/deque.ts +130 -0
  68. package/src/data-structures/queue/queue.ts +109 -0
  69. package/src/data-structures/stack/stack.ts +75 -5
  70. package/src/data-structures/trie/trie.ts +86 -2
  71. package/src/interfaces/binary-tree.ts +1 -9
  72. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  73. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  74. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  75. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
@@ -10,7 +10,7 @@
10
10
  import type { Comparator } from '../../types';
11
11
  import type { TreeMapEntryCallback, TreeMapOptions, TreeMapRangeOptions, TreeMapReduceCallback } from '../../types';
12
12
  import { RedBlackTree } from './red-black-tree';
13
- import { ERR } from '../../common';
13
+ import { ERR, raise } from '../../common';
14
14
 
15
15
  /**
16
16
  * An ordered Map backed by a red-black tree.
@@ -52,7 +52,7 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
52
52
  const comparator = options.comparator ?? TreeMap.createDefaultComparator<K>();
53
53
  this.#isDefaultComparator = options.comparator === undefined;
54
54
 
55
- this.#core = new RedBlackTree<K, V>([], { comparator, isMapMode: options.isMapMode });
55
+ this.#core = new RedBlackTree<K, V>([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
56
56
 
57
57
  for (const item of entries) {
58
58
  let k: K;
@@ -64,7 +64,7 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
64
64
  } else {
65
65
  // Validate entries like native Map: each item must be a 2-tuple-like value.
66
66
  if (!Array.isArray(item) || item.length < 2) {
67
- throw new TypeError(ERR.invalidEntry('TreeMap'));
67
+ raise(TypeError, ERR.invalidEntry('TreeMap'));
68
68
  }
69
69
  k = item[0] as K;
70
70
  v = item[1] as V | undefined;
@@ -88,7 +88,7 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
88
88
  return (a: K, b: K): number => {
89
89
  if (typeof a === 'number' && typeof b === 'number') {
90
90
  /* istanbul ignore next -- _validateKey prevents NaN from entering the tree */
91
- if (Number.isNaN(a) || Number.isNaN(b)) throw new TypeError(ERR.invalidNaN('TreeMap'));
91
+ if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN('TreeMap'));
92
92
  const aa = Object.is(a, -0) ? 0 : a;
93
93
  const bb = Object.is(b, -0) ? 0 : b;
94
94
  return aa > bb ? 1 : aa < bb ? -1 : 0;
@@ -102,11 +102,11 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
102
102
  const ta = a.getTime();
103
103
  const tb = b.getTime();
104
104
  /* istanbul ignore next -- _validateKey prevents invalid Date from entering the tree */
105
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate('TreeMap'));
105
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate('TreeMap'));
106
106
  return ta > tb ? 1 : ta < tb ? -1 : 0;
107
107
  }
108
108
 
109
- throw new TypeError(ERR.comparatorRequired('TreeMap'));
109
+ raise(TypeError, ERR.comparatorRequired('TreeMap'));
110
110
  };
111
111
  }
112
112
 
@@ -114,18 +114,18 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
114
114
  if (!this.#isDefaultComparator) return;
115
115
 
116
116
  if (typeof key === 'number') {
117
- if (Number.isNaN(key)) throw new TypeError(ERR.invalidNaN('TreeMap'));
117
+ if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN('TreeMap'));
118
118
  return;
119
119
  }
120
120
 
121
121
  if (typeof key === 'string') return;
122
122
 
123
123
  if (key instanceof Date) {
124
- if (Number.isNaN(key.getTime())) throw new TypeError(ERR.invalidDate('TreeMap'));
124
+ if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeMap'));
125
125
  return;
126
126
  }
127
127
 
128
- throw new TypeError(ERR.comparatorRequired('TreeMap'));
128
+ raise(TypeError, ERR.comparatorRequired('TreeMap'));
129
129
  }
130
130
 
131
131
  /**
@@ -259,6 +259,41 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
259
259
 
260
260
 
261
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
+
262
297
 
263
298
 
264
299
 
@@ -422,6 +457,41 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
422
457
 
423
458
 
424
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
+
425
495
 
426
496
 
427
497
 
@@ -471,8 +541,11 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
471
541
  }
472
542
 
473
543
  /**
474
- * Get the value under a key.
475
- * @remarks Expected time O(log n)
544
+ * Set multiple key-value pairs at once.
545
+ * @remarks Expected time O(m log n), where m is the number of entries.
546
+ * @param entries - Iterable of `[key, value]` tuples.
547
+ * @returns Array of booleans indicating whether each entry was successfully set.
548
+
476
549
 
477
550
 
478
551
 
@@ -488,6 +561,24 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
488
561
 
489
562
 
490
563
 
564
+ * @example
565
+ * // Set multiple key-value pairs
566
+ * const tm = new TreeMap<number, string>();
567
+ * tm.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
568
+ * console.log(tm.size); // 3;
569
+ */
570
+ setMany(entries: Iterable<[K, V | undefined]>): boolean[] {
571
+ const results: boolean[] = [];
572
+ for (const [key, value] of entries) {
573
+ this._validateKey(key);
574
+ results.push(this.#core.set(key, value as V));
575
+ }
576
+ return results;
577
+ }
578
+
579
+ /**
580
+ * Get the value under a key.
581
+ * @remarks Expected time O(log n)
491
582
 
492
583
 
493
584
 
@@ -627,26 +718,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
627
718
 
628
719
 
629
720
 
630
- * @example
631
- * // Configuration registry with typed lookups
632
- * const config = new TreeMap<string, number>([
633
- * ['maxRetries', 3],
634
- * ['timeout', 5000],
635
- * ['poolSize', 10]
636
- * ]);
637
- *
638
- * console.log(config.get('timeout')); // 5000;
639
- * console.log(config.get('missing')); // undefined;
640
- * console.log(config.size); // 3;
641
- */
642
- get(key: K): V | undefined {
643
- this._validateKey(key);
644
- return this.#core.get(key);
645
- }
646
-
647
- /**
648
- * Test whether a key exists.
649
- * @remarks Expected time O(log n)
650
721
 
651
722
 
652
723
 
@@ -697,6 +768,33 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
697
768
 
698
769
 
699
770
 
771
+ * @example
772
+ * // Configuration registry with typed lookups
773
+ * const config = new TreeMap<string, number>([
774
+ * ['maxRetries', 3],
775
+ * ['timeout', 5000],
776
+ * ['poolSize', 10]
777
+ * ]);
778
+ *
779
+ * console.log(config.get('timeout')); // 5000;
780
+ * console.log(config.get('missing')); // undefined;
781
+ * console.log(config.size); // 3;
782
+ */
783
+ get(key: K): V | undefined {
784
+ this._validateKey(key);
785
+ return this.#core.get(key);
786
+ }
787
+
788
+ /**
789
+ * Test whether a key exists.
790
+ * @remarks Expected time O(log n)
791
+
792
+
793
+
794
+
795
+
796
+
797
+
700
798
 
701
799
 
702
800
 
@@ -801,26 +899,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
801
899
 
802
900
 
803
901
 
804
- * @example
805
- * // Feature flag checking
806
- * const flags = new TreeMap<string, boolean>([
807
- * ['darkMode', true],
808
- * ['betaFeature', false],
809
- * ['notifications', true]
810
- * ]);
811
- *
812
- * console.log(flags.has('darkMode')); // true;
813
- * console.log(flags.has('unknownFlag')); // false;
814
- */
815
- has(key: K): boolean {
816
- this._validateKey(key);
817
- return this.#core.has(key);
818
- }
819
-
820
- /**
821
- * Delete a key.
822
- * @returns `true` if the key existed; otherwise `false`.
823
- * @remarks Expected time O(log n)
824
902
 
825
903
 
826
904
 
@@ -899,6 +977,27 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
899
977
 
900
978
 
901
979
 
980
+ * @example
981
+ * // Feature flag checking
982
+ * const flags = new TreeMap<string, boolean>([
983
+ * ['darkMode', true],
984
+ * ['betaFeature', false],
985
+ * ['notifications', true]
986
+ * ]);
987
+ *
988
+ * console.log(flags.has('darkMode')); // true;
989
+ * console.log(flags.has('unknownFlag')); // false;
990
+ */
991
+ has(key: K): boolean {
992
+ this._validateKey(key);
993
+ return this.#core.has(key);
994
+ }
995
+
996
+ /**
997
+ * Delete a key.
998
+ * @returns `true` if the key existed; otherwise `false`.
999
+ * @remarks Expected time O(log n)
1000
+
902
1001
 
903
1002
 
904
1003
 
@@ -975,27 +1074,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
975
1074
 
976
1075
 
977
1076
 
978
- * @example
979
- * // Session management with expiry
980
- * const sessions = new TreeMap<string, number>([
981
- * ['sess_abc', Date.now()],
982
- * ['sess_def', Date.now()],
983
- * ['sess_ghi', Date.now()]
984
- * ]);
985
- *
986
- * console.log(sessions.size); // 3;
987
- * sessions.delete('sess_def');
988
- * console.log(sessions.has('sess_def')); // false;
989
- * console.log(sessions.size); // 2;
990
- */
991
- delete(key: K): boolean {
992
- this._validateKey(key);
993
- const res = this.#core.delete(key);
994
- return Array.isArray(res) && res.length > 0 && !!res[0]?.deleted;
995
- }
996
-
997
- /**
998
- * Remove all entries.
999
1077
 
1000
1078
 
1001
1079
 
@@ -1108,6 +1186,48 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1108
1186
 
1109
1187
 
1110
1188
 
1189
+ * @example
1190
+ * // Session management with expiry
1191
+ * const sessions = new TreeMap<string, number>([
1192
+ * ['sess_abc', Date.now()],
1193
+ * ['sess_def', Date.now()],
1194
+ * ['sess_ghi', Date.now()]
1195
+ * ]);
1196
+ *
1197
+ * console.log(sessions.size); // 3;
1198
+ * sessions.delete('sess_def');
1199
+ * console.log(sessions.has('sess_def')); // false;
1200
+ * console.log(sessions.size); // 2;
1201
+ */
1202
+ delete(key: K): boolean {
1203
+ this._validateKey(key);
1204
+ return this.#core.delete(key);
1205
+ }
1206
+
1207
+ /**
1208
+ * Delete all entries matching a predicate.
1209
+ * @remarks Time O(N), Space O(N)
1210
+ * @param predicate - Function (key, value, index, map) → boolean; return true to delete.
1211
+ * @returns True if at least one entry was deleted.
1212
+ */
1213
+ deleteWhere(predicate: (key: K, value: V | undefined, index: number, map: this) => boolean): boolean {
1214
+ let deleted = false;
1215
+ let index = 0;
1216
+ for (const [key, value] of this) {
1217
+ if (predicate(key, value, index++, this)) {
1218
+ this.delete(key);
1219
+ deleted = true;
1220
+ }
1221
+ }
1222
+ return deleted;
1223
+ }
1224
+
1225
+ /**
1226
+ * Remove all entries.
1227
+
1228
+
1229
+
1230
+
1111
1231
 
1112
1232
 
1113
1233
 
@@ -1139,18 +1259,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1139
1259
 
1140
1260
 
1141
1261
 
1142
- * @example
1143
- * // Remove all
1144
- * const tm = new TreeMap<number, string>([[1, 'a']]);
1145
- * tm.clear();
1146
- * console.log(tm.isEmpty()); // true;
1147
- */
1148
- clear(): void {
1149
- this.#core.clear();
1150
- }
1151
-
1152
- /**
1153
- * Iterate over keys in ascending order.
1154
1262
 
1155
1263
 
1156
1264
 
@@ -1295,24 +1403,17 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1295
1403
 
1296
1404
 
1297
1405
  * @example
1298
- * // Get sorted keys
1299
- * const tm = new TreeMap<number, string>([[3, 'c'], [1, 'a']]);
1300
- * console.log([...tm.keys()]); // [1, 3];
1406
+ * // Remove all
1407
+ * const tm = new TreeMap<number, string>([[1, 'a']]);
1408
+ * tm.clear();
1409
+ * console.log(tm.isEmpty()); // true;
1301
1410
  */
1302
- keys(): IterableIterator<K> {
1303
- return this.#core.keys();
1304
- }
1305
-
1306
- private _entryFromKey(k: K): [K, V | undefined] {
1307
- // Keys come from `keys()` which only yields existing keys.
1308
- // We allow `undefined` as a stored value (native Map behavior), so entries are typed as `[K, V | undefined]`.
1309
- return [k, this.#core.get(k)];
1411
+ clear(): void {
1412
+ this.#core.clear();
1310
1413
  }
1311
1414
 
1312
1415
  /**
1313
- * Iterate over values in ascending key order.
1314
- *
1315
- * Note: values may be `undefined` (TreeMap allows storing `undefined`, like native `Map`).
1416
+ * Iterate over keys in ascending order.
1316
1417
 
1317
1418
 
1318
1419
 
@@ -1456,22 +1557,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1456
1557
 
1457
1558
 
1458
1559
 
1459
- * @example
1460
- * // Get values in key order
1461
- * const tm = new TreeMap<number, string>([[2, 'b'], [1, 'a']]);
1462
- * console.log([...tm.values()]); // ['a', 'b'];
1463
- */
1464
- *values(): IterableIterator<V | undefined> {
1465
- for (const k of this.keys()) yield this._entryFromKey(k)[1];
1466
- }
1467
-
1468
- /**
1469
- * Iterate over `[key, value]` entries in ascending key order.
1470
- *
1471
- * Note: values may be `undefined`.
1472
-
1473
-
1474
-
1475
1560
 
1476
1561
 
1477
1562
 
@@ -1507,6 +1592,25 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1507
1592
 
1508
1593
 
1509
1594
 
1595
+ * @example
1596
+ * // Get sorted keys
1597
+ * const tm = new TreeMap<number, string>([[3, 'c'], [1, 'a']]);
1598
+ * console.log([...tm.keys()]); // [1, 3];
1599
+ */
1600
+ keys(): IterableIterator<K> {
1601
+ return this.#core.keys();
1602
+ }
1603
+
1604
+ private _entryFromKey(k: K): [K, V | undefined] {
1605
+ // Keys come from `keys()` which only yields existing keys.
1606
+ // We allow `undefined` as a stored value (native Map behavior), so entries are typed as `[K, V | undefined]`.
1607
+ return [k, this.#core.get(k)];
1608
+ }
1609
+
1610
+ /**
1611
+ * Iterate over values in ascending key order.
1612
+ *
1613
+ * Note: values may be `undefined` (TreeMap allows storing `undefined`, like native `Map`).
1510
1614
 
1511
1615
 
1512
1616
 
@@ -1612,23 +1716,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1612
1716
 
1613
1717
 
1614
1718
 
1615
- * @example
1616
- * // Iterate key-value pairs
1617
- * const tm = new TreeMap<number, string>([[3, 'c'], [1, 'a'], [2, 'b']]);
1618
- * console.log([...tm.entries()]); // [[1, 'a'], [2, 'b'], [3, 'c']];
1619
- */
1620
- *entries(): IterableIterator<[K, V | undefined]> {
1621
- for (const k of this.keys()) yield this._entryFromKey(k);
1622
- }
1623
-
1624
- [Symbol.iterator](): IterableIterator<[K, V | undefined]> {
1625
- return this.entries();
1626
- }
1627
-
1628
- /**
1629
- * Visit each entry in ascending key order.
1630
- *
1631
- * Note: callback value may be `undefined`.
1632
1719
 
1633
1720
 
1634
1721
 
@@ -1702,6 +1789,19 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1702
1789
 
1703
1790
 
1704
1791
 
1792
+ * @example
1793
+ * // Get values in key order
1794
+ * const tm = new TreeMap<number, string>([[2, 'b'], [1, 'a']]);
1795
+ * console.log([...tm.values()]); // ['a', 'b'];
1796
+ */
1797
+ *values(): IterableIterator<V | undefined> {
1798
+ for (const k of this.keys()) yield this._entryFromKey(k)[1];
1799
+ }
1800
+
1801
+ /**
1802
+ * Iterate over `[key, value]` entries in ascending key order.
1803
+ *
1804
+ * Note: values may be `undefined`.
1705
1805
 
1706
1806
 
1707
1807
 
@@ -1772,22 +1872,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1772
1872
 
1773
1873
 
1774
1874
 
1775
- * @example
1776
- * // Execute for each entry
1777
- * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
1778
- * const pairs: string[] = [];
1779
- * tm.forEach((v, k) => pairs.push(`${k}:${v}`));
1780
- * console.log(pairs); // ['1:a', '2:b'];
1781
- */
1782
- forEach(cb: (value: V | undefined, key: K, map: TreeMap<K, V>) => void, thisArg?: unknown): void {
1783
- for (const [k, v] of this) cb.call(thisArg, v, k, this);
1784
- }
1785
-
1786
- /**
1787
- * Create a new TreeMap by mapping each entry to a new `[key, value]` entry.
1788
- *
1789
- * This mirrors `RedBlackTree.map`: mapping produces a new ordered container.
1790
- * @remarks Time O(n log n) expected, Space O(n)
1791
1875
 
1792
1876
 
1793
1877
 
@@ -1896,6 +1980,23 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1896
1980
 
1897
1981
 
1898
1982
 
1983
+ * @example
1984
+ * // Iterate key-value pairs
1985
+ * const tm = new TreeMap<number, string>([[3, 'c'], [1, 'a'], [2, 'b']]);
1986
+ * console.log([...tm.entries()]); // [[1, 'a'], [2, 'b'], [3, 'c']];
1987
+ */
1988
+ *entries(): IterableIterator<[K, V | undefined]> {
1989
+ for (const k of this.keys()) yield this._entryFromKey(k);
1990
+ }
1991
+
1992
+ [Symbol.iterator](): IterableIterator<[K, V | undefined]> {
1993
+ return this.entries();
1994
+ }
1995
+
1996
+ /**
1997
+ * Visit each entry in ascending key order.
1998
+ *
1999
+ * Note: callback value may be `undefined`.
1899
2000
 
1900
2001
 
1901
2002
 
@@ -1931,31 +2032,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
1931
2032
 
1932
2033
 
1933
2034
 
1934
- * @example
1935
- * // Transform entries
1936
- * const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
1937
- * const doubled = tm.map((v, k) => [k, (v ?? 0) * 2] as [number, number]);
1938
- * console.log([...doubled.values()]); // [20, 40];
1939
- */
1940
- map<MK, MV>(
1941
- callbackfn: TreeMapEntryCallback<K, V, [MK, MV], TreeMap<K, V>>,
1942
- options: Omit<TreeMapOptions<MK, MV>, 'toEntryFn'> & { comparator?: (a: MK, b: MK) => number } = {},
1943
- thisArg?: unknown
1944
- ): TreeMap<MK, MV> {
1945
- const out = new TreeMap<MK, MV>([], options as TreeMapOptions<MK, MV>);
1946
- let index = 0;
1947
- for (const [k, v] of this) {
1948
- const [mk, mv] = thisArg === undefined
1949
- ? callbackfn(v, k, index++, this)
1950
- : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => [MK, MV]).call(thisArg, v, k, index++, this);
1951
- out.set(mk, mv);
1952
- }
1953
- return out;
1954
- }
1955
-
1956
- /**
1957
- * Create a new TreeMap containing only entries that satisfy the predicate.
1958
- * @remarks Time O(n log n) expected, Space O(n)
1959
2035
 
1960
2036
 
1961
2037
 
@@ -2100,33 +2176,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
2100
2176
 
2101
2177
 
2102
2178
  * @example
2103
- * // Filter entries
2104
- * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
2105
- * const filtered = tm.filter((v, k) => k > 1);
2106
- * console.log([...filtered.keys()]); // [2, 3];
2179
+ * // Execute for each entry
2180
+ * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
2181
+ * const pairs: string[] = [];
2182
+ * tm.forEach((v, k) => pairs.push(`${k}:${v}`));
2183
+ * console.log(pairs); // ['1:a', '2:b'];
2107
2184
  */
2108
- filter(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): TreeMap<K, V> {
2109
- const out = new TreeMap<K, V>([], { comparator: this.#userComparator });
2110
- let index = 0;
2111
- for (const [k, v] of this) {
2112
- const ok = thisArg === undefined
2113
- ? callbackfn(v, k, index++, this)
2114
- : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => boolean).call(thisArg, v, k, index++, this);
2115
- if (ok) out.set(k, v);
2116
- }
2117
- return out;
2185
+ forEach(cb: (value: V | undefined, key: K, map: TreeMap<K, V>) => void, thisArg?: unknown): void {
2186
+ for (const [k, v] of this) cb.call(thisArg, v, k, this);
2118
2187
  }
2119
2188
 
2120
2189
  /**
2121
- * Reduce entries into a single accumulator.
2122
- * @remarks Time O(n), Space O(1)
2123
-
2124
-
2125
-
2126
-
2127
-
2128
-
2129
-
2190
+ * Create a new TreeMap by mapping each entry to a new `[key, value]` entry.
2191
+ *
2192
+ * This mirrors `RedBlackTree.map`: mapping produces a new ordered container.
2193
+ * @remarks Time O(n log n) expected, Space O(n)
2130
2194
 
2131
2195
 
2132
2196
 
@@ -2263,21 +2327,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
2263
2327
 
2264
2328
 
2265
2329
 
2266
- * @example
2267
- * // Aggregate values
2268
- * const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
2269
- * console.log(tm.reduce((acc, v) => acc + (v ?? 0), 0)); // 30;
2270
- */
2271
- reduce<A>(callbackfn: TreeMapReduceCallback<K, V, A, TreeMap<K, V>>, initialValue: A): A {
2272
- let acc = initialValue;
2273
- let index = 0;
2274
- for (const [k, v] of this) acc = callbackfn(acc, v, k, index++, this);
2275
- return acc;
2276
- }
2277
-
2278
- /**
2279
- * Test whether all entries satisfy a predicate.
2280
- * @remarks Time O(n), Space O(1)
2281
2330
 
2282
2331
 
2283
2332
 
@@ -2320,6 +2369,31 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
2320
2369
 
2321
2370
 
2322
2371
 
2372
+ * @example
2373
+ * // Transform entries
2374
+ * const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
2375
+ * const doubled = tm.map((v, k) => [k, (v ?? 0) * 2] as [number, number]);
2376
+ * console.log([...doubled.values()]); // [20, 40];
2377
+ */
2378
+ map<MK, MV>(
2379
+ callbackfn: TreeMapEntryCallback<K, V, [MK, MV], TreeMap<K, V>>,
2380
+ options: Omit<TreeMapOptions<MK, MV>, 'toEntryFn'> & { comparator?: (a: MK, b: MK) => number } = {},
2381
+ thisArg?: unknown
2382
+ ): TreeMap<MK, MV> {
2383
+ const out = new TreeMap<MK, MV>([], options as TreeMapOptions<MK, MV>);
2384
+ let index = 0;
2385
+ for (const [k, v] of this) {
2386
+ const [mk, mv] = thisArg === undefined
2387
+ ? callbackfn(v, k, index++, this)
2388
+ : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => [MK, MV]).call(thisArg, v, k, index++, this);
2389
+ out.set(mk, mv);
2390
+ }
2391
+ return out;
2392
+ }
2393
+
2394
+ /**
2395
+ * Create a new TreeMap containing only entries that satisfy the predicate.
2396
+ * @remarks Time O(n log n) expected, Space O(n)
2323
2397
 
2324
2398
 
2325
2399
 
@@ -2419,25 +2493,6 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
2419
2493
 
2420
2494
 
2421
2495
 
2422
- * @example
2423
- * // Test all entries
2424
- * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
2425
- * console.log(tm.every((v, k) => k > 0)); // true;
2426
- */
2427
- every(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): boolean {
2428
- let index = 0;
2429
- for (const [k, v] of this) {
2430
- const ok = thisArg === undefined
2431
- ? callbackfn(v, k, index++, this)
2432
- : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => boolean).call(thisArg, v, k, index++, this);
2433
- if (!ok) return false;
2434
- }
2435
- return true;
2436
- }
2437
-
2438
- /**
2439
- * Test whether any entry satisfies a predicate.
2440
- * @remarks Time O(n), Space O(1)
2441
2496
 
2442
2497
 
2443
2498
 
@@ -2517,6 +2572,27 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
2517
2572
 
2518
2573
 
2519
2574
 
2575
+ * @example
2576
+ * // Filter entries
2577
+ * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
2578
+ * const filtered = tm.filter((v, k) => k > 1);
2579
+ * console.log([...filtered.keys()]); // [2, 3];
2580
+ */
2581
+ filter(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): TreeMap<K, V> {
2582
+ const out = new TreeMap<K, V>([], { comparator: this.#userComparator });
2583
+ let index = 0;
2584
+ for (const [k, v] of this) {
2585
+ const ok = thisArg === undefined
2586
+ ? callbackfn(v, k, index++, this)
2587
+ : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => boolean).call(thisArg, v, k, index++, this);
2588
+ if (ok) out.set(k, v);
2589
+ }
2590
+ return out;
2591
+ }
2592
+
2593
+ /**
2594
+ * Reduce entries into a single accumulator.
2595
+ * @remarks Time O(n), Space O(1)
2520
2596
 
2521
2597
 
2522
2598
 
@@ -2579,26 +2655,563 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
2579
2655
 
2580
2656
 
2581
2657
 
2582
- * @example
2583
- * // Test any entry
2584
- * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
2585
- * console.log(tm.some((v, k) => k === 2)); // true;
2586
- */
2587
- some(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): boolean {
2588
- let index = 0;
2589
- for (const [k, v] of this) {
2590
- const ok = thisArg === undefined
2591
- ? callbackfn(v, k, index++, this)
2592
- : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => boolean).call(thisArg, v, k, index++, this);
2593
- if (ok) return true;
2594
- }
2595
- return false;
2596
- }
2597
-
2598
- /**
2599
- * Find the first entry that satisfies a predicate.
2600
- * @returns The first matching `[key, value]` tuple, or `undefined`.
2601
- * @remarks Time O(n), Space O(1)
2658
+
2659
+
2660
+
2661
+
2662
+
2663
+
2664
+
2665
+
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2672
+
2673
+
2674
+
2675
+
2676
+
2677
+
2678
+
2679
+
2680
+
2681
+
2682
+
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
+ * @example
2775
+ * // Aggregate values
2776
+ * const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
2777
+ * console.log(tm.reduce((acc, v) => acc + (v ?? 0), 0)); // 30;
2778
+ */
2779
+ reduce<A>(callbackfn: TreeMapReduceCallback<K, V, A, TreeMap<K, V>>, initialValue: A): A {
2780
+ let acc = initialValue;
2781
+ let index = 0;
2782
+ for (const [k, v] of this) acc = callbackfn(acc, v, k, index++, this);
2783
+ return acc;
2784
+ }
2785
+
2786
+ /**
2787
+ * Test whether all entries satisfy a predicate.
2788
+ * @remarks Time O(n), Space O(1)
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
+
2838
+
2839
+
2840
+
2841
+
2842
+
2843
+
2844
+
2845
+
2846
+
2847
+
2848
+
2849
+
2850
+
2851
+
2852
+
2853
+
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
+ * @example
2966
+ * // Test all entries
2967
+ * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
2968
+ * console.log(tm.every((v, k) => k > 0)); // true;
2969
+ */
2970
+ every(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): boolean {
2971
+ let index = 0;
2972
+ for (const [k, v] of this) {
2973
+ const ok = thisArg === undefined
2974
+ ? callbackfn(v, k, index++, this)
2975
+ : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => boolean).call(thisArg, v, k, index++, this);
2976
+ if (!ok) return false;
2977
+ }
2978
+ return true;
2979
+ }
2980
+
2981
+ /**
2982
+ * Test whether any entry satisfies a predicate.
2983
+ * @remarks Time O(n), Space O(1)
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
+
3049
+
3050
+
3051
+
3052
+
3053
+
3054
+
3055
+
3056
+
3057
+
3058
+
3059
+
3060
+
3061
+
3062
+
3063
+
3064
+
3065
+
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
+ * @example
3161
+ * // Test any entry
3162
+ * const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
3163
+ * console.log(tm.some((v, k) => k === 2)); // true;
3164
+ */
3165
+ some(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): boolean {
3166
+ let index = 0;
3167
+ for (const [k, v] of this) {
3168
+ const ok = thisArg === undefined
3169
+ ? callbackfn(v, k, index++, this)
3170
+ : (callbackfn as (this: unknown, v: V | undefined, k: K, i: number, self: TreeMap<K, V>) => boolean).call(thisArg, v, k, index++, this);
3171
+ if (ok) return true;
3172
+ }
3173
+ return false;
3174
+ }
3175
+
3176
+ /**
3177
+ * Find the first entry that satisfies a predicate.
3178
+ * @returns The first matching `[key, value]` tuple, or `undefined`.
3179
+ * @remarks Time O(n), Space O(1)
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
+
2602
3215
 
2603
3216
 
2604
3217
 
@@ -2902,18 +3515,88 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
2902
3515
 
2903
3516
 
2904
3517
 
2905
- * @example
2906
- * // Convert to array
2907
- * const tm = new TreeMap<number, string>([[2, 'b'], [1, 'a']]);
2908
- * console.log(tm.toArray()); // [[1, 'a'], [2, 'b']];
2909
- */
2910
- toArray(): Array<[K, V | undefined]> {
2911
- return [...this];
2912
- }
2913
-
2914
- /**
2915
- * Print a human-friendly representation.
2916
- * @remarks Time O(n), Space O(n)
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
+ * @example
3554
+ * // Convert to array
3555
+ * const tm = new TreeMap<number, string>([[2, 'b'], [1, 'a']]);
3556
+ * console.log(tm.toArray()); // [[1, 'a'], [2, 'b']];
3557
+ */
3558
+ toArray(): Array<[K, V | undefined]> {
3559
+ return [...this];
3560
+ }
3561
+
3562
+ /**
3563
+ * Print a human-friendly representation.
3564
+ * @remarks Time O(n), Space O(n)
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
+
2917
3600
 
2918
3601
 
2919
3602
 
@@ -3096,6 +3779,13 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3096
3779
 
3097
3780
 
3098
3781
 
3782
+
3783
+
3784
+
3785
+
3786
+
3787
+
3788
+
3099
3789
 
3100
3790
 
3101
3791
 
@@ -3162,6 +3852,13 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3162
3852
 
3163
3853
 
3164
3854
 
3855
+
3856
+
3857
+
3858
+
3859
+
3860
+
3861
+
3165
3862
 
3166
3863
 
3167
3864
 
@@ -3212,6 +3909,13 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3212
3909
 
3213
3910
 
3214
3911
 
3912
+
3913
+
3914
+
3915
+
3916
+
3917
+
3918
+
3215
3919
 
3216
3920
 
3217
3921
 
@@ -3266,6 +3970,13 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3266
3970
 
3267
3971
 
3268
3972
 
3973
+
3974
+
3975
+
3976
+
3977
+
3978
+
3979
+
3269
3980
 
3270
3981
 
3271
3982
 
@@ -3397,6 +4108,34 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3397
4108
 
3398
4109
 
3399
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
+
3400
4139
 
3401
4140
 
3402
4141
 
@@ -3559,6 +4298,34 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3559
4298
 
3560
4299
 
3561
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
+
3562
4329
 
3563
4330
 
3564
4331
 
@@ -3705,6 +4472,34 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3705
4472
 
3706
4473
 
3707
4474
 
4475
+
4476
+
4477
+
4478
+
4479
+
4480
+
4481
+
4482
+
4483
+
4484
+
4485
+
4486
+
4487
+
4488
+
4489
+
4490
+
4491
+
4492
+
4493
+
4494
+
4495
+
4496
+
4497
+
4498
+
4499
+
4500
+
4501
+
4502
+
3708
4503
 
3709
4504
 
3710
4505
 
@@ -3851,6 +4646,34 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3851
4646
 
3852
4647
 
3853
4648
 
4649
+
4650
+
4651
+
4652
+
4653
+
4654
+
4655
+
4656
+
4657
+
4658
+
4659
+
4660
+
4661
+
4662
+
4663
+
4664
+
4665
+
4666
+
4667
+
4668
+
4669
+
4670
+
4671
+
4672
+
4673
+
4674
+
4675
+
4676
+
3854
4677
 
3855
4678
 
3856
4679
 
@@ -3998,6 +4821,34 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
3998
4821
 
3999
4822
 
4000
4823
 
4824
+
4825
+
4826
+
4827
+
4828
+
4829
+
4830
+
4831
+
4832
+
4833
+
4834
+
4835
+
4836
+
4837
+
4838
+
4839
+
4840
+
4841
+
4842
+
4843
+
4844
+
4845
+
4846
+
4847
+
4848
+
4849
+
4850
+
4851
+
4001
4852
 
4002
4853
 
4003
4854
 
@@ -4070,6 +4921,82 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
4070
4921
  return out;
4071
4922
  }
4072
4923
 
4924
+ // ─── Order-Statistic Methods ───────────────────────────
4925
+
4926
+ /**
4927
+ * Returns the entry at the k-th position in tree order (0-indexed).
4928
+ * @remarks Time O(log n). Requires `enableOrderStatistic: true`.
4929
+
4930
+
4931
+
4932
+ * @example
4933
+ * // Find k-th entry in a TreeMap
4934
+ * const map = new TreeMap<string, number>(
4935
+ * [['alice', 95], ['bob', 87], ['charlie', 92]],
4936
+ * { enableOrderStatistic: true }
4937
+ * );
4938
+ * console.log(map.getByRank(0)); // 'alice';
4939
+ * console.log(map.getByRank(1)); // 'bob';
4940
+ * console.log(map.getByRank(2)); // 'charlie';
4941
+ */
4942
+ getByRank(k: number): [K, V | undefined] | undefined {
4943
+ const key = this.#core.getByRank(k);
4944
+ if (key === undefined) return undefined;
4945
+ return [key, this.#core.get(key)];
4946
+ }
4947
+
4948
+ /**
4949
+ * Returns the 0-based rank of a key (number of elements that precede it in tree order).
4950
+ * @remarks Time O(log n). Requires `enableOrderStatistic: true`.
4951
+ * @example
4952
+ * // Get the rank of a key in sorted order
4953
+ * const tree = new TreeMap<number>(
4954
+ * [10, 20, 30, 40, 50],
4955
+ * { enableOrderStatistic: true }
4956
+ * );
4957
+ * console.log(tree.getRank(10)); // 0; // smallest → rank 0
4958
+ * console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
4959
+ * console.log(tree.getRank(50)); // 4; // largest → rank 4
4960
+ * console.log(tree.getRank(25)); // 2;
4961
+ */
4962
+ getRank(key: K): number {
4963
+ return this.#core.getRank(key);
4964
+ }
4965
+
4966
+ /**
4967
+ * Returns keys by rank range (0-indexed, inclusive on both ends).
4968
+ * @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
4969
+
4970
+
4971
+
4972
+
4973
+
4974
+
4975
+
4976
+
4977
+
4978
+ * @example
4979
+ * // Pagination by position in tree order
4980
+ * const tree = new TreeMap<number>(
4981
+ * [10, 20, 30, 40, 50, 60, 70, 80, 90],
4982
+ * { enableOrderStatistic: true }
4983
+ * );
4984
+ * const pageSize = 3;
4985
+ *
4986
+ * // Page 1
4987
+ * console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
4988
+ * // Page 2
4989
+ * console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
4990
+ * // Page 3
4991
+ * console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
4992
+ */
4993
+ rangeByRank(start: number, end: number): Array<[K, V | undefined]> {
4994
+ const keys = this.#core.rangeByRank(start, end);
4995
+ return keys
4996
+ .filter((k): k is K => k !== undefined)
4997
+ .map(k => [k, this.#core.get(k)] as [K, V | undefined]);
4998
+ }
4999
+
4073
5000
  /**
4074
5001
  * Creates a shallow clone of this map.
4075
5002
  * @remarks Time O(n log n), Space O(n)
@@ -4196,6 +5123,41 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
4196
5123
 
4197
5124
 
4198
5125
 
5126
+
5127
+
5128
+
5129
+
5130
+
5131
+
5132
+
5133
+
5134
+
5135
+
5136
+
5137
+
5138
+
5139
+
5140
+
5141
+
5142
+
5143
+
5144
+
5145
+
5146
+
5147
+
5148
+
5149
+
5150
+
5151
+
5152
+
5153
+
5154
+
5155
+
5156
+
5157
+
5158
+
5159
+
5160
+
4199
5161
 
4200
5162
 
4201
5163