queue-typed 2.4.4 → 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 (85) hide show
  1. package/README.md +6 -48
  2. package/dist/cjs/index.cjs +1059 -238
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +1058 -237
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +1059 -239
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +1058 -238
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/queue-typed.js +1056 -235
  46. package/dist/umd/queue-typed.js.map +1 -1
  47. package/dist/umd/queue-typed.min.js +1 -1
  48. package/dist/umd/queue-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -25,6 +25,7 @@ var queueTyped = (() => {
25
25
  __export(src_exports, {
26
26
  DFSOperation: () => DFSOperation,
27
27
  Deque: () => Deque,
28
+ ERR: () => ERR,
28
29
  LinkedListQueue: () => LinkedListQueue,
29
30
  Queue: () => Queue,
30
31
  Range: () => Range
@@ -736,11 +737,37 @@ var queueTyped = (() => {
736
737
  return list;
737
738
  }
738
739
  /**
739
- * Append an element/node to the tail.
740
- * @remarks Time O(1), Space O(1)
741
- * @param elementOrNode - Element or node to append.
742
- * @returns True when appended.
743
- */
740
+ * Append an element/node to the tail.
741
+ * @remarks Time O(1), Space O(1)
742
+ * @param elementOrNode - Element or node to append.
743
+ * @returns True when appended.
744
+
745
+
746
+
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+ * @example
756
+ * // basic SinglyLinkedList creation and push operation
757
+ * // Create a simple SinglyLinkedList with initial values
758
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
759
+ *
760
+ * // Verify the list maintains insertion order
761
+ * console.log([...list]); // [1, 2, 3, 4, 5];
762
+ *
763
+ * // Check length
764
+ * console.log(list.length); // 5;
765
+ *
766
+ * // Push a new element to the end
767
+ * list.push(6);
768
+ * console.log(list.length); // 6;
769
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
770
+ */
744
771
  push(elementOrNode) {
745
772
  const newNode = this._ensureNode(elementOrNode);
746
773
  if (!this.head) {
@@ -754,10 +781,36 @@ var queueTyped = (() => {
754
781
  return true;
755
782
  }
756
783
  /**
757
- * Remove and return the tail element.
758
- * @remarks Time O(N), Space O(1)
759
- * @returns Removed element or undefined.
760
- */
784
+ * Remove and return the tail element.
785
+ * @remarks Time O(N), Space O(1)
786
+ * @returns Removed element or undefined.
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+ * @example
799
+ * // SinglyLinkedList pop and shift operations
800
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
801
+ *
802
+ * // Pop removes from the end
803
+ * const last = list.pop();
804
+ * console.log(last); // 50;
805
+ *
806
+ * // Shift removes from the beginning
807
+ * const first = list.shift();
808
+ * console.log(first); // 10;
809
+ *
810
+ * // Verify remaining elements
811
+ * console.log([...list]); // [20, 30, 40];
812
+ * console.log(list.length); // 3;
813
+ */
761
814
  pop() {
762
815
  var _a;
763
816
  if (!this.head) return void 0;
@@ -777,10 +830,26 @@ var queueTyped = (() => {
777
830
  return value;
778
831
  }
779
832
  /**
780
- * Remove and return the head element.
781
- * @remarks Time O(1), Space O(1)
782
- * @returns Removed element or undefined.
783
- */
833
+ * Remove and return the head element.
834
+ * @remarks Time O(1), Space O(1)
835
+ * @returns Removed element or undefined.
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
844
+
845
+
846
+
847
+ * @example
848
+ * // Remove from the front
849
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
850
+ * console.log(list.shift()); // 10;
851
+ * console.log(list.length); // 2;
852
+ */
784
853
  shift() {
785
854
  if (!this.head) return void 0;
786
855
  const removed = this.head;
@@ -790,11 +859,42 @@ var queueTyped = (() => {
790
859
  return removed.value;
791
860
  }
792
861
  /**
793
- * Prepend an element/node to the head.
794
- * @remarks Time O(1), Space O(1)
795
- * @param elementOrNode - Element or node to prepend.
796
- * @returns True when prepended.
797
- */
862
+ * Prepend an element/node to the head.
863
+ * @remarks Time O(1), Space O(1)
864
+ * @param elementOrNode - Element or node to prepend.
865
+ * @returns True when prepended.
866
+
867
+
868
+
869
+
870
+
871
+
872
+
873
+
874
+
875
+
876
+
877
+ * @example
878
+ * // SinglyLinkedList unshift and forward traversal
879
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
880
+ *
881
+ * // Unshift adds to the beginning
882
+ * list.unshift(10);
883
+ * console.log([...list]); // [10, 20, 30, 40];
884
+ *
885
+ * // Access elements (forward traversal only for singly linked)
886
+ * const second = list.at(1);
887
+ * console.log(second); // 20;
888
+ *
889
+ * // SinglyLinkedList allows forward iteration only
890
+ * const elements: number[] = [];
891
+ * for (const item of list) {
892
+ * elements.push(item);
893
+ * }
894
+ * console.log(elements); // [10, 20, 30, 40];
895
+ *
896
+ * console.log(list.length); // 4;
897
+ */
798
898
  unshift(elementOrNode) {
799
899
  const newNode = this._ensureNode(elementOrNode);
800
900
  if (!this.head) {
@@ -850,11 +950,28 @@ var queueTyped = (() => {
850
950
  return void 0;
851
951
  }
852
952
  /**
853
- * Get the element at a given index.
854
- * @remarks Time O(N), Space O(1)
855
- * @param index - Zero-based index.
856
- * @returns Element or undefined.
857
- */
953
+ * Get the element at a given index.
954
+ * @remarks Time O(N), Space O(1)
955
+ * @param index - Zero-based index.
956
+ * @returns Element or undefined.
957
+
958
+
959
+
960
+
961
+
962
+
963
+
964
+
965
+
966
+
967
+
968
+ * @example
969
+ * // Access element by index
970
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
971
+ * console.log(list.at(0)); // 'a';
972
+ * console.log(list.at(2)); // 'c';
973
+ * console.log(list.at(3)); // 'd';
974
+ */
858
975
  at(index) {
859
976
  if (index < 0 || index >= this._length) return void 0;
860
977
  let current = this.head;
@@ -871,11 +988,23 @@ var queueTyped = (() => {
871
988
  return elementNodeOrPredicate instanceof SinglyLinkedListNode;
872
989
  }
873
990
  /**
874
- * Get the node reference at a given index.
875
- * @remarks Time O(N), Space O(1)
876
- * @param index - Zero-based index.
877
- * @returns Node or undefined.
878
- */
991
+ * Get the node reference at a given index.
992
+ * @remarks Time O(N), Space O(1)
993
+ * @param index - Zero-based index.
994
+ * @returns Node or undefined.
995
+
996
+
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+ * @example
1004
+ * // Get node at index
1005
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1006
+ * console.log(list.getNodeAt(1)?.value); // 'b';
1007
+ */
879
1008
  getNodeAt(index) {
880
1009
  if (index < 0 || index >= this._length) return void 0;
881
1010
  let current = this.head;
@@ -883,11 +1012,24 @@ var queueTyped = (() => {
883
1012
  return current;
884
1013
  }
885
1014
  /**
886
- * Delete the element at an index.
887
- * @remarks Time O(N), Space O(1)
888
- * @param index - Zero-based index.
889
- * @returns Removed element or undefined.
890
- */
1015
+ * Delete the element at an index.
1016
+ * @remarks Time O(N), Space O(1)
1017
+ * @param index - Zero-based index.
1018
+ * @returns Removed element or undefined.
1019
+
1020
+
1021
+
1022
+
1023
+
1024
+
1025
+
1026
+
1027
+ * @example
1028
+ * // Remove by index
1029
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1030
+ * list.deleteAt(1);
1031
+ * console.log(list.toArray()); // ['a', 'c'];
1032
+ */
891
1033
  deleteAt(index) {
892
1034
  if (index < 0 || index >= this._length) return void 0;
893
1035
  if (index === 0) return this.shift();
@@ -900,11 +1042,24 @@ var queueTyped = (() => {
900
1042
  return value;
901
1043
  }
902
1044
  /**
903
- * Delete the first match by value/node.
904
- * @remarks Time O(N), Space O(1)
905
- * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
906
- * @returns True if removed.
907
- */
1045
+ * Delete the first match by value/node.
1046
+ * @remarks Time O(N), Space O(1)
1047
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
1048
+ * @returns True if removed.
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
1057
+ * @example
1058
+ * // Remove first occurrence
1059
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
1060
+ * list.delete(2);
1061
+ * console.log(list.toArray()); // [1, 3, 2];
1062
+ */
908
1063
  delete(elementOrNode) {
909
1064
  if (elementOrNode === void 0 || !this.head) return false;
910
1065
  const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
@@ -921,12 +1076,25 @@ var queueTyped = (() => {
921
1076
  return true;
922
1077
  }
923
1078
  /**
924
- * Insert a new element/node at an index, shifting following nodes.
925
- * @remarks Time O(N), Space O(1)
926
- * @param index - Zero-based index.
927
- * @param newElementOrNode - Element or node to insert.
928
- * @returns True if inserted.
929
- */
1079
+ * Insert a new element/node at an index, shifting following nodes.
1080
+ * @remarks Time O(N), Space O(1)
1081
+ * @param index - Zero-based index.
1082
+ * @param newElementOrNode - Element or node to insert.
1083
+ * @returns True if inserted.
1084
+
1085
+
1086
+
1087
+
1088
+
1089
+
1090
+
1091
+
1092
+ * @example
1093
+ * // Insert at index
1094
+ * const list = new SinglyLinkedList<number>([1, 3]);
1095
+ * list.addAt(1, 2);
1096
+ * console.log(list.toArray()); // [1, 2, 3];
1097
+ */
930
1098
  addAt(index, newElementOrNode) {
931
1099
  if (index < 0 || index > this._length) return false;
932
1100
  if (index === 0) return this.unshift(newElementOrNode);
@@ -952,28 +1120,70 @@ var queueTyped = (() => {
952
1120
  return true;
953
1121
  }
954
1122
  /**
955
- * Check whether the list is empty.
956
- * @remarks Time O(1), Space O(1)
957
- * @returns True if length is 0.
958
- */
1123
+ * Check whether the list is empty.
1124
+ * @remarks Time O(1), Space O(1)
1125
+ * @returns True if length is 0.
1126
+
1127
+
1128
+
1129
+
1130
+
1131
+
1132
+
1133
+
1134
+
1135
+ * @example
1136
+ * // Check empty
1137
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
1138
+ */
959
1139
  isEmpty() {
960
1140
  return this._length === 0;
961
1141
  }
962
1142
  /**
963
- * Remove all nodes and reset length.
964
- * @remarks Time O(N), Space O(1)
965
- * @returns void
966
- */
1143
+ * Remove all nodes and reset length.
1144
+ * @remarks Time O(N), Space O(1)
1145
+ * @returns void
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+ * @example
1156
+ * // Remove all
1157
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1158
+ * list.clear();
1159
+ * console.log(list.isEmpty()); // true;
1160
+ */
967
1161
  clear() {
968
1162
  this._head = void 0;
969
1163
  this._tail = void 0;
970
1164
  this._length = 0;
971
1165
  }
972
1166
  /**
973
- * Reverse the list in place.
974
- * @remarks Time O(N), Space O(1)
975
- * @returns This list.
976
- */
1167
+ * Reverse the list in place.
1168
+ * @remarks Time O(N), Space O(1)
1169
+ * @returns This list.
1170
+
1171
+
1172
+
1173
+
1174
+
1175
+
1176
+
1177
+
1178
+
1179
+
1180
+
1181
+ * @example
1182
+ * // Reverse the list in-place
1183
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
1184
+ * list.reverse();
1185
+ * console.log([...list]); // [4, 3, 2, 1];
1186
+ */
977
1187
  reverse() {
978
1188
  if (!this.head || this.head === this.tail) return this;
979
1189
  let prev;
@@ -1148,22 +1358,64 @@ var queueTyped = (() => {
1148
1358
  return false;
1149
1359
  }
1150
1360
  /**
1151
- * Deep clone this list (values are copied by reference).
1152
- * @remarks Time O(N), Space O(N)
1153
- * @returns A new list with the same element sequence.
1154
- */
1361
+ * Deep clone this list (values are copied by reference).
1362
+ * @remarks Time O(N), Space O(N)
1363
+ * @returns A new list with the same element sequence.
1364
+
1365
+
1366
+
1367
+
1368
+
1369
+
1370
+
1371
+
1372
+
1373
+ * @example
1374
+ * // Deep copy
1375
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1376
+ * const copy = list.clone();
1377
+ * copy.pop();
1378
+ * console.log(list.length); // 3;
1379
+ * console.log(copy.length); // 2;
1380
+ */
1155
1381
  clone() {
1156
1382
  const out = this._createInstance();
1157
1383
  for (const v of this) out.push(v);
1158
1384
  return out;
1159
1385
  }
1160
1386
  /**
1161
- * Filter values into a new list of the same class.
1162
- * @remarks Time O(N), Space O(N)
1163
- * @param callback - Predicate (value, index, list) → boolean to keep value.
1164
- * @param [thisArg] - Value for `this` inside the callback.
1165
- * @returns A new list with kept values.
1166
- */
1387
+ * Filter values into a new list of the same class.
1388
+ * @remarks Time O(N), Space O(N)
1389
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
1390
+ * @param [thisArg] - Value for `this` inside the callback.
1391
+ * @returns A new list with kept values.
1392
+
1393
+
1394
+
1395
+
1396
+
1397
+
1398
+
1399
+
1400
+
1401
+
1402
+
1403
+ * @example
1404
+ * // SinglyLinkedList filter and map operations
1405
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1406
+ *
1407
+ * // Filter even numbers
1408
+ * const filtered = list.filter(value => value % 2 === 0);
1409
+ * console.log(filtered.length); // 2;
1410
+ *
1411
+ * // Map to double values
1412
+ * const doubled = list.map(value => value * 2);
1413
+ * console.log(doubled.length); // 5;
1414
+ *
1415
+ * // Use reduce to sum
1416
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1417
+ * console.log(sum); // 15;
1418
+ */
1167
1419
  filter(callback, thisArg) {
1168
1420
  const out = this._createInstance();
1169
1421
  let index = 0;
@@ -1187,15 +1439,31 @@ var queueTyped = (() => {
1187
1439
  return out;
1188
1440
  }
1189
1441
  /**
1190
- * Map values into a new list (possibly different element type).
1191
- * @remarks Time O(N), Space O(N)
1192
- * @template EM
1193
- * @template RM
1194
- * @param callback - Mapping function (value, index, list) → newElement.
1195
- * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1196
- * @param [thisArg] - Value for `this` inside the callback.
1197
- * @returns A new SinglyLinkedList with mapped values.
1198
- */
1442
+ * Map values into a new list (possibly different element type).
1443
+ * @remarks Time O(N), Space O(N)
1444
+ * @template EM
1445
+ * @template RM
1446
+ * @param callback - Mapping function (value, index, list) → newElement.
1447
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1448
+ * @param [thisArg] - Value for `this` inside the callback.
1449
+ * @returns A new SinglyLinkedList with mapped values.
1450
+
1451
+
1452
+
1453
+
1454
+
1455
+
1456
+
1457
+
1458
+
1459
+
1460
+
1461
+ * @example
1462
+ * // Transform elements
1463
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1464
+ * const doubled = list.map(n => n * 2);
1465
+ * console.log([...doubled]); // [2, 4, 6];
1466
+ */
1199
1467
  map(callback, options, thisArg) {
1200
1468
  const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
1201
1469
  let index = 0;
@@ -1329,6 +1597,52 @@ var queueTyped = (() => {
1329
1597
  return (node) => equals(node.value, value);
1330
1598
  }
1331
1599
 
1600
+ // src/common/error.ts
1601
+ var ERR = {
1602
+ // Range / index
1603
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
1604
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
1605
+ // Type / argument
1606
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
1607
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
1608
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
1609
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
1610
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
1611
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
1612
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
1613
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
1614
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
1615
+ // State / operation
1616
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
1617
+ // Matrix
1618
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
1619
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
1620
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
1621
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
1622
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
1623
+ };
1624
+
1625
+ // src/common/index.ts
1626
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
1627
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
1628
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
1629
+ return DFSOperation2;
1630
+ })(DFSOperation || {});
1631
+ var Range = class {
1632
+ constructor(low, high, includeLow = true, includeHigh = true) {
1633
+ this.low = low;
1634
+ this.high = high;
1635
+ this.includeLow = includeLow;
1636
+ this.includeHigh = includeHigh;
1637
+ }
1638
+ // Determine whether a key is within the range
1639
+ isInRange(key, comparator) {
1640
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1641
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1642
+ return lowCheck && highCheck;
1643
+ }
1644
+ };
1645
+
1332
1646
  // src/data-structures/queue/queue.ts
1333
1647
  var Queue = class _Queue extends LinearBase {
1334
1648
  /**
@@ -1383,18 +1697,52 @@ var queueTyped = (() => {
1383
1697
  this._autoCompactRatio = value;
1384
1698
  }
1385
1699
  /**
1386
- * Get the number of elements currently in the queue.
1387
- * @remarks Time O(1), Space O(1)
1388
- * @returns Current length.
1389
- */
1700
+ * Get the number of elements currently in the queue.
1701
+ * @remarks Time O(1), Space O(1)
1702
+ * @returns Current length.
1703
+
1704
+
1705
+
1706
+
1707
+
1708
+
1709
+
1710
+
1711
+
1712
+
1713
+
1714
+ * @example
1715
+ * // Track queue length
1716
+ * const q = new Queue<number>();
1717
+ * console.log(q.length); // 0;
1718
+ * q.push(1);
1719
+ * q.push(2);
1720
+ * console.log(q.length); // 2;
1721
+ */
1390
1722
  get length() {
1391
1723
  return this.elements.length - this._offset;
1392
1724
  }
1393
1725
  /**
1394
- * Get the first element (front) without removing it.
1395
- * @remarks Time O(1), Space O(1)
1396
- * @returns Front element or undefined.
1397
- */
1726
+ * Get the first element (front) without removing it.
1727
+ * @remarks Time O(1), Space O(1)
1728
+ * @returns Front element or undefined.
1729
+
1730
+
1731
+
1732
+
1733
+
1734
+
1735
+
1736
+
1737
+
1738
+
1739
+
1740
+ * @example
1741
+ * // View the front element
1742
+ * const q = new Queue<string>(['first', 'second', 'third']);
1743
+ * console.log(q.first); // 'first';
1744
+ * console.log(q.length); // 3;
1745
+ */
1398
1746
  get first() {
1399
1747
  return this.length > 0 ? this.elements[this._offset] : void 0;
1400
1748
  }
@@ -1417,19 +1765,69 @@ var queueTyped = (() => {
1417
1765
  return new _Queue(elements);
1418
1766
  }
1419
1767
  /**
1420
- * Check whether the queue is empty.
1421
- * @remarks Time O(1), Space O(1)
1422
- * @returns True if length is 0.
1423
- */
1768
+ * Check whether the queue is empty.
1769
+ * @remarks Time O(1), Space O(1)
1770
+ * @returns True if length is 0.
1771
+
1772
+
1773
+
1774
+
1775
+
1776
+
1777
+
1778
+
1779
+
1780
+
1781
+
1782
+ * @example
1783
+ * // Queue for...of iteration and isEmpty check
1784
+ * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
1785
+ *
1786
+ * const elements: string[] = [];
1787
+ * for (const item of queue) {
1788
+ * elements.push(item);
1789
+ * }
1790
+ *
1791
+ * // Verify all elements are iterated in order
1792
+ * console.log(elements); // ['A', 'B', 'C', 'D'];
1793
+ *
1794
+ * // Process all elements
1795
+ * while (queue.length > 0) {
1796
+ * queue.shift();
1797
+ * }
1798
+ *
1799
+ * console.log(queue.length); // 0;
1800
+ */
1424
1801
  isEmpty() {
1425
1802
  return this.length === 0;
1426
1803
  }
1427
1804
  /**
1428
- * Enqueue one element at the back.
1429
- * @remarks Time O(1), Space O(1)
1430
- * @param element - Element to enqueue.
1431
- * @returns True on success.
1432
- */
1805
+ * Enqueue one element at the back.
1806
+ * @remarks Time O(1), Space O(1)
1807
+ * @param element - Element to enqueue.
1808
+ * @returns True on success.
1809
+
1810
+
1811
+
1812
+
1813
+
1814
+
1815
+
1816
+
1817
+
1818
+
1819
+
1820
+ * @example
1821
+ * // basic Queue creation and push operation
1822
+ * // Create a simple Queue with initial values
1823
+ * const queue = new Queue([1, 2, 3, 4, 5]);
1824
+ *
1825
+ * // Verify the queue maintains insertion order
1826
+ * console.log([...queue]); // [1, 2, 3, 4, 5];
1827
+ *
1828
+ * // Check length
1829
+ * console.log(queue.length); // 5;
1830
+ */
1433
1831
  push(element) {
1434
1832
  this.elements.push(element);
1435
1833
  if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
@@ -1450,10 +1848,35 @@ var queueTyped = (() => {
1450
1848
  return ans;
1451
1849
  }
1452
1850
  /**
1453
- * Dequeue one element from the front (amortized via offset).
1454
- * @remarks Time O(1) amortized, Space O(1)
1455
- * @returns Removed element or undefined.
1456
- */
1851
+ * Dequeue one element from the front (amortized via offset).
1852
+ * @remarks Time O(1) amortized, Space O(1)
1853
+ * @returns Removed element or undefined.
1854
+
1855
+
1856
+
1857
+
1858
+
1859
+
1860
+
1861
+
1862
+
1863
+
1864
+
1865
+ * @example
1866
+ * // Queue shift and peek operations
1867
+ * const queue = new Queue<number>([10, 20, 30, 40]);
1868
+ *
1869
+ * // Peek at the front element without removing it
1870
+ * console.log(queue.first); // 10;
1871
+ *
1872
+ * // Remove and get the first element (FIFO)
1873
+ * const first = queue.shift();
1874
+ * console.log(first); // 10;
1875
+ *
1876
+ * // Verify remaining elements and length decreased
1877
+ * console.log([...queue]); // [20, 30, 40];
1878
+ * console.log(queue.length); // 3;
1879
+ */
1457
1880
  shift() {
1458
1881
  if (this.length === 0) return void 0;
1459
1882
  const first = this.first;
@@ -1462,11 +1885,24 @@ var queueTyped = (() => {
1462
1885
  return first;
1463
1886
  }
1464
1887
  /**
1465
- * Delete the first occurrence of a specific element.
1466
- * @remarks Time O(N), Space O(1)
1467
- * @param element - Element to remove (strict equality via Object.is).
1468
- * @returns True if an element was removed.
1469
- */
1888
+ * Delete the first occurrence of a specific element.
1889
+ * @remarks Time O(N), Space O(1)
1890
+ * @param element - Element to remove (strict equality via Object.is).
1891
+ * @returns True if an element was removed.
1892
+
1893
+
1894
+
1895
+
1896
+
1897
+
1898
+
1899
+
1900
+ * @example
1901
+ * // Remove specific element
1902
+ * const q = new Queue<number>([1, 2, 3, 2]);
1903
+ * q.delete(2);
1904
+ * console.log(q.length); // 3;
1905
+ */
1470
1906
  delete(element) {
1471
1907
  for (let i = this._offset; i < this.elements.length; i++) {
1472
1908
  if (Object.is(this.elements[i], element)) {
@@ -1477,11 +1913,24 @@ var queueTyped = (() => {
1477
1913
  return false;
1478
1914
  }
1479
1915
  /**
1480
- * Get the element at a given logical index.
1481
- * @remarks Time O(1), Space O(1)
1482
- * @param index - Zero-based index from the front.
1483
- * @returns Element or undefined.
1484
- */
1916
+ * Get the element at a given logical index.
1917
+ * @remarks Time O(1), Space O(1)
1918
+ * @param index - Zero-based index from the front.
1919
+ * @returns Element or undefined.
1920
+
1921
+
1922
+
1923
+
1924
+
1925
+
1926
+
1927
+
1928
+ * @example
1929
+ * // Access element by index
1930
+ * const q = new Queue<string>(['a', 'b', 'c']);
1931
+ * console.log(q.at(0)); // 'a';
1932
+ * console.log(q.at(2)); // 'c';
1933
+ */
1485
1934
  at(index) {
1486
1935
  if (index < 0 || index >= this.length) return void 0;
1487
1936
  return this._elements[this._offset + index];
@@ -1533,19 +1982,48 @@ var queueTyped = (() => {
1533
1982
  return this;
1534
1983
  }
1535
1984
  /**
1536
- * Remove all elements and reset offset.
1537
- * @remarks Time O(1), Space O(1)
1538
- * @returns void
1539
- */
1985
+ * Remove all elements and reset offset.
1986
+ * @remarks Time O(1), Space O(1)
1987
+ * @returns void
1988
+
1989
+
1990
+
1991
+
1992
+
1993
+
1994
+
1995
+
1996
+
1997
+ * @example
1998
+ * // Remove all elements
1999
+ * const q = new Queue<number>([1, 2, 3]);
2000
+ * q.clear();
2001
+ * console.log(q.length); // 0;
2002
+ */
1540
2003
  clear() {
1541
2004
  this._elements = [];
1542
2005
  this._offset = 0;
1543
2006
  }
1544
2007
  /**
1545
- * Compact storage by discarding consumed head elements.
1546
- * @remarks Time O(N), Space O(N)
1547
- * @returns True when compaction performed.
1548
- */
2008
+ * Compact storage by discarding consumed head elements.
2009
+ * @remarks Time O(N), Space O(N)
2010
+ * @returns True when compaction performed.
2011
+
2012
+
2013
+
2014
+
2015
+
2016
+
2017
+
2018
+
2019
+ * @example
2020
+ * // Reclaim unused memory
2021
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
2022
+ * q.shift();
2023
+ * q.shift();
2024
+ * q.compact();
2025
+ * console.log(q.length); // 3;
2026
+ */
1549
2027
  compact() {
1550
2028
  this._elements = this.elements.slice(this._offset);
1551
2029
  this._offset = 0;
@@ -1571,10 +2049,26 @@ var queueTyped = (() => {
1571
2049
  return removed;
1572
2050
  }
1573
2051
  /**
1574
- * Deep clone this queue and its parameters.
1575
- * @remarks Time O(N), Space O(N)
1576
- * @returns A new queue with the same content and options.
1577
- */
2052
+ * Deep clone this queue and its parameters.
2053
+ * @remarks Time O(N), Space O(N)
2054
+ * @returns A new queue with the same content and options.
2055
+
2056
+
2057
+
2058
+
2059
+
2060
+
2061
+
2062
+
2063
+
2064
+ * @example
2065
+ * // Create independent copy
2066
+ * const q = new Queue<number>([1, 2, 3]);
2067
+ * const copy = q.clone();
2068
+ * copy.shift();
2069
+ * console.log(q.length); // 3;
2070
+ * console.log(copy.length); // 2;
2071
+ */
1578
2072
  clone() {
1579
2073
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1580
2074
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -1582,12 +2076,26 @@ var queueTyped = (() => {
1582
2076
  return out;
1583
2077
  }
1584
2078
  /**
1585
- * Filter elements into a new queue of the same class.
1586
- * @remarks Time O(N), Space O(N)
1587
- * @param predicate - Predicate (element, index, queue) → boolean to keep element.
1588
- * @param [thisArg] - Value for `this` inside the predicate.
1589
- * @returns A new queue with kept elements.
1590
- */
2079
+ * Filter elements into a new queue of the same class.
2080
+ * @remarks Time O(N), Space O(N)
2081
+ * @param predicate - Predicate (element, index, queue) → boolean to keep element.
2082
+ * @param [thisArg] - Value for `this` inside the predicate.
2083
+ * @returns A new queue with kept elements.
2084
+
2085
+
2086
+
2087
+
2088
+
2089
+
2090
+
2091
+
2092
+
2093
+ * @example
2094
+ * // Filter elements
2095
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
2096
+ * const evens = q.filter(x => x % 2 === 0);
2097
+ * console.log(evens.length); // 2;
2098
+ */
1591
2099
  filter(predicate, thisArg) {
1592
2100
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1593
2101
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -1599,15 +2107,28 @@ var queueTyped = (() => {
1599
2107
  return out;
1600
2108
  }
1601
2109
  /**
1602
- * Map each element to a new element in a possibly different-typed queue.
1603
- * @remarks Time O(N), Space O(N)
1604
- * @template EM
1605
- * @template RM
1606
- * @param callback - Mapping function (element, index, queue) → newElement.
1607
- * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
1608
- * @param [thisArg] - Value for `this` inside the callback.
1609
- * @returns A new Queue with mapped elements.
1610
- */
2110
+ * Map each element to a new element in a possibly different-typed queue.
2111
+ * @remarks Time O(N), Space O(N)
2112
+ * @template EM
2113
+ * @template RM
2114
+ * @param callback - Mapping function (element, index, queue) → newElement.
2115
+ * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
2116
+ * @param [thisArg] - Value for `this` inside the callback.
2117
+ * @returns A new Queue with mapped elements.
2118
+
2119
+
2120
+
2121
+
2122
+
2123
+
2124
+
2125
+
2126
+ * @example
2127
+ * // Transform elements
2128
+ * const q = new Queue<number>([1, 2, 3]);
2129
+ * const doubled = q.map(x => x * 2);
2130
+ * console.log(doubled.toArray()); // [2, 4, 6];
2131
+ */
1611
2132
  map(callback, options, thisArg) {
1612
2133
  var _a, _b;
1613
2134
  const out = new this.constructor([], {
@@ -1709,24 +2230,25 @@ var queueTyped = (() => {
1709
2230
  };
1710
2231
 
1711
2232
  // src/utils/utils.ts
1712
- var rangeCheck = (index, min, max, message = "Index out of bounds.") => {
1713
- if (index < min || index > max) throw new RangeError(message);
2233
+ var rangeCheck = (index, min, max, message) => {
2234
+ if (index < min || index > max) {
2235
+ throw new RangeError(message != null ? message : `Index ${index} is out of range [${min}, ${max}].`);
2236
+ }
1714
2237
  };
1715
2238
  var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
1716
2239
 
1717
2240
  // src/data-structures/queue/deque.ts
1718
2241
  var Deque = class extends LinearBase {
1719
- /**
1720
- * Create a Deque and optionally bulk-insert elements.
1721
- * @remarks Time O(N), Space O(N)
1722
- * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
1723
- * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
1724
- * @returns New Deque instance.
1725
- */
1726
2242
  constructor(elements = [], options) {
1727
2243
  super(options);
1728
2244
  __publicField(this, "_equals", (a, b) => Object.is(a, b));
1729
2245
  __publicField(this, "_bucketSize", 1 << 12);
2246
+ __publicField(this, "_autoCompactRatio", 0.5);
2247
+ /**
2248
+ * Counter for shift/pop operations since last compaction check.
2249
+ * Only checks ratio every `_bucketSize` operations to minimize overhead.
2250
+ */
2251
+ __publicField(this, "_compactCounter", 0);
1730
2252
  __publicField(this, "_bucketFirst", 0);
1731
2253
  __publicField(this, "_firstInBucket", 0);
1732
2254
  __publicField(this, "_bucketLast", 0);
@@ -1735,8 +2257,9 @@ var queueTyped = (() => {
1735
2257
  __publicField(this, "_buckets", []);
1736
2258
  __publicField(this, "_length", 0);
1737
2259
  if (options) {
1738
- const { bucketSize } = options;
2260
+ const { bucketSize, autoCompactRatio } = options;
1739
2261
  if (typeof bucketSize === "number") this._bucketSize = bucketSize;
2262
+ if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
1740
2263
  }
1741
2264
  let _size;
1742
2265
  if ("length" in elements) {
@@ -1761,6 +2284,24 @@ var queueTyped = (() => {
1761
2284
  get bucketSize() {
1762
2285
  return this._bucketSize;
1763
2286
  }
2287
+ /**
2288
+ * Get the auto-compaction ratio.
2289
+ * When `elements / (bucketCount * bucketSize)` drops below this ratio after
2290
+ * enough shift/pop operations, the deque auto-compacts.
2291
+ * @remarks Time O(1), Space O(1)
2292
+ * @returns Current ratio threshold. 0 means auto-compact is disabled.
2293
+ */
2294
+ get autoCompactRatio() {
2295
+ return this._autoCompactRatio;
2296
+ }
2297
+ /**
2298
+ * Set the auto-compaction ratio.
2299
+ * @remarks Time O(1), Space O(1)
2300
+ * @param value - Ratio in [0,1]. 0 disables auto-compact.
2301
+ */
2302
+ set autoCompactRatio(value) {
2303
+ this._autoCompactRatio = value;
2304
+ }
1764
2305
  /**
1765
2306
  * Get the index of the first bucket in use.
1766
2307
  * @remarks Time O(1), Space O(1)
@@ -1818,19 +2359,60 @@ var queueTyped = (() => {
1818
2359
  return this._length;
1819
2360
  }
1820
2361
  /**
1821
- * Get the first element without removing it.
1822
- * @remarks Time O(1), Space O(1)
1823
- * @returns First element or undefined.
1824
- */
2362
+ * Get the first element without removing it.
2363
+ * @remarks Time O(1), Space O(1)
2364
+ * @returns First element or undefined.
2365
+
2366
+
2367
+
2368
+
2369
+
2370
+
2371
+
2372
+
2373
+
2374
+
2375
+
2376
+ * @example
2377
+ * // Deque peek at both ends
2378
+ * const deque = new Deque<number>([10, 20, 30, 40, 50]);
2379
+ *
2380
+ * // Get first element without removing
2381
+ * const first = deque.at(0);
2382
+ * console.log(first); // 10;
2383
+ *
2384
+ * // Get last element without removing
2385
+ * const last = deque.at(deque.length - 1);
2386
+ * console.log(last); // 50;
2387
+ *
2388
+ * // Length unchanged
2389
+ * console.log(deque.length); // 5;
2390
+ */
1825
2391
  get first() {
1826
2392
  if (this._length === 0) return;
1827
2393
  return this._buckets[this._bucketFirst][this._firstInBucket];
1828
2394
  }
1829
2395
  /**
1830
- * Get the last element without removing it.
1831
- * @remarks Time O(1), Space O(1)
1832
- * @returns Last element or undefined.
1833
- */
2396
+ * Get the last element without removing it.
2397
+ * @remarks Time O(1), Space O(1)
2398
+ * @returns Last element or undefined.
2399
+
2400
+
2401
+
2402
+
2403
+
2404
+
2405
+
2406
+
2407
+
2408
+
2409
+
2410
+ * @example
2411
+ * // Peek at the back element
2412
+ * const dq = new Deque<string>(['a', 'b', 'c']);
2413
+ * console.log(dq.last); // 'c';
2414
+ * console.log(dq.first); // 'a';
2415
+ */
1834
2416
  get last() {
1835
2417
  if (this._length === 0) return;
1836
2418
  return this._buckets[this._bucketLast][this._lastInBucket];
@@ -1849,11 +2431,40 @@ var queueTyped = (() => {
1849
2431
  return new this(data, options);
1850
2432
  }
1851
2433
  /**
1852
- * Append one element at the back.
1853
- * @remarks Time O(1) amortized, Space O(1)
1854
- * @param element - Element to append.
1855
- * @returns True when appended.
1856
- */
2434
+ * Append one element at the back.
2435
+ * @remarks Time O(1) amortized, Space O(1)
2436
+ * @param element - Element to append.
2437
+ * @returns True when appended.
2438
+
2439
+
2440
+
2441
+
2442
+
2443
+
2444
+
2445
+
2446
+
2447
+
2448
+
2449
+ * @example
2450
+ * // basic Deque creation and push/pop operations
2451
+ * // Create a simple Deque with initial values
2452
+ * const deque = new Deque([1, 2, 3, 4, 5]);
2453
+ *
2454
+ * // Verify the deque maintains insertion order
2455
+ * console.log([...deque]); // [1, 2, 3, 4, 5];
2456
+ *
2457
+ * // Check length
2458
+ * console.log(deque.length); // 5;
2459
+ *
2460
+ * // Push to the end
2461
+ * deque.push(6);
2462
+ * console.log(deque.length); // 6;
2463
+ *
2464
+ * // Pop from the end
2465
+ * const last = deque.pop();
2466
+ * console.log(last); // 6;
2467
+ */
1857
2468
  push(element) {
1858
2469
  if (this._length) {
1859
2470
  if (this._lastInBucket < this._bucketSize - 1) {
@@ -1873,10 +2484,26 @@ var queueTyped = (() => {
1873
2484
  return true;
1874
2485
  }
1875
2486
  /**
1876
- * Remove and return the last element.
1877
- * @remarks Time O(1), Space O(1)
1878
- * @returns Removed element or undefined.
1879
- */
2487
+ * Remove and return the last element.
2488
+ * @remarks Time O(1), Space O(1)
2489
+ * @returns Removed element or undefined.
2490
+
2491
+
2492
+
2493
+
2494
+
2495
+
2496
+
2497
+
2498
+
2499
+
2500
+
2501
+ * @example
2502
+ * // Remove from the back
2503
+ * const dq = new Deque<number>([1, 2, 3]);
2504
+ * console.log(dq.pop()); // 3;
2505
+ * console.log(dq.length); // 2;
2506
+ */
1880
2507
  pop() {
1881
2508
  if (this._length === 0) return;
1882
2509
  const element = this._buckets[this._bucketLast][this._lastInBucket];
@@ -1892,13 +2519,30 @@ var queueTyped = (() => {
1892
2519
  }
1893
2520
  }
1894
2521
  this._length -= 1;
2522
+ this._autoCompact();
1895
2523
  return element;
1896
2524
  }
1897
2525
  /**
1898
- * Remove and return the first element.
1899
- * @remarks Time O(1) amortized, Space O(1)
1900
- * @returns Removed element or undefined.
1901
- */
2526
+ * Remove and return the first element.
2527
+ * @remarks Time O(1) amortized, Space O(1)
2528
+ * @returns Removed element or undefined.
2529
+
2530
+
2531
+
2532
+
2533
+
2534
+
2535
+
2536
+
2537
+
2538
+
2539
+
2540
+ * @example
2541
+ * // Remove from the front
2542
+ * const dq = new Deque<number>([1, 2, 3]);
2543
+ * console.log(dq.shift()); // 1;
2544
+ * console.log(dq.length); // 2;
2545
+ */
1902
2546
  shift() {
1903
2547
  if (this._length === 0) return;
1904
2548
  const element = this._buckets[this._bucketFirst][this._firstInBucket];
@@ -1914,14 +2558,41 @@ var queueTyped = (() => {
1914
2558
  }
1915
2559
  }
1916
2560
  this._length -= 1;
2561
+ this._autoCompact();
1917
2562
  return element;
1918
2563
  }
1919
2564
  /**
1920
- * Prepend one element at the front.
1921
- * @remarks Time O(1) amortized, Space O(1)
1922
- * @param element - Element to prepend.
1923
- * @returns True when prepended.
1924
- */
2565
+ * Prepend one element at the front.
2566
+ * @remarks Time O(1) amortized, Space O(1)
2567
+ * @param element - Element to prepend.
2568
+ * @returns True when prepended.
2569
+
2570
+
2571
+
2572
+
2573
+
2574
+
2575
+
2576
+
2577
+
2578
+
2579
+
2580
+ * @example
2581
+ * // Deque shift and unshift operations
2582
+ * const deque = new Deque<number>([20, 30, 40]);
2583
+ *
2584
+ * // Unshift adds to the front
2585
+ * deque.unshift(10);
2586
+ * console.log([...deque]); // [10, 20, 30, 40];
2587
+ *
2588
+ * // Shift removes from the front (O(1) complexity!)
2589
+ * const first = deque.shift();
2590
+ * console.log(first); // 10;
2591
+ *
2592
+ * // Verify remaining elements
2593
+ * console.log([...deque]); // [20, 30, 40];
2594
+ * console.log(deque.length); // 3;
2595
+ */
1925
2596
  unshift(element) {
1926
2597
  if (this._length) {
1927
2598
  if (this._firstInBucket > 0) {
@@ -1975,18 +2646,45 @@ var queueTyped = (() => {
1975
2646
  return ans;
1976
2647
  }
1977
2648
  /**
1978
- * Check whether the deque is empty.
1979
- * @remarks Time O(1), Space O(1)
1980
- * @returns True if length is 0.
1981
- */
2649
+ * Check whether the deque is empty.
2650
+ * @remarks Time O(1), Space O(1)
2651
+ * @returns True if length is 0.
2652
+
2653
+
2654
+
2655
+
2656
+
2657
+
2658
+
2659
+
2660
+
2661
+ * @example
2662
+ * // Check if empty
2663
+ * const dq = new Deque();
2664
+ * console.log(dq.isEmpty()); // true;
2665
+ */
1982
2666
  isEmpty() {
1983
2667
  return this._length === 0;
1984
2668
  }
1985
2669
  /**
1986
- * Remove all elements and reset structure.
1987
- * @remarks Time O(1), Space O(1)
1988
- * @returns void
1989
- */
2670
+ * Remove all elements and reset structure.
2671
+ * @remarks Time O(1), Space O(1)
2672
+ * @returns void
2673
+
2674
+
2675
+
2676
+
2677
+
2678
+
2679
+
2680
+
2681
+
2682
+ * @example
2683
+ * // Remove all elements
2684
+ * const dq = new Deque<number>([1, 2, 3]);
2685
+ * dq.clear();
2686
+ * console.log(dq.length); // 0;
2687
+ */
1990
2688
  clear() {
1991
2689
  this._buckets = [new Array(this._bucketSize)];
1992
2690
  this._bucketCount = 1;
@@ -1994,11 +2692,24 @@ var queueTyped = (() => {
1994
2692
  this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
1995
2693
  }
1996
2694
  /**
1997
- * Get the element at a given position.
1998
- * @remarks Time O(1), Space O(1)
1999
- * @param pos - Zero-based position from the front.
2000
- * @returns Element or undefined.
2001
- */
2695
+ * Get the element at a given position.
2696
+ * @remarks Time O(1), Space O(1)
2697
+ * @param pos - Zero-based position from the front.
2698
+ * @returns Element or undefined.
2699
+
2700
+
2701
+
2702
+
2703
+
2704
+
2705
+
2706
+
2707
+ * @example
2708
+ * // Access by index
2709
+ * const dq = new Deque<string>(['a', 'b', 'c']);
2710
+ * console.log(dq.at(0)); // 'a';
2711
+ * console.log(dq.at(2)); // 'c';
2712
+ */
2002
2713
  at(pos) {
2003
2714
  if (pos < 0 || pos >= this._length) return void 0;
2004
2715
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
@@ -2157,11 +2868,24 @@ var queueTyped = (() => {
2157
2868
  }
2158
2869
  }
2159
2870
  /**
2160
- * Delete the first occurrence of a value.
2161
- * @remarks Time O(N), Space O(1)
2162
- * @param element - Element to remove (using the configured equality).
2163
- * @returns True if an element was removed.
2164
- */
2871
+ * Delete the first occurrence of a value.
2872
+ * @remarks Time O(N), Space O(1)
2873
+ * @param element - Element to remove (using the configured equality).
2874
+ * @returns True if an element was removed.
2875
+
2876
+
2877
+
2878
+
2879
+
2880
+
2881
+
2882
+
2883
+ * @example
2884
+ * // Remove element
2885
+ * const dq = new Deque<number>([1, 2, 3]);
2886
+ * dq.delete(2);
2887
+ * console.log(dq.length); // 2;
2888
+ */
2165
2889
  delete(element) {
2166
2890
  const size = this._length;
2167
2891
  if (size === 0) return false;
@@ -2205,10 +2929,39 @@ var queueTyped = (() => {
2205
2929
  return this;
2206
2930
  }
2207
2931
  /**
2208
- * Reverse the deque by reversing buckets and pointers.
2209
- * @remarks Time O(N), Space O(N)
2210
- * @returns This deque.
2211
- */
2932
+ * Reverse the deque by reversing buckets and pointers.
2933
+ * @remarks Time O(N), Space O(N)
2934
+ * @returns This deque.
2935
+
2936
+
2937
+
2938
+
2939
+
2940
+
2941
+
2942
+
2943
+
2944
+
2945
+
2946
+ * @example
2947
+ * // Deque for...of iteration and reverse
2948
+ * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
2949
+ *
2950
+ * // Iterate forward
2951
+ * const forward: string[] = [];
2952
+ * for (const item of deque) {
2953
+ * forward.push(item);
2954
+ * }
2955
+ * console.log(forward); // ['A', 'B', 'C', 'D'];
2956
+ *
2957
+ * // Reverse the deque
2958
+ * deque.reverse();
2959
+ * const backward: string[] = [];
2960
+ * for (const item of deque) {
2961
+ * backward.push(item);
2962
+ * }
2963
+ * console.log(backward); // ['D', 'C', 'B', 'A'];
2964
+ */
2212
2965
  reverse() {
2213
2966
  this._buckets.reverse().forEach(function(bucket) {
2214
2967
  bucket.reverse();
@@ -2246,11 +2999,55 @@ var queueTyped = (() => {
2246
2999
  * @remarks Time O(N), Space O(1)
2247
3000
  * @returns void
2248
3001
  */
3002
+ /**
3003
+ * (Protected) Trigger auto-compaction if space utilization drops below threshold.
3004
+ * Only checks every `_bucketSize` operations to minimize hot-path overhead.
3005
+ * Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
3006
+ */
3007
+ _autoCompact() {
3008
+ if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
3009
+ this._compactCounter++;
3010
+ if (this._compactCounter < this._bucketSize) return;
3011
+ this._compactCounter = 0;
3012
+ const utilization = this._length / (this._bucketCount * this._bucketSize);
3013
+ if (utilization < this._autoCompactRatio) {
3014
+ this.shrinkToFit();
3015
+ }
3016
+ }
3017
+ /**
3018
+ * Compact the deque by removing unused buckets.
3019
+ * @remarks Time O(N), Space O(1)
3020
+ * @returns True if compaction was performed (bucket count reduced).
3021
+ */
3022
+ /**
3023
+ * Compact the deque by removing unused buckets.
3024
+ * @remarks Time O(N), Space O(1)
3025
+ * @returns True if compaction was performed (bucket count reduced).
3026
+
3027
+
3028
+
3029
+
3030
+
3031
+
3032
+
3033
+
3034
+ * @example
3035
+ * // Reclaim memory
3036
+ * const dq = new Deque<number>([1, 2, 3, 4, 5]);
3037
+ * dq.shift();
3038
+ * dq.shift();
3039
+ * dq.compact();
3040
+ * console.log(dq.length); // 3;
3041
+ */
3042
+ compact() {
3043
+ const before = this._bucketCount;
3044
+ this.shrinkToFit();
3045
+ return this._bucketCount < before;
3046
+ }
2249
3047
  shrinkToFit() {
2250
3048
  if (this._length === 0) return;
2251
3049
  const newBuckets = [];
2252
- if (this._bucketFirst === this._bucketLast) return;
2253
- else if (this._bucketFirst < this._bucketLast) {
3050
+ if (this._bucketFirst <= this._bucketLast) {
2254
3051
  for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
2255
3052
  newBuckets.push(this._buckets[i]);
2256
3053
  }
@@ -2265,12 +3062,30 @@ var queueTyped = (() => {
2265
3062
  this._bucketFirst = 0;
2266
3063
  this._bucketLast = newBuckets.length - 1;
2267
3064
  this._buckets = newBuckets;
2268
- }
2269
- /**
2270
- * Deep clone this deque, preserving options.
2271
- * @remarks Time O(N), Space O(N)
2272
- * @returns A new deque with the same content and options.
2273
- */
3065
+ this._bucketCount = newBuckets.length;
3066
+ this._compactCounter = 0;
3067
+ }
3068
+ /**
3069
+ * Deep clone this deque, preserving options.
3070
+ * @remarks Time O(N), Space O(N)
3071
+ * @returns A new deque with the same content and options.
3072
+
3073
+
3074
+
3075
+
3076
+
3077
+
3078
+
3079
+
3080
+
3081
+ * @example
3082
+ * // Create independent copy
3083
+ * const dq = new Deque<number>([1, 2, 3]);
3084
+ * const copy = dq.clone();
3085
+ * copy.pop();
3086
+ * console.log(dq.length); // 3;
3087
+ * console.log(copy.length); // 2;
3088
+ */
2274
3089
  clone() {
2275
3090
  return this._createLike(this, {
2276
3091
  bucketSize: this.bucketSize,
@@ -2279,12 +3094,26 @@ var queueTyped = (() => {
2279
3094
  });
2280
3095
  }
2281
3096
  /**
2282
- * Filter elements into a new deque of the same class.
2283
- * @remarks Time O(N), Space O(N)
2284
- * @param predicate - Predicate (value, index, deque) → boolean to keep element.
2285
- * @param [thisArg] - Value for `this` inside the predicate.
2286
- * @returns A new deque with kept elements.
2287
- */
3097
+ * Filter elements into a new deque of the same class.
3098
+ * @remarks Time O(N), Space O(N)
3099
+ * @param predicate - Predicate (value, index, deque) → boolean to keep element.
3100
+ * @param [thisArg] - Value for `this` inside the predicate.
3101
+ * @returns A new deque with kept elements.
3102
+
3103
+
3104
+
3105
+
3106
+
3107
+
3108
+
3109
+
3110
+
3111
+ * @example
3112
+ * // Filter elements
3113
+ * const dq = new Deque<number>([1, 2, 3, 4]);
3114
+ * const result = dq.filter(x => x > 2);
3115
+ * console.log(result.length); // 2;
3116
+ */
2288
3117
  filter(predicate, thisArg) {
2289
3118
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
2290
3119
  out._setBucketSize(this._bucketSize);
@@ -2313,15 +3142,28 @@ var queueTyped = (() => {
2313
3142
  return out;
2314
3143
  }
2315
3144
  /**
2316
- * Map elements into a new deque (possibly different element type).
2317
- * @remarks Time O(N), Space O(N)
2318
- * @template EM
2319
- * @template RM
2320
- * @param callback - Mapping function (value, index, deque) → newElement.
2321
- * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
2322
- * @param [thisArg] - Value for `this` inside the callback.
2323
- * @returns A new Deque with mapped elements.
2324
- */
3145
+ * Map elements into a new deque (possibly different element type).
3146
+ * @remarks Time O(N), Space O(N)
3147
+ * @template EM
3148
+ * @template RM
3149
+ * @param callback - Mapping function (value, index, deque) → newElement.
3150
+ * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
3151
+ * @param [thisArg] - Value for `this` inside the callback.
3152
+ * @returns A new Deque with mapped elements.
3153
+
3154
+
3155
+
3156
+
3157
+
3158
+
3159
+
3160
+
3161
+ * @example
3162
+ * // Transform elements
3163
+ * const dq = new Deque<number>([1, 2, 3]);
3164
+ * const result = dq.map(x => x * 10);
3165
+ * console.log(result.toArray()); // [10, 20, 30];
3166
+ */
2325
3167
  map(callback, options, thisArg) {
2326
3168
  const out = this._createLike([], {
2327
3169
  ...options != null ? options : {},
@@ -2444,27 +3286,6 @@ var queueTyped = (() => {
2444
3286
  }
2445
3287
  }
2446
3288
  };
2447
-
2448
- // src/common/index.ts
2449
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
2450
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
2451
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
2452
- return DFSOperation2;
2453
- })(DFSOperation || {});
2454
- var Range = class {
2455
- constructor(low, high, includeLow = true, includeHigh = true) {
2456
- this.low = low;
2457
- this.high = high;
2458
- this.includeLow = includeLow;
2459
- this.includeHigh = includeHigh;
2460
- }
2461
- // Determine whether a key is within the range
2462
- isInRange(key, comparator) {
2463
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
2464
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
2465
- return lowCheck && highCheck;
2466
- }
2467
- };
2468
3289
  return __toCommonJS(src_exports);
2469
3290
  })();
2470
3291
  /**