linked-list-typed 2.0.4 → 2.1.0

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/README.md +14 -14
  2. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  3. package/dist/data-structures/base/iterable-element-base.js +149 -107
  4. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  5. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  6. package/dist/data-structures/base/linear-base.d.ts +250 -192
  7. package/dist/data-structures/base/linear-base.js +137 -274
  8. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  9. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  13. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  14. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  15. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  16. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  17. package/dist/data-structures/binary-tree/bst.js +505 -481
  18. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  19. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  20. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  21. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  22. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  23. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  24. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  25. package/dist/data-structures/graph/abstract-graph.js +267 -237
  26. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  27. package/dist/data-structures/graph/directed-graph.js +146 -233
  28. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  29. package/dist/data-structures/graph/map-graph.js +56 -59
  30. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  31. package/dist/data-structures/graph/undirected-graph.js +129 -149
  32. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  33. package/dist/data-structures/hash/hash-map.js +270 -457
  34. package/dist/data-structures/heap/heap.d.ts +214 -289
  35. package/dist/data-structures/heap/heap.js +340 -349
  36. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  37. package/dist/data-structures/heap/max-heap.js +11 -66
  38. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  39. package/dist/data-structures/heap/min-heap.js +11 -66
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  43. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  44. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  45. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  51. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  52. package/dist/data-structures/queue/deque.d.ts +227 -254
  53. package/dist/data-structures/queue/deque.js +309 -348
  54. package/dist/data-structures/queue/queue.d.ts +180 -201
  55. package/dist/data-structures/queue/queue.js +265 -248
  56. package/dist/data-structures/stack/stack.d.ts +124 -102
  57. package/dist/data-structures/stack/stack.js +181 -125
  58. package/dist/data-structures/trie/trie.d.ts +164 -165
  59. package/dist/data-structures/trie/trie.js +189 -172
  60. package/dist/interfaces/binary-tree.d.ts +56 -6
  61. package/dist/interfaces/graph.d.ts +16 -0
  62. package/dist/types/data-structures/base/base.d.ts +1 -1
  63. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  64. package/dist/types/utils/utils.d.ts +6 -6
  65. package/dist/utils/utils.d.ts +110 -49
  66. package/dist/utils/utils.js +148 -73
  67. package/package.json +2 -2
  68. package/src/data-structures/base/iterable-element-base.ts +238 -115
  69. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  70. package/src/data-structures/base/linear-base.ts +271 -277
  71. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  72. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  73. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  74. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  75. package/src/data-structures/binary-tree/bst.ts +568 -570
  76. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  77. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  78. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  79. package/src/data-structures/graph/abstract-graph.ts +339 -264
  80. package/src/data-structures/graph/directed-graph.ts +146 -236
  81. package/src/data-structures/graph/map-graph.ts +63 -60
  82. package/src/data-structures/graph/undirected-graph.ts +129 -152
  83. package/src/data-structures/hash/hash-map.ts +274 -496
  84. package/src/data-structures/heap/heap.ts +389 -402
  85. package/src/data-structures/heap/max-heap.ts +12 -76
  86. package/src/data-structures/heap/min-heap.ts +13 -76
  87. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  88. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  89. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  90. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  91. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  92. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  93. package/src/data-structures/queue/deque.ts +381 -357
  94. package/src/data-structures/queue/queue.ts +310 -264
  95. package/src/data-structures/stack/stack.ts +217 -131
  96. package/src/data-structures/trie/trie.ts +240 -175
  97. package/src/interfaces/binary-tree.ts +240 -6
  98. package/src/interfaces/graph.ts +37 -0
  99. package/src/types/data-structures/base/base.ts +5 -5
  100. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  101. package/src/types/utils/utils.ts +9 -5
  102. package/src/utils/utils.ts +152 -86
