queue

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2024 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 (*Queue[T]) Empty added in v0.6.0

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

Empty returns whether or not the queue is empty.

s := queue.New[string]()
s.Empty() // true
s.Push("hello")
s.Empty() // false

func (*Queue[T]) Items

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

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 (*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