directed-graph-typed 1.48.0 → 1.48.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +45 -40
  6. package/dist/data-structures/binary-tree/binary-tree.js +91 -88
  7. package/dist/data-structures/binary-tree/tree-multimap.d.ts +12 -0
  8. package/dist/data-structures/binary-tree/tree-multimap.js +16 -0
  9. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  10. package/dist/data-structures/graph/abstract-graph.js +50 -27
  11. package/dist/data-structures/hash/hash-map.d.ts +160 -44
  12. package/dist/data-structures/hash/hash-map.js +314 -82
  13. package/dist/data-structures/heap/heap.d.ts +50 -7
  14. package/dist/data-structures/heap/heap.js +60 -30
  15. package/dist/data-structures/index.d.ts +1 -0
  16. package/dist/data-structures/index.js +1 -0
  17. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  18. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  19. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  20. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  21. package/dist/data-structures/queue/deque.d.ts +29 -51
  22. package/dist/data-structures/queue/deque.js +36 -71
  23. package/dist/data-structures/queue/queue.d.ts +49 -48
  24. package/dist/data-structures/queue/queue.js +69 -82
  25. package/dist/data-structures/stack/stack.d.ts +43 -10
  26. package/dist/data-structures/stack/stack.js +50 -31
  27. package/dist/data-structures/trie/trie.d.ts +41 -6
  28. package/dist/data-structures/trie/trie.js +53 -32
  29. package/dist/types/data-structures/base/base.d.ts +5 -0
  30. package/dist/types/data-structures/base/base.js +2 -0
  31. package/dist/types/data-structures/base/index.d.ts +1 -0
  32. package/dist/types/data-structures/base/index.js +17 -0
  33. package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
  34. package/dist/types/data-structures/index.d.ts +1 -0
  35. package/dist/types/data-structures/index.js +1 -0
  36. package/package.json +2 -2
  37. package/src/data-structures/base/index.ts +1 -0
  38. package/src/data-structures/base/iterable-base.ts +329 -0
  39. package/src/data-structures/binary-tree/binary-tree.ts +98 -93
  40. package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
  41. package/src/data-structures/graph/abstract-graph.ts +55 -28
  42. package/src/data-structures/hash/hash-map.ts +334 -83
  43. package/src/data-structures/heap/heap.ts +63 -36
  44. package/src/data-structures/index.ts +1 -0
  45. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  46. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  47. package/src/data-structures/queue/deque.ts +40 -82
  48. package/src/data-structures/queue/queue.ts +72 -87
  49. package/src/data-structures/stack/stack.ts +53 -34
  50. package/src/data-structures/trie/trie.ts +58 -35
  51. package/src/types/data-structures/base/base.ts +6 -0
  52. package/src/types/data-structures/base/index.ts +1 -0
  53. package/src/types/data-structures/hash/hash-map.ts +2 -0
  54. package/src/types/data-structures/index.ts +1 -0
@@ -5,14 +5,15 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { IterableWithSizeOrLength } from "../../types";
8
+ import { ElementCallback, IterableWithSizeOrLength } from "../../types";
9
+ import { IterableElementBase } from "../base";
9
10
  /**
10
11
  * Deque can provide random access with O(1) time complexity
11
12
  * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
12
13
  * Deque may experience performance jitter, but DoublyLinkedList will not
13
14
  * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
14
15
  */
