avl-tree-typed 1.53.4 → 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.
@@ -395,19 +395,19 @@ export declare class DoublyLinkedListNode<E = any> {
395
395
  * // Test different scenarios of lyric synchronization
396
396
  *
397
397
  * // 1. Find lyric at exact timestamp
398
- * const exactTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 36000);
398
+ * const exactTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 36000);
399
399
  * console.log(exactTimeLyric?.text); // 'And ignite your bones'
400
400
  *
401
401
  * // 2. Find lyric between timestamps
402
- * const betweenTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 22000);
402
+ * const betweenTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 22000);
403
403
  * console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
404
404
  *
405
405
  * // 3. Find first lyric when timestamp is less than first entry
406
- * const earlyTimeLyric = lyricsList.findBackward(lyric => lyric.time <= -1000);
406
+ * const earlyTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= -1000);
407
407
  * console.log(earlyTimeLyric); // undefined
408
408
  *
409
409
  * // 4. Find last lyric when timestamp is after last entry
410
- * const lateTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 50000);
410
+ * const lateTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 50000);
411
411
  * console.log(lateTimeLyric?.text); // 'And I will try to fix you'
412
412
  * @example
413
413
  * // cpu process schedules
@@ -524,25 +524,30 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
524
524
  */
525
525
  get last(): E | undefined;
