5 } from
'./utility-types.js'
9 * @typeParam T - Type of fixed queue data.
12 export class FixedQueue
<T
> implements IFixedQueue
<T
> {
13 private start
!: number
15 public readonly capacity
: number
19 public nodeArray
: FixedQueueNode
<T
>[]
22 * Constructs a fixed queue.
23 * @param size - Fixed queue size. @defaultValue defaultQueueSize
24 * @returns FixedQueue.
26 constructor (size
: number = defaultQueueSize
) {
29 this.nodeArray
= new Array<FixedQueueNode
<T
>>(this.capacity
)
34 public empty (): boolean {
35 return this.size
=== 0
39 public full (): boolean {
40 return this.size
=== this.capacity
44 public enqueue (data
: T
, priority
?: number): number {
46 throw new Error('Fixed queue is full')
48 let index
= this.start
+ this.size
49 if (index
>= this.capacity
) {
50 index
-= this.capacity
52 this.nodeArray
[index
] = { data
, priority
: priority
?? 0 }
57 public get (index
: number): T
| undefined {
58 if (this.empty() || index
>= this.size
) {
62 if (index
>= this.capacity
) {
63 index
-= this.capacity
65 return this.nodeArray
[index
].data
69 public dequeue (): T
| undefined {
73 const index
= this.start
76 if (this.start
=== this.capacity
) {
79 return this.nodeArray
[index
].data
83 public clear (): void {
89 public [Symbol
.iterator
] (): Iterator
<T
> {
90 let index
= this.start
100 const value
= this.nodeArray
[index
].data
103 if (index
=== this.capacity
) {
115 * Checks the fixed queue size.
116 * @param size - Queue size.
118 private checkSize (size
: number): void {
119 if (!Number.isSafeInteger(size
)) {
121 `Invalid fixed queue size: '${size.toString()}' is not an integer`
125 throw new RangeError(`Invalid fixed queue size: ${size.toString()} < 0`)