queue-typed 2.4.5 → 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 +997 -255
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +996 -254
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +997 -255
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +996 -254
- package/dist/esm-legacy/index.mjs.map +1 -1
- 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 +429 -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 +212 -32
- 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/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -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 +272 -65
- 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/umd/queue-typed.js +994 -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/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -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 +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 +416 -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 +272 -65
- 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 +213 -43
- 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/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,37 @@ 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
|
+
* @example
|
|
756
|
+
* // basic SinglyLinkedList creation and push operation
|
|
757
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
758
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
759
|
+
*
|
|
760
|
+
* // Verify the list maintains insertion order
|
|
761
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
762
|
+
*
|
|
763
|
+
* // Check length
|
|
764
|
+
* console.log(list.length); // 5;
|
|
765
|
+
*
|
|
766
|
+
* // Push a new element to the end
|
|
767
|
+
* list.push(6);
|
|
768
|
+
* console.log(list.length); // 6;
|
|
769
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
770
|
+
*/
|
|
791
771
|
push(elementOrNode) {
|
|
792
772
|
const newNode = this._ensureNode(elementOrNode);
|
|
793
773
|
if (!this.head) {
|
|
@@ -801,10 +781,36 @@ var queueTyped = (() => {
|
|
|
801
781
|
return true;
|
|
802
782
|
}
|
|
803
783
|
/**
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
784
|
+
* Remove and return the tail element.
|
|
785
|
+
* @remarks Time O(N), Space O(1)
|
|
786
|
+
* @returns Removed element or undefined.
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
* @example
|
|
799
|
+
* // SinglyLinkedList pop and shift operations
|
|
800
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
801
|
+
*
|
|
802
|
+
* // Pop removes from the end
|
|
803
|
+
* const last = list.pop();
|
|
804
|
+
* console.log(last); // 50;
|
|
805
|
+
*
|
|
806
|
+
* // Shift removes from the beginning
|
|
807
|
+
* const first = list.shift();
|
|
808
|
+
* console.log(first); // 10;
|
|
809
|
+
*
|
|
810
|
+
* // Verify remaining elements
|
|
811
|
+
* console.log([...list]); // [20, 30, 40];
|
|
812
|
+
* console.log(list.length); // 3;
|
|
813
|
+
*/
|
|
808
814
|
pop() {
|
|
809
815
|
var _a;
|
|
810
816
|
if (!this.head) return void 0;
|
|
@@ -824,10 +830,26 @@ var queueTyped = (() => {
|
|
|
824
830
|
return value;
|
|
825
831
|
}
|
|
826
832
|
/**
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
833
|
+
* Remove and return the head element.
|
|
834
|
+
* @remarks Time O(1), Space O(1)
|
|
835
|
+
* @returns Removed element or undefined.
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
* @example
|
|
848
|
+
* // Remove from the front
|
|
849
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
850
|
+
* console.log(list.shift()); // 10;
|
|
851
|
+
* console.log(list.length); // 2;
|
|
852
|
+
*/
|
|
831
853
|
shift() {
|
|
832
854
|
if (!this.head) return void 0;
|
|
833
855
|
const removed = this.head;
|
|
@@ -837,11 +859,42 @@ var queueTyped = (() => {
|
|
|
837
859
|
return removed.value;
|
|
838
860
|
}
|
|
839
861
|
/**
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
862
|
+
* Prepend an element/node to the head.
|
|
863
|
+
* @remarks Time O(1), Space O(1)
|
|
864
|
+
* @param elementOrNode - Element or node to prepend.
|
|
865
|
+
* @returns True when prepended.
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
* @example
|
|
878
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
879
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
880
|
+
*
|
|
881
|
+
* // Unshift adds to the beginning
|
|
882
|
+
* list.unshift(10);
|
|
883
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
884
|
+
*
|
|
885
|
+
* // Access elements (forward traversal only for singly linked)
|
|
886
|
+
* const second = list.at(1);
|
|
887
|
+
* console.log(second); // 20;
|
|
888
|
+
*
|
|
889
|
+
* // SinglyLinkedList allows forward iteration only
|
|
890
|
+
* const elements: number[] = [];
|
|
891
|
+
* for (const item of list) {
|
|
892
|
+
* elements.push(item);
|
|
893
|
+
* }
|
|
894
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
895
|
+
*
|
|
896
|
+
* console.log(list.length); // 4;
|
|
897
|
+
*/
|
|
845
898
|
unshift(elementOrNode) {
|
|
846
899
|
const newNode = this._ensureNode(elementOrNode);
|
|
847
900
|
if (!this.head) {
|
|
@@ -897,11 +950,28 @@ var queueTyped = (() => {
|
|
|
897
950
|
return void 0;
|
|
898
951
|
}
|
|
899
952
|
/**
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
953
|
+
* Get the element at a given index.
|
|
954
|
+
* @remarks Time O(N), Space O(1)
|
|
955
|
+
* @param index - Zero-based index.
|
|
956
|
+
* @returns Element or undefined.
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
* @example
|
|
969
|
+
* // Access element by index
|
|
970
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
971
|
+
* console.log(list.at(0)); // 'a';
|
|
972
|
+
* console.log(list.at(2)); // 'c';
|
|
973
|
+
* console.log(list.at(3)); // 'd';
|
|
974
|
+
*/
|
|
905
975
|
at(index) {
|
|
906
976
|
if (index < 0 || index >= this._length) return void 0;
|
|
907
977
|
let current = this.head;
|
|
@@ -918,11 +988,23 @@ var queueTyped = (() => {
|
|
|
918
988
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
919
989
|
}
|
|
920
990
|
/**
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
991
|
+
* Get the node reference at a given index.
|
|
992
|
+
* @remarks Time O(N), Space O(1)
|
|
993
|
+
* @param index - Zero-based index.
|
|
994
|
+
* @returns Node or undefined.
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
* @example
|
|
1004
|
+
* // Get node at index
|
|
1005
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1006
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1007
|
+
*/
|
|
926
1008
|
getNodeAt(index) {
|
|
927
1009
|
if (index < 0 || index >= this._length) return void 0;
|
|
928
1010
|
let current = this.head;
|
|
@@ -930,11 +1012,24 @@ var queueTyped = (() => {
|
|
|
930
1012
|
return current;
|
|
931
1013
|
}
|
|
932
1014
|
/**
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
1015
|
+
* Delete the element at an index.
|
|
1016
|
+
* @remarks Time O(N), Space O(1)
|
|
1017
|
+
* @param index - Zero-based index.
|
|
1018
|
+
* @returns Removed element or undefined.
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
* @example
|
|
1028
|
+
* // Remove by index
|
|
1029
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1030
|
+
* list.deleteAt(1);
|
|
1031
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1032
|
+
*/
|
|
938
1033
|
deleteAt(index) {
|
|
939
1034
|
if (index < 0 || index >= this._length) return void 0;
|
|
940
1035
|
if (index === 0) return this.shift();
|
|
@@ -947,11 +1042,24 @@ var queueTyped = (() => {
|
|
|
947
1042
|
return value;
|
|
948
1043
|
}
|
|
949
1044
|
/**
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
1045
|
+
* Delete the first match by value/node.
|
|
1046
|
+
* @remarks Time O(N), Space O(1)
|
|
1047
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1048
|
+
* @returns True if removed.
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
* @example
|
|
1058
|
+
* // Remove first occurrence
|
|
1059
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1060
|
+
* list.delete(2);
|
|
1061
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1062
|
+
*/
|
|
955
1063
|
delete(elementOrNode) {
|
|
956
1064
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
957
1065
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -968,12 +1076,25 @@ var queueTyped = (() => {
|
|
|
968
1076
|
return true;
|
|
969
1077
|
}
|
|
970
1078
|
/**
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
1079
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1080
|
+
* @remarks Time O(N), Space O(1)
|
|
1081
|
+
* @param index - Zero-based index.
|
|
1082
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1083
|
+
* @returns True if inserted.
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
* @example
|
|
1093
|
+
* // Insert at index
|
|
1094
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1095
|
+
* list.addAt(1, 2);
|
|
1096
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1097
|
+
*/
|
|
977
1098
|
addAt(index, newElementOrNode) {
|
|
978
1099
|
if (index < 0 || index > this._length) return false;
|
|
979
1100
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -999,28 +1120,70 @@ var queueTyped = (() => {
|
|
|
999
1120
|
return true;
|
|
1000
1121
|
}
|
|
1001
1122
|
/**
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1123
|
+
* Check whether the list is empty.
|
|
1124
|
+
* @remarks Time O(1), Space O(1)
|
|
1125
|
+
* @returns True if length is 0.
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
* @example
|
|
1136
|
+
* // Check empty
|
|
1137
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1138
|
+
*/
|
|
1006
1139
|
isEmpty() {
|
|
1007
1140
|
return this._length === 0;
|
|
1008
1141
|
}
|
|
1009
1142
|
/**
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1143
|
+
* Remove all nodes and reset length.
|
|
1144
|
+
* @remarks Time O(N), Space O(1)
|
|
1145
|
+
* @returns void
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
* @example
|
|
1156
|
+
* // Remove all
|
|
1157
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1158
|
+
* list.clear();
|
|
1159
|
+
* console.log(list.isEmpty()); // true;
|
|
1160
|
+
*/
|
|
1014
1161
|
clear() {
|
|
1015
1162
|
this._head = void 0;
|
|
1016
1163
|
this._tail = void 0;
|
|
1017
1164
|
this._length = 0;
|
|
1018
1165
|
}
|
|
1019
1166
|
/**
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1167
|
+
* Reverse the list in place.
|
|
1168
|
+
* @remarks Time O(N), Space O(1)
|
|
1169
|
+
* @returns This list.
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
* @example
|
|
1182
|
+
* // Reverse the list in-place
|
|
1183
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1184
|
+
* list.reverse();
|
|
1185
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1186
|
+
*/
|
|
1024
1187
|
reverse() {
|
|
1025
1188
|
if (!this.head || this.head === this.tail) return this;
|
|
1026
1189
|
let prev;
|
|
@@ -1195,22 +1358,64 @@ var queueTyped = (() => {
|
|
|
1195
1358
|
return false;
|
|
1196
1359
|
}
|
|
1197
1360
|
/**
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1361
|
+
* Deep clone this list (values are copied by reference).
|
|
1362
|
+
* @remarks Time O(N), Space O(N)
|
|
1363
|
+
* @returns A new list with the same element sequence.
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
* @example
|
|
1374
|
+
* // Deep copy
|
|
1375
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1376
|
+
* const copy = list.clone();
|
|
1377
|
+
* copy.pop();
|
|
1378
|
+
* console.log(list.length); // 3;
|
|
1379
|
+
* console.log(copy.length); // 2;
|
|
1380
|
+
*/
|
|
1202
1381
|
clone() {
|
|
1203
1382
|
const out = this._createInstance();
|
|
1204
1383
|
for (const v of this) out.push(v);
|
|
1205
1384
|
return out;
|
|
1206
1385
|
}
|
|
1207
1386
|
/**
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1387
|
+
* Filter values into a new list of the same class.
|
|
1388
|
+
* @remarks Time O(N), Space O(N)
|
|
1389
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1390
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1391
|
+
* @returns A new list with kept values.
|
|
1392
|
+
|
|
1393
|
+
|
|
1394
|
+
|
|
1395
|
+
|
|
1396
|
+
|
|
1397
|
+
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
* @example
|
|
1404
|
+
* // SinglyLinkedList filter and map operations
|
|
1405
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1406
|
+
*
|
|
1407
|
+
* // Filter even numbers
|
|
1408
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1409
|
+
* console.log(filtered.length); // 2;
|
|
1410
|
+
*
|
|
1411
|
+
* // Map to double values
|
|
1412
|
+
* const doubled = list.map(value => value * 2);
|
|
1413
|
+
* console.log(doubled.length); // 5;
|
|
1414
|
+
*
|
|
1415
|
+
* // Use reduce to sum
|
|
1416
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1417
|
+
* console.log(sum); // 15;
|
|
1418
|
+
*/
|
|
1214
1419
|
filter(callback, thisArg) {
|
|
1215
1420
|
const out = this._createInstance();
|
|
1216
1421
|
let index = 0;
|
|
@@ -1234,15 +1439,31 @@ var queueTyped = (() => {
|
|
|
1234
1439
|
return out;
|
|
1235
1440
|
}
|
|
1236
1441
|
/**
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1442
|
+
* Map values into a new list (possibly different element type).
|
|
1443
|
+
* @remarks Time O(N), Space O(N)
|
|
1444
|
+
* @template EM
|
|
1445
|
+
* @template RM
|
|
1446
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1447
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1448
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1449
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1450
|
+
|
|
1451
|
+
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
* @example
|
|
1462
|
+
* // Transform elements
|
|
1463
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1464
|
+
* const doubled = list.map(n => n * 2);
|
|
1465
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1466
|
+
*/
|
|
1246
1467
|
map(callback, options, thisArg) {
|
|
1247
1468
|
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1248
1469
|
let index = 0;
|
|
@@ -1376,6 +1597,52 @@ var queueTyped = (() => {
|
|
|
1376
1597
|
return (node) => equals(node.value, value);
|
|
1377
1598
|
}
|
|
1378
1599
|
|
|
1600
|
+
// src/common/error.ts
|
|
1601
|
+
var ERR = {
|
|
1602
|
+
// Range / index
|
|
1603
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
1604
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
1605
|
+
// Type / argument
|
|
1606
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
1607
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
1608
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
1609
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
1610
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
1611
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
1612
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
1613
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
1614
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
1615
|
+
// State / operation
|
|
1616
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
1617
|
+
// Matrix
|
|
1618
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
1619
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
1620
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
1621
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
1622
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
1623
|
+
};
|
|
1624
|
+
|
|
1625
|
+
// src/common/index.ts
|
|
1626
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1627
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1628
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1629
|
+
return DFSOperation2;
|
|
1630
|
+
})(DFSOperation || {});
|
|
1631
|
+
var Range = class {
|
|
1632
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1633
|
+
this.low = low;
|
|
1634
|
+
this.high = high;
|
|
1635
|
+
this.includeLow = includeLow;
|
|
1636
|
+
this.includeHigh = includeHigh;
|
|
1637
|
+
}
|
|
1638
|
+
// Determine whether a key is within the range
|
|
1639
|
+
isInRange(key, comparator) {
|
|
1640
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1641
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1642
|
+
return lowCheck && highCheck;
|
|
1643
|
+
}
|
|
1644
|
+
};
|
|
1645
|
+
|
|
1379
1646
|
// src/data-structures/queue/queue.ts
|
|
1380
1647
|
var Queue = class _Queue extends LinearBase {
|
|
1381
1648
|
/**
|
|
@@ -1430,18 +1697,52 @@ var queueTyped = (() => {
|
|
|
1430
1697
|
this._autoCompactRatio = value;
|
|
1431
1698
|
}
|
|
1432
1699
|
/**
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1700
|
+
* Get the number of elements currently in the queue.
|
|
1701
|
+
* @remarks Time O(1), Space O(1)
|
|
1702
|
+
* @returns Current length.
|
|
1703
|
+
|
|
1704
|
+
|
|
1705
|
+
|
|
1706
|
+
|
|
1707
|
+
|
|
1708
|
+
|
|
1709
|
+
|
|
1710
|
+
|
|
1711
|
+
|
|
1712
|
+
|
|
1713
|
+
|
|
1714
|
+
* @example
|
|
1715
|
+
* // Track queue length
|
|
1716
|
+
* const q = new Queue<number>();
|
|
1717
|
+
* console.log(q.length); // 0;
|
|
1718
|
+
* q.push(1);
|
|
1719
|
+
* q.push(2);
|
|
1720
|
+
* console.log(q.length); // 2;
|
|
1721
|
+
*/
|
|
1437
1722
|
get length() {
|
|
1438
1723
|
return this.elements.length - this._offset;
|
|
1439
1724
|
}
|
|
1440
1725
|
/**
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1726
|
+
* Get the first element (front) without removing it.
|
|
1727
|
+
* @remarks Time O(1), Space O(1)
|
|
1728
|
+
* @returns Front element or undefined.
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
|
|
1732
|
+
|
|
1733
|
+
|
|
1734
|
+
|
|
1735
|
+
|
|
1736
|
+
|
|
1737
|
+
|
|
1738
|
+
|
|
1739
|
+
|
|
1740
|
+
* @example
|
|
1741
|
+
* // View the front element
|
|
1742
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1743
|
+
* console.log(q.first); // 'first';
|
|
1744
|
+
* console.log(q.length); // 3;
|
|
1745
|
+
*/
|
|
1445
1746
|
get first() {
|
|
1446
1747
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1447
1748
|
}
|
|
@@ -1464,19 +1765,69 @@ var queueTyped = (() => {
|
|
|
1464
1765
|
return new _Queue(elements);
|
|
1465
1766
|
}
|
|
1466
1767
|
/**
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1768
|
+
* Check whether the queue is empty.
|
|
1769
|
+
* @remarks Time O(1), Space O(1)
|
|
1770
|
+
* @returns True if length is 0.
|
|
1771
|
+
|
|
1772
|
+
|
|
1773
|
+
|
|
1774
|
+
|
|
1775
|
+
|
|
1776
|
+
|
|
1777
|
+
|
|
1778
|
+
|
|
1779
|
+
|
|
1780
|
+
|
|
1781
|
+
|
|
1782
|
+
* @example
|
|
1783
|
+
* // Queue for...of iteration and isEmpty check
|
|
1784
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1785
|
+
*
|
|
1786
|
+
* const elements: string[] = [];
|
|
1787
|
+
* for (const item of queue) {
|
|
1788
|
+
* elements.push(item);
|
|
1789
|
+
* }
|
|
1790
|
+
*
|
|
1791
|
+
* // Verify all elements are iterated in order
|
|
1792
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1793
|
+
*
|
|
1794
|
+
* // Process all elements
|
|
1795
|
+
* while (queue.length > 0) {
|
|
1796
|
+
* queue.shift();
|
|
1797
|
+
* }
|
|
1798
|
+
*
|
|
1799
|
+
* console.log(queue.length); // 0;
|
|
1800
|
+
*/
|
|
1471
1801
|
isEmpty() {
|
|
1472
1802
|
return this.length === 0;
|
|
1473
1803
|
}
|
|
1474
1804
|
/**
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1805
|
+
* Enqueue one element at the back.
|
|
1806
|
+
* @remarks Time O(1), Space O(1)
|
|
1807
|
+
* @param element - Element to enqueue.
|
|
1808
|
+
* @returns True on success.
|
|
1809
|
+
|
|
1810
|
+
|
|
1811
|
+
|
|
1812
|
+
|
|
1813
|
+
|
|
1814
|
+
|
|
1815
|
+
|
|
1816
|
+
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
|
|
1820
|
+
* @example
|
|
1821
|
+
* // basic Queue creation and push operation
|
|
1822
|
+
* // Create a simple Queue with initial values
|
|
1823
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1824
|
+
*
|
|
1825
|
+
* // Verify the queue maintains insertion order
|
|
1826
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1827
|
+
*
|
|
1828
|
+
* // Check length
|
|
1829
|
+
* console.log(queue.length); // 5;
|
|
1830
|
+
*/
|
|
1480
1831
|
push(element) {
|
|
1481
1832
|
this.elements.push(element);
|
|
1482
1833
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1497,10 +1848,35 @@ var queueTyped = (() => {
|
|
|
1497
1848
|
return ans;
|
|
1498
1849
|
}
|
|
1499
1850
|
/**
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1851
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1852
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1853
|
+
* @returns Removed element or undefined.
|
|
1854
|
+
|
|
1855
|
+
|
|
1856
|
+
|
|
1857
|
+
|
|
1858
|
+
|
|
1859
|
+
|
|
1860
|
+
|
|
1861
|
+
|
|
1862
|
+
|
|
1863
|
+
|
|
1864
|
+
|
|
1865
|
+
* @example
|
|
1866
|
+
* // Queue shift and peek operations
|
|
1867
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1868
|
+
*
|
|
1869
|
+
* // Peek at the front element without removing it
|
|
1870
|
+
* console.log(queue.first); // 10;
|
|
1871
|
+
*
|
|
1872
|
+
* // Remove and get the first element (FIFO)
|
|
1873
|
+
* const first = queue.shift();
|
|
1874
|
+
* console.log(first); // 10;
|
|
1875
|
+
*
|
|
1876
|
+
* // Verify remaining elements and length decreased
|
|
1877
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1878
|
+
* console.log(queue.length); // 3;
|
|
1879
|
+
*/
|
|
1504
1880
|
shift() {
|
|
1505
1881
|
if (this.length === 0) return void 0;
|
|
1506
1882
|
const first = this.first;
|
|
@@ -1509,11 +1885,24 @@ var queueTyped = (() => {
|
|
|
1509
1885
|
return first;
|
|
1510
1886
|
}
|
|
1511
1887
|
/**
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1888
|
+
* Delete the first occurrence of a specific element.
|
|
1889
|
+
* @remarks Time O(N), Space O(1)
|
|
1890
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1891
|
+
* @returns True if an element was removed.
|
|
1892
|
+
|
|
1893
|
+
|
|
1894
|
+
|
|
1895
|
+
|
|
1896
|
+
|
|
1897
|
+
|
|
1898
|
+
|
|
1899
|
+
|
|
1900
|
+
* @example
|
|
1901
|
+
* // Remove specific element
|
|
1902
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1903
|
+
* q.delete(2);
|
|
1904
|
+
* console.log(q.length); // 3;
|
|
1905
|
+
*/
|
|
1517
1906
|
delete(element) {
|
|
1518
1907
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1519
1908
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1524,11 +1913,24 @@ var queueTyped = (() => {
|
|
|
1524
1913
|
return false;
|
|
1525
1914
|
}
|
|
1526
1915
|
/**
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1916
|
+
* Get the element at a given logical index.
|
|
1917
|
+
* @remarks Time O(1), Space O(1)
|
|
1918
|
+
* @param index - Zero-based index from the front.
|
|
1919
|
+
* @returns Element or undefined.
|
|
1920
|
+
|
|
1921
|
+
|
|
1922
|
+
|
|
1923
|
+
|
|
1924
|
+
|
|
1925
|
+
|
|
1926
|
+
|
|
1927
|
+
|
|
1928
|
+
* @example
|
|
1929
|
+
* // Access element by index
|
|
1930
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1931
|
+
* console.log(q.at(0)); // 'a';
|
|
1932
|
+
* console.log(q.at(2)); // 'c';
|
|
1933
|
+
*/
|
|
1532
1934
|
at(index) {
|
|
1533
1935
|
if (index < 0 || index >= this.length) return void 0;
|
|
1534
1936
|
return this._elements[this._offset + index];
|
|
@@ -1580,19 +1982,48 @@ var queueTyped = (() => {
|
|
|
1580
1982
|
return this;
|
|
1581
1983
|
}
|
|
1582
1984
|
/**
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1985
|
+
* Remove all elements and reset offset.
|
|
1986
|
+
* @remarks Time O(1), Space O(1)
|
|
1987
|
+
* @returns void
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
|
|
1991
|
+
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
* @example
|
|
1998
|
+
* // Remove all elements
|
|
1999
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2000
|
+
* q.clear();
|
|
2001
|
+
* console.log(q.length); // 0;
|
|
2002
|
+
*/
|
|
1587
2003
|
clear() {
|
|
1588
2004
|
this._elements = [];
|
|
1589
2005
|
this._offset = 0;
|
|
1590
2006
|
}
|
|
1591
2007
|
/**
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
2008
|
+
* Compact storage by discarding consumed head elements.
|
|
2009
|
+
* @remarks Time O(N), Space O(N)
|
|
2010
|
+
* @returns True when compaction performed.
|
|
2011
|
+
|
|
2012
|
+
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
|
|
2016
|
+
|
|
2017
|
+
|
|
2018
|
+
|
|
2019
|
+
* @example
|
|
2020
|
+
* // Reclaim unused memory
|
|
2021
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2022
|
+
* q.shift();
|
|
2023
|
+
* q.shift();
|
|
2024
|
+
* q.compact();
|
|
2025
|
+
* console.log(q.length); // 3;
|
|
2026
|
+
*/
|
|
1596
2027
|
compact() {
|
|
1597
2028
|
this._elements = this.elements.slice(this._offset);
|
|
1598
2029
|
this._offset = 0;
|
|
@@ -1618,10 +2049,26 @@ var queueTyped = (() => {
|
|
|
1618
2049
|
return removed;
|
|
1619
2050
|
}
|
|
1620
2051
|
/**
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
2052
|
+
* Deep clone this queue and its parameters.
|
|
2053
|
+
* @remarks Time O(N), Space O(N)
|
|
2054
|
+
* @returns A new queue with the same content and options.
|
|
2055
|
+
|
|
2056
|
+
|
|
2057
|
+
|
|
2058
|
+
|
|
2059
|
+
|
|
2060
|
+
|
|
2061
|
+
|
|
2062
|
+
|
|
2063
|
+
|
|
2064
|
+
* @example
|
|
2065
|
+
* // Create independent copy
|
|
2066
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2067
|
+
* const copy = q.clone();
|
|
2068
|
+
* copy.shift();
|
|
2069
|
+
* console.log(q.length); // 3;
|
|
2070
|
+
* console.log(copy.length); // 2;
|
|
2071
|
+
*/
|
|
1625
2072
|
clone() {
|
|
1626
2073
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1627
2074
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1629,12 +2076,26 @@ var queueTyped = (() => {
|
|
|
1629
2076
|
return out;
|
|
1630
2077
|
}
|
|
1631
2078
|
/**
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
2079
|
+
* Filter elements into a new queue of the same class.
|
|
2080
|
+
* @remarks Time O(N), Space O(N)
|
|
2081
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2082
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2083
|
+
* @returns A new queue with kept elements.
|
|
2084
|
+
|
|
2085
|
+
|
|
2086
|
+
|
|
2087
|
+
|
|
2088
|
+
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
* @example
|
|
2094
|
+
* // Filter elements
|
|
2095
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2096
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2097
|
+
* console.log(evens.length); // 2;
|
|
2098
|
+
*/
|
|
1638
2099
|
filter(predicate, thisArg) {
|
|
1639
2100
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1640
2101
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1646,15 +2107,28 @@ var queueTyped = (() => {
|
|
|
1646
2107
|
return out;
|
|
1647
2108
|
}
|
|
1648
2109
|
/**
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
2110
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2111
|
+
* @remarks Time O(N), Space O(N)
|
|
2112
|
+
* @template EM
|
|
2113
|
+
* @template RM
|
|
2114
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2115
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2116
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2117
|
+
* @returns A new Queue with mapped elements.
|
|
2118
|
+
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
* @example
|
|
2127
|
+
* // Transform elements
|
|
2128
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2129
|
+
* const doubled = q.map(x => x * 2);
|
|
2130
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2131
|
+
*/
|
|
1658
2132
|
map(callback, options, thisArg) {
|
|
1659
2133
|
var _a, _b;
|
|
1660
2134
|
const out = new this.constructor([], {
|
|
@@ -1885,19 +2359,60 @@ var queueTyped = (() => {
|
|
|
1885
2359
|
return this._length;
|
|
1886
2360
|
}
|
|
1887
2361
|
/**
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
2362
|
+
* Get the first element without removing it.
|
|
2363
|
+
* @remarks Time O(1), Space O(1)
|
|
2364
|
+
* @returns First element or undefined.
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
|
|
2368
|
+
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
|
|
2372
|
+
|
|
2373
|
+
|
|
2374
|
+
|
|
2375
|
+
|
|
2376
|
+
* @example
|
|
2377
|
+
* // Deque peek at both ends
|
|
2378
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2379
|
+
*
|
|
2380
|
+
* // Get first element without removing
|
|
2381
|
+
* const first = deque.at(0);
|
|
2382
|
+
* console.log(first); // 10;
|
|
2383
|
+
*
|
|
2384
|
+
* // Get last element without removing
|
|
2385
|
+
* const last = deque.at(deque.length - 1);
|
|
2386
|
+
* console.log(last); // 50;
|
|
2387
|
+
*
|
|
2388
|
+
* // Length unchanged
|
|
2389
|
+
* console.log(deque.length); // 5;
|
|
2390
|
+
*/
|
|
1892
2391
|
get first() {
|
|
1893
2392
|
if (this._length === 0) return;
|
|
1894
2393
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
1895
2394
|
}
|
|
1896
2395
|
/**
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
2396
|
+
* Get the last element without removing it.
|
|
2397
|
+
* @remarks Time O(1), Space O(1)
|
|
2398
|
+
* @returns Last element or undefined.
|
|
2399
|
+
|
|
2400
|
+
|
|
2401
|
+
|
|
2402
|
+
|
|
2403
|
+
|
|
2404
|
+
|
|
2405
|
+
|
|
2406
|
+
|
|
2407
|
+
|
|
2408
|
+
|
|
2409
|
+
|
|
2410
|
+
* @example
|
|
2411
|
+
* // Peek at the back element
|
|
2412
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2413
|
+
* console.log(dq.last); // 'c';
|
|
2414
|
+
* console.log(dq.first); // 'a';
|
|
2415
|
+
*/
|
|
1901
2416
|
get last() {
|
|
1902
2417
|
if (this._length === 0) return;
|
|
1903
2418
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1916,11 +2431,40 @@ var queueTyped = (() => {
|
|
|
1916
2431
|
return new this(data, options);
|
|
1917
2432
|
}
|
|
1918
2433
|
/**
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
2434
|
+
* Append one element at the back.
|
|
2435
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2436
|
+
* @param element - Element to append.
|
|
2437
|
+
* @returns True when appended.
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
|
|
2448
|
+
|
|
2449
|
+
* @example
|
|
2450
|
+
* // basic Deque creation and push/pop operations
|
|
2451
|
+
* // Create a simple Deque with initial values
|
|
2452
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
2453
|
+
*
|
|
2454
|
+
* // Verify the deque maintains insertion order
|
|
2455
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
2456
|
+
*
|
|
2457
|
+
* // Check length
|
|
2458
|
+
* console.log(deque.length); // 5;
|
|
2459
|
+
*
|
|
2460
|
+
* // Push to the end
|
|
2461
|
+
* deque.push(6);
|
|
2462
|
+
* console.log(deque.length); // 6;
|
|
2463
|
+
*
|
|
2464
|
+
* // Pop from the end
|
|
2465
|
+
* const last = deque.pop();
|
|
2466
|
+
* console.log(last); // 6;
|
|
2467
|
+
*/
|
|
1924
2468
|
push(element) {
|
|
1925
2469
|
if (this._length) {
|
|
1926
2470
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -1940,10 +2484,26 @@ var queueTyped = (() => {
|
|
|
1940
2484
|
return true;
|
|
1941
2485
|
}
|
|
1942
2486
|
/**
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
2487
|
+
* Remove and return the last element.
|
|
2488
|
+
* @remarks Time O(1), Space O(1)
|
|
2489
|
+
* @returns Removed element or undefined.
|
|
2490
|
+
|
|
2491
|
+
|
|
2492
|
+
|
|
2493
|
+
|
|
2494
|
+
|
|
2495
|
+
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2501
|
+
* @example
|
|
2502
|
+
* // Remove from the back
|
|
2503
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2504
|
+
* console.log(dq.pop()); // 3;
|
|
2505
|
+
* console.log(dq.length); // 2;
|
|
2506
|
+
*/
|
|
1947
2507
|
pop() {
|
|
1948
2508
|
if (this._length === 0) return;
|
|
1949
2509
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1963,10 +2523,26 @@ var queueTyped = (() => {
|
|
|
1963
2523
|
return element;
|
|
1964
2524
|
}
|
|
1965
2525
|
/**
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
2526
|
+
* Remove and return the first element.
|
|
2527
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2528
|
+
* @returns Removed element or undefined.
|
|
2529
|
+
|
|
2530
|
+
|
|
2531
|
+
|
|
2532
|
+
|
|
2533
|
+
|
|
2534
|
+
|
|
2535
|
+
|
|
2536
|
+
|
|
2537
|
+
|
|
2538
|
+
|
|
2539
|
+
|
|
2540
|
+
* @example
|
|
2541
|
+
* // Remove from the front
|
|
2542
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2543
|
+
* console.log(dq.shift()); // 1;
|
|
2544
|
+
* console.log(dq.length); // 2;
|
|
2545
|
+
*/
|
|
1970
2546
|
shift() {
|
|
1971
2547
|
if (this._length === 0) return;
|
|
1972
2548
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -1986,11 +2562,37 @@ var queueTyped = (() => {
|
|
|
1986
2562
|
return element;
|
|
1987
2563
|
}
|
|
1988
2564
|
/**
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
2565
|
+
* Prepend one element at the front.
|
|
2566
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2567
|
+
* @param element - Element to prepend.
|
|
2568
|
+
* @returns True when prepended.
|
|
2569
|
+
|
|
2570
|
+
|
|
2571
|
+
|
|
2572
|
+
|
|
2573
|
+
|
|
2574
|
+
|
|
2575
|
+
|
|
2576
|
+
|
|
2577
|
+
|
|
2578
|
+
|
|
2579
|
+
|
|
2580
|
+
* @example
|
|
2581
|
+
* // Deque shift and unshift operations
|
|
2582
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
2583
|
+
*
|
|
2584
|
+
* // Unshift adds to the front
|
|
2585
|
+
* deque.unshift(10);
|
|
2586
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
2587
|
+
*
|
|
2588
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
2589
|
+
* const first = deque.shift();
|
|
2590
|
+
* console.log(first); // 10;
|
|
2591
|
+
*
|
|
2592
|
+
* // Verify remaining elements
|
|
2593
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
2594
|
+
* console.log(deque.length); // 3;
|
|
2595
|
+
*/
|
|
1994
2596
|
unshift(element) {
|
|
1995
2597
|
if (this._length) {
|
|
1996
2598
|
if (this._firstInBucket > 0) {
|
|
@@ -2044,18 +2646,45 @@ var queueTyped = (() => {
|
|
|
2044
2646
|
return ans;
|
|
2045
2647
|
}
|
|
2046
2648
|
/**
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2649
|
+
* Check whether the deque is empty.
|
|
2650
|
+
* @remarks Time O(1), Space O(1)
|
|
2651
|
+
* @returns True if length is 0.
|
|
2652
|
+
|
|
2653
|
+
|
|
2654
|
+
|
|
2655
|
+
|
|
2656
|
+
|
|
2657
|
+
|
|
2658
|
+
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
* @example
|
|
2662
|
+
* // Check if empty
|
|
2663
|
+
* const dq = new Deque();
|
|
2664
|
+
* console.log(dq.isEmpty()); // true;
|
|
2665
|
+
*/
|
|
2051
2666
|
isEmpty() {
|
|
2052
2667
|
return this._length === 0;
|
|
2053
2668
|
}
|
|
2054
2669
|
/**
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2670
|
+
* Remove all elements and reset structure.
|
|
2671
|
+
* @remarks Time O(1), Space O(1)
|
|
2672
|
+
* @returns void
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
|
|
2681
|
+
|
|
2682
|
+
* @example
|
|
2683
|
+
* // Remove all elements
|
|
2684
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2685
|
+
* dq.clear();
|
|
2686
|
+
* console.log(dq.length); // 0;
|
|
2687
|
+
*/
|
|
2059
2688
|
clear() {
|
|
2060
2689
|
this._buckets = [new Array(this._bucketSize)];
|
|
2061
2690
|
this._bucketCount = 1;
|
|
@@ -2063,11 +2692,24 @@ var queueTyped = (() => {
|
|
|
2063
2692
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2064
2693
|
}
|
|
2065
2694
|
/**
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2695
|
+
* Get the element at a given position.
|
|
2696
|
+
* @remarks Time O(1), Space O(1)
|
|
2697
|
+
* @param pos - Zero-based position from the front.
|
|
2698
|
+
* @returns Element or undefined.
|
|
2699
|
+
|
|
2700
|
+
|
|
2701
|
+
|
|
2702
|
+
|
|
2703
|
+
|
|
2704
|
+
|
|
2705
|
+
|
|
2706
|
+
|
|
2707
|
+
* @example
|
|
2708
|
+
* // Access by index
|
|
2709
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2710
|
+
* console.log(dq.at(0)); // 'a';
|
|
2711
|
+
* console.log(dq.at(2)); // 'c';
|
|
2712
|
+
*/
|
|
2071
2713
|
at(pos) {
|
|
2072
2714
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
2073
2715
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -2226,11 +2868,24 @@ var queueTyped = (() => {
|
|
|
2226
2868
|
}
|
|
2227
2869
|
}
|
|
2228
2870
|
/**
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2871
|
+
* Delete the first occurrence of a value.
|
|
2872
|
+
* @remarks Time O(N), Space O(1)
|
|
2873
|
+
* @param element - Element to remove (using the configured equality).
|
|
2874
|
+
* @returns True if an element was removed.
|
|
2875
|
+
|
|
2876
|
+
|
|
2877
|
+
|
|
2878
|
+
|
|
2879
|
+
|
|
2880
|
+
|
|
2881
|
+
|
|
2882
|
+
|
|
2883
|
+
* @example
|
|
2884
|
+
* // Remove element
|
|
2885
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2886
|
+
* dq.delete(2);
|
|
2887
|
+
* console.log(dq.length); // 2;
|
|
2888
|
+
*/
|
|
2234
2889
|
delete(element) {
|
|
2235
2890
|
const size = this._length;
|
|
2236
2891
|
if (size === 0) return false;
|
|
@@ -2274,10 +2929,39 @@ var queueTyped = (() => {
|
|
|
2274
2929
|
return this;
|
|
2275
2930
|
}
|
|
2276
2931
|
/**
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2932
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
2933
|
+
* @remarks Time O(N), Space O(N)
|
|
2934
|
+
* @returns This deque.
|
|
2935
|
+
|
|
2936
|
+
|
|
2937
|
+
|
|
2938
|
+
|
|
2939
|
+
|
|
2940
|
+
|
|
2941
|
+
|
|
2942
|
+
|
|
2943
|
+
|
|
2944
|
+
|
|
2945
|
+
|
|
2946
|
+
* @example
|
|
2947
|
+
* // Deque for...of iteration and reverse
|
|
2948
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2949
|
+
*
|
|
2950
|
+
* // Iterate forward
|
|
2951
|
+
* const forward: string[] = [];
|
|
2952
|
+
* for (const item of deque) {
|
|
2953
|
+
* forward.push(item);
|
|
2954
|
+
* }
|
|
2955
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2956
|
+
*
|
|
2957
|
+
* // Reverse the deque
|
|
2958
|
+
* deque.reverse();
|
|
2959
|
+
* const backward: string[] = [];
|
|
2960
|
+
* for (const item of deque) {
|
|
2961
|
+
* backward.push(item);
|
|
2962
|
+
* }
|
|
2963
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2964
|
+
*/
|
|
2281
2965
|
reverse() {
|
|
2282
2966
|
this._buckets.reverse().forEach(function(bucket) {
|
|
2283
2967
|
bucket.reverse();
|
|
@@ -2336,10 +3020,25 @@ var queueTyped = (() => {
|
|
|
2336
3020
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2337
3021
|
*/
|
|
2338
3022
|
/**
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
3023
|
+
* Compact the deque by removing unused buckets.
|
|
3024
|
+
* @remarks Time O(N), Space O(1)
|
|
3025
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
3026
|
+
|
|
3027
|
+
|
|
3028
|
+
|
|
3029
|
+
|
|
3030
|
+
|
|
3031
|
+
|
|
3032
|
+
|
|
3033
|
+
|
|
3034
|
+
* @example
|
|
3035
|
+
* // Reclaim memory
|
|
3036
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
3037
|
+
* dq.shift();
|
|
3038
|
+
* dq.shift();
|
|
3039
|
+
* dq.compact();
|
|
3040
|
+
* console.log(dq.length); // 3;
|
|
3041
|
+
*/
|
|
2343
3042
|
compact() {
|
|
2344
3043
|
const before = this._bucketCount;
|
|
2345
3044
|
this.shrinkToFit();
|
|
@@ -2367,10 +3066,26 @@ var queueTyped = (() => {
|
|
|
2367
3066
|
this._compactCounter = 0;
|
|
2368
3067
|
}
|
|
2369
3068
|
/**
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
3069
|
+
* Deep clone this deque, preserving options.
|
|
3070
|
+
* @remarks Time O(N), Space O(N)
|
|
3071
|
+
* @returns A new deque with the same content and options.
|
|
3072
|
+
|
|
3073
|
+
|
|
3074
|
+
|
|
3075
|
+
|
|
3076
|
+
|
|
3077
|
+
|
|
3078
|
+
|
|
3079
|
+
|
|
3080
|
+
|
|
3081
|
+
* @example
|
|
3082
|
+
* // Create independent copy
|
|
3083
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3084
|
+
* const copy = dq.clone();
|
|
3085
|
+
* copy.pop();
|
|
3086
|
+
* console.log(dq.length); // 3;
|
|
3087
|
+
* console.log(copy.length); // 2;
|
|
3088
|
+
*/
|
|
2374
3089
|
clone() {
|
|
2375
3090
|
return this._createLike(this, {
|
|
2376
3091
|
bucketSize: this.bucketSize,
|
|
@@ -2379,12 +3094,26 @@ var queueTyped = (() => {
|
|
|
2379
3094
|
});
|
|
2380
3095
|
}
|
|
2381
3096
|
/**
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
3097
|
+
* Filter elements into a new deque of the same class.
|
|
3098
|
+
* @remarks Time O(N), Space O(N)
|
|
3099
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
3100
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
3101
|
+
* @returns A new deque with kept elements.
|
|
3102
|
+
|
|
3103
|
+
|
|
3104
|
+
|
|
3105
|
+
|
|
3106
|
+
|
|
3107
|
+
|
|
3108
|
+
|
|
3109
|
+
|
|
3110
|
+
|
|
3111
|
+
* @example
|
|
3112
|
+
* // Filter elements
|
|
3113
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
3114
|
+
* const result = dq.filter(x => x > 2);
|
|
3115
|
+
* console.log(result.length); // 2;
|
|
3116
|
+
*/
|
|
2388
3117
|
filter(predicate, thisArg) {
|
|
2389
3118
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2390
3119
|
out._setBucketSize(this._bucketSize);
|
|
@@ -2413,15 +3142,28 @@ var queueTyped = (() => {
|
|
|
2413
3142
|
return out;
|
|
2414
3143
|
}
|
|
2415
3144
|
/**
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
3145
|
+
* Map elements into a new deque (possibly different element type).
|
|
3146
|
+
* @remarks Time O(N), Space O(N)
|
|
3147
|
+
* @template EM
|
|
3148
|
+
* @template RM
|
|
3149
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
3150
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
3151
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
3152
|
+
* @returns A new Deque with mapped elements.
|
|
3153
|
+
|
|
3154
|
+
|
|
3155
|
+
|
|
3156
|
+
|
|
3157
|
+
|
|
3158
|
+
|
|
3159
|
+
|
|
3160
|
+
|
|
3161
|
+
* @example
|
|
3162
|
+
* // Transform elements
|
|
3163
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3164
|
+
* const result = dq.map(x => x * 10);
|
|
3165
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
3166
|
+
*/
|
|
2425
3167
|
map(callback, options, thisArg) {
|
|
2426
3168
|
const out = this._createLike([], {
|
|
2427
3169
|
...options != null ? options : {},
|