baecaa35b248d98a6cd4d378b444cfe2a4d316f2
1 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
6 * @typeParam T - Type of queue items.
8 export class Queue
<T
> {
10 private offset
: number
12 public maxSize
: number
14 public constructor () {
17 /** The size of the queue. */
19 /** The maximum size of the queue. */
26 * @param item - Item to enqueue.
27 * @returns The new size of the queue.
29 public enqueue (item
: T
): number {
32 if (this.size
> this.maxSize
) {
33 this.maxSize
= this.size
41 * @returns The dequeued item or `undefined` if the queue is empty.
43 public dequeue (): T
| undefined {
47 const item
= this.items
[this.offset
]
48 if (++this.offset
* 2 >= this.items
.length
) {
49 this.items
= this.items
.slice(this.offset
)
57 * Peek at the first item.
59 * @returns The first item or `undefined` if the queue is empty.
61 public peek (): T
| undefined {
65 return this.items
[this.offset
]
71 public clear (): void {