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
|
@@ -699,10 +699,10 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
699
699
|
*/
|
|
700
700
|
constructor(elements = [], options) {
|
|
701
701
|
super(options);
|
|
702
|
-
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
703
702
|
__publicField(this, "_head");
|
|
704
703
|
__publicField(this, "_tail");
|
|
705
704
|
__publicField(this, "_length", 0);
|
|
705
|
+
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
706
706
|
this.pushMany(elements);
|
|
707
707
|
}
|
|
708
708
|
/**
|
|
@@ -764,68 +764,26 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
764
764
|
return list;
|
|
765
765
|
}
|
|
766
766
|
/**
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
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
|
-
* @example
|
|
814
|
-
* // basic SinglyLinkedList creation and push operation
|
|
815
|
-
* // Create a simple SinglyLinkedList with initial values
|
|
816
|
-
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
817
|
-
*
|
|
818
|
-
* // Verify the list maintains insertion order
|
|
819
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
820
|
-
*
|
|
821
|
-
* // Check length
|
|
822
|
-
* console.log(list.length); // 5;
|
|
823
|
-
*
|
|
824
|
-
* // Push a new element to the end
|
|
825
|
-
* list.push(6);
|
|
826
|
-
* console.log(list.length); // 6;
|
|
827
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
828
|
-
*/
|
|
767
|
+
* Append an element/node to the tail.
|
|
768
|
+
* @remarks Time O(1), Space O(1)
|
|
769
|
+
* @param elementOrNode - Element or node to append.
|
|
770
|
+
* @returns True when appended.
|
|
771
|
+
* @example
|
|
772
|
+
* // basic SinglyLinkedList creation and push operation
|
|
773
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
774
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
775
|
+
*
|
|
776
|
+
* // Verify the list maintains insertion order
|
|
777
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
778
|
+
*
|
|
779
|
+
* // Check length
|
|
780
|
+
* console.log(list.length); // 5;
|
|
781
|
+
*
|
|
782
|
+
* // Push a new element to the end
|
|
783
|
+
* list.push(6);
|
|
784
|
+
* console.log(list.length); // 6;
|
|
785
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
786
|
+
*/
|
|
829
787
|
push(elementOrNode) {
|
|
830
788
|
const newNode = this._ensureNode(elementOrNode);
|
|
831
789
|
if (!this.head) {
|
|
@@ -839,67 +797,25 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
839
797
|
return true;
|
|
840
798
|
}
|
|
841
799
|
/**
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
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
|
-
* @example
|
|
888
|
-
* // SinglyLinkedList pop and shift operations
|
|
889
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
890
|
-
*
|
|
891
|
-
* // Pop removes from the end
|
|
892
|
-
* const last = list.pop();
|
|
893
|
-
* console.log(last); // 50;
|
|
894
|
-
*
|
|
895
|
-
* // Shift removes from the beginning
|
|
896
|
-
* const first = list.shift();
|
|
897
|
-
* console.log(first); // 10;
|
|
898
|
-
*
|
|
899
|
-
* // Verify remaining elements
|
|
900
|
-
* console.log([...list]); // [20, 30, 40];
|
|
901
|
-
* console.log(list.length); // 3;
|
|
902
|
-
*/
|
|
800
|
+
* Remove and return the tail element.
|
|
801
|
+
* @remarks Time O(N), Space O(1)
|
|
802
|
+
* @returns Removed element or undefined.
|
|
803
|
+
* @example
|
|
804
|
+
* // SinglyLinkedList pop and shift operations
|
|
805
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
806
|
+
*
|
|
807
|
+
* // Pop removes from the end
|
|
808
|
+
* const last = list.pop();
|
|
809
|
+
* console.log(last); // 50;
|
|
810
|
+
*
|
|
811
|
+
* // Shift removes from the beginning
|
|
812
|
+
* const first = list.shift();
|
|
813
|
+
* console.log(first); // 10;
|
|
814
|
+
*
|
|
815
|
+
* // Verify remaining elements
|
|
816
|
+
* console.log([...list]); // [20, 30, 40];
|
|
817
|
+
* console.log(list.length); // 3;
|
|
818
|
+
*/
|
|
903
819
|
pop() {
|
|
904
820
|
var _a;
|
|
905
821
|
if (!this.head) return void 0;
|
|
@@ -919,57 +835,15 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
919
835
|
return value;
|
|
920
836
|
}
|
|
921
837
|
/**
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
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
|
-
* @example
|
|
968
|
-
* // Remove from the front
|
|
969
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
970
|
-
* console.log(list.shift()); // 10;
|
|
971
|
-
* console.log(list.length); // 2;
|
|
972
|
-
*/
|
|
838
|
+
* Remove and return the head element.
|
|
839
|
+
* @remarks Time O(1), Space O(1)
|
|
840
|
+
* @returns Removed element or undefined.
|
|
841
|
+
* @example
|
|
842
|
+
* // Remove from the front
|
|
843
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
844
|
+
* console.log(list.shift()); // 10;
|
|
845
|
+
* console.log(list.length); // 2;
|
|
846
|
+
*/
|
|
973
847
|
shift() {
|
|
974
848
|
if (!this.head) return void 0;
|
|
975
849
|
const removed = this.head;
|
|
@@ -979,73 +853,31 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
979
853
|
return removed.value;
|
|
980
854
|
}
|
|
981
855
|
/**
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
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
|
-
* @example
|
|
1029
|
-
* // SinglyLinkedList unshift and forward traversal
|
|
1030
|
-
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
1031
|
-
*
|
|
1032
|
-
* // Unshift adds to the beginning
|
|
1033
|
-
* list.unshift(10);
|
|
1034
|
-
* console.log([...list]); // [10, 20, 30, 40];
|
|
1035
|
-
*
|
|
1036
|
-
* // Access elements (forward traversal only for singly linked)
|
|
1037
|
-
* const second = list.at(1);
|
|
1038
|
-
* console.log(second); // 20;
|
|
1039
|
-
*
|
|
1040
|
-
* // SinglyLinkedList allows forward iteration only
|
|
1041
|
-
* const elements: number[] = [];
|
|
1042
|
-
* for (const item of list) {
|
|
1043
|
-
* elements.push(item);
|
|
1044
|
-
* }
|
|
1045
|
-
* console.log(elements); // [10, 20, 30, 40];
|
|
1046
|
-
*
|
|
1047
|
-
* console.log(list.length); // 4;
|
|
1048
|
-
*/
|
|
856
|
+
* Prepend an element/node to the head.
|
|
857
|
+
* @remarks Time O(1), Space O(1)
|
|
858
|
+
* @param elementOrNode - Element or node to prepend.
|
|
859
|
+
* @returns True when prepended.
|
|
860
|
+
* @example
|
|
861
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
862
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
863
|
+
*
|
|
864
|
+
* // Unshift adds to the beginning
|
|
865
|
+
* list.unshift(10);
|
|
866
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
867
|
+
*
|
|
868
|
+
* // Access elements (forward traversal only for singly linked)
|
|
869
|
+
* const second = list.at(1);
|
|
870
|
+
* console.log(second); // 20;
|
|
871
|
+
*
|
|
872
|
+
* // SinglyLinkedList allows forward iteration only
|
|
873
|
+
* const elements: number[] = [];
|
|
874
|
+
* for (const item of list) {
|
|
875
|
+
* elements.push(item);
|
|
876
|
+
* }
|
|
877
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
878
|
+
*
|
|
879
|
+
* console.log(list.length); // 4;
|
|
880
|
+
*/
|
|
1049
881
|
unshift(elementOrNode) {
|
|
1050
882
|
const newNode = this._ensureNode(elementOrNode);
|
|
1051
883
|
if (!this.head) {
|
|
@@ -1101,59 +933,17 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1101
933
|
return void 0;
|
|
1102
934
|
}
|
|
1103
935
|
/**
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
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
|
-
* @example
|
|
1151
|
-
* // Access element by index
|
|
1152
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1153
|
-
* console.log(list.at(0)); // 'a';
|
|
1154
|
-
* console.log(list.at(2)); // 'c';
|
|
1155
|
-
* console.log(list.at(3)); // 'd';
|
|
1156
|
-
*/
|
|
936
|
+
* Get the element at a given index.
|
|
937
|
+
* @remarks Time O(N), Space O(1)
|
|
938
|
+
* @param index - Zero-based index.
|
|
939
|
+
* @returns Element or undefined.
|
|
940
|
+
* @example
|
|
941
|
+
* // Access element by index
|
|
942
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
943
|
+
* console.log(list.at(0)); // 'a';
|
|
944
|
+
* console.log(list.at(2)); // 'c';
|
|
945
|
+
* console.log(list.at(3)); // 'd';
|
|
946
|
+
*/
|
|
1157
947
|
at(index) {
|
|
1158
948
|
if (index < 0 || index >= this._length) return void 0;
|
|
1159
949
|
let current = this.head;
|
|
@@ -1170,54 +960,15 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1170
960
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
1171
961
|
}
|
|
1172
962
|
/**
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
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
|
-
* @example
|
|
1217
|
-
* // Get node at index
|
|
1218
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1219
|
-
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1220
|
-
*/
|
|
963
|
+
* Get the node reference at a given index.
|
|
964
|
+
* @remarks Time O(N), Space O(1)
|
|
965
|
+
* @param index - Zero-based index.
|
|
966
|
+
* @returns Node or undefined.
|
|
967
|
+
* @example
|
|
968
|
+
* // Get node at index
|
|
969
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
970
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
971
|
+
*/
|
|
1221
972
|
getNodeAt(index) {
|
|
1222
973
|
if (index < 0 || index >= this._length) return void 0;
|
|
1223
974
|
let current = this.head;
|
|
@@ -1225,55 +976,16 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1225
976
|
return current;
|
|
1226
977
|
}
|
|
1227
978
|
/**
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
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
|
-
* @example
|
|
1272
|
-
* // Remove by index
|
|
1273
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1274
|
-
* list.deleteAt(1);
|
|
1275
|
-
* console.log(list.toArray()); // ['a', 'c'];
|
|
1276
|
-
*/
|
|
979
|
+
* Delete the element at an index.
|
|
980
|
+
* @remarks Time O(N), Space O(1)
|
|
981
|
+
* @param index - Zero-based index.
|
|
982
|
+
* @returns Removed element or undefined.
|
|
983
|
+
* @example
|
|
984
|
+
* // Remove by index
|
|
985
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
986
|
+
* list.deleteAt(1);
|
|
987
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
988
|
+
*/
|
|
1277
989
|
deleteAt(index) {
|
|
1278
990
|
if (index < 0 || index >= this._length) return void 0;
|
|
1279
991
|
if (index === 0) return this.shift();
|
|
@@ -1286,55 +998,16 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1286
998
|
return value;
|
|
1287
999
|
}
|
|
1288
1000
|
/**
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
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
|
-
* @example
|
|
1333
|
-
* // Remove first occurrence
|
|
1334
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1335
|
-
* list.delete(2);
|
|
1336
|
-
* console.log(list.toArray()); // [1, 3, 2];
|
|
1337
|
-
*/
|
|
1001
|
+
* Delete the first match by value/node.
|
|
1002
|
+
* @remarks Time O(N), Space O(1)
|
|
1003
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1004
|
+
* @returns True if removed.
|
|
1005
|
+
* @example
|
|
1006
|
+
* // Remove first occurrence
|
|
1007
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1008
|
+
* list.delete(2);
|
|
1009
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1010
|
+
*/
|
|
1338
1011
|
delete(elementOrNode) {
|
|
1339
1012
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
1340
1013
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -1351,56 +1024,17 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1351
1024
|
return true;
|
|
1352
1025
|
}
|
|
1353
1026
|
/**
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
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
|
-
* @example
|
|
1399
|
-
* // Insert at index
|
|
1400
|
-
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1401
|
-
* list.addAt(1, 2);
|
|
1402
|
-
* console.log(list.toArray()); // [1, 2, 3];
|
|
1403
|
-
*/
|
|
1027
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1028
|
+
* @remarks Time O(N), Space O(1)
|
|
1029
|
+
* @param index - Zero-based index.
|
|
1030
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1031
|
+
* @returns True if inserted.
|
|
1032
|
+
* @example
|
|
1033
|
+
* // Insert at index
|
|
1034
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1035
|
+
* list.addAt(1, 2);
|
|
1036
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1037
|
+
*/
|
|
1404
1038
|
addAt(index, newElementOrNode) {
|
|
1405
1039
|
if (index < 0 || index > this._length) return false;
|
|
1406
1040
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -1426,163 +1060,41 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1426
1060
|
return true;
|
|
1427
1061
|
}
|
|
1428
1062
|
/**
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
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
|
-
* @example
|
|
1473
|
-
* // Check empty
|
|
1474
|
-
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1475
|
-
*/
|
|
1063
|
+
* Check whether the list is empty.
|
|
1064
|
+
* @remarks Time O(1), Space O(1)
|
|
1065
|
+
* @returns True if length is 0.
|
|
1066
|
+
* @example
|
|
1067
|
+
* // Check empty
|
|
1068
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1069
|
+
*/
|
|
1476
1070
|
isEmpty() {
|
|
1477
1071
|
return this._length === 0;
|
|
1478
1072
|
}
|
|
1479
1073
|
/**
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
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
|
-
* @example
|
|
1524
|
-
* // Remove all
|
|
1525
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1526
|
-
* list.clear();
|
|
1527
|
-
* console.log(list.isEmpty()); // true;
|
|
1528
|
-
*/
|
|
1074
|
+
* Remove all nodes and reset length.
|
|
1075
|
+
* @remarks Time O(N), Space O(1)
|
|
1076
|
+
* @returns void
|
|
1077
|
+
* @example
|
|
1078
|
+
* // Remove all
|
|
1079
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1080
|
+
* list.clear();
|
|
1081
|
+
* console.log(list.isEmpty()); // true;
|
|
1082
|
+
*/
|
|
1529
1083
|
clear() {
|
|
1530
1084
|
this._head = void 0;
|
|
1531
1085
|
this._tail = void 0;
|
|
1532
1086
|
this._length = 0;
|
|
1533
1087
|
}
|
|
1534
1088
|
/**
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
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
|
-
* @example
|
|
1581
|
-
* // Reverse the list in-place
|
|
1582
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1583
|
-
* list.reverse();
|
|
1584
|
-
* console.log([...list]); // [4, 3, 2, 1];
|
|
1585
|
-
*/
|
|
1089
|
+
* Reverse the list in place.
|
|
1090
|
+
* @remarks Time O(N), Space O(1)
|
|
1091
|
+
* @returns This list.
|
|
1092
|
+
* @example
|
|
1093
|
+
* // Reverse the list in-place
|
|
1094
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1095
|
+
* list.reverse();
|
|
1096
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1097
|
+
*/
|
|
1586
1098
|
reverse() {
|
|
1587
1099
|
if (!this.head || this.head === this.tail) return this;
|
|
1588
1100
|
let prev;
|
|
@@ -1757,126 +1269,44 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1757
1269
|
return false;
|
|
1758
1270
|
}
|
|
1759
1271
|
/**
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
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
|
-
* @example
|
|
1804
|
-
* // Deep copy
|
|
1805
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1806
|
-
* const copy = list.clone();
|
|
1807
|
-
* copy.pop();
|
|
1808
|
-
* console.log(list.length); // 3;
|
|
1809
|
-
* console.log(copy.length); // 2;
|
|
1810
|
-
*/
|
|
1272
|
+
* Deep clone this list (values are copied by reference).
|
|
1273
|
+
* @remarks Time O(N), Space O(N)
|
|
1274
|
+
* @returns A new list with the same element sequence.
|
|
1275
|
+
* @example
|
|
1276
|
+
* // Deep copy
|
|
1277
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1278
|
+
* const copy = list.clone();
|
|
1279
|
+
* copy.pop();
|
|
1280
|
+
* console.log(list.length); // 3;
|
|
1281
|
+
* console.log(copy.length); // 2;
|
|
1282
|
+
*/
|
|
1811
1283
|
clone() {
|
|
1812
1284
|
const out = this._createInstance();
|
|
1813
1285
|
for (const v of this) out.push(v);
|
|
1814
1286
|
return out;
|
|
1815
1287
|
}
|
|
1816
1288
|
/**
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
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
|
-
* @example
|
|
1865
|
-
* // SinglyLinkedList filter and map operations
|
|
1866
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1867
|
-
*
|
|
1868
|
-
* // Filter even numbers
|
|
1869
|
-
* const filtered = list.filter(value => value % 2 === 0);
|
|
1870
|
-
* console.log(filtered.length); // 2;
|
|
1871
|
-
*
|
|
1872
|
-
* // Map to double values
|
|
1873
|
-
* const doubled = list.map(value => value * 2);
|
|
1874
|
-
* console.log(doubled.length); // 5;
|
|
1875
|
-
*
|
|
1876
|
-
* // Use reduce to sum
|
|
1877
|
-
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1878
|
-
* console.log(sum); // 15;
|
|
1879
|
-
*/
|
|
1289
|
+
* Filter values into a new list of the same class.
|
|
1290
|
+
* @remarks Time O(N), Space O(N)
|
|
1291
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1292
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1293
|
+
* @returns A new list with kept values.
|
|
1294
|
+
* @example
|
|
1295
|
+
* // SinglyLinkedList filter and map operations
|
|
1296
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1297
|
+
*
|
|
1298
|
+
* // Filter even numbers
|
|
1299
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1300
|
+
* console.log(filtered.length); // 2;
|
|
1301
|
+
*
|
|
1302
|
+
* // Map to double values
|
|
1303
|
+
* const doubled = list.map(value => value * 2);
|
|
1304
|
+
* console.log(doubled.length); // 5;
|
|
1305
|
+
*
|
|
1306
|
+
* // Use reduce to sum
|
|
1307
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1308
|
+
* console.log(sum); // 15;
|
|
1309
|
+
*/
|
|
1880
1310
|
filter(callback, thisArg) {
|
|
1881
1311
|
const out = this._createInstance();
|
|
1882
1312
|
let index = 0;
|
|
@@ -1900,62 +1330,20 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1900
1330
|
return out;
|
|
1901
1331
|
}
|
|
1902
1332
|
/**
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
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
|
-
* @example
|
|
1954
|
-
* // Transform elements
|
|
1955
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1956
|
-
* const doubled = list.map(n => n * 2);
|
|
1957
|
-
* console.log([...doubled]); // [2, 4, 6];
|
|
1958
|
-
*/
|
|
1333
|
+
* Map values into a new list (possibly different element type).
|
|
1334
|
+
* @remarks Time O(N), Space O(N)
|
|
1335
|
+
* @template EM
|
|
1336
|
+
* @template RM
|
|
1337
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1338
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1339
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1340
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1341
|
+
* @example
|
|
1342
|
+
* // Transform elements
|
|
1343
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1344
|
+
* const doubled = list.map(n => n * 2);
|
|
1345
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1346
|
+
*/
|
|
1959
1347
|
map(callback, options, thisArg) {
|
|
1960
1348
|
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1961
1349
|
let index = 0;
|
|
@@ -2146,124 +1534,32 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2146
1534
|
this._autoCompactRatio = value;
|
|
2147
1535
|
}
|
|
2148
1536
|
/**
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
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
|
-
* @example
|
|
2195
|
-
* // Track queue length
|
|
2196
|
-
* const q = new Queue<number>();
|
|
2197
|
-
* console.log(q.length); // 0;
|
|
2198
|
-
* q.push(1);
|
|
2199
|
-
* q.push(2);
|
|
2200
|
-
* console.log(q.length); // 2;
|
|
2201
|
-
*/
|
|
1537
|
+
* Get the number of elements currently in the queue.
|
|
1538
|
+
* @remarks Time O(1), Space O(1)
|
|
1539
|
+
* @returns Current length.
|
|
1540
|
+
* @example
|
|
1541
|
+
* // Track queue length
|
|
1542
|
+
* const q = new Queue<number>();
|
|
1543
|
+
* console.log(q.length); // 0;
|
|
1544
|
+
* q.push(1);
|
|
1545
|
+
* q.push(2);
|
|
1546
|
+
* console.log(q.length); // 2;
|
|
1547
|
+
*/
|
|
2202
1548
|
get length() {
|
|
2203
1549
|
return this.elements.length - this._offset;
|
|
2204
1550
|
}
|
|
2205
1551
|
/**
|
|
2206
|
-
|
|
2207
|
-
* @remarks Time O(1), Space O(1)
|
|
2208
|
-
* @returns Front element or undefined.
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
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
|
-
* @example
|
|
2252
|
-
* // View the front element
|
|
2253
|
-
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2254
|
-
* console.log(q.first); // 'first';
|
|
2255
|
-
* console.log(q.length); // 3;
|
|
2256
|
-
*/
|
|
2257
|
-
get first() {
|
|
2258
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2259
|
-
}
|
|
2260
|
-
/**
|
|
2261
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
1552
|
+
* Get the first element (front) without removing it.
|
|
2262
1553
|
* @remarks Time O(1), Space O(1)
|
|
2263
1554
|
* @returns Front element or undefined.
|
|
1555
|
+
* @example
|
|
1556
|
+
* // View the front element
|
|
1557
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1558
|
+
* console.log(q.first); // 'first';
|
|
1559
|
+
* console.log(q.length); // 3;
|
|
2264
1560
|
*/
|
|
2265
|
-
|
|
2266
|
-
return this.
|
|
1561
|
+
get first() {
|
|
1562
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2267
1563
|
}
|
|
2268
1564
|
/**
|
|
2269
1565
|
* Get the last element (back) without removing it.
|
|
@@ -2284,131 +1580,55 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2284
1580
|
return new _Queue(elements);
|
|
2285
1581
|
}
|
|
2286
1582
|
/**
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
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
|
-
* @example
|
|
2333
|
-
* // Queue for...of iteration and isEmpty check
|
|
2334
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2335
|
-
*
|
|
2336
|
-
* const elements: string[] = [];
|
|
2337
|
-
* for (const item of queue) {
|
|
2338
|
-
* elements.push(item);
|
|
2339
|
-
* }
|
|
2340
|
-
*
|
|
2341
|
-
* // Verify all elements are iterated in order
|
|
2342
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2343
|
-
*
|
|
2344
|
-
* // Process all elements
|
|
2345
|
-
* while (queue.length > 0) {
|
|
2346
|
-
* queue.shift();
|
|
2347
|
-
* }
|
|
2348
|
-
*
|
|
2349
|
-
* console.log(queue.length); // 0;
|
|
2350
|
-
*/
|
|
1583
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
1584
|
+
* @remarks Time O(1), Space O(1)
|
|
1585
|
+
* @returns Front element or undefined.
|
|
1586
|
+
*/
|
|
1587
|
+
peek() {
|
|
1588
|
+
return this.first;
|
|
1589
|
+
}
|
|
1590
|
+
/**
|
|
1591
|
+
* Check whether the queue is empty.
|
|
1592
|
+
* @remarks Time O(1), Space O(1)
|
|
1593
|
+
* @returns True if length is 0.
|
|
1594
|
+
* @example
|
|
1595
|
+
* // Queue for...of iteration and isEmpty check
|
|
1596
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1597
|
+
*
|
|
1598
|
+
* const elements: string[] = [];
|
|
1599
|
+
* for (const item of queue) {
|
|
1600
|
+
* elements.push(item);
|
|
1601
|
+
* }
|
|
1602
|
+
*
|
|
1603
|
+
* // Verify all elements are iterated in order
|
|
1604
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1605
|
+
*
|
|
1606
|
+
* // Process all elements
|
|
1607
|
+
* while (queue.length > 0) {
|
|
1608
|
+
* queue.shift();
|
|
1609
|
+
* }
|
|
1610
|
+
*
|
|
1611
|
+
* console.log(queue.length); // 0;
|
|
1612
|
+
*/
|
|
2351
1613
|
isEmpty() {
|
|
2352
1614
|
return this.length === 0;
|
|
2353
1615
|
}
|
|
2354
1616
|
/**
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
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
|
-
* @example
|
|
2402
|
-
* // basic Queue creation and push operation
|
|
2403
|
-
* // Create a simple Queue with initial values
|
|
2404
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2405
|
-
*
|
|
2406
|
-
* // Verify the queue maintains insertion order
|
|
2407
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2408
|
-
*
|
|
2409
|
-
* // Check length
|
|
2410
|
-
* console.log(queue.length); // 5;
|
|
2411
|
-
*/
|
|
1617
|
+
* Enqueue one element at the back.
|
|
1618
|
+
* @remarks Time O(1), Space O(1)
|
|
1619
|
+
* @param element - Element to enqueue.
|
|
1620
|
+
* @returns True on success.
|
|
1621
|
+
* @example
|
|
1622
|
+
* // basic Queue creation and push operation
|
|
1623
|
+
* // Create a simple Queue with initial values
|
|
1624
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1625
|
+
*
|
|
1626
|
+
* // Verify the queue maintains insertion order
|
|
1627
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1628
|
+
*
|
|
1629
|
+
* // Check length
|
|
1630
|
+
* console.log(queue.length); // 5;
|
|
1631
|
+
*/
|
|
2412
1632
|
push(element) {
|
|
2413
1633
|
this.elements.push(element);
|
|
2414
1634
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -2429,66 +1649,24 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2429
1649
|
return ans;
|
|
2430
1650
|
}
|
|
2431
1651
|
/**
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
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
|
-
* @example
|
|
2478
|
-
* // Queue shift and peek operations
|
|
2479
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2480
|
-
*
|
|
2481
|
-
* // Peek at the front element without removing it
|
|
2482
|
-
* console.log(queue.first); // 10;
|
|
2483
|
-
*
|
|
2484
|
-
* // Remove and get the first element (FIFO)
|
|
2485
|
-
* const first = queue.shift();
|
|
2486
|
-
* console.log(first); // 10;
|
|
2487
|
-
*
|
|
2488
|
-
* // Verify remaining elements and length decreased
|
|
2489
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
2490
|
-
* console.log(queue.length); // 3;
|
|
2491
|
-
*/
|
|
1652
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1653
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1654
|
+
* @returns Removed element or undefined.
|
|
1655
|
+
* @example
|
|
1656
|
+
* // Queue shift and peek operations
|
|
1657
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1658
|
+
*
|
|
1659
|
+
* // Peek at the front element without removing it
|
|
1660
|
+
* console.log(queue.first); // 10;
|
|
1661
|
+
*
|
|
1662
|
+
* // Remove and get the first element (FIFO)
|
|
1663
|
+
* const first = queue.shift();
|
|
1664
|
+
* console.log(first); // 10;
|
|
1665
|
+
*
|
|
1666
|
+
* // Verify remaining elements and length decreased
|
|
1667
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1668
|
+
* console.log(queue.length); // 3;
|
|
1669
|
+
*/
|
|
2492
1670
|
shift() {
|
|
2493
1671
|
if (this.length === 0) return void 0;
|
|
2494
1672
|
const first = this.first;
|
|
@@ -2497,55 +1675,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2497
1675
|
return first;
|
|
2498
1676
|
}
|
|
2499
1677
|
/**
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
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
|
-
* @example
|
|
2544
|
-
* // Remove specific element
|
|
2545
|
-
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2546
|
-
* q.delete(2);
|
|
2547
|
-
* console.log(q.length); // 3;
|
|
2548
|
-
*/
|
|
1678
|
+
* Delete the first occurrence of a specific element.
|
|
1679
|
+
* @remarks Time O(N), Space O(1)
|
|
1680
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1681
|
+
* @returns True if an element was removed.
|
|
1682
|
+
* @example
|
|
1683
|
+
* // Remove specific element
|
|
1684
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1685
|
+
* q.delete(2);
|
|
1686
|
+
* console.log(q.length); // 3;
|
|
1687
|
+
*/
|
|
2549
1688
|
delete(element) {
|
|
2550
1689
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
2551
1690
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -2556,55 +1695,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2556
1695
|
return false;
|
|
2557
1696
|
}
|
|
2558
1697
|
/**
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
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
|
-
* @example
|
|
2603
|
-
* // Access element by index
|
|
2604
|
-
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2605
|
-
* console.log(q.at(0)); // 'a';
|
|
2606
|
-
* console.log(q.at(2)); // 'c';
|
|
2607
|
-
*/
|
|
1698
|
+
* Get the element at a given logical index.
|
|
1699
|
+
* @remarks Time O(1), Space O(1)
|
|
1700
|
+
* @param index - Zero-based index from the front.
|
|
1701
|
+
* @returns Element or undefined.
|
|
1702
|
+
* @example
|
|
1703
|
+
* // Access element by index
|
|
1704
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1705
|
+
* console.log(q.at(0)); // 'a';
|
|
1706
|
+
* console.log(q.at(2)); // 'c';
|
|
1707
|
+
*/
|
|
2608
1708
|
at(index) {
|
|
2609
1709
|
if (index < 0 || index >= this.length) return void 0;
|
|
2610
1710
|
return this._elements[this._offset + index];
|
|
@@ -2671,110 +1771,31 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2671
1771
|
return this;
|
|
2672
1772
|
}
|
|
2673
1773
|
/**
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
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
|
-
* @example
|
|
2718
|
-
* // Remove all elements
|
|
2719
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2720
|
-
* q.clear();
|
|
2721
|
-
* console.log(q.length); // 0;
|
|
2722
|
-
*/
|
|
1774
|
+
* Remove all elements and reset offset.
|
|
1775
|
+
* @remarks Time O(1), Space O(1)
|
|
1776
|
+
* @returns void
|
|
1777
|
+
* @example
|
|
1778
|
+
* // Remove all elements
|
|
1779
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1780
|
+
* q.clear();
|
|
1781
|
+
* console.log(q.length); // 0;
|
|
1782
|
+
*/
|
|
2723
1783
|
clear() {
|
|
2724
1784
|
this._elements = [];
|
|
2725
1785
|
this._offset = 0;
|
|
2726
1786
|
}
|
|
2727
1787
|
/**
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
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
|
-
* @example
|
|
2771
|
-
* // Reclaim unused memory
|
|
2772
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2773
|
-
* q.shift();
|
|
2774
|
-
* q.shift();
|
|
2775
|
-
* q.compact();
|
|
2776
|
-
* console.log(q.length); // 3;
|
|
2777
|
-
*/
|
|
1788
|
+
* Compact storage by discarding consumed head elements.
|
|
1789
|
+
* @remarks Time O(N), Space O(N)
|
|
1790
|
+
* @returns True when compaction performed.
|
|
1791
|
+
* @example
|
|
1792
|
+
* // Reclaim unused memory
|
|
1793
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1794
|
+
* q.shift();
|
|
1795
|
+
* q.shift();
|
|
1796
|
+
* q.compact();
|
|
1797
|
+
* console.log(q.length); // 3;
|
|
1798
|
+
*/
|
|
2778
1799
|
compact() {
|
|
2779
1800
|
this._elements = this.elements.slice(this._offset);
|
|
2780
1801
|
this._offset = 0;
|
|
@@ -2800,57 +1821,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2800
1821
|
return removed;
|
|
2801
1822
|
}
|
|
2802
1823
|
/**
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
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
|
-
* @example
|
|
2847
|
-
* // Create independent copy
|
|
2848
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2849
|
-
* const copy = q.clone();
|
|
2850
|
-
* copy.shift();
|
|
2851
|
-
* console.log(q.length); // 3;
|
|
2852
|
-
* console.log(copy.length); // 2;
|
|
2853
|
-
*/
|
|
1824
|
+
* Deep clone this queue and its parameters.
|
|
1825
|
+
* @remarks Time O(N), Space O(N)
|
|
1826
|
+
* @returns A new queue with the same content and options.
|
|
1827
|
+
* @example
|
|
1828
|
+
* // Create independent copy
|
|
1829
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1830
|
+
* const copy = q.clone();
|
|
1831
|
+
* copy.shift();
|
|
1832
|
+
* console.log(q.length); // 3;
|
|
1833
|
+
* console.log(copy.length); // 2;
|
|
1834
|
+
*/
|
|
2854
1835
|
clone() {
|
|
2855
1836
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2856
1837
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2858,57 +1839,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2858
1839
|
return out;
|
|
2859
1840
|
}
|
|
2860
1841
|
/**
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
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
|
-
* @example
|
|
2907
|
-
* // Filter elements
|
|
2908
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2909
|
-
* const evens = q.filter(x => x % 2 === 0);
|
|
2910
|
-
* console.log(evens.length); // 2;
|
|
2911
|
-
*/
|
|
1842
|
+
* Filter elements into a new queue of the same class.
|
|
1843
|
+
* @remarks Time O(N), Space O(N)
|
|
1844
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1845
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1846
|
+
* @returns A new queue with kept elements.
|
|
1847
|
+
* @example
|
|
1848
|
+
* // Filter elements
|
|
1849
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1850
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1851
|
+
* console.log(evens.length); // 2;
|
|
1852
|
+
*/
|
|
2912
1853
|
filter(predicate, thisArg) {
|
|
2913
1854
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2914
1855
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2920,59 +1861,20 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2920
1861
|
return out;
|
|
2921
1862
|
}
|
|
2922
1863
|
/**
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
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
|
-
* @example
|
|
2971
|
-
* // Transform elements
|
|
2972
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2973
|
-
* const doubled = q.map(x => x * 2);
|
|
2974
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2975
|
-
*/
|
|
1864
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1865
|
+
* @remarks Time O(N), Space O(N)
|
|
1866
|
+
* @template EM
|
|
1867
|
+
* @template RM
|
|
1868
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1869
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1870
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1871
|
+
* @returns A new Queue with mapped elements.
|
|
1872
|
+
* @example
|
|
1873
|
+
* // Transform elements
|
|
1874
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1875
|
+
* const doubled = q.map(x => x * 2);
|
|
1876
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1877
|
+
*/
|
|
2976
1878
|
map(callback, options, thisArg) {
|
|
2977
1879
|
var _a, _b;
|
|
2978
1880
|
const out = new this.constructor([], {
|
|
@@ -3089,14 +1991,13 @@ var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => M
|
|
|
3089
1991
|
var _Deque = class _Deque extends LinearBase {
|
|
3090
1992
|
constructor(elements = [], options) {
|
|
3091
1993
|
super(options);
|
|
3092
|
-
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
3093
|
-
__publicField(this, "_bucketSize", 1 << 12);
|
|
3094
|
-
__publicField(this, "_autoCompactRatio", 0.5);
|
|
3095
1994
|
/**
|
|
3096
1995
|
* Counter for shift/pop operations since last compaction check.
|
|
3097
1996
|
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
3098
1997
|
*/
|
|
3099
1998
|
__publicField(this, "_compactCounter", 0);
|
|
1999
|
+
__publicField(this, "_bucketSize", 1 << 12);
|
|
2000
|
+
__publicField(this, "_autoCompactRatio", 0.5);
|
|
3100
2001
|
__publicField(this, "_bucketFirst", 0);
|
|
3101
2002
|
__publicField(this, "_firstInBucket", 0);
|
|
3102
2003
|
__publicField(this, "_bucketLast", 0);
|
|
@@ -3104,6 +2005,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3104
2005
|
__publicField(this, "_bucketCount", 0);
|
|
3105
2006
|
__publicField(this, "_buckets", []);
|
|
3106
2007
|
__publicField(this, "_length", 0);
|
|
2008
|
+
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
3107
2009
|
if (options) {
|
|
3108
2010
|
const { bucketSize, autoCompactRatio } = options;
|
|
3109
2011
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
@@ -3207,146 +2109,36 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3207
2109
|
return this._length;
|
|
3208
2110
|
}
|
|
3209
2111
|
/**
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
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
|
-
* @example
|
|
3252
|
-
* // Deque peek at both ends
|
|
3253
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3254
|
-
*
|
|
3255
|
-
* // Get first element without removing
|
|
3256
|
-
* const first = deque.at(0);
|
|
3257
|
-
* console.log(first); // 10;
|
|
3258
|
-
*
|
|
3259
|
-
* // Get last element without removing
|
|
3260
|
-
* const last = deque.at(deque.length - 1);
|
|
3261
|
-
* console.log(last); // 50;
|
|
3262
|
-
*
|
|
3263
|
-
* // Length unchanged
|
|
3264
|
-
* console.log(deque.length); // 5;
|
|
3265
|
-
*/
|
|
3266
|
-
/**
|
|
3267
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
3268
|
-
* @remarks Time O(1), Space O(1)
|
|
3269
|
-
* @returns Front element or undefined.
|
|
2112
|
+
* Deque peek at both ends
|
|
2113
|
+
* @example
|
|
2114
|
+
* // Deque peek at both ends
|
|
2115
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2116
|
+
*
|
|
2117
|
+
* // Get first element without removing
|
|
2118
|
+
* const first = deque.at(0);
|
|
2119
|
+
* console.log(first); // 10;
|
|
2120
|
+
*
|
|
2121
|
+
* // Get last element without removing
|
|
2122
|
+
* const last = deque.at(deque.length - 1);
|
|
2123
|
+
* console.log(last); // 50;
|
|
2124
|
+
*
|
|
2125
|
+
* // Length unchanged
|
|
2126
|
+
* console.log(deque.length); // 5;
|
|
3270
2127
|
*/
|
|
3271
|
-
peek() {
|
|
3272
|
-
return this.first;
|
|
3273
|
-
}
|
|
3274
|
-
/**
|
|
3275
|
-
* Deque peek at both ends
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
* @example
|
|
3280
|
-
* // Deque peek at both ends
|
|
3281
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3282
|
-
*
|
|
3283
|
-
* // Get first element without removing
|
|
3284
|
-
* const first = deque.at(0);
|
|
3285
|
-
* console.log(first); // 10;
|
|
3286
|
-
*
|
|
3287
|
-
* // Get last element without removing
|
|
3288
|
-
* const last = deque.at(deque.length - 1);
|
|
3289
|
-
* console.log(last); // 50;
|
|
3290
|
-
*
|
|
3291
|
-
* // Length unchanged
|
|
3292
|
-
* console.log(deque.length); // 5;
|
|
3293
|
-
*/
|
|
3294
2128
|
get first() {
|
|
3295
2129
|
if (this._length === 0) return;
|
|
3296
2130
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3297
2131
|
}
|
|
3298
2132
|
/**
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
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
|
-
* @example
|
|
3345
|
-
* // Peek at the back element
|
|
3346
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3347
|
-
* console.log(dq.last); // 'c';
|
|
3348
|
-
* console.log(dq.first); // 'a';
|
|
3349
|
-
*/
|
|
2133
|
+
* Get the last element without removing it.
|
|
2134
|
+
* @remarks Time O(1), Space O(1)
|
|
2135
|
+
* @returns Last element or undefined.
|
|
2136
|
+
* @example
|
|
2137
|
+
* // Peek at the back element
|
|
2138
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2139
|
+
* console.log(dq.last); // 'c';
|
|
2140
|
+
* console.log(dq.first); // 'a';
|
|
2141
|
+
*/
|
|
3350
2142
|
get last() {
|
|
3351
2143
|
if (this._length === 0) return;
|
|
3352
2144
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3365,71 +2157,56 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3365
2157
|
return new this(data, options);
|
|
3366
2158
|
}
|
|
3367
2159
|
/**
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
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
|
-
* // Verify the deque maintains insertion order
|
|
3420
|
-
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3421
|
-
*
|
|
3422
|
-
* // Check length
|
|
3423
|
-
* console.log(deque.length); // 5;
|
|
3424
|
-
*
|
|
3425
|
-
* // Push to the end
|
|
3426
|
-
* deque.push(6);
|
|
3427
|
-
* console.log(deque.length); // 6;
|
|
3428
|
-
*
|
|
3429
|
-
* // Pop from the end
|
|
3430
|
-
* const last = deque.pop();
|
|
3431
|
-
* console.log(last); // 6;
|
|
3432
|
-
*/
|
|
2160
|
+
* Get the first element without removing it.
|
|
2161
|
+
* @remarks Time O(1), Space O(1)
|
|
2162
|
+
* @returns First element or undefined.
|
|
2163
|
+
* @example
|
|
2164
|
+
* // Deque peek at both ends
|
|
2165
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2166
|
+
*
|
|
2167
|
+
* // Get first element without removing
|
|
2168
|
+
* const first = deque.at(0);
|
|
2169
|
+
* console.log(first); // 10;
|
|
2170
|
+
*
|
|
2171
|
+
* // Get last element without removing
|
|
2172
|
+
* const last = deque.at(deque.length - 1);
|
|
2173
|
+
* console.log(last); // 50;
|
|
2174
|
+
*
|
|
2175
|
+
* // Length unchanged
|
|
2176
|
+
* console.log(deque.length); // 5;
|
|
2177
|
+
*/
|
|
2178
|
+
/**
|
|
2179
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
2180
|
+
* @remarks Time O(1), Space O(1)
|
|
2181
|
+
* @returns Front element or undefined.
|
|
2182
|
+
*/
|
|
2183
|
+
peek() {
|
|
2184
|
+
return this.first;
|
|
2185
|
+
}
|
|
2186
|
+
/**
|
|
2187
|
+
* Append one element at the back.
|
|
2188
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2189
|
+
* @param element - Element to append.
|
|
2190
|
+
* @returns True when appended.
|
|
2191
|
+
* @example
|
|
2192
|
+
* // basic Deque creation and push/pop operations
|
|
2193
|
+
* // Create a simple Deque with initial values
|
|
2194
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
2195
|
+
*
|
|
2196
|
+
* // Verify the deque maintains insertion order
|
|
2197
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
2198
|
+
*
|
|
2199
|
+
* // Check length
|
|
2200
|
+
* console.log(deque.length); // 5;
|
|
2201
|
+
*
|
|
2202
|
+
* // Push to the end
|
|
2203
|
+
* deque.push(6);
|
|
2204
|
+
* console.log(deque.length); // 6;
|
|
2205
|
+
*
|
|
2206
|
+
* // Pop from the end
|
|
2207
|
+
* const last = deque.pop();
|
|
2208
|
+
* console.log(last); // 6;
|
|
2209
|
+
*/
|
|
3433
2210
|
push(element) {
|
|
3434
2211
|
if (this._length) {
|
|
3435
2212
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -3449,57 +2226,15 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3449
2226
|
return true;
|
|
3450
2227
|
}
|
|
3451
2228
|
/**
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3455
|
-
|
|
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
|
-
* @example
|
|
3498
|
-
* // Remove from the back
|
|
3499
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3500
|
-
* console.log(dq.pop()); // 3;
|
|
3501
|
-
* console.log(dq.length); // 2;
|
|
3502
|
-
*/
|
|
2229
|
+
* Remove and return the last element.
|
|
2230
|
+
* @remarks Time O(1), Space O(1)
|
|
2231
|
+
* @returns Removed element or undefined.
|
|
2232
|
+
* @example
|
|
2233
|
+
* // Remove from the back
|
|
2234
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2235
|
+
* console.log(dq.pop()); // 3;
|
|
2236
|
+
* console.log(dq.length); // 2;
|
|
2237
|
+
*/
|
|
3503
2238
|
pop() {
|
|
3504
2239
|
if (this._length === 0) return;
|
|
3505
2240
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3519,57 +2254,15 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3519
2254
|
return element;
|
|
3520
2255
|
}
|
|
3521
2256
|
/**
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
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
|
-
* @example
|
|
3568
|
-
* // Remove from the front
|
|
3569
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3570
|
-
* console.log(dq.shift()); // 1;
|
|
3571
|
-
* console.log(dq.length); // 2;
|
|
3572
|
-
*/
|
|
2257
|
+
* Remove and return the first element.
|
|
2258
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2259
|
+
* @returns Removed element or undefined.
|
|
2260
|
+
* @example
|
|
2261
|
+
* // Remove from the front
|
|
2262
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2263
|
+
* console.log(dq.shift()); // 1;
|
|
2264
|
+
* console.log(dq.length); // 2;
|
|
2265
|
+
*/
|
|
3573
2266
|
shift() {
|
|
3574
2267
|
if (this._length === 0) return;
|
|
3575
2268
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -3589,68 +2282,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3589
2282
|
return element;
|
|
3590
2283
|
}
|
|
3591
2284
|
/**
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
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
|
-
* @example
|
|
3639
|
-
* // Deque shift and unshift operations
|
|
3640
|
-
* const deque = new Deque<number>([20, 30, 40]);
|
|
3641
|
-
*
|
|
3642
|
-
* // Unshift adds to the front
|
|
3643
|
-
* deque.unshift(10);
|
|
3644
|
-
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3645
|
-
*
|
|
3646
|
-
* // Shift removes from the front (O(1) complexity!)
|
|
3647
|
-
* const first = deque.shift();
|
|
3648
|
-
* console.log(first); // 10;
|
|
3649
|
-
*
|
|
3650
|
-
* // Verify remaining elements
|
|
3651
|
-
* console.log([...deque]); // [20, 30, 40];
|
|
3652
|
-
* console.log(deque.length); // 3;
|
|
3653
|
-
*/
|
|
2285
|
+
* Prepend one element at the front.
|
|
2286
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2287
|
+
* @param element - Element to prepend.
|
|
2288
|
+
* @returns True when prepended.
|
|
2289
|
+
* @example
|
|
2290
|
+
* // Deque shift and unshift operations
|
|
2291
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
2292
|
+
*
|
|
2293
|
+
* // Unshift adds to the front
|
|
2294
|
+
* deque.unshift(10);
|
|
2295
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
2296
|
+
*
|
|
2297
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
2298
|
+
* const first = deque.shift();
|
|
2299
|
+
* console.log(first); // 10;
|
|
2300
|
+
*
|
|
2301
|
+
* // Verify remaining elements
|
|
2302
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
2303
|
+
* console.log(deque.length); // 3;
|
|
2304
|
+
*/
|
|
3654
2305
|
unshift(element) {
|
|
3655
2306
|
if (this._length) {
|
|
3656
2307
|
if (this._firstInBucket > 0) {
|
|
@@ -3704,107 +2355,27 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3704
2355
|
return ans;
|
|
3705
2356
|
}
|
|
3706
2357
|
/**
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
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
|
-
* @example
|
|
3751
|
-
* // Check if empty
|
|
3752
|
-
* const dq = new Deque();
|
|
3753
|
-
* console.log(dq.isEmpty()); // true;
|
|
3754
|
-
*/
|
|
2358
|
+
* Check whether the deque is empty.
|
|
2359
|
+
* @remarks Time O(1), Space O(1)
|
|
2360
|
+
* @returns True if length is 0.
|
|
2361
|
+
* @example
|
|
2362
|
+
* // Check if empty
|
|
2363
|
+
* const dq = new Deque();
|
|
2364
|
+
* console.log(dq.isEmpty()); // true;
|
|
2365
|
+
*/
|
|
3755
2366
|
isEmpty() {
|
|
3756
2367
|
return this._length === 0;
|
|
3757
2368
|
}
|
|
3758
2369
|
/**
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
|
|
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
|
-
* @example
|
|
3803
|
-
* // Remove all elements
|
|
3804
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3805
|
-
* dq.clear();
|
|
3806
|
-
* console.log(dq.length); // 0;
|
|
3807
|
-
*/
|
|
2370
|
+
* Remove all elements and reset structure.
|
|
2371
|
+
* @remarks Time O(1), Space O(1)
|
|
2372
|
+
* @returns void
|
|
2373
|
+
* @example
|
|
2374
|
+
* // Remove all elements
|
|
2375
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2376
|
+
* dq.clear();
|
|
2377
|
+
* console.log(dq.length); // 0;
|
|
2378
|
+
*/
|
|
3808
2379
|
clear() {
|
|
3809
2380
|
this._buckets = [new Array(this._bucketSize)];
|
|
3810
2381
|
this._bucketCount = 1;
|
|
@@ -3812,55 +2383,16 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3812
2383
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
3813
2384
|
}
|
|
3814
2385
|
/**
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
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
|
-
* @example
|
|
3859
|
-
* // Access by index
|
|
3860
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3861
|
-
* console.log(dq.at(0)); // 'a';
|
|
3862
|
-
* console.log(dq.at(2)); // 'c';
|
|
3863
|
-
*/
|
|
2386
|
+
* Get the element at a given position.
|
|
2387
|
+
* @remarks Time O(1), Space O(1)
|
|
2388
|
+
* @param pos - Zero-based position from the front.
|
|
2389
|
+
* @returns Element or undefined.
|
|
2390
|
+
* @example
|
|
2391
|
+
* // Access by index
|
|
2392
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2393
|
+
* console.log(dq.at(0)); // 'a';
|
|
2394
|
+
* console.log(dq.at(2)); // 'c';
|
|
2395
|
+
*/
|
|
3864
2396
|
at(pos) {
|
|
3865
2397
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
3866
2398
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -4019,55 +2551,16 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4019
2551
|
}
|
|
4020
2552
|
}
|
|
4021
2553
|
/**
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
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
|
-
* @example
|
|
4066
|
-
* // Remove element
|
|
4067
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4068
|
-
* dq.delete(2);
|
|
4069
|
-
* console.log(dq.length); // 2;
|
|
4070
|
-
*/
|
|
2554
|
+
* Delete the first occurrence of a value.
|
|
2555
|
+
* @remarks Time O(N), Space O(1)
|
|
2556
|
+
* @param element - Element to remove (using the configured equality).
|
|
2557
|
+
* @returns True if an element was removed.
|
|
2558
|
+
* @example
|
|
2559
|
+
* // Remove element
|
|
2560
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2561
|
+
* dq.delete(2);
|
|
2562
|
+
* console.log(dq.length); // 2;
|
|
2563
|
+
*/
|
|
4071
2564
|
delete(element) {
|
|
4072
2565
|
const size = this._length;
|
|
4073
2566
|
if (size === 0) return false;
|
|
@@ -4111,78 +2604,39 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4111
2604
|
return this;
|
|
4112
2605
|
}
|
|
4113
2606
|
/**
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
|
|
4117
|
-
|
|
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
|
-
* @example
|
|
4157
|
-
* // Deque for...of iteration and reverse
|
|
4158
|
-
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
4159
|
-
*
|
|
4160
|
-
* // Iterate forward
|
|
4161
|
-
* const forward: string[] = [];
|
|
4162
|
-
* for (const item of deque) {
|
|
4163
|
-
* forward.push(item);
|
|
4164
|
-
* }
|
|
4165
|
-
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
4166
|
-
*
|
|
4167
|
-
* // Reverse the deque
|
|
4168
|
-
* deque.reverse();
|
|
4169
|
-
* const backward: string[] = [];
|
|
4170
|
-
* for (const item of deque) {
|
|
4171
|
-
* backward.push(item);
|
|
4172
|
-
* }
|
|
4173
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4174
|
-
*/
|
|
4175
|
-
/**
|
|
4176
|
-
* Find the last value matching a predicate (scans back-to-front).
|
|
4177
|
-
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
4178
|
-
* @param predicate - Function called with (value, index, deque).
|
|
4179
|
-
* @returns Matching value or undefined.
|
|
4180
|
-
* @example
|
|
4181
|
-
* // Find last matching value
|
|
4182
|
-
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
4183
|
-
* console.log(d.findLast(v => v > 2)); // 5;
|
|
4184
|
-
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
4185
|
-
*/
|
|
2607
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
2608
|
+
* @remarks Time O(N), Space O(N)
|
|
2609
|
+
* @returns This deque.
|
|
2610
|
+
* @example
|
|
2611
|
+
* // Deque for...of iteration and reverse
|
|
2612
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2613
|
+
*
|
|
2614
|
+
* // Iterate forward
|
|
2615
|
+
* const forward: string[] = [];
|
|
2616
|
+
* for (const item of deque) {
|
|
2617
|
+
* forward.push(item);
|
|
2618
|
+
* }
|
|
2619
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2620
|
+
*
|
|
2621
|
+
* // Reverse the deque
|
|
2622
|
+
* deque.reverse();
|
|
2623
|
+
* const backward: string[] = [];
|
|
2624
|
+
* for (const item of deque) {
|
|
2625
|
+
* backward.push(item);
|
|
2626
|
+
* }
|
|
2627
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2628
|
+
*/
|
|
2629
|
+
/**
|
|
2630
|
+
* Find the last value matching a predicate (scans back-to-front).
|
|
2631
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2632
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2633
|
+
* @returns Matching value or undefined.
|
|
2634
|
+
* @example
|
|
2635
|
+
* // Find last matching value
|
|
2636
|
+
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
2637
|
+
* console.log(d.findLast(v => v > 2)); // 5;
|
|
2638
|
+
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
2639
|
+
*/
|
|
4186
2640
|
findLast(predicate) {
|
|
4187
2641
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4188
2642
|
const val = this.at(i);
|
|
@@ -4191,16 +2645,16 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4191
2645
|
return void 0;
|
|
4192
2646
|
}
|
|
4193
2647
|
/**
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
2648
|
+
* Find the index of the last value matching a predicate (scans back-to-front).
|
|
2649
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2650
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2651
|
+
* @returns Matching index, or -1 if not found.
|
|
2652
|
+
* @example
|
|
2653
|
+
* // Find last matching index
|
|
2654
|
+
* const d = new Deque([10, 20, 30, 20, 10]);
|
|
2655
|
+
* console.log(d.findLastIndex(v => v === 20)); // 3;
|
|
2656
|
+
* console.log(d.findLastIndex(v => v === 10)); // 4;
|
|
2657
|
+
*/
|
|
4204
2658
|
findLastIndex(predicate) {
|
|
4205
2659
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4206
2660
|
if (predicate(this.at(i), i, this)) return i;
|
|
@@ -4208,28 +2662,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4208
2662
|
return -1;
|
|
4209
2663
|
}
|
|
4210
2664
|
/**
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4232
|
-
*/
|
|
2665
|
+
* Deque for...of iteration and reverse
|
|
2666
|
+
* @example
|
|
2667
|
+
* // Deque for...of iteration and reverse
|
|
2668
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2669
|
+
*
|
|
2670
|
+
* // Iterate forward
|
|
2671
|
+
* const forward: string[] = [];
|
|
2672
|
+
* for (const item of deque) {
|
|
2673
|
+
* forward.push(item);
|
|
2674
|
+
* }
|
|
2675
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2676
|
+
*
|
|
2677
|
+
* // Reverse the deque
|
|
2678
|
+
* deque.reverse();
|
|
2679
|
+
* const backward: string[] = [];
|
|
2680
|
+
* for (const item of deque) {
|
|
2681
|
+
* backward.push(item);
|
|
2682
|
+
* }
|
|
2683
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2684
|
+
*/
|
|
4233
2685
|
reverse() {
|
|
4234
2686
|
this._buckets.reverse().forEach(function(bucket) {
|
|
4235
2687
|
bucket.reverse();
|
|
@@ -4263,81 +2715,22 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4263
2715
|
return this;
|
|
4264
2716
|
}
|
|
4265
2717
|
/**
|
|
4266
|
-
*
|
|
2718
|
+
* Compact the deque by removing unused buckets.
|
|
4267
2719
|
* @remarks Time O(N), Space O(1)
|
|
4268
|
-
* @returns
|
|
4269
|
-
*/
|
|
4270
|
-
/**
|
|
4271
|
-
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
4272
|
-
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
4273
|
-
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2720
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
4274
2721
|
*/
|
|
4275
|
-
_autoCompact() {
|
|
4276
|
-
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
4277
|
-
this._compactCounter++;
|
|
4278
|
-
if (this._compactCounter < this._bucketSize) return;
|
|
4279
|
-
this._compactCounter = 0;
|
|
4280
|
-
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
4281
|
-
if (utilization < this._autoCompactRatio) {
|
|
4282
|
-
this.shrinkToFit();
|
|
4283
|
-
}
|
|
4284
|
-
}
|
|
4285
2722
|
/**
|
|
4286
2723
|
* Compact the deque by removing unused buckets.
|
|
4287
2724
|
* @remarks Time O(N), Space O(1)
|
|
4288
2725
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2726
|
+
* @example
|
|
2727
|
+
* // Reclaim memory
|
|
2728
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
2729
|
+
* dq.shift();
|
|
2730
|
+
* dq.shift();
|
|
2731
|
+
* dq.compact();
|
|
2732
|
+
* console.log(dq.length); // 3;
|
|
4289
2733
|
*/
|
|
4290
|
-
/**
|
|
4291
|
-
* Compact the deque by removing unused buckets.
|
|
4292
|
-
* @remarks Time O(N), Space O(1)
|
|
4293
|
-
* @returns True if compaction was performed (bucket count reduced).
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
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
|
-
* @example
|
|
4334
|
-
* // Reclaim memory
|
|
4335
|
-
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
4336
|
-
* dq.shift();
|
|
4337
|
-
* dq.shift();
|
|
4338
|
-
* dq.compact();
|
|
4339
|
-
* console.log(dq.length); // 3;
|
|
4340
|
-
*/
|
|
4341
2734
|
compact() {
|
|
4342
2735
|
const before = this._bucketCount;
|
|
4343
2736
|
this.shrinkToFit();
|
|
@@ -4365,57 +2758,17 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4365
2758
|
this._compactCounter = 0;
|
|
4366
2759
|
}
|
|
4367
2760
|
/**
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
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
|
-
* @example
|
|
4412
|
-
* // Create independent copy
|
|
4413
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4414
|
-
* const copy = dq.clone();
|
|
4415
|
-
* copy.pop();
|
|
4416
|
-
* console.log(dq.length); // 3;
|
|
4417
|
-
* console.log(copy.length); // 2;
|
|
4418
|
-
*/
|
|
2761
|
+
* Deep clone this deque, preserving options.
|
|
2762
|
+
* @remarks Time O(N), Space O(N)
|
|
2763
|
+
* @returns A new deque with the same content and options.
|
|
2764
|
+
* @example
|
|
2765
|
+
* // Create independent copy
|
|
2766
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2767
|
+
* const copy = dq.clone();
|
|
2768
|
+
* copy.pop();
|
|
2769
|
+
* console.log(dq.length); // 3;
|
|
2770
|
+
* console.log(copy.length); // 2;
|
|
2771
|
+
*/
|
|
4419
2772
|
clone() {
|
|
4420
2773
|
return this._createLike(this, {
|
|
4421
2774
|
bucketSize: this.bucketSize,
|
|
@@ -4424,57 +2777,17 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4424
2777
|
});
|
|
4425
2778
|
}
|
|
4426
2779
|
/**
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
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
|
-
* @example
|
|
4473
|
-
* // Filter elements
|
|
4474
|
-
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
4475
|
-
* const result = dq.filter(x => x > 2);
|
|
4476
|
-
* console.log(result.length); // 2;
|
|
4477
|
-
*/
|
|
2780
|
+
* Filter elements into a new deque of the same class.
|
|
2781
|
+
* @remarks Time O(N), Space O(N)
|
|
2782
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
2783
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2784
|
+
* @returns A new deque with kept elements.
|
|
2785
|
+
* @example
|
|
2786
|
+
* // Filter elements
|
|
2787
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
2788
|
+
* const result = dq.filter(x => x > 2);
|
|
2789
|
+
* console.log(result.length); // 2;
|
|
2790
|
+
*/
|
|
4478
2791
|
filter(predicate, thisArg) {
|
|
4479
2792
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
4480
2793
|
out._setBucketSize(this._bucketSize);
|
|
@@ -4503,59 +2816,20 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4503
2816
|
return out;
|
|
4504
2817
|
}
|
|
4505
2818
|
/**
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
4509
|
-
|
|
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
|
-
* @example
|
|
4554
|
-
* // Transform elements
|
|
4555
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4556
|
-
* const result = dq.map(x => x * 10);
|
|
4557
|
-
* console.log(result.toArray()); // [10, 20, 30];
|
|
4558
|
-
*/
|
|
2819
|
+
* Map elements into a new deque (possibly different element type).
|
|
2820
|
+
* @remarks Time O(N), Space O(N)
|
|
2821
|
+
* @template EM
|
|
2822
|
+
* @template RM
|
|
2823
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
2824
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
2825
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2826
|
+
* @returns A new Deque with mapped elements.
|
|
2827
|
+
* @example
|
|
2828
|
+
* // Transform elements
|
|
2829
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2830
|
+
* const result = dq.map(x => x * 10);
|
|
2831
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
2832
|
+
*/
|
|
4559
2833
|
map(callback, options, thisArg) {
|
|
4560
2834
|
const out = this._createLike([], {
|
|
4561
2835
|
...options != null ? options : {},
|
|
@@ -4570,6 +2844,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4570
2844
|
}
|
|
4571
2845
|
return out;
|
|
4572
2846
|
}
|
|
2847
|
+
/**
|
|
2848
|
+
* Trim unused buckets to fit exactly the active range.
|
|
2849
|
+
* @remarks Time O(N), Space O(1)
|
|
2850
|
+
* @returns void
|
|
2851
|
+
*/
|
|
2852
|
+
/**
|
|
2853
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2854
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2855
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2856
|
+
*/
|
|
2857
|
+
_autoCompact() {
|
|
2858
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2859
|
+
this._compactCounter++;
|
|
2860
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2861
|
+
this._compactCounter = 0;
|
|
2862
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2863
|
+
if (utilization < this._autoCompactRatio) {
|
|
2864
|
+
this.shrinkToFit();
|
|
2865
|
+
}
|
|
2866
|
+
}
|
|
4573
2867
|
/**
|
|
4574
2868
|
* (Protected) Set the internal bucket size.
|
|
4575
2869
|
* @remarks Time O(1), Space O(1)
|