Spaces:
Running
on
L40S
Running
on
L40S
File size: 715 Bytes
d69879c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/**
* Circular buffer for storing and managing response times.
*/
export class CircularBuffer<T> {
private buffer: T[];
private pointer: number;
constructor(private capacity: number) {
this.buffer = new Array<T>(capacity);
this.pointer = 0;
}
/**
* Adds an item to the buffer, overwriting the oldest item if full.
* @param item - The item to add to the buffer.
*/
push(item: T): void {
this.buffer[this.pointer] = item;
this.pointer = (this.pointer + 1) % this.capacity;
}
/**
* Retrieves all items currently in the buffer.
* @returns An array of all items in the buffer.
*/
getAll(): T[] {
return this.buffer.filter(item => item !== undefined);
}
}
|