data-structure-typed 1.50.0 → 1.50.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.
Files changed (156) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +204 -212
  3. package/benchmark/report.html +37 -1
  4. package/benchmark/report.json +370 -22
  5. package/dist/cjs/data-structures/base/iterable-base.d.ts +114 -9
  6. package/dist/cjs/data-structures/base/iterable-base.js +143 -7
  7. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +68 -71
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +54 -74
  15. package/dist/cjs/data-structures/binary-tree/bst.js +30 -71
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +84 -89
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +64 -85
  22. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
  24. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
  25. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/graph/directed-graph.d.ts +14 -0
  27. package/dist/cjs/data-structures/graph/directed-graph.js +26 -0
  28. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
  30. package/dist/cjs/data-structures/graph/map-graph.js +14 -0
  31. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  32. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +16 -0
  33. package/dist/cjs/data-structures/graph/undirected-graph.js +25 -0
  34. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  35. package/dist/cjs/data-structures/hash/hash-map.d.ts +121 -15
  36. package/dist/cjs/data-structures/hash/hash-map.js +160 -25
  37. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/heap.d.ts +66 -6
  39. package/dist/cjs/data-structures/heap/heap.js +66 -6
  40. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +70 -64
  43. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +130 -112
  46. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  47. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  48. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
  49. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
  51. package/dist/cjs/data-structures/matrix/matrix.js +50 -11
  52. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  53. package/dist/cjs/data-structures/queue/deque.d.ts +49 -19
  54. package/dist/cjs/data-structures/queue/deque.js +101 -47
  55. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  56. package/dist/cjs/data-structures/queue/queue.d.ts +39 -5
  57. package/dist/cjs/data-structures/queue/queue.js +47 -5
  58. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  59. package/dist/cjs/data-structures/stack/stack.d.ts +16 -0
  60. package/dist/cjs/data-structures/stack/stack.js +22 -0
  61. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  62. package/dist/cjs/data-structures/trie/trie.d.ts +38 -1
  63. package/dist/cjs/data-structures/trie/trie.js +41 -0
  64. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  65. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  66. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
  67. package/dist/cjs/types/utils/utils.d.ts +1 -0
  68. package/dist/mjs/data-structures/base/iterable-base.d.ts +114 -9
  69. package/dist/mjs/data-structures/base/iterable-base.js +143 -7
  70. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
  71. package/dist/mjs/data-structures/binary-tree/avl-tree.js +68 -71
  72. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  73. package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
  74. package/dist/mjs/data-structures/binary-tree/bst.d.ts +54 -74
  75. package/dist/mjs/data-structures/binary-tree/bst.js +30 -71
  76. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
  77. package/dist/mjs/data-structures/binary-tree/rb-tree.js +84 -89
  78. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
  79. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +64 -85
  80. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
  81. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
  82. package/dist/mjs/data-structures/graph/directed-graph.d.ts +14 -0
  83. package/dist/mjs/data-structures/graph/directed-graph.js +26 -0
  84. package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
  85. package/dist/mjs/data-structures/graph/map-graph.js +14 -0
  86. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +16 -0
  87. package/dist/mjs/data-structures/graph/undirected-graph.js +25 -0
  88. package/dist/mjs/data-structures/hash/hash-map.d.ts +121 -15
  89. package/dist/mjs/data-structures/hash/hash-map.js +160 -25
  90. package/dist/mjs/data-structures/heap/heap.d.ts +66 -6
  91. package/dist/mjs/data-structures/heap/heap.js +66 -6
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
  93. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +70 -64
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
  95. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +130 -112
  96. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  97. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
  98. package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
  99. package/dist/mjs/data-structures/matrix/matrix.js +50 -11
  100. package/dist/mjs/data-structures/queue/deque.d.ts +49 -19
  101. package/dist/mjs/data-structures/queue/deque.js +101 -47
  102. package/dist/mjs/data-structures/queue/queue.d.ts +39 -5
  103. package/dist/mjs/data-structures/queue/queue.js +47 -5
  104. package/dist/mjs/data-structures/stack/stack.d.ts +16 -0
  105. package/dist/mjs/data-structures/stack/stack.js +22 -0
  106. package/dist/mjs/data-structures/trie/trie.d.ts +38 -1
  107. package/dist/mjs/data-structures/trie/trie.js +41 -0
  108. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  109. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
  110. package/dist/mjs/types/utils/utils.d.ts +1 -0
  111. package/dist/umd/data-structure-typed.js +1730 -1042
  112. package/dist/umd/data-structure-typed.min.js +3 -3
  113. package/dist/umd/data-structure-typed.min.js.map +1 -1
  114. package/package.json +16 -40
  115. package/src/data-structures/base/iterable-base.ts +172 -19
  116. package/src/data-structures/binary-tree/avl-tree.ts +97 -97
  117. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  118. package/src/data-structures/binary-tree/bst.ts +89 -131
  119. package/src/data-structures/binary-tree/rb-tree.ts +127 -155
  120. package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
  121. package/src/data-structures/graph/abstract-graph.ts +4 -0
  122. package/src/data-structures/graph/directed-graph.ts +30 -0
  123. package/src/data-structures/graph/map-graph.ts +15 -0
  124. package/src/data-structures/graph/undirected-graph.ts +28 -0
  125. package/src/data-structures/hash/hash-map.ts +175 -34
  126. package/src/data-structures/heap/heap.ts +66 -6
  127. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
  128. package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
  129. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  130. package/src/data-structures/matrix/matrix.ts +52 -12
  131. package/src/data-structures/queue/deque.ts +108 -49
  132. package/src/data-structures/queue/queue.ts +51 -5
  133. package/src/data-structures/stack/stack.ts +24 -0
  134. package/src/data-structures/trie/trie.ts +45 -1
  135. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  136. package/src/types/data-structures/hash/hash-map.ts +4 -3
  137. package/src/types/utils/utils.ts +2 -0
  138. package/test/performance/data-structures/graph/directed-graph.test.ts +3 -3
  139. package/test/performance/data-structures/queue/deque.test.ts +26 -25
  140. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
  141. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
  142. package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
  143. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
  144. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
  145. package/test/unit/data-structures/graph/abstract-graph.test.ts +8 -0
  146. package/test/unit/data-structures/graph/directed-graph.test.ts +249 -0
  147. package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
  148. package/test/unit/data-structures/heap/heap.test.ts +18 -1
  149. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
  150. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
  151. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  152. package/test/unit/data-structures/queue/deque.test.ts +71 -10
  153. package/test/unit/data-structures/queue/queue.test.ts +23 -6
  154. package/test/unit/data-structures/stack/stack.test.ts +17 -0
  155. package/test/unit/data-structures/trie/trie.test.ts +17 -0
  156. package/test/unit/unrestricted-interconversion.test.ts +143 -10
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.50.0",
4
- "description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
3
+ "version": "1.50.2",
4
+ "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
7
7
  "types": "dist/mjs/index.d.ts",
