1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
203
204
205
206
207
208
209
210
211
212
213
214
215
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
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
784
785
786
787
788
789
790
791
792
793
794
795
796 |
x41
x41
x41
x62
x1
x1
x1
x1
x1
x1
x1
x62
x41
x41
x255
x255
x255
x255
x1137
x1137
x255
x255
x203
x203
x203
x203
x203
x203
x255
x1
x1
x1
x1
x1
x1
x1
x255
x255
x255
x255
x2
x2
x2
x85
x47
x47
x47
x47
x47
x47
x47
x47
x47
x47
x2
x2
x2
x68
x15
x15
x2
x2
x2
x255
x255
x142
x142
x255
x33
x33
x255
x670
x18
x1
x18
x670
x670
x670
x26
x2
x1
x2
x26
x26
x26
x670
x670
x255
x7
x28
x28
x28
x28
x28
x7
x7
x7
x7
x7
x7
x255
x3
x2
x2
x2
x2
x2
x2
x3
x255
x513
x366
x366
x366
x366
x366
x366
x513
x255
x9
x9
x8
x8
x9
x3
x9
x5
x5
x8
x8
x8
x9
x255
x7
x7
x5
x7
x255
x6
x6
x255
x3
x2
x3
x255
x3
x2
x3
x255
x167
x167
x164
x167
x255
x3
x3
x3
x3
x3
x255
x7
x7
x155
x155
x155
x17
x17
x23
x23
x7
x132
x132
x7
x7
x7
x255
x3
x3
x10
x10
x3
x3
x255
x255
x255
x255
x255
x14
x14
x14
x14
x14
x14
x14
x14
x2
x2
x2
x12
x12
x14
x4
x3
x3
x3
x3
x3
x3
x3
x3
x3
x1
x14
x8
x8
x14
x14
x14
x14
x14
x14
x24
x24
x9
x9
x9
x9
x14
x255
x50
x164
x164
x50
x255
x2
x7
x7
x2
x41
x41
x12
x12
x127
x127
x12
x12
x41
x8
x8
x8
x8
x41
x22
x64
x17
x17
x64
x5
x22
x41
x3
x2
x2
x2
x2
x3
x3
x3
x41
x5
x10
x10
x10
x10
x5
x5
x41
x5
x5
x41
x18
x18
x18
x41
x383
x383
x3
x3
x383
x41 |
|
// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.
const MIN_CAPACITY = 8;
const MIN_SHRINK_CAPACITY = 64;
/**
* Read-only view of a {@linkcode Deque}. Strips all mutation methods,
* following the `ReadonlyArray` / `ReadonlyMap` / `ReadonlySet` pattern.
* A `Deque<T>` is directly assignable to `ReadonlyDeque<T>`.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the values stored in the deque.
*/
export type ReadonlyDeque<T> = Pick<
Deque<T>,
| "length"
| "isEmpty"
| "peekFront"
| "peekBack"
| "at"
| "find"
| "findIndex"
| "toArray"
| typeof Symbol.iterator
| "reversed"
| typeof Symbol.toStringTag
>;
/** Round up to the smallest power of two >= n (at least {@linkcode MIN_CAPACITY}). */
function nextPowerOfTwo(n: number): number {
if (n <= MIN_CAPACITY) return MIN_CAPACITY;
n--;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return n + 1;
}
/**
* A double-ended queue backed by a ring buffer. Pushing, popping, and indexed
* access stay fast as the deque grows.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque<number>();
* deque.pushBack(1, 2, 3);
* assertEquals(deque.popFront(), 1);
* assertEquals(deque.length, 2);
* assertEquals([...deque], [2, 3]);
* ```
*
* @typeParam T The type of the values stored in the deque.
*/
export class Deque<T> implements Iterable<T>, ReadonlyDeque<T> {
#buffer: (T | undefined)[];
#head: number;
#length: number;
/** Always `#capacity - 1`. Used to wrap indices via `& #mask`. */
#mask: number;
get #capacity(): number {
return this.#mask + 1;
}
/**
* Creates an empty deque, optionally populated from an iterable.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Creating an empty deque
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque<number>();
* assertEquals(deque.length, 0);
* ```
*
* @example Creating a deque from an iterable
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals([...deque], [1, 2, 3]);
* ```
*
* @param source An optional iterable to populate the deque.
*/
constructor(source?: Iterable<T>) {
if (source === undefined || source === null) {
this.#buffer = new Array(MIN_CAPACITY);
this.#head = 0;
this.#length = 0;
this.#mask = MIN_CAPACITY - 1;
return;
}
if (source instanceof Deque) {
const capacity = nextPowerOfTwo(source.#length);
this.#buffer = Deque.#copyBuffer(source, capacity);
this.#head = 0;
this.#length = source.#length;
this.#mask = capacity - 1;
return;
}
if (
typeof source !== "object" && typeof source !== "string" ||
!(Symbol.iterator in Object(source))
) {
throw new TypeError(
"Cannot construct a Deque: the 'source' parameter is not iterable, did you mean to call Deque.from?",
);
}
// Fast path: copy array directly without iterator protocol overhead
if (Array.isArray(source)) {
const len = source.length;
const capacity = nextPowerOfTwo(len);
const buffer = new Array(capacity);
for (let i = 0; i < len; i++) buffer[i] = source[i];
this.#buffer = buffer;
this.#head = 0;
this.#length = len;
this.#mask = capacity - 1;
return;
}
const items = [...source];
const capacity = nextPowerOfTwo(items.length);
this.#buffer = new Array(capacity);
for (let i = 0; i < items.length; i++) {
this.#buffer[i] = items[i];
}
this.#head = 0;
this.#length = items.length;
this.#mask = capacity - 1;
}
/**
* The number of elements in the deque.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Getting the length
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals(deque.length, 3);
* ```
*
* @returns The number of elements in the deque.
*/
get length(): number {
return this.#length;
}
/**
* Checks if the deque contains no elements.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Checking if the deque is empty
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque<number>();
* assertEquals(deque.isEmpty(), true);
*
* deque.pushBack(1);
* assertEquals(deque.isEmpty(), false);
* ```
*
* @returns `true` if the deque is empty, otherwise `false`.
*/
isEmpty(): boolean {
return this.#length === 0;
}
/**
* Append one or more values to the back of the deque.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Pushing values to the back
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque<number>();
* deque.pushBack(1, 2, 3);
* assertEquals([...deque], [1, 2, 3]);
* ```
*
* @param value The first value to append.
* @param rest Additional values to append.
* @returns The new length of the deque.
*/
pushBack(value: T, ...rest: T[]): number {
if (this.#length === this.#capacity) {
if (this.#head === 0) this.#growWithoutCopying();
else this.#grow();
}
this.#buffer[(this.#head + this.#length) & this.#mask] = value;
this.#length++;
for (let i = 0; i < rest.length; i++) {
if (this.#length === this.#capacity) {
if (this.#head === 0) this.#growWithoutCopying();
else this.#grow();
}
this.#buffer[(this.#head + this.#length) & this.#mask] = rest[i]!;
this.#length++;
}
return this.#length;
}
/**
* Prepend one or more values to the front of the deque. Values are inserted
* in argument order, so `pushFront(1, 2, 3)` results in front-to-back order
* `[1, 2, 3, ...existing]`.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Pushing values to the front
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([4, 5]);
* deque.pushFront(1, 2, 3);
* assertEquals([...deque], [1, 2, 3, 4, 5]);
* ```
*
* @param value The first value to prepend.
* @param rest Additional values to prepend.
* @returns The new length of the deque.
*/
pushFront(value: T, ...rest: T[]): number {
for (let i = rest.length - 1; i >= 0; i--) {
if (this.#length === this.#capacity) this.#grow();
this.#head = (this.#head - 1) & this.#mask;
this.#buffer[this.#head] = rest[i]!;
this.#length++;
}
if (this.#length === this.#capacity) this.#grow();
this.#head = (this.#head - 1) & this.#mask;
this.#buffer[this.#head] = value;
this.#length++;
return this.#length;
}
/**
* Remove and return the back element, or `undefined` if the deque is empty.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Popping from the back
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals(deque.popBack(), 3);
* assertEquals([...deque], [1, 2]);
* ```
*
* @returns The back element, or `undefined` if empty.
*/
popBack(): T | undefined {
if (this.#length === 0) return undefined;
this.#length--;
const index = (this.#head + this.#length) & this.#mask;
const value = this.#buffer[index];
this.#buffer[index] = undefined;
this.#maybeShrink();
return value;
}
/**
* Remove and return the front element, or `undefined` if the deque is empty.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Popping from the front
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals(deque.popFront(), 1);
* assertEquals([...deque], [2, 3]);
* ```
*
* @returns The front element, or `undefined` if empty.
*/
popFront(): T | undefined {
if (this.#length === 0) return undefined;
const value = this.#buffer[this.#head];
this.#buffer[this.#head] = undefined;
this.#head = (this.#head + 1) & this.#mask;
this.#length--;
this.#maybeShrink();
return value;
}
/**
* Remove and return the first element matching the predicate, scanning from
* front to back. The gap is closed by shifting whichever side (front or back)
* has fewer elements to move, so removals near either end are fast.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Removing the first even number
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3, 4]);
* assertEquals(deque.removeFirst((v) => v % 2 === 0), 2);
* assertEquals([...deque], [1, 3, 4]);
* ```
*
* @param predicate A function called for each element with its index. The
* first element for which it returns `true` is removed and returned.
* @returns The removed element, or `undefined` if no match was found.
*/
removeFirst(predicate: (value: T, index: number) => boolean): T | undefined {
const i = this.#findIndex(predicate);
if (i === -1) return undefined;
const val = this.#buffer[(this.#head + i) & this.#mask] as T;
const closerToFront = i < this.#length - i - 1;
if (closerToFront) {
this.#closeGapFromFront(i);
} else {
this.#closeGapFromBack(i);
}
this.#length--;
this.#maybeShrink();
return val;
}
/**
* Return the first element matching the predicate, scanning from front to
* back, without removing it. Returns `undefined` if no match is found.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Finding the first even number
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3, 4]);
* assertEquals(deque.find((v) => v % 2 === 0), 2);
* assertEquals(deque.find((v) => v > 10), undefined);
* ```
*
* @param predicate A function called for each element with its index. The
* first element for which it returns `true` is returned.
* @returns The first matching element, or `undefined` if no match was found.
*/
find(predicate: (value: T, index: number) => boolean): T | undefined {
const i = this.#findIndex(predicate);
if (i === -1) return undefined;
return this.#buffer[(this.#head + i) & this.#mask] as T;
}
/**
* Return the index of the first element matching the predicate, scanning
* from front to back. Returns `-1` if no match is found.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Finding the index of the first even number
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3, 4]);
* assertEquals(deque.findIndex((v) => v % 2 === 0), 1);
* assertEquals(deque.findIndex((v) => v > 10), -1);
* ```
*
* @param predicate A function called for each element with its index. The
* index of the first element for which it returns `true` is returned.
* @returns The index of the first matching element, or `-1` if not found.
*/
findIndex(predicate: (value: T, index: number) => boolean): number {
return this.#findIndex(predicate);
}
/**
* Return the front element without removing it, or `undefined` if the deque
* is empty.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Peeking at the front
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals(deque.peekFront(), 1);
* assertEquals(deque.length, 3);
* ```
*
* @returns The front element, or `undefined` if empty.
*/
peekFront(): T | undefined {
if (this.#length === 0) return undefined;
return this.#buffer[this.#head];
}
/**
* Return the back element without removing it, or `undefined` if the deque
* is empty.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Peeking at the back
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals(deque.peekBack(), 3);
* assertEquals(deque.length, 3);
* ```
*
* @returns The back element, or `undefined` if empty.
*/
peekBack(): T | undefined {
if (this.#length === 0) return undefined;
return this.#buffer[(this.#head + this.#length - 1) & this.#mask];
}
/**
* Return the element at the given index (0-based from front). Negative
* indices count from the back (`-1` is the last element). Returns `undefined`
* for out-of-range indices.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Accessing elements by index
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([10, 20, 30, 40]);
* assertEquals(deque.at(0), 10);
* assertEquals(deque.at(-1), 40);
* assertEquals(deque.at(99), undefined);
* ```
*
* @param index The zero-based index. Negative values count from the back.
* @returns The element at the index, or `undefined` if out of range.
*/
at(index: number): T | undefined {
if (index < 0) index += this.#length;
if (index < 0 || index >= this.#length) return undefined;
return this.#buffer[(this.#head + index) & this.#mask];
}
/**
* Remove all elements and release the backing buffer.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Clearing the deque
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* deque.clear();
* assertEquals(deque.length, 0);
* assertEquals(deque.isEmpty(), true);
* ```
*/
clear(): void {
this.#buffer = new Array(MIN_CAPACITY);
this.#head = 0;
this.#length = 0;
this.#mask = MIN_CAPACITY - 1;
}
/**
* Keep only the elements for which the predicate returns `true`, removing
* the rest in place. Equivalent to an in-place
* {@linkcode Array.prototype.filter}. The predicate is called once per
* element in front-to-back order.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Retaining only odd numbers
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3, 4, 5]);
* deque.retain((v) => v % 2 !== 0);
* assertEquals([...deque], [1, 3, 5]);
* ```
*
* @param predicate A function called for each element with its index.
* Elements for which it returns `false` are removed.
*/
retain(predicate: (value: T, index: number) => boolean): void {
let write = 0;
for (let read = 0; read < this.#length; read++) {
const val = this.#buffer[(this.#head + read) & this.#mask] as T;
if (!predicate(val, read)) continue;
if (write !== read) {
this.#buffer[(this.#head + write) & this.#mask] = val;
}
write++;
}
for (let i = write; i < this.#length; i++) {
this.#buffer[(this.#head + i) & this.#mask] = undefined;
}
this.#length = write;
this.#maybeShrink();
}
/**
* Return a shallow copy of the deque's contents as an array, in
* front-to-back order.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Converting to an array
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals(deque.toArray(), [1, 2, 3]);
* ```
*
* @returns An array containing the deque's elements in order.
*/
toArray(): T[] {
const result = new Array<T>(this.#length);
for (let i = 0; i < this.#length; i++) {
result[i] = this.#buffer[(this.#head + i) & this.#mask] as T;
}
return result;
}
/**
* Create a new deque from an array-like, iterable, or existing deque.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Creating a deque from an array
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = Deque.from([1, 2, 3]);
* assertEquals([...deque], [1, 2, 3]);
* ```
*
* @example Creating a deque from an existing deque
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const original = new Deque([1, 2, 3]);
* const copy = Deque.from(original);
* assertEquals([...copy], [1, 2, 3]);
* ```
*
* @typeParam T The type of the values in the passed collection.
* @param collection An array-like, iterable, or existing deque.
* @returns A new deque containing the values from the collection.
*/
static from<T>(
collection: ArrayLike<T> | Iterable<T> | Deque<T>,
): Deque<T>;
/**
* Create a new deque from an array-like, iterable, or existing deque, with
* a mapping function applied to each element.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Creating a deque with a mapping function
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = Deque.from([1, 2, 3], { map: (v) => v * 10 });
* assertEquals([...deque], [10, 20, 30]);
* ```
*
* @typeParam T The type of the values in the passed collection.
* @typeParam U The type of the values stored in the new deque.
* @typeParam V The type of the `this` value when calling the mapping function. Defaults to `undefined`.
* @param collection An array-like, iterable, or existing deque.
* @param options Options with a map function and optional thisArg.
* @returns A new deque containing the mapped values from the collection.
*/
static from<T, U, V = undefined>(
collection: ArrayLike<T> | Iterable<T> | Deque<T>,
options: {
map: (value: T, index: number) => U;
thisArg?: V;
},
): Deque<U>;
static from<T, U, V>(
collection: ArrayLike<T> | Iterable<T> | Deque<T>,
options?: {
map?: (value: T, index: number) => U;
thisArg?: V;
},
): Deque<U> {
if (
collection === null || collection === undefined ||
typeof collection !== "object" && typeof collection !== "string" ||
!(
Symbol.iterator in Object(collection) ||
"length" in Object(collection)
)
) {
throw new TypeError(
"Cannot create a Deque: the 'collection' parameter is not iterable or array-like",
);
}
const result = new Deque<U>();
let unmappedValues: ArrayLike<T> | Iterable<T>;
if (collection instanceof Deque) {
if (!options?.map) {
const capacity = nextPowerOfTwo(collection.#length);
result.#buffer = Deque.#copyBuffer(
collection,
capacity,
) as (U | undefined)[];
result.#head = 0;
result.#length = collection.#length;
result.#mask = capacity - 1;
return result;
}
unmappedValues = collection.toArray();
} else {
unmappedValues = collection;
}
const mapped: U[] = options?.map
? Array.from(unmappedValues, options.map, options.thisArg)
: Array.from(unmappedValues as unknown as Iterable<U>);
const capacity = nextPowerOfTwo(mapped.length);
result.#buffer = new Array(capacity);
for (let i = 0; i < mapped.length; i++) {
result.#buffer[i] = mapped[i];
}
result.#head = 0;
result.#length = mapped.length;
result.#mask = capacity - 1;
return result;
}
/**
* Iterate over the deque's elements from front to back. Non-destructive
* (unlike {@linkcode BinaryHeap}).
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Iterating over the deque
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals([...deque], [1, 2, 3]);
* ```
*
* @returns An iterator yielding elements from front to back.
*/
*[Symbol.iterator](): IterableIterator<T> {
for (let i = 0; i < this.#length; i++) {
yield this.#buffer[(this.#head + i) & this.#mask] as T;
}
}
/**
* Iterate over the deque's elements from back to front.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Iterating in reverse
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque([1, 2, 3]);
* assertEquals([...deque.reversed()], [3, 2, 1]);
* ```
*
* @returns An iterator yielding elements from back to front.
*/
*reversed(): IterableIterator<T> {
for (let i = this.#length - 1; i >= 0; i--) {
yield this.#buffer[(this.#head + i) & this.#mask] as T;
}
}
/**
* The string tag used by `Object.prototype.toString`.
*
* @example Usage
* ```ts
* import { Deque } from "@std/data-structures/unstable-deque";
* import { assertEquals } from "@std/assert";
*
* const deque = new Deque<number>();
* assertEquals(deque[Symbol.toStringTag], "Deque");
* ```
*/
readonly [Symbol.toStringTag] = "Deque" as const;
static #copyBuffer<T>(source: Deque<T>, capacity: number): (T | undefined)[] {
const buffer = new Array<T | undefined>(capacity);
for (let i = 0; i < source.#length; i++) {
buffer[i] = source.#buffer[(source.#head + i) & source.#mask];
}
return buffer;
}
#realloc(newCapacity: number): void {
this.#buffer = Deque.#copyBuffer(this, newCapacity);
this.#head = 0;
this.#mask = newCapacity - 1;
}
#findIndex(predicate: (value: T, index: number) => boolean): number {
for (let i = 0; i < this.#length; i++) {
if (predicate(this.#buffer[(this.#head + i) & this.#mask] as T, i)) {
return i;
}
}
return -1;
}
/** Close the gap at `i` by shifting elements before it one slot toward the back. */
#closeGapFromFront(i: number): void {
for (let j = i; j > 0; j--) {
const dst = (this.#head + j) & this.#mask;
const src = (this.#head + j - 1) & this.#mask;
this.#buffer[dst] = this.#buffer[src];
}
this.#buffer[this.#head] = undefined;
this.#head = (this.#head + 1) & this.#mask;
}
/** Close the gap at `i` by shifting elements after it one slot toward the front. */
#closeGapFromBack(i: number): void {
for (let j = i; j < this.#length - 1; j++) {
const dst = (this.#head + j) & this.#mask;
const src = (this.#head + j + 1) & this.#mask;
this.#buffer[dst] = this.#buffer[src];
}
this.#buffer[(this.#head + this.#length - 1) & this.#mask] = undefined;
}
#grow(): void {
this.#realloc(this.#capacity * 2);
}
/** Only valid when head is 0 (elements are already packed at the start). */
#growWithoutCopying(): void {
this.#buffer.length = this.#capacity * 2;
this.#mask = this.#buffer.length - 1;
}
#maybeShrink(): void {
const capacity = this.#capacity;
if (capacity > MIN_SHRINK_CAPACITY && this.#length < capacity / 4) {
this.#realloc(capacity / 2);
}
}
}
|