Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jakzo/priority-queue
JavaScript priority queue implementation.
https://github.com/jakzo/priority-queue
computer-science data-structures
Last synced: 29 days ago
JSON representation
JavaScript priority queue implementation.
- Host: GitHub
- URL: https://github.com/jakzo/priority-queue
- Owner: jakzo
- License: cc0-1.0
- Created: 2017-03-14T07:00:04.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-15T08:03:05.000Z (almost 8 years ago)
- Last Synced: 2024-11-14T04:44:21.794Z (3 months ago)
- Topics: computer-science, data-structures
- Language: JavaScript
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# priority-queue
[![Build Status](https://travis-ci.org/jakzo/priority-queue.svg?branch=master)](https://travis-ci.org/jakzo/priority-queue)
[![Build status](https://ci.appveyor.com/api/projects/status/ov5yyh5r827nwip1?svg=true)](https://ci.appveyor.com/project/jakzo/priority-queue)
[![Coverage Status](https://coveralls.io/repos/github/jakzo/priority-queue/badge.svg?branch=master)](https://coveralls.io/github/jakzo/priority-queue?branch=master)
[![Dependency Status](https://dependencyci.com/github/jakzo/priority-queue/badge)](https://dependencyci.com/github/jakzo/priority-queue)*JavaScript priority queue implementation.*
## Usage
```js
// Include the library
// Browser:
// Node: var PriorityQueue = require('./lib/priority-queue.js')
// ES6: import PriorityQueue from './lib/priority-queue.js'function compare(a, b) {
if (a.priority < b.priority) return true;
return false;
}
var pq = new PriorityQueue(compare);
pq.push({ foo: 'bar', priority: 4 });
pq.push({ priority: 3, something: 'text' });
pq.push({ priority: 9 });
console.log(pq.length); // 3
console.log(pq.pop()); // { priority: 3, something: 'text' }
console.log(pq.peek()); // { foo: 'bar', priority: 4 }
console.log(pq.pop()); // { foo: 'bar', priority: 4 }
console.log(pq.pop()); // { priority: 9 }
```## API
### Class: `PriorityQueue`
#### `push(item)`
Pushes an element onto the queue.
**item**: `*`, Element to be pushed onto the queue.
**Returns**: `number`, The number of items on the queue after the push.
#### `pop()`
Removes and returns the item with the highest priority.
**Returns**: `*`, The item with the highest priority in the queue.
#### `peek()`
Returns the item with the highest priority without removing it.
**Returns**: `*`, The item with the highest priority in the queue.
#### `clear()`
Removes all elements from the queue.
**Returns**: `void`
#### `length`
The number of elements in the queue.
**Type**: `number`
#### `PriorityQueue.defaultCompare`
The comparison function which is used if no comparison function is passed
into the constructor. By default it evaluates `a < b`.