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
@@ -719,11 +719,37 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
719
719
  return list;
720
720
  }
721
721
  /**
722
- * Append an element/node to the tail.
723
- * @remarks Time O(1), Space O(1)
724
- * @param elementOrNode - Element or node to append.
725
- * @returns True when appended.
726
- */
722
+ * Append an element/node to the tail.
723
+ * @remarks Time O(1), Space O(1)
724
+ * @param elementOrNode - Element or node to append.
725
+ * @returns True when appended.
726
+
727
+
728
+
729
+
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+ * @example
738
+ * // basic SinglyLinkedList creation and push operation
739
+ * // Create a simple SinglyLinkedList with initial values
740
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
741
+ *
742
+ * // Verify the list maintains insertion order
743
+ * console.log([...list]); // [1, 2, 3, 4, 5];
744
+ *
745
+ * // Check length
746
+ * console.log(list.length); // 5;
747
+ *
748
+ * // Push a new element to the end
749
+ * list.push(6);
750
+ * console.log(list.length); // 6;
751
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
752
+ */
727
753
  push(elementOrNode) {
728
754
  const newNode = this._ensureNode(elementOrNode);
729
755
  if (!this.head) {
@@ -737,10 +763,36 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
737
763
  return true;
738
764
  }
739
765
  /**
740
- * Remove and return the tail element.
741
- * @remarks Time O(N), Space O(1)
742
- * @returns Removed element or undefined.
743
- */
766
+ * Remove and return the tail element.
767
+ * @remarks Time O(N), Space O(1)
768
+ * @returns Removed element or undefined.
769
+
770
+
771
+
772
+
773
+
774
+
775
+
776
+
777
+
778
+
779
+
780
+ * @example
781
+ * // SinglyLinkedList pop and shift operations
782
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
783
+ *
784
+ * // Pop removes from the end
785
+ * const last = list.pop();
786
+ * console.log(last); // 50;
787
+ *
788
+ * // Shift removes from the beginning
789
+ * const first = list.shift();
790
+ * console.log(first); // 10;
791
+ *
792
+ * // Verify remaining elements
793
+ * console.log([...list]); // [20, 30, 40];
794
+ * console.log(list.length); // 3;
795
+ */
744
796
  pop() {
745
797
  var _a;
746
798
  if (!this.head) return void 0;
@@ -760,10 +812,26 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
760
812
  return value;
761
813
  }
762
814
  /**
763
- * Remove and return the head element.
764
- * @remarks Time O(1), Space O(1)
765
- * @returns Removed element or undefined.
766
- */
815
+ * Remove and return the head element.
816
+ * @remarks Time O(1), Space O(1)
817
+ * @returns Removed element or undefined.
818
+
819
+
820
+
821
+
822
+
823
+
824
+
825
+
826
+
827
+
828
+
829
+ * @example
830
+ * // Remove from the front
831
+ * const list = new SinglyLinkedList<number>([10, 20, 30]);
832
+ * console.log(list.shift()); // 10;
833
+ * console.log(list.length); // 2;
834
+ */
767
835
  shift() {
768
836
  if (!this.head) return void 0;
769
837
  const removed = this.head;
@@ -773,11 +841,42 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
773
841
  return removed.value;
774
842
  }
775
843
  /**
776
- * Prepend an element/node to the head.
777
- * @remarks Time O(1), Space O(1)
778
- * @param elementOrNode - Element or node to prepend.
779
- * @returns True when prepended.
780
- */
844
+ * Prepend an element/node to the head.
845
+ * @remarks Time O(1), Space O(1)
846
+ * @param elementOrNode - Element or node to prepend.
847
+ * @returns True when prepended.
848
+
849
+
850
+
851
+
852
+
853
+
854
+
855
+
856
+
857
+
858
+
859
+ * @example
860
+ * // SinglyLinkedList unshift and forward traversal
861
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
862
+ *
863
+ * // Unshift adds to the beginning
864
+ * list.unshift(10);
865
+ * console.log([...list]); // [10, 20, 30, 40];
866
+ *
867
+ * // Access elements (forward traversal only for singly linked)
868
+ * const second = list.at(1);
869
+ * console.log(second); // 20;
870
+ *
871
+ * // SinglyLinkedList allows forward iteration only
872
+ * const elements: number[] = [];
873
+ * for (const item of list) {
874
+ * elements.push(item);
875
+ * }
876
+ * console.log(elements); // [10, 20, 30, 40];
877
+ *
878
+ * console.log(list.length); // 4;
879
+ */
781
880
  unshift(elementOrNode) {
782
881
  const newNode = this._ensureNode(elementOrNode);
783
882
  if (!this.head) {
@@ -833,11 +932,28 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
833
932
  return void 0;
834
933
  }
835
934
  /**
836
- * Get the element at a given index.
837
- * @remarks Time O(N), Space O(1)
838
- * @param index - Zero-based index.
839
- * @returns Element or undefined.
840
- */
935
+ * Get the element at a given index.
936
+ * @remarks Time O(N), Space O(1)
937
+ * @param index - Zero-based index.
938
+ * @returns Element or undefined.
939
+
940
+
941
+
942
+
943
+
944
+
945
+
946
+
947
+
948
+
949
+
950
+ * @example
951
+ * // Access element by index
952
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
953
+ * console.log(list.at(0)); // 'a';
954
+ * console.log(list.at(2)); // 'c';
955
+ * console.log(list.at(3)); // 'd';
956
+ */
841
957
  at(index) {
842
958
  if (index < 0 || index >= this._length) return void 0;
843
959
  let current = this.head;
@@ -854,11 +970,23 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
854
970
  return elementNodeOrPredicate instanceof SinglyLinkedListNode;
855
971
  }
856
972
  /**
857
- * Get the node reference at a given index.
858
- * @remarks Time O(N), Space O(1)
859
- * @param index - Zero-based index.
860
- * @returns Node or undefined.
861
- */
973
+ * Get the node reference at a given index.
974
+ * @remarks Time O(N), Space O(1)
975
+ * @param index - Zero-based index.
976
+ * @returns Node or undefined.
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+ * @example
986
+ * // Get node at index
987
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
988
+ * console.log(list.getNodeAt(1)?.value); // 'b';
989
+ */
862
990
  getNodeAt(index) {
863
991
  if (index < 0 || index >= this._length) return void 0;
864
992
  let current = this.head;
@@ -866,11 +994,24 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
866
994
  return current;
867
995
  }
868
996
  /**
869
- * Delete the element at an index.
870
- * @remarks Time O(N), Space O(1)
871
- * @param index - Zero-based index.
872
- * @returns Removed element or undefined.
873
- */
997
+ * Delete the element at an index.
998
+ * @remarks Time O(N), Space O(1)
999
+ * @param index - Zero-based index.
1000
+ * @returns Removed element or undefined.
1001
+
1002
+
1003
+
1004
+
1005
+
1006
+
1007
+
1008
+
1009
+ * @example
1010
+ * // Remove by index
1011
+ * const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
1012
+ * list.deleteAt(1);
1013
+ * console.log(list.toArray()); // ['a', 'c'];
1014
+ */
874
1015
  deleteAt(index) {
875
1016
  if (index < 0 || index >= this._length) return void 0;
876
1017
  if (index === 0) return this.shift();
@@ -883,11 +1024,24 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
883
1024
  return value;
884
1025
  }
885
1026
  /**
886
- * Delete the first match by value/node.
887
- * @remarks Time O(N), Space O(1)
888
- * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
889
- * @returns True if removed.
890
- */
1027
+ * Delete the first match by value/node.
1028
+ * @remarks Time O(N), Space O(1)
1029
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
1030
+ * @returns True if removed.
1031
+
1032
+
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+ * @example
1040
+ * // Remove first occurrence
1041
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
1042
+ * list.delete(2);
1043
+ * console.log(list.toArray()); // [1, 3, 2];
1044
+ */
891
1045
  delete(elementOrNode) {
892
1046
  if (elementOrNode === void 0 || !this.head) return false;
893
1047
  const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
@@ -904,12 +1058,25 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
904
1058
  return true;
905
1059
  }
906
1060
  /**
907
- * Insert a new element/node at an index, shifting following nodes.
908
- * @remarks Time O(N), Space O(1)
909
- * @param index - Zero-based index.
910
- * @param newElementOrNode - Element or node to insert.
911
- * @returns True if inserted.
912
- */
1061
+ * Insert a new element/node at an index, shifting following nodes.
1062
+ * @remarks Time O(N), Space O(1)
1063
+ * @param index - Zero-based index.
1064
+ * @param newElementOrNode - Element or node to insert.
1065
+ * @returns True if inserted.
1066
+
1067
+
1068
+
1069
+
1070
+
1071
+
1072
+
1073
+
1074
+ * @example
1075
+ * // Insert at index
1076
+ * const list = new SinglyLinkedList<number>([1, 3]);
1077
+ * list.addAt(1, 2);
1078
+ * console.log(list.toArray()); // [1, 2, 3];
1079
+ */
913
1080
  addAt(index, newElementOrNode) {
914
1081
  if (index < 0 || index > this._length) return false;
915
1082
  if (index === 0) return this.unshift(newElementOrNode);
@@ -935,28 +1102,70 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
935
1102
  return true;
936
1103
  }
937
1104
  /**
938
- * Check whether the list is empty.
939
- * @remarks Time O(1), Space O(1)
940
- * @returns True if length is 0.
941
- */
1105
+ * Check whether the list is empty.
1106
+ * @remarks Time O(1), Space O(1)
1107
+ * @returns True if length is 0.
1108
+
1109
+
1110
+
1111
+
1112
+
1113
+
1114
+
1115
+
1116
+
1117
+ * @example
1118
+ * // Check empty
1119
+ * console.log(new SinglyLinkedList().isEmpty()); // true;
1120
+ */
942
1121
  isEmpty() {
943
1122
  return this._length === 0;
944
1123
  }
945
1124
  /**
946
- * Remove all nodes and reset length.
947
- * @remarks Time O(N), Space O(1)
948
- * @returns void
949
- */
1125
+ * Remove all nodes and reset length.
1126
+ * @remarks Time O(N), Space O(1)
1127
+ * @returns void
1128
+
1129
+
1130
+
1131
+
1132
+
1133
+
1134
+
1135
+
1136
+
1137
+ * @example
1138
+ * // Remove all
1139
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1140
+ * list.clear();
1141
+ * console.log(list.isEmpty()); // true;
1142
+ */
950
1143
  clear() {
951
1144
  this._head = void 0;
952
1145
  this._tail = void 0;
953
1146
  this._length = 0;
954
1147
  }
955
1148
  /**
956
- * Reverse the list in place.
957
- * @remarks Time O(N), Space O(1)
958
- * @returns This list.
959
- */
1149
+ * Reverse the list in place.
1150
+ * @remarks Time O(N), Space O(1)
1151
+ * @returns This list.
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+ * @example
1164
+ * // Reverse the list in-place
1165
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
1166
+ * list.reverse();
1167
+ * console.log([...list]); // [4, 3, 2, 1];
1168
+ */
960
1169
  reverse() {
961
1170
  if (!this.head || this.head === this.tail) return this;
962
1171
  let prev;
@@ -1131,22 +1340,64 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
1131
1340
  return false;
1132
1341
  }
1133
1342
  /**
1134
- * Deep clone this list (values are copied by reference).
1135
- * @remarks Time O(N), Space O(N)
1136
- * @returns A new list with the same element sequence.
1137
- */
1343
+ * Deep clone this list (values are copied by reference).
1344
+ * @remarks Time O(N), Space O(N)
1345
+ * @returns A new list with the same element sequence.
1346
+
1347
+
1348
+
1349
+
1350
+
1351
+
1352
+
1353
+
1354
+
1355
+ * @example
1356
+ * // Deep copy
1357
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1358
+ * const copy = list.clone();
1359
+ * copy.pop();
1360
+ * console.log(list.length); // 3;
1361
+ * console.log(copy.length); // 2;
1362
+ */
1138
1363
  clone() {
1139
1364
  const out = this._createInstance();
1140
1365
  for (const v of this) out.push(v);
1141
1366
  return out;
1142
1367
  }
1143
1368
  /**
1144
- * Filter values into a new list of the same class.
1145
- * @remarks Time O(N), Space O(N)
1146
- * @param callback - Predicate (value, index, list) → boolean to keep value.
1147
- * @param [thisArg] - Value for `this` inside the callback.
1148
- * @returns A new list with kept values.
1149
- */
1369
+ * Filter values into a new list of the same class.
1370
+ * @remarks Time O(N), Space O(N)
1371
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
1372
+ * @param [thisArg] - Value for `this` inside the callback.
1373
+ * @returns A new list with kept values.
1374
+
1375
+
1376
+
1377
+
1378
+
1379
+
1380
+
1381
+
1382
+
1383
+
1384
+
1385
+ * @example
1386
+ * // SinglyLinkedList filter and map operations
1387
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
1388
+ *
1389
+ * // Filter even numbers
1390
+ * const filtered = list.filter(value => value % 2 === 0);
1391
+ * console.log(filtered.length); // 2;
1392
+ *
1393
+ * // Map to double values
1394
+ * const doubled = list.map(value => value * 2);
1395
+ * console.log(doubled.length); // 5;
1396
+ *
1397
+ * // Use reduce to sum
1398
+ * const sum = list.reduce((acc, value) => acc + value, 0);
1399
+ * console.log(sum); // 15;
1400
+ */
1150
1401
  filter(callback, thisArg) {
1151
1402
  const out = this._createInstance();
1152
1403
  let index = 0;
@@ -1170,15 +1421,31 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
1170
1421
  return out;
1171
1422
  }
1172
1423
  /**
1173
- * Map values into a new list (possibly different element type).
1174
- * @remarks Time O(N), Space O(N)
1175
- * @template EM
1176
- * @template RM
1177
- * @param callback - Mapping function (value, index, list) → newElement.
1178
- * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1179
- * @param [thisArg] - Value for `this` inside the callback.
1180
- * @returns A new SinglyLinkedList with mapped values.
1181
- */
1424
+ * Map values into a new list (possibly different element type).
1425
+ * @remarks Time O(N), Space O(N)
1426
+ * @template EM
1427
+ * @template RM
1428
+ * @param callback - Mapping function (value, index, list) → newElement.
1429
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
1430
+ * @param [thisArg] - Value for `this` inside the callback.
1431
+ * @returns A new SinglyLinkedList with mapped values.
1432
+
1433
+
1434
+
1435
+
1436
+
1437
+
1438
+
1439
+
1440
+
1441
+
1442
+
1443
+ * @example
1444
+ * // Transform elements
1445
+ * const list = new SinglyLinkedList<number>([1, 2, 3]);
1446
+ * const doubled = list.map(n => n * 2);
1447
+ * console.log([...doubled]); // [2, 4, 6];
1448
+ */
1182
1449
  map(callback, options, thisArg) {
1183
1450
  const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
1184
1451
  let index = 0;
@@ -1315,6 +1582,54 @@ function elementOrPredicate(input, equals) {
1315
1582
  }
1316
1583
  __name(elementOrPredicate, "elementOrPredicate");
1317
1584
 
1585
+ // src/common/error.ts
1586
+ var ERR = {
1587
+ // Range / index
1588
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
1589
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
1590
+ // Type / argument
1591
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
1592
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
1593
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
1594
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
1595
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
1596
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
1597
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
1598
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
1599
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
1600
+ // State / operation
1601
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
1602
+ // Matrix
1603
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
1604
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
1605
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
1606
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
1607
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
1608
+ };
1609
+
1610
+ // src/common/index.ts
1611
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
1612
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
1613
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
1614
+ return DFSOperation2;
1615
+ })(DFSOperation || {});
1616
+ var _Range = class _Range {
1617
+ constructor(low, high, includeLow = true, includeHigh = true) {
1618
+ this.low = low;
1619
+ this.high = high;
1620
+ this.includeLow = includeLow;
1621
+ this.includeHigh = includeHigh;
1622
+ }
1623
+ // Determine whether a key is within the range
1624
+ isInRange(key, comparator) {
1625
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1626
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1627
+ return lowCheck && highCheck;
1628
+ }
1629
+ };
1630
+ __name(_Range, "Range");
1631
+ var Range = _Range;
1632
+
1318
1633
  // src/data-structures/queue/queue.ts
1319
1634
  var _Queue = class _Queue extends LinearBase {
1320
1635
  /**
@@ -1369,18 +1684,52 @@ var _Queue = class _Queue extends LinearBase {
1369
1684
  this._autoCompactRatio = value;
1370
1685
  }
1371
1686
  /**
1372
- * Get the number of elements currently in the queue.
1373
- * @remarks Time O(1), Space O(1)
1374
- * @returns Current length.
1375
- */
1687
+ * Get the number of elements currently in the queue.
1688
+ * @remarks Time O(1), Space O(1)
1689
+ * @returns Current length.
1690
+
1691
+
1692
+
1693
+
1694
+
1695
+
1696
+
1697
+
1698
+
1699
+
1700
+
1701
+ * @example
1702
+ * // Track queue length
1703
+ * const q = new Queue<number>();
1704
+ * console.log(q.length); // 0;
1705
+ * q.push(1);
1706
+ * q.push(2);
1707
+ * console.log(q.length); // 2;
1708
+ */
1376
1709
  get length() {
1377
1710
  return this.elements.length - this._offset;
1378
1711
  }
1379
1712
  /**
1380
- * Get the first element (front) without removing it.
1381
- * @remarks Time O(1), Space O(1)
1382
- * @returns Front element or undefined.
1383
- */
1713
+ * Get the first element (front) without removing it.
1714
+ * @remarks Time O(1), Space O(1)
1715
+ * @returns Front element or undefined.
1716
+
1717
+
1718
+
1719
+
1720
+
1721
+
1722
+
1723
+
1724
+
1725
+
1726
+
1727
+ * @example
1728
+ * // View the front element
1729
+ * const q = new Queue<string>(['first', 'second', 'third']);
1730
+ * console.log(q.first); // 'first';
1731
+ * console.log(q.length); // 3;
1732
+ */
1384
1733
  get first() {
1385
1734
  return this.length > 0 ? this.elements[this._offset] : void 0;
1386
1735
  }
@@ -1403,19 +1752,69 @@ var _Queue = class _Queue extends LinearBase {
1403
1752
  return new _Queue(elements);
1404
1753
  }
1405
1754
  /**
1406
- * Check whether the queue is empty.
1407
- * @remarks Time O(1), Space O(1)
1408
- * @returns True if length is 0.
1409
- */
1755
+ * Check whether the queue is empty.
1756
+ * @remarks Time O(1), Space O(1)
1757
+ * @returns True if length is 0.
1758
+
1759
+
1760
+
1761
+
1762
+
1763
+
1764
+
1765
+
1766
+
1767
+
1768
+
1769
+ * @example
1770
+ * // Queue for...of iteration and isEmpty check
1771
+ * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
1772
+ *
1773
+ * const elements: string[] = [];
1774
+ * for (const item of queue) {
1775
+ * elements.push(item);
1776
+ * }
1777
+ *
1778
+ * // Verify all elements are iterated in order
1779
+ * console.log(elements); // ['A', 'B', 'C', 'D'];
1780
+ *
1781
+ * // Process all elements
1782
+ * while (queue.length > 0) {
1783
+ * queue.shift();
1784
+ * }
1785
+ *
1786
+ * console.log(queue.length); // 0;
1787
+ */
1410
1788
  isEmpty() {
1411
1789
  return this.length === 0;
1412
1790
  }
1413
1791
  /**
1414
- * Enqueue one element at the back.
1415
- * @remarks Time O(1), Space O(1)
1416
- * @param element - Element to enqueue.
1417
- * @returns True on success.
1418
- */
1792
+ * Enqueue one element at the back.
1793
+ * @remarks Time O(1), Space O(1)
1794
+ * @param element - Element to enqueue.
1795
+ * @returns True on success.
1796
+
1797
+
1798
+
1799
+
1800
+
1801
+
1802
+
1803
+
1804
+
1805
+
1806
+
1807
+ * @example
1808
+ * // basic Queue creation and push operation
1809
+ * // Create a simple Queue with initial values
1810
+ * const queue = new Queue([1, 2, 3, 4, 5]);
1811
+ *
1812
+ * // Verify the queue maintains insertion order
1813
+ * console.log([...queue]); // [1, 2, 3, 4, 5];
1814
+ *
1815
+ * // Check length
1816
+ * console.log(queue.length); // 5;
1817
+ */
1419
1818
  push(element) {
1420
1819
  this.elements.push(element);
1421
1820
  if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
@@ -1436,10 +1835,35 @@ var _Queue = class _Queue extends LinearBase {
1436
1835
  return ans;
1437
1836
  }
1438
1837
  /**
1439
- * Dequeue one element from the front (amortized via offset).
1440
- * @remarks Time O(1) amortized, Space O(1)
1441
- * @returns Removed element or undefined.
1442
- */
1838
+ * Dequeue one element from the front (amortized via offset).
1839
+ * @remarks Time O(1) amortized, Space O(1)
1840
+ * @returns Removed element or undefined.
1841
+
1842
+
1843
+
1844
+
1845
+
1846
+
1847
+
1848
+
1849
+
1850
+
1851
+
1852
+ * @example
1853
+ * // Queue shift and peek operations
1854
+ * const queue = new Queue<number>([10, 20, 30, 40]);
1855
+ *
1856
+ * // Peek at the front element without removing it
1857
+ * console.log(queue.first); // 10;
1858
+ *
1859
+ * // Remove and get the first element (FIFO)
1860
+ * const first = queue.shift();
1861
+ * console.log(first); // 10;
1862
+ *
1863
+ * // Verify remaining elements and length decreased
1864
+ * console.log([...queue]); // [20, 30, 40];
1865
+ * console.log(queue.length); // 3;
1866
+ */
1443
1867
  shift() {
1444
1868
  if (this.length === 0) return void 0;
1445
1869
  const first = this.first;
@@ -1448,11 +1872,24 @@ var _Queue = class _Queue extends LinearBase {
1448
1872
  return first;
1449
1873
  }
1450
1874
  /**
1451
- * Delete the first occurrence of a specific element.
1452
- * @remarks Time O(N), Space O(1)
1453
- * @param element - Element to remove (strict equality via Object.is).
1454
- * @returns True if an element was removed.
1455
- */
1875
+ * Delete the first occurrence of a specific element.
1876
+ * @remarks Time O(N), Space O(1)
1877
+ * @param element - Element to remove (strict equality via Object.is).
1878
+ * @returns True if an element was removed.
1879
+
1880
+
1881
+
1882
+
1883
+
1884
+
1885
+
1886
+
1887
+ * @example
1888
+ * // Remove specific element
1889
+ * const q = new Queue<number>([1, 2, 3, 2]);
1890
+ * q.delete(2);
1891
+ * console.log(q.length); // 3;
1892
+ */
1456
1893
  delete(element) {
1457
1894
  for (let i = this._offset; i < this.elements.length; i++) {
1458
1895
  if (Object.is(this.elements[i], element)) {
@@ -1463,11 +1900,24 @@ var _Queue = class _Queue extends LinearBase {
1463
1900
  return false;
1464
1901
  }
1465
1902
  /**
1466
- * Get the element at a given logical index.
1467
- * @remarks Time O(1), Space O(1)
1468
- * @param index - Zero-based index from the front.
1469
- * @returns Element or undefined.
1470
- */
1903
+ * Get the element at a given logical index.
1904
+ * @remarks Time O(1), Space O(1)
1905
+ * @param index - Zero-based index from the front.
1906
+ * @returns Element or undefined.
1907
+
1908
+
1909
+
1910
+
1911
+
1912
+
1913
+
1914
+
1915
+ * @example
1916
+ * // Access element by index
1917
+ * const q = new Queue<string>(['a', 'b', 'c']);
1918
+ * console.log(q.at(0)); // 'a';
1919
+ * console.log(q.at(2)); // 'c';
1920
+ */
1471
1921
  at(index) {
1472
1922
  if (index < 0 || index >= this.length) return void 0;
1473
1923
  return this._elements[this._offset + index];
@@ -1519,19 +1969,48 @@ var _Queue = class _Queue extends LinearBase {
1519
1969
  return this;
1520
1970
  }
1521
1971
  /**
1522
- * Remove all elements and reset offset.
1523
- * @remarks Time O(1), Space O(1)
1524
- * @returns void
1525
- */
1972
+ * Remove all elements and reset offset.
1973
+ * @remarks Time O(1), Space O(1)
1974
+ * @returns void
1975
+
1976
+
1977
+
1978
+
1979
+
1980
+
1981
+
1982
+
1983
+
1984
+ * @example
1985
+ * // Remove all elements
1986
+ * const q = new Queue<number>([1, 2, 3]);
1987
+ * q.clear();
1988
+ * console.log(q.length); // 0;
1989
+ */
1526
1990
  clear() {
1527
1991
  this._elements = [];
1528
1992
  this._offset = 0;
1529
1993
  }
1530
1994
  /**
1531
- * Compact storage by discarding consumed head elements.
1532
- * @remarks Time O(N), Space O(N)
1533
- * @returns True when compaction performed.
1534
- */
1995
+ * Compact storage by discarding consumed head elements.
1996
+ * @remarks Time O(N), Space O(N)
1997
+ * @returns True when compaction performed.
1998
+
1999
+
2000
+
2001
+
2002
+
2003
+
2004
+
2005
+
2006
+ * @example
2007
+ * // Reclaim unused memory
2008
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
2009
+ * q.shift();
2010
+ * q.shift();
2011
+ * q.compact();
2012
+ * console.log(q.length); // 3;
2013
+ */
1535
2014
  compact() {
1536
2015
  this._elements = this.elements.slice(this._offset);
1537
2016
  this._offset = 0;
@@ -1557,10 +2036,26 @@ var _Queue = class _Queue extends LinearBase {
1557
2036
  return removed;
1558
2037
  }
1559
2038
  /**
1560
- * Deep clone this queue and its parameters.
1561
- * @remarks Time O(N), Space O(N)
1562
- * @returns A new queue with the same content and options.
1563
- */
2039
+ * Deep clone this queue and its parameters.
2040
+ * @remarks Time O(N), Space O(N)
2041
+ * @returns A new queue with the same content and options.
2042
+
2043
+
2044
+
2045
+
2046
+
2047
+
2048
+
2049
+
2050
+
2051
+ * @example
2052
+ * // Create independent copy
2053
+ * const q = new Queue<number>([1, 2, 3]);
2054
+ * const copy = q.clone();
2055
+ * copy.shift();
2056
+ * console.log(q.length); // 3;
2057
+ * console.log(copy.length); // 2;
2058
+ */
1564
2059
  clone() {
1565
2060
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1566
2061
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -1568,12 +2063,26 @@ var _Queue = class _Queue extends LinearBase {
1568
2063
  return out;
1569
2064
  }
1570
2065
  /**
1571
- * Filter elements into a new queue of the same class.
1572
- * @remarks Time O(N), Space O(N)
1573
- * @param predicate - Predicate (element, index, queue) → boolean to keep element.
1574
- * @param [thisArg] - Value for `this` inside the predicate.
1575
- * @returns A new queue with kept elements.
1576
- */
2066
+ * Filter elements into a new queue of the same class.
2067
+ * @remarks Time O(N), Space O(N)
2068
+ * @param predicate - Predicate (element, index, queue) → boolean to keep element.
2069
+ * @param [thisArg] - Value for `this` inside the predicate.
2070
+ * @returns A new queue with kept elements.
2071
+
2072
+
2073
+
2074
+
2075
+
2076
+
2077
+
2078
+
2079
+
2080
+ * @example
2081
+ * // Filter elements
2082
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
2083
+ * const evens = q.filter(x => x % 2 === 0);
2084
+ * console.log(evens.length); // 2;
2085
+ */
1577
2086
  filter(predicate, thisArg) {
1578
2087
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1579
2088
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -1585,15 +2094,28 @@ var _Queue = class _Queue extends LinearBase {
1585
2094
  return out;
1586
2095
  }
1587
2096
  /**
1588
- * Map each element to a new element in a possibly different-typed queue.
1589
- * @remarks Time O(N), Space O(N)
1590
- * @template EM
1591
- * @template RM
1592
- * @param callback - Mapping function (element, index, queue) → newElement.
1593
- * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
1594
- * @param [thisArg] - Value for `this` inside the callback.
1595
- * @returns A new Queue with mapped elements.
1596
- */
2097
+ * Map each element to a new element in a possibly different-typed queue.
2098
+ * @remarks Time O(N), Space O(N)
2099
+ * @template EM
2100
+ * @template RM
2101
+ * @param callback - Mapping function (element, index, queue) → newElement.
2102
+ * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
2103
+ * @param [thisArg] - Value for `this` inside the callback.
2104
+ * @returns A new Queue with mapped elements.
2105
+
2106
+
2107
+
2108
+
2109
+
2110
+
2111
+
2112
+
2113
+ * @example
2114
+ * // Transform elements
2115
+ * const q = new Queue<number>([1, 2, 3]);
2116
+ * const doubled = q.map(x => x * 2);
2117
+ * console.log(doubled.toArray()); // [2, 4, 6];
2118
+ */
1597
2119
  map(callback, options, thisArg) {
1598
2120
  var _a, _b;
1599
2121
  const out = new this.constructor([], {
@@ -1699,24 +2221,25 @@ __name(_LinkedListQueue, "LinkedListQueue");
1699
2221
  var LinkedListQueue = _LinkedListQueue;
1700
2222
 
1701
2223
  // src/utils/utils.ts
1702
- var rangeCheck = /* @__PURE__ */ __name((index, min, max, message = "Index out of bounds.") => {
1703
- if (index < min || index > max) throw new RangeError(message);
2224
+ var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
2225
+ if (index < min || index > max) {
2226
+ throw new RangeError(message != null ? message : `Index ${index} is out of range [${min}, ${max}].`);
2227
+ }
1704
2228
  }, "rangeCheck");
1705
2229
  var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
1706
2230
 
1707
2231
  // src/data-structures/queue/deque.ts
1708
2232
  var _Deque = class _Deque extends LinearBase {
1709
- /**
1710
- * Create a Deque and optionally bulk-insert elements.
1711
- * @remarks Time O(N), Space O(N)
1712
- * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
1713
- * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
1714
- * @returns New Deque instance.
1715
- */
1716
2233
  constructor(elements = [], options) {
1717
2234
  super(options);
1718
2235
  __publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
1719
2236
  __publicField(this, "_bucketSize", 1 << 12);
2237
+ __publicField(this, "_autoCompactRatio", 0.5);
2238
+ /**
2239
+ * Counter for shift/pop operations since last compaction check.
2240
+ * Only checks ratio every `_bucketSize` operations to minimize overhead.
2241
+ */
2242
+ __publicField(this, "_compactCounter", 0);
1720
2243
  __publicField(this, "_bucketFirst", 0);
1721
2244
  __publicField(this, "_firstInBucket", 0);
1722
2245
  __publicField(this, "_bucketLast", 0);
@@ -1725,8 +2248,9 @@ var _Deque = class _Deque extends LinearBase {
1725
2248
  __publicField(this, "_buckets", []);
1726
2249
  __publicField(this, "_length", 0);
1727
2250
  if (options) {
1728
- const { bucketSize } = options;
2251
+ const { bucketSize, autoCompactRatio } = options;
1729
2252
  if (typeof bucketSize === "number") this._bucketSize = bucketSize;
2253
+ if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
1730
2254
  }
1731
2255
  let _size;
1732
2256
  if ("length" in elements) {
@@ -1751,6 +2275,24 @@ var _Deque = class _Deque extends LinearBase {
1751
2275
  get bucketSize() {
1752
2276
  return this._bucketSize;
1753
2277
  }
2278
+ /**
2279
+ * Get the auto-compaction ratio.
2280
+ * When `elements / (bucketCount * bucketSize)` drops below this ratio after
2281
+ * enough shift/pop operations, the deque auto-compacts.
2282
+ * @remarks Time O(1), Space O(1)
2283
+ * @returns Current ratio threshold. 0 means auto-compact is disabled.
2284
+ */
2285
+ get autoCompactRatio() {
2286
+ return this._autoCompactRatio;
2287
+ }
2288
+ /**
2289
+ * Set the auto-compaction ratio.
2290
+ * @remarks Time O(1), Space O(1)
2291
+ * @param value - Ratio in [0,1]. 0 disables auto-compact.
2292
+ */
2293
+ set autoCompactRatio(value) {
2294
+ this._autoCompactRatio = value;
2295
+ }
1754
2296
  /**
1755
2297
  * Get the index of the first bucket in use.
1756
2298
  * @remarks Time O(1), Space O(1)
@@ -1808,19 +2350,60 @@ var _Deque = class _Deque extends LinearBase {
1808
2350
  return this._length;
1809
2351
  }
1810
2352
  /**
1811
- * Get the first element without removing it.
1812
- * @remarks Time O(1), Space O(1)
1813
- * @returns First element or undefined.
1814
- */
2353
+ * Get the first element without removing it.
2354
+ * @remarks Time O(1), Space O(1)
2355
+ * @returns First element or undefined.
2356
+
2357
+
2358
+
2359
+
2360
+
2361
+
2362
+
2363
+
2364
+
2365
+
2366
+
2367
+ * @example
2368
+ * // Deque peek at both ends
2369
+ * const deque = new Deque<number>([10, 20, 30, 40, 50]);
2370
+ *
2371
+ * // Get first element without removing
2372
+ * const first = deque.at(0);
2373
+ * console.log(first); // 10;
2374
+ *
2375
+ * // Get last element without removing
2376
+ * const last = deque.at(deque.length - 1);
2377
+ * console.log(last); // 50;
2378
+ *
2379
+ * // Length unchanged
2380
+ * console.log(deque.length); // 5;
2381
+ */
1815
2382
  get first() {
1816
2383
  if (this._length === 0) return;
1817
2384
  return this._buckets[this._bucketFirst][this._firstInBucket];
1818
2385
  }
1819
2386
  /**
1820
- * Get the last element without removing it.
1821
- * @remarks Time O(1), Space O(1)
1822
- * @returns Last element or undefined.
1823
- */
2387
+ * Get the last element without removing it.
2388
+ * @remarks Time O(1), Space O(1)
2389
+ * @returns Last element or undefined.
2390
+
2391
+
2392
+
2393
+
2394
+
2395
+
2396
+
2397
+
2398
+
2399
+
2400
+
2401
+ * @example
2402
+ * // Peek at the back element
2403
+ * const dq = new Deque<string>(['a', 'b', 'c']);
2404
+ * console.log(dq.last); // 'c';
2405
+ * console.log(dq.first); // 'a';
2406
+ */
1824
2407
  get last() {
1825
2408
  if (this._length === 0) return;
1826
2409
  return this._buckets[this._bucketLast][this._lastInBucket];
@@ -1839,11 +2422,40 @@ var _Deque = class _Deque extends LinearBase {
1839
2422
  return new this(data, options);
1840
2423
  }
1841
2424
  /**
1842
- * Append one element at the back.
1843
- * @remarks Time O(1) amortized, Space O(1)
1844
- * @param element - Element to append.
1845
- * @returns True when appended.
1846
- */
2425
+ * Append one element at the back.
2426
+ * @remarks Time O(1) amortized, Space O(1)
2427
+ * @param element - Element to append.
2428
+ * @returns True when appended.
2429
+
2430
+
2431
+
2432
+
2433
+
2434
+
2435
+
2436
+
2437
+
2438
+
2439
+
2440
+ * @example
2441
+ * // basic Deque creation and push/pop operations
2442
+ * // Create a simple Deque with initial values
2443
+ * const deque = new Deque([1, 2, 3, 4, 5]);
2444
+ *
2445
+ * // Verify the deque maintains insertion order
2446
+ * console.log([...deque]); // [1, 2, 3, 4, 5];
2447
+ *
2448
+ * // Check length
2449
+ * console.log(deque.length); // 5;
2450
+ *
2451
+ * // Push to the end
2452
+ * deque.push(6);
2453
+ * console.log(deque.length); // 6;
2454
+ *
2455
+ * // Pop from the end
2456
+ * const last = deque.pop();
2457
+ * console.log(last); // 6;
2458
+ */
1847
2459
  push(element) {
1848
2460
  if (this._length) {
1849
2461
  if (this._lastInBucket < this._bucketSize - 1) {
@@ -1863,10 +2475,26 @@ var _Deque = class _Deque extends LinearBase {
1863
2475
  return true;
1864
2476
  }
1865
2477
  /**
1866
- * Remove and return the last element.
1867
- * @remarks Time O(1), Space O(1)
1868
- * @returns Removed element or undefined.
1869
- */
2478
+ * Remove and return the last element.
2479
+ * @remarks Time O(1), Space O(1)
2480
+ * @returns Removed element or undefined.
2481
+
2482
+
2483
+
2484
+
2485
+
2486
+
2487
+
2488
+
2489
+
2490
+
2491
+
2492
+ * @example
2493
+ * // Remove from the back
2494
+ * const dq = new Deque<number>([1, 2, 3]);
2495
+ * console.log(dq.pop()); // 3;
2496
+ * console.log(dq.length); // 2;
2497
+ */
1870
2498
  pop() {
1871
2499
  if (this._length === 0) return;
1872
2500
  const element = this._buckets[this._bucketLast][this._lastInBucket];
@@ -1882,13 +2510,30 @@ var _Deque = class _Deque extends LinearBase {
1882
2510
  }
1883
2511
  }
1884
2512
  this._length -= 1;
2513
+ this._autoCompact();
1885
2514
  return element;
1886
2515
  }
1887
2516
  /**
1888
- * Remove and return the first element.
1889
- * @remarks Time O(1) amortized, Space O(1)
1890
- * @returns Removed element or undefined.
1891
- */
2517
+ * Remove and return the first element.
2518
+ * @remarks Time O(1) amortized, Space O(1)
2519
+ * @returns Removed element or undefined.
2520
+
2521
+
2522
+
2523
+
2524
+
2525
+
2526
+
2527
+
2528
+
2529
+
2530
+
2531
+ * @example
2532
+ * // Remove from the front
2533
+ * const dq = new Deque<number>([1, 2, 3]);
2534
+ * console.log(dq.shift()); // 1;
2535
+ * console.log(dq.length); // 2;
2536
+ */
1892
2537
  shift() {
1893
2538
  if (this._length === 0) return;
1894
2539
  const element = this._buckets[this._bucketFirst][this._firstInBucket];
@@ -1904,14 +2549,41 @@ var _Deque = class _Deque extends LinearBase {
1904
2549
  }
1905
2550
  }
1906
2551
  this._length -= 1;
2552
+ this._autoCompact();
1907
2553
  return element;
1908
2554
  }
1909
2555
  /**
1910
- * Prepend one element at the front.
1911
- * @remarks Time O(1) amortized, Space O(1)
1912
- * @param element - Element to prepend.
1913
- * @returns True when prepended.
1914
- */
2556
+ * Prepend one element at the front.
2557
+ * @remarks Time O(1) amortized, Space O(1)
2558
+ * @param element - Element to prepend.
2559
+ * @returns True when prepended.
2560
+
2561
+
2562
+
2563
+
2564
+
2565
+
2566
+
2567
+
2568
+
2569
+
2570
+
2571
+ * @example
2572
+ * // Deque shift and unshift operations
2573
+ * const deque = new Deque<number>([20, 30, 40]);
2574
+ *
2575
+ * // Unshift adds to the front
2576
+ * deque.unshift(10);
2577
+ * console.log([...deque]); // [10, 20, 30, 40];
2578
+ *
2579
+ * // Shift removes from the front (O(1) complexity!)
2580
+ * const first = deque.shift();
2581
+ * console.log(first); // 10;
2582
+ *
2583
+ * // Verify remaining elements
2584
+ * console.log([...deque]); // [20, 30, 40];
2585
+ * console.log(deque.length); // 3;
2586
+ */
1915
2587
  unshift(element) {
1916
2588
  if (this._length) {
1917
2589
  if (this._firstInBucket > 0) {
@@ -1965,18 +2637,45 @@ var _Deque = class _Deque extends LinearBase {
1965
2637
  return ans;
1966
2638
  }
1967
2639
  /**
1968
- * Check whether the deque is empty.
1969
- * @remarks Time O(1), Space O(1)
1970
- * @returns True if length is 0.
1971
- */
2640
+ * Check whether the deque is empty.
2641
+ * @remarks Time O(1), Space O(1)
2642
+ * @returns True if length is 0.
2643
+
2644
+
2645
+
2646
+
2647
+
2648
+
2649
+
2650
+
2651
+
2652
+ * @example
2653
+ * // Check if empty
2654
+ * const dq = new Deque();
2655
+ * console.log(dq.isEmpty()); // true;
2656
+ */
1972
2657
  isEmpty() {
1973
2658
  return this._length === 0;
1974
2659
  }
1975
2660
  /**
1976
- * Remove all elements and reset structure.
1977
- * @remarks Time O(1), Space O(1)
1978
- * @returns void
1979
- */
2661
+ * Remove all elements and reset structure.
2662
+ * @remarks Time O(1), Space O(1)
2663
+ * @returns void
2664
+
2665
+
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2672
+
2673
+ * @example
2674
+ * // Remove all elements
2675
+ * const dq = new Deque<number>([1, 2, 3]);
2676
+ * dq.clear();
2677
+ * console.log(dq.length); // 0;
2678
+ */
1980
2679
  clear() {
1981
2680
  this._buckets = [new Array(this._bucketSize)];
1982
2681
  this._bucketCount = 1;
@@ -1984,11 +2683,24 @@ var _Deque = class _Deque extends LinearBase {
1984
2683
  this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
1985
2684
  }
1986
2685
  /**
1987
- * Get the element at a given position.
1988
- * @remarks Time O(1), Space O(1)
1989
- * @param pos - Zero-based position from the front.
1990
- * @returns Element or undefined.
1991
- */
2686
+ * Get the element at a given position.
2687
+ * @remarks Time O(1), Space O(1)
2688
+ * @param pos - Zero-based position from the front.
2689
+ * @returns Element or undefined.
2690
+
2691
+
2692
+
2693
+
2694
+
2695
+
2696
+
2697
+
2698
+ * @example
2699
+ * // Access by index
2700
+ * const dq = new Deque<string>(['a', 'b', 'c']);
2701
+ * console.log(dq.at(0)); // 'a';
2702
+ * console.log(dq.at(2)); // 'c';
2703
+ */
1992
2704
  at(pos) {
1993
2705
  if (pos < 0 || pos >= this._length) return void 0;
1994
2706
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
@@ -2147,11 +2859,24 @@ var _Deque = class _Deque extends LinearBase {
2147
2859
  }
2148
2860
  }
2149
2861
  /**
2150
- * Delete the first occurrence of a value.
2151
- * @remarks Time O(N), Space O(1)
2152
- * @param element - Element to remove (using the configured equality).
2153
- * @returns True if an element was removed.
2154
- */
2862
+ * Delete the first occurrence of a value.
2863
+ * @remarks Time O(N), Space O(1)
2864
+ * @param element - Element to remove (using the configured equality).
2865
+ * @returns True if an element was removed.
2866
+
2867
+
2868
+
2869
+
2870
+
2871
+
2872
+
2873
+
2874
+ * @example
2875
+ * // Remove element
2876
+ * const dq = new Deque<number>([1, 2, 3]);
2877
+ * dq.delete(2);
2878
+ * console.log(dq.length); // 2;
2879
+ */
2155
2880
  delete(element) {
2156
2881
  const size = this._length;
2157
2882
  if (size === 0) return false;
@@ -2195,10 +2920,39 @@ var _Deque = class _Deque extends LinearBase {
2195
2920
  return this;
2196
2921
  }
2197
2922
  /**
2198
- * Reverse the deque by reversing buckets and pointers.
2199
- * @remarks Time O(N), Space O(N)
2200
- * @returns This deque.
2201
- */
2923
+ * Reverse the deque by reversing buckets and pointers.
2924
+ * @remarks Time O(N), Space O(N)
2925
+ * @returns This deque.
2926
+
2927
+
2928
+
2929
+
2930
+
2931
+
2932
+
2933
+
2934
+
2935
+
2936
+
2937
+ * @example
2938
+ * // Deque for...of iteration and reverse
2939
+ * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
2940
+ *
2941
+ * // Iterate forward
2942
+ * const forward: string[] = [];
2943
+ * for (const item of deque) {
2944
+ * forward.push(item);
2945
+ * }
2946
+ * console.log(forward); // ['A', 'B', 'C', 'D'];
2947
+ *
2948
+ * // Reverse the deque
2949
+ * deque.reverse();
2950
+ * const backward: string[] = [];
2951
+ * for (const item of deque) {
2952
+ * backward.push(item);
2953
+ * }
2954
+ * console.log(backward); // ['D', 'C', 'B', 'A'];
2955
+ */
2202
2956
  reverse() {
2203
2957
  this._buckets.reverse().forEach(function(bucket) {
2204
2958
  bucket.reverse();
@@ -2236,11 +2990,55 @@ var _Deque = class _Deque extends LinearBase {
2236
2990
  * @remarks Time O(N), Space O(1)
2237
2991
  * @returns void
2238
2992
  */
2993
+ /**
2994
+ * (Protected) Trigger auto-compaction if space utilization drops below threshold.
2995
+ * Only checks every `_bucketSize` operations to minimize hot-path overhead.
2996
+ * Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
2997
+ */
2998
+ _autoCompact() {
2999
+ if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
3000
+ this._compactCounter++;
3001
+ if (this._compactCounter < this._bucketSize) return;
3002
+ this._compactCounter = 0;
3003
+ const utilization = this._length / (this._bucketCount * this._bucketSize);
3004
+ if (utilization < this._autoCompactRatio) {
3005
+ this.shrinkToFit();
3006
+ }
3007
+ }
3008
+ /**
3009
+ * Compact the deque by removing unused buckets.
3010
+ * @remarks Time O(N), Space O(1)
3011
+ * @returns True if compaction was performed (bucket count reduced).
3012
+ */
3013
+ /**
3014
+ * Compact the deque by removing unused buckets.
3015
+ * @remarks Time O(N), Space O(1)
3016
+ * @returns True if compaction was performed (bucket count reduced).
3017
+
3018
+
3019
+
3020
+
3021
+
3022
+
3023
+
3024
+
3025
+ * @example
3026
+ * // Reclaim memory
3027
+ * const dq = new Deque<number>([1, 2, 3, 4, 5]);
3028
+ * dq.shift();
3029
+ * dq.shift();
3030
+ * dq.compact();
3031
+ * console.log(dq.length); // 3;
3032
+ */
3033
+ compact() {
3034
+ const before = this._bucketCount;
3035
+ this.shrinkToFit();
3036
+ return this._bucketCount < before;
3037
+ }
2239
3038
  shrinkToFit() {
2240
3039
  if (this._length === 0) return;
2241
3040
  const newBuckets = [];
2242
- if (this._bucketFirst === this._bucketLast) return;
2243
- else if (this._bucketFirst < this._bucketLast) {
3041
+ if (this._bucketFirst <= this._bucketLast) {
2244
3042
  for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
2245
3043
  newBuckets.push(this._buckets[i]);
2246
3044
  }
@@ -2255,12 +3053,30 @@ var _Deque = class _Deque extends LinearBase {
2255
3053
  this._bucketFirst = 0;
2256
3054
  this._bucketLast = newBuckets.length - 1;
2257
3055
  this._buckets = newBuckets;
2258
- }
2259
- /**
2260
- * Deep clone this deque, preserving options.
2261
- * @remarks Time O(N), Space O(N)
2262
- * @returns A new deque with the same content and options.
2263
- */
3056
+ this._bucketCount = newBuckets.length;
3057
+ this._compactCounter = 0;
3058
+ }
3059
+ /**
3060
+ * Deep clone this deque, preserving options.
3061
+ * @remarks Time O(N), Space O(N)
3062
+ * @returns A new deque with the same content and options.
3063
+
3064
+
3065
+
3066
+
3067
+
3068
+
3069
+
3070
+
3071
+
3072
+ * @example
3073
+ * // Create independent copy
3074
+ * const dq = new Deque<number>([1, 2, 3]);
3075
+ * const copy = dq.clone();
3076
+ * copy.pop();
3077
+ * console.log(dq.length); // 3;
3078
+ * console.log(copy.length); // 2;
3079
+ */
2264
3080
  clone() {
2265
3081
  return this._createLike(this, {
2266
3082
  bucketSize: this.bucketSize,
@@ -2269,12 +3085,26 @@ var _Deque = class _Deque extends LinearBase {
2269
3085
  });
2270
3086
  }
2271
3087
  /**
2272
- * Filter elements into a new deque of the same class.
2273
- * @remarks Time O(N), Space O(N)
2274
- * @param predicate - Predicate (value, index, deque) → boolean to keep element.
2275
- * @param [thisArg] - Value for `this` inside the predicate.
2276
- * @returns A new deque with kept elements.
2277
- */
3088
+ * Filter elements into a new deque of the same class.
3089
+ * @remarks Time O(N), Space O(N)
3090
+ * @param predicate - Predicate (value, index, deque) → boolean to keep element.
3091
+ * @param [thisArg] - Value for `this` inside the predicate.
3092
+ * @returns A new deque with kept elements.
3093
+
3094
+
3095
+
3096
+
3097
+
3098
+
3099
+
3100
+
3101
+
3102
+ * @example
3103
+ * // Filter elements
3104
+ * const dq = new Deque<number>([1, 2, 3, 4]);
3105
+ * const result = dq.filter(x => x > 2);
3106
+ * console.log(result.length); // 2;
3107
+ */
2278
3108
  filter(predicate, thisArg) {
2279
3109
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
2280
3110
  out._setBucketSize(this._bucketSize);
@@ -2303,15 +3133,28 @@ var _Deque = class _Deque extends LinearBase {
2303
3133
  return out;
2304
3134
  }
2305
3135
  /**
2306
- * Map elements into a new deque (possibly different element type).
2307
- * @remarks Time O(N), Space O(N)
2308
- * @template EM
2309
- * @template RM
2310
- * @param callback - Mapping function (value, index, deque) → newElement.
2311
- * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
2312
- * @param [thisArg] - Value for `this` inside the callback.
2313
- * @returns A new Deque with mapped elements.
2314
- */
3136
+ * Map elements into a new deque (possibly different element type).
3137
+ * @remarks Time O(N), Space O(N)
3138
+ * @template EM
3139
+ * @template RM
3140
+ * @param callback - Mapping function (value, index, deque) → newElement.
3141
+ * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
3142
+ * @param [thisArg] - Value for `this` inside the callback.
3143
+ * @returns A new Deque with mapped elements.
3144
+
3145
+
3146
+
3147
+
3148
+
3149
+
3150
+
3151
+
3152
+ * @example
3153
+ * // Transform elements
3154
+ * const dq = new Deque<number>([1, 2, 3]);
3155
+ * const result = dq.map(x => x * 10);
3156
+ * console.log(result.toArray()); // [10, 20, 30];
3157
+ */
2315
3158
  map(callback, options, thisArg) {
2316
3159
  const out = this._createLike([], {
2317
3160
  ...options != null ? options : {},
@@ -2436,29 +3279,6 @@ var _Deque = class _Deque extends LinearBase {
2436
3279
  };
2437
3280
  __name(_Deque, "Deque");
2438
3281
  var Deque = _Deque;
2439
-
2440
- // src/common/index.ts
2441
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
2442
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
2443
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
2444
- return DFSOperation2;
2445
- })(DFSOperation || {});
2446
- var _Range = class _Range {
2447
- constructor(low, high, includeLow = true, includeHigh = true) {
2448
- this.low = low;
2449
- this.high = high;
2450
- this.includeLow = includeLow;
2451
- this.includeHigh = includeHigh;
2452
- }
2453
- // Determine whether a key is within the range
2454
- isInRange(key, comparator) {
2455
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
2456
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
2457
- return lowCheck && highCheck;
2458
- }
2459
- };
2460
- __name(_Range, "Range");
2461
- var Range = _Range;
2462
3282
  /**
2463
3283
  * data-structure-typed
2464
3284
  *
@@ -2467,6 +3287,6 @@ var Range = _Range;
2467
3287
  * @license MIT License
2468
3288
  */
2469
3289
 
2470
- export { DFSOperation, Deque, LinkedListQueue, Queue, Range };
3290
+ export { DFSOperation, Deque, ERR, LinkedListQueue, Queue, Range };
2471
3291
  //# sourceMappingURL=index.mjs.map
2472
3292
  //# sourceMappingURL=index.mjs.map