1.0.2 • Published 6 months ago

pq-ts v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

pq-ts

CI npm version npm downloads License: MIT codecov

pq-ts is a TypeScript library that provides efficient and flexible priority queue implementations. It includes both standard and stable priority queues, ensuring that elements with the same priority maintain their relative order of insertion. Additionally, it supports typed priority queues which use typed arrays.

Features

  • Priority Queue: A standard priority queue that orders elements based on their priority.
  • Stable Priority Queue: A stable priority queue that maintains the relative order of elements with the same priority.
  • Typed Priority Queue: A priority queue with typed arrays.
  • Stable Typed Priority Queue: A stable priority queue with typed arrays.
  • Custom Comparer: Support for custom comparison functions to define the order of elements.
  • Efficient Operations: Optimized for efficient enqueue, dequeue, and heap operations.

Installation

You can install pq-ts using npm, yarn, or bun:

# Using npm
npm install pq-ts

# Using yarn
yarn add pq-ts

# Using bun
bun add pq-ts

Usage

Importing the Library

import {
  PriorityQueue,
  StablePriorityQueue,
  StableTypedPriorityQueue,
  TypedPriorityQueue,
} from "pq-ts";

Priority Queue

Creating a Priority Queue

const pq = new PriorityQueue<number>();

Enqueuing Elements

pq.enqueue(1, 5);
pq.enqueue(2, 3);
pq.enqueue(3, 4);

Dequeuing Elements

console.log(pq.dequeue()); // 2
console.log(pq.dequeue()); // 3
console.log(pq.dequeue()); // 1

Peeking the Highest Priority Element

pq.enqueue(1, 5);
pq.enqueue(2, 3);
pq.enqueue(3, 4);

console.log(pq.peek()); // 2

Other Methods

  • clear(): Clears all elements from the queue.
  • count: Returns the number of elements in the queue.
  • isEmpty(): Checks if the queue is empty.
  • values: Returns the values in the queue (unordered).
  • toArray(): Converts the queue to an array (prioritized).
  • remove(value): Removes a specific element from the queue.
  • indexOf(value, dequeue, comparer): Returns the index of a specific element.
  • priorityAt(index, dequeue, comparer): Returns the priority of an element at a specific index.
  • clone(): Creates a shallow copy of the queue.
  • toString(): Returns a string representation of the queue.

Stable Priority Queue

Creating a Stable Priority Queue

const spq = new StablePriorityQueue<number>();

Enqueuing Elements

spq.enqueue(1, 5);
spq.enqueue(2, 3);
spq.enqueue(3, 4);

Dequeuing Elements

console.log(spq.dequeue()); // 2
console.log(spq.dequeue()); // 3
console.log(spq.dequeue()); // 1

Peeking the Highest Priority Element

spq.enqueue(1, 5);
spq.enqueue(2, 3);
spq.enqueue(3, 4);

console.log(spq.peek()); // 2

Other Methods

  • clear(): Clears all elements from the queue.
  • count: Returns the number of elements in the queue.
  • isEmpty(): Checks if the queue is empty.
  • values: Returns the values in the queue (unordered).
  • toArray(): Converts the queue to an array (prioritized).
  • remove(value): Removes a specific element from the queue.
  • indexOf(value, dequeue, comparer): Returns the index of a specific element.
  • priorityAt(index, dequeue, comparer): Returns the priority of an element at a specific index.
  • clone(): Creates a shallow copy of the queue.
  • toString(): Returns a string representation of the queue.

Typed Priority Queue

Creating a Typed Priority Queue

const tpq = new TypedPriorityQueue<number>(Int32Array, 10);

Enqueuing Elements

tpq.enqueue(1, 5);
tpq.enqueue(2, 3);
tpq.enqueue(3, 4);

Dequeuing Elements

console.log(tpq.dequeue()); // 2
console.log(tpq.dequeue()); // 3
console.log(tpq.dequeue()); // 1

Peeking the Highest Priority Element

tpq.enqueue(1, 5);
tpq.enqueue(2, 3);
tpq.enqueue(3, 4);

console.log(tpq.peek()); // 2

Other Methods

  • clear(): Clears all elements from the queue.
  • count: Returns the number of elements in the queue.
  • isEmpty(): Checks if the queue is empty.
  • values: Returns the values in the queue (unordered).
  • toArray(): Converts the queue to an array (prioritized).
  • remove(value): Removes a specific element from the queue.
  • indexOf(value, dequeue, comparer): Returns the index of a specific element.
  • priorityAt(index, dequeue, comparer): Returns the priority of an element at a specific index.
  • clone(): Creates a shallow copy of the queue.
  • toString(): Returns a string representation of the queue.

