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 /** The priority queue maximum size. */
31 public maxSize
!: number
34 * Constructs a priority queue.
36 * @param bucketSize - Prioritized bucket size. @defaultValue defaultBucketSize
37 * @returns PriorityQueue.
39 public constructor (bucketSize
: number = defaultBucketSize
) {
40 if (!Number.isSafeInteger(bucketSize
)) {
42 `Invalid bucket size: '${bucketSize}' is not an integer`
46 throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`)
48 this.bucketSize
= bucketSize
53 * The priority queue size.
55 public get
size (): number {
56 let node
: PriorityQueueNode
<T
> | undefined = this.tail
58 while (node
!= null) {
66 * The number of filled prioritized buckets.
68 public get
buckets (): number {
69 return Math.trunc(this.size
/ this.bucketSize
)
73 * Enqueue data into the priority queue.
75 * @param data - Data to enqueue.
76 * @param priority - Priority of the data. Lower values have higher priority.
77 * @returns The new size of the priority queue.
79 public enqueue (data
: T
, priority
?: number): number {
80 if (this.head
.full()) {
81 this.head
= this.head
.next
= new FixedPriorityQueue(this.bucketSize
)
83 this.head
.enqueue(data
, priority
)
84 const size
= this.size
85 if (size
> this.maxSize
) {
92 * Dequeue data from the priority queue.
94 * @param bucket - The prioritized bucket to dequeue from.
95 * @returns The dequeued data or `undefined` if the priority queue is empty.
97 public dequeue (bucket
?: number): T
| undefined {
98 let tail
: PriorityQueueNode
<T
> | undefined = this.tail
99 let tailChanged
= false
100 if (bucket
!= null && bucket
> 0) {
101 let currentBucket
= 1
102 while (tail
!= null) {
103 if (currentBucket
=== bucket
) {
111 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
112 const data
= tail
!.dequeue()
113 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
114 if (tail
!.empty() && tail
!.next
!= null) {
116 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
117 this.tail
= tail
!.next
119 let node
: PriorityQueueNode
<T
> | undefined = this.tail
120 while (node
!= null) {
121 if (node
.next
=== tail
) {
122 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
123 node
.next
= tail
!.next
129 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
136 * Clears the priority queue.
138 public clear (): void {
139 this.head
= this.tail
= new FixedPriorityQueue(this.bucketSize
)
144 * Returns an iterator for the priority queue.
146 * @returns An iterator for the priority queue.
147 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
149 public [Symbol
.iterator
] (): Iterator
<T
> {
154 const value
= node
.get(index
) as T
162 if (index
=== node
.capacity
&& node
.next
!= null) {