data-structure-typed 1.48.1 → 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/CHANGELOG.md +1 -1
- package/README.md +24 -18
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +182 -182
- package/dist/cjs/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/data-structures/base/index.js +18 -0
- package/dist/cjs/data-structures/base/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/cjs/data-structures/base/iterable-base.js +313 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/cjs/data-structures/hash/hash-map.js +69 -173
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/cjs/data-structures/heap/heap.js +60 -30
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/index.d.ts +1 -0
- package/dist/cjs/data-structures/index.js +1 -0
- package/dist/cjs/data-structures/index.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- 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 +32 -51
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/cjs/data-structures/queue/deque.js +36 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/cjs/data-structures/queue/queue.js +69 -82
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/cjs/data-structures/stack/stack.js +50 -31
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/cjs/data-structures/trie/trie.js +53 -32
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/cjs/types/data-structures/base/base.js +3 -0
- package/dist/cjs/types/data-structures/base/base.js.map +1 -0
- package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/base/index.js +18 -0
- package/dist/cjs/types/data-structures/base/index.js.map +1 -0
- package/dist/cjs/types/data-structures/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/index.js +1 -0
- package/dist/cjs/types/data-structures/index.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/data-structures/base/index.js +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/mjs/data-structures/base/iterable-base.js +307 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
- package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/mjs/data-structures/hash/hash-map.js +69 -173
- package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/mjs/data-structures/heap/heap.js +60 -30
- package/dist/mjs/data-structures/index.d.ts +1 -0
- package/dist/mjs/data-structures/index.js +1 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/mjs/data-structures/queue/deque.js +36 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/mjs/data-structures/queue/queue.js +66 -79
- package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/mjs/data-structures/stack/stack.js +50 -31
- package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/mjs/data-structures/trie/trie.js +53 -32
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/mjs/types/data-structures/base/base.js +1 -0
- package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/base/index.js +1 -0
- package/dist/mjs/types/data-structures/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/index.js +1 -0
- package/dist/umd/data-structure-typed.js +846 -715
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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 +82 -138
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- 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/index.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/bst.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +10 -10
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
- package/test/unit/data-structures/queue/deque.test.ts +25 -0
|
@@ -4,37 +4,8 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* The enqueue function adds a value to the end of an array.
|
|
10
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
11
|
-
*/
|
|
12
|
-
enqueue(value) {
|
|
13
|
-
this.push(value);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
17
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
18
|
-
*/
|
|
19
|
-
dequeue() {
|
|
20
|
-
return this.shift();
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
24
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
25
|
-
*/
|
|
26
|
-
getFirst() {
|
|
27
|
-
return this.head?.value;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
31
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
32
|
-
*/
|
|
33
|
-
peek() {
|
|
34
|
-
return this.getFirst();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
export class Queue {
|
|
7
|
+
import { IterableElementBase } from "../base";
|
|
8
|
+
export class Queue extends IterableElementBase {
|
|
38
9
|
/**
|
|
39
10
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
40
11
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
@@ -42,6 +13,7 @@ export class Queue {
|
|
|
42
13
|
* initialized as an empty array.
|
|
43
14
|
*/
|
|
44
15
|
constructor(elements) {
|
|
16
|
+
super();
|
|
45
17
|
this._nodes = elements || [];
|
|
46
18
|
this._offset = 0;
|
|
47
19
|
}
|
|
@@ -265,31 +237,6 @@ export class Queue {
|
|
|
265
237
|
print() {
|
|
266
238
|
console.log([...this]);
|
|
267
239
|
}
|
|
268
|
-
*[Symbol.iterator]() {
|
|
269
|
-
for (const item of this.nodes) {
|
|
270
|
-
yield item;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Time Complexity: O(n)
|
|
275
|
-
* Space Complexity: O(1)
|
|
276
|
-
*/
|
|
277
|
-
/**
|
|
278
|
-
* Time Complexity: O(n)
|
|
279
|
-
* Space Complexity: O(1)
|
|
280
|
-
*
|
|
281
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
282
|
-
* each element.
|
|
283
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
284
|
-
* deque. It takes three parameters:
|
|
285
|
-
*/
|
|
286
|
-
forEach(callback) {
|
|
287
|
-
let index = 0;
|
|
288
|
-
for (const el of this) {
|
|
289
|
-
callback(el, index, this);
|
|
290
|
-
index++;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
240
|
/**
|
|
294
241
|
* Time Complexity: O(n)
|
|
295
242
|
* Space Complexity: O(n)
|
|
@@ -298,18 +245,23 @@ export class Queue {
|
|
|
298
245
|
* Time Complexity: O(n)
|
|
299
246
|
* Space Complexity: O(n)
|
|
300
247
|
*
|
|
301
|
-
* The `filter` function creates a new
|
|
302
|
-
* predicate function.
|
|
303
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
|
|
308
|
-
|
|
248
|
+
* The `filter` function creates a new `Queue` object containing elements from the original `Queue`
|
|
249
|
+
* that satisfy a given predicate function.
|
|
250
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
251
|
+
* the current element being iterated over, the index of the current element, and the queue itself.
|
|
252
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
253
|
+
* queue or not.
|
|
254
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
255
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
256
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
257
|
+
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
258
|
+
* satisfy the given predicate function.
|
|
259
|
+
*/
|
|
260
|
+
filter(predicate, thisArg) {
|
|
309
261
|
const newDeque = new Queue([]);
|
|
310
262
|
let index = 0;
|
|
311
263
|
for (const el of this) {
|
|
312
|
-
if (predicate(el, index, this)) {
|
|
264
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
313
265
|
newDeque.push(el);
|
|
314
266
|
}
|
|
315
267
|
index++;
|
|
@@ -324,27 +276,62 @@ export class Queue {
|
|
|
324
276
|
* Time Complexity: O(n)
|
|
325
277
|
* Space Complexity: O(n)
|
|
326
278
|
*
|
|
327
|
-
* The `map` function takes a callback function and applies it to each element in the
|
|
328
|
-
* returning a new
|
|
329
|
-
* @param callback - The
|
|
330
|
-
*
|
|
331
|
-
|
|
332
|
-
|
|
279
|
+
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
280
|
+
* returning a new queue with the results.
|
|
281
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
282
|
+
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
283
|
+
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
284
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
285
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
286
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
287
|
+
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
288
|
+
*/
|
|
289
|
+
map(callback, thisArg) {
|
|
333
290
|
const newDeque = new Queue([]);
|
|
334
291
|
let index = 0;
|
|
335
292
|
for (const el of this) {
|
|
336
|
-
newDeque.push(callback(el, index, this));
|
|
293
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
337
294
|
index++;
|
|
338
295
|
}
|
|
339
296
|
return newDeque;
|
|
340
297
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Time Complexity: O(n)
|
|
300
|
+
* Space Complexity: O(n)
|
|
301
|
+
*/
|
|
302
|
+
*_getIterator() {
|
|
303
|
+
for (const item of this.nodes) {
|
|
304
|
+
yield item;
|
|
347
305
|
}
|
|
348
|
-
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
export class LinkedListQueue extends SinglyLinkedList {
|
|
309
|
+
/**
|
|
310
|
+
* The enqueue function adds a value to the end of an array.
|
|
311
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
312
|
+
*/
|
|
313
|
+
enqueue(value) {
|
|
314
|
+
this.push(value);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
318
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
319
|
+
*/
|
|
320
|
+
dequeue() {
|
|
321
|
+
return this.shift();
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
325
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
326
|
+
*/
|
|
327
|
+
getFirst() {
|
|
328
|
+
return this.head?.value;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
332
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
333
|
+
*/
|
|
334
|
+
peek() {
|
|
335
|
+
return this.getFirst();
|
|
349
336
|
}
|
|
350
337
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
1
3
|
/**
|
|
2
4
|
* @license MIT
|
|
3
5
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
6
|
* @class
|
|
5
7
|
*/
|
|
6
|
-
export declare class Stack<E = any> {
|
|
8
|
+
export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
7
9
|
/**
|
|
8
10
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
9
11
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -104,17 +106,48 @@ export declare class Stack<E = any> {
|
|
|
104
106
|
*/
|
|
105
107
|
clone(): Stack<E>;
|
|
106
108
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
+
* Time Complexity: O(n)
|
|
110
|
+
* Space Complexity: O(n)
|
|
109
111
|
*/
|
|
110
|
-
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
111
112
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
113
|
+
* Time Complexity: O(n)
|
|
114
|
+
* Space Complexity: O(n)
|
|
115
|
+
*
|
|
116
|
+
* The `filter` function creates a new stack containing elements from the original stack that satisfy
|
|
117
|
+
* a given predicate function.
|
|
118
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
119
|
+
* the current element being iterated over, the index of the current element, and the stack itself.
|
|
120
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
121
|
+
* stack or not.
|
|
122
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
123
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
124
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
125
|
+
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
126
|
+
* satisfy the given predicate function.
|
|
127
|
+
*/
|
|
128
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Stack<E>;
|
|
129
|
+
/**
|
|
130
|
+
* Time Complexity: O(n)
|
|
131
|
+
* Space Complexity: O(n)
|
|
114
132
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Time Complexity: O(n)
|
|
135
|
+
* Space Complexity: O(n)
|
|
136
|
+
*
|
|
137
|
+
* The `map` function takes a callback function and applies it to each element in the stack,
|
|
138
|
+
* returning a new stack with the results.
|
|
139
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
140
|
+
* the stack. It takes three arguments:
|
|
141
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
142
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
143
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
144
|
+
* @returns The `map` method is returning a new `Stack` object.
|
|
145
|
+
*/
|
|
146
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
|
|
119
147
|
print(): void;
|
|
148
|
+
/**
|
|
149
|
+
* Custom iterator for the Stack class.
|
|
150
|
+
* @returns An iterator object.
|
|
151
|
+
*/
|
|
152
|
+
protected _getIterator(): Generator<E, void, unknown>;
|
|
120
153
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
1
2
|
/**
|
|
2
3
|
* @license MIT
|
|
3
4
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
5
|
* @class
|
|
5
6
|
*/
|
|
6
|
-
export class Stack {
|
|
7
|
+
export class Stack extends IterableElementBase {
|
|
7
8
|
/**
|
|
8
9
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
9
10
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -11,6 +12,7 @@ export class Stack {
|
|
|
11
12
|
* is provided and is an array, it is assigned to the `_elements
|
|
12
13
|
*/
|
|
13
14
|
constructor(elements) {
|
|
15
|
+
super();
|
|
14
16
|
this._elements = [];
|
|
15
17
|
if (elements) {
|
|
16
18
|
for (const el of elements) {
|
|
@@ -136,55 +138,72 @@ export class Stack {
|
|
|
136
138
|
return new Stack(this.elements.slice());
|
|
137
139
|
}
|
|
138
140
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
+
* Time Complexity: O(n)
|
|
142
|
+
* Space Complexity: O(n)
|
|
141
143
|
*/
|
|
142
|
-
*[Symbol.iterator]() {
|
|
143
|
-
for (let i = 0; i < this.elements.length; i++) {
|
|
144
|
-
yield this.elements[i];
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
144
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
145
|
+
* Time Complexity: O(n)
|
|
146
|
+
* Space Complexity: O(n)
|
|
147
|
+
*
|
|
148
|
+
* The `filter` function creates a new stack containing elements from the original stack that satisfy
|
|
149
|
+
* a given predicate function.
|
|
150
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
151
|
+
* the current element being iterated over, the index of the current element, and the stack itself.
|
|
152
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
153
|
+
* stack or not.
|
|
154
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
155
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
156
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
157
|
+
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
158
|
+
* satisfy the given predicate function.
|
|
159
|
+
*/
|
|
160
|
+
filter(predicate, thisArg) {
|
|
159
161
|
const newStack = new Stack();
|
|
160
162
|
let index = 0;
|
|
161
163
|
for (const el of this) {
|
|
162
|
-
if (predicate(el, index, this)) {
|
|
164
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
163
165
|
newStack.push(el);
|
|
164
166
|
}
|
|
165
167
|
index++;
|
|
166
168
|
}
|
|
167
169
|
return newStack;
|
|
168
170
|
}
|
|
169
|
-
|
|
171
|
+
/**
|
|
172
|
+
* Time Complexity: O(n)
|
|
173
|
+
* Space Complexity: O(n)
|
|
174
|
+
*/
|
|
175
|
+
/**
|
|
176
|
+
* Time Complexity: O(n)
|
|
177
|
+
* Space Complexity: O(n)
|
|
178
|
+
*
|
|
179
|
+
* The `map` function takes a callback function and applies it to each element in the stack,
|
|
180
|
+
* returning a new stack with the results.
|
|
181
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
182
|
+
* the stack. It takes three arguments:
|
|
183
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
184
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
185
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
186
|
+
* @returns The `map` method is returning a new `Stack` object.
|
|
187
|
+
*/
|
|
188
|
+
map(callback, thisArg) {
|
|
170
189
|
const newStack = new Stack();
|
|
171
190
|
let index = 0;
|
|
172
191
|
for (const el of this) {
|
|
173
|
-
newStack.push(callback(el, index, this));
|
|
192
|
+
newStack.push(callback.call(thisArg, el, index, this));
|
|
174
193
|
index++;
|
|
175
194
|
}
|
|
176
195
|
return newStack;
|
|
177
196
|
}
|
|
178
|
-
reduce(callback, initialValue) {
|
|
179
|
-
let accumulator = initialValue;
|
|
180
|
-
let index = 0;
|
|
181
|
-
for (const el of this) {
|
|
182
|
-
accumulator = callback(accumulator, el, index, this);
|
|
183
|
-
index++;
|
|
184
|
-
}
|
|
185
|
-
return accumulator;
|
|
186
|
-
}
|
|
187
197
|
print() {
|
|
188
198
|
console.log([...this]);
|
|
189
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Custom iterator for the Stack class.
|
|
202
|
+
* @returns An iterator object.
|
|
203
|
+
*/
|
|
204
|
+
*_getIterator() {
|
|
205
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
206
|
+
yield this.elements[i];
|
|
207
|
+
}
|
|
208
|
+
}
|
|
190
209
|
}
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import { IterableElementBase } from "../base";
|
|
9
|
+
import { ElementCallback } from "../../types";
|
|
8
10
|
/**
|
|
9
11
|
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
|
10
12
|
* and a flag indicating whether it's the end of a word.
|
|
@@ -18,7 +20,7 @@ export declare class TrieNode {
|
|
|
18
20
|
/**
|
|
19
21
|
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
20
22
|
*/
|
|
21
|
-
export declare class Trie {
|
|
23
|
+
export declare class Trie extends IterableElementBase<string> {
|
|
22
24
|
constructor(words?: string[], caseSensitive?: boolean);
|
|
23
25
|
protected _size: number;
|
|
24
26
|
get size(): number;
|
|
@@ -142,12 +144,45 @@ export declare class Trie {
|
|
|
142
144
|
* @returns {string[]} an array of strings.
|
|
143
145
|
*/
|
|
144
146
|
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(n)
|
|
149
|
+
* Space Complexity: O(n)
|
|
150
|
+
*/
|
|
151
|
+
/**
|
|
152
|
+
* Time Complexity: O(n)
|
|
153
|
+
* Space Complexity: O(n)
|
|
154
|
+
*
|
|
155
|
+
* The `filter` function takes a predicate function and returns a new array containing all the
|
|
156
|
+
* elements for which the predicate function returns true.
|
|
157
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
158
|
+
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current
|
|
159
|
+
* element should be included in the filtered results or not.
|
|
160
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
161
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
162
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
163
|
+
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
164
|
+
*/
|
|
165
|
+
filter(predicate: ElementCallback<string, boolean>, thisArg?: any): string[];
|
|
166
|
+
/**
|
|
167
|
+
* Time Complexity: O(n)
|
|
168
|
+
* Space Complexity: O(n)
|
|
169
|
+
*/
|
|
170
|
+
/**
|
|
171
|
+
* Time Complexity: O(n)
|
|
172
|
+
* Space Complexity: O(n)
|
|
173
|
+
*
|
|
174
|
+
* The `map` function creates a new Trie by applying a callback function to each element in the Trie.
|
|
175
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
176
|
+
* Trie. It takes three arguments: the current element in the Trie, the index of the current element,
|
|
177
|
+
* and the Trie itself. The callback function should return a new value for the element.
|
|
178
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
179
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
180
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
181
|
+
* @returns The `map` function is returning a new Trie object.
|
|
182
|
+
*/
|
|
183
|
+
map(callback: ElementCallback<string, string>, thisArg?: any): Trie;
|
|
150
184
|
print(): void;
|
|
185
|
+
protected _getIterator(): IterableIterator<string>;
|
|
151
186
|
/**
|
|
152
187
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
153
188
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import { IterableElementBase } from "../base";
|
|
8
9
|
/**
|
|
9
10
|
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
|
10
11
|
* and a flag indicating whether it's the end of a word.
|
|
@@ -22,8 +23,9 @@ export class TrieNode {
|
|
|
22
23
|
/**
|
|
23
24
|
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
24
25
|
*/
|
|
25
|
-
export class Trie {
|
|
26
|
+
export class Trie extends IterableElementBase {
|
|
26
27
|
constructor(words, caseSensitive = true) {
|
|
28
|
+
super();
|
|
27
29
|
this._root = new TrieNode('');
|
|
28
30
|
this._caseSensitive = caseSensitive;
|
|
29
31
|
this._size = 0;
|
|
@@ -319,56 +321,75 @@ export class Trie {
|
|
|
319
321
|
dfs(startNode, prefix);
|
|
320
322
|
return words;
|
|
321
323
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
filter(predicate) {
|
|
324
|
+
/**
|
|
325
|
+
* Time Complexity: O(n)
|
|
326
|
+
* Space Complexity: O(n)
|
|
327
|
+
*/
|
|
328
|
+
/**
|
|
329
|
+
* Time Complexity: O(n)
|
|
330
|
+
* Space Complexity: O(n)
|
|
331
|
+
*
|
|
332
|
+
* The `filter` function takes a predicate function and returns a new array containing all the
|
|
333
|
+
* elements for which the predicate function returns true.
|
|
334
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
335
|
+
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current
|
|
336
|
+
* element should be included in the filtered results or not.
|
|
337
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
338
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
339
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
340
|
+
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
341
|
+
*/
|
|
342
|
+
filter(predicate, thisArg) {
|
|
341
343
|
const results = [];
|
|
342
344
|
let index = 0;
|
|
343
345
|
for (const word of this) {
|
|
344
|
-
if (predicate(word, index, this)) {
|
|
346
|
+
if (predicate.call(thisArg, word, index, this)) {
|
|
345
347
|
results.push(word);
|
|
346
348
|
}
|
|
347
349
|
index++;
|
|
348
350
|
}
|
|
349
351
|
return results;
|
|
350
352
|
}
|
|
351
|
-
|
|
353
|
+
/**
|
|
354
|
+
* Time Complexity: O(n)
|
|
355
|
+
* Space Complexity: O(n)
|
|
356
|
+
*/
|
|
357
|
+
/**
|
|
358
|
+
* Time Complexity: O(n)
|
|
359
|
+
* Space Complexity: O(n)
|
|
360
|
+
*
|
|
361
|
+
* The `map` function creates a new Trie by applying a callback function to each element in the Trie.
|
|
362
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
363
|
+
* Trie. It takes three arguments: the current element in the Trie, the index of the current element,
|
|
364
|
+
* and the Trie itself. The callback function should return a new value for the element.
|
|
365
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
366
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
367
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
368
|
+
* @returns The `map` function is returning a new Trie object.
|
|
369
|
+
*/
|
|
370
|
+
map(callback, thisArg) {
|
|
352
371
|
const newTrie = new Trie();
|
|
353
372
|
let index = 0;
|
|
354
373
|
for (const word of this) {
|
|
355
|
-
newTrie.add(callback(word, index, this));
|
|
374
|
+
newTrie.add(callback.call(thisArg, word, index, this));
|
|
356
375
|
index++;
|
|
357
376
|
}
|
|
358
377
|
return newTrie;
|
|
359
378
|
}
|
|
360
|
-
reduce(callback, initialValue) {
|
|
361
|
-
let accumulator = initialValue;
|
|
362
|
-
let index = 0;
|
|
363
|
-
for (const word of this) {
|
|
364
|
-
accumulator = callback(accumulator, word, index, this);
|
|
365
|
-
index++;
|
|
366
|
-
}
|
|
367
|
-
return accumulator;
|
|
368
|
-
}
|
|
369
379
|
print() {
|
|
370
380
|
console.log([...this]);
|
|
371
381
|
}
|
|
382
|
+
*_getIterator() {
|
|
383
|
+
function* _dfs(node, path) {
|
|
384
|
+
if (node.isEnd) {
|
|
385
|
+
yield path;
|
|
386
|
+
}
|
|
387
|
+
for (const [char, childNode] of node.children) {
|
|
388
|
+
yield* _dfs(childNode, path + char);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
yield* _dfs(this.root, '');
|
|
392
|
+
}
|
|
372
393
|
/**
|
|
373
394
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
374
395
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { IterableElementBase, IterablePairBase } from "../../../data-structures";
|
|
2
|
+
export type PairCallback<K, V, R> = (value: V, key: K, index: number, container: IterablePairBase<K, V>) => R;
|
|
3
|
+
export type ElementCallback<V, R> = (element: V, index: number, container: IterableElementBase<V>) => R;
|
|
4
|
+
export type ReducePairCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, container: IterablePairBase<K, V>) => R;
|
|
5
|
+
export type ReduceElementCallback<V, R> = (accumulator: R, element: V, index: number, container: IterableElementBase<V>) => R;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './base';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './base';
|