perf: optimize tasks queuing implementation
[poolifier.git] / src / fixed-priority-queue.ts
index 1cc1480a1b5521d18369cff68a4c2282971f6f85..1597860e3f08bfde4df64cdff149e52e154ab005 100644 (file)
@@ -1,89 +1,59 @@
-/**
- * Default buffer size.
- */
-export const defaultQueueSize = 2048
-
-/**
- * Fixed priority queue node.
- * @typeParam T - Type of priority queue node data.
- * @internal
- */
-export interface FixedPriorityQueueNode<T> {
-  data: T
-  priority: number
-}
+import { defaultQueueSize, type FixedQueueNode, type IFixedQueue } from './utility-types.js'
 
 /**
  * Fixed priority queue.
  * @typeParam T - Type of fixed priority queue data.
  * @internal
  */
-export class FixedPriorityQueue<T> {
+export class FixedPriorityQueue<T> implements IFixedQueue<T> {
   private start!: number
-  private readonly nodeArray: FixedPriorityQueueNode<T>[]
-  /** The fixed priority queue capacity. */
+  /** @inheritdoc */
   public readonly capacity: number
-  /** The fixed priority queue size. */
+  /** @inheritdoc */
   public size!: number
-  /** Whether to enable priority. */
-  public enablePriority: boolean
+  /** @inheritdoc */
+  public nodeArray: FixedQueueNode<T>[]
 
   /**
    * 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, enablePriority = false) {
+  constructor (size: number = defaultQueueSize) {
     this.checkSize(size)
     this.capacity = size
-    this.enablePriority = enablePriority
-    this.nodeArray = new Array<FixedPriorityQueueNode<T>>(this.capacity)
+    this.nodeArray = new Array<FixedQueueNode<T>>(this.capacity)
     this.clear()
   }
 
-  /**
-   * Checks if the fixed priority queue is empty.
-   * @returns `true` if the fixed priority queue is empty, `false` otherwise.
-   */
+  /** @inheritdoc */
   public empty (): boolean {
     return this.size === 0
   }
 
-  /**
-   * Checks if the fixed priority queue is full.
-   * @returns `true` if the fixed priority queue is full, `false` otherwise.
-   */
+  /** @inheritdoc */
   public full (): boolean {
     return this.size === this.capacity
   }
 
-  /**
-   * Enqueue data into the fixed 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.
-   * @throws If the fixed priority queue is full.
-   */
+  /** @inheritdoc */
   public enqueue (data: T, priority?: number): number {
     if (this.full()) {
       throw new Error('Priority queue is full')
     }
     priority = priority ?? 0
     let inserted = false
-    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
-          inserted = true
-          break
-        }
-        ++index
-        if (index === this.capacity) {
-          index = 0
-        }
+    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
+        inserted = true
+        break
+      }
+      ++index
+      if (index === this.capacity) {
+        index = 0
       }
     }
     if (!inserted) {
@@ -96,11 +66,7 @@ export class FixedPriorityQueue<T> {
     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.
-   */
+  /** @inheritdoc */
   public get (index: number): T | undefined {
     if (this.empty() || index >= this.size) {
       return undefined
@@ -112,10 +78,7 @@ export class FixedPriorityQueue<T> {
     return this.nodeArray[index].data
   }
 
-  /**
-   * Dequeue data from the fixed priority queue.
-   * @returns The dequeued data or `undefined` if the priority queue is empty.
-   */
+  /** @inheritdoc */
   public dequeue (): T | undefined {
     if (this.empty()) {
       return undefined
@@ -129,19 +92,13 @@ export class FixedPriorityQueue<T> {
     return this.nodeArray[index].data
   }
 
-  /**
-   * Clears the fixed priority queue.
-   */
+  /** @inheritdoc */
   public clear (): void {
     this.start = 0
     this.size = 0
   }
 
-  /**
-   * Returns an iterator for the fixed priority queue.
-   * @returns An iterator for the fixed priority queue.
-   * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
-   */
+  /** @inheritdoc */
   public [Symbol.iterator] (): Iterator<T> {
     let index = this.start
     let i = 0
@@ -168,7 +125,7 @@ export class FixedPriorityQueue<T> {
   }
 
   /**
-   * Checks the queue size.
+   * Checks the fixed queue size.
    * @param size - Queue size.
    */
   private checkSize (size: number): void {