data-structure-typed 1.51.8 → 1.52.0

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 (216) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +152 -152
  5. package/dist/cjs/data-structures/base/index.d.ts +2 -1
  6. package/dist/cjs/data-structures/base/index.js +2 -1
  7. package/dist/cjs/data-structures/base/index.js.map +1 -1
  8. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +171 -0
  9. package/dist/cjs/data-structures/base/iterable-element-base.js +226 -0
  10. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -0
  11. package/dist/cjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  12. package/dist/cjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +13 -190
  13. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -0
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
  18. package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
  19. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
  21. package/dist/cjs/data-structures/binary-tree/binary-tree.js +475 -363
  22. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  23. package/dist/cjs/data-structures/binary-tree/bst.d.ts +192 -202
  24. package/dist/cjs/data-structures/binary-tree/bst.js +207 -249
  25. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
  27. package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
  28. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  29. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  30. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  31. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  32. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
  33. package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
  34. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  35. package/dist/cjs/data-structures/hash/hash-map.d.ts +33 -40
  36. package/dist/cjs/data-structures/hash/hash-map.js +40 -55
  37. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/heap.d.ts +43 -114
  39. package/dist/cjs/data-structures/heap/heap.js +59 -127
  40. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  41. package/dist/cjs/data-structures/heap/max-heap.d.ts +50 -4
  42. package/dist/cjs/data-structures/heap/max-heap.js +76 -10
  43. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  44. package/dist/cjs/data-structures/heap/min-heap.d.ts +51 -5
  45. package/dist/cjs/data-structures/heap/min-heap.js +68 -11
  46. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +26 -28
  49. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +29 -26
  52. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  53. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  54. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +79 -10
  55. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  56. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  57. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +71 -11
  58. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  59. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
  60. package/dist/cjs/data-structures/priority-queue/priority-queue.js +70 -1
  61. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  62. package/dist/cjs/data-structures/queue/deque.d.ts +21 -20
  63. package/dist/cjs/data-structures/queue/deque.js +29 -23
  64. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  65. package/dist/cjs/data-structures/queue/queue.d.ts +8 -28
  66. package/dist/cjs/data-structures/queue/queue.js +15 -31
  67. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  68. package/dist/cjs/data-structures/stack/stack.d.ts +17 -22
  69. package/dist/cjs/data-structures/stack/stack.js +25 -24
  70. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  71. package/dist/cjs/data-structures/trie/trie.d.ts +19 -14
  72. package/dist/cjs/data-structures/trie/trie.js +27 -16
  73. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  74. package/dist/cjs/interfaces/binary-tree.d.ts +7 -7
  75. package/dist/cjs/types/common.d.ts +1 -2
  76. package/dist/cjs/types/data-structures/base/base.d.ts +5 -2
  77. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  78. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  79. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
  80. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -5
  81. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  82. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  83. package/dist/cjs/types/data-structures/heap/heap.d.ts +3 -2
  84. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  85. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  86. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  87. package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -2
  88. package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -1
  89. package/dist/cjs/types/data-structures/stack/stack.d.ts +2 -1
  90. package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -2
  91. package/dist/cjs/utils/utils.js +3 -5
  92. package/dist/cjs/utils/utils.js.map +1 -1
  93. package/dist/mjs/data-structures/base/index.d.ts +2 -1
  94. package/dist/mjs/data-structures/base/index.js +2 -1
  95. package/dist/mjs/data-structures/base/iterable-element-base.d.ts +171 -0
  96. package/dist/mjs/data-structures/base/iterable-element-base.js +222 -0
  97. package/dist/mjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  98. package/dist/mjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +10 -186
  99. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  100. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  101. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
  102. package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
  103. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
  104. package/dist/mjs/data-structures/binary-tree/binary-tree.js +476 -364
  105. package/dist/mjs/data-structures/binary-tree/bst.d.ts +192 -202
  106. package/dist/mjs/data-structures/binary-tree/bst.js +211 -255
  107. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
  108. package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
  109. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  110. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  111. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
  112. package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
  113. package/dist/mjs/data-structures/hash/hash-map.d.ts +33 -40
  114. package/dist/mjs/data-structures/hash/hash-map.js +41 -55
  115. package/dist/mjs/data-structures/heap/heap.d.ts +43 -114
  116. package/dist/mjs/data-structures/heap/heap.js +60 -128
  117. package/dist/mjs/data-structures/heap/max-heap.d.ts +50 -4
  118. package/dist/mjs/data-structures/heap/max-heap.js +79 -10
  119. package/dist/mjs/data-structures/heap/min-heap.d.ts +51 -5
  120. package/dist/mjs/data-structures/heap/min-heap.js +68 -11
  121. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  122. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +26 -28
  123. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  124. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +29 -26
  125. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  126. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +82 -10
  127. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  128. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +71 -11
  129. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
  130. package/dist/mjs/data-structures/priority-queue/priority-queue.js +70 -1
  131. package/dist/mjs/data-structures/queue/deque.d.ts +21 -20
  132. package/dist/mjs/data-structures/queue/deque.js +29 -23
  133. package/dist/mjs/data-structures/queue/queue.d.ts +8 -28
  134. package/dist/mjs/data-structures/queue/queue.js +15 -31
  135. package/dist/mjs/data-structures/stack/stack.d.ts +17 -22
  136. package/dist/mjs/data-structures/stack/stack.js +25 -24
  137. package/dist/mjs/data-structures/trie/trie.d.ts +19 -14
  138. package/dist/mjs/data-structures/trie/trie.js +27 -16
  139. package/dist/mjs/interfaces/binary-tree.d.ts +7 -7
  140. package/dist/mjs/types/common.d.ts +1 -2
  141. package/dist/mjs/types/data-structures/base/base.d.ts +5 -2
  142. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  143. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  144. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
  145. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -5
  146. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  147. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  148. package/dist/mjs/types/data-structures/heap/heap.d.ts +3 -2
  149. package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  150. package/dist/mjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  151. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  152. package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -2
  153. package/dist/mjs/types/data-structures/queue/queue.d.ts +2 -1
  154. package/dist/mjs/types/data-structures/stack/stack.d.ts +2 -1
  155. package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -2
  156. package/dist/mjs/utils/utils.js +3 -5
  157. package/dist/umd/data-structure-typed.js +1949 -1481
  158. package/dist/umd/data-structure-typed.min.js +2 -2
  159. package/dist/umd/data-structure-typed.min.js.map +1 -1
  160. package/package.json +6 -6
  161. package/src/data-structures/base/index.ts +2 -1
  162. package/src/data-structures/base/iterable-element-base.ts +250 -0
  163. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  164. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
  165. package/src/data-structures/binary-tree/avl-tree.ts +96 -69
  166. package/src/data-structures/binary-tree/binary-tree.ts +535 -403
  167. package/src/data-structures/binary-tree/bst.ts +247 -277
  168. package/src/data-structures/binary-tree/rb-tree.ts +123 -103
  169. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
  170. package/src/data-structures/graph/abstract-graph.ts +10 -10
  171. package/src/data-structures/hash/hash-map.ts +46 -53
  172. package/src/data-structures/heap/heap.ts +71 -152
  173. package/src/data-structures/heap/max-heap.ts +88 -13
  174. package/src/data-structures/heap/min-heap.ts +78 -15
  175. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  176. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  177. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  178. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  179. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  180. package/src/data-structures/queue/deque.ts +37 -26
  181. package/src/data-structures/queue/queue.ts +23 -36
  182. package/src/data-structures/stack/stack.ts +31 -26
  183. package/src/data-structures/trie/trie.ts +35 -20
  184. package/src/interfaces/binary-tree.ts +9 -9
  185. package/src/types/common.ts +1 -2
  186. package/src/types/data-structures/base/base.ts +14 -6
  187. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  188. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
  189. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
  190. package/src/types/data-structures/binary-tree/bst.ts +4 -5
  191. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
  192. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
  193. package/src/types/data-structures/heap/heap.ts +4 -1
  194. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  195. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  196. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  197. package/src/types/data-structures/queue/deque.ts +3 -1
  198. package/src/types/data-structures/queue/queue.ts +3 -1
  199. package/src/types/data-structures/stack/stack.ts +3 -1
  200. package/src/types/data-structures/trie/trie.ts +3 -1
  201. package/src/utils/utils.ts +3 -3
  202. package/test/integration/avl-tree.test.ts +2 -2
  203. package/test/integration/bst.test.ts +3 -3
  204. package/test/integration/index.html +25 -11
  205. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
  206. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
  207. package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
  208. package/test/unit/data-structures/heap/heap.test.ts +80 -0
  209. package/test/unit/data-structures/heap/max-heap.test.ts +44 -1
  210. package/test/unit/data-structures/heap/min-heap.test.ts +18 -1
  211. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +42 -0
  212. package/test/unit/data-structures/queue/deque.test.ts +30 -0
  213. package/test/unit/data-structures/queue/queue.test.ts +56 -0
  214. package/test/utils/big-o.ts +12 -6
  215. package/tsconfig-base.json +1 -0
  216. package/dist/cjs/data-structures/base/iterable-base.js.map +0 -1
