export const defaultQueueSize = 2048
/**
- * Priority queue node.
+ * Fixed priority queue node.
*
* @typeParam T - Type of priority queue node data.
* @internal
*/
-interface PriorityQueueNode<T> {
+export interface FixedPriorityQueueNode<T> {
data: T
priority: number
}
*/
export class FixedPriorityQueue<T> {
private start!: number
- private readonly nodeArray: Array<PriorityQueueNode<T>>
+ private readonly nodeArray: Array<FixedPriorityQueueNode<T>>
public readonly capacity: number
public size!: number
public maxSize!: number
constructor (size: number = defaultQueueSize) {
this.checkSize(size)
this.capacity = size
- this.nodeArray = new Array<PriorityQueueNode<T>>(this.capacity)
+ this.nodeArray = new Array<FixedPriorityQueueNode<T>>(this.capacity)
this.clear()
}
return this.incrementSize()
}
+ /**
+ * Gets data from the fixed priority queue.
+ *
+ * @param index - The index of the data to get.
+ * @returns The data at the index or `undefined` if the fixed priority queue is empty or the index is out of bounds.
+ */
+ public get (index: number): T | undefined {
+ if (this.empty() || index >= this.size) {
+ return undefined
+ }
+ index += this.start
+ if (index >= this.capacity) {
+ index -= this.capacity
+ }
+ return this.nodeArray[index].data
+ }
+
/**
* Dequeue data from the fixed priority queue.
*