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,61 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
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 != null ? 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 _Range {
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
+ // Determine whether a key is within the range
53
+ isInRange(key, comparator) {
54
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
55
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
56
+ return lowCheck && highCheck;
57
+ }
58
+ };
59
+ __name(_Range, "Range");
60
+ var Range = _Range;
61
+
12
62
  // src/data-structures/base/iterable-element-base.ts
13
63
  var _IterableElementBase = class _IterableElementBase {
14
64
  /**
@@ -31,7 +81,7 @@ var _IterableElementBase = class _IterableElementBase {
31
81
  if (options) {
32
82
  const { toElementFn } = options;
33
83
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
34
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
84
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
35
85
  }
36
86
  }
37
87
  /**
@@ -187,7 +237,7 @@ var _IterableElementBase = class _IterableElementBase {
187
237
  acc = initialValue;
188
238
  } else {
189
239
  const first = iter.next();
190
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
240
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
191
241
  acc = first.value;
192
242
  index = 1;
193
243
  }
@@ -425,17 +475,16 @@ var LinearBase = _LinearBase;
425
475
 
426
476
  // src/data-structures/queue/deque.ts
427
477
  var _Deque = class _Deque extends LinearBase {
428
- /**
429
- * Create a Deque and optionally bulk-insert elements.
430
- * @remarks Time O(N), Space O(N)
431
- * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
432
- * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
433
- * @returns New Deque instance.
434
- */
435
478
  constructor(elements = [], options) {
436
479
  super(options);
437
480
  __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
438
481
  __publicField(this, "_bucketSize", 1 << 12);
482
+ __publicField(this, "_autoCompactRatio", 0.5);
483
+ /**
484
+ * Counter for shift/pop operations since last compaction check.
485
+ * Only checks ratio every `_bucketSize` operations to minimize overhead.
486
+ */
487
+ __publicField(this, "_compactCounter", 0);
439
488
  __publicField(this, "_bucketFirst", 0);
440
489
  __publicField(this, "_firstInBucket", 0);
441
490
  __publicField(this, "_bucketLast", 0);
@@ -444,8 +493,9 @@ var _Deque = class _Deque extends LinearBase {
444
493
  __publicField(this, "_buckets", []);
445
494
  __publicField(this, "_length", 0);
446
495
  if (options) {
447
- const { bucketSize } = options;
496
+ const { bucketSize, autoCompactRatio } = options;
448
497
  if (typeof bucketSize === "number") this._bucketSize = bucketSize;
498
+ if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
449
499
  }
450
500
  let _size;
451
501
  if ("length" in elements) {
@@ -470,6 +520,24 @@ var _Deque = class _Deque extends LinearBase {
470
520
  get bucketSize() {
471
521
  return this._bucketSize;
472
522
  }
523
+ /**
524
+ * Get the auto-compaction ratio.
525
+ * When `elements / (bucketCount * bucketSize)` drops below this ratio after
526
+ * enough shift/pop operations, the deque auto-compacts.
527
+ * @remarks Time O(1), Space O(1)
528
+ * @returns Current ratio threshold. 0 means auto-compact is disabled.
529
+ */
530
+ get autoCompactRatio() {
531
+ return this._autoCompactRatio;
532
+ }
533
+ /**
534
+ * Set the auto-compaction ratio.
535
+ * @remarks Time O(1), Space O(1)
536
+ * @param value - Ratio in [0,1]. 0 disables auto-compact.
537
+ */
538
+ set autoCompactRatio(value) {
539
+ this._autoCompactRatio = value;
540
+ }
473
541
  /**
474
542
  * Get the index of the first bucket in use.
475
543
  * @remarks Time O(1), Space O(1)
@@ -601,6 +669,7 @@ var _Deque = class _Deque extends LinearBase {
601
669
  }
602
670
  }
603
671
  this._length -= 1;
672
+ this._autoCompact();
604
673
  return element;
605
674
  }
606
675
  /**
@@ -623,6 +692,7 @@ var _Deque = class _Deque extends LinearBase {
623
692
  }
624
693
  }
625
694
  this._length -= 1;
695
+ this._autoCompact();
626
696
  return element;
627
697
  }
628
698
  /**
@@ -955,11 +1025,40 @@ var _Deque = class _Deque extends LinearBase {
955
1025
  * @remarks Time O(N), Space O(1)
956
1026
  * @returns void
957
1027
  */
1028
+ /**
1029
+ * (Protected) Trigger auto-compaction if space utilization drops below threshold.
1030
+ * Only checks every `_bucketSize` operations to minimize hot-path overhead.
1031
+ * Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
1032
+ */
1033
+ _autoCompact() {
1034
+ if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
1035
+ this._compactCounter++;
1036
+ if (this._compactCounter < this._bucketSize) return;
1037
+ this._compactCounter = 0;
1038
+ const utilization = this._length / (this._bucketCount * this._bucketSize);
1039
+ if (utilization < this._autoCompactRatio) {
1040
+ this.shrinkToFit();
1041
+ }
1042
+ }
1043
+ /**
1044
+ * Compact the deque by removing unused buckets.
1045
+ * @remarks Time O(N), Space O(1)
1046
+ * @returns True if compaction was performed (bucket count reduced).
1047
+ */
1048
+ /**
1049
+ * Compact the deque by removing unused buckets.
1050
+ * @remarks Time O(N), Space O(1)
1051
+ * @returns True if compaction was performed (bucket count reduced).
1052
+ */
1053
+ compact() {
1054
+ const before = this._bucketCount;
1055
+ this.shrinkToFit();
1056
+ return this._bucketCount < before;
1057
+ }
958
1058
  shrinkToFit() {
959
1059
  if (this._length === 0) return;
960
1060
  const newBuckets = [];
961
- if (this._bucketFirst === this._bucketLast) return;
962
- else if (this._bucketFirst < this._bucketLast) {
1061
+ if (this._bucketFirst <= this._bucketLast) {
963
1062
  for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
964
1063
  newBuckets.push(this._buckets[i]);
965
1064
  }
@@ -974,6 +1073,8 @@ var _Deque = class _Deque extends LinearBase {
974
1073
  this._bucketFirst = 0;
975
1074
  this._bucketLast = newBuckets.length - 1;
976
1075
  this._buckets = newBuckets;
1076
+ this._bucketCount = newBuckets.length;
1077
+ this._compactCounter = 0;
977
1078
  }
978
1079
  /**
979
1080
  * Deep clone this deque, preserving options.
@@ -1155,29 +1256,6 @@ var _Deque = class _Deque extends LinearBase {
1155
1256
  };
1156
1257
  __name(_Deque, "Deque");
1157
1258
  var Deque = _Deque;
1158
-
1159
- // src/common/index.ts
1160
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
1161
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
1162
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
1163
- return DFSOperation2;
1164
- })(DFSOperation || {});
1165
- var _Range = class _Range {
1166
- constructor(low, high, includeLow = true, includeHigh = true) {
1167
- this.low = low;
1168
- this.high = high;
1169
- this.includeLow = includeLow;
1170
- this.includeHigh = includeHigh;
1171
- }
1172
- // Determine whether a key is within the range
1173
- isInRange(key, comparator) {
1174
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1175
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1176
- return lowCheck && highCheck;
1177
- }
1178
- };
1179
- __name(_Range, "Range");
1180
- var Range = _Range;
1181
1259
  /**
1182
1260
  * data-structure-typed
1183
1261
  *
@@ -1186,6 +1264,6 @@ var Range = _Range;
1186
1264
  * @license MIT License
1187
1265
  */
1188
1266
 
1189
- export { DFSOperation, Deque, Range };
1267
+ export { DFSOperation, Deque, ERR, Range };
1190
1268
  //# sourceMappingURL=index.mjs.map
1191
1269
  //# sourceMappingURL=index.mjs.map