X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fqueue.ts;h=86b69051174672a92c158317f84beb1874cebf07;hb=4134429293801daed1762d927f66cee979de8864;hp=ab606547b1f77fb08ab6b203e0053ad182b481e6;hpb=557991e8ddb66a72a47d62606e142e8be86677f2;p=poolifier.git diff --git a/src/queue.ts b/src/queue.ts index ab606547..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,6 +47,7 @@ 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 } @@ -55,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] + } }