* 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) {
+ public constructor (
+ bucketSize: number = defaultBucketSize,
+ enablePriority = false
+ ) {
if (!Number.isSafeInteger(bucketSize)) {
throw new TypeError(
`Invalid bucket size: '${bucketSize}' is not an integer`
throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`)
}
this.bucketSize = bucketSize
- this.clear()
+ this.head = this.tail = new FixedPriorityQueue(
+ this.bucketSize,
+ enablePriority
+ )
+ this.maxSize = 0
}
/**
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.
*/
*/
public enqueue (data: T, priority?: number): number {
if (this.head.full()) {
- this.head = this.head.next = new FixedPriorityQueue(this.bucketSize)
+ this.head = this.head.next = new FixedPriorityQueue(
+ this.bucketSize,
+ this.enablePriority
+ )
}
this.head.enqueue(data, priority)
const size = this.size
* Clears the priority queue.
*/
public clear (): void {
- this.head = this.tail = new FixedPriorityQueue(this.bucketSize)
+ this.head = this.tail = new FixedPriorityQueue(
+ this.bucketSize,
+ this.enablePriority
+ )
this.maxSize = 0
}