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/esm/index.mjs
CHANGED
|
@@ -1,55 +1,6 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/common/error.ts
|
|
5
|
-
var ERR = {
|
|
6
|
-
// Range / index
|
|
7
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
8
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
9
|
-
// Type / argument
|
|
10
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
11
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
12
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
13
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
14
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
15
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
16
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
17
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
18
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
19
|
-
// State / operation
|
|
20
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
21
|
-
// Matrix
|
|
22
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
23
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
24
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
25
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
26
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// src/common/index.ts
|
|
30
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
31
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
32
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
33
|
-
return DFSOperation2;
|
|
34
|
-
})(DFSOperation || {});
|
|
35
|
-
var Range = class {
|
|
36
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
37
|
-
this.low = low;
|
|
38
|
-
this.high = high;
|
|
39
|
-
this.includeLow = includeLow;
|
|
40
|
-
this.includeHigh = includeHigh;
|
|
41
|
-
}
|
|
42
|
-
static {
|
|
43
|
-
__name(this, "Range");
|
|
44
|
-
}
|
|
45
|
-
// Determine whether a key is within the range
|
|
46
|
-
isInRange(key, comparator) {
|
|
47
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
48
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
49
|
-
return lowCheck && highCheck;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
4
|
// src/data-structures/base/iterable-element-base.ts
|
|
54
5
|
var IterableElementBase = class {
|
|
55
6
|
static {
|
|
@@ -68,7 +19,7 @@ var IterableElementBase = class {
|
|
|
68
19
|
if (options) {
|
|
69
20
|
const { toElementFn } = options;
|
|
70
21
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
71
|
-
else if (toElementFn) throw new TypeError(
|
|
22
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
72
23
|
}
|
|
73
24
|
}
|
|
74
25
|
/**
|
|
@@ -231,7 +182,7 @@ var IterableElementBase = class {
|
|
|
231
182
|
acc = initialValue;
|
|
232
183
|
} else {
|
|
233
184
|
const first = iter.next();
|
|
234
|
-
if (first.done) throw new TypeError(
|
|
185
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
235
186
|
acc = first.value;
|
|
236
187
|
index = 1;
|
|
237
188
|
}
|
|
@@ -772,11 +723,58 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
772
723
|
return list;
|
|
773
724
|
}
|
|
774
725
|
/**
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
726
|
+
* Append an element/node to the tail.
|
|
727
|
+
* @remarks Time O(1), Space O(1)
|
|
728
|
+
* @param elementOrNode - Element or node to append.
|
|
729
|
+
* @returns True when appended.
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
* @example
|
|
763
|
+
* // basic SinglyLinkedList creation and push operation
|
|
764
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
765
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
766
|
+
*
|
|
767
|
+
* // Verify the list maintains insertion order
|
|
768
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
769
|
+
*
|
|
770
|
+
* // Check length
|
|
771
|
+
* console.log(list.length); // 5;
|
|
772
|
+
*
|
|
773
|
+
* // Push a new element to the end
|
|
774
|
+
* list.push(6);
|
|
775
|
+
* console.log(list.length); // 6;
|
|
776
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
777
|
+
*/
|
|
780
778
|
push(elementOrNode) {
|
|
781
779
|
const newNode = this._ensureNode(elementOrNode);
|
|
782
780
|
if (!this.head) {
|
|
@@ -790,10 +788,57 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
790
788
|
return true;
|
|
791
789
|
}
|
|
792
790
|
/**
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
791
|
+
* Remove and return the tail element.
|
|
792
|
+
* @remarks Time O(N), Space O(1)
|
|
793
|
+
* @returns Removed element or undefined.
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
* @example
|
|
827
|
+
* // SinglyLinkedList pop and shift operations
|
|
828
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
829
|
+
*
|
|
830
|
+
* // Pop removes from the end
|
|
831
|
+
* const last = list.pop();
|
|
832
|
+
* console.log(last); // 50;
|
|
833
|
+
*
|
|
834
|
+
* // Shift removes from the beginning
|
|
835
|
+
* const first = list.shift();
|
|
836
|
+
* console.log(first); // 10;
|
|
837
|
+
*
|
|
838
|
+
* // Verify remaining elements
|
|
839
|
+
* console.log([...list]); // [20, 30, 40];
|
|
840
|
+
* console.log(list.length); // 3;
|
|
841
|
+
*/
|
|
797
842
|
pop() {
|
|
798
843
|
if (!this.head) return void 0;
|
|
799
844
|
if (this.head === this.tail) {
|
|
@@ -812,10 +857,47 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
812
857
|
return value;
|
|
813
858
|
}
|
|
814
859
|
/**
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
860
|
+
* Remove and return the head element.
|
|
861
|
+
* @remarks Time O(1), Space O(1)
|
|
862
|
+
* @returns Removed element or undefined.
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
* @example
|
|
896
|
+
* // Remove from the front
|
|
897
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
898
|
+
* console.log(list.shift()); // 10;
|
|
899
|
+
* console.log(list.length); // 2;
|
|
900
|
+
*/
|
|
819
901
|
shift() {
|
|
820
902
|
if (!this.head) return void 0;
|
|
821
903
|
const removed = this.head;
|
|
@@ -825,11 +907,63 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
825
907
|
return removed.value;
|
|
826
908
|
}
|
|
827
909
|
/**
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
910
|
+
* Prepend an element/node to the head.
|
|
911
|
+
* @remarks Time O(1), Space O(1)
|
|
912
|
+
* @param elementOrNode - Element or node to prepend.
|
|
913
|
+
* @returns True when prepended.
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
* @example
|
|
947
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
948
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
949
|
+
*
|
|
950
|
+
* // Unshift adds to the beginning
|
|
951
|
+
* list.unshift(10);
|
|
952
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
953
|
+
*
|
|
954
|
+
* // Access elements (forward traversal only for singly linked)
|
|
955
|
+
* const second = list.at(1);
|
|
956
|
+
* console.log(second); // 20;
|
|
957
|
+
*
|
|
958
|
+
* // SinglyLinkedList allows forward iteration only
|
|
959
|
+
* const elements: number[] = [];
|
|
960
|
+
* for (const item of list) {
|
|
961
|
+
* elements.push(item);
|
|
962
|
+
* }
|
|
963
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
964
|
+
*
|
|
965
|
+
* console.log(list.length); // 4;
|
|
966
|
+
*/
|
|
833
967
|
unshift(elementOrNode) {
|
|
834
968
|
const newNode = this._ensureNode(elementOrNode);
|
|
835
969
|
if (!this.head) {
|
|
@@ -885,11 +1019,49 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
885
1019
|
return void 0;
|
|
886
1020
|
}
|
|
887
1021
|
/**
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
1022
|
+
* Get the element at a given index.
|
|
1023
|
+
* @remarks Time O(N), Space O(1)
|
|
1024
|
+
* @param index - Zero-based index.
|
|
1025
|
+
* @returns Element or undefined.
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
* @example
|
|
1059
|
+
* // Access element by index
|
|
1060
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1061
|
+
* console.log(list.at(0)); // 'a';
|
|
1062
|
+
* console.log(list.at(2)); // 'c';
|
|
1063
|
+
* console.log(list.at(3)); // 'd';
|
|
1064
|
+
*/
|
|
893
1065
|
at(index) {
|
|
894
1066
|
if (index < 0 || index >= this._length) return void 0;
|
|
895
1067
|
let current = this.head;
|
|
@@ -906,11 +1078,44 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
906
1078
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
907
1079
|
}
|
|
908
1080
|
/**
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1081
|
+
* Get the node reference at a given index.
|
|
1082
|
+
* @remarks Time O(N), Space O(1)
|
|
1083
|
+
* @param index - Zero-based index.
|
|
1084
|
+
* @returns Node or undefined.
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
* @example
|
|
1115
|
+
* // Get node at index
|
|
1116
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1117
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1118
|
+
*/
|
|
914
1119
|
getNodeAt(index) {
|
|
915
1120
|
if (index < 0 || index >= this._length) return void 0;
|
|
916
1121
|
let current = this.head;
|
|
@@ -918,11 +1123,45 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
918
1123
|
return current;
|
|
919
1124
|
}
|
|
920
1125
|
/**
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
1126
|
+
* Delete the element at an index.
|
|
1127
|
+
* @remarks Time O(N), Space O(1)
|
|
1128
|
+
* @param index - Zero-based index.
|
|
1129
|
+
* @returns Removed element or undefined.
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
* @example
|
|
1160
|
+
* // Remove by index
|
|
1161
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1162
|
+
* list.deleteAt(1);
|
|
1163
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1164
|
+
*/
|
|
926
1165
|
deleteAt(index) {
|
|
927
1166
|
if (index < 0 || index >= this._length) return void 0;
|
|
928
1167
|
if (index === 0) return this.shift();
|
|
@@ -935,11 +1174,45 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
935
1174
|
return value;
|
|
936
1175
|
}
|
|
937
1176
|
/**
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
1177
|
+
* Delete the first match by value/node.
|
|
1178
|
+
* @remarks Time O(N), Space O(1)
|
|
1179
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1180
|
+
* @returns True if removed.
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
* @example
|
|
1211
|
+
* // Remove first occurrence
|
|
1212
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1213
|
+
* list.delete(2);
|
|
1214
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1215
|
+
*/
|
|
943
1216
|
delete(elementOrNode) {
|
|
944
1217
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
945
1218
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -956,12 +1229,46 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
956
1229
|
return true;
|
|
957
1230
|
}
|
|
958
1231
|
/**
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
1232
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1233
|
+
* @remarks Time O(N), Space O(1)
|
|
1234
|
+
* @param index - Zero-based index.
|
|
1235
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1236
|
+
* @returns True if inserted.
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
* @example
|
|
1267
|
+
* // Insert at index
|
|
1268
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1269
|
+
* list.addAt(1, 2);
|
|
1270
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1271
|
+
*/
|
|
965
1272
|
addAt(index, newElementOrNode) {
|
|
966
1273
|
if (index < 0 || index > this._length) return false;
|
|
967
1274
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -987,28 +1294,133 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
987
1294
|
return true;
|
|
988
1295
|
}
|
|
989
1296
|
/**
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
1297
|
+
* Check whether the list is empty.
|
|
1298
|
+
* @remarks Time O(1), Space O(1)
|
|
1299
|
+
* @returns True if length is 0.
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
* @example
|
|
1331
|
+
* // Check empty
|
|
1332
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1333
|
+
*/
|
|
994
1334
|
isEmpty() {
|
|
995
1335
|
return this._length === 0;
|
|
996
1336
|
}
|
|
997
1337
|
/**
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1338
|
+
* Remove all nodes and reset length.
|
|
1339
|
+
* @remarks Time O(N), Space O(1)
|
|
1340
|
+
* @returns void
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
* @example
|
|
1372
|
+
* // Remove all
|
|
1373
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1374
|
+
* list.clear();
|
|
1375
|
+
* console.log(list.isEmpty()); // true;
|
|
1376
|
+
*/
|
|
1002
1377
|
clear() {
|
|
1003
1378
|
this._head = void 0;
|
|
1004
1379
|
this._tail = void 0;
|
|
1005
1380
|
this._length = 0;
|
|
1006
1381
|
}
|
|
1007
1382
|
/**
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1383
|
+
* Reverse the list in place.
|
|
1384
|
+
* @remarks Time O(N), Space O(1)
|
|
1385
|
+
* @returns This list.
|
|
1386
|
+
|
|
1387
|
+
|
|
1388
|
+
|
|
1389
|
+
|
|
1390
|
+
|
|
1391
|
+
|
|
1392
|
+
|
|
1393
|
+
|
|
1394
|
+
|
|
1395
|
+
|
|
1396
|
+
|
|
1397
|
+
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
|
|
1405
|
+
|
|
1406
|
+
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
* @example
|
|
1419
|
+
* // Reverse the list in-place
|
|
1420
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1421
|
+
* list.reverse();
|
|
1422
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1423
|
+
*/
|
|
1012
1424
|
reverse() {
|
|
1013
1425
|
if (!this.head || this.head === this.tail) return this;
|
|
1014
1426
|
let prev;
|
|
@@ -1183,22 +1595,106 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1183
1595
|
return false;
|
|
1184
1596
|
}
|
|
1185
1597
|
/**
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1598
|
+
* Deep clone this list (values are copied by reference).
|
|
1599
|
+
* @remarks Time O(N), Space O(N)
|
|
1600
|
+
* @returns A new list with the same element sequence.
|
|
1601
|
+
|
|
1602
|
+
|
|
1603
|
+
|
|
1604
|
+
|
|
1605
|
+
|
|
1606
|
+
|
|
1607
|
+
|
|
1608
|
+
|
|
1609
|
+
|
|
1610
|
+
|
|
1611
|
+
|
|
1612
|
+
|
|
1613
|
+
|
|
1614
|
+
|
|
1615
|
+
|
|
1616
|
+
|
|
1617
|
+
|
|
1618
|
+
|
|
1619
|
+
|
|
1620
|
+
|
|
1621
|
+
|
|
1622
|
+
|
|
1623
|
+
|
|
1624
|
+
|
|
1625
|
+
|
|
1626
|
+
|
|
1627
|
+
|
|
1628
|
+
|
|
1629
|
+
|
|
1630
|
+
|
|
1631
|
+
* @example
|
|
1632
|
+
* // Deep copy
|
|
1633
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1634
|
+
* const copy = list.clone();
|
|
1635
|
+
* copy.pop();
|
|
1636
|
+
* console.log(list.length); // 3;
|
|
1637
|
+
* console.log(copy.length); // 2;
|
|
1638
|
+
*/
|
|
1190
1639
|
clone() {
|
|
1191
1640
|
const out = this._createInstance();
|
|
1192
1641
|
for (const v of this) out.push(v);
|
|
1193
1642
|
return out;
|
|
1194
1643
|
}
|
|
1195
1644
|
/**
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1645
|
+
* Filter values into a new list of the same class.
|
|
1646
|
+
* @remarks Time O(N), Space O(N)
|
|
1647
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1648
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1649
|
+
* @returns A new list with kept values.
|
|
1650
|
+
|
|
1651
|
+
|
|
1652
|
+
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
|
|
1656
|
+
|
|
1657
|
+
|
|
1658
|
+
|
|
1659
|
+
|
|
1660
|
+
|
|
1661
|
+
|
|
1662
|
+
|
|
1663
|
+
|
|
1664
|
+
|
|
1665
|
+
|
|
1666
|
+
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
|
|
1670
|
+
|
|
1671
|
+
|
|
1672
|
+
|
|
1673
|
+
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
|
|
1677
|
+
|
|
1678
|
+
|
|
1679
|
+
|
|
1680
|
+
|
|
1681
|
+
|
|
1682
|
+
* @example
|
|
1683
|
+
* // SinglyLinkedList filter and map operations
|
|
1684
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1685
|
+
*
|
|
1686
|
+
* // Filter even numbers
|
|
1687
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1688
|
+
* console.log(filtered.length); // 2;
|
|
1689
|
+
*
|
|
1690
|
+
* // Map to double values
|
|
1691
|
+
* const doubled = list.map(value => value * 2);
|
|
1692
|
+
* console.log(doubled.length); // 5;
|
|
1693
|
+
*
|
|
1694
|
+
* // Use reduce to sum
|
|
1695
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1696
|
+
* console.log(sum); // 15;
|
|
1697
|
+
*/
|
|
1202
1698
|
filter(callback, thisArg) {
|
|
1203
1699
|
const out = this._createInstance();
|
|
1204
1700
|
let index = 0;
|
|
@@ -1222,15 +1718,52 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1222
1718
|
return out;
|
|
1223
1719
|
}
|
|
1224
1720
|
/**
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1721
|
+
* Map values into a new list (possibly different element type).
|
|
1722
|
+
* @remarks Time O(N), Space O(N)
|
|
1723
|
+
* @template EM
|
|
1724
|
+
* @template RM
|
|
1725
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1726
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1727
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1728
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
|
|
1732
|
+
|
|
1733
|
+
|
|
1734
|
+
|
|
1735
|
+
|
|
1736
|
+
|
|
1737
|
+
|
|
1738
|
+
|
|
1739
|
+
|
|
1740
|
+
|
|
1741
|
+
|
|
1742
|
+
|
|
1743
|
+
|
|
1744
|
+
|
|
1745
|
+
|
|
1746
|
+
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
|
|
1750
|
+
|
|
1751
|
+
|
|
1752
|
+
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
|
|
1756
|
+
|
|
1757
|
+
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
* @example
|
|
1762
|
+
* // Transform elements
|
|
1763
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1764
|
+
* const doubled = list.map(n => n * 2);
|
|
1765
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1766
|
+
*/
|
|
1234
1767
|
map(callback, options, thisArg) {
|
|
1235
1768
|
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
1236
1769
|
let index = 0;
|
|
@@ -1365,6 +1898,55 @@ function elementOrPredicate(input, equals) {
|
|
|
1365
1898
|
}
|
|
1366
1899
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
1367
1900
|
|
|
1901
|
+
// src/common/error.ts
|
|
1902
|
+
var ERR = {
|
|
1903
|
+
// Range / index
|
|
1904
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1905
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1906
|
+
// Type / argument
|
|
1907
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1908
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1909
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1910
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1911
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1912
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1913
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1914
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1915
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1916
|
+
// State / operation
|
|
1917
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1918
|
+
// Matrix
|
|
1919
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1920
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1921
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1922
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1923
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1924
|
+
};
|
|
1925
|
+
|
|
1926
|
+
// src/common/index.ts
|
|
1927
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1928
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1929
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1930
|
+
return DFSOperation2;
|
|
1931
|
+
})(DFSOperation || {});
|
|
1932
|
+
var Range = class {
|
|
1933
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1934
|
+
this.low = low;
|
|
1935
|
+
this.high = high;
|
|
1936
|
+
this.includeLow = includeLow;
|
|
1937
|
+
this.includeHigh = includeHigh;
|
|
1938
|
+
}
|
|
1939
|
+
static {
|
|
1940
|
+
__name(this, "Range");
|
|
1941
|
+
}
|
|
1942
|
+
// Determine whether a key is within the range
|
|
1943
|
+
isInRange(key, comparator) {
|
|
1944
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1945
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1946
|
+
return lowCheck && highCheck;
|
|
1947
|
+
}
|
|
1948
|
+
};
|
|
1949
|
+
|
|
1368
1950
|
// src/data-structures/queue/queue.ts
|
|
1369
1951
|
var Queue = class _Queue extends LinearBase {
|
|
1370
1952
|
static {
|
|
@@ -1422,18 +2004,94 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1422
2004
|
this._autoCompactRatio = value;
|
|
1423
2005
|
}
|
|
1424
2006
|
/**
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
2007
|
+
* Get the number of elements currently in the queue.
|
|
2008
|
+
* @remarks Time O(1), Space O(1)
|
|
2009
|
+
* @returns Current length.
|
|
2010
|
+
|
|
2011
|
+
|
|
2012
|
+
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
|
|
2016
|
+
|
|
2017
|
+
|
|
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
|
+
* @example
|
|
2043
|
+
* // Track queue length
|
|
2044
|
+
* const q = new Queue<number>();
|
|
2045
|
+
* console.log(q.length); // 0;
|
|
2046
|
+
* q.push(1);
|
|
2047
|
+
* q.push(2);
|
|
2048
|
+
* console.log(q.length); // 2;
|
|
2049
|
+
*/
|
|
1429
2050
|
get length() {
|
|
1430
2051
|
return this.elements.length - this._offset;
|
|
1431
2052
|
}
|
|
1432
2053
|
/**
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
2054
|
+
* Get the first element (front) without removing it.
|
|
2055
|
+
* @remarks Time O(1), Space O(1)
|
|
2056
|
+
* @returns Front element or undefined.
|
|
2057
|
+
|
|
2058
|
+
|
|
2059
|
+
|
|
2060
|
+
|
|
2061
|
+
|
|
2062
|
+
|
|
2063
|
+
|
|
2064
|
+
|
|
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
|
+
* @example
|
|
2090
|
+
* // View the front element
|
|
2091
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2092
|
+
* console.log(q.first); // 'first';
|
|
2093
|
+
* console.log(q.length); // 3;
|
|
2094
|
+
*/
|
|
1437
2095
|
get first() {
|
|
1438
2096
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1439
2097
|
}
|
|
@@ -1456,19 +2114,111 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1456
2114
|
return new _Queue(elements);
|
|
1457
2115
|
}
|
|
1458
2116
|
/**
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
2117
|
+
* Check whether the queue is empty.
|
|
2118
|
+
* @remarks Time O(1), Space O(1)
|
|
2119
|
+
* @returns True if length is 0.
|
|
2120
|
+
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
|
|
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
|
+
* @example
|
|
2153
|
+
* // Queue for...of iteration and isEmpty check
|
|
2154
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2155
|
+
*
|
|
2156
|
+
* const elements: string[] = [];
|
|
2157
|
+
* for (const item of queue) {
|
|
2158
|
+
* elements.push(item);
|
|
2159
|
+
* }
|
|
2160
|
+
*
|
|
2161
|
+
* // Verify all elements are iterated in order
|
|
2162
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2163
|
+
*
|
|
2164
|
+
* // Process all elements
|
|
2165
|
+
* while (queue.length > 0) {
|
|
2166
|
+
* queue.shift();
|
|
2167
|
+
* }
|
|
2168
|
+
*
|
|
2169
|
+
* console.log(queue.length); // 0;
|
|
2170
|
+
*/
|
|
1463
2171
|
isEmpty() {
|
|
1464
2172
|
return this.length === 0;
|
|
1465
2173
|
}
|
|
1466
2174
|
/**
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
2175
|
+
* Enqueue one element at the back.
|
|
2176
|
+
* @remarks Time O(1), Space O(1)
|
|
2177
|
+
* @param element - Element to enqueue.
|
|
2178
|
+
* @returns True on success.
|
|
2179
|
+
|
|
2180
|
+
|
|
2181
|
+
|
|
2182
|
+
|
|
2183
|
+
|
|
2184
|
+
|
|
2185
|
+
|
|
2186
|
+
|
|
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
|
+
* @example
|
|
2212
|
+
* // basic Queue creation and push operation
|
|
2213
|
+
* // Create a simple Queue with initial values
|
|
2214
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2215
|
+
*
|
|
2216
|
+
* // Verify the queue maintains insertion order
|
|
2217
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2218
|
+
*
|
|
2219
|
+
* // Check length
|
|
2220
|
+
* console.log(queue.length); // 5;
|
|
2221
|
+
*/
|
|
1472
2222
|
push(element) {
|
|
1473
2223
|
this.elements.push(element);
|
|
1474
2224
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1489,10 +2239,56 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1489
2239
|
return ans;
|
|
1490
2240
|
}
|
|
1491
2241
|
/**
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
2242
|
+
* Dequeue one element from the front (amortized via offset).
|
|
2243
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2244
|
+
* @returns Removed element or undefined.
|
|
2245
|
+
|
|
2246
|
+
|
|
2247
|
+
|
|
2248
|
+
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
|
|
2252
|
+
|
|
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
|
+
* @example
|
|
2278
|
+
* // Queue shift and peek operations
|
|
2279
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2280
|
+
*
|
|
2281
|
+
* // Peek at the front element without removing it
|
|
2282
|
+
* console.log(queue.first); // 10;
|
|
2283
|
+
*
|
|
2284
|
+
* // Remove and get the first element (FIFO)
|
|
2285
|
+
* const first = queue.shift();
|
|
2286
|
+
* console.log(first); // 10;
|
|
2287
|
+
*
|
|
2288
|
+
* // Verify remaining elements and length decreased
|
|
2289
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
2290
|
+
* console.log(queue.length); // 3;
|
|
2291
|
+
*/
|
|
1496
2292
|
shift() {
|
|
1497
2293
|
if (this.length === 0) return void 0;
|
|
1498
2294
|
const first = this.first;
|
|
@@ -1501,11 +2297,45 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1501
2297
|
return first;
|
|
1502
2298
|
}
|
|
1503
2299
|
/**
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
2300
|
+
* Delete the first occurrence of a specific element.
|
|
2301
|
+
* @remarks Time O(N), Space O(1)
|
|
2302
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
2303
|
+
* @returns True if an element was removed.
|
|
2304
|
+
|
|
2305
|
+
|
|
2306
|
+
|
|
2307
|
+
|
|
2308
|
+
|
|
2309
|
+
|
|
2310
|
+
|
|
2311
|
+
|
|
2312
|
+
|
|
2313
|
+
|
|
2314
|
+
|
|
2315
|
+
|
|
2316
|
+
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
|
|
2320
|
+
|
|
2321
|
+
|
|
2322
|
+
|
|
2323
|
+
|
|
2324
|
+
|
|
2325
|
+
|
|
2326
|
+
|
|
2327
|
+
|
|
2328
|
+
|
|
2329
|
+
|
|
2330
|
+
|
|
2331
|
+
|
|
2332
|
+
|
|
2333
|
+
* @example
|
|
2334
|
+
* // Remove specific element
|
|
2335
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2336
|
+
* q.delete(2);
|
|
2337
|
+
* console.log(q.length); // 3;
|
|
2338
|
+
*/
|
|
1509
2339
|
delete(element) {
|
|
1510
2340
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1511
2341
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1516,11 +2346,45 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1516
2346
|
return false;
|
|
1517
2347
|
}
|
|
1518
2348
|
/**
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
2349
|
+
* Get the element at a given logical index.
|
|
2350
|
+
* @remarks Time O(1), Space O(1)
|
|
2351
|
+
* @param index - Zero-based index from the front.
|
|
2352
|
+
* @returns Element or undefined.
|
|
2353
|
+
|
|
2354
|
+
|
|
2355
|
+
|
|
2356
|
+
|
|
2357
|
+
|
|
2358
|
+
|
|
2359
|
+
|
|
2360
|
+
|
|
2361
|
+
|
|
2362
|
+
|
|
2363
|
+
|
|
2364
|
+
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
|
|
2368
|
+
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
|
|
2372
|
+
|
|
2373
|
+
|
|
2374
|
+
|
|
2375
|
+
|
|
2376
|
+
|
|
2377
|
+
|
|
2378
|
+
|
|
2379
|
+
|
|
2380
|
+
|
|
2381
|
+
|
|
2382
|
+
* @example
|
|
2383
|
+
* // Access element by index
|
|
2384
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2385
|
+
* console.log(q.at(0)); // 'a';
|
|
2386
|
+
* console.log(q.at(2)); // 'c';
|
|
2387
|
+
*/
|
|
1524
2388
|
at(index) {
|
|
1525
2389
|
if (index < 0 || index >= this.length) return void 0;
|
|
1526
2390
|
return this._elements[this._offset + index];
|
|
@@ -1572,19 +2436,90 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1572
2436
|
return this;
|
|
1573
2437
|
}
|
|
1574
2438
|
/**
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
2439
|
+
* Remove all elements and reset offset.
|
|
2440
|
+
* @remarks Time O(1), Space O(1)
|
|
2441
|
+
* @returns void
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
|
|
2448
|
+
|
|
2449
|
+
|
|
2450
|
+
|
|
2451
|
+
|
|
2452
|
+
|
|
2453
|
+
|
|
2454
|
+
|
|
2455
|
+
|
|
2456
|
+
|
|
2457
|
+
|
|
2458
|
+
|
|
2459
|
+
|
|
2460
|
+
|
|
2461
|
+
|
|
2462
|
+
|
|
2463
|
+
|
|
2464
|
+
|
|
2465
|
+
|
|
2466
|
+
|
|
2467
|
+
|
|
2468
|
+
|
|
2469
|
+
|
|
2470
|
+
|
|
2471
|
+
|
|
2472
|
+
* @example
|
|
2473
|
+
* // Remove all elements
|
|
2474
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2475
|
+
* q.clear();
|
|
2476
|
+
* console.log(q.length); // 0;
|
|
2477
|
+
*/
|
|
1579
2478
|
clear() {
|
|
1580
2479
|
this._elements = [];
|
|
1581
2480
|
this._offset = 0;
|
|
1582
2481
|
}
|
|
1583
2482
|
/**
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
2483
|
+
* Compact storage by discarding consumed head elements.
|
|
2484
|
+
* @remarks Time O(N), Space O(N)
|
|
2485
|
+
* @returns True when compaction performed.
|
|
2486
|
+
|
|
2487
|
+
|
|
2488
|
+
|
|
2489
|
+
|
|
2490
|
+
|
|
2491
|
+
|
|
2492
|
+
|
|
2493
|
+
|
|
2494
|
+
|
|
2495
|
+
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2501
|
+
|
|
2502
|
+
|
|
2503
|
+
|
|
2504
|
+
|
|
2505
|
+
|
|
2506
|
+
|
|
2507
|
+
|
|
2508
|
+
|
|
2509
|
+
|
|
2510
|
+
|
|
2511
|
+
|
|
2512
|
+
|
|
2513
|
+
|
|
2514
|
+
|
|
2515
|
+
* @example
|
|
2516
|
+
* // Reclaim unused memory
|
|
2517
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2518
|
+
* q.shift();
|
|
2519
|
+
* q.shift();
|
|
2520
|
+
* q.compact();
|
|
2521
|
+
* console.log(q.length); // 3;
|
|
2522
|
+
*/
|
|
1588
2523
|
compact() {
|
|
1589
2524
|
this._elements = this.elements.slice(this._offset);
|
|
1590
2525
|
this._offset = 0;
|
|
@@ -1610,10 +2545,47 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1610
2545
|
return removed;
|
|
1611
2546
|
}
|
|
1612
2547
|
/**
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
2548
|
+
* Deep clone this queue and its parameters.
|
|
2549
|
+
* @remarks Time O(N), Space O(N)
|
|
2550
|
+
* @returns A new queue with the same content and options.
|
|
2551
|
+
|
|
2552
|
+
|
|
2553
|
+
|
|
2554
|
+
|
|
2555
|
+
|
|
2556
|
+
|
|
2557
|
+
|
|
2558
|
+
|
|
2559
|
+
|
|
2560
|
+
|
|
2561
|
+
|
|
2562
|
+
|
|
2563
|
+
|
|
2564
|
+
|
|
2565
|
+
|
|
2566
|
+
|
|
2567
|
+
|
|
2568
|
+
|
|
2569
|
+
|
|
2570
|
+
|
|
2571
|
+
|
|
2572
|
+
|
|
2573
|
+
|
|
2574
|
+
|
|
2575
|
+
|
|
2576
|
+
|
|
2577
|
+
|
|
2578
|
+
|
|
2579
|
+
|
|
2580
|
+
|
|
2581
|
+
* @example
|
|
2582
|
+
* // Create independent copy
|
|
2583
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2584
|
+
* const copy = q.clone();
|
|
2585
|
+
* copy.shift();
|
|
2586
|
+
* console.log(q.length); // 3;
|
|
2587
|
+
* console.log(copy.length); // 2;
|
|
2588
|
+
*/
|
|
1617
2589
|
clone() {
|
|
1618
2590
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1619
2591
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1621,12 +2593,47 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1621
2593
|
return out;
|
|
1622
2594
|
}
|
|
1623
2595
|
/**
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
2596
|
+
* Filter elements into a new queue of the same class.
|
|
2597
|
+
* @remarks Time O(N), Space O(N)
|
|
2598
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2599
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2600
|
+
* @returns A new queue with kept elements.
|
|
2601
|
+
|
|
2602
|
+
|
|
2603
|
+
|
|
2604
|
+
|
|
2605
|
+
|
|
2606
|
+
|
|
2607
|
+
|
|
2608
|
+
|
|
2609
|
+
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2613
|
+
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
|
|
2617
|
+
|
|
2618
|
+
|
|
2619
|
+
|
|
2620
|
+
|
|
2621
|
+
|
|
2622
|
+
|
|
2623
|
+
|
|
2624
|
+
|
|
2625
|
+
|
|
2626
|
+
|
|
2627
|
+
|
|
2628
|
+
|
|
2629
|
+
|
|
2630
|
+
|
|
2631
|
+
* @example
|
|
2632
|
+
* // Filter elements
|
|
2633
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2634
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2635
|
+
* console.log(evens.length); // 2;
|
|
2636
|
+
*/
|
|
1630
2637
|
filter(predicate, thisArg) {
|
|
1631
2638
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1632
2639
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1638,15 +2645,49 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1638
2645
|
return out;
|
|
1639
2646
|
}
|
|
1640
2647
|
/**
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
2648
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2649
|
+
* @remarks Time O(N), Space O(N)
|
|
2650
|
+
* @template EM
|
|
2651
|
+
* @template RM
|
|
2652
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2653
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2654
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2655
|
+
* @returns A new Queue with mapped elements.
|
|
2656
|
+
|
|
2657
|
+
|
|
2658
|
+
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
|
|
2681
|
+
|
|
2682
|
+
|
|
2683
|
+
|
|
2684
|
+
|
|
2685
|
+
* @example
|
|
2686
|
+
* // Transform elements
|
|
2687
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2688
|
+
* const doubled = q.map(x => x * 2);
|
|
2689
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2690
|
+
*/
|
|
1650
2691
|
map(callback, options, thisArg) {
|
|
1651
2692
|
const out = new this.constructor([], {
|
|
1652
2693
|
toElementFn: options?.toElementFn,
|
|
@@ -1881,19 +2922,102 @@ var Deque = class extends LinearBase {
|
|
|
1881
2922
|
return this._length;
|
|
1882
2923
|
}
|
|
1883
2924
|
/**
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
2925
|
+
* Get the first element without removing it.
|
|
2926
|
+
* @remarks Time O(1), Space O(1)
|
|
2927
|
+
* @returns First element or undefined.
|
|
2928
|
+
|
|
2929
|
+
|
|
2930
|
+
|
|
2931
|
+
|
|
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
|
+
* @example
|
|
2961
|
+
* // Deque peek at both ends
|
|
2962
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2963
|
+
*
|
|
2964
|
+
* // Get first element without removing
|
|
2965
|
+
* const first = deque.at(0);
|
|
2966
|
+
* console.log(first); // 10;
|
|
2967
|
+
*
|
|
2968
|
+
* // Get last element without removing
|
|
2969
|
+
* const last = deque.at(deque.length - 1);
|
|
2970
|
+
* console.log(last); // 50;
|
|
2971
|
+
*
|
|
2972
|
+
* // Length unchanged
|
|
2973
|
+
* console.log(deque.length); // 5;
|
|
2974
|
+
*/
|
|
1888
2975
|
get first() {
|
|
1889
2976
|
if (this._length === 0) return;
|
|
1890
2977
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
1891
2978
|
}
|
|
1892
2979
|
/**
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
2980
|
+
* Get the last element without removing it.
|
|
2981
|
+
* @remarks Time O(1), Space O(1)
|
|
2982
|
+
* @returns Last element or undefined.
|
|
2983
|
+
|
|
2984
|
+
|
|
2985
|
+
|
|
2986
|
+
|
|
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
|
+
* @example
|
|
3016
|
+
* // Peek at the back element
|
|
3017
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3018
|
+
* console.log(dq.last); // 'c';
|
|
3019
|
+
* console.log(dq.first); // 'a';
|
|
3020
|
+
*/
|
|
1897
3021
|
get last() {
|
|
1898
3022
|
if (this._length === 0) return;
|
|
1899
3023
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1912,11 +3036,61 @@ var Deque = class extends LinearBase {
|
|
|
1912
3036
|
return new this(data, options);
|
|
1913
3037
|
}
|
|
1914
3038
|
/**
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
3039
|
+
* Append one element at the back.
|
|
3040
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3041
|
+
* @param element - Element to append.
|
|
3042
|
+
* @returns True when appended.
|
|
3043
|
+
|
|
3044
|
+
|
|
3045
|
+
|
|
3046
|
+
|
|
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
|
+
* @example
|
|
3076
|
+
* // basic Deque creation and push/pop operations
|
|
3077
|
+
* // Create a simple Deque with initial values
|
|
3078
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
3079
|
+
*
|
|
3080
|
+
* // Verify the deque maintains insertion order
|
|
3081
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3082
|
+
*
|
|
3083
|
+
* // Check length
|
|
3084
|
+
* console.log(deque.length); // 5;
|
|
3085
|
+
*
|
|
3086
|
+
* // Push to the end
|
|
3087
|
+
* deque.push(6);
|
|
3088
|
+
* console.log(deque.length); // 6;
|
|
3089
|
+
*
|
|
3090
|
+
* // Pop from the end
|
|
3091
|
+
* const last = deque.pop();
|
|
3092
|
+
* console.log(last); // 6;
|
|
3093
|
+
*/
|
|
1920
3094
|
push(element) {
|
|
1921
3095
|
if (this._length) {
|
|
1922
3096
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -1936,10 +3110,47 @@ var Deque = class extends LinearBase {
|
|
|
1936
3110
|
return true;
|
|
1937
3111
|
}
|
|
1938
3112
|
/**
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
3113
|
+
* Remove and return the last element.
|
|
3114
|
+
* @remarks Time O(1), Space O(1)
|
|
3115
|
+
* @returns Removed element or undefined.
|
|
3116
|
+
|
|
3117
|
+
|
|
3118
|
+
|
|
3119
|
+
|
|
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
|
+
* @example
|
|
3149
|
+
* // Remove from the back
|
|
3150
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3151
|
+
* console.log(dq.pop()); // 3;
|
|
3152
|
+
* console.log(dq.length); // 2;
|
|
3153
|
+
*/
|
|
1943
3154
|
pop() {
|
|
1944
3155
|
if (this._length === 0) return;
|
|
1945
3156
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1959,10 +3170,47 @@ var Deque = class extends LinearBase {
|
|
|
1959
3170
|
return element;
|
|
1960
3171
|
}
|
|
1961
3172
|
/**
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
3173
|
+
* Remove and return the first element.
|
|
3174
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3175
|
+
* @returns Removed element or undefined.
|
|
3176
|
+
|
|
3177
|
+
|
|
3178
|
+
|
|
3179
|
+
|
|
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
|
+
* @example
|
|
3209
|
+
* // Remove from the front
|
|
3210
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3211
|
+
* console.log(dq.shift()); // 1;
|
|
3212
|
+
* console.log(dq.length); // 2;
|
|
3213
|
+
*/
|
|
1966
3214
|
shift() {
|
|
1967
3215
|
if (this._length === 0) return;
|
|
1968
3216
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -1982,11 +3230,58 @@ var Deque = class extends LinearBase {
|
|
|
1982
3230
|
return element;
|
|
1983
3231
|
}
|
|
1984
3232
|
/**
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
3233
|
+
* Prepend one element at the front.
|
|
3234
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3235
|
+
* @param element - Element to prepend.
|
|
3236
|
+
* @returns True when prepended.
|
|
3237
|
+
|
|
3238
|
+
|
|
3239
|
+
|
|
3240
|
+
|
|
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
|
+
* @example
|
|
3270
|
+
* // Deque shift and unshift operations
|
|
3271
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
3272
|
+
*
|
|
3273
|
+
* // Unshift adds to the front
|
|
3274
|
+
* deque.unshift(10);
|
|
3275
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3276
|
+
*
|
|
3277
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
3278
|
+
* const first = deque.shift();
|
|
3279
|
+
* console.log(first); // 10;
|
|
3280
|
+
*
|
|
3281
|
+
* // Verify remaining elements
|
|
3282
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
3283
|
+
* console.log(deque.length); // 3;
|
|
3284
|
+
*/
|
|
1990
3285
|
unshift(element) {
|
|
1991
3286
|
if (this._length) {
|
|
1992
3287
|
if (this._firstInBucket > 0) {
|
|
@@ -2040,18 +3335,87 @@ var Deque = class extends LinearBase {
|
|
|
2040
3335
|
return ans;
|
|
2041
3336
|
}
|
|
2042
3337
|
/**
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
3338
|
+
* Check whether the deque is empty.
|
|
3339
|
+
* @remarks Time O(1), Space O(1)
|
|
3340
|
+
* @returns True if length is 0.
|
|
3341
|
+
|
|
3342
|
+
|
|
3343
|
+
|
|
3344
|
+
|
|
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
|
+
* @example
|
|
3372
|
+
* // Check if empty
|
|
3373
|
+
* const dq = new Deque();
|
|
3374
|
+
* console.log(dq.isEmpty()); // true;
|
|
3375
|
+
*/
|
|
2047
3376
|
isEmpty() {
|
|
2048
3377
|
return this._length === 0;
|
|
2049
3378
|
}
|
|
2050
3379
|
/**
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
3380
|
+
* Remove all elements and reset structure.
|
|
3381
|
+
* @remarks Time O(1), Space O(1)
|
|
3382
|
+
* @returns void
|
|
3383
|
+
|
|
3384
|
+
|
|
3385
|
+
|
|
3386
|
+
|
|
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
|
+
* @example
|
|
3414
|
+
* // Remove all elements
|
|
3415
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3416
|
+
* dq.clear();
|
|
3417
|
+
* console.log(dq.length); // 0;
|
|
3418
|
+
*/
|
|
2055
3419
|
clear() {
|
|
2056
3420
|
this._buckets = [new Array(this._bucketSize)];
|
|
2057
3421
|
this._bucketCount = 1;
|
|
@@ -2059,11 +3423,45 @@ var Deque = class extends LinearBase {
|
|
|
2059
3423
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2060
3424
|
}
|
|
2061
3425
|
/**
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
3426
|
+
* Get the element at a given position.
|
|
3427
|
+
* @remarks Time O(1), Space O(1)
|
|
3428
|
+
* @param pos - Zero-based position from the front.
|
|
3429
|
+
* @returns Element or undefined.
|
|
3430
|
+
|
|
3431
|
+
|
|
3432
|
+
|
|
3433
|
+
|
|
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
|
+
* @example
|
|
3460
|
+
* // Access by index
|
|
3461
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3462
|
+
* console.log(dq.at(0)); // 'a';
|
|
3463
|
+
* console.log(dq.at(2)); // 'c';
|
|
3464
|
+
*/
|
|
2067
3465
|
at(pos) {
|
|
2068
3466
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
2069
3467
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -2222,11 +3620,45 @@ var Deque = class extends LinearBase {
|
|
|
2222
3620
|
}
|
|
2223
3621
|
}
|
|
2224
3622
|
/**
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
3623
|
+
* Delete the first occurrence of a value.
|
|
3624
|
+
* @remarks Time O(N), Space O(1)
|
|
3625
|
+
* @param element - Element to remove (using the configured equality).
|
|
3626
|
+
* @returns True if an element was removed.
|
|
3627
|
+
|
|
3628
|
+
|
|
3629
|
+
|
|
3630
|
+
|
|
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
|
+
* @example
|
|
3657
|
+
* // Remove element
|
|
3658
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3659
|
+
* dq.delete(2);
|
|
3660
|
+
* console.log(dq.length); // 2;
|
|
3661
|
+
*/
|
|
2230
3662
|
delete(element) {
|
|
2231
3663
|
const size = this._length;
|
|
2232
3664
|
if (size === 0) return false;
|
|
@@ -2270,10 +3702,60 @@ var Deque = class extends LinearBase {
|
|
|
2270
3702
|
return this;
|
|
2271
3703
|
}
|
|
2272
3704
|
/**
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
3705
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
3706
|
+
* @remarks Time O(N), Space O(N)
|
|
3707
|
+
* @returns This deque.
|
|
3708
|
+
|
|
3709
|
+
|
|
3710
|
+
|
|
3711
|
+
|
|
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
|
+
* @example
|
|
3741
|
+
* // Deque for...of iteration and reverse
|
|
3742
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
3743
|
+
*
|
|
3744
|
+
* // Iterate forward
|
|
3745
|
+
* const forward: string[] = [];
|
|
3746
|
+
* for (const item of deque) {
|
|
3747
|
+
* forward.push(item);
|
|
3748
|
+
* }
|
|
3749
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
3750
|
+
*
|
|
3751
|
+
* // Reverse the deque
|
|
3752
|
+
* deque.reverse();
|
|
3753
|
+
* const backward: string[] = [];
|
|
3754
|
+
* for (const item of deque) {
|
|
3755
|
+
* backward.push(item);
|
|
3756
|
+
* }
|
|
3757
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
3758
|
+
*/
|
|
2277
3759
|
reverse() {
|
|
2278
3760
|
this._buckets.reverse().forEach(function(bucket) {
|
|
2279
3761
|
bucket.reverse();
|
|
@@ -2332,10 +3814,46 @@ var Deque = class extends LinearBase {
|
|
|
2332
3814
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2333
3815
|
*/
|
|
2334
3816
|
/**
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
3817
|
+
* Compact the deque by removing unused buckets.
|
|
3818
|
+
* @remarks Time O(N), Space O(1)
|
|
3819
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
3820
|
+
|
|
3821
|
+
|
|
3822
|
+
|
|
3823
|
+
|
|
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
|
+
* @example
|
|
3850
|
+
* // Reclaim memory
|
|
3851
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
3852
|
+
* dq.shift();
|
|
3853
|
+
* dq.shift();
|
|
3854
|
+
* dq.compact();
|
|
3855
|
+
* console.log(dq.length); // 3;
|
|
3856
|
+
*/
|
|
2339
3857
|
compact() {
|
|
2340
3858
|
const before = this._bucketCount;
|
|
2341
3859
|
this.shrinkToFit();
|
|
@@ -2363,10 +3881,47 @@ var Deque = class extends LinearBase {
|
|
|
2363
3881
|
this._compactCounter = 0;
|
|
2364
3882
|
}
|
|
2365
3883
|
/**
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
3884
|
+
* Deep clone this deque, preserving options.
|
|
3885
|
+
* @remarks Time O(N), Space O(N)
|
|
3886
|
+
* @returns A new deque with the same content and options.
|
|
3887
|
+
|
|
3888
|
+
|
|
3889
|
+
|
|
3890
|
+
|
|
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
|
+
* @example
|
|
3918
|
+
* // Create independent copy
|
|
3919
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3920
|
+
* const copy = dq.clone();
|
|
3921
|
+
* copy.pop();
|
|
3922
|
+
* console.log(dq.length); // 3;
|
|
3923
|
+
* console.log(copy.length); // 2;
|
|
3924
|
+
*/
|
|
2370
3925
|
clone() {
|
|
2371
3926
|
return this._createLike(this, {
|
|
2372
3927
|
bucketSize: this.bucketSize,
|
|
@@ -2375,12 +3930,47 @@ var Deque = class extends LinearBase {
|
|
|
2375
3930
|
});
|
|
2376
3931
|
}
|
|
2377
3932
|
/**
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
3933
|
+
* Filter elements into a new deque of the same class.
|
|
3934
|
+
* @remarks Time O(N), Space O(N)
|
|
3935
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
3936
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
3937
|
+
* @returns A new deque with kept elements.
|
|
3938
|
+
|
|
3939
|
+
|
|
3940
|
+
|
|
3941
|
+
|
|
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
|
+
* @example
|
|
3969
|
+
* // Filter elements
|
|
3970
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
3971
|
+
* const result = dq.filter(x => x > 2);
|
|
3972
|
+
* console.log(result.length); // 2;
|
|
3973
|
+
*/
|
|
2384
3974
|
filter(predicate, thisArg) {
|
|
2385
3975
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2386
3976
|
out._setBucketSize(this._bucketSize);
|
|
@@ -2409,15 +3999,49 @@ var Deque = class extends LinearBase {
|
|
|
2409
3999
|
return out;
|
|
2410
4000
|
}
|
|
2411
4001
|
/**
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
4002
|
+
* Map elements into a new deque (possibly different element type).
|
|
4003
|
+
* @remarks Time O(N), Space O(N)
|
|
4004
|
+
* @template EM
|
|
4005
|
+
* @template RM
|
|
4006
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
4007
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
4008
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
4009
|
+
* @returns A new Deque with mapped elements.
|
|
4010
|
+
|
|
4011
|
+
|
|
4012
|
+
|
|
4013
|
+
|
|
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
|
+
* @example
|
|
4040
|
+
* // Transform elements
|
|
4041
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
4042
|
+
* const result = dq.map(x => x * 10);
|
|
4043
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
4044
|
+
*/
|
|
2421
4045
|
map(callback, options, thisArg) {
|
|
2422
4046
|
const out = this._createLike([], {
|
|
2423
4047
|
...options ?? {},
|