data-structure-typed 2.6.1 → 2.6.2
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/CHANGELOG.md +9 -1
- package/dist/cjs/binary-tree.cjs +2927 -23688
- package/dist/cjs/graph.cjs +845 -2634
- package/dist/cjs/hash.cjs +185 -616
- package/dist/cjs/heap.cjs +266 -818
- package/dist/cjs/index.cjs +5116 -31358
- package/dist/cjs/linked-list.cjs +644 -2615
- package/dist/cjs/matrix.cjs +220 -535
- package/dist/cjs/priority-queue.cjs +266 -818
- package/dist/cjs/queue.cjs +637 -2343
- package/dist/cjs/stack.cjs +124 -530
- package/dist/cjs/trie.cjs +145 -592
- package/dist/cjs-legacy/binary-tree.cjs +4352 -25113
- package/dist/cjs-legacy/graph.cjs +847 -2636
- package/dist/cjs-legacy/hash.cjs +181 -612
- package/dist/cjs-legacy/heap.cjs +266 -818
- package/dist/cjs-legacy/index.cjs +6657 -32899
- package/dist/cjs-legacy/linked-list.cjs +640 -2611
- package/dist/cjs-legacy/matrix.cjs +220 -535
- package/dist/cjs-legacy/priority-queue.cjs +266 -818
- package/dist/cjs-legacy/queue.cjs +634 -2340
- package/dist/cjs-legacy/stack.cjs +124 -530
- package/dist/cjs-legacy/trie.cjs +145 -592
- package/dist/esm/binary-tree.mjs +2927 -23688
- package/dist/esm/graph.mjs +845 -2634
- package/dist/esm/hash.mjs +185 -616
- package/dist/esm/heap.mjs +266 -818
- package/dist/esm/index.mjs +5116 -31358
- package/dist/esm/linked-list.mjs +644 -2615
- package/dist/esm/matrix.mjs +220 -535
- package/dist/esm/priority-queue.mjs +266 -818
- package/dist/esm/queue.mjs +637 -2343
- package/dist/esm/stack.mjs +124 -530
- package/dist/esm/trie.mjs +145 -592
- package/dist/esm-legacy/binary-tree.mjs +4352 -25113
- package/dist/esm-legacy/graph.mjs +847 -2636
- package/dist/esm-legacy/hash.mjs +181 -612
- package/dist/esm-legacy/heap.mjs +266 -818
- package/dist/esm-legacy/index.mjs +6657 -32899
- package/dist/esm-legacy/linked-list.mjs +640 -2611
- package/dist/esm-legacy/matrix.mjs +220 -535
- package/dist/esm-legacy/priority-queue.mjs +266 -818
- package/dist/esm-legacy/queue.mjs +634 -2340
- package/dist/esm-legacy/stack.mjs +124 -530
- package/dist/esm-legacy/trie.mjs +145 -592
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +29 -500
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +38 -563
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +230 -1212
- package/dist/types/data-structures/binary-tree/bst.d.ts +124 -1063
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +99 -854
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +62 -314
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +366 -5057
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +277 -4885
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +253 -3929
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +261 -4782
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -44
- package/dist/types/data-structures/graph/directed-graph.d.ts +130 -527
- package/dist/types/data-structures/graph/undirected-graph.d.ts +125 -482
- package/dist/types/data-structures/hash/hash-map.d.ts +132 -562
- package/dist/types/data-structures/heap/heap.d.ts +194 -746
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +120 -809
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -733
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +193 -863
- package/dist/types/data-structures/matrix/matrix.d.ts +159 -474
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -6
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +6 -11
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +196 -804
- package/dist/types/data-structures/queue/queue.d.ts +99 -585
- package/dist/types/data-structures/stack/stack.d.ts +75 -481
- package/dist/types/data-structures/trie/trie.d.ts +98 -584
- package/dist/umd/data-structure-typed.js +6580 -32822
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +162 -215
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/BST.md +155 -208
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +7 -7
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -29
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +104 -158
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +72 -104
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +67 -101
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +52 -48
- package/docs-site-docusaurus/docs/api/classes/Heap.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +17 -17
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +38 -38
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +68 -96
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +57 -52
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +41 -74
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +46 -79
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/Queue.md +54 -78
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +171 -225
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -22
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +68 -98
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +4 -4
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +98 -75
- package/docs-site-docusaurus/docs/api/classes/Stack.md +33 -53
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +57 -124
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +45 -116
- package/docs-site-docusaurus/docs/api/classes/Trie.md +37 -61
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +59 -77
- package/package.json +1 -1
- package/src/data-structures/binary-tree/tree-multi-set.ts +15 -11
package/dist/cjs/queue.cjs
CHANGED
|
@@ -698,7 +698,6 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
698
698
|
static {
|
|
699
699
|
__name(this, "SinglyLinkedList");
|
|
700
700
|
}
|
|
701
|
-
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
702
701
|
/**
|
|
703
702
|
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
704
703
|
* @remarks Time O(N), Space O(N)
|
|
@@ -770,68 +769,26 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
770
769
|
return list;
|
|
771
770
|
}
|
|
772
771
|
/**
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
* @example
|
|
820
|
-
* // basic SinglyLinkedList creation and push operation
|
|
821
|
-
* // Create a simple SinglyLinkedList with initial values
|
|
822
|
-
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
823
|
-
*
|
|
824
|
-
* // Verify the list maintains insertion order
|
|
825
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
826
|
-
*
|
|
827
|
-
* // Check length
|
|
828
|
-
* console.log(list.length); // 5;
|
|
829
|
-
*
|
|
830
|
-
* // Push a new element to the end
|
|
831
|
-
* list.push(6);
|
|
832
|
-
* console.log(list.length); // 6;
|
|
833
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
834
|
-
*/
|
|
772
|
+
* Append an element/node to the tail.
|
|
773
|
+
* @remarks Time O(1), Space O(1)
|
|
774
|
+
* @param elementOrNode - Element or node to append.
|
|
775
|
+
* @returns True when appended.
|
|
776
|
+
* @example
|
|
777
|
+
* // basic SinglyLinkedList creation and push operation
|
|
778
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
779
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
780
|
+
*
|
|
781
|
+
* // Verify the list maintains insertion order
|
|
782
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
783
|
+
*
|
|
784
|
+
* // Check length
|
|
785
|
+
* console.log(list.length); // 5;
|
|
786
|
+
*
|
|
787
|
+
* // Push a new element to the end
|
|
788
|
+
* list.push(6);
|
|
789
|
+
* console.log(list.length); // 6;
|
|
790
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
791
|
+
*/
|
|
835
792
|
push(elementOrNode) {
|
|
836
793
|
const newNode = this._ensureNode(elementOrNode);
|
|
837
794
|
if (!this.head) {
|
|
@@ -845,67 +802,25 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
845
802
|
return true;
|
|
846
803
|
}
|
|
847
804
|
/**
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
* @example
|
|
894
|
-
* // SinglyLinkedList pop and shift operations
|
|
895
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
896
|
-
*
|
|
897
|
-
* // Pop removes from the end
|
|
898
|
-
* const last = list.pop();
|
|
899
|
-
* console.log(last); // 50;
|
|
900
|
-
*
|
|
901
|
-
* // Shift removes from the beginning
|
|
902
|
-
* const first = list.shift();
|
|
903
|
-
* console.log(first); // 10;
|
|
904
|
-
*
|
|
905
|
-
* // Verify remaining elements
|
|
906
|
-
* console.log([...list]); // [20, 30, 40];
|
|
907
|
-
* console.log(list.length); // 3;
|
|
908
|
-
*/
|
|
805
|
+
* Remove and return the tail element.
|
|
806
|
+
* @remarks Time O(N), Space O(1)
|
|
807
|
+
* @returns Removed element or undefined.
|
|
808
|
+
* @example
|
|
809
|
+
* // SinglyLinkedList pop and shift operations
|
|
810
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
811
|
+
*
|
|
812
|
+
* // Pop removes from the end
|
|
813
|
+
* const last = list.pop();
|
|
814
|
+
* console.log(last); // 50;
|
|
815
|
+
*
|
|
816
|
+
* // Shift removes from the beginning
|
|
817
|
+
* const first = list.shift();
|
|
818
|
+
* console.log(first); // 10;
|
|
819
|
+
*
|
|
820
|
+
* // Verify remaining elements
|
|
821
|
+
* console.log([...list]); // [20, 30, 40];
|
|
822
|
+
* console.log(list.length); // 3;
|
|
823
|
+
*/
|
|
909
824
|
pop() {
|
|
910
825
|
if (!this.head) return void 0;
|
|
911
826
|
if (this.head === this.tail) {
|
|
@@ -924,57 +839,15 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
924
839
|
return value;
|
|
925
840
|
}
|
|
926
841
|
/**
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
* @example
|
|
973
|
-
* // Remove from the front
|
|
974
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
975
|
-
* console.log(list.shift()); // 10;
|
|
976
|
-
* console.log(list.length); // 2;
|
|
977
|
-
*/
|
|
842
|
+
* Remove and return the head element.
|
|
843
|
+
* @remarks Time O(1), Space O(1)
|
|
844
|
+
* @returns Removed element or undefined.
|
|
845
|
+
* @example
|
|
846
|
+
* // Remove from the front
|
|
847
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
848
|
+
* console.log(list.shift()); // 10;
|
|
849
|
+
* console.log(list.length); // 2;
|
|
850
|
+
*/
|
|
978
851
|
shift() {
|
|
979
852
|
if (!this.head) return void 0;
|
|
980
853
|
const removed = this.head;
|
|
@@ -984,73 +857,31 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
984
857
|
return removed.value;
|
|
985
858
|
}
|
|
986
859
|
/**
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
* @example
|
|
1034
|
-
* // SinglyLinkedList unshift and forward traversal
|
|
1035
|
-
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
1036
|
-
*
|
|
1037
|
-
* // Unshift adds to the beginning
|
|
1038
|
-
* list.unshift(10);
|
|
1039
|
-
* console.log([...list]); // [10, 20, 30, 40];
|
|
1040
|
-
*
|
|
1041
|
-
* // Access elements (forward traversal only for singly linked)
|
|
1042
|
-
* const second = list.at(1);
|
|
1043
|
-
* console.log(second); // 20;
|
|
1044
|
-
*
|
|
1045
|
-
* // SinglyLinkedList allows forward iteration only
|
|
1046
|
-
* const elements: number[] = [];
|
|
1047
|
-
* for (const item of list) {
|
|
1048
|
-
* elements.push(item);
|
|
1049
|
-
* }
|
|
1050
|
-
* console.log(elements); // [10, 20, 30, 40];
|
|
1051
|
-
*
|
|
1052
|
-
* console.log(list.length); // 4;
|
|
1053
|
-
*/
|
|
860
|
+
* Prepend an element/node to the head.
|
|
861
|
+
* @remarks Time O(1), Space O(1)
|
|
862
|
+
* @param elementOrNode - Element or node to prepend.
|
|
863
|
+
* @returns True when prepended.
|
|
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
|
+
*/
|
|
1054
885
|
unshift(elementOrNode) {
|
|
1055
886
|
const newNode = this._ensureNode(elementOrNode);
|
|
1056
887
|
if (!this.head) {
|
|
@@ -1106,59 +937,17 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1106
937
|
return void 0;
|
|
1107
938
|
}
|
|
1108
939
|
/**
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
* @example
|
|
1156
|
-
* // Access element by index
|
|
1157
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1158
|
-
* console.log(list.at(0)); // 'a';
|
|
1159
|
-
* console.log(list.at(2)); // 'c';
|
|
1160
|
-
* console.log(list.at(3)); // 'd';
|
|
1161
|
-
*/
|
|
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
|
+
* @example
|
|
945
|
+
* // Access element by index
|
|
946
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
947
|
+
* console.log(list.at(0)); // 'a';
|
|
948
|
+
* console.log(list.at(2)); // 'c';
|
|
949
|
+
* console.log(list.at(3)); // 'd';
|
|
950
|
+
*/
|
|
1162
951
|
at(index) {
|
|
1163
952
|
if (index < 0 || index >= this._length) return void 0;
|
|
1164
953
|
let current = this.head;
|
|
@@ -1175,54 +964,15 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1175
964
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
1176
965
|
}
|
|
1177
966
|
/**
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
* @example
|
|
1222
|
-
* // Get node at index
|
|
1223
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1224
|
-
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1225
|
-
*/
|
|
967
|
+
* Get the node reference at a given index.
|
|
968
|
+
* @remarks Time O(N), Space O(1)
|
|
969
|
+
* @param index - Zero-based index.
|
|
970
|
+
* @returns Node or undefined.
|
|
971
|
+
* @example
|
|
972
|
+
* // Get node at index
|
|
973
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
974
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
975
|
+
*/
|
|
1226
976
|
getNodeAt(index) {
|
|
1227
977
|
if (index < 0 || index >= this._length) return void 0;
|
|
1228
978
|
let current = this.head;
|
|
@@ -1230,55 +980,16 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1230
980
|
return current;
|
|
1231
981
|
}
|
|
1232
982
|
/**
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
* @example
|
|
1277
|
-
* // Remove by index
|
|
1278
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1279
|
-
* list.deleteAt(1);
|
|
1280
|
-
* console.log(list.toArray()); // ['a', 'c'];
|
|
1281
|
-
*/
|
|
983
|
+
* Delete the element at an index.
|
|
984
|
+
* @remarks Time O(N), Space O(1)
|
|
985
|
+
* @param index - Zero-based index.
|
|
986
|
+
* @returns Removed element or undefined.
|
|
987
|
+
* @example
|
|
988
|
+
* // Remove by index
|
|
989
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
990
|
+
* list.deleteAt(1);
|
|
991
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
992
|
+
*/
|
|
1282
993
|
deleteAt(index) {
|
|
1283
994
|
if (index < 0 || index >= this._length) return void 0;
|
|
1284
995
|
if (index === 0) return this.shift();
|
|
@@ -1291,55 +1002,16 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1291
1002
|
return value;
|
|
1292
1003
|
}
|
|
1293
1004
|
/**
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
* @example
|
|
1338
|
-
* // Remove first occurrence
|
|
1339
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1340
|
-
* list.delete(2);
|
|
1341
|
-
* console.log(list.toArray()); // [1, 3, 2];
|
|
1342
|
-
*/
|
|
1005
|
+
* Delete the first match by value/node.
|
|
1006
|
+
* @remarks Time O(N), Space O(1)
|
|
1007
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1008
|
+
* @returns True if removed.
|
|
1009
|
+
* @example
|
|
1010
|
+
* // Remove first occurrence
|
|
1011
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1012
|
+
* list.delete(2);
|
|
1013
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1014
|
+
*/
|
|
1343
1015
|
delete(elementOrNode) {
|
|
1344
1016
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
1345
1017
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -1356,56 +1028,17 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1356
1028
|
return true;
|
|
1357
1029
|
}
|
|
1358
1030
|
/**
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
* @example
|
|
1404
|
-
* // Insert at index
|
|
1405
|
-
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1406
|
-
* list.addAt(1, 2);
|
|
1407
|
-
* console.log(list.toArray()); // [1, 2, 3];
|
|
1408
|
-
*/
|
|
1031
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1032
|
+
* @remarks Time O(N), Space O(1)
|
|
1033
|
+
* @param index - Zero-based index.
|
|
1034
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1035
|
+
* @returns True if inserted.
|
|
1036
|
+
* @example
|
|
1037
|
+
* // Insert at index
|
|
1038
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1039
|
+
* list.addAt(1, 2);
|
|
1040
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1041
|
+
*/
|
|
1409
1042
|
addAt(index, newElementOrNode) {
|
|
1410
1043
|
if (index < 0 || index > this._length) return false;
|
|
1411
1044
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -1431,163 +1064,41 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1431
1064
|
return true;
|
|
1432
1065
|
}
|
|
1433
1066
|
/**
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
* @example
|
|
1478
|
-
* // Check empty
|
|
1479
|
-
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1480
|
-
*/
|
|
1067
|
+
* Check whether the list is empty.
|
|
1068
|
+
* @remarks Time O(1), Space O(1)
|
|
1069
|
+
* @returns True if length is 0.
|
|
1070
|
+
* @example
|
|
1071
|
+
* // Check empty
|
|
1072
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1073
|
+
*/
|
|
1481
1074
|
isEmpty() {
|
|
1482
1075
|
return this._length === 0;
|
|
1483
1076
|
}
|
|
1484
1077
|
/**
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
* @example
|
|
1529
|
-
* // Remove all
|
|
1530
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1531
|
-
* list.clear();
|
|
1532
|
-
* console.log(list.isEmpty()); // true;
|
|
1533
|
-
*/
|
|
1078
|
+
* Remove all nodes and reset length.
|
|
1079
|
+
* @remarks Time O(N), Space O(1)
|
|
1080
|
+
* @returns void
|
|
1081
|
+
* @example
|
|
1082
|
+
* // Remove all
|
|
1083
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1084
|
+
* list.clear();
|
|
1085
|
+
* console.log(list.isEmpty()); // true;
|
|
1086
|
+
*/
|
|
1534
1087
|
clear() {
|
|
1535
1088
|
this._head = void 0;
|
|
1536
1089
|
this._tail = void 0;
|
|
1537
1090
|
this._length = 0;
|
|
1538
1091
|
}
|
|
1539
1092
|
/**
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
* @example
|
|
1586
|
-
* // Reverse the list in-place
|
|
1587
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1588
|
-
* list.reverse();
|
|
1589
|
-
* console.log([...list]); // [4, 3, 2, 1];
|
|
1590
|
-
*/
|
|
1093
|
+
* Reverse the list in place.
|
|
1094
|
+
* @remarks Time O(N), Space O(1)
|
|
1095
|
+
* @returns This list.
|
|
1096
|
+
* @example
|
|
1097
|
+
* // Reverse the list in-place
|
|
1098
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1099
|
+
* list.reverse();
|
|
1100
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1101
|
+
*/
|
|
1591
1102
|
reverse() {
|
|
1592
1103
|
if (!this.head || this.head === this.tail) return this;
|
|
1593
1104
|
let prev;
|
|
@@ -1762,126 +1273,44 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1762
1273
|
return false;
|
|
1763
1274
|
}
|
|
1764
1275
|
/**
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
* @example
|
|
1809
|
-
* // Deep copy
|
|
1810
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1811
|
-
* const copy = list.clone();
|
|
1812
|
-
* copy.pop();
|
|
1813
|
-
* console.log(list.length); // 3;
|
|
1814
|
-
* console.log(copy.length); // 2;
|
|
1815
|
-
*/
|
|
1276
|
+
* Deep clone this list (values are copied by reference).
|
|
1277
|
+
* @remarks Time O(N), Space O(N)
|
|
1278
|
+
* @returns A new list with the same element sequence.
|
|
1279
|
+
* @example
|
|
1280
|
+
* // Deep copy
|
|
1281
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1282
|
+
* const copy = list.clone();
|
|
1283
|
+
* copy.pop();
|
|
1284
|
+
* console.log(list.length); // 3;
|
|
1285
|
+
* console.log(copy.length); // 2;
|
|
1286
|
+
*/
|
|
1816
1287
|
clone() {
|
|
1817
1288
|
const out = this._createInstance();
|
|
1818
1289
|
for (const v of this) out.push(v);
|
|
1819
1290
|
return out;
|
|
1820
1291
|
}
|
|
1821
1292
|
/**
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
* @example
|
|
1870
|
-
* // SinglyLinkedList filter and map operations
|
|
1871
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1872
|
-
*
|
|
1873
|
-
* // Filter even numbers
|
|
1874
|
-
* const filtered = list.filter(value => value % 2 === 0);
|
|
1875
|
-
* console.log(filtered.length); // 2;
|
|
1876
|
-
*
|
|
1877
|
-
* // Map to double values
|
|
1878
|
-
* const doubled = list.map(value => value * 2);
|
|
1879
|
-
* console.log(doubled.length); // 5;
|
|
1880
|
-
*
|
|
1881
|
-
* // Use reduce to sum
|
|
1882
|
-
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1883
|
-
* console.log(sum); // 15;
|
|
1884
|
-
*/
|
|
1293
|
+
* Filter values into a new list of the same class.
|
|
1294
|
+
* @remarks Time O(N), Space O(N)
|
|
1295
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1296
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1297
|
+
* @returns A new list with kept values.
|
|
1298
|
+
* @example
|
|
1299
|
+
* // SinglyLinkedList filter and map operations
|
|
1300
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1301
|
+
*
|
|
1302
|
+
* // Filter even numbers
|
|
1303
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1304
|
+
* console.log(filtered.length); // 2;
|
|
1305
|
+
*
|
|
1306
|
+
* // Map to double values
|
|
1307
|
+
* const doubled = list.map(value => value * 2);
|
|
1308
|
+
* console.log(doubled.length); // 5;
|
|
1309
|
+
*
|
|
1310
|
+
* // Use reduce to sum
|
|
1311
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1312
|
+
* console.log(sum); // 15;
|
|
1313
|
+
*/
|
|
1885
1314
|
filter(callback, thisArg) {
|
|
1886
1315
|
const out = this._createInstance();
|
|
1887
1316
|
let index = 0;
|
|
@@ -1905,68 +1334,27 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1905
1334
|
return out;
|
|
1906
1335
|
}
|
|
1907
1336
|
/**
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
* @example
|
|
1959
|
-
* // Transform elements
|
|
1960
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1961
|
-
* const doubled = list.map(n => n * 2);
|
|
1962
|
-
* console.log([...doubled]); // [2, 4, 6];
|
|
1963
|
-
*/
|
|
1337
|
+
* Map values into a new list (possibly different element type).
|
|
1338
|
+
* @remarks Time O(N), Space O(N)
|
|
1339
|
+
* @template EM
|
|
1340
|
+
* @template RM
|
|
1341
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1342
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1343
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1344
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1345
|
+
* @example
|
|
1346
|
+
* // Transform elements
|
|
1347
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1348
|
+
* const doubled = list.map(n => n * 2);
|
|
1349
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1350
|
+
*/
|
|
1964
1351
|
map(callback, options, thisArg) {
|
|
1965
1352
|
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
1966
1353
|
let index = 0;
|
|
1967
1354
|
for (const value of this) out.push(callback.call(thisArg, value, index++, this));
|
|
1968
1355
|
return out;
|
|
1969
1356
|
}
|
|
1357
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
1970
1358
|
/**
|
|
1971
1359
|
* (Protected) Create a node from a value.
|
|
1972
1360
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2152,124 +1540,32 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2152
1540
|
this._autoCompactRatio = value;
|
|
2153
1541
|
}
|
|
2154
1542
|
/**
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
* @example
|
|
2201
|
-
* // Track queue length
|
|
2202
|
-
* const q = new Queue<number>();
|
|
2203
|
-
* console.log(q.length); // 0;
|
|
2204
|
-
* q.push(1);
|
|
2205
|
-
* q.push(2);
|
|
2206
|
-
* console.log(q.length); // 2;
|
|
2207
|
-
*/
|
|
1543
|
+
* Get the number of elements currently in the queue.
|
|
1544
|
+
* @remarks Time O(1), Space O(1)
|
|
1545
|
+
* @returns Current length.
|
|
1546
|
+
* @example
|
|
1547
|
+
* // Track queue length
|
|
1548
|
+
* const q = new Queue<number>();
|
|
1549
|
+
* console.log(q.length); // 0;
|
|
1550
|
+
* q.push(1);
|
|
1551
|
+
* q.push(2);
|
|
1552
|
+
* console.log(q.length); // 2;
|
|
1553
|
+
*/
|
|
2208
1554
|
get length() {
|
|
2209
1555
|
return this.elements.length - this._offset;
|
|
2210
1556
|
}
|
|
2211
1557
|
/**
|
|
2212
|
-
|
|
2213
|
-
* @remarks Time O(1), Space O(1)
|
|
2214
|
-
* @returns Front element or undefined.
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
* @example
|
|
2258
|
-
* // View the front element
|
|
2259
|
-
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2260
|
-
* console.log(q.first); // 'first';
|
|
2261
|
-
* console.log(q.length); // 3;
|
|
2262
|
-
*/
|
|
2263
|
-
get first() {
|
|
2264
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2265
|
-
}
|
|
2266
|
-
/**
|
|
2267
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
1558
|
+
* Get the first element (front) without removing it.
|
|
2268
1559
|
* @remarks Time O(1), Space O(1)
|
|
2269
1560
|
* @returns Front element or undefined.
|
|
1561
|
+
* @example
|
|
1562
|
+
* // View the front element
|
|
1563
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1564
|
+
* console.log(q.first); // 'first';
|
|
1565
|
+
* console.log(q.length); // 3;
|
|
2270
1566
|
*/
|
|
2271
|
-
|
|
2272
|
-
return this.
|
|
1567
|
+
get first() {
|
|
1568
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2273
1569
|
}
|
|
2274
1570
|
/**
|
|
2275
1571
|
* Get the last element (back) without removing it.
|
|
@@ -2290,131 +1586,55 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2290
1586
|
return new _Queue(elements);
|
|
2291
1587
|
}
|
|
2292
1588
|
/**
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
* @example
|
|
2339
|
-
* // Queue for...of iteration and isEmpty check
|
|
2340
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2341
|
-
*
|
|
2342
|
-
* const elements: string[] = [];
|
|
2343
|
-
* for (const item of queue) {
|
|
2344
|
-
* elements.push(item);
|
|
2345
|
-
* }
|
|
2346
|
-
*
|
|
2347
|
-
* // Verify all elements are iterated in order
|
|
2348
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2349
|
-
*
|
|
2350
|
-
* // Process all elements
|
|
2351
|
-
* while (queue.length > 0) {
|
|
2352
|
-
* queue.shift();
|
|
2353
|
-
* }
|
|
2354
|
-
*
|
|
2355
|
-
* console.log(queue.length); // 0;
|
|
2356
|
-
*/
|
|
1589
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
1590
|
+
* @remarks Time O(1), Space O(1)
|
|
1591
|
+
* @returns Front element or undefined.
|
|
1592
|
+
*/
|
|
1593
|
+
peek() {
|
|
1594
|
+
return this.first;
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Check whether the queue is empty.
|
|
1598
|
+
* @remarks Time O(1), Space O(1)
|
|
1599
|
+
* @returns True if length is 0.
|
|
1600
|
+
* @example
|
|
1601
|
+
* // Queue for...of iteration and isEmpty check
|
|
1602
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1603
|
+
*
|
|
1604
|
+
* const elements: string[] = [];
|
|
1605
|
+
* for (const item of queue) {
|
|
1606
|
+
* elements.push(item);
|
|
1607
|
+
* }
|
|
1608
|
+
*
|
|
1609
|
+
* // Verify all elements are iterated in order
|
|
1610
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1611
|
+
*
|
|
1612
|
+
* // Process all elements
|
|
1613
|
+
* while (queue.length > 0) {
|
|
1614
|
+
* queue.shift();
|
|
1615
|
+
* }
|
|
1616
|
+
*
|
|
1617
|
+
* console.log(queue.length); // 0;
|
|
1618
|
+
*/
|
|
2357
1619
|
isEmpty() {
|
|
2358
1620
|
return this.length === 0;
|
|
2359
1621
|
}
|
|
2360
1622
|
/**
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
* @example
|
|
2408
|
-
* // basic Queue creation and push operation
|
|
2409
|
-
* // Create a simple Queue with initial values
|
|
2410
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2411
|
-
*
|
|
2412
|
-
* // Verify the queue maintains insertion order
|
|
2413
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2414
|
-
*
|
|
2415
|
-
* // Check length
|
|
2416
|
-
* console.log(queue.length); // 5;
|
|
2417
|
-
*/
|
|
1623
|
+
* Enqueue one element at the back.
|
|
1624
|
+
* @remarks Time O(1), Space O(1)
|
|
1625
|
+
* @param element - Element to enqueue.
|
|
1626
|
+
* @returns True on success.
|
|
1627
|
+
* @example
|
|
1628
|
+
* // basic Queue creation and push operation
|
|
1629
|
+
* // Create a simple Queue with initial values
|
|
1630
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1631
|
+
*
|
|
1632
|
+
* // Verify the queue maintains insertion order
|
|
1633
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1634
|
+
*
|
|
1635
|
+
* // Check length
|
|
1636
|
+
* console.log(queue.length); // 5;
|
|
1637
|
+
*/
|
|
2418
1638
|
push(element) {
|
|
2419
1639
|
this.elements.push(element);
|
|
2420
1640
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -2435,66 +1655,24 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2435
1655
|
return ans;
|
|
2436
1656
|
}
|
|
2437
1657
|
/**
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
* @example
|
|
2484
|
-
* // Queue shift and peek operations
|
|
2485
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2486
|
-
*
|
|
2487
|
-
* // Peek at the front element without removing it
|
|
2488
|
-
* console.log(queue.first); // 10;
|
|
2489
|
-
*
|
|
2490
|
-
* // Remove and get the first element (FIFO)
|
|
2491
|
-
* const first = queue.shift();
|
|
2492
|
-
* console.log(first); // 10;
|
|
2493
|
-
*
|
|
2494
|
-
* // Verify remaining elements and length decreased
|
|
2495
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
2496
|
-
* console.log(queue.length); // 3;
|
|
2497
|
-
*/
|
|
1658
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1659
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1660
|
+
* @returns Removed element or undefined.
|
|
1661
|
+
* @example
|
|
1662
|
+
* // Queue shift and peek operations
|
|
1663
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1664
|
+
*
|
|
1665
|
+
* // Peek at the front element without removing it
|
|
1666
|
+
* console.log(queue.first); // 10;
|
|
1667
|
+
*
|
|
1668
|
+
* // Remove and get the first element (FIFO)
|
|
1669
|
+
* const first = queue.shift();
|
|
1670
|
+
* console.log(first); // 10;
|
|
1671
|
+
*
|
|
1672
|
+
* // Verify remaining elements and length decreased
|
|
1673
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1674
|
+
* console.log(queue.length); // 3;
|
|
1675
|
+
*/
|
|
2498
1676
|
shift() {
|
|
2499
1677
|
if (this.length === 0) return void 0;
|
|
2500
1678
|
const first = this.first;
|
|
@@ -2503,55 +1681,16 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2503
1681
|
return first;
|
|
2504
1682
|
}
|
|
2505
1683
|
/**
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
* @example
|
|
2550
|
-
* // Remove specific element
|
|
2551
|
-
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2552
|
-
* q.delete(2);
|
|
2553
|
-
* console.log(q.length); // 3;
|
|
2554
|
-
*/
|
|
1684
|
+
* Delete the first occurrence of a specific element.
|
|
1685
|
+
* @remarks Time O(N), Space O(1)
|
|
1686
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1687
|
+
* @returns True if an element was removed.
|
|
1688
|
+
* @example
|
|
1689
|
+
* // Remove specific element
|
|
1690
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1691
|
+
* q.delete(2);
|
|
1692
|
+
* console.log(q.length); // 3;
|
|
1693
|
+
*/
|
|
2555
1694
|
delete(element) {
|
|
2556
1695
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
2557
1696
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -2562,55 +1701,16 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2562
1701
|
return false;
|
|
2563
1702
|
}
|
|
2564
1703
|
/**
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
* @example
|
|
2609
|
-
* // Access element by index
|
|
2610
|
-
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2611
|
-
* console.log(q.at(0)); // 'a';
|
|
2612
|
-
* console.log(q.at(2)); // 'c';
|
|
2613
|
-
*/
|
|
1704
|
+
* Get the element at a given logical index.
|
|
1705
|
+
* @remarks Time O(1), Space O(1)
|
|
1706
|
+
* @param index - Zero-based index from the front.
|
|
1707
|
+
* @returns Element or undefined.
|
|
1708
|
+
* @example
|
|
1709
|
+
* // Access element by index
|
|
1710
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1711
|
+
* console.log(q.at(0)); // 'a';
|
|
1712
|
+
* console.log(q.at(2)); // 'c';
|
|
1713
|
+
*/
|
|
2614
1714
|
at(index) {
|
|
2615
1715
|
if (index < 0 || index >= this.length) return void 0;
|
|
2616
1716
|
return this._elements[this._offset + index];
|
|
@@ -2677,110 +1777,31 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2677
1777
|
return this;
|
|
2678
1778
|
}
|
|
2679
1779
|
/**
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
* @example
|
|
2724
|
-
* // Remove all elements
|
|
2725
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2726
|
-
* q.clear();
|
|
2727
|
-
* console.log(q.length); // 0;
|
|
2728
|
-
*/
|
|
1780
|
+
* Remove all elements and reset offset.
|
|
1781
|
+
* @remarks Time O(1), Space O(1)
|
|
1782
|
+
* @returns void
|
|
1783
|
+
* @example
|
|
1784
|
+
* // Remove all elements
|
|
1785
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1786
|
+
* q.clear();
|
|
1787
|
+
* console.log(q.length); // 0;
|
|
1788
|
+
*/
|
|
2729
1789
|
clear() {
|
|
2730
1790
|
this._elements = [];
|
|
2731
1791
|
this._offset = 0;
|
|
2732
1792
|
}
|
|
2733
1793
|
/**
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
* @example
|
|
2777
|
-
* // Reclaim unused memory
|
|
2778
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2779
|
-
* q.shift();
|
|
2780
|
-
* q.shift();
|
|
2781
|
-
* q.compact();
|
|
2782
|
-
* console.log(q.length); // 3;
|
|
2783
|
-
*/
|
|
1794
|
+
* Compact storage by discarding consumed head elements.
|
|
1795
|
+
* @remarks Time O(N), Space O(N)
|
|
1796
|
+
* @returns True when compaction performed.
|
|
1797
|
+
* @example
|
|
1798
|
+
* // Reclaim unused memory
|
|
1799
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1800
|
+
* q.shift();
|
|
1801
|
+
* q.shift();
|
|
1802
|
+
* q.compact();
|
|
1803
|
+
* console.log(q.length); // 3;
|
|
1804
|
+
*/
|
|
2784
1805
|
compact() {
|
|
2785
1806
|
this._elements = this.elements.slice(this._offset);
|
|
2786
1807
|
this._offset = 0;
|
|
@@ -2806,57 +1827,17 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2806
1827
|
return removed;
|
|
2807
1828
|
}
|
|
2808
1829
|
/**
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
* @example
|
|
2853
|
-
* // Create independent copy
|
|
2854
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2855
|
-
* const copy = q.clone();
|
|
2856
|
-
* copy.shift();
|
|
2857
|
-
* console.log(q.length); // 3;
|
|
2858
|
-
* console.log(copy.length); // 2;
|
|
2859
|
-
*/
|
|
1830
|
+
* Deep clone this queue and its parameters.
|
|
1831
|
+
* @remarks Time O(N), Space O(N)
|
|
1832
|
+
* @returns A new queue with the same content and options.
|
|
1833
|
+
* @example
|
|
1834
|
+
* // Create independent copy
|
|
1835
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1836
|
+
* const copy = q.clone();
|
|
1837
|
+
* copy.shift();
|
|
1838
|
+
* console.log(q.length); // 3;
|
|
1839
|
+
* console.log(copy.length); // 2;
|
|
1840
|
+
*/
|
|
2860
1841
|
clone() {
|
|
2861
1842
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2862
1843
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2864,57 +1845,17 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2864
1845
|
return out;
|
|
2865
1846
|
}
|
|
2866
1847
|
/**
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
* @example
|
|
2913
|
-
* // Filter elements
|
|
2914
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2915
|
-
* const evens = q.filter(x => x % 2 === 0);
|
|
2916
|
-
* console.log(evens.length); // 2;
|
|
2917
|
-
*/
|
|
1848
|
+
* Filter elements into a new queue of the same class.
|
|
1849
|
+
* @remarks Time O(N), Space O(N)
|
|
1850
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1851
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1852
|
+
* @returns A new queue with kept elements.
|
|
1853
|
+
* @example
|
|
1854
|
+
* // Filter elements
|
|
1855
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1856
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1857
|
+
* console.log(evens.length); // 2;
|
|
1858
|
+
*/
|
|
2918
1859
|
filter(predicate, thisArg) {
|
|
2919
1860
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2920
1861
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2926,59 +1867,20 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2926
1867
|
return out;
|
|
2927
1868
|
}
|
|
2928
1869
|
/**
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
* @example
|
|
2977
|
-
* // Transform elements
|
|
2978
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2979
|
-
* const doubled = q.map(x => x * 2);
|
|
2980
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2981
|
-
*/
|
|
1870
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1871
|
+
* @remarks Time O(N), Space O(N)
|
|
1872
|
+
* @template EM
|
|
1873
|
+
* @template RM
|
|
1874
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1875
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1876
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1877
|
+
* @returns A new Queue with mapped elements.
|
|
1878
|
+
* @example
|
|
1879
|
+
* // Transform elements
|
|
1880
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1881
|
+
* const doubled = q.map(x => x * 2);
|
|
1882
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1883
|
+
*/
|
|
2982
1884
|
map(callback, options, thisArg) {
|
|
2983
1885
|
const out = new this.constructor([], {
|
|
2984
1886
|
toElementFn: options?.toElementFn,
|
|
@@ -3093,7 +1995,11 @@ var Deque = class extends LinearBase {
|
|
|
3093
1995
|
static {
|
|
3094
1996
|
__name(this, "Deque");
|
|
3095
1997
|
}
|
|
3096
|
-
|
|
1998
|
+
/**
|
|
1999
|
+
* Counter for shift/pop operations since last compaction check.
|
|
2000
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
2001
|
+
*/
|
|
2002
|
+
_compactCounter = 0;
|
|
3097
2003
|
constructor(elements = [], options) {
|
|
3098
2004
|
super(options);
|
|
3099
2005
|
if (options) {
|
|
@@ -3144,11 +2050,6 @@ var Deque = class extends LinearBase {
|
|
|
3144
2050
|
set autoCompactRatio(value) {
|
|
3145
2051
|
this._autoCompactRatio = value;
|
|
3146
2052
|
}
|
|
3147
|
-
/**
|
|
3148
|
-
* Counter for shift/pop operations since last compaction check.
|
|
3149
|
-
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
3150
|
-
*/
|
|
3151
|
-
_compactCounter = 0;
|
|
3152
2053
|
_bucketFirst = 0;
|
|
3153
2054
|
/**
|
|
3154
2055
|
* Get the index of the first bucket in use.
|
|
@@ -3213,146 +2114,36 @@ var Deque = class extends LinearBase {
|
|
|
3213
2114
|
return this._length;
|
|
3214
2115
|
}
|
|
3215
2116
|
/**
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
* @example
|
|
3258
|
-
* // Deque peek at both ends
|
|
3259
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3260
|
-
*
|
|
3261
|
-
* // Get first element without removing
|
|
3262
|
-
* const first = deque.at(0);
|
|
3263
|
-
* console.log(first); // 10;
|
|
3264
|
-
*
|
|
3265
|
-
* // Get last element without removing
|
|
3266
|
-
* const last = deque.at(deque.length - 1);
|
|
3267
|
-
* console.log(last); // 50;
|
|
3268
|
-
*
|
|
3269
|
-
* // Length unchanged
|
|
3270
|
-
* console.log(deque.length); // 5;
|
|
3271
|
-
*/
|
|
3272
|
-
/**
|
|
3273
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
3274
|
-
* @remarks Time O(1), Space O(1)
|
|
3275
|
-
* @returns Front element or undefined.
|
|
2117
|
+
* Deque peek at both ends
|
|
2118
|
+
* @example
|
|
2119
|
+
* // Deque peek at both ends
|
|
2120
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2121
|
+
*
|
|
2122
|
+
* // Get first element without removing
|
|
2123
|
+
* const first = deque.at(0);
|
|
2124
|
+
* console.log(first); // 10;
|
|
2125
|
+
*
|
|
2126
|
+
* // Get last element without removing
|
|
2127
|
+
* const last = deque.at(deque.length - 1);
|
|
2128
|
+
* console.log(last); // 50;
|
|
2129
|
+
*
|
|
2130
|
+
* // Length unchanged
|
|
2131
|
+
* console.log(deque.length); // 5;
|
|
3276
2132
|
*/
|
|
3277
|
-
peek() {
|
|
3278
|
-
return this.first;
|
|
3279
|
-
}
|
|
3280
|
-
/**
|
|
3281
|
-
* Deque peek at both ends
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
* @example
|
|
3286
|
-
* // Deque peek at both ends
|
|
3287
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3288
|
-
*
|
|
3289
|
-
* // Get first element without removing
|
|
3290
|
-
* const first = deque.at(0);
|
|
3291
|
-
* console.log(first); // 10;
|
|
3292
|
-
*
|
|
3293
|
-
* // Get last element without removing
|
|
3294
|
-
* const last = deque.at(deque.length - 1);
|
|
3295
|
-
* console.log(last); // 50;
|
|
3296
|
-
*
|
|
3297
|
-
* // Length unchanged
|
|
3298
|
-
* console.log(deque.length); // 5;
|
|
3299
|
-
*/
|
|
3300
2133
|
get first() {
|
|
3301
2134
|
if (this._length === 0) return;
|
|
3302
2135
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3303
2136
|
}
|
|
3304
2137
|
/**
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
* @example
|
|
3351
|
-
* // Peek at the back element
|
|
3352
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3353
|
-
* console.log(dq.last); // 'c';
|
|
3354
|
-
* console.log(dq.first); // 'a';
|
|
3355
|
-
*/
|
|
2138
|
+
* Get the last element without removing it.
|
|
2139
|
+
* @remarks Time O(1), Space O(1)
|
|
2140
|
+
* @returns Last element or undefined.
|
|
2141
|
+
* @example
|
|
2142
|
+
* // Peek at the back element
|
|
2143
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2144
|
+
* console.log(dq.last); // 'c';
|
|
2145
|
+
* console.log(dq.first); // 'a';
|
|
2146
|
+
*/
|
|
3356
2147
|
get last() {
|
|
3357
2148
|
if (this._length === 0) return;
|
|
3358
2149
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3371,71 +2162,56 @@ var Deque = class extends LinearBase {
|
|
|
3371
2162
|
return new this(data, options);
|
|
3372
2163
|
}
|
|
3373
2164
|
/**
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
*
|
|
3425
|
-
* // Verify the deque maintains insertion order
|
|
3426
|
-
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3427
|
-
*
|
|
3428
|
-
* // Check length
|
|
3429
|
-
* console.log(deque.length); // 5;
|
|
3430
|
-
*
|
|
3431
|
-
* // Push to the end
|
|
3432
|
-
* deque.push(6);
|
|
3433
|
-
* console.log(deque.length); // 6;
|
|
3434
|
-
*
|
|
3435
|
-
* // Pop from the end
|
|
3436
|
-
* const last = deque.pop();
|
|
3437
|
-
* console.log(last); // 6;
|
|
3438
|
-
*/
|
|
2165
|
+
* Get the first element without removing it.
|
|
2166
|
+
* @remarks Time O(1), Space O(1)
|
|
2167
|
+
* @returns First element or undefined.
|
|
2168
|
+
* @example
|
|
2169
|
+
* // Deque peek at both ends
|
|
2170
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2171
|
+
*
|
|
2172
|
+
* // Get first element without removing
|
|
2173
|
+
* const first = deque.at(0);
|
|
2174
|
+
* console.log(first); // 10;
|
|
2175
|
+
*
|
|
2176
|
+
* // Get last element without removing
|
|
2177
|
+
* const last = deque.at(deque.length - 1);
|
|
2178
|
+
* console.log(last); // 50;
|
|
2179
|
+
*
|
|
2180
|
+
* // Length unchanged
|
|
2181
|
+
* console.log(deque.length); // 5;
|
|
2182
|
+
*/
|
|
2183
|
+
/**
|
|
2184
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
2185
|
+
* @remarks Time O(1), Space O(1)
|
|
2186
|
+
* @returns Front element or undefined.
|
|
2187
|
+
*/
|
|
2188
|
+
peek() {
|
|
2189
|
+
return this.first;
|
|
2190
|
+
}
|
|
2191
|
+
/**
|
|
2192
|
+
* Append one element at the back.
|
|
2193
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2194
|
+
* @param element - Element to append.
|
|
2195
|
+
* @returns True when appended.
|
|
2196
|
+
* @example
|
|
2197
|
+
* // basic Deque creation and push/pop operations
|
|
2198
|
+
* // Create a simple Deque with initial values
|
|
2199
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
2200
|
+
*
|
|
2201
|
+
* // Verify the deque maintains insertion order
|
|
2202
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
2203
|
+
*
|
|
2204
|
+
* // Check length
|
|
2205
|
+
* console.log(deque.length); // 5;
|
|
2206
|
+
*
|
|
2207
|
+
* // Push to the end
|
|
2208
|
+
* deque.push(6);
|
|
2209
|
+
* console.log(deque.length); // 6;
|
|
2210
|
+
*
|
|
2211
|
+
* // Pop from the end
|
|
2212
|
+
* const last = deque.pop();
|
|
2213
|
+
* console.log(last); // 6;
|
|
2214
|
+
*/
|
|
3439
2215
|
push(element) {
|
|
3440
2216
|
if (this._length) {
|
|
3441
2217
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -3455,57 +2231,15 @@ var Deque = class extends LinearBase {
|
|
|
3455
2231
|
return true;
|
|
3456
2232
|
}
|
|
3457
2233
|
/**
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
|
|
3502
|
-
|
|
3503
|
-
* @example
|
|
3504
|
-
* // Remove from the back
|
|
3505
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3506
|
-
* console.log(dq.pop()); // 3;
|
|
3507
|
-
* console.log(dq.length); // 2;
|
|
3508
|
-
*/
|
|
2234
|
+
* Remove and return the last element.
|
|
2235
|
+
* @remarks Time O(1), Space O(1)
|
|
2236
|
+
* @returns Removed element or undefined.
|
|
2237
|
+
* @example
|
|
2238
|
+
* // Remove from the back
|
|
2239
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2240
|
+
* console.log(dq.pop()); // 3;
|
|
2241
|
+
* console.log(dq.length); // 2;
|
|
2242
|
+
*/
|
|
3509
2243
|
pop() {
|
|
3510
2244
|
if (this._length === 0) return;
|
|
3511
2245
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3525,57 +2259,15 @@ var Deque = class extends LinearBase {
|
|
|
3525
2259
|
return element;
|
|
3526
2260
|
}
|
|
3527
2261
|
/**
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
* @example
|
|
3574
|
-
* // Remove from the front
|
|
3575
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3576
|
-
* console.log(dq.shift()); // 1;
|
|
3577
|
-
* console.log(dq.length); // 2;
|
|
3578
|
-
*/
|
|
2262
|
+
* Remove and return the first element.
|
|
2263
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2264
|
+
* @returns Removed element or undefined.
|
|
2265
|
+
* @example
|
|
2266
|
+
* // Remove from the front
|
|
2267
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2268
|
+
* console.log(dq.shift()); // 1;
|
|
2269
|
+
* console.log(dq.length); // 2;
|
|
2270
|
+
*/
|
|
3579
2271
|
shift() {
|
|
3580
2272
|
if (this._length === 0) return;
|
|
3581
2273
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -3595,68 +2287,26 @@ var Deque = class extends LinearBase {
|
|
|
3595
2287
|
return element;
|
|
3596
2288
|
}
|
|
3597
2289
|
/**
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
* @example
|
|
3645
|
-
* // Deque shift and unshift operations
|
|
3646
|
-
* const deque = new Deque<number>([20, 30, 40]);
|
|
3647
|
-
*
|
|
3648
|
-
* // Unshift adds to the front
|
|
3649
|
-
* deque.unshift(10);
|
|
3650
|
-
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3651
|
-
*
|
|
3652
|
-
* // Shift removes from the front (O(1) complexity!)
|
|
3653
|
-
* const first = deque.shift();
|
|
3654
|
-
* console.log(first); // 10;
|
|
3655
|
-
*
|
|
3656
|
-
* // Verify remaining elements
|
|
3657
|
-
* console.log([...deque]); // [20, 30, 40];
|
|
3658
|
-
* console.log(deque.length); // 3;
|
|
3659
|
-
*/
|
|
2290
|
+
* Prepend one element at the front.
|
|
2291
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2292
|
+
* @param element - Element to prepend.
|
|
2293
|
+
* @returns True when prepended.
|
|
2294
|
+
* @example
|
|
2295
|
+
* // Deque shift and unshift operations
|
|
2296
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
2297
|
+
*
|
|
2298
|
+
* // Unshift adds to the front
|
|
2299
|
+
* deque.unshift(10);
|
|
2300
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
2301
|
+
*
|
|
2302
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
2303
|
+
* const first = deque.shift();
|
|
2304
|
+
* console.log(first); // 10;
|
|
2305
|
+
*
|
|
2306
|
+
* // Verify remaining elements
|
|
2307
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
2308
|
+
* console.log(deque.length); // 3;
|
|
2309
|
+
*/
|
|
3660
2310
|
unshift(element) {
|
|
3661
2311
|
if (this._length) {
|
|
3662
2312
|
if (this._firstInBucket > 0) {
|
|
@@ -3710,107 +2360,27 @@ var Deque = class extends LinearBase {
|
|
|
3710
2360
|
return ans;
|
|
3711
2361
|
}
|
|
3712
2362
|
/**
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
* @example
|
|
3757
|
-
* // Check if empty
|
|
3758
|
-
* const dq = new Deque();
|
|
3759
|
-
* console.log(dq.isEmpty()); // true;
|
|
3760
|
-
*/
|
|
2363
|
+
* Check whether the deque is empty.
|
|
2364
|
+
* @remarks Time O(1), Space O(1)
|
|
2365
|
+
* @returns True if length is 0.
|
|
2366
|
+
* @example
|
|
2367
|
+
* // Check if empty
|
|
2368
|
+
* const dq = new Deque();
|
|
2369
|
+
* console.log(dq.isEmpty()); // true;
|
|
2370
|
+
*/
|
|
3761
2371
|
isEmpty() {
|
|
3762
2372
|
return this._length === 0;
|
|
3763
2373
|
}
|
|
3764
2374
|
/**
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
|
|
3792
|
-
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
|
|
3799
|
-
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
* @example
|
|
3809
|
-
* // Remove all elements
|
|
3810
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3811
|
-
* dq.clear();
|
|
3812
|
-
* console.log(dq.length); // 0;
|
|
3813
|
-
*/
|
|
2375
|
+
* Remove all elements and reset structure.
|
|
2376
|
+
* @remarks Time O(1), Space O(1)
|
|
2377
|
+
* @returns void
|
|
2378
|
+
* @example
|
|
2379
|
+
* // Remove all elements
|
|
2380
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2381
|
+
* dq.clear();
|
|
2382
|
+
* console.log(dq.length); // 0;
|
|
2383
|
+
*/
|
|
3814
2384
|
clear() {
|
|
3815
2385
|
this._buckets = [new Array(this._bucketSize)];
|
|
3816
2386
|
this._bucketCount = 1;
|
|
@@ -3818,55 +2388,16 @@ var Deque = class extends LinearBase {
|
|
|
3818
2388
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
3819
2389
|
}
|
|
3820
2390
|
/**
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
* @example
|
|
3865
|
-
* // Access by index
|
|
3866
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3867
|
-
* console.log(dq.at(0)); // 'a';
|
|
3868
|
-
* console.log(dq.at(2)); // 'c';
|
|
3869
|
-
*/
|
|
2391
|
+
* Get the element at a given position.
|
|
2392
|
+
* @remarks Time O(1), Space O(1)
|
|
2393
|
+
* @param pos - Zero-based position from the front.
|
|
2394
|
+
* @returns Element or undefined.
|
|
2395
|
+
* @example
|
|
2396
|
+
* // Access by index
|
|
2397
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2398
|
+
* console.log(dq.at(0)); // 'a';
|
|
2399
|
+
* console.log(dq.at(2)); // 'c';
|
|
2400
|
+
*/
|
|
3870
2401
|
at(pos) {
|
|
3871
2402
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
3872
2403
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -4025,55 +2556,16 @@ var Deque = class extends LinearBase {
|
|
|
4025
2556
|
}
|
|
4026
2557
|
}
|
|
4027
2558
|
/**
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
* @example
|
|
4072
|
-
* // Remove element
|
|
4073
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4074
|
-
* dq.delete(2);
|
|
4075
|
-
* console.log(dq.length); // 2;
|
|
4076
|
-
*/
|
|
2559
|
+
* Delete the first occurrence of a value.
|
|
2560
|
+
* @remarks Time O(N), Space O(1)
|
|
2561
|
+
* @param element - Element to remove (using the configured equality).
|
|
2562
|
+
* @returns True if an element was removed.
|
|
2563
|
+
* @example
|
|
2564
|
+
* // Remove element
|
|
2565
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2566
|
+
* dq.delete(2);
|
|
2567
|
+
* console.log(dq.length); // 2;
|
|
2568
|
+
*/
|
|
4077
2569
|
delete(element) {
|
|
4078
2570
|
const size = this._length;
|
|
4079
2571
|
if (size === 0) return false;
|
|
@@ -4117,78 +2609,39 @@ var Deque = class extends LinearBase {
|
|
|
4117
2609
|
return this;
|
|
4118
2610
|
}
|
|
4119
2611
|
/**
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
* @example
|
|
4163
|
-
* // Deque for...of iteration and reverse
|
|
4164
|
-
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
4165
|
-
*
|
|
4166
|
-
* // Iterate forward
|
|
4167
|
-
* const forward: string[] = [];
|
|
4168
|
-
* for (const item of deque) {
|
|
4169
|
-
* forward.push(item);
|
|
4170
|
-
* }
|
|
4171
|
-
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
4172
|
-
*
|
|
4173
|
-
* // Reverse the deque
|
|
4174
|
-
* deque.reverse();
|
|
4175
|
-
* const backward: string[] = [];
|
|
4176
|
-
* for (const item of deque) {
|
|
4177
|
-
* backward.push(item);
|
|
4178
|
-
* }
|
|
4179
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4180
|
-
*/
|
|
4181
|
-
/**
|
|
4182
|
-
* Find the last value matching a predicate (scans back-to-front).
|
|
4183
|
-
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
4184
|
-
* @param predicate - Function called with (value, index, deque).
|
|
4185
|
-
* @returns Matching value or undefined.
|
|
4186
|
-
* @example
|
|
4187
|
-
* // Find last matching value
|
|
4188
|
-
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
4189
|
-
* console.log(d.findLast(v => v > 2)); // 5;
|
|
4190
|
-
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
4191
|
-
*/
|
|
2612
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
2613
|
+
* @remarks Time O(N), Space O(N)
|
|
2614
|
+
* @returns This deque.
|
|
2615
|
+
* @example
|
|
2616
|
+
* // Deque for...of iteration and reverse
|
|
2617
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2618
|
+
*
|
|
2619
|
+
* // Iterate forward
|
|
2620
|
+
* const forward: string[] = [];
|
|
2621
|
+
* for (const item of deque) {
|
|
2622
|
+
* forward.push(item);
|
|
2623
|
+
* }
|
|
2624
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2625
|
+
*
|
|
2626
|
+
* // Reverse the deque
|
|
2627
|
+
* deque.reverse();
|
|
2628
|
+
* const backward: string[] = [];
|
|
2629
|
+
* for (const item of deque) {
|
|
2630
|
+
* backward.push(item);
|
|
2631
|
+
* }
|
|
2632
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2633
|
+
*/
|
|
2634
|
+
/**
|
|
2635
|
+
* Find the last value matching a predicate (scans back-to-front).
|
|
2636
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2637
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2638
|
+
* @returns Matching value or undefined.
|
|
2639
|
+
* @example
|
|
2640
|
+
* // Find last matching value
|
|
2641
|
+
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
2642
|
+
* console.log(d.findLast(v => v > 2)); // 5;
|
|
2643
|
+
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
2644
|
+
*/
|
|
4192
2645
|
findLast(predicate) {
|
|
4193
2646
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4194
2647
|
const val = this.at(i);
|
|
@@ -4197,16 +2650,16 @@ var Deque = class extends LinearBase {
|
|
|
4197
2650
|
return void 0;
|
|
4198
2651
|
}
|
|
4199
2652
|
/**
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
2653
|
+
* Find the index of the last value matching a predicate (scans back-to-front).
|
|
2654
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2655
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2656
|
+
* @returns Matching index, or -1 if not found.
|
|
2657
|
+
* @example
|
|
2658
|
+
* // Find last matching index
|
|
2659
|
+
* const d = new Deque([10, 20, 30, 20, 10]);
|
|
2660
|
+
* console.log(d.findLastIndex(v => v === 20)); // 3;
|
|
2661
|
+
* console.log(d.findLastIndex(v => v === 10)); // 4;
|
|
2662
|
+
*/
|
|
4210
2663
|
findLastIndex(predicate) {
|
|
4211
2664
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4212
2665
|
if (predicate(this.at(i), i, this)) return i;
|
|
@@ -4214,28 +2667,26 @@ var Deque = class extends LinearBase {
|
|
|
4214
2667
|
return -1;
|
|
4215
2668
|
}
|
|
4216
2669
|
/**
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
|
|
4236
|
-
|
|
4237
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4238
|
-
*/
|
|
2670
|
+
* Deque for...of iteration and reverse
|
|
2671
|
+
* @example
|
|
2672
|
+
* // Deque for...of iteration and reverse
|
|
2673
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2674
|
+
*
|
|
2675
|
+
* // Iterate forward
|
|
2676
|
+
* const forward: string[] = [];
|
|
2677
|
+
* for (const item of deque) {
|
|
2678
|
+
* forward.push(item);
|
|
2679
|
+
* }
|
|
2680
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2681
|
+
*
|
|
2682
|
+
* // Reverse the deque
|
|
2683
|
+
* deque.reverse();
|
|
2684
|
+
* const backward: string[] = [];
|
|
2685
|
+
* for (const item of deque) {
|
|
2686
|
+
* backward.push(item);
|
|
2687
|
+
* }
|
|
2688
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2689
|
+
*/
|
|
4239
2690
|
reverse() {
|
|
4240
2691
|
this._buckets.reverse().forEach(function(bucket) {
|
|
4241
2692
|
bucket.reverse();
|
|
@@ -4269,81 +2720,22 @@ var Deque = class extends LinearBase {
|
|
|
4269
2720
|
return this;
|
|
4270
2721
|
}
|
|
4271
2722
|
/**
|
|
4272
|
-
*
|
|
2723
|
+
* Compact the deque by removing unused buckets.
|
|
4273
2724
|
* @remarks Time O(N), Space O(1)
|
|
4274
|
-
* @returns
|
|
4275
|
-
*/
|
|
4276
|
-
/**
|
|
4277
|
-
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
4278
|
-
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
4279
|
-
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2725
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
4280
2726
|
*/
|
|
4281
|
-
_autoCompact() {
|
|
4282
|
-
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
4283
|
-
this._compactCounter++;
|
|
4284
|
-
if (this._compactCounter < this._bucketSize) return;
|
|
4285
|
-
this._compactCounter = 0;
|
|
4286
|
-
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
4287
|
-
if (utilization < this._autoCompactRatio) {
|
|
4288
|
-
this.shrinkToFit();
|
|
4289
|
-
}
|
|
4290
|
-
}
|
|
4291
2727
|
/**
|
|
4292
2728
|
* Compact the deque by removing unused buckets.
|
|
4293
2729
|
* @remarks Time O(N), Space O(1)
|
|
4294
2730
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2731
|
+
* @example
|
|
2732
|
+
* // Reclaim memory
|
|
2733
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
2734
|
+
* dq.shift();
|
|
2735
|
+
* dq.shift();
|
|
2736
|
+
* dq.compact();
|
|
2737
|
+
* console.log(dq.length); // 3;
|
|
4295
2738
|
*/
|
|
4296
|
-
/**
|
|
4297
|
-
* Compact the deque by removing unused buckets.
|
|
4298
|
-
* @remarks Time O(N), Space O(1)
|
|
4299
|
-
* @returns True if compaction was performed (bucket count reduced).
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
* @example
|
|
4340
|
-
* // Reclaim memory
|
|
4341
|
-
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
4342
|
-
* dq.shift();
|
|
4343
|
-
* dq.shift();
|
|
4344
|
-
* dq.compact();
|
|
4345
|
-
* console.log(dq.length); // 3;
|
|
4346
|
-
*/
|
|
4347
2739
|
compact() {
|
|
4348
2740
|
const before = this._bucketCount;
|
|
4349
2741
|
this.shrinkToFit();
|
|
@@ -4371,57 +2763,17 @@ var Deque = class extends LinearBase {
|
|
|
4371
2763
|
this._compactCounter = 0;
|
|
4372
2764
|
}
|
|
4373
2765
|
/**
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
|
|
4380
|
-
|
|
4381
|
-
|
|
4382
|
-
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
* @example
|
|
4418
|
-
* // Create independent copy
|
|
4419
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4420
|
-
* const copy = dq.clone();
|
|
4421
|
-
* copy.pop();
|
|
4422
|
-
* console.log(dq.length); // 3;
|
|
4423
|
-
* console.log(copy.length); // 2;
|
|
4424
|
-
*/
|
|
2766
|
+
* Deep clone this deque, preserving options.
|
|
2767
|
+
* @remarks Time O(N), Space O(N)
|
|
2768
|
+
* @returns A new deque with the same content and options.
|
|
2769
|
+
* @example
|
|
2770
|
+
* // Create independent copy
|
|
2771
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2772
|
+
* const copy = dq.clone();
|
|
2773
|
+
* copy.pop();
|
|
2774
|
+
* console.log(dq.length); // 3;
|
|
2775
|
+
* console.log(copy.length); // 2;
|
|
2776
|
+
*/
|
|
4425
2777
|
clone() {
|
|
4426
2778
|
return this._createLike(this, {
|
|
4427
2779
|
bucketSize: this.bucketSize,
|
|
@@ -4430,57 +2782,17 @@ var Deque = class extends LinearBase {
|
|
|
4430
2782
|
});
|
|
4431
2783
|
}
|
|
4432
2784
|
/**
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
* @example
|
|
4479
|
-
* // Filter elements
|
|
4480
|
-
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
4481
|
-
* const result = dq.filter(x => x > 2);
|
|
4482
|
-
* console.log(result.length); // 2;
|
|
4483
|
-
*/
|
|
2785
|
+
* Filter elements into a new deque of the same class.
|
|
2786
|
+
* @remarks Time O(N), Space O(N)
|
|
2787
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
2788
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2789
|
+
* @returns A new deque with kept elements.
|
|
2790
|
+
* @example
|
|
2791
|
+
* // Filter elements
|
|
2792
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
2793
|
+
* const result = dq.filter(x => x > 2);
|
|
2794
|
+
* console.log(result.length); // 2;
|
|
2795
|
+
*/
|
|
4484
2796
|
filter(predicate, thisArg) {
|
|
4485
2797
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
4486
2798
|
out._setBucketSize(this._bucketSize);
|
|
@@ -4509,59 +2821,20 @@ var Deque = class extends LinearBase {
|
|
|
4509
2821
|
return out;
|
|
4510
2822
|
}
|
|
4511
2823
|
/**
|
|
4512
|
-
|
|
4513
|
-
|
|
4514
|
-
|
|
4515
|
-
|
|
4516
|
-
|
|
4517
|
-
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4542
|
-
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
* @example
|
|
4560
|
-
* // Transform elements
|
|
4561
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4562
|
-
* const result = dq.map(x => x * 10);
|
|
4563
|
-
* console.log(result.toArray()); // [10, 20, 30];
|
|
4564
|
-
*/
|
|
2824
|
+
* Map elements into a new deque (possibly different element type).
|
|
2825
|
+
* @remarks Time O(N), Space O(N)
|
|
2826
|
+
* @template EM
|
|
2827
|
+
* @template RM
|
|
2828
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
2829
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
2830
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2831
|
+
* @returns A new Deque with mapped elements.
|
|
2832
|
+
* @example
|
|
2833
|
+
* // Transform elements
|
|
2834
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2835
|
+
* const result = dq.map(x => x * 10);
|
|
2836
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
2837
|
+
*/
|
|
4565
2838
|
map(callback, options, thisArg) {
|
|
4566
2839
|
const out = this._createLike([], {
|
|
4567
2840
|
...options ?? {},
|
|
@@ -4576,6 +2849,27 @@ var Deque = class extends LinearBase {
|
|
|
4576
2849
|
}
|
|
4577
2850
|
return out;
|
|
4578
2851
|
}
|
|
2852
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
2853
|
+
/**
|
|
2854
|
+
* Trim unused buckets to fit exactly the active range.
|
|
2855
|
+
* @remarks Time O(N), Space O(1)
|
|
2856
|
+
* @returns void
|
|
2857
|
+
*/
|
|
2858
|
+
/**
|
|
2859
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2860
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2861
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2862
|
+
*/
|
|
2863
|
+
_autoCompact() {
|
|
2864
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2865
|
+
this._compactCounter++;
|
|
2866
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2867
|
+
this._compactCounter = 0;
|
|
2868
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2869
|
+
if (utilization < this._autoCompactRatio) {
|
|
2870
|
+
this.shrinkToFit();
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
4579
2873
|
/**
|
|
4580
2874
|
* (Protected) Set the internal bucket size.
|
|
4581
2875
|
* @remarks Time O(1), Space O(1)
|