deque-typed 2.4.4 → 2.4.5

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 (45) hide show
  1. package/dist/cjs/index.cjs +117 -38
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +116 -37
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +117 -39
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +116 -38
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +23 -0
  10. package/dist/types/common/index.d.ts +1 -0
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +10 -0
  12. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
  13. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  14. package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
  15. package/dist/types/data-structures/graph/undirected-graph.d.ts +14 -0
  16. package/dist/types/data-structures/queue/deque.d.ts +41 -1
  17. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  18. package/dist/umd/deque-typed.js +114 -35
  19. package/dist/umd/deque-typed.js.map +1 -1
  20. package/dist/umd/deque-typed.min.js +1 -1
  21. package/dist/umd/deque-typed.min.js.map +1 -1
  22. package/package.json +2 -2
  23. package/src/common/error.ts +60 -0
  24. package/src/common/index.ts +2 -0
  25. package/src/data-structures/base/iterable-element-base.ts +3 -2
  26. package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
  27. package/src/data-structures/binary-tree/binary-tree.ts +113 -42
  28. package/src/data-structures/binary-tree/bst.ts +11 -3
  29. package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
  30. package/src/data-structures/binary-tree/tree-map.ts +8 -7
  31. package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
  32. package/src/data-structures/binary-tree/tree-multi-set.ts +5 -4
  33. package/src/data-structures/binary-tree/tree-set.ts +7 -6
  34. package/src/data-structures/graph/abstract-graph.ts +106 -1
  35. package/src/data-structures/graph/directed-graph.ts +4 -0
  36. package/src/data-structures/graph/undirected-graph.ts +95 -0
  37. package/src/data-structures/hash/hash-map.ts +13 -2
  38. package/src/data-structures/heap/heap.ts +4 -3
  39. package/src/data-structures/heap/max-heap.ts +2 -3
  40. package/src/data-structures/matrix/matrix.ts +9 -10
  41. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
  42. package/src/data-structures/queue/deque.ts +71 -3
  43. package/src/data-structures/trie/trie.ts +2 -1
  44. package/src/types/data-structures/queue/deque.ts +7 -0
  45. package/src/utils/utils.ts +4 -2
@@ -4,11 +4,62 @@ var __defProp = Object.defineProperty;
4
4
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
5
 
6
6
  // src/utils/utils.ts
7
- var rangeCheck = /* @__PURE__ */ __name((index, min, max, message = "Index out of bounds.") => {
8
- if (index < min || index > max) throw new RangeError(message);
7
+ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
8
+ if (index < min || index > max) {
9
+ throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
10
+ }
9
11
  }, "rangeCheck");
10
12
  var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
11
13
 
14
+ // src/common/error.ts
15
+ var ERR = {
16
+ // Range / index
17
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
18
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
19
+ // Type / argument
20
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
21
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
22
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
23
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
24
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
25
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
26
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
27
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
28
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
29
+ // State / operation
30
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
31
+ // Matrix
32
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
33
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
34
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
35
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
36
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
37
+ };
38
+
39
+ // src/common/index.ts
40
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
41
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
42
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
43
+ return DFSOperation2;
44
+ })(DFSOperation || {});
45
+ var Range = class {
46
+ constructor(low, high, includeLow = true, includeHigh = true) {
47
+ this.low = low;
48
+ this.high = high;
49
+ this.includeLow = includeLow;
50
+ this.includeHigh = includeHigh;
51
+ }
52
+ static {
53
+ __name(this, "Range");
54
+ }
55
+ // Determine whether a key is within the range
56
+ isInRange(key, comparator) {
57
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
58
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
59
+ return lowCheck && highCheck;
60
+ }
61
+ };
62
+
12
63
  // src/data-structures/base/iterable-element-base.ts
