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