00d4dddab8aa9344af5de73580249b311ba17005
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
17 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 * Checks the fixed queue size.
35 * @param size - Queue size.
37 private checkSize (size
: number): void {
38 if (!Number.isSafeInteger(size
)) {
40 `Invalid fixed queue size: '${size.toString()}' is not an integer`
44 throw new RangeError(`Invalid fixed queue size: ${size.toString()} < 0`)
49 public clear (): void {
55 public dequeue (): T
| undefined {
59 const index
= this.start
62 if (this.start
=== this.capacity
) {
65 return this.nodeArray
[index
].data
69 public empty (): boolean {
70 return this.size
=== 0
74 public abstract enqueue (data
: T
, priority
?: number): number
77 public full (): boolean {
78 return this.size
=== this.capacity
82 public get (index
: number): T
| undefined {
83 if (this.empty() || index
>= this.size
) {
87 if (index
>= this.capacity
) {
88 index
-= this.capacity
90 return this.nodeArray
[index
].data
94 public [Symbol
.iterator
] (): Iterator
<T
> {
95 let index
= this.start
105 const value
= this.nodeArray
[index
].data
108 if (index
=== this.capacity
) {