data-structure-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 (144) 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 +188 -308
  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/avl-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
  14. package/dist/cjs/data-structures/binary-tree/binary-tree.js +91 -88
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +16 -0
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  22. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
  23. package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
  24. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  25. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  27. package/dist/cjs/data-structures/hash/hash-map.d.ts +160 -44
  28. package/dist/cjs/data-structures/hash/hash-map.js +314 -82
  29. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  30. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  31. package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
  32. package/dist/cjs/data-structures/heap/heap.js +60 -30
  33. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  34. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  35. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  36. package/dist/cjs/data-structures/index.d.ts +1 -0
  37. package/dist/cjs/data-structures/index.js +1 -0
  38. package/dist/cjs/data-structures/index.js.map +1 -1
  39. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  40. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  42. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  43. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  45. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  46. package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
  47. package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
  48. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  49. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  51. package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
  52. package/dist/cjs/data-structures/queue/deque.js +36 -71
  53. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
  55. package/dist/cjs/data-structures/queue/queue.js +69 -82
  56. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  57. package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
  58. package/dist/cjs/data-structures/stack/stack.js +50 -31
  59. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  60. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  61. package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
  62. package/dist/cjs/data-structures/trie/trie.js +53 -32
  63. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  64. package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
  65. package/dist/cjs/types/data-structures/base/base.js +3 -0
  66. package/dist/cjs/types/data-structures/base/base.js.map +1 -0
  67. package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
  68. package/dist/cjs/types/data-structures/base/index.js +18 -0
  69. package/dist/cjs/types/data-structures/base/index.js.map +1 -0
  70. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
  71. package/dist/cjs/types/data-structures/index.d.ts +1 -0
  72. package/dist/cjs/types/data-structures/index.js +1 -0
  73. package/dist/cjs/types/data-structures/index.js.map +1 -1
  74. package/dist/cjs/utils/utils.js.map +1 -1
  75. package/dist/mjs/data-structures/base/index.d.ts +1 -0
  76. package/dist/mjs/data-structures/base/index.js +1 -0
  77. package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
  78. package/dist/mjs/data-structures/base/iterable-base.js +307 -0
  79. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
  80. package/dist/mjs/data-structures/binary-tree/binary-tree.js +91 -88
  81. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
  82. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +16 -0
  83. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
  84. package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
  85. package/dist/mjs/data-structures/hash/hash-map.d.ts +160 -44
  86. package/dist/mjs/data-structures/hash/hash-map.js +310 -80
  87. package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
  88. package/dist/mjs/data-structures/heap/heap.js +60 -30
  89. package/dist/mjs/data-structures/index.d.ts +1 -0
  90. package/dist/mjs/data-structures/index.js +1 -0
  91. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  93. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
  95. package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
  96. package/dist/mjs/data-structures/queue/deque.js +36 -71
  97. package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
  98. package/dist/mjs/data-structures/queue/queue.js +66 -79
  99. package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
  100. package/dist/mjs/data-structures/stack/stack.js +50 -31
  101. package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
  102. package/dist/mjs/data-structures/trie/trie.js +53 -32
  103. package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
  104. package/dist/mjs/types/data-structures/base/base.js +1 -0
  105. package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
  106. package/dist/mjs/types/data-structures/base/index.js +1 -0
  107. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
  108. package/dist/mjs/types/data-structures/index.d.ts +1 -0
  109. package/dist/mjs/types/data-structures/index.js +1 -0
  110. package/dist/umd/data-structure-typed.js +1104 -575
  111. package/dist/umd/data-structure-typed.min.js +2 -2
  112. package/dist/umd/data-structure-typed.min.js.map +1 -1
  113. package/package.json +2 -2
  114. package/src/data-structures/base/index.ts +1 -0
  115. package/src/data-structures/base/iterable-base.ts +329 -0
  116. package/src/data-structures/binary-tree/binary-tree.ts +98 -93
  117. package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
  118. package/src/data-structures/graph/abstract-graph.ts +55 -28
  119. package/src/data-structures/hash/hash-map.ts +334 -83
  120. package/src/data-structures/heap/heap.ts +63 -36
  121. package/src/data-structures/index.ts +1 -0
  122. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  123. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  124. package/src/data-structures/queue/deque.ts +40 -82
  125. package/src/data-structures/queue/queue.ts +72 -87
  126. package/src/data-structures/stack/stack.ts +53 -34
  127. package/src/data-structures/trie/trie.ts +58 -35
  128. package/src/types/data-structures/base/base.ts +6 -0
  129. package/src/types/data-structures/base/index.ts +1 -0
  130. package/src/types/data-structures/hash/hash-map.ts +2 -0
  131. package/src/types/data-structures/index.ts +1 -0
  132. package/test/integration/avl-tree.test.ts +2 -2
  133. package/test/integration/bst.test.ts +21 -25
  134. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
  135. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -8
  136. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +24 -9
  137. package/test/unit/data-structures/binary-tree/bst.test.ts +24 -8
  138. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
  139. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
  140. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  141. package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
  142. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
  143. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
  144. package/test/unit/data-structures/queue/deque.test.ts +25 -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)
@@ -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
  }
@@ -4,37 +4,8 @@
4
4
  * @class
5
5
  */
6
6
  import { SinglyLinkedList } from '../linked-list';
