stack-typed 2.0.5 → 2.1.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 (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  72. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  74. package/src/data-structures/binary-tree/bst.ts +565 -572
  75. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  76. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -1,10 +1,3 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
1
  import type { SkipLinkedListOptions } from '../../types';
9
2
 
10
3
  export class SkipListNode<K, V> {
@@ -19,18 +12,7 @@ export class SkipListNode<K, V> {
19
12
  }
20
13
  }
21
14
 
22
- /**
23
- *
24
- */
25
15
  export class SkipList<K, V> {
26
- /**
27
- * The constructor function initializes a SkipLinkedList object with optional options and elements.
28
- * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
29
- * is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are
30
- * provided, the SkipLinkedList will be empty.
31
- * @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can
32
- * contain two properties:
33
- */
34
16
  constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) {
35
17
  if (options) {
36
18
  const { maxLevel, probability } = options;
@@ -43,65 +25,35 @@ export class SkipList<K, V> {
43
25
  }
44
26
  }
45
27
 
46
- protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as any, undefined as any, this.maxLevel);
28
+ protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as K, undefined as V, this.maxLevel);
47
29
 
48
- /**
49
- * The function returns the head node of a SkipList.
50
- * @returns The method is returning a SkipListNode object with generic key type K and value type V.
51
- */
52
30
  get head(): SkipListNode<K, V> {
53
31
  return this._head;
54
32
  }
55
33
 
56
34
  protected _level: number = 0;
57
35
 
58
- /**
59
- * The function returns the value of the protected variable _level.
60
- * @returns The level property of the object.
61
- */
62
36
  get level(): number {
63
37
  return this._level;
64
38
  }
65
39
 
66
40
  protected _maxLevel: number = 16;
67
41
 
68
- /**
69
- * The function returns the maximum level.
70
- * @returns The value of the variable `_maxLevel` is being returned.
71
- */
72
42
  get maxLevel(): number {
73
43
  return this._maxLevel;
74
44
  }
75
45
 
76
46
  protected _probability: number = 0.5;
77
47
 
78
- /**
79
- * The function returns the probability value.
80
- * @returns The probability value stored in the protected variable `_probability` is being returned.
81
- */
82
48
  get probability(): number {
83
49
  return this._probability;
84
50
  }
85
51
 
86
- /**
87
- * Time Complexity: O(1)
88
- * Space Complexity: O(1)
89
- *
90
- * Get the value of the first element (the smallest element) in the Skip List.
91
- * @returns The value of the first element, or undefined if the Skip List is empty.
92
- */
93
52
  get first(): V | undefined {
94
53
  const firstNode = this.head.forward[0];
95
54
  return firstNode ? firstNode.value : undefined;
96
55
  }
97
56
 