@@ -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
  export declare class SkipListNode<K, V> {
10
3
  key: K;
@@ -12,123 +5,23 @@ export declare class SkipListNode<K, V> {
12
5
  forward: SkipListNode<K, V>[];
13
6
  constructor(key: K, value: V, level: number);
14
7
  }
15
- /**
16
- *
17
- */
18
8
  export declare class SkipList<K, V> {
19
- /**
20
- * The constructor function initializes a SkipLinkedList object with optional options and elements.
21
- * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
22
- * is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are
23
- * provided, the SkipLinkedList will be empty.
24
- * @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can
25
- * contain two properties:
26
- */
27
9
  constructor(elements?: Iterable<[K, V]>, options?: SkipLinkedListOptions);
28
10
  protected _head: SkipListNode<K, V>;
29
- /**
30
- * The function returns the head node of a SkipList.
31
- * @returns The method is returning a SkipListNode object with generic key type K and value type V.
32
- */
33
11
  get head(): SkipListNode<K, V>;
34
12
  protected _level: number;
35
- /**
36
- * The function returns the value of the protected variable _level.
37
- * @returns The level property of the object.
38
- */
39
13
  get level(): number;
40
14
  protected _maxLevel: number;
41
- /**
42
- * The function returns the maximum level.
43
- * @returns The value of the variable `_maxLevel` is being returned.
44
- */
45
15
  get maxLevel(): number;
46
16
  protected _probability: number;
47
- /**
48
- * The function returns the probability value.
49
- * @returns The probability value stored in the protected variable `_probability` is being returned.
50
- */
51
17
  get probability(): number;
52
- /**
53
- * Time Complexity: O(1)
54
- * Space Complexity: O(1)
55
- *
56
- * Get the value of the first element (the smallest element) in the Skip List.
57
- * @returns The value of the first element, or undefined if the Skip List is empty.
58
- */
59
18
  get first(): V | undefined;
60
- /**
61
- * Time Complexity: O(log n)
62
- * Space Complexity: O(1)
63
- *
64
- * Get the value of the last element (the largest element) in the Skip List.
65
- * @returns The value of the last element, or undefined if the Skip List is empty.
66
- */
67
19
  get last(): V | undefined;
68
- /**
69
- * Time Complexity: O(log n)
70
- * Space Complexity: O(1)
71
- *
72
- * The add function adds a new node with a given key and value to a Skip List data structure.
73
- * @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
74
- * @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
75
- * List.
76
- */
77
20
  add(key: K, value: V): void;
78
- /**
79
- * Time Complexity: O(log n)
80
- * Space Complexity: O(1)
81
- *
82
- * The function `get` retrieves the value associated with a given key from a skip list data structure.
83
- * @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
84
- * @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
85
- * otherwise it returns `undefined`.
86
- */
87
21
  get(key: K): V | undefined;
88
- /**
89
- * Time Complexity: O(log n)
90
- * Space Complexity: O(1)
91
- *
92
- * The function checks if a key exists in a data structure.
93
- * @param {K} key - The parameter "key" is of type K, which represents the type of the key being
94
- * checked.
95
- * @returns a boolean value.
96
- */
97
22
  has(key: K): boolean;
98
- /**
99
- * Time Complexity: O(log n)
100
- * Space Complexity: O(1)
101
- *
102
- * The `delete` function removes a node with a specific key from a Skip List data structure.
103
- * @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
104
- * @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
105
- * skip list, and `false` if the key was not found in the skip list.
106
- */
107
23
  delete(key: K): boolean;
108
- /**
109
- * Time Complexity: O(log n)
110
- * Space Complexity: O(1)
111
- *
112
- * Get the value of the first element in the Skip List that is greater than the given key.
113
- * @param key - the given key.
114
- * @returns The value of the first element greater than the given key, or undefined if there is no such element.
115
- */
116
24
  higher(key: K): V | undefined;
117
- /**
118
- * Time Complexity: O(log n)
119
- * Space Complexity: O(1)
120
- *
121
- * Get the value of the last element in the Skip List that is less than the given key.
122
- * @param key - the given key.
123
- * @returns The value of the last element less than the given key, or undefined if there is no such element.
124
- */
125
25
  lower(key: K): V | undefined;
126
- /**
127
- * Time Complexity: O(maxLevel)
128
- * Space Complexity: O(1)
129
- *
130
- * The function "_randomLevel" generates a random level based on a given probability and maximum level.
131
- * @returns the level, which is a number.
132
- */
133
26
  protected _randomLevel(): number;
134
27
  }
@@ -9,18 +9,7 @@ class SkipListNode {
9
9
  }
10
10
  }
