JSPM

@datastructures-js/deque

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3688
  • Score
    100M100P100Q128877F
  • License MIT

A performant double-ended queue (deque) implementation in javascript.

Package Exports

  • @datastructures-js/deque
  • @datastructures-js/deque/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@datastructures-js/deque) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@datastructures-js/deque

npm npm npm

A performant double-ended queue (deque) implementation in javascript.

Contents

Install

npm install --save @datastructures-js/deque

require

const { Deque } = require('@datastructures-js/deque');

import

import { Deque } from '@datastructures-js/deque';

API

constructor

JS
// empty queue
const deque = new Deque();

// from an array
const deque = new Deque([1, 2, 3]);
TS
// empty queue
const deque = new Deque<number>();

// from an array
const deque = new Deque<number>([1, 2, 3]);

Deque.fromArray(elements)

JS
// empty queue
const deque = Deque.fromArray([]);

// with elements
const list = [10, 3, 8, 40, 1];
const deque = Deque.fromArray(list);

// If the list should not be mutated, use a copy of it.
const deque = Deque.fromArray(list.slice());
TS
// empty queue
const deque = Deque.fromArray<number>([]);

// with elements
const list = [10, 3, 8, 40, 1];
const deque = Deque.fromArray<number>(list);

.pushFront(element)

adds an element at the front of the queue.

params return runtime
element: T Deque<T> O(1)
deque.pushFront(30).pushFront(20).pushFront(10);

.pushBack(element)

adds an element at the back of the queue.

params return runtime
element: T Deque<T> O(1)
deque.pushBack(40).pushBack(50).pushBack(60);

.front()

peeks on the front element of the queue.

return runtime
T O(1)
console.log(deque.front()); // 10

.back()

peeks on the back element of the queue.

return runtime
T O(1)
console.log(deque.back()); // 60

.popFront()

removes the front element in the queue. It uses a pointer to get the front element and only remove popped elements when reaching half size of the queue.

return runtime
T O(n*log(n))
console.log(deque.popFront()); // 10
console.log(deque.front()); // 20

.popBack()

removes the back element in the queue. It uses a pointer to get the back element and only remove popped elements when reaching half size of the queue.

return runtime
T O(n*log(n))
console.log(deque.popBack()); // 60
console.log(deque.back()); // 50

.isEmpty()

checks if the queue is empty.

return runtime
boolean O(1)
console.log(deque.isEmpty()); // false

.size()

returns the number of elements in the queue.

return runtime
number O(1)
console.log(deque.size()); // 4

.clone()

creates a shallow copy of the queue.

return runtime
Queue<T> O(n)
const deque2 = Deque.fromArray([{ id: 2 }, { id: 4 } , { id: 8 }]);
const clone =  deque2.clone();

clone.popFront();

console.log(deque2.front()); // { id: 2 }
console.log(clone.front()); // { id: 4 }

.toArray()

returns a copy of the remaining elements as an array.

return runtime
T[] O(n)
console.log(deque.toArray()); // [ 20, 30, 40, 50 ]

.clear()

clears all elements from the queue.

runtime
O(1)
deque.clear();
deque.size(); // 0

Build

grunt build

License

The MIT License. Full License is here