priorityqueue

package
v0.10.8 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option[E any] func(q *Queue[E]) error

Option is a function type that can be used to configures properties of a priority queue..

func WithMaxPriority

func WithMaxPriority[E any]() Option[E]

WithMaxPriority returns an Option that configures the priority queue to use max-heap ordering, where larger values have higher priority. By default, the queue uses min-heap ordering (smaller values have higher priority).

func WithSafe

func WithSafe[E any]() Option[E]

WithSafe creates a Option to configures a priority queue to be thread-safe.

type Queue

type Queue[E any] struct {
	// contains filtered or unexported fields
}

Queue represents a priority queue. The queue is implemented by a binary heap.

func New

func New[E any](cmp func(E, E) int, ops ...Option[E]) (*Queue[E], error)

New creates and returns a priority queue with the given comprarison function and options. The provided options can be used to configures the priority queue property. For example:

  • WithSafe(): creates a thread-safe priority queue.
  • WithMaxPriority(): creates a priority queue where higher value has higher priority.

By defaults, it creates a non-thread-safe priority queue use min-heap ordering. (smaller values have higher priority).

func (*Queue[E]) Clear

func (q *Queue[E]) Clear()

Clear removes all elements from the queue.

func (*Queue[E]) Clone

func (q *Queue[E]) Clone() *Queue[E]

Clone returns a deep copy of the queue.

func (*Queue[E]) Dequeue

func (q *Queue[E]) Dequeue() (E, bool)

Dequeue removes and returns the first element from the queue. Returns zero value of element and false if queue is empty.

func (*Queue[E]) Enqueue

func (q *Queue[E]) Enqueue(e E)

Enqueue adds an element to the queue.

func (*Queue[E]) IsEmpty

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

IsEmpty reports whether the queue has no element.

func (*Queue[E]) Len

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

Len returns the number of elements currently in the queue.

func (*Queue[E]) MarshalJSON

func (q *Queue[E]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Queue[E]) Peek

func (q *Queue[E]) Peek() (E, bool)

Peek returns first element of the queue without remove it. Returns zero value of element and false if queue is empty.

func (*Queue[E]) String

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

String returns a string representation of the queue.

func (*Queue[E]) UnmarshalJSON

func (q *Queue[E]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Queue[E]) Values

func (q *Queue[E]) Values() []E

Values returns all elements in the queue.

Jump to

Keyboard shortcuts

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