deque-typed 1.48.0 → 1.48.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.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +45 -40
- package/dist/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +334 -83
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/index.ts +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './iterable-base';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./iterable-base"), exports);
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { ElementCallback, PairCallback, ReduceElementCallback, ReducePairCallback } from "../../types";
|
|
2
|
+
export declare abstract class IterablePairBase<K = any, V = any> {
|
|
3
|
+
/**
|
|
4
|
+
* Time Complexity: O(n)
|
|
5
|
+
* Space Complexity: O(1)
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Time Complexity: O(n)
|
|
9
|
+
* Space Complexity: O(1)
|
|
10
|
+
*
|
|
11
|
+
* The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
|
|
12
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
13
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
14
|
+
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
15
|
+
*/
|
|
16
|
+
[Symbol.iterator](...args: any[]): IterableIterator<[K, V]>;
|
|
17
|
+
/**
|
|
18
|
+
* Time Complexity: O(n)
|
|
19
|
+
* Space Complexity: O(n)
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Time Complexity: O(n)
|
|
23
|
+
* Space Complexity: O(n)
|
|
24
|
+
*
|
|
25
|
+
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
26
|
+
* be undefined.
|
|
27
|
+
*/
|
|
28
|
+
entries(): IterableIterator<[K, V | undefined]>;
|
|
29
|
+
/**
|
|
30
|
+
* Time Complexity: O(n)
|
|
31
|
+
* Space Complexity: O(n)
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Time Complexity: O(n)
|
|
35
|
+
* Space Complexity: O(n)
|
|
36
|
+
*
|
|
37
|
+
* The function returns an iterator that yields the keys of a data structure.
|
|
38
|
+
*/
|
|
39
|
+
keys(): IterableIterator<K>;
|
|
40
|
+
/**
|
|
41
|
+
* Time Complexity: O(n)
|
|
42
|
+
* Space Complexity: O(n)
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Time Complexity: O(n)
|
|
46
|
+
* Space Complexity: O(n)
|
|
47
|
+
*
|
|
48
|
+
* The function returns an iterator that yields the values of a collection.
|
|
49
|
+
*/
|
|
50
|
+
values(): IterableIterator<V>;
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(n)
|
|
53
|
+
* Space Complexity: O(1)
|
|
54
|
+
*/
|
|
55
|
+
/**
|
|
56
|
+
* Time Complexity: O(n)
|
|
57
|
+
* Space Complexity: O(1)
|
|
58
|
+
*
|
|
59
|
+
* The `every` function checks if every element in a collection satisfies a given condition.
|
|
60
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
61
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
62
|
+
* met for the current element in the iteration.
|
|
63
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
64
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
65
|
+
* passed as the first argument to the `predicate` function. If `thisArg` is not provided
|
|
66
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
67
|
+
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
68
|
+
*/
|
|
69
|
+
every(predicate: PairCallback<K, V, boolean>, thisArg?: any): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Time Complexity: O(n)
|
|
72
|
+
* Space Complexity: O(1)
|
|
73
|
+
*/
|
|
74
|
+
/**
|
|
75
|
+
* Time Complexity: O(n)
|
|
76
|
+
* Space Complexity: O(1)
|
|
77
|
+
*
|
|
78
|
+
* The "some" function iterates over a collection and returns true if at least one element satisfies
|
|
79
|
+
* a given predicate.
|
|
80
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
81
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
82
|
+
* met for the current element in the iteration.
|
|
83
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
84
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
85
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
86
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
87
|
+
* the collection, and false otherwise.
|
|
88
|
+
*/
|
|
89
|
+
some(predicate: PairCallback<K, V, boolean>, thisArg?: any): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Time Complexity: O(n)
|
|
92
|
+
* Space Complexity: O(1)
|
|
93
|
+
*/
|
|
94
|
+
/**
|
|
95
|
+
* Time Complexity: O(n)
|
|
96
|
+
* Space Complexity: O(1)
|
|
97
|
+
*
|
|
98
|
+
* The `forEach` function iterates over each key-value pair in a collection and executes a callback
|
|
99
|
+
* function for each pair.
|
|
100
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
101
|
+
* It takes four parameters: the value of the current element, the key of the current element, the
|
|
102
|
+
* index of the current element, and the collection itself.
|
|
103
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
104
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
105
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
106
|
+
*/
|
|
107
|
+
forEach(callbackfn: PairCallback<K, V, void>, thisArg?: any): void;
|
|
108
|
+
/**
|
|
109
|
+
* Time Complexity: O(n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
111
|
+
*/
|
|
112
|
+
/**
|
|
113
|
+
* Time Complexity: O(n)
|
|
114
|
+
* Space Complexity: O(1)
|
|
115
|
+
*
|
|
116
|
+
* The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
|
|
117
|
+
* accumulating a single value.
|
|
118
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
119
|
+
* It takes four arguments: the current accumulator value, the current value of the element, the key
|
|
120
|
+
* of the element, and the index of the element in the collection. It should return the updated
|
|
121
|
+
* accumulator value.
|
|
122
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
123
|
+
* is the value that will be used as the first argument to the `callbackfn` function when reducing
|
|
124
|
+
* the elements of the collection.
|
|
125
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
126
|
+
* all the elements in the collection.
|
|
127
|
+
*/
|
|
128
|
+
reduce<U>(callbackfn: ReducePairCallback<K, V, U>, initialValue: U): U;
|
|
129
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
130
|
+
}
|
|
131
|
+
export declare abstract class IterableElementBase<V> {
|
|
132
|
+
/**
|
|
133
|
+
* Time Complexity: O(n)
|
|
134
|
+
* Space Complexity: O(1)
|
|
135
|
+
*/
|
|
136
|
+
/**
|
|
137
|
+
* Time Complexity: O(n)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
|
+
*
|
|
140
|
+
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
141
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
142
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
143
|
+
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
144
|
+
*/
|
|
145
|
+
[Symbol.iterator](...args: any[]): IterableIterator<V>;
|
|
146
|
+
/**
|
|
147
|
+
* Time Complexity: O(n)
|
|
148
|
+
* Space Complexity: O(n)
|
|
149
|
+
*/
|
|
150
|
+
/**
|
|
151
|
+
* Time Complexity: O(n)
|
|
152
|
+
* Space Complexity: O(n)
|
|
153
|
+
*
|
|
154
|
+
* The function returns an iterator that yields all the values in the object.
|
|
155
|
+
*/
|
|
156
|
+
values(): IterableIterator<V>;
|
|
157
|
+
/**
|
|
158
|
+
* Time Complexity: O(n)
|
|
159
|
+
* Space Complexity: O(1)
|
|
160
|
+
*/
|
|
161
|
+
/**
|
|
162
|
+
* Time Complexity: O(n)
|
|
163
|
+
* Space Complexity: O(1)
|
|
164
|
+
*
|
|
165
|
+
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
166
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
167
|
+
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
168
|
+
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
169
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
170
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
171
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
172
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
173
|
+
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
174
|
+
*/
|
|
175
|
+
every(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Time Complexity: O(n)
|
|
178
|
+
* Space Complexity: O(1)
|
|
179
|
+
*/
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(n)
|
|
182
|
+
* Space Complexity: O(1)
|
|
183
|
+
*
|
|
184
|
+
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
185
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
186
|
+
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
187
|
+
* element satisfies the condition.
|
|
188
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
189
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
190
|
+
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
191
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
192
|
+
* in the collection, and false otherwise.
|
|
193
|
+
*/
|
|
194
|
+
some(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean;
|
|
195
|
+
/**
|
|
196
|
+
* Time Complexity: O(n)
|
|
197
|
+
* Space Complexity: O(1)
|
|
198
|
+
*/
|
|
199
|
+
/**
|
|
200
|
+
* Time Complexity: O(n)
|
|
201
|
+
* Space Complexity: O(1)
|
|
202
|
+
*
|
|
203
|
+
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
204
|
+
* function for each element.
|
|
205
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
206
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
207
|
+
* element, and the array that forEach was called upon.
|
|
208
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
209
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
210
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
211
|
+
*/
|
|
212
|
+
forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void;
|
|
213
|
+
/**
|
|
214
|
+
* Time Complexity: O(n)
|
|
215
|
+
* Space Complexity: O(1)
|
|
216
|
+
*/
|
|
217
|
+
/**
|
|
218
|
+
* Time Complexity: O(n)
|
|
219
|
+
* Space Complexity: O(1)
|
|
220
|
+
*
|
|
221
|
+
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
222
|
+
* function to reduce them into a single value.
|
|
223
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
224
|
+
* the array. It takes four arguments:
|
|
225
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
226
|
+
* is the value that the accumulator starts with before the reduction operation begins.
|
|
227
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
228
|
+
* all the elements in the array and applying the callback function to each element.
|
|
229
|
+
*/
|
|
230
|
+
reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U;
|
|
231
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<V>;
|
|
232
|
+
}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IterableElementBase = exports.IterablePairBase = void 0;
|
|
4
|
+
class IterablePairBase {
|
|
5
|
+
/**
|
|
6
|
+
* Time Complexity: O(n)
|
|
7
|
+
* Space Complexity: O(1)
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Time Complexity: O(n)
|
|
11
|
+
* Space Complexity: O(1)
|
|
12
|
+
*
|
|
13
|
+
* The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
|
|
14
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
15
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
16
|
+
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
17
|
+
*/
|
|
18
|
+
*[Symbol.iterator](...args) {
|
|
19
|
+
yield* this._getIterator(...args);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Time Complexity: O(n)
|
|
23
|
+
* Space Complexity: O(n)
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Time Complexity: O(n)
|
|
27
|
+
* Space Complexity: O(n)
|
|
28
|
+
*
|
|
29
|
+
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
30
|
+
* be undefined.
|
|
31
|
+
*/
|
|
32
|
+
*entries() {
|
|
33
|
+
for (const item of this) {
|
|
34
|
+
yield item;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Time Complexity: O(n)
|
|
39
|
+
* Space Complexity: O(n)
|
|
40
|
+
*/
|
|
41
|
+
/**
|
|
42
|
+
* Time Complexity: O(n)
|
|
43
|
+
* Space Complexity: O(n)
|
|
44
|
+
*
|
|
45
|
+
* The function returns an iterator that yields the keys of a data structure.
|
|
46
|
+
*/
|
|
47
|
+
*keys() {
|
|
48
|
+
for (const item of this) {
|
|
49
|
+
yield item[0];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(n)
|
|
54
|
+
* Space Complexity: O(n)
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* Time Complexity: O(n)
|
|
58
|
+
* Space Complexity: O(n)
|
|
59
|
+
*
|
|
60
|
+
* The function returns an iterator that yields the values of a collection.
|
|
61
|
+
*/
|
|
62
|
+
*values() {
|
|
63
|
+
for (const item of this) {
|
|
64
|
+
yield item[1];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Time Complexity: O(n)
|
|
69
|
+
* Space Complexity: O(1)
|
|
70
|
+
*/
|
|
71
|
+
/**
|
|
72
|
+
* Time Complexity: O(n)
|
|
73
|
+
* Space Complexity: O(1)
|
|
74
|
+
*
|
|
75
|
+
* The `every` function checks if every element in a collection satisfies a given condition.
|
|
76
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
77
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
78
|
+
* met for the current element in the iteration.
|
|
79
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
80
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
81
|
+
* passed as the first argument to the `predicate` function. If `thisArg` is not provided
|
|
82
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
83
|
+
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
84
|
+
*/
|
|
85
|
+
every(predicate, thisArg) {
|
|
86
|
+
let index = 0;
|
|
87
|
+
for (const item of this) {
|
|
88
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Time Complexity: O(n)
|
|
96
|
+
* Space Complexity: O(1)
|
|
97
|
+
*/
|
|
98
|
+
/**
|
|
99
|
+
* Time Complexity: O(n)
|
|
100
|
+
* Space Complexity: O(1)
|
|
101
|
+
*
|
|
102
|
+
* The "some" function iterates over a collection and returns true if at least one element satisfies
|
|
103
|
+
* a given predicate.
|
|
104
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
105
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
106
|
+
* met for the current element in the iteration.
|
|
107
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
108
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
109
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
110
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
111
|
+
* the collection, and false otherwise.
|
|
112
|
+
*/
|
|
113
|
+
some(predicate, thisArg) {
|
|
114
|
+
let index = 0;
|
|
115
|
+
for (const item of this) {
|
|
116
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Time Complexity: O(n)
|
|
124
|
+
* Space Complexity: O(1)
|
|
125
|
+
*/
|
|
126
|
+
/**
|
|
127
|
+
* Time Complexity: O(n)
|
|
128
|
+
* Space Complexity: O(1)
|
|
129
|
+
*
|
|
130
|
+
* The `forEach` function iterates over each key-value pair in a collection and executes a callback
|
|
131
|
+
* function for each pair.
|
|
132
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
133
|
+
* It takes four parameters: the value of the current element, the key of the current element, the
|
|
134
|
+
* index of the current element, and the collection itself.
|
|
135
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
136
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
137
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
138
|
+
*/
|
|
139
|
+
forEach(callbackfn, thisArg) {
|
|
140
|
+
let index = 0;
|
|
141
|
+
for (const item of this) {
|
|
142
|
+
const [key, value] = item;
|
|
143
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Time Complexity: O(n)
|
|
148
|
+
* Space Complexity: O(1)
|
|
149
|
+
*/
|
|
150
|
+
/**
|
|
151
|
+
* Time Complexity: O(n)
|
|
152
|
+
* Space Complexity: O(1)
|
|
153
|
+
*
|
|
154
|
+
* The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
|
|
155
|
+
* accumulating a single value.
|
|
156
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
157
|
+
* It takes four arguments: the current accumulator value, the current value of the element, the key
|
|
158
|
+
* of the element, and the index of the element in the collection. It should return the updated
|
|
159
|
+
* accumulator value.
|
|
160
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
161
|
+
* is the value that will be used as the first argument to the `callbackfn` function when reducing
|
|
162
|
+
* the elements of the collection.
|
|
163
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
164
|
+
* all the elements in the collection.
|
|
165
|
+
*/
|
|
166
|
+
reduce(callbackfn, initialValue) {
|
|
167
|
+
let accumulator = initialValue;
|
|
168
|
+
let index = 0;
|
|
169
|
+
for (const item of this) {
|
|
170
|
+
const [key, value] = item;
|
|
171
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
172
|
+
}
|
|
173
|
+
return accumulator;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.IterablePairBase = IterablePairBase;
|
|
177
|
+
class IterableElementBase {
|
|
178
|
+
/**
|
|
179
|
+
* Time Complexity: O(n)
|
|
180
|
+
* Space Complexity: O(1)
|
|
181
|
+
*/
|
|
182
|
+
/**
|
|
183
|
+
* Time Complexity: O(n)
|
|
184
|
+
* Space Complexity: O(1)
|
|
185
|
+
*
|
|
186
|
+
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
187
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
188
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
189
|
+
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
190
|
+
*/
|
|
191
|
+
*[Symbol.iterator](...args) {
|
|
192
|
+
yield* this._getIterator(...args);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Time Complexity: O(n)
|
|
196
|
+
* Space Complexity: O(n)
|
|
197
|
+
*/
|
|
198
|
+
/**
|
|
199
|
+
* Time Complexity: O(n)
|
|
200
|
+
* Space Complexity: O(n)
|
|
201
|
+
*
|
|
202
|
+
* The function returns an iterator that yields all the values in the object.
|
|
203
|
+
*/
|
|
204
|
+
*values() {
|
|
205
|
+
for (const item of this) {
|
|
206
|
+
yield item;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(n)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*/
|
|
213
|
+
/**
|
|
214
|
+
* Time Complexity: O(n)
|
|
215
|
+
* Space Complexity: O(1)
|
|
216
|
+
*
|
|
217
|
+
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
218
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
219
|
+
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
220
|
+
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
221
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
222
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
223
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
224
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
225
|
+
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
226
|
+
*/
|
|
227
|
+
every(predicate, thisArg) {
|
|
228
|
+
let index = 0;
|
|
229
|
+
for (const item of this) {
|
|
230
|
+
if (!predicate.call(thisArg, item, index++, this)) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Time Complexity: O(n)
|
|
238
|
+
* Space Complexity: O(1)
|
|
239
|
+
*/
|
|
240
|
+
/**
|
|
241
|
+
* Time Complexity: O(n)
|
|
242
|
+
* Space Complexity: O(1)
|
|
243
|
+
*
|
|
244
|
+
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
245
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
246
|
+
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
247
|
+
* element satisfies the condition.
|
|
248
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
249
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
250
|
+
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
251
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
252
|
+
* in the collection, and false otherwise.
|
|
253
|
+
*/
|
|
254
|
+
some(predicate, thisArg) {
|
|
255
|
+
let index = 0;
|
|
256
|
+
for (const item of this) {
|
|
257
|
+
if (predicate.call(thisArg, item, index++, this)) {
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Time Complexity: O(n)
|
|
265
|
+
* Space Complexity: O(1)
|
|
266
|
+
*/
|
|
267
|
+
/**
|
|
268
|
+
* Time Complexity: O(n)
|
|
269
|
+
* Space Complexity: O(1)
|
|
270
|
+
*
|
|
271
|
+
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
272
|
+
* function for each element.
|
|
273
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
274
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
275
|
+
* element, and the array that forEach was called upon.
|
|
276
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
277
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
278
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
279
|
+
*/
|
|
280
|
+
forEach(callbackfn, thisArg) {
|
|
281
|
+
let index = 0;
|
|
282
|
+
for (const item of this) {
|
|
283
|
+
callbackfn.call(thisArg, item, index++, this);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Time Complexity: O(n)
|
|
288
|
+
* Space Complexity: O(1)
|
|
289
|
+
*/
|
|
290
|
+
/**
|
|
291
|
+
* Time Complexity: O(n)
|
|
292
|
+
* Space Complexity: O(1)
|
|
293
|
+
*
|
|
294
|
+
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
295
|
+
* function to reduce them into a single value.
|
|
296
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
297
|
+
* the array. It takes four arguments:
|
|
298
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
299
|
+
* is the value that the accumulator starts with before the reduction operation begins.
|
|
300
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
301
|
+
* all the elements in the array and applying the callback function to each element.
|
|
302
|
+
*/
|
|
303
|
+
reduce(callbackfn, initialValue) {
|
|
304
|
+
let accumulator = initialValue;
|
|
305
|
+
let index = 0;
|
|
306
|
+
for (const item of this) {
|
|
307
|
+
accumulator = callbackfn(accumulator, item, index++, this);
|
|
308
|
+
}
|
|
309
|
+
return accumulator;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
exports.IterableElementBase = IterableElementBase;
|