data-structure-typed 2.5.3 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/.husky/pre-commit +3 -0
  4. package/CHANGELOG.md +1 -1
  5. package/MIGRATION.md +48 -0
  6. package/README.md +20 -2
  7. package/README_CN.md +20 -2
  8. package/SPECIFICATION.md +24 -0
  9. package/SPECIFICATION.zh-CN.md +24 -0
  10. package/dist/cjs/binary-tree.cjs +1897 -19
  11. package/dist/cjs/graph.cjs +174 -0
  12. package/dist/cjs/hash.cjs +33 -0
  13. package/dist/cjs/heap.cjs +71 -0
  14. package/dist/cjs/index.cjs +2383 -3
  15. package/dist/cjs/linked-list.cjs +224 -2
  16. package/dist/cjs/matrix.cjs +24 -0
  17. package/dist/cjs/priority-queue.cjs +71 -0
  18. package/dist/cjs/queue.cjs +221 -1
  19. package/dist/cjs/stack.cjs +59 -0
  20. package/dist/cjs/trie.cjs +62 -0
  21. package/dist/cjs-legacy/binary-tree.cjs +1897 -19
  22. package/dist/cjs-legacy/graph.cjs +174 -0
  23. package/dist/cjs-legacy/hash.cjs +33 -0
  24. package/dist/cjs-legacy/heap.cjs +71 -0
  25. package/dist/cjs-legacy/index.cjs +2383 -3
  26. package/dist/cjs-legacy/linked-list.cjs +224 -2
  27. package/dist/cjs-legacy/matrix.cjs +24 -0
  28. package/dist/cjs-legacy/priority-queue.cjs +71 -0
  29. package/dist/cjs-legacy/queue.cjs +221 -1
  30. package/dist/cjs-legacy/stack.cjs +59 -0
  31. package/dist/cjs-legacy/trie.cjs +62 -0
  32. package/dist/esm/binary-tree.mjs +1897 -19
  33. package/dist/esm/graph.mjs +174 -0
  34. package/dist/esm/hash.mjs +33 -0
  35. package/dist/esm/heap.mjs +71 -0
  36. package/dist/esm/index.mjs +2383 -3
  37. package/dist/esm/linked-list.mjs +224 -2
  38. package/dist/esm/matrix.mjs +24 -0
  39. package/dist/esm/priority-queue.mjs +71 -0
  40. package/dist/esm/queue.mjs +221 -1
  41. package/dist/esm/stack.mjs +59 -0
  42. package/dist/esm/trie.mjs +62 -0
  43. package/dist/esm-legacy/binary-tree.mjs +1897 -19
  44. package/dist/esm-legacy/graph.mjs +174 -0
  45. package/dist/esm-legacy/hash.mjs +33 -0
  46. package/dist/esm-legacy/heap.mjs +71 -0
  47. package/dist/esm-legacy/index.mjs +2383 -3
  48. package/dist/esm-legacy/linked-list.mjs +224 -2
  49. package/dist/esm-legacy/matrix.mjs +24 -0
  50. package/dist/esm-legacy/priority-queue.mjs +71 -0
  51. package/dist/esm-legacy/queue.mjs +221 -1
  52. package/dist/esm-legacy/stack.mjs +59 -0
  53. package/dist/esm-legacy/trie.mjs +62 -0
  54. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  55. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
  66. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  67. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  68. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  69. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  70. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
  71. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  72. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  73. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  74. package/dist/types/data-structures/queue/deque.d.ts +90 -1
  75. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  76. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  77. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  78. package/dist/umd/data-structure-typed.js +2383 -3
  79. package/dist/umd/data-structure-typed.min.js +3 -3
  80. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
  81. package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
  82. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
  83. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
  84. package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
  85. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
  86. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
  87. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  88. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  89. package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
  90. package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
  91. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
  92. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
  93. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
  94. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
  95. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
  96. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
  97. package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
  98. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
  99. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
  100. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
  101. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
  102. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
  103. package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
  104. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
  105. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
  106. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
  107. package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
  108. package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
  109. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
  110. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
  111. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
  112. package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
  113. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
  114. package/jest.integration.config.js +1 -2
  115. package/package.json +51 -50
  116. package/src/common/error.ts +15 -32
  117. package/src/common/index.ts +0 -3
  118. package/src/data-structures/base/iterable-element-base.ts +32 -3
  119. package/src/data-structures/base/linear-base.ts +13 -36
  120. package/src/data-structures/binary-tree/avl-tree.ts +31 -493
  121. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
  122. package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
  123. package/src/data-structures/binary-tree/bst.ts +158 -1010
  124. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
  125. package/src/data-structures/binary-tree/segment-tree.ts +73 -333
  126. package/src/data-structures/binary-tree/tree-map.ts +462 -4749
  127. package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
  128. package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
  129. package/src/data-structures/binary-tree/tree-set.ts +437 -4443
  130. package/src/data-structures/graph/abstract-graph.ts +98 -167
  131. package/src/data-structures/graph/directed-graph.ts +137 -532
  132. package/src/data-structures/graph/map-graph.ts +0 -3
  133. package/src/data-structures/graph/undirected-graph.ts +132 -484
  134. package/src/data-structures/hash/hash-map.ts +154 -549
  135. package/src/data-structures/heap/heap.ts +200 -753
  136. package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
  137. package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
  138. package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
  139. package/src/data-structures/matrix/matrix.ts +179 -494
  140. package/src/data-structures/matrix/navigator.ts +0 -1
  141. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  142. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  143. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  144. package/src/data-structures/queue/deque.ts +241 -807
  145. package/src/data-structures/queue/queue.ts +102 -589
  146. package/src/data-structures/stack/stack.ts +76 -475
  147. package/src/data-structures/trie/trie.ts +98 -592
  148. package/src/types/common.ts +0 -10
  149. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  150. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  151. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  152. package/src/types/data-structures/hash/hash-map.ts +0 -3
  153. package/src/types/data-structures/hash/index.ts +0 -1
  154. package/src/types/data-structures/matrix/navigator.ts +0 -2
  155. package/src/types/utils/utils.ts +0 -7
  156. package/src/types/utils/validate-type.ts +0 -7
  157. package/src/utils/number.ts +0 -2
  158. 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,54 +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
