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,29 +4,9 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
11
|
-
*/
|
|
12
|
-
enqueue(value: E): void;
|
|
13
|
-
/**
|
|
14
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
15
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
16
|
-
*/
|
|
17
|
-
dequeue(): E | undefined;
|
|
18
|
-
/**
|
|
19
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
20
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
21
|
-
*/
|
|
22
|
-
getFirst(): E | undefined;
|
|
23
|
-
/**
|
|
24
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
25
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
26
|
-
*/
|
|
27
|
-
peek(): E | undefined;
|
|
28
|
-
}
|
|
29
|
-
export declare class Queue<E = any> {
|
|
7
|
+
import { IterableElementBase } from "../base";
|
|
8
|
+
import { ElementCallback } from "../../types";
|
|
9
|
+
export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
30
10
|
/**
|
|
31
11
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
32
12
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
@@ -206,21 +186,27 @@ export declare class Queue<E = any> {
|
|
|
206
186
|
*/
|
|
207
187
|
clone(): Queue<E>;
|
|
208
188
|
print(): void;
|
|
209
|
-
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
210
189
|
/**
|
|
211
190
|
* Time Complexity: O(n)
|
|
212
|
-
* Space Complexity: O(
|
|
191
|
+
* Space Complexity: O(n)
|
|
213
192
|
*/
|
|
214
193
|
/**
|
|
215
194
|
* Time Complexity: O(n)
|
|
216
|
-
* Space Complexity: O(
|
|
195
|
+
* Space Complexity: O(n)
|
|
217
196
|
*
|
|
218
|
-
* The `
|
|
219
|
-
*
|
|
220
|
-
* @param
|
|
221
|
-
*
|
|
222
|
-
|
|
223
|
-
|
|
197
|
+
* The `filter` function creates a new `Queue` object containing elements from the original `Queue`
|
|
198
|
+
* that satisfy a given predicate function.
|
|
199
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
200
|
+
* the current element being iterated over, the index of the current element, and the queue itself.
|
|
201
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
202
|
+
* queue or not.
|
|
203
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
204
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
205
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
206
|
+
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
207
|
+
* satisfy the given predicate function.
|
|
208
|
+
*/
|
|
209
|
+
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E>;
|
|
224
210
|
/**
|
|
225
211
|
* Time Complexity: O(n)
|
|
226
212
|
* Space Complexity: O(n)
|
|
@@ -229,27 +215,42 @@ export declare class Queue<E = any> {
|
|
|
229
215
|
* Time Complexity: O(n)
|
|
230
216
|
* Space Complexity: O(n)
|
|
231
217
|
*
|
|
232
|
-
* The `
|
|
233
|
-
*
|
|
234
|
-
* @param
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
218
|
+
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
219
|
+
* returning a new queue with the results.
|
|
220
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
221
|
+
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
222
|
+
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
223
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
224
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
225
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
226
|
+
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
238
227
|
*/
|
|
239
|
-
|
|
228
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T>;
|
|
240
229
|
/**
|
|
241
230
|
* Time Complexity: O(n)
|
|
242
231
|
* Space Complexity: O(n)
|
|
243
232
|
*/
|
|
233
|
+
protected _getIterator(): Generator<E, void, unknown>;
|
|
234
|
+
}
|
|
235
|
+
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
244
236
|
/**
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
249
|
-
* returning a new deque with the results.
|
|
250
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
251
|
-
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
237
|
+
* The enqueue function adds a value to the end of an array.
|
|
238
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
252
239
|
*/
|
|
253
|
-
|
|
254
|
-
|
|
240
|
+
enqueue(value: E): void;
|
|
241
|
+
/**
|
|
242
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
243
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
244
|
+
*/
|
|
245
|
+
dequeue(): E | undefined;
|
|
246
|
+
/**
|
|
247
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
248
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
249
|
+
*/
|
|
250
|
+
getFirst(): E | undefined;
|
|
251
|
+
/**
|
|
252
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
253
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
254
|
+
*/
|
|
255
|
+
peek(): E | undefined;
|
|
255
256
|
}
|
|
@@ -1,45 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.LinkedListQueue = exports.Queue = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* @license MIT
|
|
6
6
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
9
|
const linked_list_1 = require("../linked-list");
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* The enqueue function adds a value to the end of an array.
|
|
13
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
14
|
-
*/
|
|
15
|
-
enqueue(value) {
|
|
16
|
-
this.push(value);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
20
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
21
|
-
*/
|
|
22
|
-
dequeue() {
|
|
23
|
-
return this.shift();
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
-
*/
|
|
29
|
-
getFirst() {
|
|
30
|
-
var _a;
|
|
31
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
35
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
36
|
-
*/
|
|
37
|
-
peek() {
|
|
38
|
-
return this.getFirst();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
exports.LinkedListQueue = LinkedListQueue;
|
|
42
|
-
class Queue {
|
|
10
|
+
const base_1 = require("../base");
|
|
11
|
+
class Queue extends base_1.IterableElementBase {
|
|
43
12
|
/**
|
|
44
13
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
45
14
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
@@ -47,6 +16,7 @@ class Queue {
|
|
|
47
16
|
* initialized as an empty array.
|
|
48
17
|
*/
|
|
49
18
|
constructor(elements) {
|
|
19
|
+
super();
|
|
50
20
|
this._nodes = elements || [];
|
|
51
21
|
this._offset = 0;
|
|
52
22
|
}
|
|
@@ -268,31 +238,6 @@ class Queue {
|
|
|
268
238
|
print() {
|
|
269
239
|
console.log([...this]);
|
|
270
240
|
}
|
|
271
|
-
*[Symbol.iterator]() {
|
|
272
|
-
for (const item of this.nodes) {
|
|
273
|
-
yield item;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Time Complexity: O(n)
|
|
278
|
-
* Space Complexity: O(1)
|
|
279
|
-
*/
|
|
280
|
-
/**
|
|
281
|
-
* Time Complexity: O(n)
|
|
282
|
-
* Space Complexity: O(1)
|
|
283
|
-
*
|
|
284
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
285
|
-
* each element.
|
|
286
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
287
|
-
* deque. It takes three parameters:
|
|
288
|
-
*/
|
|
289
|
-
forEach(callback) {
|
|
290
|
-
let index = 0;
|
|
291
|
-
for (const el of this) {
|
|
292
|
-
callback(el, index, this);
|
|
293
|
-
index++;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
241
|
/**
|
|
297
242
|
* Time Complexity: O(n)
|
|
298
243
|
* Space Complexity: O(n)
|
|
@@ -301,18 +246,23 @@ class Queue {
|
|
|
301
246
|
* Time Complexity: O(n)
|
|
302
247
|
* Space Complexity: O(n)
|
|
303
248
|
*
|
|
304
|
-
* The `filter` function creates a new
|
|
305
|
-
* predicate function.
|
|
306
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
|
|
311
|
-
|
|
249
|
+
* The `filter` function creates a new `Queue` object containing elements from the original `Queue`
|
|
250
|
+
* that satisfy a given predicate function.
|
|
251
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
252
|
+
* the current element being iterated over, the index of the current element, and the queue itself.
|
|
253
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
254
|
+
* queue or not.
|
|
255
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
256
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
257
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
258
|
+
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
259
|
+
* satisfy the given predicate function.
|
|
260
|
+
*/
|
|
261
|
+
filter(predicate, thisArg) {
|
|
312
262
|
const newDeque = new Queue([]);
|
|
313
263
|
let index = 0;
|
|
314
264
|
for (const el of this) {
|
|
315
|
-
if (predicate(el, index, this)) {
|
|
265
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
316
266
|
newDeque.push(el);
|
|
317
267
|
}
|
|
318
268
|
index++;
|
|
@@ -327,29 +277,66 @@ class Queue {
|
|
|
327
277
|
* Time Complexity: O(n)
|
|
328
278
|
* Space Complexity: O(n)
|
|
329
279
|
*
|
|
330
|
-
* The `map` function takes a callback function and applies it to each element in the
|
|
331
|
-
* returning a new
|
|
332
|
-
* @param callback - The
|
|
333
|
-
*
|
|
334
|
-
|
|
335
|
-
|
|
280
|
+
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
281
|
+
* returning a new queue with the results.
|
|
282
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
283
|
+
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
284
|
+
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
285
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
286
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
287
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
288
|
+
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
289
|
+
*/
|
|
290
|
+
map(callback, thisArg) {
|
|
336
291
|
const newDeque = new Queue([]);
|
|
337
292
|
let index = 0;
|
|
338
293
|
for (const el of this) {
|
|
339
|
-
newDeque.push(callback(el, index, this));
|
|
294
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
340
295
|
index++;
|
|
341
296
|
}
|
|
342
297
|
return newDeque;
|
|
343
298
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Time Complexity: O(n)
|
|
301
|
+
* Space Complexity: O(n)
|
|
302
|
+
*/
|
|
303
|
+
*_getIterator() {
|
|
304
|
+
for (const item of this.nodes) {
|
|
305
|
+
yield item;
|
|
350
306
|
}
|
|
351
|
-
return accumulator;
|
|
352
307
|
}
|
|
353
308
|
}
|
|
354
309
|
exports.Queue = Queue;
|
|
310
|
+
class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
311
|
+
/**
|
|
312
|
+
* The enqueue function adds a value to the end of an array.
|
|
313
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
314
|
+
*/
|
|
315
|
+
enqueue(value) {
|
|
316
|
+
this.push(value);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
320
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
321
|
+
*/
|
|
322
|
+
dequeue() {
|
|
323
|
+
return this.shift();
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
327
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
328
|
+
*/
|
|
329
|
+
getFirst() {
|
|
330
|
+
var _a;
|
|
331
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
335
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
336
|
+
*/
|
|
337
|
+
peek() {
|
|
338
|
+
return this.getFirst();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
exports.LinkedListQueue = LinkedListQueue;
|
|
355
342
|
//# sourceMappingURL=queue.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,gDAAkD;
|
|
1
|
+
{"version":3,"file":"queue.js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,CAAC;IAClB,CAAC;IAED;;;OAGG;IAEM,CAAE,YAAY;QACrB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAC9B,MAAM,IAAI,CAAC;QACb,CAAC;IACH,CAAC;CACF;AAjVD,sBAiVC;AAED,MAAa,eAAyB,SAAQ,8BAAmB;IAC/D;;;OAGG;IACH,OAAO,CAAC,KAAQ;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;;OAGG;IACH,QAAQ;;QACN,OAAO,MAAA,IAAI,CAAC,IAAI,0CAAE,KAAK,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACH,IAAI;QACF,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;IACzB,CAAC;CACF;AAhCD,0CAgCC"}
|
|
@@ -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,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Stack = void 0;
|
|
4
|
+
const base_1 = require("../base");
|
|
4
5
|
/**
|
|
5
6
|
* @license MIT
|
|
6
7
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
7
8
|
* @class
|
|
8
9
|
*/
|
|
9
|
-
class Stack {
|
|
10
|
+
class Stack extends base_1.IterableElementBase {
|
|
10
11
|
/**
|
|
11
12
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
12
13
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -14,6 +15,7 @@ class Stack {
|
|
|
14
15
|
* is provided and is an array, it is assigned to the `_elements
|
|
15
16
|
*/
|
|
16
17
|
constructor(elements) {
|
|
18
|
+
super();
|
|
17
19
|
this._elements = [];
|
|
18
20
|
if (elements) {
|
|
19
21
|
for (const el of elements) {
|
|
@@ -138,57 +140,74 @@ class Stack {
|
|
|
138
140
|
return new Stack(this.elements.slice());
|
|
139
141
|
}
|
|
140
142
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
+
* Time Complexity: O(n)
|
|
144
|
+
* Space Complexity: O(n)
|
|
143
145
|
*/
|
|
144
|
-
*[Symbol.iterator]() {
|
|
145
|
-
for (let i = 0; i < this.elements.length; i++) {
|
|
146
|
-
yield this.elements[i];
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
146
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
147
|
+
* Time Complexity: O(n)
|
|
148
|
+
* Space Complexity: O(n)
|
|
149
|
+
*
|
|
150
|
+
* The `filter` function creates a new stack containing elements from the original stack that satisfy
|
|
151
|
+
* a given predicate function.
|
|
152
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
153
|
+
* the current element being iterated over, the index of the current element, and the stack itself.
|
|
154
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
155
|
+
* stack or not.
|
|
156
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
157
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
158
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
159
|
+
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
160
|
+
* satisfy the given predicate function.
|
|
161
|
+
*/
|
|
162
|
+
filter(predicate, thisArg) {
|
|
161
163
|
const newStack = new Stack();
|
|
162
164
|
let index = 0;
|
|
163
165
|
for (const el of this) {
|
|
164
|
-
if (predicate(el, index, this)) {
|
|
166
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
165
167
|
newStack.push(el);
|
|
166
168
|
}
|
|
167
169
|
index++;
|
|
168
170
|
}
|
|
169
171
|
return newStack;
|
|
170
172
|
}
|
|
171
|
-
|
|
173
|
+
/**
|
|
174
|
+
* Time Complexity: O(n)
|
|
175
|
+
* Space Complexity: O(n)
|
|
176
|
+
*/
|
|
177
|
+
/**
|
|
178
|
+
* Time Complexity: O(n)
|
|
179
|
+
* Space Complexity: O(n)
|
|
180
|
+
*
|
|
181
|
+
* The `map` function takes a callback function and applies it to each element in the stack,
|
|
182
|
+
* returning a new stack with the results.
|
|
183
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
184
|
+
* the stack. It takes three arguments:
|
|
185
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
186
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
187
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
188
|
+
* @returns The `map` method is returning a new `Stack` object.
|
|
189
|
+
*/
|
|
190
|
+
map(callback, thisArg) {
|
|
172
191
|
const newStack = new Stack();
|
|
173
192
|
let index = 0;
|
|
174
193
|
for (const el of this) {
|
|
175
|
-
newStack.push(callback(el, index, this));
|
|
194
|
+
newStack.push(callback.call(thisArg, el, index, this));
|
|
176
195
|
index++;
|
|
177
196
|
}
|
|
178
197
|
return newStack;
|
|
179
198
|
}
|
|
180
|
-
reduce(callback, initialValue) {
|
|
181
|
-
let accumulator = initialValue;
|
|
182
|
-
let index = 0;
|
|
183
|
-
for (const el of this) {
|
|
184
|
-
accumulator = callback(accumulator, el, index, this);
|
|
185
|
-
index++;
|
|
186
|
-
}
|
|
187
|
-
return accumulator;
|
|
188
|
-
}
|
|
189
199
|
print() {
|
|
190
200
|
console.log([...this]);
|
|
191
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Custom iterator for the Stack class.
|
|
204
|
+
* @returns An iterator object.
|
|
205
|
+
*/
|
|
206
|
+
*_getIterator() {
|
|
207
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
208
|
+
yield this.elements[i];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
192
211
|
}
|
|
193
212
|
exports.Stack = Stack;
|
|
194
213
|
//# sourceMappingURL=stack.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../../../../src/data-structures/stack/stack.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,MAAa,
|
|
1
|
+
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../../../../src/data-structures/stack/stack.ts"],"names":[],"mappings":";;;AAAA,kCAA8C;AAG9C;;;;GAIG;AACH,MAAa,KAAe,SAAQ,0BAAsB;IACxD;;;;;OAKG;IACH,YAAY,QAAsB;QAChC,KAAK,EAAE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IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC9C,MAAM,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;CACF;AAjOD,sBAiOC"}
|
|
@@ -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.
|