X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fqueue.ts;h=86b69051174672a92c158317f84beb1874cebf07;hb=e460940e8d2a41a846b2334d54410807ffc2f421;hp=a2a613814e86dcd5ab13b7122d8c26a7e647d610;hpb=3db6a2b42ee697b6f587b0f41020771941a65e68;p=poolifier.git diff --git a/src/queue.ts b/src/queue.ts index a2a61381..86b69051 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -9,11 +9,13 @@ export class Queue { private items: Record private head: number private tail: number + private max: number public constructor () { this.items = {} this.head = 0 this.tail = 0 + this.max = 0 } /** @@ -26,6 +28,16 @@ export class Queue { 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. * @@ -35,14 +47,14 @@ export class 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. - * @returns `undefined` if the queue is empty. + * @returns The dequeued item or `undefined` if the queue is empty. */ public dequeue (): T | undefined { if (this.size <= 0) return undefined @@ -56,4 +68,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] + } }