Documentation
¶
Index ¶
- type BufferType
- type RingBuffer
- func (rb *RingBuffer[T]) Capacity() int
- func (rb *RingBuffer[T]) IsEmpty() bool
- func (rb *RingBuffer[T]) IsFull() bool
- func (rb *RingBuffer[T]) Length() int
- func (rb *RingBuffer[T]) NewSize(capacity int) (*RingBuffer[T], error)
- func (rb *RingBuffer[T]) Read() (result []T)
- func (rb *RingBuffer[T]) Reset()
- func (rb *RingBuffer[T]) Size() int
- func (rb *RingBuffer[T]) String() string
- func (rb *RingBuffer[T]) Write(value T)
- func (rb *RingBuffer[T]) WriteMany(values []T) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferType ¶
type BufferType interface {
int | int16 | int32 | int64 |
byte | uint | uint16 | uint32 | uint64 |
float32 | float64 | bool | string
}
BufferType provides constraints on the types that may be used for a New RingBuffer
type RingBuffer ¶
type RingBuffer[T BufferType] struct { // contains filtered or unexported fields }
RingBuffer is effectively a fixed-size container as a data structure. Fields defined in this struct are named in the context as a fixed-size container.
func New ¶
func New[T BufferType](capacity int) (*RingBuffer[T], error)
New is effectively a constructor that creates a new ring buffer with a fixed, zero-indexed capacity and specified type constrained by the BufferType interface
func (*RingBuffer[T]) Capacity ¶
func (rb *RingBuffer[T]) Capacity() int
Capacity returns the zero-indexed capacity of the ring buffer itself, as opposed to the number of elements within the buffer.
For getting the number of elements in a buffer, use Length()
func (*RingBuffer[T]) IsEmpty ¶
func (rb *RingBuffer[T]) IsEmpty() bool
IsEmpty returns a boolean indicating if the number of elements or values within the buffer is zero. Additionally, the write index must be zero.
func (*RingBuffer[T]) IsFull ¶
func (rb *RingBuffer[T]) IsFull() bool
IsFull returns a boolean indicating if the number of elements or values of the buffer equals the buffer capacity or size.
func (*RingBuffer[T]) Length ¶
func (rb *RingBuffer[T]) Length() int
Length returns the number of elements or values within the buffer.
For getting the total capacity of the buffer, use Capacity() or Size()
func (*RingBuffer[T]) NewSize ¶
func (rb *RingBuffer[T]) NewSize(capacity int) (*RingBuffer[T], error)
NewSize recreates a new ring buffer with a different capacity or size, but with the same data as the old ring buffer. The NEW capacity cannot be smaller than the number of values or elements contained in the OLD buffer.
func (*RingBuffer[T]) Read ¶
func (rb *RingBuffer[T]) Read() (result []T)
Read returns the contents of the buffer in "First-In First-Out" (FIFO) order
func (*RingBuffer[T]) Reset ¶
func (rb *RingBuffer[T]) Reset()
Reset deletes all data within the buffer by re-allocation but retains the same exact capacity
func (*RingBuffer[T]) Size ¶
func (rb *RingBuffer[T]) Size() int
Size returns the zero-indexed capacity of the ring buffer itself, as opposed to the number of elements within the buffer.
Size works exactly the same as Capacity() because it calls Capacity() directly in order to retain backwards compatability.
For getting the number of elements in a buffer, use Length()
func (*RingBuffer[T]) String ¶
func (rb *RingBuffer[T]) String() string
String converts the capacity, writeIndex pointer, count of elements, and contents of the ring buffer into a string, then returns that string
func (*RingBuffer[T]) Write ¶
func (rb *RingBuffer[T]) Write(value T)
Write inserts one element into the thread-safe buffer, overwriting the oldest element (without error) if the buffer is full
func (*RingBuffer[T]) WriteMany ¶
func (rb *RingBuffer[T]) WriteMany(values []T) error
WriteMany first checks if the number of values is greater than the buffer or if the length of values is zero. If either of those conditions are true, then their respective error is returned.
Otherwise, WriteMany iterates over each slice of elements or values passed into it and calls Write for each element / value.