Creates a new RingBuffer instance.
Maximum number of items the buffer can hold.
Optional
items: T[]Optional array of initial items to populate the buffer. If the number of items exceeds capacity, the oldest items will be discarded.
The capacity of the ring buffer.
The current size of the ring buffer.
An iterator for the items in the buffer.
Gets the item at index, allowing for positive and negative integers. Negative integers count back from the last item in the buffer.
The index of the item to get.
The item at the specified index.
Removes and returns the last item from the buffer.
The last item from the buffer, or undefined if the buffer is empty.
Adds items to the end of the buffer. If the buffer is full, it will delete the items at the head.
The items to add to the end of the buffer.
The new size of the buffer.
Sets an item at index, allowing for positive and negative integers. Negative integers count back from the last item in the buffer.
The index of the item to set.
The new item to set at the specified index.
Removes and returns the first item from the buffer.
The first item from the buffer, or undefined if the buffer is empty.
Adds items to the start of the buffer. If the buffer is full, it will delete the items at the tail.
The items to add to the start of the buffer.
The new size of the buffer.
A ring buffer (or circular buffer) is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. It is particularly useful for buffering data streams and implementing memory efficient queues and stacks.