queue-typed 2.4.5 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -48
- package/dist/cjs/index.cjs +997 -255
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +996 -254
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +997 -255
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +996 -254
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/queue-typed.js +994 -252
- package/dist/umd/queue-typed.js.map +1 -1
- package/dist/umd/queue-typed.min.js +1 -1
- package/dist/umd/queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -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,37 @@ 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
|
+
* @example
|
|
738
|
+
* // basic SinglyLinkedList creation and push operation
|
|
739
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
740
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
741
|
+
*
|
|
742
|
+
* // Verify the list maintains insertion order
|
|
743
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
744
|
+
*
|
|
745
|
+
* // Check length
|
|
746
|
+
* console.log(list.length); // 5;
|
|
747
|
+
*
|
|
748
|
+
* // Push a new element to the end
|
|
749
|
+
* list.push(6);
|
|
750
|
+
* console.log(list.length); // 6;
|
|
751
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
752
|
+
*/
|
|
775
753
|
push(elementOrNode) {
|
|
776
754
|
const newNode = this._ensureNode(elementOrNode);
|
|
777
755
|
if (!this.head) {
|
|
@@ -785,10 +763,36 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
785
763
|
return true;
|
|
786
764
|
}
|
|
787
765
|
/**
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
766
|
+
* Remove and return the tail element.
|
|
767
|
+
* @remarks Time O(N), Space O(1)
|
|
768
|
+
* @returns Removed element or undefined.
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
* @example
|
|
781
|
+
* // SinglyLinkedList pop and shift operations
|
|
782
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
783
|
+
*
|
|
784
|
+
* // Pop removes from the end
|
|
785
|
+
* const last = list.pop();
|
|
786
|
+
* console.log(last); // 50;
|
|
787
|
+
*
|
|
788
|
+
* // Shift removes from the beginning
|
|
789
|
+
* const first = list.shift();
|
|
790
|
+
* console.log(first); // 10;
|
|
791
|
+
*
|
|
792
|
+
* // Verify remaining elements
|
|
793
|
+
* console.log([...list]); // [20, 30, 40];
|
|
794
|
+
* console.log(list.length); // 3;
|
|
795
|
+
*/
|
|
792
796
|
pop() {
|
|
793
797
|
var _a;
|
|
794
798
|
if (!this.head) return void 0;
|
|
@@ -808,10 +812,26 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
808
812
|
return value;
|
|
809
813
|
}
|
|
810
814
|
/**
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
+
* Remove and return the head element.
|
|
816
|
+
* @remarks Time O(1), Space O(1)
|
|
817
|
+
* @returns Removed element or undefined.
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
* @example
|
|
830
|
+
* // Remove from the front
|
|
831
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
832
|
+
* console.log(list.shift()); // 10;
|
|
833
|
+
* console.log(list.length); // 2;
|
|
834
|
+
*/
|
|
815
835
|
shift() {
|
|
816
836
|
if (!this.head) return void 0;
|
|
817
837
|
const removed = this.head;
|
|
@@ -821,11 +841,42 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
821
841
|
return removed.value;
|
|
822
842
|
}
|
|
823
843
|
/**
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
844
|
+
* Prepend an element/node to the head.
|
|
845
|
+
* @remarks Time O(1), Space O(1)
|
|
846
|
+
* @param elementOrNode - Element or node to prepend.
|
|
847
|
+
* @returns True when prepended.
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
* @example
|
|
860
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
861
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
862
|
+
*
|
|
863
|
+
* // Unshift adds to the beginning
|
|
864
|
+
* list.unshift(10);
|
|
865
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
866
|
+
*
|
|
867
|
+
* // Access elements (forward traversal only for singly linked)
|
|
868
|
+
* const second = list.at(1);
|
|
869
|
+
* console.log(second); // 20;
|
|
870
|
+
*
|
|
871
|
+
* // SinglyLinkedList allows forward iteration only
|
|
872
|
+
* const elements: number[] = [];
|
|
873
|
+
* for (const item of list) {
|
|
874
|
+
* elements.push(item);
|
|
875
|
+
* }
|
|
876
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
877
|
+
*
|
|
878
|
+
* console.log(list.length); // 4;
|
|
879
|
+
*/
|
|
829
880
|
unshift(elementOrNode) {
|
|
830
881
|
const newNode = this._ensureNode(elementOrNode);
|
|
831
882
|
if (!this.head) {
|
|
@@ -881,11 +932,28 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
881
932
|
return void 0;
|
|
882
933
|
}
|
|
883
934
|
/**
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
935
|
+
* Get the element at a given index.
|
|
936
|
+
* @remarks Time O(N), Space O(1)
|
|
937
|
+
* @param index - Zero-based index.
|
|
938
|
+
* @returns Element or undefined.
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
* @example
|
|
951
|
+
* // Access element by index
|
|
952
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
953
|
+
* console.log(list.at(0)); // 'a';
|
|
954
|
+
* console.log(list.at(2)); // 'c';
|
|
955
|
+
* console.log(list.at(3)); // 'd';
|
|
956
|
+
*/
|
|
889
957
|
at(index) {
|
|
890
958
|
if (index < 0 || index >= this._length) return void 0;
|
|
891
959
|
let current = this.head;
|
|
@@ -902,11 +970,23 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
902
970
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
903
971
|
}
|
|
904
972
|
/**
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
973
|
+
* Get the node reference at a given index.
|
|
974
|
+
* @remarks Time O(N), Space O(1)
|
|
975
|
+
* @param index - Zero-based index.
|
|
976
|
+
* @returns Node or undefined.
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
* @example
|
|
986
|
+
* // Get node at index
|
|
987
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
988
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
989
|
+
*/
|
|
910
990
|
getNodeAt(index) {
|
|
911
991
|
if (index < 0 || index >= this._length) return void 0;
|
|
912
992
|
let current = this.head;
|
|
@@ -914,11 +994,24 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
914
994
|
return current;
|
|
915
995
|
}
|
|
916
996
|
/**
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
997
|
+
* Delete the element at an index.
|
|
998
|
+
* @remarks Time O(N), Space O(1)
|
|
999
|
+
* @param index - Zero-based index.
|
|
1000
|
+
* @returns Removed element or undefined.
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
* @example
|
|
1010
|
+
* // Remove by index
|
|
1011
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1012
|
+
* list.deleteAt(1);
|
|
1013
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1014
|
+
*/
|
|
922
1015
|
deleteAt(index) {
|
|
923
1016
|
if (index < 0 || index >= this._length) return void 0;
|
|
924
1017
|
if (index === 0) return this.shift();
|
|
@@ -931,11 +1024,24 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
931
1024
|
return value;
|
|
932
1025
|
}
|
|
933
1026
|
/**
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
1027
|
+
* Delete the first match by value/node.
|
|
1028
|
+
* @remarks Time O(N), Space O(1)
|
|
1029
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1030
|
+
* @returns True if removed.
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
* @example
|
|
1040
|
+
* // Remove first occurrence
|
|
1041
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1042
|
+
* list.delete(2);
|
|
1043
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1044
|
+
*/
|
|
939
1045
|
delete(elementOrNode) {
|
|
940
1046
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
941
1047
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -952,12 +1058,25 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
952
1058
|
return true;
|
|
953
1059
|
}
|
|
954
1060
|
/**
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
1061
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1062
|
+
* @remarks Time O(N), Space O(1)
|
|
1063
|
+
* @param index - Zero-based index.
|
|
1064
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1065
|
+
* @returns True if inserted.
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
* @example
|
|
1075
|
+
* // Insert at index
|
|
1076
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1077
|
+
* list.addAt(1, 2);
|
|
1078
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1079
|
+
*/
|
|
961
1080
|
addAt(index, newElementOrNode) {
|
|
962
1081
|
if (index < 0 || index > this._length) return false;
|
|
963
1082
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -983,28 +1102,70 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
983
1102
|
return true;
|
|
984
1103
|
}
|
|
985
1104
|
/**
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1105
|
+
* Check whether the list is empty.
|
|
1106
|
+
* @remarks Time O(1), Space O(1)
|
|
1107
|
+
* @returns True if length is 0.
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
* @example
|
|
1118
|
+
* // Check empty
|
|
1119
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1120
|
+
*/
|
|
990
1121
|
isEmpty() {
|
|
991
1122
|
return this._length === 0;
|
|
992
1123
|
}
|
|
993
1124
|
/**
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1125
|
+
* Remove all nodes and reset length.
|
|
1126
|
+
* @remarks Time O(N), Space O(1)
|
|
1127
|
+
* @returns void
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
* @example
|
|
1138
|
+
* // Remove all
|
|
1139
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1140
|
+
* list.clear();
|
|
1141
|
+
* console.log(list.isEmpty()); // true;
|
|
1142
|
+
*/
|
|
998
1143
|
clear() {
|
|
999
1144
|
this._head = void 0;
|
|
1000
1145
|
this._tail = void 0;
|
|
1001
1146
|
this._length = 0;
|
|
1002
1147
|
}
|
|
1003
1148
|
/**
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1149
|
+
* Reverse the list in place.
|
|
1150
|
+
* @remarks Time O(N), Space O(1)
|
|
1151
|
+
* @returns This list.
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
* @example
|
|
1164
|
+
* // Reverse the list in-place
|
|
1165
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1166
|
+
* list.reverse();
|
|
1167
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1168
|
+
*/
|
|
1008
1169
|
reverse() {
|
|
1009
1170
|
if (!this.head || this.head === this.tail) return this;
|
|
1010
1171
|
let prev;
|
|
@@ -1179,22 +1340,64 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1179
1340
|
return false;
|
|
1180
1341
|
}
|
|
1181
1342
|
/**
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1343
|
+
* Deep clone this list (values are copied by reference).
|
|
1344
|
+
* @remarks Time O(N), Space O(N)
|
|
1345
|
+
* @returns A new list with the same element sequence.
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
* @example
|
|
1356
|
+
* // Deep copy
|
|
1357
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1358
|
+
* const copy = list.clone();
|
|
1359
|
+
* copy.pop();
|
|
1360
|
+
* console.log(list.length); // 3;
|
|
1361
|
+
* console.log(copy.length); // 2;
|
|
1362
|
+
*/
|
|
1186
1363
|
clone() {
|
|
1187
1364
|
const out = this._createInstance();
|
|
1188
1365
|
for (const v of this) out.push(v);
|
|
1189
1366
|
return out;
|
|
1190
1367
|
}
|
|
1191
1368
|
/**
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1369
|
+
* Filter values into a new list of the same class.
|
|
1370
|
+
* @remarks Time O(N), Space O(N)
|
|
1371
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1372
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1373
|
+
* @returns A new list with kept values.
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
* @example
|
|
1386
|
+
* // SinglyLinkedList filter and map operations
|
|
1387
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1388
|
+
*
|
|
1389
|
+
* // Filter even numbers
|
|
1390
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1391
|
+
* console.log(filtered.length); // 2;
|
|
1392
|
+
*
|
|
1393
|
+
* // Map to double values
|
|
1394
|
+
* const doubled = list.map(value => value * 2);
|
|
1395
|
+
* console.log(doubled.length); // 5;
|
|
1396
|
+
*
|
|
1397
|
+
* // Use reduce to sum
|
|
1398
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1399
|
+
* console.log(sum); // 15;
|
|
1400
|
+
*/
|
|
1198
1401
|
filter(callback, thisArg) {
|
|
1199
1402
|
const out = this._createInstance();
|
|
1200
1403
|
let index = 0;
|
|
@@ -1218,15 +1421,31 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1218
1421
|
return out;
|
|
1219
1422
|
}
|
|
1220
1423
|
/**
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1424
|
+
* Map values into a new list (possibly different element type).
|
|
1425
|
+
* @remarks Time O(N), Space O(N)
|
|
1426
|
+
* @template EM
|
|
1427
|
+
* @template RM
|
|
1428
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1429
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1430
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1431
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
|
|
1438
|
+
|
|
1439
|
+
|
|
1440
|
+
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
* @example
|
|
1444
|
+
* // Transform elements
|
|
1445
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1446
|
+
* const doubled = list.map(n => n * 2);
|
|
1447
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1448
|
+
*/
|
|
1230
1449
|
map(callback, options, thisArg) {
|
|
1231
1450
|
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1232
1451
|
let index = 0;
|
|
@@ -1363,6 +1582,54 @@ function elementOrPredicate(input, equals) {
|
|
|
1363
1582
|
}
|
|
1364
1583
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
1365
1584
|
|
|
1585
|
+
// src/common/error.ts
|
|
1586
|
+
var ERR = {
|
|
1587
|
+
// Range / index
|
|
1588
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1589
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1590
|
+
// Type / argument
|
|
1591
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1592
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1593
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1594
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1595
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1596
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1597
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1598
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1599
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1600
|
+
// State / operation
|
|
1601
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1602
|
+
// Matrix
|
|
1603
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1604
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1605
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1606
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1607
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1608
|
+
};
|
|
1609
|
+
|
|
1610
|
+
// src/common/index.ts
|
|
1611
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1612
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1613
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1614
|
+
return DFSOperation2;
|
|
1615
|
+
})(DFSOperation || {});
|
|
1616
|
+
var _Range = class _Range {
|
|
1617
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1618
|
+
this.low = low;
|
|
1619
|
+
this.high = high;
|
|
1620
|
+
this.includeLow = includeLow;
|
|
1621
|
+
this.includeHigh = includeHigh;
|
|
1622
|
+
}
|
|
1623
|
+
// Determine whether a key is within the range
|
|
1624
|
+
isInRange(key, comparator) {
|
|
1625
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1626
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1627
|
+
return lowCheck && highCheck;
|
|
1628
|
+
}
|
|
1629
|
+
};
|
|
1630
|
+
__name(_Range, "Range");
|
|
1631
|
+
var Range = _Range;
|
|
1632
|
+
|
|
1366
1633
|
// src/data-structures/queue/queue.ts
|
|
1367
1634
|
var _Queue = class _Queue extends LinearBase {
|
|
1368
1635
|
/**
|
|
@@ -1417,18 +1684,52 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1417
1684
|
this._autoCompactRatio = value;
|
|
1418
1685
|
}
|
|
1419
1686
|
/**
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1687
|
+
* Get the number of elements currently in the queue.
|
|
1688
|
+
* @remarks Time O(1), Space O(1)
|
|
1689
|
+
* @returns Current length.
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
|
|
1694
|
+
|
|
1695
|
+
|
|
1696
|
+
|
|
1697
|
+
|
|
1698
|
+
|
|
1699
|
+
|
|
1700
|
+
|
|
1701
|
+
* @example
|
|
1702
|
+
* // Track queue length
|
|
1703
|
+
* const q = new Queue<number>();
|
|
1704
|
+
* console.log(q.length); // 0;
|
|
1705
|
+
* q.push(1);
|
|
1706
|
+
* q.push(2);
|
|
1707
|
+
* console.log(q.length); // 2;
|
|
1708
|
+
*/
|
|
1424
1709
|
get length() {
|
|
1425
1710
|
return this.elements.length - this._offset;
|
|
1426
1711
|
}
|
|
1427
1712
|
/**
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1713
|
+
* Get the first element (front) without removing it.
|
|
1714
|
+
* @remarks Time O(1), Space O(1)
|
|
1715
|
+
* @returns Front element or undefined.
|
|
1716
|
+
|
|
1717
|
+
|
|
1718
|
+
|
|
1719
|
+
|
|
1720
|
+
|
|
1721
|
+
|
|
1722
|
+
|
|
1723
|
+
|
|
1724
|
+
|
|
1725
|
+
|
|
1726
|
+
|
|
1727
|
+
* @example
|
|
1728
|
+
* // View the front element
|
|
1729
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1730
|
+
* console.log(q.first); // 'first';
|
|
1731
|
+
* console.log(q.length); // 3;
|
|
1732
|
+
*/
|
|
1432
1733
|
get first() {
|
|
1433
1734
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1434
1735
|
}
|
|
@@ -1451,19 +1752,69 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1451
1752
|
return new _Queue(elements);
|
|
1452
1753
|
}
|
|
1453
1754
|
/**
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1755
|
+
* Check whether the queue is empty.
|
|
1756
|
+
* @remarks Time O(1), Space O(1)
|
|
1757
|
+
* @returns True if length is 0.
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
|
|
1762
|
+
|
|
1763
|
+
|
|
1764
|
+
|
|
1765
|
+
|
|
1766
|
+
|
|
1767
|
+
|
|
1768
|
+
|
|
1769
|
+
* @example
|
|
1770
|
+
* // Queue for...of iteration and isEmpty check
|
|
1771
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1772
|
+
*
|
|
1773
|
+
* const elements: string[] = [];
|
|
1774
|
+
* for (const item of queue) {
|
|
1775
|
+
* elements.push(item);
|
|
1776
|
+
* }
|
|
1777
|
+
*
|
|
1778
|
+
* // Verify all elements are iterated in order
|
|
1779
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1780
|
+
*
|
|
1781
|
+
* // Process all elements
|
|
1782
|
+
* while (queue.length > 0) {
|
|
1783
|
+
* queue.shift();
|
|
1784
|
+
* }
|
|
1785
|
+
*
|
|
1786
|
+
* console.log(queue.length); // 0;
|
|
1787
|
+
*/
|
|
1458
1788
|
isEmpty() {
|
|
1459
1789
|
return this.length === 0;
|
|
1460
1790
|
}
|
|
1461
1791
|
/**
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1792
|
+
* Enqueue one element at the back.
|
|
1793
|
+
* @remarks Time O(1), Space O(1)
|
|
1794
|
+
* @param element - Element to enqueue.
|
|
1795
|
+
* @returns True on success.
|
|
1796
|
+
|
|
1797
|
+
|
|
1798
|
+
|
|
1799
|
+
|
|
1800
|
+
|
|
1801
|
+
|
|
1802
|
+
|
|
1803
|
+
|
|
1804
|
+
|
|
1805
|
+
|
|
1806
|
+
|
|
1807
|
+
* @example
|
|
1808
|
+
* // basic Queue creation and push operation
|
|
1809
|
+
* // Create a simple Queue with initial values
|
|
1810
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1811
|
+
*
|
|
1812
|
+
* // Verify the queue maintains insertion order
|
|
1813
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1814
|
+
*
|
|
1815
|
+
* // Check length
|
|
1816
|
+
* console.log(queue.length); // 5;
|
|
1817
|
+
*/
|
|
1467
1818
|
push(element) {
|
|
1468
1819
|
this.elements.push(element);
|
|
1469
1820
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1484,10 +1835,35 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1484
1835
|
return ans;
|
|
1485
1836
|
}
|
|
1486
1837
|
/**
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1838
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1839
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1840
|
+
* @returns Removed element or undefined.
|
|
1841
|
+
|
|
1842
|
+
|
|
1843
|
+
|
|
1844
|
+
|
|
1845
|
+
|
|
1846
|
+
|
|
1847
|
+
|
|
1848
|
+
|
|
1849
|
+
|
|
1850
|
+
|
|
1851
|
+
|
|
1852
|
+
* @example
|
|
1853
|
+
* // Queue shift and peek operations
|
|
1854
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1855
|
+
*
|
|
1856
|
+
* // Peek at the front element without removing it
|
|
1857
|
+
* console.log(queue.first); // 10;
|
|
1858
|
+
*
|
|
1859
|
+
* // Remove and get the first element (FIFO)
|
|
1860
|
+
* const first = queue.shift();
|
|
1861
|
+
* console.log(first); // 10;
|
|
1862
|
+
*
|
|
1863
|
+
* // Verify remaining elements and length decreased
|
|
1864
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1865
|
+
* console.log(queue.length); // 3;
|
|
1866
|
+
*/
|
|
1491
1867
|
shift() {
|
|
1492
1868
|
if (this.length === 0) return void 0;
|
|
1493
1869
|
const first = this.first;
|
|
@@ -1496,11 +1872,24 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1496
1872
|
return first;
|
|
1497
1873
|
}
|
|
1498
1874
|
/**
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1875
|
+
* Delete the first occurrence of a specific element.
|
|
1876
|
+
* @remarks Time O(N), Space O(1)
|
|
1877
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1878
|
+
* @returns True if an element was removed.
|
|
1879
|
+
|
|
1880
|
+
|
|
1881
|
+
|
|
1882
|
+
|
|
1883
|
+
|
|
1884
|
+
|
|
1885
|
+
|
|
1886
|
+
|
|
1887
|
+
* @example
|
|
1888
|
+
* // Remove specific element
|
|
1889
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1890
|
+
* q.delete(2);
|
|
1891
|
+
* console.log(q.length); // 3;
|
|
1892
|
+
*/
|
|
1504
1893
|
delete(element) {
|
|
1505
1894
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1506
1895
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1511,11 +1900,24 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1511
1900
|
return false;
|
|
1512
1901
|
}
|
|
1513
1902
|
/**
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1903
|
+
* Get the element at a given logical index.
|
|
1904
|
+
* @remarks Time O(1), Space O(1)
|
|
1905
|
+
* @param index - Zero-based index from the front.
|
|
1906
|
+
* @returns Element or undefined.
|
|
1907
|
+
|
|
1908
|
+
|
|
1909
|
+
|
|
1910
|
+
|
|
1911
|
+
|
|
1912
|
+
|
|
1913
|
+
|
|
1914
|
+
|
|
1915
|
+
* @example
|
|
1916
|
+
* // Access element by index
|
|
1917
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1918
|
+
* console.log(q.at(0)); // 'a';
|
|
1919
|
+
* console.log(q.at(2)); // 'c';
|
|
1920
|
+
*/
|
|
1519
1921
|
at(index) {
|
|
1520
1922
|
if (index < 0 || index >= this.length) return void 0;
|
|
1521
1923
|
return this._elements[this._offset + index];
|
|
@@ -1567,19 +1969,48 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1567
1969
|
return this;
|
|
1568
1970
|
}
|
|
1569
1971
|
/**
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1972
|
+
* Remove all elements and reset offset.
|
|
1973
|
+
* @remarks Time O(1), Space O(1)
|
|
1974
|
+
* @returns void
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
|
|
1982
|
+
|
|
1983
|
+
|
|
1984
|
+
* @example
|
|
1985
|
+
* // Remove all elements
|
|
1986
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1987
|
+
* q.clear();
|
|
1988
|
+
* console.log(q.length); // 0;
|
|
1989
|
+
*/
|
|
1574
1990
|
clear() {
|
|
1575
1991
|
this._elements = [];
|
|
1576
1992
|
this._offset = 0;
|
|
1577
1993
|
}
|
|
1578
1994
|
/**
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1995
|
+
* Compact storage by discarding consumed head elements.
|
|
1996
|
+
* @remarks Time O(N), Space O(N)
|
|
1997
|
+
* @returns True when compaction performed.
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
|
|
2001
|
+
|
|
2002
|
+
|
|
2003
|
+
|
|
2004
|
+
|
|
2005
|
+
|
|
2006
|
+
* @example
|
|
2007
|
+
* // Reclaim unused memory
|
|
2008
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2009
|
+
* q.shift();
|
|
2010
|
+
* q.shift();
|
|
2011
|
+
* q.compact();
|
|
2012
|
+
* console.log(q.length); // 3;
|
|
2013
|
+
*/
|
|
1583
2014
|
compact() {
|
|
1584
2015
|
this._elements = this.elements.slice(this._offset);
|
|
1585
2016
|
this._offset = 0;
|
|
@@ -1605,10 +2036,26 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1605
2036
|
return removed;
|
|
1606
2037
|
}
|
|
1607
2038
|
/**
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
2039
|
+
* Deep clone this queue and its parameters.
|
|
2040
|
+
* @remarks Time O(N), Space O(N)
|
|
2041
|
+
* @returns A new queue with the same content and options.
|
|
2042
|
+
|
|
2043
|
+
|
|
2044
|
+
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
|
|
2051
|
+
* @example
|
|
2052
|
+
* // Create independent copy
|
|
2053
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2054
|
+
* const copy = q.clone();
|
|
2055
|
+
* copy.shift();
|
|
2056
|
+
* console.log(q.length); // 3;
|
|
2057
|
+
* console.log(copy.length); // 2;
|
|
2058
|
+
*/
|
|
1612
2059
|
clone() {
|
|
1613
2060
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1614
2061
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1616,12 +2063,26 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1616
2063
|
return out;
|
|
1617
2064
|
}
|
|
1618
2065
|
/**
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
2066
|
+
* Filter elements into a new queue of the same class.
|
|
2067
|
+
* @remarks Time O(N), Space O(N)
|
|
2068
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
2069
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2070
|
+
* @returns A new queue with kept elements.
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
|
|
2074
|
+
|
|
2075
|
+
|
|
2076
|
+
|
|
2077
|
+
|
|
2078
|
+
|
|
2079
|
+
|
|
2080
|
+
* @example
|
|
2081
|
+
* // Filter elements
|
|
2082
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2083
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
2084
|
+
* console.log(evens.length); // 2;
|
|
2085
|
+
*/
|
|
1625
2086
|
filter(predicate, thisArg) {
|
|
1626
2087
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1627
2088
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1633,15 +2094,28 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1633
2094
|
return out;
|
|
1634
2095
|
}
|
|
1635
2096
|
/**
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
2097
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
2098
|
+
* @remarks Time O(N), Space O(N)
|
|
2099
|
+
* @template EM
|
|
2100
|
+
* @template RM
|
|
2101
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
2102
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
2103
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2104
|
+
* @returns A new Queue with mapped elements.
|
|
2105
|
+
|
|
2106
|
+
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
|
|
2110
|
+
|
|
2111
|
+
|
|
2112
|
+
|
|
2113
|
+
* @example
|
|
2114
|
+
* // Transform elements
|
|
2115
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
2116
|
+
* const doubled = q.map(x => x * 2);
|
|
2117
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2118
|
+
*/
|
|
1645
2119
|
map(callback, options, thisArg) {
|
|
1646
2120
|
var _a, _b;
|
|
1647
2121
|
const out = new this.constructor([], {
|
|
@@ -1876,19 +2350,60 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1876
2350
|
return this._length;
|
|
1877
2351
|
}
|
|
1878
2352
|
/**
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
2353
|
+
* Get the first element without removing it.
|
|
2354
|
+
* @remarks Time O(1), Space O(1)
|
|
2355
|
+
* @returns First element or undefined.
|
|
2356
|
+
|
|
2357
|
+
|
|
2358
|
+
|
|
2359
|
+
|
|
2360
|
+
|
|
2361
|
+
|
|
2362
|
+
|
|
2363
|
+
|
|
2364
|
+
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
* @example
|
|
2368
|
+
* // Deque peek at both ends
|
|
2369
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2370
|
+
*
|
|
2371
|
+
* // Get first element without removing
|
|
2372
|
+
* const first = deque.at(0);
|
|
2373
|
+
* console.log(first); // 10;
|
|
2374
|
+
*
|
|
2375
|
+
* // Get last element without removing
|
|
2376
|
+
* const last = deque.at(deque.length - 1);
|
|
2377
|
+
* console.log(last); // 50;
|
|
2378
|
+
*
|
|
2379
|
+
* // Length unchanged
|
|
2380
|
+
* console.log(deque.length); // 5;
|
|
2381
|
+
*/
|
|
1883
2382
|
get first() {
|
|
1884
2383
|
if (this._length === 0) return;
|
|
1885
2384
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
1886
2385
|
}
|
|
1887
2386
|
/**
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
2387
|
+
* Get the last element without removing it.
|
|
2388
|
+
* @remarks Time O(1), Space O(1)
|
|
2389
|
+
* @returns Last element or undefined.
|
|
2390
|
+
|
|
2391
|
+
|
|
2392
|
+
|
|
2393
|
+
|
|
2394
|
+
|
|
2395
|
+
|
|
2396
|
+
|
|
2397
|
+
|
|
2398
|
+
|
|
2399
|
+
|
|
2400
|
+
|
|
2401
|
+
* @example
|
|
2402
|
+
* // Peek at the back element
|
|
2403
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2404
|
+
* console.log(dq.last); // 'c';
|
|
2405
|
+
* console.log(dq.first); // 'a';
|
|
2406
|
+
*/
|
|
1892
2407
|
get last() {
|
|
1893
2408
|
if (this._length === 0) return;
|
|
1894
2409
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1907,11 +2422,40 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1907
2422
|
return new this(data, options);
|
|
1908
2423
|
}
|
|
1909
2424
|
/**
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
2425
|
+
* Append one element at the back.
|
|
2426
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2427
|
+
* @param element - Element to append.
|
|
2428
|
+
* @returns True when appended.
|
|
2429
|
+
|
|
2430
|
+
|
|
2431
|
+
|
|
2432
|
+
|
|
2433
|
+
|
|
2434
|
+
|
|
2435
|
+
|
|
2436
|
+
|
|
2437
|
+
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
* @example
|
|
2441
|
+
* // basic Deque creation and push/pop operations
|
|
2442
|
+
* // Create a simple Deque with initial values
|
|
2443
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
2444
|
+
*
|
|
2445
|
+
* // Verify the deque maintains insertion order
|
|
2446
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
2447
|
+
*
|
|
2448
|
+
* // Check length
|
|
2449
|
+
* console.log(deque.length); // 5;
|
|
2450
|
+
*
|
|
2451
|
+
* // Push to the end
|
|
2452
|
+
* deque.push(6);
|
|
2453
|
+
* console.log(deque.length); // 6;
|
|
2454
|
+
*
|
|
2455
|
+
* // Pop from the end
|
|
2456
|
+
* const last = deque.pop();
|
|
2457
|
+
* console.log(last); // 6;
|
|
2458
|
+
*/
|
|
1915
2459
|
push(element) {
|
|
1916
2460
|
if (this._length) {
|
|
1917
2461
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -1931,10 +2475,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1931
2475
|
return true;
|
|
1932
2476
|
}
|
|
1933
2477
|
/**
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
2478
|
+
* Remove and return the last element.
|
|
2479
|
+
* @remarks Time O(1), Space O(1)
|
|
2480
|
+
* @returns Removed element or undefined.
|
|
2481
|
+
|
|
2482
|
+
|
|
2483
|
+
|
|
2484
|
+
|
|
2485
|
+
|
|
2486
|
+
|
|
2487
|
+
|
|
2488
|
+
|
|
2489
|
+
|
|
2490
|
+
|
|
2491
|
+
|
|
2492
|
+
* @example
|
|
2493
|
+
* // Remove from the back
|
|
2494
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2495
|
+
* console.log(dq.pop()); // 3;
|
|
2496
|
+
* console.log(dq.length); // 2;
|
|
2497
|
+
*/
|
|
1938
2498
|
pop() {
|
|
1939
2499
|
if (this._length === 0) return;
|
|
1940
2500
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -1954,10 +2514,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1954
2514
|
return element;
|
|
1955
2515
|
}
|
|
1956
2516
|
/**
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
2517
|
+
* Remove and return the first element.
|
|
2518
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2519
|
+
* @returns Removed element or undefined.
|
|
2520
|
+
|
|
2521
|
+
|
|
2522
|
+
|
|
2523
|
+
|
|
2524
|
+
|
|
2525
|
+
|
|
2526
|
+
|
|
2527
|
+
|
|
2528
|
+
|
|
2529
|
+
|
|
2530
|
+
|
|
2531
|
+
* @example
|
|
2532
|
+
* // Remove from the front
|
|
2533
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2534
|
+
* console.log(dq.shift()); // 1;
|
|
2535
|
+
* console.log(dq.length); // 2;
|
|
2536
|
+
*/
|
|
1961
2537
|
shift() {
|
|
1962
2538
|
if (this._length === 0) return;
|
|
1963
2539
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -1977,11 +2553,37 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1977
2553
|
return element;
|
|
1978
2554
|
}
|
|
1979
2555
|
/**
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
2556
|
+
* Prepend one element at the front.
|
|
2557
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2558
|
+
* @param element - Element to prepend.
|
|
2559
|
+
* @returns True when prepended.
|
|
2560
|
+
|
|
2561
|
+
|
|
2562
|
+
|
|
2563
|
+
|
|
2564
|
+
|
|
2565
|
+
|
|
2566
|
+
|
|
2567
|
+
|
|
2568
|
+
|
|
2569
|
+
|
|
2570
|
+
|
|
2571
|
+
* @example
|
|
2572
|
+
* // Deque shift and unshift operations
|
|
2573
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
2574
|
+
*
|
|
2575
|
+
* // Unshift adds to the front
|
|
2576
|
+
* deque.unshift(10);
|
|
2577
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
2578
|
+
*
|
|
2579
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
2580
|
+
* const first = deque.shift();
|
|
2581
|
+
* console.log(first); // 10;
|
|
2582
|
+
*
|
|
2583
|
+
* // Verify remaining elements
|
|
2584
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
2585
|
+
* console.log(deque.length); // 3;
|
|
2586
|
+
*/
|
|
1985
2587
|
unshift(element) {
|
|
1986
2588
|
if (this._length) {
|
|
1987
2589
|
if (this._firstInBucket > 0) {
|
|
@@ -2035,18 +2637,45 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2035
2637
|
return ans;
|
|
2036
2638
|
}
|
|
2037
2639
|
/**
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2640
|
+
* Check whether the deque is empty.
|
|
2641
|
+
* @remarks Time O(1), Space O(1)
|
|
2642
|
+
* @returns True if length is 0.
|
|
2643
|
+
|
|
2644
|
+
|
|
2645
|
+
|
|
2646
|
+
|
|
2647
|
+
|
|
2648
|
+
|
|
2649
|
+
|
|
2650
|
+
|
|
2651
|
+
|
|
2652
|
+
* @example
|
|
2653
|
+
* // Check if empty
|
|
2654
|
+
* const dq = new Deque();
|
|
2655
|
+
* console.log(dq.isEmpty()); // true;
|
|
2656
|
+
*/
|
|
2042
2657
|
isEmpty() {
|
|
2043
2658
|
return this._length === 0;
|
|
2044
2659
|
}
|
|
2045
2660
|
/**
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2661
|
+
* Remove all elements and reset structure.
|
|
2662
|
+
* @remarks Time O(1), Space O(1)
|
|
2663
|
+
* @returns void
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
* @example
|
|
2674
|
+
* // Remove all elements
|
|
2675
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2676
|
+
* dq.clear();
|
|
2677
|
+
* console.log(dq.length); // 0;
|
|
2678
|
+
*/
|
|
2050
2679
|
clear() {
|
|
2051
2680
|
this._buckets = [new Array(this._bucketSize)];
|
|
2052
2681
|
this._bucketCount = 1;
|
|
@@ -2054,11 +2683,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2054
2683
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
2055
2684
|
}
|
|
2056
2685
|
/**
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2686
|
+
* Get the element at a given position.
|
|
2687
|
+
* @remarks Time O(1), Space O(1)
|
|
2688
|
+
* @param pos - Zero-based position from the front.
|
|
2689
|
+
* @returns Element or undefined.
|
|
2690
|
+
|
|
2691
|
+
|
|
2692
|
+
|
|
2693
|
+
|
|
2694
|
+
|
|
2695
|
+
|
|
2696
|
+
|
|
2697
|
+
|
|
2698
|
+
* @example
|
|
2699
|
+
* // Access by index
|
|
2700
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2701
|
+
* console.log(dq.at(0)); // 'a';
|
|
2702
|
+
* console.log(dq.at(2)); // 'c';
|
|
2703
|
+
*/
|
|
2062
2704
|
at(pos) {
|
|
2063
2705
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
2064
2706
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -2217,11 +2859,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2217
2859
|
}
|
|
2218
2860
|
}
|
|
2219
2861
|
/**
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2862
|
+
* Delete the first occurrence of a value.
|
|
2863
|
+
* @remarks Time O(N), Space O(1)
|
|
2864
|
+
* @param element - Element to remove (using the configured equality).
|
|
2865
|
+
* @returns True if an element was removed.
|
|
2866
|
+
|
|
2867
|
+
|
|
2868
|
+
|
|
2869
|
+
|
|
2870
|
+
|
|
2871
|
+
|
|
2872
|
+
|
|
2873
|
+
|
|
2874
|
+
* @example
|
|
2875
|
+
* // Remove element
|
|
2876
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2877
|
+
* dq.delete(2);
|
|
2878
|
+
* console.log(dq.length); // 2;
|
|
2879
|
+
*/
|
|
2225
2880
|
delete(element) {
|
|
2226
2881
|
const size = this._length;
|
|
2227
2882
|
if (size === 0) return false;
|
|
@@ -2265,10 +2920,39 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2265
2920
|
return this;
|
|
2266
2921
|
}
|
|
2267
2922
|
/**
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2923
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
2924
|
+
* @remarks Time O(N), Space O(N)
|
|
2925
|
+
* @returns This deque.
|
|
2926
|
+
|
|
2927
|
+
|
|
2928
|
+
|
|
2929
|
+
|
|
2930
|
+
|
|
2931
|
+
|
|
2932
|
+
|
|
2933
|
+
|
|
2934
|
+
|
|
2935
|
+
|
|
2936
|
+
|
|
2937
|
+
* @example
|
|
2938
|
+
* // Deque for...of iteration and reverse
|
|
2939
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2940
|
+
*
|
|
2941
|
+
* // Iterate forward
|
|
2942
|
+
* const forward: string[] = [];
|
|
2943
|
+
* for (const item of deque) {
|
|
2944
|
+
* forward.push(item);
|
|
2945
|
+
* }
|
|
2946
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2947
|
+
*
|
|
2948
|
+
* // Reverse the deque
|
|
2949
|
+
* deque.reverse();
|
|
2950
|
+
* const backward: string[] = [];
|
|
2951
|
+
* for (const item of deque) {
|
|
2952
|
+
* backward.push(item);
|
|
2953
|
+
* }
|
|
2954
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2955
|
+
*/
|
|
2272
2956
|
reverse() {
|
|
2273
2957
|
this._buckets.reverse().forEach(function(bucket) {
|
|
2274
2958
|
bucket.reverse();
|
|
@@ -2327,10 +3011,25 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2327
3011
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2328
3012
|
*/
|
|
2329
3013
|
/**
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
3014
|
+
* Compact the deque by removing unused buckets.
|
|
3015
|
+
* @remarks Time O(N), Space O(1)
|
|
3016
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
3017
|
+
|
|
3018
|
+
|
|
3019
|
+
|
|
3020
|
+
|
|
3021
|
+
|
|
3022
|
+
|
|
3023
|
+
|
|
3024
|
+
|
|
3025
|
+
* @example
|
|
3026
|
+
* // Reclaim memory
|
|
3027
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
3028
|
+
* dq.shift();
|
|
3029
|
+
* dq.shift();
|
|
3030
|
+
* dq.compact();
|
|
3031
|
+
* console.log(dq.length); // 3;
|
|
3032
|
+
*/
|
|
2334
3033
|
compact() {
|
|
2335
3034
|
const before = this._bucketCount;
|
|
2336
3035
|
this.shrinkToFit();
|
|
@@ -2358,10 +3057,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2358
3057
|
this._compactCounter = 0;
|
|
2359
3058
|
}
|
|
2360
3059
|
/**
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
3060
|
+
* Deep clone this deque, preserving options.
|
|
3061
|
+
* @remarks Time O(N), Space O(N)
|
|
3062
|
+
* @returns A new deque with the same content and options.
|
|
3063
|
+
|
|
3064
|
+
|
|
3065
|
+
|
|
3066
|
+
|
|
3067
|
+
|
|
3068
|
+
|
|
3069
|
+
|
|
3070
|
+
|
|
3071
|
+
|
|
3072
|
+
* @example
|
|
3073
|
+
* // Create independent copy
|
|
3074
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3075
|
+
* const copy = dq.clone();
|
|
3076
|
+
* copy.pop();
|
|
3077
|
+
* console.log(dq.length); // 3;
|
|
3078
|
+
* console.log(copy.length); // 2;
|
|
3079
|
+
*/
|
|
2365
3080
|
clone() {
|
|
2366
3081
|
return this._createLike(this, {
|
|
2367
3082
|
bucketSize: this.bucketSize,
|
|
@@ -2370,12 +3085,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2370
3085
|
});
|
|
2371
3086
|
}
|
|
2372
3087
|
/**
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
3088
|
+
* Filter elements into a new deque of the same class.
|
|
3089
|
+
* @remarks Time O(N), Space O(N)
|
|
3090
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
3091
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
3092
|
+
* @returns A new deque with kept elements.
|
|
3093
|
+
|
|
3094
|
+
|
|
3095
|
+
|
|
3096
|
+
|
|
3097
|
+
|
|
3098
|
+
|
|
3099
|
+
|
|
3100
|
+
|
|
3101
|
+
|
|
3102
|
+
* @example
|
|
3103
|
+
* // Filter elements
|
|
3104
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
3105
|
+
* const result = dq.filter(x => x > 2);
|
|
3106
|
+
* console.log(result.length); // 2;
|
|
3107
|
+
*/
|
|
2379
3108
|
filter(predicate, thisArg) {
|
|
2380
3109
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2381
3110
|
out._setBucketSize(this._bucketSize);
|
|
@@ -2404,15 +3133,28 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2404
3133
|
return out;
|
|
2405
3134
|
}
|
|
2406
3135
|
/**
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
3136
|
+
* Map elements into a new deque (possibly different element type).
|
|
3137
|
+
* @remarks Time O(N), Space O(N)
|
|
3138
|
+
* @template EM
|
|
3139
|
+
* @template RM
|
|
3140
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
3141
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
3142
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
3143
|
+
* @returns A new Deque with mapped elements.
|
|
3144
|
+
|
|
3145
|
+
|
|
3146
|
+
|
|
3147
|
+
|
|
3148
|
+
|
|
3149
|
+
|
|
3150
|
+
|
|
3151
|
+
|
|
3152
|
+
* @example
|
|
3153
|
+
* // Transform elements
|
|
3154
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
3155
|
+
* const result = dq.map(x => x * 10);
|
|
3156
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
3157
|
+
*/
|
|
2416
3158
|
map(callback, options, thisArg) {
|
|
2417
3159
|
const out = this._createLike([], {
|
|
2418
3160
|
...options != null ? options : {},
|