@@ -1,25 +1,82 @@
1
1
  import { Heap } from './heap';
2
2
  /**
3
3
  * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
4
- * 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
4
+ * 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
5
5
  * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
6
6
  * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
7
7
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
8
8
  * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
9
- * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
9
+ * 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
10
10
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
11
11
  */
12
12
  export class MinHeap extends Heap {
13
- constructor(elements = [], options = {
14
- comparator: (a, b) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- }
18
- else {
19
- return a - b;
13
+ constructor(elements = [], options) {
14
+ super(elements, options);
15
+ }
16
+ /**
17
+ * The `clone` function returns a new instance of the `MinHeap` class with the same comparator and
18
+ * toElementFn as the original instance.
19
+ * @returns The `clone()` method is returning a new instance of the `MinHeap` class with the same
20
+ * properties as the current instance.
21
+ */
22
+ clone() {
23
+ return new MinHeap(this, { comparator: this.comparator, toElementFn: this.toElementFn });
24
+ }
25
+ /**
26
+ * Time Complexity: O(n)
27
+ * Space Complexity: O(n)
28
+ *
29
+ * The `filter` function creates a new MinHeap object containing elements that pass a given callback
30
+ * function.
31
+ * @param callback - The `callback` parameter is a function that will be called for each element in
32
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
33
+ * heap itself. The callback function should return a boolean value indicating whether the current
34
+ * element should be included in the filtered list
35
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
36
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
37
+ * passed as the `this` value to the `callback` function. If `thisArg` is
38
+ * @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
39
+ * the filter condition specified by the `callback` function.
40
+ */
41
+ filter(callback, thisArg) {
42
+ const filteredList = new MinHeap([], { toElementFn: this.toElementFn, comparator: this.comparator });
43
+ let index = 0;
44
+ for (const current of this) {
45
+ if (callback.call(thisArg, current, index, this)) {
46
+ filteredList.add(current);
20
47
  }
48
+ index++;
21
49
  }
22
- }) {
23
- super(elements, options);
50
+ return filteredList;
51
+ }
52
+ /**
53
+ * Time Complexity: O(n log n)
54
+ * Space Complexity: O(n)
55
+ *
56
+ * The `map` function creates a new heap by applying a callback function to each element of the
57
+ * original heap.
58
+ * @param callback - The `callback` parameter is a function that will be called for each element in
59
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
60
+ * element), and `this` (the heap itself). The callback function should return a value of
61
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
62
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
63
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
64
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
65
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
66
+ * returns a value of type `T`. This function is used to transform the elements of the original
67
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
68
+ * specify the value of `this` within the callback function. It is used to set the context or scope
69
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
70
+ * value of
71
+ * @returns a new instance of the `MinHeap` class with the mapped elements.
72
+ */
73
+ map(callback, comparator, toElementFn, thisArg) {
74
+ const mappedHeap = new MinHeap([], { comparator, toElementFn });
75
+ let index = 0;
76
+ for (const el of this) {
77
+ mappedHeap.add(callback.call(thisArg, el, index, this));
78
+ index++;
79
+ }
80
+ return mappedHeap;
24
81
  }
25
82
  }
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from '../../types';
8
+ import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
9
9
  import { IterableElementBase } from '../base';
10
10
  export declare class DoublyLinkedListNode<E = any> {
11
11
  /**
@@ -60,14 +60,8 @@ export declare class DoublyLinkedListNode<E = any> {
60
60
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
61
61
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
62
62
  */
63
- export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
64
- /**
65
- * The constructor initializes a linked list with optional elements.
66
- * @param elements - The `elements` parameter is an optional iterable object that contains the
67
- * initial elements to be added to the data structure. It defaults to an empty array if no elements
68
- * are provided.
69
- */
70
- constructor(elements?: Iterable<E>);
63
+ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
64
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: DoublyLinkedListOptions<E, R>);
71
65
  protected _head: DoublyLinkedListNode<E> | undefined;
72
66
  /**
73
67
  * The `head` function returns the first node of a doubly linked list.
@@ -125,7 +119,7 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
125
119
  * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
126
120
  * @returns The `fromArray` function returns a DoublyLinkedList object.
127
121
  */
128
- static fromArray<E>(data: E[]): DoublyLinkedList<E>;
122
+ static fromArray<E>(data: E[]): DoublyLinkedList<E, any>;
129
123
  /**
130
124
  * Time Complexity: O(1)
131
125
  * Space Complexity: O(1)
@@ -393,7 +387,7 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
393
387
  * @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
394
388
  * is a copy of the original list.
395
389
  */
396
- clone(): DoublyLinkedList<E>;
390
+ clone(): DoublyLinkedList<E, R>;
397
391
  /**
398
392
  * Time Complexity: O(n)
399
393
  * Space Complexity: O(n)
@@ -415,29 +409,29 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
415
409
  * @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
416
410
  * elements that pass the filter condition specified by the `callback` function.
417
411
  */
418
- filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E>;
412
+ filter(callback: ElementCallback<E, R, boolean, DoublyLinkedList<E, R>>, thisArg?: any): DoublyLinkedList<E, R>;
419
413
  /**
420
414
  * Time Complexity: O(n)
421
415
  * Space Complexity: O(n)
422
416
  */
423
417
  /**
424
- * Time Complexity: O(n)
425
- * Space Complexity: O(n)
426
- *
427
- * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
428
- * in the original list.
418
+ * The `map` function takes a callback function and returns a new DoublyLinkedList with the results
419
+ * of applying the callback to each element in the original list.
429
420
  * @param callback - The callback parameter is a function that will be called for each element in the
430
- * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
431
- * and the DoublyLinkedList itself. The callback function should return a value that will be added to
432
- * the new DoublyLinkedList that
433
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
434
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
435
- * passed as the `this` value to the `callback` function. If `thisArg` is
436
- * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
437
- * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
438
- * object.
439
- */
440
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T>;
421
+ * original DoublyLinkedList. It takes three arguments: current (the current element being
422
+ * processed), index (the index of the current element), and this (the original DoublyLinkedList).
423
+ * The callback function should return a value of type
424
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
425
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
426
+ * input and returns the converted element. If this parameter is not provided, the raw element will
427
+ * be used as is.
428
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
429
+ * specify the value of `this` within the callback function. It is used to set the context or scope
430
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
431
+ * value of
432
+ * @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
433
+ */
434
+ map<EM, RM>(callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): DoublyLinkedList<EM, RM>;
441
435
  /**
442
436
  * The function returns an iterator that iterates over the values of a linked list.
443
437
  */
@@ -69,20 +69,18 @@ export class DoublyLinkedListNode {
69
69
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
70
70
  */
71
71
  export class DoublyLinkedList extends IterableElementBase {
72
- /**
73
- * The constructor initializes a linked list with optional elements.
74
- * @param elements - The `elements` parameter is an optional iterable object that contains the
75
- * initial elements to be added to the data structure. It defaults to an empty array if no elements
76
- * are provided.
77
- */
78
- constructor(elements = []) {
79
- super();
72
+ constructor(elements = [], options) {
73
+ super(options);
80
74
  this._head = undefined;
81
75
  this._tail = undefined;
82
76
  this._size = 0;
83
77
  if (elements) {
84
78
  for (const el of elements) {
85
- this.push(el);
79
+ if (this.toElementFn) {
80
+ this.push(this.toElementFn(el));
81
+ }
82
+ else
83
+ this.push(el);
86
84
  }
87
85
  }
88
86
  }
@@ -663,7 +661,7 @@ export class DoublyLinkedList extends IterableElementBase {
663
661
  * is a copy of the original list.
664
662
  */
665
663
  clone() {
666
- return new DoublyLinkedList(this.values());
664
+ return new DoublyLinkedList(this);
667
665
  }
668
666
  /**
669
667
  * Time Complexity: O(n)
@@ -687,7 +685,7 @@ export class DoublyLinkedList extends IterableElementBase {
687
685
  * elements that pass the filter condition specified by the `callback` function.
688
686
  */
689
687
  filter(callback, thisArg) {
690
- const filteredList = new DoublyLinkedList();
688
+ const filteredList = new DoublyLinkedList([], { toElementFn: this.toElementFn });
691
689
  let index = 0;
692
690
  for (const current of this) {
693
691
  if (callback.call(thisArg, current, index, this)) {
@@ -702,24 +700,24 @@ export class DoublyLinkedList extends IterableElementBase {
702
700
  * Space Complexity: O(n)
703
701
  */
704
702
  /**
705
- * Time Complexity: O(n)
706
- * Space Complexity: O(n)
707
- *
708
- * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
709
- * in the original list.
703
+ * The `map` function takes a callback function and returns a new DoublyLinkedList with the results
704
+ * of applying the callback to each element in the original list.
710
705
  * @param callback - The callback parameter is a function that will be called for each element in the
711
- * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
712
- * and the DoublyLinkedList itself. The callback function should return a value that will be added to
713
- * the new DoublyLinkedList that
714
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
715
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
716
- * passed as the `this` value to the `callback` function. If `thisArg` is
717
- * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
718
- * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
719
- * object.
720
- */
721
- map(callback, thisArg) {
722
- const mappedList = new DoublyLinkedList();
706
+ * original DoublyLinkedList. It takes three arguments: current (the current element being
707
+ * processed), index (the index of the current element), and this (the original DoublyLinkedList).
708
+ * The callback function should return a value of type
709
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
710
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
711
+ * input and returns the converted element. If this parameter is not provided, the raw element will
712
+ * be used as is.
713
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
714
+ * specify the value of `this` within the callback function. It is used to set the context or scope
715
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
716
+ * value of
717
+ * @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
718
+ */
719
+ map(callback, toElementFn, thisArg) {
720
+ const mappedList = new DoublyLinkedList([], { toElementFn });
723
721
  let index = 0;
724
722
  for (const current of this) {
725
723
  mappedList.push(callback.call(thisArg, current, index, this));
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from '../../types';
8
+ import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
9
9
  import { IterableElementBase } from '../base';
10
10
  export declare class SinglyLinkedListNode<E = any> {
11
11
  /**
@@ -40,14 +40,8 @@ export declare class SinglyLinkedListNode<E = any> {
40
40
  */
41
41
  set next(value: SinglyLinkedListNode<E> | undefined);
42
42
  }
43
- export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
44
- /**
45
- * The constructor initializes a new instance of a class with an optional iterable of elements.
46
- * @param elements - The `elements` parameter is an optional iterable object that contains the
47
- * initial elements to be added to the instance of the class. If no `elements` are provided, an empty
48
- * array will be used as the default value.
49
- */
50
- constructor(elements?: Iterable<E>);
43
+ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
44
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: SinglyLinkedListOptions<E, R>);
51
45
  protected _head: SinglyLinkedListNode<E> | undefined;
52
46
  /**
53
47
  * The `head` function returns the first node of a singly linked list.
@@ -93,7 +87,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
93
87
  * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
94
88
  * @returns The `fromArray` function returns a `SinglyLinkedList` object.
95
89
  */
96
- static fromArray<E>(data: E[]): SinglyLinkedList<E>;
90
+ static fromArray<E>(data: E[]): SinglyLinkedList<E, any>;
97
91
  /**
98
92
  * Time Complexity: O(1)
99
93
  * Space Complexity: O(1)
@@ -348,7 +342,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
348
342
  * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
349
343
  * is a clone of the original list.
350
344
  */
351
- clone(): SinglyLinkedList<E>;
345
+ clone(): SinglyLinkedList<E, R>;
352
346
  /**
353
347
  * Time Complexity: O(n)
354
348
  * Space Complexity: O(n)
@@ -370,26 +364,29 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
370
364
  * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
371
365
  * elements that pass the filter condition specified by the `callback` function.
372
366
  */
373
- filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E>;
367
+ filter(callback: ElementCallback<E, R, boolean, SinglyLinkedList<E, R>>, thisArg?: any): SinglyLinkedList<E, R>;
374
368
  /**
375
369
  * Time Complexity: O(n)
376
370
  * Space Complexity: O(n)
377
371
  */
378
372
  /**
379
- * Time Complexity: O(n)
380
- * Space Complexity: O(n)
381
- *
382
- * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
383
- * of the original list.
373
+ * The `map` function takes a callback function and returns a new SinglyLinkedList with the results
374
+ * of applying the callback to each element in the original list.
384
375
  * @param callback - The `callback` parameter is a function that will be called for each element in
385
- * the linked list. It takes three arguments:
386
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
387
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
388
- * passed as the `this` value to the `callback` function. If `thisArg` is
389
- * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
390
- * of applying the provided `callback` function to each element in the original list.
391
- */
392
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T>;
376
+ * the original list. It takes three arguments: `current` (the current element being processed),
377
+ * `index` (the index of the current element), and `this` (the original list). It should return a
378
+ * value
379
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
380
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
381
+ * input and returns the converted element. If this parameter is not provided, the raw element will
382
+ * be used as is.
383
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
384
+ * specify the value of `this` within the callback function. It is used to set the context or scope
385
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
386
+ * value of
387
+ * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
388
+ */
389
+ map<EM, RM>(callback: ElementCallback<E, R, EM, SinglyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): SinglyLinkedList<EM, RM>;
393
390
  /**
394
391
  * The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
395
392
  */
@@ -44,17 +44,17 @@ export class SinglyLinkedListNode {
44
44
  }
45
45
  }
46
46
  export class SinglyLinkedList extends IterableElementBase {
47
- /**
48
- * The constructor initializes a new instance of a class with an optional iterable of elements.
49
- * @param elements - The `elements` parameter is an optional iterable object that contains the
50
- * initial elements to be added to the instance of the class. If no `elements` are provided, an empty
51
- * array will be used as the default value.
52
- */
53
- constructor(elements = []) {
54
- super();
47
+ constructor(elements = [], options) {
48
+ super(options);
55
49
  if (elements) {
56
- for (const el of elements)
57
- this.push(el);
50
+ for (const el of elements) {
51
+ if (this.toElementFn) {
52
+ this.push(this.toElementFn(el));
53
+ }
54
+ else {
55
+ this.push(el);
56
+ }
57
+ }
58
58
  }
59
59
  }
60
60
  _head;
@@ -606,7 +606,7 @@ export class SinglyLinkedList extends IterableElementBase {
606
606
  * is a clone of the original list.
607
607
  */
608
608
  clone() {
609
- return new SinglyLinkedList(this.values());
609
+ return new SinglyLinkedList(this, { toElementFn: this.toElementFn });
610
610
  }
611
611
  /**
612
612
  * Time Complexity: O(n)
@@ -630,7 +630,7 @@ export class SinglyLinkedList extends IterableElementBase {
630
630
  * elements that pass the filter condition specified by the `callback` function.
631
631
  */
632
632
  filter(callback, thisArg) {
633
- const filteredList = new SinglyLinkedList();
633
+ const filteredList = new SinglyLinkedList([], { toElementFn: this.toElementFn });
634
634
  let index = 0;
635
635
  for (const current of this) {
636
636
  if (callback.call(thisArg, current, index, this)) {
@@ -645,21 +645,24 @@ export class SinglyLinkedList extends IterableElementBase {
645
645
  * Space Complexity: O(n)
646
646
  */
647
647
  /**
648
- * Time Complexity: O(n)
649
- * Space Complexity: O(n)
650
- *
651
- * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
652
- * of the original list.
648
+ * The `map` function takes a callback function and returns a new SinglyLinkedList with the results
649
+ * of applying the callback to each element in the original list.
653
650
  * @param callback - The `callback` parameter is a function that will be called for each element in
654
- * the linked list. It takes three arguments:
655
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
656
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
657
- * passed as the `this` value to the `callback` function. If `thisArg` is
658
- * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
659
- * of applying the provided `callback` function to each element in the original list.
660
- */
661
- map(callback, thisArg) {
662
- const mappedList = new SinglyLinkedList();
651
+ * the original list. It takes three arguments: `current` (the current element being processed),
652
+ * `index` (the index of the current element), and `this` (the original list). It should return a
653
+ * value
654
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
655
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
656
+ * input and returns the converted element. If this parameter is not provided, the raw element will
657
+ * be used as is.
658
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
659
+ * specify the value of `this` within the callback function. It is used to set the context or scope
660
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
661
+ * value of
662
+ * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
663
+ */
664
+ map(callback, toElementFn, thisArg) {
665
+ const mappedList = new SinglyLinkedList([], { toElementFn });
663
666
  let index = 0;
664
667
  for (const current of this) {
665
668
  mappedList.push(callback.call(thisArg, current, index, this));
@@ -5,9 +5,9 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { PriorityQueueOptions } from '../../types';
8
+ import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
- export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
10
+ export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
11
11
  /**
12
12
  * The constructor initializes a PriorityQueue with optional elements and options, including a
13
13
  * comparator function.
@@ -15,8 +15,54 @@ export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
15
15
  * elements to be added to the priority queue. It is optional and defaults to an empty array if not
16
16
  * provided.
17
17
  * @param options - The `options` parameter is an object that contains additional configuration
18
- * options for the priority queue. In this case, it has a property called `comparator` which is a
18
+ * options for the priority queue. In this case, it has a property called `comparator,` which is a
19
19
  * function used to compare elements in the priority queue.
20
20
  */
21
- constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
21
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
22
+ /**
23
+ * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
24
+ * comparator and toElementFn as the current instance.
25
+ * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
26
+ * comparator and toElementFn as the current instance.
27
+ */
28
+ clone(): MaxPriorityQueue<E, R>;
29
+ /**
30
+ * Time Complexity: O(n)
31
+ * Space Complexity: O(n)
32
+ *
33
+ * The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
34
+ * function.
35
+ * @param callback - The `callback` parameter is a function that will be called for each element in
36
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
37
+ * heap itself. The callback function should return a boolean value indicating whether the current
38
+ * element should be included in the filtered list
39
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
40
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
41
+ * passed as the `this` value to the `callback` function. If `thisArg` is
42
+ * @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
43
+ * the filter condition specified by the `callback` function.
44
+ */
45
+ filter(callback: ElementCallback<E, R, boolean, MaxPriorityQueue<E, R>>, thisArg?: any): MaxPriorityQueue<E, R>;
46
+ /**
47
+ * Time Complexity: O(n log n)
48
+ * Space Complexity: O(n)
49
+ *
50
+ * The `map` function creates a new heap by applying a callback function to each element of the
51
+ * original heap.
52
+ * @param callback - The `callback` parameter is a function that will be called for each element in
53
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
54
+ * element), and `this` (the heap itself). The callback function should return a value of
55
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
56
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
57
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
58
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
59
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
60
+ * returns a value of type `T`. This function is used to transform the elements of the original
61
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
62
+ * specify the value of `this` within the callback function. It is used to set the context or scope
63
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
64
+ * value of
65
+ * @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
66
+ */
67
+ map<EM, RM>(callback: ElementCallback<E, R, EM, MaxPriorityQueue<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MaxPriorityQueue<EM, RM>;
22
68
  }
@@ -7,19 +7,91 @@ export class MaxPriorityQueue extends PriorityQueue {
7
7
  * elements to be added to the priority queue. It is optional and defaults to an empty array if not
8
8
  * provided.
9
9
  * @param options - The `options` parameter is an object that contains additional configuration
10
- * options for the priority queue. In this case, it has a property called `comparator` which is a
10
+ * options for the priority queue. In this case, it has a property called `comparator,` which is a
11
11
  * function used to compare elements in the priority queue.
12
12
  */
13
- constructor(elements = [], options = {
14
- comparator: (a, b) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- }
18
- else {
19
- return b - a;
13
+ constructor(elements = [], options) {
14
+ super(elements, {
15
+ comparator: (a, b) => {
16
+ if (typeof a === 'object' || typeof b === 'object') {
17
+ throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
18
+ }
19
+ if (a < b)
20
+ return 1;
21
+ if (a > b)
22
+ return -1;
23
+ return 0;
24
+ },
25
+ ...options
26
+ });
27
+ }
28
+ /**
29
+ * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
30
+ * comparator and toElementFn as the current instance.
31
+ * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
32
+ * comparator and toElementFn as the current instance.
33
+ */
34
+ clone() {
35
+ return new MaxPriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
36
+ }
37
+ /**
38
+ * Time Complexity: O(n)
39
+ * Space Complexity: O(n)
40
+ *
41
+ * The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
42
+ * function.
43
+ * @param callback - The `callback` parameter is a function that will be called for each element in
44
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
45
+ * heap itself. The callback function should return a boolean value indicating whether the current
46
+ * element should be included in the filtered list
47
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
48
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
49
+ * passed as the `this` value to the `callback` function. If `thisArg` is
50
+ * @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
51
+ * the filter condition specified by the `callback` function.
52
+ */
53
+ filter(callback, thisArg) {
54
+ const filteredPriorityQueue = new MaxPriorityQueue([], {
55
+ toElementFn: this.toElementFn,
56
+ comparator: this.comparator
57
+ });
58
+ let index = 0;
59
+ for (const current of this) {
60
+ if (callback.call(thisArg, current, index, this)) {
61
+ filteredPriorityQueue.add(current);
20
62
  }
63
+ index++;
64
+ }
65
+ return filteredPriorityQueue;
66
+ }
67
+ /**
68
+ * Time Complexity: O(n log n)
69
+ * Space Complexity: O(n)
70
+ *
71
+ * The `map` function creates a new heap by applying a callback function to each element of the
72
+ * original heap.
73
+ * @param callback - The `callback` parameter is a function that will be called for each element in
74
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
75
+ * element), and `this` (the heap itself). The callback function should return a value of
76
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
77
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
78
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
79
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
80
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
81
+ * returns a value of type `T`. This function is used to transform the elements of the original
82
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
83
+ * specify the value of `this` within the callback function. It is used to set the context or scope
84
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
85
+ * value of
86
+ * @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
87
+ */
88
+ map(callback, comparator, toElementFn, thisArg) {
89
+ const mappedPriorityQueue = new MaxPriorityQueue([], { comparator, toElementFn });
90
+ let index = 0;
91
+ for (const el of this) {
92
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
93
+ index++;
21
94
  }
22
- }) {
23
- super(elements, options);
95
+ return mappedPriorityQueue;
24
96
  }
25
97
  }