86b69051174672a92c158317f84beb1874cebf07
1 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
6 * @typeParam T - Type of queue items.
8 export class Queue
<T
> {
9 private items
: Record
<number, T
>
14 public constructor () {
22 * Get the size of the queue.
24 * @returns The size of the queue.
27 public get
size (): number {
28 return this.tail
- this.head
32 * Get the maximum size of the queue.
34 * @returns The maximum size of the queue.
37 public get
maxSize (): number {
44 * @param item - Item to enqueue.
45 * @returns The new size of the queue.
47 public enqueue (item
: T
): number {
48 this.items
[this.tail
] = item
50 if (this.size
> this.max
) this.max
= this.size
57 * @returns The dequeued item or `undefined` if the queue is empty.
59 public dequeue (): T
| undefined {
60 if (this.size
<= 0) return undefined
61 const item
= this.items
[this.head
]
62 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
63 delete this.items
[this.head
]
65 if (this.head
=== this.tail
) {
73 * Peek at the first item.
75 public peek (): T
| undefined {
76 if (this.size
<= 0) return undefined
77 return this.items
[this.head
]