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