ringbuffer

package
v0.9.43 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item[T any] struct {
	Timestamp int64
	Value     T
}

Item represents a single item in the ring buffer with a timestamp.

type RingBuffer

type RingBuffer[T any] struct {
	// contains filtered or unexported fields
}

RingBuffer is a fixed-size circular buffer that stores items ordered by timestamp. It automatically removes old items when the buffer is full or when cleanup is triggered. Uses an internal map index for O(1) Get operations.

func New

func New[T any](capacity int) *RingBuffer[T]

New creates a new RingBuffer with the specified capacity.

func (*RingBuffer[T]) Capacity

func (rb *RingBuffer[T]) Capacity() int

Capacity returns the maximum capacity of the buffer.

func (*RingBuffer[T]) CleanupBefore

func (rb *RingBuffer[T]) CleanupBefore(cutoff int64) int

CleanupBefore removes all items with timestamps before the cutoff. This is an O(k) operation where k is the number of items to remove.

func (*RingBuffer[T]) Clear

func (rb *RingBuffer[T]) Clear()

Clear removes all items from the buffer.

func (*RingBuffer[T]) Get

func (rb *RingBuffer[T]) Get(timestamp int64) (T, bool)

Get retrieves an item by timestamp in O(1) time. Returns the value and true if found, zero value and false otherwise.

func (*RingBuffer[T]) GetAll

func (rb *RingBuffer[T]) GetAll() []Item[T]

GetAll returns a slice of all items currently in the buffer. Items are returned in order from oldest to newest.

func (*RingBuffer[T]) Len

func (rb *RingBuffer[T]) Len() int

Len returns the current number of items in the buffer.

func (*RingBuffer[T]) Push

func (rb *RingBuffer[T]) Push(timestamp int64, value T)

Push adds a new item to the ring buffer. If an item with the same timestamp exists, it updates the value. If the buffer is full, the oldest item is removed.

func (*RingBuffer[T]) Range

func (rb *RingBuffer[T]) Range(fn func(timestamp int64, value T) bool)

Range iterates over all items in the buffer, calling fn for each item. If fn returns false, iteration stops. Items are visited in order from oldest to newest.

Jump to

Keyboard shortcuts

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