max-priority-queue-typed 2.5.1 → 2.5.2
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.
- package/dist/cjs/index.cjs +104 -55
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +103 -54
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +104 -56
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +103 -55
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +77 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +409 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +411 -6
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +339 -6
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +391 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +51 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +45 -0
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/umd/max-priority-queue-typed.js +101 -53
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +47 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
- package/src/data-structures/binary-tree/binary-tree.ts +79 -4
- package/src/data-structures/binary-tree/bst.ts +441 -6
- package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
- package/src/data-structures/binary-tree/segment-tree.ts +18 -0
- package/src/data-structures/binary-tree/tree-map.ts +434 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
- package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
- package/src/data-structures/binary-tree/tree-set.ts +410 -8
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/undirected-graph.ts +27 -0
- package/src/data-structures/hash/hash-map.ts +35 -4
- package/src/data-structures/heap/heap.ts +46 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
- package/src/data-structures/matrix/matrix.ts +33 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +45 -0
- package/src/data-structures/queue/queue.ts +36 -0
- package/src/data-structures/stack/stack.ts +30 -0
- package/src/data-structures/trie/trie.ts +38 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- 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
|
-
|
|
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))
|
|
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))
|
|
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
|
-
|
|
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))
|
|
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()))
|
|
124
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeMap'));
|
|
125
125
|
return;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
|
|
128
|
+
raise(TypeError, ERR.comparatorRequired('TreeMap'));
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/**
|
|
@@ -264,6 +264,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
264
264
|
|
|
265
265
|
|
|
266
266
|
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
267
282
|
|
|
268
283
|
|
|
269
284
|
|
|
@@ -427,6 +442,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
427
442
|
|
|
428
443
|
|
|
429
444
|
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
430
460
|
|
|
431
461
|
|
|
432
462
|
|
|
@@ -611,6 +641,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
611
641
|
|
|
612
642
|
|
|
613
643
|
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
614
659
|
|
|
615
660
|
|
|
616
661
|
|
|
@@ -785,6 +830,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
785
830
|
|
|
786
831
|
|
|
787
832
|
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
788
848
|
|
|
789
849
|
|
|
790
850
|
|
|
@@ -959,6 +1019,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
959
1019
|
|
|
960
1020
|
|
|
961
1021
|
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
962
1037
|
|
|
963
1038
|
|
|
964
1039
|
|
|
@@ -1123,6 +1198,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
1123
1198
|
|
|
1124
1199
|
|
|
1125
1200
|
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1126
1216
|
|
|
1127
1217
|
|
|
1128
1218
|
|
|
@@ -1278,6 +1368,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
1278
1368
|
|
|
1279
1369
|
|
|
1280
1370
|
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
|
|
1281
1386
|
|
|
1282
1387
|
|
|
1283
1388
|
|
|
@@ -1440,6 +1545,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
1440
1545
|
|
|
1441
1546
|
|
|
1442
1547
|
|
|
1548
|
+
|
|
1549
|
+
|
|
1550
|
+
|
|
1551
|
+
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
|
|
1558
|
+
|
|
1559
|
+
|
|
1560
|
+
|
|
1561
|
+
|
|
1562
|
+
|
|
1443
1563
|
|
|
1444
1564
|
|
|
1445
1565
|
|
|
@@ -1596,6 +1716,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
1596
1716
|
|
|
1597
1717
|
|
|
1598
1718
|
|
|
1719
|
+
|
|
1720
|
+
|
|
1721
|
+
|
|
1722
|
+
|
|
1723
|
+
|
|
1724
|
+
|
|
1725
|
+
|
|
1726
|
+
|
|
1727
|
+
|
|
1728
|
+
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
|
|
1732
|
+
|
|
1733
|
+
|
|
1599
1734
|
|
|
1600
1735
|
|
|
1601
1736
|
|
|
@@ -1756,6 +1891,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
1756
1891
|
|
|
1757
1892
|
|
|
1758
1893
|
|
|
1894
|
+
|
|
1895
|
+
|
|
1896
|
+
|
|
1897
|
+
|
|
1898
|
+
|
|
1899
|
+
|
|
1900
|
+
|
|
1901
|
+
|
|
1902
|
+
|
|
1903
|
+
|
|
1904
|
+
|
|
1905
|
+
|
|
1906
|
+
|
|
1907
|
+
|
|
1908
|
+
|
|
1759
1909
|
|
|
1760
1910
|
|
|
1761
1911
|
|
|
@@ -1915,6 +2065,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
1915
2065
|
|
|
1916
2066
|
|
|
1917
2067
|
|
|
2068
|
+
|
|
2069
|
+
|
|
2070
|
+
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
|
|
2074
|
+
|
|
2075
|
+
|
|
2076
|
+
|
|
2077
|
+
|
|
2078
|
+
|
|
2079
|
+
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
|
|
1918
2083
|
|
|
1919
2084
|
|
|
1920
2085
|
|
|
@@ -2083,6 +2248,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
2083
2248
|
|
|
2084
2249
|
|
|
2085
2250
|
|
|
2251
|
+
|
|
2252
|
+
|
|
2253
|
+
|
|
2254
|
+
|
|
2255
|
+
|
|
2256
|
+
|
|
2257
|
+
|
|
2258
|
+
|
|
2259
|
+
|
|
2260
|
+
|
|
2261
|
+
|
|
2262
|
+
|
|
2263
|
+
|
|
2264
|
+
|
|
2265
|
+
|
|
2086
2266
|
|
|
2087
2267
|
|
|
2088
2268
|
|
|
@@ -2247,6 +2427,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
2247
2427
|
|
|
2248
2428
|
|
|
2249
2429
|
|
|
2430
|
+
|
|
2431
|
+
|
|
2432
|
+
|
|
2433
|
+
|
|
2434
|
+
|
|
2435
|
+
|
|
2436
|
+
|
|
2437
|
+
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2250
2445
|
|
|
2251
2446
|
|
|
2252
2447
|
|
|
@@ -2403,6 +2598,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
2403
2598
|
|
|
2404
2599
|
|
|
2405
2600
|
|
|
2601
|
+
|
|
2602
|
+
|
|
2603
|
+
|
|
2604
|
+
|
|
2605
|
+
|
|
2606
|
+
|
|
2607
|
+
|
|
2608
|
+
|
|
2609
|
+
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2613
|
+
|
|
2614
|
+
|
|
2615
|
+
|
|
2406
2616
|
|
|
2407
2617
|
|
|
2408
2618
|
|
|
@@ -2563,6 +2773,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
2563
2773
|
|
|
2564
2774
|
|
|
2565
2775
|
|
|
2776
|
+
|
|
2777
|
+
|
|
2778
|
+
|
|
2779
|
+
|
|
2780
|
+
|
|
2781
|
+
|
|
2782
|
+
|
|
2783
|
+
|
|
2784
|
+
|
|
2785
|
+
|
|
2786
|
+
|
|
2787
|
+
|
|
2788
|
+
|
|
2789
|
+
|
|
2790
|
+
|
|
2566
2791
|
|
|
2567
2792
|
|
|
2568
2793
|
|
|
@@ -2724,6 +2949,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
2724
2949
|
|
|
2725
2950
|
|
|
2726
2951
|
|
|
2952
|
+
|
|
2953
|
+
|
|
2954
|
+
|
|
2955
|
+
|
|
2956
|
+
|
|
2957
|
+
|
|
2958
|
+
|
|
2959
|
+
|
|
2960
|
+
|
|
2961
|
+
|
|
2962
|
+
|
|
2963
|
+
|
|
2964
|
+
|
|
2965
|
+
|
|
2966
|
+
|
|
2727
2967
|
|
|
2728
2968
|
|
|
2729
2969
|
|
|
@@ -2886,6 +3126,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
2886
3126
|
|
|
2887
3127
|
|
|
2888
3128
|
|
|
3129
|
+
|
|
3130
|
+
|
|
3131
|
+
|
|
3132
|
+
|
|
3133
|
+
|
|
3134
|
+
|
|
3135
|
+
|
|
3136
|
+
|
|
3137
|
+
|
|
3138
|
+
|
|
3139
|
+
|
|
3140
|
+
|
|
3141
|
+
|
|
3142
|
+
|
|
3143
|
+
|
|
2889
3144
|
|
|
2890
3145
|
|
|
2891
3146
|
|
|
@@ -3041,6 +3296,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3041
3296
|
|
|
3042
3297
|
|
|
3043
3298
|
|
|
3299
|
+
|
|
3300
|
+
|
|
3301
|
+
|
|
3302
|
+
|
|
3303
|
+
|
|
3304
|
+
|
|
3305
|
+
|
|
3306
|
+
|
|
3307
|
+
|
|
3308
|
+
|
|
3309
|
+
|
|
3310
|
+
|
|
3311
|
+
|
|
3312
|
+
|
|
3313
|
+
|
|
3044
3314
|
|
|
3045
3315
|
|
|
3046
3316
|
|
|
@@ -3100,6 +3370,9 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3100
3370
|
|
|
3101
3371
|
|
|
3102
3372
|
|
|
3373
|
+
|
|
3374
|
+
|
|
3375
|
+
|
|
3103
3376
|
|
|
3104
3377
|
|
|
3105
3378
|
|
|
@@ -3166,6 +3439,9 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3166
3439
|
|
|
3167
3440
|
|
|
3168
3441
|
|
|
3442
|
+
|
|
3443
|
+
|
|
3444
|
+
|
|
3169
3445
|
|
|
3170
3446
|
|
|
3171
3447
|
|
|
@@ -3216,6 +3492,9 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3216
3492
|
|
|
3217
3493
|
|
|
3218
3494
|
|
|
3495
|
+
|
|
3496
|
+
|
|
3497
|
+
|
|
3219
3498
|
|
|
3220
3499
|
|
|
3221
3500
|
|
|
@@ -3270,6 +3549,9 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3270
3549
|
|
|
3271
3550
|
|
|
3272
3551
|
|
|
3552
|
+
|
|
3553
|
+
|
|
3554
|
+
|
|
3273
3555
|
|
|
3274
3556
|
|
|
3275
3557
|
|
|
@@ -3405,6 +3687,18 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3405
3687
|
|
|
3406
3688
|
|
|
3407
3689
|
|
|
3690
|
+
|
|
3691
|
+
|
|
3692
|
+
|
|
3693
|
+
|
|
3694
|
+
|
|
3695
|
+
|
|
3696
|
+
|
|
3697
|
+
|
|
3698
|
+
|
|
3699
|
+
|
|
3700
|
+
|
|
3701
|
+
|
|
3408
3702
|
|
|
3409
3703
|
|
|
3410
3704
|
|
|
@@ -3567,6 +3861,18 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3567
3861
|
|
|
3568
3862
|
|
|
3569
3863
|
|
|
3864
|
+
|
|
3865
|
+
|
|
3866
|
+
|
|
3867
|
+
|
|
3868
|
+
|
|
3869
|
+
|
|
3870
|
+
|
|
3871
|
+
|
|
3872
|
+
|
|
3873
|
+
|
|
3874
|
+
|
|
3875
|
+
|
|
3570
3876
|
|
|
3571
3877
|
|
|
3572
3878
|
|
|
@@ -3713,6 +4019,18 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3713
4019
|
|
|
3714
4020
|
|
|
3715
4021
|
|
|
4022
|
+
|
|
4023
|
+
|
|
4024
|
+
|
|
4025
|
+
|
|
4026
|
+
|
|
4027
|
+
|
|
4028
|
+
|
|
4029
|
+
|
|
4030
|
+
|
|
4031
|
+
|
|
4032
|
+
|
|
4033
|
+
|
|
3716
4034
|
|
|
3717
4035
|
|
|
3718
4036
|
|
|
@@ -3859,6 +4177,18 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
3859
4177
|
|
|
3860
4178
|
|
|
3861
4179
|
|
|
4180
|
+
|
|
4181
|
+
|
|
4182
|
+
|
|
4183
|
+
|
|
4184
|
+
|
|
4185
|
+
|
|
4186
|
+
|
|
4187
|
+
|
|
4188
|
+
|
|
4189
|
+
|
|
4190
|
+
|
|
4191
|
+
|
|
3862
4192
|
|
|
3863
4193
|
|
|
3864
4194
|
|
|
@@ -4006,6 +4336,18 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
4006
4336
|
|
|
4007
4337
|
|
|
4008
4338
|
|
|
4339
|
+
|
|
4340
|
+
|
|
4341
|
+
|
|
4342
|
+
|
|
4343
|
+
|
|
4344
|
+
|
|
4345
|
+
|
|
4346
|
+
|
|
4347
|
+
|
|
4348
|
+
|
|
4349
|
+
|
|
4350
|
+
|
|
4009
4351
|
|
|
4010
4352
|
|
|
4011
4353
|
|
|
@@ -4070,6 +4412,74 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
4070
4412
|
return out;
|
|
4071
4413
|
}
|
|
4072
4414
|
|
|
4415
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
4416
|
+
|
|
4417
|
+
/**
|
|
4418
|
+
* Returns the entry at the k-th position in tree order (0-indexed).
|
|
4419
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
4420
|
+
|
|
4421
|
+
|
|
4422
|
+
|
|
4423
|
+
* @example
|
|
4424
|
+
* // Find k-th entry in a TreeMap
|
|
4425
|
+
* const map = new TreeMap<string, number>(
|
|
4426
|
+
* [['alice', 95], ['bob', 87], ['charlie', 92]],
|
|
4427
|
+
* { enableOrderStatistic: true }
|
|
4428
|
+
* );
|
|
4429
|
+
* console.log(map.getByRank(0)); // 'alice';
|
|
4430
|
+
* console.log(map.getByRank(1)); // 'bob';
|
|
4431
|
+
* console.log(map.getByRank(2)); // 'charlie';
|
|
4432
|
+
*/
|
|
4433
|
+
getByRank(k: number): [K, V | undefined] | undefined {
|
|
4434
|
+
const key = this.#core.getByRank(k);
|
|
4435
|
+
if (key === undefined) return undefined;
|
|
4436
|
+
return [key, this.#core.get(key)];
|
|
4437
|
+
}
|
|
4438
|
+
|
|
4439
|
+
/**
|
|
4440
|
+
* Returns the 0-based rank of a key (number of elements that precede it in tree order).
|
|
4441
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
4442
|
+
* @example
|
|
4443
|
+
* // Get the rank of a key in sorted order
|
|
4444
|
+
* const tree = new TreeMap<number>(
|
|
4445
|
+
* [10, 20, 30, 40, 50],
|
|
4446
|
+
* { enableOrderStatistic: true }
|
|
4447
|
+
* );
|
|
4448
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
4449
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
4450
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
4451
|
+
* console.log(tree.getRank(25)); // 2;
|
|
4452
|
+
*/
|
|
4453
|
+
getRank(key: K): number {
|
|
4454
|
+
return this.#core.getRank(key);
|
|
4455
|
+
}
|
|
4456
|
+
|
|
4457
|
+
/**
|
|
4458
|
+
* Returns keys by rank range (0-indexed, inclusive on both ends).
|
|
4459
|
+
* @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
|
|
4460
|
+
|
|
4461
|
+
* @example
|
|
4462
|
+
* // Pagination with rangeByRank
|
|
4463
|
+
* const tree = new TreeMap<number>(
|
|
4464
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
4465
|
+
* { enableOrderStatistic: true }
|
|
4466
|
+
* );
|
|
4467
|
+
* const pageSize = 3;
|
|
4468
|
+
*
|
|
4469
|
+
* // Page 1
|
|
4470
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
4471
|
+
* // Page 2
|
|
4472
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
4473
|
+
* // Page 3
|
|
4474
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
4475
|
+
*/
|
|
4476
|
+
rangeByRank(start: number, end: number): Array<[K, V | undefined]> {
|
|
4477
|
+
const keys = this.#core.rangeByRank(start, end);
|
|
4478
|
+
return keys
|
|
4479
|
+
.filter((k): k is K => k !== undefined)
|
|
4480
|
+
.map(k => [k, this.#core.get(k)] as [K, V | undefined]);
|
|
4481
|
+
}
|
|
4482
|
+
|
|
4073
4483
|
/**
|
|
4074
4484
|
* Creates a shallow clone of this map.
|
|
4075
4485
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -4201,6 +4611,21 @@ export class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | u
|
|
|
4201
4611
|
|
|
4202
4612
|
|
|
4203
4613
|
|
|
4614
|
+
|
|
4615
|
+
|
|
4616
|
+
|
|
4617
|
+
|
|
4618
|
+
|
|
4619
|
+
|
|
4620
|
+
|
|
4621
|
+
|
|
4622
|
+
|
|
4623
|
+
|
|
4624
|
+
|
|
4625
|
+
|
|
4626
|
+
|
|
4627
|
+
|
|
4628
|
+
|
|
4204
4629
|
|
|
4205
4630
|
|
|
4206
4631
|
|