Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RingBuffer ¶
type RingBuffer[T any] struct { // contains filtered or unexported fields }
RinfBugger (FIFO) with any elements
func NewRingBuffer ¶
func NewRingBuffer[T any](values ...T) *RingBuffer[T]
NewRingBuffer creates and initializes a new ring buffer. The capacity of underlying slice increases dynamically
Example ¶
package main import ( "fmt" "github.com/mfmayer/algos/ringbuffer" ) func main() { // Creates ring buffer (FIFO) with fixed capacity of 5 elements rFixed := ringbuffer.NewRingBuffer(1, 2, 3) // Push 5 elements into the ring buffer rFixed.Push(4, 5) for rFixed.Len() > 0 { fmt.Printf("%v ", rFixed.Pop()) } rFixed.Push(6, 7, 8, 9, 10) // pushing additional elements into the ringbuffer for rFixed.Len() > 0 { fmt.Printf("%v ", rFixed.Pop()) } }
Output: 1 2 3 4 5 6 7 8 9 10
func NewRingBufferWithFixedCapacity ¶
func NewRingBufferWithFixedCapacity[T any](capacity int, values ...T) *RingBuffer[T]
NewRingBufferWithFixedCapacity creates and initialized a new ring buffer with a fixed capacaity. Pushing more values into the buffer than it can store results in dropping first inserted values first.
Example ¶
package main import ( "fmt" "github.com/mfmayer/algos/ringbuffer" ) func main() { // Creates ring buffer (FIFO) with fixed capacity of 5 elements rFixed := ringbuffer.NewRingBufferWithFixedCapacity[int](5) // Push 5 elements into the ring buffer rFixed.Push(1, 2, 3, 4, 5) rFixed.Push(6) // pushing another element 6 drops element 1, because ring buffer is full for rFixed.Len() > 0 { fmt.Printf("%v ", rFixed.Pop()) } }
Output: 2 3 4 5 6
func (*RingBuffer[T]) Len ¶
func (r *RingBuffer[T]) Len() int
Len returns the number of values in the ring buffer
func (*RingBuffer[T]) PeekLastIn ¶
func (r *RingBuffer[T]) PeekLastIn() (value T)
PeekLastIn to see what value was lastly pushed into the ring buffer
func (*RingBuffer[T]) PeekNextOut ¶
func (r *RingBuffer[T]) PeekNextOut() (value T)
PeekNextOut to see what value would pop next (without popping it)
func (*RingBuffer[T]) Push ¶
func (r *RingBuffer[T]) Push(values ...T)
Push values into the ring buffer. If overwrite is enabled and no capacity left oldest calues will be overwritten first. Otherwise the underlying buffer size will be dynamically increased.