Stable Typed Priority Queue

Creating a Stable Typed Priority Queue

const stpq = new StableTypedPriorityQueue<number>(Int32Array, 10);

Enqueuing Elements

stpq.enqueue(1, 5);
stpq.enqueue(2, 3);
stpq.enqueue(3, 4);

Dequeuing Elements

console.log(stpq.dequeue()); // 2
console.log(stpq.dequeue()); // 3
console.log(stpq.dequeue()); // 1

Peeking the Highest Priority Element

stpq.enqueue(1, 5);
stpq.enqueue(2, 3);
stpq.enqueue(3, 4);

console.log(stpq.peek()); // 2

Other Methods

  • clear(): Clears all elements from the queue.
  • count: Returns the number of elements in the queue.
  • isEmpty(): Checks if the queue is empty.
  • values: Returns the values in the queue (unordered).
  • toArray(): Converts the queue to an array (prioritized).
  • remove(value): Removes a specific element from the queue.
  • indexOf(value, dequeue, comparer): Returns the index of a specific element.
  • priorityAt(index, dequeue, comparer): Returns the priority of an element at a specific index.
  • clone(): Creates a shallow copy of the queue.
  • toString(): Returns a string representation of the queue.

Examples

Using a Custom Comparer

You can define a custom comparer to change the order of elements in the queue:

const customComparer = (
  a: { value: number; priority: number },
  b: { value: number; priority: number },
) => {
  return b.priority - a.priority; // Max-heap
};

const pq = new PriorityQueue<number>(customComparer);
pq.enqueue(1, 5);
pq.enqueue(2, 3);
pq.enqueue(3, 4);

console.log(pq.dequeue()); // 1
console.log(pq.dequeue()); // 3
console.log(pq.dequeue()); // 2

Benchmarks

bun run bench.ts

Results

The benchmark below ran on an Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz using bun 1.1.43 (x64-win32)

BenchmarkAvg (min … max)p75 / p99
PriorityQueue enqueue 1000000 items73.97 ms/iter108.20 ms
PriorityQueue dequeue 1000000 items1.76 ms/iter1.70 ms
StablePriorityQueue enqueue 1000000 items156.84 ms/iter221.69 ms
StablePriorityQueue dequeue 1000000 items1.94 ms/iter1.93 ms
TypedPriorityQueue enqueue 1000000 items69.31 ms/iter57.97 ms
TypedPriorityQueue dequeue 1000000 items1.45 ms/iter1.45 ms
StableTypedPriorityQueue enqueue 1000000 items205.20 ms/iter202.10 ms
StableTypedPriorityQueue dequeue 1000000 items1.25 ms/iter1.22 ms
Native Array enqueue 1000000 items43.05 ms/iter52.73 ms
Native Array dequeue 1000000 items17.18 ms/iter18.03 ms

The benchmark below ran on an Apple M1 Pro (2021) using bun 1.1.43 (aarch64-darwin)

BenchmarkAvg (min … max)p75 / p99
PriorityQueue enqueue 1000000 items20.56 ms/iter25.00 ms
PriorityQueue dequeue 1000000 items629.17 µs/iter627.21 µs
StablePriorityQueue enqueue 1000000 items51.82 ms/iter54.80 ms
StablePriorityQueue dequeue 1000000 items630.16 µs/iter628.88 µs
TypedPriorityQueue enqueue 1000000 items34.00 ms/iter34.59 ms
TypedPriorityQueue dequeue 1000000 items470.82 µs/iter470.96 µs
StableTypedPriorityQueue enqueue 1000000 items112.37 ms/iter112.92 ms
StableTypedPriorityQueue dequeue 1000000 items476.81 µs/iter484.38 µs
Native Array enqueue 1000000 items8.43 ms/iter10.69 ms
Native Array dequeue 1000000 items6.59 ms/iter6.57 ms

Documentation

Deno is required to generate the documentation.

# Generate the docs
npm run docs:generate
# View the docs
npm run docs:serve

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Credits

License

This project is licensed under the MIT License.

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago

0.7.0

6 months ago

0.6.0

6 months ago

0.5.0

6 months ago

0.4.0

6 months ago

0.3.0

6 months ago

0.2.0

6 months ago

0.1.0

6 months ago