11
11
  exports.SkipListNode = SkipListNode;
12
- /**
13
- *
14
- */
15
12
  class SkipList {
16
- /**
17
- * The constructor function initializes a SkipLinkedList object with optional options and elements.
18
- * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
19
- * is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are
20
- * provided, the SkipLinkedList will be empty.
21
- * @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can
22
- * contain two properties:
23
- */
24
13
  constructor(elements = [], options) {
25
14
  this._head = new SkipListNode(undefined, undefined, this.maxLevel);
26
15
  this._level = 0;
@@ -38,52 +27,22 @@ class SkipList {
38
27
  this.add(key, value);
39
28
  }
40
29
  }
41
- /**
42
- * The function returns the head node of a SkipList.
43
- * @returns The method is returning a SkipListNode object with generic key type K and value type V.
44
- */
45
30
  get head() {
46
31
  return this._head;
47
32
  }
48
- /**
49
- * The function returns the value of the protected variable _level.
50
- * @returns The level property of the object.
51
- */
52
33
  get level() {
53
34
  return this._level;
54
35
  }
55
- /**
56
- * The function returns the maximum level.
57
- * @returns The value of the variable `_maxLevel` is being returned.
58
- */
59
36
  get maxLevel() {
60
37
  return this._maxLevel;
61
38
  }
62
- /**
63
- * The function returns the probability value.
64
- * @returns The probability value stored in the protected variable `_probability` is being returned.
65
- */
66
39
  get probability() {
67
40
  return this._probability;
68
41
  }
69
- /**
70
- * Time Complexity: O(1)
71
- * Space Complexity: O(1)
72
- *
73
- * Get the value of the first element (the smallest element) in the Skip List.
74
- * @returns The value of the first element, or undefined if the Skip List is empty.
75
- */
76
42
  get first() {
77
43
  const firstNode = this.head.forward[0];
78
44
  return firstNode ? firstNode.value : undefined;
79
45
  }
80
- /**
81
- * Time Complexity: O(log n)
82
- * Space Complexity: O(1)
83
- *
84
- * Get the value of the last element (the largest element) in the Skip List.
85
- * @returns The value of the last element, or undefined if the Skip List is empty.
86
- */
87
46
  get last() {
88
47
  let current = this.head;
89
48
  for (let i = this.level - 1; i >= 0; i--) {
@@ -93,15 +52,6 @@ class SkipList {
93
52
  }
94
53
  return current.value;
95
54
  }
96
- /**
97
- * Time Complexity: O(log n)
98
- * Space Complexity: O(1)
99
- *
100
- * The add function adds a new node with a given key and value to a Skip List data structure.
101
- * @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
102
- * @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
103
- * List.
104
- */
105
55
  add(key, value) {
106
56
  const newNode = new SkipListNode(key, value, this._randomLevel());
107
57
  const update = new Array(this.maxLevel).fill(this.head);
@@ -120,15 +70,6 @@ class SkipList {
120
70
  this._level = Math.max(this.level, newNode.forward.length);
121
71
  }
122
72
  }
123
- /**
124
- * Time Complexity: O(log n)
125
- * Space Complexity: O(1)
126
- *
127
- * The function `get` retrieves the value associated with a given key from a skip list data structure.
128
- * @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
129
- * @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
130
- * otherwise it returns `undefined`.
131
- */
132
73
  get(key) {
133
74
  let current = this.head;
134
75
  for (let i = this.level - 1; i >= 0; i--) {
@@ -142,27 +83,9 @@ class SkipList {
142
83
  }
143
84
  return undefined;
144
85
  }
145
- /**
146
- * Time Complexity: O(log n)
147
- * Space Complexity: O(1)
148
- *
149
- * The function checks if a key exists in a data structure.
150
- * @param {K} key - The parameter "key" is of type K, which represents the type of the key being
151
- * checked.
152
- * @returns a boolean value.
153
- */
154
86
  has(key) {
155
87
  return this.get(key) !== undefined;
156
88
  }
157
- /**
158
- * Time Complexity: O(log n)
159
- * Space Complexity: O(1)
160
- *
161
- * The `delete` function removes a node with a specific key from a Skip List data structure.
162
- * @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
163
- * @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
164
- * skip list, and `false` if the key was not found in the skip list.
165
- */
166
89
  delete(key) {
167
90
  const update = new Array(this.maxLevel).fill(this.head);
168
91
  let current = this.head;
@@ -187,14 +110,6 @@ class SkipList {
187
110
  }
188
111
  return false;
189
112
  }
190
- /**
191
- * Time Complexity: O(log n)
192
- * Space Complexity: O(1)
193
- *
194
- * Get the value of the first element in the Skip List that is greater than the given key.
195
- * @param key - the given key.
196
- * @returns The value of the first element greater than the given key, or undefined if there is no such element.
197
- */
198
113
  higher(key) {
199
114
  let current = this.head;
200
115
  for (let i = this.level - 1; i >= 0; i--) {
@@ -205,14 +120,6 @@ class SkipList {
205
120
  const nextNode = current.forward[0];
206
121
  return nextNode ? nextNode.value : undefined;
207
122
  }
208
- /**
209
- * Time Complexity: O(log n)
210
- * Space Complexity: O(1)
211
- *
212
- * Get the value of the last element in the Skip List that is less than the given key.
213
- * @param key - the given key.
214
- * @returns The value of the last element less than the given key, or undefined if there is no such element.
215
- */
216
123
  lower(key) {
217
124
  let current = this.head;
218
125
  let lastLess = undefined;
@@ -226,13 +133,6 @@ class SkipList {
226
133
  }
227
134
  return lastLess ? lastLess.value : undefined;
228
135
  }
229
- /**
230
- * Time Complexity: O(maxLevel)
231
- * Space Complexity: O(1)
232
- *
233
- * The function "_randomLevel" generates a random level based on a given probability and maximum level.
234
- * @returns the level, which is a number.
235
- */
236
136
  _randomLevel() {
237
137
  let level = 1;
238
138
  while (Math.random() < this.probability && level < this.maxLevel) {
@@ -5,67 +5,23 @@
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
+ * Max-oriented priority queue (max-heap) built on {@link PriorityQueue}.
12
+ * The default comparator orders primitive values in descending order. If you store objects,
13
+ * you must provide a custom comparator via {@link PriorityQueueOptions}.
14
+ * @template E Element type stored in the queue.
15
+ * @template R Extra record/metadata associated with each element.
16
+ * @example
12
17
  */
13
18
  export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
14
19
  /**
15
- * The constructor initializes a PriorityQueue with optional elements and options, including a
16
- * comparator function.
17
- * @param elements - The `elements` parameter is an iterable object that contains the initial
18
- * elements to be added to the priority queue. It is optional and defaults to an empty array if not
19
- * provided.
20
- * @param options - The `options` parameter is an object that contains additional configuration
21
- * options for the priority queue. In this case, it has a property called `comparator,` which is a
22
- * function used to compare elements in the priority queue.
20
+ * Creates a max-priority queue.
21
+ * @param elements Optional initial elements to insert.
22
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
23
+ * @throws {TypeError} Thrown when using the default comparator with object elements (provide a custom comparator).
24
+ * @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
23
25
  */
24
26
  constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
25
- /**
26
- * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
27
- * comparator and toElementFn as the current instance.
28
- * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
29
- * comparator and toElementFn as the current instance.
30
- */
31
- clone(): MaxPriorityQueue<E, R>;
32
- /**
33
- * Time Complexity: O(n)
34
- * Space Complexity: O(n)
35
- *
36
- * The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
37
- * function.
38
- * @param callback - The `callback` parameter is a function that will be called for each element in
39
- * the heap. It takes three arguments: the current element, the index of the current element, and the
40
- * heap itself. The callback function should return a boolean value indicating whether the current
41
- * element should be included in the filtered list
42
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
43
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
44
- * passed as the `this` value to the `callback` function. If `thisArg` is
45
- * @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
46
- * the filter condition specified by the `callback` function.
47
- */
48
- filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MaxPriorityQueue<E, R>;
49
- /**
50
- * Time Complexity: O(n log n)
51
- * Space Complexity: O(n)
52
- *
53
- * The `map` function creates a new heap by applying a callback function to each element of the
54
- * original heap.
55
- * @param callback - The `callback` parameter is a function that will be called for each element in
56
- * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
57
- * element), and `this` (the heap itself). The callback function should return a value of
58
- * @param comparator - The `comparator` parameter is a function that defines the order of the
59
- * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
60
- * if `a` should be placed before `b`, a positive number if `a` should be placed after
61
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
62
- * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
63
- * returns a value of type `T`. This function is used to transform the elements of the original
64
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
65
- * specify the value of `this` within the callback function. It is used to set the context or scope
66
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
67
- * value of
68
- * @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
69
- */
70
- map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MaxPriorityQueue<EM, RM>;
71
27
  }
@@ -3,18 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MaxPriorityQueue = void 0;
4
4
  const priority_queue_1 = require("./priority-queue");
5
5
  /**
6
- *
6
+ * Max-oriented priority queue (max-heap) built on {@link PriorityQueue}.
7
+ * The default comparator orders primitive values in descending order. If you store objects,
8
+ * you must provide a custom comparator via {@link PriorityQueueOptions}.
9
+ * @template E Element type stored in the queue.
10
+ * @template R Extra record/metadata associated with each element.
11
+ * @example
7
12
  */
8
13
  class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
9
14
  /**
10
- * The constructor initializes a PriorityQueue with optional elements and options, including a
11
- * comparator function.
12
- * @param elements - The `elements` parameter is an iterable object that contains the initial
13
- * elements to be added to the priority queue. It is optional and defaults to an empty array if not
14
- * provided.
15
- * @param options - The `options` parameter is an object that contains additional configuration
16
- * options for the priority queue. In this case, it has a property called `comparator,` which is a
17
- * function used to compare elements in the priority queue.
15
+ * Creates a max-priority queue.
16
+ * @param elements Optional initial elements to insert.
17
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
18
+ * @throws {TypeError} Thrown when using the default comparator with object elements (provide a custom comparator).
19
+ * @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
18
20
  */
19
21
  constructor(elements = [], options) {
20
22
  super(elements, Object.assign({ comparator: (a, b) => {
@@ -28,74 +30,5 @@ class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
28
30
  return 0;
29
31
  } }, options));
30
32
  }
31
- /**
32
- * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
33
- * comparator and toElementFn as the current instance.
34
- * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
35
- * comparator and toElementFn as the current instance.
36
- */
37
- clone() {
38
- return new MaxPriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
39
- }
40
- /**
41
- * Time Complexity: O(n)
42
- * Space Complexity: O(n)
43
- *
44
- * The `filter` function creates a new MaxPriorityQueue 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 `MaxPriorityQueue` object that contains the elements that pass
54
- * the filter condition specified by the `callback` function.
55
- */
56
- filter(callback, thisArg) {
57
- const filteredPriorityQueue = new MaxPriorityQueue([], {
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
- * Time Complexity: O(n log n)
72
- * Space Complexity: O(n)
73
- *
74
- * The `map` function creates a new heap by applying a callback function to each element of the
75
- * original heap.
76
- * @param callback - The `callback` parameter is a function that will be called for each element in
77
- * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
78
- * element), and `this` (the heap itself). The callback function should return a value of
79
- * @param comparator - The `comparator` parameter is a function that defines the order of the
80
- * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
81
- * if `a` should be placed before `b`, a positive number if `a` should be placed after
82
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
83
- * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
84
- * returns a value of type `T`. This function is used to transform the elements of the original
85
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
86
- * specify the value of `this` within the callback function. It is used to set the context or scope
87
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
88
- * value of
89
- * @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
90
- */
91
- map(callback, comparator, toElementFn, thisArg) {
92
- const mappedPriorityQueue = new MaxPriorityQueue([], { comparator, toElementFn });
93
- let index = 0;
94
- for (const el of this) {
95
- mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
96
- index++;
97
- }
98
- return mappedPriorityQueue;
99
- }
100
33
  }
101
34
  exports.MaxPriorityQueue = MaxPriorityQueue;
@@ -5,68 +5,22 @@
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
+ * Min-oriented priority queue (min-heap) built on {@link PriorityQueue}.
12
+ * The queue removes the smallest element first under the provided comparator.
13
+ * Provide a custom comparator if you store non-primitive objects.
14
+ * @template E Element type stored in the queue.
15
+ * @template R Extra record/metadata associated with each element.
16
+ * @example
12
17
  */
13
18
  export declare class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
14
19
  /**
15
- * The constructor initializes a PriorityQueue with optional elements and options, including a
16
- * comparator function.
17
- * @param elements - The `elements` parameter is an iterable object that contains the initial
18
- * elements to be added to the priority queue. It is optional and defaults to an empty array if not
19
- * provided.
20
- * @param options - The `options` parameter is an object that contains additional configuration
21
- * options for the priority queue. In this case, it has a property called `comparator,` which is a
22
- * function used to compare elements in the priority queue. The `comparator` function takes two
23
- * parameters `a` and `b`
20
+ * Creates a min-priority queue.
21
+ * @param elements Optional initial elements to insert.
22
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
23
+ * @remarks Complexity Time: O(n log n) when inserting n elements incrementally; Space: O(n).
24
24
  */
25
25
  constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
26
- /**
27
- * The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
28
- * comparator and toElementFn as the original instance.
29
- * @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
30
- * properties as the current instance.
31
- */
32
- clone(): MinPriorityQueue<E, R>;
33
- /**
34
- * Time Complexity: O(n)
35
- * Space Complexity: O(n)
36
- *
37
- * The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
38
- * function.
39
- * @param callback - The `callback` parameter is a function that will be called for each element in
40
- * the heap. It takes three arguments: the current element, the index of the current element, and the
41
- * heap itself. The callback function should return a boolean value indicating whether the current
42
- * element should be included in the filtered list
43
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
44
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
45
- * passed as the `this` value to the `callback` function. If `thisArg` is
46
- * @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
47
- * the filter condition specified by the `callback` function.
48
- */
49
- filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MinPriorityQueue<E, R>;
50
- /**
51
- * Time Complexity: O(n log n)
52
- * Space Complexity: O(n)
53
- *
54
- * The `map` function creates a new heap by applying a callback function to each element of the
55
- * original heap.
56
- * @param callback - The `callback` parameter is a function that will be called for each element in
57
- * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
58
- * element), and `this` (the heap itself). The callback function should return a value of
59
- * @param comparator - The `comparator` parameter is a function that defines the order of the
60
- * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
61
- * if `a` should be placed before `b`, a positive number if `a` should be placed after
62
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
63
- * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
64
- * returns a value of type `T`. This function is used to transform the elements of the original
65
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
66
- * specify the value of `this` within the callback function. It is used to set the context or scope
67
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
68
- * value of
69
- * @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
70
- */
71
- map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MinPriorityQueue<EM, RM>;
72
26
  }