Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

priorityqueuejs

janogonzalez1.6mMIT2.0.0TypeScript support: definitely-typed

a simple priority queue data structure

heap, priority, queue, priority queue, data structure

readme

priorityqueue.js

Build Status

A simple priority queue data structure for Node.js.

Installation

$ npm install priorityqueuejs

Example

var PriorityQueue = require('priorityqueuejs');

var queue = new PriorityQueue(function(a, b) {
  return a.cash - b.cash;
});

queue.enq({ cash: 250, name: 'Valentina' });
queue.enq({ cash: 300, name: 'Jano' });
queue.enq({ cash: 150, name: 'Fran' });
queue.size(); // 3
queue.peek(); // { cash: 300, name: 'Jano' }
queue.deq(); // { cash: 300, name: 'Jano' }
queue.size(); // 2

API

PriorityQueue()

Initializes a new empty PriorityQueue wich uses .DEFAULT_COMPARATOR() as the comparator function for its elements.

PriorityQueue(comparator)

Initializes a new empty PriorityQueue with uses the given comparator(a, b) function as the comparator for its elements.

The comparator function must return a positive number when a > b, 0 when a == b and a negative number when a < b.

PriorityQueue.DEFAULT_COMPARATOR(a, b)

Compares two Number or String objects.

PriorityQueue#deq()

Dequeues the top element of the priority queue. Throws an Error when the queue is empty.

PriorityQueue#enq(element)

Enqueues the element at the priority queue and returns its new size.

PriorityQueue#forEach(fn)

Executes fn on each element. Just be careful to not modify the priorities, since the queue won't reorder itself.

PriorityQueue#isEmpty()

Returns whether the priority queue is empty or not.

PriorityQueue#peek()

Peeks at the top element of the priority queue. Throws an Error when the queue is empty.

PriorityQueue#size()

Returns the size of the priority queue.

Testing

$ npm install
$ npm test

Licence

MIT

changelog

2.0.0 / 2020-05-07

  • Stop supporting component and browser builds.

1.0.0 / 2015-02-23

  • Respect queue behavior when elements have the same priority, thank you @xgbuils.
  • Fixes default comparator to also work with Number objects.

0.2.0 / 2014-02-06

  • Add #forEach(fn) method, thank you @kessler.
  • Now you can find a pre-built file for people who don't use component when developing for the browser.

0.1.0 / 2013-03-03

  • Change #empty() for #isEmpty().

0.0.2 / 2013-03-02

  • Testing in both Node.js and the browser.

0.0.1 / 2013-03-01

  • First version with .DEFAULT_COMPARATOR(), #deq(), #empty(), #enq(), #peek(), and #size().