Something went wrong. Try again.
A collection of generic data structures in Go
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137// 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.package queue
import ( "errors" "fmt" "iter")
// Queue is a FIFO queue generic over any type.//// A Queue should be instantiated by the New function and not directly.type Queue[T any] struct { container []T // Underlying slice}
// New constructs and returns a new Queue.func New[T any]() *Queue[T] { return &Queue[T]{container: make([]T, 0)}}
// 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 WithCapacity[T any](capacity int) *Queue[T] { return &Queue[T]{container: make([]T, 0, capacity)}}
// 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 From[T any](items []T) *Queue[T] { queue := WithCapacity[T](len(items)) for _, item := range items { queue.Push(item) }
return queue}
// Collect builds a [Queue] from an iterator of items, pushing items// into the queue in the order of iteration.func Collect[T any](items iter.Seq[T]) *Queue[T] { queue := New[T]() for item := range items { queue.Push(item) }
return queue}
// Push adds an item to the back of the queue.//// q := queue.New[string]()// q.Push("hello")func (q *Queue[T]) Push(item T) { q.container = append(q.container, item)}
// 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 (q *Queue[T]) Pop() (T, error) { l := len(q.container) if l == 0 { var none T return none, errors.New("pop from empty queue") } item := (q.container)[0] q.container = (q.container)[1:]
return item, nil}
// Size returns the number of items in the queue.//// s := queue.New[string]()// s.Size() // 0// s.Push("hello")// s.Push("there")// s.Size() // 2func (q *Queue[T]) Size() int { return len(q.container)}
// 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() // 10func (q *Queue[T]) Capacity() int { return cap(q.container)}
// IsEmpty returns whether or not the queue is empty.//// s := queue.New[string]()// s.IsEmpty() // true// s.Push("hello")// s.IsEmpty() // falsefunc (q *Queue[T]) IsEmpty() bool { return len(q.container) == 0}
// Items returns the an iterator over the queue in FIFO order.//// q := queue.New[string]()// q.Push("hello")// q.Push("there")// qlices.Collect(s.Items()) // [hello there]func (q *Queue[T]) Items() iter.Seq[T] { return func(yield func(T) bool) { for _, item := range q.container { if !yield(item) { return } } }}
// String satisfies the [fmt.Stringer] interface and allows a Queue to be printed.func (q *Queue[T]) String() string { return fmt.Sprintf("%v", q.container)}