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.
- package/CHANGELOG.md +2 -1
- package/README.md +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- 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.
|
|
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
|
|
96
|
+
"data structures",
|
|
97
97
|
"datastructure",
|
|
98
|
-
"
|
|
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<
|
|
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<
|
|
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<
|
|
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<
|
|
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
|
|
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<
|
|
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
|
|
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<
|
|
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
|
|
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<
|
|
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
|
|
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
|
-
|
|
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
|
}
|