ringidx

package
v0.0.0-...-531e5d3 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2023 License: BSD-3-Clause Imports: 0 Imported by: 0

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 {
	Zi  int `desc:"the zero index position -- where logical 0 is in physical buffer"`
	Len int `desc:"the length of the buffer -- wraps around at this modulus"`
}

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

func (*FIx) Idx

func (fi *FIx) Idx(i int) int

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

func (*FIx) IdxIsValid

func (fi *FIx) IdxIsValid(i int) bool

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

func (*FIx) Shift

func (fi *FIx) Shift(n int)

Shift moves the zero index up by n.

type Idx

type Idx struct {
	StIdx int `` /* 175-byte string literal not displayed */
	Len   int `desc:"the number of items stored starting at StIdx.  Capped at Max"`
	Max   int `desc:"the maximum number of items that can be stored in this ring"`
}

Idx is the ring index structure, 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 (*Idx) Add

func (ri *Idx) 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 (*Idx) Idx

func (ri *Idx) Idx(i int) int

Idx returns the index of the i'th item starting from StIdx. i must be < Len.

func (*Idx) IdxIsValid

func (ri *Idx) IdxIsValid(i int) bool

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

func (*Idx) LastIdx

func (ri *Idx) LastIdx() int

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

func (*Idx) Reset

func (ri *Idx) Reset()

Reset initializes start index and length to 0

func (*Idx) Shift

func (ri *Idx) 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