@@ -92,17 +92,19 @@
92
92
  "typescript": "^5.3.2"
93
93
  },
94
94
  "keywords": [
95
+ "data structure",
96
+ "data structures",
97
+ "javascript data structure",
98
+ "javascript data structures",
99
+ "typescript data structure",
100
+ "typescript data structures",
101
+ "js data structure",
102
+ "js data structures",
103
+ "data-structure",
104
+ "datastructure",
95
105
  "data",
96
106
  "structure",
97
107
  "structures",
98
- "data structure",
99
- "datastructure",
100
- "data-structure",
101
- "data structures",
102
- "datastructures",
103
- "data-structures",
104
- "in data structures",
105
- "in data structure",
106
108
  "binary",
107
109
  "depth",
108
110
  "breadth",
@@ -112,7 +114,7 @@
112
114
  "avl",
113
115
  "red",
114
116
  "black",
115
- "redblack",
117
+ "red black",
116
118
  "RB",
117
119
  "segment",
118
120
  "prefix",
@@ -150,7 +152,6 @@
150
152
  "Python",
151
153
  "collections",
152
154
  "Python Collections",
153
- "pythoncollections",
154
155
  "python-collections",
155
156
  "C#",
156
157
  "System.Collections.Generic",
@@ -159,41 +160,30 @@
159
160
  "Java",
160
161
  "util",
161
162
  "binary search tree",
162
- "binarysearchtree",
163
163
  "binary-search-tree",
164
164
  "BST",
165
165
  "binary tree",
166
- "binarytree",
167
166
  "binary-tree",
168
167
  "red black tree",
169
- "redblacktree",
170
- "redblack tree",
168
+ "red black tree",
171
169
  "red-black-tree",
172
- "redblack-tree",
170
+ "rb tree",
173
171
  "trie",
174
172
  "prefix tree",
175
- "prefixtree",
176
173
  "prefix-tree",
177
174
  "avl tree",
178
- "avltree",
179
175
  "avl-tree",
180
176
  "tree set",
181
- "treeset",
182
177
  "tree-set",
183
178
  "tree multiset",
184
- "treemultiset",
185
179
  "tree-multiset",
186
180
  "tree map",
187
- "treemap",
188
181
  "tree-map",
189
182
  "tree multimap",
190
- "treemultimap",
191
183
  "tree-multimap",
192
184
  "binary indexed tree",
193
- "binaryindexedtree",
194
185
  "binary-indexed-tree",
195
186
  "segment tree",
196
- "segmenttree",
197
187
  "segment-tree",
198
188
  "sort",
199
189
  "sorted",
@@ -203,60 +193,46 @@
203
193
  "morris",
204
194
  "Morris",
205
195
  "bellman ford",
206
- "bellmanford",
207
196
  "bellman-ford",
208
197
  "dijkstra",
209
198
  "Dijkstra",
210
199
  "floyd warshall",
211
- "floydwarshall",
212
200
  "floyd-warshall",
213
201
  "tarjan",
214
202
  "tarjan's",
215
203
  "dfs",
216
204
  "depth first Search",
217
- "depthfirstSearch",
218
205
  "depth-first-Search",
219
206
  "bfs",
220
207
  "breadth first search",
221
- "breadthfirstsearch",
222
208
  "dfs iterative",
223
209
  "recursive",
224
210
  "iterative",
225
211
  "directed graph",
226
- "directedgraph",
227
212
  "directed-graph",
228
213
  "undirected graph",
229
- "undirectedgraph",
230
214
  "undirected-graph",
231
215
  "min heap",
232
- "minheap",
233
216
  "min-heap",
234
217
  "max heap",
235
- "maxheap",
236
218
  "max-heap",
237
219
  "priority queue",
238
- "priorityqueue",
239
220
  "priority-queue",
240
221
  "max priority queue",
241
- "maxpriorityqueue",
242
222
  "max-priority-queue",
243
223
  "min priority queue",
244
- "minpriorityqueue",
245
224
  "min-priority-queue",
246
225
  "hash",
247
226
  "map",
248
- "hash map",
249
227
  "hashmap",
228
+ "hash map",
250
229
  "hash-map",
251
230
  "deque",
252
231
  "linked list",
253
- "linkedlist",
254
232
  "linked-list",
255
233
  "singly linked list",
256
- "singlylinkedlist",
257
234
  "singly-linked-list",
258
235
  "doubly linked list",
259
- "doublylinkedlist",
260
236
  "doubly-linked-list",
261
237
  "stack",
262
238
  "CommonJS",
@@ -154,6 +154,105 @@ export abstract class IterableEntryBase<K = any, V = any> {
154
154
  * Time Complexity: O(n)
155
155
  * Space Complexity: O(1)
156
156
  */
157
+
158
+ /**
159
+ * Time Complexity: O(n)
160
+ * Space Complexity: O(1)
161
+ *
162
+ * The `find` function iterates over the entries of a collection and returns the first value for
163
+ * which the callback function returns true.
164
+ * @param callbackfn - The callback function that will be called for each entry in the collection. It
165
+ * takes three arguments: the value of the entry, the key of the entry, and the index of the entry in
166
+ * the collection. It should return a boolean value indicating whether the current entry matches the
167
+ * desired condition.
168
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
169
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
170
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
171
+ * @returns The method `find` returns the value of the first element in the iterable that satisfies
172
+ * the provided callback function. If no element satisfies the callback function, `undefined` is
173
+ * returned.
174
+ */
175
+ find(callbackfn: EntryCallback<K, V, [K, V]>, thisArg?: any): [K, V] | undefined {
176
+ let index = 0;
177
+ for (const item of this) {
178
+ const [key, value] = item;
179
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
180
+ }
181
+ return;
182
+ }
183
+
184
+ /**
185
+ * Time Complexity: O(n)
186
+ * Space Complexity: O(1)
187
+ */
188
+
189
+ /**
190
+ * Time Complexity: O(n)
191
+ * Space Complexity: O(1)
192
+ *
193
+ * The function checks if a given key exists in a collection.
194
+ * @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents
195
+ * the key that we want to check for existence in the data structure.
196
+ * @returns a boolean value. It returns true if the key is found in the collection, and false
197
+ * otherwise.
198
+ */
199
+ has(key: K): boolean {
200
+ for (const item of this) {
201
+ const [itemKey] = item;
202
+ if (itemKey === key) return true;
203
+ }
204
+ return false;
205
+ }
206
+
207
+ /**
208
+ * Time Complexity: O(n)
209
+ * Space Complexity: O(1)
210
+ */
211
+
212
+ /**
213
+ * Time Complexity: O(n)
214
+ * Space Complexity: O(1)
215
+ *
216
+ * The function checks if a given value exists in a collection.
217
+ * @param {V} value - The parameter "value" is the value that we want to check if it exists in the
218
+ * collection.
219
+ * @returns a boolean value, either true or false.
220
+ */
221
+ hasValue(value: V): boolean {
222
+ for (const [, elementValue] of this) {
223
+ if (elementValue === value) return true;
224
+ }
225
+ return false;
226
+ }
227
+
228
+ /**
229
+ * Time Complexity: O(n)
230
+ * Space Complexity: O(1)
231
+ */
232
+
233
+ /**
234
+ * Time Complexity: O(n)
235
+ * Space Complexity: O(1)
236
+ *
237
+ * The `get` function retrieves the value associated with a given key from a collection.
238
+ * @param {K} key - K (the type of the key) - This parameter represents the key that is being
239
+ * searched for in the collection.
240
+ * @returns The `get` method returns the value associated with the specified key if it exists in the
241
+ * collection, otherwise it returns `undefined`.
242
+ */
243
+ get(key: K): V | undefined {
244
+ for (const item of this) {
245
+ const [itemKey, value] = item;
246
+ if (itemKey === key) return value;
247
+ }
248
+ return;
249
+ }
250
+
251
+ /**
252
+ * Time Complexity: O(n)
253
+ * Space Complexity: O(1)
254
+ */
255
+
157
256
  /**
158
257
  * Time Complexity: O(n)
159
258
  * Space Complexity: O(1)
@@ -180,13 +279,6 @@ export abstract class IterableEntryBase<K = any, V = any> {
180
279
  return accumulator;
181
280
  }
182
281
 
183
- hasValue(value: V): boolean {
184
- for (const [, elementValue] of this) {
185
- if (elementValue === value) return true;
186
- }
187
- return false;
188
- }
189
-
190
282
  /**
191
283
  * Time Complexity: O(n)
192
284
  * Space Complexity: O(n)
@@ -195,10 +287,14 @@ export abstract class IterableEntryBase<K = any, V = any> {
195
287
  console.log([...this]);
196
288
  }
197
289
 
290
+ abstract isEmpty(): boolean;
291
+
292
+ abstract clone(): any;
293
+
198
294
  protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
199
295
  }
200
296
 
201
- export abstract class IterableElementBase<V> {
297
+ export abstract class IterableElementBase<E = any, C = any> {
202
298
  /**
203
299
  * Time Complexity: O(n)
204
300
  * Space Complexity: O(1)
@@ -212,7 +308,7 @@ export abstract class IterableElementBase<V> {
212
308
  * allows the function to accept any number of arguments as an array. In this case, the `args`
213
309
  * parameter is used to pass any number of arguments to the `_getIterator` method.
214
310
  */
215
- * [Symbol.iterator](...args: any[]): IterableIterator<V> {
311
+ * [Symbol.iterator](...args: any[]): IterableIterator<E> {
216
312
  yield* this._getIterator(...args);
217
313
  }
218
314
 
@@ -226,7 +322,7 @@ export abstract class IterableElementBase<V> {
226
322
  *
227
323
  * The function returns an iterator that yields all the values in the object.
228
324
  */
229
- * values(): IterableIterator<V> {
325
+ * values(): IterableIterator<E> {
230
326
  for (const item of this) {
231
327
  yield item;
232
328
  }
@@ -250,10 +346,10 @@ export abstract class IterableElementBase<V> {
250
346
  * @returns The `every` method is returning a boolean value. It returns `true` if every element in
251
347
  * the array satisfies the provided predicate function, and `false` otherwise.
252
348
  */
253
- every(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
349
+ every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
254
350
  let index = 0;
255
351
  for (const item of this) {
256
- if (!predicate.call(thisArg, item as V, index++, this)) {
352
+ if (!predicate.call(thisArg, item, index++, this)) {
257
353
  return false;
258
354
  }
259
355
  }
@@ -278,10 +374,10 @@ export abstract class IterableElementBase<V> {
278
374
  * @returns a boolean value. It returns true if the predicate function returns true for any element
279
375
  * in the collection, and false otherwise.
280
376
  */
281
- some(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
377
+ some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
282
378
  let index = 0;
283
379
  for (const item of this) {
284
- if (predicate.call(thisArg, item as V, index++, this)) {
380
+ if (predicate.call(thisArg, item, index++, this)) {
285
381
  return true;
286
382
  }
287
383
  }
@@ -292,6 +388,7 @@ export abstract class IterableElementBase<V> {
292
388
  * Time Complexity: O(n)
293
389
  * Space Complexity: O(1)
294
390
  */
391
+
295
392
  /**
296
393
  * Time Complexity: O(n)
297
394
  * Space Complexity: O(1)
@@ -305,13 +402,65 @@ export abstract class IterableElementBase<V> {
305
402
  * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
306
403
  * be passed as the `this` value to the `callbackfn` function. If `thisArg
307
404
  */
308
- forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void {
405
+ forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void {
309
406
  let index = 0;
310
407
  for (const item of this) {
311
- callbackfn.call(thisArg, item as V, index++, this);
408
+ callbackfn.call(thisArg, item, index++, this);
312
409
  }
313
410
  }
314
411
 
412
+ /**
413
+ * Time Complexity: O(n)
414
+ * Space Complexity: O(1)
415
+ */
416
+
417
+ /**
418
+ * Time Complexity: O(n)
419
+ * Space Complexity: O(1)
420
+ *
421
+ * The `find` function iterates over the elements of an array-like object and returns the first
422
+ * element that satisfies the provided callback function.
423
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
424
+ * the array. It takes three arguments: the current element being processed, the index of the current
425
+ * element, and the array itself. The function should return a boolean value indicating whether the
426
+ * current element matches the desired condition.
427
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
428
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
429
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
430
+ * @returns The `find` method returns the first element in the array that satisfies the provided
431
+ * callback function. If no element satisfies the callback function, `undefined` is returned.
432
+ */
433
+ find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined {
434
+ let index = 0;
435
+ for (const item of this) {
436
+ if (callbackfn.call(thisArg, item, index++, this)) return item;
437
+ }
438
+
439
+ return;
440
+ }
441
+
442
+ /**
443
+ * Time Complexity: O(n)
444
+ * Space Complexity: O(1)
445
+ */
446
+
447
+ /**
448
+ * Time Complexity: O(n)
449
+ * Space Complexity: O(1)
450
+ *
451
+ * The function checks if a given element exists in a collection.
452
+ * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
453
+ * represents the element that we want to check for existence in the collection.
454
+ * @returns a boolean value. It returns true if the element is found in the collection, and false
455
+ * otherwise.
456
+ */
457
+ has(element: E): boolean {
458
+ for (const ele of this) {
459
+ if (ele === element) return true;
460
+ }
461
+ return false;
462
+ }
463
+
315
464
  /**
316
465
  * Time Complexity: O(n)
317
466
  * Space Complexity: O(1)
@@ -329,11 +478,11 @@ export abstract class IterableElementBase<V> {
329
478
  * @returns The `reduce` method is returning the final value of the accumulator after iterating over
330
479
  * all the elements in the array and applying the callback function to each element.
331
480
  */
332
- reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U {
481
+ reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U {
333
482
  let accumulator = initialValue;
334
483
  let index = 0;
335
484
  for (const item of this) {
336
- accumulator = callbackfn(accumulator, item as V, index++, this);
485
+ accumulator = callbackfn(accumulator, item as E, index++, this);
337
486
  }
338
487
  return accumulator;
339
488
  }
@@ -346,5 +495,9 @@ export abstract class IterableElementBase<V> {
346
495
  console.log([...this]);
347
496
  }
348
497
 
349
- protected abstract _getIterator(...args: any[]): IterableIterator<V>;
498
+ abstract isEmpty(): boolean;
499
+
500
+ abstract clone(): C;
501
+
502
+ protected abstract _getIterator(...args: any[]): IterableIterator<E>;
350
503
  }