Files
rea/src/kernel/FifoQueue.ts
T
morluto 91b45c0aee perf(model/uefi/rizin): cache GGUF/UEFI BinaryReaders and share O(1) FifoQueue (#13745)
* perf(model/uefi/rizin): cache GGUF/UEFI BinaryReaders and share O(1) FifoQueue

* perf(uefi): reuse GUID reader across firmware file headers
2026-11-22 19:21:55 -05:00

43 lines
970 B
TypeScript

/**
* Arrival-ordered FIFO queue whose dequeue advances a head offset.
* Compacting once half the backing slots are evicted keeps push/shift
* amortized O(1) without shifting remaining elements on every dequeue.
*/
export class FifoQueue<T> {
#items: (T | undefined)[] = [];
#head = 0;
get size(): number {
return this.#items.length - this.#head;
}
push(item: T): void {
this.#items.push(item);
}
peek(): T | undefined {
return this.#items[this.#head];
}
shift(): T | undefined {
if (this.#head >= this.#items.length) return undefined;
const item = this.#items[this.#head];
this.#items[this.#head] = undefined;
this.#head += 1;
if (this.#head * 2 >= this.#items.length) {
this.#items = this.#items.slice(this.#head);
this.#head = 0;
}
return item;
}
clear(): void {
this.#items = [];
this.#head = 0;
}
toArray(): T[] {
return this.#items.slice(this.#head) as T[];
}
}