avl-tree-typed 1.53.3 → 1.53.5
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.
|
@@ -335,8 +335,8 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
335
335
|
* cache.set('c', 3);
|
|
336
336
|
* cache.set('d', 4); // This will eliminate 'a'
|
|
337
337
|
*
|
|
338
|
-
* console.log(cache.get('a'))
|
|
339
|
-
*
|
|
338
|
+
* console.log(cache.get('a')); // undefined
|
|
339
|
+
* console.log(cache.get('b')); // 2
|
|
340
340
|
* console.log(cache.get('c')); // 3
|
|
341
341
|
* console.log(cache.get('d')); // 4
|
|
342
342
|
*
|
|
@@ -350,8 +350,8 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
350
350
|
* cache.set('d', 4); // This will eliminate 'b'
|
|
351
351
|
*
|
|
352
352
|
* console.log(cache.get('a')); // 1
|
|
353
|
-
* console.log(cache.get('b'))
|
|
354
|
-
*
|
|
353
|
+
* console.log(cache.get('b')); // undefined
|
|
354
|
+
* console.log(cache.get('c')); // 3
|
|
355
355
|
* console.log(cache.get('d')); // 4
|
|
356
356
|
*
|
|
357
357
|
* // Should support updating existing keys
|
|
@@ -367,8 +367,8 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
367
367
|
* cache.set('b', 2);
|
|
368
368
|
*
|
|
369
369
|
* console.log(cache.delete('a')); // true
|
|
370
|
-
* console.log(cache.get('a'))
|
|
371
|
-
*
|
|
370
|
+
* console.log(cache.get('a')); // undefined
|
|
371
|
+
* console.log(cache.size); // 1
|
|
372
372
|
*
|
|
373
373
|
* // Should support clearing cache
|
|
374
374
|
* cache.clear();
|
|
@@ -404,20 +404,20 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
404
404
|
* // Test different scenarios of lyric synchronization
|
|
405
405
|
*
|
|
406
406
|
* // 1. Find lyric at exact timestamp
|
|
407
|
-
* const exactTimeLyric = lyricsList.
|
|
407
|
+
* const exactTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 36000);
|
|
408
408
|
* console.log(exactTimeLyric?.text); // 'And ignite your bones'
|
|
409
409
|
*
|
|
410
410
|
* // 2. Find lyric between timestamps
|
|
411
|
-
* const betweenTimeLyric = lyricsList.
|
|
411
|
+
* const betweenTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 22000);
|
|
412
412
|
* console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
|
|
413
413
|
*
|
|
414
414
|
* // 3. Find first lyric when timestamp is less than first entry
|
|
415
|
-
* const earlyTimeLyric = lyricsList.
|
|
416
|
-
* console.log(earlyTimeLyric)
|
|
415
|
+
* const earlyTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= -1000);
|
|
416
|
+
* console.log(earlyTimeLyric); // undefined
|
|
417
417
|
*
|
|
418
418
|
* // 4. Find last lyric when timestamp is after last entry
|
|
419
|
-
* const lateTimeLyric = lyricsList.
|
|
420
|
-
*
|
|
419
|
+
* const lateTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 50000);
|
|
420
|
+
* console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
421
421
|
* @example
|
|
422
422
|
* // cpu process schedules
|
|
423
423
|
* class Process {
|
|
@@ -556,28 +556,33 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
556
556
|
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.value;
|
|
557
557
|
}
|
|
558
558
|
/**
|
|
559
|
-
* Time Complexity: O(
|
|
560
|
-
* Space Complexity: O(
|
|
559
|
+
* Time Complexity: O(1)
|
|
560
|
+
* Space Complexity: O(1)
|
|
561
561
|
*
|
|
562
|
-
* The `
|
|
563
|
-
*
|
|
564
|
-
* @param {E
|
|
565
|
-
*
|
|
562
|
+
* The function `isNode` in TypeScript checks if a given input is an instance of
|
|
563
|
+
* `DoublyLinkedListNode`.
|
|
564
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
565
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
|
|
566
|
+
* be one of the following types:
|
|
567
|
+
* @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
|
|
568
|
+
* instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
|
|
569
|
+
* parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
|
|
570
|
+
* the function returns `false`.
|
|
566
571
|
*/
|
|
567
|
-
|
|
568
|
-
return
|
|
572
|
+
isNode(elementNodeOrPredicate) {
|
|
573
|
+
return elementNodeOrPredicate instanceof DoublyLinkedListNode;
|
|
569
574
|
}
|
|
570
575
|
/**
|
|
571
576
|
* Time Complexity: O(1)
|
|
572
577
|
* Space Complexity: O(1)
|
|
573
578
|
*
|
|
574
|
-
* The push function adds a new element to the end of a doubly linked list.
|
|
575
|
-
* @param {E}
|
|
576
|
-
*
|
|
577
|
-
* @returns The `push` method is returning a boolean value, `true`.
|
|
579
|
+
* The `push` function adds a new element or node to the end of a doubly linked list.
|
|
580
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
|
|
581
|
+
* method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
582
|
+
* @returns The `push` method is returning a boolean value, specifically `true`.
|
|
578
583
|
*/
|
|
579
|
-
push(
|
|
580
|
-
const newNode =
|
|
584
|
+
push(elementOrNode) {
|
|
585
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
581
586
|
if (!this.head) {
|
|
582
587
|
this._head = newNode;
|
|
583
588
|
this._tail = newNode;
|
|
@@ -638,13 +643,14 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
638
643
|
* Time Complexity: O(1)
|
|
639
644
|
* Space Complexity: O(1)
|
|
640
645
|
*
|
|
641
|
-
* The unshift function adds a new element to the beginning of a doubly linked list.
|
|
642
|
-
* @param {E}
|
|
643
|
-
*
|
|
644
|
-
*
|
|
646
|
+
* The unshift function adds a new element or node to the beginning of a doubly linked list.
|
|
647
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
648
|
+
* `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
|
|
649
|
+
* element of type `E`.
|
|
650
|
+
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
645
651
|
*/
|
|
646
|
-
unshift(
|
|
647
|
-
const newNode =
|
|
652
|
+
unshift(elementOrNode) {
|
|
653
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
648
654
|
if (!this.head) {
|
|
649
655
|
this._head = newNode;
|
|
650
656
|
this._tail = newNode;
|
|
@@ -700,16 +706,27 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
700
706
|
* Time Complexity: O(n)
|
|
701
707
|
* Space Complexity: O(1)
|
|
702
708
|
*
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
* @param {E
|
|
706
|
-
*
|
|
707
|
-
*
|
|
709
|
+
* This TypeScript function searches for a node in a doubly linked list based on a given element node
|
|
710
|
+
* or predicate.
|
|
711
|
+
* @param {| E
|
|
712
|
+
* | DoublyLinkedListNode<E>
|
|
713
|
+
* | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
714
|
+
* | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
|
|
715
|
+
* node in a doubly linked list based on a given element, node, or predicate function. The
|
|
716
|
+
* `elementNodeOrPredicate` parameter can be one of the following:
|
|
717
|
+
* @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
|
|
718
|
+
* input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
|
|
719
|
+
* Otherwise, it iterates through the linked list starting from the head node and applies the
|
|
720
|
+
* provided predicate function to each node. If a node satisfies the predicate, that node is
|
|
721
|
+
* returned. If
|
|
708
722
|
*/
|
|
709
|
-
getNode(
|
|
723
|
+
getNode(elementNodeOrPredicate) {
|
|
724
|
+
if (elementNodeOrPredicate === undefined)
|
|
725
|
+
return;
|
|
726
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
710
727
|
let current = this.head;
|
|
711
728
|
while (current) {
|
|
712
|
-
if (current
|
|
729
|
+
if (predicate(current)) {
|
|
713
730
|
return current;
|
|
714
731
|
}
|
|
715
732
|
current = current.next;
|
|
@@ -720,26 +737,28 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
720
737
|
* Time Complexity: O(n)
|
|
721
738
|
* Space Complexity: O(1)
|
|
722
739
|
*
|
|
723
|
-
* The `
|
|
724
|
-
* @param {number} index - The index parameter
|
|
725
|
-
*
|
|
726
|
-
*
|
|
727
|
-
*
|
|
728
|
-
*
|
|
729
|
-
* if the
|
|
740
|
+
* The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
|
|
741
|
+
* @param {number} index - The `index` parameter in the `addAt` method represents the position at
|
|
742
|
+
* which you want to add a new element or node in the doubly linked list. It indicates the location
|
|
743
|
+
* where the new element or node should be inserted.
|
|
744
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
745
|
+
* `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
746
|
+
* @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
|
|
747
|
+
* successfully added at the specified index, and `false` if the index is out of bounds (less than 0
|
|
748
|
+
* or greater than the size of the list).
|
|
730
749
|
*/
|
|
731
|
-
addAt(index,
|
|
750
|
+
addAt(index, newElementOrNode) {
|
|
732
751
|
if (index < 0 || index > this._size)
|
|
733
752
|
return false;
|
|
734
753
|
if (index === 0) {
|
|
735
|
-
this.unshift(
|
|
754
|
+
this.unshift(newElementOrNode);
|
|
736
755
|
return true;
|
|
737
756
|
}
|
|
738
757
|
if (index === this._size) {
|
|
739
|
-
this.push(
|
|
758
|
+
this.push(newElementOrNode);
|
|
740
759
|
return true;
|
|
741
760
|
}
|
|
742
|
-
const newNode =
|
|
761
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
743
762
|
const prevNode = this.getNodeAt(index - 1);
|
|
744
763
|
const nextNode = prevNode.next;
|
|
745
764
|
newNode.prev = prevNode;
|
|
@@ -753,25 +772,27 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
753
772
|
* Time Complexity: O(1) or O(n)
|
|
754
773
|
* Space Complexity: O(1)
|
|
755
774
|
*
|
|
756
|
-
* The `addBefore` function
|
|
757
|
-
*
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
* @param {E}
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
775
|
+
* The `addBefore` function in TypeScript adds a new element or node before an existing element or
|
|
776
|
+
* node in a doubly linked list.
|
|
777
|
+
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
|
|
778
|
+
* in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
|
|
779
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
|
|
780
|
+
* represents the element or node that you want to add before the `existingElementOrNode` in a doubly
|
|
781
|
+
* linked list.
|
|
782
|
+
* @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
|
|
783
|
+
* successfully added before the existing element or node, and `false` if the existing element or
|
|
784
|
+
* node was not found.
|
|
764
785
|
*/
|
|
765
|
-
addBefore(
|
|
786
|
+
addBefore(existingElementOrNode, newElementOrNode) {
|
|
766
787
|
let existingNode;
|
|
767
|
-
if (
|
|
768
|
-
existingNode =
|
|
788
|
+
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
789
|
+
existingNode = existingElementOrNode;
|
|
769
790
|
}
|
|
770
791
|
else {
|
|
771
|
-
existingNode = this.getNode(
|
|
792
|
+
existingNode = this.getNode(existingElementOrNode);
|
|
772
793
|
}
|
|
773
794
|
if (existingNode) {
|
|
774
|
-
const newNode =
|
|
795
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
775
796
|
newNode.prev = existingNode.prev;
|
|
776
797
|
if (existingNode.prev) {
|
|
777
798
|
existingNode.prev.next = newNode;
|
|
@@ -790,24 +811,28 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
790
811
|
* Time Complexity: O(1) or O(n)
|
|
791
812
|
* Space Complexity: O(1)
|
|
792
813
|
*
|
|
793
|
-
* The `addAfter` function
|
|
794
|
-
*
|
|
795
|
-
*
|
|
796
|
-
*
|
|
797
|
-
* @param {E}
|
|
798
|
-
*
|
|
799
|
-
*
|
|
814
|
+
* The `addAfter` function in TypeScript adds a new element or node after an existing element or node
|
|
815
|
+
* in a doubly linked list.
|
|
816
|
+
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
|
|
817
|
+
* element or node in the doubly linked list after which you want to add a new element or node.
|
|
818
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
819
|
+
* `addAfter` method represents the element or node that you want to add after the existing element
|
|
820
|
+
* or node in a doubly linked list. This parameter can be either an element value or a
|
|
821
|
+
* `DoublyLinkedListNode` object that you want to insert
|
|
822
|
+
* @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
|
|
823
|
+
* successfully added after the existing element or node, and `false` if the existing element or node
|
|
824
|
+
* was not found in the linked list.
|
|
800
825
|
*/
|
|
801
|
-
addAfter(
|
|
826
|
+
addAfter(existingElementOrNode, newElementOrNode) {
|
|
802
827
|
let existingNode;
|
|
803
|
-
if (
|
|
804
|
-
existingNode =
|
|
828
|
+
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
829
|
+
existingNode = existingElementOrNode;
|
|
805
830
|
}
|
|
806
831
|
else {
|
|
807
|
-
existingNode = this.getNode(
|
|
832
|
+
existingNode = this.getNode(existingElementOrNode);
|
|
808
833
|
}
|
|
809
834
|
if (existingNode) {
|
|
810
|
-
const newNode =
|
|
835
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
811
836
|
newNode.next = existingNode.next;
|
|
812
837
|
if (existingNode.next) {
|
|
813
838
|
existingNode.next.prev = newNode;
|
|
@@ -855,20 +880,17 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
855
880
|
* Time Complexity: O(1) or O(n)
|
|
856
881
|
* Space Complexity: O(1)
|
|
857
882
|
*
|
|
858
|
-
* The `delete` function removes a node from a doubly linked list
|
|
859
|
-
* @param {E | DoublyLinkedListNode<E>}
|
|
860
|
-
* a `DoublyLinkedListNode
|
|
861
|
-
*
|
|
862
|
-
*
|
|
883
|
+
* The `delete` function removes a specified element or node from a doubly linked list if it exists.
|
|
884
|
+
* @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
|
|
885
|
+
* the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
|
|
886
|
+
* can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
|
|
887
|
+
* doubly linked list
|
|
888
|
+
* @returns The `delete` method returns a boolean value - `true` if the element or node was
|
|
889
|
+
* successfully deleted from the doubly linked list, and `false` if the element or node was not found
|
|
890
|
+
* in the list.
|
|
863
891
|
*/
|
|
864
|
-
delete(
|
|
865
|
-
|
|
866
|
-
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
867
|
-
node = valOrNode;
|
|
868
|
-
}
|
|
869
|
-
else {
|
|
870
|
-
node = this.getNode(valOrNode);
|
|
871
|
-
}
|
|
892
|
+
delete(elementOrNode) {
|
|
893
|
+
const node = this.getNode(elementOrNode);
|
|
872
894
|
if (node) {
|
|
873
895
|
if (node === this.head) {
|
|
874
896
|
this.shift();
|
|
@@ -914,17 +936,21 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
914
936
|
* Time Complexity: O(n)
|
|
915
937
|
* Space Complexity: O(1)
|
|
916
938
|
*
|
|
917
|
-
* The function returns the index of
|
|
918
|
-
*
|
|
919
|
-
*
|
|
920
|
-
*
|
|
921
|
-
*
|
|
939
|
+
* The indexOf function in TypeScript returns the index of a specified element or node in a Doubly
|
|
940
|
+
* Linked List.
|
|
941
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
942
|
+
* `indexOf` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
|
|
943
|
+
* element of type `E`.
|
|
944
|
+
* @returns The `indexOf` method is returning the index of the element or node in the doubly linked
|
|
945
|
+
* list. If the element or node is found in the list, the method returns the index of that element or
|
|
946
|
+
* node. If the element or node is not found in the list, the method returns -1.
|
|
922
947
|
*/
|
|
923
|
-
indexOf(
|
|
948
|
+
indexOf(elementOrNode) {
|
|
949
|
+
const predicate = this._ensurePredicate(elementOrNode);
|
|
924
950
|
let index = 0;
|
|
925
951
|
let current = this.head;
|
|
926
952
|
while (current) {
|
|
927
|
-
if (current
|
|
953
|
+
if (predicate(current)) {
|
|
928
954
|
return index;
|
|
929
955
|
}
|
|
930
956
|
index++;
|
|
@@ -936,19 +962,45 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
936
962
|
* Time Complexity: O(n)
|
|
937
963
|
* Space Complexity: O(1)
|
|
938
964
|
*
|
|
939
|
-
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
940
|
-
* value that satisfies the given callback function, or undefined if no value satisfies the callback.
|
|
941
|
-
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
942
|
-
* function is used to determine whether a given value satisfies a certain condition.
|
|
943
|
-
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
944
|
-
* the callback function. If no value satisfies the condition, it returns `undefined`.
|
|
945
965
|
*/
|
|
946
|
-
|
|
966
|
+
/**
|
|
967
|
+
* This function retrieves an element from a doubly linked list based on a given element
|
|
968
|
+
* node or predicate.
|
|
969
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
970
|
+
* elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
|
|
971
|
+
* which can be one of the following types:
|
|
972
|
+
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
973
|
+
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
974
|
+
*/
|
|
975
|
+
get(elementNodeOrPredicate) {
|
|
976
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
977
|
+
let current = this.head;
|
|
978
|
+
while (current) {
|
|
979
|
+
if (predicate(current))
|
|
980
|
+
return current.value;
|
|
981
|
+
current = current.next;
|
|
982
|
+
}
|
|
983
|
+
return undefined;
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Time Complexity: O(n)
|
|
987
|
+
* Space Complexity: O(1)
|
|
988
|
+
*
|
|
989
|
+
* The `getBackward` function searches for a specific element in a doubly linked list starting from
|
|
990
|
+
* the tail and moving backwards.
|
|
991
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
992
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
|
|
993
|
+
* function can be one of the following types:
|
|
994
|
+
* @returns The `getBackward` method returns the value of the element node that matches the provided
|
|
995
|
+
* predicate when traversing the doubly linked list backwards. If no matching element is found, it
|
|
996
|
+
* returns `undefined`.
|
|
997
|
+
*/
|
|
998
|
+
getBackward(elementNodeOrPredicate) {
|
|
999
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
947
1000
|
let current = this.tail;
|
|
948
1001
|
while (current) {
|
|
949
|
-
if (
|
|
1002
|
+
if (predicate(current))
|
|
950
1003
|
return current.value;
|
|
951
|
-
}
|
|
952
1004
|
current = current.prev;
|
|
953
1005
|
}
|
|
954
1006
|
return undefined;
|
|
@@ -1070,6 +1122,18 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1070
1122
|
}
|
|
1071
1123
|
return mappedList;
|
|
1072
1124
|
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Time Complexity: O(n)
|
|
1127
|
+
* Space Complexity: O(n)
|
|
1128
|
+
*
|
|
1129
|
+
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
1130
|
+
* given array.
|
|
1131
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
1132
|
+
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
1133
|
+
*/
|
|
1134
|
+
static fromArray(data) {
|
|
1135
|
+
return new DoublyLinkedList(data);
|
|
1136
|
+
}
|
|
1073
1137
|
/**
|
|
1074
1138
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
1075
1139
|
*/
|
|
@@ -1080,5 +1144,47 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1080
1144
|
current = current.next;
|
|
1081
1145
|
}
|
|
1082
1146
|
}
|
|
1147
|
+
/**
|
|
1148
|
+
* The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
|
|
1149
|
+
* as an argument and returns a boolean.
|
|
1150
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1151
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
1152
|
+
* types:
|
|
1153
|
+
* @returns The _isPredicate method is returning a boolean value indicating whether the
|
|
1154
|
+
* elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
|
|
1155
|
+
* function, the method will return true, indicating that it is a predicate function.
|
|
1156
|
+
*/
|
|
1157
|
+
_isPredicate(elementNodeOrPredicate) {
|
|
1158
|
+
return typeof elementNodeOrPredicate === 'function';
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
|
|
1162
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
|
|
1163
|
+
* an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
|
|
1164
|
+
* @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
|
|
1165
|
+
* as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
|
|
1166
|
+
* value and returned.
|
|
1167
|
+
*/
|
|
1168
|
+
_ensureNode(elementOrNode) {
|
|
1169
|
+
if (this.isNode(elementOrNode))
|
|
1170
|
+
return elementOrNode;
|
|
1171
|
+
return new DoublyLinkedListNode(elementOrNode);
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
|
|
1175
|
+
* function, or a value to compare with the node's value.
|
|
1176
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1177
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
1178
|
+
* types:
|
|
1179
|
+
* @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
|
|
1180
|
+
* returns a boolean value based on the conditions specified in the code.
|
|
1181
|
+
*/
|
|
1182
|
+
_ensurePredicate(elementNodeOrPredicate) {
|
|
1183
|
+
if (this.isNode(elementNodeOrPredicate))
|
|
1184
|
+
return (node) => node === elementNodeOrPredicate;
|
|
1185
|
+
if (this._isPredicate(elementNodeOrPredicate))
|
|
1186
|
+
return elementNodeOrPredicate;
|
|
1187
|
+
return (node) => node.value === elementNodeOrPredicate;
|
|
1188
|
+
}
|
|
1083
1189
|
}
|
|
1084
1190
|
exports.DoublyLinkedList = DoublyLinkedList;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "avl-tree-typed",
|
|
3
|
-
"version": "1.53.
|
|
3
|
+
"version": "1.53.5",
|
|
4
4
|
"description": "AVLTree(Adelson-Velsky and Landis Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -163,6 +163,6 @@
|
|
|
163
163
|
"typescript": "^4.9.5"
|
|
164
164
|
},
|
|
165
165
|
"dependencies": {
|
|
166
|
-
"data-structure-typed": "^1.53.
|
|
166
|
+
"data-structure-typed": "^1.53.5"
|
|
167
167
|
}
|
|
168
168
|
}
|