queue

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 3 Imported by: 1

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 added in v0.7.0

func Collect[T any](items iter.Seq[T]) *Queue[T]

Collect builds a Queue from an iterator of items, pushing items into the queue in the order of iteration.

func From added in v0.7.0

func From[T any](items []T) *Queue[T]

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 New

func New[T any]() *Queue[T]

New constructs and returns a new Queue.

func WithCapacity added in v0.3.0

func WithCapacity[T any](capacity int) *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 (*Queue[T]) All added in v0.18.0

func (q *Queue[T]) All() iter.Seq[T]

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 added in v0.15.0

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

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

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

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

func (q *Queue[T]) Pop() (T, error)

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 added in v0.6.0

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

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

func (q *Queue[T]) String() string

String satisfies the fmt.Stringer interface and allows a Queue to be printed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL