linked-list-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 +62 -36
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +62 -35
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +62 -37
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +62 -36
  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/linked-list-typed.js +60 -33
  29. package/dist/umd/linked-list-typed.js.map +1 -1
  30. package/dist/umd/linked-list-typed.min.js +1 -1
  31. package/dist/umd/linked-list-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
@@ -5,6 +5,54 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
5
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
6
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
7
 
8
+ // src/common/error.ts
9
+ var ERR = {
10
+ // Range / index
11
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
12
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
13
+ // Type / argument
14
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
15
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
16
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
17
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
18
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
19
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
20
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
21
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
22
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
23
+ // State / operation
24
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
25
+ // Matrix
26
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
27
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
28
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
29
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
30
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
31
+ };
32
+
33
+ // src/common/index.ts
34
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
35
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
36
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
37
+ return DFSOperation2;
38
+ })(DFSOperation || {});
39
+ var _Range = class _Range {
40
+ constructor(low, high, includeLow = true, includeHigh = true) {
41
+ this.low = low;
42
+ this.high = high;
43
+ this.includeLow = includeLow;
44
+ this.includeHigh = includeHigh;
45
+ }
46
+ // Determine whether a key is within the range
47
+ isInRange(key, comparator) {
48
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
49
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
50
+ return lowCheck && highCheck;
51
+ }
52
+ };
53
+ __name(_Range, "Range");
54
+ var Range = _Range;
55
+
8
56
  // src/data-structures/base/iterable-element-base.ts
9
57
  var _IterableElementBase = class _IterableElementBase {
10
58
  /**
@@ -27,7 +75,7 @@ var _IterableElementBase = class _IterableElementBase {
27
75
  if (options) {
28
76
  const { toElementFn } = options;
29
77
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
30
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
78
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
31
79
  }
32
80
  }
33
81
  /**
@@ -183,7 +231,7 @@ var _IterableElementBase = class _IterableElementBase {
183
231
  acc = initialValue;
184
232
  } else {
185
233
  const first = iter.next();
186
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
234
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
187
235
  acc = first.value;
188
236
  index = 1;
189
237
  }
@@ -656,7 +704,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
656
704
  */
657
705
  constructor(elements = [], options) {
658
706
  super(options);
659
- __publicField(this, "_equals", Object.is);
707
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
660
708
  __publicField(this, "_head");
661
709
  __publicField(this, "_tail");
662
710
  __publicField(this, "_length", 0);
@@ -744,6 +792,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
744
792
  * @returns Removed element or undefined.
745
793
  */
746
794
  pop() {
795
+ var _a;
747
796
  if (!this.head) return void 0;
748
797
  if (this.head === this.tail) {
749
798
  const value2 = this.head.value;
@@ -753,8 +802,8 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
753
802
  return value2;
754
803
  }
755
804
  let current = this.head;
756
- while (current.next !== this.tail) current = current.next;
757
- const value = this.tail.value;
805
+ while (current.next && current.next !== this.tail) current = current.next;
806
+ const value = (_a = this.tail) == null ? void 0 : _a.value;
758
807
  current.next = void 0;
759
808
  this._tail = current;
760
809
  this._length--;
@@ -842,8 +891,8 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
842
891
  at(index) {
843
892
  if (index < 0 || index >= this._length) return void 0;
844
893
  let current = this.head;
845
- for (let i = 0; i < index; i++) current = current.next;
846
- return current.value;
894
+ for (let i = 0; i < index && current; i++) current = current.next;
895
+ return current == null ? void 0 : current.value;
847
896
  }
848
897
  /**
849
898
  * Type guard: check whether the input is a SinglyLinkedListNode.
@@ -863,7 +912,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
863
912
  getNodeAt(index) {
864
913
  if (index < 0 || index >= this._length) return void 0;
865
914
  let current = this.head;
866
- for (let i = 0; i < index; i++) current = current.next;
915
+ for (let i = 0; i < index && current; i++) current = current.next;
867
916
  return current;
868
917
  }
869
918
  /**
@@ -1379,7 +1428,7 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
1379
1428
  */
1380
1429
  constructor(elements = [], options) {
1381
1430
  super(options);
1382
- __publicField(this, "_equals", Object.is);
1431
+ __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
1383
1432
  __publicField(this, "_head");
1384
1433
  __publicField(this, "_tail");
1385
1434
  __publicField(this, "_length", 0);
@@ -1567,8 +1616,8 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
1567
1616
  at(index) {
1568
1617
  if (index < 0 || index >= this._length) return void 0;
1569
1618
  let current = this.head;
1570
- for (let i = 0; i < index; i++) current = current.next;
1571
- return current.value;
1619
+ for (let i = 0; i < index && current; i++) current = current.next;
1620
+ return current == null ? void 0 : current.value;
1572
1621
  }
1573
1622
  /**
1574
1623
  * Get the node reference at a given index.
@@ -1579,7 +1628,7 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
1579
1628
  getNodeAt(index) {
1580
1629
  if (index < 0 || index >= this._length) return void 0;
1581
1630
  let current = this.head;
1582
- for (let i = 0; i < index; i++) current = current.next;
1631
+ for (let i = 0; i < index && current; i++) current = current.next;
1583
1632
  return current;
1584
1633
  }
1585
1634
  /**
@@ -2079,29 +2128,6 @@ var _SkipList = class _SkipList {
2079
2128
  };
2080
2129
  __name(_SkipList, "SkipList");
2081
2130
  var SkipList = _SkipList;
2082
-
2083
- // src/common/index.ts
2084
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
2085
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
2086
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
2087
- return DFSOperation2;
2088
- })(DFSOperation || {});
2089
- var _Range = class _Range {
2090
- constructor(low, high, includeLow = true, includeHigh = true) {
2091
- this.low = low;
2092
- this.high = high;
2093
- this.includeLow = includeLow;
2094
- this.includeHigh = includeHigh;
2095
- }
2096
- // Determine whether a key is within the range
2097
- isInRange(key, comparator) {
2098
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
2099
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
2100
- return lowCheck && highCheck;
2101
- }
2102
- };
2103
- __name(_Range, "Range");
2104
- var Range = _Range;
2105
2131
  /**
2106
2132
  * data-structure-typed
2107
2133
  *
@@ -2113,6 +2139,7 @@ var Range = _Range;
2113
2139
  exports.DFSOperation = DFSOperation;
2114
2140
  exports.DoublyLinkedList = DoublyLinkedList;
2115
2141
  exports.DoublyLinkedListNode = DoublyLinkedListNode;
2142
+ exports.ERR = ERR;
2116
2143
  exports.Range = Range;
2117
2144
  exports.SinglyLinkedList = SinglyLinkedList;
2118
2145
  exports.SinglyLinkedListNode = SinglyLinkedListNode;