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/cjs/index.cjs
CHANGED
|
@@ -3,55 +3,6 @@
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
|
|
6
|
-
// src/common/error.ts
|
|
7
|
-
var ERR = {
|
|
8
|
-
// Range / index
|
|
9
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
10
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
11
|
-
// Type / argument
|
|
12
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
13
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
14
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
15
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
16
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
17
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
18
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
19
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
20
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
21
|
-
// State / operation
|
|
22
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
23
|
-
// Matrix
|
|
24
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
25
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
26
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
27
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
28
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
// src/common/index.ts
|
|
32
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
33
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
34
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
35
|
-
return DFSOperation2;
|
|
36
|
-
})(DFSOperation || {});
|
|
37
|
-
var Range = class {
|
|
38
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
39
|
-
this.low = low;
|
|
40
|
-
this.high = high;
|
|
41
|
-
this.includeLow = includeLow;
|
|
42
|
-
this.includeHigh = includeHigh;
|
|
43
|
-
}
|
|
44
|
-
static {
|
|
45
|
-
__name(this, "Range");
|
|
46
|
-
}
|
|
47
|
-
// Determine whether a key is within the range
|
|
48
|
-
isInRange(key, comparator) {
|
|
49
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
50
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
51
|
-
return lowCheck && highCheck;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
6
|
// src/data-structures/base/iterable-element-base.ts
|
|
56
7
|
var IterableElementBase = class {
|
|
57
8
|
static {
|
|
@@ -70,7 +21,7 @@ var IterableElementBase = class {
|
|
|
70
21
|
if (options) {
|
|
71
22
|
const { toElementFn } = options;
|
|
72
23
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
73
|
-
else if (toElementFn) throw new TypeError(
|
|
24
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
74
25
|
}
|
|
75
26
|
}
|
|
76
27
|
/**
|
|
@@ -233,7 +184,7 @@ var IterableElementBase = class {
|
|
|
233
184
|
acc = initialValue;
|
|
234
185
|
} else {
|
|
235
186
|
const first = iter.next();
|
|
236
|
-
if (first.done) throw new TypeError(
|
|
187
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
237
188
|
acc = first.value;
|
|
238
189
|
index = 1;
|
|
239
190
|
}
|
|
@@ -774,11 +725,58 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
774
725
|
return list;
|
|
775
726
|
}
|
|
776
727
|
/**
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
728
|
+
* Append an element/node to the tail.
|
|
729
|
+
* @remarks Time O(1), Space O(1)
|
|
730
|
+
* @param elementOrNode - Element or node to append.
|
|
731
|
+
* @returns True when appended.
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
* @example
|
|
765
|
+
* // basic SinglyLinkedList creation and push operation
|
|
766
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
767
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
768
|
+
*
|
|
769
|
+
* // Verify the list maintains insertion order
|
|
770
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
771
|
+
*
|
|
772
|
+
* // Check length
|
|
773
|
+
* console.log(list.length); // 5;
|
|
774
|
+
*
|
|
775
|
+
* // Push a new element to the end
|
|
776
|
+
* list.push(6);
|
|
777
|
+
* console.log(list.length); // 6;
|
|
778
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
779
|
+
*/
|
|
782
780
|
push(elementOrNode) {
|
|
783
781
|
const newNode = this._ensureNode(elementOrNode);
|
|
784
782
|
if (!this.head) {
|
|
@@ -792,10 +790,57 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
792
790
|
return true;
|
|
793
791
|
}
|
|
794
792
|
/**
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
793
|
+
* Remove and return the tail element.
|
|
794
|
+
* @remarks Time O(N), Space O(1)
|
|
795
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
827
|
+
|
|
828
|
+
* @example
|
|
829
|
+
* // SinglyLinkedList pop and shift operations
|
|
830
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
831
|
+
*
|
|
832
|
+
* // Pop removes from the end
|
|
833
|
+
* const last = list.pop();
|
|
834
|
+
* console.log(last); // 50;
|
|
835
|
+
*
|
|
836
|
+
* // Shift removes from the beginning
|
|
837
|
+
* const first = list.shift();
|
|
838
|
+
* console.log(first); // 10;
|
|
839
|
+
*
|
|
840
|
+
* // Verify remaining elements
|
|
841
|
+
* console.log([...list]); // [20, 30, 40];
|
|
842
|
+
* console.log(list.length); // 3;
|
|
843
|
+
*/
|
|
799
844
|
pop() {
|
|
800
845
|
if (!this.head) return void 0;
|
|
801
846
|
if (this.head === this.tail) {
|
|
@@ -814,10 +859,47 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
814
859
|
return value;
|
|
815
860
|
}
|
|
816
861
|
/**
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
862
|
+
* Remove and return the head element.
|
|
863
|
+
* @remarks Time O(1), Space O(1)
|
|
864
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
896
|
+
|
|
897
|
+
* @example
|
|
898
|
+
* // Remove from the front
|
|
899
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
900
|
+
* console.log(list.shift()); // 10;
|
|
901
|
+
* console.log(list.length); // 2;
|
|
902
|
+
*/
|
|
821
903
|
shift() {
|
|
822
904
|
if (!this.head) return void 0;
|
|
823
905
|
const removed = this.head;
|
|
@@ -827,11 +909,63 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
827
909
|
return removed.value;
|
|
828
910
|
}
|
|
829
911
|
/**
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
912
|
+
* Prepend an element/node to the head.
|
|
913
|
+
* @remarks Time O(1), Space O(1)
|
|
914
|
+
* @param elementOrNode - Element or node to prepend.
|
|
915
|
+
* @returns True when prepended.
|
|
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
|
+
|
|
947
|
+
|
|
948
|
+
* @example
|
|
949
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
950
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
951
|
+
*
|
|
952
|
+
* // Unshift adds to the beginning
|
|
953
|
+
* list.unshift(10);
|
|
954
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
955
|
+
*
|
|
956
|
+
* // Access elements (forward traversal only for singly linked)
|
|
957
|
+
* const second = list.at(1);
|
|
958
|
+
* console.log(second); // 20;
|
|
959
|
+
*
|
|
960
|
+
* // SinglyLinkedList allows forward iteration only
|
|
961
|
+
* const elements: number[] = [];
|
|
962
|
+
* for (const item of list) {
|
|
963
|
+
* elements.push(item);
|
|
964
|
+
* }
|
|
965
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
966
|
+
*
|
|
967
|
+
* console.log(list.length); // 4;
|
|
968
|
+
*/
|
|
835
969
|
unshift(elementOrNode) {
|
|
836
970
|
const newNode = this._ensureNode(elementOrNode);
|
|
837
971
|
if (!this.head) {
|
|
@@ -887,11 +1021,49 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
887
1021
|
return void 0;
|
|
888
1022
|
}
|
|
889
1023
|
/**
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
1024
|
+
* Get the element at a given index.
|
|
1025
|
+
* @remarks Time O(N), Space O(1)
|
|
1026
|
+
* @param index - Zero-based index.
|
|
1027
|
+
* @returns Element or undefined.
|
|
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
|
+
|
|
1059
|
+
|
|
1060
|
+
* @example
|
|
1061
|
+
* // Access element by index
|
|
1062
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1063
|
+
* console.log(list.at(0)); // 'a';
|
|
1064
|
+
* console.log(list.at(2)); // 'c';
|
|
1065
|
+
* console.log(list.at(3)); // 'd';
|
|
1066
|
+
*/
|
|
895
1067
|
at(index) {
|
|
896
1068
|
if (index < 0 || index >= this._length) return void 0;
|
|
897
1069
|
let current = this.head;
|
|
@@ -908,11 +1080,44 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
908
1080
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
909
1081
|
}
|
|
910
1082
|
/**
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
1083
|
+
* Get the node reference at a given index.
|
|
1084
|
+
* @remarks Time O(N), Space O(1)
|
|
1085
|
+
* @param index - Zero-based index.
|
|
1086
|
+
* @returns Node or undefined.
|
|
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
|
+
|
|
1115
|
+
|
|
1116
|
+
* @example
|
|
1117
|
+
* // Get node at index
|
|
1118
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1119
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1120
|
+
*/
|
|
916
1121
|
getNodeAt(index) {
|
|
917
1122
|
if (index < 0 || index >= this._length) return void 0;
|
|
918
1123
|
let current = this.head;
|
|
@@ -920,11 +1125,45 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
920
1125
|
return current;
|
|
921
1126
|
}
|
|
922
1127
|
/**
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
1128
|
+
* Delete the element at an index.
|
|
1129
|
+
* @remarks Time O(N), Space O(1)
|
|
1130
|
+
* @param index - Zero-based index.
|
|
1131
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
1160
|
+
|
|
1161
|
+
* @example
|
|
1162
|
+
* // Remove by index
|
|
1163
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1164
|
+
* list.deleteAt(1);
|
|
1165
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1166
|
+
*/
|
|
928
1167
|
deleteAt(index) {
|
|
929
1168
|
if (index < 0 || index >= this._length) return void 0;
|
|
930
1169
|
if (index === 0) return this.shift();
|
|
@@ -937,11 +1176,45 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
937
1176
|
return value;
|
|
938
1177
|
}
|
|
939
1178
|
/**
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
1179
|
+
* Delete the first match by value/node.
|
|
1180
|
+
* @remarks Time O(N), Space O(1)
|
|
1181
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1182
|
+
* @returns True if removed.
|
|
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
|
+
|
|
1211
|
+
|
|
1212
|
+
* @example
|
|
1213
|
+
* // Remove first occurrence
|
|
1214
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1215
|
+
* list.delete(2);
|
|
1216
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1217
|
+
*/
|
|
945
1218
|
delete(elementOrNode) {
|
|
946
1219
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
947
1220
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -958,12 +1231,46 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
958
1231
|
return true;
|
|
959
1232
|
}
|
|
960
1233
|
/**
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
1234
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1235
|
+
* @remarks Time O(N), Space O(1)
|
|
1236
|
+
* @param index - Zero-based index.
|
|
1237
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1238
|
+
* @returns True if inserted.
|
|
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
|
+
|
|
1267
|
+
|
|
1268
|
+
* @example
|
|
1269
|
+
* // Insert at index
|
|
1270
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1271
|
+
* list.addAt(1, 2);
|
|
1272
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1273
|
+
*/
|
|
967
1274
|
addAt(index, newElementOrNode) {
|
|
968
1275
|
if (index < 0 || index > this._length) return false;
|
|
969
1276
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -989,28 +1296,133 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
989
1296
|
return true;
|
|
990
1297
|
}
|
|
991
1298
|
/**
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
1299
|
+
* Check whether the list is empty.
|
|
1300
|
+
* @remarks Time O(1), Space O(1)
|
|
1301
|
+
* @returns True if length is 0.
|
|
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
|
+
|
|
1331
|
+
|
|
1332
|
+
* @example
|
|
1333
|
+
* // Check empty
|
|
1334
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1335
|
+
*/
|
|
996
1336
|
isEmpty() {
|
|
997
1337
|
return this._length === 0;
|
|
998
1338
|
}
|
|
999
1339
|
/**
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1340
|
+
* Remove all nodes and reset length.
|
|
1341
|
+
* @remarks Time O(N), Space O(1)
|
|
1342
|
+
* @returns void
|
|
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
|
+
|
|
1372
|
+
|
|
1373
|
+
* @example
|
|
1374
|
+
* // Remove all
|
|
1375
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1376
|
+
* list.clear();
|
|
1377
|
+
* console.log(list.isEmpty()); // true;
|
|
1378
|
+
*/
|
|
1004
1379
|
clear() {
|
|
1005
1380
|
this._head = void 0;
|
|
1006
1381
|
this._tail = void 0;
|
|
1007
1382
|
this._length = 0;
|
|
1008
1383
|
}
|
|
1009
1384
|
/**
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1385
|
+
* Reverse the list in place.
|
|
1386
|
+
* @remarks Time O(N), Space O(1)
|
|
1387
|
+
* @returns This list.
|
|
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
|
+
|
|
1419
|
+
|
|
1420
|
+
* @example
|
|
1421
|
+
* // Reverse the list in-place
|
|
1422
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1423
|
+
* list.reverse();
|
|
1424
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1425
|
+
*/
|
|
1014
1426
|
reverse() {
|
|
1015
1427
|
if (!this.head || this.head === this.tail) return this;
|
|
1016
1428
|
let prev;
|
|
@@ -1185,22 +1597,106 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1185
1597
|
return false;
|
|
1186
1598
|
}
|
|
1187
1599
|
/**
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1600
|
+
* Deep clone this list (values are copied by reference).
|
|
1601
|
+
* @remarks Time O(N), Space O(N)
|
|
1602
|
+
* @returns A new list with the same element sequence.
|
|
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
|
+
|
|
1632
|
+
|
|
1633
|
+
* @example
|
|
1634
|
+
* // Deep copy
|
|
1635
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1636
|
+
* const copy = list.clone();
|
|
1637
|
+
* copy.pop();
|
|
1638
|
+
* console.log(list.length); // 3;
|
|
1639
|
+
* console.log(copy.length); // 2;
|
|
1640
|
+
*/
|
|
1192
1641
|
clone() {
|
|
1193
1642
|
const out = this._createInstance();
|
|
1194
1643
|
for (const v of this) out.push(v);
|
|
1195
1644
|
return out;
|
|
1196
1645
|
}
|
|
1197
1646
|
/**
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1647
|
+
* Filter values into a new list of the same class.
|
|
1648
|
+
* @remarks Time O(N), Space O(N)
|
|
1649
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1650
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1651
|
+
* @returns A new list with kept values.
|
|
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
|
+
|
|
1683
|
+
|
|
1684
|
+
* @example
|
|
1685
|
+
* // SinglyLinkedList filter and map operations
|
|
1686
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1687
|
+
*
|
|
1688
|
+
* // Filter even numbers
|
|
1689
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1690
|
+
* console.log(filtered.length); // 2;
|
|
1691
|
+
*
|
|
1692
|
+
* // Map to double values
|
|
1693
|
+
* const doubled = list.map(value => value * 2);
|
|
1694
|
+
* console.log(doubled.length); // 5;
|
|
1695
|
+
*
|
|
1696
|
+
* // Use reduce to sum
|
|
1697
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1698
|
+
* console.log(sum); // 15;
|
|
1699
|
+
*/
|
|
1204
1700
|
filter(callback, thisArg) {
|
|
1205
1701
|
const out = this._createInstance();
|
|
1206
1702
|
let index = 0;
|
|
@@ -1224,15 +1720,52 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1224
1720
|
return out;
|
|
1225
1721
|
}
|
|
1226
1722
|
/**
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1723
|
+
* Map values into a new list (possibly different element type).
|
|
1724
|
+
* @remarks Time O(N), Space O(N)
|
|
1725
|
+
* @template EM
|
|
1726
|
+
* @template RM
|
|
1727
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1728
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1729
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1730
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
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
|
+
|
|
1762
|
+
|
|
1763
|
+
* @example
|
|
1764
|
+
* // Transform elements
|
|
1765
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1766
|
+
* const doubled = list.map(n => n * 2);
|
|
1767
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1768
|
+
*/
|
|
1236
1769
|
map(callback, options, thisArg) {
|
|
1237
1770
|
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
1238
1771
|
let index = 0;
|
|
@@ -1367,6 +1900,55 @@ function elementOrPredicate(input, equals) {
|
|
|
1367
1900
|
}
|
|
1368
1901
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
1369
1902
|
|
|
1903
|
+
// src/common/error.ts
|
|
1904
|
+
var ERR = {
|
|
1905
|
+
// Range / index
|
|
1906
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1907
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1908
|
+
// Type / argument
|
|
1909
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1910
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1911
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1912
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1913
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1914
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1915
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1916
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1917
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1918
|
+
// State / operation
|
|
1919
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1920
|
+
// Matrix
|
|
1921
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1922
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1923
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1924
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1925
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1926
|
+
};
|
|
1927
|
+
|
|
1928
|
+
// src/common/index.ts
|
|
1929
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1930
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1931
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1932
|
+
return DFSOperation2;
|
|
1933
|
+
})(DFSOperation || {});
|
|
1934
|
+
var Range = class {
|
|
1935
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1936
|
+
this.low = low;
|
|
1937
|
+
this.high = high;
|
|
1938
|
+
this.includeLow = includeLow;
|
|
1939
|
+
this.includeHigh = includeHigh;
|
|
1940
|
+
}
|
|
1941
|
+
static {
|
|
1942
|
+
__name(this, "Range");
|
|
1943
|
+
}
|
|
1944
|
+
// Determine whether a key is within the range
|
|
1945
|
+
isInRange(key, comparator) {
|
|
1946
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1947
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1948
|
+
return lowCheck && highCheck;
|
|
1949
|
+
}
|
|
1950
|
+
};
|
|
1951
|
+
|
|
1370
1952
|
// src/data-structures/queue/queue.ts
|
|
1371
1953
|
var Queue = class _Queue extends LinearBase {
|
|
1372
1954
|
static {
|
|
@@ -1424,18 +2006,94 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1424
2006
|
this._autoCompactRatio = value;
|
|
1425
2007
|
}
|
|
1426
2008
|
/**
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
2009
|
+
* Get the number of elements currently in the queue.
|
|
2010
|
+
* @remarks Time O(1), Space O(1)
|
|
2011
|
+
* @returns Current length.
|
|
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
|
+
|
|
2043
|
+
|
|
2044
|
+
* @example
|
|
2045
|
+
* // Track queue length
|
|
2046
|
+
* const q = new Queue<number>();
|
|
2047
|
+
* console.log(q.length); // 0;
|
|
2048
|
+
* q.push(1);
|
|
2049
|
+
* q.push(2);
|
|
2050
|
+
* console.log(q.length); // 2;
|
|
2051
|
+
*/
|
|
1431
2052
|
get length() {
|
|
1432
2053
|
return this.elements.length - this._offset;
|
|
1433
2054
|
}
|
|
1434
2055
|
/**
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
2056
|
+
* Get the first element (front) without removing it.
|
|
2057
|
+
* @remarks Time O(1), Space O(1)
|
|
2058
|
+
* @returns Front element or undefined.
|
|
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
|
+
|
|
2090
|
+
|
|
2091
|
+
* @example
|
|
2092
|
+
* // View the front element
|
|
2093
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2094
|
+
* console.log(q.first); // 'first';
|
|
2095
|
+
* console.log(q.length); // 3;
|
|
2096
|
+
*/
|
|
1439
2097
|
get first() {
|
|
1440
2098
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1441
2099
|
}
|
|
@@ -1458,19 +2116,111 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1458
2116
|
return new _Queue(elements);
|
|
1459
2117
|
}
|
|
1460
2118
|
/**
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
2119
|
+
* Check whether the queue is empty.
|
|
2120
|
+
* @remarks Time O(1), Space O(1)
|
|
2121
|
+
* @returns True if length is 0.
|
|
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
|
+
|
|
2153
|
+
|
|
2154
|
+
* @example
|
|
2155
|
+
* // Queue for...of iteration and isEmpty check
|
|
2156
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2157
|
+
*
|
|
2158
|
+
* const elements: string[] = [];
|
|
2159
|
+
* for (const item of queue) {
|
|
2160
|
+
* elements.push(item);
|
|
2161
|
+
* }
|
|
2162
|
+
*
|
|
2163
|
+
* // Verify all elements are iterated in order
|
|
2164
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2165
|
+
*
|
|
2166
|
+
* // Process all elements
|
|
2167
|
+
* while (queue.length > 0) {
|
|
2168
|
+
* queue.shift();
|
|
2169
|
+
* }
|
|
2170
|
+
*
|
|
2171
|
+
* console.log(queue.length); // 0;
|
|
2172
|
+
*/
|
|
1465
2173
|
isEmpty() {
|
|
1466
2174
|
return this.length === 0;
|
|
1467
2175
|
}
|
|
1468
2176
|
/**
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
2177
|
+
* Enqueue one element at the back.
|
|
2178
|
+
* @remarks Time O(1), Space O(1)
|
|
2179
|
+
* @param element - Element to enqueue.
|
|
2180
|
+
* @returns True on success.
|
|
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
|
+
|
|
2212
|
+
|
|
2213
|
+
* @example
|
|
2214
|
+
* // basic Queue creation and push operation
|
|
2215
|
+
* // Create a simple Queue with initial values
|
|
2216
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2217
|
+
*
|
|
2218
|
+
* // Verify the queue maintains insertion order
|
|
2219
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2220
|
+
*
|
|
2221
|
+
* // Check length
|
|
2222
|
+
* console.log(queue.length); // 5;
|
|
2223
|
+
*/
|
|
1474
2224
|
push(element) {
|
|
1475
2225
|
this.elements.push(element);
|
|
1476
2226
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1491,10 +2241,56 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1491
2241
|
return ans;
|
|
1492
2242
|
}
|
|
1493
2243
|
/**
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
2244
|
+
* Dequeue one element from the front (amortized via offset).
|
|
2245
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2246
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
2278
|
+
|
|
2279
|
+
* @example
|
|
2280
|
+
* // Queue shift and peek operations
|
|
2281
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2282
|
+
*
|
|
2283
|
+
* // Peek at the front element without removing it
|
|
2284
|
+
* console.log(queue.first); // 10;
|
|
2285
|
+
*
|
|
2286
|
+
* // Remove and get the first element (FIFO)
|
|
2287
|
+
* const first = queue.shift();
|
|
2288
|
+
* console.log(first); // 10;
|
|
2289
|
+
*
|
|
2290
|
+
* // Verify remaining elements and length decreased
|
|
2291
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
2292
|
+
* console.log(queue.length); // 3;
|
|
2293
|
+
*/
|
|
1498
2294
|
shift() {
|
|
1499
2295
|
if (this.length === 0) return void 0;
|
|
1500
2296
|
const first = this.first;
|
|
@@ -1503,11 +2299,45 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1503
2299
|
return first;
|
|
1504
2300
|
}
|
|
1505
2301
|
/**
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
2302
|
+
* Delete the first occurrence of a specific element.
|
|
2303
|
+
* @remarks Time O(N), Space O(1)
|
|
2304
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
2305
|
+
* @returns True if an element was removed.
|
|
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
|
+
|
|
2334
|
+
|
|
2335
|
+
* @example
|
|
2336
|
+
* // Remove specific element
|
|
2337
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2338
|
+
* q.delete(2);
|
|
2339
|
+
* console.log(q.length); // 3;
|
|
2340
|
+
*/
|
|
1511
2341
|
delete(element) {
|
|
1512
2342
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1513
2343
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1518,11 +2348,45 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1518
2348
|
return false;
|
|
1519
2349
|
}
|
|
1520
2350
|
/**
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
2351
|
+
* Get the element at a given logical index.
|
|
2352
|
+
* @remarks Time O(1), Space O(1)
|
|
2353
|
+
* @param index - Zero-based index from the front.
|
|
2354
|
+
* @returns Element or undefined.
|
|
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
|
+
|
|
2383
|
+
|
|
2384
|
+
* @example
|
|
2385
|
+
* // Access element by index
|
|
2386
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2387
|
+
* console.log(q.at(0)); // 'a';
|
|
2388
|
+
* console.log(q.at(2)); // 'c';
|
|
2389
|
+
*/
|
|
1526
2390
|
at(index) {
|
|
1527
2391
|
if (index < 0 || index >= this.length) return void 0;
|
|
1528
2392
|
return this._elements[this._offset + index];
|
|
@@ -1574,19 +2438,90 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1574
2438
|
return this;
|
|
1575
2439
|
}
|
|
1576
2440
|
/**
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
2441
|
+
* Remove all elements and reset offset.
|
|
2442
|
+
* @remarks Time O(1), Space O(1)
|
|
2443
|
+
* @returns void
|
|
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
|
+
|
|
2473
|
+
|
|
2474
|
+
* @example
|
|
2475
|
+
* // Remove all elements
|
|
2476
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2477
|
+
* q.clear();
|
|
2478
|
+
* console.log(q.length); // 0;
|
|
2479
|
+
*/
|
|
1581
2480
|
clear() {
|
|
1582
2481
|
this._elements = [];
|
|
1583
2482
|
this._offset = 0;
|
|
1584
2483
|
}
|
|
1585
2484
|
/**
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
2485
|
+
* Compact storage by discarding consumed head elements.
|
|
2486
|
+
* @remarks Time O(N), Space O(N)
|
|
2487
|
+
* @returns True when compaction performed.
|
|
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
|
+
|
|
2516
|
+
|
|
2517
|
+
* @example
|
|
2518
|
+
* // Reclaim unused memory
|
|
2519
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2520
|
+
* q.shift();
|
|
2521
|
+
* q.shift();
|
|
2522
|
+
* q.compact();
|
|
2523
|
+
* console.log(q.length); // 3;
|
|
2524
|
+
*/
|
|
1590
2525
|
compact() {
|
|
1591
2526
|
this._elements = this.elements.slice(this._offset);
|
|
1592
2527
|
this._offset = 0;
|
|
@@ -1612,10 +2547,47 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1612
2547
|
return removed;
|
|
1613
2548
|
}
|
|
1614
2549
|
/**
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
2550
|
+
* Deep clone this queue and its parameters.
|
|
2551
|
+
* @remarks Time O(N), Space O(N)
|
|
2552
|
+
* @returns A new queue with the same content and options.
|
|
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
|
+
|
|
2582
|
+
|
|
2583
|
+
* @example
|
|
2584
|
+
* // Create independent copy
|
|
2585
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2586
|
+
* const copy = q.clone();
|
|
2587
|
+
* copy.shift();
|
|
2588
|
+
* console.log(q.length); // 3;
|
|
2589
|
+
* console.log(copy.length); // 2;
|
|
2590
|
+
*/
|
|
1619
2591
|
clone() {
|
|
1620
2592
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1621
2593
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1623,12 +2595,47 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1623
2595
|
return out;
|
|
1624
2596
|
}
|
|
1625
2597
|
/**
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
2598
|
+
* Filter elements into a new queue of the same class.
|
|
2599
|
+
* @remarks Time O(N), Space O(N)
|
|
2600
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2601
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2602
|
+
* @returns A new queue with kept elements.
|
|
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
|
+
|
|
2632
|
+
|
|
2633
|
+
* @example
|
|
2634
|
+
* // Filter elements
|
|
2635
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2636
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2637
|
+
* console.log(evens.length); // 2;
|
|
2638
|
+
*/
|
|
1632
2639
|
filter(predicate, thisArg) {
|
|
1633
2640
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1634
2641
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1640,15 +2647,49 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1640
2647
|
return out;
|
|
1641
2648
|
}
|
|
1642
2649
|
/**
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
2650
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2651
|
+
* @remarks Time O(N), Space O(N)
|
|
2652
|
+
* @template EM
|
|
2653
|
+
* @template RM
|
|
2654
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2655
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2656
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2657
|
+
* @returns A new Queue with mapped elements.
|
|
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
|
+
|
|
2686
|
+
|
|
2687
|
+
* @example
|
|
2688
|
+
* // Transform elements
|
|
2689
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2690
|
+
* const doubled = q.map(x => x * 2);
|
|
2691
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2692
|
+
*/
|
|
1652
2693
|
map(callback, options, thisArg) {
|
|
1653
2694
|
const out = new this.constructor([], {
|
|
1654
2695
|
toElementFn: options?.toElementFn,
|
|
@@ -1883,19 +2924,102 @@ var Deque = class extends LinearBase {
|
|
|
1883
2924
|
return this._length;
|
|
1884
2925
|
}
|
|
1885
2926
|
/**
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
2927
|
+
* Get the first element without removing it.
|
|
2928
|
+
* @remarks Time O(1), Space O(1)
|
|
2929
|
+
* @returns First element or undefined.
|
|
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
|
+
|
|
2961
|
+
|
|
2962
|
+
* @example
|
|
2963
|
+
* // Deque peek at both ends
|
|
2964
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2965
|
+
*
|
|
2966
|
+
* // Get first element without removing
|
|
2967
|
+
* const first = deque.at(0);
|
|
2968
|
+
* console.log(first); // 10;
|
|
2969
|
+
*
|
|
2970
|
+
* // Get last element without removing
|
|
2971
|
+
* const last = deque.at(deque.length - 1);
|
|
2972
|
+
* console.log(last); // 50;
|
|
2973
|
+
*
|
|
2974
|
+
* // Length unchanged
|
|
2975
|
+
* console.log(deque.length); // 5;
|
|
2976
|
+
*/
|
|
1890
2977
|
get first() {
|
|
1891
2978
|
if (this._length === 0) return;
|
|
1892
2979
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
1893
2980
|
}
|
|
1894
2981
|
/**
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
2982
|
+
* Get the last element without removing it.
|
|
2983
|
+
* @remarks Time O(1), Space O(1)
|
|
2984
|
+
* @returns Last element or undefined.
|
|
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
|
+
|
|
3016
|
+
|
|
3017
|
+
* @example
|
|
3018
|
+
* // Peek at the back element
|
|
3019
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3020
|
+
* console.log(dq.last); // 'c';
|
|
3021
|
+
* console.log(dq.first); // 'a';
|
|
3022
|
+
*/
|
|
1899
3023
|
get last() {
|
|
1900
3024
|
if (this._length === 0) return;
|
|
1901
3025
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1914,11 +3038,61 @@ var Deque = class extends LinearBase {
|
|
|
1914
3038
|
return new this(data, options);
|
|
1915
3039
|
}
|
|
1916
3040
|
/**
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
3041
|
+
* Append one element at the back.
|
|
3042
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3043
|
+
* @param element - Element to append.
|
|
3044
|
+
* @returns True when appended.
|
|
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
|
+
|
|
3076
|
+
|
|
3077
|
+
* @example
|
|
3078
|
+
* // basic Deque creation and push/pop operations
|
|
3079
|
+
* // Create a simple Deque with initial values
|
|
3080
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
3081
|
+
*
|
|
3082
|
+
* // Verify the deque maintains insertion order
|
|
3083
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3084
|
+
*
|
|
3085
|
+
* // Check length
|
|
3086
|
+
* console.log(deque.length); // 5;
|
|
3087
|
+
*
|
|
3088
|
+
* // Push to the end
|
|
3089
|
+
* deque.push(6);
|
|
3090
|
+
* console.log(deque.length); // 6;
|
|
3091
|
+
*
|
|
3092
|
+
* // Pop from the end
|
|
3093
|
+
* const last = deque.pop();
|
|
3094
|
+
* console.log(last); // 6;
|
|
3095
|
+
*/
|
|
1922
3096
|
push(element) {
|
|
1923
3097
|
if (this._length) {
|
|
1924
3098
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -1938,10 +3112,47 @@ var Deque = class extends LinearBase {
|
|
|
1938
3112
|
return true;
|
|
1939
3113
|
}
|
|
1940
3114
|
/**
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
3115
|
+
* Remove and return the last element.
|
|
3116
|
+
* @remarks Time O(1), Space O(1)
|
|
3117
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
3149
|
+
|
|
3150
|
+
* @example
|
|
3151
|
+
* // Remove from the back
|
|
3152
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3153
|
+
* console.log(dq.pop()); // 3;
|
|
3154
|
+
* console.log(dq.length); // 2;
|
|
3155
|
+
*/
|
|
1945
3156
|
pop() {
|
|
1946
3157
|
if (this._length === 0) return;
|
|
1947
3158
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1961,10 +3172,47 @@ var Deque = class extends LinearBase {
|
|
|
1961
3172
|
return element;
|
|
1962
3173
|
}
|
|
1963
3174
|
/**
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
3175
|
+
* Remove and return the first element.
|
|
3176
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3177
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
3209
|
+
|
|
3210
|
+
* @example
|
|
3211
|
+
* // Remove from the front
|
|
3212
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3213
|
+
* console.log(dq.shift()); // 1;
|
|
3214
|
+
* console.log(dq.length); // 2;
|
|
3215
|
+
*/
|
|
1968
3216
|
shift() {
|
|
1969
3217
|
if (this._length === 0) return;
|
|
1970
3218
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -1984,11 +3232,58 @@ var Deque = class extends LinearBase {
|
|
|
1984
3232
|
return element;
|
|
1985
3233
|
}
|
|
1986
3234
|
/**
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
3235
|
+
* Prepend one element at the front.
|
|
3236
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3237
|
+
* @param element - Element to prepend.
|
|
3238
|
+
* @returns True when prepended.
|
|
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
|
+
|
|
3270
|
+
|
|
3271
|
+
* @example
|
|
3272
|
+
* // Deque shift and unshift operations
|
|
3273
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
3274
|
+
*
|
|
3275
|
+
* // Unshift adds to the front
|
|
3276
|
+
* deque.unshift(10);
|
|
3277
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3278
|
+
*
|
|
3279
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
3280
|
+
* const first = deque.shift();
|
|
3281
|
+
* console.log(first); // 10;
|
|
3282
|
+
*
|
|
3283
|
+
* // Verify remaining elements
|
|
3284
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
3285
|
+
* console.log(deque.length); // 3;
|
|
3286
|
+
*/
|
|
1992
3287
|
unshift(element) {
|
|
1993
3288
|
if (this._length) {
|
|
1994
3289
|
if (this._firstInBucket > 0) {
|
|
@@ -2042,18 +3337,87 @@ var Deque = class extends LinearBase {
|
|
|
2042
3337
|
return ans;
|
|
2043
3338
|
}
|
|
2044
3339
|
/**
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
3340
|
+
* Check whether the deque is empty.
|
|
3341
|
+
* @remarks Time O(1), Space O(1)
|
|
3342
|
+
* @returns True if length is 0.
|
|
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
|
+
|
|
3372
|
+
|
|
3373
|
+
* @example
|
|
3374
|
+
* // Check if empty
|
|
3375
|
+
* const dq = new Deque();
|
|
3376
|
+
* console.log(dq.isEmpty()); // true;
|
|
3377
|
+
*/
|
|
2049
3378
|
isEmpty() {
|
|
2050
3379
|
return this._length === 0;
|
|
2051
3380
|
}
|
|
2052
3381
|
/**
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
3382
|
+
* Remove all elements and reset structure.
|
|
3383
|
+
* @remarks Time O(1), Space O(1)
|
|
3384
|
+
* @returns void
|
|
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
|
+
|
|
3414
|
+
|
|
3415
|
+
* @example
|
|
3416
|
+
* // Remove all elements
|
|
3417
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3418
|
+
* dq.clear();
|
|
3419
|
+
* console.log(dq.length); // 0;
|
|
3420
|
+
*/
|
|
2057
3421
|
clear() {
|
|
2058
3422
|
this._buckets = [new Array(this._bucketSize)];
|
|
2059
3423
|
this._bucketCount = 1;
|
|
@@ -2061,11 +3425,45 @@ var Deque = class extends LinearBase {
|
|
|
2061
3425
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2062
3426
|
}
|
|
2063
3427
|
/**
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
3428
|
+
* Get the element at a given position.
|
|
3429
|
+
* @remarks Time O(1), Space O(1)
|
|
3430
|
+
* @param pos - Zero-based position from the front.
|
|
3431
|
+
* @returns Element or undefined.
|
|
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
|
+
|
|
3460
|
+
|
|
3461
|
+
* @example
|
|
3462
|
+
* // Access by index
|
|
3463
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3464
|
+
* console.log(dq.at(0)); // 'a';
|
|
3465
|
+
* console.log(dq.at(2)); // 'c';
|
|
3466
|
+
*/
|
|
2069
3467
|
at(pos) {
|
|
2070
3468
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
2071
3469
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -2224,11 +3622,45 @@ var Deque = class extends LinearBase {
|
|
|
2224
3622
|
}
|
|
2225
3623
|
}
|
|
2226
3624
|
/**
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
3625
|
+
* Delete the first occurrence of a value.
|
|
3626
|
+
* @remarks Time O(N), Space O(1)
|
|
3627
|
+
* @param element - Element to remove (using the configured equality).
|
|
3628
|
+
* @returns True if an element was removed.
|
|
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
|
+
|
|
3657
|
+
|
|
3658
|
+
* @example
|
|
3659
|
+
* // Remove element
|
|
3660
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3661
|
+
* dq.delete(2);
|
|
3662
|
+
* console.log(dq.length); // 2;
|
|
3663
|
+
*/
|
|
2232
3664
|
delete(element) {
|
|
2233
3665
|
const size = this._length;
|
|
2234
3666
|
if (size === 0) return false;
|
|
@@ -2272,10 +3704,60 @@ var Deque = class extends LinearBase {
|
|
|
2272
3704
|
return this;
|
|
2273
3705
|
}
|
|
2274
3706
|
/**
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
3707
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
3708
|
+
* @remarks Time O(N), Space O(N)
|
|
3709
|
+
* @returns This deque.
|
|
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
|
+
|
|
3741
|
+
|
|
3742
|
+
* @example
|
|
3743
|
+
* // Deque for...of iteration and reverse
|
|
3744
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
3745
|
+
*
|
|
3746
|
+
* // Iterate forward
|
|
3747
|
+
* const forward: string[] = [];
|
|
3748
|
+
* for (const item of deque) {
|
|
3749
|
+
* forward.push(item);
|
|
3750
|
+
* }
|
|
3751
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
3752
|
+
*
|
|
3753
|
+
* // Reverse the deque
|
|
3754
|
+
* deque.reverse();
|
|
3755
|
+
* const backward: string[] = [];
|
|
3756
|
+
* for (const item of deque) {
|
|
3757
|
+
* backward.push(item);
|
|
3758
|
+
* }
|
|
3759
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
3760
|
+
*/
|
|
2279
3761
|
reverse() {
|
|
2280
3762
|
this._buckets.reverse().forEach(function(bucket) {
|
|
2281
3763
|
bucket.reverse();
|
|
@@ -2334,10 +3816,46 @@ var Deque = class extends LinearBase {
|
|
|
2334
3816
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2335
3817
|
*/
|
|
2336
3818
|
/**
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
3819
|
+
* Compact the deque by removing unused buckets.
|
|
3820
|
+
* @remarks Time O(N), Space O(1)
|
|
3821
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
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
|
+
|
|
3850
|
+
|
|
3851
|
+
* @example
|
|
3852
|
+
* // Reclaim memory
|
|
3853
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
3854
|
+
* dq.shift();
|
|
3855
|
+
* dq.shift();
|
|
3856
|
+
* dq.compact();
|
|
3857
|
+
* console.log(dq.length); // 3;
|
|
3858
|
+
*/
|
|
2341
3859
|
compact() {
|
|
2342
3860
|
const before = this._bucketCount;
|
|
2343
3861
|
this.shrinkToFit();
|
|
@@ -2365,10 +3883,47 @@ var Deque = class extends LinearBase {
|
|
|
2365
3883
|
this._compactCounter = 0;
|
|
2366
3884
|
}
|
|
2367
3885
|
/**
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
3886
|
+
* Deep clone this deque, preserving options.
|
|
3887
|
+
* @remarks Time O(N), Space O(N)
|
|
3888
|
+
* @returns A new deque with the same content and options.
|
|
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
|
+
|
|
3918
|
+
|
|
3919
|
+
* @example
|
|
3920
|
+
* // Create independent copy
|
|
3921
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3922
|
+
* const copy = dq.clone();
|
|
3923
|
+
* copy.pop();
|
|
3924
|
+
* console.log(dq.length); // 3;
|
|
3925
|
+
* console.log(copy.length); // 2;
|
|
3926
|
+
*/
|
|
2372
3927
|
clone() {
|
|
2373
3928
|
return this._createLike(this, {
|
|
2374
3929
|
bucketSize: this.bucketSize,
|
|
@@ -2377,12 +3932,47 @@ var Deque = class extends LinearBase {
|
|
|
2377
3932
|
});
|
|
2378
3933
|
}
|
|
2379
3934
|
/**
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
3935
|
+
* Filter elements into a new deque of the same class.
|
|
3936
|
+
* @remarks Time O(N), Space O(N)
|
|
3937
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
3938
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
3939
|
+
* @returns A new deque with kept elements.
|
|
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
|
+
|
|
3969
|
+
|
|
3970
|
+
* @example
|
|
3971
|
+
* // Filter elements
|
|
3972
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
3973
|
+
* const result = dq.filter(x => x > 2);
|
|
3974
|
+
* console.log(result.length); // 2;
|
|
3975
|
+
*/
|
|
2386
3976
|
filter(predicate, thisArg) {
|
|
2387
3977
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2388
3978
|
out._setBucketSize(this._bucketSize);
|
|
@@ -2411,15 +4001,49 @@ var Deque = class extends LinearBase {
|
|
|
2411
4001
|
return out;
|
|
2412
4002
|
}
|
|
2413
4003
|
/**
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
4004
|
+
* Map elements into a new deque (possibly different element type).
|
|
4005
|
+
* @remarks Time O(N), Space O(N)
|
|
4006
|
+
* @template EM
|
|
4007
|
+
* @template RM
|
|
4008
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
4009
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
4010
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
4011
|
+
* @returns A new Deque with mapped elements.
|
|
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
|
+
|
|
4040
|
+
|
|
4041
|
+
* @example
|
|
4042
|
+
* // Transform elements
|
|
4043
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
4044
|
+
* const result = dq.map(x => x * 10);
|
|
4045
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
4046
|
+
*/
|
|
2423
4047
|
map(callback, options, thisArg) {
|
|
2424
4048
|
const out = this._createLike([], {
|
|
2425
4049
|
...options ?? {},
|