data-structure-typed 2.6.0 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/CHANGELOG.md +9 -1
  4. package/dist/cjs/binary-tree.cjs +2927 -23688
  5. package/dist/cjs/graph.cjs +845 -2634
  6. package/dist/cjs/hash.cjs +185 -616
  7. package/dist/cjs/heap.cjs +266 -818
  8. package/dist/cjs/index.cjs +5116 -31358
  9. package/dist/cjs/linked-list.cjs +644 -2615
  10. package/dist/cjs/matrix.cjs +220 -535
  11. package/dist/cjs/priority-queue.cjs +266 -818
  12. package/dist/cjs/queue.cjs +637 -2343
  13. package/dist/cjs/stack.cjs +124 -530
  14. package/dist/cjs/trie.cjs +145 -592
  15. package/dist/cjs-legacy/binary-tree.cjs +4352 -25113
  16. package/dist/cjs-legacy/graph.cjs +847 -2636
  17. package/dist/cjs-legacy/hash.cjs +181 -612
  18. package/dist/cjs-legacy/heap.cjs +266 -818
  19. package/dist/cjs-legacy/index.cjs +6657 -32899
  20. package/dist/cjs-legacy/linked-list.cjs +640 -2611
  21. package/dist/cjs-legacy/matrix.cjs +220 -535
  22. package/dist/cjs-legacy/priority-queue.cjs +266 -818
  23. package/dist/cjs-legacy/queue.cjs +634 -2340
  24. package/dist/cjs-legacy/stack.cjs +124 -530
  25. package/dist/cjs-legacy/trie.cjs +145 -592
  26. package/dist/esm/binary-tree.mjs +2927 -23688
  27. package/dist/esm/graph.mjs +845 -2634
  28. package/dist/esm/hash.mjs +185 -616
  29. package/dist/esm/heap.mjs +266 -818
  30. package/dist/esm/index.mjs +5116 -31358
  31. package/dist/esm/linked-list.mjs +644 -2615
  32. package/dist/esm/matrix.mjs +220 -535
  33. package/dist/esm/priority-queue.mjs +266 -818
  34. package/dist/esm/queue.mjs +637 -2343
  35. package/dist/esm/stack.mjs +124 -530
  36. package/dist/esm/trie.mjs +145 -592
  37. package/dist/esm-legacy/binary-tree.mjs +4352 -25113
  38. package/dist/esm-legacy/graph.mjs +847 -2636
  39. package/dist/esm-legacy/hash.mjs +181 -612
  40. package/dist/esm-legacy/heap.mjs +266 -818
  41. package/dist/esm-legacy/index.mjs +6657 -32899
  42. package/dist/esm-legacy/linked-list.mjs +640 -2611
  43. package/dist/esm-legacy/matrix.mjs +220 -535
  44. package/dist/esm-legacy/priority-queue.mjs +266 -818
  45. package/dist/esm-legacy/queue.mjs +634 -2340
  46. package/dist/esm-legacy/stack.mjs +124 -530
  47. package/dist/esm-legacy/trie.mjs +145 -592
  48. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +29 -500
  49. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +38 -563
  50. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +230 -1212
  51. package/dist/types/data-structures/binary-tree/bst.d.ts +124 -1063
  52. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +99 -854
  53. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +62 -314
  54. package/dist/types/data-structures/binary-tree/tree-map.d.ts +366 -5057
  55. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +277 -4885
  56. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +253 -3929
  57. package/dist/types/data-structures/binary-tree/tree-set.d.ts +261 -4782
  58. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -44
  59. package/dist/types/data-structures/graph/directed-graph.d.ts +130 -527
  60. package/dist/types/data-structures/graph/undirected-graph.d.ts +125 -482
  61. package/dist/types/data-structures/hash/hash-map.d.ts +132 -562
  62. package/dist/types/data-structures/heap/heap.d.ts +194 -746
  63. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +120 -809
  64. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -733
  65. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +193 -863
  66. package/dist/types/data-structures/matrix/matrix.d.ts +159 -474
  67. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -6
  68. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +6 -11
  69. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  70. package/dist/types/data-structures/queue/deque.d.ts +196 -804
  71. package/dist/types/data-structures/queue/queue.d.ts +99 -585
  72. package/dist/types/data-structures/stack/stack.d.ts +75 -481
  73. package/dist/types/data-structures/trie/trie.d.ts +98 -584
  74. package/dist/umd/data-structure-typed.js +6580 -32822
  75. package/dist/umd/data-structure-typed.min.js +3 -3
  76. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +162 -215
  77. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +9 -9
  78. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +47 -47
  79. package/docs-site-docusaurus/docs/api/classes/BST.md +155 -208
  80. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +7 -7
  81. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -29
  82. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +104 -158
  83. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
  84. package/docs-site-docusaurus/docs/api/classes/Deque.md +253 -101
  85. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
  86. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +244 -114
  87. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +6 -6
  88. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  89. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  90. package/docs-site-docusaurus/docs/api/classes/HashMap.md +52 -48
  91. package/docs-site-docusaurus/docs/api/classes/Heap.md +119 -64
  92. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +84 -14
  93. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +138 -34
  94. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +149 -41
  95. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
  96. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +172 -92
  97. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +60 -80
  98. package/docs-site-docusaurus/docs/api/classes/Matrix.md +57 -52
  99. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -62
  100. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +120 -70
  101. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -62
  102. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +125 -75
  103. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +119 -64
  104. package/docs-site-docusaurus/docs/api/classes/Queue.md +158 -74
  105. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +171 -225
  106. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -22
  107. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +172 -94
  108. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +4 -4
  109. package/docs-site-docusaurus/docs/api/classes/SkipList.md +98 -75
  110. package/docs-site-docusaurus/docs/api/classes/Stack.md +112 -50
  111. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
  112. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +90 -121
  113. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +298 -107
  114. package/docs-site-docusaurus/docs/api/classes/Trie.md +116 -58
  115. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
  116. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +59 -77
  117. package/package.json +45 -46
  118. package/src/common/error.ts +15 -32
  119. package/src/common/index.ts +0 -3
  120. package/src/data-structures/base/iterable-element-base.ts +0 -3
  121. package/src/data-structures/base/linear-base.ts +2 -36
  122. package/src/data-structures/binary-tree/avl-tree.ts +31 -529
  123. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
  124. package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
  125. package/src/data-structures/binary-tree/bst.ts +158 -1082
  126. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
  127. package/src/data-structures/binary-tree/segment-tree.ts +73 -351
  128. package/src/data-structures/binary-tree/tree-map.ts +462 -5124
  129. package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
  130. package/src/data-structures/binary-tree/tree-multi-set.ts +299 -3983
  131. package/src/data-structures/binary-tree/tree-set.ts +338 -4836
  132. package/src/data-structures/graph/abstract-graph.ts +98 -167
  133. package/src/data-structures/graph/directed-graph.ts +137 -562
  134. package/src/data-structures/graph/map-graph.ts +0 -3
  135. package/src/data-structures/graph/undirected-graph.ts +132 -511
  136. package/src/data-structures/hash/hash-map.ts +154 -582
  137. package/src/data-structures/heap/heap.ts +200 -795
  138. package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
  139. package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
  140. package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
  141. package/src/data-structures/matrix/matrix.ts +179 -518
  142. package/src/data-structures/matrix/navigator.ts +0 -1
  143. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  144. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  145. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  146. package/src/data-structures/queue/deque.ts +214 -882
  147. package/src/data-structures/queue/queue.ts +102 -625
  148. package/src/data-structures/stack/stack.ts +76 -505
  149. package/src/data-structures/trie/trie.ts +98 -628
  150. package/src/types/common.ts +0 -10
  151. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  152. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  153. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  154. package/src/types/data-structures/hash/hash-map.ts +0 -3
  155. package/src/types/data-structures/hash/index.ts +0 -1
  156. package/src/types/data-structures/matrix/navigator.ts +0 -2
  157. package/src/types/utils/utils.ts +0 -7
  158. package/src/types/utils/validate-type.ts +0 -7
  159. package/src/utils/number.ts +0 -2
  160. package/src/utils/utils.ts +0 -5