98
- /**
99
- * Time Complexity: O(log n)
100
- * Space Complexity: O(1)
101
- *
102
- * Get the value of the last element (the largest element) in the Skip List.
103
- * @returns The value of the last element, or undefined if the Skip List is empty.
104
- */
105
57
  get last(): V | undefined {
106
58
  let current = this.head;
107
59
  for (let i = this.level - 1; i >= 0; i--) {
@@ -112,15 +64,6 @@ export class SkipList<K, V> {
112
64
  return current.value;
113
65
  }
114
66
 
115
- /**
116
- * Time Complexity: O(log n)
117
- * Space Complexity: O(1)
118
- *
119
- * The add function adds a new node with a given key and value to a Skip List data structure.
120
- * @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
121
- * @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
122
- * List.
123
- */
124
67
  add(key: K, value: V): void {
125
68
  const newNode = new SkipListNode(key, value, this._randomLevel());
126
69
  const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
@@ -143,15 +86,6 @@ export class SkipList<K, V> {
143
86
  }
144
87
  }
145
88
 
146
- /**
147
- * Time Complexity: O(log n)
148
- * Space Complexity: O(1)
149
- *
150
- * The function `get` retrieves the value associated with a given key from a skip list data structure.
151
- * @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
152
- * @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
153
- * otherwise it returns `undefined`.
154
- */
155
89
  get(key: K): V | undefined {
156
90
  let current = this.head;
157
91
  for (let i = this.level - 1; i >= 0; i--) {
@@ -169,28 +103,10 @@ export class SkipList<K, V> {
169
103
  return undefined;
170
104
  }
171
105
 
172
- /**
173
- * Time Complexity: O(log n)
174
- * Space Complexity: O(1)
175
- *
176
- * The function checks if a key exists in a data structure.
177
- * @param {K} key - The parameter "key" is of type K, which represents the type of the key being
178
- * checked.
179
- * @returns a boolean value.
180
- */
181
106
  has(key: K): boolean {
182
107
  return this.get(key) !== undefined;
183
108
  }
184
109
 
185
- /**
186
- * Time Complexity: O(log n)
187
- * Space Complexity: O(1)
188
- *
189
- * The `delete` function removes a node with a specific key from a Skip List data structure.
190
- * @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
191
- * @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
192
- * skip list, and `false` if the key was not found in the skip list.
193
- */
194
110
  delete(key: K): boolean {
195
111
  const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
196
112
  let current = this.head;
@@ -220,14 +136,6 @@ export class SkipList<K, V> {
220
136
  return false;
221
137
  }
222
138
 
223
- /**
224
- * Time Complexity: O(log n)
225
- * Space Complexity: O(1)
226
- *
227
- * Get the value of the first element in the Skip List that is greater than the given key.
228
- * @param key - the given key.
229
- * @returns The value of the first element greater than the given key, or undefined if there is no such element.
230
- */
231
139
  higher(key: K): V | undefined {
232
140
  let current = this.head;
233
141
  for (let i = this.level - 1; i >= 0; i--) {
@@ -239,14 +147,6 @@ export class SkipList<K, V> {
239
147
  return nextNode ? nextNode.value : undefined;
240
148
  }
241
149
 
242
- /**
243
- * Time Complexity: O(log n)
244
- * Space Complexity: O(1)
245
- *
246
- * Get the value of the last element in the Skip List that is less than the given key.
247
- * @param key - the given key.
248
- * @returns The value of the last element less than the given key, or undefined if there is no such element.
249
- */
250
150
  lower(key: K): V | undefined {
251
151
  let current = this.head;
252
152
  let lastLess = undefined;
@@ -263,13 +163,6 @@ export class SkipList<K, V> {
263
163
  return lastLess ? lastLess.value : undefined;
264
164
  }
265
165
 
266
- /**
267
- * Time Complexity: O(maxLevel)
268
- * Space Complexity: O(1)
269
- *
270
- * The function "_randomLevel" generates a random level based on a given probability and maximum level.
271
- * @returns the level, which is a number.
272
- */
273
166
  protected _randomLevel(): number {
274
167
  let level = 1;
275
168
  while (Math.random() < this.probability && level < this.maxLevel) {
@@ -5,22 +5,24 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
8
+ import type { PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
10
 
11
11
  /**
12
- *
12
+ * Max-oriented priority queue (max-heap) built on {@link PriorityQueue}.
13
+ * The default comparator orders primitive values in descending order. If you store objects,
14
+ * you must provide a custom comparator via {@link PriorityQueueOptions}.
15
+ * @template E Element type stored in the queue.
16
+ * @template R Extra record/metadata associated with each element.
17
+ * @example
13
18
  */
14
19
  export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
15
20
  /**
16
- * The constructor initializes a PriorityQueue with optional elements and options, including a
17
- * comparator function.
18
- * @param elements - The `elements` parameter is an iterable object that contains the initial
19
- * elements to be added to the priority queue. It is optional and defaults to an empty array if not
20
- * provided.
21
- * @param options - The `options` parameter is an object that contains additional configuration
22
- * options for the priority queue. In this case, it has a property called `comparator,` which is a
23
- * function used to compare elements in the priority queue.
21
+ * Creates a max-priority queue.
22
+ * @param elements Optional initial elements to insert.
23
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
24
+ * @throws {TypeError} Thrown when using the default comparator with object elements (provide a custom comparator).
25
+ * @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
24
26
  */
25
27
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
26
28
  super(elements, {
@@ -37,81 +39,4 @@ export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
37
39
  ...options
38
40
  });
39
41
  }
40
-
41
- /**
42
- * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
43
- * comparator and toElementFn as the current instance.
44
- * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
45
- * comparator and toElementFn as the current instance.
46
- */
47
- override clone(): MaxPriorityQueue<E, R> {
48
- return new MaxPriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
49
- }
50
-
51
- /**
52
- * Time Complexity: O(n)
53
- * Space Complexity: O(n)
54
- *
55
- * The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
56
- * function.
57
- * @param callback - The `callback` parameter is a function that will be called for each element in
58
- * the heap. It takes three arguments: the current element, the index of the current element, and the
59
- * heap itself. The callback function should return a boolean value indicating whether the current
60
- * element should be included in the filtered list
61
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
62
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
63
- * passed as the `this` value to the `callback` function. If `thisArg` is
64
- * @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
65
- * the filter condition specified by the `callback` function.
66
- */
67
- override filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): 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);
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>,
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++;
114
- }
115
- return mappedPriorityQueue;
116
- }
117
42
  }
@@ -5,102 +5,25 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
8
+ import type { PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
10
 
11
11
  /**
12
- *
12
+ * Min-oriented priority queue (min-heap) built on {@link PriorityQueue}.
13
+ * The queue removes the smallest element first under the provided comparator.
14
+ * Provide a custom comparator if you store non-primitive objects.
15
+ * @template E Element type stored in the queue.
16
+ * @template R Extra record/metadata associated with each element.
17
+ * @example
13
18
  */
14
19
  export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
15
20
  /**
16
- * The constructor initializes a PriorityQueue with optional elements and options, including a
17
- * comparator function.
18
- * @param elements - The `elements` parameter is an iterable object that contains the initial
19
- * elements to be added to the priority queue. It is optional and defaults to an empty array if not
20
- * provided.
21
- * @param options - The `options` parameter is an object that contains additional configuration
22
- * options for the priority queue. In this case, it has a property called `comparator,` which is a
23
- * function used to compare elements in the priority queue. The `comparator` function takes two
24
- * parameters `a` and `b`
21
+ * Creates a min-priority queue.
22
+ * @param elements Optional initial elements to insert.
23
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
24
+ * @remarks Complexity Time: O(n log n) when inserting n elements incrementally; Space: O(n).
25
25
  */
26
26
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
27
27
  super(elements, options);
28
28
  }
29
-
30
- /**
31
- * The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
32
- * comparator and toElementFn as the original instance.
33
- * @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
34
- * properties as the current instance.
35
- */
36
- override clone(): MinPriorityQueue<E, R> {
37
- return new MinPriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
38
- }
39
-
40
- /**
41
- * Time Complexity: O(n)
42
- * Space Complexity: O(n)
43
- *
44
- * The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
45
- * function.
46
- * @param callback - The `callback` parameter is a function that will be called for each element in
47
- * the heap. It takes three arguments: the current element, the index of the current element, and the
48
- * heap itself. The callback function should return a boolean value indicating whether the current
49
- * element should be included in the filtered list
50
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
51
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
52
- * passed as the `this` value to the `callback` function. If `thisArg` is
53
- * @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
54
- * the filter condition specified by the `callback` function.
55
- */
56
- override filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): 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);
65
- }
66
- index++;
67
- }
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>,
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;
105
- }
106
29
  }
@@ -5,104 +5,15 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
8
+
9
+ import type { PriorityQueueOptions } from '../../types';
9
10
  import { Heap } from '../heap';
10
11
 
11
12
  /**
12
- * 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
13
- * 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
14
- * 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
15
- * 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
16
- * 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
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
13
+ * @example
18
14
  */
19
15
  export class PriorityQueue<E = any, R = any> extends Heap<E, R> {
20
- /**
21
- * The constructor initializes a priority queue with optional elements and options.
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
24
- * priority queue will be initialized as empty.
25
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
26
- * behavior of the priority queue. It can contain the following properties:
27
- */
28
16
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
29
17
  super(elements, options);
30
18
  }
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>, 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>,
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
- }
108
19
  }