-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
priority-queue.ts
207 lines (194 loc) · 5.65 KB
/
priority-queue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright Jerome Benoit. 2024. All Rights Reserved.
import { FixedPriorityQueue } from './fixed-priority-queue.js'
/**
* Default bucket size.
*/
export const defaultBucketSize = 2048
/**
* Priority queue node.
* @typeParam T - Type of priority queue node data.
* @internal
*/
export interface PriorityQueueNode<T> extends FixedPriorityQueue<T> {
next?: FixedPriorityQueue<T>
}
/**
* Priority queue.
* @typeParam T - Type of priority queue data.
* @internal
*/
export class PriorityQueue<T> {
private head!: PriorityQueueNode<T>
private tail!: PriorityQueueNode<T>
private readonly bucketSize: number
/** The priority queue maximum size. */
public maxSize!: number
/**
* Constructs a priority queue.
* @param bucketSize - Prioritized bucket size. @defaultValue defaultBucketSize
* @param enablePriority - Whether to enable priority. @defaultValue false
* @returns PriorityQueue.
*/
public constructor (
bucketSize: number = defaultBucketSize,
enablePriority = false
) {
if (!Number.isSafeInteger(bucketSize)) {
throw new TypeError(
`Invalid bucket size: '${bucketSize.toString()}' is not an integer`
)
}
if (bucketSize < 0) {
throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} < 0`)
}
this.bucketSize = bucketSize
this.head = this.tail = new FixedPriorityQueue(
this.bucketSize,
enablePriority
)
this.maxSize = 0
}
/**
* The priority queue size.
* @returns The priority queue size.
*/
public get size (): number {
let node: PriorityQueueNode<T> | undefined = this.tail
let size = 0
while (node != null) {
size += node.size
node = node.next
}
return size
}
public get enablePriority (): boolean {
return this.head.enablePriority
}
public set enablePriority (enablePriority: boolean) {
if (this.head.enablePriority === enablePriority) {
return
}
let node: PriorityQueueNode<T> | undefined = this.tail
while (node != null) {
node.enablePriority = enablePriority
node = node.next
}
}
/**
* The number of filled prioritized buckets.
* @returns The number of filled prioritized buckets.
*/
public get buckets (): number {
return Math.trunc(this.size / this.bucketSize)
}
/**
* Enqueue data into the priority queue.
* @param data - Data to enqueue.
* @param priority - Priority of the data. Lower values have higher priority.
* @returns The new size of the priority queue.
*/
public enqueue (data: T, priority?: number): number {
if (this.head.full()) {
this.head = this.head.next = new FixedPriorityQueue(
this.bucketSize,
this.enablePriority
)
}
this.head.enqueue(data, priority)
const size = this.size
if (size > this.maxSize) {
this.maxSize = size
}
return size
}
/**
* Dequeue data from the priority queue.
* @param bucket - The prioritized bucket to dequeue from.
* @returns The dequeued data or `undefined` if the priority queue is empty.
*/
public dequeue (bucket?: number): T | undefined {
let tail: PriorityQueueNode<T> | undefined = this.tail
let tailChanged = false
if (bucket != null && bucket > 0) {
let currentBucket = 1
while (tail != null) {
if (currentBucket === bucket) {
break
}
++currentBucket
tail = tail.next
}
tailChanged = tail !== this.tail
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const data = tail!.dequeue()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (tail!.empty()) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (!tailChanged && tail!.next != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.tail = tail!.next
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
delete tail!.next
} else if (tailChanged) {
let node: PriorityQueueNode<T> | undefined = this.tail
while (node != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (node.next === tail && tail!.next != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
node.next = tail!.next
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
delete tail!.next
break
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
} else if (node.next === tail && tail!.next == null) {
delete node.next
this.head = node
break
}
node = node.next
}
}
}
return data
}
/**
* Clears the priority queue.
*/
public clear (): void {
this.head = this.tail = new FixedPriorityQueue(
this.bucketSize,
this.enablePriority
)
this.maxSize = 0
}
/**
* Returns an iterator for the priority queue.
* @returns An iterator for the priority queue.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
*/
public [Symbol.iterator] (): Iterator<T> {
let index = 0
let node = this.tail
return {
next: () => {
const value = node.get(index) as T
if (value == null) {
return {
value: undefined,
done: true,
}
}
++index
if (index === node.capacity && node.next != null) {
node = node.next
index = 0
}
return {
value,
done: false,
}
},
}
}
}