@technobuddha/library
Version:
A large library of useful functions
81 lines (80 loc) • 2.06 kB
JavaScript
/**
* A simple priority queue
*/
export class PriorityQueue {
comparator;
/**
* @param comparator Function to compare two elements and puts them in priority order. Takes two elements as arguments and returns a number greater, less
* then or equal to zero.
* @param contents Initial contents of the queue
*/
constructor(comparator, contents) {
this.comparator = comparator;
this.contents = Array.from(contents ?? []);
this.sorted = false;
}
contents;
sorted;
sort() {
this.contents.sort(this.comparator);
this.sorted = true;
}
/**
* Add an element to the queue
* @param o element to be added
*/
push(...o) {
this.contents.push(...o);
this.sorted = false;
}
/**
* Return and remove the highest priority item from the queue
*
* @returns queue element
*/
pop() {
if (!this.sorted)
this.sort();
return this.contents.shift();
}
/**
* Iterate through all elements in the queue
*
* @returns generator function
*/
*[Symbol.iterator]() {
if (!this.sorted)
this.sort();
yield* this.contents;
}
/**
* Determine the number of items in the queue
*
* @returns number of element in the queue
*/
get size() {
return this.contents.length;
}
/**
* Transform all elements in the queue
*
* @param f Function to transforme each element of the queue
* @returns array of transformed queue elements
*/
map(f) {
if (!this.sorted)
this.sort();
// eslint-disable-next-line unicorn/no-array-callback-reference
return this.contents.map(f);
}
/**
* Change the function used to order the queue
*
* @param newComparator function to compare elements of the queue
*/
reorder(newComparator) {
this.comparator = newComparator;
this.sorted = false;
}
}
export default PriorityQueue;