This commit is contained in:
Schluffe
2026-03-17 23:26:16 +01:00
parent 3242e8cbbb
commit 31ae9d94d5
5 changed files with 51 additions and 36 deletions
+25
View File
@@ -0,0 +1,25 @@
export default class RingBuffer<T> {
size: number;
mask: number;
cursor: number;
buffer: Array<T>;
constructor(size: number, blueprint: T) {
if ((size & (size - 1)) !== 0) {
throw new Error("size must be a power of two");
}
this.size = size;
this.mask = size - 1;
this.cursor = 0;
this.buffer = [];
for (let i = 0; i < size; i++) {
this.buffer.push(structuredClone(blueprint));
}
}
next(): T {
return this.buffer[this.cursor++ & this.mask];
}
}