ring

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: Apache-2.0 Imports: 0 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 is a deque maintained over a ring buffer.

The zero value is ready to use. See MakeBuffer() for initializing a Buffer with pre-allocated space.

Note: it is backed by a slice (unlike container/ring/ring_buffer.go which is backed by a linked list). There is also a container/ring/buffer.go, that is backed by a slice and can both grow and shrink and uses bit arithmetic. We should replace this implementation with that one.

func MakeBuffer

func MakeBuffer[T any](scratch []T) Buffer[T]

MakeBuffer creates a buffer.

scratch, if not nil, represents pre-allocated space that the Buffer takes ownership of. The whole backing array of the provided slice is taken over, included elements and available capacity.

func (*Buffer[T]) AddFirst

func (r *Buffer[T]) AddFirst(element T)

AddFirst add element to the front of the Buffer and doubles it's underlying slice if necessary.

func (*Buffer[T]) AddLast

func (r *Buffer[T]) AddLast(element T)

AddLast adds element to the end of the Buffer and doubles it's underlying slice if necessary.

func (*Buffer[T]) Cap

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

Cap returns the capacity of the Buffer.

func (*Buffer[T]) Discard

func (r *Buffer[T]) Discard()

Discard is like Reset, except it also does Resize(0) to nil out the underlying slice. This makes the backing storage for the slice available to GC if nobody else is referencing it. This is useful if r is still referenced, but *r will be reassigned.

See also Reset and Resize.

func (*Buffer[T]) Get

func (r *Buffer[T]) Get(pos int) T

Get returns an element at position pos in the Buffer (zero-based).

func (*Buffer[T]) GetFirst

func (r *Buffer[T]) GetFirst() T

GetFirst returns an element at the front of the Buffer.

func (*Buffer[T]) GetLast

func (r *Buffer[T]) GetLast() T

GetLast returns an element at the front of the Buffer.

func (*Buffer[T]) Len

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

Len returns the number of elements in the Buffer.

func (*Buffer[T]) RemoveFirst

func (r *Buffer[T]) RemoveFirst()

RemoveFirst removes a single element from the front of the Buffer.

func (*Buffer[T]) RemoveLast

func (r *Buffer[T]) RemoveLast()

RemoveLast removes a single element from the end of the Buffer.

func (*Buffer[T]) Reserve

func (r *Buffer[T]) Reserve(n int)

Reserve reserves the provided number of elements in the Buffer. It is illegal to reserve a size less than the r.Len().

If the Buffer already has a capacity of n or larger, this is a no-op.

func (*Buffer[T]) Reset

func (r *Buffer[T]) Reset()

Reset makes Buffer treat its underlying memory as if it were empty. This allows for reusing the same memory again without explicitly removing old elements. Note that this does not nil out the elements, so they're not made available to GC.

See also Discard.

func (*Buffer[T]) Resize

func (r *Buffer[T]) Resize(n int)

Resize changes the Buffer's storage to be of the specified size. It is illegal to resize to a size less than r.Len().

This is a more general version of Reserve: Reserve only ever grows the storage, whereas Resize() can also shrink it.

Note that, if n != r.Len(), Resize always allocates new storage, even when n is less than the current capacity. This can be useful to make the storage for a buffer that used to be large available for GC, but it can also be wasteful.

Jump to

Keyboard shortcuts

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