queue

package
v0.0.0-...-5bd4ecb Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	// The value of the item; arbitrary.
	Value any

	// The priority of the item in the queue.
	Priority int
}

An Item is something to be managed in a priority queue.

type PriorityQueue

type PriorityQueue []*Item

A PriorityQueue implements heap.Interface and holds Items. This is a min-heap, so the item with the lowest priority is popped first.

func (*PriorityQueue) IsEmpty

func (pq *PriorityQueue) IsEmpty() bool

IsEmpty is used to check whether the queue is empty or not (length == 0).

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() (v any)

Pop removes and returns the minimum element (according to Item.Priority) from the queue.

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(v any)

Push pushes the value v in the queue.

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

type Queue

type Queue[T any] []T

Queue represents a simple queue data structure. It is backed by a slice of an unconstrained type T.

func New

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

New returns an initialized queue, optionally with the given elements. The elements are added in the same order as provided.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (e T, ok bool)

Dequeue is used to dequeue or remove the frontmost element from the queue and return it.

An attempt to dequeue when the queue is empty will return the zero value for the type of the elements in the queue. Using multiple assignment, one can distinguish a missing entry from a zero value. This is referred to as the "comma ok" idiom.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(es ...T)

Enqueue is used to enqueue all the given elements to the queue. Multiple elements are added in the same order as provided.

func (*Queue[T]) IsEmpty

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

IsEmpty is used to check whether the queue is empty or not.

func (*Queue[T]) Len

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

Len returns the number of elements in the queue.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (e T, ok bool)

Peek returns the frontmost element of the queue without removing it.

An attempt to peek when the queue is empty will return the zero value for the type of the elements in the queue. Using multiple assignment, one can distinguish a missing entry from a zero value. This is referred to as the "comma ok" idiom.

func (*Queue[T]) String

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

func (*Queue[T]) ToSlice

func (q *Queue[T]) ToSlice() []T

ToSlice returns a slice containing the elements of the queue where the first element is the start of the queue. Mutating the returned slice will not affect the underlying implementation.

Jump to

Keyboard shortcuts

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