mirror of
https://github.com/caprover/caprover
synced 2026-05-05 03:00:34 +00:00
23 lines
683 B
TypeScript
23 lines
683 B
TypeScript
export default class CircularQueue<T> {
|
|
private values: (T | undefined)[] = []
|
|
private currSize = 0
|
|
|
|
constructor(private maxSize: number) {
|
|
if (!this.maxSize) throw new Error('invalid size of zero')
|
|
if (this.maxSize === 1) throw new Error('invalid size of one')
|
|
for (let index = 0; index < this.maxSize; index++) {
|
|
this.values.push(undefined)
|
|
}
|
|
}
|
|
|
|
push(value: T) {
|
|
this.values[this.currSize % this.maxSize] = value
|
|
this.currSize++
|
|
}
|
|
|
|
peek(): T | undefined {
|
|
const nextPositionToBeOverwritten = this.currSize % this.maxSize
|
|
return this.values[nextPositionToBeOverwritten]
|
|
}
|
|
}
|