X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpriority-queue.ts;h=b4d70b5ea8424c444570ac8acc4b03d2e33d4530;hb=f45a8925025d4b76849ea359542bb6bafd39695d;hp=e68fe37d16e732efc907562adbb9716b9793c612;hpb=65729b19f8371b8034d61c7771c8ece6e3692260;p=poolifier.git diff --git a/src/priority-queue.ts b/src/priority-queue.ts index e68fe37d..b4d70b5e 100644 --- a/src/priority-queue.ts +++ b/src/priority-queue.ts @@ -20,7 +20,7 @@ export interface PriorityQueueNode { export class PriorityQueue { private nodeArray!: Array> /** Prioritized bucket size. */ - private readonly k: number + private readonly bucketSize: number /** The size of the priority queue. */ public size!: number /** The maximum size of the priority queue. */ @@ -30,22 +30,27 @@ export class PriorityQueue { * The number of filled prioritized buckets. */ public get buckets (): number { - return this.k === Infinity ? 1 : Math.trunc(this.nodeArray.length / this.k) + return this.bucketSize === Number.POSITIVE_INFINITY + ? 1 + : Math.trunc(this.nodeArray.length / this.bucketSize) } /** * Constructs a priority queue. * - * @param k - Prioritized bucket size. @defaultValue Infinity + * @param bucketSize - Prioritized bucket size. @defaultValue Number.POSITIVE_INFINITY */ - public constructor (k = Infinity) { - if (k !== Infinity && !Number.isSafeInteger(k)) { - throw new TypeError('k must be an integer') + public constructor (bucketSize = Number.POSITIVE_INFINITY) { + if ( + bucketSize !== Number.POSITIVE_INFINITY && + !Number.isSafeInteger(bucketSize) + ) { + throw new TypeError('bucketSize must be an integer') } - if (k < 1) { - throw new RangeError('k must be greater than or equal to 1') + if (bucketSize < 1) { + throw new RangeError('bucketSize must be greater than or equal to 1') } - this.k = k + this.bucketSize = bucketSize this.clear() } @@ -58,7 +63,10 @@ export class PriorityQueue { */ public enqueue (data: T, priority?: number): number { priority = priority ?? 0 - const startIndex = this.k === Infinity ? 0 : this.buckets * this.k + const startIndex = + this.bucketSize === Number.POSITIVE_INFINITY + ? 0 + : this.buckets * this.bucketSize let inserted = false for (let index = startIndex; index < this.nodeArray.length; index++) { if (this.nodeArray[index].priority > priority) { @@ -80,9 +88,9 @@ export class PriorityQueue { * @returns The dequeued data or `undefined` if the priority queue is empty. */ public dequeue (bucket = 0): T | undefined { - if (this.k !== Infinity && bucket > 0) { + if (this.bucketSize !== Number.POSITIVE_INFINITY && bucket > 0) { while (bucket > 0) { - const index = bucket * this.k + const index = bucket * this.bucketSize // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (this.nodeArray[index] != null) { --this.size