pq

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package pq exposes a generic priority queue implemented using a heap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item[T any] struct {
	Payload  T
	Priority int
}

Item encapsulates a payload and its priority.

type PriorityQueue

type PriorityQueue[T any] struct {
	// contains filtered or unexported fields
}

PriorityQueue implements a basic priority queue which accepts a generic payload with an integer priority.

func NewPriorityQueue

func NewPriorityQueue[T any](capacity int) *PriorityQueue[T]

NewPriorityQueue creates a new priority queue where the underlying capacity is set to the given value.

NOTE: The 'PriorityQueue' capacity has the same behavior as a slices capacity meaning it may grow beyond the given capacity, the capacity is there for performance optimizations.

func (*PriorityQueue[T]) Dequeue

func (p *PriorityQueue[T]) Dequeue() Item[T]

Dequeue returns the item from the queue with the highest priority, where multiple items have the same priority, they're returned in an arbitrary order.

func (*PriorityQueue[T]) Drain

func (p *PriorityQueue[T]) Drain(fn func(item Item[T]) error) error

Drain removes all items from the queue running the given function on each item. In the event of an error, dequeuing stops early, and returns the error.

func (*PriorityQueue[T]) Enqueue

func (p *PriorityQueue[T]) Enqueue(item Item[T])

Enqueue adds the given item to the priority queue.

func (*PriorityQueue[T]) Len

func (p *PriorityQueue[T]) Len() int

Len returns the number of items in the priority queue.

Jump to

Keyboard shortcuts

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