ringbuf

package module
v0.0.0-...-80cf31c Latest Latest
Warning

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

Go to latest
Published: May 11, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

ringbuf provides the RingBuf data structure.

A RingBuf is a slice of contiguous data that acts as a fixed-length queue. Writes exceeding the capacity of the RingBuf will wrap around to the front, and begin overwriting values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[T any] struct {
	*RingBuf[T]
}

Iterator provides an iterator over the items in a RingBuf

func (*Iterator[T]) Next

func (iter *Iterator[T]) Next() (*T, error)

Next returns the next item in the RingBuf. Returns nil, errors.Empty after the last item has been read.

type RingBuf

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

RingBuf is the ring buffer data structure

func New

func New[T any](capacity int) *RingBuf[T]

New constructs a new RingBuf with capacity

func (*RingBuf[T]) Clear

func (b *RingBuf[T]) Clear()

Clear reconstructs the RingBuf in place, effectively zeroing all the items.

func (*RingBuf[T]) Collect

func (b *RingBuf[T]) Collect(values ...T)

Collect writes a variable number of items into the RingBuf. This method implements part of the Iterator interface.

func (*RingBuf[T]) Dequeue

func (b *RingBuf[T]) Dequeue() (*T, error)

Dequeue reads an item from the RingBuf, advancing the head pointer. Returns nil, errors.Empty if ring buffer is empty.

func (*RingBuf[T]) Enqueue

func (b *RingBuf[T]) Enqueue(item T)

Enqueue writes an item into the RingBuf, advancing the tail pointer.

func (*RingBuf[T]) FromIterator

func (b *RingBuf[T]) FromIterator(iter iterator.Iterator[T]) error

FromIterator collects the elements from the iterator into the ring buffer. This method implements part of the Iterable interface.

func (*RingBuf[T]) IntoIterator

func (b *RingBuf[T]) IntoIterator() iterator.Iterator[T]

IntoIterator returns an iterator over the items in the RingBuf. This method implements part of the Iterable interface.

func (RingBuf[T]) Peek

func (b RingBuf[T]) Peek() (*T, error)

Peek reads an item from the RingBuf without advancing the head pointer, allowing the value to be read again. Returns nil, errors.Empty if ring buffer is empty.

Jump to

Keyboard shortcuts

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