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
package/dist/esm/linked-list.mjs
CHANGED
|
@@ -721,7 +721,6 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
721
721
|
static {
|
|
722
722
|
__name(this, "SinglyLinkedList");
|
|
723
723
|
}
|
|
724
|
-
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
725
724
|
/**
|
|
726
725
|
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
727
726
|
* @remarks Time O(N), Space O(N)
|
|
@@ -793,68 +792,26 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
793
792
|
return list;
|
|
794
793
|
}
|
|
795
794
|
/**
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
* @example
|
|
843
|
-
* // basic SinglyLinkedList creation and push operation
|
|
844
|
-
* // Create a simple SinglyLinkedList with initial values
|
|
845
|
-
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
846
|
-
*
|
|
847
|
-
* // Verify the list maintains insertion order
|
|
848
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
849
|
-
*
|
|
850
|
-
* // Check length
|
|
851
|
-
* console.log(list.length); // 5;
|
|
852
|
-
*
|
|
853
|
-
* // Push a new element to the end
|
|
854
|
-
* list.push(6);
|
|
855
|
-
* console.log(list.length); // 6;
|
|
856
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
857
|
-
*/
|
|
795
|
+
* Append an element/node to the tail.
|
|
796
|
+
* @remarks Time O(1), Space O(1)
|
|
797
|
+
* @param elementOrNode - Element or node to append.
|
|
798
|
+
* @returns True when appended.
|
|
799
|
+
* @example
|
|
800
|
+
* // basic SinglyLinkedList creation and push operation
|
|
801
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
802
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
803
|
+
*
|
|
804
|
+
* // Verify the list maintains insertion order
|
|
805
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
806
|
+
*
|
|
807
|
+
* // Check length
|
|
808
|
+
* console.log(list.length); // 5;
|
|
809
|
+
*
|
|
810
|
+
* // Push a new element to the end
|
|
811
|
+
* list.push(6);
|
|
812
|
+
* console.log(list.length); // 6;
|
|
813
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
814
|
+
*/
|
|
858
815
|
push(elementOrNode) {
|
|
859
816
|
const newNode = this._ensureNode(elementOrNode);
|
|
860
817
|
if (!this.head) {
|
|
@@ -868,67 +825,25 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
868
825
|
return true;
|
|
869
826
|
}
|
|
870
827
|
/**
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
* @example
|
|
917
|
-
* // SinglyLinkedList pop and shift operations
|
|
918
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
919
|
-
*
|
|
920
|
-
* // Pop removes from the end
|
|
921
|
-
* const last = list.pop();
|
|
922
|
-
* console.log(last); // 50;
|
|
923
|
-
*
|
|
924
|
-
* // Shift removes from the beginning
|
|
925
|
-
* const first = list.shift();
|
|
926
|
-
* console.log(first); // 10;
|
|
927
|
-
*
|
|
928
|
-
* // Verify remaining elements
|
|
929
|
-
* console.log([...list]); // [20, 30, 40];
|
|
930
|
-
* console.log(list.length); // 3;
|
|
931
|
-
*/
|
|
828
|
+
* Remove and return the tail element.
|
|
829
|
+
* @remarks Time O(N), Space O(1)
|
|
830
|
+
* @returns Removed element or undefined.
|
|
831
|
+
* @example
|
|
832
|
+
* // SinglyLinkedList pop and shift operations
|
|
833
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
834
|
+
*
|
|
835
|
+
* // Pop removes from the end
|
|
836
|
+
* const last = list.pop();
|
|
837
|
+
* console.log(last); // 50;
|
|
838
|
+
*
|
|
839
|
+
* // Shift removes from the beginning
|
|
840
|
+
* const first = list.shift();
|
|
841
|
+
* console.log(first); // 10;
|
|
842
|
+
*
|
|
843
|
+
* // Verify remaining elements
|
|
844
|
+
* console.log([...list]); // [20, 30, 40];
|
|
845
|
+
* console.log(list.length); // 3;
|
|
846
|
+
*/
|
|
932
847
|
pop() {
|
|
933
848
|
if (!this.head) return void 0;
|
|
934
849
|
if (this.head === this.tail) {
|
|
@@ -947,57 +862,15 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
947
862
|
return value;
|
|
948
863
|
}
|
|
949
864
|
/**
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
* @example
|
|
996
|
-
* // Remove from the front
|
|
997
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
998
|
-
* console.log(list.shift()); // 10;
|
|
999
|
-
* console.log(list.length); // 2;
|
|
1000
|
-
*/
|
|
865
|
+
* Remove and return the head element.
|
|
866
|
+
* @remarks Time O(1), Space O(1)
|
|
867
|
+
* @returns Removed element or undefined.
|
|
868
|
+
* @example
|
|
869
|
+
* // Remove from the front
|
|
870
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
871
|
+
* console.log(list.shift()); // 10;
|
|
872
|
+
* console.log(list.length); // 2;
|
|
873
|
+
*/
|
|
1001
874
|
shift() {
|
|
1002
875
|
if (!this.head) return void 0;
|
|
1003
876
|
const removed = this.head;
|
|
@@ -1007,73 +880,31 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1007
880
|
return removed.value;
|
|
1008
881
|
}
|
|
1009
882
|
/**
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
* @example
|
|
1057
|
-
* // SinglyLinkedList unshift and forward traversal
|
|
1058
|
-
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
1059
|
-
*
|
|
1060
|
-
* // Unshift adds to the beginning
|
|
1061
|
-
* list.unshift(10);
|
|
1062
|
-
* console.log([...list]); // [10, 20, 30, 40];
|
|
1063
|
-
*
|
|
1064
|
-
* // Access elements (forward traversal only for singly linked)
|
|
1065
|
-
* const second = list.at(1);
|
|
1066
|
-
* console.log(second); // 20;
|
|
1067
|
-
*
|
|
1068
|
-
* // SinglyLinkedList allows forward iteration only
|
|
1069
|
-
* const elements: number[] = [];
|
|
1070
|
-
* for (const item of list) {
|
|
1071
|
-
* elements.push(item);
|
|
1072
|
-
* }
|
|
1073
|
-
* console.log(elements); // [10, 20, 30, 40];
|
|
1074
|
-
*
|
|
1075
|
-
* console.log(list.length); // 4;
|
|
1076
|
-
*/
|
|
883
|
+
* Prepend an element/node to the head.
|
|
884
|
+
* @remarks Time O(1), Space O(1)
|
|
885
|
+
* @param elementOrNode - Element or node to prepend.
|
|
886
|
+
* @returns True when prepended.
|
|
887
|
+
* @example
|
|
888
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
889
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
890
|
+
*
|
|
891
|
+
* // Unshift adds to the beginning
|
|
892
|
+
* list.unshift(10);
|
|
893
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
894
|
+
*
|
|
895
|
+
* // Access elements (forward traversal only for singly linked)
|
|
896
|
+
* const second = list.at(1);
|
|
897
|
+
* console.log(second); // 20;
|
|
898
|
+
*
|
|
899
|
+
* // SinglyLinkedList allows forward iteration only
|
|
900
|
+
* const elements: number[] = [];
|
|
901
|
+
* for (const item of list) {
|
|
902
|
+
* elements.push(item);
|
|
903
|
+
* }
|
|
904
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
905
|
+
*
|
|
906
|
+
* console.log(list.length); // 4;
|
|
907
|
+
*/
|
|
1077
908
|
unshift(elementOrNode) {
|
|
1078
909
|
const newNode = this._ensureNode(elementOrNode);
|
|
1079
910
|
if (!this.head) {
|
|
@@ -1129,59 +960,17 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1129
960
|
return void 0;
|
|
1130
961
|
}
|
|
1131
962
|
/**
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
* @example
|
|
1179
|
-
* // Access element by index
|
|
1180
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
1181
|
-
* console.log(list.at(0)); // 'a';
|
|
1182
|
-
* console.log(list.at(2)); // 'c';
|
|
1183
|
-
* console.log(list.at(3)); // 'd';
|
|
1184
|
-
*/
|
|
963
|
+
* Get the element at a given index.
|
|
964
|
+
* @remarks Time O(N), Space O(1)
|
|
965
|
+
* @param index - Zero-based index.
|
|
966
|
+
* @returns Element or undefined.
|
|
967
|
+
* @example
|
|
968
|
+
* // Access element by index
|
|
969
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
970
|
+
* console.log(list.at(0)); // 'a';
|
|
971
|
+
* console.log(list.at(2)); // 'c';
|
|
972
|
+
* console.log(list.at(3)); // 'd';
|
|
973
|
+
*/
|
|
1185
974
|
at(index) {
|
|
1186
975
|
if (index < 0 || index >= this._length) return void 0;
|
|
1187
976
|
let current = this.head;
|
|
@@ -1198,54 +987,15 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1198
987
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
1199
988
|
}
|
|
1200
989
|
/**
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
* @example
|
|
1245
|
-
* // Get node at index
|
|
1246
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1247
|
-
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1248
|
-
*/
|
|
990
|
+
* Get the node reference at a given index.
|
|
991
|
+
* @remarks Time O(N), Space O(1)
|
|
992
|
+
* @param index - Zero-based index.
|
|
993
|
+
* @returns Node or undefined.
|
|
994
|
+
* @example
|
|
995
|
+
* // Get node at index
|
|
996
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
997
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
998
|
+
*/
|
|
1249
999
|
getNodeAt(index) {
|
|
1250
1000
|
if (index < 0 || index >= this._length) return void 0;
|
|
1251
1001
|
let current = this.head;
|
|
@@ -1253,55 +1003,16 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1253
1003
|
return current;
|
|
1254
1004
|
}
|
|
1255
1005
|
/**
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
* @example
|
|
1300
|
-
* // Remove by index
|
|
1301
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1302
|
-
* list.deleteAt(1);
|
|
1303
|
-
* console.log(list.toArray()); // ['a', 'c'];
|
|
1304
|
-
*/
|
|
1006
|
+
* Delete the element at an index.
|
|
1007
|
+
* @remarks Time O(N), Space O(1)
|
|
1008
|
+
* @param index - Zero-based index.
|
|
1009
|
+
* @returns Removed element or undefined.
|
|
1010
|
+
* @example
|
|
1011
|
+
* // Remove by index
|
|
1012
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1013
|
+
* list.deleteAt(1);
|
|
1014
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1015
|
+
*/
|
|
1305
1016
|
deleteAt(index) {
|
|
1306
1017
|
if (index < 0 || index >= this._length) return void 0;
|
|
1307
1018
|
if (index === 0) return this.shift();
|
|
@@ -1314,55 +1025,16 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1314
1025
|
return value;
|
|
1315
1026
|
}
|
|
1316
1027
|
/**
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
* @example
|
|
1361
|
-
* // Remove first occurrence
|
|
1362
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1363
|
-
* list.delete(2);
|
|
1364
|
-
* console.log(list.toArray()); // [1, 3, 2];
|
|
1365
|
-
*/
|
|
1028
|
+
* Delete the first match by value/node.
|
|
1029
|
+
* @remarks Time O(N), Space O(1)
|
|
1030
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1031
|
+
* @returns True if removed.
|
|
1032
|
+
* @example
|
|
1033
|
+
* // Remove first occurrence
|
|
1034
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1035
|
+
* list.delete(2);
|
|
1036
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1037
|
+
*/
|
|
1366
1038
|
delete(elementOrNode) {
|
|
1367
1039
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
1368
1040
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -1379,56 +1051,17 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1379
1051
|
return true;
|
|
1380
1052
|
}
|
|
1381
1053
|
/**
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
* @example
|
|
1427
|
-
* // Insert at index
|
|
1428
|
-
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1429
|
-
* list.addAt(1, 2);
|
|
1430
|
-
* console.log(list.toArray()); // [1, 2, 3];
|
|
1431
|
-
*/
|
|
1054
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1055
|
+
* @remarks Time O(N), Space O(1)
|
|
1056
|
+
* @param index - Zero-based index.
|
|
1057
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1058
|
+
* @returns True if inserted.
|
|
1059
|
+
* @example
|
|
1060
|
+
* // Insert at index
|
|
1061
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1062
|
+
* list.addAt(1, 2);
|
|
1063
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1064
|
+
*/
|
|
1432
1065
|
addAt(index, newElementOrNode) {
|
|
1433
1066
|
if (index < 0 || index > this._length) return false;
|
|
1434
1067
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -1454,163 +1087,41 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1454
1087
|
return true;
|
|
1455
1088
|
}
|
|
1456
1089
|
/**
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
* @example
|
|
1501
|
-
* // Check empty
|
|
1502
|
-
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1503
|
-
*/
|
|
1090
|
+
* Check whether the list is empty.
|
|
1091
|
+
* @remarks Time O(1), Space O(1)
|
|
1092
|
+
* @returns True if length is 0.
|
|
1093
|
+
* @example
|
|
1094
|
+
* // Check empty
|
|
1095
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1096
|
+
*/
|
|
1504
1097
|
isEmpty() {
|
|
1505
1098
|
return this._length === 0;
|
|
1506
1099
|
}
|
|
1507
1100
|
/**
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
* @example
|
|
1552
|
-
* // Remove all
|
|
1553
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1554
|
-
* list.clear();
|
|
1555
|
-
* console.log(list.isEmpty()); // true;
|
|
1556
|
-
*/
|
|
1101
|
+
* Remove all nodes and reset length.
|
|
1102
|
+
* @remarks Time O(N), Space O(1)
|
|
1103
|
+
* @returns void
|
|
1104
|
+
* @example
|
|
1105
|
+
* // Remove all
|
|
1106
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1107
|
+
* list.clear();
|
|
1108
|
+
* console.log(list.isEmpty()); // true;
|
|
1109
|
+
*/
|
|
1557
1110
|
clear() {
|
|
1558
1111
|
this._head = void 0;
|
|
1559
1112
|
this._tail = void 0;
|
|
1560
1113
|
this._length = 0;
|
|
1561
1114
|
}
|
|
1562
1115
|
/**
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
* @example
|
|
1609
|
-
* // Reverse the list in-place
|
|
1610
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1611
|
-
* list.reverse();
|
|
1612
|
-
* console.log([...list]); // [4, 3, 2, 1];
|
|
1613
|
-
*/
|
|
1116
|
+
* Reverse the list in place.
|
|
1117
|
+
* @remarks Time O(N), Space O(1)
|
|
1118
|
+
* @returns This list.
|
|
1119
|
+
* @example
|
|
1120
|
+
* // Reverse the list in-place
|
|
1121
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1122
|
+
* list.reverse();
|
|
1123
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1124
|
+
*/
|
|
1614
1125
|
reverse() {
|
|
1615
1126
|
if (!this.head || this.head === this.tail) return this;
|
|
1616
1127
|
let prev;
|
|
@@ -1785,126 +1296,44 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1785
1296
|
return false;
|
|
1786
1297
|
}
|
|
1787
1298
|
/**
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
* @example
|
|
1832
|
-
* // Deep copy
|
|
1833
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1834
|
-
* const copy = list.clone();
|
|
1835
|
-
* copy.pop();
|
|
1836
|
-
* console.log(list.length); // 3;
|
|
1837
|
-
* console.log(copy.length); // 2;
|
|
1838
|
-
*/
|
|
1299
|
+
* Deep clone this list (values are copied by reference).
|
|
1300
|
+
* @remarks Time O(N), Space O(N)
|
|
1301
|
+
* @returns A new list with the same element sequence.
|
|
1302
|
+
* @example
|
|
1303
|
+
* // Deep copy
|
|
1304
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1305
|
+
* const copy = list.clone();
|
|
1306
|
+
* copy.pop();
|
|
1307
|
+
* console.log(list.length); // 3;
|
|
1308
|
+
* console.log(copy.length); // 2;
|
|
1309
|
+
*/
|
|
1839
1310
|
clone() {
|
|
1840
1311
|
const out = this._createInstance();
|
|
1841
1312
|
for (const v of this) out.push(v);
|
|
1842
1313
|
return out;
|
|
1843
1314
|
}
|
|
1844
1315
|
/**
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
* @example
|
|
1893
|
-
* // SinglyLinkedList filter and map operations
|
|
1894
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1895
|
-
*
|
|
1896
|
-
* // Filter even numbers
|
|
1897
|
-
* const filtered = list.filter(value => value % 2 === 0);
|
|
1898
|
-
* console.log(filtered.length); // 2;
|
|
1899
|
-
*
|
|
1900
|
-
* // Map to double values
|
|
1901
|
-
* const doubled = list.map(value => value * 2);
|
|
1902
|
-
* console.log(doubled.length); // 5;
|
|
1903
|
-
*
|
|
1904
|
-
* // Use reduce to sum
|
|
1905
|
-
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1906
|
-
* console.log(sum); // 15;
|
|
1907
|
-
*/
|
|
1316
|
+
* Filter values into a new list of the same class.
|
|
1317
|
+
* @remarks Time O(N), Space O(N)
|
|
1318
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1319
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1320
|
+
* @returns A new list with kept values.
|
|
1321
|
+
* @example
|
|
1322
|
+
* // SinglyLinkedList filter and map operations
|
|
1323
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1324
|
+
*
|
|
1325
|
+
* // Filter even numbers
|
|
1326
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1327
|
+
* console.log(filtered.length); // 2;
|
|
1328
|
+
*
|
|
1329
|
+
* // Map to double values
|
|
1330
|
+
* const doubled = list.map(value => value * 2);
|
|
1331
|
+
* console.log(doubled.length); // 5;
|
|
1332
|
+
*
|
|
1333
|
+
* // Use reduce to sum
|
|
1334
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1335
|
+
* console.log(sum); // 15;
|
|
1336
|
+
*/
|
|
1908
1337
|
filter(callback, thisArg) {
|
|
1909
1338
|
const out = this._createInstance();
|
|
1910
1339
|
let index = 0;
|
|
@@ -1928,68 +1357,27 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1928
1357
|
return out;
|
|
1929
1358
|
}
|
|
1930
1359
|
/**
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
* @example
|
|
1982
|
-
* // Transform elements
|
|
1983
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1984
|
-
* const doubled = list.map(n => n * 2);
|
|
1985
|
-
* console.log([...doubled]); // [2, 4, 6];
|
|
1986
|
-
*/
|
|
1360
|
+
* Map values into a new list (possibly different element type).
|
|
1361
|
+
* @remarks Time O(N), Space O(N)
|
|
1362
|
+
* @template EM
|
|
1363
|
+
* @template RM
|
|
1364
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1365
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1366
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1367
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1368
|
+
* @example
|
|
1369
|
+
* // Transform elements
|
|
1370
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1371
|
+
* const doubled = list.map(n => n * 2);
|
|
1372
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1373
|
+
*/
|
|
1987
1374
|
map(callback, options, thisArg) {
|
|
1988
1375
|
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
1989
1376
|
let index = 0;
|
|
1990
1377
|
for (const value of this) out.push(callback.call(thisArg, value, index++, this));
|
|
1991
1378
|
return out;
|
|
1992
1379
|
}
|
|
1380
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
1993
1381
|
/**
|
|
1994
1382
|
* (Protected) Create a node from a value.
|
|
1995
1383
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2176,7 +1564,6 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2176
1564
|
static {
|
|
2177
1565
|
__name(this, "DoublyLinkedList");
|
|
2178
1566
|
}
|
|
2179
|
-
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
2180
1567
|
/**
|
|
2181
1568
|
* Create a DoublyLinkedList and optionally bulk-insert elements.
|
|
2182
1569
|
* @remarks Time O(N), Space O(N)
|
|
@@ -2259,68 +1646,26 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2259
1646
|
return elementNodeOrPredicate instanceof DoublyLinkedListNode;
|
|
2260
1647
|
}
|
|
2261
1648
|
/**
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
* @example
|
|
2309
|
-
* // basic DoublyLinkedList creation and push operation
|
|
2310
|
-
* // Create a simple DoublyLinkedList with initial values
|
|
2311
|
-
* const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
2312
|
-
*
|
|
2313
|
-
* // Verify the list maintains insertion order
|
|
2314
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
2315
|
-
*
|
|
2316
|
-
* // Check length
|
|
2317
|
-
* console.log(list.length); // 5;
|
|
2318
|
-
*
|
|
2319
|
-
* // Push a new element to the end
|
|
2320
|
-
* list.push(6);
|
|
2321
|
-
* console.log(list.length); // 6;
|
|
2322
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
2323
|
-
*/
|
|
1649
|
+
* Append an element/node to the tail.
|
|
1650
|
+
* @remarks Time O(1), Space O(1)
|
|
1651
|
+
* @param elementOrNode - Element or node to append.
|
|
1652
|
+
* @returns True when appended.
|
|
1653
|
+
* @example
|
|
1654
|
+
* // basic DoublyLinkedList creation and push operation
|
|
1655
|
+
* // Create a simple DoublyLinkedList with initial values
|
|
1656
|
+
* const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
1657
|
+
*
|
|
1658
|
+
* // Verify the list maintains insertion order
|
|
1659
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
1660
|
+
*
|
|
1661
|
+
* // Check length
|
|
1662
|
+
* console.log(list.length); // 5;
|
|
1663
|
+
*
|
|
1664
|
+
* // Push a new element to the end
|
|
1665
|
+
* list.push(6);
|
|
1666
|
+
* console.log(list.length); // 6;
|
|
1667
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
1668
|
+
*/
|
|
2324
1669
|
push(elementOrNode) {
|
|
2325
1670
|
const newNode = this._ensureNode(elementOrNode);
|
|
2326
1671
|
if (!this.head) {
|
|
@@ -2336,67 +1681,25 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2336
1681
|
return true;
|
|
2337
1682
|
}
|
|
2338
1683
|
/**
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
* @example
|
|
2385
|
-
* // DoublyLinkedList pop and shift operations
|
|
2386
|
-
* const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
2387
|
-
*
|
|
2388
|
-
* // Pop removes from the end
|
|
2389
|
-
* const last = list.pop();
|
|
2390
|
-
* console.log(last); // 50;
|
|
2391
|
-
*
|
|
2392
|
-
* // Shift removes from the beginning
|
|
2393
|
-
* const first = list.shift();
|
|
2394
|
-
* console.log(first); // 10;
|
|
2395
|
-
*
|
|
2396
|
-
* // Verify remaining elements
|
|
2397
|
-
* console.log([...list]); // [20, 30, 40];
|
|
2398
|
-
* console.log(list.length); // 3;
|
|
2399
|
-
*/
|
|
1684
|
+
* Remove and return the tail element.
|
|
1685
|
+
* @remarks Time O(1), Space O(1)
|
|
1686
|
+
* @returns Removed element or undefined.
|
|
1687
|
+
* @example
|
|
1688
|
+
* // DoublyLinkedList pop and shift operations
|
|
1689
|
+
* const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
1690
|
+
*
|
|
1691
|
+
* // Pop removes from the end
|
|
1692
|
+
* const last = list.pop();
|
|
1693
|
+
* console.log(last); // 50;
|
|
1694
|
+
*
|
|
1695
|
+
* // Shift removes from the beginning
|
|
1696
|
+
* const first = list.shift();
|
|
1697
|
+
* console.log(first); // 10;
|
|
1698
|
+
*
|
|
1699
|
+
* // Verify remaining elements
|
|
1700
|
+
* console.log([...list]); // [20, 30, 40];
|
|
1701
|
+
* console.log(list.length); // 3;
|
|
1702
|
+
*/
|
|
2400
1703
|
pop() {
|
|
2401
1704
|
if (!this.tail) return void 0;
|
|
2402
1705
|
const removed = this.tail;
|
|
@@ -2411,57 +1714,15 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2411
1714
|
return removed.value;
|
|
2412
1715
|
}
|
|
2413
1716
|
/**
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
* @example
|
|
2460
|
-
* // Remove from the front
|
|
2461
|
-
* const list = new DoublyLinkedList<number>([10, 20, 30]);
|
|
2462
|
-
* console.log(list.shift()); // 10;
|
|
2463
|
-
* console.log(list.first); // 20;
|
|
2464
|
-
*/
|
|
1717
|
+
* Remove and return the head element.
|
|
1718
|
+
* @remarks Time O(1), Space O(1)
|
|
1719
|
+
* @returns Removed element or undefined.
|
|
1720
|
+
* @example
|
|
1721
|
+
* // Remove from the front
|
|
1722
|
+
* const list = new DoublyLinkedList<number>([10, 20, 30]);
|
|
1723
|
+
* console.log(list.shift()); // 10;
|
|
1724
|
+
* console.log(list.first); // 20;
|
|
1725
|
+
*/
|
|
2465
1726
|
shift() {
|
|
2466
1727
|
if (!this.head) return void 0;
|
|
2467
1728
|
const removed = this.head;
|
|
@@ -2476,58 +1737,16 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2476
1737
|
return removed.value;
|
|
2477
1738
|
}
|
|
2478
1739
|
/**
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
* @example
|
|
2526
|
-
* // Add to the front
|
|
2527
|
-
* const list = new DoublyLinkedList<number>([2, 3]);
|
|
2528
|
-
* list.unshift(1);
|
|
2529
|
-
* console.log([...list]); // [1, 2, 3];
|
|
2530
|
-
*/
|
|
1740
|
+
* Prepend an element/node to the head.
|
|
1741
|
+
* @remarks Time O(1), Space O(1)
|
|
1742
|
+
* @param elementOrNode - Element or node to prepend.
|
|
1743
|
+
* @returns True when prepended.
|
|
1744
|
+
* @example
|
|
1745
|
+
* // Add to the front
|
|
1746
|
+
* const list = new DoublyLinkedList<number>([2, 3]);
|
|
1747
|
+
* list.unshift(1);
|
|
1748
|
+
* console.log([...list]); // [1, 2, 3];
|
|
1749
|
+
*/
|
|
2531
1750
|
unshift(elementOrNode) {
|
|
2532
1751
|
const newNode = this._ensureNode(elementOrNode);
|
|
2533
1752
|
if (!this.head) {
|
|
@@ -2571,58 +1790,16 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2571
1790
|
return ans;
|
|
2572
1791
|
}
|
|
2573
1792
|
/**
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
* @example
|
|
2621
|
-
* // Access by index
|
|
2622
|
-
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
2623
|
-
* console.log(list.at(1)); // 'b';
|
|
2624
|
-
* console.log(list.at(2)); // 'c';
|
|
2625
|
-
*/
|
|
1793
|
+
* Get the element at a given index.
|
|
1794
|
+
* @remarks Time O(N), Space O(1)
|
|
1795
|
+
* @param index - Zero-based index.
|
|
1796
|
+
* @returns Element or undefined.
|
|
1797
|
+
* @example
|
|
1798
|
+
* // Access by index
|
|
1799
|
+
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
1800
|
+
* console.log(list.at(1)); // 'b';
|
|
1801
|
+
* console.log(list.at(2)); // 'c';
|
|
1802
|
+
*/
|
|
2626
1803
|
at(index) {
|
|
2627
1804
|
if (index < 0 || index >= this._length) return void 0;
|
|
2628
1805
|
let current = this.head;
|
|
@@ -2630,54 +1807,15 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2630
1807
|
return current?.value;
|
|
2631
1808
|
}
|
|
2632
1809
|
/**
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
* @example
|
|
2677
|
-
* // Get node at index
|
|
2678
|
-
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
2679
|
-
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
2680
|
-
*/
|
|
1810
|
+
* Get the node reference at a given index.
|
|
1811
|
+
* @remarks Time O(N), Space O(1)
|
|
1812
|
+
* @param index - Zero-based index.
|
|
1813
|
+
* @returns Node or undefined.
|
|
1814
|
+
* @example
|
|
1815
|
+
* // Get node at index
|
|
1816
|
+
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
1817
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
1818
|
+
*/
|
|
2681
1819
|
getNodeAt(index) {
|
|
2682
1820
|
if (index < 0 || index >= this._length) return void 0;
|
|
2683
1821
|
let current = this.head;
|
|
@@ -2716,56 +1854,17 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2716
1854
|
return void 0;
|
|
2717
1855
|
}
|
|
2718
1856
|
/**
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
* @example
|
|
2764
|
-
* // Insert at position
|
|
2765
|
-
* const list = new DoublyLinkedList<number>([1, 3]);
|
|
2766
|
-
* list.addAt(1, 2);
|
|
2767
|
-
* console.log(list.toArray()); // [1, 2, 3];
|
|
2768
|
-
*/
|
|
1857
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1858
|
+
* @remarks Time O(N), Space O(1)
|
|
1859
|
+
* @param index - Zero-based index.
|
|
1860
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1861
|
+
* @returns True if inserted.
|
|
1862
|
+
* @example
|
|
1863
|
+
* // Insert at position
|
|
1864
|
+
* const list = new DoublyLinkedList<number>([1, 3]);
|
|
1865
|
+
* list.addAt(1, 2);
|
|
1866
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1867
|
+
*/
|
|
2769
1868
|
addAt(index, newElementOrNode) {
|
|
2770
1869
|
if (index < 0 || index > this._length) return false;
|
|
2771
1870
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -2832,55 +1931,16 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2832
1931
|
return true;
|
|
2833
1932
|
}
|
|
2834
1933
|
/**
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
* @example
|
|
2879
|
-
* // Remove by index
|
|
2880
|
-
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
2881
|
-
* list.deleteAt(1);
|
|
2882
|
-
* console.log(list.toArray()); // ['a', 'c'];
|
|
2883
|
-
*/
|
|
1934
|
+
* Delete the element at an index.
|
|
1935
|
+
* @remarks Time O(N), Space O(1)
|
|
1936
|
+
* @param index - Zero-based index.
|
|
1937
|
+
* @returns Removed element or undefined.
|
|
1938
|
+
* @example
|
|
1939
|
+
* // Remove by index
|
|
1940
|
+
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
1941
|
+
* list.deleteAt(1);
|
|
1942
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1943
|
+
*/
|
|
2884
1944
|
deleteAt(index) {
|
|
2885
1945
|
if (index < 0 || index >= this._length) return;
|
|
2886
1946
|
if (index === 0) return this.shift();
|
|
@@ -2894,55 +1954,16 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2894
1954
|
return removedNode.value;
|
|
2895
1955
|
}
|
|
2896
1956
|
/**
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
* @example
|
|
2941
|
-
* // Remove first occurrence
|
|
2942
|
-
* const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
|
|
2943
|
-
* list.delete(2);
|
|
2944
|
-
* console.log(list.toArray()); // [1, 3, 2];
|
|
2945
|
-
*/
|
|
1957
|
+
* Delete the first match by value/node.
|
|
1958
|
+
* @remarks Time O(N), Space O(1)
|
|
1959
|
+
* @param [elementOrNode] - Element or node to remove.
|
|
1960
|
+
* @returns True if removed.
|
|
1961
|
+
* @example
|
|
1962
|
+
* // Remove first occurrence
|
|
1963
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
|
|
1964
|
+
* list.delete(2);
|
|
1965
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1966
|
+
*/
|
|
2946
1967
|
delete(elementOrNode) {
|
|
2947
1968
|
const node = this.getNode(elementOrNode);
|
|
2948
1969
|
if (!node) return false;
|
|
@@ -2958,161 +1979,42 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
2958
1979
|
return true;
|
|
2959
1980
|
}
|
|
2960
1981
|
/**
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
* @example
|
|
3005
|
-
* // Check empty
|
|
3006
|
-
* console.log(new DoublyLinkedList().isEmpty()); // true;
|
|
3007
|
-
*/
|
|
1982
|
+
* Check whether the list is empty.
|
|
1983
|
+
* @remarks Time O(1), Space O(1)
|
|
1984
|
+
* @returns True if length is 0.
|
|
1985
|
+
* @example
|
|
1986
|
+
* // Check empty
|
|
1987
|
+
* console.log(new DoublyLinkedList().isEmpty()); // true;
|
|
1988
|
+
*/
|
|
3008
1989
|
isEmpty() {
|
|
3009
1990
|
return this._length === 0;
|
|
3010
1991
|
}
|
|
3011
1992
|
/**
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
* @example
|
|
3056
|
-
* // Remove all
|
|
3057
|
-
* const list = new DoublyLinkedList<number>([1, 2]);
|
|
3058
|
-
* list.clear();
|
|
3059
|
-
* console.log(list.isEmpty()); // true;
|
|
3060
|
-
*/
|
|
1993
|
+
* Remove all nodes and reset length.
|
|
1994
|
+
* @remarks Time O(N), Space O(1)
|
|
1995
|
+
* @returns void
|
|
1996
|
+
* @example
|
|
1997
|
+
* // Remove all
|
|
1998
|
+
* const list = new DoublyLinkedList<number>([1, 2]);
|
|
1999
|
+
* list.clear();
|
|
2000
|
+
* console.log(list.isEmpty()); // true;
|
|
2001
|
+
*/
|
|
3061
2002
|
clear() {
|
|
3062
2003
|
this._head = void 0;
|
|
3063
2004
|
this._tail = void 0;
|
|
3064
2005
|
this._length = 0;
|
|
3065
2006
|
}
|
|
3066
2007
|
/**
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
* @example
|
|
3111
|
-
* // Search with predicate
|
|
3112
|
-
* const list = new DoublyLinkedList<number>([10, 20, 30]);
|
|
3113
|
-
* const found = list.search(node => node.value > 15);
|
|
3114
|
-
* console.log(found); // 20;
|
|
3115
|
-
*/
|
|
2008
|
+
* Find the first value matching a predicate scanning forward.
|
|
2009
|
+
* @remarks Time O(N), Space O(1)
|
|
2010
|
+
* @param elementNodeOrPredicate - Element, node, or predicate to match.
|
|
2011
|
+
* @returns Matched value or undefined.
|
|
2012
|
+
* @example
|
|
2013
|
+
* // Search with predicate
|
|
2014
|
+
* const list = new DoublyLinkedList<number>([10, 20, 30]);
|
|
2015
|
+
* const found = list.search(node => node.value > 15);
|
|
2016
|
+
* console.log(found); // 20;
|
|
2017
|
+
*/
|
|
3116
2018
|
search(elementNodeOrPredicate) {
|
|
3117
2019
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
3118
2020
|
let current = this.head;
|
|
@@ -3123,53 +2025,17 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
3123
2025
|
return void 0;
|
|
3124
2026
|
}
|
|
3125
2027
|
/**
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
* @example
|
|
3167
|
-
* // Find value scanning from tail
|
|
3168
|
-
* const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
|
|
3169
|
-
* // findLast scans from tail to head, returns first match
|
|
3170
|
-
* const found = list.findLast(node => node.value < 4);
|
|
3171
|
-
* console.log(found); // 3;
|
|
3172
|
-
*/
|
|
2028
|
+
* Find the first value matching a predicate scanning backward.
|
|
2029
|
+
* @remarks Time O(N), Space O(1)
|
|
2030
|
+
* @param elementNodeOrPredicate - Element, node, or predicate to match.
|
|
2031
|
+
* @returns Matched value or undefined.
|
|
2032
|
+
* @example
|
|
2033
|
+
* // Find value scanning from tail
|
|
2034
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
|
|
2035
|
+
* // findLast scans from tail to head, returns first match
|
|
2036
|
+
* const found = list.findLast(node => node.value < 4);
|
|
2037
|
+
* console.log(found); // 3;
|
|
2038
|
+
*/
|
|
3173
2039
|
/**
|
|
3174
2040
|
* @deprecated Use `findLast` instead. Will be removed in a future major version.
|
|
3175
2041
|
*/
|
|
@@ -3177,19 +2043,17 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
3177
2043
|
return this.findLast(elementNodeOrPredicate);
|
|
3178
2044
|
}
|
|
3179
2045
|
/**
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
* console.log(found); // 3;
|
|
3192
|
-
*/
|
|
2046
|
+
* Find the first value matching a predicate scanning backward (tail → head).
|
|
2047
|
+
* @remarks Time O(N), Space O(1)
|
|
2048
|
+
* @param elementNodeOrPredicate - Element, node, or predicate to match.
|
|
2049
|
+
* @returns Matching value or undefined.
|
|
2050
|
+
* @example
|
|
2051
|
+
* // Find value scanning from tail
|
|
2052
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
|
|
2053
|
+
* // findLast scans from tail to head, returns first match
|
|
2054
|
+
* const found = list.findLast(node => node.value < 4);
|
|
2055
|
+
* console.log(found); // 3;
|
|
2056
|
+
*/
|
|
3193
2057
|
findLast(elementNodeOrPredicate) {
|
|
3194
2058
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
3195
2059
|
let current = this.tail;
|
|
@@ -3216,57 +2080,15 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
3216
2080
|
return -1;
|
|
3217
2081
|
}
|
|
3218
2082
|
/**
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
* @example
|
|
3265
|
-
* // Reverse in-place
|
|
3266
|
-
* const list = new DoublyLinkedList<number>([1, 2, 3]);
|
|
3267
|
-
* list.reverse();
|
|
3268
|
-
* console.log([...list]); // [3, 2, 1];
|
|
3269
|
-
*/
|
|
2083
|
+
* Reverse the list in place.
|
|
2084
|
+
* @remarks Time O(N), Space O(1)
|
|
2085
|
+
* @returns This list.
|
|
2086
|
+
* @example
|
|
2087
|
+
* // Reverse in-place
|
|
2088
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3]);
|
|
2089
|
+
* list.reverse();
|
|
2090
|
+
* console.log([...list]); // [3, 2, 1];
|
|
2091
|
+
*/
|
|
3270
2092
|
reverse() {
|
|
3271
2093
|
let current = this.head;
|
|
3272
2094
|
[this._head, this._tail] = [this.tail, this.head];
|
|
@@ -3307,115 +2129,33 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
3307
2129
|
return this;
|
|
3308
2130
|
}
|
|
3309
2131
|
/**
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
* @example
|
|
3354
|
-
* // Deep copy
|
|
3355
|
-
* const list = new DoublyLinkedList<number>([1, 2, 3]);
|
|
3356
|
-
* const copy = list.clone();
|
|
3357
|
-
* copy.pop();
|
|
3358
|
-
* console.log(list.length); // 3;
|
|
3359
|
-
*/
|
|
2132
|
+
* Deep clone this list (values are copied by reference).
|
|
2133
|
+
* @remarks Time O(N), Space O(N)
|
|
2134
|
+
* @returns A new list with the same element sequence.
|
|
2135
|
+
* @example
|
|
2136
|
+
* // Deep copy
|
|
2137
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3]);
|
|
2138
|
+
* const copy = list.clone();
|
|
2139
|
+
* copy.pop();
|
|
2140
|
+
* console.log(list.length); // 3;
|
|
2141
|
+
*/
|
|
3360
2142
|
clone() {
|
|
3361
2143
|
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
3362
2144
|
for (const v of this) out.push(v);
|
|
3363
2145
|
return out;
|
|
3364
2146
|
}
|
|
3365
2147
|
/**
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
* @example
|
|
3414
|
-
* // Filter elements
|
|
3415
|
-
* const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
3416
|
-
* const evens = list.filter(n => n % 2 === 0);
|
|
3417
|
-
* console.log([...evens]); // [2, 4];
|
|
3418
|
-
*/
|
|
2148
|
+
* Filter values into a new list of the same class.
|
|
2149
|
+
* @remarks Time O(N), Space O(N)
|
|
2150
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
2151
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2152
|
+
* @returns A new list with kept values.
|
|
2153
|
+
* @example
|
|
2154
|
+
* // Filter elements
|
|
2155
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
2156
|
+
* const evens = list.filter(n => n % 2 === 0);
|
|
2157
|
+
* console.log([...evens]); // [2, 4];
|
|
2158
|
+
*/
|
|
3419
2159
|
filter(callback, thisArg) {
|
|
3420
2160
|
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
3421
2161
|
let index = 0;
|
|
@@ -3439,77 +2179,36 @@ var DoublyLinkedList = class extends LinearLinkedBase {
|
|
|
3439
2179
|
return out;
|
|
3440
2180
|
}
|
|
3441
2181
|
/**
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
* @example
|
|
3493
|
-
* // DoublyLinkedList for...of iteration and map operation
|
|
3494
|
-
* const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
3495
|
-
*
|
|
3496
|
-
* // Iterate through list
|
|
3497
|
-
* const doubled = list.map(value => value * 2);
|
|
3498
|
-
* console.log(doubled.length); // 5;
|
|
3499
|
-
*
|
|
3500
|
-
* // Use for...of loop
|
|
3501
|
-
* const result: number[] = [];
|
|
3502
|
-
* for (const item of list) {
|
|
3503
|
-
* result.push(item);
|
|
3504
|
-
* }
|
|
3505
|
-
* console.log(result); // [1, 2, 3, 4, 5];
|
|
3506
|
-
*/
|
|
2182
|
+
* Map values into a new list (possibly different element type).
|
|
2183
|
+
* @remarks Time O(N), Space O(N)
|
|
2184
|
+
* @template EM
|
|
2185
|
+
* @template RM
|
|
2186
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
2187
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
2188
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
2189
|
+
* @returns A new DoublyLinkedList with mapped values.
|
|
2190
|
+
* @example
|
|
2191
|
+
* // DoublyLinkedList for...of iteration and map operation
|
|
2192
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
2193
|
+
*
|
|
2194
|
+
* // Iterate through list
|
|
2195
|
+
* const doubled = list.map(value => value * 2);
|
|
2196
|
+
* console.log(doubled.length); // 5;
|
|
2197
|
+
*
|
|
2198
|
+
* // Use for...of loop
|
|
2199
|
+
* const result: number[] = [];
|
|
2200
|
+
* for (const item of list) {
|
|
2201
|
+
* result.push(item);
|
|
2202
|
+
* }
|
|
2203
|
+
* console.log(result); // [1, 2, 3, 4, 5];
|
|
2204
|
+
*/
|
|
3507
2205
|
map(callback, options, thisArg) {
|
|
3508
2206
|
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
3509
2207
|
let index = 0;
|
|
3510
2208
|
for (const v of this) out.push(callback.call(thisArg, v, index++, this));
|
|
3511
2209
|
return out;
|
|
3512
2210
|
}
|
|
2211
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
3513
2212
|
/**
|
|
3514
2213
|
* (Protected) Create or return a node for the given input (node or raw element).
|
|
3515
2214
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3792,6 +2491,9 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
3792
2491
|
static {
|
|
3793
2492
|
__name(this, "SkipList");
|
|
3794
2493
|
}
|
|
2494
|
+
// ─── Internal state ──────────────────────────────────────────
|
|
2495
|
+
_head;
|
|
2496
|
+
_level = 0;
|
|
3795
2497
|
#comparator;
|
|
3796
2498
|
#isDefaultComparator;
|
|
3797
2499
|
constructor(entries = [], options = {}) {
|
|
@@ -3816,6 +2518,22 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
3816
2518
|
this.set(k, v);
|
|
3817
2519
|
}
|
|
3818
2520
|
}
|
|
2521
|
+
_size = 0;
|
|
2522
|
+
// ─── Size & lifecycle ────────────────────────────────────────
|
|
2523
|
+
get size() {
|
|
2524
|
+
return this._size;
|
|
2525
|
+
}
|
|
2526
|
+
_maxLevel = 16;
|
|
2527
|
+
get maxLevel() {
|
|
2528
|
+
return this._maxLevel;
|
|
2529
|
+
}
|
|
2530
|
+
_probability = 0.5;
|
|
2531
|
+
get probability() {
|
|
2532
|
+
return this._probability;
|
|
2533
|
+
}
|
|
2534
|
+
get comparator() {
|
|
2535
|
+
return this.#comparator;
|
|
2536
|
+
}
|
|
3819
2537
|
/**
|
|
3820
2538
|
* Creates a default comparator supporting number, string, Date, and bigint.
|
|
3821
2539
|
*/
|
|
@@ -3839,174 +2557,44 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
3839
2557
|
raise(TypeError, ERR.comparatorRequired("SkipList"));
|
|
3840
2558
|
};
|
|
3841
2559
|
}
|
|
3842
|
-
// ─── Internal state ──────────────────────────────────────────
|
|
3843
|
-
_head;
|
|
3844
|
-
_level = 0;
|
|
3845
|
-
_size = 0;
|
|
3846
|
-
_maxLevel = 16;
|
|
3847
|
-
_probability = 0.5;
|
|
3848
|
-
// ─── Size & lifecycle ────────────────────────────────────────
|
|
3849
|
-
get size() {
|
|
3850
|
-
return this._size;
|
|
3851
|
-
}
|
|
3852
|
-
get maxLevel() {
|
|
3853
|
-
return this._maxLevel;
|
|
3854
|
-
}
|
|
3855
|
-
get probability() {
|
|
3856
|
-
return this._probability;
|
|
3857
|
-
}
|
|
3858
|
-
get comparator() {
|
|
3859
|
-
return this.#comparator;
|
|
3860
|
-
}
|
|
3861
2560
|
/**
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
* @example
|
|
3903
|
-
* // Check if empty
|
|
3904
|
-
* const sl = new SkipList<number, string>();
|
|
3905
|
-
* console.log(sl.isEmpty()); // true;
|
|
3906
|
-
*/
|
|
2561
|
+
* Check if empty
|
|
2562
|
+
* @example
|
|
2563
|
+
* // Check if empty
|
|
2564
|
+
* const sl = new SkipList<number, string>();
|
|
2565
|
+
* console.log(sl.isEmpty()); // true;
|
|
2566
|
+
*/
|
|
3907
2567
|
isEmpty() {
|
|
3908
2568
|
return this._size === 0;
|
|
3909
2569
|
}
|
|
3910
2570
|
/**
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
* @example
|
|
3952
|
-
* // Remove all entries
|
|
3953
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
|
|
3954
|
-
* sl.clear();
|
|
3955
|
-
* console.log(sl.isEmpty()); // true;
|
|
3956
|
-
*/
|
|
2571
|
+
* Remove all entries
|
|
2572
|
+
* @example
|
|
2573
|
+
* // Remove all entries
|
|
2574
|
+
* const sl = new SkipList<number, string>([
|
|
2575
|
+
* [1, 'a'],
|
|
2576
|
+
* [2, 'b']
|
|
2577
|
+
* ]);
|
|
2578
|
+
* sl.clear();
|
|
2579
|
+
* console.log(sl.isEmpty()); // true;
|
|
2580
|
+
*/
|
|
3957
2581
|
clear() {
|
|
3958
2582
|
this._head = new SkipListNode(void 0, void 0, this._maxLevel);
|
|
3959
2583
|
this._level = 0;
|
|
3960
2584
|
this._size = 0;
|
|
3961
2585
|
}
|
|
3962
2586
|
/**
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
|
|
3989
|
-
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
* @example
|
|
4004
|
-
* // Create independent copy
|
|
4005
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
|
|
4006
|
-
* const copy = sl.clone();
|
|
4007
|
-
* copy.delete(1);
|
|
4008
|
-
* console.log(sl.has(1)); // true;
|
|
4009
|
-
*/
|
|
2587
|
+
* Create independent copy
|
|
2588
|
+
* @example
|
|
2589
|
+
* // Create independent copy
|
|
2590
|
+
* const sl = new SkipList<number, string>([
|
|
2591
|
+
* [1, 'a'],
|
|
2592
|
+
* [2, 'b']
|
|
2593
|
+
* ]);
|
|
2594
|
+
* const copy = sl.clone();
|
|
2595
|
+
* copy.delete(1);
|
|
2596
|
+
* console.log(sl.has(1)); // true;
|
|
2597
|
+
*/
|
|
4010
2598
|
clone() {
|
|
4011
2599
|
return new _SkipList(this, {
|
|
4012
2600
|
comparator: this.#isDefaultComparator ? void 0 : this.#comparator,
|
|
@@ -4016,67 +2604,25 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4016
2604
|
}
|
|
4017
2605
|
// ─── Core CRUD ───────────────────────────────────────────────
|
|
4018
2606
|
/**
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
* @example
|
|
4064
|
-
* // In-memory sorted key-value store
|
|
4065
|
-
* const store = new SkipList<number, string>();
|
|
4066
|
-
*
|
|
4067
|
-
* store.set(3, 'three');
|
|
4068
|
-
* store.set(1, 'one');
|
|
4069
|
-
* store.set(5, 'five');
|
|
4070
|
-
* store.set(2, 'two');
|
|
4071
|
-
*
|
|
4072
|
-
* console.log(store.get(3)); // 'three';
|
|
4073
|
-
* console.log(store.get(1)); // 'one';
|
|
4074
|
-
* console.log(store.get(5)); // 'five';
|
|
4075
|
-
*
|
|
4076
|
-
* // Update existing key
|
|
4077
|
-
* store.set(3, 'THREE');
|
|
4078
|
-
* console.log(store.get(3)); // 'THREE';
|
|
4079
|
-
*/
|
|
2607
|
+
* Insert or update a key-value pair. Returns `this` for chaining.
|
|
2608
|
+
* Unique keys only — if key exists, value is updated in place.
|
|
2609
|
+
* @example
|
|
2610
|
+
* // In-memory sorted key-value store
|
|
2611
|
+
* const store = new SkipList<number, string>();
|
|
2612
|
+
*
|
|
2613
|
+
* store.set(3, 'three');
|
|
2614
|
+
* store.set(1, 'one');
|
|
2615
|
+
* store.set(5, 'five');
|
|
2616
|
+
* store.set(2, 'two');
|
|
2617
|
+
*
|
|
2618
|
+
* console.log(store.get(3)); // 'three';
|
|
2619
|
+
* console.log(store.get(1)); // 'one';
|
|
2620
|
+
* console.log(store.get(5)); // 'five';
|
|
2621
|
+
*
|
|
2622
|
+
* // Update existing key
|
|
2623
|
+
* store.set(3, 'THREE');
|
|
2624
|
+
* console.log(store.get(3)); // 'THREE';
|
|
2625
|
+
*/
|
|
4080
2626
|
set(key, value) {
|
|
4081
2627
|
const cmp = this.#comparator;
|
|
4082
2628
|
const update = this._findUpdate(key);
|
|
@@ -4101,186 +2647,64 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4101
2647
|
return this;
|
|
4102
2648
|
}
|
|
4103
2649
|
/**
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
* @example
|
|
4149
|
-
* // Building a sorted index
|
|
4150
|
-
* type Product = { id: number; name: string; price: number };
|
|
4151
|
-
* const products: Product[] = [
|
|
4152
|
-
* { id: 1, name: 'Widget', price: 25 },
|
|
4153
|
-
* { id: 2, name: 'Gadget', price: 50 },
|
|
4154
|
-
* { id: 3, name: 'Doohickey', price: 15 }
|
|
4155
|
-
* ];
|
|
4156
|
-
*
|
|
4157
|
-
* const index = new SkipList<number, Product, Product>(products, {
|
|
4158
|
-
* toEntryFn: (p: Product) => [p.price, p]
|
|
4159
|
-
* });
|
|
4160
|
-
*
|
|
4161
|
-
* // Iterate in sorted order by price
|
|
4162
|
-
* const names = [...index.values()].map(p => p!.name);
|
|
4163
|
-
* console.log(names); // ['Doohickey', 'Widget', 'Gadget'];
|
|
4164
|
-
*
|
|
4165
|
-
* // Range search: products between $20 and $60
|
|
4166
|
-
* const range = index.rangeSearch([20, 60]);
|
|
4167
|
-
* console.log(range.map(([, p]) => p!.name)); // ['Widget', 'Gadget'];
|
|
4168
|
-
*/
|
|
2650
|
+
* Get the value for a key, or `undefined` if not found.
|
|
2651
|
+
* Overrides base O(n) with O(log n) skip-list search.
|
|
2652
|
+
* @example
|
|
2653
|
+
* // Building a sorted index
|
|
2654
|
+
* type Product = { id: number; name: string; price: number };
|
|
2655
|
+
* const products: Product[] = [
|
|
2656
|
+
* { id: 1, name: 'Widget', price: 25 },
|
|
2657
|
+
* { id: 2, name: 'Gadget', price: 50 },
|
|
2658
|
+
* { id: 3, name: 'Doohickey', price: 15 }
|
|
2659
|
+
* ];
|
|
2660
|
+
*
|
|
2661
|
+
* const index = new SkipList<number, Product, Product>(products, {
|
|
2662
|
+
* toEntryFn: (p: Product) => [p.price, p]
|
|
2663
|
+
* });
|
|
2664
|
+
*
|
|
2665
|
+
* // Iterate in sorted order by price
|
|
2666
|
+
* const names = [...index.values()].map(p => p!.name);
|
|
2667
|
+
* console.log(names); // ['Doohickey', 'Widget', 'Gadget'];
|
|
2668
|
+
*
|
|
2669
|
+
* // Range search: products between $20 and $60
|
|
2670
|
+
* const range = index.rangeSearch([20, 60]);
|
|
2671
|
+
* console.log(range.map(([, p]) => p!.name)); // ['Widget', 'Gadget'];
|
|
2672
|
+
*/
|
|
4169
2673
|
get(key) {
|
|
4170
2674
|
const node = this._findNode(key);
|
|
4171
2675
|
return node ? node.value : void 0;
|
|
4172
2676
|
}
|
|
4173
2677
|
/**
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
|
|
4179
|
-
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
* @example
|
|
4219
|
-
* // Check key existence
|
|
4220
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [3, 'c'], [5, 'e']]);
|
|
4221
|
-
* console.log(sl.has(3)); // true;
|
|
4222
|
-
* console.log(sl.has(4)); // false;
|
|
4223
|
-
*/
|
|
2678
|
+
* Check if a key exists.
|
|
2679
|
+
* Overrides base O(n) with O(log n) skip-list search.
|
|
2680
|
+
* @example
|
|
2681
|
+
* // Check key existence
|
|
2682
|
+
* const sl = new SkipList<number, string>([
|
|
2683
|
+
* [1, 'a'],
|
|
2684
|
+
* [3, 'c'],
|
|
2685
|
+
* [5, 'e']
|
|
2686
|
+
* ]);
|
|
2687
|
+
* console.log(sl.has(3)); // true;
|
|
2688
|
+
* console.log(sl.has(4)); // false;
|
|
2689
|
+
*/
|
|
4224
2690
|
has(key) {
|
|
4225
2691
|
return this._findNode(key) !== void 0;
|
|
4226
2692
|
}
|
|
4227
2693
|
/**
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
|
|
4236
|
-
|
|
4237
|
-
|
|
4238
|
-
|
|
4239
|
-
|
|
4240
|
-
|
|
4241
|
-
|
|
4242
|
-
|
|
4243
|
-
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
* @example
|
|
4272
|
-
* // Fast lookup with deletion
|
|
4273
|
-
* const cache = new SkipList<string, number>();
|
|
4274
|
-
*
|
|
4275
|
-
* cache.set('alpha', 1);
|
|
4276
|
-
* cache.set('beta', 2);
|
|
4277
|
-
* cache.set('gamma', 3);
|
|
4278
|
-
*
|
|
4279
|
-
* console.log(cache.has('beta')); // true;
|
|
4280
|
-
* cache.delete('beta');
|
|
4281
|
-
* console.log(cache.has('beta')); // false;
|
|
4282
|
-
* console.log(cache.size); // 2;
|
|
4283
|
-
*/
|
|
2694
|
+
* Delete a key. Returns `true` if the key was found and removed.
|
|
2695
|
+
* @example
|
|
2696
|
+
* // Fast lookup with deletion
|
|
2697
|
+
* const cache = new SkipList<string, number>();
|
|
2698
|
+
*
|
|
2699
|
+
* cache.set('alpha', 1);
|
|
2700
|
+
* cache.set('beta', 2);
|
|
2701
|
+
* cache.set('gamma', 3);
|
|
2702
|
+
*
|
|
2703
|
+
* console.log(cache.has('beta')); // true;
|
|
2704
|
+
* cache.delete('beta');
|
|
2705
|
+
* console.log(cache.has('beta')); // false;
|
|
2706
|
+
* console.log(cache.size); // 2;
|
|
2707
|
+
*/
|
|
4284
2708
|
delete(key) {
|
|
4285
2709
|
const cmp = this.#comparator;
|
|
4286
2710
|
const update = this._findUpdate(key);
|
|
@@ -4298,107 +2722,31 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4298
2722
|
}
|
|
4299
2723
|
// ─── Navigation ──────────────────────────────────────────────
|
|
4300
2724
|
/**
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
* @example
|
|
4345
|
-
* // Access the minimum entry
|
|
4346
|
-
* const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
|
|
4347
|
-
* console.log(sl.first()); // [1, 'a'];
|
|
4348
|
-
*/
|
|
2725
|
+
* Returns the first (smallest key) entry, or `undefined` if empty.
|
|
2726
|
+
* @example
|
|
2727
|
+
* // Access the minimum entry
|
|
2728
|
+
* const sl = new SkipList<number, string>([
|
|
2729
|
+
* [5, 'e'],
|
|
2730
|
+
* [1, 'a'],
|
|
2731
|
+
* [3, 'c']
|
|
2732
|
+
* ]);
|
|
2733
|
+
* console.log(sl.first()); // [1, 'a'];
|
|
2734
|
+
*/
|
|
4349
2735
|
first() {
|
|
4350
2736
|
const node = this._head.forward[0];
|
|
4351
2737
|
return node ? [node.key, node.value] : void 0;
|
|
4352
2738
|
}
|
|
4353
2739
|
/**
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
|
|
4380
|
-
|
|
4381
|
-
|
|
4382
|
-
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
* @example
|
|
4398
|
-
* // Access the maximum entry
|
|
4399
|
-
* const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
|
|
4400
|
-
* console.log(sl.last()); // [5, 'e'];
|
|
4401
|
-
*/
|
|
2740
|
+
* Returns the last (largest key) entry, or `undefined` if empty.
|
|
2741
|
+
* @example
|
|
2742
|
+
* // Access the maximum entry
|
|
2743
|
+
* const sl = new SkipList<number, string>([
|
|
2744
|
+
* [5, 'e'],
|
|
2745
|
+
* [1, 'a'],
|
|
2746
|
+
* [3, 'c']
|
|
2747
|
+
* ]);
|
|
2748
|
+
* console.log(sl.last()); // [5, 'e'];
|
|
2749
|
+
*/
|
|
4402
2750
|
last() {
|
|
4403
2751
|
let current = this._head;
|
|
4404
2752
|
for (let i = this._level - 1; i >= 0; i--) {
|
|
@@ -4409,52 +2757,17 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4409
2757
|
return current === this._head ? void 0 : [current.key, current.value];
|
|
4410
2758
|
}
|
|
4411
2759
|
/**
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
* @example
|
|
4453
|
-
* // Remove and return smallest
|
|
4454
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
4455
|
-
* console.log(sl.pollFirst()); // [1, 'a'];
|
|
4456
|
-
* console.log(sl.size); // 2;
|
|
4457
|
-
*/
|
|
2760
|
+
* Remove and return the first (smallest key) entry.
|
|
2761
|
+
* @example
|
|
2762
|
+
* // Remove and return smallest
|
|
2763
|
+
* const sl = new SkipList<number, string>([
|
|
2764
|
+
* [1, 'a'],
|
|
2765
|
+
* [2, 'b'],
|
|
2766
|
+
* [3, 'c']
|
|
2767
|
+
* ]);
|
|
2768
|
+
* console.log(sl.pollFirst()); // [1, 'a'];
|
|
2769
|
+
* console.log(sl.size); // 2;
|
|
2770
|
+
*/
|
|
4458
2771
|
pollFirst() {
|
|
4459
2772
|
const entry = this.first();
|
|
4460
2773
|
if (!entry) return void 0;
|
|
@@ -4462,52 +2775,17 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4462
2775
|
return entry;
|
|
4463
2776
|
}
|
|
4464
2777
|
/**
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
|
|
4490
|
-
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
|
|
4494
|
-
|
|
4495
|
-
|
|
4496
|
-
|
|
4497
|
-
|
|
4498
|
-
|
|
4499
|
-
|
|
4500
|
-
|
|
4501
|
-
|
|
4502
|
-
|
|
4503
|
-
|
|
4504
|
-
|
|
4505
|
-
* @example
|
|
4506
|
-
* // Remove and return largest
|
|
4507
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
4508
|
-
* console.log(sl.pollLast()); // [3, 'c'];
|
|
4509
|
-
* console.log(sl.size); // 2;
|
|
4510
|
-
*/
|
|
2778
|
+
* Remove and return the last (largest key) entry.
|
|
2779
|
+
* @example
|
|
2780
|
+
* // Remove and return largest
|
|
2781
|
+
* const sl = new SkipList<number, string>([
|
|
2782
|
+
* [1, 'a'],
|
|
2783
|
+
* [2, 'b'],
|
|
2784
|
+
* [3, 'c']
|
|
2785
|
+
* ]);
|
|
2786
|
+
* console.log(sl.pollLast()); // [3, 'c'];
|
|
2787
|
+
* console.log(sl.size); // 2;
|
|
2788
|
+
*/
|
|
4511
2789
|
pollLast() {
|
|
4512
2790
|
const entry = this.last();
|
|
4513
2791
|
if (!entry) return void 0;
|
|
@@ -4515,55 +2793,17 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4515
2793
|
return entry;
|
|
4516
2794
|
}
|
|
4517
2795
|
/**
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4542
|
-
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
* @example
|
|
4562
|
-
* // Least entry ≥ key
|
|
4563
|
-
* const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
|
|
4564
|
-
* console.log(sl.ceiling(15)); // [20, 'b'];
|
|
4565
|
-
* console.log(sl.ceiling(20)); // [20, 'b'];
|
|
4566
|
-
*/
|
|
2796
|
+
* Least entry ≥ key, or `undefined`.
|
|
2797
|
+
* @example
|
|
2798
|
+
* // Least entry ≥ key
|
|
2799
|
+
* const sl = new SkipList<number, string>([
|
|
2800
|
+
* [10, 'a'],
|
|
2801
|
+
* [20, 'b'],
|
|
2802
|
+
* [30, 'c']
|
|
2803
|
+
* ]);
|
|
2804
|
+
* console.log(sl.ceiling(15)); // [20, 'b'];
|
|
2805
|
+
* console.log(sl.ceiling(20)); // [20, 'b'];
|
|
2806
|
+
*/
|
|
4567
2807
|
ceiling(key) {
|
|
4568
2808
|
const cmp = this.#comparator;
|
|
4569
2809
|
let current = this._head;
|
|
@@ -4576,55 +2816,17 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4576
2816
|
return node ? [node.key, node.value] : void 0;
|
|
4577
2817
|
}
|
|
4578
2818
|
/**
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
|
|
4595
|
-
|
|
4596
|
-
|
|
4597
|
-
|
|
4598
|
-
|
|
4599
|
-
|
|
4600
|
-
|
|
4601
|
-
|
|
4602
|
-
|
|
4603
|
-
|
|
4604
|
-
|
|
4605
|
-
|
|
4606
|
-
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
|
|
4610
|
-
|
|
4611
|
-
|
|
4612
|
-
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
4616
|
-
|
|
4617
|
-
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
|
|
4621
|
-
|
|
4622
|
-
* @example
|
|
4623
|
-
* // Greatest entry ≤ key
|
|
4624
|
-
* const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
|
|
4625
|
-
* console.log(sl.floor(25)); // [20, 'b'];
|
|
4626
|
-
* console.log(sl.floor(5)); // undefined;
|
|
4627
|
-
*/
|
|
2819
|
+
* Greatest entry ≤ key, or `undefined`.
|
|
2820
|
+
* @example
|
|
2821
|
+
* // Greatest entry ≤ key
|
|
2822
|
+
* const sl = new SkipList<number, string>([
|
|
2823
|
+
* [10, 'a'],
|
|
2824
|
+
* [20, 'b'],
|
|
2825
|
+
* [30, 'c']
|
|
2826
|
+
* ]);
|
|
2827
|
+
* console.log(sl.floor(25)); // [20, 'b'];
|
|
2828
|
+
* console.log(sl.floor(5)); // undefined;
|
|
2829
|
+
*/
|
|
4628
2830
|
floor(key) {
|
|
4629
2831
|
const cmp = this.#comparator;
|
|
4630
2832
|
let current = this._head;
|
|
@@ -4638,52 +2840,17 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4638
2840
|
return void 0;
|
|
4639
2841
|
}
|
|
4640
2842
|
/**
|
|
4641
|
-
|
|
4642
|
-
|
|
4643
|
-
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
4670
|
-
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
|
|
4681
|
-
* @example
|
|
4682
|
-
* // Strictly greater entry
|
|
4683
|
-
* const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
|
|
4684
|
-
* console.log(sl.higher(15)); // [20, 'b'];
|
|
4685
|
-
* console.log(sl.higher(30)); // undefined;
|
|
4686
|
-
*/
|
|
2843
|
+
* Least entry strictly > key, or `undefined`.
|
|
2844
|
+
* @example
|
|
2845
|
+
* // Strictly greater entry
|
|
2846
|
+
* const sl = new SkipList<number, string>([
|
|
2847
|
+
* [10, 'a'],
|
|
2848
|
+
* [20, 'b'],
|
|
2849
|
+
* [30, 'c']
|
|
2850
|
+
* ]);
|
|
2851
|
+
* console.log(sl.higher(15)); // [20, 'b'];
|
|
2852
|
+
* console.log(sl.higher(30)); // undefined;
|
|
2853
|
+
*/
|
|
4687
2854
|
higher(key) {
|
|
4688
2855
|
const cmp = this.#comparator;
|
|
4689
2856
|
let current = this._head;
|
|
@@ -4696,52 +2863,17 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4696
2863
|
return node ? [node.key, node.value] : void 0;
|
|
4697
2864
|
}
|
|
4698
2865
|
/**
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4716
|
-
|
|
4717
|
-
|
|
4718
|
-
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
|
|
4724
|
-
|
|
4725
|
-
|
|
4726
|
-
|
|
4727
|
-
|
|
4728
|
-
|
|
4729
|
-
|
|
4730
|
-
|
|
4731
|
-
|
|
4732
|
-
|
|
4733
|
-
|
|
4734
|
-
|
|
4735
|
-
|
|
4736
|
-
|
|
4737
|
-
|
|
4738
|
-
|
|
4739
|
-
* @example
|
|
4740
|
-
* // Strictly less entry
|
|
4741
|
-
* const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
|
|
4742
|
-
* console.log(sl.lower(25)); // [20, 'b'];
|
|
4743
|
-
* console.log(sl.lower(10)); // undefined;
|
|
4744
|
-
*/
|
|
2866
|
+
* Greatest entry strictly < key, or `undefined`.
|
|
2867
|
+
* @example
|
|
2868
|
+
* // Strictly less entry
|
|
2869
|
+
* const sl = new SkipList<number, string>([
|
|
2870
|
+
* [10, 'a'],
|
|
2871
|
+
* [20, 'b'],
|
|
2872
|
+
* [30, 'c']
|
|
2873
|
+
* ]);
|
|
2874
|
+
* console.log(sl.lower(25)); // [20, 'b'];
|
|
2875
|
+
* console.log(sl.lower(10)); // undefined;
|
|
2876
|
+
*/
|
|
4745
2877
|
lower(key) {
|
|
4746
2878
|
const cmp = this.#comparator;
|
|
4747
2879
|
let current = this._head;
|
|
@@ -4757,55 +2889,23 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4757
2889
|
return result ? [result.key, result.value] : void 0;
|
|
4758
2890
|
}
|
|
4759
2891
|
/**
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
|
|
4766
|
-
|
|
4767
|
-
|
|
4768
|
-
|
|
4769
|
-
|
|
4770
|
-
|
|
4771
|
-
|
|
4772
|
-
|
|
4773
|
-
|
|
4774
|
-
|
|
4775
|
-
|
|
4776
|
-
|
|
4777
|
-
|
|
4778
|
-
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
|
|
4783
|
-
|
|
4784
|
-
|
|
4785
|
-
|
|
4786
|
-
|
|
4787
|
-
|
|
4788
|
-
|
|
4789
|
-
|
|
4790
|
-
|
|
4791
|
-
|
|
4792
|
-
|
|
4793
|
-
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
|
|
4798
|
-
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
* @example
|
|
4804
|
-
* // Find entries in a range
|
|
4805
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]);
|
|
4806
|
-
* const result = sl.rangeSearch([2, 4]);
|
|
4807
|
-
* console.log(result); // [[2, 'b'], [3, 'c'], [4, 'd']];
|
|
4808
|
-
*/
|
|
2892
|
+
* Returns entries within the given key range.
|
|
2893
|
+
* @example
|
|
2894
|
+
* // Find entries in a range
|
|
2895
|
+
* const sl = new SkipList<number, string>([
|
|
2896
|
+
* [1, 'a'],
|
|
2897
|
+
* [2, 'b'],
|
|
2898
|
+
* [3, 'c'],
|
|
2899
|
+
* [4, 'd'],
|
|
2900
|
+
* [5, 'e']
|
|
2901
|
+
* ]);
|
|
2902
|
+
* const result = sl.rangeSearch([2, 4]);
|
|
2903
|
+
* console.log(result); // [
|
|
2904
|
+
* // [2, 'b'],
|
|
2905
|
+
* // [3, 'c'],
|
|
2906
|
+
* // [4, 'd']
|
|
2907
|
+
* // ];
|
|
2908
|
+
*/
|
|
4809
2909
|
rangeSearch(range, options = {}) {
|
|
4810
2910
|
const { lowInclusive = true, highInclusive = true } = options;
|
|
4811
2911
|
const [low, high] = range;
|
|
@@ -4832,52 +2932,16 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4832
2932
|
}
|
|
4833
2933
|
// ─── Functional (overrides) ──────────────────────────────────
|
|
4834
2934
|
/**
|
|
4835
|
-
|
|
4836
|
-
|
|
4837
|
-
|
|
4838
|
-
|
|
4839
|
-
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4844
|
-
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4872
|
-
|
|
4873
|
-
|
|
4874
|
-
|
|
4875
|
-
* @example
|
|
4876
|
-
* // Transform entries
|
|
4877
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
|
|
4878
|
-
* const mapped = sl.map((v, k) => [k, v?.toUpperCase()] as [number, string]);
|
|
4879
|
-
* console.log([...mapped.values()]); // ['A', 'B'];
|
|
4880
|
-
*/
|
|
2935
|
+
* Creates a new SkipList with entries transformed by callback.
|
|
2936
|
+
* @example
|
|
2937
|
+
* // Transform entries
|
|
2938
|
+
* const sl = new SkipList<number, string>([
|
|
2939
|
+
* [1, 'a'],
|
|
2940
|
+
* [2, 'b']
|
|
2941
|
+
* ]);
|
|
2942
|
+
* const mapped = sl.map((v, k) => [k, v?.toUpperCase()] as [number, string]);
|
|
2943
|
+
* console.log([...mapped.values()]); // ['A', 'B'];
|
|
2944
|
+
*/
|
|
4881
2945
|
map(callback, options) {
|
|
4882
2946
|
const out = new _SkipList([], options ?? {});
|
|
4883
2947
|
let i = 0;
|
|
@@ -4888,52 +2952,17 @@ var SkipList = class _SkipList extends IterableEntryBase {
|
|
|
4888
2952
|
return out;
|
|
4889
2953
|
}
|
|
4890
2954
|
/**
|
|
4891
|
-
|
|
4892
|
-
|
|
4893
|
-
|
|
4894
|
-
|
|
4895
|
-
|
|
4896
|
-
|
|
4897
|
-
|
|
4898
|
-
|
|
4899
|
-
|
|
4900
|
-
|
|
4901
|
-
|
|
4902
|
-
|
|
4903
|
-
|
|
4904
|
-
|
|
4905
|
-
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4909
|
-
|
|
4910
|
-
|
|
4911
|
-
|
|
4912
|
-
|
|
4913
|
-
|
|
4914
|
-
|
|
4915
|
-
|
|
4916
|
-
|
|
4917
|
-
|
|
4918
|
-
|
|
4919
|
-
|
|
4920
|
-
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
4924
|
-
|
|
4925
|
-
|
|
4926
|
-
|
|
4927
|
-
|
|
4928
|
-
|
|
4929
|
-
|
|
4930
|
-
|
|
4931
|
-
* @example
|
|
4932
|
-
* // Filter entries
|
|
4933
|
-
* const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
4934
|
-
* const result = sl.filter((v, k) => k > 1);
|
|
4935
|
-
* console.log(result.size); // 2;
|
|
4936
|
-
*/
|
|
2955
|
+
* Creates a new SkipList with entries that pass the predicate.
|
|
2956
|
+
* @example
|
|
2957
|
+
* // Filter entries
|
|
2958
|
+
* const sl = new SkipList<number, string>([
|
|
2959
|
+
* [1, 'a'],
|
|
2960
|
+
* [2, 'b'],
|
|
2961
|
+
* [3, 'c']
|
|
2962
|
+
* ]);
|
|
2963
|
+
* const result = sl.filter((v, k) => k > 1);
|
|
2964
|
+
* console.log(result.size); // 2;
|
|
2965
|
+
*/
|
|
4937
2966
|
filter(callbackfn, thisArg) {
|
|
4938
2967
|
const out = new _SkipList([], {
|
|
4939
2968
|
comparator: this.#isDefaultComparator ? void 0 : this.#comparator,
|