queue-typed 2.4.3 → 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 (65) hide show
  1. package/dist/cjs/index.cjs +124 -45
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +124 -44
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +124 -46
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +124 -45
  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/base/iterable-element-base.d.ts +1 -1
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +15 -5
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
  15. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  16. package/dist/types/data-structures/graph/directed-graph.d.ts +3 -2
  17. package/dist/types/data-structures/graph/undirected-graph.d.ts +16 -2
  18. package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
  19. package/dist/types/data-structures/heap/heap.d.ts +3 -7
  20. package/dist/types/data-structures/queue/deque.d.ts +41 -1
  21. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
  22. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  23. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  24. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  25. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  26. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  27. package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
  28. package/dist/umd/queue-typed.js +122 -42
  29. package/dist/umd/queue-typed.js.map +1 -1
  30. package/dist/umd/queue-typed.min.js +1 -1
  31. package/dist/umd/queue-typed.min.js.map +1 -1
  32. package/package.json +2 -2
  33. package/src/common/error.ts +60 -0
  34. package/src/common/index.ts +2 -0
  35. package/src/data-structures/base/iterable-element-base.ts +5 -4
  36. package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
  37. package/src/data-structures/binary-tree/binary-tree.ts +121 -49
  38. package/src/data-structures/binary-tree/bst.ts +12 -4
  39. package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
  40. package/src/data-structures/binary-tree/tree-map.ts +8 -7
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
  42. package/src/data-structures/binary-tree/tree-multi-set.ts +10 -9
  43. package/src/data-structures/binary-tree/tree-set.ts +7 -6
  44. package/src/data-structures/graph/abstract-graph.ts +124 -19
  45. package/src/data-structures/graph/directed-graph.ts +8 -4
  46. package/src/data-structures/graph/map-graph.ts +1 -1
  47. package/src/data-structures/graph/undirected-graph.ts +99 -4
  48. package/src/data-structures/hash/hash-map.ts +19 -6
  49. package/src/data-structures/heap/heap.ts +21 -17
  50. package/src/data-structures/heap/max-heap.ts +2 -3
  51. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  52. package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
  53. package/src/data-structures/matrix/matrix.ts +9 -10
  54. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
  55. package/src/data-structures/queue/deque.ts +72 -4
  56. package/src/data-structures/stack/stack.ts +1 -1
  57. package/src/data-structures/trie/trie.ts +12 -6
  58. package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
  59. package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
  60. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
  61. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
  62. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  63. package/src/types/data-structures/queue/deque.ts +7 -0
  64. package/src/types/data-structures/stack/stack.ts +1 -1
  65. package/src/utils/utils.ts +4 -2
@@ -3,6 +3,54 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
 
6
+ // src/common/error.ts
7
+ var ERR = {
8
+ // Range / index
9
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
10
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
11
+ // Type / argument
12
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
13
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
14
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
15
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
16
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
17
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
18
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
19
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
20
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
21
+ // State / operation
22
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
23
+ // Matrix
24
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
25
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
26
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
27
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
28
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
29
+ };
30
+
31
+ // src/common/index.ts
32
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
33
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
34
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
35
+ return DFSOperation2;
36
+ })(DFSOperation || {});
37
+ var _Range = class _Range {
38
+ constructor(low, high, includeLow = true, includeHigh = true) {
39
+ this.low = low;
40
+ this.high = high;
41
+ this.includeLow = includeLow;
42
+ this.includeHigh = includeHigh;
43
+ }
44
+ // Determine whether a key is within the range
45
+ isInRange(key, comparator) {
46
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
47
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
48
+ return lowCheck && highCheck;
49
+ }
50
+ };
51
+ __name(_Range, "Range");
52
+ var Range = _Range;
53
+
6
54
  // src/data-structures/base/iterable-element-base.ts
