Inspired by Java's PriorityQueue<T extends Sortable>
. Sorts elements based on the
comparison function the type T
provides and also optionally limits the size.
import {
Sortable,
PriorityQueue,
} from '@bausano/data-structures'
export class Element implements Sortable {
constructor (public priority: number, public value: string) {
//
}
public comp (a: Element) : number {
return a.priority < this.priority ? 1 : a.priority === this.priority ? 0 : -1
}
}
const queue = new PriorityQueue(2)
queue.push(new Element(1, 'hey'))
queue.push(new Element(3, 'there'))
queue.push(new Element(1, 'stranger'))
console.assert(queue.length === 2)
-
new<T extends Sortable> (maxLength?: number) : PriorityQueue<T>
IfmaxLength
is provided, queue ensures it's size doesn't grow over this number afterpush
method. -
length: number
andsize () : number
Number of elements in the queue. -
pop () : Optional<T>
ReturnsSome
with element on top (best rated). If the queue is empty, returnsNone
. -
push (el: T) : void
Adds new element to the queue. If the queue size grows over limit, discards the worst rated element. -
clear () : void
Empties the queue.
The Sortable
's method comp (another: Sortable)
should return 1 if this
is
larger, 0 if they're equal and -1 if another
is larger. Where larger means
"is on top of the queue".