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