priority-queue-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 (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 +612 -879
  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 +6 -6
  64. package/dist/utils/utils.d.ts +110 -49
  65. package/dist/utils/utils.js +148 -73
  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 +198 -216
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  72. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  74. package/src/data-structures/binary-tree/bst.ts +568 -570
  75. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  76. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  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 +9 -5
  101. package/src/utils/utils.ts +152 -86
@@ -1,9 +1,20 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.LinkedListQueue = exports.Queue = void 0;
4
11
  const linked_list_1 = require("../linked-list");
5
12
  const linear_base_1 = require("../base/linear-base");
6
13
  /**
14
+ * Array-backed queue with amortized O(1) enqueue/dequeue via an offset pointer and optional auto-compaction.
15
+ * @remarks Time O(1), Space O(1)
16
+ * @template E
17
+ * @template R
7
18
  * 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
8
19
  * 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
9
20
  * 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
@@ -20,7 +31,7 @@ const linear_base_1 = require("../base/linear-base");
20
31
  * let maxSum = 0;
21
32
  * let currentSum = 0;
22
33
  *
23
- * nums.forEach((num) => {
34
+ * nums.forEach(num => {
24
35
  * queue.push(num);
25
36
  * currentSum += num;
26
37
  *
@@ -60,6 +71,13 @@ const linear_base_1 = require("../base/linear-base");
60
71
  * console.log(visited); // [1, 2, 3, 4, 5]
61
72
  */
62
73
  class Queue extends linear_base_1.LinearBase {
74
+ /**
75
+ * Create a Queue and optionally bulk-insert elements.
76
+ * @remarks Time O(N), Space O(N)
77
+ * @param [elements] - Iterable of elements (or raw records if toElementFn is set).
78
+ * @param [options] - Options such as toElementFn, maxLen, and autoCompactRatio.
79
+ * @returns New Queue instance.
80
+ */
63
81
  constructor(elements = [], options) {
64
82
  super(options);
65
83
  this._elements = [];
@@ -71,63 +89,86 @@ class Queue extends linear_base_1.LinearBase {
71
89
  }
72
90
  this.pushMany(elements);
73
91
  }
92
+ /**
93
+ * Get the underlying array buffer.
94
+ * @remarks Time O(1), Space O(1)
95
+ * @returns Backing array of elements.
96
+ */
74
97
  get elements() {
75
98
  return this._elements;
76
99
  }
100
+ /**
101
+ * Get the current start offset into the array.
102
+ * @remarks Time O(1), Space O(1)
103
+ * @returns Zero-based offset.
104
+ */
77
105
  get offset() {
78
106
  return this._offset;
79
107
  }
80
- get length() {
81
- return this.elements.length - this.offset;
82
- }
108
+ /**
109
+ * Get the compaction threshold (offset/size).
110
+ * @remarks Time O(1), Space O(1)
111
+ * @returns Auto-compaction ratio in (0,1].
112
+ */
83
113
  get autoCompactRatio() {
84
114
  return this._autoCompactRatio;
85
115
  }
86
- set autoCompactRatio(v) {
87
- this._autoCompactRatio = v;
116
+ /**
117
+ * Set the compaction threshold.
118
+ * @remarks Time O(1), Space O(1)
119
+ * @param value - New ratio; compacts when offset/size exceeds this value.
120
+ * @returns void
121
+ */
122
+ set autoCompactRatio(value) {
123
+ this._autoCompactRatio = value;
88
124
  }
89
125
  /**
90
- * Time Complexity: O(1)
91
- * Space Complexity: O(1)
92
- *
93
- * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
94
- * @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
95
- * the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
126
+ * Get the number of elements currently in the queue.
127
+ * @remarks Time O(1), Space O(1)
128
+ * @returns Current length.
129
+ */
130
+ get length() {
131
+ return this.elements.length - this._offset;
132
+ }
133
+ /**
134
+ * Get the first element (front) without removing it.
135
+ * @remarks Time O(1), Space O(1)
136
+ * @returns Front element or undefined.
96
137
  */
97
138
  get first() {
98
- return this.length > 0 ? this.elements[this.offset] : undefined;
139
+ return this.length > 0 ? this.elements[this._offset] : undefined;
99
140
  }
100
141
  /**
101
- * Time Complexity: O(1)
102
- * Space Complexity: O(1)
103
- *
104
- * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
105
- * @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
106
- * array is empty, it returns `undefined`.
142
+ * Get the last element (back) without removing it.
143
+ * @remarks Time O(1), Space O(1)
144
+ * @returns Back element or undefined.
107
145
  */
108
146
  get last() {
109
147
  return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
110
148
  }
111
149
  /**
112
- * Time Complexity: O(n)
113
- * Space Complexity: O(n)
114
- *
115
- * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
116
- * @public
117
- * @param {E[]} elements - The "elements" parameter is an array of elements of type E.
118
- * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
119
- * array.
150
+ * Create a queue from an array of elements.
151
+ * @remarks Time O(N), Space O(N)
152
+ * @template E
153
+ * @param elements - Array of elements to enqueue in order.
154
+ * @returns A new queue populated from the array.
120
155
  */
121
156
  static fromArray(elements) {
122
157
  return new Queue(elements);
123
158
  }
124
159
  /**
125
- * Time Complexity: O(1)
126
- * Space Complexity: O(1)
127
- *
128
- * The push function adds an element to the end of the queue and returns true. Adds an element at the back of the queue.
129
- * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
130
- * @returns Always returns true, indicating the element was successfully added.
160
+ * Check whether the queue is empty.
161
+ * @remarks Time O(1), Space O(1)
162
+ * @returns True if length is 0.
163
+ */
164
+ isEmpty() {
165
+ return this.length === 0;
166
+ }
167
+ /**
168
+ * Enqueue one element at the back.
169
+ * @remarks Time O(1), Space O(1)
170
+ * @param element - Element to enqueue.
171
+ * @returns True on success.
131
172
  */
132
173
  push(element) {
133
174
  this.elements.push(element);
@@ -136,15 +177,10 @@ class Queue extends linear_base_1.LinearBase {
136
177
  return true;
137
178
  }
138
179
  /**
139
- * Time Complexity: O(k)
140
- * Space Complexity: O(k)
141
- *
142
- * The `pushMany` function iterates over elements and pushes them into an array after applying a
143
- * transformation function if provided.
144
- * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
145
- * is an iterable containing elements of type `E` or `R`.
146
- * @returns The `pushMany` function is returning an array of boolean values indicating whether each
147
- * element was successfully pushed into the data structure.
180
+ * Enqueue many elements from an iterable.
181
+ * @remarks Time O(N), Space O(1)
182
+ * @param elements - Iterable of elements (or raw records if toElementFn is set).
183
+ * @returns Array of per-element success flags.
148
184
  */
149
185
  pushMany(elements) {
150
186
  const ans = [];
@@ -157,300 +193,281 @@ class Queue extends linear_base_1.LinearBase {
157
193
  return ans;
158
194
  }
159
195
  /**
160
- * Time Complexity: O(1)
161
- * Space Complexity: O(1)
162
- *
163
- * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
164
- * necessary to optimize performance.
165
- * @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
196
+ * Dequeue one element from the front (amortized via offset).
197
+ * @remarks Time O(1) amortized, Space O(1)
198
+ * @returns Removed element or undefined.
166
199
  */
167
200
  shift() {
168
201
  if (this.length === 0)
169
202
  return undefined;
170
203
  const first = this.first;
171
204
  this._offset += 1;
172
- if (this.offset / this.elements.length > this.autoCompactRatio)
205
+ if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio)
173
206
  this.compact();
174
207
  return first;
175
208
  }
176
209
  /**
177
- * Time Complexity: O(n)
178
- * Space Complexity: O(1)
179
- *
180
- * The delete function removes an element from the list.
181
- * @param {E} element - Specify the element to be deleted
182
- * @return A boolean value indicating whether the element was successfully deleted or not
210
+ * Delete the first occurrence of a specific element.
211
+ * @remarks Time O(N), Space O(1)
212
+ * @param element - Element to remove (strict equality via Object.is).
213
+ * @returns True if an element was removed.
183
214
  */
184
215
  delete(element) {
185
- const index = this.elements.indexOf(element);
186
- return !!this.deleteAt(index);
187
- }
188
- /**
189
- * Time Complexity: O(n)
190
- * Space Complexity: O(1)
191
- *
192
- * The deleteAt function deletes the element at a given index.
193
- * @param {number} index - Determine the index of the element to be deleted
194
- * @return A boolean value
195
- */
196
- deleteAt(index) {
197
- const deleted = this.elements[index];
198
- this.elements.splice(index, 1);
199
- return deleted;
216
+ for (let i = this._offset; i < this.elements.length; i++) {
217
+ if (Object.is(this.elements[i], element)) {
218
+ this.elements.splice(i, 1);
219
+ return true;
220
+ }
221
+ }
222
+ return false;
200
223
  }
201
224
  /**
202
- * Time Complexity: O(1)
203
- * Space Complexity: O(1)
204
- *
205
- * The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
206
- * if the index is out of bounds.
207
- * @param {number} index - The `index` parameter represents the position of the element you want to
208
- * retrieve from the data structure.
209
- * @returns The `at` method is returning the element at the specified index adjusted by the offset
210
- * `_offset`.
225
+ * Get the element at a given logical index.
226
+ * @remarks Time O(1), Space O(1)
227
+ * @param index - Zero-based index from the front.
228
+ * @returns Element or undefined.
211
229
  */
212
230
  at(index) {
213
- return this.elements[index + this._offset];
231
+ if (index < 0 || index >= this.length)
232
+ return undefined;
233
+ return this._elements[this._offset + index];
214
234
  }
215
235
  /**
216
- * Time Complexity: O(n)
217
- * Space Complexity: O(1)
218
- *
219
- * The `reverse` function in TypeScript reverses the elements of an array starting from a specified
220
- * offset.
221
- * @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
222
- * the elements in the array and resetting the offset to 0.
236
+ * Delete the element at a given index.
237
+ * @remarks Time O(N), Space O(1)
238
+ * @param index - Zero-based index from the front.
239
+ * @returns Removed element or undefined.
223
240
  */
224
- reverse() {
225
- this._elements = this.elements.slice(this.offset).reverse();
226
- this._offset = 0;
227
- return this;
241
+ deleteAt(index) {
242
+ if (index < 0 || index >= this.length)
243
+ return undefined;
244
+ const gi = this._offset + index;
245
+ const [deleted] = this.elements.splice(gi, 1);
246
+ return deleted;
228
247
  }
229
248
  /**
230
- * Time Complexity: O(n)
231
- * Space Complexity: O(1)
232
- *
233
- * The function `addAt` inserts a new element at a specified index in an array, returning true if
234
- * successful and false if the index is out of bounds.
235
- * @param {number} index - The `index` parameter represents the position at which the `newElement`
236
- * should be added in the array.
237
- * @param {E} newElement - The `newElement` parameter represents the element that you want to insert
238
- * into the array at the specified index.
239
- * @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
240
- * added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
241
- * than the length of the array).
249
+ * Insert a new element at a given index.
250
+ * @remarks Time O(N), Space O(1)
251
+ * @param index - Zero-based index from the front.
252
+ * @param newElement - Element to insert.
253
+ * @returns True if inserted.
242
254
  */
243
255
  addAt(index, newElement) {
244
256
  if (index < 0 || index > this.length)
245
257
  return false;
246
- this._elements.splice(this.offset + index, 0, newElement);
258
+ this._elements.splice(this._offset + index, 0, newElement);
247
259
  return true;
248
260
  }
249
261
  /**
250
- * Time Complexity: O(1)
251
- * Space Complexity: O(1)
252
- *
253
- * The function `setAt` updates an element at a specified index in an array-like data structure.
254
- * @param {number} index - The `index` parameter is a number that represents the position in the
255
- * array where the new element will be set.
256
- * @param {E} newElement - The `newElement` parameter represents the new value that you want to set
257
- * at the specified index in the array.
258
- * @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
259
- * at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
260
- * length of the array).
262
+ * Replace the element at a given index.
263
+ * @remarks Time O(1), Space O(1)
264
+ * @param index - Zero-based index from the front.
265
+ * @param newElement - New element to set.
266
+ * @returns True if updated.
261
267
  */
262
268
  setAt(index, newElement) {
263
- if (index < 0 || index > this.length)
269
+ if (index < 0 || index >= this.length)
264
270
  return false;
265
- this._elements[this.offset + index] = newElement;
271
+ this._elements[this._offset + index] = newElement;
266
272
  return true;
267
273
  }
268
274
  /**
269
- * Time Complexity: O(1)
270
- * Space Complexity: O(1)
271
- *
272
- * The function checks if a data structure is empty by comparing its length to zero.
273
- * @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
275
+ * Reverse the queue in-place by compacting then reversing.
276
+ * @remarks Time O(N), Space O(N)
277
+ * @returns This queue.
274
278
  */
275
- isEmpty() {
276
- return this.length === 0;
279
+ reverse() {
280
+ this._elements = this.elements.slice(this._offset).reverse();
281
+ this._offset = 0;
282
+ return this;
277
283
  }
278
284
  /**
279
- * Time Complexity: O(1)
280
- * Space Complexity: O(1)
281
- *
282
- * The clear function resets the elements array and offset to their initial values.
285
+ * Remove all elements and reset offset.
286
+ * @remarks Time O(1), Space O(1)
287
+ * @returns void
283
288
  */
284
289
  clear() {
285
290
  this._elements = [];
286
291
  this._offset = 0;
287
292
  }
288
293
  /**
289
- * Time Complexity: O(n)
290
- * Space Complexity: O(1)
291
- *
292
- * The `compact` function in TypeScript slices the elements array based on the offset and resets the
293
- * offset to zero.
294
- * @returns The `compact()` method is returning a boolean value of `true`.
294
+ * Compact storage by discarding consumed head elements.
295
+ * @remarks Time O(N), Space O(N)
296
+ * @returns True when compaction performed.
295
297
  */
296
298
  compact() {
297
- this._elements = this.elements.slice(this.offset);
299
+ this._elements = this.elements.slice(this._offset);
298
300
  this._offset = 0;
299
301
  return true;
300
302
  }
301
303
  /**
302
- * Time Complexity: O(n)
303
- * Space Complexity: O(n)
304
- *
305
- * The function overrides the splice method to remove and insert elements in a queue-like data
306
- * structure.
307
- * @param {number} start - The `start` parameter in the `splice` method specifies the index at which
308
- * to start changing the array. Items will be added or removed starting from this index.
309
- * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
310
- * number of elements to remove from the array starting at the specified `start` index. If
311
- * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
312
- * elements can still be inserted at
313
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
314
- * will be added to the array at the specified `start` index. These elements will replace the
315
- * existing elements starting from the `start` index for the `deleteCount` number of elements.
316
- * @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
317
- * class as the original object.
304
+ * Remove and/or insert elements at a position (array-like).
305
+ * @remarks Time O(N + M), Space O(M)
306
+ * @param start - Start index (clamped to [0, length]).
307
+ * @param [deleteCount] - Number of elements to remove (default 0).
308
+ * @param [items] - Elements to insert after `start`.
309
+ * @returns A new queue containing the removed elements (typed as `this`).
318
310
  */
319
311
  splice(start, deleteCount = 0, ...items) {
320
- const removedQueue = this._createInstance();
321
312
  start = Math.max(0, Math.min(start, this.length));
322
313
  deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
323
- const globalStartIndex = this.offset + start;
324
- const removedElements = this._elements.splice(globalStartIndex, deleteCount, ...items);
325
- removedQueue.pushMany(removedElements);
326
- this.compact();
327
- return removedQueue;
314
+ const gi = this._offset + start;
315
+ const removedArray = this._elements.splice(gi, deleteCount, ...items);
316
+ if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio)
317
+ this.compact();
318
+ const removed = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
319
+ removed._setAutoCompactRatio(this._autoCompactRatio);
320
+ removed.pushMany(removedArray);
321
+ return removed;
328
322
  }
329
323
  /**
330
- * Time Complexity: O(n)
331
- * Space Complexity: O(n)
332
- *
333
- * The `clone()` function returns a new Queue object with the same elements as the original Queue.
334
- * @returns The `clone()` method is returning a new instance of the `Queue` class.
324
+ * Deep clone this queue and its parameters.
325
+ * @remarks Time O(N), Space O(N)
326
+ * @returns A new queue with the same content and options.
335
327
  */
336
328
  clone() {
337
- return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn, maxLen: this._maxLen });
338
- }
339
- /**
340
- * Time Complexity: O(n)
341
- * Space Complexity: O(n)
342
- *
343
- * The `filter` function creates a new `Queue` object containing elements from the original `Queue`
344
- * that satisfy a given predicate function.
345
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
346
- * the current element being iterated over, the index of the current element, and the queue itself.
347
- * It should return a boolean value indicating whether the element should be included in the filtered
348
- * queue or not.
349
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
350
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
351
- * passed as the `this` value to the `predicate` function. If `thisArg` is
352
- * @returns The `filter` method is returning a new `Queue` object that contains the elements that
353
- * satisfy the given predicate function.
329
+ const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
330
+ out._setAutoCompactRatio(this._autoCompactRatio);
331
+ for (let i = this._offset; i < this.elements.length; i++)
332
+ out.push(this.elements[i]);
333
+ return out;
334
+ }
335
+ /**
336
+ * Filter elements into a new queue of the same class.
337
+ * @remarks Time O(N), Space O(N)
338
+ * @param predicate - Predicate (element, index, queue) boolean to keep element.
339
+ * @param [thisArg] - Value for `this` inside the predicate.
340
+ * @returns A new queue with kept elements.
354
341
  */
355
342
  filter(predicate, thisArg) {
356
- const newDeque = this._createInstance({
357
- toElementFn: this._toElementFn,
358
- autoCompactRatio: this._autoCompactRatio,
359
- maxLen: this._maxLen
360
- });
343
+ const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
344
+ out._setAutoCompactRatio(this._autoCompactRatio);
361
345
  let index = 0;
362
- for (const el of this) {
363
- if (predicate.call(thisArg, el, index, this)) {
364
- newDeque.push(el);
365
- }
346
+ for (const v of this) {
347
+ if (predicate.call(thisArg, v, index, this))
348
+ out.push(v);
366
349
  index++;
367
350
  }
368
- return newDeque;
369
- }
370
- /**
371
- * Time Complexity: O(n)
372
- * Space Complexity: O(n)
373
- *
374
- * The `map` function in TypeScript creates a new Queue by applying a callback function to each
375
- * element in the original Queue.
376
- * @param callback - The `callback` parameter is a function that will be applied to each element in
377
- * the queue. It takes the current element, its index, and the queue itself as arguments, and returns
378
- * a new element.
379
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
380
- * convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
381
- * `map` method to transform each raw element before passing it to the `callback` function. If
382
- * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
383
- * value of `this` when executing the `callback` function. It allows you to set the context (the
384
- * value of `this`) within the callback function. If `thisArg` is provided, it will be
385
- * @returns A new Queue object containing elements of type EM, which are the result of applying the
386
- * callback function to each element in the original Queue object.
387
- */
388
- map(callback, toElementFn, thisArg) {
389
- const newDeque = new Queue([], {
390
- toElementFn,
391
- autoCompactRatio: this._autoCompactRatio,
392
- maxLen: this._maxLen
351
+ return out;
352
+ }
353
+ /**
354
+ * Map each element to a new element in a possibly different-typed queue.
355
+ * @remarks Time O(N), Space O(N)
356
+ * @template EM
357
+ * @template RM
358
+ * @param callback - Mapping function (element, index, queue) newElement.
359
+ * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
360
+ * @param [thisArg] - Value for `this` inside the callback.
361
+ * @returns A new Queue with mapped elements.
362
+ */
363
+ map(callback, options, thisArg) {
364
+ var _a, _b;
365
+ const out = new this.constructor([], {
366
+ toElementFn: options === null || options === void 0 ? void 0 : options.toElementFn,
367
+ maxLen: (_a = options === null || options === void 0 ? void 0 : options.maxLen) !== null && _a !== void 0 ? _a : this._maxLen,
368
+ autoCompactRatio: (_b = options === null || options === void 0 ? void 0 : options.autoCompactRatio) !== null && _b !== void 0 ? _b : this._autoCompactRatio
393
369
  });
394
370
  let index = 0;
395
- for (const el of this) {
396
- newDeque.push(callback.call(thisArg, el, index, this));
397
- index++;
398
- }
399
- return newDeque;
371
+ for (const v of this)
372
+ out.push(thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this));
373
+ return out;
400
374
  }
401
375
  /**
402
- * Time Complexity: O(n)
403
- * Space Complexity: O(n)
404
- *
405
- * The function `_getIterator` returns an iterable iterator for the elements in the class.
376
+ * Map each element to a new value of the same type.
377
+ * @remarks Time O(N), Space O(N)
378
+ * @param callback - Mapping function (element, index, queue) → element.
379
+ * @param [thisArg] - Value for `this` inside the callback.
380
+ * @returns A new queue with mapped elements (same element type).
406
381
  */
407
- *_getIterator() {
408
- for (const item of this.elements.slice(this.offset)) {
409
- yield item;
382
+ mapSame(callback, thisArg) {
383
+ var _a;
384
+ const Ctor = this.constructor;
385
+ const out = new Ctor([], {
386
+ toElementFn: this.toElementFn,
387
+ maxLen: this._maxLen,
388
+ autoCompactRatio: this._autoCompactRatio
389
+ });
390
+ (_a = out._setAutoCompactRatio) === null || _a === void 0 ? void 0 : _a.call(out, this._autoCompactRatio);
391
+ let index = 0;
392
+ for (const v of this) {
393
+ const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
394
+ out.push(mv);
410
395
  }
396
+ return out;
411
397
  }
412
398
  /**
413
- * The function `_createInstance` returns a new instance of the `Queue` class with the specified
414
- * options.
415
- * @param [options] - The `options` parameter in the `_createInstance` method is of type
416
- * `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
417
- * allows you to specify settings or properties that can influence how the queue operates.
418
- * @returns An instance of the `Queue` class with an empty array and the provided options is being
419
- * returned.
399
+ * (Protected) Set the internal auto-compaction ratio.
400
+ * @remarks Time O(1), Space O(1)
401
+ * @param value - New ratio to assign.
402
+ * @returns void
420
403
  */
421
- _createInstance(options) {
422
- return new Queue([], options);
404
+ _setAutoCompactRatio(value) {
405
+ this._autoCompactRatio = value;
423
406
  }
424
407
  /**
425
- * The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
426
- * order.
408
+ * (Protected) Iterate elements from front to back.
409
+ * @remarks Time O(N), Space O(1)
410
+ * @returns Iterator of E.
411
+ */
412
+ *_getIterator() {
413
+ for (let i = this._offset; i < this.elements.length; i++)
414
+ yield this.elements[i];
415
+ }
416
+ /**
417
+ * (Protected) Iterate elements from back to front.
418
+ * @remarks Time O(N), Space O(1)
419
+ * @returns Iterator of E.
427
420
  */
428
421
  *_getReverseIterator() {
429
422
  for (let i = this.length - 1; i >= 0; i--) {
430
- const cur = this.at(i); // `at()` handles the offset.
423
+ const cur = this.at(i);
431
424
  if (cur !== undefined)
432
425
  yield cur;
433
426
  }
434
427
  }
428
+ /**
429
+ * (Protected) Create an empty instance of the same concrete class.
430
+ * @remarks Time O(1), Space O(1)
431
+ * @param [options] - Options forwarded to the constructor.
432
+ * @returns An empty like-kind queue instance.
433
+ */
434
+ _createInstance(options) {
435
+ const Ctor = this.constructor;
436
+ return new Ctor([], options);
437
+ }
438
+ /**
439
+ * (Protected) Create a like-kind queue and seed it from an iterable.
440
+ * @remarks Time O(N), Space O(N)
441
+ * @template EM
442
+ * @template RM
443
+ * @param [elements] - Iterable used to seed the new queue.
444
+ * @param [options] - Options forwarded to the constructor.
445
+ * @returns A like-kind Queue instance.
446
+ */
447
+ _createLike(elements = [], options) {
448
+ const Ctor = this.constructor;
449
+ return new Ctor(elements, options);
450
+ }
435
451
  }
436
452
  exports.Queue = Queue;
437
453
  /**
438
- * 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
439
- * 2. Based on Linked List: LinkedListQueue uses a linked list to store elements. Each node in the linked list contains data and a pointer to the next node.
440
- * 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
441
- * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
454
+ * Queue implemented over a singly linked list; preserves head/tail operations with linear scans for queries.
455
+ * @remarks Time O(1), Space O(1)
456
+ * @template E
457
+ * @template R
458
+ * @example examples will be generated by unit test
442
459
  */
443
460
  class LinkedListQueue extends linked_list_1.SinglyLinkedList {
444
461
  /**
445
- * Time Complexity: O(n)
446
- * Space Complexity: O(n)
447
- * The `clone` function returns a new instance of the `LinkedListQueue` class with the same values as
448
- * the current instance.
449
- * @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
450
- * values as the original `LinkedListQueue`.
462
+ * Deep clone this linked-list-based queue.
463
+ * @remarks Time O(N), Space O(N)
464
+ * @returns A new queue with the same sequence of elements.
451
465
  */
452
466
  clone() {
453
- return new LinkedListQueue(this, { toElementFn: this.toElementFn, maxLen: this._maxLen });
467
+ const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
468
+ for (const v of this)
469
+ out.push(v);
470
+ return out;
454
471
  }
455
472
  }
456
473
  exports.LinkedListQueue = LinkedListQueue;