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
|
@@ -5,54 +5,6 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
6
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
7
|
|
|
8
|
-
// src/common/error.ts
|
|
9
|
-
var ERR = {
|
|
10
|
-
// Range / index
|
|
11
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
12
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
13
|
-
// Type / argument
|
|
14
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
15
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
16
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
17
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
18
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
19
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
20
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
21
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
22
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
23
|
-
// State / operation
|
|
24
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
25
|
-
// Matrix
|
|
26
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
27
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
28
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
29
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
30
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// src/common/index.ts
|
|
34
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
35
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
36
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
37
|
-
return DFSOperation2;
|
|
38
|
-
})(DFSOperation || {});
|
|
39
|
-
var _Range = class _Range {
|
|
40
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
41
|
-
this.low = low;
|
|
42
|
-
this.high = high;
|
|
43
|
-
this.includeLow = includeLow;
|
|
44
|
-
this.includeHigh = includeHigh;
|
|
45
|
-
}
|
|
46
|
-
// Determine whether a key is within the range
|
|
47
|
-
isInRange(key, comparator) {
|
|
48
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
49
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
50
|
-
return lowCheck && highCheck;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
__name(_Range, "Range");
|
|
54
|
-
var Range = _Range;
|
|
55
|
-
|
|
56
8
|
// src/data-structures/base/iterable-element-base.ts
|
|
57
9
|
var _IterableElementBase = class _IterableElementBase {
|
|
58
10
|
/**
|
|
@@ -75,7 +27,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
75
27
|
if (options) {
|
|
76
28
|
const { toElementFn } = options;
|
|
77
29
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
78
|
-
else if (toElementFn) throw new TypeError(
|
|
30
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
79
31
|
}
|
|
80
32
|
}
|
|
81
33
|
/**
|
|
@@ -231,7 +183,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
231
183
|
acc = initialValue;
|
|
232
184
|
} else {
|
|
233
185
|
const first = iter.next();
|
|
234
|
-
if (first.done) throw new TypeError(
|
|
186
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
235
187
|
acc = first.value;
|
|
236
188
|
index = 1;
|
|
237
189
|
}
|
|
@@ -769,11 +721,58 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
769
721
|
return list;
|
|
770
722
|
}
|
|
771
723
|
/**
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
724
|
+
* Append an element/node to the tail.
|
|
725
|
+
* @remarks Time O(1), Space O(1)
|
|
726
|
+
* @param elementOrNode - Element or node to append.
|
|
727
|
+
* @returns True when appended.
|
|
728
|
+
|
|
729
|
+
|
|
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
|
+
* @example
|
|
761
|
+
* // basic SinglyLinkedList creation and push operation
|
|
762
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
763
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
764
|
+
*
|
|
765
|
+
* // Verify the list maintains insertion order
|
|
766
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
767
|
+
*
|
|
768
|
+
* // Check length
|
|
769
|
+
* console.log(list.length); // 5;
|
|
770
|
+
*
|
|
771
|
+
* // Push a new element to the end
|
|
772
|
+
* list.push(6);
|
|
773
|
+
* console.log(list.length); // 6;
|
|
774
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
775
|
+
*/
|
|
777
776
|
push(elementOrNode) {
|
|
778
777
|
const newNode = this._ensureNode(elementOrNode);
|
|
779
778
|
if (!this.head) {
|
|
@@ -787,10 +786,57 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
787
786
|
return true;
|
|
788
787
|
}
|
|
789
788
|
/**
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
789
|
+
* Remove and return the tail element.
|
|
790
|
+
* @remarks Time O(N), Space O(1)
|
|
791
|
+
* @returns Removed element or undefined.
|
|
792
|
+
|
|
793
|
+
|
|
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
|
+
* @example
|
|
825
|
+
* // SinglyLinkedList pop and shift operations
|
|
826
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
827
|
+
*
|
|
828
|
+
* // Pop removes from the end
|
|
829
|
+
* const last = list.pop();
|
|
830
|
+
* console.log(last); // 50;
|
|
831
|
+
*
|
|
832
|
+
* // Shift removes from the beginning
|
|
833
|
+
* const first = list.shift();
|
|
834
|
+
* console.log(first); // 10;
|
|
835
|
+
*
|
|
836
|
+
* // Verify remaining elements
|
|
837
|
+
* console.log([...list]); // [20, 30, 40];
|
|
838
|
+
* console.log(list.length); // 3;
|
|
839
|
+
*/
|
|
794
840
|
pop() {
|
|
795
841
|
var _a;
|
|
796
842
|
if (!this.head) return void 0;
|
|
@@ -810,10 +856,47 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
810
856
|
return value;
|
|
811
857
|
}
|
|
812
858
|
/**
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
859
|
+
* Remove and return the head element.
|
|
860
|
+
* @remarks Time O(1), Space O(1)
|
|
861
|
+
* @returns Removed element or undefined.
|
|
862
|
+
|
|
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
|
+
* @example
|
|
895
|
+
* // Remove from the front
|
|
896
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
897
|
+
* console.log(list.shift()); // 10;
|
|
898
|
+
* console.log(list.length); // 2;
|
|
899
|
+
*/
|
|
817
900
|
shift() {
|
|
818
901
|
if (!this.head) return void 0;
|
|
819
902
|
const removed = this.head;
|
|
@@ -823,11 +906,63 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
823
906
|
return removed.value;
|
|
824
907
|
}
|
|
825
908
|
/**
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
909
|
+
* Prepend an element/node to the head.
|
|
910
|
+
* @remarks Time O(1), Space O(1)
|
|
911
|
+
* @param elementOrNode - Element or node to prepend.
|
|
912
|
+
* @returns True when prepended.
|
|
913
|
+
|
|
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
|
+
* @example
|
|
946
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
947
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
948
|
+
*
|
|
949
|
+
* // Unshift adds to the beginning
|
|
950
|
+
* list.unshift(10);
|
|
951
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
952
|
+
*
|
|
953
|
+
* // Access elements (forward traversal only for singly linked)
|
|
954
|
+
* const second = list.at(1);
|
|
955
|
+
* console.log(second); // 20;
|
|
956
|
+
*
|
|
957
|
+
* // SinglyLinkedList allows forward iteration only
|
|
958
|
+
* const elements: number[] = [];
|
|
959
|
+
* for (const item of list) {
|
|
960
|
+
* elements.push(item);
|
|
961
|
+
* }
|
|
962
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
963
|
+
*
|
|
964
|
+
* console.log(list.length); // 4;
|
|
965
|
+
*/
|
|
831
966
|
unshift(elementOrNode) {
|
|
832
967
|
const newNode = this._ensureNode(elementOrNode);
|
|
833
968
|
if (!this.head) {
|
|
@@ -883,11 +1018,49 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
883
1018
|
return void 0;
|
|
884
1019
|
}
|
|
885
1020
|
/**
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
1021
|
+
* Get the element at a given index.
|
|
1022
|
+
* @remarks Time O(N), Space O(1)
|
|
1023
|
+
* @param index - Zero-based index.
|
|
1024
|
+
* @returns Element or undefined.
|
|
1025
|
+
|
|
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
|
+
* @example
|
|
1058
|
+
* // Access element by index
|
|
1059
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1060
|
+
* console.log(list.at(0)); // 'a';
|
|
1061
|
+
* console.log(list.at(2)); // 'c';
|
|
1062
|
+
* console.log(list.at(3)); // 'd';
|
|
1063
|
+
*/
|
|
891
1064
|
at(index) {
|
|
892
1065
|
if (index < 0 || index >= this._length) return void 0;
|
|
893
1066
|
let current = this.head;
|
|
@@ -904,11 +1077,44 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
904
1077
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
905
1078
|
}
|
|
906
1079
|
/**
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
1080
|
+
* Get the node reference at a given index.
|
|
1081
|
+
* @remarks Time O(N), Space O(1)
|
|
1082
|
+
* @param index - Zero-based index.
|
|
1083
|
+
* @returns Node or undefined.
|
|
1084
|
+
|
|
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
|
+
* @example
|
|
1114
|
+
* // Get node at index
|
|
1115
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1116
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1117
|
+
*/
|
|
912
1118
|
getNodeAt(index) {
|
|
913
1119
|
if (index < 0 || index >= this._length) return void 0;
|
|
914
1120
|
let current = this.head;
|
|
@@ -916,11 +1122,45 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
916
1122
|
return current;
|
|
917
1123
|
}
|
|
918
1124
|
/**
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
1125
|
+
* Delete the element at an index.
|
|
1126
|
+
* @remarks Time O(N), Space O(1)
|
|
1127
|
+
* @param index - Zero-based index.
|
|
1128
|
+
* @returns Removed element or undefined.
|
|
1129
|
+
|
|
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
|
+
* @example
|
|
1159
|
+
* // Remove by index
|
|
1160
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1161
|
+
* list.deleteAt(1);
|
|
1162
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1163
|
+
*/
|
|
924
1164
|
deleteAt(index) {
|
|
925
1165
|
if (index < 0 || index >= this._length) return void 0;
|
|
926
1166
|
if (index === 0) return this.shift();
|
|
@@ -933,11 +1173,45 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
933
1173
|
return value;
|
|
934
1174
|
}
|
|
935
1175
|
/**
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
1176
|
+
* Delete the first match by value/node.
|
|
1177
|
+
* @remarks Time O(N), Space O(1)
|
|
1178
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1179
|
+
* @returns True if removed.
|
|
1180
|
+
|
|
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
|
+
* @example
|
|
1210
|
+
* // Remove first occurrence
|
|
1211
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1212
|
+
* list.delete(2);
|
|
1213
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1214
|
+
*/
|
|
941
1215
|
delete(elementOrNode) {
|
|
942
1216
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
943
1217
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -954,12 +1228,46 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
954
1228
|
return true;
|
|
955
1229
|
}
|
|
956
1230
|
/**
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
1231
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1232
|
+
* @remarks Time O(N), Space O(1)
|
|
1233
|
+
* @param index - Zero-based index.
|
|
1234
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1235
|
+
* @returns True if inserted.
|
|
1236
|
+
|
|
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
|
+
* @example
|
|
1266
|
+
* // Insert at index
|
|
1267
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1268
|
+
* list.addAt(1, 2);
|
|
1269
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1270
|
+
*/
|
|
963
1271
|
addAt(index, newElementOrNode) {
|
|
964
1272
|
if (index < 0 || index > this._length) return false;
|
|
965
1273
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -985,28 +1293,133 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
985
1293
|
return true;
|
|
986
1294
|
}
|
|
987
1295
|
/**
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1296
|
+
* Check whether the list is empty.
|
|
1297
|
+
* @remarks Time O(1), Space O(1)
|
|
1298
|
+
* @returns True if length is 0.
|
|
1299
|
+
|
|
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
|
+
* @example
|
|
1330
|
+
* // Check empty
|
|
1331
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1332
|
+
*/
|
|
992
1333
|
isEmpty() {
|
|
993
1334
|
return this._length === 0;
|
|
994
1335
|
}
|
|
995
1336
|
/**
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1337
|
+
* Remove all nodes and reset length.
|
|
1338
|
+
* @remarks Time O(N), Space O(1)
|
|
1339
|
+
* @returns void
|
|
1340
|
+
|
|
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
|
+
* @example
|
|
1371
|
+
* // Remove all
|
|
1372
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1373
|
+
* list.clear();
|
|
1374
|
+
* console.log(list.isEmpty()); // true;
|
|
1375
|
+
*/
|
|
1000
1376
|
clear() {
|
|
1001
1377
|
this._head = void 0;
|
|
1002
1378
|
this._tail = void 0;
|
|
1003
1379
|
this._length = 0;
|
|
1004
1380
|
}
|
|
1005
1381
|
/**
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1382
|
+
* Reverse the list in place.
|
|
1383
|
+
* @remarks Time O(N), Space O(1)
|
|
1384
|
+
* @returns This list.
|
|
1385
|
+
|
|
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
|
+
* @example
|
|
1418
|
+
* // Reverse the list in-place
|
|
1419
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1420
|
+
* list.reverse();
|
|
1421
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1422
|
+
*/
|
|
1010
1423
|
reverse() {
|
|
1011
1424
|
if (!this.head || this.head === this.tail) return this;
|
|
1012
1425
|
let prev;
|
|
@@ -1181,22 +1594,106 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1181
1594
|
return false;
|
|
1182
1595
|
}
|
|
1183
1596
|
/**
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1597
|
+
* Deep clone this list (values are copied by reference).
|
|
1598
|
+
* @remarks Time O(N), Space O(N)
|
|
1599
|
+
* @returns A new list with the same element sequence.
|
|
1600
|
+
|
|
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
|
+
* @example
|
|
1631
|
+
* // Deep copy
|
|
1632
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1633
|
+
* const copy = list.clone();
|
|
1634
|
+
* copy.pop();
|
|
1635
|
+
* console.log(list.length); // 3;
|
|
1636
|
+
* console.log(copy.length); // 2;
|
|
1637
|
+
*/
|
|
1188
1638
|
clone() {
|
|
1189
1639
|
const out = this._createInstance();
|
|
1190
1640
|
for (const v of this) out.push(v);
|
|
1191
1641
|
return out;
|
|
1192
1642
|
}
|
|
1193
1643
|
/**
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1644
|
+
* Filter values into a new list of the same class.
|
|
1645
|
+
* @remarks Time O(N), Space O(N)
|
|
1646
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1647
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1648
|
+
* @returns A new list with kept values.
|
|
1649
|
+
|
|
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
|
+
* @example
|
|
1682
|
+
* // SinglyLinkedList filter and map operations
|
|
1683
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1684
|
+
*
|
|
1685
|
+
* // Filter even numbers
|
|
1686
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1687
|
+
* console.log(filtered.length); // 2;
|
|
1688
|
+
*
|
|
1689
|
+
* // Map to double values
|
|
1690
|
+
* const doubled = list.map(value => value * 2);
|
|
1691
|
+
* console.log(doubled.length); // 5;
|
|
1692
|
+
*
|
|
1693
|
+
* // Use reduce to sum
|
|
1694
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1695
|
+
* console.log(sum); // 15;
|
|
1696
|
+
*/
|
|
1200
1697
|
filter(callback, thisArg) {
|
|
1201
1698
|
const out = this._createInstance();
|
|
1202
1699
|
let index = 0;
|
|
@@ -1220,15 +1717,52 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1220
1717
|
return out;
|
|
1221
1718
|
}
|
|
1222
1719
|
/**
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1720
|
+
* Map values into a new list (possibly different element type).
|
|
1721
|
+
* @remarks Time O(N), Space O(N)
|
|
1722
|
+
* @template EM
|
|
1723
|
+
* @template RM
|
|
1724
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1725
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1726
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1727
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1728
|
+
|
|
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
|
+
* @example
|
|
1761
|
+
* // Transform elements
|
|
1762
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1763
|
+
* const doubled = list.map(n => n * 2);
|
|
1764
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1765
|
+
*/
|
|
1232
1766
|
map(callback, options, thisArg) {
|
|
1233
1767
|
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1234
1768
|
let index = 0;
|
|
@@ -1365,6 +1899,54 @@ function elementOrPredicate(input, equals) {
|
|
|
1365
1899
|
}
|
|
1366
1900
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
1367
1901
|
|
|
1902
|
+
// src/common/error.ts
|
|
1903
|
+
var ERR = {
|
|
1904
|
+
// Range / index
|
|
1905
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1906
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1907
|
+
// Type / argument
|
|
1908
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1909
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1910
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1911
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1912
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1913
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1914
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1915
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1916
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1917
|
+
// State / operation
|
|
1918
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1919
|
+
// Matrix
|
|
1920
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1921
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1922
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1923
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1924
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1925
|
+
};
|
|
1926
|
+
|
|
1927
|
+
// src/common/index.ts
|
|
1928
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1929
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1930
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1931
|
+
return DFSOperation2;
|
|
1932
|
+
})(DFSOperation || {});
|
|
1933
|
+
var _Range = class _Range {
|
|
1934
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1935
|
+
this.low = low;
|
|
1936
|
+
this.high = high;
|
|
1937
|
+
this.includeLow = includeLow;
|
|
1938
|
+
this.includeHigh = includeHigh;
|
|
1939
|
+
}
|
|
1940
|
+
// Determine whether a key is within the range
|
|
1941
|
+
isInRange(key, comparator) {
|
|
1942
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1943
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1944
|
+
return lowCheck && highCheck;
|
|
1945
|
+
}
|
|
1946
|
+
};
|
|
1947
|
+
__name(_Range, "Range");
|
|
1948
|
+
var Range = _Range;
|
|
1949
|
+
|
|
1368
1950
|
// src/data-structures/queue/queue.ts
|
|
1369
1951
|
var _Queue = class _Queue extends LinearBase {
|
|
1370
1952
|
/**
|
|
@@ -1419,18 +2001,94 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1419
2001
|
this._autoCompactRatio = value;
|
|
1420
2002
|
}
|
|
1421
2003
|
/**
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
2004
|
+
* Get the number of elements currently in the queue.
|
|
2005
|
+
* @remarks Time O(1), Space O(1)
|
|
2006
|
+
* @returns Current length.
|
|
2007
|
+
|
|
2008
|
+
|
|
2009
|
+
|
|
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
|
+
* @example
|
|
2040
|
+
* // Track queue length
|
|
2041
|
+
* const q = new Queue<number>();
|
|
2042
|
+
* console.log(q.length); // 0;
|
|
2043
|
+
* q.push(1);
|
|
2044
|
+
* q.push(2);
|
|
2045
|
+
* console.log(q.length); // 2;
|
|
2046
|
+
*/
|
|
1426
2047
|
get length() {
|
|
1427
2048
|
return this.elements.length - this._offset;
|
|
1428
2049
|
}
|
|
1429
2050
|
/**
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
2051
|
+
* Get the first element (front) without removing it.
|
|
2052
|
+
* @remarks Time O(1), Space O(1)
|
|
2053
|
+
* @returns Front element or undefined.
|
|
2054
|
+
|
|
2055
|
+
|
|
2056
|
+
|
|
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
|
+
* @example
|
|
2087
|
+
* // View the front element
|
|
2088
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2089
|
+
* console.log(q.first); // 'first';
|
|
2090
|
+
* console.log(q.length); // 3;
|
|
2091
|
+
*/
|
|
1434
2092
|
get first() {
|
|
1435
2093
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1436
2094
|
}
|
|
@@ -1453,19 +2111,111 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1453
2111
|
return new _Queue(elements);
|
|
1454
2112
|
}
|
|
1455
2113
|
/**
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
2114
|
+
* Check whether the queue is empty.
|
|
2115
|
+
* @remarks Time O(1), Space O(1)
|
|
2116
|
+
* @returns True if length is 0.
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
|
|
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
|
+
* @example
|
|
2150
|
+
* // Queue for...of iteration and isEmpty check
|
|
2151
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2152
|
+
*
|
|
2153
|
+
* const elements: string[] = [];
|
|
2154
|
+
* for (const item of queue) {
|
|
2155
|
+
* elements.push(item);
|
|
2156
|
+
* }
|
|
2157
|
+
*
|
|
2158
|
+
* // Verify all elements are iterated in order
|
|
2159
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2160
|
+
*
|
|
2161
|
+
* // Process all elements
|
|
2162
|
+
* while (queue.length > 0) {
|
|
2163
|
+
* queue.shift();
|
|
2164
|
+
* }
|
|
2165
|
+
*
|
|
2166
|
+
* console.log(queue.length); // 0;
|
|
2167
|
+
*/
|
|
1460
2168
|
isEmpty() {
|
|
1461
2169
|
return this.length === 0;
|
|
1462
2170
|
}
|
|
1463
2171
|
/**
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
2172
|
+
* Enqueue one element at the back.
|
|
2173
|
+
* @remarks Time O(1), Space O(1)
|
|
2174
|
+
* @param element - Element to enqueue.
|
|
2175
|
+
* @returns True on success.
|
|
2176
|
+
|
|
2177
|
+
|
|
2178
|
+
|
|
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
|
+
* @example
|
|
2209
|
+
* // basic Queue creation and push operation
|
|
2210
|
+
* // Create a simple Queue with initial values
|
|
2211
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2212
|
+
*
|
|
2213
|
+
* // Verify the queue maintains insertion order
|
|
2214
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2215
|
+
*
|
|
2216
|
+
* // Check length
|
|
2217
|
+
* console.log(queue.length); // 5;
|
|
2218
|
+
*/
|
|
1469
2219
|
push(element) {
|
|
1470
2220
|
this.elements.push(element);
|
|
1471
2221
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1486,10 +2236,56 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1486
2236
|
return ans;
|
|
1487
2237
|
}
|
|
1488
2238
|
/**
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
2239
|
+
* Dequeue one element from the front (amortized via offset).
|
|
2240
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2241
|
+
* @returns Removed element or undefined.
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
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
|
+
* @example
|
|
2275
|
+
* // Queue shift and peek operations
|
|
2276
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2277
|
+
*
|
|
2278
|
+
* // Peek at the front element without removing it
|
|
2279
|
+
* console.log(queue.first); // 10;
|
|
2280
|
+
*
|
|
2281
|
+
* // Remove and get the first element (FIFO)
|
|
2282
|
+
* const first = queue.shift();
|
|
2283
|
+
* console.log(first); // 10;
|
|
2284
|
+
*
|
|
2285
|
+
* // Verify remaining elements and length decreased
|
|
2286
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
2287
|
+
* console.log(queue.length); // 3;
|
|
2288
|
+
*/
|
|
1493
2289
|
shift() {
|
|
1494
2290
|
if (this.length === 0) return void 0;
|
|
1495
2291
|
const first = this.first;
|
|
@@ -1498,11 +2294,45 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1498
2294
|
return first;
|
|
1499
2295
|
}
|
|
1500
2296
|
/**
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
2297
|
+
* Delete the first occurrence of a specific element.
|
|
2298
|
+
* @remarks Time O(N), Space O(1)
|
|
2299
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
2300
|
+
* @returns True if an element was removed.
|
|
2301
|
+
|
|
2302
|
+
|
|
2303
|
+
|
|
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
|
+
* @example
|
|
2331
|
+
* // Remove specific element
|
|
2332
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2333
|
+
* q.delete(2);
|
|
2334
|
+
* console.log(q.length); // 3;
|
|
2335
|
+
*/
|
|
1506
2336
|
delete(element) {
|
|
1507
2337
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1508
2338
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1513,11 +2343,45 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1513
2343
|
return false;
|
|
1514
2344
|
}
|
|
1515
2345
|
/**
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
2346
|
+
* Get the element at a given logical index.
|
|
2347
|
+
* @remarks Time O(1), Space O(1)
|
|
2348
|
+
* @param index - Zero-based index from the front.
|
|
2349
|
+
* @returns Element or undefined.
|
|
2350
|
+
|
|
2351
|
+
|
|
2352
|
+
|
|
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
|
+
* @example
|
|
2380
|
+
* // Access element by index
|
|
2381
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2382
|
+
* console.log(q.at(0)); // 'a';
|
|
2383
|
+
* console.log(q.at(2)); // 'c';
|
|
2384
|
+
*/
|
|
1521
2385
|
at(index) {
|
|
1522
2386
|
if (index < 0 || index >= this.length) return void 0;
|
|
1523
2387
|
return this._elements[this._offset + index];
|
|
@@ -1569,19 +2433,90 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1569
2433
|
return this;
|
|
1570
2434
|
}
|
|
1571
2435
|
/**
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
2436
|
+
* Remove all elements and reset offset.
|
|
2437
|
+
* @remarks Time O(1), Space O(1)
|
|
2438
|
+
* @returns void
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
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
|
+
* @example
|
|
2470
|
+
* // Remove all elements
|
|
2471
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2472
|
+
* q.clear();
|
|
2473
|
+
* console.log(q.length); // 0;
|
|
2474
|
+
*/
|
|
1576
2475
|
clear() {
|
|
1577
2476
|
this._elements = [];
|
|
1578
2477
|
this._offset = 0;
|
|
1579
2478
|
}
|
|
1580
2479
|
/**
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
2480
|
+
* Compact storage by discarding consumed head elements.
|
|
2481
|
+
* @remarks Time O(N), Space O(N)
|
|
2482
|
+
* @returns True when compaction performed.
|
|
2483
|
+
|
|
2484
|
+
|
|
2485
|
+
|
|
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
|
+
* @example
|
|
2513
|
+
* // Reclaim unused memory
|
|
2514
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2515
|
+
* q.shift();
|
|
2516
|
+
* q.shift();
|
|
2517
|
+
* q.compact();
|
|
2518
|
+
* console.log(q.length); // 3;
|
|
2519
|
+
*/
|
|
1585
2520
|
compact() {
|
|
1586
2521
|
this._elements = this.elements.slice(this._offset);
|
|
1587
2522
|
this._offset = 0;
|
|
@@ -1607,10 +2542,47 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1607
2542
|
return removed;
|
|
1608
2543
|
}
|
|
1609
2544
|
/**
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
2545
|
+
* Deep clone this queue and its parameters.
|
|
2546
|
+
* @remarks Time O(N), Space O(N)
|
|
2547
|
+
* @returns A new queue with the same content and options.
|
|
2548
|
+
|
|
2549
|
+
|
|
2550
|
+
|
|
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
|
+
* @example
|
|
2579
|
+
* // Create independent copy
|
|
2580
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2581
|
+
* const copy = q.clone();
|
|
2582
|
+
* copy.shift();
|
|
2583
|
+
* console.log(q.length); // 3;
|
|
2584
|
+
* console.log(copy.length); // 2;
|
|
2585
|
+
*/
|
|
1614
2586
|
clone() {
|
|
1615
2587
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1616
2588
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1618,12 +2590,47 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1618
2590
|
return out;
|
|
1619
2591
|
}
|
|
1620
2592
|
/**
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
2593
|
+
* Filter elements into a new queue of the same class.
|
|
2594
|
+
* @remarks Time O(N), Space O(N)
|
|
2595
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2596
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2597
|
+
* @returns A new queue with kept elements.
|
|
2598
|
+
|
|
2599
|
+
|
|
2600
|
+
|
|
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
|
+
* @example
|
|
2629
|
+
* // Filter elements
|
|
2630
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2631
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2632
|
+
* console.log(evens.length); // 2;
|
|
2633
|
+
*/
|
|
1627
2634
|
filter(predicate, thisArg) {
|
|
1628
2635
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1629
2636
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1635,15 +2642,49 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1635
2642
|
return out;
|
|
1636
2643
|
}
|
|
1637
2644
|
/**
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
2645
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2646
|
+
* @remarks Time O(N), Space O(N)
|
|
2647
|
+
* @template EM
|
|
2648
|
+
* @template RM
|
|
2649
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2650
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2651
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2652
|
+
* @returns A new Queue with mapped elements.
|
|
2653
|
+
|
|
2654
|
+
|
|
2655
|
+
|
|
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
|
+
* @example
|
|
2683
|
+
* // Transform elements
|
|
2684
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2685
|
+
* const doubled = q.map(x => x * 2);
|
|
2686
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2687
|
+
*/
|
|
1647
2688
|
map(callback, options, thisArg) {
|
|
1648
2689
|
var _a, _b;
|
|
1649
2690
|
const out = new this.constructor([], {
|
|
@@ -1878,19 +2919,102 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1878
2919
|
return this._length;
|
|
1879
2920
|
}
|
|
1880
2921
|
/**
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
2922
|
+
* Get the first element without removing it.
|
|
2923
|
+
* @remarks Time O(1), Space O(1)
|
|
2924
|
+
* @returns First element or undefined.
|
|
2925
|
+
|
|
2926
|
+
|
|
2927
|
+
|
|
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
|
+
* @example
|
|
2958
|
+
* // Deque peek at both ends
|
|
2959
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2960
|
+
*
|
|
2961
|
+
* // Get first element without removing
|
|
2962
|
+
* const first = deque.at(0);
|
|
2963
|
+
* console.log(first); // 10;
|
|
2964
|
+
*
|
|
2965
|
+
* // Get last element without removing
|
|
2966
|
+
* const last = deque.at(deque.length - 1);
|
|
2967
|
+
* console.log(last); // 50;
|
|
2968
|
+
*
|
|
2969
|
+
* // Length unchanged
|
|
2970
|
+
* console.log(deque.length); // 5;
|
|
2971
|
+
*/
|
|
1885
2972
|
get first() {
|
|
1886
2973
|
if (this._length === 0) return;
|
|
1887
2974
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
1888
2975
|
}
|
|
1889
2976
|
/**
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
2977
|
+
* Get the last element without removing it.
|
|
2978
|
+
* @remarks Time O(1), Space O(1)
|
|
2979
|
+
* @returns Last element or undefined.
|
|
2980
|
+
|
|
2981
|
+
|
|
2982
|
+
|
|
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
|
+
* @example
|
|
3013
|
+
* // Peek at the back element
|
|
3014
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3015
|
+
* console.log(dq.last); // 'c';
|
|
3016
|
+
* console.log(dq.first); // 'a';
|
|
3017
|
+
*/
|
|
1894
3018
|
get last() {
|
|
1895
3019
|
if (this._length === 0) return;
|
|
1896
3020
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1909,11 +3033,61 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1909
3033
|
return new this(data, options);
|
|
1910
3034
|
}
|
|
1911
3035
|
/**
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
3036
|
+
* Append one element at the back.
|
|
3037
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3038
|
+
* @param element - Element to append.
|
|
3039
|
+
* @returns True when appended.
|
|
3040
|
+
|
|
3041
|
+
|
|
3042
|
+
|
|
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
|
+
* @example
|
|
3073
|
+
* // basic Deque creation and push/pop operations
|
|
3074
|
+
* // Create a simple Deque with initial values
|
|
3075
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
3076
|
+
*
|
|
3077
|
+
* // Verify the deque maintains insertion order
|
|
3078
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3079
|
+
*
|
|
3080
|
+
* // Check length
|
|
3081
|
+
* console.log(deque.length); // 5;
|
|
3082
|
+
*
|
|
3083
|
+
* // Push to the end
|
|
3084
|
+
* deque.push(6);
|
|
3085
|
+
* console.log(deque.length); // 6;
|
|
3086
|
+
*
|
|
3087
|
+
* // Pop from the end
|
|
3088
|
+
* const last = deque.pop();
|
|
3089
|
+
* console.log(last); // 6;
|
|
3090
|
+
*/
|
|
1917
3091
|
push(element) {
|
|
1918
3092
|
if (this._length) {
|
|
1919
3093
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -1933,10 +3107,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1933
3107
|
return true;
|
|
1934
3108
|
}
|
|
1935
3109
|
/**
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
3110
|
+
* Remove and return the last element.
|
|
3111
|
+
* @remarks Time O(1), Space O(1)
|
|
3112
|
+
* @returns Removed element or undefined.
|
|
3113
|
+
|
|
3114
|
+
|
|
3115
|
+
|
|
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
|
+
* @example
|
|
3146
|
+
* // Remove from the back
|
|
3147
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3148
|
+
* console.log(dq.pop()); // 3;
|
|
3149
|
+
* console.log(dq.length); // 2;
|
|
3150
|
+
*/
|
|
1940
3151
|
pop() {
|
|
1941
3152
|
if (this._length === 0) return;
|
|
1942
3153
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1956,10 +3167,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1956
3167
|
return element;
|
|
1957
3168
|
}
|
|
1958
3169
|
/**
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
3170
|
+
* Remove and return the first element.
|
|
3171
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3172
|
+
* @returns Removed element or undefined.
|
|
3173
|
+
|
|
3174
|
+
|
|
3175
|
+
|
|
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
|
+
* @example
|
|
3206
|
+
* // Remove from the front
|
|
3207
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3208
|
+
* console.log(dq.shift()); // 1;
|
|
3209
|
+
* console.log(dq.length); // 2;
|
|
3210
|
+
*/
|
|
1963
3211
|
shift() {
|
|
1964
3212
|
if (this._length === 0) return;
|
|
1965
3213
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -1979,11 +3227,58 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1979
3227
|
return element;
|
|
1980
3228
|
}
|
|
1981
3229
|
/**
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
3230
|
+
* Prepend one element at the front.
|
|
3231
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3232
|
+
* @param element - Element to prepend.
|
|
3233
|
+
* @returns True when prepended.
|
|
3234
|
+
|
|
3235
|
+
|
|
3236
|
+
|
|
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
|
+
* @example
|
|
3267
|
+
* // Deque shift and unshift operations
|
|
3268
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
3269
|
+
*
|
|
3270
|
+
* // Unshift adds to the front
|
|
3271
|
+
* deque.unshift(10);
|
|
3272
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3273
|
+
*
|
|
3274
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
3275
|
+
* const first = deque.shift();
|
|
3276
|
+
* console.log(first); // 10;
|
|
3277
|
+
*
|
|
3278
|
+
* // Verify remaining elements
|
|
3279
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
3280
|
+
* console.log(deque.length); // 3;
|
|
3281
|
+
*/
|
|
1987
3282
|
unshift(element) {
|
|
1988
3283
|
if (this._length) {
|
|
1989
3284
|
if (this._firstInBucket > 0) {
|
|
@@ -2037,18 +3332,87 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2037
3332
|
return ans;
|
|
2038
3333
|
}
|
|
2039
3334
|
/**
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
3335
|
+
* Check whether the deque is empty.
|
|
3336
|
+
* @remarks Time O(1), Space O(1)
|
|
3337
|
+
* @returns True if length is 0.
|
|
3338
|
+
|
|
3339
|
+
|
|
3340
|
+
|
|
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
|
+
* @example
|
|
3369
|
+
* // Check if empty
|
|
3370
|
+
* const dq = new Deque();
|
|
3371
|
+
* console.log(dq.isEmpty()); // true;
|
|
3372
|
+
*/
|
|
2044
3373
|
isEmpty() {
|
|
2045
3374
|
return this._length === 0;
|
|
2046
3375
|
}
|
|
2047
3376
|
/**
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
3377
|
+
* Remove all elements and reset structure.
|
|
3378
|
+
* @remarks Time O(1), Space O(1)
|
|
3379
|
+
* @returns void
|
|
3380
|
+
|
|
3381
|
+
|
|
3382
|
+
|
|
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
|
+
* @example
|
|
3411
|
+
* // Remove all elements
|
|
3412
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3413
|
+
* dq.clear();
|
|
3414
|
+
* console.log(dq.length); // 0;
|
|
3415
|
+
*/
|
|
2052
3416
|
clear() {
|
|
2053
3417
|
this._buckets = [new Array(this._bucketSize)];
|
|
2054
3418
|
this._bucketCount = 1;
|
|
@@ -2056,11 +3420,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2056
3420
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2057
3421
|
}
|
|
2058
3422
|
/**
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
3423
|
+
* Get the element at a given position.
|
|
3424
|
+
* @remarks Time O(1), Space O(1)
|
|
3425
|
+
* @param pos - Zero-based position from the front.
|
|
3426
|
+
* @returns Element or undefined.
|
|
3427
|
+
|
|
3428
|
+
|
|
3429
|
+
|
|
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
|
+
* @example
|
|
3457
|
+
* // Access by index
|
|
3458
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3459
|
+
* console.log(dq.at(0)); // 'a';
|
|
3460
|
+
* console.log(dq.at(2)); // 'c';
|
|
3461
|
+
*/
|
|
2064
3462
|
at(pos) {
|
|
2065
3463
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
2066
3464
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -2219,11 +3617,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2219
3617
|
}
|
|
2220
3618
|
}
|
|
2221
3619
|
/**
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
3620
|
+
* Delete the first occurrence of a value.
|
|
3621
|
+
* @remarks Time O(N), Space O(1)
|
|
3622
|
+
* @param element - Element to remove (using the configured equality).
|
|
3623
|
+
* @returns True if an element was removed.
|
|
3624
|
+
|
|
3625
|
+
|
|
3626
|
+
|
|
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
|
+
* @example
|
|
3654
|
+
* // Remove element
|
|
3655
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3656
|
+
* dq.delete(2);
|
|
3657
|
+
* console.log(dq.length); // 2;
|
|
3658
|
+
*/
|
|
2227
3659
|
delete(element) {
|
|
2228
3660
|
const size = this._length;
|
|
2229
3661
|
if (size === 0) return false;
|
|
@@ -2267,10 +3699,60 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2267
3699
|
return this;
|
|
2268
3700
|
}
|
|
2269
3701
|
/**
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
3702
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
3703
|
+
* @remarks Time O(N), Space O(N)
|
|
3704
|
+
* @returns This deque.
|
|
3705
|
+
|
|
3706
|
+
|
|
3707
|
+
|
|
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
|
+
* @example
|
|
3738
|
+
* // Deque for...of iteration and reverse
|
|
3739
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
3740
|
+
*
|
|
3741
|
+
* // Iterate forward
|
|
3742
|
+
* const forward: string[] = [];
|
|
3743
|
+
* for (const item of deque) {
|
|
3744
|
+
* forward.push(item);
|
|
3745
|
+
* }
|
|
3746
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
3747
|
+
*
|
|
3748
|
+
* // Reverse the deque
|
|
3749
|
+
* deque.reverse();
|
|
3750
|
+
* const backward: string[] = [];
|
|
3751
|
+
* for (const item of deque) {
|
|
3752
|
+
* backward.push(item);
|
|
3753
|
+
* }
|
|
3754
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
3755
|
+
*/
|
|
2274
3756
|
reverse() {
|
|
2275
3757
|
this._buckets.reverse().forEach(function(bucket) {
|
|
2276
3758
|
bucket.reverse();
|
|
@@ -2329,10 +3811,46 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2329
3811
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2330
3812
|
*/
|
|
2331
3813
|
/**
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
3814
|
+
* Compact the deque by removing unused buckets.
|
|
3815
|
+
* @remarks Time O(N), Space O(1)
|
|
3816
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
3817
|
+
|
|
3818
|
+
|
|
3819
|
+
|
|
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
|
+
* @example
|
|
3847
|
+
* // Reclaim memory
|
|
3848
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
3849
|
+
* dq.shift();
|
|
3850
|
+
* dq.shift();
|
|
3851
|
+
* dq.compact();
|
|
3852
|
+
* console.log(dq.length); // 3;
|
|
3853
|
+
*/
|
|
2336
3854
|
compact() {
|
|
2337
3855
|
const before = this._bucketCount;
|
|
2338
3856
|
this.shrinkToFit();
|
|
@@ -2360,10 +3878,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2360
3878
|
this._compactCounter = 0;
|
|
2361
3879
|
}
|
|
2362
3880
|
/**
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
3881
|
+
* Deep clone this deque, preserving options.
|
|
3882
|
+
* @remarks Time O(N), Space O(N)
|
|
3883
|
+
* @returns A new deque with the same content and options.
|
|
3884
|
+
|
|
3885
|
+
|
|
3886
|
+
|
|
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
|
+
* @example
|
|
3915
|
+
* // Create independent copy
|
|
3916
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3917
|
+
* const copy = dq.clone();
|
|
3918
|
+
* copy.pop();
|
|
3919
|
+
* console.log(dq.length); // 3;
|
|
3920
|
+
* console.log(copy.length); // 2;
|
|
3921
|
+
*/
|
|
2367
3922
|
clone() {
|
|
2368
3923
|
return this._createLike(this, {
|
|
2369
3924
|
bucketSize: this.bucketSize,
|
|
@@ -2372,12 +3927,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2372
3927
|
});
|
|
2373
3928
|
}
|
|
2374
3929
|
/**
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
3930
|
+
* Filter elements into a new deque of the same class.
|
|
3931
|
+
* @remarks Time O(N), Space O(N)
|
|
3932
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
3933
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
3934
|
+
* @returns A new deque with kept elements.
|
|
3935
|
+
|
|
3936
|
+
|
|
3937
|
+
|
|
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
|
+
* @example
|
|
3966
|
+
* // Filter elements
|
|
3967
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
3968
|
+
* const result = dq.filter(x => x > 2);
|
|
3969
|
+
* console.log(result.length); // 2;
|
|
3970
|
+
*/
|
|
2381
3971
|
filter(predicate, thisArg) {
|
|
2382
3972
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2383
3973
|
out._setBucketSize(this._bucketSize);
|
|
@@ -2406,15 +3996,49 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2406
3996
|
return out;
|
|
2407
3997
|
}
|
|
2408
3998
|
/**
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
3999
|
+
* Map elements into a new deque (possibly different element type).
|
|
4000
|
+
* @remarks Time O(N), Space O(N)
|
|
4001
|
+
* @template EM
|
|
4002
|
+
* @template RM
|
|
4003
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
4004
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
4005
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
4006
|
+
* @returns A new Deque with mapped elements.
|
|
4007
|
+
|
|
4008
|
+
|
|
4009
|
+
|
|
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
|
+
* @example
|
|
4037
|
+
* // Transform elements
|
|
4038
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
4039
|
+
* const result = dq.map(x => x * 10);
|
|
4040
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
4041
|
+
*/
|
|
2418
4042
|
map(callback, options, thisArg) {
|
|
2419
4043
|
const out = this._createLike([], {
|
|
2420
4044
|
...options != null ? options : {},
|