A double-ended queue, or deque, is a data-structure that support both stack and queue semantics. This NumericDeque is implemented as fixed-length circular buffer that is backed by a TypedArray for fast, efficient performance.

import { NumericDeque } from '@4bitlabs/numeric-deque';

// Create a numeric deque to hold *atleast* 10 items.
const deque = new NumericDeque(10);

deque.push(2);
deque.push(3);
deque.push(4);
deque.unshift(1);

while (!deque.isEmpty()) {
console.log(deque.shift());
}

// Output: 1, 2, 3, 4
const bytes = new NumericDeque(1_024, Uint8ClampedArray);
bytes.push(0xFF);
bytes.push(0x7f);
bytes.push(0x00);

bytes.shift()
bytes.pop()
bytes.shift()

Constructors

Accessors

  • get length(): number
  • Get the current length of elements in the deque.

    Returns number

Methods

  • Returns true if the deque is empty

    Returns boolean

  • Push an element on the TAIL of the deque.

    Parameters

    • value: number

      The value to add to the TAIL of the deque.

    Returns void

    Error if the deque is full.

  • Pop an element off the TAIL of the deque

    Returns number

    The value at the TAIL of the deque.

    Error if the deque is empty.

  • Prepend an element to the HEAD of the deque

    Parameters

    • value: number

      The value to append to the HEAD of the deque.

    Returns void

    Error if the deque is full.

  • Shift an element off the HEAD of the deque.

    Returns number

    The value at the HEAD of the deque.

    Error if the deque is empty.

  • Peek at the element at the HEAD of the deque.

    Returns number

    The value at the HEAD of the deque.

    Error if the deque is empty.

  • Peek at the element at the TAIL of the deque.

    Returns number

    The value at the TAIL of the deque.

    Error if the deque is empty.