7
55
  var _IterableElementBase = class _IterableElementBase {
8
56
  /**
@@ -25,7 +73,7 @@ var _IterableElementBase = class _IterableElementBase {
25
73
  if (options) {
26
74
  const { toElementFn } = options;
27
75
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
28
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
76
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
29
77
  }
30
78
  }
31
79
  /**
@@ -181,7 +229,7 @@ var _IterableElementBase = class _IterableElementBase {
181
229
  acc = initialValue;
182
230
  } else {
183
231
  const first = iter.next();
184
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
232
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
185
233
  acc = first.value;
186
234
  index = 1;
187
235
  }
@@ -654,7 +702,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
654
702
  */
655
703
  constructor(elements = [], options) {
656
704
  super(options);
657
- __publicField(this, "_equals", Object.is);
705
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
658
706
  __publicField(this, "_head");
659
707
  __publicField(this, "_tail");
660
708
  __publicField(this, "_length", 0);
@@ -742,6 +790,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
742
790
  * @returns Removed element or undefined.
743
791
  */
744
792
  pop() {
793
+ var _a;
745
794
  if (!this.head) return void 0;
746
795
  if (this.head === this.tail) {
747
796
  const value2 = this.head.value;
@@ -751,8 +800,8 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
751
800
  return value2;
752
801
  }
753
802
  let current = this.head;
754
- while (current.next !== this.tail) current = current.next;
755
- const value = this.tail.value;
803
+ while (current.next && current.next !== this.tail) current = current.next;
804
+ const value = (_a = this.tail) == null ? void 0 : _a.value;
756
805
  current.next = void 0;
757
806
  this._tail = current;
758
807
  this._length--;
@@ -840,8 +889,8 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
840
889
  at(index) {
841
890
  if (index < 0 || index >= this._length) return void 0;
842
891
  let current = this.head;
843
- for (let i = 0; i < index; i++) current = current.next;
844
- return current.value;
892
+ for (let i = 0; i < index && current; i++) current = current.next;
893
+ return current == null ? void 0 : current.value;
845
894
  }
846
895
  /**
847
896
  * Type guard: check whether the input is a SinglyLinkedListNode.
@@ -861,7 +910,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
861
910
  getNodeAt(index) {
862
911
  if (index < 0 || index >= this._length) return void 0;
863
912
  let current = this.head;
864
- for (let i = 0; i < index; i++) current = current.next;
913
+ for (let i = 0; i < index && current; i++) current = current.next;
865
914
  return current;
866
915
  }
867
916
  /**
@@ -1698,24 +1747,25 @@ __name(_LinkedListQueue, "LinkedListQueue");
1698
1747
  var LinkedListQueue = _LinkedListQueue;
1699
1748
 
1700
1749
  // src/utils/utils.ts
1701
- var rangeCheck = /* @__PURE__ */ __name((index, min, max, message = "Index out of bounds.") => {
1702
- if (index < min || index > max) throw new RangeError(message);
1750
+ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
1751
+ if (index < min || index > max) {
1752
+ throw new RangeError(message != null ? message : `Index ${index} is out of range [${min}, ${max}].`);
1753
+ }
1703
1754
  }, "rangeCheck");
1704
1755
  var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
1705
1756
 
1706
1757
  // src/data-structures/queue/deque.ts
1707
1758
  var _Deque = class _Deque extends LinearBase {
1708
- /**
1709
- * Create a Deque and optionally bulk-insert elements.
1710
- * @remarks Time O(N), Space O(N)
1711
- * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
1712
- * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
1713
- * @returns New Deque instance.
1714
- */
1715
1759
  constructor(elements = [], options) {
1716
1760
  super(options);
1717
- __publicField(this, "_equals", Object.is);
1761
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
1718
1762
  __publicField(this, "_bucketSize", 1 << 12);
1763
+ __publicField(this, "_autoCompactRatio", 0.5);
1764
+ /**
1765
+ * Counter for shift/pop operations since last compaction check.
1766
+ * Only checks ratio every `_bucketSize` operations to minimize overhead.
1767
+ */
1768
+ __publicField(this, "_compactCounter", 0);
1719
1769
  __publicField(this, "_bucketFirst", 0);
1720
1770
  __publicField(this, "_firstInBucket", 0);
1721
1771
  __publicField(this, "_bucketLast", 0);
@@ -1724,8 +1774,9 @@ var _Deque = class _Deque extends LinearBase {
1724
1774
  __publicField(this, "_buckets", []);
1725
1775
  __publicField(this, "_length", 0);
1726
1776
  if (options) {
1727
- const { bucketSize } = options;
1777
+ const { bucketSize, autoCompactRatio } = options;
1728
1778
  if (typeof bucketSize === "number") this._bucketSize = bucketSize;
1779
+ if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
1729
1780
  }
1730
1781
  let _size;
1731
1782
  if ("length" in elements) {
@@ -1750,6 +1801,24 @@ var _Deque = class _Deque extends LinearBase {
1750
1801
  get bucketSize() {
1751
1802
  return this._bucketSize;
1752
1803
  }
1804
+ /**
1805
+ * Get the auto-compaction ratio.
1806
+ * When `elements / (bucketCount * bucketSize)` drops below this ratio after
1807
+ * enough shift/pop operations, the deque auto-compacts.
1808
+ * @remarks Time O(1), Space O(1)
1809
+ * @returns Current ratio threshold. 0 means auto-compact is disabled.
1810
+ */
1811
+ get autoCompactRatio() {
1812
+ return this._autoCompactRatio;
1813
+ }
1814
+ /**
1815
+ * Set the auto-compaction ratio.
1816
+ * @remarks Time O(1), Space O(1)
1817
+ * @param value - Ratio in [0,1]. 0 disables auto-compact.
1818
+ */
1819
+ set autoCompactRatio(value) {
1820
+ this._autoCompactRatio = value;
1821
+ }
1753
1822
  /**
1754
1823
  * Get the index of the first bucket in use.
1755
1824
  * @remarks Time O(1), Space O(1)
@@ -1881,6 +1950,7 @@ var _Deque = class _Deque extends LinearBase {
1881
1950
  }
1882
1951
  }
1883
1952
  this._length -= 1;
1953
+ this._autoCompact();
1884
1954
  return element;
1885
1955
  }
1886
1956
  /**
@@ -1903,6 +1973,7 @@ var _Deque = class _Deque extends LinearBase {
1903
1973
  }
1904
1974
  }
1905
1975
  this._length -= 1;
1976
+ this._autoCompact();
1906
1977
  return element;
1907
1978
  }
1908
1979
  /**
@@ -2235,11 +2306,40 @@ var _Deque = class _Deque extends LinearBase {
2235
2306
  * @remarks Time O(N), Space O(1)
2236
2307
  * @returns void
2237
2308
  */
2309
+ /**
2310
+ * (Protected) Trigger auto-compaction if space utilization drops below threshold.
2311
+ * Only checks every `_bucketSize` operations to minimize hot-path overhead.
2312
+ * Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
2313
+ */
2314
+ _autoCompact() {
2315
+ if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
2316
+ this._compactCounter++;
2317
+ if (this._compactCounter < this._bucketSize) return;
2318
+ this._compactCounter = 0;
2319
+ const utilization = this._length / (this._bucketCount * this._bucketSize);
2320
+ if (utilization < this._autoCompactRatio) {
2321
+ this.shrinkToFit();
2322
+ }
2323
+ }
2324
+ /**
2325
+ * Compact the deque by removing unused buckets.
2326
+ * @remarks Time O(N), Space O(1)
2327
+ * @returns True if compaction was performed (bucket count reduced).
2328
+ */
2329
+ /**
2330
+ * Compact the deque by removing unused buckets.
2331
+ * @remarks Time O(N), Space O(1)
2332
+ * @returns True if compaction was performed (bucket count reduced).
2333
+ */
2334
+ compact() {
2335
+ const before = this._bucketCount;
2336
+ this.shrinkToFit();
2337
+ return this._bucketCount < before;
2338
+ }
2238
2339
  shrinkToFit() {
2239
2340
  if (this._length === 0) return;
2240
2341
  const newBuckets = [];
2241
- if (this._bucketFirst === this._bucketLast) return;
2242
- else if (this._bucketFirst < this._bucketLast) {
2342
+ if (this._bucketFirst <= this._bucketLast) {
2243
2343
  for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
2244
2344
  newBuckets.push(this._buckets[i]);
2245
2345
  }
@@ -2254,6 +2354,8 @@ var _Deque = class _Deque extends LinearBase {
2254
2354
  this._bucketFirst = 0;
2255
2355
  this._bucketLast = newBuckets.length - 1;
2256
2356
  this._buckets = newBuckets;
2357
+ this._bucketCount = newBuckets.length;
2358
+ this._compactCounter = 0;
2257
2359
  }
2258
2360
  /**
2259
2361
  * Deep clone this deque, preserving options.
@@ -2435,29 +2537,6 @@ var _Deque = class _Deque extends LinearBase {
2435
2537
  };
2436
2538
  __name(_Deque, "Deque");
2437
2539
  var Deque = _Deque;
2438
-
2439
- // src/common/index.ts
2440
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
2441
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
2442
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
2443
- return DFSOperation2;
2444
- })(DFSOperation || {});
2445
- var _Range = class _Range {
2446
- constructor(low, high, includeLow = true, includeHigh = true) {
2447
- this.low = low;
2448
- this.high = high;
2449
- this.includeLow = includeLow;
2450
- this.includeHigh = includeHigh;
2451
- }
2452
- // Determine whether a key is within the range
2453
- isInRange(key, comparator) {
2454
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
2455
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
2456
- return lowCheck && highCheck;
2457
- }
2458
- };
2459
- __name(_Range, "Range");
2460
- var Range = _Range;
2461
2540
  /**
2462
2541
  * data-structure-typed
2463
2542
  *
@@ -2466,6 +2545,6 @@ var Range = _Range;
2466
2545
  * @license MIT License
2467
2546
  */
2468
2547
 
2469
- export { DFSOperation, Deque, LinkedListQueue, Queue, Range };
2548
+ export { DFSOperation, Deque, ERR, LinkedListQueue, Queue, Range };
2470
2549
  //# sourceMappingURL=index.mjs.map
2471
2550
  //# sourceMappingURL=index.mjs.map