X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpriority-queue.ts;h=2a266d28f6b54bc7c9655de941cb9b5de88a779d;hb=HEAD;hp=55a97169f757beb0f53c51843d6a8a87f6392612;hpb=bcfb06ce041a682baf396a099c633a848d6a4045;p=poolifier.git diff --git a/src/priority-queue.ts b/src/priority-queue.ts deleted file mode 100644 index 55a97169..00000000 --- a/src/priority-queue.ts +++ /dev/null @@ -1,98 +0,0 @@ -/** - * @internal - */ -interface PriorityQueueNode { - data: T - priority: number -} - -/** - * Priority queue. - * - * @typeParam T - Type of priority queue data. - * @internal - */ -export class PriorityQueue { - private nodeArray!: Array> - /** The size of the priority queue. */ - public size!: number - /** The maximum size of the priority queue. */ - public maxSize!: number - - public constructor () { - this.clear() - } - - /** - * 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. - */ - public enqueue (data: T, priority?: number): number { - priority = priority ?? 0 - let inserted = false - for (const [index, node] of this.nodeArray.entries()) { - if (node.priority > priority) { - this.nodeArray.splice(index, 0, { data, priority }) - inserted = true - break - } - } - if (!inserted) { - this.nodeArray.push({ data, priority }) - } - return this.incrementSize() - } - - /** - * Dequeue data from the priority queue. - * - * @returns The dequeued data or `undefined` if the priority queue is empty. - */ - public dequeue (): T | undefined { - if (this.size > 0) { - --this.size - } - return this.nodeArray.shift()?.data - } - - /** - * Peeks at the first data. - * @returns The first data or `undefined` if the priority queue is empty. - */ - public peekFirst (): T | undefined { - return this.nodeArray[0]?.data - } - - /** - * Peeks at the last data. - * @returns The last data or `undefined` if the priority queue is empty. - */ - public peekLast (): T | undefined { - return this.nodeArray[this.nodeArray.length - 1]?.data - } - - /** - * Clears the priority queue. - */ - public clear (): void { - this.nodeArray = [] - this.size = 0 - this.maxSize = 0 - } - - /** - * Increments the size of the deque. - * - * @returns The new size of the deque. - */ - private incrementSize (): number { - ++this.size - if (this.size > this.maxSize) { - this.maxSize = this.size - } - return this.size - } -}