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.
68 * @throws If the fixed priority queue is full.
70 public enqueue (data
: T
, priority
?: number): number {
72 throw new Error('Priority queue is full')
74 priority
= priority
?? 0
75 let index
= this.start
77 for (let i
= 0; i
< this.size
; i
++) {
78 if (this.nodeArray
[index
].priority
> priority
) {
79 this.nodeArray
.splice(index
, 0, { data
, priority
})
80 this.nodeArray
.length
!== this.capacity
&&
81 (this.nodeArray
.length
= this.capacity
)
86 if (index
=== this.capacity
) {
91 let index
= this.start
+ this.size
92 if (index
>= this.capacity
) {
93 index
-= this.capacity
95 this.nodeArray
[index
] = { data
, priority
}
101 * Gets data from the fixed priority queue.
103 * @param index - The index of the data to get.
104 * @returns The data at the index or `undefined` if the fixed priority queue is empty or the index is out of bounds.
106 public get (index
: number): T
| undefined {
107 if (this.empty() || index
>= this.size
) {
111 if (index
>= this.capacity
) {
112 index
-= this.capacity
114 return this.nodeArray
[index
].data
118 * Dequeue data from the fixed priority queue.
120 * @returns The dequeued data or `undefined` if the priority queue is empty.
122 public dequeue (): T
| undefined {
126 const index
= this.start
129 if (this.start
=== this.capacity
) {
132 return this.nodeArray
[index
].data
136 * Clears the fixed priority queue.
138 public clear (): void {
144 * Returns an iterator for the fixed priority queue.
146 * @returns An iterator for the fixed priority queue.
147 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
149 public [Symbol
.iterator
] (): Iterator
<T
> {
150 let index
= this.start
154 if (i
>= this.size
) {
160 const value
= this.nodeArray
[index
].data
163 if (index
=== this.capacity
) {
177 * @param size - The size to check.
179 private checkSize (size
: number): void {
180 if (!Number.isSafeInteger(size
)) {
182 `Invalid fixed priority queue size: '${size}' is not an integer`
186 throw new RangeError(`Invalid fixed priority queue size: ${size} < 0`)