15
- export declare class Deque<E> {
16
+ export declare class Deque<E> extends IterableElementBase<E> {
16
17
  protected _bucketFirst: number;
17
18
  protected _firstInBucket: number;
18
19
  protected _bucketLast: number;
@@ -354,32 +355,6 @@ export declare class Deque<E> {
354
355
  * @returns The `toArray()` method is returning an array of elements of type `E`.
355
356
  */
356
357
  toArray(): E[];
357
- /**
358
- * Time Complexity: O(n)
359
- * Space Complexity: O(1)
360
- */
361
- /**
362
- * Time Complexity: O(n)
363
- * Space Complexity: O(1)
364
- *
365
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
366
- * object to be iterated over using a for...of loop.
367
- */
368
- [Symbol.iterator](): Generator<E, void, unknown>;
369
- /**
370
- * Time Complexity: O(n)
371
- * Space Complexity: O(1)
372
- */
373
- /**
374
- * Time Complexity: O(n)
375
- * Space Complexity: O(1)
376
- *
377
- * The `forEach` function iterates over each element in a deque and applies a callback function to
378
- * each element.
379
- * @param callback - The callback parameter is a function that will be called for each element in the
380
- * deque. It takes three parameters:
381
- */
382
- forEach(callback: (element: E, index: number, deque: this) => void): void;
383
358
  /**
384
359
  * Time Complexity: O(n)
385
360
  * Space Complexity: O(n)
@@ -388,14 +363,19 @@ export declare class Deque<E> {
388
363
  * Time Complexity: O(n)
389
364
  * Space Complexity: O(n)
390
365
  *
391
- * The `filter` function creates a new deque containing only the elements that satisfy the given
392
- * predicate function.
393
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
394
- * `index`, and `deque`.
395
- * @returns The `filter` method is returning a new `Deque` object that contains only the elements
396
- * that satisfy the given `predicate` function.
366
+ * The `filter` function creates a new deque containing elements from the original deque that satisfy
367
+ * a given predicate function.
368
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
369
+ * the current element being iterated over, the index of the current element, and the deque itself.
370
+ * It should return a boolean value indicating whether the element should be included in the filtered
371
+ * deque or not.
372
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
373
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
374
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
375
+ * @returns The `filter` method is returning a new `Deque` object that contains the elements that
376
+ * satisfy the given predicate function.
397
377
  */
398
- filter(predicate: (element: E, index: number, deque: this) => boolean): Deque<E>;
378
+ filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E>;
399
379
  /**
400
380
  * Time Complexity: O(n)
401
381
  * Space Complexity: O(n)
@@ -404,31 +384,29 @@ export declare class Deque<E> {
404
384
  * Time Complexity: O(n)
405
385
  * Space Complexity: O(n)
406
386
  *
407
- * The `map` function takes a callback function and applies it to each element in the deque,
408
- * returning a new deque with the results.
409
- * @param callback - The `callback` parameter is a function that takes three arguments:
410
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
387
+ * The `map` function creates a new Deque by applying a callback function to each element of the
388
+ * original Deque.
389
+ * @param callback - The `callback` parameter is a function that will be called for each element in
390
+ * the deque. It takes three arguments:
391
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
392
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
393
+ * passed as the `this` value to the `callback` function. If `thisArg` is
394
+ * @returns a new Deque object with the mapped values.
411
395
  */
412
- map<T>(callback: (element: E, index: number, deque: this) => T): Deque<T>;
396
+ map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>;
413
397
  /**
414
398
  * Time Complexity: O(n)
415
- * Space Complexity: O(1)
399
+ * Space Complexity: O(n)
416
400
  */
401
+ print(): void;
417
402
  /**
418
403
  * Time Complexity: O(n)
419
404
  * Space Complexity: O(1)
420
405
  *
421
- * The `reduce` function iterates over the elements of a deque and applies a callback function to
422
- * each element, accumulating a single value.
423
- * @param callback - The `callback` parameter is a function that takes four arguments:
424
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
425
- * is the value that will be passed as the first argument to the `callback` function when reducing
426
- * the elements of the deque.
427
- * @returns the final value of the accumulator after iterating over all elements in the deque and
428
- * applying the callback function to each element.
406
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
407
+ * object to be iterated over using a for...of loop.
429
408
  */
430
- reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T;
431
- print(): void;
409
+ protected _getIterator(): Generator<E, void, unknown>;
432
410
  /**
433
411
  * Time Complexity: O(n)
434
412
  * Space Complexity: O(n)
@@ -9,13 +9,14 @@
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.ObjectDeque = exports.Deque = void 0;
11
11
  const utils_1 = require("../../utils");
12
+ const base_1 = require("../base");
12
13
  /**
13
14
  * Deque can provide random access with O(1) time complexity
14
15
  * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
15
16
  * Deque may experience performance jitter, but DoublyLinkedList will not
16
17
  * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
17
18
  */
18
- class Deque {
19
+ class Deque extends base_1.IterableElementBase {
19
20
  /**
20
21
  * The constructor initializes a data structure with a specified bucket size and populates it with
21
22
  * elements from an iterable.
@@ -26,6 +27,7 @@ class Deque {
26
27
  * stored in each bucket. It determines the size of each bucket in the data structure.
27
28
  */
28
29
  constructor(elements = [], bucketSize = (1 << 12)) {
30
+ super();
29
31
  this._bucketFirst = 0;
30
32
  this._firstInBucket = 0;
31
33
  this._bucketLast = 0;
@@ -651,42 +653,6 @@ class Deque {
651
653
  }
652
654
  return arr;
653
655
  }
654
- /**
655
- * Time Complexity: O(n)
656
- * Space Complexity: O(1)
657
- */
658
- /**
659
- * Time Complexity: O(n)
660
- * Space Complexity: O(1)
661
- *
662
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
663
- * object to be iterated over using a for...of loop.
664
- */
665
- *[Symbol.iterator]() {
666
- for (let i = 0; i < this.size; ++i) {
667
- yield this.getAt(i);
668
- }
669
- }
670
- /**
671
- * Time Complexity: O(n)
672
- * Space Complexity: O(1)
673
- */
674
- /**
675
- * Time Complexity: O(n)
676
- * Space Complexity: O(1)
677
- *
678
- * The `forEach` function iterates over each element in a deque and applies a callback function to
679
- * each element.
680
- * @param callback - The callback parameter is a function that will be called for each element in the
681
- * deque. It takes three parameters:
682
- */
683
- forEach(callback) {
684
- let index = 0;
685
- for (const el of this) {
686
- callback(el, index, this);
687
- index++;
688
- }
689
- }
690
656
  /**
691
657
  * Time Complexity: O(n)
692
658
  * Space Complexity: O(n)
@@ -695,18 +661,23 @@ class Deque {
695
661
  * Time Complexity: O(n)
696
662
  * Space Complexity: O(n)
697
663
  *
698
- * The `filter` function creates a new deque containing only the elements that satisfy the given
699
- * predicate function.
700
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
701
- * `index`, and `deque`.
702
- * @returns The `filter` method is returning a new `Deque` object that contains only the elements
703
- * that satisfy the given `predicate` function.
704
- */
705
- filter(predicate) {
664
+ * The `filter` function creates a new deque containing elements from the original deque that satisfy
665
+ * a given predicate function.
666
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
667
+ * the current element being iterated over, the index of the current element, and the deque itself.
668
+ * It should return a boolean value indicating whether the element should be included in the filtered
669
+ * deque or not.
670
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
671
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
672
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
673
+ * @returns The `filter` method is returning a new `Deque` object that contains the elements that
674
+ * satisfy the given predicate function.
675
+ */
676
+ filter(predicate, thisArg) {
706
677
  const newDeque = new Deque([], this._bucketSize);
707
678
  let index = 0;
708
679
  for (const el of this) {
709
- if (predicate(el, index, this)) {
680
+ if (predicate.call(thisArg, el, index, this)) {
710
681
  newDeque.push(el);
711
682
  }
712
683
  index++;
@@ -721,48 +692,42 @@ class Deque {
721
692
  * Time Complexity: O(n)
722
693
  * Space Complexity: O(n)
723
694
  *
724
- * The `map` function takes a callback function and applies it to each element in the deque,
725
- * returning a new deque with the results.
726
- * @param callback - The `callback` parameter is a function that takes three arguments:
727
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
695
+ * The `map` function creates a new Deque by applying a callback function to each element of the
696
+ * original Deque.
697
+ * @param callback - The `callback` parameter is a function that will be called for each element in
698
+ * the deque. It takes three arguments:
699
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
700
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
701
+ * passed as the `this` value to the `callback` function. If `thisArg` is
702
+ * @returns a new Deque object with the mapped values.
728
703
  */
729
- map(callback) {
704
+ map(callback, thisArg) {
730
705
  const newDeque = new Deque([], this._bucketSize);
731
706
  let index = 0;
732
707
  for (const el of this) {
733
- newDeque.push(callback(el, index, this));
708
+ newDeque.push(callback.call(thisArg, el, index, this));
734
709
  index++;
735
710
  }
736
711
  return newDeque;
737
712
  }
738
713
  /**
739
714
  * Time Complexity: O(n)
740
- * Space Complexity: O(1)
715
+ * Space Complexity: O(n)
741
716
  */
717
+ print() {
718
+ console.log([...this]);
719
+ }
742
720
  /**
743
721
  * Time Complexity: O(n)
744
722
  * Space Complexity: O(1)
745
723
  *
746
- * The `reduce` function iterates over the elements of a deque and applies a callback function to
747
- * each element, accumulating a single value.
748
- * @param callback - The `callback` parameter is a function that takes four arguments:
749
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
750
- * is the value that will be passed as the first argument to the `callback` function when reducing
751
- * the elements of the deque.
752
- * @returns the final value of the accumulator after iterating over all elements in the deque and
753
- * applying the callback function to each element.
724
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
725
+ * object to be iterated over using a for...of loop.
754
726
  */
755
- reduce(callback, initialValue) {
756
- let accumulator = initialValue;
757
- let index = 0;
758
- for (const el of this) {
759
- accumulator = callback(accumulator, el, index, this);
760
- index++;
727
+ *_getIterator() {
728
+ for (let i = 0; i < this.size; ++i) {
729
+ yield this.getAt(i);
761
730
  }
762
- return accumulator;
763
- }
764
- print() {
765
- console.log([...this]);
766
731
  }
767
732
  /**
768
733
  * Time Complexity: O(n)
@@ -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,28 +277,65 @@ 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;