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
>
13 public constructor () {
20 * Get the size of the queue.
22 * @returns The size of the queue.
25 public get
size (): number {
26 return this.tail
- this.head
32 * @param item - Item to enqueue.
33 * @returns The new size of the queue.
35 public enqueue (item
: T
): number {
36 this.items
[this.tail
] = item
44 * @returns The dequeued item or `undefined` if the queue is empty.
46 public dequeue (): T
| undefined {
47 if (this.size
<= 0) return undefined
48 const item
= this.items
[this.head
]
49 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
50 delete this.items
[this.head
]
52 if (this.head
=== this.tail
) {
60 * Peek at the first item.
62 public peek (): T
| undefined {
63 if (this.size
<= 0) return undefined
64 return this.items
[this.head
]