graph-typed 1.48.1 → 1.48.3

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 (76) 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/avl-tree.d.ts +16 -16
  6. package/dist/data-structures/binary-tree/avl-tree.js +7 -7
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
  8. package/dist/data-structures/binary-tree/binary-tree.js +140 -182
  9. package/dist/data-structures/binary-tree/bst.d.ts +28 -47
  10. package/dist/data-structures/binary-tree/bst.js +54 -57
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
  12. package/dist/data-structures/binary-tree/rb-tree.js +7 -7
  13. package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  14. package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
  15. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  16. package/dist/data-structures/graph/abstract-graph.js +50 -27
  17. package/dist/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/data-structures/hash/hash-map.js +69 -173
  19. package/dist/data-structures/heap/heap.d.ts +50 -7
  20. package/dist/data-structures/heap/heap.js +60 -30
  21. package/dist/data-structures/index.d.ts +1 -0
  22. package/dist/data-structures/index.js +1 -0
  23. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  24. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  25. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  26. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  27. package/dist/data-structures/queue/deque.d.ts +29 -51
  28. package/dist/data-structures/queue/deque.js +36 -71
  29. package/dist/data-structures/queue/queue.d.ts +49 -48
  30. package/dist/data-structures/queue/queue.js +69 -82
  31. package/dist/data-structures/stack/stack.d.ts +43 -10
  32. package/dist/data-structures/stack/stack.js +50 -31
  33. package/dist/data-structures/trie/trie.d.ts +41 -6
  34. package/dist/data-structures/trie/trie.js +53 -32
  35. package/dist/interfaces/binary-tree.d.ts +6 -6
  36. package/dist/types/common.d.ts +11 -8
  37. package/dist/types/common.js +6 -1
  38. package/dist/types/data-structures/base/base.d.ts +5 -0
  39. package/dist/types/data-structures/base/base.js +2 -0
  40. package/dist/types/data-structures/base/index.d.ts +1 -0
  41. package/dist/types/data-structures/base/index.js +17 -0
  42. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  45. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  46. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  47. package/dist/types/data-structures/index.d.ts +1 -0
  48. package/dist/types/data-structures/index.js +1 -0
  49. package/package.json +2 -2
  50. package/src/data-structures/base/index.ts +1 -0
  51. package/src/data-structures/base/iterable-base.ts +329 -0
  52. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  53. package/src/data-structures/binary-tree/binary-tree.ts +222 -267
  54. package/src/data-structures/binary-tree/bst.ts +86 -82
  55. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  56. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  57. package/src/data-structures/graph/abstract-graph.ts +55 -28
  58. package/src/data-structures/hash/hash-map.ts +76 -185
  59. package/src/data-structures/heap/heap.ts +63 -36
  60. package/src/data-structures/index.ts +1 -0
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  63. package/src/data-structures/queue/deque.ts +40 -82
  64. package/src/data-structures/queue/queue.ts +72 -87
  65. package/src/data-structures/stack/stack.ts +53 -34
  66. package/src/data-structures/trie/trie.ts +58 -35
  67. package/src/interfaces/binary-tree.ts +5 -6
  68. package/src/types/common.ts +11 -8
  69. package/src/types/data-structures/base/base.ts +6 -0
  70. package/src/types/data-structures/base/index.ts +1 -0
  71. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  72. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  73. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  74. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  75. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  76. package/src/types/data-structures/index.ts +1 -0
@@ -1,3 +1,5 @@
1
+ import { IterableElementBase } from "../base";
2
+ import { ElementCallback } from "../../types";
1
3
  /**
2
4
  * data-structure-typed
3
5
  *
@@ -16,7 +18,7 @@ export declare class DoublyLinkedListNode<E = any> {
16
18
  */
17
19
  constructor(value: E);
18
20
  }
