5 } from
'./queue-types.js'
8 * Base fixed queue class
9 * @typeParam T - Type of fixed queue data.
12 export abstract class AbstractFixedQueue
<T
> implements IFixedQueue
<T
> {
13 protected 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 IFixedQueue.
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 abstract enqueue (data
: T
, priority
?: number): number
47 public get (index
: number): T
| undefined {
48 if (this.empty() || index
>= this.size
) {
52 if (index
>= this.capacity
) {
53 index
-= this.capacity
55 return this.nodeArray
[index
].data
59 public dequeue (): T
| undefined {
63 const index
= this.start
66 if (this.start
=== this.capacity
) {
69 return this.nodeArray
[index
].data
73 public clear (): void {
79 public [Symbol
.iterator
] (): Iterator
<T
> {
80 let index
= this.start
90 const value
= this.nodeArray
[index
].data
93 if (index
=== this.capacity
) {
105 * Checks the fixed queue size.
106 * @param size - Queue size.
108 private checkSize (size
: number): void {
109 if (!Number.isSafeInteger(size
)) {
111 `Invalid fixed queue size: '${size.toString()}' is not an integer`
115 throw new RangeError(`Invalid fixed queue size: ${size.toString()} < 0`)