class ArrayDeque<T> implements Iterable<T>
A double-ended queue backed by a circular buffer.
ArrayDeques support amortized constant-time insertion and removal at both ends, and constant-time access to elements by index. Iterating over an ArrayDeque returns the elements sequentially.
new ArrayDeque(capacity?: number)
Constructs an empty ArrayDeque, optionally specifying the initial capacity.
static readonly MAX_CAPACITY = Math.pow(2, 31)
The maximum number of elements that an ArrayDeque can contain.
readonly size: number
The number of elements in the ArrayDeque.
at(index: number): T | undefined
Returns the element at the specified index. See ArrayDeque.get.
clone(): ArrayDeque<T>
Returns a shallow copy of this ArrayDeque with the same capacity.
dequeue(): T | undefined
Removes and returns the first element. See ArrayDeque.shift.
dequeueUnchecked(): T
Removes and returns the first element, assuming the ArrayDeque is non-empty. See ArrayDeque.shiftUnchecked.
enqueue(value: T): void
Inserts a new element at the back. See ArrayDeque.push.
ensureCapacity(capacity: number): void
Ensures that the ArrayDeque can grow to this size without additional allocations in the future.
first(): T | undefined
Returns the first element without removing it.
Returns undefined if the ArrayDeque is empty. If you know the ArrayDeque is non-empty, consider ArrayDeque.firstUnchecked, which excludes undefined from the return type.
firstUnchecked(): T
Returns the first element without removing it, assuming the ArrayDeque is non-empty. See ArrayDeque.first.
If the ArrayDeque is empty, the behavior is not defined.
get(index: number): T | undefined
Returns the element at the specified index. Negative indices count from the end, like Array.at.
Returns undefined if the index is out of range. If you know the index is in range, consider ArrayDeque.getUnchecked or ArrayDeque.getNonNegativeUnchecked, which exclude undefined from the return type and might be faster.
If the index is not an integer, the behavior is not defined.
getNonNegativeUnchecked(index: number): T
Returns the element at the specified index, assuming that the index is
in [0, size)
. See ArrayDeque.get.
If the index is not an integer in the required range, the behavior is not defined.
getUnchecked(index: number): T
Returns the element at the specified index, assuming that the index is
in [-size, size)
. See ArrayDeque.get.
If the index is not an integer in the required range, the behavior is not defined.
last(): T | undefined
Returns the last element without removing it.
Returns undefined if the ArrayDeque is empty. If you know the ArrayDeque is non-empty, consider ArrayDeque.lastUnchecked, which excludes undefined from the return type.
lastUnchecked(): T
Returns the last element without removing it, assuming the ArrayDeque is non-empty. See ArrayDeque.last.
If the ArrayDeque is empty, the behavior is not defined.
pop(): T | undefined
Removes and returns the last element.
Returns undefined if the ArrayDeque is empty. If you know the ArrayDeque is non-empty, consider ArrayDeque.popUnchecked, which excludes undefined from the return type and might be faster.
popUnchecked(): T
Removes and returns the last element, assuming the ArrayDeque is non-empty.
This might be faster than ArrayDeque.pop, but if the ArrayDeque is empty, the behavior is not defined.
push(value: T): void
Inserts a new element at the back.
Throws RangeError if the ArrayDeque's size would exceed ArrayDeque.MAX_CAPACITY.
Replaces the element at the specified index. Negative indices count from the end, like Array.at.
Throws RangeError if the index is out of range. If you know the index is in range, consider ArrayDeque.setUnchecked or ArrayDeque.setNonNegativeUnchecked, which might be faster.
If the index is not an integer, the behavior is not defined.
setNonNegativeUnchecked(index: number, value: T): void
Replaces the element at the specified index, assuming that the index is in
[0, size)
. See ArrayDeque.set.
If the index is not an integer in the required range, the behavior is not defined.
setUnchecked(index: number, value: T): void
Replaces the element at the specified index, assuming that the index is in
[-size, size)
. See ArrayDeque.set.
If the index is not an integer in the required range, the behavior is not defined.
shift(): T | undefined
Removes and returns the first element.
Returns undefined if the ArrayDeque is empty. If you know the ArrayDeque is non-empty, consider ArrayDeque.shiftUnchecked, which excludes undefined from the return type and might be faster.
shiftUnchecked(): T
Removes and returns the first element, assuming the ArrayDeque is non-empty.
This might be faster than ArrayDeque.shift, but if the ArrayDeque is empty, the behavior is not defined.
toArray(): T[]
Returns an array containing the elements of this ArrayDeque in the same order.
toJSON(): T[]
Returns an array containing the elements of this ArrayDeque in the same order. Equivalent to ArrayDeque.toArray.
unshift(value: T): void
Inserts a new element at the front.
Throws RangeError if the ArrayDeque's size would exceed ArrayDeque.MAX_CAPACITY.
[Symbol.iterator](): IterableIterator<T>
Returns an iterator that returns elements sequentially.
The ArrayDeque must not be modified while the iterator is in use. Otherwise, the behavior of the iterator is not defined.
class BlockingQueue<T> implements AsyncIterable<T>
A queue that supports waiting for elements to become available.
This can be used to implement the producer-consumer pattern, with producers calling BlockingQueue.enqueue and consumers calling BlockingQueue.dequeue.
Iterating over a BlockingQueue with for await...of dequeues and returns elements in the order they were inserted. This loop will run forever unless you break out of it, because there is no way to know whether another element will be inserted in the future.
new BlockingQueue();
Constructs an empty BlockingQueue.
dequeue(): Promise<T>
The n'th call to this method returns a Promise that will resolve to the n'th value enqueued.
If the n'th value has already been enqueued, the Promise resolves immediately. Otherwise, awaiting the Promise will block until the corresponding value becomes available. If the value is never enqueued, the Promise will never resolve either.
enqueue(value: T): void
Inserts an element at the tail of the queue.
[Symbol.asyncIterator](): AsyncIterableIterator<T>
Returns an async iterator whose next()
method returns the Promise that
would be returned by BlockingQueue.dequeue.