X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Ffixed-priority-queue.ts;h=fadaee750515b3de72bcc6c5a9f6cd70de0b68cd;hb=f45a8925025d4b76849ea359542bb6bafd39695d;hp=60096ce04e9c77da42639420ba4a4a9675b9d2ea;hpb=f8d5d8fdf46b7182f5efa3503bb5172ec199fb45;p=poolifier.git diff --git a/src/fixed-priority-queue.ts b/src/fixed-priority-queue.ts index 60096ce0..fadaee75 100644 --- a/src/fixed-priority-queue.ts +++ b/src/fixed-priority-queue.ts @@ -14,6 +14,12 @@ export interface PriorityQueueNode { priority: number } +/** + * Fixed priority queue. + * + * @typeParam T - Type of fixed priority queue data. + * @internal + */ export class FixedPriorityQueue { private start!: number private readonly nodeArray: Array> @@ -39,18 +45,21 @@ export class FixedPriorityQueue { throw new Error('Priority queue is full') } priority = priority ?? 0 + const nodeArrayLength = this.nodeArray.length let inserted = false - for (let index = this.start; index < this.nodeArray.length; index++) { + for (let index = this.start; index < nodeArrayLength; index++) { if (this.nodeArray[index]?.priority > priority) { this.nodeArray.splice(index, 0, { data, priority }) inserted = true break } } + this.nodeArray.length !== nodeArrayLength && + (this.nodeArray.length = nodeArrayLength) if (!inserted) { let index = this.start + this.size - if (index >= this.nodeArray.length) { - index -= this.nodeArray.length + if (index >= nodeArrayLength) { + index -= nodeArrayLength } this.nodeArray[index] = { data, priority } } @@ -121,7 +130,7 @@ export class FixedPriorityQueue { private checkSize (size: number): void { if (!Number.isSafeInteger(size)) { throw new TypeError( - `Invalid fixed priority queue size: ${size} is not an integer` + `Invalid fixed priority queue size: '${size}' is not an integer` ) } if (size < 0) {