ringidx

package
v2.0.0-dev0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

README

Docs: GoDoc

Package ringidx provides circular indexing logic for writing a given length of data into a fixed-sized buffer and wrapping around this buffer, overwriting the oldest data. No copying is required so it is highly efficient.

Documentation

Overview

Package ringidx provides circular indexing logic for writing a given length of data into a fixed-sized buffer and wrapping around this buffer, overwriting the oldest data. No copying is required so it is highly efficient

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FIx

type FIx struct {

	// the zero index position -- where logical 0 is in physical buffer
	Zi uint32

	// the length of the buffer -- wraps around at this modulus
	Len uint32
	// contains filtered or unexported fields
}

FIx is a fixed-length ring index structure -- does not grow or shrink dynamically.

func (*FIx) Index

func (fi *FIx) Index(i uint32) uint32

Index returns the physical index of the logical index i. i must be < Len.

func (*FIx) IndexIsValid

func (fi *FIx) IndexIsValid(i uint32) bool

IndexIsValid returns true if given index is valid: >= 0 and < Len

func (*FIx) Shift

func (fi *FIx) Shift(n uint32)

Shift moves the zero index up by n.

type Index

type Index struct {

	// Start the starting index where current data starts.
	// The oldest data is at this index, and continues for Len items,
	// wrapping around at Max, coming back up at most to Start-1.
	Start int

	// Len is the number of items stored starting at Start. Capped at Max.
	Len int

	// Max is the maximum number of items that can be stored in this ring.
	Max int
}

Index is the ring index structure for a dynamically-sized ring buffer, maintaining starting index and length into a ring-buffer with maximum length Max. Max must be > 0 and Len <= Max. When adding new items would overflow Max, starting index is shifted over to overwrite the oldest items with the new ones. No moving is ever required: just a fixed-length buffer of size Max.

func (*Index) Add

func (ri *Index) Add(n int)

Add adds given number of items to the ring (n <= Len. Shift is called for Len+n - Max extra items to make room.

func (*Index) Index

func (ri *Index) Index(i int) int

Index returns the index of the i'th item starting from Start. i must be < Len.

func (*Index) IndexIsValid

func (ri *Index) IndexIsValid(i int) bool

IndexIsValid returns true if given index is valid: >= 0 and < Len

func (*Index) LastIndex

func (ri *Index) LastIndex() int

LastIndex returns the index of the last (most recently added) item in the ring. Only valid if Len > 0

func (*Index) Reset

func (ri *Index) Reset()

Reset initializes start index and length to 0

func (*Index) Shift

func (ri *Index) Shift(n int)

Shift moves the starting index up by n, and decrements the Len by n as well. This is called prior to adding new items if doing so would exceed Max length.

Jump to

Keyboard shortcuts

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