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
|
@@ -3,54 +3,6 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
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 _Range {
|
|
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
|
-
// Determine whether a key is within the range
|
|
45
|
-
isInRange(key, comparator) {
|
|
46
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
47
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
48
|
-
return lowCheck && highCheck;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
__name(_Range, "Range");
|
|
52
|
-
var Range = _Range;
|
|
53
|
-
|
|
54
6
|
// src/data-structures/base/iterable-element-base.ts
|
|
55
7
|
var _IterableElementBase = class _IterableElementBase {
|
|
56
8
|
/**
|
|
@@ -73,7 +25,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
73
25
|
if (options) {
|
|
74
26
|
const { toElementFn } = options;
|
|
75
27
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
76
|
-
else if (toElementFn) throw new TypeError(
|
|
28
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
77
29
|
}
|
|
78
30
|
}
|
|
79
31
|
/**
|
|
@@ -229,7 +181,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
229
181
|
acc = initialValue;
|
|
230
182
|
} else {
|
|
231
183
|
const first = iter.next();
|
|
232
|
-
if (first.done) throw new TypeError(
|
|
184
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
233
185
|
acc = first.value;
|
|
234
186
|
index = 1;
|
|
235
187
|
}
|
|
@@ -767,11 +719,58 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
767
719
|
return list;
|
|
768
720
|
}
|
|
769
721
|
/**
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
722
|
+
* Append an element/node to the tail.
|
|
723
|
+
* @remarks Time O(1), Space O(1)
|
|
724
|
+
* @param elementOrNode - Element or node to append.
|
|
725
|
+
* @returns True when appended.
|
|
726
|
+
|
|
727
|
+
|
|
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
|
+
* @example
|
|
759
|
+
* // basic SinglyLinkedList creation and push operation
|
|
760
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
761
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
762
|
+
*
|
|
763
|
+
* // Verify the list maintains insertion order
|
|
764
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
765
|
+
*
|
|
766
|
+
* // Check length
|
|
767
|
+
* console.log(list.length); // 5;
|
|
768
|
+
*
|
|
769
|
+
* // Push a new element to the end
|
|
770
|
+
* list.push(6);
|
|
771
|
+
* console.log(list.length); // 6;
|
|
772
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
773
|
+
*/
|
|
775
774
|
push(elementOrNode) {
|
|
776
775
|
const newNode = this._ensureNode(elementOrNode);
|
|
777
776
|
if (!this.head) {
|
|
@@ -785,10 +784,57 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
785
784
|
return true;
|
|
786
785
|
}
|
|
787
786
|
/**
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
787
|
+
* Remove and return the tail element.
|
|
788
|
+
* @remarks Time O(N), Space O(1)
|
|
789
|
+
* @returns Removed element or undefined.
|
|
790
|
+
|
|
791
|
+
|
|
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
|
+
* @example
|
|
823
|
+
* // SinglyLinkedList pop and shift operations
|
|
824
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
825
|
+
*
|
|
826
|
+
* // Pop removes from the end
|
|
827
|
+
* const last = list.pop();
|
|
828
|
+
* console.log(last); // 50;
|
|
829
|
+
*
|
|
830
|
+
* // Shift removes from the beginning
|
|
831
|
+
* const first = list.shift();
|
|
832
|
+
* console.log(first); // 10;
|
|
833
|
+
*
|
|
834
|
+
* // Verify remaining elements
|
|
835
|
+
* console.log([...list]); // [20, 30, 40];
|
|
836
|
+
* console.log(list.length); // 3;
|
|
837
|
+
*/
|
|
792
838
|
pop() {
|
|
793
839
|
var _a;
|
|
794
840
|
if (!this.head) return void 0;
|
|
@@ -808,10 +854,47 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
808
854
|
return value;
|
|
809
855
|
}
|
|
810
856
|
/**
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
857
|
+
* Remove and return the head element.
|
|
858
|
+
* @remarks Time O(1), Space O(1)
|
|
859
|
+
* @returns Removed element or undefined.
|
|
860
|
+
|
|
861
|
+
|
|
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
|
+
* @example
|
|
893
|
+
* // Remove from the front
|
|
894
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
895
|
+
* console.log(list.shift()); // 10;
|
|
896
|
+
* console.log(list.length); // 2;
|
|
897
|
+
*/
|
|
815
898
|
shift() {
|
|
816
899
|
if (!this.head) return void 0;
|
|
817
900
|
const removed = this.head;
|
|
@@ -821,11 +904,63 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
821
904
|
return removed.value;
|
|
822
905
|
}
|
|
823
906
|
/**
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
907
|
+
* Prepend an element/node to the head.
|
|
908
|
+
* @remarks Time O(1), Space O(1)
|
|
909
|
+
* @param elementOrNode - Element or node to prepend.
|
|
910
|
+
* @returns True when prepended.
|
|
911
|
+
|
|
912
|
+
|
|
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
|
+
* @example
|
|
944
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
945
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
946
|
+
*
|
|
947
|
+
* // Unshift adds to the beginning
|
|
948
|
+
* list.unshift(10);
|
|
949
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
950
|
+
*
|
|
951
|
+
* // Access elements (forward traversal only for singly linked)
|
|
952
|
+
* const second = list.at(1);
|
|
953
|
+
* console.log(second); // 20;
|
|
954
|
+
*
|
|
955
|
+
* // SinglyLinkedList allows forward iteration only
|
|
956
|
+
* const elements: number[] = [];
|
|
957
|
+
* for (const item of list) {
|
|
958
|
+
* elements.push(item);
|
|
959
|
+
* }
|
|
960
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
961
|
+
*
|
|
962
|
+
* console.log(list.length); // 4;
|
|
963
|
+
*/
|
|
829
964
|
unshift(elementOrNode) {
|
|
830
965
|
const newNode = this._ensureNode(elementOrNode);
|
|
831
966
|
if (!this.head) {
|
|
@@ -881,11 +1016,49 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
881
1016
|
return void 0;
|
|
882
1017
|
}
|
|
883
1018
|
/**
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
1019
|
+
* Get the element at a given index.
|
|
1020
|
+
* @remarks Time O(N), Space O(1)
|
|
1021
|
+
* @param index - Zero-based index.
|
|
1022
|
+
* @returns Element or undefined.
|
|
1023
|
+
|
|
1024
|
+
|
|
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
|
+
* @example
|
|
1056
|
+
* // Access element by index
|
|
1057
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1058
|
+
* console.log(list.at(0)); // 'a';
|
|
1059
|
+
* console.log(list.at(2)); // 'c';
|
|
1060
|
+
* console.log(list.at(3)); // 'd';
|
|
1061
|
+
*/
|
|
889
1062
|
at(index) {
|
|
890
1063
|
if (index < 0 || index >= this._length) return void 0;
|
|
891
1064
|
let current = this.head;
|
|
@@ -902,11 +1075,44 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
902
1075
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
903
1076
|
}
|
|
904
1077
|
/**
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
1078
|
+
* Get the node reference at a given index.
|
|
1079
|
+
* @remarks Time O(N), Space O(1)
|
|
1080
|
+
* @param index - Zero-based index.
|
|
1081
|
+
* @returns Node or undefined.
|
|
1082
|
+
|
|
1083
|
+
|
|
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
|
+
* @example
|
|
1112
|
+
* // Get node at index
|
|
1113
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1114
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1115
|
+
*/
|
|
910
1116
|
getNodeAt(index) {
|
|
911
1117
|
if (index < 0 || index >= this._length) return void 0;
|
|
912
1118
|
let current = this.head;
|
|
@@ -914,11 +1120,45 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
914
1120
|
return current;
|
|
915
1121
|
}
|
|
916
1122
|
/**
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
1123
|
+
* Delete the element at an index.
|
|
1124
|
+
* @remarks Time O(N), Space O(1)
|
|
1125
|
+
* @param index - Zero-based index.
|
|
1126
|
+
* @returns Removed element or undefined.
|
|
1127
|
+
|
|
1128
|
+
|
|
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
|
+
* @example
|
|
1157
|
+
* // Remove by index
|
|
1158
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1159
|
+
* list.deleteAt(1);
|
|
1160
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1161
|
+
*/
|
|
922
1162
|
deleteAt(index) {
|
|
923
1163
|
if (index < 0 || index >= this._length) return void 0;
|
|
924
1164
|
if (index === 0) return this.shift();
|
|
@@ -931,11 +1171,45 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
931
1171
|
return value;
|
|
932
1172
|
}
|
|
933
1173
|
/**
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
1174
|
+
* Delete the first match by value/node.
|
|
1175
|
+
* @remarks Time O(N), Space O(1)
|
|
1176
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1177
|
+
* @returns True if removed.
|
|
1178
|
+
|
|
1179
|
+
|
|
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
|
+
* @example
|
|
1208
|
+
* // Remove first occurrence
|
|
1209
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1210
|
+
* list.delete(2);
|
|
1211
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1212
|
+
*/
|
|
939
1213
|
delete(elementOrNode) {
|
|
940
1214
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
941
1215
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -952,12 +1226,46 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
952
1226
|
return true;
|
|
953
1227
|
}
|
|
954
1228
|
/**
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
1229
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1230
|
+
* @remarks Time O(N), Space O(1)
|
|
1231
|
+
* @param index - Zero-based index.
|
|
1232
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1233
|
+
* @returns True if inserted.
|
|
1234
|
+
|
|
1235
|
+
|
|
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
|
+
* @example
|
|
1264
|
+
* // Insert at index
|
|
1265
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1266
|
+
* list.addAt(1, 2);
|
|
1267
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1268
|
+
*/
|
|
961
1269
|
addAt(index, newElementOrNode) {
|
|
962
1270
|
if (index < 0 || index > this._length) return false;
|
|
963
1271
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -983,28 +1291,133 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
983
1291
|
return true;
|
|
984
1292
|
}
|
|
985
1293
|
/**
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1294
|
+
* Check whether the list is empty.
|
|
1295
|
+
* @remarks Time O(1), Space O(1)
|
|
1296
|
+
* @returns True if length is 0.
|
|
1297
|
+
|
|
1298
|
+
|
|
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
|
+
* @example
|
|
1328
|
+
* // Check empty
|
|
1329
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1330
|
+
*/
|
|
990
1331
|
isEmpty() {
|
|
991
1332
|
return this._length === 0;
|
|
992
1333
|
}
|
|
993
1334
|
/**
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1335
|
+
* Remove all nodes and reset length.
|
|
1336
|
+
* @remarks Time O(N), Space O(1)
|
|
1337
|
+
* @returns void
|
|
1338
|
+
|
|
1339
|
+
|
|
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
|
+
* @example
|
|
1369
|
+
* // Remove all
|
|
1370
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1371
|
+
* list.clear();
|
|
1372
|
+
* console.log(list.isEmpty()); // true;
|
|
1373
|
+
*/
|
|
998
1374
|
clear() {
|
|
999
1375
|
this._head = void 0;
|
|
1000
1376
|
this._tail = void 0;
|
|
1001
1377
|
this._length = 0;
|
|
1002
1378
|
}
|
|
1003
1379
|
/**
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1380
|
+
* Reverse the list in place.
|
|
1381
|
+
* @remarks Time O(N), Space O(1)
|
|
1382
|
+
* @returns This list.
|
|
1383
|
+
|
|
1384
|
+
|
|
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
|
+
* @example
|
|
1416
|
+
* // Reverse the list in-place
|
|
1417
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1418
|
+
* list.reverse();
|
|
1419
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1420
|
+
*/
|
|
1008
1421
|
reverse() {
|
|
1009
1422
|
if (!this.head || this.head === this.tail) return this;
|
|
1010
1423
|
let prev;
|
|
@@ -1179,22 +1592,106 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1179
1592
|
return false;
|
|
1180
1593
|
}
|
|
1181
1594
|
/**
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1595
|
+
* Deep clone this list (values are copied by reference).
|
|
1596
|
+
* @remarks Time O(N), Space O(N)
|
|
1597
|
+
* @returns A new list with the same element sequence.
|
|
1598
|
+
|
|
1599
|
+
|
|
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
|
+
* @example
|
|
1629
|
+
* // Deep copy
|
|
1630
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1631
|
+
* const copy = list.clone();
|
|
1632
|
+
* copy.pop();
|
|
1633
|
+
* console.log(list.length); // 3;
|
|
1634
|
+
* console.log(copy.length); // 2;
|
|
1635
|
+
*/
|
|
1186
1636
|
clone() {
|
|
1187
1637
|
const out = this._createInstance();
|
|
1188
1638
|
for (const v of this) out.push(v);
|
|
1189
1639
|
return out;
|
|
1190
1640
|
}
|
|
1191
1641
|
/**
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1642
|
+
* Filter values into a new list of the same class.
|
|
1643
|
+
* @remarks Time O(N), Space O(N)
|
|
1644
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1645
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1646
|
+
* @returns A new list with kept values.
|
|
1647
|
+
|
|
1648
|
+
|
|
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
|
+
* @example
|
|
1680
|
+
* // SinglyLinkedList filter and map operations
|
|
1681
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1682
|
+
*
|
|
1683
|
+
* // Filter even numbers
|
|
1684
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1685
|
+
* console.log(filtered.length); // 2;
|
|
1686
|
+
*
|
|
1687
|
+
* // Map to double values
|
|
1688
|
+
* const doubled = list.map(value => value * 2);
|
|
1689
|
+
* console.log(doubled.length); // 5;
|
|
1690
|
+
*
|
|
1691
|
+
* // Use reduce to sum
|
|
1692
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1693
|
+
* console.log(sum); // 15;
|
|
1694
|
+
*/
|
|
1198
1695
|
filter(callback, thisArg) {
|
|
1199
1696
|
const out = this._createInstance();
|
|
1200
1697
|
let index = 0;
|
|
@@ -1218,15 +1715,52 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1218
1715
|
return out;
|
|
1219
1716
|
}
|
|
1220
1717
|
/**
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1718
|
+
* Map values into a new list (possibly different element type).
|
|
1719
|
+
* @remarks Time O(N), Space O(N)
|
|
1720
|
+
* @template EM
|
|
1721
|
+
* @template RM
|
|
1722
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1723
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1724
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1725
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1726
|
+
|
|
1727
|
+
|
|
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
|
+
* @example
|
|
1759
|
+
* // Transform elements
|
|
1760
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1761
|
+
* const doubled = list.map(n => n * 2);
|
|
1762
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1763
|
+
*/
|
|
1230
1764
|
map(callback, options, thisArg) {
|
|
1231
1765
|
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1232
1766
|
let index = 0;
|
|
@@ -1363,6 +1897,54 @@ function elementOrPredicate(input, equals) {
|
|
|
1363
1897
|
}
|
|
1364
1898
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
1365
1899
|
|
|
1900
|
+
// src/common/error.ts
|
|
1901
|
+
var ERR = {
|
|
1902
|
+
// Range / index
|
|
1903
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1904
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1905
|
+
// Type / argument
|
|
1906
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1907
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1908
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1909
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1910
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1911
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1912
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1913
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1914
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1915
|
+
// State / operation
|
|
1916
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1917
|
+
// Matrix
|
|
1918
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1919
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1920
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1921
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1922
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1923
|
+
};
|
|
1924
|
+
|
|
1925
|
+
// src/common/index.ts
|
|
1926
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1927
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1928
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1929
|
+
return DFSOperation2;
|
|
1930
|
+
})(DFSOperation || {});
|
|
1931
|
+
var _Range = class _Range {
|
|
1932
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1933
|
+
this.low = low;
|
|
1934
|
+
this.high = high;
|
|
1935
|
+
this.includeLow = includeLow;
|
|
1936
|
+
this.includeHigh = includeHigh;
|
|
1937
|
+
}
|
|
1938
|
+
// Determine whether a key is within the range
|
|
1939
|
+
isInRange(key, comparator) {
|
|
1940
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1941
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1942
|
+
return lowCheck && highCheck;
|
|
1943
|
+
}
|
|
1944
|
+
};
|
|
1945
|
+
__name(_Range, "Range");
|
|
1946
|
+
var Range = _Range;
|
|
1947
|
+
|
|
1366
1948
|
// src/data-structures/queue/queue.ts
|
|
1367
1949
|
var _Queue = class _Queue extends LinearBase {
|
|
1368
1950
|
/**
|
|
@@ -1417,18 +1999,94 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1417
1999
|
this._autoCompactRatio = value;
|
|
1418
2000
|
}
|
|
1419
2001
|
/**
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
2002
|
+
* Get the number of elements currently in the queue.
|
|
2003
|
+
* @remarks Time O(1), Space O(1)
|
|
2004
|
+
* @returns Current length.
|
|
2005
|
+
|
|
2006
|
+
|
|
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
|
+
* @example
|
|
2038
|
+
* // Track queue length
|
|
2039
|
+
* const q = new Queue<number>();
|
|
2040
|
+
* console.log(q.length); // 0;
|
|
2041
|
+
* q.push(1);
|
|
2042
|
+
* q.push(2);
|
|
2043
|
+
* console.log(q.length); // 2;
|
|
2044
|
+
*/
|
|
1424
2045
|
get length() {
|
|
1425
2046
|
return this.elements.length - this._offset;
|
|
1426
2047
|
}
|
|
1427
2048
|
/**
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
2049
|
+
* Get the first element (front) without removing it.
|
|
2050
|
+
* @remarks Time O(1), Space O(1)
|
|
2051
|
+
* @returns Front element or undefined.
|
|
2052
|
+
|
|
2053
|
+
|
|
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
|
+
* @example
|
|
2085
|
+
* // View the front element
|
|
2086
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2087
|
+
* console.log(q.first); // 'first';
|
|
2088
|
+
* console.log(q.length); // 3;
|
|
2089
|
+
*/
|
|
1432
2090
|
get first() {
|
|
1433
2091
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1434
2092
|
}
|
|
@@ -1451,19 +2109,111 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1451
2109
|
return new _Queue(elements);
|
|
1452
2110
|
}
|
|
1453
2111
|
/**
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
2112
|
+
* Check whether the queue is empty.
|
|
2113
|
+
* @remarks Time O(1), Space O(1)
|
|
2114
|
+
* @returns True if length is 0.
|
|
2115
|
+
|
|
2116
|
+
|
|
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
|
+
* @example
|
|
2148
|
+
* // Queue for...of iteration and isEmpty check
|
|
2149
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2150
|
+
*
|
|
2151
|
+
* const elements: string[] = [];
|
|
2152
|
+
* for (const item of queue) {
|
|
2153
|
+
* elements.push(item);
|
|
2154
|
+
* }
|
|
2155
|
+
*
|
|
2156
|
+
* // Verify all elements are iterated in order
|
|
2157
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2158
|
+
*
|
|
2159
|
+
* // Process all elements
|
|
2160
|
+
* while (queue.length > 0) {
|
|
2161
|
+
* queue.shift();
|
|
2162
|
+
* }
|
|
2163
|
+
*
|
|
2164
|
+
* console.log(queue.length); // 0;
|
|
2165
|
+
*/
|
|
1458
2166
|
isEmpty() {
|
|
1459
2167
|
return this.length === 0;
|
|
1460
2168
|
}
|
|
1461
2169
|
/**
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
2170
|
+
* Enqueue one element at the back.
|
|
2171
|
+
* @remarks Time O(1), Space O(1)
|
|
2172
|
+
* @param element - Element to enqueue.
|
|
2173
|
+
* @returns True on success.
|
|
2174
|
+
|
|
2175
|
+
|
|
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
|
+
* @example
|
|
2207
|
+
* // basic Queue creation and push operation
|
|
2208
|
+
* // Create a simple Queue with initial values
|
|
2209
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2210
|
+
*
|
|
2211
|
+
* // Verify the queue maintains insertion order
|
|
2212
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2213
|
+
*
|
|
2214
|
+
* // Check length
|
|
2215
|
+
* console.log(queue.length); // 5;
|
|
2216
|
+
*/
|
|
1467
2217
|
push(element) {
|
|
1468
2218
|
this.elements.push(element);
|
|
1469
2219
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1484,10 +2234,56 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1484
2234
|
return ans;
|
|
1485
2235
|
}
|
|
1486
2236
|
/**
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
2237
|
+
* Dequeue one element from the front (amortized via offset).
|
|
2238
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2239
|
+
* @returns Removed element or undefined.
|
|
2240
|
+
|
|
2241
|
+
|
|
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
|
+
* @example
|
|
2273
|
+
* // Queue shift and peek operations
|
|
2274
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2275
|
+
*
|
|
2276
|
+
* // Peek at the front element without removing it
|
|
2277
|
+
* console.log(queue.first); // 10;
|
|
2278
|
+
*
|
|
2279
|
+
* // Remove and get the first element (FIFO)
|
|
2280
|
+
* const first = queue.shift();
|
|
2281
|
+
* console.log(first); // 10;
|
|
2282
|
+
*
|
|
2283
|
+
* // Verify remaining elements and length decreased
|
|
2284
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
2285
|
+
* console.log(queue.length); // 3;
|
|
2286
|
+
*/
|
|
1491
2287
|
shift() {
|
|
1492
2288
|
if (this.length === 0) return void 0;
|
|
1493
2289
|
const first = this.first;
|
|
@@ -1496,11 +2292,45 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1496
2292
|
return first;
|
|
1497
2293
|
}
|
|
1498
2294
|
/**
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
2295
|
+
* Delete the first occurrence of a specific element.
|
|
2296
|
+
* @remarks Time O(N), Space O(1)
|
|
2297
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
2298
|
+
* @returns True if an element was removed.
|
|
2299
|
+
|
|
2300
|
+
|
|
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
|
+
* @example
|
|
2329
|
+
* // Remove specific element
|
|
2330
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2331
|
+
* q.delete(2);
|
|
2332
|
+
* console.log(q.length); // 3;
|
|
2333
|
+
*/
|
|
1504
2334
|
delete(element) {
|
|
1505
2335
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1506
2336
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1511,11 +2341,45 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1511
2341
|
return false;
|
|
1512
2342
|
}
|
|
1513
2343
|
/**
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
2344
|
+
* Get the element at a given logical index.
|
|
2345
|
+
* @remarks Time O(1), Space O(1)
|
|
2346
|
+
* @param index - Zero-based index from the front.
|
|
2347
|
+
* @returns Element or undefined.
|
|
2348
|
+
|
|
2349
|
+
|
|
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
|
+
* @example
|
|
2378
|
+
* // Access element by index
|
|
2379
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2380
|
+
* console.log(q.at(0)); // 'a';
|
|
2381
|
+
* console.log(q.at(2)); // 'c';
|
|
2382
|
+
*/
|
|
1519
2383
|
at(index) {
|
|
1520
2384
|
if (index < 0 || index >= this.length) return void 0;
|
|
1521
2385
|
return this._elements[this._offset + index];
|
|
@@ -1567,19 +2431,90 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1567
2431
|
return this;
|
|
1568
2432
|
}
|
|
1569
2433
|
/**
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
2434
|
+
* Remove all elements and reset offset.
|
|
2435
|
+
* @remarks Time O(1), Space O(1)
|
|
2436
|
+
* @returns void
|
|
2437
|
+
|
|
2438
|
+
|
|
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
|
+
* @example
|
|
2468
|
+
* // Remove all elements
|
|
2469
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2470
|
+
* q.clear();
|
|
2471
|
+
* console.log(q.length); // 0;
|
|
2472
|
+
*/
|
|
1574
2473
|
clear() {
|
|
1575
2474
|
this._elements = [];
|
|
1576
2475
|
this._offset = 0;
|
|
1577
2476
|
}
|
|
1578
2477
|
/**
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
2478
|
+
* Compact storage by discarding consumed head elements.
|
|
2479
|
+
* @remarks Time O(N), Space O(N)
|
|
2480
|
+
* @returns True when compaction performed.
|
|
2481
|
+
|
|
2482
|
+
|
|
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
|
+
* @example
|
|
2511
|
+
* // Reclaim unused memory
|
|
2512
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2513
|
+
* q.shift();
|
|
2514
|
+
* q.shift();
|
|
2515
|
+
* q.compact();
|
|
2516
|
+
* console.log(q.length); // 3;
|
|
2517
|
+
*/
|
|
1583
2518
|
compact() {
|
|
1584
2519
|
this._elements = this.elements.slice(this._offset);
|
|
1585
2520
|
this._offset = 0;
|
|
@@ -1605,10 +2540,47 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1605
2540
|
return removed;
|
|
1606
2541
|
}
|
|
1607
2542
|
/**
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
2543
|
+
* Deep clone this queue and its parameters.
|
|
2544
|
+
* @remarks Time O(N), Space O(N)
|
|
2545
|
+
* @returns A new queue with the same content and options.
|
|
2546
|
+
|
|
2547
|
+
|
|
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
|
+
* @example
|
|
2577
|
+
* // Create independent copy
|
|
2578
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2579
|
+
* const copy = q.clone();
|
|
2580
|
+
* copy.shift();
|
|
2581
|
+
* console.log(q.length); // 3;
|
|
2582
|
+
* console.log(copy.length); // 2;
|
|
2583
|
+
*/
|
|
1612
2584
|
clone() {
|
|
1613
2585
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1614
2586
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1616,12 +2588,47 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1616
2588
|
return out;
|
|
1617
2589
|
}
|
|
1618
2590
|
/**
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
2591
|
+
* Filter elements into a new queue of the same class.
|
|
2592
|
+
* @remarks Time O(N), Space O(N)
|
|
2593
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2594
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2595
|
+
* @returns A new queue with kept elements.
|
|
2596
|
+
|
|
2597
|
+
|
|
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
|
+
* @example
|
|
2627
|
+
* // Filter elements
|
|
2628
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2629
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2630
|
+
* console.log(evens.length); // 2;
|
|
2631
|
+
*/
|
|
1625
2632
|
filter(predicate, thisArg) {
|
|
1626
2633
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1627
2634
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1633,15 +2640,49 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1633
2640
|
return out;
|
|
1634
2641
|
}
|
|
1635
2642
|
/**
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
2643
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2644
|
+
* @remarks Time O(N), Space O(N)
|
|
2645
|
+
* @template EM
|
|
2646
|
+
* @template RM
|
|
2647
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2648
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2649
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2650
|
+
* @returns A new Queue with mapped elements.
|
|
2651
|
+
|
|
2652
|
+
|
|
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
|
+
* @example
|
|
2681
|
+
* // Transform elements
|
|
2682
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2683
|
+
* const doubled = q.map(x => x * 2);
|
|
2684
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2685
|
+
*/
|
|
1645
2686
|
map(callback, options, thisArg) {
|
|
1646
2687
|
var _a, _b;
|
|
1647
2688
|
const out = new this.constructor([], {
|
|
@@ -1876,19 +2917,102 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1876
2917
|
return this._length;
|
|
1877
2918
|
}
|
|
1878
2919
|
/**
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
2920
|
+
* Get the first element without removing it.
|
|
2921
|
+
* @remarks Time O(1), Space O(1)
|
|
2922
|
+
* @returns First element or undefined.
|
|
2923
|
+
|
|
2924
|
+
|
|
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
|
+
* @example
|
|
2956
|
+
* // Deque peek at both ends
|
|
2957
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2958
|
+
*
|
|
2959
|
+
* // Get first element without removing
|
|
2960
|
+
* const first = deque.at(0);
|
|
2961
|
+
* console.log(first); // 10;
|
|
2962
|
+
*
|
|
2963
|
+
* // Get last element without removing
|
|
2964
|
+
* const last = deque.at(deque.length - 1);
|
|
2965
|
+
* console.log(last); // 50;
|
|
2966
|
+
*
|
|
2967
|
+
* // Length unchanged
|
|
2968
|
+
* console.log(deque.length); // 5;
|
|
2969
|
+
*/
|
|
1883
2970
|
get first() {
|
|
1884
2971
|
if (this._length === 0) return;
|
|
1885
2972
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
1886
2973
|
}
|
|
1887
2974
|
/**
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
2975
|
+
* Get the last element without removing it.
|
|
2976
|
+
* @remarks Time O(1), Space O(1)
|
|
2977
|
+
* @returns Last element or undefined.
|
|
2978
|
+
|
|
2979
|
+
|
|
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
|
+
* @example
|
|
3011
|
+
* // Peek at the back element
|
|
3012
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3013
|
+
* console.log(dq.last); // 'c';
|
|
3014
|
+
* console.log(dq.first); // 'a';
|
|
3015
|
+
*/
|
|
1892
3016
|
get last() {
|
|
1893
3017
|
if (this._length === 0) return;
|
|
1894
3018
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1907,11 +3031,61 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1907
3031
|
return new this(data, options);
|
|
1908
3032
|
}
|
|
1909
3033
|
/**
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
3034
|
+
* Append one element at the back.
|
|
3035
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3036
|
+
* @param element - Element to append.
|
|
3037
|
+
* @returns True when appended.
|
|
3038
|
+
|
|
3039
|
+
|
|
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
|
+
* @example
|
|
3071
|
+
* // basic Deque creation and push/pop operations
|
|
3072
|
+
* // Create a simple Deque with initial values
|
|
3073
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
3074
|
+
*
|
|
3075
|
+
* // Verify the deque maintains insertion order
|
|
3076
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3077
|
+
*
|
|
3078
|
+
* // Check length
|
|
3079
|
+
* console.log(deque.length); // 5;
|
|
3080
|
+
*
|
|
3081
|
+
* // Push to the end
|
|
3082
|
+
* deque.push(6);
|
|
3083
|
+
* console.log(deque.length); // 6;
|
|
3084
|
+
*
|
|
3085
|
+
* // Pop from the end
|
|
3086
|
+
* const last = deque.pop();
|
|
3087
|
+
* console.log(last); // 6;
|
|
3088
|
+
*/
|
|
1915
3089
|
push(element) {
|
|
1916
3090
|
if (this._length) {
|
|
1917
3091
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -1931,10 +3105,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1931
3105
|
return true;
|
|
1932
3106
|
}
|
|
1933
3107
|
/**
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
3108
|
+
* Remove and return the last element.
|
|
3109
|
+
* @remarks Time O(1), Space O(1)
|
|
3110
|
+
* @returns Removed element or undefined.
|
|
3111
|
+
|
|
3112
|
+
|
|
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
|
+
* @example
|
|
3144
|
+
* // Remove from the back
|
|
3145
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3146
|
+
* console.log(dq.pop()); // 3;
|
|
3147
|
+
* console.log(dq.length); // 2;
|
|
3148
|
+
*/
|
|
1938
3149
|
pop() {
|
|
1939
3150
|
if (this._length === 0) return;
|
|
1940
3151
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1954,10 +3165,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1954
3165
|
return element;
|
|
1955
3166
|
}
|
|
1956
3167
|
/**
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
3168
|
+
* Remove and return the first element.
|
|
3169
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3170
|
+
* @returns Removed element or undefined.
|
|
3171
|
+
|
|
3172
|
+
|
|
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
|
+
* @example
|
|
3204
|
+
* // Remove from the front
|
|
3205
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3206
|
+
* console.log(dq.shift()); // 1;
|
|
3207
|
+
* console.log(dq.length); // 2;
|
|
3208
|
+
*/
|
|
1961
3209
|
shift() {
|
|
1962
3210
|
if (this._length === 0) return;
|
|
1963
3211
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -1977,11 +3225,58 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1977
3225
|
return element;
|
|
1978
3226
|
}
|
|
1979
3227
|
/**
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
3228
|
+
* Prepend one element at the front.
|
|
3229
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
3230
|
+
* @param element - Element to prepend.
|
|
3231
|
+
* @returns True when prepended.
|
|
3232
|
+
|
|
3233
|
+
|
|
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
|
+
* @example
|
|
3265
|
+
* // Deque shift and unshift operations
|
|
3266
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
3267
|
+
*
|
|
3268
|
+
* // Unshift adds to the front
|
|
3269
|
+
* deque.unshift(10);
|
|
3270
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3271
|
+
*
|
|
3272
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
3273
|
+
* const first = deque.shift();
|
|
3274
|
+
* console.log(first); // 10;
|
|
3275
|
+
*
|
|
3276
|
+
* // Verify remaining elements
|
|
3277
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
3278
|
+
* console.log(deque.length); // 3;
|
|
3279
|
+
*/
|
|
1985
3280
|
unshift(element) {
|
|
1986
3281
|
if (this._length) {
|
|
1987
3282
|
if (this._firstInBucket > 0) {
|
|
@@ -2035,18 +3330,87 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2035
3330
|
return ans;
|
|
2036
3331
|
}
|
|
2037
3332
|
/**
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
3333
|
+
* Check whether the deque is empty.
|
|
3334
|
+
* @remarks Time O(1), Space O(1)
|
|
3335
|
+
* @returns True if length is 0.
|
|
3336
|
+
|
|
3337
|
+
|
|
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
|
+
* @example
|
|
3367
|
+
* // Check if empty
|
|
3368
|
+
* const dq = new Deque();
|
|
3369
|
+
* console.log(dq.isEmpty()); // true;
|
|
3370
|
+
*/
|
|
2042
3371
|
isEmpty() {
|
|
2043
3372
|
return this._length === 0;
|
|
2044
3373
|
}
|
|
2045
3374
|
/**
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
3375
|
+
* Remove all elements and reset structure.
|
|
3376
|
+
* @remarks Time O(1), Space O(1)
|
|
3377
|
+
* @returns void
|
|
3378
|
+
|
|
3379
|
+
|
|
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
|
+
* @example
|
|
3409
|
+
* // Remove all elements
|
|
3410
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3411
|
+
* dq.clear();
|
|
3412
|
+
* console.log(dq.length); // 0;
|
|
3413
|
+
*/
|
|
2050
3414
|
clear() {
|
|
2051
3415
|
this._buckets = [new Array(this._bucketSize)];
|
|
2052
3416
|
this._bucketCount = 1;
|
|
@@ -2054,11 +3418,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2054
3418
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2055
3419
|
}
|
|
2056
3420
|
/**
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
3421
|
+
* Get the element at a given position.
|
|
3422
|
+
* @remarks Time O(1), Space O(1)
|
|
3423
|
+
* @param pos - Zero-based position from the front.
|
|
3424
|
+
* @returns Element or undefined.
|
|
3425
|
+
|
|
3426
|
+
|
|
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
|
+
* @example
|
|
3455
|
+
* // Access by index
|
|
3456
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3457
|
+
* console.log(dq.at(0)); // 'a';
|
|
3458
|
+
* console.log(dq.at(2)); // 'c';
|
|
3459
|
+
*/
|
|
2062
3460
|
at(pos) {
|
|
2063
3461
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
2064
3462
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -2217,11 +3615,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2217
3615
|
}
|
|
2218
3616
|
}
|
|
2219
3617
|
/**
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
3618
|
+
* Delete the first occurrence of a value.
|
|
3619
|
+
* @remarks Time O(N), Space O(1)
|
|
3620
|
+
* @param element - Element to remove (using the configured equality).
|
|
3621
|
+
* @returns True if an element was removed.
|
|
3622
|
+
|
|
3623
|
+
|
|
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
|
+
* @example
|
|
3652
|
+
* // Remove element
|
|
3653
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3654
|
+
* dq.delete(2);
|
|
3655
|
+
* console.log(dq.length); // 2;
|
|
3656
|
+
*/
|
|
2225
3657
|
delete(element) {
|
|
2226
3658
|
const size = this._length;
|
|
2227
3659
|
if (size === 0) return false;
|
|
@@ -2265,10 +3697,60 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2265
3697
|
return this;
|
|
2266
3698
|
}
|
|
2267
3699
|
/**
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
3700
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
3701
|
+
* @remarks Time O(N), Space O(N)
|
|
3702
|
+
* @returns This deque.
|
|
3703
|
+
|
|
3704
|
+
|
|
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
|
+
* @example
|
|
3736
|
+
* // Deque for...of iteration and reverse
|
|
3737
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
3738
|
+
*
|
|
3739
|
+
* // Iterate forward
|
|
3740
|
+
* const forward: string[] = [];
|
|
3741
|
+
* for (const item of deque) {
|
|
3742
|
+
* forward.push(item);
|
|
3743
|
+
* }
|
|
3744
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
3745
|
+
*
|
|
3746
|
+
* // Reverse the deque
|
|
3747
|
+
* deque.reverse();
|
|
3748
|
+
* const backward: string[] = [];
|
|
3749
|
+
* for (const item of deque) {
|
|
3750
|
+
* backward.push(item);
|
|
3751
|
+
* }
|
|
3752
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
3753
|
+
*/
|
|
2272
3754
|
reverse() {
|
|
2273
3755
|
this._buckets.reverse().forEach(function(bucket) {
|
|
2274
3756
|
bucket.reverse();
|
|
@@ -2327,10 +3809,46 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2327
3809
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2328
3810
|
*/
|
|
2329
3811
|
/**
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
3812
|
+
* Compact the deque by removing unused buckets.
|
|
3813
|
+
* @remarks Time O(N), Space O(1)
|
|
3814
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
3815
|
+
|
|
3816
|
+
|
|
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
|
+
* @example
|
|
3845
|
+
* // Reclaim memory
|
|
3846
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
3847
|
+
* dq.shift();
|
|
3848
|
+
* dq.shift();
|
|
3849
|
+
* dq.compact();
|
|
3850
|
+
* console.log(dq.length); // 3;
|
|
3851
|
+
*/
|
|
2334
3852
|
compact() {
|
|
2335
3853
|
const before = this._bucketCount;
|
|
2336
3854
|
this.shrinkToFit();
|
|
@@ -2358,10 +3876,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2358
3876
|
this._compactCounter = 0;
|
|
2359
3877
|
}
|
|
2360
3878
|
/**
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
3879
|
+
* Deep clone this deque, preserving options.
|
|
3880
|
+
* @remarks Time O(N), Space O(N)
|
|
3881
|
+
* @returns A new deque with the same content and options.
|
|
3882
|
+
|
|
3883
|
+
|
|
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
|
+
* @example
|
|
3913
|
+
* // Create independent copy
|
|
3914
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3915
|
+
* const copy = dq.clone();
|
|
3916
|
+
* copy.pop();
|
|
3917
|
+
* console.log(dq.length); // 3;
|
|
3918
|
+
* console.log(copy.length); // 2;
|
|
3919
|
+
*/
|
|
2365
3920
|
clone() {
|
|
2366
3921
|
return this._createLike(this, {
|
|
2367
3922
|
bucketSize: this.bucketSize,
|
|
@@ -2370,12 +3925,47 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2370
3925
|
});
|
|
2371
3926
|
}
|
|
2372
3927
|
/**
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
3928
|
+
* Filter elements into a new deque of the same class.
|
|
3929
|
+
* @remarks Time O(N), Space O(N)
|
|
3930
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
3931
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
3932
|
+
* @returns A new deque with kept elements.
|
|
3933
|
+
|
|
3934
|
+
|
|
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
|
+
* @example
|
|
3964
|
+
* // Filter elements
|
|
3965
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
3966
|
+
* const result = dq.filter(x => x > 2);
|
|
3967
|
+
* console.log(result.length); // 2;
|
|
3968
|
+
*/
|
|
2379
3969
|
filter(predicate, thisArg) {
|
|
2380
3970
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2381
3971
|
out._setBucketSize(this._bucketSize);
|
|
@@ -2404,15 +3994,49 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2404
3994
|
return out;
|
|
2405
3995
|
}
|
|
2406
3996
|
/**
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
3997
|
+
* Map elements into a new deque (possibly different element type).
|
|
3998
|
+
* @remarks Time O(N), Space O(N)
|
|
3999
|
+
* @template EM
|
|
4000
|
+
* @template RM
|
|
4001
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
4002
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
4003
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
4004
|
+
* @returns A new Deque with mapped elements.
|
|
4005
|
+
|
|
4006
|
+
|
|
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
|
+
* @example
|
|
4035
|
+
* // Transform elements
|
|
4036
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
4037
|
+
* const result = dq.map(x => x * 10);
|
|
4038
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
4039
|
+
*/
|
|
2416
4040
|
map(callback, options, thisArg) {
|
|
2417
4041
|
const out = this._createLike([], {
|
|
2418
4042
|
...options != null ? options : {},
|