Documentation
¶
Overview ¶
Package queue implements a FIFO queue generic over any type.
The queue is not safe for concurrent access across goroutines, the caller is responsible for synchronising concurrent access.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a FIFO queue generic over any type.
A Queue should be instantiated by the New function and not directly.
func Collect ¶
Collect builds a Queue from an iterator of items, pushing items into the queue in the order of iteration.
func From ¶
From builds a Queue from an existing slice of items, pushing items into the queue in the order of the slice.
The queue will be preallocated the size of len(items).
func WithCapacity ¶
WithCapacity constructs and returns a new Queue with the given capacity.
This can be a useful performance improvement when the expected maximum size of the queue is known ahead of time as it eliminates the need for reallocation.
func (*Queue[T]) All ¶
All returns the an iterator over the queue in FIFO order.
q := queue.New[string]()
q.Push("hello")
q.Push("there")
qlices.Collect(s.All()) // [hello there]
func (*Queue[T]) Capacity ¶
Capacity returns the capacity of the queue, i.e. the number of items it can contain without the need for reallocation.
Use WithCapacity to create a queue of a given capacity.
q := queue.WithCapacity[string](10) q.Capacity() // 10
func (*Queue[T]) IsEmpty ¶
IsEmpty returns whether or not the queue is empty.
s := queue.New[string]()
s.IsEmpty() // true
s.Push("hello")
s.IsEmpty() // false
func (*Queue[T]) Pop ¶
Pop removes an item from the front of the queue, if the queue is empty, an error will be returned.
q := queue.New[string]()
q.Push("hello")
q.Push("there")
item, _ := q.Pop()
fmt.Println(item) // "hello"
func (*Queue[T]) Push ¶
func (q *Queue[T]) Push(item T)
Push adds an item to the back of the queue.
q := queue.New[string]()
q.Push("hello")
func (*Queue[T]) Size ¶
Size returns the number of items in the queue.
s := queue.New[string]()
s.Size() // 0
s.Push("hello")
s.Push("there")
s.Size() // 2
func (*Queue[T]) String ¶
String satisfies the fmt.Stringer interface and allows a Queue to be printed.