4 export const defaultQueueSize
= 2048
7 * Fixed priority queue node.
8 * @typeParam T - Type of priority queue node data.
11 export interface FixedPriorityQueueNode
<T
> {
17 * Fixed priority queue.
18 * @typeParam T - Type of fixed priority queue data.
21 export class FixedPriorityQueue
<T
> {
22 private start
!: number
23 private readonly nodeArray
: FixedPriorityQueueNode
<T
>[]
24 /** The fixed priority queue capacity. */
25 public readonly capacity
: number
26 /** The fixed priority queue size. */
28 /** Whether to enable priority. */
29 public enablePriority
: boolean
32 * Constructs a fixed priority queue.
33 * @param size - Fixed priority queue size. @defaultValue defaultQueueSize
34 * @param enablePriority - Whether to enable priority. @defaultValue false
35 * @returns FixedPriorityQueue.
37 constructor (size
: number = defaultQueueSize
, enablePriority
= false) {
40 this.enablePriority
= enablePriority
41 this.nodeArray
= new Array<FixedPriorityQueueNode
<T
>>(this.capacity
)
46 * 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.
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.
63 * @param data - Data to enqueue.
64 * @param priority - Priority of the data. Lower values have higher priority.
65 * @returns The new size of the priority queue.
66 * @throws If the fixed priority queue is full.
68 public enqueue (data
: T
, priority
?: number): number {
70 throw new Error('Priority queue is full')
72 priority
= priority
?? 0
74 if (this.enablePriority
) {
75 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
})
79 this.nodeArray
.length
!== this.capacity
&&
80 (this.nodeArray
.length
= this.capacity
)
85 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.
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.
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 {
142 * Returns an iterator for the fixed priority queue.
143 * @returns An iterator for the fixed priority queue.
144 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
146 public [Symbol
.iterator
] (): Iterator
<T
> {
147 let index
= this.start
151 if (i
>= this.size
) {
157 const value
= this.nodeArray
[index
].data
160 if (index
=== this.capacity
) {
172 * Checks the queue size.
173 * @param size - Queue size.
175 private checkSize (size
: number): void {
176 if (!Number.isSafeInteger(size
)) {
178 `Invalid fixed priority queue size: '${size}' is not an integer`
182 throw new RangeError(`Invalid fixed priority queue size: ${size} < 0`)