X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fqueue.ts;h=7682f65f952136743c1f81724a26e53264ac18ed;hb=956cfeccbd168a90f3d9acb1729975cf3f861a77;hp=11766e4e9109dc4356be0a4b589ee5e5ed9b7bd3;hpb=70c7d7d3af9c38417416ca9096a6ab3ae835b5d0;p=poolifier.git diff --git a/src/queue.ts b/src/queue.ts index 11766e4e..7682f65f 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -1,5 +1,9 @@ +// Copyright Jerome Benoit. 2021-2023. All Rights Reserved. + /** * Queue + * + * @typeParam T - Type of queue items. */ export class Queue { private items: Record @@ -12,16 +16,33 @@ export class Queue { this.tail = 0 } + /** + * Get the size of the queue. + * + * @returns The size of the queue. + * @readonly + */ public get size (): number { return this.tail - this.head } + /** + * 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++ 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] @@ -34,4 +55,12 @@ export class Queue { } return item } + + /** + * Peek at the first item. + */ + public peek (): T | undefined { + if (this.size <= 0) return undefined + return this.items[this.head] + } }