queue-typed 1.48.1 → 1.48.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/data-structures/hash/hash-map.js +69 -173
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +82 -138
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/index.ts +1 -0
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* data-structure-typed
|
|
3
6
|
*
|
|
@@ -22,11 +25,12 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
22
25
|
}
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
export class DoublyLinkedList<E = any> {
|
|
28
|
+
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
26
29
|
/**
|
|
27
30
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
31
|
*/
|
|
29
32
|
constructor(elements?: Iterable<E>) {
|
|
33
|
+
super();
|
|
30
34
|
this._head = undefined;
|
|
31
35
|
this._tail = undefined;
|
|
32
36
|
this._length = 0;
|
|
@@ -724,59 +728,32 @@ export class DoublyLinkedList<E = any> {
|
|
|
724
728
|
}
|
|
725
729
|
|
|
726
730
|
/**
|
|
727
|
-
*
|
|
728
|
-
*/
|
|
729
|
-
* [Symbol.iterator]() {
|
|
730
|
-
let current = this.head;
|
|
731
|
-
|
|
732
|
-
while (current) {
|
|
733
|
-
yield current.value;
|
|
734
|
-
current = current.next;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
/**
|
|
739
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
740
|
-
* Space Complexity: O(1)
|
|
741
|
-
*/
|
|
742
|
-
|
|
743
|
-
/**
|
|
744
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
745
|
-
* Space Complexity: O(1)
|
|
746
|
-
*
|
|
747
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
748
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
749
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
750
|
-
* current node in the linked list.
|
|
751
|
-
*/
|
|
752
|
-
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void {
|
|
753
|
-
let index = 0;
|
|
754
|
-
for (const el of this) {
|
|
755
|
-
callback(el, index, this);
|
|
756
|
-
index++;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
/**
|
|
761
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
731
|
+
* Time Complexity: O(n)
|
|
762
732
|
* Space Complexity: O(n)
|
|
763
733
|
*/
|
|
764
734
|
|
|
765
735
|
/**
|
|
766
|
-
* Time Complexity: O(n)
|
|
736
|
+
* Time Complexity: O(n)
|
|
767
737
|
* Space Complexity: O(n)
|
|
768
738
|
*
|
|
769
|
-
* The `filter` function
|
|
770
|
-
*
|
|
771
|
-
*
|
|
772
|
-
*
|
|
773
|
-
*
|
|
774
|
-
|
|
775
|
-
|
|
739
|
+
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
|
|
740
|
+
* list and applying a callback function to each element, returning only the elements for which the
|
|
741
|
+
* callback function returns true.
|
|
742
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
743
|
+
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current
|
|
744
|
+
* element, and the DoublyLinkedList itself. The callback function should return a boolean value
|
|
745
|
+
* indicating whether the current element should be included
|
|
746
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
747
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
748
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
749
|
+
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
|
|
750
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
751
|
+
*/
|
|
752
|
+
filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E> {
|
|
776
753
|
const filteredList = new DoublyLinkedList<E>();
|
|
777
754
|
let index = 0;
|
|
778
755
|
for (const current of this) {
|
|
779
|
-
if (callback(current, index, this)) {
|
|
756
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
780
757
|
filteredList.push(current);
|
|
781
758
|
}
|
|
782
759
|
index++;
|
|
@@ -790,21 +767,27 @@ export class DoublyLinkedList<E = any> {
|
|
|
790
767
|
*/
|
|
791
768
|
|
|
792
769
|
/**
|
|
793
|
-
* Time Complexity: O(n)
|
|
770
|
+
* Time Complexity: O(n)
|
|
794
771
|
* Space Complexity: O(n)
|
|
795
772
|
*
|
|
796
|
-
* The `map` function
|
|
797
|
-
*
|
|
798
|
-
* @param callback - The callback parameter is a function that
|
|
799
|
-
*
|
|
800
|
-
* DoublyLinkedList
|
|
801
|
-
*
|
|
802
|
-
|
|
803
|
-
|
|
773
|
+
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element
|
|
774
|
+
* in the original list.
|
|
775
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
776
|
+
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
|
|
777
|
+
* and the DoublyLinkedList itself. The callback function should return a value that will be added to
|
|
778
|
+
* the new DoublyLinkedList that
|
|
779
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
780
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
781
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
782
|
+
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
|
|
783
|
+
* of applying the provided `callback` function to each element in the original `DoublyLinkedList`
|
|
784
|
+
* object.
|
|
785
|
+
*/
|
|
786
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T> {
|
|
804
787
|
const mappedList = new DoublyLinkedList<T>();
|
|
805
788
|
let index = 0;
|
|
806
789
|
for (const current of this) {
|
|
807
|
-
mappedList.push(callback(current, index, this));
|
|
790
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
808
791
|
index++;
|
|
809
792
|
}
|
|
810
793
|
|
|
@@ -816,31 +799,19 @@ export class DoublyLinkedList<E = any> {
|
|
|
816
799
|
* Space Complexity: O(n)
|
|
817
800
|
*/
|
|
818
801
|
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
821
|
-
* Space Complexity: O(n)
|
|
822
|
-
*
|
|
823
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
824
|
-
* single value.
|
|
825
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
826
|
-
* used to perform a specific operation on each element of the linked list.
|
|
827
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
828
|
-
* point for the reduction operation.
|
|
829
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
830
|
-
* elements in the linked list.
|
|
831
|
-
*/
|
|
832
|
-
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T {
|
|
833
|
-
let accumulator = initialValue;
|
|
834
|
-
let index = 0;
|
|
835
|
-
for (const current of this) {
|
|
836
|
-
accumulator = callback(accumulator, current, index, this);
|
|
837
|
-
index++;
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
return accumulator;
|
|
841
|
-
}
|
|
842
|
-
|
|
843
802
|
print(): void {
|
|
844
803
|
console.log([...this]);
|
|
845
804
|
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
808
|
+
*/
|
|
809
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
810
|
+
let current = this.head;
|
|
811
|
+
|
|
812
|
+
while (current) {
|
|
813
|
+
yield current.value;
|
|
814
|
+
current = current.next;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
846
817
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* data-structure-typed
|
|
3
6
|
*
|
|
@@ -20,11 +23,12 @@ export class SinglyLinkedListNode<E = any> {
|
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
export class SinglyLinkedList<E = any> {
|
|
26
|
+
export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
24
27
|
/**
|
|
25
28
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
29
|
*/
|
|
27
30
|
constructor(elements?: Iterable<E>) {
|
|
31
|
+
super();
|
|
28
32
|
this._head = undefined;
|
|
29
33
|
this._tail = undefined;
|
|
30
34
|
this._length = 0;
|
|
@@ -670,59 +674,32 @@ export class SinglyLinkedList<E = any> {
|
|
|
670
674
|
}
|
|
671
675
|
|
|
672
676
|
/**
|
|
673
|
-
*
|
|
674
|
-
*/
|
|
675
|
-
* [Symbol.iterator]() {
|
|
676
|
-
let current = this.head;
|
|
677
|
-
|
|
678
|
-
while (current) {
|
|
679
|
-
yield current.value;
|
|
680
|
-
current = current.next;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
/**
|
|
685
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
686
|
-
* Space Complexity: O(1)
|
|
687
|
-
*/
|
|
688
|
-
|
|
689
|
-
/**
|
|
690
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
691
|
-
* Space Complexity: O(1)
|
|
692
|
-
*
|
|
693
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
694
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
695
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
696
|
-
* current node in the linked list.
|
|
697
|
-
*/
|
|
698
|
-
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void {
|
|
699
|
-
let index = 0;
|
|
700
|
-
for (const el of this) {
|
|
701
|
-
callback(el, index, this);
|
|
702
|
-
index++;
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
/**
|
|
707
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
677
|
+
* Time Complexity: O(n)
|
|
708
678
|
* Space Complexity: O(n)
|
|
709
679
|
*/
|
|
710
680
|
|
|
711
681
|
/**
|
|
712
|
-
* Time Complexity: O(n)
|
|
682
|
+
* Time Complexity: O(n)
|
|
713
683
|
* Space Complexity: O(n)
|
|
714
684
|
*
|
|
715
|
-
* The `filter` function
|
|
716
|
-
*
|
|
717
|
-
*
|
|
718
|
-
*
|
|
719
|
-
*
|
|
720
|
-
|
|
721
|
-
|
|
685
|
+
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
|
|
686
|
+
* list and applying a callback function to each element to determine if it should be included in the
|
|
687
|
+
* filtered list.
|
|
688
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
689
|
+
* list. It takes three arguments: the current element, the index of the current element, and the
|
|
690
|
+
* list itself. The callback function should return a boolean value indicating whether the current
|
|
691
|
+
* element should be included in the filtered list or not
|
|
692
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
693
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
694
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
695
|
+
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
|
|
696
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
697
|
+
*/
|
|
698
|
+
filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E> {
|
|
722
699
|
const filteredList = new SinglyLinkedList<E>();
|
|
723
700
|
let index = 0;
|
|
724
701
|
for (const current of this) {
|
|
725
|
-
if (callback(current, index, this)) {
|
|
702
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
726
703
|
filteredList.push(current);
|
|
727
704
|
}
|
|
728
705
|
index++;
|
|
@@ -730,27 +707,30 @@ export class SinglyLinkedList<E = any> {
|
|
|
730
707
|
return filteredList;
|
|
731
708
|
}
|
|
732
709
|
|
|
710
|
+
|
|
733
711
|
/**
|
|
734
712
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
735
713
|
* Space Complexity: O(n)
|
|
736
714
|
*/
|
|
737
|
-
|
|
738
715
|
/**
|
|
739
|
-
* Time Complexity: O(n)
|
|
716
|
+
* Time Complexity: O(n)
|
|
740
717
|
* Space Complexity: O(n)
|
|
741
718
|
*
|
|
742
|
-
* The `map` function
|
|
743
|
-
*
|
|
744
|
-
* @param callback - The callback parameter is a function that
|
|
745
|
-
* the
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
|
|
749
|
-
|
|
719
|
+
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element
|
|
720
|
+
* of the original list.
|
|
721
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
722
|
+
* the linked list. It takes three arguments:
|
|
723
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
724
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
725
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
726
|
+
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
|
|
727
|
+
* of applying the provided `callback` function to each element in the original list.
|
|
728
|
+
*/
|
|
729
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T> {
|
|
750
730
|
const mappedList = new SinglyLinkedList<T>();
|
|
751
731
|
let index = 0;
|
|
752
732
|
for (const current of this) {
|
|
753
|
-
mappedList.push(callback(current, index, this));
|
|
733
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
754
734
|
index++;
|
|
755
735
|
}
|
|
756
736
|
|
|
@@ -762,31 +742,16 @@ export class SinglyLinkedList<E = any> {
|
|
|
762
742
|
* Space Complexity: O(n)
|
|
763
743
|
*/
|
|
764
744
|
|
|
765
|
-
/**
|
|
766
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
767
|
-
* Space Complexity: O(n)
|
|
768
|
-
*
|
|
769
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
770
|
-
* single value.
|
|
771
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
772
|
-
* used to perform a specific operation on each element of the linked list.
|
|
773
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
774
|
-
* point for the reduction operation.
|
|
775
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
776
|
-
* elements in the linked list.
|
|
777
|
-
*/
|
|
778
|
-
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T {
|
|
779
|
-
let accumulator = initialValue;
|
|
780
|
-
let index = 0;
|
|
781
|
-
for (const current of this) {
|
|
782
|
-
accumulator = callback(accumulator, current, index, this);
|
|
783
|
-
index++;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
return accumulator;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
745
|
print(): void {
|
|
790
746
|
console.log([...this]);
|
|
791
747
|
}
|
|
748
|
+
|
|
749
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
750
|
+
let current = this.head;
|
|
751
|
+
|
|
752
|
+
while (current) {
|
|
753
|
+
yield current.value;
|
|
754
|
+
current = current.next;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
792
757
|
}
|
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
import { IterableWithSizeOrLength } from "../../types";
|
|
10
|
+
import { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
|
11
11
|
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
12
|
+
import { IterableElementBase } from "../base";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Deque can provide random access with O(1) time complexity
|
|
@@ -17,7 +18,7 @@ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
|
17
18
|
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
18
19
|
*/
|
|
19
20
|
|
|
20
|
-
export class Deque<E> {
|
|
21
|
+
export class Deque<E> extends IterableElementBase<E> {
|
|
21
22
|
protected _bucketFirst = 0;
|
|
22
23
|
protected _firstInBucket = 0;
|
|
23
24
|
protected _bucketLast = 0;
|
|
@@ -35,7 +36,7 @@ export class Deque<E> {
|
|
|
35
36
|
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
36
37
|
*/
|
|
37
38
|
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
|
|
38
|
-
|
|
39
|
+
super();
|
|
39
40
|
let _size: number;
|
|
40
41
|
if ('length' in elements) {
|
|
41
42
|
if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
|
|
@@ -699,67 +700,31 @@ export class Deque<E> {
|
|
|
699
700
|
return arr;
|
|
700
701
|
}
|
|
701
702
|
|
|
702
|
-
/**
|
|
703
|
-
* Time Complexity: O(n)
|
|
704
|
-
* Space Complexity: O(1)
|
|
705
|
-
*/
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Time Complexity: O(n)
|
|
709
|
-
* Space Complexity: O(1)
|
|
710
|
-
*
|
|
711
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
712
|
-
* object to be iterated over using a for...of loop.
|
|
713
|
-
*/
|
|
714
|
-
* [Symbol.iterator]() {
|
|
715
|
-
for (let i = 0; i < this.size; ++i) {
|
|
716
|
-
yield this.getAt(i);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/**
|
|
721
|
-
* Time Complexity: O(n)
|
|
722
|
-
* Space Complexity: O(1)
|
|
723
|
-
*/
|
|
724
|
-
|
|
725
|
-
/**
|
|
726
|
-
* Time Complexity: O(n)
|
|
727
|
-
* Space Complexity: O(1)
|
|
728
|
-
*
|
|
729
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
730
|
-
* each element.
|
|
731
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
732
|
-
* deque. It takes three parameters:
|
|
733
|
-
*/
|
|
734
|
-
forEach(callback: (element: E, index: number, deque: this) => void) {
|
|
735
|
-
let index = 0;
|
|
736
|
-
for (const el of this) {
|
|
737
|
-
callback(el, index, this);
|
|
738
|
-
index++;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
|
|
742
703
|
/**
|
|
743
704
|
* Time Complexity: O(n)
|
|
744
705
|
* Space Complexity: O(n)
|
|
745
706
|
*/
|
|
746
|
-
|
|
747
707
|
/**
|
|
748
708
|
* Time Complexity: O(n)
|
|
749
709
|
* Space Complexity: O(n)
|
|
750
710
|
*
|
|
751
|
-
* The `filter` function creates a new deque containing
|
|
752
|
-
* predicate function.
|
|
753
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
754
|
-
*
|
|
755
|
-
*
|
|
756
|
-
*
|
|
757
|
-
|
|
758
|
-
|
|
711
|
+
* The `filter` function creates a new deque containing elements from the original deque that satisfy
|
|
712
|
+
* a given predicate function.
|
|
713
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
714
|
+
* the current element being iterated over, the index of the current element, and the deque itself.
|
|
715
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
716
|
+
* deque or not.
|
|
717
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
718
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
719
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
720
|
+
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
721
|
+
* satisfy the given predicate function.
|
|
722
|
+
*/
|
|
723
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E> {
|
|
759
724
|
const newDeque = new Deque<E>([], this._bucketSize);
|
|
760
725
|
let index = 0;
|
|
761
726
|
for (const el of this) {
|
|
762
|
-
if (predicate(el, index, this)) {
|
|
727
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
763
728
|
newDeque.push(el);
|
|
764
729
|
}
|
|
765
730
|
index++;
|
|
@@ -771,21 +736,24 @@ export class Deque<E> {
|
|
|
771
736
|
* Time Complexity: O(n)
|
|
772
737
|
* Space Complexity: O(n)
|
|
773
738
|
*/
|
|
774
|
-
|
|
775
739
|
/**
|
|
776
740
|
* Time Complexity: O(n)
|
|
777
741
|
* Space Complexity: O(n)
|
|
778
742
|
*
|
|
779
|
-
* The `map` function
|
|
780
|
-
*
|
|
781
|
-
* @param callback - The `callback` parameter is a function that
|
|
782
|
-
*
|
|
783
|
-
|
|
784
|
-
|
|
743
|
+
* The `map` function creates a new Deque by applying a callback function to each element of the
|
|
744
|
+
* original Deque.
|
|
745
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
746
|
+
* the deque. It takes three arguments:
|
|
747
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
748
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
749
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
750
|
+
* @returns a new Deque object with the mapped values.
|
|
751
|
+
*/
|
|
752
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T> {
|
|
785
753
|
const newDeque = new Deque<T>([], this._bucketSize);
|
|
786
754
|
let index = 0;
|
|
787
755
|
for (const el of this) {
|
|
788
|
-
newDeque.push(callback(el, index, this));
|
|
756
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
789
757
|
index++;
|
|
790
758
|
}
|
|
791
759
|
return newDeque;
|
|
@@ -793,34 +761,24 @@ export class Deque<E> {
|
|
|
793
761
|
|
|
794
762
|
/**
|
|
795
763
|
* Time Complexity: O(n)
|
|
796
|
-
* Space Complexity: O(
|
|
764
|
+
* Space Complexity: O(n)
|
|
797
765
|
*/
|
|
798
766
|
|
|
767
|
+
print(): void {
|
|
768
|
+
console.log([...this])
|
|
769
|
+
}
|
|
770
|
+
|
|
799
771
|
/**
|
|
800
772
|
* Time Complexity: O(n)
|
|
801
773
|
* Space Complexity: O(1)
|
|
802
774
|
*
|
|
803
|
-
* The
|
|
804
|
-
*
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
810
|
-
* applying the callback function to each element.
|
|
811
|
-
*/
|
|
812
|
-
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T {
|
|
813
|
-
let accumulator = initialValue;
|
|
814
|
-
let index = 0;
|
|
815
|
-
for (const el of this) {
|
|
816
|
-
accumulator = callback(accumulator, el, index, this);
|
|
817
|
-
index++;
|
|
775
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
776
|
+
* object to be iterated over using a for...of loop.
|
|
777
|
+
*/
|
|
778
|
+
protected* _getIterator() {
|
|
779
|
+
for (let i = 0; i < this.size; ++i) {
|
|
780
|
+
yield this.getAt(i);
|
|
818
781
|
}
|
|
819
|
-
return accumulator;
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
print(): void {
|
|
823
|
-
console.log([...this])
|
|
824
782
|
}
|
|
825
783
|
|
|
826
784
|
/**
|