queue

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

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue[T comparable] struct {
	// contains filtered or unexported fields
}

func FromSlice

func FromSlice[T comparable, A ~[]T](s A, capacity ...int) *Queue[T]

FromSlice creates a new Queue 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 FromSyncQueue

func FromSyncQueue[T comparable](src *SyncQueue[T]) *Queue[T]

FromSyncQueue creates a new Queue from a given SyncQueue. This results in a deep copy so the underlying buffer won't be connected to the original SyncQueue.

func New

func New[T comparable](capacity ...int) *Queue[T]

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

func (*Queue[T]) Cap

func (q *Queue[T]) Cap() int

Cap returns the total capacity of the buffer.

func (*Queue[T]) Clone

func (q *Queue[T]) Clone() *Queue[T]

Clone creates a deep copy of the source Queue.

func (*Queue[T]) Dequeue

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

func (q *Queue[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 (*Queue[T]) Equals

func (q *Queue[T]) Equals(other *Queue[T]) bool

Equals compares the lenght and elements in the Queue to the other Queue.

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty returns true if the buffer contains no elements.

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

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

func (*Queue[T]) Peek

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

func (q *Queue[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 SyncQueue

type SyncQueue[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSync

func NewSync[T comparable](capacity ...int) *SyncQueue[T]

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

func SyncFromQueue

func SyncFromQueue[T comparable](src *Queue[T]) *SyncQueue[T]

FromSyncQueue creates a new Queue from a given SyncQueue. This results in a deep copy so the underlying buffer won't be connected to the original SyncQueue.

func SyncFromSlice

func SyncFromSlice[T comparable, A ~[]T](s A, capacity ...int) *SyncQueue[T]

FromSlice creates a new Queue 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 (*SyncQueue[T]) Cap

func (q *SyncQueue[T]) Cap() int

Cap returns the total capacity of the buffer.

func (*SyncQueue[T]) Clone

func (q *SyncQueue[T]) Clone() *SyncQueue[T]

Clone creates a deep copy of the source Queue.

func (*SyncQueue[T]) Dequeue

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

func (q *SyncQueue[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 (*SyncQueue[T]) Equals

func (q *SyncQueue[T]) Equals(other *SyncQueue[T]) bool

Equals compares the lenght and elements in the Queue to the other Queue.

func (*SyncQueue[T]) IsEmpty

func (q *SyncQueue[T]) IsEmpty() bool

IsEmpty returns true if the buffer contains no elements.

func (*SyncQueue[T]) Len

func (q *SyncQueue[T]) Len() int

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

func (*SyncQueue[T]) Peek

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

func (q *SyncQueue[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