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