7
- export class LinkedListQueue extends SinglyLinkedList {
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) {
13
- this.push(value);
14
- }
15
- /**
16
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
17
- * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
18
- */
19
- dequeue() {
20
- return this.shift();
21
- }
22
- /**
23
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
24
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
25
- */
26
- getFirst() {
27
- return this.head?.value;
28
- }
29
- /**
30
- * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
31
- * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
32
- */
33
- peek() {
34
- return this.getFirst();
35
- }
36
- }
37
- export class Queue {
7
+ import { IterableElementBase } from "../base";
8
+ export class Queue extends IterableElementBase {
38
9
  /**
39
10
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
40
11
  * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
@@ -42,6 +13,7 @@ export class Queue {
42
13
  * initialized as an empty array.
43
14
  */
44
15
  constructor(elements) {
16
+ super();
45
17
  this._nodes = elements || [];
46
18
  this._offset = 0;
47
19
  }
@@ -265,31 +237,6 @@ export class Queue {
265
237
  print() {
266
238
  console.log([...this]);
267
239
  }
268
- *[Symbol.iterator]() {
269
- for (const item of this.nodes) {
270
- yield item;
271
- }
272
- }
273
- /**
274
- * Time Complexity: O(n)
275
- * Space Complexity: O(1)
276
- */
277
- /**
278
- * Time Complexity: O(n)
279
- * Space Complexity: O(1)
280
- *
281
- * The `forEach` function iterates over each element in a deque and applies a callback function to
282
- * each element.
283
- * @param callback - The callback parameter is a function that will be called for each element in the
284
- * deque. It takes three parameters:
285
- */
286
- forEach(callback) {
287
- let index = 0;
288
- for (const el of this) {
289
- callback(el, index, this);
290
- index++;
291
- }
292
- }
293
240
  /**
294
241
  * Time Complexity: O(n)
295
242
  * Space Complexity: O(n)
@@ -298,18 +245,23 @@ export class Queue {
298
245
  * Time Complexity: O(n)
299
246
  * Space Complexity: O(n)
300
247
  *
301
- * The `filter` function creates a new deque containing only the elements that satisfy the given
302
- * predicate function.
303
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
304
- * `index`, and `deque`.
305
- * @returns The `filter` method is returning a new `Queue` object that contains only the elements
306
- * that satisfy the given `predicate` function.
307
- */
308
- filter(predicate) {
248
+ * The `filter` function creates a new `Queue` object containing elements from the original `Queue`
249
+ * that satisfy a given predicate function.
250
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
251
+ * the current element being iterated over, the index of the current element, and the queue itself.
252
+ * It should return a boolean value indicating whether the element should be included in the filtered
253
+ * queue or not.
254
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
255
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
256
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
257
+ * @returns The `filter` method is returning a new `Queue` object that contains the elements that
258
+ * satisfy the given predicate function.
259
+ */
260
+ filter(predicate, thisArg) {
309
261
  const newDeque = new Queue([]);
310
262
  let index = 0;
311
263
  for (const el of this) {
312
- if (predicate(el, index, this)) {
264
+ if (predicate.call(thisArg, el, index, this)) {
313
265
  newDeque.push(el);
314
266
  }
315
267
  index++;
@@ -324,27 +276,62 @@ export class Queue {
324
276
  * Time Complexity: O(n)
325
277
  * Space Complexity: O(n)
326
278
  *
327
- * The `map` function takes a callback function and applies it to each element in the deque,
328
- * returning a new deque with the results.
329
- * @param callback - The `callback` parameter is a function that takes three arguments:
330
- * @returns The `map` method is returning a new `Queue` object with the transformed elements.
331
- */
332
- map(callback) {
279
+ * The `map` function takes a callback function and applies it to each element in the queue,
280
+ * returning a new queue with the results.
281
+ * @param callback - The callback parameter is a function that will be called for each element in the
282
+ * queue. It takes three arguments: the current element, the index of the current element, and the
283
+ * queue itself. The callback function should return a new value that will be added to the new queue.
284
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
285
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
286
+ * passed as the `this` value to the `callback` function. If `thisArg` is
287
+ * @returns The `map` function is returning a new `Queue` object with the transformed elements.
288
+ */
289
+ map(callback, thisArg) {
333
290
  const newDeque = new Queue([]);
334
291
  let index = 0;
335
292
  for (const el of this) {
336
- newDeque.push(callback(el, index, this));
293
+ newDeque.push(callback.call(thisArg, el, index, this));
337
294
  index++;
338
295
  }
339
296
  return newDeque;
340
297
  }
341
- reduce(callback, initialValue) {
342
- let accumulator = initialValue;
343
- let index = 0;
344
- for (const el of this) {
345
- accumulator = callback(accumulator, el, index, this);
346
- index++;
298
+ /**
299
+ * Time Complexity: O(n)
300
+ * Space Complexity: O(n)
301
+ */
302
+ *_getIterator() {
303
+ for (const item of this.nodes) {
304
+ yield item;
347
305
  }
348
- return accumulator;
306
+ }
307
+ }
308
+ export class LinkedListQueue extends SinglyLinkedList {
309
+ /**
310
+ * The enqueue function adds a value to the end of an array.
311
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
312
+ */
313
+ enqueue(value) {
314
+ this.push(value);
315
+ }
316
+ /**
317
+ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
318
+ * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
319
+ */
320
+ dequeue() {
321
+ return this.shift();
322
+ }
323
+ /**
324
+ * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
325
+ * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
326
+ */
327
+ getFirst() {
328
+ return this.head?.value;
329
+ }
330
+ /**
331
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
332
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
333
+ */
334
+ peek() {
335
+ return this.getFirst();
349
336
  }
350
337
  }