data-structure-typed 1.47.4 → 1.47.6

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.
Files changed (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +2 -2
  3. package/benchmark/report.json +12 -18
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
  11. package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  19. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  20. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  21. package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
  22. package/dist/cjs/data-structures/hash/hash-map.js +10 -4
  23. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
  25. package/dist/cjs/data-structures/hash/hash-table.js +50 -5
  26. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
  28. package/dist/cjs/data-structures/heap/heap.js +101 -41
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  31. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  32. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  33. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  34. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  35. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  36. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  37. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  38. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  39. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  40. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
  41. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  42. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  43. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  44. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  45. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  46. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  47. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  48. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  49. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  50. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  51. package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
  52. package/dist/cjs/data-structures/queue/deque.js +81 -71
  53. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
  55. package/dist/cjs/data-structures/queue/queue.js +80 -0
  56. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  57. package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
  58. package/dist/cjs/data-structures/stack/stack.js +65 -8
  59. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  60. package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
  61. package/dist/cjs/data-structures/trie/trie.js +47 -0
  62. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  63. package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
  64. package/dist/cjs/types/common.d.ts +2 -0
  65. package/dist/cjs/types/common.js.map +1 -1
  66. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  68. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  69. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  70. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  71. package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
  72. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  73. package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
  74. package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
  75. package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
  76. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  77. package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
  78. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  79. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
  80. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  81. package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
  82. package/dist/mjs/data-structures/hash/hash-map.js +10 -4
  83. package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
  84. package/dist/mjs/data-structures/hash/hash-table.js +50 -5
  85. package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
  86. package/dist/mjs/data-structures/heap/heap.js +102 -42
  87. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  88. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  89. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  90. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  91. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  93. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
  95. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  96. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  97. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  98. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  99. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  100. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  101. package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
  102. package/dist/mjs/data-structures/queue/deque.js +81 -71
  103. package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
  104. package/dist/mjs/data-structures/queue/queue.js +80 -0
  105. package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
  106. package/dist/mjs/data-structures/stack/stack.js +65 -8
  107. package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
  108. package/dist/mjs/data-structures/trie/trie.js +47 -0
  109. package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
  110. package/dist/mjs/types/common.d.ts +2 -0
  111. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  112. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  113. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  114. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  115. package/dist/umd/data-structure-typed.js +856 -509
  116. package/dist/umd/data-structure-typed.min.js +2 -2
  117. package/dist/umd/data-structure-typed.min.js.map +1 -1
  118. package/package.json +1 -1
  119. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  120. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  121. package/src/data-structures/binary-tree/bst.ts +67 -47
  122. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  123. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  124. package/src/data-structures/graph/abstract-graph.ts +1 -1
  125. package/src/data-structures/hash/hash-map.ts +13 -7
  126. package/src/data-structures/hash/hash-table.ts +59 -9
  127. package/src/data-structures/heap/heap.ts +115 -46
  128. package/src/data-structures/heap/max-heap.ts +5 -5
  129. package/src/data-structures/heap/min-heap.ts +5 -5
  130. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  131. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  132. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  133. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  134. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  135. package/src/data-structures/queue/deque.ts +86 -75
  136. package/src/data-structures/queue/queue.ts +88 -0
  137. package/src/data-structures/stack/stack.ts +75 -10
  138. package/src/data-structures/trie/trie.ts +53 -0
  139. package/src/interfaces/binary-tree.ts +13 -1
  140. package/src/types/common.ts +5 -1
  141. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  142. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  143. package/src/types/data-structures/heap/heap.ts +3 -1
  144. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  145. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  146. package/test/performance/data-structures/heap/heap.test.ts +14 -5
  147. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  148. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
  149. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
  150. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  151. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
  152. package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
  153. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  154. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  155. package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
  156. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  157. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  158. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  159. package/test/unit/data-structures/queue/queue.test.ts +37 -0
  160. package/test/unit/data-structures/stack/stack.test.ts +55 -5
  161. package/test/unit/data-structures/trie/trie.test.ts +33 -0
  162. package/test/unit/unrestricted-interconversion.test.ts +100 -0
@@ -22,10 +22,14 @@ export class SinglyLinkedList {
22
22
  /**
23
23
  * The constructor initializes the linked list with an empty head, tail, and length.
24
24
  */
25
- constructor() {
25
+ constructor(elements) {
26
26
  this._head = null;
27
27
  this._tail = null;
28
28
  this._length = 0;
29
+ if (elements) {
30
+ for (const el of elements)
31
+ this.push(el);
32
+ }
29
33
  }
30
34
  _head;
31
35
  get head() {
@@ -606,58 +610,42 @@ export class SinglyLinkedList {
606
610
  return count;
607
611
  }
608
612
  /**
609
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
610
- * Space Complexity: O(1) - Constant space.
611
- */
612
- /**
613
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
614
- * Space Complexity: O(1) - Constant space.
615
- *
616
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
617
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
618
- * represents the value of the current node in the linked list, and the index argument represents the index of the
619
- * current node in the linked list.
613
+ * The function returns an iterator that iterates over the values of a linked list.
620
614
  */
621
- forEach(callback) {
615
+ *[Symbol.iterator]() {
622
616
  let current = this.head;
623
- let index = 0;
624
617
  while (current) {
625
- callback(current.value, index);
618
+ yield current.value;
626
619
  current = current.next;
627
- index++;
628
620
  }
629
621
  }
630
622
  /**
631
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
632
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
623
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
624
+ * Space Complexity: O(1)
633
625
  */
634
626
  /**
635
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
636
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
627
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
628
+ * Space Complexity: O(1)
637
629
  *
638
- * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
639
- * SinglyLinkedList with the transformed values.
640
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
641
- * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
642
- * SinglyLinkedList).
643
- * @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
630
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
631
+ * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
632
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
633
+ * current node in the linked list.
644
634
  */
645
- map(callback) {
646
- const mappedList = new SinglyLinkedList();
647
- let current = this.head;
648
- while (current) {
649
- mappedList.push(callback(current.value));
650
- current = current.next;
635
+ forEach(callback) {
636
+ let index = 0;
637
+ for (const el of this) {
638
+ callback(el, index, this);
639
+ index++;
651
640
  }
652
- return mappedList;
653
641
  }
654
642
  /**
655
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
656
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
643
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
644
+ * Space Complexity: O(n)
657
645
  */
658
646
  /**
659
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
660
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
647
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
648
+ * Space Complexity: O(n)
661
649
  *
662
650
  * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
663
651
  * elements that satisfy the given callback function.
@@ -667,49 +655,66 @@ export class SinglyLinkedList {
667
655
  */
668
656
  filter(callback) {
669
657
  const filteredList = new SinglyLinkedList();
670
- let current = this.head;
671
- while (current) {
672
- if (callback(current.value)) {
673
- filteredList.push(current.value);
658
+ let index = 0;
659
+ for (const current of this) {
660
+ if (callback(current, index, this)) {
661
+ filteredList.push(current);
674
662
  }
675
- current = current.next;
663
+ index++;
676
664
  }
677
665
  return filteredList;
678
666
  }
679
667
  /**
680
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
681
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
668
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
669
+ * Space Complexity: O(n)
682
670
  */
683
671
  /**
684
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
685
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
672
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
673
+ * Space Complexity: O(n)
674
+ *
675
+ * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
676
+ * SinglyLinkedList with the transformed values.
677
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
678
+ * the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
679
+ * SinglyLinkedList).
680
+ * @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
681
+ */
682
+ map(callback) {
683
+ const mappedList = new SinglyLinkedList();
684
+ let index = 0;
685
+ for (const current of this) {
686
+ mappedList.push(callback(current, index, this));
687
+ index++;
688
+ }
689
+ return mappedList;
690
+ }
691
+ /**
692
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
693
+ * Space Complexity: O(n)
694
+ */
695
+ /**
696
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
697
+ * Space Complexity: O(n)
686
698
  *
687
699
  * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
688
700
  * single value.
689
701
  * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
690
702
  * used to perform a specific operation on each element of the linked list.
691
- * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
703
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
692
704
  * point for the reduction operation.
693
705
  * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
694
706
  * elements in the linked list.
695
707
  */
696
708
  reduce(callback, initialValue) {
697
709
  let accumulator = initialValue;
698
- let current = this.head;
699
- while (current) {
700
- accumulator = callback(accumulator, current.value);
701
- current = current.next;
710
+ let index = 0;
711
+ for (const current of this) {
712
+ accumulator = callback(accumulator, current, index, this);
713
+ index++;
702
714
  }
703
715
  return accumulator;
704
716
  }
705
- /**
706
- * The function returns an iterator that iterates over the values of a linked list.
707
- */
708
- *[Symbol.iterator]() {
709
- let current = this.head;
710
- while (current) {
711
- yield current.value;
712
- current = current.next;
713
- }
717
+ print() {
718
+ console.log([...this]);
714
719
  }
715
720
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
- import type { Comparator } from '../../types';
9
+ import type { PriorityQueueOptions } from '../../types';
10
10
  export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  export class MaxPriorityQueue extends PriorityQueue {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MaxPriorityQueue extends PriorityQueue {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
- import type { Comparator } from '../../types';
9
+ import type { PriorityQueueOptions } from '../../types';
10
10
  export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  export class MinPriorityQueue extends PriorityQueue {
10
- constructor(options = {
10
+ constructor(elements, options = {
11
11
  comparator: (a, b) => {
12
12
  if (!(typeof a === 'number' && typeof b === 'number')) {
13
13
  throw new Error('The a, b params of compare function must be number');
@@ -17,6 +17,6 @@ export class MinPriorityQueue extends PriorityQueue {
17
17
  }
18
18
  }
19
19
  }) {
20
- super(options);
20
+ super(elements, options);
21
21
  }
22
22
  }
@@ -6,10 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { Heap } from '../heap';
9
- import { Comparator } from '../../types';
9
+ import { PriorityQueueOptions } from '../../types';
10
10
  export declare class PriorityQueue<E = any> extends Heap<E> {
11
- constructor(options: {
12
- comparator: Comparator<E>;
13
- nodes?: E[];
14
- });
11
+ constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
15
12
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { Heap } from '../heap';
9
9
  export class PriorityQueue extends Heap {
10
- constructor(options) {
11
- super(options);
10
+ constructor(elements, options) {
11
+ super(elements, options);
12
12
  }
13
13
  }
@@ -318,12 +318,14 @@ export declare class Deque<E> {
318
318
  * Time Complexity: O(n)
319
319
  * Space Complexity: O(1)
320
320
  *
321
- * The `forEach` function iterates over each element in a deque and applies a callback function to
322
- * each element.
323
- * @param callback - The callback parameter is a function that will be called for each element in the
324
- * deque. It takes three parameters:
321
+ * The `find` function iterates over the elements in a deque and returns the first element for which
322
+ * the callback function returns true, or undefined if no such element is found.
323
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
324
+ * return a boolean value indicating whether the element satisfies a certain condition.
325
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
326
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
325
327
  */
326
- forEach(callback: (element: E, index: number, deque: Deque<E>) => void): void;
328
+ find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined;
327
329
  /**
328
330
  * Time Complexity: O(n)
329
331
  * Space Complexity: O(1)
@@ -332,14 +334,14 @@ export declare class Deque<E> {
332
334
  * Time Complexity: O(n)
333
335
  * Space Complexity: O(1)
334
336
  *
335
- * The `find` function iterates over the elements in a deque and returns the first element for which
336
- * the callback function returns true, or undefined if no such element is found.
337
- * @param callback - A function that takes three parameters: element, index, and deque. It should
338
- * return a boolean value indicating whether the element satisfies a certain condition.
339
- * @returns The method `find` returns the first element in the deque that satisfies the condition
340
- * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
337
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
338
+ * or -1 if the element is not found.
339
+ * @param {E} element - The "element" parameter represents the element that you want to find the
340
+ * index of in the data structure.
341
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
342
+ * in the data structure. If the element is not found, it returns -1.
341
343
  */
342
- find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined;
344
+ indexOf(element: E): number;
343
345
  /**
344
346
  * Time Complexity: O(n)
345
347
  * Space Complexity: O(n)
@@ -354,18 +356,30 @@ export declare class Deque<E> {
354
356
  toArray(): E[];
355
357
  /**
356
358
  * Time Complexity: O(n)
357
- * Space Complexity: O(n)
359
+ * Space Complexity: O(1)
358
360
  */
359
361
  /**
360
362
  * Time Complexity: O(n)
361
- * Space Complexity: O(n)
363
+ * Space Complexity: O(1)
362
364
  *
363
- * The `map` function takes a callback function and applies it to each element in the deque,
364
- * returning a new deque with the results.
365
- * @param callback - The `callback` parameter is a function that takes three arguments:
366
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
365
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
366
+ * object to be iterated over using a for...of loop.
367
+ */
368
+ [Symbol.iterator](): Generator<E, void, unknown>;
369
+ /**
370
+ * Time Complexity: O(n)
371
+ * Space Complexity: O(1)
372
+ */
373
+ /**
374
+ * Time Complexity: O(n)
375
+ * Space Complexity: O(1)
376
+ *
377
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
378
+ * each element.
379
+ * @param callback - The callback parameter is a function that will be called for each element in the
380
+ * deque. It takes three parameters:
367
381
  */
368
- map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T>;
382
+ forEach(callback: (element: E, index: number, deque: this) => void): void;
369
383
  /**
370
384
  * Time Complexity: O(n)
371
385
  * Space Complexity: O(n)
@@ -381,7 +395,21 @@ export declare class Deque<E> {
381
395
  * @returns The `filter` method is returning a new `Deque` object that contains only the elements
382
396
  * that satisfy the given `predicate` function.
383
397
  */
384
- filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E>;
398
+ filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E>;
399
+ /**
400
+ * Time Complexity: O(n)
401
+ * Space Complexity: O(n)
402
+ */
403
+ /**
404
+ * Time Complexity: O(n)
405
+ * Space Complexity: O(n)
406
+ *
407
+ * The `map` function takes a callback function and applies it to each element in the deque,
408
+ * returning a new deque with the results.
409
+ * @param callback - The `callback` parameter is a function that takes three arguments:
410
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
411
+ */
412
+ map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T>;
385
413
  /**
386
414
  * Time Complexity: O(n)
387
415
  * Space Complexity: O(1)
@@ -399,35 +427,8 @@ export declare class Deque<E> {
399
427
  * @returns the final value of the accumulator after iterating over all elements in the deque and
400
428
  * applying the callback function to each element.
401
429
  */
402
- reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T;
403
- /**
404
- * Time Complexity: O(n)
405
- * Space Complexity: O(1)
406
- */
407
- /**
408
- * Time Complexity: O(n)
409
- * Space Complexity: O(1)
410
- *
411
- * The function "indexOf" returns the index of the first occurrence of a given element in an array,
412
- * or -1 if the element is not found.
413
- * @param {E} element - The "element" parameter represents the element that you want to find the
414
- * index of in the data structure.
415
- * @returns The indexOf function returns the index of the first occurrence of the specified element
416
- * in the data structure. If the element is not found, it returns -1.
417
- */
418
- indexOf(element: E): number;
419
- /**
420
- * Time Complexity: O(n)
421
- * Space Complexity: O(1)
422
- */
423
- /**
424
- * Time Complexity: O(n)
425
- * Space Complexity: O(1)
426
- *
427
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
428
- * object to be iterated over using a for...of loop.
429
- */
430
- [Symbol.iterator](): Generator<E, void, unknown>;
430
+ reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T;
431
+ print(): void;
431
432
  /**
432
433
  * Time Complexity: O(n)
433
434
  * Space Complexity: O(n)
@@ -592,15 +592,21 @@ export class Deque {
592
592
  * Time Complexity: O(n)
593
593
  * Space Complexity: O(1)
594
594
  *
595
- * The `forEach` function iterates over each element in a deque and applies a callback function to
596
- * each element.
597
- * @param callback - The callback parameter is a function that will be called for each element in the
598
- * deque. It takes three parameters:
595
+ * The `find` function iterates over the elements in a deque and returns the first element for which
596
+ * the callback function returns true, or undefined if no such element is found.
597
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
598
+ * return a boolean value indicating whether the element satisfies a certain condition.
599
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
600
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
599
601
  */
600
- forEach(callback) {
602
+ find(callback) {
601
603
  for (let i = 0; i < this.size; ++i) {
602
- callback(this.getAt(i), i, this);
604
+ const element = this.getAt(i);
605
+ if (callback(element, i, this)) {
606
+ return element;
607
+ }
603
608
  }
609
+ return undefined;
604
610
  }
605
611
  /**
606
612
  * Time Complexity: O(n)
@@ -610,21 +616,20 @@ export class Deque {
610
616
  * Time Complexity: O(n)
611
617
  * Space Complexity: O(1)
612
618
  *
613
- * The `find` function iterates over the elements in a deque and returns the first element for which
614
- * the callback function returns true, or undefined if no such element is found.
615
- * @param callback - A function that takes three parameters: element, index, and deque. It should
616
- * return a boolean value indicating whether the element satisfies a certain condition.
617
- * @returns The method `find` returns the first element in the deque that satisfies the condition
618
- * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
619
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
620
+ * or -1 if the element is not found.
621
+ * @param {E} element - The "element" parameter represents the element that you want to find the
622
+ * index of in the data structure.
623
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
624
+ * in the data structure. If the element is not found, it returns -1.
619
625
  */
620
- find(callback) {
626
+ indexOf(element) {
621
627
  for (let i = 0; i < this.size; ++i) {
622
- const element = this.getAt(i);
623
- if (callback(element, i, this)) {
624
- return element;
628
+ if (this.getAt(i) === element) {
629
+ return i;
625
630
  }
626
631
  }
627
- return undefined;
632
+ return -1;
628
633
  }
629
634
  /**
630
635
  * Time Complexity: O(n)
@@ -646,23 +651,39 @@ export class Deque {
646
651
  }
647
652
  /**
648
653
  * Time Complexity: O(n)
649
- * Space Complexity: O(n)
654
+ * Space Complexity: O(1)
650
655
  */
651
656
  /**
652
657
  * Time Complexity: O(n)
653
- * Space Complexity: O(n)
658
+ * Space Complexity: O(1)
654
659
  *
655
- * The `map` function takes a callback function and applies it to each element in the deque,
656
- * returning a new deque with the results.
657
- * @param callback - The `callback` parameter is a function that takes three arguments:
658
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
660
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
661
+ * object to be iterated over using a for...of loop.
659
662
  */
660
- map(callback) {
661
- const newDeque = new Deque([], this._bucketSize);
663
+ *[Symbol.iterator]() {
662
664
  for (let i = 0; i < this.size; ++i) {
663
- newDeque.push(callback(this.getAt(i), i, this));
665
+ yield this.getAt(i);
666
+ }
667
+ }
668
+ /**
669
+ * Time Complexity: O(n)
670
+ * Space Complexity: O(1)
671
+ */
672
+ /**
673
+ * Time Complexity: O(n)
674
+ * Space Complexity: O(1)
675
+ *
676
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
677
+ * each element.
678
+ * @param callback - The callback parameter is a function that will be called for each element in the
679
+ * deque. It takes three parameters:
680
+ */
681
+ forEach(callback) {
682
+ let index = 0;
683
+ for (const el of this) {
684
+ callback(el, index, this);
685
+ index++;
664
686
  }
665
- return newDeque;
666
687
  }
667
688
  /**
668
689
  * Time Complexity: O(n)
@@ -681,11 +702,34 @@ export class Deque {
681
702
  */
682
703
  filter(predicate) {
683
704
  const newDeque = new Deque([], this._bucketSize);
684
- for (let i = 0; i < this.size; ++i) {
685
- const element = this.getAt(i);
686
- if (predicate(element, i, this)) {
687
- newDeque.push(element);
705
+ let index = 0;
706
+ for (const el of this) {
707
+ if (predicate(el, index, this)) {
708
+ newDeque.push(el);
688
709
  }
710
+ index++;
711
+ }
712
+ return newDeque;
713
+ }
714
+ /**
715
+ * Time Complexity: O(n)
716
+ * Space Complexity: O(n)
717
+ */
718
+ /**
719
+ * Time Complexity: O(n)
720
+ * Space Complexity: O(n)
721
+ *
722
+ * The `map` function takes a callback function and applies it to each element in the deque,
723
+ * returning a new deque with the results.
724
+ * @param callback - The `callback` parameter is a function that takes three arguments:
725
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
726
+ */
727
+ map(callback) {
728
+ const newDeque = new Deque([], this._bucketSize);
729
+ let index = 0;
730
+ for (const el of this) {
731
+ newDeque.push(callback(el, index, this));
732
+ index++;
689
733
  }
690
734
  return newDeque;
691
735
  }
@@ -708,49 +752,15 @@ export class Deque {
708
752
  */
709
753
  reduce(callback, initialValue) {
710
754
  let accumulator = initialValue;
711
- for (let i = 0; i < this.size; ++i) {
712
- accumulator = callback(accumulator, this.getAt(i), i, this);
755
+ let index = 0;
756
+ for (const el of this) {
757
+ accumulator = callback(accumulator, el, index, this);
758
+ index++;
713
759
  }
714
760
  return accumulator;
715
761
  }
716
- /**
717
- * Time Complexity: O(n)
718
- * Space Complexity: O(1)
719
- */
720
- /**
721
- * Time Complexity: O(n)
722
- * Space Complexity: O(1)
723
- *
724
- * The function "indexOf" returns the index of the first occurrence of a given element in an array,
725
- * or -1 if the element is not found.
726
- * @param {E} element - The "element" parameter represents the element that you want to find the
727
- * index of in the data structure.
728
- * @returns The indexOf function returns the index of the first occurrence of the specified element
729
- * in the data structure. If the element is not found, it returns -1.
730
- */
731
- indexOf(element) {
732
- for (let i = 0; i < this.size; ++i) {
733
- if (this.getAt(i) === element) {
734
- return i;
735
- }
736
- }
737
- return -1;
738
- }
739
- /**
740
- * Time Complexity: O(n)
741
- * Space Complexity: O(1)
742
- */
743
- /**
744
- * Time Complexity: O(n)
745
- * Space Complexity: O(1)
746
- *
747
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
748
- * object to be iterated over using a for...of loop.
749
- */
750
- *[Symbol.iterator]() {
751
- for (let i = 0; i < this.size; ++i) {
752
- yield this.getAt(i);
753
- }
762
+ print() {
763
+ console.log([...this]);
754
764
  }
755
765
  /**
756
766
  * Time Complexity: O(n)