X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpriority-queue.ts;h=2a266d28f6b54bc7c9655de941cb9b5de88a779d;hb=7169bda30538a5244b2598a4ef466c5687953ebd;hp=c613cd2e9ee393589a80950bda985bb7004f35ff;hpb=3a5027122ca6401ae1d755843b20f714c61e3240;p=poolifier.git diff --git a/src/priority-queue.ts b/src/priority-queue.ts index c613cd2e..2a266d28 100644 --- a/src/priority-queue.ts +++ b/src/priority-queue.ts @@ -40,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( @@ -56,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 @@ -84,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) @@ -126,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 }