data-structure-typed 1.50.1 → 1.50.3

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 (196) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +30 -28
  3. package/README_zh-CN.md +1 -1
  4. package/SPECIFICATION.md +56 -0
  5. package/SPONSOR-zh-CN.md +90 -0
  6. package/SPONSOR.md +54 -0
  7. package/benchmark/report.html +13 -13
  8. package/benchmark/report.json +149 -161
  9. package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
  10. package/dist/cjs/data-structures/base/iterable-base.js +143 -7
  11. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  16. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  17. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
  22. package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
  23. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  25. package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  28. package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
  29. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  31. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
  32. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
  34. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
  35. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
  37. package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
  38. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
  40. package/dist/cjs/data-structures/graph/map-graph.js +14 -0
  41. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  42. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
  43. package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
  44. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  45. package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
  46. package/dist/cjs/data-structures/hash/hash-map.js +347 -78
  47. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  48. package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
  49. package/dist/cjs/data-structures/heap/heap.js +95 -26
  50. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  52. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
  53. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  55. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
  56. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  57. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  58. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
  59. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  60. package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
  61. package/dist/cjs/data-structures/matrix/matrix.js +50 -11
  62. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  63. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  64. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  65. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  66. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  67. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  68. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  69. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  70. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
  71. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  72. package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
  73. package/dist/cjs/data-structures/queue/deque.js +200 -62
  74. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  75. package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
  76. package/dist/cjs/data-structures/queue/queue.js +111 -49
  77. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  78. package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
  79. package/dist/cjs/data-structures/stack/stack.js +58 -22
  80. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  81. package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
  82. package/dist/cjs/data-structures/tree/tree.js +77 -11
  83. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  84. package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
  85. package/dist/cjs/data-structures/trie/trie.js +153 -33
  86. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  87. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  88. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
  89. package/dist/cjs/types/utils/utils.d.ts +1 -0
  90. package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
  91. package/dist/mjs/data-structures/base/iterable-base.js +143 -7
  92. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  93. package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
  94. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  95. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  96. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  97. package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
  98. package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
  99. package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
  100. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  101. package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
  102. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  103. package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
  104. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  105. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
  106. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
  107. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
  108. package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
  109. package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
  110. package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
  111. package/dist/mjs/data-structures/graph/map-graph.js +14 -0
  112. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
  113. package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
  114. package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
  115. package/dist/mjs/data-structures/hash/hash-map.js +353 -84
  116. package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
  117. package/dist/mjs/data-structures/heap/heap.js +95 -26
  118. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  119. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
  120. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  121. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
  122. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  123. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
  124. package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
  125. package/dist/mjs/data-structures/matrix/matrix.js +50 -11
  126. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  127. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  128. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  129. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  130. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  131. package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
  132. package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
  133. package/dist/mjs/data-structures/queue/deque.js +205 -67
  134. package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
  135. package/dist/mjs/data-structures/queue/queue.js +111 -49
  136. package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
  137. package/dist/mjs/data-structures/stack/stack.js +58 -22
  138. package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
  139. package/dist/mjs/data-structures/tree/tree.js +80 -14
  140. package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
  141. package/dist/mjs/data-structures/trie/trie.js +156 -36
  142. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  143. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
  144. package/dist/mjs/types/utils/utils.d.ts +1 -0
  145. package/dist/umd/data-structure-typed.js +3121 -1583
  146. package/dist/umd/data-structure-typed.min.js +3 -3
  147. package/dist/umd/data-structure-typed.min.js.map +1 -1
  148. package/package.json +61 -55
  149. package/src/data-structures/base/iterable-base.ts +184 -19
  150. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  151. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  152. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  153. package/src/data-structures/binary-tree/bst.ts +127 -136
  154. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  155. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  156. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  157. package/src/data-structures/graph/abstract-graph.ts +4 -211
  158. package/src/data-structures/graph/directed-graph.ts +152 -0
  159. package/src/data-structures/graph/map-graph.ts +15 -0
  160. package/src/data-structures/graph/undirected-graph.ts +171 -19
  161. package/src/data-structures/hash/hash-map.ts +389 -96
  162. package/src/data-structures/heap/heap.ts +97 -26
  163. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  164. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  165. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  166. package/src/data-structures/matrix/matrix.ts +52 -12
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  170. package/src/data-structures/queue/deque.ts +225 -70
  171. package/src/data-structures/queue/queue.ts +118 -49
  172. package/src/data-structures/stack/stack.ts +63 -23
  173. package/src/data-structures/tree/tree.ts +89 -15
  174. package/src/data-structures/trie/trie.ts +173 -38
  175. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  176. package/src/types/data-structures/hash/hash-map.ts +4 -3
  177. package/src/types/utils/utils.ts +2 -0
  178. package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
  179. package/test/performance/data-structures/queue/deque.test.ts +26 -25
  180. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
  181. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
  182. package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
  183. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
  184. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
  185. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  186. package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
  187. package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
  188. package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
  189. package/test/unit/data-structures/heap/heap.test.ts +18 -1
  190. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
  191. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  193. package/test/unit/data-structures/queue/deque.test.ts +65 -5
  194. package/test/unit/data-structures/queue/queue.test.ts +24 -5
  195. package/test/unit/data-structures/stack/stack.test.ts +17 -0
  196. package/test/unit/data-structures/trie/trie.test.ts +17 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.50.1",
