piring

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package piring provides a ring buffer implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer is a ring buffer (circular queue) that allows reusing elements stored in the buffer.

It supports writing new elements. When full, the oldest element is overwritten with the new one. All elements can be accessed at any time by index.

func NewBuffer

func NewBuffer[E any](size int) *Buffer[E]

func (*Buffer[E]) Cap

func (b *Buffer[E]) Cap() int

Cap returns the maximum number of elements the buffer can hold.

func (*Buffer[E]) Len

func (b *Buffer[E]) Len() int

Len returns the number of elements currently stored in the buffer.

func (*Buffer[E]) NextWritePointer

func (b *Buffer[E]) NextWritePointer() *E

NextWritePointer returns a pointer to the next write location.

If there is no space left, the oldest element is returned. This allows reusing the oldest element and can help avoid allocations.

func (*Buffer[E]) PointerTo

func (b *Buffer[E]) PointerTo(index int) *E

PointerTo returns a pointer to the element at the given index.

PointerTo(0) returns the oldest element, while PointerTo(Len()-1) returns the newest. The index can be out of range — negative or greater than Len or Cap. In such cases, the index is wrapped until it fits within the buffer and corresponds to a valid element.

PointerTo always returns a pointer to an element in the buffer.

func (*Buffer[E]) Reset

func (b *Buffer[E]) Reset()

Reset clears the buffer logically for the user, but does not actually remove the underlying stored elements.

Jump to

Keyboard shortcuts

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