/**
* Priority queue node.
- *
* @typeParam T - Type of priority queue node data.
* @internal
*/
/**
* Priority queue.
- *
* @typeParam T - Type of priority queue data.
* @internal
*/
/**
* Constructs a priority queue.
- *
* @param bucketSize - Prioritized bucket size. @defaultValue defaultBucketSize
* @param enablePriority - Whether to enable priority. @defaultValue false
* @returns PriorityQueue.
) {
if (!Number.isSafeInteger(bucketSize)) {
throw new TypeError(
- `Invalid bucket size: '${bucketSize}' is not an integer`
+ `Invalid bucket size: '${bucketSize.toString()}' is not an integer`
)
}
if (bucketSize < 0) {
- throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`)
+ throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} < 0`)
}
this.bucketSize = bucketSize
this.head = this.tail = new FixedPriorityQueue(
/**
* The priority queue size.
+ * @returns The priority queue size.
*/
public get size (): number {
let node: PriorityQueueNode<T> | undefined = this.tail
/**
* 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.
/**
* 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.
*/
}
++currentBucket
tail = tail.next
- tailChanged = true
}
+ 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() && tail!.next != null) {
- if (!tailChanged) {
+ 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
- } else {
+ // 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) {
- if (node.next === tail) {
+ // 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
}
}
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- delete tail!.next
}
return data
}
/**
* 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
*/
if (value == null) {
return {
value: undefined,
- done: true
+ done: true,
}
}
++index
}
return {
value,
- done: false
+ done: false,
}
- }
+ },
}
}
}