Something went wrong. Try again.
A collection of generic data structures in Go
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155// 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 "go.followtheprocess.codes/collections/queue"
import ( "fmt" "iter")
// Queue is a FIFO queue generic over any type.type Queue[T any] struct { container []T // Ring buffer backing array (len == capacity) head int // Index of the next item to pop tail int // Index of the next slot to push into size int // Number of items currently in the queue}
// New constructs and returns a new Queue.func New[T any]() *Queue[T] { return &Queue[T]{}}
// 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, 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) { if len(q.container) == 0 { q.container = make([]T, 1) }
if q.size == len(q.container) { q.grow() }
q.container[q.tail] = item q.tail = (q.tail + 1) % len(q.container) q.size++}
// Pop removes an item from the front of the queue. The boolean is false// (and the item is the zero value of T) if the queue is empty.//// q := queue.New[string]()// q.Push("hello")// q.Push("there")// item, _ := q.Pop()// fmt.Println(item) // "hello"func (q *Queue[T]) Pop() (item T, ok bool) { if q.size == 0 { var zero T
return zero, false }
item = q.container[q.head] var zero T q.container[q.head] = zero q.head = (q.head + 1) % len(q.container) q.size--
return item, true}
// 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 q.size}
// 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 q.size == 0}
// All returns the an iterator over the queue in FIFO order.//// q := queue.New[string]()// q.Push("hello")// q.Push("there")// slices.Collect(q.All()) // [hello there]func (q *Queue[T]) All() iter.Seq[T] { return func(yield func(T) bool) { for i := range q.size { if !yield(q.container[(q.head+i)%len(q.container)]) { return } } }}
// String satisfies the [fmt.Stringer] interface and allows a Queue to be printed.func (q *Queue[T]) String() string { items := make([]T, q.size) for i := range q.size { items[i] = q.container[(q.head+i)%len(q.container)] }
return fmt.Sprintf("%v", items)}
// grow doubles the capacity of the ring buffer, copying items into logical order.func (q *Queue[T]) grow() { newContainer := make([]T, len(q.container)*2) n := copy(newContainer, q.container[q.head:]) copy(newContainer[n:], q.container[:q.head]) q.head = 0 q.tail = q.size q.container = newContainer}