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.
Files changed (114) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +24 -18
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +182 -182
  5. package/dist/cjs/data-structures/base/index.d.ts +1 -0
  6. package/dist/cjs/data-structures/base/index.js +18 -0
  7. package/dist/cjs/data-structures/base/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
  9. package/dist/cjs/data-structures/base/iterable-base.js +313 -0
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +78 -129
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
  15. package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
  16. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  17. package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/cjs/data-structures/hash/hash-map.js +69 -173
  19. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  20. package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
  21. package/dist/cjs/data-structures/heap/heap.js +60 -30
  22. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  23. package/dist/cjs/data-structures/index.d.ts +1 -0
  24. package/dist/cjs/data-structures/index.js +1 -0
  25. package/dist/cjs/data-structures/index.js.map +1 -1
  26. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  27. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  28. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  30. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
  33. package/dist/cjs/data-structures/queue/deque.js +36 -71
  34. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  35. package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
  36. package/dist/cjs/data-structures/queue/queue.js +69 -82
  37. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  38. package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
  39. package/dist/cjs/data-structures/stack/stack.js +50 -31
  40. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  41. package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
  42. package/dist/cjs/data-structures/trie/trie.js +53 -32
  43. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  44. package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
  45. package/dist/cjs/types/data-structures/base/base.js +3 -0
  46. package/dist/cjs/types/data-structures/base/base.js.map +1 -0
  47. package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
  48. package/dist/cjs/types/data-structures/base/index.js +18 -0
  49. package/dist/cjs/types/data-structures/base/index.js.map +1 -0
  50. package/dist/cjs/types/data-structures/index.d.ts +1 -0
  51. package/dist/cjs/types/data-structures/index.js +1 -0
  52. package/dist/cjs/types/data-structures/index.js.map +1 -1
  53. package/dist/mjs/data-structures/base/index.d.ts +1 -0
  54. package/dist/mjs/data-structures/base/index.js +1 -0
  55. package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
  56. package/dist/mjs/data-structures/base/iterable-base.js +307 -0
  57. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  58. package/dist/mjs/data-structures/binary-tree/binary-tree.js +78 -129
  59. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
  60. package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
  61. package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
  62. package/dist/mjs/data-structures/hash/hash-map.js +69 -173
  63. package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
  64. package/dist/mjs/data-structures/heap/heap.js +60 -30
  65. package/dist/mjs/data-structures/index.d.ts +1 -0
  66. package/dist/mjs/data-structures/index.js +1 -0
  67. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  68. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  69. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  70. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
  71. package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
  72. package/dist/mjs/data-structures/queue/deque.js +36 -71
  73. package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
  74. package/dist/mjs/data-structures/queue/queue.js +66 -79
  75. package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
  76. package/dist/mjs/data-structures/stack/stack.js +50 -31
  77. package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
  78. package/dist/mjs/data-structures/trie/trie.js +53 -32
  79. package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
  80. package/dist/mjs/types/data-structures/base/base.js +1 -0
  81. package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
  82. package/dist/mjs/types/data-structures/base/index.js +1 -0
  83. package/dist/mjs/types/data-structures/index.d.ts +1 -0
  84. package/dist/mjs/types/data-structures/index.js +1 -0
  85. package/dist/umd/data-structure-typed.js +846 -715
  86. package/dist/umd/data-structure-typed.min.js +2 -2
  87. package/dist/umd/data-structure-typed.min.js.map +1 -1
  88. package/package.json +1 -1
  89. package/src/data-structures/base/index.ts +1 -0
  90. package/src/data-structures/base/iterable-base.ts +329 -0
  91. package/src/data-structures/binary-tree/binary-tree.ts +82 -138
  92. package/src/data-structures/graph/abstract-graph.ts +55 -28
  93. package/src/data-structures/hash/hash-map.ts +76 -185
  94. package/src/data-structures/heap/heap.ts +63 -36
  95. package/src/data-structures/index.ts +1 -0
  96. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  97. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  98. package/src/data-structures/queue/deque.ts +40 -82
  99. package/src/data-structures/queue/queue.ts +72 -87
  100. package/src/data-structures/stack/stack.ts +53 -34
  101. package/src/data-structures/trie/trie.ts +58 -35
  102. package/src/types/data-structures/base/base.ts +6 -0
  103. package/src/types/data-structures/base/index.ts +1 -0
  104. package/src/types/data-structures/index.ts +1 -0
  105. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
  106. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
  107. package/test/unit/data-structures/binary-tree/bst.test.ts +10 -10
  108. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
  109. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +10 -10
  110. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  111. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  112. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
  113. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
  114. 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
