queue-typed 2.4.5 → 2.5.0

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 (76) hide show
  1. package/README.md +6 -48
  2. package/dist/cjs/index.cjs +997 -255
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +996 -254
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +997 -255
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +996 -254
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +272 -65
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/umd/queue-typed.js +994 -252
  42. package/dist/umd/queue-typed.js.map +1 -1
  43. package/dist/umd/queue-typed.min.js +1 -1
  44. package/dist/umd/queue-typed.min.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/data-structures/base/iterable-element-base.ts +4 -5
  47. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  48. package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
  49. package/src/data-structures/binary-tree/binary-tree.ts +429 -79
  50. package/src/data-structures/binary-tree/bst.ts +335 -34
  51. package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
  52. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  53. package/src/data-structures/binary-tree/tree-map.ts +1284 -6
  54. package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
  55. package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
  56. package/src/data-structures/binary-tree/tree-set.ts +1136 -9
  57. package/src/data-structures/graph/directed-graph.ts +219 -47
  58. package/src/data-structures/graph/map-graph.ts +59 -1
  59. package/src/data-structures/graph/undirected-graph.ts +204 -59
  60. package/src/data-structures/hash/hash-map.ts +230 -77
  61. package/src/data-structures/heap/heap.ts +287 -99
  62. package/src/data-structures/heap/max-heap.ts +46 -0
  63. package/src/data-structures/heap/min-heap.ts +59 -0
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  65. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  66. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  67. package/src/data-structures/matrix/matrix.ts +416 -12
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  70. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  71. package/src/data-structures/queue/deque.ts +272 -65
  72. package/src/data-structures/queue/queue.ts +211 -42
  73. package/src/data-structures/stack/stack.ts +174 -32
  74. package/src/data-structures/trie/trie.ts +213 -43
  75. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  76. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
@@ -1,55 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
- // src/common/error.ts
5
- var ERR = {
6
- // Range / index
7
- indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
8
- invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
9
- // Type / argument
10
- invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
11
- comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
12
- invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
13
- notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
14
- invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
15
- invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
16
- invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
17
- reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
18
- callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
19
- // State / operation
20
- invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
21
- // Matrix
22
- matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
23
- matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
24
- matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
25
- matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
26
- matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
27
- };
28
-
29
- // src/common/index.ts
30
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
31
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
32
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
33
- return DFSOperation2;
34
- })(DFSOperation || {});
35
- var Range = class {
36
- constructor(low, high, includeLow = true, includeHigh = true) {
37
- this.low = low;
38
- this.high = high;
39
- this.includeLow = includeLow;
40
- this.includeHigh = includeHigh;
41
- }
42
- static {
43
- __name(this, "Range");
44
- }
45
- // Determine whether a key is within the range
46
- isInRange(key, comparator) {
47
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
48
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
49
- return lowCheck && highCheck;
50
- }
51
- };
52
-
53
4
  // src/data-structures/base/iterable-element-base.ts
