ring

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: Apache-2.0, BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer holds a slice-backed ring buffer. Elements can be added and removed at both the start and the end of the buffer.

Elements are indexed from zero (the start) to the end. Pushing elements at the start will implicitly reindex all previous elements.

The zero-value is OK to use.

func NewBuffer

func NewBuffer[T any](minCap int) *Buffer[T]

NewBuffer returns a buffer with at least the specified capacity.

func (*Buffer[T]) All

func (b *Buffer[T]) All() iter.Seq[T]

All returns an iterator over all the values in the buffer.

func (*Buffer[T]) Cap

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

Cap returns the capacity of the underlying buffer.

func (*Buffer[T]) Copy

func (b *Buffer[T]) Copy(dst []T, i int) int

Copy copies min(b.Len(), len(dst)) values into dst from index i in the buffer onwards. It does not affect the size of the buffer. It returns the number of elements actually copied. It panics if i is out of range.

func (*Buffer[T]) DiscardFromEnd

func (b *Buffer[T]) DiscardFromEnd(n int) int

DiscardFromEnd discards min(b.Len(), n) elements from the end of the buffer and returns the number actually discarded

func (*Buffer[T]) DiscardFromStart

func (b *Buffer[T]) DiscardFromStart(n int) int

DiscardFromStart discards min(b.Len(), n) elements from the start of the buffer and returns the number actually discarded

func (*Buffer[T]) Get

func (b *Buffer[T]) Get(i int) T

Get returns the i'th element in the buffer; the start element is at index zero; the end is at b.Len() - 1. It panics if i is out of range.

func (*Buffer[T]) Len

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

Len returns the number of elements in the buffer.

func (*Buffer[T]) PeekEnd

func (b *Buffer[T]) PeekEnd() T

PeekEnd returns the element at the end of the buffer without consuming it. It's equivalent to b.Get(b.Len()-1).

func (*Buffer[T]) PeekStart

func (b *Buffer[T]) PeekStart() T

PeekStart returns the element at the start of the buffer without consuming it. It's equivalent to b.Get(0), and panics if the buffer is empty.

func (*Buffer[T]) PopEnd

func (b *Buffer[T]) PopEnd() T

PopStart removes and returns the element from the end of the buffer. If the buffer is empty, the call will panic.

func (*Buffer[T]) PopStart

func (b *Buffer[T]) PopStart() T

PopStart removes and returns the element from the start of the buffer. If the buffer is empty, the call will panic.

func (*Buffer[T]) PushEnd

func (b *Buffer[T]) PushEnd(x T)

PushEnd adds an element to the end of the buffer.

func (*Buffer[T]) PushSliceEnd

func (b *Buffer[T]) PushSliceEnd(src []T)

PushSliceEnd pushes all the elements of the given slice onto the end of the buffer. It's just like:

for _, x := range src {
	b.PushEnd(x)
}

but more efficient.

func (*Buffer[T]) PushSliceStart

func (b *Buffer[T]) PushSliceStart(src []T)

PushSliceStart pushes all the elements of the given slice onto the end of the buffer. It's just like:

for i := len(src)-1; i>=0; i-- {
	b.PushStart(src[i])
}

but more efficient.

func (*Buffer[T]) PushStart

func (b *Buffer[T]) PushStart(x T)

PushStart pushes an element to the start of the buffer.

func (*Buffer[T]) SetCap

func (b *Buffer[T]) SetCap(n int)

SetCap sets the capacity of the underlying slice to at least max(n, b.Len()). This can be used to shrink the capacity an over-large buffer.

Note: the resulting capacity can still be as much as b.Len() * 2.

func (*Buffer[T]) String

func (b *Buffer[T]) String() string

Jump to

Keyboard shortcuts

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