data-structure-typed 2.6.0 → 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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- 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 +253 -101
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +244 -114
- 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 +119 -64
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +84 -14
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +138 -34
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +149 -41
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +172 -92
- 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 +117 -62
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +120 -70
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -62
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +125 -75
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +119 -64
- package/docs-site-docusaurus/docs/api/classes/Queue.md +158 -74
- 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 +172 -94
- 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 +112 -50
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +90 -121
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +298 -107
- package/docs-site-docusaurus/docs/api/classes/Trie.md +116 -58
- 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 +45 -46
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +0 -3
- package/src/data-structures/base/linear-base.ts +2 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -529
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
- package/src/data-structures/binary-tree/bst.ts +158 -1082
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
- package/src/data-structures/binary-tree/segment-tree.ts +73 -351
- package/src/data-structures/binary-tree/tree-map.ts +462 -5124
- package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
- package/src/data-structures/binary-tree/tree-multi-set.ts +299 -3983
- package/src/data-structures/binary-tree/tree-set.ts +338 -4836
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -562
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -511
- package/src/data-structures/hash/hash-map.ts +154 -582
- package/src/data-structures/heap/heap.ts +200 -795
- package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
- package/src/data-structures/matrix/matrix.ts +179 -518
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +214 -882
- package/src/data-structures/queue/queue.ts +102 -625
- package/src/data-structures/stack/stack.ts +76 -505
- package/src/data-structures/trie/trie.ts +98 -628
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -701,10 +701,10 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
701
701
|
*/
|
|
702
702
|
constructor(elements = [], options) {
|
|
703
703
|
super(options);
|
|
704
|
-
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
705
704
|
__publicField(this, "_head");
|
|
706
705
|
__publicField(this, "_tail");
|
|
707
706
|
__publicField(this, "_length", 0);
|
|
707
|
+
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
708
708
|
this.pushMany(elements);
|
|
709
709
|
}
|
|
710
710
|
/**
|
|
@@ -766,68 +766,26 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
766
766
|
return list;
|
|
767
767
|
}
|
|
768
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
|
-
|
|
814
|
-
|
|
815
|
-
* @example
|
|
816
|
-
* // basic SinglyLinkedList creation and push operation
|
|
817
|
-
* // Create a simple SinglyLinkedList with initial values
|
|
818
|
-
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
819
|
-
*
|
|
820
|
-
* // Verify the list maintains insertion order
|
|
821
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
822
|
-
*
|
|
823
|
-
* // Check length
|
|
824
|
-
* console.log(list.length); // 5;
|
|
825
|
-
*
|
|
826
|
-
* // Push a new element to the end
|
|
827
|
-
* list.push(6);
|
|
828
|
-
* console.log(list.length); // 6;
|
|
829
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
830
|
-
*/
|
|
769
|
+
* Append an element/node to the tail.
|
|
770
|
+
* @remarks Time O(1), Space O(1)
|
|
771
|
+
* @param elementOrNode - Element or node to append.
|
|
772
|
+
* @returns True when appended.
|
|
773
|
+
* @example
|
|
774
|
+
* // basic SinglyLinkedList creation and push operation
|
|
775
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
776
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
777
|
+
*
|
|
778
|
+
* // Verify the list maintains insertion order
|
|
779
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
780
|
+
*
|
|
781
|
+
* // Check length
|
|
782
|
+
* console.log(list.length); // 5;
|
|
783
|
+
*
|
|
784
|
+
* // Push a new element to the end
|
|
785
|
+
* list.push(6);
|
|
786
|
+
* console.log(list.length); // 6;
|
|
787
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
788
|
+
*/
|
|
831
789
|
push(elementOrNode) {
|
|
832
790
|
const newNode = this._ensureNode(elementOrNode);
|
|
833
791
|
if (!this.head) {
|
|
@@ -841,67 +799,25 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
841
799
|
return true;
|
|
842
800
|
}
|
|
843
801
|
/**
|
|
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
|
-
|
|
888
|
-
|
|
889
|
-
* @example
|
|
890
|
-
* // SinglyLinkedList pop and shift operations
|
|
891
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
892
|
-
*
|
|
893
|
-
* // Pop removes from the end
|
|
894
|
-
* const last = list.pop();
|
|
895
|
-
* console.log(last); // 50;
|
|
896
|
-
*
|
|
897
|
-
* // Shift removes from the beginning
|
|
898
|
-
* const first = list.shift();
|
|
899
|
-
* console.log(first); // 10;
|
|
900
|
-
*
|
|
901
|
-
* // Verify remaining elements
|
|
902
|
-
* console.log([...list]); // [20, 30, 40];
|
|
903
|
-
* console.log(list.length); // 3;
|
|
904
|
-
*/
|
|
802
|
+
* Remove and return the tail element.
|
|
803
|
+
* @remarks Time O(N), Space O(1)
|
|
804
|
+
* @returns Removed element or undefined.
|
|
805
|
+
* @example
|
|
806
|
+
* // SinglyLinkedList pop and shift operations
|
|
807
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
808
|
+
*
|
|
809
|
+
* // Pop removes from the end
|
|
810
|
+
* const last = list.pop();
|
|
811
|
+
* console.log(last); // 50;
|
|
812
|
+
*
|
|
813
|
+
* // Shift removes from the beginning
|
|
814
|
+
* const first = list.shift();
|
|
815
|
+
* console.log(first); // 10;
|
|
816
|
+
*
|
|
817
|
+
* // Verify remaining elements
|
|
818
|
+
* console.log([...list]); // [20, 30, 40];
|
|
819
|
+
* console.log(list.length); // 3;
|
|
820
|
+
*/
|
|
905
821
|
pop() {
|
|
906
822
|
var _a;
|
|
907
823
|
if (!this.head) return void 0;
|
|
@@ -921,57 +837,15 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
921
837
|
return value;
|
|
922
838
|
}
|
|
923
839
|
/**
|
|
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
|
-
|
|
968
|
-
|
|
969
|
-
* @example
|
|
970
|
-
* // Remove from the front
|
|
971
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
972
|
-
* console.log(list.shift()); // 10;
|
|
973
|
-
* console.log(list.length); // 2;
|
|
974
|
-
*/
|
|
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
|
+
*/
|
|
975
849
|
shift() {
|
|
976
850
|
if (!this.head) return void 0;
|
|
977
851
|
const removed = this.head;
|
|
@@ -981,73 +855,31 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
981
855
|
return removed.value;
|
|
982
856
|
}
|
|
983
857
|
/**
|
|
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
|
-
|
|
1029
|
-
|
|
1030
|
-
* @example
|
|
1031
|
-
* // SinglyLinkedList unshift and forward traversal
|
|
1032
|
-
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
1033
|
-
*
|
|
1034
|
-
* // Unshift adds to the beginning
|
|
1035
|
-
* list.unshift(10);
|
|
1036
|
-
* console.log([...list]); // [10, 20, 30, 40];
|
|
1037
|
-
*
|
|
1038
|
-
* // Access elements (forward traversal only for singly linked)
|
|
1039
|
-
* const second = list.at(1);
|
|
1040
|
-
* console.log(second); // 20;
|
|
1041
|
-
*
|
|
1042
|
-
* // SinglyLinkedList allows forward iteration only
|
|
1043
|
-
* const elements: number[] = [];
|
|
1044
|
-
* for (const item of list) {
|
|
1045
|
-
* elements.push(item);
|
|
1046
|
-
* }
|
|
1047
|
-
* console.log(elements); // [10, 20, 30, 40];
|
|
1048
|
-
*
|
|
1049
|
-
* console.log(list.length); // 4;
|
|
1050
|
-
*/
|
|
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
|
+
*/
|
|
1051
883
|
unshift(elementOrNode) {
|
|
1052
884
|
const newNode = this._ensureNode(elementOrNode);
|
|
1053
885
|
if (!this.head) {
|
|
@@ -1103,59 +935,17 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1103
935
|
return void 0;
|
|
1104
936
|
}
|
|
1105
937
|
/**
|
|
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
|
-
|
|
1151
|
-
|
|
1152
|
-
* @example
|
|
1153
|
-
* // Access element by index
|
|
1154
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1155
|
-
* console.log(list.at(0)); // 'a';
|
|
1156
|
-
* console.log(list.at(2)); // 'c';
|
|
1157
|
-
* console.log(list.at(3)); // 'd';
|
|
1158
|
-
*/
|
|
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
|
+
*/
|
|
1159
949
|
at(index) {
|
|
1160
950
|
if (index < 0 || index >= this._length) return void 0;
|
|
1161
951
|
let current = this.head;
|
|
@@ -1172,54 +962,15 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1172
962
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
1173
963
|
}
|
|
1174
964
|
/**
|
|
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
|
-
|
|
1217
|
-
|
|
1218
|
-
* @example
|
|
1219
|
-
* // Get node at index
|
|
1220
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1221
|
-
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1222
|
-
*/
|
|
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
|
+
*/
|
|
1223
974
|
getNodeAt(index) {
|
|
1224
975
|
if (index < 0 || index >= this._length) return void 0;
|
|
1225
976
|
let current = this.head;
|
|
@@ -1227,55 +978,16 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1227
978
|
return current;
|
|
1228
979
|
}
|
|
1229
980
|
/**
|
|
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
|
-
|
|
1272
|
-
|
|
1273
|
-
* @example
|
|
1274
|
-
* // Remove by index
|
|
1275
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1276
|
-
* list.deleteAt(1);
|
|
1277
|
-
* console.log(list.toArray()); // ['a', 'c'];
|
|
1278
|
-
*/
|
|
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
|
+
*/
|
|
1279
991
|
deleteAt(index) {
|
|
1280
992
|
if (index < 0 || index >= this._length) return void 0;
|
|
1281
993
|
if (index === 0) return this.shift();
|
|
@@ -1288,55 +1000,16 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1288
1000
|
return value;
|
|
1289
1001
|
}
|
|
1290
1002
|
/**
|
|
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
|
-
|
|
1333
|
-
|
|
1334
|
-
* @example
|
|
1335
|
-
* // Remove first occurrence
|
|
1336
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1337
|
-
* list.delete(2);
|
|
1338
|
-
* console.log(list.toArray()); // [1, 3, 2];
|
|
1339
|
-
*/
|
|
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
|
+
*/
|
|
1340
1013
|
delete(elementOrNode) {
|
|
1341
1014
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
1342
1015
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -1353,56 +1026,17 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1353
1026
|
return true;
|
|
1354
1027
|
}
|
|
1355
1028
|
/**
|
|
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
|
-
|
|
1399
|
-
|
|
1400
|
-
* @example
|
|
1401
|
-
* // Insert at index
|
|
1402
|
-
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1403
|
-
* list.addAt(1, 2);
|
|
1404
|
-
* console.log(list.toArray()); // [1, 2, 3];
|
|
1405
|
-
*/
|
|
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
|
+
*/
|
|
1406
1040
|
addAt(index, newElementOrNode) {
|
|
1407
1041
|
if (index < 0 || index > this._length) return false;
|
|
1408
1042
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -1428,163 +1062,41 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1428
1062
|
return true;
|
|
1429
1063
|
}
|
|
1430
1064
|
/**
|
|
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
|
-
|
|
1473
|
-
|
|
1474
|
-
* @example
|
|
1475
|
-
* // Check empty
|
|
1476
|
-
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1477
|
-
*/
|
|
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
|
+
*/
|
|
1478
1072
|
isEmpty() {
|
|
1479
1073
|
return this._length === 0;
|
|
1480
1074
|
}
|
|
1481
1075
|
/**
|
|
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
|
-
|
|
1524
|
-
|
|
1525
|
-
* @example
|
|
1526
|
-
* // Remove all
|
|
1527
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1528
|
-
* list.clear();
|
|
1529
|
-
* console.log(list.isEmpty()); // true;
|
|
1530
|
-
*/
|
|
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
|
+
*/
|
|
1531
1085
|
clear() {
|
|
1532
1086
|
this._head = void 0;
|
|
1533
1087
|
this._tail = void 0;
|
|
1534
1088
|
this._length = 0;
|
|
1535
1089
|
}
|
|
1536
1090
|
/**
|
|
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
|
-
|
|
1581
|
-
|
|
1582
|
-
* @example
|
|
1583
|
-
* // Reverse the list in-place
|
|
1584
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1585
|
-
* list.reverse();
|
|
1586
|
-
* console.log([...list]); // [4, 3, 2, 1];
|
|
1587
|
-
*/
|
|
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
|
+
*/
|
|
1588
1100
|
reverse() {
|
|
1589
1101
|
if (!this.head || this.head === this.tail) return this;
|
|
1590
1102
|
let prev;
|
|
@@ -1759,126 +1271,44 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1759
1271
|
return false;
|
|
1760
1272
|
}
|
|
1761
1273
|
/**
|
|
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
|
-
|
|
1804
|
-
|
|
1805
|
-
* @example
|
|
1806
|
-
* // Deep copy
|
|
1807
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1808
|
-
* const copy = list.clone();
|
|
1809
|
-
* copy.pop();
|
|
1810
|
-
* console.log(list.length); // 3;
|
|
1811
|
-
* console.log(copy.length); // 2;
|
|
1812
|
-
*/
|
|
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
|
+
*/
|
|
1813
1285
|
clone() {
|
|
1814
1286
|
const out = this._createInstance();
|
|
1815
1287
|
for (const v of this) out.push(v);
|
|
1816
1288
|
return out;
|
|
1817
1289
|
}
|
|
1818
1290
|
/**
|
|
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
|
-
|
|
1865
|
-
|
|
1866
|
-
* @example
|
|
1867
|
-
* // SinglyLinkedList filter and map operations
|
|
1868
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1869
|
-
*
|
|
1870
|
-
* // Filter even numbers
|
|
1871
|
-
* const filtered = list.filter(value => value % 2 === 0);
|
|
1872
|
-
* console.log(filtered.length); // 2;
|
|
1873
|
-
*
|
|
1874
|
-
* // Map to double values
|
|
1875
|
-
* const doubled = list.map(value => value * 2);
|
|
1876
|
-
* console.log(doubled.length); // 5;
|
|
1877
|
-
*
|
|
1878
|
-
* // Use reduce to sum
|
|
1879
|
-
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1880
|
-
* console.log(sum); // 15;
|
|
1881
|
-
*/
|
|
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
|
+
*/
|
|
1882
1312
|
filter(callback, thisArg) {
|
|
1883
1313
|
const out = this._createInstance();
|
|
1884
1314
|
let index = 0;
|
|
@@ -1902,62 +1332,20 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1902
1332
|
return out;
|
|
1903
1333
|
}
|
|
1904
1334
|
/**
|
|
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
|
-
|
|
1954
|
-
|
|
1955
|
-
* @example
|
|
1956
|
-
* // Transform elements
|
|
1957
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1958
|
-
* const doubled = list.map(n => n * 2);
|
|
1959
|
-
* console.log([...doubled]); // [2, 4, 6];
|
|
1960
|
-
*/
|
|
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
|
+
*/
|
|
1961
1349
|
map(callback, options, thisArg) {
|
|
1962
1350
|
const out = this._createLike([], { ...options != null ? options : {}, maxLen: this._maxLen });
|
|
1963
1351
|
let index = 0;
|
|
@@ -2148,124 +1536,32 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2148
1536
|
this._autoCompactRatio = value;
|
|
2149
1537
|
}
|
|
2150
1538
|
/**
|
|
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
|
-
|
|
2195
|
-
|
|
2196
|
-
* @example
|
|
2197
|
-
* // Track queue length
|
|
2198
|
-
* const q = new Queue<number>();
|
|
2199
|
-
* console.log(q.length); // 0;
|
|
2200
|
-
* q.push(1);
|
|
2201
|
-
* q.push(2);
|
|
2202
|
-
* console.log(q.length); // 2;
|
|
2203
|
-
*/
|
|
1539
|
+
* Get the number of elements currently in the queue.
|
|
1540
|
+
* @remarks Time O(1), Space O(1)
|
|
1541
|
+
* @returns Current length.
|
|
1542
|
+
* @example
|
|
1543
|
+
* // Track queue length
|
|
1544
|
+
* const q = new Queue<number>();
|
|
1545
|
+
* console.log(q.length); // 0;
|
|
1546
|
+
* q.push(1);
|
|
1547
|
+
* q.push(2);
|
|
1548
|
+
* console.log(q.length); // 2;
|
|
1549
|
+
*/
|
|
2204
1550
|
get length() {
|
|
2205
1551
|
return this.elements.length - this._offset;
|
|
2206
1552
|
}
|
|
2207
1553
|
/**
|
|
2208
|
-
|
|
2209
|
-
* @remarks Time O(1), Space O(1)
|
|
2210
|
-
* @returns Front element or undefined.
|
|
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
|
-
|
|
2252
|
-
|
|
2253
|
-
* @example
|
|
2254
|
-
* // View the front element
|
|
2255
|
-
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2256
|
-
* console.log(q.first); // 'first';
|
|
2257
|
-
* console.log(q.length); // 3;
|
|
2258
|
-
*/
|
|
2259
|
-
get first() {
|
|
2260
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2261
|
-
}
|
|
2262
|
-
/**
|
|
2263
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
1554
|
+
* Get the first element (front) without removing it.
|
|
2264
1555
|
* @remarks Time O(1), Space O(1)
|
|
2265
1556
|
* @returns Front element or undefined.
|
|
1557
|
+
* @example
|
|
1558
|
+
* // View the front element
|
|
1559
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1560
|
+
* console.log(q.first); // 'first';
|
|
1561
|
+
* console.log(q.length); // 3;
|
|
2266
1562
|
*/
|
|
2267
|
-
|
|
2268
|
-
return this.
|
|
1563
|
+
get first() {
|
|
1564
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2269
1565
|
}
|
|
2270
1566
|
/**
|
|
2271
1567
|
* Get the last element (back) without removing it.
|
|
@@ -2286,131 +1582,55 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2286
1582
|
return new _Queue(elements);
|
|
2287
1583
|
}
|
|
2288
1584
|
/**
|
|
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
|
-
|
|
2333
|
-
|
|
2334
|
-
* @example
|
|
2335
|
-
* // Queue for...of iteration and isEmpty check
|
|
2336
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2337
|
-
*
|
|
2338
|
-
* const elements: string[] = [];
|
|
2339
|
-
* for (const item of queue) {
|
|
2340
|
-
* elements.push(item);
|
|
2341
|
-
* }
|
|
2342
|
-
*
|
|
2343
|
-
* // Verify all elements are iterated in order
|
|
2344
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2345
|
-
*
|
|
2346
|
-
* // Process all elements
|
|
2347
|
-
* while (queue.length > 0) {
|
|
2348
|
-
* queue.shift();
|
|
2349
|
-
* }
|
|
2350
|
-
*
|
|
2351
|
-
* console.log(queue.length); // 0;
|
|
2352
|
-
*/
|
|
1585
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
1586
|
+
* @remarks Time O(1), Space O(1)
|
|
1587
|
+
* @returns Front element or undefined.
|
|
1588
|
+
*/
|
|
1589
|
+
peek() {
|
|
1590
|
+
return this.first;
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* Check whether the queue is empty.
|
|
1594
|
+
* @remarks Time O(1), Space O(1)
|
|
1595
|
+
* @returns True if length is 0.
|
|
1596
|
+
* @example
|
|
1597
|
+
* // Queue for...of iteration and isEmpty check
|
|
1598
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1599
|
+
*
|
|
1600
|
+
* const elements: string[] = [];
|
|
1601
|
+
* for (const item of queue) {
|
|
1602
|
+
* elements.push(item);
|
|
1603
|
+
* }
|
|
1604
|
+
*
|
|
1605
|
+
* // Verify all elements are iterated in order
|
|
1606
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1607
|
+
*
|
|
1608
|
+
* // Process all elements
|
|
1609
|
+
* while (queue.length > 0) {
|
|
1610
|
+
* queue.shift();
|
|
1611
|
+
* }
|
|
1612
|
+
*
|
|
1613
|
+
* console.log(queue.length); // 0;
|
|
1614
|
+
*/
|
|
2353
1615
|
isEmpty() {
|
|
2354
1616
|
return this.length === 0;
|
|
2355
1617
|
}
|
|
2356
1618
|
/**
|
|
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
|
-
|
|
2402
|
-
|
|
2403
|
-
* @example
|
|
2404
|
-
* // basic Queue creation and push operation
|
|
2405
|
-
* // Create a simple Queue with initial values
|
|
2406
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2407
|
-
*
|
|
2408
|
-
* // Verify the queue maintains insertion order
|
|
2409
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2410
|
-
*
|
|
2411
|
-
* // Check length
|
|
2412
|
-
* console.log(queue.length); // 5;
|
|
2413
|
-
*/
|
|
1619
|
+
* Enqueue one element at the back.
|
|
1620
|
+
* @remarks Time O(1), Space O(1)
|
|
1621
|
+
* @param element - Element to enqueue.
|
|
1622
|
+
* @returns True on success.
|
|
1623
|
+
* @example
|
|
1624
|
+
* // basic Queue creation and push operation
|
|
1625
|
+
* // Create a simple Queue with initial values
|
|
1626
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1627
|
+
*
|
|
1628
|
+
* // Verify the queue maintains insertion order
|
|
1629
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1630
|
+
*
|
|
1631
|
+
* // Check length
|
|
1632
|
+
* console.log(queue.length); // 5;
|
|
1633
|
+
*/
|
|
2414
1634
|
push(element) {
|
|
2415
1635
|
this.elements.push(element);
|
|
2416
1636
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -2431,66 +1651,24 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2431
1651
|
return ans;
|
|
2432
1652
|
}
|
|
2433
1653
|
/**
|
|
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
|
-
|
|
2478
|
-
|
|
2479
|
-
* @example
|
|
2480
|
-
* // Queue shift and peek operations
|
|
2481
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2482
|
-
*
|
|
2483
|
-
* // Peek at the front element without removing it
|
|
2484
|
-
* console.log(queue.first); // 10;
|
|
2485
|
-
*
|
|
2486
|
-
* // Remove and get the first element (FIFO)
|
|
2487
|
-
* const first = queue.shift();
|
|
2488
|
-
* console.log(first); // 10;
|
|
2489
|
-
*
|
|
2490
|
-
* // Verify remaining elements and length decreased
|
|
2491
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
2492
|
-
* console.log(queue.length); // 3;
|
|
2493
|
-
*/
|
|
1654
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1655
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1656
|
+
* @returns Removed element or undefined.
|
|
1657
|
+
* @example
|
|
1658
|
+
* // Queue shift and peek operations
|
|
1659
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1660
|
+
*
|
|
1661
|
+
* // Peek at the front element without removing it
|
|
1662
|
+
* console.log(queue.first); // 10;
|
|
1663
|
+
*
|
|
1664
|
+
* // Remove and get the first element (FIFO)
|
|
1665
|
+
* const first = queue.shift();
|
|
1666
|
+
* console.log(first); // 10;
|
|
1667
|
+
*
|
|
1668
|
+
* // Verify remaining elements and length decreased
|
|
1669
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1670
|
+
* console.log(queue.length); // 3;
|
|
1671
|
+
*/
|
|
2494
1672
|
shift() {
|
|
2495
1673
|
if (this.length === 0) return void 0;
|
|
2496
1674
|
const first = this.first;
|
|
@@ -2499,55 +1677,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2499
1677
|
return first;
|
|
2500
1678
|
}
|
|
2501
1679
|
/**
|
|
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
|
-
|
|
2544
|
-
|
|
2545
|
-
* @example
|
|
2546
|
-
* // Remove specific element
|
|
2547
|
-
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2548
|
-
* q.delete(2);
|
|
2549
|
-
* console.log(q.length); // 3;
|
|
2550
|
-
*/
|
|
1680
|
+
* Delete the first occurrence of a specific element.
|
|
1681
|
+
* @remarks Time O(N), Space O(1)
|
|
1682
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1683
|
+
* @returns True if an element was removed.
|
|
1684
|
+
* @example
|
|
1685
|
+
* // Remove specific element
|
|
1686
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1687
|
+
* q.delete(2);
|
|
1688
|
+
* console.log(q.length); // 3;
|
|
1689
|
+
*/
|
|
2551
1690
|
delete(element) {
|
|
2552
1691
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
2553
1692
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -2558,55 +1697,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2558
1697
|
return false;
|
|
2559
1698
|
}
|
|
2560
1699
|
/**
|
|
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
|
-
|
|
2603
|
-
|
|
2604
|
-
* @example
|
|
2605
|
-
* // Access element by index
|
|
2606
|
-
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2607
|
-
* console.log(q.at(0)); // 'a';
|
|
2608
|
-
* console.log(q.at(2)); // 'c';
|
|
2609
|
-
*/
|
|
1700
|
+
* Get the element at a given logical index.
|
|
1701
|
+
* @remarks Time O(1), Space O(1)
|
|
1702
|
+
* @param index - Zero-based index from the front.
|
|
1703
|
+
* @returns Element or undefined.
|
|
1704
|
+
* @example
|
|
1705
|
+
* // Access element by index
|
|
1706
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1707
|
+
* console.log(q.at(0)); // 'a';
|
|
1708
|
+
* console.log(q.at(2)); // 'c';
|
|
1709
|
+
*/
|
|
2610
1710
|
at(index) {
|
|
2611
1711
|
if (index < 0 || index >= this.length) return void 0;
|
|
2612
1712
|
return this._elements[this._offset + index];
|
|
@@ -2673,110 +1773,31 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2673
1773
|
return this;
|
|
2674
1774
|
}
|
|
2675
1775
|
/**
|
|
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
|
-
|
|
2718
|
-
|
|
2719
|
-
* @example
|
|
2720
|
-
* // Remove all elements
|
|
2721
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2722
|
-
* q.clear();
|
|
2723
|
-
* console.log(q.length); // 0;
|
|
2724
|
-
*/
|
|
1776
|
+
* Remove all elements and reset offset.
|
|
1777
|
+
* @remarks Time O(1), Space O(1)
|
|
1778
|
+
* @returns void
|
|
1779
|
+
* @example
|
|
1780
|
+
* // Remove all elements
|
|
1781
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1782
|
+
* q.clear();
|
|
1783
|
+
* console.log(q.length); // 0;
|
|
1784
|
+
*/
|
|
2725
1785
|
clear() {
|
|
2726
1786
|
this._elements = [];
|
|
2727
1787
|
this._offset = 0;
|
|
2728
1788
|
}
|
|
2729
1789
|
/**
|
|
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
|
-
|
|
2771
|
-
|
|
2772
|
-
* @example
|
|
2773
|
-
* // Reclaim unused memory
|
|
2774
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2775
|
-
* q.shift();
|
|
2776
|
-
* q.shift();
|
|
2777
|
-
* q.compact();
|
|
2778
|
-
* console.log(q.length); // 3;
|
|
2779
|
-
*/
|
|
1790
|
+
* Compact storage by discarding consumed head elements.
|
|
1791
|
+
* @remarks Time O(N), Space O(N)
|
|
1792
|
+
* @returns True when compaction performed.
|
|
1793
|
+
* @example
|
|
1794
|
+
* // Reclaim unused memory
|
|
1795
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1796
|
+
* q.shift();
|
|
1797
|
+
* q.shift();
|
|
1798
|
+
* q.compact();
|
|
1799
|
+
* console.log(q.length); // 3;
|
|
1800
|
+
*/
|
|
2780
1801
|
compact() {
|
|
2781
1802
|
this._elements = this.elements.slice(this._offset);
|
|
2782
1803
|
this._offset = 0;
|
|
@@ -2802,57 +1823,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2802
1823
|
return removed;
|
|
2803
1824
|
}
|
|
2804
1825
|
/**
|
|
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
|
-
|
|
2847
|
-
|
|
2848
|
-
* @example
|
|
2849
|
-
* // Create independent copy
|
|
2850
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2851
|
-
* const copy = q.clone();
|
|
2852
|
-
* copy.shift();
|
|
2853
|
-
* console.log(q.length); // 3;
|
|
2854
|
-
* console.log(copy.length); // 2;
|
|
2855
|
-
*/
|
|
1826
|
+
* Deep clone this queue and its parameters.
|
|
1827
|
+
* @remarks Time O(N), Space O(N)
|
|
1828
|
+
* @returns A new queue with the same content and options.
|
|
1829
|
+
* @example
|
|
1830
|
+
* // Create independent copy
|
|
1831
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1832
|
+
* const copy = q.clone();
|
|
1833
|
+
* copy.shift();
|
|
1834
|
+
* console.log(q.length); // 3;
|
|
1835
|
+
* console.log(copy.length); // 2;
|
|
1836
|
+
*/
|
|
2856
1837
|
clone() {
|
|
2857
1838
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2858
1839
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2860,57 +1841,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2860
1841
|
return out;
|
|
2861
1842
|
}
|
|
2862
1843
|
/**
|
|
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
|
-
|
|
2907
|
-
|
|
2908
|
-
* @example
|
|
2909
|
-
* // Filter elements
|
|
2910
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2911
|
-
* const evens = q.filter(x => x % 2 === 0);
|
|
2912
|
-
* console.log(evens.length); // 2;
|
|
2913
|
-
*/
|
|
1844
|
+
* Filter elements into a new queue of the same class.
|
|
1845
|
+
* @remarks Time O(N), Space O(N)
|
|
1846
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1847
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1848
|
+
* @returns A new queue with kept elements.
|
|
1849
|
+
* @example
|
|
1850
|
+
* // Filter elements
|
|
1851
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1852
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1853
|
+
* console.log(evens.length); // 2;
|
|
1854
|
+
*/
|
|
2914
1855
|
filter(predicate, thisArg) {
|
|
2915
1856
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2916
1857
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2922,59 +1863,20 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2922
1863
|
return out;
|
|
2923
1864
|
}
|
|
2924
1865
|
/**
|
|
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
|
-
|
|
2971
|
-
|
|
2972
|
-
* @example
|
|
2973
|
-
* // Transform elements
|
|
2974
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2975
|
-
* const doubled = q.map(x => x * 2);
|
|
2976
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2977
|
-
*/
|
|
1866
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1867
|
+
* @remarks Time O(N), Space O(N)
|
|
1868
|
+
* @template EM
|
|
1869
|
+
* @template RM
|
|
1870
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1871
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1872
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1873
|
+
* @returns A new Queue with mapped elements.
|
|
1874
|
+
* @example
|
|
1875
|
+
* // Transform elements
|
|
1876
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1877
|
+
* const doubled = q.map(x => x * 2);
|
|
1878
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1879
|
+
*/
|
|
2978
1880
|
map(callback, options, thisArg) {
|
|
2979
1881
|
var _a, _b;
|
|
2980
1882
|
const out = new this.constructor([], {
|
|
@@ -3091,14 +1993,13 @@ var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => M
|
|
|
3091
1993
|
var _Deque = class _Deque extends LinearBase {
|
|
3092
1994
|
constructor(elements = [], options) {
|
|
3093
1995
|
super(options);
|
|
3094
|
-
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
3095
|
-
__publicField(this, "_bucketSize", 1 << 12);
|
|
3096
|
-
__publicField(this, "_autoCompactRatio", 0.5);
|
|
3097
1996
|
/**
|
|
3098
1997
|
* Counter for shift/pop operations since last compaction check.
|
|
3099
1998
|
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
3100
1999
|
*/
|
|
3101
2000
|
__publicField(this, "_compactCounter", 0);
|
|
2001
|
+
__publicField(this, "_bucketSize", 1 << 12);
|
|
2002
|
+
__publicField(this, "_autoCompactRatio", 0.5);
|
|
3102
2003
|
__publicField(this, "_bucketFirst", 0);
|
|
3103
2004
|
__publicField(this, "_firstInBucket", 0);
|
|
3104
2005
|
__publicField(this, "_bucketLast", 0);
|
|
@@ -3106,6 +2007,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3106
2007
|
__publicField(this, "_bucketCount", 0);
|
|
3107
2008
|
__publicField(this, "_buckets", []);
|
|
3108
2009
|
__publicField(this, "_length", 0);
|
|
2010
|
+
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
3109
2011
|
if (options) {
|
|
3110
2012
|
const { bucketSize, autoCompactRatio } = options;
|
|
3111
2013
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
@@ -3209,146 +2111,36 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3209
2111
|
return this._length;
|
|
3210
2112
|
}
|
|
3211
2113
|
/**
|
|
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
|
-
|
|
3252
|
-
|
|
3253
|
-
* @example
|
|
3254
|
-
* // Deque peek at both ends
|
|
3255
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3256
|
-
*
|
|
3257
|
-
* // Get first element without removing
|
|
3258
|
-
* const first = deque.at(0);
|
|
3259
|
-
* console.log(first); // 10;
|
|
3260
|
-
*
|
|
3261
|
-
* // Get last element without removing
|
|
3262
|
-
* const last = deque.at(deque.length - 1);
|
|
3263
|
-
* console.log(last); // 50;
|
|
3264
|
-
*
|
|
3265
|
-
* // Length unchanged
|
|
3266
|
-
* console.log(deque.length); // 5;
|
|
3267
|
-
*/
|
|
3268
|
-
/**
|
|
3269
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
3270
|
-
* @remarks Time O(1), Space O(1)
|
|
3271
|
-
* @returns Front element or undefined.
|
|
2114
|
+
* Deque peek at both ends
|
|
2115
|
+
* @example
|
|
2116
|
+
* // Deque peek at both ends
|
|
2117
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2118
|
+
*
|
|
2119
|
+
* // Get first element without removing
|
|
2120
|
+
* const first = deque.at(0);
|
|
2121
|
+
* console.log(first); // 10;
|
|
2122
|
+
*
|
|
2123
|
+
* // Get last element without removing
|
|
2124
|
+
* const last = deque.at(deque.length - 1);
|
|
2125
|
+
* console.log(last); // 50;
|
|
2126
|
+
*
|
|
2127
|
+
* // Length unchanged
|
|
2128
|
+
* console.log(deque.length); // 5;
|
|
3272
2129
|
*/
|
|
3273
|
-
peek() {
|
|
3274
|
-
return this.first;
|
|
3275
|
-
}
|
|
3276
|
-
/**
|
|
3277
|
-
* Deque peek at both ends
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
* @example
|
|
3282
|
-
* // Deque peek at both ends
|
|
3283
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
3284
|
-
*
|
|
3285
|
-
* // Get first element without removing
|
|
3286
|
-
* const first = deque.at(0);
|
|
3287
|
-
* console.log(first); // 10;
|
|
3288
|
-
*
|
|
3289
|
-
* // Get last element without removing
|
|
3290
|
-
* const last = deque.at(deque.length - 1);
|
|
3291
|
-
* console.log(last); // 50;
|
|
3292
|
-
*
|
|
3293
|
-
* // Length unchanged
|
|
3294
|
-
* console.log(deque.length); // 5;
|
|
3295
|
-
*/
|
|
3296
2130
|
get first() {
|
|
3297
2131
|
if (this._length === 0) return;
|
|
3298
2132
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3299
2133
|
}
|
|
3300
2134
|
/**
|
|
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
|
-
|
|
3345
|
-
|
|
3346
|
-
* @example
|
|
3347
|
-
* // Peek at the back element
|
|
3348
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3349
|
-
* console.log(dq.last); // 'c';
|
|
3350
|
-
* console.log(dq.first); // 'a';
|
|
3351
|
-
*/
|
|
2135
|
+
* Get the last element without removing it.
|
|
2136
|
+
* @remarks Time O(1), Space O(1)
|
|
2137
|
+
* @returns Last element or undefined.
|
|
2138
|
+
* @example
|
|
2139
|
+
* // Peek at the back element
|
|
2140
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2141
|
+
* console.log(dq.last); // 'c';
|
|
2142
|
+
* console.log(dq.first); // 'a';
|
|
2143
|
+
*/
|
|
3352
2144
|
get last() {
|
|
3353
2145
|
if (this._length === 0) return;
|
|
3354
2146
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3367,71 +2159,56 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3367
2159
|
return new this(data, options);
|
|
3368
2160
|
}
|
|
3369
2161
|
/**
|
|
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
|
-
|
|
3420
|
-
*
|
|
3421
|
-
* // Verify the deque maintains insertion order
|
|
3422
|
-
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
3423
|
-
*
|
|
3424
|
-
* // Check length
|
|
3425
|
-
* console.log(deque.length); // 5;
|
|
3426
|
-
*
|
|
3427
|
-
* // Push to the end
|
|
3428
|
-
* deque.push(6);
|
|
3429
|
-
* console.log(deque.length); // 6;
|
|
3430
|
-
*
|
|
3431
|
-
* // Pop from the end
|
|
3432
|
-
* const last = deque.pop();
|
|
3433
|
-
* console.log(last); // 6;
|
|
3434
|
-
*/
|
|
2162
|
+
* Get the first element without removing it.
|
|
2163
|
+
* @remarks Time O(1), Space O(1)
|
|
2164
|
+
* @returns First element or undefined.
|
|
2165
|
+
* @example
|
|
2166
|
+
* // Deque peek at both ends
|
|
2167
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
2168
|
+
*
|
|
2169
|
+
* // Get first element without removing
|
|
2170
|
+
* const first = deque.at(0);
|
|
2171
|
+
* console.log(first); // 10;
|
|
2172
|
+
*
|
|
2173
|
+
* // Get last element without removing
|
|
2174
|
+
* const last = deque.at(deque.length - 1);
|
|
2175
|
+
* console.log(last); // 50;
|
|
2176
|
+
*
|
|
2177
|
+
* // Length unchanged
|
|
2178
|
+
* console.log(deque.length); // 5;
|
|
2179
|
+
*/
|
|
2180
|
+
/**
|
|
2181
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
2182
|
+
* @remarks Time O(1), Space O(1)
|
|
2183
|
+
* @returns Front element or undefined.
|
|
2184
|
+
*/
|
|
2185
|
+
peek() {
|
|
2186
|
+
return this.first;
|
|
2187
|
+
}
|
|
2188
|
+
/**
|
|
2189
|
+
* Append one element at the back.
|
|
2190
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2191
|
+
* @param element - Element to append.
|
|
2192
|
+
* @returns True when appended.
|
|
2193
|
+
* @example
|
|
2194
|
+
* // basic Deque creation and push/pop operations
|
|
2195
|
+
* // Create a simple Deque with initial values
|
|
2196
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
2197
|
+
*
|
|
2198
|
+
* // Verify the deque maintains insertion order
|
|
2199
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
2200
|
+
*
|
|
2201
|
+
* // Check length
|
|
2202
|
+
* console.log(deque.length); // 5;
|
|
2203
|
+
*
|
|
2204
|
+
* // Push to the end
|
|
2205
|
+
* deque.push(6);
|
|
2206
|
+
* console.log(deque.length); // 6;
|
|
2207
|
+
*
|
|
2208
|
+
* // Pop from the end
|
|
2209
|
+
* const last = deque.pop();
|
|
2210
|
+
* console.log(last); // 6;
|
|
2211
|
+
*/
|
|
3435
2212
|
push(element) {
|
|
3436
2213
|
if (this._length) {
|
|
3437
2214
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -3451,57 +2228,15 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3451
2228
|
return true;
|
|
3452
2229
|
}
|
|
3453
2230
|
/**
|
|
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
|
-
|
|
3498
|
-
|
|
3499
|
-
* @example
|
|
3500
|
-
* // Remove from the back
|
|
3501
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3502
|
-
* console.log(dq.pop()); // 3;
|
|
3503
|
-
* console.log(dq.length); // 2;
|
|
3504
|
-
*/
|
|
2231
|
+
* Remove and return the last element.
|
|
2232
|
+
* @remarks Time O(1), Space O(1)
|
|
2233
|
+
* @returns Removed element or undefined.
|
|
2234
|
+
* @example
|
|
2235
|
+
* // Remove from the back
|
|
2236
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2237
|
+
* console.log(dq.pop()); // 3;
|
|
2238
|
+
* console.log(dq.length); // 2;
|
|
2239
|
+
*/
|
|
3505
2240
|
pop() {
|
|
3506
2241
|
if (this._length === 0) return;
|
|
3507
2242
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -3521,57 +2256,15 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3521
2256
|
return element;
|
|
3522
2257
|
}
|
|
3523
2258
|
/**
|
|
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
|
-
|
|
3568
|
-
|
|
3569
|
-
* @example
|
|
3570
|
-
* // Remove from the front
|
|
3571
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3572
|
-
* console.log(dq.shift()); // 1;
|
|
3573
|
-
* console.log(dq.length); // 2;
|
|
3574
|
-
*/
|
|
2259
|
+
* Remove and return the first element.
|
|
2260
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2261
|
+
* @returns Removed element or undefined.
|
|
2262
|
+
* @example
|
|
2263
|
+
* // Remove from the front
|
|
2264
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2265
|
+
* console.log(dq.shift()); // 1;
|
|
2266
|
+
* console.log(dq.length); // 2;
|
|
2267
|
+
*/
|
|
3575
2268
|
shift() {
|
|
3576
2269
|
if (this._length === 0) return;
|
|
3577
2270
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
@@ -3591,68 +2284,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3591
2284
|
return element;
|
|
3592
2285
|
}
|
|
3593
2286
|
/**
|
|
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
|
-
|
|
3639
|
-
|
|
3640
|
-
* @example
|
|
3641
|
-
* // Deque shift and unshift operations
|
|
3642
|
-
* const deque = new Deque<number>([20, 30, 40]);
|
|
3643
|
-
*
|
|
3644
|
-
* // Unshift adds to the front
|
|
3645
|
-
* deque.unshift(10);
|
|
3646
|
-
* console.log([...deque]); // [10, 20, 30, 40];
|
|
3647
|
-
*
|
|
3648
|
-
* // Shift removes from the front (O(1) complexity!)
|
|
3649
|
-
* const first = deque.shift();
|
|
3650
|
-
* console.log(first); // 10;
|
|
3651
|
-
*
|
|
3652
|
-
* // Verify remaining elements
|
|
3653
|
-
* console.log([...deque]); // [20, 30, 40];
|
|
3654
|
-
* console.log(deque.length); // 3;
|
|
3655
|
-
*/
|
|
2287
|
+
* Prepend one element at the front.
|
|
2288
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
2289
|
+
* @param element - Element to prepend.
|
|
2290
|
+
* @returns True when prepended.
|
|
2291
|
+
* @example
|
|
2292
|
+
* // Deque shift and unshift operations
|
|
2293
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
2294
|
+
*
|
|
2295
|
+
* // Unshift adds to the front
|
|
2296
|
+
* deque.unshift(10);
|
|
2297
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
2298
|
+
*
|
|
2299
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
2300
|
+
* const first = deque.shift();
|
|
2301
|
+
* console.log(first); // 10;
|
|
2302
|
+
*
|
|
2303
|
+
* // Verify remaining elements
|
|
2304
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
2305
|
+
* console.log(deque.length); // 3;
|
|
2306
|
+
*/
|
|
3656
2307
|
unshift(element) {
|
|
3657
2308
|
if (this._length) {
|
|
3658
2309
|
if (this._firstInBucket > 0) {
|
|
@@ -3706,107 +2357,27 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3706
2357
|
return ans;
|
|
3707
2358
|
}
|
|
3708
2359
|
/**
|
|
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
|
-
|
|
3751
|
-
|
|
3752
|
-
* @example
|
|
3753
|
-
* // Check if empty
|
|
3754
|
-
* const dq = new Deque();
|
|
3755
|
-
* console.log(dq.isEmpty()); // true;
|
|
3756
|
-
*/
|
|
2360
|
+
* Check whether the deque is empty.
|
|
2361
|
+
* @remarks Time O(1), Space O(1)
|
|
2362
|
+
* @returns True if length is 0.
|
|
2363
|
+
* @example
|
|
2364
|
+
* // Check if empty
|
|
2365
|
+
* const dq = new Deque();
|
|
2366
|
+
* console.log(dq.isEmpty()); // true;
|
|
2367
|
+
*/
|
|
3757
2368
|
isEmpty() {
|
|
3758
2369
|
return this._length === 0;
|
|
3759
2370
|
}
|
|
3760
2371
|
/**
|
|
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
|
-
|
|
3803
|
-
|
|
3804
|
-
* @example
|
|
3805
|
-
* // Remove all elements
|
|
3806
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
3807
|
-
* dq.clear();
|
|
3808
|
-
* console.log(dq.length); // 0;
|
|
3809
|
-
*/
|
|
2372
|
+
* Remove all elements and reset structure.
|
|
2373
|
+
* @remarks Time O(1), Space O(1)
|
|
2374
|
+
* @returns void
|
|
2375
|
+
* @example
|
|
2376
|
+
* // Remove all elements
|
|
2377
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2378
|
+
* dq.clear();
|
|
2379
|
+
* console.log(dq.length); // 0;
|
|
2380
|
+
*/
|
|
3810
2381
|
clear() {
|
|
3811
2382
|
this._buckets = [new Array(this._bucketSize)];
|
|
3812
2383
|
this._bucketCount = 1;
|
|
@@ -3814,55 +2385,16 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3814
2385
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
3815
2386
|
}
|
|
3816
2387
|
/**
|
|
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
|
-
|
|
3859
|
-
|
|
3860
|
-
* @example
|
|
3861
|
-
* // Access by index
|
|
3862
|
-
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
3863
|
-
* console.log(dq.at(0)); // 'a';
|
|
3864
|
-
* console.log(dq.at(2)); // 'c';
|
|
3865
|
-
*/
|
|
2388
|
+
* Get the element at a given position.
|
|
2389
|
+
* @remarks Time O(1), Space O(1)
|
|
2390
|
+
* @param pos - Zero-based position from the front.
|
|
2391
|
+
* @returns Element or undefined.
|
|
2392
|
+
* @example
|
|
2393
|
+
* // Access by index
|
|
2394
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
2395
|
+
* console.log(dq.at(0)); // 'a';
|
|
2396
|
+
* console.log(dq.at(2)); // 'c';
|
|
2397
|
+
*/
|
|
3866
2398
|
at(pos) {
|
|
3867
2399
|
if (pos < 0 || pos >= this._length) return void 0;
|
|
3868
2400
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
@@ -4021,55 +2553,16 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4021
2553
|
}
|
|
4022
2554
|
}
|
|
4023
2555
|
/**
|
|
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
|
-
|
|
4066
|
-
|
|
4067
|
-
* @example
|
|
4068
|
-
* // Remove element
|
|
4069
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4070
|
-
* dq.delete(2);
|
|
4071
|
-
* console.log(dq.length); // 2;
|
|
4072
|
-
*/
|
|
2556
|
+
* Delete the first occurrence of a value.
|
|
2557
|
+
* @remarks Time O(N), Space O(1)
|
|
2558
|
+
* @param element - Element to remove (using the configured equality).
|
|
2559
|
+
* @returns True if an element was removed.
|
|
2560
|
+
* @example
|
|
2561
|
+
* // Remove element
|
|
2562
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2563
|
+
* dq.delete(2);
|
|
2564
|
+
* console.log(dq.length); // 2;
|
|
2565
|
+
*/
|
|
4073
2566
|
delete(element) {
|
|
4074
2567
|
const size = this._length;
|
|
4075
2568
|
if (size === 0) return false;
|
|
@@ -4113,78 +2606,39 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4113
2606
|
return this;
|
|
4114
2607
|
}
|
|
4115
2608
|
/**
|
|
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
|
-
|
|
4157
|
-
|
|
4158
|
-
* @example
|
|
4159
|
-
* // Deque for...of iteration and reverse
|
|
4160
|
-
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
4161
|
-
*
|
|
4162
|
-
* // Iterate forward
|
|
4163
|
-
* const forward: string[] = [];
|
|
4164
|
-
* for (const item of deque) {
|
|
4165
|
-
* forward.push(item);
|
|
4166
|
-
* }
|
|
4167
|
-
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
4168
|
-
*
|
|
4169
|
-
* // Reverse the deque
|
|
4170
|
-
* deque.reverse();
|
|
4171
|
-
* const backward: string[] = [];
|
|
4172
|
-
* for (const item of deque) {
|
|
4173
|
-
* backward.push(item);
|
|
4174
|
-
* }
|
|
4175
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4176
|
-
*/
|
|
4177
|
-
/**
|
|
4178
|
-
* Find the last value matching a predicate (scans back-to-front).
|
|
4179
|
-
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
4180
|
-
* @param predicate - Function called with (value, index, deque).
|
|
4181
|
-
* @returns Matching value or undefined.
|
|
4182
|
-
* @example
|
|
4183
|
-
* // Find last matching value
|
|
4184
|
-
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
4185
|
-
* console.log(d.findLast(v => v > 2)); // 5;
|
|
4186
|
-
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
4187
|
-
*/
|
|
2609
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
2610
|
+
* @remarks Time O(N), Space O(N)
|
|
2611
|
+
* @returns This deque.
|
|
2612
|
+
* @example
|
|
2613
|
+
* // Deque for...of iteration and reverse
|
|
2614
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2615
|
+
*
|
|
2616
|
+
* // Iterate forward
|
|
2617
|
+
* const forward: string[] = [];
|
|
2618
|
+
* for (const item of deque) {
|
|
2619
|
+
* forward.push(item);
|
|
2620
|
+
* }
|
|
2621
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2622
|
+
*
|
|
2623
|
+
* // Reverse the deque
|
|
2624
|
+
* deque.reverse();
|
|
2625
|
+
* const backward: string[] = [];
|
|
2626
|
+
* for (const item of deque) {
|
|
2627
|
+
* backward.push(item);
|
|
2628
|
+
* }
|
|
2629
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2630
|
+
*/
|
|
2631
|
+
/**
|
|
2632
|
+
* Find the last value matching a predicate (scans back-to-front).
|
|
2633
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2634
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2635
|
+
* @returns Matching value or undefined.
|
|
2636
|
+
* @example
|
|
2637
|
+
* // Find last matching value
|
|
2638
|
+
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
2639
|
+
* console.log(d.findLast(v => v > 2)); // 5;
|
|
2640
|
+
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
2641
|
+
*/
|
|
4188
2642
|
findLast(predicate) {
|
|
4189
2643
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4190
2644
|
const val = this.at(i);
|
|
@@ -4193,16 +2647,16 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4193
2647
|
return void 0;
|
|
4194
2648
|
}
|
|
4195
2649
|
/**
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
2650
|
+
* Find the index of the last value matching a predicate (scans back-to-front).
|
|
2651
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
2652
|
+
* @param predicate - Function called with (value, index, deque).
|
|
2653
|
+
* @returns Matching index, or -1 if not found.
|
|
2654
|
+
* @example
|
|
2655
|
+
* // Find last matching index
|
|
2656
|
+
* const d = new Deque([10, 20, 30, 20, 10]);
|
|
2657
|
+
* console.log(d.findLastIndex(v => v === 20)); // 3;
|
|
2658
|
+
* console.log(d.findLastIndex(v => v === 10)); // 4;
|
|
2659
|
+
*/
|
|
4206
2660
|
findLastIndex(predicate) {
|
|
4207
2661
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
4208
2662
|
if (predicate(this.at(i), i, this)) return i;
|
|
@@ -4210,28 +2664,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4210
2664
|
return -1;
|
|
4211
2665
|
}
|
|
4212
2666
|
/**
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
4234
|
-
*/
|
|
2667
|
+
* Deque for...of iteration and reverse
|
|
2668
|
+
* @example
|
|
2669
|
+
* // Deque for...of iteration and reverse
|
|
2670
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
2671
|
+
*
|
|
2672
|
+
* // Iterate forward
|
|
2673
|
+
* const forward: string[] = [];
|
|
2674
|
+
* for (const item of deque) {
|
|
2675
|
+
* forward.push(item);
|
|
2676
|
+
* }
|
|
2677
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
2678
|
+
*
|
|
2679
|
+
* // Reverse the deque
|
|
2680
|
+
* deque.reverse();
|
|
2681
|
+
* const backward: string[] = [];
|
|
2682
|
+
* for (const item of deque) {
|
|
2683
|
+
* backward.push(item);
|
|
2684
|
+
* }
|
|
2685
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
2686
|
+
*/
|
|
4235
2687
|
reverse() {
|
|
4236
2688
|
this._buckets.reverse().forEach(function(bucket) {
|
|
4237
2689
|
bucket.reverse();
|
|
@@ -4265,81 +2717,22 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4265
2717
|
return this;
|
|
4266
2718
|
}
|
|
4267
2719
|
/**
|
|
4268
|
-
*
|
|
2720
|
+
* Compact the deque by removing unused buckets.
|
|
4269
2721
|
* @remarks Time O(N), Space O(1)
|
|
4270
|
-
* @returns
|
|
4271
|
-
*/
|
|
4272
|
-
/**
|
|
4273
|
-
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
4274
|
-
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
4275
|
-
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2722
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
4276
2723
|
*/
|
|
4277
|
-
_autoCompact() {
|
|
4278
|
-
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
4279
|
-
this._compactCounter++;
|
|
4280
|
-
if (this._compactCounter < this._bucketSize) return;
|
|
4281
|
-
this._compactCounter = 0;
|
|
4282
|
-
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
4283
|
-
if (utilization < this._autoCompactRatio) {
|
|
4284
|
-
this.shrinkToFit();
|
|
4285
|
-
}
|
|
4286
|
-
}
|
|
4287
2724
|
/**
|
|
4288
2725
|
* Compact the deque by removing unused buckets.
|
|
4289
2726
|
* @remarks Time O(N), Space O(1)
|
|
4290
2727
|
* @returns True if compaction was performed (bucket count reduced).
|
|
2728
|
+
* @example
|
|
2729
|
+
* // Reclaim memory
|
|
2730
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
2731
|
+
* dq.shift();
|
|
2732
|
+
* dq.shift();
|
|
2733
|
+
* dq.compact();
|
|
2734
|
+
* console.log(dq.length); // 3;
|
|
4291
2735
|
*/
|
|
4292
|
-
/**
|
|
4293
|
-
* Compact the deque by removing unused buckets.
|
|
4294
|
-
* @remarks Time O(N), Space O(1)
|
|
4295
|
-
* @returns True if compaction was performed (bucket count reduced).
|
|
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
|
-
|
|
4334
|
-
|
|
4335
|
-
* @example
|
|
4336
|
-
* // Reclaim memory
|
|
4337
|
-
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
4338
|
-
* dq.shift();
|
|
4339
|
-
* dq.shift();
|
|
4340
|
-
* dq.compact();
|
|
4341
|
-
* console.log(dq.length); // 3;
|
|
4342
|
-
*/
|
|
4343
2736
|
compact() {
|
|
4344
2737
|
const before = this._bucketCount;
|
|
4345
2738
|
this.shrinkToFit();
|
|
@@ -4367,57 +2760,17 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4367
2760
|
this._compactCounter = 0;
|
|
4368
2761
|
}
|
|
4369
2762
|
/**
|
|
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
|
-
|
|
4412
|
-
|
|
4413
|
-
* @example
|
|
4414
|
-
* // Create independent copy
|
|
4415
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4416
|
-
* const copy = dq.clone();
|
|
4417
|
-
* copy.pop();
|
|
4418
|
-
* console.log(dq.length); // 3;
|
|
4419
|
-
* console.log(copy.length); // 2;
|
|
4420
|
-
*/
|
|
2763
|
+
* Deep clone this deque, preserving options.
|
|
2764
|
+
* @remarks Time O(N), Space O(N)
|
|
2765
|
+
* @returns A new deque with the same content and options.
|
|
2766
|
+
* @example
|
|
2767
|
+
* // Create independent copy
|
|
2768
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2769
|
+
* const copy = dq.clone();
|
|
2770
|
+
* copy.pop();
|
|
2771
|
+
* console.log(dq.length); // 3;
|
|
2772
|
+
* console.log(copy.length); // 2;
|
|
2773
|
+
*/
|
|
4421
2774
|
clone() {
|
|
4422
2775
|
return this._createLike(this, {
|
|
4423
2776
|
bucketSize: this.bucketSize,
|
|
@@ -4426,57 +2779,17 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4426
2779
|
});
|
|
4427
2780
|
}
|
|
4428
2781
|
/**
|
|
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
|
-
|
|
4473
|
-
|
|
4474
|
-
* @example
|
|
4475
|
-
* // Filter elements
|
|
4476
|
-
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
4477
|
-
* const result = dq.filter(x => x > 2);
|
|
4478
|
-
* console.log(result.length); // 2;
|
|
4479
|
-
*/
|
|
2782
|
+
* Filter elements into a new deque of the same class.
|
|
2783
|
+
* @remarks Time O(N), Space O(N)
|
|
2784
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
2785
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
2786
|
+
* @returns A new deque with kept elements.
|
|
2787
|
+
* @example
|
|
2788
|
+
* // Filter elements
|
|
2789
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
2790
|
+
* const result = dq.filter(x => x > 2);
|
|
2791
|
+
* console.log(result.length); // 2;
|
|
2792
|
+
*/
|
|
4480
2793
|
filter(predicate, thisArg) {
|
|
4481
2794
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
4482
2795
|
out._setBucketSize(this._bucketSize);
|
|
@@ -4505,59 +2818,20 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4505
2818
|
return out;
|
|
4506
2819
|
}
|
|
4507
2820
|
/**
|
|
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
|
-
|
|
4554
|
-
|
|
4555
|
-
* @example
|
|
4556
|
-
* // Transform elements
|
|
4557
|
-
* const dq = new Deque<number>([1, 2, 3]);
|
|
4558
|
-
* const result = dq.map(x => x * 10);
|
|
4559
|
-
* console.log(result.toArray()); // [10, 20, 30];
|
|
4560
|
-
*/
|
|
2821
|
+
* Map elements into a new deque (possibly different element type).
|
|
2822
|
+
* @remarks Time O(N), Space O(N)
|
|
2823
|
+
* @template EM
|
|
2824
|
+
* @template RM
|
|
2825
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
2826
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
2827
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2828
|
+
* @returns A new Deque with mapped elements.
|
|
2829
|
+
* @example
|
|
2830
|
+
* // Transform elements
|
|
2831
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
2832
|
+
* const result = dq.map(x => x * 10);
|
|
2833
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
2834
|
+
*/
|
|
4561
2835
|
map(callback, options, thisArg) {
|
|
4562
2836
|
const out = this._createLike([], {
|
|
4563
2837
|
...options != null ? options : {},
|
|
@@ -4572,6 +2846,26 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4572
2846
|
}
|
|
4573
2847
|
return out;
|
|
4574
2848
|
}
|
|
2849
|
+
/**
|
|
2850
|
+
* Trim unused buckets to fit exactly the active range.
|
|
2851
|
+
* @remarks Time O(N), Space O(1)
|
|
2852
|
+
* @returns void
|
|
2853
|
+
*/
|
|
2854
|
+
/**
|
|
2855
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2856
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2857
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2858
|
+
*/
|
|
2859
|
+
_autoCompact() {
|
|
2860
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2861
|
+
this._compactCounter++;
|
|
2862
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2863
|
+
this._compactCounter = 0;
|
|
2864
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2865
|
+
if (utilization < this._autoCompactRatio) {
|
|
2866
|
+
this.shrinkToFit();
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
4575
2869
|
/**
|
|
4576
2870
|
* (Protected) Set the internal bucket size.
|
|
4577
2871
|
* @remarks Time O(1), Space O(1)
|