3
+ "version": "1.50.3",
4
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",
@@ -93,13 +93,53 @@
93
93
  },
94
94
  "keywords": [
95
95
  "data structure",
96
- "data-structure",
96
+ "data structures",
97
97
  "datastructure",
98
- "javascript data structure",
99
- "typescript data structure",
100
- "js data structure",
98
+ "datastructures",
101
99
  "data",
102
100
  "structure",
101
+ "structures",
102
+ "min",
103
+ "max",
104
+ "heap",
105
+ "priority",
106
+ "queue",
107
+ "min heap",
108
+ "min-heap",
109
+ "max heap",
110
+ "max-heap",
111
+ "priority queue",
112
+ "priority-queue",
113
+ "max priority queue",
114
+ "max-priority-queue",
115
+ "min priority queue",
116
+ "min-priority-queue",
117
+ "binary search tree",
118
+ "binary-search-tree",
119
+ "BST",
120
+ "binary tree",
121
+ "binary-tree",
122
+ "red black tree",
123
+ "red black tree",
124
+ "red-black-tree",
125
+ "rb tree",
126
+ "trie",
127
+ "prefix tree",
128
+ "prefix-tree",
129
+ "avl tree",
130
+ "avl-tree",
131
+ "tree set",
132
+ "tree-set",
133
+ "tree multiset",
134
+ "tree-multiset",
135
+ "tree map",
136
+ "tree-map",
137
+ "tree multimap",
138
+ "tree-multimap",
139
+ "binary indexed tree",
140
+ "binary-indexed-tree",
141
+ "segment tree",
142
+ "segment-tree",
103
143
  "binary",
104
144
  "depth",
105
145
  "breadth",
@@ -119,18 +159,26 @@
119
159
  "set",
120
160
  "multiset",
121
161
  "multimap",
122
- "directed",
123
- "undirected",
124
- "graph",
125
- "min",
126
- "max",
127
- "heap",
128
- "priority",
129
- "queue",
130
162
  "singly",
131
163
  "doubly",
132
164
  "linked",
133
165
  "list",
166
+ "linked list",
167
+ "linked-list",
168
+ "singly linked list",
169
+ "singly-linked-list",
170
+ "doubly linked list",
171
+ "doubly-linked-list",
172
+ "javascript data structure",
173
+ "javascript data structures",
174
+ "typescript data structures",
175
+ "js data structure",
176
+ "js data structures",
177
+ "data-structure",
178
+ "data-structures",
179
+ "directed",
180
+ "undirected",
181
+ "graph",
134
182
  "js",
135
183
  "ts",
136
184
  "javascript",
@@ -154,32 +202,6 @@
154
202
  "java.util",
155
203
  "Java",
156
204
  "util",
157
- "binary search tree",
158
- "binary-search-tree",
159
- "BST",
160
- "binary tree",
161
- "binary-tree",
162
- "red black tree",
163
- "red black tree",
164
- "red-black-tree",
165
- "rb tree",
166
- "trie",
167
- "prefix tree",
168
- "prefix-tree",
169
- "avl tree",
170
- "avl-tree",
171
- "tree set",
172
- "tree-set",
173
- "tree multiset",
174
- "tree-multiset",
175
- "tree map",
176
- "tree-map",
177
- "tree multimap",
178
- "tree-multimap",
179
- "binary indexed tree",
180
- "binary-indexed-tree",
181
- "segment tree",
182
- "segment-tree",
183
205
  "sort",
184
206
  "sorted",
185
207
  "order",
@@ -207,28 +229,12 @@
207
229
  "directed-graph",
208
230
  "undirected graph",
209
231
  "undirected-graph",
210
- "min heap",
211
- "min-heap",
212
- "max heap",
213
- "max-heap",
214
- "priority queue",
215
- "priority-queue",
216
- "max priority queue",
217
- "max-priority-queue",
218
- "min priority queue",
219
- "min-priority-queue",
220
232
  "hash",
221
233
  "map",
222
234
  "hashmap",
223
235
  "hash map",
224
236
  "hash-map",
225
237
  "deque",
226
- "linked list",
227
- "linked-list",
228
- "singly linked list",
229
- "singly-linked-list",
230
- "doubly linked list",
231
- "doubly-linked-list",
232
238
  "stack",
233
239
  "CommonJS",
234
240
  "ES6",
@@ -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,20 @@ export abstract class IterableEntryBase<K = any, V = any> {
195
287
  console.log([...this]);
196
288
  }
197
289
 
290
+ abstract isEmpty(): boolean;
291
+
292
+ abstract clear(): void;
293
+
294
+ abstract clone(): any;
295
+
296
+ abstract map(...args: any[]): any;
297
+
298
+ abstract filter(...args: any[]): any;
299
+
198
300
  protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
199
301
  }