@@ -5,7 +5,6 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
-
9
8
  import type { ElementCallback, LinearBaseOptions, QueueOptions } from '../../types';
10
9
  import { SinglyLinkedList } from '../linked-list';
11
10
  import { LinearBase } from '../base/linear-base';
@@ -96,7 +95,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
96
95
  * @param [options] - Options such as toElementFn, maxLen, and autoCompactRatio.
97
96
  * @returns New Queue instance.
98
97
  */
99
-
100
98
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: QueueOptions<E, R>) {
101
99
  super(options);
102
100
  if (options) {
@@ -113,7 +111,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
113
111
  * @remarks Time O(1), Space O(1)
114
112
  * @returns Backing array of elements.
115
113
  */
116
-
117
114
  get elements(): E[] {
118
115
  return this._elements;
119
116
  }
@@ -125,7 +122,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
125
122
  * @remarks Time O(1), Space O(1)
126
123
  * @returns Zero-based offset.
127
124
  */
128
-
129
125
  get offset(): number {
130
126
  return this._offset;
131
127
  }
@@ -137,7 +133,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
137
133
  * @remarks Time O(1), Space O(1)
138
134
  * @returns Auto-compaction ratio in (0,1].
139
135
  */
140
-
141
136
  get autoCompactRatio(): number {
142
137
  return this._autoCompactRatio;
143
138
  }
@@ -148,7 +143,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
148
143
  * @param value - New ratio; compacts when offset/size exceeds this value.
149
144
  * @returns void
150
145
  */
151
-
152
146
  set autoCompactRatio(value: number) {
153
147
  this._autoCompactRatio = value;
154
148
  }
@@ -157,57 +151,14 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
157
151
  * Get the number of elements currently in the queue.
158
152
  * @remarks Time O(1), Space O(1)
159
153
  * @returns Current length.
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
- * @example
203
- * // Track queue length
204
- * const q = new Queue<number>();
205
- * console.log(q.length); // 0;
206
- * q.push(1);
207
- * q.push(2);
208
- * console.log(q.length); // 2;
154
+ * @example
155
+ * // Track queue length
156
+ * const q = new Queue<number>();
157
+ * console.log(q.length); // 0;
158
+ * q.push(1);
159
+ * q.push(2);
160
+ * console.log(q.length); // 2;
209
161
  */
210
-
211
162
  get length(): number {
212
163
  return this.elements.length - this._offset;
213
164
  }
@@ -216,74 +167,21 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
216
167
  * Get the first element (front) without removing it.
217
168
  * @remarks Time O(1), Space O(1)
218
169
  * @returns Front element or undefined.
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
- * @example
262
- * // View the front element
263
- * const q = new Queue<string>(['first', 'second', 'third']);
264
- * console.log(q.first); // 'first';
265
- * console.log(q.length); // 3;
170
+ * @example
171
+ * // View the front element
172
+ * const q = new Queue<string>(['first', 'second', 'third']);
173
+ * console.log(q.first); // 'first';
174
+ * console.log(q.length); // 3;
266
175
  */
267
-
268
176
  get first(): E | undefined {
269
177
  return this.length > 0 ? this.elements[this._offset] : undefined;
270
178
  }
271
179
 
272
- /**
273
- * Peek at the front element without removing it (alias for `first`).
274
- * @remarks Time O(1), Space O(1)
275
- * @returns Front element or undefined.
276
- */
277
- peek(): E | undefined {
278
- return this.first;
279
- }
280
-
281
180
  /**
282
181
  * Get the last element (back) without removing it.
283
182
  * @remarks Time O(1), Space O(1)
284
183
  * @returns Back element or undefined.
285
184
  */
286
-
287
185
  get last(): E | undefined {
288
186
  return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
289
187
  }
@@ -295,77 +193,42 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
295
193
  * @param elements - Array of elements to enqueue in order.
296
194
  * @returns A new queue populated from the array.
297
195
  */
298
-
299
196
  static fromArray<E>(elements: E[]): Queue<E> {
300
197
  return new Queue(elements);
301
198
  }
302
199
 
200
+ /**
201
+ * Peek at the front element without removing it (alias for `first`).
202
+ * @remarks Time O(1), Space O(1)
203
+ * @returns Front element or undefined.
204
+ */
205
+ peek(): E | undefined {
206
+ return this.first;
207
+ }
208
+
303
209
  /**
304
210
  * Check whether the queue is empty.
305
211
  * @remarks Time O(1), Space O(1)
306
212
  * @returns True if length is 0.
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
- * @example
350
- * // Queue for...of iteration and isEmpty check
351
- * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
352
- *
353
- * const elements: string[] = [];
354
- * for (const item of queue) {
355
- * elements.push(item);
356
- * }
357
- *
358
- * // Verify all elements are iterated in order
359
- * console.log(elements); // ['A', 'B', 'C', 'D'];
360
- *
361
- * // Process all elements
362
- * while (queue.length > 0) {
363
- * queue.shift();
364
- * }
365
- *
366
- * console.log(queue.length); // 0;
213
+ * @example
214
+ * // Queue for...of iteration and isEmpty check
215
+ * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
216
+ *
217
+ * const elements: string[] = [];
218
+ * for (const item of queue) {
219
+ * elements.push(item);
220
+ * }
221
+ *
222
+ * // Verify all elements are iterated in order
223
+ * console.log(elements); // ['A', 'B', 'C', 'D'];
224
+ *
225
+ * // Process all elements
226
+ * while (queue.length > 0) {
227
+ * queue.shift();
228
+ * }
229
+ *
230
+ * console.log(queue.length); // 0;
367
231
  */
368
-
369
232
  isEmpty(): boolean {
370
233
  return this.length === 0;
371
234
  }
@@ -375,60 +238,17 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
375
238
  * @remarks Time O(1), Space O(1)
376
239
  * @param element - Element to enqueue.
377
240
  * @returns True on success.
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
- * @example
421
- * // basic Queue creation and push operation
422
- * // Create a simple Queue with initial values
423
- * const queue = new Queue([1, 2, 3, 4, 5]);
424
- *
425
- * // Verify the queue maintains insertion order
426
- * console.log([...queue]); // [1, 2, 3, 4, 5];
427
- *
428
- * // Check length
429
- * console.log(queue.length); // 5;
241
+ * @example
242
+ * // basic Queue creation and push operation
243
+ * // Create a simple Queue with initial values
244
+ * const queue = new Queue([1, 2, 3, 4, 5]);
245
+ *
246
+ * // Verify the queue maintains insertion order
247
+ * console.log([...queue]); // [1, 2, 3, 4, 5];
248
+ *
249
+ * // Check length
250
+ * console.log(queue.length); // 5;
430
251
  */
431
-
432
252
  push(element: E): boolean {
433
253
  this.elements.push(element);
434
254
  if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
@@ -441,7 +261,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
441
261
  * @param elements - Iterable of elements (or raw records if toElementFn is set).
442
262
  * @returns Array of per-element success flags.
443
263
  */
444
-
445
264
  pushMany(elements: Iterable<E> | Iterable<R>): boolean[] {
446
265
  const ans: boolean[] = [];
447
266
  for (const el of elements) {
@@ -455,64 +274,21 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
455
274
  * Dequeue one element from the front (amortized via offset).
456
275
  * @remarks Time O(1) amortized, Space O(1)
457
276
  * @returns Removed element or undefined.
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
- * @example
501
- * // Queue shift and peek operations
502
- * const queue = new Queue<number>([10, 20, 30, 40]);
503
- *
504
- * // Peek at the front element without removing it
505
- * console.log(queue.first); // 10;
506
- *
507
- * // Remove and get the first element (FIFO)
508
- * const first = queue.shift();
509
- * console.log(first); // 10;
510
- *
511
- * // Verify remaining elements and length decreased
512
- * console.log([...queue]); // [20, 30, 40];
513
- * console.log(queue.length); // 3;
277
+ * @example
278
+ * // Queue shift and peek operations
279
+ * const queue = new Queue<number>([10, 20, 30, 40]);
280
+ *
281
+ * // Peek at the front element without removing it
282
+ * console.log(queue.first); // 10;
283
+ *
284
+ * // Remove and get the first element (FIFO)
285
+ * const first = queue.shift();
286
+ * console.log(first); // 10;
287
+ *
288
+ * // Verify remaining elements and length decreased
289
+ * console.log([...queue]); // [20, 30, 40];
290
+ * console.log(queue.length); // 3;
514
291
  */
515
-
516
292
  shift(): E | undefined {
517
293
  if (this.length === 0) return undefined;
518
294
  const first = this.first;
@@ -526,52 +302,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
526
302
  * @remarks Time O(N), Space O(1)
527
303
  * @param element - Element to remove (strict equality via Object.is).
528
304
  * @returns True if an element was removed.
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
- * @example
569
- * // Remove specific element
570
- * const q = new Queue<number>([1, 2, 3, 2]);
571
- * q.delete(2);
572
- * console.log(q.length); // 3;
305
+ * @example
306
+ * // Remove specific element
307
+ * const q = new Queue<number>([1, 2, 3, 2]);
308
+ * q.delete(2);
309
+ * console.log(q.length); // 3;
573
310
  */
574
-
575
311
  delete(element: E): boolean {
576
312
  for (let i = this._offset; i < this.elements.length; i++) {
577
313
  if (Object.is(this.elements[i], element)) {
@@ -587,52 +323,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
587
323
  * @remarks Time O(1), Space O(1)
588
324
  * @param index - Zero-based index from the front.
589
325
  * @returns Element or undefined.
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
- * @example
630
- * // Access element by index
631
- * const q = new Queue<string>(['a', 'b', 'c']);
632
- * console.log(q.at(0)); // 'a';
633
- * console.log(q.at(2)); // 'c';
326
+ * @example
327
+ * // Access element by index
328
+ * const q = new Queue<string>(['a', 'b', 'c']);
329
+ * console.log(q.at(0)); // 'a';
330
+ * console.log(q.at(2)); // 'c';
634
331
  */
635
-
636
332
  at(index: number): E | undefined {
637
333
  if (index < 0 || index >= this.length) return undefined;
638
334
  return this._elements[this._offset + index];
@@ -644,7 +340,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
644
340
  * @param index - Zero-based index from the front.
645
341
  * @returns Removed element or undefined.
646
342
  */
647
-
648
343
  deleteAt(index: number): E | undefined {
649
344
  if (index < 0 || index >= this.length) return undefined;
650
345
  const gi = this._offset + index;
@@ -659,7 +354,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
659
354
  * @param newElement - Element to insert.
660
355
  * @returns True if inserted.
661
356
  */
662
-
663
357
  addAt(index: number, newElement: E): boolean {
664
358
  if (index < 0 || index > this.length) return false;
665
359
  this._elements.splice(this._offset + index, 0, newElement);
@@ -673,7 +367,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
673
367
  * @param newElement - New element to set.
674
368
  * @returns True if updated.
675
369
  */
676
-
677
370
  setAt(index: number, newElement: E): boolean {
678
371
  if (index < 0 || index >= this.length) return false;
679
372
  this._elements[this._offset + index] = newElement;
@@ -701,7 +394,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
701
394
  * @remarks Time O(N), Space O(N)
702
395
  * @returns This queue.
703
396
  */
704
-
705
397
  reverse(): this {
706
398
  this._elements = this.elements.slice(this._offset).reverse();
707
399
  this._offset = 0;
@@ -712,53 +404,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
712
404
  * Remove all elements and reset offset.
713
405
  * @remarks Time O(1), Space O(1)
714
406
  * @returns void
715
-
716
-
717
-
718
-
719
-
720
-
721
-
722
-
723
-
724
-
725
-
726
-
727
-
728
-
729
-
730
-
731
-
732
-
733
-
734
-
735
-
736
-
737
-
738
-
739
-
740
-
741
-
742
-
743
-
744
-
745
-
746
-
747
-
748
-
749
-
750
-
751
-
752
-
753
-
754
-
755
- * @example
756
- * // Remove all elements
757
- * const q = new Queue<number>([1, 2, 3]);
758
- * q.clear();
759
- * console.log(q.length); // 0;
407
+ * @example
408
+ * // Remove all elements
409
+ * const q = new Queue<number>([1, 2, 3]);
410
+ * q.clear();
411
+ * console.log(q.length); // 0;
760
412
  */
761
-
762
413
  clear(): void {
763
414
  this._elements = [];
764
415
  this._offset = 0;
@@ -768,54 +419,14 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
768
419
  * Compact storage by discarding consumed head elements.
769
420
  * @remarks Time O(N), Space O(N)
770
421
  * @returns True when compaction performed.
771
-
772
-
773
-
774
-
775
-
776
-
777
-
778
-
779
-
780
-
781
-
782
-
783
-
784
-
785
-
786
-
787
-
788
-
789
-
790
-
791
-
792
-
793
-
794
-
795
-
796
-
797
-
798
-
799
-
800
-
801
-
802
-
803
-
804
-
805
-
806
-
807
-
808
-
809
-
810
- * @example
811
- * // Reclaim unused memory
812
- * const q = new Queue<number>([1, 2, 3, 4, 5]);
813
- * q.shift();
814
- * q.shift();
815
- * q.compact();
816
- * console.log(q.length); // 3;
422
+ * @example
423
+ * // Reclaim unused memory
424
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
425
+ * q.shift();
426
+ * q.shift();
427
+ * q.compact();
428
+ * console.log(q.length); // 3;
817
429
  */
818
-
819
430
  compact(): boolean {
820
431
  this._elements = this.elements.slice(this._offset);
821
432
  this._offset = 0;
@@ -830,20 +441,15 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
830
441
  * @param [items] - Elements to insert after `start`.
831
442
  * @returns A new queue containing the removed elements (typed as `this`).
832
443
  */
833
-
834
444
  override splice(start: number, deleteCount: number = 0, ...items: E[]): this {
835
445
  start = Math.max(0, Math.min(start, this.length));
836
446
  deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
837
-
838
447
  const gi = this._offset + start;
839
448
  const removedArray = this._elements.splice(gi, deleteCount, ...items);
840
-
841
449
  if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio) this.compact();
842
-
843
450
  const removed = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
844
451
  removed._setAutoCompactRatio(this._autoCompactRatio);
845
452
  removed.pushMany(removedArray);
846
-
847
453
  return removed as unknown as this;
848
454
  }
849
455
 
@@ -851,55 +457,14 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
851
457
  * Deep clone this queue and its parameters.
852
458
  * @remarks Time O(N), Space O(N)
853
459
  * @returns A new queue with the same content and options.
854
-
855
-
856
-
857
-
858
-
859
-
860
-
861
-
862
-
863
-
864
-
865
-
866
-
867
-
868
-
869
-
870
-
871
-
872
-
873
-
874
-
875
-
876
-
877
-
878
-
879
-
880
-
881
-
882
-
883
-
884
-
885
-
886
-
887
-
888
-
889
-
890
-
891
-
892
-
893
-
894
- * @example
895
- * // Create independent copy
896
- * const q = new Queue<number>([1, 2, 3]);
897
- * const copy = q.clone();
898
- * copy.shift();
899
- * console.log(q.length); // 3;
900
- * console.log(copy.length); // 2;
460
+ * @example
461
+ * // Create independent copy
462
+ * const q = new Queue<number>([1, 2, 3]);
463
+ * const copy = q.clone();
464
+ * copy.shift();
465
+ * console.log(q.length); // 3;
466
+ * console.log(copy.length); // 2;
901
467
  */
902
-
903
468
  clone(): this {
904
469
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
905
470
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -913,53 +478,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
913
478
  * @param predicate - Predicate (element, index, queue) → boolean to keep element.
914
479
  * @param [thisArg] - Value for `this` inside the predicate.
915
480
  * @returns A new queue with kept elements.
916
-
917
-
918
-
919
-
920
-
921
-
922
-
923
-
924
-
925
-
926
-
927
-
928
-
929
-
930
-
931
-
932
-
933
-
934
-
935
-
936
-
937
-
938
-
939
-
940
-
941
-
942
-
943
-
944
-
945
-
946
-
947
-
948
-
949
-
950
-
951
-
952
-
953
-
954
-
955
-
956
- * @example
957
- * // Filter elements
958
- * const q = new Queue<number>([1, 2, 3, 4, 5]);
959
- * const evens = q.filter(x => x % 2 === 0);
960
- * console.log(evens.length); // 2;
481
+ * @example
482
+ * // Filter elements
483
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
484
+ * const evens = q.filter(x => x % 2 === 0);
485
+ * console.log(evens.length); // 2;
961
486
  */
962
-
963
487
  filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
964
488
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
965
489
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -980,52 +504,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
980
504
  * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
981
505
  * @param [thisArg] - Value for `this` inside the callback.
982
506
  * @returns A new Queue with mapped elements.
983
-
984
-
985
-
986
-
987
-
988
-
989
-
990
-
991
-
992
-
993
-
994
-
995
-
996
-
997
-
998
-
999
-
1000
-
1001
-
1002
-
1003
-
1004
-
1005
-
1006
-
1007
-
1008
-
1009
-
1010
-
1011
-
1012
-
1013
-
1014
-
1015
-
1016
-
1017
-
1018
-
1019
-
1020
-
1021
-
1022
- * @example
1023
- * // Transform elements
1024
- * const q = new Queue<number>([1, 2, 3]);
1025
- * const doubled = q.map(x => x * 2);
1026
- * console.log(doubled.toArray()); // [2, 4, 6];
507
+ * @example
508
+ * // Transform elements
509
+ * const q = new Queue<number>([1, 2, 3]);
510
+ * const doubled = q.map(x => x * 2);
511
+ * console.log(doubled.toArray()); // [2, 4, 6];
1027
512
  */
1028
-
1029
513
  map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: QueueOptions<EM, RM>, thisArg?: unknown): Queue<EM, RM> {
1030
514
  const out = new (this.constructor as new (
1031
515
  elements?: Iterable<EM> | Iterable<RM>,
@@ -1048,7 +532,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1048
532
  * @param [thisArg] - Value for `this` inside the callback.
1049
533
  * @returns A new queue with mapped elements (same element type).
1050
534
  */
1051
-
1052
535
  mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
1053
536
  const Ctor = this.constructor as new (
1054
537
  elements?: Iterable<E> | Iterable<R>,
@@ -1074,7 +557,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1074
557
  * @param value - New ratio to assign.
1075
558
  * @returns void
1076
559
  */
1077
-
1078
560
  protected _setAutoCompactRatio(value: number): void {
1079
561
  this._autoCompactRatio = value;
1080
562
  }
@@ -1084,7 +566,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1084
566
  * @remarks Time O(N), Space O(1)
1085
567
  * @returns Iterator of E.
1086
568
  */
1087
-
1088
569
  protected *_getIterator(): IterableIterator<E> {
1089
570
  for (let i = this._offset; i < this.elements.length; i++) yield this.elements[i];
1090
571
  }
@@ -1094,7 +575,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1094
575
  * @remarks Time O(N), Space O(1)
1095
576
  * @returns Iterator of E.
1096
577
  */
1097
-
1098
578
  protected *_getReverseIterator(): IterableIterator<E> {
1099
579
  for (let i = this.length - 1; i >= 0; i--) {
1100
580
  const cur = this.at(i);
@@ -1108,7 +588,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1108
588
  * @param [options] - Options forwarded to the constructor.
1109
589
  * @returns An empty like-kind queue instance.
1110
590
  */
1111
-
1112
591
  protected override _createInstance(options?: LinearBaseOptions<E, R>): this {
1113
592
  const Ctor = this.constructor as new (elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>) => this;
1114
593
  return new Ctor([], options as QueueOptions<E, R> | undefined);
@@ -1123,7 +602,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1123
602
  * @param [options] - Options forwarded to the constructor.
1124
603
  * @returns A like-kind Queue instance.
1125
604
  */
1126
-
1127
605
  protected _createLike<EM = E, RM = R>(
1128
606
  elements: Iterable<EM> | Iterable<RM> = [],
1129
607
  options?: QueueOptions<EM, RM>
@@ -1149,7 +627,6 @@ export class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
1149
627
  * @remarks Time O(N), Space O(N)
1150
628
  * @returns A new queue with the same sequence of elements.
1151
629
  */
1152
-
1153
630
  override clone(): this {
1154
631
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1155
632
  for (const v of this) out.push(v);