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/esm/queue.mjs
CHANGED
|
@@ -696,7 +696,6 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
696
696
|
static {
|
|
697
697
|
__name(this, "SinglyLinkedList");
|
|
698
698
|
}
|
|
699
|
-
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
700
699
|
/**
|
|
701
700
|
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
702
701
|
* @remarks Time O(N), Space O(N)
|
|
@@ -768,68 +767,26 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
768
767
|
return list;
|
|
769
768
|
}
|
|
770
769
|
/**
|
|
771
|
-
|
|
772
|
-
|
|
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
|
-
* @example
|
|
818
|
-
* // basic SinglyLinkedList creation and push operation
|
|
819
|
-
* // Create a simple SinglyLinkedList with initial values
|
|
820
|
-
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
821
|
-
*
|
|
822
|
-
* // Verify the list maintains insertion order
|
|
823
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
824
|
-
*
|
|
825
|
-
* // Check length
|
|
826
|
-
* console.log(list.length); // 5;
|
|
827
|
-
*
|
|
828
|
-
* // Push a new element to the end
|
|
829
|
-
* list.push(6);
|
|
830
|
-
* console.log(list.length); // 6;
|
|
831
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
832
|
-
*/
|
|
770
|
+
* Append an element/node to the tail.
|
|
771
|
+
* @remarks Time O(1), Space O(1)
|
|
772
|
+
* @param elementOrNode - Element or node to append.
|
|
773
|
+
* @returns True when appended.
|
|
774
|
+
* @example
|
|
775
|
+
* // basic SinglyLinkedList creation and push operation
|
|
776
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
777
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
778
|
+
*
|
|
779
|
+
* // Verify the list maintains insertion order
|
|
780
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
781
|
+
*
|
|
782
|
+
* // Check length
|
|
783
|
+
* console.log(list.length); // 5;
|
|
784
|
+
*
|
|
785
|
+
* // Push a new element to the end
|
|
786
|
+
* list.push(6);
|
|
787
|
+
* console.log(list.length); // 6;
|
|
788
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
789
|
+
*/
|
|
833
790
|
push(elementOrNode) {
|
|
834
791
|
const newNode = this._ensureNode(elementOrNode);
|
|
835
792
|
if (!this.head) {
|
|
@@ -843,67 +800,25 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
843
800
|
return true;
|
|
844
801
|
}
|
|
845
802
|
/**
|
|
846
|
-
|
|
847
|
-
|
|
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
|
-
* @example
|
|
892
|
-
* // SinglyLinkedList pop and shift operations
|
|
893
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
894
|
-
*
|
|
895
|
-
* // Pop removes from the end
|
|
896
|
-
* const last = list.pop();
|
|
897
|
-
* console.log(last); // 50;
|
|
898
|
-
*
|
|
899
|
-
* // Shift removes from the beginning
|
|
900
|
-
* const first = list.shift();
|
|
901
|
-
* console.log(first); // 10;
|
|
902
|
-
*
|
|
903
|
-
* // Verify remaining elements
|
|
904
|
-
* console.log([...list]); // [20, 30, 40];
|
|
905
|
-
* console.log(list.length); // 3;
|
|
906
|
-
*/
|
|
803
|
+
* Remove and return the tail element.
|
|
804
|
+
* @remarks Time O(N), Space O(1)
|
|
805
|
+
* @returns Removed element or undefined.
|
|
806
|
+
* @example
|
|
807
|
+
* // SinglyLinkedList pop and shift operations
|
|
808
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
809
|
+
*
|
|
810
|
+
* // Pop removes from the end
|
|
811
|
+
* const last = list.pop();
|
|
812
|
+
* console.log(last); // 50;
|
|
813
|
+
*
|
|
814
|
+
* // Shift removes from the beginning
|
|
815
|
+
* const first = list.shift();
|
|
816
|
+
* console.log(first); // 10;
|
|
817
|
+
*
|
|
818
|
+
* // Verify remaining elements
|
|
819
|
+
* console.log([...list]); // [20, 30, 40];
|
|
820
|
+
* console.log(list.length); // 3;
|
|
821
|
+
*/
|
|
907
822
|
pop() {
|
|
908
823
|
if (!this.head) return void 0;
|
|
909
824
|
if (this.head === this.tail) {
|
|
@@ -922,57 +837,15 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
922
837
|
return value;
|
|
923
838
|
}
|
|
924
839
|
/**
|
|
925
|
-
|
|
926
|
-
|
|
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
|
-
* @example
|
|
971
|
-
* // Remove from the front
|
|
972
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
973
|
-
* console.log(list.shift()); // 10;
|
|
974
|
-
* console.log(list.length); // 2;
|
|
975
|
-
*/
|
|
840
|
+
* Remove and return the head element.
|
|
841
|
+
* @remarks Time O(1), Space O(1)
|
|
842
|
+
* @returns Removed element or undefined.
|
|
843
|
+
* @example
|
|
844
|
+
* // Remove from the front
|
|
845
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
846
|
+
* console.log(list.shift()); // 10;
|
|
847
|
+
* console.log(list.length); // 2;
|
|
848
|
+
*/
|
|
976
849
|
shift() {
|
|
977
850
|
if (!this.head) return void 0;
|
|
978
851
|
const removed = this.head;
|
|
@@ -982,73 +855,31 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
982
855
|
return removed.value;
|
|
983
856
|
}
|
|
984
857
|
/**
|
|
985
|
-
|
|
986
|
-
|
|
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
|
-
* @example
|
|
1032
|
-
* // SinglyLinkedList unshift and forward traversal
|
|
1033
|
-
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
1034
|
-
*
|
|
1035
|
-
* // Unshift adds to the beginning
|
|
1036
|
-
* list.unshift(10);
|
|
1037
|
-
* console.log([...list]); // [10, 20, 30, 40];
|
|
1038
|
-
*
|
|
1039
|
-
* // Access elements (forward traversal only for singly linked)
|
|
1040
|
-
* const second = list.at(1);
|
|
1041
|
-
* console.log(second); // 20;
|
|
1042
|
-
*
|
|
1043
|
-
* // SinglyLinkedList allows forward iteration only
|
|
1044
|
-
* const elements: number[] = [];
|
|
1045
|
-
* for (const item of list) {
|
|
1046
|
-
* elements.push(item);
|
|
1047
|
-
* }
|
|
1048
|
-
* console.log(elements); // [10, 20, 30, 40];
|
|
1049
|
-
*
|
|
1050
|
-
* console.log(list.length); // 4;
|
|
1051
|
-
*/
|
|
858
|
+
* Prepend an element/node to the head.
|
|
859
|
+
* @remarks Time O(1), Space O(1)
|
|
860
|
+
* @param elementOrNode - Element or node to prepend.
|
|
861
|
+
* @returns True when prepended.
|
|
862
|
+
* @example
|
|
863
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
864
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
865
|
+
*
|
|
866
|
+
* // Unshift adds to the beginning
|
|
867
|
+
* list.unshift(10);
|
|
868
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
869
|
+
*
|
|
870
|
+
* // Access elements (forward traversal only for singly linked)
|
|
871
|
+
* const second = list.at(1);
|
|
872
|
+
* console.log(second); // 20;
|
|
873
|
+
*
|
|
874
|
+
* // SinglyLinkedList allows forward iteration only
|
|
875
|
+
* const elements: number[] = [];
|
|
876
|
+
* for (const item of list) {
|
|
877
|
+
* elements.push(item);
|
|
878
|
+
* }
|
|
879
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
880
|
+
*
|
|
881
|
+
* console.log(list.length); // 4;
|
|
882
|
+
*/
|
|
1052
883
|
unshift(elementOrNode) {
|
|
1053
884
|
const newNode = this._ensureNode(elementOrNode);
|
|
1054
885
|
if (!this.head) {
|
|
@@ -1104,59 +935,17 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1104
935
|
return void 0;
|
|
1105
936
|
}
|
|
1106
937
|
/**
|
|
1107
|
-
|
|
1108
|
-
|
|
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
|
-
* @example
|
|
1154
|
-
* // Access element by index
|
|
1155
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1156
|
-
* console.log(list.at(0)); // 'a';
|
|
1157
|
-
* console.log(list.at(2)); // 'c';
|
|
1158
|
-
* console.log(list.at(3)); // 'd';
|
|
1159
|
-
*/
|
|
938
|
+
* Get the element at a given index.
|
|
939
|
+
* @remarks Time O(N), Space O(1)
|
|
940
|
+
* @param index - Zero-based index.
|
|
941
|
+
* @returns Element or undefined.
|
|
942
|
+
* @example
|
|
943
|
+
* // Access element by index
|
|
944
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
945
|
+
* console.log(list.at(0)); // 'a';
|
|
946
|
+
* console.log(list.at(2)); // 'c';
|
|
947
|
+
* console.log(list.at(3)); // 'd';
|
|
948
|
+
*/
|
|
1160
949
|
at(index) {
|
|
1161
950
|
if (index < 0 || index >= this._length) return void 0;
|
|
1162
951
|
let current = this.head;
|
|
@@ -1173,54 +962,15 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1173
962
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
1174
963
|
}
|
|
1175
964
|
/**
|
|
1176
|
-
|
|
1177
|
-
|
|
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
|
-
* @example
|
|
1220
|
-
* // Get node at index
|
|
1221
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1222
|
-
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1223
|
-
*/
|
|
965
|
+
* Get the node reference at a given index.
|
|
966
|
+
* @remarks Time O(N), Space O(1)
|
|
967
|
+
* @param index - Zero-based index.
|
|
968
|
+
* @returns Node or undefined.
|
|
969
|
+
* @example
|
|
970
|
+
* // Get node at index
|
|
971
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
972
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
973
|
+
*/
|
|
1224
974
|
getNodeAt(index) {
|
|
1225
975
|
if (index < 0 || index >= this._length) return void 0;
|
|
1226
976
|
let current = this.head;
|
|
@@ -1228,55 +978,16 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1228
978
|
return current;
|
|
1229
979
|
}
|
|
1230
980
|
/**
|
|
1231
|
-
|
|
1232
|
-
|
|
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
|
-
* @example
|
|
1275
|
-
* // Remove by index
|
|
1276
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1277
|
-
* list.deleteAt(1);
|
|
1278
|
-
* console.log(list.toArray()); // ['a', 'c'];
|
|
1279
|
-
*/
|
|
981
|
+
* Delete the element at an index.
|
|
982
|
+
* @remarks Time O(N), Space O(1)
|
|
983
|
+
* @param index - Zero-based index.
|
|
984
|
+
* @returns Removed element or undefined.
|
|
985
|
+
* @example
|
|
986
|
+
* // Remove by index
|
|
987
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
988
|
+
* list.deleteAt(1);
|
|
989
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
990
|
+
*/
|
|
1280
991
|
deleteAt(index) {
|
|
1281
992
|
if (index < 0 || index >= this._length) return void 0;
|
|
1282
993
|
if (index === 0) return this.shift();
|
|
@@ -1289,55 +1000,16 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1289
1000
|
return value;
|
|
1290
1001
|
}
|
|
1291
1002
|
/**
|
|
1292
|
-
|
|
1293
|
-
|
|
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
|
-
* @example
|
|
1336
|
-
* // Remove first occurrence
|
|
1337
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1338
|
-
* list.delete(2);
|
|
1339
|
-
* console.log(list.toArray()); // [1, 3, 2];
|
|
1340
|
-
*/
|
|
1003
|
+
* Delete the first match by value/node.
|
|
1004
|
+
* @remarks Time O(N), Space O(1)
|
|
1005
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1006
|
+
* @returns True if removed.
|
|
1007
|
+
* @example
|
|
1008
|
+
* // Remove first occurrence
|
|
1009
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1010
|
+
* list.delete(2);
|
|
1011
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1012
|
+
*/
|
|
1341
1013
|
delete(elementOrNode) {
|
|
1342
1014
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
1343
1015
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -1354,56 +1026,17 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1354
1026
|
return true;
|
|
1355
1027
|
}
|
|
1356
1028
|
/**
|
|
1357
|
-
|
|
1358
|
-
|
|
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
|
-
* @example
|
|
1402
|
-
* // Insert at index
|
|
1403
|
-
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1404
|
-
* list.addAt(1, 2);
|
|
1405
|
-
* console.log(list.toArray()); // [1, 2, 3];
|
|
1406
|
-
*/
|
|
1029
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1030
|
+
* @remarks Time O(N), Space O(1)
|
|
1031
|
+
* @param index - Zero-based index.
|
|
1032
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1033
|
+
* @returns True if inserted.
|
|
1034
|
+
* @example
|
|
1035
|
+
* // Insert at index
|
|
1036
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1037
|
+
* list.addAt(1, 2);
|
|
1038
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1039
|
+
*/
|
|
1407
1040
|
addAt(index, newElementOrNode) {
|
|
1408
1041
|
if (index < 0 || index > this._length) return false;
|
|
1409
1042
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -1429,163 +1062,41 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1429
1062
|
return true;
|
|
1430
1063
|
}
|
|
1431
1064
|
/**
|
|
1432
|
-
|
|
1433
|
-
|
|
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
|
-
* @example
|
|
1476
|
-
* // Check empty
|
|
1477
|
-
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1478
|
-
*/
|
|
1065
|
+
* Check whether the list is empty.
|
|
1066
|
+
* @remarks Time O(1), Space O(1)
|
|
1067
|
+
* @returns True if length is 0.
|
|
1068
|
+
* @example
|
|
1069
|
+
* // Check empty
|
|
1070
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1071
|
+
*/
|
|
1479
1072
|
isEmpty() {
|
|
1480
1073
|
return this._length === 0;
|
|
1481
1074
|
}
|
|
1482
1075
|
/**
|
|
1483
|
-
|
|
1484
|
-
|
|
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
|
-
* @example
|
|
1527
|
-
* // Remove all
|
|
1528
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1529
|
-
* list.clear();
|
|
1530
|
-
* console.log(list.isEmpty()); // true;
|
|
1531
|
-
*/
|
|
1076
|
+
* Remove all nodes and reset length.
|
|
1077
|
+
* @remarks Time O(N), Space O(1)
|
|
1078
|
+
* @returns void
|
|
1079
|
+
* @example
|
|
1080
|
+
* // Remove all
|
|
1081
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1082
|
+
* list.clear();
|
|
1083
|
+
* console.log(list.isEmpty()); // true;
|
|
1084
|
+
*/
|
|
1532
1085
|
clear() {
|
|
1533
1086
|
this._head = void 0;
|
|
1534
1087
|
this._tail = void 0;
|
|
1535
1088
|
this._length = 0;
|
|
1536
1089
|
}
|
|
1537
1090
|
/**
|
|
1538
|
-
|
|
1539
|
-
|
|
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
|
-
* @example
|
|
1584
|
-
* // Reverse the list in-place
|
|
1585
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1586
|
-
* list.reverse();
|
|
1587
|
-
* console.log([...list]); // [4, 3, 2, 1];
|
|
1588
|
-
*/
|
|
1091
|
+
* Reverse the list in place.
|
|
1092
|
+
* @remarks Time O(N), Space O(1)
|
|
1093
|
+
* @returns This list.
|
|
1094
|
+
* @example
|
|
1095
|
+
* // Reverse the list in-place
|
|
1096
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1097
|
+
* list.reverse();
|
|
1098
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1099
|
+
*/
|
|
1589
1100
|
reverse() {
|
|
1590
1101
|
if (!this.head || this.head === this.tail) return this;
|
|
1591
1102
|
let prev;
|
|
@@ -1760,126 +1271,44 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1760
1271
|
return false;
|
|
1761
1272
|
}
|
|
1762
1273
|
/**
|
|
1763
|
-
|
|
1764
|
-
|
|
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
|
-
* @example
|
|
1807
|
-
* // Deep copy
|
|
1808
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1809
|
-
* const copy = list.clone();
|
|
1810
|
-
* copy.pop();
|
|
1811
|
-
* console.log(list.length); // 3;
|
|
1812
|
-
* console.log(copy.length); // 2;
|
|
1813
|
-
*/
|
|
1274
|
+
* Deep clone this list (values are copied by reference).
|
|
1275
|
+
* @remarks Time O(N), Space O(N)
|
|
1276
|
+
* @returns A new list with the same element sequence.
|
|
1277
|
+
* @example
|
|
1278
|
+
* // Deep copy
|
|
1279
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1280
|
+
* const copy = list.clone();
|
|
1281
|
+
* copy.pop();
|
|
1282
|
+
* console.log(list.length); // 3;
|
|
1283
|
+
* console.log(copy.length); // 2;
|
|
1284
|
+
*/
|
|
1814
1285
|
clone() {
|
|
1815
1286
|
const out = this._createInstance();
|
|
1816
1287
|
for (const v of this) out.push(v);
|
|
1817
1288
|
return out;
|
|
1818
1289
|
}
|
|
1819
1290
|
/**
|
|
1820
|
-
|
|
1821
|
-
|
|
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
|
-
* @example
|
|
1868
|
-
* // SinglyLinkedList filter and map operations
|
|
1869
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1870
|
-
*
|
|
1871
|
-
* // Filter even numbers
|
|
1872
|
-
* const filtered = list.filter(value => value % 2 === 0);
|
|
1873
|
-
* console.log(filtered.length); // 2;
|
|
1874
|
-
*
|
|
1875
|
-
* // Map to double values
|
|
1876
|
-
* const doubled = list.map(value => value * 2);
|
|
1877
|
-
* console.log(doubled.length); // 5;
|
|
1878
|
-
*
|
|
1879
|
-
* // Use reduce to sum
|
|
1880
|
-
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1881
|
-
* console.log(sum); // 15;
|
|
1882
|
-
*/
|
|
1291
|
+
* Filter values into a new list of the same class.
|
|
1292
|
+
* @remarks Time O(N), Space O(N)
|
|
1293
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1294
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1295
|
+
* @returns A new list with kept values.
|
|
1296
|
+
* @example
|
|
1297
|
+
* // SinglyLinkedList filter and map operations
|
|
1298
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1299
|
+
*
|
|
1300
|
+
* // Filter even numbers
|
|
1301
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1302
|
+
* console.log(filtered.length); // 2;
|
|
1303
|
+
*
|
|
1304
|
+
* // Map to double values
|
|
1305
|
+
* const doubled = list.map(value => value * 2);
|
|
1306
|
+
* console.log(doubled.length); // 5;
|
|
1307
|
+
*
|
|
1308
|
+
* // Use reduce to sum
|
|
1309
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1310
|
+
* console.log(sum); // 15;
|
|
1311
|
+
*/
|
|
1883
1312
|
filter(callback, thisArg) {
|
|
1884
1313
|
const out = this._createInstance();
|
|
1885
1314
|
let index = 0;
|
|
@@ -1903,68 +1332,27 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1903
1332
|
return out;
|
|
1904
1333
|
}
|
|
1905
1334
|
/**
|
|
1906
|
-
|
|
1907
|
-
|
|
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
|
-
* @example
|
|
1957
|
-
* // Transform elements
|
|
1958
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1959
|
-
* const doubled = list.map(n => n * 2);
|
|
1960
|
-
* console.log([...doubled]); // [2, 4, 6];
|
|
1961
|
-
*/
|
|
1335
|
+
* Map values into a new list (possibly different element type).
|
|
1336
|
+
* @remarks Time O(N), Space O(N)
|
|
1337
|
+
* @template EM
|
|
1338
|
+
* @template RM
|
|
1339
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1340
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1341
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1342
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1343
|
+
* @example
|
|
1344
|
+
* // Transform elements
|
|
1345
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1346
|
+
* const doubled = list.map(n => n * 2);
|
|
1347
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1348
|
+
*/
|
|
1962
1349
|
map(callback, options, thisArg) {
|
|
1963
1350
|
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
1964
1351
|
let index = 0;
|
|
1965
1352
|
for (const value of this) out.push(callback.call(thisArg, value, index++, this));
|
|
1966
1353
|
return out;
|
|
1967
1354
|
}
|
|
1355
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
1968
1356
|
/**
|
|
1969
1357
|
* (Protected) Create a node from a value.
|
|
1970
1358
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2150,124 +1538,32 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2150
1538
|
this._autoCompactRatio = value;
|
|
2151
1539
|
}
|
|
2152
1540
|
/**
|
|
2153
|
-
|
|
2154
|
-
|
|
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
|
-
* @example
|
|
2199
|
-
* // Track queue length
|
|
2200
|
-
* const q = new Queue<number>();
|
|
2201
|
-
* console.log(q.length); // 0;
|
|
2202
|
-
* q.push(1);
|
|
2203
|
-
* q.push(2);
|
|
2204
|
-
* console.log(q.length); // 2;
|
|
2205
|
-
*/
|
|
1541
|
+
* Get the number of elements currently in the queue.
|
|
1542
|
+
* @remarks Time O(1), Space O(1)
|
|
1543
|
+
* @returns Current length.
|
|
1544
|
+
* @example
|
|
1545
|
+
* // Track queue length
|
|
1546
|
+
* const q = new Queue<number>();
|
|
1547
|
+
* console.log(q.length); // 0;
|
|
1548
|
+
* q.push(1);
|
|
1549
|
+
* q.push(2);
|
|
1550
|
+
* console.log(q.length); // 2;
|
|
1551
|
+
*/
|
|
2206
1552
|
get length() {
|
|
2207
1553
|
return this.elements.length - this._offset;
|
|
2208
1554
|
}
|
|
2209
1555
|
/**
|
|
2210
|
-
|
|
2211
|
-
* @remarks Time O(1), Space O(1)
|
|
2212
|
-
* @returns Front element or undefined.
|
|
2213
|
-
|
|
2214
|
-
|
|
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
|
-
* @example
|
|
2256
|
-
* // View the front element
|
|
2257
|
-
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2258
|
-
* console.log(q.first); // 'first';
|
|
2259
|
-
* console.log(q.length); // 3;
|
|
2260
|
-
*/
|
|
2261
|
-
get first() {
|
|
2262
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2263
|
-
}
|
|
2264
|
-
/**
|
|
2265
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
1556
|
+
* Get the first element (front) without removing it.
|
|
2266
1557
|
* @remarks Time O(1), Space O(1)
|
|
2267
1558
|
* @returns Front element or undefined.
|
|
1559
|
+
* @example
|
|
1560
|
+
* // View the front element
|
|
1561
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1562
|
+
* console.log(q.first); // 'first';
|
|
1563
|
+
* console.log(q.length); // 3;
|
|
2268
1564
|
*/
|
|
2269
|
-
|
|
2270
|
-
return this.
|
|
1565
|
+
get first() {
|
|
1566
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2271
1567
|
}
|
|
2272
1568
|
/**
|
|
2273
1569
|
* Get the last element (back) without removing it.
|
|
@@ -2288,131 +1584,55 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2288
1584
|
return new _Queue(elements);
|
|
2289
1585
|
}
|
|
2290
1586
|
/**
|
|
2291
|
-
|
|
2292
|
-
|
|
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
|
-
* @example
|
|
2337
|
-
* // Queue for...of iteration and isEmpty check
|
|
2338
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2339
|
-
*
|
|
2340
|
-
* const elements: string[] = [];
|
|
2341
|
-
* for (const item of queue) {
|
|
2342
|
-
* elements.push(item);
|
|
2343
|
-
* }
|
|
2344
|
-
*
|
|
2345
|
-
* // Verify all elements are iterated in order
|
|
2346
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2347
|
-
*
|
|
2348
|
-
* // Process all elements
|
|
2349
|
-
* while (queue.length > 0) {
|
|
2350
|
-
* queue.shift();
|
|
2351
|
-
* }
|
|
2352
|
-
*
|
|
2353
|
-
* console.log(queue.length); // 0;
|
|
2354
|
-
*/
|
|
1587
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
1588
|
+
* @remarks Time O(1), Space O(1)
|
|
1589
|
+
* @returns Front element or undefined.
|
|
1590
|
+
*/
|
|
1591
|
+
peek() {
|
|
1592
|
+
return this.first;
|
|
1593
|
+
}
|
|
1594
|
+
/**
|
|
1595
|
+
* Check whether the queue is empty.
|
|
1596
|
+
* @remarks Time O(1), Space O(1)
|
|
1597
|
+
* @returns True if length is 0.
|
|
1598
|
+
* @example
|
|
1599
|
+
* // Queue for...of iteration and isEmpty check
|
|
1600
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1601
|
+
*
|
|
1602
|
+
* const elements: string[] = [];
|
|
1603
|
+
* for (const item of queue) {
|
|
1604
|
+
* elements.push(item);
|
|
1605
|
+
* }
|
|
1606
|
+
*
|
|
1607
|
+
* // Verify all elements are iterated in order
|
|
1608
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1609
|
+
*
|
|
1610
|
+
* // Process all elements
|
|
1611
|
+
* while (queue.length > 0) {
|
|
1612
|
+
* queue.shift();
|
|
1613
|
+
* }
|
|
1614
|
+
*
|
|
1615
|
+
* console.log(queue.length); // 0;
|
|
1616
|
+
*/
|
|
2355
1617
|
isEmpty() {
|
|
2356
1618
|
return this.length === 0;
|
|
2357
1619
|
}
|
|
2358
1620
|
/**
|
|
2359
|
-
|
|
2360
|
-
|
|
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
|
-
* @example
|
|
2406
|
-
* // basic Queue creation and push operation
|
|
2407
|
-
* // Create a simple Queue with initial values
|
|
2408
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2409
|
-
*
|
|
2410
|
-
* // Verify the queue maintains insertion order
|
|
2411
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2412
|
-
*
|
|
2413
|
-
* // Check length
|
|
2414
|
-
* console.log(queue.length); // 5;
|
|
2415
|
-
*/
|
|
1621
|
+
* Enqueue one element at the back.
|
|
1622
|
+
* @remarks Time O(1), Space O(1)
|
|
1623
|
+
* @param element - Element to enqueue.
|
|
1624
|
+
* @returns True on success.
|
|
1625
|
+
* @example
|
|
1626
|
+
* // basic Queue creation and push operation
|
|
1627
|
+
* // Create a simple Queue with initial values
|
|
1628
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1629
|
+
*
|
|
1630
|
+
* // Verify the queue maintains insertion order
|
|
1631
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1632
|
+
*
|
|
1633
|
+
* // Check length
|
|
1634
|
+
* console.log(queue.length); // 5;
|
|
1635
|
+
*/
|
|
2416
1636
|
push(element) {
|
|
2417
1637
|
this.elements.push(element);
|
|
2418
1638
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -2433,66 +1653,24 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2433
1653
|
return ans;
|
|
2434
1654
|
}
|
|
2435
1655
|
/**
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
* @example
|
|
2482
|
-
* // Queue shift and peek operations
|
|
2483
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2484
|
-
*
|
|
2485
|
-
* // Peek at the front element without removing it
|
|
2486
|
-
* console.log(queue.first); // 10;
|
|
2487
|
-
*
|
|
2488
|
-
* // Remove and get the first element (FIFO)
|
|
2489
|
-
* const first = queue.shift();
|
|
2490
|
-
* console.log(first); // 10;
|
|
2491
|
-
*
|
|
2492
|
-
* // Verify remaining elements and length decreased
|
|
2493
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
2494
|
-
* console.log(queue.length); // 3;
|
|
2495
|
-
*/
|
|
1656
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1657
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1658
|
+
* @returns Removed element or undefined.
|
|
1659
|
+
* @example
|
|
1660
|
+
* // Queue shift and peek operations
|
|
1661
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1662
|
+
*
|
|
1663
|
+
* // Peek at the front element without removing it
|
|
1664
|
+
* console.log(queue.first); // 10;
|
|
1665
|
+
*
|
|
1666
|
+
* // Remove and get the first element (FIFO)
|
|
1667
|
+
* const first = queue.shift();
|
|
1668
|
+
* console.log(first); // 10;
|
|
1669
|
+
*
|
|
1670
|
+
* // Verify remaining elements and length decreased
|
|
1671
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1672
|
+
* console.log(queue.length); // 3;
|
|
1673
|
+
*/
|
|
2496
1674
|
shift() {
|
|
2497
1675
|
if (this.length === 0) return void 0;
|
|
2498
1676
|
const first = this.first;
|
|
@@ -2501,55 +1679,16 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2501
1679
|
return first;
|
|
2502
1680
|
}
|
|
2503
1681
|
/**
|
|
2504
|
-
|
|
2505
|
-
|
|
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
|
-
* @example
|
|
2548
|
-
* // Remove specific element
|
|
2549
|
-
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2550
|
-
* q.delete(2);
|
|
2551
|
-
* console.log(q.length); // 3;
|
|
2552
|
-
*/
|
|
1682
|
+
* Delete the first occurrence of a specific element.
|
|
1683
|
+
* @remarks Time O(N), Space O(1)
|
|
1684
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1685
|
+
* @returns True if an element was removed.
|
|
1686
|
+
* @example
|
|
1687
|
+
* // Remove specific element
|
|
1688
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1689
|
+
* q.delete(2);
|
|
1690
|
+
* console.log(q.length); // 3;
|
|
1691
|
+
*/
|
|
2553
1692
|
delete(element) {
|
|
2554
1693
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
2555
1694
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -2560,55 +1699,16 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2560
1699
|
return false;
|
|
2561
1700
|
}
|
|
2562
1701
|
/**
|
|
2563
|
-
|
|
2564
|
-
|
|
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
|
-
* @example
|
|
2607
|
-
* // Access element by index
|
|
2608
|
-
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2609
|
-
* console.log(q.at(0)); // 'a';
|
|
2610
|
-
* console.log(q.at(2)); // 'c';
|
|
2611
|
-
*/
|
|
1702
|
+
* Get the element at a given logical index.
|
|
1703
|
+
* @remarks Time O(1), Space O(1)
|
|
1704
|
+
* @param index - Zero-based index from the front.
|
|
1705
|
+
* @returns Element or undefined.
|
|
1706
|
+
* @example
|
|
1707
|
+
* // Access element by index
|
|
1708
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1709
|
+
* console.log(q.at(0)); // 'a';
|
|
1710
|
+
* console.log(q.at(2)); // 'c';
|
|
1711
|
+
*/
|
|
2612
1712
|
at(index) {
|
|
2613
1713
|
if (index < 0 || index >= this.length) return void 0;
|
|
2614
1714
|
return this._elements[this._offset + index];
|
|
@@ -2675,110 +1775,31 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2675
1775
|
return this;
|
|
2676
1776
|
}
|
|
2677
1777
|
/**
|
|
2678
|
-
|
|
2679
|
-
|
|
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
|
-
* @example
|
|
2722
|
-
* // Remove all elements
|
|
2723
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2724
|
-
* q.clear();
|
|
2725
|
-
* console.log(q.length); // 0;
|
|
2726
|
-
*/
|
|
1778
|
+
* Remove all elements and reset offset.
|
|
1779
|
+
* @remarks Time O(1), Space O(1)
|
|
1780
|
+
* @returns void
|
|
1781
|
+
* @example
|
|
1782
|
+
* // Remove all elements
|
|
1783
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1784
|
+
* q.clear();
|
|
1785
|
+
* console.log(q.length); // 0;
|
|
1786
|
+
*/
|
|
2727
1787
|
clear() {
|
|
2728
1788
|
this._elements = [];
|
|
2729
1789
|
this._offset = 0;
|
|
2730
1790
|
}
|
|
2731
1791
|
/**
|
|
2732
|
-
|
|
2733
|
-
|
|
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
|
-
* @example
|
|
2775
|
-
* // Reclaim unused memory
|
|
2776
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2777
|
-
* q.shift();
|
|
2778
|
-
* q.shift();
|
|
2779
|
-
* q.compact();
|
|
2780
|
-
* console.log(q.length); // 3;
|
|
2781
|
-
*/
|
|
1792
|
+
* Compact storage by discarding consumed head elements.
|
|
1793
|
+
* @remarks Time O(N), Space O(N)
|
|
1794
|
+
* @returns True when compaction performed.
|
|
1795
|
+
* @example
|
|
1796
|
+
* // Reclaim unused memory
|
|
1797
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1798
|
+
* q.shift();
|
|
1799
|
+
* q.shift();
|
|
1800
|
+
* q.compact();
|
|
1801
|
+
* console.log(q.length); // 3;
|
|
1802
|
+
*/
|
|
2782
1803
|
compact() {
|
|
2783
1804
|
this._elements = this.elements.slice(this._offset);
|
|
2784
1805
|
this._offset = 0;
|
|
@@ -2804,57 +1825,17 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2804
1825
|
return removed;
|
|
2805
1826
|
}
|
|
2806
1827
|
/**
|
|
2807
|
-
|
|
2808
|
-
|
|
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
|
-
* @example
|
|
2851
|
-
* // Create independent copy
|
|
2852
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2853
|
-
* const copy = q.clone();
|
|
2854
|
-
* copy.shift();
|
|
2855
|
-
* console.log(q.length); // 3;
|
|
2856
|
-
* console.log(copy.length); // 2;
|
|
2857
|
-
*/
|
|
1828
|
+
* Deep clone this queue and its parameters.
|
|
1829
|
+
* @remarks Time O(N), Space O(N)
|
|
1830
|
+
* @returns A new queue with the same content and options.
|
|
1831
|
+
* @example
|
|
1832
|
+
* // Create independent copy
|
|
1833
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1834
|
+
* const copy = q.clone();
|
|
1835
|
+
* copy.shift();
|
|
1836
|
+
* console.log(q.length); // 3;
|
|
1837
|
+
* console.log(copy.length); // 2;
|
|
1838
|
+
*/
|
|
2858
1839
|
clone() {
|
|
2859
1840
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2860
1841
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2862,57 +1843,17 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2862
1843
|
return out;
|
|
2863
1844
|
}
|
|
2864
1845
|
/**
|
|
2865
|
-
|
|
2866
|
-
|
|
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
|
-
* @example
|
|
2911
|
-
* // Filter elements
|
|
2912
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2913
|
-
* const evens = q.filter(x => x % 2 === 0);
|
|
2914
|
-
* console.log(evens.length); // 2;
|
|
2915
|
-
*/
|
|
1846
|
+
* Filter elements into a new queue of the same class.
|
|
1847
|
+
* @remarks Time O(N), Space O(N)
|
|
1848
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1849
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1850
|
+
* @returns A new queue with kept elements.
|
|
1851
|
+
* @example
|
|
1852
|
+
* // Filter elements
|
|
1853
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1854
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1855
|
+
* console.log(evens.length); // 2;
|
|
1856
|
+
*/
|
|
2916
1857
|
filter(predicate, thisArg) {
|
|
2917
1858
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2918
1859
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2924,59 +1865,20 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2924
1865
|
return out;
|
|
2925
1866
|
}
|
|
2926
1867
|
/**
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
* @example
|
|
2975
|
-
* // Transform elements
|
|
2976
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2977
|
-
* const doubled = q.map(x => x * 2);
|
|
2978
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2979
|
-
*/
|
|
1868
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1869
|
+
* @remarks Time O(N), Space O(N)
|
|
1870
|
+
* @template EM
|
|
1871
|
+
* @template RM
|
|
1872
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1873
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1874
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1875
|
+
* @returns A new Queue with mapped elements.
|
|
1876
|
+
* @example
|
|
1877
|
+
* // Transform elements
|
|
1878
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1879
|
+
* const doubled = q.map(x => x * 2);
|
|
1880
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1881
|
+
*/
|
|
2980
1882
|
map(callback, options, thisArg) {
|
|
2981
1883
|
const out = new this.constructor([], {
|
|
2982
1884
|
toElementFn: options?.toElementFn,
|
|
@@ -3091,7 +1993,11 @@ var Deque = class extends LinearBase {
|
|
|
3091
1993
|
static {
|
|
3092
1994
|
__name(this, "Deque");
|
|
3093
1995
|
}
|
|
3094
|
-
|
|
1996
|
+
/**
|
|
1997
|
+
* Counter for shift/pop operations since last compaction check.
|
|
1998
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
1999
|
+
*/
|
|
2000
|
+
_compactCounter = 0;
|
|
3095
2001
|
constructor(elements = [], options) {
|
|
3096
2002
|
super(options);
|
|
3097
2003
|
if (options) {
|
|
@@ -3142,11 +2048,6 @@ var Deque = class extends LinearBase {
|
|
|
3142
2048
|
set autoCompactRatio(value) {
|
|
3143
2049
|
this._autoCompactRatio = value;
|
|
3144
2050
|
}
|
|
3145
|
-
/**
|
|
3146
|
-
* Counter for shift/pop operations since last compaction check.
|
|
3147
|
-
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
3148
|
-
*/
|
|
3149
|
-
_compactCounter = 0;
|
|
3150
2051
|
_bucketFirst = 0;
|
|
3151
2052
|
/**
|
|
3152
2053
|
* Get the index of the first bucket in use.
|
|
@@ -3211,146 +2112,36 @@ var Deque = class extends LinearBase {
|
|
|
3211
2112
|
return this._length;
|
|
3212
2113
|
}
|
|
3213
2114
|
/**
|
|
3214
|
-
|
|
3215
|
-
|
|
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
|
-
* @example
|
|
3256
|
-
* // Deque peek at both ends
|
|
3257
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3258
|
-
*
|
|
3259
|
-
* // Get first element without removing
|
|
3260
|
-
* const first = deque.at(0);
|
|
3261
|
-
* console.log(first); // 10;
|
|
3262
|
-
*
|
|
3263
|
-
* // Get last element without removing
|
|
3264
|
-
* const last = deque.at(deque.length - 1);
|
|
3265
|
-
* console.log(last); // 50;
|
|
3266
|
-
*
|
|
3267
|
-
* // Length unchanged
|
|
3268
|
-
* console.log(deque.length); // 5;
|
|
3269
|
-
*/
|
|
3270
|
-
/**
|
|
3271
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
3272
|
-
* @remarks Time O(1), Space O(1)
|
|
3273
|
-
* @returns Front element or undefined.
|
|
2115
|
+
* Deque peek at both ends
|
|
2116
|
+
* @example
|
|
2117
|
+
* // Deque peek at both ends
|
|
2118
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2119
|
+
*
|
|
2120
|
+
* // Get first element without removing
|
|
2121
|
+
* const first = deque.at(0);
|
|
2122
|
+
* console.log(first); // 10;
|
|
2123
|
+
*
|
|
2124
|
+
* // Get last element without removing
|
|
2125
|
+
* const last = deque.at(deque.length - 1);
|
|
2126
|
+
* console.log(last); // 50;
|
|
2127
|
+
*
|
|
2128
|
+
* // Length unchanged
|
|
2129
|
+
* console.log(deque.length); // 5;
|
|
3274
2130
|
*/
|
|
3275
|
-
peek() {
|
|
3276
|
-
return this.first;
|
|
3277
|
-
}
|
|
3278
|
-
/**
|
|
3279
|
-
* Deque peek at both ends
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
* @example
|
|
3284
|
-
* // Deque peek at both ends
|
|
3285
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3286
|
-
*
|
|
3287
|
-
* // Get first element without removing
|
|
3288
|
-
* const first = deque.at(0);
|
|
3289
|
-
* console.log(first); // 10;
|
|
3290
|
-
*
|
|
3291
|
-
* // Get last element without removing
|
|
3292
|
-
* const last = deque.at(deque.length - 1);
|
|
3293
|
-
* console.log(last); // 50;
|
|
3294
|
-
*
|
|
3295
|
-
* // Length unchanged
|
|
3296
|
-
* console.log(deque.length); // 5;
|
|
3297
|
-
*/
|
|
3298
2131
|
get first() {
|
|
3299
2132
|
if (this._length === 0) return;
|
|
3300
2133
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3301
2134
|
}
|
|
3302
2135
|
/**
|
|
3303
|
-
|
|
3304
|
-
|
|
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
|
-
* @example
|
|
3349
|
-
* // Peek at the back element
|
|
3350
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3351
|
-
* console.log(dq.last); // 'c';
|
|
3352
|
-
* console.log(dq.first); // 'a';
|
|
3353
|
-
*/
|
|
2136
|
+
* Get the last element without removing it.
|
|
2137
|
+
* @remarks Time O(1), Space O(1)
|
|
2138
|
+
* @returns Last element or undefined.
|
|
2139
|
+
* @example
|
|
2140
|
+
* // Peek at the back element
|
|
2141
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2142
|
+
* console.log(dq.last); // 'c';
|
|
2143
|
+
* console.log(dq.first); // 'a';
|
|
2144
|
+
*/
|
|
3354
2145
|
get last() {
|
|
3355
2146
|
if (this._length === 0) return;
|
|
3356
2147
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3369,71 +2160,56 @@ var Deque = class extends LinearBase {
|
|
|
3369
2160
|
return new this(data, options);
|
|
3370
2161
|
}
|
|
3371
2162
|
/**
|
|
3372
|
-
|
|
3373
|
-
|
|
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
|
-
* // Verify the deque maintains insertion order
|
|
3424
|
-
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3425
|
-
*
|
|
3426
|
-
* // Check length
|
|
3427
|
-
* console.log(deque.length); // 5;
|
|
3428
|
-
*
|
|
3429
|
-
* // Push to the end
|
|
3430
|
-
* deque.push(6);
|
|
3431
|
-
* console.log(deque.length); // 6;
|
|
3432
|
-
*
|
|
3433
|
-
* // Pop from the end
|
|
3434
|
-
* const last = deque.pop();
|
|
3435
|
-
* console.log(last); // 6;
|
|
3436
|
-
*/
|
|
2163
|
+
* Get the first element without removing it.
|
|
2164
|
+
* @remarks Time O(1), Space O(1)
|
|
2165
|
+
* @returns First element or undefined.
|
|
2166
|
+
* @example
|
|
2167
|
+
* // Deque peek at both ends
|
|
2168
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2169
|
+
*
|
|
2170
|
+
* // Get first element without removing
|
|
2171
|
+
* const first = deque.at(0);
|
|
2172
|
+
* console.log(first); // 10;
|
|
2173
|
+
*
|
|
2174
|
+
* // Get last element without removing
|
|
2175
|
+
* const last = deque.at(deque.length - 1);
|
|
2176
|
+
* console.log(last); // 50;
|
|
2177
|
+
*
|
|
2178
|
+
* // Length unchanged
|
|
2179
|
+
* console.log(deque.length); // 5;
|
|
2180
|
+
*/
|
|
2181
|
+
/**
|
|
2182
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
2183
|
+
* @remarks Time O(1), Space O(1)
|
|
2184
|
+
* @returns Front element or undefined.
|
|
2185
|
+
*/
|
|
2186
|
+
peek() {
|
|
2187
|
+
return this.first;
|
|
2188
|
+
}
|
|
2189
|
+
/**
|
|
2190
|
+
* Append one element at the back.
|
|
2191
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2192
|
+
* @param element - Element to append.
|
|
2193
|
+
* @returns True when appended.
|
|
2194
|
+
* @example
|
|
2195
|
+
* // basic Deque creation and push/pop operations
|
|
2196
|
+
* // Create a simple Deque with initial values
|
|
2197
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
2198
|
+
*
|
|
2199
|
+
* // Verify the deque maintains insertion order
|
|
2200
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
2201
|
+
*
|
|
2202
|
+
* // Check length
|
|
2203
|
+
* console.log(deque.length); // 5;
|
|
2204
|
+
*
|
|
2205
|
+
* // Push to the end
|
|
2206
|
+
* deque.push(6);
|
|
2207
|
+
* console.log(deque.length); // 6;
|
|
2208
|
+
*
|
|
2209
|
+
* // Pop from the end
|
|
2210
|
+
* const last = deque.pop();
|
|
2211
|
+
* console.log(last); // 6;
|
|
2212
|
+
*/
|
|
3437
2213
|
push(element) {
|
|
3438
2214
|
if (this._length) {
|
|
3439
2215
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -3453,57 +2229,15 @@ var Deque = class extends LinearBase {
|
|
|
3453
2229
|
return true;
|
|
3454
2230
|
}
|
|
3455
2231
|
/**
|
|
3456
|
-
|
|
3457
|
-
|
|
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
|
-
* @example
|
|
3502
|
-
* // Remove from the back
|
|
3503
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3504
|
-
* console.log(dq.pop()); // 3;
|
|
3505
|
-
* console.log(dq.length); // 2;
|
|
3506
|
-
*/
|
|
2232
|
+
* Remove and return the last element.
|
|
2233
|
+
* @remarks Time O(1), Space O(1)
|
|
2234
|
+
* @returns Removed element or undefined.
|
|
2235
|
+
* @example
|
|
2236
|
+
* // Remove from the back
|
|
2237
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2238
|
+
* console.log(dq.pop()); // 3;
|
|
2239
|
+
* console.log(dq.length); // 2;
|
|
2240
|
+
*/
|
|
3507
2241
|
pop() {
|
|
3508
2242
|
if (this._length === 0) return;
|
|
3509
2243
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3523,57 +2257,15 @@ var Deque = class extends LinearBase {
|
|
|
3523
2257
|
return element;
|
|
3524
2258
|
}
|
|
3525
2259
|
/**
|
|
3526
|
-
|
|
3527
|
-
|
|
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
|
-
* @example
|
|
3572
|
-
* // Remove from the front
|
|
3573
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3574
|
-
* console.log(dq.shift()); // 1;
|
|
3575
|
-
* console.log(dq.length); // 2;
|
|
3576
|
-
*/
|
|
2260
|
+
* Remove and return the first element.
|
|
2261
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2262
|
+
* @returns Removed element or undefined.
|
|
2263
|
+
* @example
|
|
2264
|
+
* // Remove from the front
|
|
2265
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2266
|
+
* console.log(dq.shift()); // 1;
|
|
2267
|
+
* console.log(dq.length); // 2;
|
|
2268
|
+
*/
|
|
3577
2269
|
shift() {
|
|
3578
2270
|
if (this._length === 0) return;
|
|
3579
2271
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -3593,68 +2285,26 @@ var Deque = class extends LinearBase {
|
|
|
3593
2285
|
return element;
|
|
3594
2286
|
}
|
|
3595
2287
|
/**
|
|
3596
|
-
|
|
3597
|
-
|
|
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
|
-
* @example
|
|
3643
|
-
* // Deque shift and unshift operations
|
|
3644
|
-
* const deque = new Deque<number>([20, 30, 40]);
|
|
3645
|
-
*
|
|
3646
|
-
* // Unshift adds to the front
|
|
3647
|
-
* deque.unshift(10);
|
|
3648
|
-
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3649
|
-
*
|
|
3650
|
-
* // Shift removes from the front (O(1) complexity!)
|
|
3651
|
-
* const first = deque.shift();
|
|
3652
|
-
* console.log(first); // 10;
|
|
3653
|
-
*
|
|
3654
|
-
* // Verify remaining elements
|
|
3655
|
-
* console.log([...deque]); // [20, 30, 40];
|
|
3656
|
-
* console.log(deque.length); // 3;
|
|
3657
|
-
*/
|
|
2288
|
+
* Prepend one element at the front.
|
|
2289
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2290
|
+
* @param element - Element to prepend.
|
|
2291
|
+
* @returns True when prepended.
|
|
2292
|
+
* @example
|
|
2293
|
+
* // Deque shift and unshift operations
|
|
2294
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
2295
|
+
*
|
|
2296
|
+
* // Unshift adds to the front
|
|
2297
|
+
* deque.unshift(10);
|
|
2298
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
2299
|
+
*
|
|
2300
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
2301
|
+
* const first = deque.shift();
|
|
2302
|
+
* console.log(first); // 10;
|
|
2303
|
+
*
|
|
2304
|
+
* // Verify remaining elements
|
|
2305
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
2306
|
+
* console.log(deque.length); // 3;
|
|
2307
|
+
*/
|
|
3658
2308
|
unshift(element) {
|
|
3659
2309
|
if (this._length) {
|
|
3660
2310
|
if (this._firstInBucket > 0) {
|
|
@@ -3708,107 +2358,27 @@ var Deque = class extends LinearBase {
|
|
|
3708
2358
|
return ans;
|
|
3709
2359
|
}
|
|
3710
2360
|
/**
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
* @example
|
|
3755
|
-
* // Check if empty
|
|
3756
|
-
* const dq = new Deque();
|
|
3757
|
-
* console.log(dq.isEmpty()); // true;
|
|
3758
|
-
*/
|
|
2361
|
+
* Check whether the deque is empty.
|
|
2362
|
+
* @remarks Time O(1), Space O(1)
|
|
2363
|
+
* @returns True if length is 0.
|
|
2364
|
+
* @example
|
|
2365
|
+
* // Check if empty
|
|
2366
|
+
* const dq = new Deque();
|
|
2367
|
+
* console.log(dq.isEmpty()); // true;
|
|
2368
|
+
*/
|
|
3759
2369
|
isEmpty() {
|
|
3760
2370
|
return this._length === 0;
|
|
3761
2371
|
}
|
|
3762
2372
|
/**
|
|
3763
|
-
|
|
3764
|
-
|
|
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
|
-
* @example
|
|
3807
|
-
* // Remove all elements
|
|
3808
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3809
|
-
* dq.clear();
|
|
3810
|
-
* console.log(dq.length); // 0;
|
|
3811
|
-
*/
|
|
2373
|
+
* Remove all elements and reset structure.
|
|
2374
|
+
* @remarks Time O(1), Space O(1)
|
|
2375
|
+
* @returns void
|
|
2376
|
+
* @example
|
|
2377
|
+
* // Remove all elements
|
|
2378
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2379
|
+
* dq.clear();
|
|
2380
|
+
* console.log(dq.length); // 0;
|
|
2381
|
+
*/
|
|
3812
2382
|
clear() {
|
|
3813
2383
|
this._buckets = [new Array(this._bucketSize)];
|
|
3814
2384
|
this._bucketCount = 1;
|
|
@@ -3816,55 +2386,16 @@ var Deque = class extends LinearBase {
|
|
|
3816
2386
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
3817
2387
|
}
|
|
3818
2388
|
/**
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
* @example
|
|
3863
|
-
* // Access by index
|
|
3864
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3865
|
-
* console.log(dq.at(0)); // 'a';
|
|
3866
|
-
* console.log(dq.at(2)); // 'c';
|
|
3867
|
-
*/
|
|
2389
|
+
* Get the element at a given position.
|
|
2390
|
+
* @remarks Time O(1), Space O(1)
|
|
2391
|
+
* @param pos - Zero-based position from the front.
|
|
2392
|
+
* @returns Element or undefined.
|
|
2393
|
+
* @example
|
|
2394
|
+
* // Access by index
|
|
2395
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2396
|
+
* console.log(dq.at(0)); // 'a';
|
|
2397
|
+
* console.log(dq.at(2)); // 'c';
|
|
2398
|
+
*/
|
|
3868
2399
|
at(pos) {
|
|
3869
2400
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
3870
2401
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -4023,55 +2554,16 @@ var Deque = class extends LinearBase {
|
|
|
4023
2554
|
}
|
|
4024
2555
|
}
|
|
4025
2556
|
/**
|
|
4026
|
-
|
|
4027
|
-
|
|
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
|
-
* @example
|
|
4070
|
-
* // Remove element
|
|
4071
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4072
|
-
* dq.delete(2);
|
|
4073
|
-
* console.log(dq.length); // 2;
|
|
4074
|
-
*/
|
|
2557
|
+
* Delete the first occurrence of a value.
|
|
2558
|
+
* @remarks Time O(N), Space O(1)
|
|
2559
|
+
* @param element - Element to remove (using the configured equality).
|
|
2560
|
+
* @returns True if an element was removed.
|
|
2561
|
+
* @example
|
|
2562
|
+
* // Remove element
|
|
2563
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2564
|
+
* dq.delete(2);
|
|
2565
|
+
* console.log(dq.length); // 2;
|
|
2566
|
+
*/
|
|
4075
2567
|
delete(element) {
|
|
4076
2568
|
const size = this._length;
|
|
4077
2569
|
if (size === 0) return false;
|
|
@@ -4115,78 +2607,39 @@ var Deque = class extends LinearBase {
|
|
|
4115
2607
|
return this;
|
|
4116
2608
|
}
|
|
4117
2609
|
/**
|
|
4118
|
-
|
|
4119
|
-
|
|
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
|
-
* @example
|
|
4161
|
-
* // Deque for...of iteration and reverse
|
|
4162
|
-
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
4163
|
-
*
|
|
4164
|
-
* // Iterate forward
|
|
4165
|
-
* const forward: string[] = [];
|
|
4166
|
-
* for (const item of deque) {
|
|
4167
|
-
* forward.push(item);
|
|
4168
|
-
* }
|
|
4169
|
-
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
4170
|
-
*
|
|
4171
|
-
* // Reverse the deque
|
|
4172
|
-
* deque.reverse();
|
|
4173
|
-
* const backward: string[] = [];
|
|
4174
|
-
* for (const item of deque) {
|
|
4175
|
-
* backward.push(item);
|
|
4176
|
-
* }
|
|
4177
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4178
|
-
*/
|
|
4179
|
-
/**
|
|
4180
|
-
* Find the last value matching a predicate (scans back-to-front).
|
|
4181
|
-
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
4182
|
-
* @param predicate - Function called with (value, index, deque).
|
|
4183
|
-
* @returns Matching value or undefined.
|
|
4184
|
-
* @example
|
|
4185
|
-
* // Find last matching value
|
|
4186
|
-
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
4187
|
-
* console.log(d.findLast(v => v > 2)); // 5;
|
|
4188
|
-
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
4189
|
-
*/
|
|
2610
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
2611
|
+
* @remarks Time O(N), Space O(N)
|
|
2612
|
+
* @returns This deque.
|
|
2613
|
+
* @example
|
|
2614
|
+
* // Deque for...of iteration and reverse
|
|
2615
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2616
|
+
*
|
|
2617
|
+
* // Iterate forward
|
|
2618
|
+
* const forward: string[] = [];
|
|
2619
|
+
* for (const item of deque) {
|
|
2620
|
+
* forward.push(item);
|
|
2621
|
+
* }
|
|
2622
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2623
|
+
*
|
|
2624
|
+
* // Reverse the deque
|
|
2625
|
+
* deque.reverse();
|
|
2626
|
+
* const backward: string[] = [];
|
|
2627
|
+
* for (const item of deque) {
|
|
2628
|
+
* backward.push(item);
|
|
2629
|
+
* }
|
|
2630
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2631
|
+
*/
|
|
2632
|
+
/**
|
|
2633
|
+
* Find the last value matching a predicate (scans back-to-front).
|
|
2634
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2635
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2636
|
+
* @returns Matching value or undefined.
|
|
2637
|
+
* @example
|
|
2638
|
+
* // Find last matching value
|
|
2639
|
+
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
2640
|
+
* console.log(d.findLast(v => v > 2)); // 5;
|
|
2641
|
+
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
2642
|
+
*/
|
|
4190
2643
|
findLast(predicate) {
|
|
4191
2644
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4192
2645
|
const val = this.at(i);
|
|
@@ -4195,16 +2648,16 @@ var Deque = class extends LinearBase {
|
|
|
4195
2648
|
return void 0;
|
|
4196
2649
|
}
|
|
4197
2650
|
/**
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
2651
|
+
* Find the index of the last value matching a predicate (scans back-to-front).
|
|
2652
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2653
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2654
|
+
* @returns Matching index, or -1 if not found.
|
|
2655
|
+
* @example
|
|
2656
|
+
* // Find last matching index
|
|
2657
|
+
* const d = new Deque([10, 20, 30, 20, 10]);
|
|
2658
|
+
* console.log(d.findLastIndex(v => v === 20)); // 3;
|
|
2659
|
+
* console.log(d.findLastIndex(v => v === 10)); // 4;
|
|
2660
|
+
*/
|
|
4208
2661
|
findLastIndex(predicate) {
|
|
4209
2662
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4210
2663
|
if (predicate(this.at(i), i, this)) return i;
|
|
@@ -4212,28 +2665,26 @@ var Deque = class extends LinearBase {
|
|
|
4212
2665
|
return -1;
|
|
4213
2666
|
}
|
|
4214
2667
|
/**
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4236
|
-
*/
|
|
2668
|
+
* Deque for...of iteration and reverse
|
|
2669
|
+
* @example
|
|
2670
|
+
* // Deque for...of iteration and reverse
|
|
2671
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2672
|
+
*
|
|
2673
|
+
* // Iterate forward
|
|
2674
|
+
* const forward: string[] = [];
|
|
2675
|
+
* for (const item of deque) {
|
|
2676
|
+
* forward.push(item);
|
|
2677
|
+
* }
|
|
2678
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2679
|
+
*
|
|
2680
|
+
* // Reverse the deque
|
|
2681
|
+
* deque.reverse();
|
|
2682
|
+
* const backward: string[] = [];
|
|
2683
|
+
* for (const item of deque) {
|
|
2684
|
+
* backward.push(item);
|
|
2685
|
+
* }
|
|
2686
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2687
|
+
*/
|
|
4237
2688
|
reverse() {
|
|
4238
2689
|
this._buckets.reverse().forEach(function(bucket) {
|
|
4239
2690
|
bucket.reverse();
|
|
@@ -4267,81 +2718,22 @@ var Deque = class extends LinearBase {
|
|
|
4267
2718
|
return this;
|
|
4268
2719
|
}
|
|
4269
2720
|
/**
|
|
4270
|
-
*
|
|
2721
|
+
* Compact the deque by removing unused buckets.
|
|
4271
2722
|
* @remarks Time O(N), Space O(1)
|
|
4272
|
-
* @returns
|
|
4273
|
-
*/
|
|
4274
|
-
/**
|
|
4275
|
-
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
4276
|
-
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
4277
|
-
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2723
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
4278
2724
|
*/
|
|
4279
|
-
_autoCompact() {
|
|
4280
|
-
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
4281
|
-
this._compactCounter++;
|
|
4282
|
-
if (this._compactCounter < this._bucketSize) return;
|
|
4283
|
-
this._compactCounter = 0;
|
|
4284
|
-
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
4285
|
-
if (utilization < this._autoCompactRatio) {
|
|
4286
|
-
this.shrinkToFit();
|
|
4287
|
-
}
|
|
4288
|
-
}
|
|
4289
2725
|
/**
|
|
4290
2726
|
* Compact the deque by removing unused buckets.
|
|
4291
2727
|
* @remarks Time O(N), Space O(1)
|
|
4292
2728
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2729
|
+
* @example
|
|
2730
|
+
* // Reclaim memory
|
|
2731
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
2732
|
+
* dq.shift();
|
|
2733
|
+
* dq.shift();
|
|
2734
|
+
* dq.compact();
|
|
2735
|
+
* console.log(dq.length); // 3;
|
|
4293
2736
|
*/
|
|
4294
|
-
/**
|
|
4295
|
-
* Compact the deque by removing unused buckets.
|
|
4296
|
-
* @remarks Time O(N), Space O(1)
|
|
4297
|
-
* @returns True if compaction was performed (bucket count reduced).
|
|
4298
|
-
|
|
4299
|
-
|
|
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
|
-
* @example
|
|
4338
|
-
* // Reclaim memory
|
|
4339
|
-
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
4340
|
-
* dq.shift();
|
|
4341
|
-
* dq.shift();
|
|
4342
|
-
* dq.compact();
|
|
4343
|
-
* console.log(dq.length); // 3;
|
|
4344
|
-
*/
|
|
4345
2737
|
compact() {
|
|
4346
2738
|
const before = this._bucketCount;
|
|
4347
2739
|
this.shrinkToFit();
|
|
@@ -4369,57 +2761,17 @@ var Deque = class extends LinearBase {
|
|
|
4369
2761
|
this._compactCounter = 0;
|
|
4370
2762
|
}
|
|
4371
2763
|
/**
|
|
4372
|
-
|
|
4373
|
-
|
|
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
|
-
* @example
|
|
4416
|
-
* // Create independent copy
|
|
4417
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4418
|
-
* const copy = dq.clone();
|
|
4419
|
-
* copy.pop();
|
|
4420
|
-
* console.log(dq.length); // 3;
|
|
4421
|
-
* console.log(copy.length); // 2;
|
|
4422
|
-
*/
|
|
2764
|
+
* Deep clone this deque, preserving options.
|
|
2765
|
+
* @remarks Time O(N), Space O(N)
|
|
2766
|
+
* @returns A new deque with the same content and options.
|
|
2767
|
+
* @example
|
|
2768
|
+
* // Create independent copy
|
|
2769
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2770
|
+
* const copy = dq.clone();
|
|
2771
|
+
* copy.pop();
|
|
2772
|
+
* console.log(dq.length); // 3;
|
|
2773
|
+
* console.log(copy.length); // 2;
|
|
2774
|
+
*/
|
|
4423
2775
|
clone() {
|
|
4424
2776
|
return this._createLike(this, {
|
|
4425
2777
|
bucketSize: this.bucketSize,
|
|
@@ -4428,57 +2780,17 @@ var Deque = class extends LinearBase {
|
|
|
4428
2780
|
});
|
|
4429
2781
|
}
|
|
4430
2782
|
/**
|
|
4431
|
-
|
|
4432
|
-
|
|
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
|
-
* @example
|
|
4477
|
-
* // Filter elements
|
|
4478
|
-
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
4479
|
-
* const result = dq.filter(x => x > 2);
|
|
4480
|
-
* console.log(result.length); // 2;
|
|
4481
|
-
*/
|
|
2783
|
+
* Filter elements into a new deque of the same class.
|
|
2784
|
+
* @remarks Time O(N), Space O(N)
|
|
2785
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
2786
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2787
|
+
* @returns A new deque with kept elements.
|
|
2788
|
+
* @example
|
|
2789
|
+
* // Filter elements
|
|
2790
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
2791
|
+
* const result = dq.filter(x => x > 2);
|
|
2792
|
+
* console.log(result.length); // 2;
|
|
2793
|
+
*/
|
|
4482
2794
|
filter(predicate, thisArg) {
|
|
4483
2795
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
4484
2796
|
out._setBucketSize(this._bucketSize);
|
|
@@ -4507,59 +2819,20 @@ var Deque = class extends LinearBase {
|
|
|
4507
2819
|
return out;
|
|
4508
2820
|
}
|
|
4509
2821
|
/**
|
|
4510
|
-
|
|
4511
|
-
|
|
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
|
-
* @example
|
|
4558
|
-
* // Transform elements
|
|
4559
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4560
|
-
* const result = dq.map(x => x * 10);
|
|
4561
|
-
* console.log(result.toArray()); // [10, 20, 30];
|
|
4562
|
-
*/
|
|
2822
|
+
* Map elements into a new deque (possibly different element type).
|
|
2823
|
+
* @remarks Time O(N), Space O(N)
|
|
2824
|
+
* @template EM
|
|
2825
|
+
* @template RM
|
|
2826
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
2827
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
2828
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2829
|
+
* @returns A new Deque with mapped elements.
|
|
2830
|
+
* @example
|
|
2831
|
+
* // Transform elements
|
|
2832
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2833
|
+
* const result = dq.map(x => x * 10);
|
|
2834
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
2835
|
+
*/
|
|
4563
2836
|
map(callback, options, thisArg) {
|
|
4564
2837
|
const out = this._createLike([], {
|
|
4565
2838
|
...options ?? {},
|
|
@@ -4574,6 +2847,27 @@ var Deque = class extends LinearBase {
|
|
|
4574
2847
|
}
|
|
4575
2848
|
return out;
|
|
4576
2849
|
}
|
|
2850
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
2851
|
+
/**
|
|
2852
|
+
* Trim unused buckets to fit exactly the active range.
|
|
2853
|
+
* @remarks Time O(N), Space O(1)
|
|
2854
|
+
* @returns void
|
|
2855
|
+
*/
|
|
2856
|
+
/**
|
|
2857
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2858
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2859
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2860
|
+
*/
|
|
2861
|
+
_autoCompact() {
|
|
2862
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2863
|
+
this._compactCounter++;
|
|
2864
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2865
|
+
this._compactCounter = 0;
|
|
2866
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2867
|
+
if (utilization < this._autoCompactRatio) {
|
|
2868
|
+
this.shrinkToFit();
|
|
2869
|
+
}
|
|
2870
|
+
}
|
|
4577
2871
|
/**
|
|
4578
2872
|
* (Protected) Set the internal bucket size.
|
|
4579
2873
|
* @remarks Time O(1), Space O(1)
|