275a78403f9292c67c12e58cf39aaff29a076dde
1 import type { IFixedQueue
} from
'./queue-types.js'
3 import { AbstractFixedQueue
} from
'./abstract-fixed-queue.js'
6 * Fixed priority queue.
7 * @typeParam T - Type of fixed priority queue data.
10 export class FixedPriorityQueue
<T
>
11 extends AbstractFixedQueue
<T
>
12 implements IFixedQueue
<T
> {
14 public enqueue (data
: T
, priority
?: number): number {
16 throw new Error('Fixed priority queue is full')
18 priority
= priority
?? 0
20 let index
= this.start
21 for (let i
= 0; i
< this.size
; i
++) {
22 if (this.nodeArray
[index
].priority
> priority
) {
23 this.nodeArray
.splice(index
, 0, { data
, priority
})
24 this.nodeArray
.length
= this.capacity
29 if (index
=== this.capacity
) {
34 let index
= this.start
+ this.size
35 if (index
>= this.capacity
) {
36 index
-= this.capacity
38 this.nodeArray
[index
] = { data
, priority
}