Documentation
¶
Overview ¶
Package queue provides a generic, thread-safe, dynamically resizing double-ended queue (deque). It offers efficient O(1) time complexity for adding and removing items from both ends, making it suitable for both queue (FIFO) and stack (LIFO) operations.
The queue automatically resizes its internal buffer by powers of two to optimize for CPU and garbage collection performance, growing when capacity is needed and shrinking when the buffer is less than a quarter full. It uses bitwise arithmetic for efficient index calculations and supports concurrent use through internal locking.
Index ¶
- Variables
- type Dequeuer
- type Enqueuer
- type Queue
- func (q *Queue[T]) AddBack(item T) error
- func (q *Queue[T]) AddFront(item T) error
- func (q *Queue[T]) BroadcastWaiters()
- func (q *Queue[T]) Cap() int
- func (q *Queue[T]) Clear()
- func (q *Queue[T]) Close()
- func (q *Queue[T]) Dequeue() (T, error)
- func (q *Queue[T]) DequeueWait(ctx context.Context) (T, error)
- func (q *Queue[T]) Enqueue(item T) error
- func (q *Queue[T]) IsEmpty() bool
- func (q *Queue[T]) Len() int
- func (q *Queue[T]) MaxCap() int
- func (q *Queue[T]) PeekBack() (T, error)
- func (q *Queue[T]) PeekFront() (T, error)
- func (q *Queue[T]) Pop() (T, error)
- func (q *Queue[T]) Push(item T) error
- func (q *Queue[T]) RemoveBack() (T, error)
- func (q *Queue[T]) RemoveFront() (T, error)
- func (q *Queue[T]) Slice() []T
- func (q *Queue[T]) Stats() QueueStats
- type QueueStats
- type Queuer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyQueue is returned when attempting to remove or peek from an empty queue. ErrEmptyQueue = errors.New("queue is empty") // ErrClosedQueue is returned when operating on a closed queue. ErrClosedQueue = errors.New("queue is closed") // ErrQueueFull is returned when attempting to Enqueue or AddFront on a queue // that has reached its maximum capacity. ErrQueueFull = errors.New("queue is full") )
Functions ¶
This section is empty.
Types ¶
type Dequeuer ¶
type Dequeuer[T any] interface { // Dequeue removes and returns the item at the front. // Returns ErrEmptyQueue if empty, ErrClosedQueue if closed. Dequeue() (T, error) // Len returns the current number of items. Len() int // IsEmpty returns true if the queue has no items. IsEmpty() bool }
Dequeuer abstracts read operations on a queue. Used by consumers that only need to pull items.
type Enqueuer ¶
type Enqueuer[T any] interface { // Enqueue adds an item to the back of the queue. // Returns ErrClosedQueue if closed. // Returns ErrQueueFull if the queue has reached its maximum capacity. Enqueue(item T) error // Len returns the current number of items. Len() int }
Enqueuer abstracts write operations on a queue. Used by producers that only need to push items.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a generic, thread-safe, dynamically resizing double-ended queue (deque). Use NewQueue or NewBoundedQueue to create a queue. All methods are safe for concurrent use.
func NewBoundedQueue ¶ added in v0.9.0
NewBoundedQueue creates a new queue with at least initialCap capacity and a maximum capacity of maxCap. Once the queue reaches maxCap items, Enqueue/AddBack and AddFront return ErrQueueFull. A maxCap of 0 (or less than initialCap) means the queue is unbounded.
func NewQueue ¶
NewQueue creates a new queue with at least initialCap capacity. The actual capacity will be the next power of two greater than or equal to initialCap. The queue is unbounded (no maximum capacity).
func (*Queue[T]) AddBack ¶
AddBack adds an item to the back of the queue. Returns ErrClosedQueue if the queue is closed. Returns ErrQueueFull if the queue has reached its maximum capacity.
func (*Queue[T]) AddFront ¶
AddFront adds an item to the front of the queue. Returns ErrClosedQueue if the queue is closed. Returns ErrQueueFull if the queue has reached its maximum capacity.
func (*Queue[T]) BroadcastWaiters ¶ added in v0.16.0
func (q *Queue[T]) BroadcastWaiters()
BroadcastWaiters wakes all goroutines blocked in DequeueWait without enqueueing. Waiters that find the queue still empty return ErrEmptyQueue (see wakeEpoch).
func (*Queue[T]) Clear ¶
func (q *Queue[T]) Clear()
Clear removes all items from the queue and resets its capacity.
func (*Queue[T]) Close ¶
func (q *Queue[T]) Close()
Close marks the queue as closed. Further operations will return ErrClosedQueue.
func (*Queue[T]) Dequeue ¶
Dequeue removes and returns the item at the front of the queue. Returns ErrEmptyQueue if the queue is empty, or ErrClosedQueue if closed.
func (*Queue[T]) DequeueWait ¶ added in v0.16.0
DequeueWait removes and returns the front item, blocking while the queue is empty. It returns ctx.Err() if the context is canceled while waiting, or ErrClosedQueue if the queue is closed while empty. Non-blocking Dequeue behavior is unchanged.
func (*Queue[T]) Enqueue ¶
Enqueue adds an item to the back of the queue. Returns ErrClosedQueue if the queue is closed.
func (*Queue[T]) MaxCap ¶ added in v0.9.0
MaxCap returns the maximum capacity of the queue. Returns 0 if the queue is unbounded.
func (*Queue[T]) PeekBack ¶
PeekBack returns the item at the back of the queue without removing it. Returns ErrEmptyQueue if the queue is empty, or ErrClosedQueue if closed.
func (*Queue[T]) PeekFront ¶
PeekFront returns the item at the front of the queue without removing it. Returns ErrEmptyQueue if the queue is empty, or ErrClosedQueue if closed.
func (*Queue[T]) Pop ¶
Pop removes and returns the item at the back of the queue. Returns ErrEmptyQueue if the queue is empty, or ErrClosedQueue if closed.
func (*Queue[T]) Push ¶
Push adds an item to the back of the queue. Returns ErrClosedQueue if the queue is closed.
func (*Queue[T]) RemoveBack ¶
RemoveBack removes and returns the item at the back of the queue. Returns ErrEmptyQueue if the queue is empty, or ErrClosedQueue if closed.
func (*Queue[T]) RemoveFront ¶
RemoveFront removes and returns the item at the front of the queue. Returns ErrEmptyQueue if the queue is empty, or ErrClosedQueue if closed.
func (*Queue[T]) Slice ¶
func (q *Queue[T]) Slice() []T
Slice returns a copy of the queue's contents as a slice.
func (*Queue[T]) Stats ¶
func (q *Queue[T]) Stats() QueueStats
Stats returns statistics about the queue.
type QueueStats ¶
type QueueStats struct {
CtAddBack int // Total number of AddBack operations.
CtAddFront int // Total number of AddFront operations.
CtRemoveBack int // Total number of RemoveBack operations.
CtRemoveFront int // Total number of RemoveFront operations.
Size int // Current number of items in the queue.
Capacity int // Current capacity of the queue's internal buffer.
MaxCapacity int // Maximum capacity (0 = unlimited).
HeadIndex int // Current head index of the internal buffer.
TailIndex int // Current tail index of the internal buffer.
IsClosed bool // Whether the queue is closed.
}
QueueStats holds various statistics about the queue's current state.