prioqueuejs
v0.0.2
Published
Priority Queue implementation in typescript.
Readme
Prioqueuejs
Priority Queue implementation in typescript.
Getting started
npm install prioqueuejsUsage
import { PrioQueue, CreateIterator } from "prioqueuejs";
// initializing with new values
const initialValues = [3, 4, 5];
const indexMap = new Map();
const queue = new PrioQueue({
initialValues,
// function to compare two items of the queue
compareFn: (a, b) => a - b,
// optional callback that will informs us if the ordering of the items change
onIndexUpdatedFn: (index, item) => {
indexMap.set(index, item);
},
});
// Enqueueing new items
queue.enqueue(12);
queue.enqueue(1);
queue.enqueue(34);
queue.dequeue(); // returns 1
queue.dequeue(); // returns 3
// Using the Iterator
const iterator = CreateIterator(queue);
iterator.next(); // {value: 3, done: false}
// that also means you could use it in a loop
// which will exhaust the iterator and dequeue all
// items from the priority queue
for (const item of iterator) {
console.log(item);
}
// Prints:
// 5
// 12
// 34