X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Ffixed-priority-queue.ts;h=792f23d7e1347558cfdb55d30bc1436ad848ecdf;hb=8f823919891065f39bcec123be14ecc8fdc6d005;hp=b50853dd596db8f63d0fd6182e18c38b523b4f1c;hpb=65c178f18dbb814d8f15aec51c993e2ed1f79901;p=poolifier.git diff --git a/src/fixed-priority-queue.ts b/src/fixed-priority-queue.ts index b50853dd..792f23d7 100644 --- a/src/fixed-priority-queue.ts +++ b/src/fixed-priority-queue.ts @@ -23,19 +23,24 @@ export interface FixedPriorityQueueNode { export class FixedPriorityQueue { private start!: number private readonly nodeArray: Array> + /** The fixed priority queue capacity. */ public readonly capacity: number + /** The fixed priority queue size. */ public size!: number - public maxSize!: number + /** Whether to enable priority. */ + public enablePriority: boolean /** * Constructs a fixed priority queue. * * @param size - Fixed priority queue size. @defaultValue defaultQueueSize + * @param enablePriority - Whether to enable priority. @defaultValue false * @returns FixedPriorityQueue. */ - constructor (size: number = defaultQueueSize) { + constructor (size: number = defaultQueueSize, enablePriority = false) { this.checkSize(size) this.capacity = size + this.enablePriority = enablePriority this.nodeArray = new Array>(this.capacity) this.clear() } @@ -64,27 +69,30 @@ export class FixedPriorityQueue { * @param data - Data to enqueue. * @param priority - Priority of the data. Lower values have higher priority. * @returns The new size of the priority queue. + * @throws If the fixed priority queue is full. */ public enqueue (data: T, priority?: number): number { if (this.full()) { throw new Error('Priority queue is full') } priority = priority ?? 0 - let index = this.start let inserted = false - for (let i = 0; i < this.size; i++) { - if (this.nodeArray[index]?.priority > priority) { - this.nodeArray.splice(index, 0, { data, priority }) - inserted = true - break - } - ++index - if (index === this.capacity) { - index = 0 + if (this.enablePriority) { + let index = this.start + for (let i = 0; i < this.size; i++) { + if (this.nodeArray[index].priority > priority) { + this.nodeArray.splice(index, 0, { data, priority }) + this.nodeArray.length !== this.capacity && + (this.nodeArray.length = this.capacity) + inserted = true + break + } + ++index + if (index === this.capacity) { + index = 0 + } } } - this.nodeArray.length !== this.capacity && - (this.nodeArray.length = this.capacity) if (!inserted) { let index = this.start + this.size if (index >= this.capacity) { @@ -92,7 +100,24 @@ export class FixedPriorityQueue { } this.nodeArray[index] = { data, priority } } - return this.incrementSize() + return ++this.size + } + + /** + * Gets data from the fixed priority queue. + * + * @param index - The index of the data to get. + * @returns The data at the index or `undefined` if the fixed priority queue is empty or the index is out of bounds. + */ + public get (index: number): T | undefined { + if (this.empty() || index >= this.size) { + return undefined + } + index += this.start + if (index >= this.capacity) { + index -= this.capacity + } + return this.nodeArray[index].data } /** @@ -119,7 +144,6 @@ export class FixedPriorityQueue { public clear (): void { this.start = 0 this.size = 0 - this.maxSize = 0 } /** @@ -154,22 +178,9 @@ export class FixedPriorityQueue { } /** - * Increments the size of the fixed priority queue. - * - * @returns The new size of the fixed priority queue. - */ - private incrementSize (): number { - ++this.size - if (this.size > this.maxSize) { - this.maxSize = this.size - } - return this.size - } - - /** - * Checks the size. + * Checks the queue size. * - * @param size - The size to check. + * @param size - Queue size. */ private checkSize (size: number): void { if (!Number.isSafeInteger(size)) {