circularbuffer

package
v0.10.9 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CircularBuffer

type CircularBuffer[E any] struct {
	// contains filtered or unexported fields
}

CircularBuffer represents a generic circular buffer implementation. It supports thread-safe operations and can be configured to either drop new data or overwrite old data when the buffer is full.

func New

func New[E any](size int, ops ...Option[E]) (*CircularBuffer[E], error)

New creates a new circular buffer with the specified size.

func NewFromSlice

func NewFromSlice[E any](slice []E, ops ...Option[E]) (*CircularBuffer[E], error)

NewFromSlice creates a new circular buffer initialized with the given slice. The buffer size will be equal to the length of the slice and will contain all elements from the slice.

func (*CircularBuffer[E]) Clear

func (cb *CircularBuffer[E]) Clear()

Clear removes all elements from the CircularBuffer.

func (*CircularBuffer[E]) Clone

func (cb *CircularBuffer[E]) Clone() *CircularBuffer[E]

Clone returns a copy of the CircularBuffer.

func (*CircularBuffer[E]) Dequeue

func (cb *CircularBuffer[E]) Dequeue() (E, bool)

Dequeue removes an element from the CircularBuffer. Returns zero value and false if the buffer is empty, otherwise the element and true.

func (*CircularBuffer[E]) Enqueue

func (cb *CircularBuffer[E]) Enqueue(e E) bool

Enqueue adds an element to the CircularBuffer. Returns false if the buffer is full, otherwise true.

func (*CircularBuffer[E]) IsEmpty

func (cb *CircularBuffer[E]) IsEmpty() bool

IsEmpty reports whether the CircularBuffer is empty.

func (*CircularBuffer[E]) IsFull

func (cb *CircularBuffer[E]) IsFull() bool

IsFull reports whether the CircularBuffer is full.

func (*CircularBuffer[E]) Len

func (cb *CircularBuffer[E]) Len() int

Len returns the number of elements in the CircularBuffer.

func (*CircularBuffer[E]) MarshalJSON

func (cb *CircularBuffer[E]) MarshalJSON() ([]byte, error)

func (*CircularBuffer[E]) Peek

func (cb *CircularBuffer[E]) Peek() (E, bool)

Peek returns the first element in the CircularBuffer without removing it. Returns zero value and false if the buffer is empty, otherwise the element and true.

func (*CircularBuffer[E]) Range

func (cb *CircularBuffer[E]) Range(fn func(e E) bool)

Range calls "fn" for each element in the CircularBuffer. function "fn" returns false to stop the iteration.

func (*CircularBuffer[E]) Slice

func (cb *CircularBuffer[E]) Slice() []E

Slice returns a slice of all elements in the CircularBuffer. It returns empty slice(not nil) if the CircularBuffer is empty.

func (*CircularBuffer[E]) String

func (cb *CircularBuffer[E]) String() string

String returns a string representation of the CircularBuffer.

func (*CircularBuffer[E]) UnmarshalJSON

func (cb *CircularBuffer[E]) UnmarshalJSON(data []byte) error

type Option

type Option[T any] func(*CircularBuffer[T]) error

Option is a function that can be used to configure a CircularBuffer.

func WithDrop

func WithDrop[T any]() Option[T]

WithDrop returns an Option that configures the CircularBuffer to drop elements when full.

func WithSafe

func WithSafe[T any]() Option[T]

WithSafe returns an Option that configures the CircularBuffer to be thread-safe.

Jump to

Keyboard shortcuts

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