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 public readonly capacity
: number
28 public maxSize
!: number
31 * Constructs a fixed priority queue.
33 * @param size - Fixed priority queue size. @defaultValue defaultQueueSize
34 * @returns FixedPriorityQueue.
36 constructor (size
: number = defaultQueueSize
) {
39 this.nodeArray
= new Array<FixedPriorityQueueNode
<T
>>(this.capacity
)
44 * Checks if the fixed priority queue is empty.
46 * @returns `true` if the fixed priority queue is empty, `false` otherwise.
48 public empty (): boolean {
49 return this.size
=== 0
53 * Checks if the fixed priority queue is full.
55 * @returns `true` if the fixed priority queue is full, `false` otherwise.
57 public full (): boolean {
58 return this.size
=== this.capacity
62 * Enqueue data into the fixed priority queue.
64 * @param data - Data to enqueue.
65 * @param priority - Priority of the data. Lower values have higher priority.
66 * @returns The new size of the priority queue.
68 public enqueue (data
: T
, priority
?: number): number {
70 throw new Error('Priority queue is full')
72 priority
= priority
?? 0
73 let index
= this.start
75 for (let i
= 0; i
< this.size
; i
++) {
76 if (this.nodeArray
[index
]?.priority
> priority
) {
77 this.nodeArray
.splice(index
, 0, { data
, priority
})
82 if (index
=== this.capacity
) {
86 this.nodeArray
.length
!== this.capacity
&&
87 (this.nodeArray
.length
= this.capacity
)
89 let index
= this.start
+ this.size
90 if (index
>= this.capacity
) {
91 index
-= this.capacity
93 this.nodeArray
[index
] = { data
, priority
}
95 return this.incrementSize()
99 * Gets data from the fixed priority queue.
101 * @param index - The index of the data to get.
102 * @returns The data at the index or `undefined` if the fixed priority queue is empty or the index is out of bounds.
104 public get (index
: number): T
| undefined {
105 if (this.empty() || index
>= this.size
) {
109 if (index
>= this.capacity
) {
110 index
-= this.capacity
112 return this.nodeArray
[index
].data
116 * Dequeue data from the fixed priority queue.
118 * @returns The dequeued data or `undefined` if the priority queue is empty.
120 public dequeue (): T
| undefined {
124 const index
= this.start
127 if (this.start
=== this.capacity
) {
130 return this.nodeArray
[index
].data
134 * Clears the fixed priority queue.
136 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
) {
174 * Increments the size of the fixed priority queue.
176 * @returns The new size of the fixed priority queue.
178 private incrementSize (): number {
180 if (this.size
> this.maxSize
) {
181 this.maxSize
= this.size
189 * @param size - The size to check.
191 private checkSize (size
: number): void {
192 if (!Number.isSafeInteger(size
)) {
194 `Invalid fixed priority queue size: '${size}' is not an integer`
198 throw new RangeError(`Invalid fixed priority queue size: ${size} < 0`)