class documentation

A simple ring buffer implementation that allows for fixed-size storage of items. It overwrites the oldest item when the buffer is full.

Calling functions need to maintain their own index to track the current position in the buffer. This is useful where an arbitrary number of functions need to access the buffer concurrently, and each function needs to maintain its own position without interfering with others.

Method __getitem__ Get an item from the buffer by index.
Method __init__ Initialize a ring buffer with a fixed size.
Method append Add an item to the ring buffer. If the buffer is full, the oldest item is removed.
Method incrementHead Increment the head in a circular manner.
Method nextIndex Increment an index in a circular manner without modifying the current index.
Instance Variable buffer The internal storage for the ring buffer, initialized with None values.
Instance Variable capacity The maximum number of items the buffer can hold.
Instance Variable head The current head index of the ring buffer, initialized to -1 indicating an empty buffer.
def __getitem__(self, index: int) -> T | None:

Get an item from the buffer by index.

Parameters
index:intThe index of the item to retrieve.
Returns
T | NoneThe item at the specified index, or None if the index is out of bounds.
def __init__(self, size: int):

Initialize a ring buffer with a fixed size.

Parameters
size:intThe maximum number of items the buffer can hold.
def append(self, item: T):

Add an item to the ring buffer. If the buffer is full, the oldest item is removed.

Parameters
item:TThe item to add to the buffer.
def incrementHead(self) -> int:

Increment the head in a circular manner.

Returns
intThe new head value.
def nextIndex(self, index: int) -> int:

Increment an index in a circular manner without modifying the current index.

Parameters
index:intThe index to increment.
Returns
intThe new index value.
buffer: list[T] =

The internal storage for the ring buffer, initialized with None values.

capacity =

The maximum number of items the buffer can hold.

head =

The current head index of the ring buffer, initialized to -1 indicating an empty buffer.