Documentation
¶
Overview ¶
Package gocircular provides a generic circular buffer implementation.
A circular buffer (also known as a ring buffer) is a fixed-size data structure that uses a single, continuous buffer as if it were connected end-to-end. When the buffer is full, new elements overwrite the oldest elements.
Index ¶
- type Buffer
- func (b *Buffer[T]) All() iter.Seq2[int, T]
- func (b *Buffer[T]) At(i int) (T, bool)
- func (b *Buffer[T]) Back() (T, bool)
- func (b *Buffer[T]) Backward() iter.Seq2[int, T]
- func (b *Buffer[T]) Cap() int
- func (b *Buffer[T]) Clear()
- func (b *Buffer[T]) Clone() *Buffer[T]
- func (b *Buffer[T]) Do(f func(T) bool)
- func (b *Buffer[T]) Empty() bool
- func (b *Buffer[T]) Front() (T, bool)
- func (b *Buffer[T]) Full() bool
- func (b *Buffer[T]) Len() int
- func (b *Buffer[T]) PopBack() (T, bool)
- func (b *Buffer[T]) PopFront() (T, bool)
- func (b *Buffer[T]) PushBack(v T) bool
- func (b *Buffer[T]) PushFront(v T) bool
- func (b *Buffer[T]) Resize(n int)
- func (b *Buffer[T]) Set(i int, v T) bool
- func (b *Buffer[T]) ToSlice() []T
- func (b *Buffer[T]) Values() iter.Seq[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer[T any] struct { // contains filtered or unexported fields }
Buffer is a generic circular buffer with a fixed capacity. It supports efficient O(1) operations at both ends.
func New ¶
New creates a new circular buffer with the given capacity. Panics if capacity is less than 1.
func (*Buffer[T]) All ¶
All returns an iterator over all elements from front to back. The iterator yields index and value pairs.
func (*Buffer[T]) At ¶
At returns the element at the given index (0-based from front). Returns the zero value and false if the index is out of bounds.
func (*Buffer[T]) Back ¶
Back returns the back element without removing it. Returns the zero value and false if the buffer is empty.
func (*Buffer[T]) Backward ¶
Backward returns an iterator over all elements from back to front. The iterator yields index and value pairs (indices are still front-based).
func (*Buffer[T]) Do ¶
Do calls the function f on each element from front to back. If f returns false, iteration stops early.
func (*Buffer[T]) Front ¶
Front returns the front element without removing it. Returns the zero value and false if the buffer is empty.
func (*Buffer[T]) PopBack ¶
PopBack removes and returns the back element. Returns the zero value and false if the buffer is empty.
func (*Buffer[T]) PopFront ¶
PopFront removes and returns the front element. Returns the zero value and false if the buffer is empty.
func (*Buffer[T]) PushBack ¶
PushBack adds an element to the back of the buffer. If the buffer is full, the front element is overwritten. Returns true if an element was overwritten.
func (*Buffer[T]) PushFront ¶
PushFront adds an element to the front of the buffer. If the buffer is full, the back element is overwritten. Returns true if an element was overwritten.
func (*Buffer[T]) Resize ¶
Resize changes the buffer capacity. If shrinking, elements at the back are discarded to fit the new capacity. Panics if n is less than 1.
func (*Buffer[T]) Set ¶
Set updates the element at the given index (0-based from front). Returns false if the index is out of bounds.