19
- export declare class DoublyLinkedList<E = any> {
21
+ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
20
22
  /**
21
23
  * The constructor initializes the linked list with an empty head, tail, and length.
22
24
  */
@@ -387,71 +389,56 @@ export declare class DoublyLinkedList<E = any> {
387
389
  */
388
390
  toReversedArray(): E[];
389
391
  /**
390
- * The function returns an iterator that iterates over the values of a linked list.
391
- */
392
- [Symbol.iterator](): Generator<E, void, unknown>;
393
- /**
394
- * Time Complexity: O(n), where n is the number of elements in the linked list.
395
- * Space Complexity: O(1)
396
- */
397
- /**
398
- * Time Complexity: O(n), where n is the number of elements in the linked list.
399
- * Space Complexity: O(1)
400
- *
401
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
402
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
403
- * represents the value of the current node in the linked list, and the index argument represents the index of the
404
- * current node in the linked list.
405
- */
406
- forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void;
407
- /**
408
- * Time Complexity: O(n), where n is the number of elements in the linked list.
392
+ * Time Complexity: O(n)
409
393
  * Space Complexity: O(n)
410
394
  */
411
395
  /**
412
- * Time Complexity: O(n), where n is the number of elements in the linked list.
396
+ * Time Complexity: O(n)
413
397
  * Space Complexity: O(n)
414
398
  *
415
- * The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
416
- * elements that satisfy the given callback function.
417
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
418
- * It is used to determine whether a value should be included in the filtered list or not.
419
- * @returns The filtered list, which is an instance of the DoublyLinkedList class.
420
- */
421
- filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E>;
399
+ * The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
400
+ * list and applying a callback function to each element, returning only the elements for which the
401
+ * callback function returns true.
402
+ * @param callback - The `callback` parameter is a function that will be called for each element in
403
+ * the DoublyLinkedList. It takes three arguments: the current element, the index of the current
404
+ * element, and the DoublyLinkedList itself. The callback function should return a boolean value
405
+ * indicating whether the current element should be included
406
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
407
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
408
+ * passed as the `this` value to the `callback` function. If `thisArg` is
409
+ * @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
410
+ * elements that pass the filter condition specified by the `callback` function.
411
+ */
412
+ filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E>;
422
413
  /**
423
414
  * Time Complexity: O(n), where n is the number of elements in the linked list.
424
415
  * Space Complexity: O(n)
425
416
  */
426
417
  /**
427
- * Time Complexity: O(n), where n is the number of elements in the linked list.
418
+ * Time Complexity: O(n)
428
419
  * Space Complexity: O(n)
429
420
  *
430
- * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
431
- * DoublyLinkedList with the transformed values.
432
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
433
- * the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
434
- * DoublyLinkedList).
435
- * @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
436
- */
437
- map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T>;
421
+ * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
422
+ * in the original list.
423
+ * @param callback - The callback parameter is a function that will be called for each element in the
424
+ * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
425
+ * and the DoublyLinkedList itself. The callback function should return a value that will be added to
426
+ * the new DoublyLinkedList that
427
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
428
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
429
+ * passed as the `this` value to the `callback` function. If `thisArg` is
430
+ * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
431
+ * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
432
+ * object.
433
+ */
434
+ map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T>;
438
435
  /**
439
436
  * Time Complexity: O(n), where n is the number of elements in the linked list.
440
437
  * Space Complexity: O(n)
441
438
  */
442
- /**
443
- * Time Complexity: O(n), where n is the number of elements in the linked list.
444
- * Space Complexity: O(n)
445
- *
446
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
447
- * single value.
448
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
449
- * used to perform a specific operation on each element of the linked list.
450
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
451
- * point for the reduction operation.
452
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
453
- * elements in the linked list.
454
- */
455
- reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T;
456
439
  print(): void;
440
+ /**
441
+ * The function returns an iterator that iterates over the values of a linked list.
442
+ */
443
+ protected _getIterator(): IterableIterator<E>;
457
444
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
4
+ const base_1 = require("../base");
4
5
  /**
5
6
  * data-structure-typed
6
7
  *
@@ -21,11 +22,12 @@ class DoublyLinkedListNode {
21
22
  }
22
23
  }
23
24
  exports.DoublyLinkedListNode = DoublyLinkedListNode;
24
- class DoublyLinkedList {
25
+ class DoublyLinkedList extends base_1.IterableElementBase {
25
26
  /**
26
27
  * The constructor initializes the linked list with an empty head, tail, and length.
27
28
  */
28
29
  constructor(elements) {
30
+ super();
29
31
  this._head = undefined;
30
32
  this._tail = undefined;
31
33
  this._length = 0;
@@ -667,54 +669,31 @@ class DoublyLinkedList {
667
669
  return array;
668
670
  }
669
671
  /**
670
- * The function returns an iterator that iterates over the values of a linked list.
671
- */
672
- *[Symbol.iterator]() {
673
- let current = this.head;
674
- while (current) {
675
- yield current.value;
676
- current = current.next;
677
- }
678
- }
679
- /**
680
- * Time Complexity: O(n), where n is the number of elements in the linked list.
681
- * Space Complexity: O(1)
682
- */
683
- /**
684
- * Time Complexity: O(n), where n is the number of elements in the linked list.
685
- * Space Complexity: O(1)
686
- *
687
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
688
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
689
- * represents the value of the current node in the linked list, and the index argument represents the index of the
690
- * current node in the linked list.
691
- */
692
- forEach(callback) {
693
- let index = 0;
694
- for (const el of this) {
695
- callback(el, index, this);
696
- index++;
697
- }
698
- }
699
- /**
700
- * Time Complexity: O(n), where n is the number of elements in the linked list.
672
+ * Time Complexity: O(n)
701
673
  * Space Complexity: O(n)
702
674
  */
703
675
  /**
704
- * Time Complexity: O(n), where n is the number of elements in the linked list.
676
+ * Time Complexity: O(n)
705
677
  * Space Complexity: O(n)
706
678
  *
707
- * The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
708
- * elements that satisfy the given callback function.
709
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
710
- * It is used to determine whether a value should be included in the filtered list or not.
711
- * @returns The filtered list, which is an instance of the DoublyLinkedList class.
712
- */
713
- filter(callback) {
679
+ * The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
680
+ * list and applying a callback function to each element, returning only the elements for which the
681
+ * callback function returns true.
682
+ * @param callback - The `callback` parameter is a function that will be called for each element in
683
+ * the DoublyLinkedList. It takes three arguments: the current element, the index of the current
684
+ * element, and the DoublyLinkedList itself. The callback function should return a boolean value
685
+ * indicating whether the current element should be included
686
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
687
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
688
+ * passed as the `this` value to the `callback` function. If `thisArg` is
689
+ * @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
690
+ * elements that pass the filter condition specified by the `callback` function.
691
+ */
692
+ filter(callback, thisArg) {
714
693
  const filteredList = new DoublyLinkedList();
715
694
  let index = 0;
716
695
  for (const current of this) {
717
- if (callback(current, index, this)) {
696
+ if (callback.call(thisArg, current, index, this)) {
718
697
  filteredList.push(current);
719
698
  }
720
699
  index++;
@@ -726,21 +705,27 @@ class DoublyLinkedList {
726
705
  * Space Complexity: O(n)
727
706
  */
728
707
  /**
729
- * Time Complexity: O(n), where n is the number of elements in the linked list.
708
+ * Time Complexity: O(n)
730
709
  * Space Complexity: O(n)
731
710
  *
732
- * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
733
- * DoublyLinkedList with the transformed values.
734
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
735
- * the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
736
- * DoublyLinkedList).
737
- * @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
738
- */
739
- map(callback) {
711
+ * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
712
+ * in the original list.
713
+ * @param callback - The callback parameter is a function that will be called for each element in the
714
+ * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
715
+ * and the DoublyLinkedList itself. The callback function should return a value that will be added to
716
+ * the new DoublyLinkedList that
717
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
718
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
719
+ * passed as the `this` value to the `callback` function. If `thisArg` is
720
+ * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
721
+ * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
722
+ * object.
723
+ */
724
+ map(callback, thisArg) {
740
725
  const mappedList = new DoublyLinkedList();
741
726
  let index = 0;
742
727
  for (const current of this) {
743
- mappedList.push(callback(current, index, this));
728
+ mappedList.push(callback.call(thisArg, current, index, this));
744
729
  index++;
745
730
  }
746
731
  return mappedList;
@@ -749,30 +734,18 @@ class DoublyLinkedList {
749
734
  * Time Complexity: O(n), where n is the number of elements in the linked list.
750
735
  * Space Complexity: O(n)
751
736
  */
752
- /**
753
- * Time Complexity: O(n), where n is the number of elements in the linked list.
754
- * Space Complexity: O(n)
755
- *
756
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
757
- * single value.
758
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
759
- * used to perform a specific operation on each element of the linked list.
760
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
761
- * point for the reduction operation.
762
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
763
- * elements in the linked list.
764
- */
765
- reduce(callback, initialValue) {
766
- let accumulator = initialValue;
767
- let index = 0;
768
- for (const current of this) {
769
- accumulator = callback(accumulator, current, index, this);
770
- index++;
771
- }
772
- return accumulator;
773
- }
774
737
  print() {
775
738
  console.log([...this]);
776
739
  }
740
+ /**
741
+ * The function returns an iterator that iterates over the values of a linked list.
742
+ */
743
+ *_getIterator() {
744
+ let current = this.head;
745
+ while (current) {
746
+ yield current.value;
747
+ current = current.next;
748
+ }
749
+ }
777
750
  }
778
751
  exports.DoublyLinkedList = DoublyLinkedList;
@@ -1,3 +1,5 @@
1
+ import { IterableElementBase } from "../base";
2
+ import { ElementCallback } from "../../types";
1
3
  /**
2
4
  * data-structure-typed
3
5
  *
@@ -15,7 +17,7 @@ export declare class SinglyLinkedListNode<E = any> {
15
17
  */
16
18
  constructor(value: E);
17
19
  }
18
- export declare class SinglyLinkedList<E = any> {
20
+ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
19
21
  /**
20
22
  * The constructor initializes the linked list with an empty head, tail, and length.
21
23
  */
@@ -345,71 +347,50 @@ export declare class SinglyLinkedList<E = any> {
345
347
  */
346
348
  countOccurrences(value: E): number;
347
349
  /**
348
- * The function returns an iterator that iterates over the values of a linked list.
349
- */
350
- [Symbol.iterator](): Generator<E, void, unknown>;
351
- /**
352
- * Time Complexity: O(n), where n is the number of elements in the linked list.
353
- * Space Complexity: O(1)
354
- */
355
- /**
356
- * Time Complexity: O(n), where n is the number of elements in the linked list.
357
- * Space Complexity: O(1)
358
- *
359
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
360
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
361
- * represents the value of the current node in the linked list, and the index argument represents the index of the
362
- * current node in the linked list.
363
- */
364
- forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void;
365
- /**
366
- * Time Complexity: O(n), where n is the number of elements in the linked list.
350
+ * Time Complexity: O(n)
367
351
  * Space Complexity: O(n)
368
352
  */
369
353
  /**
370
- * Time Complexity: O(n), where n is the number of elements in the linked list.
354
+ * Time Complexity: O(n)
371
355
  * Space Complexity: O(n)
372
356
  *
373
- * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
374
- * elements that satisfy the given callback function.
375
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
376
- * It is used to determine whether a value should be included in the filtered list or not.
377
- * @returns The filtered list, which is an instance of the SinglyLinkedList class.
378
- */
379
- filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E>;
357
+ * The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
358
+ * list and applying a callback function to each element to determine if it should be included in the
359
+ * filtered list.
360
+ * @param callback - The callback parameter is a function that will be called for each element in the
361
+ * list. It takes three arguments: the current element, the index of the current element, and the
362
+ * list itself. The callback function should return a boolean value indicating whether the current
363
+ * element should be included in the filtered list or not
364
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
365
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
366
+ * passed as the `this` value to the `callback` function. If `thisArg` is
367
+ * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
368
+ * elements that pass the filter condition specified by the `callback` function.
369
+ */
370
+ filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E>;
380
371
  /**
381
372
  * Time Complexity: O(n), where n is the number of elements in the linked list.
382
373
  * Space Complexity: O(n)
383
374
  */
384
375
  /**
385
- * Time Complexity: O(n), where n is the number of elements in the linked list.
376
+ * Time Complexity: O(n)
386
377
  * Space Complexity: O(n)
387
378
  *
388
- * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
389
- * SinglyLinkedList with the transformed values.
390
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
391
- * the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
392
- * SinglyLinkedList).
393
- * @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
394
- */
395
- map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T>;
379
+ * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
380
+ * of the original list.
381
+ * @param callback - The `callback` parameter is a function that will be called for each element in
382
+ * the linked list. It takes three arguments:
383
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
384
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
385
+ * passed as the `this` value to the `callback` function. If `thisArg` is
386
+ * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
387
+ * of applying the provided `callback` function to each element in the original list.
388
+ */
389
+ map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T>;
396
390
  /**
397
391
  * Time Complexity: O(n), where n is the number of elements in the linked list.
398
392
  * Space Complexity: O(n)
399
393
  */
400
- /**
401
- * Time Complexity: O(n), where n is the number of elements in the linked list.
402
- * Space Complexity: O(n)
403
- *
404
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
405
- * single value.
406
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
407
- * used to perform a specific operation on each element of the linked list.
408
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
409
- * point for the reduction operation.
410
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
411
- * elements in the linked list.
412
- */
413
- reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
414
394
  print(): void;
395
+ protected _getIterator(): IterableIterator<E>;
415
396
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
4
+ const base_1 = require("../base");
4
5
  /**
5
6
  * data-structure-typed
6
7
  *
@@ -20,11 +21,12 @@ class SinglyLinkedListNode {
20
21
  }
21
22
  }
22
23
  exports.SinglyLinkedListNode = SinglyLinkedListNode;
23
- class SinglyLinkedList {
24
+ class SinglyLinkedList extends base_1.IterableElementBase {
24
25
  /**
25
26
  * The constructor initializes the linked list with an empty head, tail, and length.
26
27
  */
27
28
  constructor(elements) {
29
+ super();
28
30
  this._head = undefined;
29
31
  this._tail = undefined;
30
32
  this._length = 0;
@@ -609,54 +611,31 @@ class SinglyLinkedList {
609
611
  return count;
610
612
  }
611
613
  /**
612
- * The function returns an iterator that iterates over the values of a linked list.
613
- */
614
- *[Symbol.iterator]() {
615
- let current = this.head;
616
- while (current) {
617
- yield current.value;
618
- current = current.next;
619
- }
620
- }
621
- /**
622
- * Time Complexity: O(n), where n is the number of elements in the linked list.
623
- * Space Complexity: O(1)
624
- */
625
- /**
626
- * Time Complexity: O(n), where n is the number of elements in the linked list.
627
- * Space Complexity: O(1)
628
- *
629
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
630
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
631
- * represents the value of the current node in the linked list, and the index argument represents the index of the
632
- * current node in the linked list.
633
- */
634
- forEach(callback) {
635
- let index = 0;
636
- for (const el of this) {
637
- callback(el, index, this);
638
- index++;
639
- }
640
- }
641
- /**
642
- * Time Complexity: O(n), where n is the number of elements in the linked list.
614
+ * Time Complexity: O(n)
643
615
  * Space Complexity: O(n)
644
616
  */
645
617
  /**
646
- * Time Complexity: O(n), where n is the number of elements in the linked list.
618
+ * Time Complexity: O(n)
647
619
  * Space Complexity: O(n)
648
620
  *
649
- * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
650
- * elements that satisfy the given callback function.
651
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
652
- * It is used to determine whether a value should be included in the filtered list or not.
653
- * @returns The filtered list, which is an instance of the SinglyLinkedList class.
654
- */
655
- filter(callback) {
621
+ * The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
622
+ * list and applying a callback function to each element to determine if it should be included in the
623
+ * filtered list.
624
+ * @param callback - The callback parameter is a function that will be called for each element in the
625
+ * list. It takes three arguments: the current element, the index of the current element, and the
626
+ * list itself. The callback function should return a boolean value indicating whether the current
627
+ * element should be included in the filtered list or not
628
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
629
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
630
+ * passed as the `this` value to the `callback` function. If `thisArg` is
631
+ * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
632
+ * elements that pass the filter condition specified by the `callback` function.
633
+ */
634
+ filter(callback, thisArg) {
656
635
  const filteredList = new SinglyLinkedList();
657
636
  let index = 0;
658
637
  for (const current of this) {
659
- if (callback(current, index, this)) {
638
+ if (callback.call(thisArg, current, index, this)) {
660
639
  filteredList.push(current);
661
640
  }
662
641
  index++;
@@ -668,21 +647,24 @@ class SinglyLinkedList {
668
647
  * Space Complexity: O(n)
669
648
  */
670
649
  /**
671
- * Time Complexity: O(n), where n is the number of elements in the linked list.
650
+ * Time Complexity: O(n)
672
651
  * Space Complexity: O(n)
673
652
  *
674
- * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
675
- * SinglyLinkedList with the transformed values.
676
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
677
- * the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
678
- * SinglyLinkedList).
679
- * @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
680
- */
681
- map(callback) {
653
+ * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
654
+ * of the original list.
655
+ * @param callback - The `callback` parameter is a function that will be called for each element in
656
+ * the linked list. It takes three arguments:
657
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
658
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
659
+ * passed as the `this` value to the `callback` function. If `thisArg` is
660
+ * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
661
+ * of applying the provided `callback` function to each element in the original list.
662
+ */
663
+ map(callback, thisArg) {
682
664
  const mappedList = new SinglyLinkedList();
683
665
  let index = 0;
684
666
  for (const current of this) {
685
- mappedList.push(callback(current, index, this));
667
+ mappedList.push(callback.call(thisArg, current, index, this));
686
668
  index++;
687
669
  }
688
670
  return mappedList;
@@ -691,30 +673,15 @@ class SinglyLinkedList {
691
673
  * Time Complexity: O(n), where n is the number of elements in the linked list.
692
674
  * Space Complexity: O(n)
693
675
  */
694
- /**
695
- * Time Complexity: O(n), where n is the number of elements in the linked list.
696
- * Space Complexity: O(n)
697
- *
698
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
699
- * single value.
700
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
701
- * used to perform a specific operation on each element of the linked list.
702
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
703
- * point for the reduction operation.
704
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
705
- * elements in the linked list.
706
- */
707
- reduce(callback, initialValue) {
708
- let accumulator = initialValue;
709
- let index = 0;
710
- for (const current of this) {
711
- accumulator = callback(accumulator, current, index, this);
712
- index++;
713
- }
714
- return accumulator;
715
- }
716
676
  print() {
717
677
  console.log([...this]);
718
678
  }
679
+ *_getIterator() {
680
+ let current = this.head;
681
+ while (current) {
682
+ yield current.value;
683
+ current = current.next;
684
+ }
685
+ }
719
686
  }
720
687
  exports.SinglyLinkedList = SinglyLinkedList;