54
5
  var IterableElementBase = class {
55
6
  static {
@@ -68,7 +19,7 @@ var IterableElementBase = class {
68
19
  if (options) {
69
20
  const { toElementFn } = options;
70
21
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
71
- else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
22
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
72
23
  }
73
24
  }
74
25
  /**
@@ -231,7 +182,7 @@ var IterableElementBase = class {
231
182
  acc = initialValue;
232
183
  } else {
233
184
  const first = iter.next();
234
- if (first.done) throw new TypeError(ERR.reduceEmpty());
185
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
235
186
  acc = first.value;
236
187
  index = 1;
237
188
  }
@@ -772,11 +723,37 @@ var SinglyLinkedList = class extends LinearLinkedBase {
772
723
  return list;
773
724
  }
774
725
  /**
775
- * Append an element/node to the tail.
776
- * @remarks Time O(1), Space O(1)
777
- * @param elementOrNode - Element or node to append.
778
- * @returns True when appended.
779
- */
726
+ * Append an element/node to the tail.
727
+ * @remarks Time O(1), Space O(1)
728
+ * @param elementOrNode - Element or node to append.
729
+ * @returns True when appended.
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+
738
+
739
+
740
+
741
+ * @example
742
+ * // basic SinglyLinkedList creation and push operation
743
+ * // Create a simple SinglyLinkedList with initial values
744
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
745
+ *
746
+ * // Verify the list maintains insertion order
747
+ * console.log([...list]); // [1, 2, 3, 4, 5];
748
+ *
749
+ * // Check length
750
+ * console.log(list.length); // 5;
751
+ *
752
+ * // Push a new element to the end
753
+ * list.push(6);
754
+ * console.log(list.length); // 6;
755
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
756
+ */
780
757
  push(elementOrNode) {
781
758
  const newNode = this._ensureNode(elementOrNode);
782
759
  if (!this.head) {
@@ -790,10 +767,36 @@ var SinglyLinkedList = class extends LinearLinkedBase {
790
767
  return true;
791
768
  }
792
769
  /**
793
- * Remove and return the tail element.
794
- * @remarks Time O(N), Space O(1)
795
- * @returns Removed element or undefined.
796
- */
770
+ * Remove and return the tail element.
771
+ * @remarks Time O(N), Space O(1)
772
+ * @returns Removed element or undefined.
773
+
774
+
775
+
776
+
777
+
778
+
779
+
780
+
781
+
782
+
783
+
784
+ * @example
785
+ * // SinglyLinkedList pop and shift operations
786
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
787
+ *
788
+ * // Pop removes from the end
789
+ * const last = list.pop();
790
+ * console.log(last); // 50;
791
+ *
792
+ * // Shift removes from the beginning
793
+ * const first = list.shift();
794
+ * console.log(first); // 10;
795
+ *
796
+ * // Verify remaining elements
797
+ * console.log([...list]); // [20, 30, 40];
798
+ * console.log(list.length); // 3;
799
+ */
797
800
  pop() {
798
801
  if (!this.head) return void 0;
799
802
  if (this.head === this.tail) {
@@ -812,10 +815,26 @@ var SinglyLinkedList = class extends LinearLinkedBase {
812
815
  return value;
813
816
  }
814
817
  /**
815
- * Remove and return the head element.
816
- * @remarks Time O(1), Space O(1)
817
- * @returns Removed element or undefined.
818
- */
818
+ * Remove and return the head element.
819
+ * @remarks Time O(1), Space O(1)
820
+ * @returns Removed element or undefined.
821
+
822
+
823
+
824
+
825
+
826
+
827
+
828
+
829
+
830
+
831
+
832
+ * @example
833
+ * // Remove from the front
834
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
835
+ * console.log(list.shift()); // 10;
836
+ * console.log(list.length); // 2;
837
+ */
819
838
  shift() {
820
839
  if (!this.head) return void 0;
821
840
  const removed = this.head;
@@ -825,11 +844,42 @@ var SinglyLinkedList = class extends LinearLinkedBase {
825
844
  return removed.value;
826
845
  }
827
846
  /**
828
- * Prepend an element/node to the head.
829
- * @remarks Time O(1), Space O(1)
830
- * @param elementOrNode - Element or node to prepend.
831
- * @returns True when prepended.
832
- */
847
+ * Prepend an element/node to the head.
848
+ * @remarks Time O(1), Space O(1)
849
+ * @param elementOrNode - Element or node to prepend.
850
+ * @returns True when prepended.
851
+
852
+
853
+
854
+
855
+
856
+
857
+
858
+
859
+
860
+
861
+
862
+ * @example
863
+ * // SinglyLinkedList unshift and forward traversal
864
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
865
+ *
866
+ * // Unshift adds to the beginning
867
+ * list.unshift(10);
868
+ * console.log([...list]); // [10, 20, 30, 40];
869
+ *
870
+ * // Access elements (forward traversal only for singly linked)
871
+ * const second = list.at(1);
872
+ * console.log(second); // 20;
873
+ *
874
+ * // SinglyLinkedList allows forward iteration only
875
+ * const elements: number[] = [];
876
+ * for (const item of list) {
877
+ * elements.push(item);
878
+ * }
879
+ * console.log(elements); // [10, 20, 30, 40];
880
+ *
881
+ * console.log(list.length); // 4;
882
+ */
833
883
  unshift(elementOrNode) {
834
884
  const newNode = this._ensureNode(elementOrNode);
835
885
  if (!this.head) {
@@ -885,11 +935,28 @@ var SinglyLinkedList = class extends LinearLinkedBase {
885
935
  return void 0;
886
936
  }
887
937
  /**
888
- * Get the element at a given index.
889
- * @remarks Time O(N), Space O(1)
890
- * @param index - Zero-based index.
891
- * @returns Element or undefined.
892
- */
938
+ * Get the element at a given index.
939
+ * @remarks Time O(N), Space O(1)
940
+ * @param index - Zero-based index.
941
+ * @returns Element or undefined.
942
+
943
+
944
+
945
+
946
+
947
+
948
+
949
+
950
+
951
+
952
+
953
+ * @example
954
+ * // Access element by index
955
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
956
+ * console.log(list.at(0)); // 'a';
957
+ * console.log(list.at(2)); // 'c';
958
+ * console.log(list.at(3)); // 'd';
959
+ */
893
960
  at(index) {
894
961
  if (index < 0 || index >= this._length) return void 0;
895
962
  let current = this.head;
@@ -906,11 +973,23 @@ var SinglyLinkedList = class extends LinearLinkedBase {
906
973
  return elementNodeOrPredicate instanceof SinglyLinkedListNode;
907
974
  }
908
975
  /**
909
- * Get the node reference at a given index.
910
- * @remarks Time O(N), Space O(1)
911
- * @param index - Zero-based index.
912
- * @returns Node or undefined.
913
- */
976
+ * Get the node reference at a given index.
977
+ * @remarks Time O(N), Space O(1)
978
+ * @param index - Zero-based index.
979
+ * @returns Node or undefined.
980
+
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+ * @example
989
+ * // Get node at index
990
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
991
+ * console.log(list.getNodeAt(1)?.value); // 'b';
992
+ */
914
993
  getNodeAt(index) {
915
994
  if (index < 0 || index >= this._length) return void 0;
916
995
  let current = this.head;
@@ -918,11 +997,24 @@ var SinglyLinkedList = class extends LinearLinkedBase {
918
997
  return current;
919
998
  }
920
999
  /**
921
- * Delete the element at an index.
922
- * @remarks Time O(N), Space O(1)
923
- * @param index - Zero-based index.
924
- * @returns Removed element or undefined.
925
- */
1000
+ * Delete the element at an index.
1001
+ * @remarks Time O(N), Space O(1)
1002
+ * @param index - Zero-based index.
1003
+ * @returns Removed element or undefined.
1004
+
1005
+
1006
+
1007
+
1008
+
1009
+
1010
+
1011
+
1012
+ * @example
1013
+ * // Remove by index
1014
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1015
+ * list.deleteAt(1);
1016
+ * console.log(list.toArray()); // ['a', 'c'];
1017
+ */
926
1018
  deleteAt(index) {
927
1019
  if (index < 0 || index >= this._length) return void 0;
928
1020
  if (index === 0) return this.shift();
@@ -935,11 +1027,24 @@ var SinglyLinkedList = class extends LinearLinkedBase {
935
1027
  return value;
936
1028
  }
937
1029
  /**
938
- * Delete the first match by value/node.
939
- * @remarks Time O(N), Space O(1)
940
- * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
941
- * @returns True if removed.
942
- */
1030
+ * Delete the first match by value/node.
1031
+ * @remarks Time O(N), Space O(1)
1032
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
1033
+ * @returns True if removed.
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+ * @example
1043
+ * // Remove first occurrence
1044
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
1045
+ * list.delete(2);
1046
+ * console.log(list.toArray()); // [1, 3, 2];
1047
+ */
943
1048
  delete(elementOrNode) {
944
1049
  if (elementOrNode === void 0 || !this.head) return false;
945
1050
  const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
@@ -956,12 +1061,25 @@ var SinglyLinkedList = class extends LinearLinkedBase {
956
1061
  return true;
957
1062
  }
958
1063
  /**
959
- * Insert a new element/node at an index, shifting following nodes.
960
- * @remarks Time O(N), Space O(1)
961
- * @param index - Zero-based index.
962
- * @param newElementOrNode - Element or node to insert.
963
- * @returns True if inserted.
964
- */
1064
+ * Insert a new element/node at an index, shifting following nodes.
1065
+ * @remarks Time O(N), Space O(1)
1066
+ * @param index - Zero-based index.
1067
+ * @param newElementOrNode - Element or node to insert.
1068
+ * @returns True if inserted.
1069
+
1070
+
1071
+
1072
+
1073
+
1074
+
1075
+
1076
+
1077
+ * @example
1078
+ * // Insert at index
1079
+ * const list = new SinglyLinkedList<number>([1, 3]);
1080
+ * list.addAt(1, 2);
1081
+ * console.log(list.toArray()); // [1, 2, 3];
1082
+ */
965
1083
  addAt(index, newElementOrNode) {
966
1084
  if (index < 0 || index > this._length) return false;
967
1085
  if (index === 0) return this.unshift(newElementOrNode);
@@ -987,28 +1105,70 @@ var SinglyLinkedList = class extends LinearLinkedBase {
987
1105
  return true;
988
1106
  }
989
1107
  /**
990
- * Check whether the list is empty.
991
- * @remarks Time O(1), Space O(1)
992
- * @returns True if length is 0.
993
- */
1108
+ * Check whether the list is empty.
1109
+ * @remarks Time O(1), Space O(1)
1110
+ * @returns True if length is 0.
1111
+
1112
+
1113
+
1114
+
1115
+
1116
+
1117
+
1118
+
1119
+
1120
+ * @example
1121
+ * // Check empty
1122
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
1123
+ */
994
1124
  isEmpty() {
995
1125
  return this._length === 0;
996
1126
  }
997
1127
  /**
998
- * Remove all nodes and reset length.
999
- * @remarks Time O(N), Space O(1)
1000
- * @returns void
1001
- */
1128
+ * Remove all nodes and reset length.
1129
+ * @remarks Time O(N), Space O(1)
1130
+ * @returns void
1131
+
1132
+
1133
+
1134
+
1135
+
1136
+
1137
+
1138
+
1139
+
1140
+ * @example
1141
+ * // Remove all
1142
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1143
+ * list.clear();
1144
+ * console.log(list.isEmpty()); // true;
1145
+ */
1002
1146
  clear() {
1003
1147
  this._head = void 0;
1004
1148
  this._tail = void 0;
1005
1149
  this._length = 0;
1006
1150
  }
1007
1151
  /**
1008
- * Reverse the list in place.
1009
- * @remarks Time O(N), Space O(1)
1010
- * @returns This list.
1011
- */
1152
+ * Reverse the list in place.
1153
+ * @remarks Time O(N), Space O(1)
1154
+ * @returns This list.
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+
1166
+ * @example
1167
+ * // Reverse the list in-place
1168
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
1169
+ * list.reverse();
1170
+ * console.log([...list]); // [4, 3, 2, 1];
1171
+ */
1012
1172
  reverse() {
1013
1173
  if (!this.head || this.head === this.tail) return this;
1014
1174
  let prev;
@@ -1183,22 +1343,64 @@ var SinglyLinkedList = class extends LinearLinkedBase {
1183
1343
  return false;
1184
1344
  }
1185
1345
  /**
1186
- * Deep clone this list (values are copied by reference).
1187
- * @remarks Time O(N), Space O(N)
1188
- * @returns A new list with the same element sequence.
1189
- */
1346
+ * Deep clone this list (values are copied by reference).
1347
+ * @remarks Time O(N), Space O(N)
1348
+ * @returns A new list with the same element sequence.
1349
+
1350
+
1351
+
1352
+
1353
+
1354
+
1355
+
1356
+
1357
+
1358
+ * @example
1359
+ * // Deep copy
1360
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1361
+ * const copy = list.clone();
1362
+ * copy.pop();
1363
+ * console.log(list.length); // 3;
1364
+ * console.log(copy.length); // 2;
1365
+ */
1190
1366
  clone() {
1191
1367
  const out = this._createInstance();
1192
1368
  for (const v of this) out.push(v);
1193
1369
  return out;
1194
1370
  }
1195
1371
  /**
1196
- * Filter values into a new list of the same class.
1197
- * @remarks Time O(N), Space O(N)
1198
- * @param callback - Predicate (value, index, list) → boolean to keep value.
1199
- * @param [thisArg] - Value for `this` inside the callback.
1200
- * @returns A new list with kept values.
1201
- */
1372
+ * Filter values into a new list of the same class.
1373
+ * @remarks Time O(N), Space O(N)
1374
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
1375
+ * @param [thisArg] - Value for `this` inside the callback.
1376
+ * @returns A new list with kept values.
1377
+
1378
+
1379
+
1380
+
1381
+
1382
+
1383
+
1384
+
1385
+
1386
+
1387
+
1388
+ * @example
1389
+ * // SinglyLinkedList filter and map operations
1390
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1391
+ *
1392
+ * // Filter even numbers
1393
+ * const filtered = list.filter(value => value % 2 === 0);
1394
+ * console.log(filtered.length); // 2;
1395
+ *
1396
+ * // Map to double values
1397
+ * const doubled = list.map(value => value * 2);
1398
+ * console.log(doubled.length); // 5;
1399
+ *
1400
+ * // Use reduce to sum
1401
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1402
+ * console.log(sum); // 15;
1403
+ */
1202
1404
  filter(callback, thisArg) {
1203
1405
  const out = this._createInstance();
1204
1406
  let index = 0;
@@ -1222,15 +1424,31 @@ var SinglyLinkedList = class extends LinearLinkedBase {
1222
1424
  return out;
1223
1425
  }
1224
1426
  /**
1225
- * Map values into a new list (possibly different element type).
1226
- * @remarks Time O(N), Space O(N)
1227
- * @template EM
1228
- * @template RM
1229
- * @param callback - Mapping function (value, index, list) → newElement.
1230
- * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1231
- * @param [thisArg] - Value for `this` inside the callback.
1232
- * @returns A new SinglyLinkedList with mapped values.
1233
- */
1427
+ * Map values into a new list (possibly different element type).
1428
+ * @remarks Time O(N), Space O(N)
1429
+ * @template EM
1430
+ * @template RM
1431
+ * @param callback - Mapping function (value, index, list) → newElement.
1432
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1433
+ * @param [thisArg] - Value for `this` inside the callback.
1434
+ * @returns A new SinglyLinkedList with mapped values.
1435
+
1436
+
1437
+
1438
+
1439
+
1440
+
1441
+
1442
+
1443
+
1444
+
1445
+
1446
+ * @example
1447
+ * // Transform elements
1448
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1449
+ * const doubled = list.map(n => n * 2);
1450
+ * console.log([...doubled]); // [2, 4, 6];
1451
+ */
1234
1452
  map(callback, options, thisArg) {
1235
1453
  const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
1236
1454
  let index = 0;
@@ -1365,6 +1583,55 @@ function elementOrPredicate(input, equals) {
1365
1583
  }
1366
1584
  __name(elementOrPredicate, "elementOrPredicate");
1367
1585
 
1586
+ // src/common/error.ts
1587
+ var ERR = {
1588
+ // Range / index
1589
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
1590
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
1591
+ // Type / argument
1592
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
1593
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
1594
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
1595
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
1596
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
1597
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
1598
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
1599
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
1600
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
1601
+ // State / operation
1602
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
1603
+ // Matrix
1604
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
1605
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
1606
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
1607
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
1608
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
1609
+ };
1610
+
1611
+ // src/common/index.ts
1612
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
1613
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
1614
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
1615
+ return DFSOperation2;
1616
+ })(DFSOperation || {});
1617
+ var Range = class {
1618
+ constructor(low, high, includeLow = true, includeHigh = true) {
1619
+ this.low = low;
1620
+ this.high = high;
1621
+ this.includeLow = includeLow;
1622
+ this.includeHigh = includeHigh;
1623
+ }
1624
+ static {
1625
+ __name(this, "Range");
1626
+ }
1627
+ // Determine whether a key is within the range
1628
+ isInRange(key, comparator) {
1629
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1630
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1631
+ return lowCheck && highCheck;
1632
+ }
1633
+ };
1634
+
1368
1635
  // src/data-structures/queue/queue.ts
1369
1636
  var Queue = class _Queue extends LinearBase {
1370
1637
  static {
@@ -1422,18 +1689,52 @@ var Queue = class _Queue extends LinearBase {
1422
1689
  this._autoCompactRatio = value;
1423
1690
  }
1424
1691
  /**
1425
- * Get the number of elements currently in the queue.
1426
- * @remarks Time O(1), Space O(1)
1427
- * @returns Current length.
1428
- */
1692
+ * Get the number of elements currently in the queue.
1693
+ * @remarks Time O(1), Space O(1)
1694
+ * @returns Current length.
1695
+
1696
+
1697
+
1698
+
1699
+
1700
+
1701
+
1702
+
1703
+
1704
+
1705
+
1706
+ * @example
1707
+ * // Track queue length
1708
+ * const q = new Queue<number>();
1709
+ * console.log(q.length); // 0;
1710
+ * q.push(1);
1711
+ * q.push(2);
1712
+ * console.log(q.length); // 2;
1713
+ */
1429
1714
  get length() {
1430
1715
  return this.elements.length - this._offset;
1431
1716
  }
1432
1717
  /**
1433
- * Get the first element (front) without removing it.
1434
- * @remarks Time O(1), Space O(1)
1435
- * @returns Front element or undefined.
1436
- */
1718
+ * Get the first element (front) without removing it.
1719
+ * @remarks Time O(1), Space O(1)
1720
+ * @returns Front element or undefined.
1721
+
1722
+
1723
+
1724
+
1725
+
1726
+
1727
+
1728
+
1729
+
1730
+
1731
+
1732
+ * @example
1733
+ * // View the front element
1734
+ * const q = new Queue<string>(['first', 'second', 'third']);
1735
+ * console.log(q.first); // 'first';
1736
+ * console.log(q.length); // 3;
1737
+ */
1437
1738
  get first() {
1438
1739
  return this.length > 0 ? this.elements[this._offset] : void 0;
1439
1740
  }
@@ -1456,19 +1757,69 @@ var Queue = class _Queue extends LinearBase {
1456
1757
  return new _Queue(elements);
1457
1758
  }
1458
1759
  /**
1459
- * Check whether the queue is empty.
1460
- * @remarks Time O(1), Space O(1)
1461
- * @returns True if length is 0.
1462
- */
1760
+ * Check whether the queue is empty.
1761
+ * @remarks Time O(1), Space O(1)
1762
+ * @returns True if length is 0.
1763
+
1764
+
1765
+
1766
+
1767
+
1768
+
1769
+
1770
+
1771
+
1772
+
1773
+
1774
+ * @example
1775
+ * // Queue for...of iteration and isEmpty check
1776
+ * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
1777
+ *
1778
+ * const elements: string[] = [];
1779
+ * for (const item of queue) {
1780
+ * elements.push(item);
1781
+ * }
1782
+ *
1783
+ * // Verify all elements are iterated in order
1784
+ * console.log(elements); // ['A', 'B', 'C', 'D'];
1785
+ *
1786
+ * // Process all elements
1787
+ * while (queue.length > 0) {
1788
+ * queue.shift();
1789
+ * }
1790
+ *
1791
+ * console.log(queue.length); // 0;
1792
+ */
1463
1793
  isEmpty() {
1464
1794
  return this.length === 0;
1465
1795
  }
1466
1796
  /**
1467
- * Enqueue one element at the back.
1468
- * @remarks Time O(1), Space O(1)
1469
- * @param element - Element to enqueue.
1470
- * @returns True on success.
1471
- */
1797
+ * Enqueue one element at the back.
1798
+ * @remarks Time O(1), Space O(1)
1799
+ * @param element - Element to enqueue.
1800
+ * @returns True on success.
1801
+
1802
+
1803
+
1804
+
1805
+
1806
+
1807
+
1808
+
1809
+
1810
+
1811
+
1812
+ * @example
1813
+ * // basic Queue creation and push operation
1814
+ * // Create a simple Queue with initial values
1815
+ * const queue = new Queue([1, 2, 3, 4, 5]);
1816
+ *
1817
+ * // Verify the queue maintains insertion order
1818
+ * console.log([...queue]); // [1, 2, 3, 4, 5];
1819
+ *
1820
+ * // Check length
1821
+ * console.log(queue.length); // 5;
1822
+ */
1472
1823
  push(element) {
1473
1824
  this.elements.push(element);
1474
1825
  if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
@@ -1489,10 +1840,35 @@ var Queue = class _Queue extends LinearBase {
1489
1840
  return ans;
1490
1841
  }
1491
1842
  /**
1492
- * Dequeue one element from the front (amortized via offset).
1493
- * @remarks Time O(1) amortized, Space O(1)
1494
- * @returns Removed element or undefined.
1495
- */
1843
+ * Dequeue one element from the front (amortized via offset).
1844
+ * @remarks Time O(1) amortized, Space O(1)
1845
+ * @returns Removed element or undefined.
1846
+
1847
+
1848
+
1849
+
1850
+
1851
+
1852
+
1853
+
1854
+
1855
+
1856
+
1857
+ * @example
1858
+ * // Queue shift and peek operations
1859
+ * const queue = new Queue<number>([10, 20, 30, 40]);
1860
+ *
1861
+ * // Peek at the front element without removing it
1862
+ * console.log(queue.first); // 10;
1863
+ *
1864
+ * // Remove and get the first element (FIFO)
1865
+ * const first = queue.shift();
1866
+ * console.log(first); // 10;
1867
+ *
1868
+ * // Verify remaining elements and length decreased
1869
+ * console.log([...queue]); // [20, 30, 40];
1870
+ * console.log(queue.length); // 3;
1871
+ */
1496
1872
  shift() {
1497
1873
  if (this.length === 0) return void 0;
1498
1874
  const first = this.first;
@@ -1501,11 +1877,24 @@ var Queue = class _Queue extends LinearBase {
1501
1877
  return first;
1502
1878
  }
1503
1879
  /**
1504
- * Delete the first occurrence of a specific element.
1505
- * @remarks Time O(N), Space O(1)
1506
- * @param element - Element to remove (strict equality via Object.is).
1507
- * @returns True if an element was removed.
1508
- */
1880
+ * Delete the first occurrence of a specific element.
1881
+ * @remarks Time O(N), Space O(1)
1882
+ * @param element - Element to remove (strict equality via Object.is).
1883
+ * @returns True if an element was removed.
1884
+
1885
+
1886
+
1887
+
1888
+
1889
+
1890
+
1891
+
1892
+ * @example
1893
+ * // Remove specific element
1894
+ * const q = new Queue<number>([1, 2, 3, 2]);
1895
+ * q.delete(2);
1896
+ * console.log(q.length); // 3;
1897
+ */
1509
1898
  delete(element) {
1510
1899
  for (let i = this._offset; i < this.elements.length; i++) {
1511
1900
  if (Object.is(this.elements[i], element)) {
@@ -1516,11 +1905,24 @@ var Queue = class _Queue extends LinearBase {
1516
1905
  return false;
1517
1906
  }
1518
1907
  /**
1519
- * Get the element at a given logical index.
1520
- * @remarks Time O(1), Space O(1)
1521
- * @param index - Zero-based index from the front.
1522
- * @returns Element or undefined.
1523
- */
1908
+ * Get the element at a given logical index.
1909
+ * @remarks Time O(1), Space O(1)
1910
+ * @param index - Zero-based index from the front.
1911
+ * @returns Element or undefined.
1912
+
1913
+
1914
+
1915
+
1916
+
1917
+
1918
+
1919
+
1920
+ * @example
1921
+ * // Access element by index
1922
+ * const q = new Queue<string>(['a', 'b', 'c']);
1923
+ * console.log(q.at(0)); // 'a';
1924
+ * console.log(q.at(2)); // 'c';
1925
+ */
1524
1926
  at(index) {
1525
1927
  if (index < 0 || index >= this.length) return void 0;
1526
1928
  return this._elements[this._offset + index];
@@ -1572,19 +1974,48 @@ var Queue = class _Queue extends LinearBase {
1572
1974
  return this;
1573
1975
  }
1574
1976
  /**
1575
- * Remove all elements and reset offset.
1576
- * @remarks Time O(1), Space O(1)
1577
- * @returns void
1578
- */
1977
+ * Remove all elements and reset offset.
1978
+ * @remarks Time O(1), Space O(1)
1979
+ * @returns void
1980
+
1981
+
1982
+
1983
+
1984
+
1985
+
1986
+
1987
+
1988
+
1989
+ * @example
1990
+ * // Remove all elements
1991
+ * const q = new Queue<number>([1, 2, 3]);
1992
+ * q.clear();
1993
+ * console.log(q.length); // 0;
1994
+ */
1579
1995
  clear() {
1580
1996
  this._elements = [];
1581
1997
  this._offset = 0;
1582
1998
  }
1583
1999
  /**
1584
- * Compact storage by discarding consumed head elements.
1585
- * @remarks Time O(N), Space O(N)
1586
- * @returns True when compaction performed.
1587
- */
2000
+ * Compact storage by discarding consumed head elements.
2001
+ * @remarks Time O(N), Space O(N)
2002
+ * @returns True when compaction performed.
2003
+
2004
+
2005
+
2006
+
2007
+
2008
+
2009
+
2010
+
2011
+ * @example
2012
+ * // Reclaim unused memory
2013
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
2014
+ * q.shift();
2015
+ * q.shift();
2016
+ * q.compact();
2017
+ * console.log(q.length); // 3;
2018
+ */
1588
2019
  compact() {
1589
2020
  this._elements = this.elements.slice(this._offset);
1590
2021
  this._offset = 0;
@@ -1610,10 +2041,26 @@ var Queue = class _Queue extends LinearBase {
1610
2041
  return removed;
1611
2042
  }
1612
2043
  /**
1613
- * Deep clone this queue and its parameters.
1614
- * @remarks Time O(N), Space O(N)
1615
- * @returns A new queue with the same content and options.
1616
- */
2044
+ * Deep clone this queue and its parameters.
2045
+ * @remarks Time O(N), Space O(N)
2046
+ * @returns A new queue with the same content and options.
2047
+
2048
+
2049
+
2050
+
2051
+
2052
+
2053
+
2054
+
2055
+
2056
+ * @example
2057
+ * // Create independent copy
2058
+ * const q = new Queue<number>([1, 2, 3]);
2059
+ * const copy = q.clone();
2060
+ * copy.shift();
2061
+ * console.log(q.length); // 3;
2062
+ * console.log(copy.length); // 2;
2063
+ */
1617
2064
  clone() {
1618
2065
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1619
2066
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -1621,12 +2068,26 @@ var Queue = class _Queue extends LinearBase {
1621
2068
  return out;
1622
2069
  }
1623
2070
  /**
1624
- * Filter elements into a new queue of the same class.
1625
- * @remarks Time O(N), Space O(N)
1626
- * @param predicate - Predicate (element, index, queue) → boolean to keep element.
1627
- * @param [thisArg] - Value for `this` inside the predicate.
1628
- * @returns A new queue with kept elements.
1629
- */
2071
+ * Filter elements into a new queue of the same class.
2072
+ * @remarks Time O(N), Space O(N)
2073
+ * @param predicate - Predicate (element, index, queue) → boolean to keep element.
2074
+ * @param [thisArg] - Value for `this` inside the predicate.
2075
+ * @returns A new queue with kept elements.
2076
+
2077
+
2078
+
2079
+
2080
+
2081
+
2082
+
2083
+
2084
+
2085
+ * @example
2086
+ * // Filter elements
2087
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
2088
+ * const evens = q.filter(x => x % 2 === 0);
2089
+ * console.log(evens.length); // 2;
2090
+ */
1630
2091
  filter(predicate, thisArg) {
1631
2092
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1632
2093
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -1638,15 +2099,28 @@ var Queue = class _Queue extends LinearBase {
1638
2099
  return out;
1639
2100
  }
1640
2101
  /**
1641
- * Map each element to a new element in a possibly different-typed queue.
1642
- * @remarks Time O(N), Space O(N)
1643
- * @template EM
1644
- * @template RM
1645
- * @param callback - Mapping function (element, index, queue) → newElement.
1646
- * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
1647
- * @param [thisArg] - Value for `this` inside the callback.
1648
- * @returns A new Queue with mapped elements.
1649
- */
2102
+ * Map each element to a new element in a possibly different-typed queue.
2103
+ * @remarks Time O(N), Space O(N)
2104
+ * @template EM
2105
+ * @template RM
2106
+ * @param callback - Mapping function (element, index, queue) → newElement.
2107
+ * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
2108
+ * @param [thisArg] - Value for `this` inside the callback.
2109
+ * @returns A new Queue with mapped elements.
2110
+
2111
+
2112
+
2113
+
2114
+
2115
+
2116
+
2117
+
2118
+ * @example
2119
+ * // Transform elements
2120
+ * const q = new Queue<number>([1, 2, 3]);
2121
+ * const doubled = q.map(x => x * 2);
2122
+ * console.log(doubled.toArray()); // [2, 4, 6];
2123
+ */
1650
2124
  map(callback, options, thisArg) {
1651
2125
  const out = new this.constructor([], {
1652
2126
  toElementFn: options?.toElementFn,
@@ -1881,19 +2355,60 @@ var Deque = class extends LinearBase {
1881
2355
  return this._length;
1882
2356
  }
1883
2357
  /**
1884
- * Get the first element without removing it.
1885
- * @remarks Time O(1), Space O(1)
1886
- * @returns First element or undefined.
1887
- */
2358
+ * Get the first element without removing it.
2359
+ * @remarks Time O(1), Space O(1)
2360
+ * @returns First element or undefined.
2361
+
2362
+
2363
+
2364
+
2365
+
2366
+
2367
+
2368
+
2369
+
2370
+
2371
+
2372
+ * @example
2373
+ * // Deque peek at both ends
2374
+ * const deque = new Deque<number>([10, 20, 30, 40, 50]);
2375
+ *
2376
+ * // Get first element without removing
2377
+ * const first = deque.at(0);
2378
+ * console.log(first); // 10;
2379
+ *
2380
+ * // Get last element without removing
2381
+ * const last = deque.at(deque.length - 1);
2382
+ * console.log(last); // 50;
2383
+ *
2384
+ * // Length unchanged
2385
+ * console.log(deque.length); // 5;
2386
+ */
1888
2387
  get first() {
1889
2388
  if (this._length === 0) return;
1890
2389
  return this._buckets[this._bucketFirst][this._firstInBucket];
1891
2390
  }
1892
2391
  /**
1893
- * Get the last element without removing it.
1894
- * @remarks Time O(1), Space O(1)
1895
- * @returns Last element or undefined.
1896
- */
2392
+ * Get the last element without removing it.
2393
+ * @remarks Time O(1), Space O(1)
2394
+ * @returns Last element or undefined.
2395
+
2396
+
2397
+
2398
+
2399
+
2400
+
2401
+
2402
+
2403
+
2404
+
2405
+
2406
+ * @example
2407
+ * // Peek at the back element
2408
+ * const dq = new Deque<string>(['a', 'b', 'c']);
2409
+ * console.log(dq.last); // 'c';
2410
+ * console.log(dq.first); // 'a';
2411
+ */
1897
2412
  get last() {
1898
2413
  if (this._length === 0) return;
1899
2414
  return this._buckets[this._bucketLast][this._lastInBucket];
@@ -1912,11 +2427,40 @@ var Deque = class extends LinearBase {
1912
2427
  return new this(data, options);
1913
2428
  }
1914
2429
  /**
1915
- * Append one element at the back.
1916
- * @remarks Time O(1) amortized, Space O(1)
1917
- * @param element - Element to append.
1918
- * @returns True when appended.
1919
- */
2430
+ * Append one element at the back.
2431
+ * @remarks Time O(1) amortized, Space O(1)
2432
+ * @param element - Element to append.
2433
+ * @returns True when appended.
2434
+
2435
+
2436
+
2437
+
2438
+
2439
+
2440
+
2441
+
2442
+
2443
+
2444
+
2445
+ * @example
2446
+ * // basic Deque creation and push/pop operations
2447
+ * // Create a simple Deque with initial values
2448
+ * const deque = new Deque([1, 2, 3, 4, 5]);
2449
+ *
2450
+ * // Verify the deque maintains insertion order
2451
+ * console.log([...deque]); // [1, 2, 3, 4, 5];
2452
+ *
2453
+ * // Check length
2454
+ * console.log(deque.length); // 5;
2455
+ *
2456
+ * // Push to the end
2457
+ * deque.push(6);
2458
+ * console.log(deque.length); // 6;
2459
+ *
2460
+ * // Pop from the end
2461
+ * const last = deque.pop();
2462
+ * console.log(last); // 6;
2463
+ */
1920
2464
  push(element) {
1921
2465
  if (this._length) {
1922
2466
  if (this._lastInBucket < this._bucketSize - 1) {
@@ -1936,10 +2480,26 @@ var Deque = class extends LinearBase {
1936
2480
  return true;
1937
2481
  }
1938
2482
  /**
1939
- * Remove and return the last element.
1940
- * @remarks Time O(1), Space O(1)
1941
- * @returns Removed element or undefined.
1942
- */
2483
+ * Remove and return the last element.
2484
+ * @remarks Time O(1), Space O(1)
2485
+ * @returns Removed element or undefined.
2486
+
2487
+
2488
+
2489
+
2490
+
2491
+
2492
+
2493
+
2494
+
2495
+
2496
+
2497
+ * @example
2498
+ * // Remove from the back
2499
+ * const dq = new Deque<number>([1, 2, 3]);
2500
+ * console.log(dq.pop()); // 3;
2501
+ * console.log(dq.length); // 2;
2502
+ */
1943
2503
  pop() {
1944
2504
  if (this._length === 0) return;
1945
2505
  const element = this._buckets[this._bucketLast][this._lastInBucket];
@@ -1959,10 +2519,26 @@ var Deque = class extends LinearBase {
1959
2519
  return element;
1960
2520
  }
1961
2521
  /**
1962
- * Remove and return the first element.
1963
- * @remarks Time O(1) amortized, Space O(1)
1964
- * @returns Removed element or undefined.
1965
- */
2522
+ * Remove and return the first element.
2523
+ * @remarks Time O(1) amortized, Space O(1)
2524
+ * @returns Removed element or undefined.
2525
+
2526
+
2527
+
2528
+
2529
+
2530
+
2531
+
2532
+
2533
+
2534
+
2535
+
2536
+ * @example
2537
+ * // Remove from the front
2538
+ * const dq = new Deque<number>([1, 2, 3]);
2539
+ * console.log(dq.shift()); // 1;
2540
+ * console.log(dq.length); // 2;
2541
+ */
1966
2542
  shift() {
1967
2543
  if (this._length === 0) return;
1968
2544
  const element = this._buckets[this._bucketFirst][this._firstInBucket];
@@ -1982,11 +2558,37 @@ var Deque = class extends LinearBase {
1982
2558
  return element;
1983
2559
  }
1984
2560
  /**
1985
- * Prepend one element at the front.
1986
- * @remarks Time O(1) amortized, Space O(1)
1987
- * @param element - Element to prepend.
1988
- * @returns True when prepended.
1989
- */
2561
+ * Prepend one element at the front.
2562
+ * @remarks Time O(1) amortized, Space O(1)
2563
+ * @param element - Element to prepend.
2564
+ * @returns True when prepended.
2565
+
2566
+
2567
+
2568
+
2569
+
2570
+
2571
+
2572
+
2573
+
2574
+
2575
+
2576
+ * @example
2577
+ * // Deque shift and unshift operations
2578
+ * const deque = new Deque<number>([20, 30, 40]);
2579
+ *
2580
+ * // Unshift adds to the front
2581
+ * deque.unshift(10);
2582
+ * console.log([...deque]); // [10, 20, 30, 40];
2583
+ *
2584
+ * // Shift removes from the front (O(1) complexity!)
2585
+ * const first = deque.shift();
2586
+ * console.log(first); // 10;
2587
+ *
2588
+ * // Verify remaining elements
2589
+ * console.log([...deque]); // [20, 30, 40];
2590
+ * console.log(deque.length); // 3;
2591
+ */
1990
2592
  unshift(element) {
1991
2593
  if (this._length) {
1992
2594
  if (this._firstInBucket > 0) {
@@ -2040,18 +2642,45 @@ var Deque = class extends LinearBase {
2040
2642
  return ans;
2041
2643
  }
2042
2644
  /**
2043
- * Check whether the deque is empty.
2044
- * @remarks Time O(1), Space O(1)
2045
- * @returns True if length is 0.
2046
- */
2645
+ * Check whether the deque is empty.
2646
+ * @remarks Time O(1), Space O(1)
2647
+ * @returns True if length is 0.
2648
+
2649
+
2650
+
2651
+
2652
+
2653
+
2654
+
2655
+
2656
+
2657
+ * @example
2658
+ * // Check if empty
2659
+ * const dq = new Deque();
2660
+ * console.log(dq.isEmpty()); // true;
2661
+ */
2047
2662
  isEmpty() {
2048
2663
  return this._length === 0;
2049
2664
  }
2050
2665
  /**
2051
- * Remove all elements and reset structure.
2052
- * @remarks Time O(1), Space O(1)
2053
- * @returns void
2054
- */
2666
+ * Remove all elements and reset structure.
2667
+ * @remarks Time O(1), Space O(1)
2668
+ * @returns void
2669
+
2670
+
2671
+
2672
+
2673
+
2674
+
2675
+
2676
+
2677
+
2678
+ * @example
2679
+ * // Remove all elements
2680
+ * const dq = new Deque<number>([1, 2, 3]);
2681
+ * dq.clear();
2682
+ * console.log(dq.length); // 0;
2683
+ */
2055
2684
  clear() {
2056
2685
  this._buckets = [new Array(this._bucketSize)];
2057
2686
  this._bucketCount = 1;
@@ -2059,11 +2688,24 @@ var Deque = class extends LinearBase {
2059
2688
  this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
2060
2689
  }
2061
2690
  /**
2062
- * Get the element at a given position.
2063
- * @remarks Time O(1), Space O(1)
2064
- * @param pos - Zero-based position from the front.
2065
- * @returns Element or undefined.
2066
- */
2691
+ * Get the element at a given position.
2692
+ * @remarks Time O(1), Space O(1)
2693
+ * @param pos - Zero-based position from the front.
2694
+ * @returns Element or undefined.
2695
+
2696
+
2697
+
2698
+
2699
+
2700
+
2701
+
2702
+
2703
+ * @example
2704
+ * // Access by index
2705
+ * const dq = new Deque<string>(['a', 'b', 'c']);
2706
+ * console.log(dq.at(0)); // 'a';
2707
+ * console.log(dq.at(2)); // 'c';
2708
+ */
2067
2709
  at(pos) {
2068
2710
  if (pos < 0 || pos >= this._length) return void 0;
2069
2711
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
@@ -2222,11 +2864,24 @@ var Deque = class extends LinearBase {
2222
2864
  }
2223
2865
  }
2224
2866
  /**
2225
- * Delete the first occurrence of a value.
2226
- * @remarks Time O(N), Space O(1)
2227
- * @param element - Element to remove (using the configured equality).
2228
- * @returns True if an element was removed.
2229
- */
2867
+ * Delete the first occurrence of a value.
2868
+ * @remarks Time O(N), Space O(1)
2869
+ * @param element - Element to remove (using the configured equality).
2870
+ * @returns True if an element was removed.
2871
+
2872
+
2873
+
2874
+
2875
+
2876
+
2877
+
2878
+
2879
+ * @example
2880
+ * // Remove element
2881
+ * const dq = new Deque<number>([1, 2, 3]);
2882
+ * dq.delete(2);
2883
+ * console.log(dq.length); // 2;
2884
+ */
2230
2885
  delete(element) {
2231
2886
  const size = this._length;
2232
2887
  if (size === 0) return false;
@@ -2270,10 +2925,39 @@ var Deque = class extends LinearBase {
2270
2925
  return this;
2271
2926
  }
2272
2927
  /**
2273
- * Reverse the deque by reversing buckets and pointers.
2274
- * @remarks Time O(N), Space O(N)
2275
- * @returns This deque.
2276
- */
2928
+ * Reverse the deque by reversing buckets and pointers.
2929
+ * @remarks Time O(N), Space O(N)
2930
+ * @returns This deque.
2931
+
2932
+
2933
+
2934
+
2935
+
2936
+
2937
+
2938
+
2939
+
2940
+
2941
+
2942
+ * @example
2943
+ * // Deque for...of iteration and reverse
2944
+ * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
2945
+ *
2946
+ * // Iterate forward
2947
+ * const forward: string[] = [];
2948
+ * for (const item of deque) {
2949
+ * forward.push(item);
2950
+ * }
2951
+ * console.log(forward); // ['A', 'B', 'C', 'D'];
2952
+ *
2953
+ * // Reverse the deque
2954
+ * deque.reverse();
2955
+ * const backward: string[] = [];
2956
+ * for (const item of deque) {
2957
+ * backward.push(item);
2958
+ * }
2959
+ * console.log(backward); // ['D', 'C', 'B', 'A'];
2960
+ */
2277
2961
  reverse() {
2278
2962
  this._buckets.reverse().forEach(function(bucket) {
2279
2963
  bucket.reverse();
@@ -2332,10 +3016,25 @@ var Deque = class extends LinearBase {
2332
3016
  * @returns True if compaction was performed (bucket count reduced).
2333
3017
  */
2334
3018
  /**
2335
- * Compact the deque by removing unused buckets.
2336
- * @remarks Time O(N), Space O(1)
2337
- * @returns True if compaction was performed (bucket count reduced).
2338
- */
3019
+ * Compact the deque by removing unused buckets.
3020
+ * @remarks Time O(N), Space O(1)
3021
+ * @returns True if compaction was performed (bucket count reduced).
3022
+
3023
+
3024
+
3025
+
3026
+
3027
+
3028
+
3029
+
3030
+ * @example
3031
+ * // Reclaim memory
3032
+ * const dq = new Deque<number>([1, 2, 3, 4, 5]);
3033
+ * dq.shift();
3034
+ * dq.shift();
3035
+ * dq.compact();
3036
+ * console.log(dq.length); // 3;
3037
+ */
2339
3038
  compact() {
2340
3039
  const before = this._bucketCount;
2341
3040
  this.shrinkToFit();
@@ -2363,10 +3062,26 @@ var Deque = class extends LinearBase {
2363
3062
  this._compactCounter = 0;
2364
3063
  }
2365
3064
  /**
2366
- * Deep clone this deque, preserving options.
2367
- * @remarks Time O(N), Space O(N)
2368
- * @returns A new deque with the same content and options.
2369
- */
3065
+ * Deep clone this deque, preserving options.
3066
+ * @remarks Time O(N), Space O(N)
3067
+ * @returns A new deque with the same content and options.
3068
+
3069
+
3070
+
3071
+
3072
+
3073
+
3074
+
3075
+
3076
+
3077
+ * @example
3078
+ * // Create independent copy
3079
+ * const dq = new Deque<number>([1, 2, 3]);
3080
+ * const copy = dq.clone();
3081
+ * copy.pop();
3082
+ * console.log(dq.length); // 3;
3083
+ * console.log(copy.length); // 2;
3084
+ */
2370
3085
  clone() {
2371
3086
  return this._createLike(this, {
2372
3087
  bucketSize: this.bucketSize,
@@ -2375,12 +3090,26 @@ var Deque = class extends LinearBase {
2375
3090
  });
2376
3091
  }
2377
3092
  /**
2378
- * Filter elements into a new deque of the same class.
2379
- * @remarks Time O(N), Space O(N)
2380
- * @param predicate - Predicate (value, index, deque) → boolean to keep element.
2381
- * @param [thisArg] - Value for `this` inside the predicate.
2382
- * @returns A new deque with kept elements.
2383
- */
3093
+ * Filter elements into a new deque of the same class.
3094
+ * @remarks Time O(N), Space O(N)
3095
+ * @param predicate - Predicate (value, index, deque) → boolean to keep element.
3096
+ * @param [thisArg] - Value for `this` inside the predicate.
3097
+ * @returns A new deque with kept elements.
3098
+
3099
+
3100
+
3101
+
3102
+
3103
+
3104
+
3105
+
3106
+
3107
+ * @example
3108
+ * // Filter elements
3109
+ * const dq = new Deque<number>([1, 2, 3, 4]);
3110
+ * const result = dq.filter(x => x > 2);
3111
+ * console.log(result.length); // 2;
3112
+ */
2384
3113
  filter(predicate, thisArg) {
2385
3114
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
2386
3115
  out._setBucketSize(this._bucketSize);
@@ -2409,15 +3138,28 @@ var Deque = class extends LinearBase {
2409
3138
  return out;
2410
3139
  }
2411
3140
  /**
2412
- * Map elements into a new deque (possibly different element type).
2413
- * @remarks Time O(N), Space O(N)
2414
- * @template EM
2415
- * @template RM
2416
- * @param callback - Mapping function (value, index, deque) → newElement.
2417
- * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
2418
- * @param [thisArg] - Value for `this` inside the callback.
2419
- * @returns A new Deque with mapped elements.
2420
- */
3141
+ * Map elements into a new deque (possibly different element type).
3142
+ * @remarks Time O(N), Space O(N)
3143
+ * @template EM
3144
+ * @template RM
3145
+ * @param callback - Mapping function (value, index, deque) → newElement.
3146
+ * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
3147
+ * @param [thisArg] - Value for `this` inside the callback.
3148
+ * @returns A new Deque with mapped elements.
3149
+
3150
+
3151
+
3152
+
3153
+
3154
+
3155
+
3156
+
3157
+ * @example
3158
+ * // Transform elements
3159
+ * const dq = new Deque<number>([1, 2, 3]);
3160
+ * const result = dq.map(x => x * 10);
3161
+ * console.log(result.toArray()); // [10, 20, 30];
3162
+ */
2421
3163
  map(callback, options, thisArg) {
2422
3164
  const out = this._createLike([], {
2423
3165
  ...options ?? {},