200
302
 
201
- export abstract class IterableElementBase<V> {
303
+ export abstract class IterableElementBase<E = any, C = any> {
202
304
  /**
203
305
  * Time Complexity: O(n)
204
306
  * Space Complexity: O(1)
@@ -212,7 +314,7 @@ export abstract class IterableElementBase<V> {
212
314
  * allows the function to accept any number of arguments as an array. In this case, the `args`
213
315
  * parameter is used to pass any number of arguments to the `_getIterator` method.
214
316
  */
215
- * [Symbol.iterator](...args: any[]): IterableIterator<V> {
317
+ * [Symbol.iterator](...args: any[]): IterableIterator<E> {
216
318
  yield* this._getIterator(...args);
217
319
  }
218
320
 
@@ -226,7 +328,7 @@ export abstract class IterableElementBase<V> {
226
328
  *
227
329
  * The function returns an iterator that yields all the values in the object.
228
330
  */
229
- * values(): IterableIterator<V> {
331
+ * values(): IterableIterator<E> {
230
332
  for (const item of this) {
231
333
  yield item;
232
334
  }
@@ -250,10 +352,10 @@ export abstract class IterableElementBase<V> {
250
352
  * @returns The `every` method is returning a boolean value. It returns `true` if every element in
251
353
  * the array satisfies the provided predicate function, and `false` otherwise.
252
354
  */
253
- every(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
355
+ every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
254
356
  let index = 0;
255
357
  for (const item of this) {
256
- if (!predicate.call(thisArg, item as V, index++, this)) {
358
+ if (!predicate.call(thisArg, item, index++, this)) {
257
359
  return false;
258
360
  }
259
361
  }
@@ -278,10 +380,10 @@ export abstract class IterableElementBase<V> {
278
380
  * @returns a boolean value. It returns true if the predicate function returns true for any element
279
381
  * in the collection, and false otherwise.
280
382
  */
281
- some(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
383
+ some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
282
384
  let index = 0;
283
385
  for (const item of this) {
284
- if (predicate.call(thisArg, item as V, index++, this)) {
386
+ if (predicate.call(thisArg, item, index++, this)) {
285
387
  return true;
286
388
  }
287
389
  }
@@ -292,6 +394,7 @@ export abstract class IterableElementBase<V> {
292
394
  * Time Complexity: O(n)
293
395
  * Space Complexity: O(1)
294
396
  */
397
+
295
398
  /**
296
399
  * Time Complexity: O(n)
297
400
  * Space Complexity: O(1)
@@ -305,11 +408,63 @@ export abstract class IterableElementBase<V> {
305
408
  * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
306
409
  * be passed as the `this` value to the `callbackfn` function. If `thisArg
307
410
  */
308
- forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void {
411
+ forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void {
412
+ let index = 0;
413
+ for (const item of this) {
414
+ callbackfn.call(thisArg, item, index++, this);
415
+ }
416
+ }
417
+
418
+ /**
419
+ * Time Complexity: O(n)
420
+ * Space Complexity: O(1)
421
+ */
422
+
423
+ /**
424
+ * Time Complexity: O(n)
425
+ * Space Complexity: O(1)
426
+ *
427
+ * The `find` function iterates over the elements of an array-like object and returns the first
428
+ * element that satisfies the provided callback function.
429
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
430
+ * the array. It takes three arguments: the current element being processed, the index of the current
431
+ * element, and the array itself. The function should return a boolean value indicating whether the
432
+ * current element matches the desired condition.
433
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
434
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
435
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
436
+ * @returns The `find` method returns the first element in the array that satisfies the provided
437
+ * callback function. If no element satisfies the callback function, `undefined` is returned.
438
+ */
439
+ find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined {
309
440
  let index = 0;
310
441
  for (const item of this) {
311
- callbackfn.call(thisArg, item as V, index++, this);
442
+ if (callbackfn.call(thisArg, item, index++, this)) return item;
443
+ }
444
+
445
+ return;
446
+ }
447
+
448
+ /**
449
+ * Time Complexity: O(n)
450
+ * Space Complexity: O(1)
451
+ */
452
+
453
+ /**
454
+ * Time Complexity: O(n)
455
+ * Space Complexity: O(1)
456
+ *
457
+ * The function checks if a given element exists in a collection.
458
+ * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
459
+ * represents the element that we want to check for existence in the collection.
460
+ * @returns a boolean value. It returns true if the element is found in the collection, and false
461
+ * otherwise.
462
+ */
463
+ has(element: E): boolean {
464
+ for (const ele of this) {
465
+ if (ele === element) return true;
312
466
  }
467
+ return false;
313
468
  }
314
469
 
315
470
  /**
@@ -329,11 +484,11 @@ export abstract class IterableElementBase<V> {
329
484
  * @returns The `reduce` method is returning the final value of the accumulator after iterating over
330
485
  * all the elements in the array and applying the callback function to each element.
331
486
  */
332
- reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U {
487
+ reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U {
333
488
  let accumulator = initialValue;
334
489
  let index = 0;
335
490
  for (const item of this) {
336
- accumulator = callbackfn(accumulator, item as V, index++, this);
491
+ accumulator = callbackfn(accumulator, item as E, index++, this);
337
492
  }
338
493
  return accumulator;
339
494
  }
@@ -346,5 +501,15 @@ export abstract class IterableElementBase<V> {
346
501
  console.log([...this]);
347
502
  }
348
503
 
349
- protected abstract _getIterator(...args: any[]): IterableIterator<V>;
504
+ abstract isEmpty(): boolean;
505
+
506
+ abstract clear(): void;
507
+
508
+ abstract clone(): C;
509
+
510
+ abstract map(...args: any[]): any;
511
+
512
+ abstract filter(...args: any[]): any;
513
+
514
+ protected abstract _getIterator(...args: any[]): IterableIterator<E>;
350
515
  }