526
526
  /**
527
- * Time Complexity: O(n)
528
- * Space Complexity: O(n)
527
+ * Time Complexity: O(1)
528
+ * Space Complexity: O(1)
529
529
  *
530
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
531
- * given array.
532
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
533
- * @returns The `fromArray` function returns a DoublyLinkedList object.
534
- */
535
- static fromArray<E>(data: E[]): DoublyLinkedList<E, any>;
530
+ * The function `isNode` in TypeScript checks if a given input is an instance of
531
+ * `DoublyLinkedListNode`.
532
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
533
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
534
+ * be one of the following types:
535
+ * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
536
+ * instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
537
+ * parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
538
+ * the function returns `false`.
539
+ */
540
+ isNode(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is DoublyLinkedListNode<E>;
536
541
  /**
537
542
  * Time Complexity: O(1)
538
543
  * Space Complexity: O(1)
539
544
  *
540
- * The push function adds a new element to the end of a doubly linked list.
541
- * @param {E} element - The "element" parameter represents the value that you want to add to the
542
- * doubly linked list.
543
- * @returns The `push` method is returning a boolean value, `true`.
545
+ * The `push` function adds a new element or node to the end of a doubly linked list.
546
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
547
+ * method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
548
+ * @returns The `push` method is returning a boolean value, specifically `true`.
544
549
  */
545
- push(element: E): boolean;
550
+ push(elementOrNode: E | DoublyLinkedListNode<E>): boolean;
546
551
  /**
547
552
  * Time Complexity: O(1)
548
553
  * Space Complexity: O(1)
@@ -563,12 +568,13 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
563
568
  * Time Complexity: O(1)
564
569
  * Space Complexity: O(1)
565
570
  *
566
- * The unshift function adds a new element to the beginning of a doubly linked list.
567
- * @param {E} element - The "element" parameter represents the value of the element that you want to
568
- * add to the beginning of the doubly linked list.
569
- * @returns The `unshift` method is returning a boolean value, `true`.
571
+ * The unshift function adds a new element or node to the beginning of a doubly linked list.
572
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
573
+ * `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
574
+ * element of type `E`.
575
+ * @returns The `unshift` method is returning a boolean value, specifically `true`.
570
576
  */
571
- unshift(element: E): boolean;
577
+ unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean;
572
578
  /**
573
579
  * Time Complexity: O(n)
574
580
  * Space Complexity: O(1)
@@ -596,53 +602,69 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
596
602
  * Time Complexity: O(n)
597
603
  * Space Complexity: O(1)
598
604
  *
599
- * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
600
- * node if found, otherwise it returns undefined.
601
- * @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
602
- * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
603
- * is found in the linked list. If no such node is found, it returns `undefined`.
604
- */
605
- getNode(value: E | undefined): DoublyLinkedListNode<E> | undefined;
605
+ * This TypeScript function searches for a node in a doubly linked list based on a given element node
606
+ * or predicate.
607
+ * @param {| E
608
+ * | DoublyLinkedListNode<E>
609
+ * | ((node: DoublyLinkedListNode<E>) => boolean)
610
+ * | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
611
+ * node in a doubly linked list based on a given element, node, or predicate function. The
612
+ * `elementNodeOrPredicate` parameter can be one of the following:
613
+ * @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
614
+ * input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
615
+ * Otherwise, it iterates through the linked list starting from the head node and applies the
616
+ * provided predicate function to each node. If a node satisfies the predicate, that node is
617
+ * returned. If
618
+ */
619
+ getNode(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean) | undefined): DoublyLinkedListNode<E> | undefined;
606
620
  /**
607
621
  * Time Complexity: O(n)
608
622
  * Space Complexity: O(1)
609
623
  *
610
- * The `insert` function inserts a value at a specified index in a doubly linked list.
611
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
612
- * DoublyLinkedList. It is of type number.
613
- * @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
614
- * specified index.
615
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
616
- * if the index is out of bounds.
617
- */
618
- addAt(index: number, value: E): boolean;
624
+ * The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
625
+ * @param {number} index - The `index` parameter in the `addAt` method represents the position at
626
+ * which you want to add a new element or node in the doubly linked list. It indicates the location
627
+ * where the new element or node should be inserted.
628
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
629
+ * `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
630
+ * @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
631
+ * successfully added at the specified index, and `false` if the index is out of bounds (less than 0
632
+ * or greater than the size of the list).
633
+ */
634
+ addAt(index: number, newElementOrNode: E | DoublyLinkedListNode<E>): boolean;
619
635
  /**
620
636
  * Time Complexity: O(1) or O(n)
621
637
  * Space Complexity: O(1)
622
638
  *
623
- * The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
624
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
625
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
626
- * itself.
627
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
628
- * list.
629
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
630
- * insertion fails.
631
- */
632
- addBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
639
+ * The `addBefore` function in TypeScript adds a new element or node before an existing element or
640
+ * node in a doubly linked list.
641
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
642
+ * in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
643
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
644
+ * represents the element or node that you want to add before the `existingElementOrNode` in a doubly
645
+ * linked list.
646
+ * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
647
+ * successfully added before the existing element or node, and `false` if the existing element or
648
+ * node was not found.
649
+ */
650
+ addBefore(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean;
633
651
  /**
634
652
  * Time Complexity: O(1) or O(n)
635
653
  * Space Complexity: O(1)
636
654
  *
637
- * The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
638
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
639
- * after which the new value will be inserted. It can be either the value of the existing node or the existing node
640
- * itself.
641
- * @param {E} newValue - The value that you want to insert into the doubly linked list.
642
- * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
643
- * existing value or node is not found in the doubly linked list.
644
- */
645
- addAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
655
+ * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
656
+ * in a doubly linked list.
657
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
658
+ * element or node in the doubly linked list after which you want to add a new element or node.
659
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
660
+ * `addAfter` method represents the element or node that you want to add after the existing element
661
+ * or node in a doubly linked list. This parameter can be either an element value or a
662
+ * `DoublyLinkedListNode` object that you want to insert
663
+ * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
664
+ * successfully added after the existing element or node, and `false` if the existing element or node
665
+ * was not found in the linked list.
666
+ */
667
+ addAfter(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean;
646
668
  /**
647
669
  * Time Complexity: O(n)
648
670
  * Space Complexity: O(1)
@@ -658,13 +680,16 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
658
680
  * Time Complexity: O(1) or O(n)
659
681
  * Space Complexity: O(1)
660
682
  *
661
- * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
662
- * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
663
- * a `DoublyLinkedListNode<E>` object.
664
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
665
- * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
683
+ * The `delete` function removes a specified element or node from a doubly linked list if it exists.
684
+ * @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
685
+ * the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
686
+ * can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
687
+ * doubly linked list
688
+ * @returns The `delete` method returns a boolean value - `true` if the element or node was
689
+ * successfully deleted from the doubly linked list, and `false` if the element or node was not found
690
+ * in the list.
666
691
  */
667
- delete(valOrNode: E | DoublyLinkedListNode<E> | undefined): boolean;
692
+ delete(elementOrNode: E | DoublyLinkedListNode<E> | undefined): boolean;
668
693
  /**
669
694
  * Time Complexity: O(1)
670
695
  * Space Complexity: O(1)
@@ -684,25 +709,45 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
684
709
  * Time Complexity: O(n)
685
710
  * Space Complexity: O(1)
686
711
  *
687
- * The function returns the index of the first occurrence of a given value in a linked list.
688
- * @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
689
- * that we are searching for in the linked list.
690
- * @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
691
- * list. If the value is not found, it returns -1.
712
+ * The indexOf function in TypeScript returns the index of a specified element or node in a Doubly
713
+ * Linked List.
714
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
715
+ * `indexOf` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
716
+ * element of type `E`.
717
+ * @returns The `indexOf` method is returning the index of the element or node in the doubly linked
718
+ * list. If the element or node is found in the list, the method returns the index of that element or
719
+ * node. If the element or node is not found in the list, the method returns -1.
720
+ */
721
+ indexOf(elementOrNode: E | DoublyLinkedListNode<E>): number;
722
+ /**
723
+ * Time Complexity: O(n)
724
+ * Space Complexity: O(1)
725
+ *
726
+ */
727
+ /**
728
+ * This function retrieves an element from a doubly linked list based on a given element
729
+ * node or predicate.
730
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
731
+ * elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
732
+ * which can be one of the following types:
733
+ * @returns The `get` method returns the value of the first node in the doubly linked list that
734
+ * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
692
735
  */
693
- indexOf(value: E): number;
736
+ get(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
694
737
  /**
695
738
  * Time Complexity: O(n)
696
739
  * Space Complexity: O(1)
697
740
  *
698
- * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
699
- * value that satisfies the given callback function, or undefined if no value satisfies the callback.
700
- * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
701
- * function is used to determine whether a given value satisfies a certain condition.
702
- * @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
703
- * the callback function. If no value satisfies the condition, it returns `undefined`.
741
+ * The `getBackward` function searches for a specific element in a doubly linked list starting from
742
+ * the tail and moving backwards.
743
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
744
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
745
+ * function can be one of the following types:
746
+ * @returns The `getBackward` method returns the value of the element node that matches the provided
747
+ * predicate when traversing the doubly linked list backwards. If no matching element is found, it
748
+ * returns `undefined`.
704
749
  */
705
- findBackward(callback: (value: E) => boolean): E | undefined;
750
+ getBackward(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
706
751
  /**
707
752
  * Time Complexity: O(n)
708
753
  * Space Complexity: O(1)
@@ -775,8 +820,48 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
775
820
  * @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
776
821
  */
777
822
  map<EM, RM>(callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): DoublyLinkedList<EM, RM>;
823
+ /**
824
+ * Time Complexity: O(n)
825
+ * Space Complexity: O(n)
826
+ *
827
+ * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
828
+ * given array.
829
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
830
+ * @returns The `fromArray` function returns a DoublyLinkedList object.
831
+ */
832
+ static fromArray<E>(data: E[]): DoublyLinkedList<E, any>;
778
833
  /**
779
834
  * The function returns an iterator that iterates over the values of a linked list.
780
835
  */
781
836
  protected _getIterator(): IterableIterator<E>;
837
+ /**
838
+ * The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
839
+ * as an argument and returns a boolean.
840
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
841
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
842
+ * types:
843
+ * @returns The _isPredicate method is returning a boolean value indicating whether the
844
+ * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
845
+ * function, the method will return true, indicating that it is a predicate function.
846
+ */
847
+ protected _isPredicate(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is (node: DoublyLinkedListNode<E>) => boolean;
848
+ /**
849
+ * The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
850
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
851
+ * an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
852
+ * @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
853
+ * as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
854
+ * value and returned.
855
+ */
856
+ protected _ensureNode(elementOrNode: E | DoublyLinkedListNode<E>): DoublyLinkedListNode<E>;
857
+ /**
858
+ * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
859
+ * function, or a value to compare with the node's value.
860
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
861
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
862
+ * types:
863
+ * @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
864
+ * returns a boolean value based on the conditions specified in the code.
865
+ */
866
+ protected _ensurePredicate(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): (node: DoublyLinkedListNode<E>) => boolean;
782
867
  }
@@ -404,19 +404,19 @@ 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.findBackward(lyric => lyric.time <= 36000);
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.findBackward(lyric => lyric.time <= 22000);
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.findBackward(lyric => lyric.time <= -1000);
415
+ * const earlyTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= -1000);
416
416
  * console.log(earlyTimeLyric); // undefined
417
417
  *
418
418
  * // 4. Find last lyric when timestamp is after last entry
419
- * const lateTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 50000);
419
+ * const lateTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 50000);
420
420
  * console.log(lateTimeLyric?.text); // 'And I will try to fix you'
421
421
  * @example
422
422
  * // cpu process schedules
@@ -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(n)
560
- * Space Complexity: O(n)
559
+ * Time Complexity: O(1)
560
+ * Space Complexity: O(1)
561
561
  *
562
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
563
- * given array.
564
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
565
- * @returns The `fromArray` function returns a DoublyLinkedList object.
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
- static fromArray(data) {
568
- return new DoublyLinkedList(data);
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} element - The "element" parameter represents the value that you want to add to the
576
- * doubly linked list.
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(element) {
580
- const newNode = new DoublyLinkedListNode(element);
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} element - The "element" parameter represents the value of the element that you want to
643
- * add to the beginning of the doubly linked list.
644
- * @returns The `unshift` method is returning a boolean value, `true`.
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(element) {
647
- const newNode = new DoublyLinkedListNode(element);
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
- * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
704
- * node if found, otherwise it returns undefined.
705
- * @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
706
- * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
707
- * is found in the linked list. If no such node is found, it returns `undefined`.
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(value) {
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.value === value) {
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 `insert` function inserts a value at a specified index in a doubly linked list.
724
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
725
- * DoublyLinkedList. It is of type number.
726
- * @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
727
- * specified index.
728
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
729
- * if the index is out of bounds.
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, value) {
750
+ addAt(index, newElementOrNode) {
732
751
  if (index < 0 || index > this._size)
733
752
  return false;
734
753
  if (index === 0) {
735
- this.unshift(value);
754
+ this.unshift(newElementOrNode);
736
755
  return true;
737
756
  }
738
757
  if (index === this._size) {
739
- this.push(value);
758
+ this.push(newElementOrNode);
740
759
  return true;
741
760
  }
742
- const newNode = new DoublyLinkedListNode(value);
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 inserts a new value before an existing value or node in a doubly linked list.
757
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
758
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
759
- * itself.
760
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
761
- * list.
762
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
763
- * insertion fails.
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(existingValueOrNode, newValue) {
786
+ addBefore(existingElementOrNode, newElementOrNode) {
766
787
  let existingNode;
767
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
768
- existingNode = existingValueOrNode;
788
+ if (existingElementOrNode instanceof DoublyLinkedListNode) {
789
+ existingNode = existingElementOrNode;
769
790
  }
770
791
  else {
771
- existingNode = this.getNode(existingValueOrNode);
792
+ existingNode = this.getNode(existingElementOrNode);
772
793
  }
773
794
  if (existingNode) {
774
- const newNode = new DoublyLinkedListNode(newValue);
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 inserts a new node with a given value after an existing node in a doubly linked list.
794
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
795
- * after which the new value will be inserted. It can be either the value of the existing node or the existing node
796
- * itself.
797
- * @param {E} newValue - The value that you want to insert into the doubly linked list.
798
- * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
799
- * existing value or node is not found in the doubly linked list.
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(existingValueOrNode, newValue) {
826
+ addAfter(existingElementOrNode, newElementOrNode) {
802
827
  let existingNode;
803
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
804
- existingNode = existingValueOrNode;
828
+ if (existingElementOrNode instanceof DoublyLinkedListNode) {
829
+ existingNode = existingElementOrNode;
805
830
  }
806
831
  else {
807
- existingNode = this.getNode(existingValueOrNode);
832
+ existingNode = this.getNode(existingElementOrNode);
808
833
  }
809
834
  if (existingNode) {
810
- const newNode = new DoublyLinkedListNode(newValue);
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 based on either the node itself or its value.
859
- * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
860
- * a `DoublyLinkedListNode<E>` object.
861
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
862
- * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
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(valOrNode) {
865
- let node;
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 the first occurrence of a given value in a linked list.
918
- * @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
919
- * that we are searching for in the linked list.
920
- * @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
921
- * list. If the value is not found, it returns -1.
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(value) {
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.value === value) {
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
- findBackward(callback) {
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 (callback(current.value)) {
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.4",
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.4"
166
+ "data-structure-typed": "^1.53.5"
167
167
  }
168
168
  }
@@ -422,19 +422,19 @@ export class DoublyLinkedListNode<E = any> {
422
422
  * // Test different scenarios of lyric synchronization
423
423
  *
424
424
  * // 1. Find lyric at exact timestamp
425
- * const exactTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 36000);
425
+ * const exactTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 36000);
426
426
  * console.log(exactTimeLyric?.text); // 'And ignite your bones'
427
427
  *
428
428
  * // 2. Find lyric between timestamps
429
- * const betweenTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 22000);
429
+ * const betweenTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 22000);
430
430
  * console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
431
431
  *
432
432
  * // 3. Find first lyric when timestamp is less than first entry
433
- * const earlyTimeLyric = lyricsList.findBackward(lyric => lyric.time <= -1000);
433
+ * const earlyTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= -1000);
434
434
  * console.log(earlyTimeLyric); // undefined
435
435
  *
436
436
  * // 4. Find last lyric when timestamp is after last entry
437
- * const lateTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 50000);
437
+ * const lateTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 50000);
438
438
  * console.log(lateTimeLyric?.text); // 'And I will try to fix you'
439
439
  * @example
440
440
  * // cpu process schedules
@@ -582,29 +582,36 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
582
582
  }
583
583
 
584
584
  /**
585
- * Time Complexity: O(n)
586
- * Space Complexity: O(n)
585
+ * Time Complexity: O(1)
586
+ * Space Complexity: O(1)
587
587
  *
588
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
589
- * given array.
590
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
591
- * @returns The `fromArray` function returns a DoublyLinkedList object.
588
+ * The function `isNode` in TypeScript checks if a given input is an instance of
589
+ * `DoublyLinkedListNode`.
590
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
591
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
592
+ * be one of the following types:
593
+ * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
594
+ * instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
595
+ * parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
596
+ * the function returns `false`.
592
597
  */
593
- static fromArray<E>(data: E[]) {
594
- return new DoublyLinkedList<E>(data);
598
+ isNode(
599
+ elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
600
+ ): elementNodeOrPredicate is DoublyLinkedListNode<E> {
601
+ return elementNodeOrPredicate instanceof DoublyLinkedListNode;
595
602
  }
596
603
 
597
604
  /**
598
605
  * Time Complexity: O(1)
599
606
  * Space Complexity: O(1)
600
607
  *
601
- * The push function adds a new element to the end of a doubly linked list.
602
- * @param {E} element - The "element" parameter represents the value that you want to add to the
603
- * doubly linked list.
604
- * @returns The `push` method is returning a boolean value, `true`.
608
+ * The `push` function adds a new element or node to the end of a doubly linked list.
609
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
610
+ * method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
611
+ * @returns The `push` method is returning a boolean value, specifically `true`.
605
612
  */
606
- push(element: E): boolean {
607
- const newNode = new DoublyLinkedListNode(element);
613
+ push(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
614
+ const newNode = this._ensureNode(elementOrNode);
608
615
  if (!this.head) {
609
616
  this._head = newNode;
610
617
  this._tail = newNode;
@@ -663,13 +670,14 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
663
670
  * Time Complexity: O(1)
664
671
  * Space Complexity: O(1)
665
672
  *
666
- * The unshift function adds a new element to the beginning of a doubly linked list.
667
- * @param {E} element - The "element" parameter represents the value of the element that you want to
668
- * add to the beginning of the doubly linked list.
669
- * @returns The `unshift` method is returning a boolean value, `true`.
673
+ * The unshift function adds a new element or node to the beginning of a doubly linked list.
674
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
675
+ * `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
676
+ * element of type `E`.
677
+ * @returns The `unshift` method is returning a boolean value, specifically `true`.
670
678
  */
671
- unshift(element: E): boolean {
672
- const newNode = new DoublyLinkedListNode(element);
679
+ unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
680
+ const newNode = this._ensureNode(elementOrNode);
673
681
  if (!this.head) {
674
682
  this._head = newNode;
675
683
  this._tail = newNode;
@@ -725,17 +733,29 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
725
733
  * Time Complexity: O(n)
726
734
  * Space Complexity: O(1)
727
735
  *
728
- * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
729
- * node if found, otherwise it returns undefined.
730
- * @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
731
- * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
732
- * is found in the linked list. If no such node is found, it returns `undefined`.
736
+ * This TypeScript function searches for a node in a doubly linked list based on a given element node
737
+ * or predicate.
738
+ * @param {| E
739
+ * | DoublyLinkedListNode<E>
740
+ * | ((node: DoublyLinkedListNode<E>) => boolean)
741
+ * | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
742
+ * node in a doubly linked list based on a given element, node, or predicate function. The
743
+ * `elementNodeOrPredicate` parameter can be one of the following:
744
+ * @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
745
+ * input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
746
+ * Otherwise, it iterates through the linked list starting from the head node and applies the
747
+ * provided predicate function to each node. If a node satisfies the predicate, that node is
748
+ * returned. If
733
749
  */
734
- getNode(value: E | undefined): DoublyLinkedListNode<E> | undefined {
750
+ getNode(
751
+ elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean) | undefined
752
+ ): DoublyLinkedListNode<E> | undefined {
753
+ if (elementNodeOrPredicate === undefined) return;
754
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
735
755
  let current = this.head;
736
756
 
737
757
  while (current) {
738
- if (current.value === value) {
758
+ if (predicate(current)) {
739
759
  return current;
740
760
  }
741
761
  current = current.next;
@@ -748,26 +768,28 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
748
768
  * Time Complexity: O(n)
749
769
  * Space Complexity: O(1)
750
770
  *
751
- * The `insert` function inserts a value at a specified index in a doubly linked list.
752
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
753
- * DoublyLinkedList. It is of type number.
754
- * @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
755
- * specified index.
756
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
757
- * if the index is out of bounds.
771
+ * The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
772
+ * @param {number} index - The `index` parameter in the `addAt` method represents the position at
773
+ * which you want to add a new element or node in the doubly linked list. It indicates the location
774
+ * where the new element or node should be inserted.
775
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
776
+ * `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
777
+ * @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
778
+ * successfully added at the specified index, and `false` if the index is out of bounds (less than 0
779
+ * or greater than the size of the list).
758
780
  */
759
- addAt(index: number, value: E): boolean {
781
+ addAt(index: number, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
760
782
  if (index < 0 || index > this._size) return false;
761
783
  if (index === 0) {
762
- this.unshift(value);
784
+ this.unshift(newElementOrNode);
763
785
  return true;
764
786
  }
765
787
  if (index === this._size) {
766
- this.push(value);
788
+ this.push(newElementOrNode);
767
789
  return true;
768
790
  }
769
791
 
770
- const newNode = new DoublyLinkedListNode(value);
792
+ const newNode = this._ensureNode(newElementOrNode);
771
793
  const prevNode = this.getNodeAt(index - 1);
772
794
  const nextNode = prevNode!.next;
773
795
  newNode.prev = prevNode;
@@ -782,26 +804,31 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
782
804
  * Time Complexity: O(1) or O(n)
783
805
  * Space Complexity: O(1)
784
806
  *
785
- * The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
786
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
787
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
788
- * itself.
789
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
790
- * list.
791
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
792
- * insertion fails.
807
+ * The `addBefore` function in TypeScript adds a new element or node before an existing element or
808
+ * node in a doubly linked list.
809
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
810
+ * in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
811
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
812
+ * represents the element or node that you want to add before the `existingElementOrNode` in a doubly
813
+ * linked list.
814
+ * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
815
+ * successfully added before the existing element or node, and `false` if the existing element or
816
+ * node was not found.
793
817
  */
794
- addBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
818
+ addBefore(
819
+ existingElementOrNode: E | DoublyLinkedListNode<E>,
820
+ newElementOrNode: E | DoublyLinkedListNode<E>
821
+ ): boolean {
795
822
  let existingNode;
796
823
 
797
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
798
- existingNode = existingValueOrNode;
824
+ if (existingElementOrNode instanceof DoublyLinkedListNode) {
825
+ existingNode = existingElementOrNode;
799
826
  } else {
800
- existingNode = this.getNode(existingValueOrNode);
827
+ existingNode = this.getNode(existingElementOrNode);
801
828
  }
802
829
 
803
830
  if (existingNode) {
804
- const newNode = new DoublyLinkedListNode(newValue);
831
+ const newNode = this._ensureNode(newElementOrNode);
805
832
  newNode.prev = existingNode.prev;
806
833
  if (existingNode.prev) {
807
834
  existingNode.prev.next = newNode;
@@ -822,25 +849,29 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
822
849
  * Time Complexity: O(1) or O(n)
823
850
  * Space Complexity: O(1)
824
851
  *
825
- * The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
826
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
827
- * after which the new value will be inserted. It can be either the value of the existing node or the existing node
828
- * itself.
829
- * @param {E} newValue - The value that you want to insert into the doubly linked list.
830
- * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
831
- * existing value or node is not found in the doubly linked list.
852
+ * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
853
+ * in a doubly linked list.
854
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
855
+ * element or node in the doubly linked list after which you want to add a new element or node.
856
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
857
+ * `addAfter` method represents the element or node that you want to add after the existing element
858
+ * or node in a doubly linked list. This parameter can be either an element value or a
859
+ * `DoublyLinkedListNode` object that you want to insert
860
+ * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
861
+ * successfully added after the existing element or node, and `false` if the existing element or node
862
+ * was not found in the linked list.
832
863
  */
833
- addAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
864
+ addAfter(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
834
865
  let existingNode;
835
866
 
836
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
837
- existingNode = existingValueOrNode;
867
+ if (existingElementOrNode instanceof DoublyLinkedListNode) {
868
+ existingNode = existingElementOrNode;
838
869
  } else {
839
- existingNode = this.getNode(existingValueOrNode);
870
+ existingNode = this.getNode(existingElementOrNode);
840
871
  }
841
872
 
842
873
  if (existingNode) {
843
- const newNode = new DoublyLinkedListNode(newValue);
874
+ const newNode = this._ensureNode(newElementOrNode);
844
875
  newNode.next = existingNode.next;
845
876
  if (existingNode.next) {
846
877
  existingNode.next.prev = newNode;
@@ -891,20 +922,17 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
891
922
  * Time Complexity: O(1) or O(n)
892
923
  * Space Complexity: O(1)
893
924
  *
894
- * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
895
- * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
896
- * a `DoublyLinkedListNode<E>` object.
897
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
898
- * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
925
+ * The `delete` function removes a specified element or node from a doubly linked list if it exists.
926
+ * @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
927
+ * the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
928
+ * can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
929
+ * doubly linked list
930
+ * @returns The `delete` method returns a boolean value - `true` if the element or node was
931
+ * successfully deleted from the doubly linked list, and `false` if the element or node was not found
932
+ * in the list.
899
933
  */
900
- delete(valOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
901
- let node: DoublyLinkedListNode<E> | undefined;
902
-
903
- if (valOrNode instanceof DoublyLinkedListNode) {
904
- node = valOrNode;
905
- } else {
906
- node = this.getNode(valOrNode);
907
- }
934
+ delete(elementOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
935
+ const node: DoublyLinkedListNode<E> | undefined = this.getNode(elementOrNode);
908
936
 
909
937
  if (node) {
910
938
  if (node === this.head) {
@@ -950,17 +978,21 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
950
978
  * Time Complexity: O(n)
951
979
  * Space Complexity: O(1)
952
980
  *
953
- * The function returns the index of the first occurrence of a given value in a linked list.
954
- * @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
955
- * that we are searching for in the linked list.
956
- * @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
957
- * list. If the value is not found, it returns -1.
981
+ * The indexOf function in TypeScript returns the index of a specified element or node in a Doubly
982
+ * Linked List.
983
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
984
+ * `indexOf` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
985
+ * element of type `E`.
986
+ * @returns The `indexOf` method is returning the index of the element or node in the doubly linked
987
+ * list. If the element or node is found in the list, the method returns the index of that element or
988
+ * node. If the element or node is not found in the list, the method returns -1.
958
989
  */
959
- indexOf(value: E): number {
990
+ indexOf(elementOrNode: E | DoublyLinkedListNode<E>): number {
991
+ const predicate = this._ensurePredicate(elementOrNode);
960
992
  let index = 0;
961
993
  let current = this.head;
962
994
  while (current) {
963
- if (current.value === value) {
995
+ if (predicate(current)) {
964
996
  return index;
965
997
  }
966
998
  index++;
@@ -973,19 +1005,48 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
973
1005
  * Time Complexity: O(n)
974
1006
  * Space Complexity: O(1)
975
1007
  *
976
- * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
977
- * value that satisfies the given callback function, or undefined if no value satisfies the callback.
978
- * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
979
- * function is used to determine whether a given value satisfies a certain condition.
980
- * @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
981
- * the callback function. If no value satisfies the condition, it returns `undefined`.
982
1008
  */
983
- findBackward(callback: (value: E) => boolean): E | undefined {
1009
+ /**
1010
+ * This function retrieves an element from a doubly linked list based on a given element
1011
+ * node or predicate.
1012
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1013
+ * elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
1014
+ * which can be one of the following types:
1015
+ * @returns The `get` method returns the value of the first node in the doubly linked list that
1016
+ * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
1017
+ */
1018
+ get(
1019
+ elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
1020
+ ): E | undefined {
1021
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1022
+ let current = this.head;
1023
+ while (current) {
1024
+ if (predicate(current)) return current.value;
1025
+ current = current.next;
1026
+ }
1027
+ return undefined;
1028
+ }
1029
+
1030
+ /**
1031
+ * Time Complexity: O(n)
1032
+ * Space Complexity: O(1)
1033
+ *
1034
+ * The `getBackward` function searches for a specific element in a doubly linked list starting from
1035
+ * the tail and moving backwards.
1036
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1037
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
1038
+ * function can be one of the following types:
1039
+ * @returns The `getBackward` method returns the value of the element node that matches the provided
1040
+ * predicate when traversing the doubly linked list backwards. If no matching element is found, it
1041
+ * returns `undefined`.
1042
+ */
1043
+ getBackward(
1044
+ elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
1045
+ ): E | undefined {
1046
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
984
1047
  let current = this.tail;
985
1048
  while (current) {
986
- if (callback(current.value)) {
987
- return current.value;
988
- }
1049
+ if (predicate(current)) return current.value;
989
1050
  current = current.prev;
990
1051
  }
991
1052
  return undefined;
@@ -1119,6 +1180,19 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
1119
1180
  return mappedList;
1120
1181
  }
1121
1182
 
1183
+ /**
1184
+ * Time Complexity: O(n)
1185
+ * Space Complexity: O(n)
1186
+ *
1187
+ * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
1188
+ * given array.
1189
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
1190
+ * @returns The `fromArray` function returns a DoublyLinkedList object.
1191
+ */
1192
+ static fromArray<E>(data: E[]) {
1193
+ return new DoublyLinkedList<E>(data);
1194
+ }
1195
+
1122
1196
  /**
1123
1197
  * The function returns an iterator that iterates over the values of a linked list.
1124
1198
  */
@@ -1130,4 +1204,53 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
1130
1204
  current = current.next;
1131
1205
  }
1132
1206
  }
1207
+
1208
+ /**
1209
+ * The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
1210
+ * as an argument and returns a boolean.
1211
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1212
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
1213
+ * types:
1214
+ * @returns The _isPredicate method is returning a boolean value indicating whether the
1215
+ * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
1216
+ * function, the method will return true, indicating that it is a predicate function.
1217
+ */
1218
+ protected _isPredicate(
1219
+ elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
1220
+ ): elementNodeOrPredicate is (node: DoublyLinkedListNode<E>) => boolean {
1221
+ return typeof elementNodeOrPredicate === 'function';
1222
+ }
1223
+
1224
+ /**
1225
+ * The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
1226
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
1227
+ * an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
1228
+ * @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
1229
+ * as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
1230
+ * value and returned.
1231
+ */
1232
+ protected _ensureNode(elementOrNode: E | DoublyLinkedListNode<E>) {
1233
+ if (this.isNode(elementOrNode)) return elementOrNode;
1234
+
1235
+ return new DoublyLinkedListNode<E>(elementOrNode);
1236
+ }
1237
+
1238
+ /**
1239
+ * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
1240
+ * function, or a value to compare with the node's value.
1241
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1242
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
1243
+ * types:
1244
+ * @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
1245
+ * returns a boolean value based on the conditions specified in the code.
1246
+ */
1247
+ protected _ensurePredicate(
1248
+ elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
1249
+ ) {
1250
+ if (this.isNode(elementNodeOrPredicate)) return (node: DoublyLinkedListNode<E>) => node === elementNodeOrPredicate;
1251
+
1252
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
1253
+
1254
+ return (node: DoublyLinkedListNode<E>) => node.value === elementNodeOrPredicate;
1255
+ }
1133
1256
  }