/**
 * Circular buffer implementation for efficient log storage
 */
/**
 * Circular buffer for storing log entries with O(1) add/remove
 */
export class CircularBuffer {
    entries;
    head = 0; // Index of oldest entry
    tail = 0; // Index where next entry goes
    count = 0; // Current number of entries
    maxEntries;
    constructor(maxEntries) {
        this.maxEntries = maxEntries;
        this.entries = new Array(maxEntries); // Pre-allocate array
    }
    /**
     * Add entry to buffer - O(1) operation
     * Returns the removed entry if buffer was full, undefined otherwise
     */
    add(entry) {
        let removed;
        // If buffer is full, remove oldest entry
        if (this.count === this.maxEntries) {
            removed = this.entries[this.head];
            // Advance head (oldest entry pointer)
            this.head = (this.head + 1) % this.maxEntries;
        }
        else {
            this.count++;
        }
        // Write new entry at tail position - O(1)
        this.entries[this.tail] = entry;
        this.tail = (this.tail + 1) % this.maxEntries;
        return removed;
    }
    /**
     * Get all entries as array (oldest to newest)
     */
    getAll() {
        const result = [];
        for (let i = 0; i < this.count; i++) {
            const entry = this.entries[(this.head + i) % this.maxEntries];
            if (entry) {
                result.push(entry);
            }
        }
        return result;
    }
    /**
     * Get current count of entries
     */
    getCount() {
        return this.count;
    }
    /**
     * Clear all entries
     */
    clear() {
        this.entries = new Array(this.maxEntries);
        this.head = 0;
        this.tail = 0;
        this.count = 0;
    }
}
//# sourceMappingURL=circular-buffer.js.map