queue-typed 2.4.5 → 2.5.1
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 +1879 -255
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1878 -254
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1879 -255
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1878 -254
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -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 +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -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 +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/queue-typed.js +1876 -252
- 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/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- 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 +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
package/dist/umd/queue-typed.js
CHANGED
|
@@ -31,52 +31,6 @@ var queueTyped = (() => {
|
|
|
31
31
|
Range: () => Range
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
// src/common/error.ts
|
|
35
|
-
var ERR = {
|
|
36
|
-
// Range / index
|
|
37
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
38
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
39
|
-
// Type / argument
|
|
40
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
41
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
42
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
43
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
44
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
45
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
46
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
47
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
48
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
49
|
-
// State / operation
|
|
50
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
51
|
-
// Matrix
|
|
52
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
53
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
54
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
55
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
56
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// src/common/index.ts
|
|
60
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
61
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
62
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
63
|
-
return DFSOperation2;
|
|
64
|
-
})(DFSOperation || {});
|
|
65
|
-
var Range = class {
|
|
66
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
67
|
-
this.low = low;
|
|
68
|
-
this.high = high;
|
|
69
|
-
this.includeLow = includeLow;
|
|
70
|
-
this.includeHigh = includeHigh;
|
|
71
|
-
}
|
|
72
|
-
// Determine whether a key is within the range
|
|
73
|
-
isInRange(key, comparator) {
|
|
74
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
75
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
76
|
-
return lowCheck && highCheck;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
34
|
// src/data-structures/base/iterable-element-base.ts
|
|
81
35
|
var IterableElementBase = class {
|
|
82
36
|
/**
|
|
@@ -99,7 +53,7 @@ var queueTyped = (() => {
|
|
|
99
53
|
if (options) {
|
|
100
54
|
const { toElementFn } = options;
|
|
101
55
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
102
|
-
else if (toElementFn) throw new TypeError(
|
|
56
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
103
57
|
}
|
|
104
58
|
}
|
|
105
59
|
/**
|
|
@@ -255,7 +209,7 @@ var queueTyped = (() => {
|
|
|
255
209
|
acc = initialValue;
|
|
256
210
|
} else {
|
|
257
211
|
const first = iter.next();
|
|
258
|
-
if (first.done) throw new TypeError(
|
|
212
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
259
213
|
acc = first.value;
|
|
260
214
|
index = 1;
|
|
261
215
|
}
|
|
@@ -783,11 +737,58 @@ var queueTyped = (() => {
|
|
|
783
737
|
return list;
|
|
784
738
|
}
|
|
785
739
|
/**
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
740
|
+
* Append an element/node to the tail.
|
|
741
|
+
* @remarks Time O(1), Space O(1)
|
|
742
|
+
* @param elementOrNode - Element or node to append.
|
|
743
|
+
* @returns True when appended.
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
* @example
|
|
777
|
+
* // basic SinglyLinkedList creation and push operation
|
|
778
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
779
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
780
|
+
*
|
|
781
|
+
* // Verify the list maintains insertion order
|
|
782
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
783
|
+
*
|
|
784
|
+
* // Check length
|
|
785
|
+
* console.log(list.length); // 5;
|
|
786
|
+
*
|
|
787
|
+
* // Push a new element to the end
|
|
788
|
+
* list.push(6);
|
|
789
|
+
* console.log(list.length); // 6;
|
|
790
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
791
|
+
*/
|
|
791
792
|
push(elementOrNode) {
|
|
792
793
|
const newNode = this._ensureNode(elementOrNode);
|
|
793
794
|
if (!this.head) {
|
|
@@ -801,10 +802,57 @@ var queueTyped = (() => {
|
|
|
801
802
|
return true;
|
|
802
803
|
}
|
|
803
804
|
/**
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
805
|
+
* Remove and return the tail element.
|
|
806
|
+
* @remarks Time O(N), Space O(1)
|
|
807
|
+
* @returns Removed element or undefined.
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
* @example
|
|
841
|
+
* // SinglyLinkedList pop and shift operations
|
|
842
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
843
|
+
*
|
|
844
|
+
* // Pop removes from the end
|
|
845
|
+
* const last = list.pop();
|
|
846
|
+
* console.log(last); // 50;
|
|
847
|
+
*
|
|
848
|
+
* // Shift removes from the beginning
|
|
849
|
+
* const first = list.shift();
|
|
850
|
+
* console.log(first); // 10;
|
|
851
|
+
*
|
|
852
|
+
* // Verify remaining elements
|
|
853
|
+
* console.log([...list]); // [20, 30, 40];
|
|
854
|
+
* console.log(list.length); // 3;
|
|
855
|
+
*/
|
|
808
856
|
pop() {
|
|
809
857
|
var _a;
|
|
810
858
|
if (!this.head) return void 0;
|
|
@@ -824,10 +872,47 @@ var queueTyped = (() => {
|
|
|
824
872
|
return value;
|
|
825
873
|
}
|
|
826
874
|
/**
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
875
|
+
* Remove and return the head element.
|
|
876
|
+
* @remarks Time O(1), Space O(1)
|
|
877
|
+
* @returns Removed element or undefined.
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
* @example
|
|
911
|
+
* // Remove from the front
|
|
912
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
913
|
+
* console.log(list.shift()); // 10;
|
|
914
|
+
* console.log(list.length); // 2;
|
|
915
|
+
*/
|
|
831
916
|
shift() {
|
|
832
917
|
if (!this.head) return void 0;
|
|
833
918
|
const removed = this.head;
|
|
@@ -837,11 +922,63 @@ var queueTyped = (() => {
|
|
|
837
922
|
return removed.value;
|
|
838
923
|
}
|
|
839
924
|
/**
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
925
|
+
* Prepend an element/node to the head.
|
|
926
|
+
* @remarks Time O(1), Space O(1)
|
|
927
|
+
* @param elementOrNode - Element or node to prepend.
|
|
928
|
+
* @returns True when prepended.
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
* @example
|
|
962
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
963
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
964
|
+
*
|
|
965
|
+
* // Unshift adds to the beginning
|
|
966
|
+
* list.unshift(10);
|
|
967
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
968
|
+
*
|
|
969
|
+
* // Access elements (forward traversal only for singly linked)
|
|
970
|
+
* const second = list.at(1);
|
|
971
|
+
* console.log(second); // 20;
|
|
972
|
+
*
|
|
973
|
+
* // SinglyLinkedList allows forward iteration only
|
|
974
|
+
* const elements: number[] = [];
|
|
975
|
+
* for (const item of list) {
|
|
976
|
+
* elements.push(item);
|
|
977
|
+
* }
|
|
978
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
979
|
+
*
|
|
980
|
+
* console.log(list.length); // 4;
|
|
981
|
+
*/
|
|
845
982
|
unshift(elementOrNode) {
|
|
846
983
|
const newNode = this._ensureNode(elementOrNode);
|
|
847
984
|
if (!this.head) {
|
|
@@ -897,11 +1034,49 @@ var queueTyped = (() => {
|
|
|
897
1034
|
return void 0;
|
|
898
1035
|
}
|
|
899
1036
|
/**
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
1037
|
+
* Get the element at a given index.
|
|
1038
|
+
* @remarks Time O(N), Space O(1)
|
|
1039
|
+
* @param index - Zero-based index.
|
|
1040
|
+
* @returns Element or undefined.
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
* @example
|
|
1074
|
+
* // Access element by index
|
|
1075
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1076
|
+
* console.log(list.at(0)); // 'a';
|
|
1077
|
+
* console.log(list.at(2)); // 'c';
|
|
1078
|
+
* console.log(list.at(3)); // 'd';
|
|
1079
|
+
*/
|
|
905
1080
|
at(index) {
|
|
906
1081
|
if (index < 0 || index >= this._length) return void 0;
|
|
907
1082
|
let current = this.head;
|
|
@@ -918,11 +1093,44 @@ var queueTyped = (() => {
|
|
|
918
1093
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
919
1094
|
}
|
|
920
1095
|
/**
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
1096
|
+
* Get the node reference at a given index.
|
|
1097
|
+
* @remarks Time O(N), Space O(1)
|
|
1098
|
+
* @param index - Zero-based index.
|
|
1099
|
+
* @returns Node or undefined.
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
* @example
|
|
1130
|
+
* // Get node at index
|
|
1131
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1132
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1133
|
+
*/
|
|
926
1134
|
getNodeAt(index) {
|
|
927
1135
|
if (index < 0 || index >= this._length) return void 0;
|
|
928
1136
|
let current = this.head;
|
|
@@ -930,11 +1138,45 @@ var queueTyped = (() => {
|
|
|
930
1138
|
return current;
|
|
931
1139
|
}
|
|
932
1140
|
/**
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
1141
|
+
* Delete the element at an index.
|
|
1142
|
+
* @remarks Time O(N), Space O(1)
|
|
1143
|
+
* @param index - Zero-based index.
|
|
1144
|
+
* @returns Removed element or undefined.
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
* @example
|
|
1175
|
+
* // Remove by index
|
|
1176
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1177
|
+
* list.deleteAt(1);
|
|
1178
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1179
|
+
*/
|
|
938
1180
|
deleteAt(index) {
|
|
939
1181
|
if (index < 0 || index >= this._length) return void 0;
|
|
940
1182
|
if (index === 0) return this.shift();
|
|
@@ -947,11 +1189,45 @@ var queueTyped = (() => {
|
|
|
947
1189
|
return value;
|
|
948
1190
|
}
|
|
949
1191
|
/**
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
1192
|
+
* Delete the first match by value/node.
|
|
1193
|
+
* @remarks Time O(N), Space O(1)
|
|
1194
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1195
|
+
* @returns True if removed.
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
* @example
|
|
1226
|
+
* // Remove first occurrence
|
|
1227
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1228
|
+
* list.delete(2);
|
|
1229
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1230
|
+
*/
|
|
955
1231
|
delete(elementOrNode) {
|
|
956
1232
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
957
1233
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -968,12 +1244,46 @@ var queueTyped = (() => {
|
|
|
968
1244
|
return true;
|
|
969
1245
|
}
|
|
970
1246
|
/**
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
1247
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1248
|
+
* @remarks Time O(N), Space O(1)
|
|
1249
|
+
* @param index - Zero-based index.
|
|
1250
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1251
|
+
* @returns True if inserted.
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
|
|
1277
|
+
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
* @example
|
|
1282
|
+
* // Insert at index
|
|
1283
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1284
|
+
* list.addAt(1, 2);
|
|
1285
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1286
|
+
*/
|
|
977
1287
|
addAt(index, newElementOrNode) {
|
|
978
1288
|
if (index < 0 || index > this._length) return false;
|
|
979
1289
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -999,28 +1309,133 @@ var queueTyped = (() => {
|
|
|
999
1309
|
return true;
|
|
1000
1310
|
}
|
|
1001
1311
|
/**
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1312
|
+
* Check whether the list is empty.
|
|
1313
|
+
* @remarks Time O(1), Space O(1)
|
|
1314
|
+
* @returns True if length is 0.
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
* @example
|
|
1346
|
+
* // Check empty
|
|
1347
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1348
|
+
*/
|
|
1006
1349
|
isEmpty() {
|
|
1007
1350
|
return this._length === 0;
|
|
1008
1351
|
}
|
|
1009
1352
|
/**
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1353
|
+
* Remove all nodes and reset length.
|
|
1354
|
+
* @remarks Time O(N), Space O(1)
|
|
1355
|
+
* @returns void
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
|
|
1386
|
+
* @example
|
|
1387
|
+
* // Remove all
|
|
1388
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1389
|
+
* list.clear();
|
|
1390
|
+
* console.log(list.isEmpty()); // true;
|
|
1391
|
+
*/
|
|
1014
1392
|
clear() {
|
|
1015
1393
|
this._head = void 0;
|
|
1016
1394
|
this._tail = void 0;
|
|
1017
1395
|
this._length = 0;
|
|
1018
1396
|
}
|
|
1019
1397
|
/**
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1398
|
+
* Reverse the list in place.
|
|
1399
|
+
* @remarks Time O(N), Space O(1)
|
|
1400
|
+
* @returns This list.
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
|
|
1405
|
+
|
|
1406
|
+
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
* @example
|
|
1434
|
+
* // Reverse the list in-place
|
|
1435
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1436
|
+
* list.reverse();
|
|
1437
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1438
|
+
*/
|
|
1024
1439
|
reverse() {
|
|
1025
1440
|
if (!this.head || this.head === this.tail) return this;
|
|
1026
1441
|
let prev;
|
|
@@ -1195,22 +1610,106 @@ var queueTyped = (() => {
|
|
|
1195
1610
|
return false;
|
|
1196
1611
|
}
|
|
1197
1612
|
/**
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1613
|
+
* Deep clone this list (values are copied by reference).
|
|
1614
|
+
* @remarks Time O(N), Space O(N)
|
|
1615
|
+
* @returns A new list with the same element sequence.
|
|
1616
|
+
|
|
1617
|
+
|
|
1618
|
+
|
|
1619
|
+
|
|
1620
|
+
|
|
1621
|
+
|
|
1622
|
+
|
|
1623
|
+
|
|
1624
|
+
|
|
1625
|
+
|
|
1626
|
+
|
|
1627
|
+
|
|
1628
|
+
|
|
1629
|
+
|
|
1630
|
+
|
|
1631
|
+
|
|
1632
|
+
|
|
1633
|
+
|
|
1634
|
+
|
|
1635
|
+
|
|
1636
|
+
|
|
1637
|
+
|
|
1638
|
+
|
|
1639
|
+
|
|
1640
|
+
|
|
1641
|
+
|
|
1642
|
+
|
|
1643
|
+
|
|
1644
|
+
|
|
1645
|
+
|
|
1646
|
+
* @example
|
|
1647
|
+
* // Deep copy
|
|
1648
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1649
|
+
* const copy = list.clone();
|
|
1650
|
+
* copy.pop();
|
|
1651
|
+
* console.log(list.length); // 3;
|
|
1652
|
+
* console.log(copy.length); // 2;
|
|
1653
|
+
*/
|
|
1202
1654
|
clone() {
|
|
1203
1655
|
const out = this._createInstance();
|
|
1204
1656
|
for (const v of this) out.push(v);
|
|
1205
1657
|
return out;
|
|
1206
1658
|
}
|
|
1207
1659
|
/**
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1660
|
+
* Filter values into a new list of the same class.
|
|
1661
|
+
* @remarks Time O(N), Space O(N)
|
|
1662
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1663
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1664
|
+
* @returns A new list with kept values.
|
|
1665
|
+
|
|
1666
|
+
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
|
|
1670
|
+
|
|
1671
|
+
|
|
1672
|
+
|
|
1673
|
+
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
|
|
1677
|
+
|
|
1678
|
+
|
|
1679
|
+
|
|
1680
|
+
|
|
1681
|
+
|
|
1682
|
+
|
|
1683
|
+
|
|
1684
|
+
|
|
1685
|
+
|
|
1686
|
+
|
|
1687
|
+
|
|
1688
|
+
|
|
1689
|
+
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
|
|
1694
|
+
|
|
1695
|
+
|
|
1696
|
+
|
|
1697
|
+
* @example
|
|
1698
|
+
* // SinglyLinkedList filter and map operations
|
|
1699
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1700
|
+
*
|
|
1701
|
+
* // Filter even numbers
|
|
1702
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1703
|
+
* console.log(filtered.length); // 2;
|
|
1704
|
+
*
|
|
1705
|
+
* // Map to double values
|
|
1706
|
+
* const doubled = list.map(value => value * 2);
|
|
1707
|
+
* console.log(doubled.length); // 5;
|
|
1708
|
+
*
|
|
1709
|
+
* // Use reduce to sum
|
|
1710
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1711
|
+
* console.log(sum); // 15;
|
|
1712
|
+
*/
|
|
1214
1713
|
filter(callback, thisArg) {
|
|
1215
1714
|
const out = this._createInstance();
|
|
1216
1715
|
let index = 0;
|
|
@@ -1234,15 +1733,52 @@ var queueTyped = (() => {
|
|
|
1234
1733
|
return out;
|
|
1235
1734
|
}
|
|
1236
1735
|
/**
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1736
|
+
* Map values into a new list (possibly different element type).
|
|
1737
|
+
* @remarks Time O(N), Space O(N)
|
|
1738
|
+
* @template EM
|
|
1739
|
+
* @template RM
|
|
1740
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1741
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1742
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1743
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1744
|
+
|
|
1745
|
+
|
|
1746
|
+
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
|
|
1750
|
+
|
|
1751
|
+
|
|
1752
|
+
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
|
|
1756
|
+
|
|
1757
|
+
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
|
|
1762
|
+
|
|
1763
|
+
|
|
1764
|
+
|
|
1765
|
+
|
|
1766
|
+
|
|
1767
|
+
|
|
1768
|
+
|
|
1769
|
+
|
|
1770
|
+
|
|
1771
|
+
|
|
1772
|
+
|
|
1773
|
+
|
|
1774
|
+
|
|
1775
|
+
|
|
1776
|
+
* @example
|
|
1777
|
+
* // Transform elements
|
|
1778
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1779
|
+
* const doubled = list.map(n => n * 2);
|
|
1780
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1781
|
+
*/
|
|
1246
1782
|
map(callback, options, thisArg) {
|
|
1247
1783
|
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1248
1784
|
let index = 0;
|
|
@@ -1376,6 +1912,52 @@ var queueTyped = (() => {
|
|
|
1376
1912
|
return (node) => equals(node.value, value);
|
|
1377
1913
|
}
|
|
1378
1914
|
|
|
1915
|
+
// src/common/error.ts
|
|
1916
|
+
var ERR = {
|
|
1917
|
+
// Range / index
|
|
1918
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
1919
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
1920
|
+
// Type / argument
|
|
1921
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
1922
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
1923
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
1924
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
1925
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
1926
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
1927
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
1928
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
1929
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
1930
|
+
// State / operation
|
|
1931
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
1932
|
+
// Matrix
|
|
1933
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
1934
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
1935
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
1936
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
1937
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
1938
|
+
};
|
|
1939
|
+
|
|
1940
|
+
// src/common/index.ts
|
|
1941
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1942
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1943
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1944
|
+
return DFSOperation2;
|
|
1945
|
+
})(DFSOperation || {});
|
|
1946
|
+
var Range = class {
|
|
1947
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1948
|
+
this.low = low;
|
|
1949
|
+
this.high = high;
|
|
1950
|
+
this.includeLow = includeLow;
|
|
1951
|
+
this.includeHigh = includeHigh;
|
|
1952
|
+
}
|
|
1953
|
+
// Determine whether a key is within the range
|
|
1954
|
+
isInRange(key, comparator) {
|
|
1955
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1956
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1957
|
+
return lowCheck && highCheck;
|
|
1958
|
+
}
|
|
1959
|
+
};
|
|
1960
|
+
|
|
1379
1961
|
// src/data-structures/queue/queue.ts
|
|
1380
1962
|
var Queue = class _Queue extends LinearBase {
|
|
1381
1963
|
/**
|
|
@@ -1430,18 +2012,94 @@ var queueTyped = (() => {
|
|
|
1430
2012
|
this._autoCompactRatio = value;
|
|
1431
2013
|
}
|
|
1432
2014
|
/**
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
2015
|
+
* Get the number of elements currently in the queue.
|
|
2016
|
+
* @remarks Time O(1), Space O(1)
|
|
2017
|
+
* @returns Current length.
|
|
2018
|
+
|
|
2019
|
+
|
|
2020
|
+
|
|
2021
|
+
|
|
2022
|
+
|
|
2023
|
+
|
|
2024
|
+
|
|
2025
|
+
|
|
2026
|
+
|
|
2027
|
+
|
|
2028
|
+
|
|
2029
|
+
|
|
2030
|
+
|
|
2031
|
+
|
|
2032
|
+
|
|
2033
|
+
|
|
2034
|
+
|
|
2035
|
+
|
|
2036
|
+
|
|
2037
|
+
|
|
2038
|
+
|
|
2039
|
+
|
|
2040
|
+
|
|
2041
|
+
|
|
2042
|
+
|
|
2043
|
+
|
|
2044
|
+
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
* @example
|
|
2051
|
+
* // Track queue length
|
|
2052
|
+
* const q = new Queue<number>();
|
|
2053
|
+
* console.log(q.length); // 0;
|
|
2054
|
+
* q.push(1);
|
|
2055
|
+
* q.push(2);
|
|
2056
|
+
* console.log(q.length); // 2;
|
|
2057
|
+
*/
|
|
1437
2058
|
get length() {
|
|
1438
2059
|
return this.elements.length - this._offset;
|
|
1439
2060
|
}
|
|
1440
2061
|
/**
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
2062
|
+
* Get the first element (front) without removing it.
|
|
2063
|
+
* @remarks Time O(1), Space O(1)
|
|
2064
|
+
* @returns Front element or undefined.
|
|
2065
|
+
|
|
2066
|
+
|
|
2067
|
+
|
|
2068
|
+
|
|
2069
|
+
|
|
2070
|
+
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
|
|
2074
|
+
|
|
2075
|
+
|
|
2076
|
+
|
|
2077
|
+
|
|
2078
|
+
|
|
2079
|
+
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
|
|
2083
|
+
|
|
2084
|
+
|
|
2085
|
+
|
|
2086
|
+
|
|
2087
|
+
|
|
2088
|
+
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
|
|
2096
|
+
|
|
2097
|
+
* @example
|
|
2098
|
+
* // View the front element
|
|
2099
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2100
|
+
* console.log(q.first); // 'first';
|
|
2101
|
+
* console.log(q.length); // 3;
|
|
2102
|
+
*/
|
|
1445
2103
|
get first() {
|
|
1446
2104
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1447
2105
|
}
|
|
@@ -1464,19 +2122,111 @@ var queueTyped = (() => {
|
|
|
1464
2122
|
return new _Queue(elements);
|
|
1465
2123
|
}
|
|
1466
2124
|
/**
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
2125
|
+
* Check whether the queue is empty.
|
|
2126
|
+
* @remarks Time O(1), Space O(1)
|
|
2127
|
+
* @returns True if length is 0.
|
|
2128
|
+
|
|
2129
|
+
|
|
2130
|
+
|
|
2131
|
+
|
|
2132
|
+
|
|
2133
|
+
|
|
2134
|
+
|
|
2135
|
+
|
|
2136
|
+
|
|
2137
|
+
|
|
2138
|
+
|
|
2139
|
+
|
|
2140
|
+
|
|
2141
|
+
|
|
2142
|
+
|
|
2143
|
+
|
|
2144
|
+
|
|
2145
|
+
|
|
2146
|
+
|
|
2147
|
+
|
|
2148
|
+
|
|
2149
|
+
|
|
2150
|
+
|
|
2151
|
+
|
|
2152
|
+
|
|
2153
|
+
|
|
2154
|
+
|
|
2155
|
+
|
|
2156
|
+
|
|
2157
|
+
|
|
2158
|
+
|
|
2159
|
+
|
|
2160
|
+
* @example
|
|
2161
|
+
* // Queue for...of iteration and isEmpty check
|
|
2162
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2163
|
+
*
|
|
2164
|
+
* const elements: string[] = [];
|
|
2165
|
+
* for (const item of queue) {
|
|
2166
|
+
* elements.push(item);
|
|
2167
|
+
* }
|
|
2168
|
+
*
|
|
2169
|
+
* // Verify all elements are iterated in order
|
|
2170
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2171
|
+
*
|
|
2172
|
+
* // Process all elements
|
|
2173
|
+
* while (queue.length > 0) {
|
|
2174
|
+
* queue.shift();
|
|
2175
|
+
* }
|
|
2176
|
+
*
|
|
2177
|
+
* console.log(queue.length); // 0;
|
|
2178
|
+
*/
|
|
1471
2179
|
isEmpty() {
|
|
1472
2180
|
return this.length === 0;
|
|
1473
2181
|
}
|
|
1474
2182
|
/**
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
2183
|
+
* Enqueue one element at the back.
|
|
2184
|
+
* @remarks Time O(1), Space O(1)
|
|
2185
|
+
* @param element - Element to enqueue.
|
|
2186
|
+
* @returns True on success.
|
|
2187
|
+
|
|
2188
|
+
|
|
2189
|
+
|
|
2190
|
+
|
|
2191
|
+
|
|
2192
|
+
|
|
2193
|
+
|
|
2194
|
+
|
|
2195
|
+
|
|
2196
|
+
|
|
2197
|
+
|
|
2198
|
+
|
|
2199
|
+
|
|
2200
|
+
|
|
2201
|
+
|
|
2202
|
+
|
|
2203
|
+
|
|
2204
|
+
|
|
2205
|
+
|
|
2206
|
+
|
|
2207
|
+
|
|
2208
|
+
|
|
2209
|
+
|
|
2210
|
+
|
|
2211
|
+
|
|
2212
|
+
|
|
2213
|
+
|
|
2214
|
+
|
|
2215
|
+
|
|
2216
|
+
|
|
2217
|
+
|
|
2218
|
+
|
|
2219
|
+
* @example
|
|
2220
|
+
* // basic Queue creation and push operation
|
|
2221
|
+
* // Create a simple Queue with initial values
|
|
2222
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2223
|
+
*
|
|
2224
|
+
* // Verify the queue maintains insertion order
|
|
2225
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2226
|
+
*
|
|
2227
|
+
* // Check length
|
|
2228
|
+
* console.log(queue.length); // 5;
|
|
2229
|
+
*/
|
|
1480
2230
|
push(element) {
|
|
1481
2231
|
this.elements.push(element);
|
|
1482
2232
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1497,10 +2247,56 @@ var queueTyped = (() => {
|
|
|
1497
2247
|
return ans;
|
|
1498
2248
|
}
|
|
1499
2249
|
/**
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
2250
|
+
* Dequeue one element from the front (amortized via offset).
|
|
2251
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2252
|
+
* @returns Removed element or undefined.
|
|
2253
|
+
|
|
2254
|
+
|
|
2255
|
+
|
|
2256
|
+
|
|
2257
|
+
|
|
2258
|
+
|
|
2259
|
+
|
|
2260
|
+
|
|
2261
|
+
|
|
2262
|
+
|
|
2263
|
+
|
|
2264
|
+
|
|
2265
|
+
|
|
2266
|
+
|
|
2267
|
+
|
|
2268
|
+
|
|
2269
|
+
|
|
2270
|
+
|
|
2271
|
+
|
|
2272
|
+
|
|
2273
|
+
|
|
2274
|
+
|
|
2275
|
+
|
|
2276
|
+
|
|
2277
|
+
|
|
2278
|
+
|
|
2279
|
+
|
|
2280
|
+
|
|
2281
|
+
|
|
2282
|
+
|
|
2283
|
+
|
|
2284
|
+
|
|
2285
|
+
* @example
|
|
2286
|
+
* // Queue shift and peek operations
|
|
2287
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2288
|
+
*
|
|
2289
|
+
* // Peek at the front element without removing it
|
|
2290
|
+
* console.log(queue.first); // 10;
|
|
2291
|
+
*
|
|
2292
|
+
* // Remove and get the first element (FIFO)
|
|
2293
|
+
* const first = queue.shift();
|
|
2294
|
+
* console.log(first); // 10;
|
|
2295
|
+
*
|
|
2296
|
+
* // Verify remaining elements and length decreased
|
|
2297
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
2298
|
+
* console.log(queue.length); // 3;
|
|
2299
|
+
*/
|
|
1504
2300
|
shift() {
|
|
1505
2301
|
if (this.length === 0) return void 0;
|
|
1506
2302
|
const first = this.first;
|
|
@@ -1509,11 +2305,45 @@ var queueTyped = (() => {
|
|
|
1509
2305
|
return first;
|
|
1510
2306
|
}
|
|
1511
2307
|
/**
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
2308
|
+
* Delete the first occurrence of a specific element.
|
|
2309
|
+
* @remarks Time O(N), Space O(1)
|
|
2310
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
2311
|
+
* @returns True if an element was removed.
|
|
2312
|
+
|
|
2313
|
+
|
|
2314
|
+
|
|
2315
|
+
|
|
2316
|
+
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
|
|
2320
|
+
|
|
2321
|
+
|
|
2322
|
+
|
|
2323
|
+
|
|
2324
|
+
|
|
2325
|
+
|
|
2326
|
+
|
|
2327
|
+
|
|
2328
|
+
|
|
2329
|
+
|
|
2330
|
+
|
|
2331
|
+
|
|
2332
|
+
|
|
2333
|
+
|
|
2334
|
+
|
|
2335
|
+
|
|
2336
|
+
|
|
2337
|
+
|
|
2338
|
+
|
|
2339
|
+
|
|
2340
|
+
|
|
2341
|
+
* @example
|
|
2342
|
+
* // Remove specific element
|
|
2343
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2344
|
+
* q.delete(2);
|
|
2345
|
+
* console.log(q.length); // 3;
|
|
2346
|
+
*/
|
|
1517
2347
|
delete(element) {
|
|
1518
2348
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1519
2349
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1524,11 +2354,45 @@ var queueTyped = (() => {
|
|
|
1524
2354
|
return false;
|
|
1525
2355
|
}
|
|
1526
2356
|
/**
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
2357
|
+
* Get the element at a given logical index.
|
|
2358
|
+
* @remarks Time O(1), Space O(1)
|
|
2359
|
+
* @param index - Zero-based index from the front.
|
|
2360
|
+
* @returns Element or undefined.
|
|
2361
|
+
|
|
2362
|
+
|
|
2363
|
+
|
|
2364
|
+
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
|
|
2368
|
+
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
|
|
2372
|
+
|
|
2373
|
+
|
|
2374
|
+
|
|
2375
|
+
|
|
2376
|
+
|
|
2377
|
+
|
|
2378
|
+
|
|
2379
|
+
|
|
2380
|
+
|
|
2381
|
+
|
|
2382
|
+
|
|
2383
|
+
|
|
2384
|
+
|
|
2385
|
+
|
|
2386
|
+
|
|
2387
|
+
|
|
2388
|
+
|
|
2389
|
+
|
|
2390
|
+
* @example
|
|
2391
|
+
* // Access element by index
|
|
2392
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2393
|
+
* console.log(q.at(0)); // 'a';
|
|
2394
|
+
* console.log(q.at(2)); // 'c';
|
|
2395
|
+
*/
|
|
1532
2396
|
at(index) {
|
|
1533
2397
|
if (index < 0 || index >= this.length) return void 0;
|
|
1534
2398
|
return this._elements[this._offset + index];
|
|
@@ -1580,19 +2444,90 @@ var queueTyped = (() => {
|
|
|
1580
2444
|
return this;
|
|
1581
2445
|
}
|
|
1582
2446
|
/**
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
2447
|
+
* Remove all elements and reset offset.
|
|
2448
|
+
* @remarks Time O(1), Space O(1)
|
|
2449
|
+
* @returns void
|
|
2450
|
+
|
|
2451
|
+
|
|
2452
|
+
|
|
2453
|
+
|
|
2454
|
+
|
|
2455
|
+
|
|
2456
|
+
|
|
2457
|
+
|
|
2458
|
+
|
|
2459
|
+
|
|
2460
|
+
|
|
2461
|
+
|
|
2462
|
+
|
|
2463
|
+
|
|
2464
|
+
|
|
2465
|
+
|
|
2466
|
+
|
|
2467
|
+
|
|
2468
|
+
|
|
2469
|
+
|
|
2470
|
+
|
|
2471
|
+
|
|
2472
|
+
|
|
2473
|
+
|
|
2474
|
+
|
|
2475
|
+
|
|
2476
|
+
|
|
2477
|
+
|
|
2478
|
+
|
|
2479
|
+
|
|
2480
|
+
* @example
|
|
2481
|
+
* // Remove all elements
|
|
2482
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2483
|
+
* q.clear();
|
|
2484
|
+
* console.log(q.length); // 0;
|
|
2485
|
+
*/
|
|
1587
2486
|
clear() {
|
|
1588
2487
|
this._elements = [];
|
|
1589
2488
|
this._offset = 0;
|
|
1590
2489
|
}
|
|
1591
2490
|
/**
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
2491
|
+
* Compact storage by discarding consumed head elements.
|
|
2492
|
+
* @remarks Time O(N), Space O(N)
|
|
2493
|
+
* @returns True when compaction performed.
|
|
2494
|
+
|
|
2495
|
+
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2501
|
+
|
|
2502
|
+
|
|
2503
|
+
|
|
2504
|
+
|
|
2505
|
+
|
|
2506
|
+
|
|
2507
|
+
|
|
2508
|
+
|
|
2509
|
+
|
|
2510
|
+
|
|
2511
|
+
|
|
2512
|
+
|
|
2513
|
+
|
|
2514
|
+
|
|
2515
|
+
|
|
2516
|
+
|
|
2517
|
+
|
|
2518
|
+
|
|
2519
|
+
|
|
2520
|
+
|
|
2521
|
+
|
|
2522
|
+
|
|
2523
|
+
* @example
|
|
2524
|
+
* // Reclaim unused memory
|
|
2525
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2526
|
+
* q.shift();
|
|
2527
|
+
* q.shift();
|
|
2528
|
+
* q.compact();
|
|
2529
|
+
* console.log(q.length); // 3;
|
|
2530
|
+
*/
|
|
1596
2531
|
compact() {
|
|
1597
2532
|
this._elements = this.elements.slice(this._offset);
|
|
1598
2533
|
this._offset = 0;
|
|
@@ -1618,10 +2553,47 @@ var queueTyped = (() => {
|
|
|
1618
2553
|
return removed;
|
|
1619
2554
|
}
|
|
1620
2555
|
/**
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
2556
|
+
* Deep clone this queue and its parameters.
|
|
2557
|
+
* @remarks Time O(N), Space O(N)
|
|
2558
|
+
* @returns A new queue with the same content and options.
|
|
2559
|
+
|
|
2560
|
+
|
|
2561
|
+
|
|
2562
|
+
|
|
2563
|
+
|
|
2564
|
+
|
|
2565
|
+
|
|
2566
|
+
|
|
2567
|
+
|
|
2568
|
+
|
|
2569
|
+
|
|
2570
|
+
|
|
2571
|
+
|
|
2572
|
+
|
|
2573
|
+
|
|
2574
|
+
|
|
2575
|
+
|
|
2576
|
+
|
|
2577
|
+
|
|
2578
|
+
|
|
2579
|
+
|
|
2580
|
+
|
|
2581
|
+
|
|
2582
|
+
|
|
2583
|
+
|
|
2584
|
+
|
|
2585
|
+
|
|
2586
|
+
|
|
2587
|
+
|
|
2588
|
+
|
|
2589
|
+
* @example
|
|
2590
|
+
* // Create independent copy
|
|
2591
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2592
|
+
* const copy = q.clone();
|
|
2593
|
+
* copy.shift();
|
|
2594
|
+
* console.log(q.length); // 3;
|
|
2595
|
+
* console.log(copy.length); // 2;
|
|
2596
|
+
*/
|
|
1625
2597
|
clone() {
|
|
1626
2598
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1627
2599
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1629,12 +2601,47 @@ var queueTyped = (() => {
|
|
|
1629
2601
|
return out;
|
|
1630
2602
|
}
|
|
1631
2603
|
/**
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
2604
|
+
* Filter elements into a new queue of the same class.
|
|
2605
|
+
* @remarks Time O(N), Space O(N)
|
|
2606
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2607
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2608
|
+
* @returns A new queue with kept elements.
|
|
2609
|
+
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2613
|
+
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
|
|
2617
|
+
|
|
2618
|
+
|
|
2619
|
+
|
|
2620
|
+
|
|
2621
|
+
|
|
2622
|
+
|
|
2623
|
+
|
|
2624
|
+
|
|
2625
|
+
|
|
2626
|
+
|
|
2627
|
+
|
|
2628
|
+
|
|
2629
|
+
|
|
2630
|
+
|
|
2631
|
+
|
|
2632
|
+
|
|
2633
|
+
|
|
2634
|
+
|
|
2635
|
+
|
|
2636
|
+
|
|
2637
|
+
|
|
2638
|
+
|
|
2639
|
+
* @example
|
|
2640
|
+
* // Filter elements
|
|
2641
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2642
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2643
|
+
* console.log(evens.length); // 2;
|
|
2644
|
+
*/
|
|
1638
2645
|
filter(predicate, thisArg) {
|
|
1639
2646
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1640
2647
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1646,15 +2653,49 @@ var queueTyped = (() => {
|
|
|
1646
2653
|
return out;
|
|
1647
2654
|
}
|
|
1648
2655
|
/**
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
2656
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2657
|
+
* @remarks Time O(N), Space O(N)
|
|
2658
|
+
* @template EM
|
|
2659
|
+
* @template RM
|
|
2660
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2661
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2662
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2663
|
+
* @returns A new Queue with mapped elements.
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
|
|
2681
|
+
|
|
2682
|
+
|
|
2683
|
+
|
|
2684
|
+
|
|
2685
|
+
|
|
2686
|
+
|
|
2687
|
+
|
|
2688
|
+
|
|
2689
|
+
|
|
2690
|
+
|
|
2691
|
+
|
|
2692
|
+
|
|
2693
|
+
* @example
|
|
2694
|
+
* // Transform elements
|
|
2695
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2696
|
+
* const doubled = q.map(x => x * 2);
|
|
2697
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2698
|
+
*/
|
|
1658
2699
|
map(callback, options, thisArg) {
|
|
1659
2700
|
var _a, _b;
|
|
1660
2701
|
const out = new this.constructor([], {
|
|
@@ -1885,19 +2926,102 @@ var queueTyped = (() => {
|
|
|
1885
2926
|
return this._length;
|
|
1886
2927
|
}
|
|
1887
2928
|
/**
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
2929
|
+
* Get the first element without removing it.
|
|
2930
|
+
* @remarks Time O(1), Space O(1)
|
|
2931
|
+
* @returns First element or undefined.
|
|
2932
|
+
|
|
2933
|
+
|
|
2934
|
+
|
|
2935
|
+
|
|
2936
|
+
|
|
2937
|
+
|
|
2938
|
+
|
|
2939
|
+
|
|
2940
|
+
|
|
2941
|
+
|
|
2942
|
+
|
|
2943
|
+
|
|
2944
|
+
|
|
2945
|
+
|
|
2946
|
+
|
|
2947
|
+
|
|
2948
|
+
|
|
2949
|
+
|
|
2950
|
+
|
|
2951
|
+
|
|
2952
|
+
|
|
2953
|
+
|
|
2954
|
+
|
|
2955
|
+
|
|
2956
|
+
|
|
2957
|
+
|
|
2958
|
+
|
|
2959
|
+
|
|
2960
|
+
|
|
2961
|
+
|
|
2962
|
+
|
|
2963
|
+
|
|
2964
|
+
* @example
|
|
2965
|
+
* // Deque peek at both ends
|
|
2966
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2967
|
+
*
|
|
2968
|
+
* // Get first element without removing
|
|
2969
|
+
* const first = deque.at(0);
|
|
2970
|
+
* console.log(first); // 10;
|
|
2971
|
+
*
|
|
2972
|
+
* // Get last element without removing
|
|
2973
|
+
* const last = deque.at(deque.length - 1);
|
|
2974
|
+
* console.log(last); // 50;
|
|
2975
|
+
*
|
|
2976
|
+
* // Length unchanged
|
|
2977
|
+
* console.log(deque.length); // 5;
|
|
2978
|
+
*/
|
|
1892
2979
|
get first() {
|
|
1893
2980
|
if (this._length === 0) return;
|
|
1894
2981
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
1895
2982
|
}
|
|
1896
2983
|
/**
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
2984
|
+
* Get the last element without removing it.
|
|
2985
|
+
* @remarks Time O(1), Space O(1)
|
|
2986
|
+
* @returns Last element or undefined.
|
|
2987
|
+
|
|
2988
|
+
|
|
2989
|
+
|
|
2990
|
+
|
|
2991
|
+
|
|
2992
|
+
|
|
2993
|
+
|
|
2994
|
+
|
|
2995
|
+
|
|
2996
|
+
|
|
2997
|
+
|
|
2998
|
+
|
|
2999
|
+
|
|
3000
|
+
|
|
3001
|
+
|
|
3002
|
+
|
|
3003
|
+
|
|
3004
|
+
|
|
3005
|
+
|
|
3006
|
+
|
|
3007
|
+
|
|
3008
|
+
|
|
3009
|
+
|
|
3010
|
+
|
|
3011
|
+
|
|
3012
|
+
|
|
3013
|
+
|
|
3014
|
+
|
|
3015
|
+
|
|
3016
|
+
|
|
3017
|
+
|
|
3018
|
+
|
|
3019
|
+
* @example
|
|
3020
|
+
* // Peek at the back element
|
|
3021
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3022
|
+
* console.log(dq.last); // 'c';
|
|
3023
|
+
* console.log(dq.first); // 'a';
|
|
3024
|
+
*/
|
|
1901
3025
|
get last() {
|
|
1902
3026
|
if (this._length === 0) return;
|
|
1903
3027
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1916,11 +3040,61 @@ var queueTyped = (() => {
|
|
|
1916
3040
|
return new this(data, options);
|
|
1917
3041
|
}
|
|
1918
3042
|
/**
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
3043
|
+
* Append one element at the back.
|
|
3044
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3045
|
+
* @param element - Element to append.
|
|
3046
|
+
* @returns True when appended.
|
|
3047
|
+
|
|
3048
|
+
|
|
3049
|
+
|
|
3050
|
+
|
|
3051
|
+
|
|
3052
|
+
|
|
3053
|
+
|
|
3054
|
+
|
|
3055
|
+
|
|
3056
|
+
|
|
3057
|
+
|
|
3058
|
+
|
|
3059
|
+
|
|
3060
|
+
|
|
3061
|
+
|
|
3062
|
+
|
|
3063
|
+
|
|
3064
|
+
|
|
3065
|
+
|
|
3066
|
+
|
|
3067
|
+
|
|
3068
|
+
|
|
3069
|
+
|
|
3070
|
+
|
|
3071
|
+
|
|
3072
|
+
|
|
3073
|
+
|
|
3074
|
+
|
|
3075
|
+
|
|
3076
|
+
|
|
3077
|
+
|
|
3078
|
+
|
|
3079
|
+
* @example
|
|
3080
|
+
* // basic Deque creation and push/pop operations
|
|
3081
|
+
* // Create a simple Deque with initial values
|
|
3082
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
3083
|
+
*
|
|
3084
|
+
* // Verify the deque maintains insertion order
|
|
3085
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3086
|
+
*
|
|
3087
|
+
* // Check length
|
|
3088
|
+
* console.log(deque.length); // 5;
|
|
3089
|
+
*
|
|
3090
|
+
* // Push to the end
|
|
3091
|
+
* deque.push(6);
|
|
3092
|
+
* console.log(deque.length); // 6;
|
|
3093
|
+
*
|
|
3094
|
+
* // Pop from the end
|
|
3095
|
+
* const last = deque.pop();
|
|
3096
|
+
* console.log(last); // 6;
|
|
3097
|
+
*/
|
|
1924
3098
|
push(element) {
|
|
1925
3099
|
if (this._length) {
|
|
1926
3100
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -1940,10 +3114,47 @@ var queueTyped = (() => {
|
|
|
1940
3114
|
return true;
|
|
1941
3115
|
}
|
|
1942
3116
|
/**
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
3117
|
+
* Remove and return the last element.
|
|
3118
|
+
* @remarks Time O(1), Space O(1)
|
|
3119
|
+
* @returns Removed element or undefined.
|
|
3120
|
+
|
|
3121
|
+
|
|
3122
|
+
|
|
3123
|
+
|
|
3124
|
+
|
|
3125
|
+
|
|
3126
|
+
|
|
3127
|
+
|
|
3128
|
+
|
|
3129
|
+
|
|
3130
|
+
|
|
3131
|
+
|
|
3132
|
+
|
|
3133
|
+
|
|
3134
|
+
|
|
3135
|
+
|
|
3136
|
+
|
|
3137
|
+
|
|
3138
|
+
|
|
3139
|
+
|
|
3140
|
+
|
|
3141
|
+
|
|
3142
|
+
|
|
3143
|
+
|
|
3144
|
+
|
|
3145
|
+
|
|
3146
|
+
|
|
3147
|
+
|
|
3148
|
+
|
|
3149
|
+
|
|
3150
|
+
|
|
3151
|
+
|
|
3152
|
+
* @example
|
|
3153
|
+
* // Remove from the back
|
|
3154
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3155
|
+
* console.log(dq.pop()); // 3;
|
|
3156
|
+
* console.log(dq.length); // 2;
|
|
3157
|
+
*/
|
|
1947
3158
|
pop() {
|
|
1948
3159
|
if (this._length === 0) return;
|
|
1949
3160
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1963,10 +3174,47 @@ var queueTyped = (() => {
|
|
|
1963
3174
|
return element;
|
|
1964
3175
|
}
|
|
1965
3176
|
/**
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
3177
|
+
* Remove and return the first element.
|
|
3178
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3179
|
+
* @returns Removed element or undefined.
|
|
3180
|
+
|
|
3181
|
+
|
|
3182
|
+
|
|
3183
|
+
|
|
3184
|
+
|
|
3185
|
+
|
|
3186
|
+
|
|
3187
|
+
|
|
3188
|
+
|
|
3189
|
+
|
|
3190
|
+
|
|
3191
|
+
|
|
3192
|
+
|
|
3193
|
+
|
|
3194
|
+
|
|
3195
|
+
|
|
3196
|
+
|
|
3197
|
+
|
|
3198
|
+
|
|
3199
|
+
|
|
3200
|
+
|
|
3201
|
+
|
|
3202
|
+
|
|
3203
|
+
|
|
3204
|
+
|
|
3205
|
+
|
|
3206
|
+
|
|
3207
|
+
|
|
3208
|
+
|
|
3209
|
+
|
|
3210
|
+
|
|
3211
|
+
|
|
3212
|
+
* @example
|
|
3213
|
+
* // Remove from the front
|
|
3214
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3215
|
+
* console.log(dq.shift()); // 1;
|
|
3216
|
+
* console.log(dq.length); // 2;
|
|
3217
|
+
*/
|
|
1970
3218
|
shift() {
|
|
1971
3219
|
if (this._length === 0) return;
|
|
1972
3220
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -1986,11 +3234,58 @@ var queueTyped = (() => {
|
|
|
1986
3234
|
return element;
|
|
1987
3235
|
}
|
|
1988
3236
|
/**
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
3237
|
+
* Prepend one element at the front.
|
|
3238
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3239
|
+
* @param element - Element to prepend.
|
|
3240
|
+
* @returns True when prepended.
|
|
3241
|
+
|
|
3242
|
+
|
|
3243
|
+
|
|
3244
|
+
|
|
3245
|
+
|
|
3246
|
+
|
|
3247
|
+
|
|
3248
|
+
|
|
3249
|
+
|
|
3250
|
+
|
|
3251
|
+
|
|
3252
|
+
|
|
3253
|
+
|
|
3254
|
+
|
|
3255
|
+
|
|
3256
|
+
|
|
3257
|
+
|
|
3258
|
+
|
|
3259
|
+
|
|
3260
|
+
|
|
3261
|
+
|
|
3262
|
+
|
|
3263
|
+
|
|
3264
|
+
|
|
3265
|
+
|
|
3266
|
+
|
|
3267
|
+
|
|
3268
|
+
|
|
3269
|
+
|
|
3270
|
+
|
|
3271
|
+
|
|
3272
|
+
|
|
3273
|
+
* @example
|
|
3274
|
+
* // Deque shift and unshift operations
|
|
3275
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
3276
|
+
*
|
|
3277
|
+
* // Unshift adds to the front
|
|
3278
|
+
* deque.unshift(10);
|
|
3279
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3280
|
+
*
|
|
3281
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
3282
|
+
* const first = deque.shift();
|
|
3283
|
+
* console.log(first); // 10;
|
|
3284
|
+
*
|
|
3285
|
+
* // Verify remaining elements
|
|
3286
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
3287
|
+
* console.log(deque.length); // 3;
|
|
3288
|
+
*/
|
|
1994
3289
|
unshift(element) {
|
|
1995
3290
|
if (this._length) {
|
|
1996
3291
|
if (this._firstInBucket > 0) {
|
|
@@ -2044,18 +3339,87 @@ var queueTyped = (() => {
|
|
|
2044
3339
|
return ans;
|
|
2045
3340
|
}
|
|
2046
3341
|
/**
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
3342
|
+
* Check whether the deque is empty.
|
|
3343
|
+
* @remarks Time O(1), Space O(1)
|
|
3344
|
+
* @returns True if length is 0.
|
|
3345
|
+
|
|
3346
|
+
|
|
3347
|
+
|
|
3348
|
+
|
|
3349
|
+
|
|
3350
|
+
|
|
3351
|
+
|
|
3352
|
+
|
|
3353
|
+
|
|
3354
|
+
|
|
3355
|
+
|
|
3356
|
+
|
|
3357
|
+
|
|
3358
|
+
|
|
3359
|
+
|
|
3360
|
+
|
|
3361
|
+
|
|
3362
|
+
|
|
3363
|
+
|
|
3364
|
+
|
|
3365
|
+
|
|
3366
|
+
|
|
3367
|
+
|
|
3368
|
+
|
|
3369
|
+
|
|
3370
|
+
|
|
3371
|
+
|
|
3372
|
+
|
|
3373
|
+
|
|
3374
|
+
|
|
3375
|
+
* @example
|
|
3376
|
+
* // Check if empty
|
|
3377
|
+
* const dq = new Deque();
|
|
3378
|
+
* console.log(dq.isEmpty()); // true;
|
|
3379
|
+
*/
|
|
2051
3380
|
isEmpty() {
|
|
2052
3381
|
return this._length === 0;
|
|
2053
3382
|
}
|
|
2054
3383
|
/**
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
3384
|
+
* Remove all elements and reset structure.
|
|
3385
|
+
* @remarks Time O(1), Space O(1)
|
|
3386
|
+
* @returns void
|
|
3387
|
+
|
|
3388
|
+
|
|
3389
|
+
|
|
3390
|
+
|
|
3391
|
+
|
|
3392
|
+
|
|
3393
|
+
|
|
3394
|
+
|
|
3395
|
+
|
|
3396
|
+
|
|
3397
|
+
|
|
3398
|
+
|
|
3399
|
+
|
|
3400
|
+
|
|
3401
|
+
|
|
3402
|
+
|
|
3403
|
+
|
|
3404
|
+
|
|
3405
|
+
|
|
3406
|
+
|
|
3407
|
+
|
|
3408
|
+
|
|
3409
|
+
|
|
3410
|
+
|
|
3411
|
+
|
|
3412
|
+
|
|
3413
|
+
|
|
3414
|
+
|
|
3415
|
+
|
|
3416
|
+
|
|
3417
|
+
* @example
|
|
3418
|
+
* // Remove all elements
|
|
3419
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3420
|
+
* dq.clear();
|
|
3421
|
+
* console.log(dq.length); // 0;
|
|
3422
|
+
*/
|
|
2059
3423
|
clear() {
|
|
2060
3424
|
this._buckets = [new Array(this._bucketSize)];
|
|
2061
3425
|
this._bucketCount = 1;
|
|
@@ -2063,11 +3427,45 @@ var queueTyped = (() => {
|
|
|
2063
3427
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2064
3428
|
}
|
|
2065
3429
|
/**
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
3430
|
+
* Get the element at a given position.
|
|
3431
|
+
* @remarks Time O(1), Space O(1)
|
|
3432
|
+
* @param pos - Zero-based position from the front.
|
|
3433
|
+
* @returns Element or undefined.
|
|
3434
|
+
|
|
3435
|
+
|
|
3436
|
+
|
|
3437
|
+
|
|
3438
|
+
|
|
3439
|
+
|
|
3440
|
+
|
|
3441
|
+
|
|
3442
|
+
|
|
3443
|
+
|
|
3444
|
+
|
|
3445
|
+
|
|
3446
|
+
|
|
3447
|
+
|
|
3448
|
+
|
|
3449
|
+
|
|
3450
|
+
|
|
3451
|
+
|
|
3452
|
+
|
|
3453
|
+
|
|
3454
|
+
|
|
3455
|
+
|
|
3456
|
+
|
|
3457
|
+
|
|
3458
|
+
|
|
3459
|
+
|
|
3460
|
+
|
|
3461
|
+
|
|
3462
|
+
|
|
3463
|
+
* @example
|
|
3464
|
+
* // Access by index
|
|
3465
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3466
|
+
* console.log(dq.at(0)); // 'a';
|
|
3467
|
+
* console.log(dq.at(2)); // 'c';
|
|
3468
|
+
*/
|
|
2071
3469
|
at(pos) {
|
|
2072
3470
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
2073
3471
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -2226,11 +3624,45 @@ var queueTyped = (() => {
|
|
|
2226
3624
|
}
|
|
2227
3625
|
}
|
|
2228
3626
|
/**
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
3627
|
+
* Delete the first occurrence of a value.
|
|
3628
|
+
* @remarks Time O(N), Space O(1)
|
|
3629
|
+
* @param element - Element to remove (using the configured equality).
|
|
3630
|
+
* @returns True if an element was removed.
|
|
3631
|
+
|
|
3632
|
+
|
|
3633
|
+
|
|
3634
|
+
|
|
3635
|
+
|
|
3636
|
+
|
|
3637
|
+
|
|
3638
|
+
|
|
3639
|
+
|
|
3640
|
+
|
|
3641
|
+
|
|
3642
|
+
|
|
3643
|
+
|
|
3644
|
+
|
|
3645
|
+
|
|
3646
|
+
|
|
3647
|
+
|
|
3648
|
+
|
|
3649
|
+
|
|
3650
|
+
|
|
3651
|
+
|
|
3652
|
+
|
|
3653
|
+
|
|
3654
|
+
|
|
3655
|
+
|
|
3656
|
+
|
|
3657
|
+
|
|
3658
|
+
|
|
3659
|
+
|
|
3660
|
+
* @example
|
|
3661
|
+
* // Remove element
|
|
3662
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3663
|
+
* dq.delete(2);
|
|
3664
|
+
* console.log(dq.length); // 2;
|
|
3665
|
+
*/
|
|
2234
3666
|
delete(element) {
|
|
2235
3667
|
const size = this._length;
|
|
2236
3668
|
if (size === 0) return false;
|
|
@@ -2274,10 +3706,60 @@ var queueTyped = (() => {
|
|
|
2274
3706
|
return this;
|
|
2275
3707
|
}
|
|
2276
3708
|
/**
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
3709
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
3710
|
+
* @remarks Time O(N), Space O(N)
|
|
3711
|
+
* @returns This deque.
|
|
3712
|
+
|
|
3713
|
+
|
|
3714
|
+
|
|
3715
|
+
|
|
3716
|
+
|
|
3717
|
+
|
|
3718
|
+
|
|
3719
|
+
|
|
3720
|
+
|
|
3721
|
+
|
|
3722
|
+
|
|
3723
|
+
|
|
3724
|
+
|
|
3725
|
+
|
|
3726
|
+
|
|
3727
|
+
|
|
3728
|
+
|
|
3729
|
+
|
|
3730
|
+
|
|
3731
|
+
|
|
3732
|
+
|
|
3733
|
+
|
|
3734
|
+
|
|
3735
|
+
|
|
3736
|
+
|
|
3737
|
+
|
|
3738
|
+
|
|
3739
|
+
|
|
3740
|
+
|
|
3741
|
+
|
|
3742
|
+
|
|
3743
|
+
|
|
3744
|
+
* @example
|
|
3745
|
+
* // Deque for...of iteration and reverse
|
|
3746
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
3747
|
+
*
|
|
3748
|
+
* // Iterate forward
|
|
3749
|
+
* const forward: string[] = [];
|
|
3750
|
+
* for (const item of deque) {
|
|
3751
|
+
* forward.push(item);
|
|
3752
|
+
* }
|
|
3753
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
3754
|
+
*
|
|
3755
|
+
* // Reverse the deque
|
|
3756
|
+
* deque.reverse();
|
|
3757
|
+
* const backward: string[] = [];
|
|
3758
|
+
* for (const item of deque) {
|
|
3759
|
+
* backward.push(item);
|
|
3760
|
+
* }
|
|
3761
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
3762
|
+
*/
|
|
2281
3763
|
reverse() {
|
|
2282
3764
|
this._buckets.reverse().forEach(function(bucket) {
|
|
2283
3765
|
bucket.reverse();
|
|
@@ -2336,10 +3818,46 @@ var queueTyped = (() => {
|
|
|
2336
3818
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2337
3819
|
*/
|
|
2338
3820
|
/**
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
3821
|
+
* Compact the deque by removing unused buckets.
|
|
3822
|
+
* @remarks Time O(N), Space O(1)
|
|
3823
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
3824
|
+
|
|
3825
|
+
|
|
3826
|
+
|
|
3827
|
+
|
|
3828
|
+
|
|
3829
|
+
|
|
3830
|
+
|
|
3831
|
+
|
|
3832
|
+
|
|
3833
|
+
|
|
3834
|
+
|
|
3835
|
+
|
|
3836
|
+
|
|
3837
|
+
|
|
3838
|
+
|
|
3839
|
+
|
|
3840
|
+
|
|
3841
|
+
|
|
3842
|
+
|
|
3843
|
+
|
|
3844
|
+
|
|
3845
|
+
|
|
3846
|
+
|
|
3847
|
+
|
|
3848
|
+
|
|
3849
|
+
|
|
3850
|
+
|
|
3851
|
+
|
|
3852
|
+
|
|
3853
|
+
* @example
|
|
3854
|
+
* // Reclaim memory
|
|
3855
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
3856
|
+
* dq.shift();
|
|
3857
|
+
* dq.shift();
|
|
3858
|
+
* dq.compact();
|
|
3859
|
+
* console.log(dq.length); // 3;
|
|
3860
|
+
*/
|
|
2343
3861
|
compact() {
|
|
2344
3862
|
const before = this._bucketCount;
|
|
2345
3863
|
this.shrinkToFit();
|
|
@@ -2367,10 +3885,47 @@ var queueTyped = (() => {
|
|
|
2367
3885
|
this._compactCounter = 0;
|
|
2368
3886
|
}
|
|
2369
3887
|
/**
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
3888
|
+
* Deep clone this deque, preserving options.
|
|
3889
|
+
* @remarks Time O(N), Space O(N)
|
|
3890
|
+
* @returns A new deque with the same content and options.
|
|
3891
|
+
|
|
3892
|
+
|
|
3893
|
+
|
|
3894
|
+
|
|
3895
|
+
|
|
3896
|
+
|
|
3897
|
+
|
|
3898
|
+
|
|
3899
|
+
|
|
3900
|
+
|
|
3901
|
+
|
|
3902
|
+
|
|
3903
|
+
|
|
3904
|
+
|
|
3905
|
+
|
|
3906
|
+
|
|
3907
|
+
|
|
3908
|
+
|
|
3909
|
+
|
|
3910
|
+
|
|
3911
|
+
|
|
3912
|
+
|
|
3913
|
+
|
|
3914
|
+
|
|
3915
|
+
|
|
3916
|
+
|
|
3917
|
+
|
|
3918
|
+
|
|
3919
|
+
|
|
3920
|
+
|
|
3921
|
+
* @example
|
|
3922
|
+
* // Create independent copy
|
|
3923
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3924
|
+
* const copy = dq.clone();
|
|
3925
|
+
* copy.pop();
|
|
3926
|
+
* console.log(dq.length); // 3;
|
|
3927
|
+
* console.log(copy.length); // 2;
|
|
3928
|
+
*/
|
|
2374
3929
|
clone() {
|
|
2375
3930
|
return this._createLike(this, {
|
|
2376
3931
|
bucketSize: this.bucketSize,
|
|
@@ -2379,12 +3934,47 @@ var queueTyped = (() => {
|
|
|
2379
3934
|
});
|
|
2380
3935
|
}
|
|
2381
3936
|
/**
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
3937
|
+
* Filter elements into a new deque of the same class.
|
|
3938
|
+
* @remarks Time O(N), Space O(N)
|
|
3939
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
3940
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
3941
|
+
* @returns A new deque with kept elements.
|
|
3942
|
+
|
|
3943
|
+
|
|
3944
|
+
|
|
3945
|
+
|
|
3946
|
+
|
|
3947
|
+
|
|
3948
|
+
|
|
3949
|
+
|
|
3950
|
+
|
|
3951
|
+
|
|
3952
|
+
|
|
3953
|
+
|
|
3954
|
+
|
|
3955
|
+
|
|
3956
|
+
|
|
3957
|
+
|
|
3958
|
+
|
|
3959
|
+
|
|
3960
|
+
|
|
3961
|
+
|
|
3962
|
+
|
|
3963
|
+
|
|
3964
|
+
|
|
3965
|
+
|
|
3966
|
+
|
|
3967
|
+
|
|
3968
|
+
|
|
3969
|
+
|
|
3970
|
+
|
|
3971
|
+
|
|
3972
|
+
* @example
|
|
3973
|
+
* // Filter elements
|
|
3974
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
3975
|
+
* const result = dq.filter(x => x > 2);
|
|
3976
|
+
* console.log(result.length); // 2;
|
|
3977
|
+
*/
|
|
2388
3978
|
filter(predicate, thisArg) {
|
|
2389
3979
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2390
3980
|
out._setBucketSize(this._bucketSize);
|
|
@@ -2413,15 +4003,49 @@ var queueTyped = (() => {
|
|
|
2413
4003
|
return out;
|
|
2414
4004
|
}
|
|
2415
4005
|
/**
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
4006
|
+
* Map elements into a new deque (possibly different element type).
|
|
4007
|
+
* @remarks Time O(N), Space O(N)
|
|
4008
|
+
* @template EM
|
|
4009
|
+
* @template RM
|
|
4010
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
4011
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
4012
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
4013
|
+
* @returns A new Deque with mapped elements.
|
|
4014
|
+
|
|
4015
|
+
|
|
4016
|
+
|
|
4017
|
+
|
|
4018
|
+
|
|
4019
|
+
|
|
4020
|
+
|
|
4021
|
+
|
|
4022
|
+
|
|
4023
|
+
|
|
4024
|
+
|
|
4025
|
+
|
|
4026
|
+
|
|
4027
|
+
|
|
4028
|
+
|
|
4029
|
+
|
|
4030
|
+
|
|
4031
|
+
|
|
4032
|
+
|
|
4033
|
+
|
|
4034
|
+
|
|
4035
|
+
|
|
4036
|
+
|
|
4037
|
+
|
|
4038
|
+
|
|
4039
|
+
|
|
4040
|
+
|
|
4041
|
+
|
|
4042
|
+
|
|
4043
|
+
* @example
|
|
4044
|
+
* // Transform elements
|
|
4045
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
4046
|
+
* const result = dq.map(x => x * 10);
|
|
4047
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
4048
|
+
*/
|
|
2425
4049
|
map(callback, options, thisArg) {
|
|
2426
4050
|
const out = this._createLike([], {
|
|
2427
4051
|
...options != null ? options : {},
|