13
64
  var IterableElementBase = class {
14
65
  static {
@@ -27,7 +78,7 @@ var IterableElementBase = class {
27
78
  if (options) {
28
79
  const { toElementFn } = options;
29
80
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
30
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
81
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
31
82
  }
32
83
  }
33
84
  /**
@@ -190,7 +241,7 @@ var IterableElementBase = class {
190
241
  acc = initialValue;
191
242
  } else {
192
243
  const first = iter.next();
193
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
244
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
194
245
  acc = first.value;
195
246
  index = 1;
196
247
  }
@@ -431,18 +482,12 @@ var Deque = class extends LinearBase {
431
482
  __name(this, "Deque");
432
483
  }
433
484
  _equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
434
- /**
435
- * Create a Deque and optionally bulk-insert elements.
436
- * @remarks Time O(N), Space O(N)
437
- * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
438
- * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
439
- * @returns New Deque instance.
440
- */
441
485
  constructor(elements = [], options) {
442
486
  super(options);
443
487
  if (options) {
444
- const { bucketSize } = options;
488
+ const { bucketSize, autoCompactRatio } = options;
445
489
  if (typeof bucketSize === "number") this._bucketSize = bucketSize;
490
+ if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
446
491
  }
447
492
  let _size;
448
493
  if ("length" in elements) {
@@ -468,6 +513,30 @@ var Deque = class extends LinearBase {
468
513
  get bucketSize() {
469
514
  return this._bucketSize;
470
515
  }
516
+ _autoCompactRatio = 0.5;
517
+ /**
518
+ * Get the auto-compaction ratio.
519
+ * When `elements / (bucketCount * bucketSize)` drops below this ratio after
520
+ * enough shift/pop operations, the deque auto-compacts.
521
+ * @remarks Time O(1), Space O(1)
522
+ * @returns Current ratio threshold. 0 means auto-compact is disabled.
523
+ */
524
+ get autoCompactRatio() {
525
+ return this._autoCompactRatio;
526
+ }
527
+ /**
528
+ * Set the auto-compaction ratio.
529
+ * @remarks Time O(1), Space O(1)
530
+ * @param value - Ratio in [0,1]. 0 disables auto-compact.
531
+ */
532
+ set autoCompactRatio(value) {
533
+ this._autoCompactRatio = value;
534
+ }
535
+ /**
536
+ * Counter for shift/pop operations since last compaction check.
537
+ * Only checks ratio every `_bucketSize` operations to minimize overhead.
538
+ */
539
+ _compactCounter = 0;
471
540
  _bucketFirst = 0;
472
541
  /**
473
542
  * Get the index of the first bucket in use.
@@ -606,6 +675,7 @@ var Deque = class extends LinearBase {
606
675
  }
607
676
  }
608
677
  this._length -= 1;
678
+ this._autoCompact();
609
679
  return element;
610
680
  }
611
681
  /**
@@ -628,6 +698,7 @@ var Deque = class extends LinearBase {
628
698
  }
629
699
  }
630
700
  this._length -= 1;
701
+ this._autoCompact();
631
702
  return element;
632
703
  }
633
704
  /**
@@ -960,11 +1031,40 @@ var Deque = class extends LinearBase {
960
1031
  * @remarks Time O(N), Space O(1)
961
1032
  * @returns void
962
1033
  */
1034
+ /**
1035
+ * (Protected) Trigger auto-compaction if space utilization drops below threshold.
1036
+ * Only checks every `_bucketSize` operations to minimize hot-path overhead.
1037
+ * Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
1038
+ */
1039
+ _autoCompact() {
1040
+ if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
1041
+ this._compactCounter++;
1042
+ if (this._compactCounter < this._bucketSize) return;
1043
+ this._compactCounter = 0;
1044
+ const utilization = this._length / (this._bucketCount * this._bucketSize);
1045
+ if (utilization < this._autoCompactRatio) {
1046
+ this.shrinkToFit();
1047
+ }
1048
+ }
1049
+ /**
1050
+ * Compact the deque by removing unused buckets.
1051
+ * @remarks Time O(N), Space O(1)
1052
+ * @returns True if compaction was performed (bucket count reduced).
1053
+ */
1054
+ /**
1055
+ * Compact the deque by removing unused buckets.
1056
+ * @remarks Time O(N), Space O(1)
1057
+ * @returns True if compaction was performed (bucket count reduced).
1058
+ */
1059
+ compact() {
1060
+ const before = this._bucketCount;
1061
+ this.shrinkToFit();
1062
+ return this._bucketCount < before;
1063
+ }
963
1064
  shrinkToFit() {
964
1065
  if (this._length === 0) return;
965
1066
  const newBuckets = [];
966
- if (this._bucketFirst === this._bucketLast) return;
967
- else if (this._bucketFirst < this._bucketLast) {
1067
+ if (this._bucketFirst <= this._bucketLast) {
968
1068
  for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
969
1069
  newBuckets.push(this._buckets[i]);
970
1070
  }
@@ -979,6 +1079,8 @@ var Deque = class extends LinearBase {
979
1079
  this._bucketFirst = 0;
980
1080
  this._bucketLast = newBuckets.length - 1;
981
1081
  this._buckets = newBuckets;
1082
+ this._bucketCount = newBuckets.length;
1083
+ this._compactCounter = 0;
982
1084
  }
983
1085
  /**
984
1086
  * Deep clone this deque, preserving options.
@@ -1158,30 +1260,6 @@ var Deque = class extends LinearBase {
1158
1260
  }
1159
1261
  }
1160
1262
  };
1161
-
1162
- // src/common/index.ts
1163
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
1164
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
1165
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
1166
- return DFSOperation2;
1167
- })(DFSOperation || {});
1168
- var Range = class {
1169
- constructor(low, high, includeLow = true, includeHigh = true) {
1170
- this.low = low;
1171
- this.high = high;
1172
- this.includeLow = includeLow;
1173
- this.includeHigh = includeHigh;
1174
- }
1175
- static {
1176
- __name(this, "Range");
1177
- }
1178
- // Determine whether a key is within the range
1179
- isInRange(key, comparator) {
1180
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1181
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1182
- return lowCheck && highCheck;
1183
- }
1184
- };
1185
1263
  /**
1186
1264
  * data-structure-typed
1187
1265
  *
@@ -1192,6 +1270,7 @@ var Range = class {
1192
1270
 
1193
1271
  exports.DFSOperation = DFSOperation;
1194
1272
  exports.Deque = Deque;
1273
+ exports.ERR = ERR;
1195
1274
  exports.Range = Range;
1196
1275
  //# sourceMappingURL=index.cjs.map
1197
1276
  //# sourceMappingURL=index.cjs.map