craigdallimore / priority-queue

A simple priority queue

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Priority Queue

Build Coverage Downloads Size

Installation

npm install @decoy9697/priority-queue

Usage

import PQ, { LOW_FIRST } from "@decoy9697/priority-queue";

const pq = new PQ({ sort: LOW_FIRST });

// The number influences priority
// - lower takes precedence (given LOW_FIRST, otherwise use HIGH_FIRST)
// - with equal numbers, recency of insert takes precendence
pq.insert("🌡", 5);
pq.insert("🌿", 10);
pq.insert("🌴", 3);

// .peek() gives the item with the lowest number, but does not dequeue it
pq.peek(); // 🌴
pq.peek(); // 🌴

// .pop() gives the item with the lowest number, dequeuing it
pq.pop(); // 🌴
pq.pop(); // 🌡
pq.pop(); // 🌿
pq.pop(); // undefined

// .isEmpty() returns a boolean, true given it is empty
pq.isEmpty(); // true

Development

This project uses nix to install dependencies for the development shell.

Commands

nix-shell - starts a development shell with system dependencies

Within the shell

yarn - install project dependencies

yarn test - run the tests

yarn bench - run benchmarkjs

yarn format - run the linter

About

A simple priority queue

License:MIT License


Languages

Language:TypeScript 92.4%Language:Nix 5.5%Language:JavaScript 2.1%