Merge branch 'master' into elu-strategy
[poolifier.git] / src / queue.ts
index 35f9025e121bb861729df62a0064a3aa0176387c..86b69051174672a92c158317f84beb1874cebf07 100644 (file)
@@ -2,28 +2,60 @@
 
 /**
  * Queue
+ *
+ * @typeParam T - Type of queue items.
  */
 export class Queue<T> {
   private items: Record<number, T>
   private head: number
   private tail: number
+  private max: number
 
   public constructor () {
     this.items = {}
     this.head = 0
     this.tail = 0
+    this.max = 0
   }
 
+  /**
+   * Get the size of the queue.
+   *
+   * @returns The size of the queue.
+   * @readonly
+   */
   public get size (): number {
     return this.tail - this.head
   }
 
+  /**
+   * Get the maximum size of the queue.
+   *
+   * @returns The maximum size of the queue.
+   * @readonly
+   */
+  public get maxSize (): number {
+    return this.max
+  }
+
+  /**
+   * Enqueue an item.
+   *
+   * @param item - Item to enqueue.
+   * @returns The new size of the queue.
+   */
   public enqueue (item: T): number {
     this.items[this.tail] = item
     this.tail++
+    if (this.size > this.max) this.max = this.size
     return this.size
   }
 
+  /**
+   * Dequeue an item.
+   *
+   * @returns The dequeued item or `undefined` if the queue is empty.
+   */
   public dequeue (): T | undefined {
     if (this.size <= 0) return undefined
     const item = this.items[this.head]
@@ -36,4 +68,12 @@ export class Queue<T> {
     }
     return item
   }
+
+  /**
+   * Peek at the first item.
+   */
+  public peek (): T | undefined {
+    if (this.size <= 0) return undefined
+    return this.items[this.head]
+  }
 }