- export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
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: 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(1)
191
+ * Space Complexity: O(n)
213
192
  */
214
193
  /**
215
194
  * Time Complexity: O(n)
216
- * Space Complexity: O(1)
195
+ * Space Complexity: O(n)
217
196
  *
218
- * The `forEach` function iterates over each element in a deque and applies a callback function to
219
- * each element.
220
- * @param callback - The callback parameter is a function that will be called for each element in the
221
- * deque. It takes three parameters:
222
- */
223
- forEach(callback: (element: E, index: number, queue: this) => void): void;
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 `filter` function creates a new deque containing only the elements that satisfy the given
233
- * predicate function.
234
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
235
- * `index`, and `deque`.
236
- * @returns The `filter` method is returning a new `Queue` object that contains only the elements
237
- * that satisfy the given `predicate` function.
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
- filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E>;
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
- * Time Complexity: O(n)
246
- * Space Complexity: O(n)
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
- map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T>;
254
- reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T;
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.Queue = exports.LinkedListQueue = void 0;
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
- class LinkedListQueue extends linked_list_1.SinglyLinkedList {
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 deque containing only the elements that satisfy the given
305
- * predicate function.
306
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
307
- * `index`, and `deque`.
308
- * @returns The `filter` method is returning a new `Queue` object that contains only the elements
309
- * that satisfy the given `predicate` function.
310
- */
311
- filter(predicate) {
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 deque,
331
- * returning a new deque with the results.
332
- * @param callback - The `callback` parameter is a function that takes three arguments:
333
- * @returns The `map` method is returning a new `Queue` object with the transformed elements.
334
- */
335
- map(callback) {
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
- reduce(callback, initialValue) {
345
- let accumulator = initialValue;
346
- let index = 0;
347
- for (const el of this) {
348
- accumulator = callback(accumulator, el, index, this);
349
- index++;
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;AAElD,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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,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
- * Custom iterator for the Stack class.
108
- * @returns An iterator object.
109
+ * Time Complexity: O(n)
110
+ * Space Complexity: O(n)
109
111
  */
110
- [Symbol.iterator](): Generator<E, void, unknown>;
111
112
  /**
112
- * Applies a function to each element of the stack.
113
- * @param {function(E): void} callback - A function to apply to each element.
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
- forEach(callback: (element: E, index: number, stack: this) => void): void;
116
- filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
117
- map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
118
- reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
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
- * Custom iterator for the Stack class.
142
- * @returns An iterator object.
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
- * Applies a function to each element of the stack.
151
- * @param {function(E): void} callback - A function to apply to each element.
152
- */
153
- forEach(callback) {
154
- let index = 0;
155
- for (const el of this) {
156
- callback(el, index, this);
157
- index++;
158
- }
159
- }
160
- filter(predicate) {
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
- map(callback) {
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,KAAK;IAChB;;;;;OAKG;IACH,YAAY,QAAsB;QAChC,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,IAAI,QAAQ,EAAE,CAAC;YACb,KAAK,MAAM,EAAE,IAAI,QAAQ,EAAE,CAAC;gBAC1B,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAChB,CAAC;QACH,CAAC;IACH,CAAC;IAID,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;;OAGG;IAEH;;;OAGG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC9B,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,SAAS,CAAI,QAAa;QAC/B,OAAO,IAAI,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,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YACvD,KAAK,EAAE,CAAC;QACV,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,KAAK;QACH,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACzB,CAAC;IAED;;;OAGG;IACM,CAAE,YAAY;QACrB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,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
- [Symbol.iterator](): IterableIterator<string>;
146
- forEach(callback: (word: string, index: number, trie: this) => void): void;
147
- filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
148
- map(callback: (word: string, index: number, trie: this) => string): Trie;
149
- reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
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.