4 export const defaultQueueSize
= 2048
7 * Fixed priority queue node.
9 * @typeParam T - Type of priority queue node data.
12 export interface FixedPriorityQueueNode
<T
> {
18 * Fixed priority queue.
20 * @typeParam T - Type of fixed priority queue data.
23 export class FixedPriorityQueue
<T
> {
24 private start
!: number
25 private readonly nodeArray
: Array<FixedPriorityQueueNode
<T
>>
26 /** The fixed priority queue capacity. */
27 public readonly capacity
: number
28 /** The fixed priority queue size */
32 * Constructs a fixed priority queue.
34 * @param size - Fixed priority queue size. @defaultValue defaultQueueSize
35 * @returns FixedPriorityQueue.
37 constructor (size
: number = defaultQueueSize
) {
40 this.nodeArray
= new Array<FixedPriorityQueueNode
<T
>>(this.capacity
)
45 * Checks if the fixed priority queue is empty.
47 * @returns `true` if the fixed priority queue is empty, `false` otherwise.
49 public empty (): boolean {
50 return this.size
=== 0
54 * Checks if the fixed priority queue is full.
56 * @returns `true` if the fixed priority queue is full, `false` otherwise.
58 public full (): boolean {
59 return this.size
=== this.capacity
63 * Enqueue data into the fixed priority queue.
65 * @param data - Data to enqueue.
66 * @param priority - Priority of the data. Lower values have higher priority.
67 * @returns The new size of the priority queue.
69 public enqueue (data
: T
, priority
?: number): number {
71 throw new Error('Priority queue is full')
73 priority
= priority
?? 0
74 let index
= this.start
76 for (let i
= 0; i
< this.size
; i
++) {
77 if (this.nodeArray
[index
]?.priority
> priority
) {
78 this.nodeArray
.splice(index
, 0, { data
, priority
})
83 if (index
=== this.capacity
) {
87 this.nodeArray
.length
!== this.capacity
&&
88 (this.nodeArray
.length
= this.capacity
)
90 let index
= this.start
+ this.size
91 if (index
>= this.capacity
) {
92 index
-= this.capacity
94 this.nodeArray
[index
] = { data
, priority
}
100 * Gets data from the fixed priority queue.
102 * @param index - The index of the data to get.
103 * @returns The data at the index or `undefined` if the fixed priority queue is empty or the index is out of bounds.
105 public get (index
: number): T
| undefined {
106 if (this.empty() || index
>= this.size
) {
110 if (index
>= this.capacity
) {
111 index
-= this.capacity
113 return this.nodeArray
[index
].data
117 * Dequeue data from the fixed priority queue.
119 * @returns The dequeued data or `undefined` if the priority queue is empty.
121 public dequeue (): T
| undefined {
125 const index
= this.start
128 if (this.start
=== this.capacity
) {
131 return this.nodeArray
[index
].data
135 * Clears the fixed priority queue.
137 public clear (): void {
143 * Returns an iterator for the fixed priority queue.
145 * @returns An iterator for the fixed priority queue.
146 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
148 public [Symbol
.iterator
] (): Iterator
<T
> {
149 let index
= this.start
153 if (i
>= this.size
) {
159 const value
= this.nodeArray
[index
].data
162 if (index
=== this.capacity
) {
176 * @param size - The size to check.
178 private checkSize (size
: number): void {
179 if (!Number.isSafeInteger(size
)) {
181 `Invalid fixed priority queue size: '${size}' is not an integer`
185 throw new RangeError(`Invalid fixed priority queue size: ${size} < 0`)