ring

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package ring provides a generic ring buffer (circular buffer) implementation. This internal version serves as the core data structure for FIFO queues and other public-facing abstractions. It supports dynamic resizing and is optimized for enqueue/dequeue performance without relying on third-party libraries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InternalRingBuffer

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

InternalRingBuffer is a generic dynamically resizable circular buffer. It supports enqueue and dequeue operations in constant amortized time, and grows or shrinks based on usage to optimize memory consumption.

T represents the type of elements stored in the buffer.

func FromSlice

func FromSlice[T any, A ~[]T](s A, capacity ...int) *InternalRingBuffer[T]

FromSlice creates a new InternalRingBuffer from a given slice. An optional capacity may be provided. If the capacity is less than the slice length, the slice length is used as the minimum capacity.

func New

func New[T any](capacity ...int) *InternalRingBuffer[T]

New returns a new InternalRingBuffer with an optional initial capacity. If no capacity is provided or the provided value is <= 0, a default of 8 is used.

func (*InternalRingBuffer[T]) Cap

func (rb *InternalRingBuffer[T]) Cap() int

Cap returns the total capacity of the buffer.

func (*InternalRingBuffer[T]) Clone

func (rb *InternalRingBuffer[T]) Clone() *InternalRingBuffer[T]

Clone creates a deep copy of the source InternalRingBuffer.

func (*InternalRingBuffer[T]) Dequeue

func (rb *InternalRingBuffer[T]) Dequeue() (T, bool)

Dequeue removes and returns the element at the front of the buffer. If the buffer is empty, it returns the zero value of T and false. The buffer may shrink if usage falls below 25% of capacity.

func (*InternalRingBuffer[T]) Enqueue

func (rb *InternalRingBuffer[T]) Enqueue(values ...T)

Enqueue appends one or more values to the end of the buffer. If necessary, the buffer is resized to accommodate the new values.

func (*InternalRingBuffer[T]) IsEmpty

func (rb *InternalRingBuffer[T]) IsEmpty() bool

IsEmpty returns true if the buffer contains no elements.

func (*InternalRingBuffer[T]) Len

func (rb *InternalRingBuffer[T]) Len() int

Len returns the number of elements currently stored in the buffer.

func (*InternalRingBuffer[T]) Peek

func (rb *InternalRingBuffer[T]) Peek() (T, bool)

Peek returns the element at the front of the buffer without removing it. If the buffer is empty, it returns the zero value of T and false.

func (*InternalRingBuffer[T]) ToSlice

func (rb *InternalRingBuffer[T]) ToSlice() []T

ToSlice returns a new slice containing all elements in the buffer in their logical order. The returned slice is independent of the internal buffer state.

Jump to

Keyboard shortcuts

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