npm.io
2.0.0 • Published 3d ago

@4bitlabs/numeric-deque

Licence
ISC
Version
2.0.0
Deps
0
Size
15 kB
Vulns
0
Weekly
0

@4bitlabs/numeric-deque License NPM Version NPM Downloads

A numeric-container backed by a TypedArray ring-buffer. Implements both stack (FILO) and queue (FIFO) methods, a.k.a. a deque.

Installation

Using npm:

$ npm install --save @4bitlabs/numeric-deque

Using yarn:

$ yarn add @4bitlabs/numeric-deque

Using pnpm:

$ pnpm add @4bitlabs/numeric-deque

Documentation

Full documentation for the library can be found here

Basic Usage

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.

Peek
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

TypedArray backed deques

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);

License

ISC

Keywords