Skip to content

Useful queue data structures for JavaScript

License

Notifications You must be signed in to change notification settings

justinyaodu/superlative-queues

Repository files navigation

API Reference


ArrayDeque

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.

ArrayDeque.constructor

new ArrayDeque(capacity?: number)

Constructs an empty ArrayDeque, optionally specifying the initial capacity.

ArrayDeque.MAX_CAPACITY

static readonly MAX_CAPACITY = Math.pow(2, 31)

The maximum number of elements that an ArrayDeque can contain.

ArrayDeque.size

readonly size: number

The number of elements in the ArrayDeque.

ArrayDeque.at

at(index: number): T | undefined

Returns the element at the specified index. See ArrayDeque.get.

ArrayDeque.clone

clone(): ArrayDeque<T>

Returns a shallow copy of this ArrayDeque with the same capacity.

ArrayDeque.dequeue

dequeue(): T | undefined

Removes and returns the first element. See ArrayDeque.shift.

ArrayDeque.dequeueUnchecked

dequeueUnchecked(): T

Removes and returns the first element, assuming the ArrayDeque is non-empty. See ArrayDeque.shiftUnchecked.

ArrayDeque.enqueue

enqueue(value: T): void

Inserts a new element at the back. See ArrayDeque.push.

ArrayDeque.ensureCapacity

ensureCapacity(capacity: number): void

Ensures that the ArrayDeque can grow to this size without additional allocations in the future.

ArrayDeque.first

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.

ArrayDeque.firstUnchecked

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.

ArrayDeque.get

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.

ArrayDeque.getNonNegativeUnchecked

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.

ArrayDeque.getUnchecked

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.

ArrayDeque.last

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.

ArrayDeque.lastUnchecked

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.

ArrayDeque.pop

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.

ArrayDeque.popUnchecked

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.

ArrayDeque.push

push(value: T): void

Inserts a new element at the back.

Throws RangeError if the ArrayDeque's size would exceed ArrayDeque.MAX_CAPACITY.

ArrayDeque.set

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.

ArrayDeque.setNonNegativeUnchecked

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.

ArrayDeque.setUnchecked

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.

ArrayDeque.shift

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.

ArrayDeque.shiftUnchecked

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.

ArrayDeque.toArray

toArray(): T[]

Returns an array containing the elements of this ArrayDeque in the same order.

ArrayDeque.toJSON

toJSON(): T[]

Returns an array containing the elements of this ArrayDeque in the same order. Equivalent to ArrayDeque.toArray.

ArrayDeque.unshift

unshift(value: T): void

Inserts a new element at the front.

Throws RangeError if the ArrayDeque's size would exceed ArrayDeque.MAX_CAPACITY.

ArrayDeque[Symbol.iterator]

[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.


BlockingQueue

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.

BlockingQueue.constructor

new BlockingQueue();

Constructs an empty BlockingQueue.

BlockingQueue.dequeue

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.

BlockingQueue.enqueue

enqueue(value: T): void

Inserts an element at the tail of the queue.

BlockingQueue[Symbol.asyncIterator]

[Symbol.asyncIterator](): AsyncIterableIterator<T>

Returns an async iterator whose next() method returns the Promise that would be returned by BlockingQueue.dequeue.

About

Useful queue data structures for JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published