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 Copy
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() Copy
const bytes = new NumericDeque(1_024, Uint8ClampedArray);bytes.push(0xFF);bytes.push(0x7f);bytes.push(0x00);bytes.shift()bytes.pop()bytes.shift()
The minimum size required by the deque. The actual capacity will be the next largest power-of-two.
The TypedArray backing store for the deque. Defaults to Float64Array, making it naturally compatible with the native Number type.
Get the current length of elements in the deque.
Returns true if the deque is empty
true
Push an element on the TAIL of the deque.
TAIL
The value to add to the TAIL of the deque.
Error if the deque is full.
Pop an element off the TAIL of the deque
The value at the TAIL of the deque.
Error if the deque is empty.
Prepend an element to the HEAD of the deque
HEAD
The value to append to the HEAD of the deque.
Shift an element off the HEAD of the deque.
The value at the HEAD of the deque.
Peek at the element at the HEAD of the deque.
Peek at the element at the TAIL of the deque.
Remove all elements from the deque.
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.
Example: A basic deque of numbers
Example: A deque of 1,024 bytes