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.
Files changed (73) hide show
  1. package/dist/cjs/index.cjs +104 -55
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +103 -54
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +104 -56
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +103 -55
  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 +36 -0
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +77 -2
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -0
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +409 -0
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +411 -6
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +339 -6
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +391 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  24. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +51 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +45 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  32. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  33. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  34. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  37. package/dist/umd/max-priority-queue-typed.js +101 -53
  38. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  39. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  40. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  41. package/package.json +2 -2
  42. package/src/common/error.ts +19 -1
  43. package/src/common/index.ts +1 -1
  44. package/src/data-structures/base/iterable-element-base.ts +3 -2
  45. package/src/data-structures/binary-tree/avl-tree.ts +47 -0
  46. package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
  47. package/src/data-structures/binary-tree/binary-tree.ts +79 -4
  48. package/src/data-structures/binary-tree/bst.ts +441 -6
  49. package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
  50. package/src/data-structures/binary-tree/segment-tree.ts +18 -0
  51. package/src/data-structures/binary-tree/tree-map.ts +434 -9
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
  53. package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
  54. package/src/data-structures/binary-tree/tree-set.ts +410 -8
  55. package/src/data-structures/graph/abstract-graph.ts +2 -2
  56. package/src/data-structures/graph/directed-graph.ts +30 -0
  57. package/src/data-structures/graph/undirected-graph.ts +27 -0
  58. package/src/data-structures/hash/hash-map.ts +35 -4
  59. package/src/data-structures/heap/heap.ts +46 -4
  60. package/src/data-structures/heap/max-heap.ts +2 -2
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
  63. package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
  64. package/src/data-structures/matrix/matrix.ts +33 -9
  65. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  66. package/src/data-structures/queue/deque.ts +45 -0
  67. package/src/data-structures/queue/queue.ts +36 -0
  68. package/src/data-structures/stack/stack.ts +30 -0
  69. package/src/data-structures/trie/trie.ts +38 -2
  70. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  71. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  72. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  73. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
@@ -9,7 +9,7 @@
9
9
 
10
10
  import type { Comparator } from '../../types';
11
11
  import type { TreeSetElementCallback, TreeSetOptions, TreeSetRangeOptions, TreeSetReduceCallback } from '../../types';
12
- import { ERR } from '../../common';
12
+ import { ERR, raise } from '../../common';
13
13
  import { RedBlackTree } from './red-black-tree';
14
14
 
15
15
  /**
@@ -50,7 +50,7 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
50
50
  this.#isDefaultComparator = options.comparator === undefined;
51
51
 
52
52
  // RedBlackTree expects an iterable of keys/entries/nodes/raws; for TreeSet we only accept keys.
53
- this.#core = new RedBlackTree<K, undefined>([], { comparator, isMapMode: options.isMapMode });
53
+ this.#core = new RedBlackTree<K, undefined>([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
54
54
 
55
55
  for (const item of elements) {
56
56
  const k = toElementFn ? toElementFn(item as R) : item as K;
@@ -73,7 +73,7 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
73
73
  // numbers
74
74
  if (typeof a === 'number' && typeof b === 'number') {
75
75
  /* istanbul ignore next -- _validateKey prevents NaN from entering the tree */
76
- if (Number.isNaN(a) || Number.isNaN(b)) throw new TypeError(ERR.invalidNaN('TreeSet'));
76
+ if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN('TreeSet'));
77
77
  const aa = Object.is(a, -0) ? 0 : a;
78
78
  const bb = Object.is(b, -0) ? 0 : b;
79
79
  return aa > bb ? 1 : aa < bb ? -1 : 0;
@@ -87,11 +87,11 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
87
87
  const ta = a.getTime();
88
88
  const tb = b.getTime();
89
89
  /* istanbul ignore next -- _validateKey prevents invalid Date from entering the tree */
90
- if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate('TreeSet'));
90
+ if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate('TreeSet'));
91
91
  return ta > tb ? 1 : ta < tb ? -1 : 0;
92
92
  }
93
93
 
