Files
caprover/src/utils/CircularQueue.ts
T
2020-01-19 16:04:57 -05:00

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]
}
}