ee3310c3d8f2cb8cf6e4d6287d0a668eb20f4a9c
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 * @param enablePriority - Whether to enable priority. @defaultValue false
38 * @returns PriorityQueue.
41 bucketSize
: number = defaultBucketSize
,
42 enablePriority
= false
44 if (!Number.isSafeInteger(bucketSize
)) {
46 `Invalid bucket size: '${bucketSize}' is not an integer`
50 throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`)
52 this.bucketSize
= bucketSize
53 this.head
= this.tail
= new FixedPriorityQueue(
61 * The priority queue size.
63 public get
size (): number {
64 let node
: PriorityQueueNode
<T
> | undefined = this.tail
66 while (node
!= null) {
73 public get
enablePriority (): boolean {
74 return this.head
.enablePriority
77 public set
enablePriority (enablePriority
: boolean) {
78 if (this.head
.enablePriority
=== enablePriority
) {
81 let node
: PriorityQueueNode
<T
> | undefined = this.tail
82 while (node
!= null) {
83 node
.enablePriority
= enablePriority
89 * The number of filled prioritized buckets.
91 public get
buckets (): number {
92 return Math.trunc(this.size
/ this.bucketSize
)
96 * Enqueue data into the priority queue.
98 * @param data - Data to enqueue.
99 * @param priority - Priority of the data. Lower values have higher priority.
100 * @returns The new size of the priority queue.
102 public enqueue (data
: T
, priority
?: number): number {
103 if (this.head
.full()) {
104 this.head
= this.head
.next
= new FixedPriorityQueue(
109 this.head
.enqueue(data
, priority
)
110 const size
= this.size
111 if (size
> this.maxSize
) {
118 * Dequeue data from the priority queue.
120 * @param bucket - The prioritized bucket to dequeue from.
121 * @returns The dequeued data or `undefined` if the priority queue is empty.
123 public dequeue (bucket
?: number): T
| undefined {
124 let tail
: PriorityQueueNode
<T
> | undefined = this.tail
125 let tailChanged
= false
126 if (bucket
!= null && bucket
> 0) {
127 let currentBucket
= 1
128 while (tail
!= null) {
129 if (currentBucket
=== bucket
) {
137 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
138 const data
= tail
!.dequeue()
139 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
140 if (tail
!.empty() && tail
!.next
!= null) {
142 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
143 this.tail
= tail
!.next
145 let node
: PriorityQueueNode
<T
> | undefined = this.tail
146 while (node
!= null) {
147 if (node
.next
=== tail
) {
148 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
149 node
.next
= tail
!.next
155 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
162 * Clears the priority queue.
164 public clear (): void {
165 this.head
= this.tail
= new FixedPriorityQueue(
173 * Returns an iterator for the priority queue.
175 * @returns An iterator for the priority queue.
176 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
178 public [Symbol
.iterator
] (): Iterator
<T
> {
183 const value
= node
.get(index
) as T
191 if (index
=== node
.capacity
&& node
.next
!= null) {