doubly-linked-list-typed 1.51.9 → 1.52.1

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 (102) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +13 -13
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +13 -13
  10. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -99
  12. package/dist/data-structures/binary-tree/binary-tree.js +54 -52
  13. package/dist/data-structures/binary-tree/bst.d.ts +37 -45
  14. package/dist/data-structures/binary-tree/bst.js +17 -25
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -10
  16. package/dist/data-structures/binary-tree/rb-tree.js +6 -6
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
  19. package/dist/data-structures/graph/directed-graph.js +2 -1
  20. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  21. package/dist/data-structures/heap/heap.d.ts +43 -114
  22. package/dist/data-structures/heap/heap.js +59 -127
  23. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  24. package/dist/data-structures/heap/max-heap.js +76 -10
  25. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  26. package/dist/data-structures/heap/min-heap.js +68 -11
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  30. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  31. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  33. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  34. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  35. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  36. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  37. package/dist/data-structures/queue/deque.d.ts +27 -18
  38. package/dist/data-structures/queue/deque.js +43 -21
  39. package/dist/data-structures/queue/queue.d.ts +8 -29
  40. package/dist/data-structures/queue/queue.js +15 -32
  41. package/dist/data-structures/stack/stack.d.ts +17 -22
  42. package/dist/data-structures/stack/stack.js +25 -24
  43. package/dist/data-structures/trie/trie.d.ts +18 -13
  44. package/dist/data-structures/trie/trie.js +26 -15
  45. package/dist/interfaces/binary-tree.d.ts +4 -4
  46. package/dist/types/common.d.ts +1 -22
  47. package/dist/types/data-structures/base/base.d.ts +5 -2
  48. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
  49. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  50. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
  51. package/dist/types/data-structures/binary-tree/bst.d.ts +5 -3
  52. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
  54. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  55. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  56. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  57. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  58. package/dist/types/data-structures/queue/deque.d.ts +4 -2
  59. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  60. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  61. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  62. package/package.json +2 -2
  63. package/src/data-structures/base/index.ts +2 -1
  64. package/src/data-structures/base/iterable-element-base.ts +250 -0
  65. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  66. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +14 -15
  67. package/src/data-structures/binary-tree/avl-tree.ts +13 -14
  68. package/src/data-structures/binary-tree/binary-tree.ts +156 -152
  69. package/src/data-structures/binary-tree/bst.ts +52 -60
  70. package/src/data-structures/binary-tree/rb-tree.ts +12 -13
  71. package/src/data-structures/binary-tree/tree-multi-map.ts +12 -13
  72. package/src/data-structures/graph/directed-graph.ts +2 -1
  73. package/src/data-structures/hash/hash-map.ts +4 -4
  74. package/src/data-structures/heap/heap.ts +71 -152
  75. package/src/data-structures/heap/max-heap.ts +88 -13
  76. package/src/data-structures/heap/min-heap.ts +78 -15
  77. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  78. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  79. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  80. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  81. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  82. package/src/data-structures/queue/deque.ts +50 -25
  83. package/src/data-structures/queue/queue.ts +23 -37
  84. package/src/data-structures/stack/stack.ts +31 -26
  85. package/src/data-structures/trie/trie.ts +33 -18
  86. package/src/interfaces/binary-tree.ts +4 -5
  87. package/src/types/common.ts +2 -24
  88. package/src/types/data-structures/base/base.ts +14 -6
  89. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
  90. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
  91. package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
  92. package/src/types/data-structures/binary-tree/bst.ts +9 -3
  93. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
  94. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
  95. package/src/types/data-structures/heap/heap.ts +4 -1
  96. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  97. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  98. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  99. package/src/types/data-structures/queue/deque.ts +6 -1
  100. package/src/types/data-structures/queue/queue.ts +3 -1
  101. package/src/types/data-structures/stack/stack.ts +3 -1
  102. package/src/types/data-structures/trie/trie.ts +3 -1
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from '../../types';
8
+ import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
9
9
  import { IterableElementBase } from '../base';
10
10
 