94
- throw new TypeError(ERR.comparatorRequired('TreeSet'));
94
+ raise(TypeError, ERR.comparatorRequired('TreeSet'));
95
95
  };
96
96
  }
97
97
 
@@ -231,6 +231,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
231
231
 
232
232
 
233
233
 
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
234
249
 
235
250
 
236
251
 
@@ -259,19 +274,19 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
259
274
  if (!this.#isDefaultComparator) return;
260
275
 
261
276
  if (typeof key === 'number') {
262
- if (Number.isNaN(key)) throw new TypeError(ERR.invalidNaN('TreeSet'));
277
+ if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN('TreeSet'));
263
278
  return;
264
279
  }
265
280
 
266
281
  if (typeof key === 'string') return;
267
282
 
268
283
  if (key instanceof Date) {
269
- if (Number.isNaN(key.getTime())) throw new TypeError(ERR.invalidDate('TreeSet'));
284
+ if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate('TreeSet'));
270
285
  return;
271
286
  }
272
287
 
273
288
  // Other key types should have provided a comparator, so reaching here means misuse.
274
- throw new TypeError(ERR.comparatorRequired('TreeSet'));
289
+ raise(TypeError, ERR.comparatorRequired('TreeSet'));
275
290
  }
276
291
 
277
292
  /**
@@ -407,6 +422,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
407
422
 
408
423
 
409
424
 
425
+
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
410
440
 
411
441
 
412
442
 
@@ -583,6 +613,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
583
613
 
584
614
 
585
615
 
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+
628
+
629
+
630
+
586
631
 
587
632
 
588
633
 
@@ -753,6 +798,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
753
798
 
754
799
 
755
800
 
801
+
802
+
803
+
804
+
805
+
806
+
807
+
808
+
809
+
810
+
811
+
812
+
813
+
814
+
815
+
756
816
 
757
817
 
758
818
 
@@ -912,6 +972,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
912
972
 
913
973
 
914
974
 
975
+
976
+
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+
989
+
915
990
 
916
991
 
917
992
 
@@ -1067,6 +1142,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1067
1142
 
1068
1143
 
1069
1144
 
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1070
1160
 
1071
1161
 
1072
1162
 
@@ -1223,6 +1313,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1223
1313
 
1224
1314
 
1225
1315
 
1316
+
1317
+
1318
+
1319
+
1320
+
1321
+
1322
+
1323
+
1324
+
1325
+
1326
+
1327
+
1328
+
1329
+
1330
+
1226
1331
 
1227
1332
 
1228
1333
 
@@ -1379,6 +1484,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1379
1484
 
1380
1485
 
1381
1486
 
1487
+
1488
+
1489
+
1490
+
1491
+
1492
+
1493
+
1494
+
1495
+
1496
+
1497
+
1498
+
1499
+
1500
+
1501
+
1382
1502
 
1383
1503
 
1384
1504
 
@@ -1539,6 +1659,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1539
1659
 
1540
1660
 
1541
1661
 
1662
+
1663
+
1664
+
1665
+
1666
+
1667
+
1668
+
1669
+
1670
+
1671
+
1672
+
1673
+
1674
+
1675
+
1676
+
1542
1677
 
1543
1678
 
1544
1679
 
@@ -1698,6 +1833,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1698
1833
 
1699
1834
 
1700
1835
 
1836
+
1837
+
1838
+
1839
+
1840
+
1841
+
1842
+
1843
+
1844
+
1845
+
1846
+
1847
+
1848
+
1849
+
1850
+
1701
1851
 
1702
1852
 
1703
1853
 
@@ -1866,6 +2016,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
1866
2016
 
1867
2017
 
1868
2018
 
2019
+
2020
+
2021
+
2022
+
2023
+
2024
+
2025
+
2026
+
2027
+
2028
+
2029
+
2030
+
2031
+
2032
+
2033
+
1869
2034
 
1870
2035
 
1871
2036
 
@@ -2030,6 +2195,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2030
2195
 
2031
2196
 
2032
2197
 
2198
+
2199
+
2200
+
2201
+
2202
+
2203
+
2204
+
2205
+
2206
+
2207
+
2208
+
2209
+
2210
+
2211
+
2212
+
2033
2213
 
2034
2214
 
2035
2215
 
@@ -2187,6 +2367,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2187
2367
 
2188
2368
 
2189
2369
 
2370
+
2371
+
2372
+
2373
+
2374
+
2375
+
2376
+
2377
+
2378
+
2379
+
2380
+
2381
+
2382
+
2383
+
2384
+
2190
2385
 
2191
2386
 
2192
2387
 
@@ -2347,6 +2542,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2347
2542
 
2348
2543
 
2349
2544
 
2545
+
2546
+
2547
+
2548
+
2549
+
2550
+
2551
+
2552
+
2553
+
2554
+
2555
+
2556
+
2557
+
2558
+
2559
+
2350
2560
 
2351
2561
 
2352
2562
 
@@ -2507,6 +2717,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2507
2717
 
2508
2718
 
2509
2719
 
2720
+
2721
+
2722
+
2723
+
2724
+
2725
+
2726
+
2727
+
2728
+
2729
+
2730
+
2731
+
2732
+
2733
+
2734
+
2510
2735
 
2511
2736
 
2512
2737
 
@@ -2670,6 +2895,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2670
2895
 
2671
2896
 
2672
2897
 
2898
+
2899
+
2900
+
2901
+
2902
+
2903
+
2904
+
2905
+
2906
+
2907
+
2908
+
2909
+
2910
+
2911
+
2912
+
2673
2913
 
2674
2914
 
2675
2915
 
@@ -2825,6 +3065,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2825
3065
 
2826
3066
 
2827
3067
 
3068
+
3069
+
3070
+
3071
+
3072
+
3073
+
3074
+
3075
+
3076
+
3077
+
3078
+
3079
+
3080
+
3081
+
3082
+
2828
3083
 
2829
3084
 
2830
3085
 
@@ -2883,6 +3138,9 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2883
3138
 
2884
3139
 
2885
3140
 
3141
+
3142
+
3143
+
2886
3144
 
2887
3145
 
2888
3146
 
@@ -2949,6 +3207,9 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2949
3207
 
2950
3208
 
2951
3209
 
3210
+
3211
+
3212
+
2952
3213
 
2953
3214
 
2954
3215
 
@@ -2993,6 +3254,9 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
2993
3254
 
2994
3255
 
2995
3256
 
3257
+
3258
+
3259
+
2996
3260
 
2997
3261
 
2998
3262
 
@@ -3042,6 +3306,9 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3042
3306
 
3043
3307
 
3044
3308
 
3309
+
3310
+
3311
+
3045
3312
 
3046
3313
 
3047
3314
 
@@ -3171,6 +3438,18 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3171
3438
 
3172
3439
 
3173
3440
 
3441
+
3442
+
3443
+
3444
+
3445
+
3446
+
3447
+
3448
+
3449
+
3450
+
3451
+
3452
+
3174
3453
 
3175
3454
 
3176
3455
 
@@ -3317,6 +3596,18 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3317
3596
 
3318
3597
 
3319
3598
 
3599
+
3600
+
3601
+
3602
+
3603
+
3604
+
3605
+
3606
+
3607
+
3608
+
3609
+
3610
+
3320
3611
 
3321
3612
 
3322
3613
 
@@ -3455,6 +3746,18 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3455
3746
 
3456
3747
 
3457
3748
 
3749
+
3750
+
3751
+
3752
+
3753
+
3754
+
3755
+
3756
+
3757
+
3758
+
3759
+
3760
+
3458
3761
 
3459
3762
 
3460
3763
 
@@ -3591,6 +3894,18 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3591
3894
 
3592
3895
 
3593
3896
 
3897
+
3898
+
3899
+
3900
+
3901
+
3902
+
3903
+
3904
+
3905
+
3906
+
3907
+
3908
+
3594
3909
 
3595
3910
 
3596
3911
 
@@ -3730,6 +4045,18 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3730
4045
 
3731
4046
 
3732
4047
 
4048
+
4049
+
4050
+
4051
+
4052
+
4053
+
4054
+
4055
+
4056
+
4057
+
4058
+
4059
+
3733
4060
 
3734
4061
 
3735
4062
 
@@ -3782,6 +4109,66 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3782
4109
  return out;
3783
4110
  }
3784
4111
 
4112
+ // ─── Order-Statistic Methods ───────────────────────────
4113
+
4114
+ /**
4115
+ * Returns the element at the k-th position in tree order (0-indexed).
4116
+ * @remarks Time O(log n). Requires `enableOrderStatistic: true`.
4117
+
4118
+
4119
+
4120
+ * @example
4121
+ * // Find k-th element in a TreeSet
4122
+ * const set = new TreeSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
4123
+ * console.log(set.getByRank(0)); // 10;
4124
+ * console.log(set.getByRank(2)); // 30;
4125
+ * console.log(set.getRank(30)); // 2;
4126
+ */
4127
+ getByRank(k: number): K | undefined {
4128
+ return this.#core.getByRank(k);
4129
+ }
4130
+
4131
+ /**
4132
+ * Returns the 0-based rank of a key (number of elements that precede it in tree order).
4133
+ * @remarks Time O(log n). Requires `enableOrderStatistic: true`.
4134
+ * @example
4135
+ * // Get the rank of a key in sorted order
4136
+ * const tree = new TreeSet<number>(
4137
+ * [10, 20, 30, 40, 50],
4138
+ * { enableOrderStatistic: true }
4139
+ * );
4140
+ * console.log(tree.getRank(10)); // 0; // smallest → rank 0
4141
+ * console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
4142
+ * console.log(tree.getRank(50)); // 4; // largest → rank 4
4143
+ * console.log(tree.getRank(25)); // 2;
4144
+ */
4145
+ getRank(key: K): number {
4146
+ return this.#core.getRank(key);
4147
+ }
4148
+
4149
+ /**
4150
+ * Returns elements by rank range (0-indexed, inclusive on both ends).
4151
+ * @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
4152
+
4153
+ * @example
4154
+ * // Pagination with rangeByRank
4155
+ * const tree = new TreeSet<number>(
4156
+ * [10, 20, 30, 40, 50, 60, 70, 80, 90],
4157
+ * { enableOrderStatistic: true }
4158
+ * );
4159
+ * const pageSize = 3;
4160
+ *
4161
+ * // Page 1
4162
+ * console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
4163
+ * // Page 2
4164
+ * console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
4165
+ * // Page 3
4166
+ * console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
4167
+ */
4168
+ rangeByRank(start: number, end: number): K[] {
4169
+ return this.#core.rangeByRank(start, end).filter((k): k is K => k !== undefined);
4170
+ }
4171
+
3785
4172
  /**
3786
4173
  * Creates a shallow clone of this set.
3787
4174
  * @remarks Time O(n log n), Space O(n)
@@ -3913,6 +4300,21 @@ export class TreeSet<K = any, R = K> implements Iterable<K> {
3913
4300
 
3914
4301
 
3915
4302
 
4303
+
4304
+
4305
+
4306
+
4307
+
4308
+
4309
+
4310
+
4311
+
4312
+
4313
+
4314
+
4315
+
4316
+
4317
+
3916
4318
 
3917
4319
 
3918
4320
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  import type { DijkstraResult, EntryCallback, GraphOptions, VertexKey } from '../../types';
10
10
  import { uuidV4 } from '../../utils';
11
- import { ERR } from '../../common';
11
+ import { ERR, raise } from '../../common';
12
12
  import { IterableEntryBase } from '../base';
13
13
  import { IGraph } from '../../interfaces';
14
14
  import { Heap } from '../heap';
@@ -275,7 +275,7 @@ export abstract class AbstractGraph<
275
275
  const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
276
276
  return this._addEdge(newEdge);
277
277
  } else {
278
- throw new TypeError(ERR.invalidArgument('dest must be a Vertex or vertex key when srcOrEdge is an Edge.', 'Graph'));
278
+ raise(TypeError, ERR.invalidArgument('dest must be a Vertex or vertex key when srcOrEdge is an Edge.', 'Graph'));
279
279
  }
280
280
  }
281
281
  }