ce96b4e8b9480df9a134c72cd340122903b04db5
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
98 let tailChanged
= false
99 if (bucket
!= null && bucket
> 0) {
100 let currentBucket
= 1
101 while (tail
!= null) {
102 if (currentBucket
=== bucket
) {
110 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
111 const data
= tail
!.dequeue()
112 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
113 if (tail
!.empty() && tail
!.next
!= null) {
115 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
116 this.tail
= tail
!.next
118 let node
: PriorityQueueNode
<T
> | undefined = this.tail
119 while (node
!= null) {
120 if (node
.next
=== tail
) {
121 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
122 node
.next
= tail
!.next
128 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
135 * Clears the priority queue.
137 public clear (): void {
138 this.head
= this.tail
= new FixedPriorityQueue(this.bucketSize
)
143 * Returns an iterator for the priority queue.
145 * @returns An iterator for the priority queue.
146 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
148 public [Symbol
.iterator
] (): Iterator
<T
> {
153 const value
= node
.get(index
) as T
161 if (index
=== node
.capacity
&& node
.next
!= null) {