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 +105 -56
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +104 -55
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +105 -57
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +104 -56
  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/priority-queue-typed.js +102 -54
  38. package/dist/umd/priority-queue-typed.js.map +1 -1
  39. package/dist/umd/priority-queue-typed.min.js +1 -1
  40. package/dist/umd/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
@@ -8,6 +8,11 @@ export interface TreeMapOptions<K, V, R = [K, V]> {
8
8
  * - `false`: store values on tree nodes (Node Mode).
9
9
  */
10
10
  isMapMode?: boolean;
11
+ /**
12
+ * Enable order-statistic operations (select, rank, rangeByRank).
13
+ * When true, subtree counts are maintained on every node.
14
+ */
15
+ enableOrderStatistic?: boolean;
11
16
  /**
12
17
  * Transform raw elements into `[key, value]` entries.
13
18
  * When provided, the constructor accepts `Iterable<R>` instead of `Iterable<[K, V]>`.
@@ -8,6 +8,10 @@ export interface TreeMultiSetOptions<K, R = K> {
8
8
  * - `false`: Node Mode.
9
9
  */
10
10
  isMapMode?: boolean;
11
+ /**
12
+ * Enable order-statistic operations (select, rank, rangeByRank).
13
+ */
14
+ enableOrderStatistic?: boolean;
11
15
  /**
12
16
  * Transform raw elements into keys.
13
17
  * When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
@@ -8,6 +8,10 @@ export interface TreeSetOptions<K, R = K> {
8
8
  * - `false`: store values on tree nodes (Node Mode).
9
9
  */
10
10
  isMapMode?: boolean;
11
+ /**
12
+ * Enable order-statistic operations (select, rank, rangeByRank).
13
+ */
14
+ enableOrderStatistic?: boolean;
11
15
  /**
12
16
  * Transform raw elements into keys.
13
17
  * When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
@@ -33,9 +33,61 @@ var priorityQueueTyped = (() => {
33
33
  MinHeap: () => MinHeap,
34
34
  MinPriorityQueue: () => MinPriorityQueue,
35
35
  PriorityQueue: () => PriorityQueue,
36
- Range: () => Range
36
+ Range: () => Range,
37
+ raise: () => raise
37
38
  });
38
39
 
40
+ // src/common/error.ts
41
+ function raise(ErrorClass, message) {
42
+ throw new ErrorClass(message);
43
+ }
44
+ var ERR = {
45
+ // Range / index
46
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
47
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
48
+ // Type / argument
49
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
50
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
51
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
52
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
53
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
54
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
55
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
56
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
57
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
58
+ // State / operation
59
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
60
+ // Matrix
61
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
62
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
63
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
64
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
65
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`,
66
+ // Order statistic
67
+ orderStatisticNotEnabled: (method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`
68
+ };
69
+
70
+ // src/common/index.ts
71
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
72
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
73
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
74
+ return DFSOperation2;
75
+ })(DFSOperation || {});
76
+ var Range = class {
77
+ constructor(low, high, includeLow = true, includeHigh = true) {
78
+ this.low = low;
79
+ this.high = high;
80
+ this.includeLow = includeLow;
81
+ this.includeHigh = includeHigh;
82
+ }
83
+ // Determine whether a key is within the range
84
+ isInRange(key, comparator) {
85
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
86
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
87
+ return lowCheck && highCheck;
88
+ }
89
+ };
90
+
39
91
  // src/data-structures/base/iterable-element-base.ts
40
92
  var IterableElementBase = class {
41
93
  /**
@@ -58,7 +110,7 @@ var priorityQueueTyped = (() => {
58
110
  if (options) {
59
111
  const { toElementFn } = options;
60
112
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
61
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
113
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
62
114
  }
63
115
  }
64
116
  /**
@@ -214,7 +266,7 @@ var priorityQueueTyped = (() => {
214
266
  acc = initialValue;
215
267
  } else {
216
268
  const first = iter.next();
217
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
269
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
218
270
  acc = first.value;
219
271
  index = 1;
220
272
  }
@@ -256,52 +308,6 @@ var priorityQueueTyped = (() => {
256
308
  }
257
309
  };
258
310
 
259
- // src/common/error.ts
260
- var ERR = {
261
- // Range / index
262
- indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
263
- invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
264
- // Type / argument
265
- invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
266
- comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
267
- invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
268
- notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
269
- invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
270
- invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
271
- invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
272
- reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
273
- callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
274
- // State / operation
275
- invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
276
- // Matrix
277
- matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
278
- matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
279
- matrixNotSquare: () => "Matrix: Must be square for inversion.",
280
- matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
281
- matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
282
- };
283
-
284
- // src/common/index.ts
285
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
286
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
287
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
288
- return DFSOperation2;
289
- })(DFSOperation || {});
290
- var Range = class {
291
- constructor(low, high, includeLow = true, includeHigh = true) {
292
- this.low = low;
293
- this.high = high;
294
- this.includeLow = includeLow;
295
- this.includeHigh = includeHigh;
296
- }
297
- // Determine whether a key is within the range
298
- isInRange(key, comparator) {
299
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
300
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
301
- return lowCheck && highCheck;
302
- }
303
- };
304
-
305
311
  // src/data-structures/heap/heap.ts
306
312
  var Heap = class _Heap extends IterableElementBase {
307
313
  /**
@@ -317,7 +323,7 @@ var priorityQueueTyped = (() => {
317
323
  __publicField(this, "_elements", []);
318
324
  __publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
319
325
  if (typeof a === "object" || typeof b === "object") {
320
- throw new TypeError(ERR.comparatorRequired("Heap"));
326
+ raise(TypeError, ERR.comparatorRequired("Heap"));
321
327
  }
322
328
  if (a > b) return 1;
323
329
  if (a < b) return -1;
@@ -370,6 +376,9 @@ var priorityQueueTyped = (() => {
370
376
 
371
377
 
372
378
 
379
+
380
+
381
+
373
382
 
374
383
 
375
384
 
@@ -454,6 +463,9 @@ var priorityQueueTyped = (() => {
454
463
 
455
464
 
456
465
 
466
+
467
+
468
+
457
469
 
458
470
 
459
471
 
@@ -508,6 +520,9 @@ var priorityQueueTyped = (() => {
508
520
 
509
521
 
510
522
 
523
+
524
+
525
+
511
526
 
512
527
 
513
528
 
@@ -564,6 +579,9 @@ var priorityQueueTyped = (() => {
564
579
 
565
580
 
566
581
 
582
+
583
+
584
+
567
585
 
568
586
 
569
587
 
@@ -636,6 +654,9 @@ var priorityQueueTyped = (() => {
636
654
 
637
655
 
638
656
 
657
+
658
+
659
+
639
660
 
640
661
 
641
662
 
@@ -733,6 +754,9 @@ var priorityQueueTyped = (() => {
733
754
 
734
755
 
735
756
 
757
+
758
+
759
+
736
760
 
737
761
 
738
762
 
@@ -777,6 +801,9 @@ var priorityQueueTyped = (() => {
777
801
 
778
802
 
779
803
 
804
+
805
+
806
+
780
807
 
781
808
 
782
809
 
@@ -824,6 +851,9 @@ var priorityQueueTyped = (() => {
824
851
 
825
852
 
826
853
 
854
+
855
+
856
+
827
857
 
828
858
 
829
859
 
@@ -868,6 +898,9 @@ var priorityQueueTyped = (() => {
868
898
 
869
899
 
870
900
 
901
+
902
+
903
+
871
904
 
872
905
 
873
906
 
@@ -958,6 +991,9 @@ var priorityQueueTyped = (() => {
958
991
 
959
992
 
960
993
 
994
+
995
+
996
+
961
997
 
962
998
 
963
999
 
@@ -1035,6 +1071,9 @@ var priorityQueueTyped = (() => {
1035
1071
 
1036
1072
 
1037
1073
 
1074
+
1075
+
1076
+
1038
1077
 
1039
1078
 
1040
1079
 
@@ -1085,6 +1124,9 @@ var priorityQueueTyped = (() => {
1085
1124
 
1086
1125
 
1087
1126
 
1127
+
1128
+
1129
+
1088
1130
 
1089
1131
 
1090
1132
 
@@ -1134,6 +1176,9 @@ var priorityQueueTyped = (() => {
1134
1176
 
1135
1177
 
1136
1178
 
1179
+
1180
+
1181
+
1137
1182
 
1138
1183
 
1139
1184
 
@@ -1190,6 +1235,9 @@ var priorityQueueTyped = (() => {
1190
1235
 
1191
1236
 
1192
1237
 
1238
+
1239
+
1240
+
1193
1241
 
1194
1242
 
1195
1243
 
@@ -1202,7 +1250,7 @@ var priorityQueueTyped = (() => {
1202
1250
  */
1203
1251
  map(callback, options, thisArg) {
1204
1252
  const { comparator, toElementFn, ...rest } = options != null ? options : {};
1205
- if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
1253
+ if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
1206
1254
  const out = this._createLike([], { ...rest, comparator, toElementFn });
1207
1255
  let i = 0;
1208
1256
  for (const x of this) {
@@ -1330,7 +1378,7 @@ var priorityQueueTyped = (() => {
1330
1378
  __publicField(this, "_comparator");
1331
1379
  this.clear();
1332
1380
  this._comparator = comparator || this._defaultComparator;
1333
- if (typeof this.comparator !== "function") throw new TypeError(ERR.notAFunction("comparator", "FibonacciHeap"));
1381
+ if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
1334
1382
  }
1335
1383
  /**
1336
1384
  * Get the circular root list head.
@@ -1539,7 +1587,7 @@ var priorityQueueTyped = (() => {
1539
1587
  super(elements, {
1540
1588
  comparator: (a, b) => {
1541
1589
  if (typeof a === "object" || typeof b === "object") {
1542
- throw new TypeError(ERR.comparatorRequired("MaxHeap"));
1590
+ raise(TypeError, ERR.comparatorRequired("MaxHeap"));
1543
1591
  }
1544
1592
  if (a < b) return 1;
1545
1593
  if (a > b) return -1;
@@ -1595,7 +1643,7 @@ var priorityQueueTyped = (() => {
1595
1643
  super(elements, {
1596
1644
  comparator: (a, b) => {
1597
1645
  if (typeof a === "object" || typeof b === "object") {
1598
- throw new TypeError(ERR.comparatorRequired("MaxPriorityQueue"));
1646
+ raise(TypeError, ERR.comparatorRequired("MaxPriorityQueue"));
1599
1647
  }
1600
1648
  if (a < b) return 1;
1601
1649
  if (a > b) return -1;