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