X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpriority-queue.ts;h=2a266d28f6b54bc7c9655de941cb9b5de88a779d;hb=a17b6fe6a9b7cde367fa1f1a0a89f6ee5db46ad6;hp=ee3310c3d8f2cb8cf6e4d6287d0a668eb20f4a9c;hpb=fcfc3353eb4053c02f64c80a14ae142d44388a71;p=poolifier.git diff --git a/src/priority-queue.ts b/src/priority-queue.ts index ee3310c3..2a266d28 100644 --- a/src/priority-queue.ts +++ b/src/priority-queue.ts @@ -9,7 +9,6 @@ export const defaultBucketSize = 2048 /** * Priority queue node. - * * @typeParam T - Type of priority queue node data. * @internal */ @@ -19,7 +18,6 @@ export interface PriorityQueueNode extends FixedPriorityQueue { /** * Priority queue. - * * @typeParam T - Type of priority queue data. * @internal */ @@ -32,7 +30,6 @@ export class PriorityQueue { /** * Constructs a priority queue. - * * @param bucketSize - Prioritized bucket size. @defaultValue defaultBucketSize * @param enablePriority - Whether to enable priority. @defaultValue false * @returns PriorityQueue. @@ -43,11 +40,11 @@ export class 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( @@ -59,6 +56,7 @@ export class PriorityQueue { /** * The priority queue size. + * @returns The priority queue size. */ public get size (): number { let node: PriorityQueueNode | undefined = this.tail @@ -87,6 +85,7 @@ export class PriorityQueue { /** * The number of filled prioritized buckets. + * @returns The number of filled prioritized buckets. */ public get buckets (): number { return Math.trunc(this.size / this.bucketSize) @@ -94,7 +93,6 @@ export class PriorityQueue { /** * 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. @@ -116,7 +114,6 @@ export class PriorityQueue { /** * 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. */ @@ -131,29 +128,38 @@ export class PriorityQueue { } ++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 | 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 } @@ -171,7 +177,6 @@ export class PriorityQueue { /** * 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 */ @@ -184,7 +189,7 @@ export class PriorityQueue { if (value == null) { return { value: undefined, - done: true + done: true, } } ++index @@ -194,9 +199,9 @@ export class PriorityQueue { } return { value, - done: false + done: false, } - } + }, } } }