add method

void add(
  1. T item
)

Adds an item to the buffer in O(1) time. If the buffer is full, overwrites the oldest item.

Implementation

void add(T item) {
  _buffer[_head] = item;
  _head = (_head + 1) % capacity;

  if (_count < capacity) {
    _count++;
  } else {
    // Overwrote the oldest item, advance tail
    _tail = (_tail + 1) % capacity;
  }
}