ring

package
v1.3.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

ring implements RingBuffer

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Numeric

type Numeric interface {
	~int | ~int64 | ~float64
}

Numeric is a constraint that allows only numeric types (int, int64, float64, etc.)

type RingBuffer

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

RingBuffer is a generic fixed-size circular buffer for numeric types.

func NewRingBuffer

func NewRingBuffer[T Numeric](size int) *RingBuffer[T]

NewRingBuffer creates a new RingBuffer of the given size.

func (*RingBuffer[T]) Add

func (rb *RingBuffer[T]) Add(value T)

Add adds a new element to the buffer.

func (*RingBuffer[T]) AverageNonZero

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

AverageNonZero calculates the average of non-zero elements in the buffer.

func (*RingBuffer[T]) GetAll

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

GetAll retrieves all elements from the buffer in order.

func (*RingBuffer[T]) GetLast

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

GetLast retrieves the last element from the buffer. If the buffer is empty, it returns the zero value of the type.

func (*RingBuffer[T]) GetSecondLast added in v1.1.3

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

GetSecondLast retrieves the second-to-last element from the buffer. If the buffer has fewer than two elements, it returns the zero value of the type.

Jump to

Keyboard shortcuts

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