ringbuffer

package module
v0.0.0-...-ece2149 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2022 License: BSD-3-Clause Imports: 1 Imported by: 0

README

An easy to use RingBuffer in Go

First of all run: go get https://github.com/floscodes/golang-ringbuffer

Usage:

func main() {
	rb := ringbuffer.New(6) // creates a new ringbuffer with capacity 6

	rb.Push(1)       // pushes value 1 to buffer
	rb.Push(2, 3)    // pushes values 2 and 3 to buffer
	rb.Push(4, 5, 6) // pushes values 4, 5 and 6 to buffer

	v, err := rb.Pop()  // returns value 1 from buffer
	fmt.Println(v, err) // Output: 1 <nil>

	v, err = rb.PopMany(2) // returns a slice with the values 2 and 3 from the buffer
	fmt.Println(v, err)    // Output: [2 3] <nil>

}

read the docs for more -> Go Reference

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RingBuffer

type RingBuffer struct {
	// contains filtered or unexported fields
}

func New

func New(s uint) RingBuffer

Returns a new RingBuffer with capacity s

func (*RingBuffer) Capacity

func (rb *RingBuffer) Capacity() int

Returns the capacity of the RingBuffer

func (*RingBuffer) Clear

func (rb *RingBuffer) Clear()

Emptys the RingBuffer

func (*RingBuffer) IsEmpty

func (rb *RingBuffer) IsEmpty() bool

Checks if the RingBuffer ist empty

func (*RingBuffer) IsFull

func (rb *RingBuffer) IsFull() bool

Checks if the Ringbuffer is full

func (*RingBuffer) Occupied

func (rb *RingBuffer) Occupied() int

Returns the occupied capacity of the RingBuffer

func (*RingBuffer) Pop

func (rb *RingBuffer) Pop() (interface{}, error)

Returns the oldest element of the RingBuffer

func (*RingBuffer) PopMany

func (rb *RingBuffer) PopMany(number uint) ([]interface{}, error)

Returns a given number(n) of elements of the RingBuffer starting with the oldest one

func (*RingBuffer) Push

func (rb *RingBuffer) Push(elements ...interface{}) error

Pushes one or more elements to the Ringbuffer

func (*RingBuffer) Remaining

func (rb *RingBuffer) Remaining() int

Returns the remaining capacity of the RingBuffer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL