You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
528 B
TypeScript
32 lines
528 B
TypeScript
export class RingBuffer<T> {
|
|
private buffer: Array<T> = [];
|
|
private readonly capacity: number;
|
|
|
|
constructor(capacity: number) {
|
|
this.capacity = capacity;
|
|
}
|
|
|
|
public getCapacity(): number {
|
|
return this.capacity;
|
|
}
|
|
|
|
public add(item: T) {
|
|
this.buffer.push(item);
|
|
this.crop();
|
|
}
|
|
|
|
public has(item: T) {
|
|
return this.buffer.includes(item);
|
|
}
|
|
|
|
public clear() {
|
|
this.buffer = [];
|
|
}
|
|
|
|
private crop() {
|
|
while (this.buffer.length > this.capacity) {
|
|
this.buffer.shift();
|
|
}
|
|
}
|
|
}
|