11
11
  export class DoublyLinkedListNode<E = any> {
@@ -87,21 +87,17 @@ export class DoublyLinkedListNode<E = any> {
87
87
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
88
88
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
89
89
  */
90
- export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
91
- /**
92
- * The constructor initializes a linked list with optional elements.
93
- * @param elements - The `elements` parameter is an optional iterable object that contains the
94
- * initial elements to be added to the data structure. It defaults to an empty array if no elements
95
- * are provided.
96
- */
97
- constructor(elements: Iterable<E> = []) {
98
- super();
90
+ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
91
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: DoublyLinkedListOptions<E, R>) {
92
+ super(options);
99
93
  this._head = undefined;
100
94
  this._tail = undefined;
101
95
  this._size = 0;
102
96
  if (elements) {
103
97
  for (const el of elements) {
104
- this.push(el);
98
+ if (this.toElementFn) {
99
+ this.push(this.toElementFn(el as R));
100
+ } else this.push(el as E);
105
101
  }
106
102
  }
107
103
  }
@@ -729,8 +725,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
729
725
  * @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
730
726
  * is a copy of the original list.
731
727
  */
732
- clone(): DoublyLinkedList<E> {
733
- return new DoublyLinkedList(this.values());
728
+ clone(): DoublyLinkedList<E, R> {
729
+ return new DoublyLinkedList<E, R>(this);
734
730
  }
735
731
 
736
732
  /**
@@ -755,8 +751,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
755
751
  * @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
756
752
  * elements that pass the filter condition specified by the `callback` function.
757
753
  */
758
- filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E> {
759
- const filteredList = new DoublyLinkedList<E>();
754
+ filter(callback: ElementCallback<E, R, boolean, DoublyLinkedList<E, R>>, thisArg?: any): DoublyLinkedList<E, R> {
755
+ const filteredList = new DoublyLinkedList<E, R>([], { toElementFn: this.toElementFn });
760
756
  let index = 0;
761
757
  for (const current of this) {
762
758
  if (callback.call(thisArg, current, index, this)) {
@@ -773,24 +769,28 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
773
769
  */
774
770
 
775
771
  /**
776
- * Time Complexity: O(n)
777
- * Space Complexity: O(n)
778
- *
779
- * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
780
- * in the original list.
772
+ * The `map` function takes a callback function and returns a new DoublyLinkedList with the results
773
+ * of applying the callback to each element in the original list.
781
774
  * @param callback - The callback parameter is a function that will be called for each element in the
782
- * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
783
- * and the DoublyLinkedList itself. The callback function should return a value that will be added to
784
- * the new DoublyLinkedList that
785
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
786
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
787
- * passed as the `this` value to the `callback` function. If `thisArg` is
788
- * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
789
- * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
790
- * object.
791
- */
792
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T> {
793
- const mappedList = new DoublyLinkedList<T>();
775
+ * original DoublyLinkedList. It takes three arguments: current (the current element being
776
+ * processed), index (the index of the current element), and this (the original DoublyLinkedList).
777
+ * The callback function should return a value of type
778
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
779
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
780
+ * input and returns the converted element. If this parameter is not provided, the raw element will
781
+ * be used as is.
782
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
783
+ * specify the value of `this` within the callback function. It is used to set the context or scope
784
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
785
+ * value of
786
+ * @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
787
+ */
788
+ map<EM, RM>(
789
+ callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>,
790
+ toElementFn?: (rawElement: RM) => EM,
791
+ thisArg?: any
792
+ ): DoublyLinkedList<EM, RM> {
793
+ const mappedList = new DoublyLinkedList<EM, RM>([], { toElementFn });
794
794
  let index = 0;
795
795
  for (const current of this) {
796
796
  mappedList.push(callback.call(thisArg, current, index, this));
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from '../../types';
8
+ import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
9
9
  import { IterableElementBase } from '../base';
10
10
 
11
11
  export class SinglyLinkedListNode<E = any> {
@@ -59,17 +59,17 @@ export class SinglyLinkedListNode<E = any> {
59
59
  }
60
60
  }
61
61
 
62
- export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
63
- /**
64
- * The constructor initializes a new instance of a class with an optional iterable of elements.
65
- * @param elements - The `elements` parameter is an optional iterable object that contains the
66
- * initial elements to be added to the instance of the class. If no `elements` are provided, an empty
67
- * array will be used as the default value.
68
- */
69
- constructor(elements: Iterable<E> = []) {
70
- super();
62
+ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
63
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: SinglyLinkedListOptions<E, R>) {
64
+ super(options);
71
65
  if (elements) {
72
- for (const el of elements) this.push(el);
66
+ for (const el of elements) {
67
+ if (this.toElementFn) {
68
+ this.push(this.toElementFn(el as R));
69
+ } else {
70
+ this.push(el as E);
71
+ }
72
+ }
73
73
  }
74
74
  }
75
75
 
@@ -673,8 +673,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
673
673
  * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
674
674
  * is a clone of the original list.
675
675
  */
676
- clone(): SinglyLinkedList<E> {
677
- return new SinglyLinkedList<E>(this.values());
676
+ clone(): SinglyLinkedList<E, R> {
677
+ return new SinglyLinkedList<E, R>(this, { toElementFn: this.toElementFn });
678
678
  }
679
679
 
680
680
  /**
@@ -699,8 +699,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
699
699
  * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
700
700
  * elements that pass the filter condition specified by the `callback` function.
701
701
  */
702
- filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E> {
703
- const filteredList = new SinglyLinkedList<E>();
702
+ filter(callback: ElementCallback<E, R, boolean, SinglyLinkedList<E, R>>, thisArg?: any): SinglyLinkedList<E, R> {
703
+ const filteredList = new SinglyLinkedList<E, R>([], { toElementFn: this.toElementFn });
704
704
  let index = 0;
705
705
  for (const current of this) {
706
706
  if (callback.call(thisArg, current, index, this)) {
@@ -715,22 +715,30 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
715
715
  * Time Complexity: O(n)
716
716
  * Space Complexity: O(n)
717
717
  */
718
+
718
719
  /**
719
- * Time Complexity: O(n)
720
- * Space Complexity: O(n)
721
- *
722
- * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
723
- * of the original list.
720
+ * The `map` function takes a callback function and returns a new SinglyLinkedList with the results
721
+ * of applying the callback to each element in the original list.
724
722
  * @param callback - The `callback` parameter is a function that will be called for each element in
725
- * the linked list. It takes three arguments:
726
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
727
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
728
- * passed as the `this` value to the `callback` function. If `thisArg` is
729
- * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
730
- * of applying the provided `callback` function to each element in the original list.
731
- */
732
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T> {
733
- const mappedList = new SinglyLinkedList<T>();
723
+ * the original list. It takes three arguments: `current` (the current element being processed),
724
+ * `index` (the index of the current element), and `this` (the original list). It should return a
725
+ * value
726
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
727
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
728
+ * input and returns the converted element. If this parameter is not provided, the raw element will
729
+ * be used as is.
730
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
731
+ * specify the value of `this` within the callback function. It is used to set the context or scope
732
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
733
+ * value of
734
+ * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
735
+ */
736
+ map<EM, RM>(
737
+ callback: ElementCallback<E, R, EM, SinglyLinkedList<E, R>>,
738
+ toElementFn?: (rawElement: RM) => EM,
739
+ thisArg?: any
740
+ ): SinglyLinkedList<EM, RM> {
741
+ const mappedList = new SinglyLinkedList<EM, RM>([], { toElementFn });
734
742
  let index = 0;
735
743
  for (const current of this) {
736
744
  mappedList.push(callback.call(thisArg, current, index, this));
@@ -5,10 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { PriorityQueueOptions } from '../../types';
8
+ import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
10
 
11
- export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
11
+ export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
12
12
  /**
13
13
  * The constructor initializes a PriorityQueue with optional elements and options, including a
14
14
  * comparator function.
@@ -16,21 +16,102 @@ export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
16
16
  * elements to be added to the priority queue. It is optional and defaults to an empty array if not
17
17
  * provided.
18
18
  * @param options - The `options` parameter is an object that contains additional configuration
19
- * options for the priority queue. In this case, it has a property called `comparator` which is a
19
+ * options for the priority queue. In this case, it has a property called `comparator,` which is a
20
20
  * function used to compare elements in the priority queue.
21
21
  */
22
- constructor(
23
- elements: Iterable<E> = [],
24
- options: PriorityQueueOptions<E> = {
25
- comparator: (a: E, b: E) => {
26
- if (!(typeof a === 'number' && typeof b === 'number')) {
27
- throw new Error('The a, b params of compare function must be number');
28
- } else {
29
- return b - a;
22
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
23
+ super(elements, {
24
+ comparator: (a: E, b: E): number => {
25
+ if (typeof a === 'object' || typeof b === 'object') {
26
+ throw TypeError(
27
+ `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
28
+ );
30
29
  }
30
+ if (a < b) return 1;
31
+ if (a > b) return -1;
32
+ return 0;
33
+ },
34
+ ...options
35
+ });
36
+ }
37
+
38
+ /**
39
+ * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
40
+ * comparator and toElementFn as the current instance.
41
+ * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
42
+ * comparator and toElementFn as the current instance.
43
+ */
44
+ override clone(): MaxPriorityQueue<E, R> {
45
+ return new MaxPriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
46
+ }
47
+
48
+ /**
49
+ * Time Complexity: O(n)
50
+ * Space Complexity: O(n)
51
+ *
52
+ * The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
53
+ * function.
54
+ * @param callback - The `callback` parameter is a function that will be called for each element in
55
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
56
+ * heap itself. The callback function should return a boolean value indicating whether the current
57
+ * element should be included in the filtered list
58
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
59
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
60
+ * passed as the `this` value to the `callback` function. If `thisArg` is
61
+ * @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
62
+ * the filter condition specified by the `callback` function.
63
+ */
64
+ override filter(
65
+ callback: ElementCallback<E, R, boolean, MaxPriorityQueue<E, R>>,
66
+ thisArg?: any
67
+ ): MaxPriorityQueue<E, R> {
68
+ const filteredPriorityQueue = new MaxPriorityQueue<E, R>([], {
69
+ toElementFn: this.toElementFn,
70
+ comparator: this.comparator
71
+ });
72
+ let index = 0;
73
+ for (const current of this) {
74
+ if (callback.call(thisArg, current, index, this)) {
75
+ filteredPriorityQueue.add(current);
31
76
  }
77
+ index++;
78
+ }
79
+ return filteredPriorityQueue;
80
+ }
81
+
82
+ /**
83
+ * Time Complexity: O(n log n)
84
+ * Space Complexity: O(n)
85
+ *
86
+ * The `map` function creates a new heap by applying a callback function to each element of the
87
+ * original heap.
88
+ * @param callback - The `callback` parameter is a function that will be called for each element in
89
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
90
+ * element), and `this` (the heap itself). The callback function should return a value of
91
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
92
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
93
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
94
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
95
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
96
+ * returns a value of type `T`. This function is used to transform the elements of the original
97
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
98
+ * specify the value of `this` within the callback function. It is used to set the context or scope
99
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
100
+ * value of
101
+ * @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
102
+ */
103
+ override map<EM, RM>(
104
+ callback: ElementCallback<E, R, EM, MaxPriorityQueue<E, R>>,
105
+ comparator: Comparator<EM>,
106
+ toElementFn?: (rawElement: RM) => EM,
107
+ thisArg?: any
108
+ ): MaxPriorityQueue<EM, RM> {
109
+ const mappedPriorityQueue = new MaxPriorityQueue<EM, RM>([], { comparator, toElementFn });
110
+ let index = 0;
111
+ for (const el of this) {
112
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
113
+ index++;
32
114
  }
33
- ) {
34
- super(elements, options);
115
+ return mappedPriorityQueue;
35
116
  }
36
117
  }
@@ -5,10 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { PriorityQueueOptions } from '../../types';
8
+ import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
10
 
11
- export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
11
+ export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
12
12
  /**
13
13
  * The constructor initializes a PriorityQueue with optional elements and options, including a
14
14
  * comparator function.
@@ -16,22 +16,91 @@ export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
16
16
  * elements to be added to the priority queue. It is optional and defaults to an empty array if not
17
17
  * provided.
18
18
  * @param options - The `options` parameter is an object that contains additional configuration
19
- * options for the priority queue. In this case, it has a property called `comparator` which is a
19
+ * options for the priority queue. In this case, it has a property called `comparator,` which is a
20
20
  * function used to compare elements in the priority queue. The `comparator` function takes two
21
- * parameters `a` and `b`,
21
+ * parameters `a` and `b`
22
22
  */
23
- constructor(
24
- elements: Iterable<E> = [],
25
- options: PriorityQueueOptions<E> = {
26
- comparator: (a: E, b: E) => {
27
- if (!(typeof a === 'number' && typeof b === 'number')) {
28
- throw new Error('The a, b params of compare function must be number');
29
- } else {
30
- return a - b;
31
- }
23
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
24
+ super(elements, options);
25
+ }
26
+
27
+ /**
28
+ * The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
29
+ * comparator and toElementFn as the original instance.
30
+ * @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
31
+ * properties as the current instance.
32
+ */
33
+ override clone(): MinPriorityQueue<E, R> {
34
+ return new MinPriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
35
+ }
36
+
37
+ /**
38
+ * Time Complexity: O(n)
39
+ * Space Complexity: O(n)
40
+ *
41
+ * The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
42
+ * function.
43
+ * @param callback - The `callback` parameter is a function that will be called for each element in
44
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
45
+ * heap itself. The callback function should return a boolean value indicating whether the current
46
+ * element should be included in the filtered list
47
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
48
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
49
+ * passed as the `this` value to the `callback` function. If `thisArg` is
50
+ * @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
51
+ * the filter condition specified by the `callback` function.
52
+ */
53
+ override filter(
54
+ callback: ElementCallback<E, R, boolean, MinPriorityQueue<E, R>>,
55
+ thisArg?: any
56
+ ): MinPriorityQueue<E, R> {
57
+ const filteredPriorityQueue = new MinPriorityQueue<E, R>([], {
58
+ toElementFn: this.toElementFn,
59
+ comparator: this.comparator
60
+ });
61
+ let index = 0;
62
+ for (const current of this) {
63
+ if (callback.call(thisArg, current, index, this)) {
64
+ filteredPriorityQueue.add(current);
32
65
  }
66
+ index++;
33
67
  }
34
- ) {
35
- super(elements, options);
68
+ return filteredPriorityQueue;
69
+ }
70
+
71
+ /**
72
+ * Time Complexity: O(n log n)
73
+ * Space Complexity: O(n)
74
+ *
75
+ * The `map` function creates a new heap by applying a callback function to each element of the
76
+ * original heap.
77
+ * @param callback - The `callback` parameter is a function that will be called for each element in
78
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
79
+ * element), and `this` (the heap itself). The callback function should return a value of
80
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
81
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
82
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
83
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
84
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
85
+ * returns a value of type `T`. This function is used to transform the elements of the original
86
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
87
+ * specify the value of `this` within the callback function. It is used to set the context or scope
88
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
89
+ * value of
90
+ * @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
91
+ */
92
+ override map<EM, RM>(
93
+ callback: ElementCallback<E, R, EM, MinPriorityQueue<E, R>>,
94
+ comparator: Comparator<EM>,
95
+ toElementFn?: (rawElement: RM) => EM,
96
+ thisArg?: any
97
+ ): MinPriorityQueue<EM, RM> {
98
+ const mappedPriorityQueue: MinPriorityQueue<EM, RM> = new MinPriorityQueue<EM, RM>([], { comparator, toElementFn });
99
+ let index = 0;
100
+ for (const el of this) {
101
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
102
+ index++;
103
+ }
104
+ return mappedPriorityQueue;
36
105
  }
37
106
  }
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { PriorityQueueOptions } from '../../types';
8
+ import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
9
9
  import { Heap } from '../heap';
10
10
 
11
11
  /**
@@ -16,16 +16,93 @@ import { Heap } from '../heap';
16
16
  * 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
17
17
  * 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
18
18
  */
19
- export class PriorityQueue<E = any> extends Heap<E> {
19
+ export class PriorityQueue<E = any, R = any> extends Heap<E, R> {
20
20
  /**
21
21
  * The constructor initializes a priority queue with optional elements and options.
22
22
  * @param elements - The `elements` parameter is an iterable object that contains the initial
23
- * elements to be added to the priority queue. It is an optional parameter and if not provided, the
23
+ * elements to be added to the priority queue. It is an optional parameter, and if not provided, the
24
24
  * priority queue will be initialized as empty.
25
25
  * @param [options] - The `options` parameter is an optional object that can be used to customize the
26
26
  * behavior of the priority queue. It can contain the following properties:
27
27
  */
28
- constructor(elements: Iterable<E> = [], options?: PriorityQueueOptions<E>) {
28
+ constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
29
29
  super(elements, options);
30
30
  }
31
+
32
+ /**
33
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
34
+ * and toElementFn as the original instance.
35
+ * @returns The method is returning a new instance of the `PriorityQueue` class with the same
36
+ * elements and properties as the current instance.
37
+ */
38
+ override clone(): PriorityQueue<E, R> {
39
+ return new PriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
40
+ }
41
+
42
+ /**
43
+ * Time Complexity: O(n)
44
+ * Space Complexity: O(n)
45
+ *
46
+ * The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
47
+ * function.
48
+ * @param callback - The `callback` parameter is a function that will be called for each element in
49
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
50
+ * heap itself. The callback function should return a boolean value indicating whether the current
51
+ * element should be included in the filtered list
52
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
53
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
54
+ * passed as the `this` value to the `callback` function. If `thisArg` is
55
+ * @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
56
+ * the filter condition specified by the `callback` function.
57
+ */
58
+ override filter(callback: ElementCallback<E, R, boolean, PriorityQueue<E, R>>, thisArg?: any): PriorityQueue<E, R> {
59
+ const filteredPriorityQueue = new PriorityQueue<E, R>([], {
60
+ toElementFn: this.toElementFn,
61
+ comparator: this.comparator
62
+ });
63
+ let index = 0;
64
+ for (const current of this) {
65
+ if (callback.call(thisArg, current, index, this)) {
66
+ filteredPriorityQueue.add(current);
67
+ }
68
+ index++;
69
+ }
70
+ return filteredPriorityQueue;
71
+ }
72
+
73
+ /**
74
+ * Time Complexity: O(n log n)
75
+ * Space Complexity: O(n)
76
+ *
77
+ * The `map` function creates a new heap by applying a callback function to each element of the
78
+ * original heap.
79
+ * @param callback - The `callback` parameter is a function that will be called for each element in
80
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
81
+ * element), and `this` (the heap itself). The callback function should return a value of
82
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
83
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
84
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
85
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
86
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
87
+ * returns a value of type `T`. This function is used to transform the elements of the original
88
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
89
+ * specify the value of `this` within the callback function. It is used to set the context or scope
90
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
91
+ * value of
92
+ * @returns a new instance of the `PriorityQueue` class with the mapped elements.
93
+ */
94
+ override map<EM, RM>(
95
+ callback: ElementCallback<E, R, EM, PriorityQueue<E, R>>,
96
+ comparator: Comparator<EM>,
97
+ toElementFn?: (rawElement: RM) => EM,
98
+ thisArg?: any
99
+ ): PriorityQueue<EM, RM> {
100
+ const mappedPriorityQueue: PriorityQueue<EM, RM> = new PriorityQueue<EM, RM>([], { comparator, toElementFn });
101
+ let index = 0;
102
+ for (const el of this) {
103
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
104
+ index++;
105
+ }
106
+ return mappedPriorityQueue;
107
+ }
31
108
  }