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: 2 Imported by: 0

Documentation

Overview

Package ring provides a generic ring buffer (circular buffer) implementation.

Package ring provides a generic ring buffer (circular buffer) implementation with thread-safety.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RingBuffer

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

RingBuffer 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) *RingBuffer[T]

FromSlice creates a new RingBuffer 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 FromSyncRingBuffer

func FromSyncRingBuffer[T any](src *SyncRingBuffer[T]) *RingBuffer[T]

FromSyncRingBuffer creates a new RingBuffer from a given SyncRingBuffer. This results in a deep copy so the underlying buffer won't be connected to the original SyncRingBuffer.

func New

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

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

func (*RingBuffer[T]) Cap

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

Cap returns the total capacity of the buffer.

func (*RingBuffer[T]) Clone

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

Clone creates a deep copy of the source RingBuffer.

func (*RingBuffer[T]) Dequeue

func (rb *RingBuffer[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 (*RingBuffer[T]) Enqueue

func (rb *RingBuffer[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 (*RingBuffer[T]) IsEmpty

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

IsEmpty returns true if the buffer contains no elements.

func (*RingBuffer[T]) Len

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

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

func (*RingBuffer[T]) Peek

func (rb *RingBuffer[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 (*RingBuffer[T]) ToSlice

func (rb *RingBuffer[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.

type SyncRingBuffer

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

SyncRingBuffer is a generic dynamically resizable circular buffer with thread-safety. 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 NewSync

func NewSync[T any](capacity ...int) *SyncRingBuffer[T]

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

func SyncFromRingBuffer

func SyncFromRingBuffer[T any](src *RingBuffer[T]) *SyncRingBuffer[T]

SyncFromRingBuffer creates a new SyncRingBuffer from a given RingBuffer. This results in a deep copy so the underlying buffer won't be connected to the original RingBuffer.

func SyncFromSlice

func SyncFromSlice[T any, A ~[]T](s A, capacity ...int) *SyncRingBuffer[T]

SyncFromSlice creates a new SyncRingBuffer 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 (*SyncRingBuffer[T]) Cap

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

Cap returns the total capacity of the buffer.

func (*SyncRingBuffer[T]) Clone

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

Clone creates a deep copy of the source SyncRingBuffer.

func (*SyncRingBuffer[T]) Dequeue

func (rb *SyncRingBuffer[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 (*SyncRingBuffer[T]) Enqueue

func (rb *SyncRingBuffer[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 (*SyncRingBuffer[T]) IsEmpty

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

IsEmpty returns true if the buffer contains no elements.

func (*SyncRingBuffer[T]) Len

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

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

func (*SyncRingBuffer[T]) Peek

func (rb *SyncRingBuffer[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 (*SyncRingBuffer[T]) ToSlice

func (rb *SyncRingBuffer[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