File size: 1,888 Bytes
064bfd6 | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /**
* A fixed-size circular buffer that automatically evicts the oldest items
* when the buffer is full. Useful for maintaining a rolling window of data.
*/
export class CircularBuffer<T> {
private buffer: T[]
private head = 0
private size = 0
constructor(private capacity: number) {
this.buffer = new Array(capacity)
}
/**
* Add an item to the buffer. If the buffer is full,
* the oldest item will be evicted.
*/
add(item: T): void {
this.buffer[this.head] = item
this.head = (this.head + 1) % this.capacity
if (this.size < this.capacity) {
this.size++
}
}
/**
* Add multiple items to the buffer at once.
*/
addAll(items: T[]): void {
for (const item of items) {
this.add(item)
}
}
/**
* Get the most recent N items from the buffer.
* Returns fewer items if the buffer contains less than N items.
*/
getRecent(count: number): T[] {
const result: T[] = []
const start = this.size < this.capacity ? 0 : this.head
const available = Math.min(count, this.size)
for (let i = 0; i < available; i++) {
const index = (start + this.size - available + i) % this.capacity
result.push(this.buffer[index]!)
}
return result
}
/**
* Get all items currently in the buffer, in order from oldest to newest.
*/
toArray(): T[] {
if (this.size === 0) return []
const result: T[] = []
const start = this.size < this.capacity ? 0 : this.head
for (let i = 0; i < this.size; i++) {
const index = (start + i) % this.capacity
result.push(this.buffer[index]!)
}
return result
}
/**
* Clear all items from the buffer.
*/
clear(): void {
this.buffer.length = 0
this.head = 0
this.size = 0
}
/**
* Get the current number of items in the buffer.
*/
length(): number {
return this.size
}
}
|