- * @example
200
- * // Track queue length
201
- * const q = new Queue<number>();
202
- * console.log(q.length); // 0;
203
- * q.push(1);
204
- * q.push(2);
205
- * 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;
206
161
  */
207
-
208
162
  get length(): number {
209
163
  return this.elements.length - this._offset;
210
164
  }
@@ -213,71 +167,21 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
213
167
  * Get the first element (front) without removing it.
214
168
  * @remarks Time O(1), Space O(1)
215
169
  * @returns Front element or undefined.
216
-
217
-
218
-
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
- * @example
256
- * // View the front element
257
- * const q = new Queue<string>(['first', 'second', 'third']);
258
- * console.log(q.first); // 'first';
259
- * 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;
260
175
  */
261
-
262
176
  get first(): E | undefined {
263
177
  return this.length > 0 ? this.elements[this._offset] : undefined;
264
178
  }
265
179
 
266
- /**
267
- * Peek at the front element without removing it (alias for `first`).
268
- * @remarks Time O(1), Space O(1)
269
- * @returns Front element or undefined.
270
- */
271
- peek(): E | undefined {
272
- return this.first;
273
- }
274
-
275
180
  /**
276
181
  * Get the last element (back) without removing it.
277
182
  * @remarks Time O(1), Space O(1)
278
183
  * @returns Back element or undefined.
279
184
  */
280
-
281
185
  get last(): E | undefined {
282
186
  return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
283
187
  }
@@ -289,74 +193,42 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
289
193
  * @param elements - Array of elements to enqueue in order.
290
194
  * @returns A new queue populated from the array.
291
195
  */
292
-
293
196
  static fromArray<E>(elements: E[]): Queue<E> {
294
197
  return new Queue(elements);
295
198
  }
296
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
+
297
209
  /**
298
210
  * Check whether the queue is empty.
299
211
  * @remarks Time O(1), Space O(1)
300
212
  * @returns True if length is 0.
301
-
302
-
303
-
304
-
305
-
306
-
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
- * @example
341
- * // Queue for...of iteration and isEmpty check
342
- * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
343
- *
344
- * const elements: string[] = [];
345
- * for (const item of queue) {
346
- * elements.push(item);
347
- * }
348
- *
349
- * // Verify all elements are iterated in order
350
- * console.log(elements); // ['A', 'B', 'C', 'D'];
351
- *
352
- * // Process all elements
353
- * while (queue.length > 0) {
354
- * queue.shift();
355
- * }
356
- *
357
- * 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;
358
231
  */
