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
@@ -1,3 +1,4 @@
1
+ import { IterableElementBase } from "../base";
1
2
  /**
2
3
  * data-structure-typed
3
4
  *
@@ -18,11 +19,12 @@ export class SinglyLinkedListNode {
18
19
  this.next = undefined;
19
20
  }
20
21
  }
21
- export class SinglyLinkedList {
22
+ export class SinglyLinkedList extends IterableElementBase {
22
23
  /**
23
24
  * The constructor initializes the linked list with an empty head, tail, and length.
24
25
  */
25
26
  constructor(elements) {
27
+ super();
26
28
  this._head = undefined;
27
29
  this._tail = undefined;
28
30
  this._length = 0;
@@ -610,54 +612,31 @@ export class SinglyLinkedList {
610
612
  return count;
611
613
  }
612
614
  /**
613
- * The function returns an iterator that iterates over the values of a linked list.
614
- */
615
- *[Symbol.iterator]() {
616
- let current = this.head;
617
- while (current) {
618
- yield current.value;
619
- current = current.next;
620
- }
621
- }
622
- /**
623
- * Time Complexity: O(n), where n is the number of elements in the linked list.
624
- * Space Complexity: O(1)
625
- */
626
- /**
627
- * Time Complexity: O(n), where n is the number of elements in the linked list.
628
- * Space Complexity: O(1)
629
- *
630
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
631
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
632
- * represents the value of the current node in the linked list, and the index argument represents the index of the
633
- * current node in the linked list.
634
- */
635
- forEach(callback) {
636
- let index = 0;
637
- for (const el of this) {
638
- callback(el, index, this);
639
- index++;
640
- }
641
- }
642
- /**
643
- * Time Complexity: O(n), where n is the number of elements in the linked list.
615
+ * Time Complexity: O(n)
644
616
  * Space Complexity: O(n)
645
617
  */
646
618
  /**
647
- * Time Complexity: O(n), where n is the number of elements in the linked list.
619
+ * Time Complexity: O(n)
648
620
  * Space Complexity: O(n)
649
621
  *
650
- * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
651
- * elements that satisfy the given callback function.
652
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
653
- * It is used to determine whether a value should be included in the filtered list or not.
654
- * @returns The filtered list, which is an instance of the SinglyLinkedList class.
655
- */
656
- filter(callback) {
622
+ * The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
623
+ * list and applying a callback function to each element to determine if it should be included in the
624
+ * filtered list.
625
+ * @param callback - The callback parameter is a function that will be called for each element in the
626
+ * list. It takes three arguments: the current element, the index of the current element, and the
627
+ * list itself. The callback function should return a boolean value indicating whether the current
628
+ * element should be included in the filtered list or not
629
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
630
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
631
+ * passed as the `this` value to the `callback` function. If `thisArg` is
632
+ * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
633
+ * elements that pass the filter condition specified by the `callback` function.
634
+ */
635
+ filter(callback, thisArg) {
657
636
  const filteredList = new SinglyLinkedList();
658
637
  let index = 0;
659
638
  for (const current of this) {
660
- if (callback(current, index, this)) {
639
+ if (callback.call(thisArg, current, index, this)) {
661
640
  filteredList.push(current);
662
641
  }
663
642
  index++;
@@ -669,21 +648,24 @@ export class SinglyLinkedList {
669
648
  * Space Complexity: O(n)
670
649
  */
671
650
  /**
672
- * Time Complexity: O(n), where n is the number of elements in the linked list.
651
+ * Time Complexity: O(n)
673
652
  * Space Complexity: O(n)
674
653
  *
675
- * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
676
- * SinglyLinkedList with the transformed values.
677
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
678
- * the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
679
- * SinglyLinkedList).
680
- * @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
681
- */
682
- map(callback) {
654
+ * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
655
+ * of the original list.
656
+ * @param callback - The `callback` parameter is a function that will be called for each element in
657
+ * the linked list. It takes three arguments:
658
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
659
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
660
+ * passed as the `this` value to the `callback` function. If `thisArg` is
661
+ * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
662
+ * of applying the provided `callback` function to each element in the original list.
663
+ */
664
+ map(callback, thisArg) {
683
665
  const mappedList = new SinglyLinkedList();
684
666
  let index = 0;
685
667
  for (const current of this) {
686
- mappedList.push(callback(current, index, this));
668
+ mappedList.push(callback.call(thisArg, current, index, this));
687
669
  index++;
688
670
  }
689
671
  return mappedList;
@@ -692,29 +674,14 @@ export class SinglyLinkedList {
692
674
  * Time Complexity: O(n), where n is the number of elements in the linked list.
693
675
  * Space Complexity: O(n)
694
676
  */
695
- /**
696
- * Time Complexity: O(n), where n is the number of elements in the linked list.
697
- * Space Complexity: O(n)
698
- *
699
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
700
- * single value.
701
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
702
- * used to perform a specific operation on each element of the linked list.
703
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
704
- * point for the reduction operation.
705
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
706
- * elements in the linked list.
707
- */
708
- reduce(callback, initialValue) {
709
- let accumulator = initialValue;
710
- let index = 0;
711
- for (const current of this) {
712
- accumulator = callback(accumulator, current, index, this);
713
- index++;
714
- }
715
- return accumulator;
716
- }
717
677
  print() {
718
678
  console.log([...this]);
719
679
  }
680
+ *_getIterator() {
681
+ let current = this.head;
682
+ while (current) {
683
+ yield current.value;
684
+ current = current.next;
685
+ }
686
+ }
720
687
  }
@@ -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)
@@ -6,13 +6,14 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { calcMinUnitsRequired, rangeCheck } from "../../utils";
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 class Deque {
16
+ export class Deque extends IterableElementBase {
16
17
  _bucketFirst = 0;
17
18
  _firstInBucket = 0;
18
19
  _bucketLast = 0;
@@ -29,6 +30,7 @@ export class Deque {
29
30
  * stored in each bucket. It determines the size of each bucket in the data structure.
30
31
  */
31
32
  constructor(elements = [], bucketSize = (1 << 12)) {
33
+ super();
32
34
  let _size;
33
35
  if ('length' in elements) {
34
36
  if (elements.length instanceof Function)
@@ -649,42 +651,6 @@ export class Deque {
649
651
  }
650
652
  return arr;
651
653
  }
652
- /**
653
- * Time Complexity: O(n)
654
- * Space Complexity: O(1)
655
- */
656
- /**
657
- * Time Complexity: O(n)
658
- * Space Complexity: O(1)
659
- *
660
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
661
- * object to be iterated over using a for...of loop.
662
- */
663
- *[Symbol.iterator]() {
664
- for (let i = 0; i < this.size; ++i) {
665
- yield this.getAt(i);
666
- }
667
- }
668
- /**
669
- * Time Complexity: O(n)
670
- * Space Complexity: O(1)
671
- */
672
- /**
673
- * Time Complexity: O(n)
674
- * Space Complexity: O(1)
675
- *
676
- * The `forEach` function iterates over each element in a deque and applies a callback function to
677
- * each element.
678
- * @param callback - The callback parameter is a function that will be called for each element in the
679
- * deque. It takes three parameters:
680
- */
681
- forEach(callback) {
682
- let index = 0;
683
- for (const el of this) {
684
- callback(el, index, this);
685
- index++;
686
- }
687
- }
688
654
  /**
689
655
  * Time Complexity: O(n)
690
656
  * Space Complexity: O(n)
@@ -693,18 +659,23 @@ export class Deque {
693
659
  * Time Complexity: O(n)
694
660
  * Space Complexity: O(n)
695
661
  *
696
- * The `filter` function creates a new deque containing only the elements that satisfy the given
697
- * predicate function.
698
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
699
- * `index`, and `deque`.
700
- * @returns The `filter` method is returning a new `Deque` object that contains only the elements
701
- * that satisfy the given `predicate` function.
702
- */
703
- filter(predicate) {
662
+ * The `filter` function creates a new deque containing elements from the original deque that satisfy
663
+ * a given predicate function.
664
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
665
+ * the current element being iterated over, the index of the current element, and the deque itself.
666
+ * It should return a boolean value indicating whether the element should be included in the filtered
667
+ * deque or not.
668
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
669
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
670
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
671
+ * @returns The `filter` method is returning a new `Deque` object that contains the elements that
672
+ * satisfy the given predicate function.
673
+ */
674
+ filter(predicate, thisArg) {
704
675
  const newDeque = new Deque([], this._bucketSize);
705
676
  let index = 0;
706
677
  for (const el of this) {
707
- if (predicate(el, index, this)) {
678
+ if (predicate.call(thisArg, el, index, this)) {
708
679
  newDeque.push(el);
709
680
  }
710
681
  index++;
@@ -719,48 +690,42 @@ export class Deque {
719
690
  * Time Complexity: O(n)
720
691
  * Space Complexity: O(n)
721
692
  *
722
- * The `map` function takes a callback function and applies it to each element in the deque,
723
- * returning a new deque with the results.
724
- * @param callback - The `callback` parameter is a function that takes three arguments:
725
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
693
+ * The `map` function creates a new Deque by applying a callback function to each element of the
694
+ * original Deque.
695
+ * @param callback - The `callback` parameter is a function that will be called for each element in
696
+ * the deque. It takes three arguments:
697
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
698
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
699
+ * passed as the `this` value to the `callback` function. If `thisArg` is
700
+ * @returns a new Deque object with the mapped values.
726
701
  */
727
- map(callback) {
702
+ map(callback, thisArg) {
728
703
  const newDeque = new Deque([], this._bucketSize);
729
704
  let index = 0;
730
705
  for (const el of this) {
731
- newDeque.push(callback(el, index, this));
706
+ newDeque.push(callback.call(thisArg, el, index, this));
732
707
  index++;
733
708
  }
734
709
  return newDeque;
735
710
  }
736
711
  /**
737
712
  * Time Complexity: O(n)
738
- * Space Complexity: O(1)
713
+ * Space Complexity: O(n)
739
714
  */
715
+ print() {
716
+ console.log([...this]);
717
+ }
740
718
  /**
741
719
  * Time Complexity: O(n)
742
720
  * Space Complexity: O(1)
743
721
  *
744
- * The `reduce` function iterates over the elements of a deque and applies a callback function to
745
- * each element, accumulating a single value.
746
- * @param callback - The `callback` parameter is a function that takes four arguments:
747
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
748
- * is the value that will be passed as the first argument to the `callback` function when reducing
749
- * the elements of the deque.
750
- * @returns the final value of the accumulator after iterating over all elements in the deque and
751
- * applying the callback function to each element.
722
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
723
+ * object to be iterated over using a for...of loop.
752
724
  */
753
- reduce(callback, initialValue) {
754
- let accumulator = initialValue;
755
- let index = 0;
756
- for (const el of this) {
757
- accumulator = callback(accumulator, el, index, this);
758
- index++;
725
+ *_getIterator() {
726
+ for (let i = 0; i < this.size; ++i) {
727
+ yield this.getAt(i);
759
728
  }
760
- return accumulator;
761
- }
762
- print() {
763
- console.log([...this]);
764
729
  }
765
730
  /**
766
731
  * 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
  }