1 import { defaultQueueSize
, type FixedQueueNode
, type IFixedQueue
} from
'./utility-types.js'
5 * @typeParam T - Type of fixed queue data.
8 export class FixedQueue
<T
> implements IFixedQueue
<T
> {
11 public readonly capacity
: number
15 public nodeArray
: FixedQueueNode
<T
>[]
18 * Constructs a fixed queue.
19 * @param size - Fixed queue size. @defaultValue defaultQueueSize
20 * @returns FixedQueue.
22 constructor (size
: number = defaultQueueSize
) {
25 this.nodeArray
= new Array<FixedQueueNode
<T
>>(this.capacity
)
30 public empty (): boolean {
31 return this.size
=== 0
35 public full (): boolean {
36 return this.size
=== this.capacity
40 public enqueue (data
: T
, priority
?: number): number {
42 throw new Error('Priority queue is full')
44 let index
= this.start
+ this.size
45 if (index
>= this.capacity
) {
46 index
-= this.capacity
48 this.nodeArray
[index
] = { data
, priority
: priority
?? 0 }
53 public get (index
: number): T
| undefined {
54 if (this.empty() || index
>= this.size
) {
58 if (index
>= this.capacity
) {
59 index
-= this.capacity
61 return this.nodeArray
[index
].data
65 public dequeue (): T
| undefined {
69 const index
= this.start
72 if (this.start
=== this.capacity
) {
75 return this.nodeArray
[index
].data
79 public clear (): void {
85 public [Symbol
.iterator
] (): Iterator
<T
> {
86 let index
= this.start
96 const value
= this.nodeArray
[index
].data
99 if (index
=== this.capacity
) {
111 * Checks the fixed queue size.
112 * @param size - Queue size.
114 private checkSize (size
: number): void {
115 if (!Number.isSafeInteger(size
)) {
117 `Invalid fixed queue size: '${size.toString()}' is not an integer`
121 throw new RangeError(
122 `Invalid fixed queue size: ${size.toString()} < 0`