359
-
360
232
  isEmpty(): boolean {
361
233
  return this.length === 0;
362
234
  }
@@ -366,57 +238,17 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
366
238
  * @remarks Time O(1), Space O(1)
367
239
  * @param element - Element to enqueue.
368
240
  * @returns True on success.
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
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
- * @example
409
- * // basic Queue creation and push operation
410
- * // Create a simple Queue with initial values
411
- * const queue = new Queue([1, 2, 3, 4, 5]);
412
- *
413
- * // Verify the queue maintains insertion order
414
- * console.log([...queue]); // [1, 2, 3, 4, 5];
415
- *
416
- * // Check length
417
- * 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;
418
251
  */
419
-
420
252
  push(element: E): boolean {
421
253
  this.elements.push(element);
422
254
  if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
@@ -429,7 +261,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
429
261
  * @param elements - Iterable of elements (or raw records if toElementFn is set).
430
262
  * @returns Array of per-element success flags.
431
263
  */
432
-
433
264
  pushMany(elements: Iterable<E> | Iterable<R>): boolean[] {
434
265
  const ans: boolean[] = [];
435
266
  for (const el of elements) {
@@ -443,61 +274,21 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
443
274
  * Dequeue one element from the front (amortized via offset).
444
275
  * @remarks Time O(1) amortized, Space O(1)
445
276
  * @returns Removed element or undefined.
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
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
- * @example
486
- * // Queue shift and peek operations
487
- * const queue = new Queue<number>([10, 20, 30, 40]);
488
- *
489
- * // Peek at the front element without removing it
490
- * console.log(queue.first); // 10;
491
- *
492
- * // Remove and get the first element (FIFO)
493
- * const first = queue.shift();
494
- * console.log(first); // 10;
495
- *
496
- * // Verify remaining elements and length decreased
497
- * console.log([...queue]); // [20, 30, 40];
498
- * 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;
499
291
  */
500
-
501
292
  shift(): E | undefined {
502
293
  if (this.length === 0) return undefined;
503
294
  const first = this.first;
@@ -511,49 +302,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
511
302
  * @remarks Time O(N), Space O(1)
512
303
  * @param element - Element to remove (strict equality via Object.is).
513
304
  * @returns True if an element was removed.
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
- * @example
551
- * // Remove specific element
552
- * const q = new Queue<number>([1, 2, 3, 2]);
553
- * q.delete(2);
554
- * 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;
555
310
  */
556
-
557
311
  delete(element: E): boolean {
558
312
  for (let i = this._offset; i < this.elements.length; i++) {
559
313
  if (Object.is(this.elements[i], element)) {
@@ -569,49 +323,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
569
323
  * @remarks Time O(1), Space O(1)
570
324
  * @param index - Zero-based index from the front.
571
325
  * @returns Element or undefined.
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
- * @example
609
- * // Access element by index
610
- * const q = new Queue<string>(['a', 'b', 'c']);
611
- * console.log(q.at(0)); // 'a';
612
- * 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';
613
331
  */
614
-
615
332
  at(index: number): E | undefined {
616
333
  if (index < 0 || index >= this.length) return undefined;
617
334
  return this._elements[this._offset + index];
@@ -623,7 +340,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
623
340
  * @param index - Zero-based index from the front.
624
341
  * @returns Removed element or undefined.
625
342
  */
626
-
627
343
  deleteAt(index: number): E | undefined {
628
344
  if (index < 0 || index >= this.length) return undefined;
629
345
  const gi = this._offset + index;
@@ -638,7 +354,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
638
354
  * @param newElement - Element to insert.
639
355
  * @returns True if inserted.
640
356
  */
641
-
642
357
  addAt(index: number, newElement: E): boolean {
643
358
  if (index < 0 || index > this.length) return false;
644
359
  this._elements.splice(this._offset + index, 0, newElement);
@@ -652,7 +367,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
652
367
  * @param newElement - New element to set.
653
368
  * @returns True if updated.
654
369
  */
655
-
656
370
  setAt(index: number, newElement: E): boolean {
657
371
  if (index < 0 || index >= this.length) return false;
658
372
  this._elements[this._offset + index] = newElement;
@@ -680,7 +394,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
680
394
  * @remarks Time O(N), Space O(N)
681
395
  * @returns This queue.
682
396
  */
683
-
684
397
  reverse(): this {
685
398
  this._elements = this.elements.slice(this._offset).reverse();
686
399
  this._offset = 0;
@@ -691,50 +404,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
691
404
  * Remove all elements and reset offset.
692
405
  * @remarks Time O(1), Space O(1)
693
406
  * @returns void
694
-
695
-
696
-
697
-
698
-
699
-
700
-
701
-
702
-
703
-
704
-
705
-
706
-
707
-
708
-
709
-
710
-
711
-
712
-
713
-
714
-
715
-
716
-
717
-
718
-
719
-
720
-
721
-
722
-
723
-
724
-
725
-
726
-
727
-
728
-
729
-
730
-
731
- * @example
732
- * // Remove all elements
733
- * const q = new Queue<number>([1, 2, 3]);
734
- * q.clear();
735
- * 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;
736
412
  */
737
-
738
413
  clear(): void {
739
414
  this._elements = [];
740
415
  this._offset = 0;
@@ -744,51 +419,14 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
744
419
  * Compact storage by discarding consumed head elements.
745
420
  * @remarks Time O(N), Space O(N)
746
421
  * @returns True when compaction performed.
747
-
748
-
749
-
750
-
751
-
752
-
753
-
754
-
755
-
756
-
757
-
758
-
759
-
760
-
761
-
762
-
763
-
764
-
765
-
766
-
767
-
768
-
769
-
770
-
771
-
772
-
773
-
774
-
775
-
776
-
777
-
778
-
779
-
780
-
781
-
782
-
783
- * @example
784
- * // Reclaim unused memory
785
- * const q = new Queue<number>([1, 2, 3, 4, 5]);
786
- * q.shift();
787
- * q.shift();
788
- * q.compact();
789
- * 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;
790
429
  */
791
-
792
430
  compact(): boolean {
793
431
  this._elements = this.elements.slice(this._offset);
794
432
  this._offset = 0;
@@ -803,20 +441,15 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
803
441
  * @param [items] - Elements to insert after `start`.
804
442
  * @returns A new queue containing the removed elements (typed as `this`).
805
443
  */
806
-
807
444
  override splice(start: number, deleteCount: number = 0, ...items: E[]): this {
808
445
  start = Math.max(0, Math.min(start, this.length));
809
446
  deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
810
-
811
447
  const gi = this._offset + start;
812
448
  const removedArray = this._elements.splice(gi, deleteCount, ...items);
813
-
814
449
  if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio) this.compact();
815
-
816
450
  const removed = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
817
451
  removed._setAutoCompactRatio(this._autoCompactRatio);
818
452
  removed.pushMany(removedArray);
819
-
820
453
  return removed as unknown as this;
821
454
  }
822
455
 
@@ -824,52 +457,14 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
824
457
  * Deep clone this queue and its parameters.
825
458
  * @remarks Time O(N), Space O(N)
826
459
  * @returns A new queue with the same content and options.
827
-
828
-
829
-
830
-
831
-
832
-
833
-
834
-
835
-
836
-
837
-
838
-
839
-
840
-
841
-
842
-
843
-
844
-
845
-
846
-
847
-
848
-
849
-
850
-
851
-
852
-
853
-
854
-
855
-
856
-
857
-
858
-
859
-
860
-
861
-
862
-
863
-
864
- * @example
865
- * // Create independent copy
866
- * const q = new Queue<number>([1, 2, 3]);
867
- * const copy = q.clone();
868
- * copy.shift();
869
- * console.log(q.length); // 3;
870
- * 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;
871
467
  */
872
-
873
468
  clone(): this {
874
469
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
875
470
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -883,50 +478,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
883
478
  * @param predicate - Predicate (element, index, queue) → boolean to keep element.
884
479
  * @param [thisArg] - Value for `this` inside the predicate.
885
480
  * @returns A new queue with kept elements.
886
-
887
-
888
-
889
-
890
-
891
-
892
-
893
-
894
-
895
-
896
-
897
-
898
-
899
-
900
-
901
-
902
-
903
-
904
-
905
-
906
-
907
-
908
-
909
-
910
-
911
-
912
-
913
-
914
-
915
-
916
-
917
-
918
-
919
-
920
-
921
-
922
-
923
- * @example
924
- * // Filter elements
925
- * const q = new Queue<number>([1, 2, 3, 4, 5]);
926
- * const evens = q.filter(x => x % 2 === 0);
927
- * 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;
928
486
  */
929
-
930
487
  filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
931
488
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
932
489
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -947,49 +504,12 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
947
504
  * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
948
505
  * @param [thisArg] - Value for `this` inside the callback.
949
506
  * @returns A new Queue with mapped elements.
950
-
951
-
952
-
953
-
954
-
955
-
956
-
957
-
958
-
959
-
960
-
961
-
962
-
963
-
964
-
965
-
966
-
967
-
968
-
969
-
970
-
971
-
972
-
973
-
974
-
975
-
976
-
977
-
978
-
979
-
980
-
981
-
982
-
983
-
984
-
985
-
986
- * @example
987
- * // Transform elements
988
- * const q = new Queue<number>([1, 2, 3]);
989
- * const doubled = q.map(x => x * 2);
990
- * 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];
991
512
  */
992
-
993
513
  map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: QueueOptions<EM, RM>, thisArg?: unknown): Queue<EM, RM> {
994
514
  const out = new (this.constructor as new (
995
515
  elements?: Iterable<EM> | Iterable<RM>,
@@ -1012,7 +532,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1012
532
  * @param [thisArg] - Value for `this` inside the callback.
1013
533
  * @returns A new queue with mapped elements (same element type).
1014
534
  */
1015
-
1016
535
  mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
1017
536
  const Ctor = this.constructor as new (
1018
537
  elements?: Iterable<E> | Iterable<R>,
@@ -1038,7 +557,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1038
557
  * @param value - New ratio to assign.
1039
558
  * @returns void
1040
559
  */
1041
-
1042
560
  protected _setAutoCompactRatio(value: number): void {
1043
561
  this._autoCompactRatio = value;
1044
562
  }
@@ -1048,7 +566,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1048
566
  * @remarks Time O(N), Space O(1)
1049
567
  * @returns Iterator of E.
1050
568
  */
1051
-
1052
569
  protected *_getIterator(): IterableIterator<E> {
1053
570
  for (let i = this._offset; i < this.elements.length; i++) yield this.elements[i];
1054
571
  }
@@ -1058,7 +575,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1058
575
  * @remarks Time O(N), Space O(1)
1059
576
  * @returns Iterator of E.
1060
577
  */
1061
-
1062
578
  protected *_getReverseIterator(): IterableIterator<E> {
1063
579
  for (let i = this.length - 1; i >= 0; i--) {
1064
580
  const cur = this.at(i);
@@ -1072,7 +588,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1072
588
  * @param [options] - Options forwarded to the constructor.
1073
589
  * @returns An empty like-kind queue instance.
1074
590
  */
1075
-
1076
591
  protected override _createInstance(options?: LinearBaseOptions<E, R>): this {
1077
592
  const Ctor = this.constructor as new (elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>) => this;
1078
593
  return new Ctor([], options as QueueOptions<E, R> | undefined);
@@ -1087,7 +602,6 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
1087
602
  * @param [options] - Options forwarded to the constructor.
1088
603
  * @returns A like-kind Queue instance.
1089
604
  */
1090
-
1091
605
  protected _createLike<EM = E, RM = R>(
1092
606
  elements: Iterable<EM> | Iterable<RM> = [],
1093
607
  options?: QueueOptions<EM, RM>
@@ -1113,7 +627,6 @@ export class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
1113
627
  * @remarks Time O(N), Space O(N)
1114
628
  * @returns A new queue with the same sequence of elements.
1115
629
  */
1116
-
1117
630
  override clone(): this {
1118
631
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1119
632
  for (const v of this) out.push(v);