A numeric-container backed by a TypedArray ring-buffer. Implements both stack (FILO) and queue (FIFO) methods, a.k.a. a deque.
Using npm:
$ npm install --save @4bitlabs/numeric-deque
Using yarn:
$ yarn add @4bitlabs/numeric-deque
Using pnpm:
$ pnpm add @4bitlabs/numeric-deque
Full documentation for the library can be found here
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
Note, the capacity given to the constructor is the minimum required capacity. The actual capacity of the deque may be larger.
const deque = new NumericDeque(10);
deque.push(1);
deque.push(2);
deque.push(3);
deque.push(4);
console.log(deque.peekHead()); // 1
console.log(deque.peekTail()); // 4
By the default, the ring-buffer is backed by Float64Array
, however, you can any of the numeric TypedArrays
for your
backing buffer:
import { NumericDeque } from '@4bitlabs/numeric-deque';
const bytes = new NumericDeque(300, Uint8ClampedArray);
const signedData = new NumericDeque(16, Int16Array);
const buffer = new NumericDeque(2_000, Uint32Array);