1 // Copyright Jerome Benoit. 2024. All Rights Reserved.
3 import { FixedPriorityQueue
} from
'./fixed-priority-queue.js'
8 export const defaultBucketSize
= 2048
11 * Priority queue node.
13 * @typeParam T - Type of priority queue node data.
16 export interface PriorityQueueNode
<T
> extends FixedPriorityQueue
<T
> {
17 next
?: FixedPriorityQueue
<T
>
23 * @typeParam T - Type of priority queue data.
26 export class PriorityQueue
<T
> {
27 private head
!: PriorityQueueNode
<T
>
28 private tail
!: PriorityQueueNode
<T
>
29 private readonly bucketSize
: number
30 public maxSize
!: number
33 * Constructs a priority queue.
35 * @param bucketSize - Prioritized bucket size. @defaultValue defaultBucketSize
36 * @returns PriorityQueue.
38 public constructor (bucketSize
: number = defaultBucketSize
) {
39 if (!Number.isSafeInteger(bucketSize
)) {
41 `Invalid bucket size: '${bucketSize}' is not an integer`
45 throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`)
47 this.bucketSize
= bucketSize
52 * The size of the priority queue.
54 public get
size (): number {
55 let node
: PriorityQueueNode
<T
> | undefined = this.tail
57 while (node
!= null) {
65 * The number of filled prioritized buckets.
67 public get
buckets (): number {
68 return Math.trunc(this.size
/ this.bucketSize
)
72 * Enqueue data into the priority queue.
74 * @param data - Data to enqueue.
75 * @param priority - Priority of the data. Lower values have higher priority.
76 * @returns The new size of the priority queue.
78 public enqueue (data
: T
, priority
?: number): number {
79 if (this.head
.full()) {
80 this.head
= this.head
.next
= new FixedPriorityQueue(this.bucketSize
)
82 this.head
.enqueue(data
, priority
)
83 const size
= this.size
84 if (size
> this.maxSize
) {
91 * Dequeue data from the priority queue.
93 * @param bucket - The prioritized bucket to dequeue from.
94 * @returns The dequeued data or `undefined` if the priority queue is empty.
96 public dequeue (bucket
?: number): T
| undefined {
97 let tail
: PriorityQueueNode
<T
> | undefined = this.tail
100 while (tail
!= null) {
102 if (currentBucket
=== bucket
) {
108 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
109 const data
= tail
!.dequeue()
110 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
111 if (tail
!.empty() && tail
!.next
!= null) {
112 if (tail
=== this.tail
) {
113 this.tail
= tail
.next
115 let node
: PriorityQueueNode
<T
> | undefined = this.tail
116 while (node
!= null) {
117 if (node
.next
=== tail
) {
118 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
119 node
.next
= tail
!.next
125 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
132 * Clears the priority queue.
134 public clear (): void {
135 this.head
= this.tail
= new FixedPriorityQueue(this.bucketSize
)
140 * Returns an iterator for the priority queue.
142 * @returns An iterator for the priority queue.
143 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
145 public [Symbol
.iterator
] (): Iterator
<T
> {
150 if (node
.empty() || (i
>= node
.capacity
&& node
.next
== null)) {
156 const value
= node
.dequeue() as T
158 if (i
=== node
.capacity
&& node
.next
!= null) {