prque

package
v1.14.12 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: GPL-3.0 Imports: 4 Imported by: 1,349

Documentation

Overview

Package prque implements a priority queue data structure supporting arbitrary value types and int64 priorities.

If you would like to use a min-priority queue, simply negate the priorities.

Internally the queue is based on the standard heap package working on a sortable version of the block based stack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LazyQueue added in v1.9.2

type LazyQueue[P cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

LazyQueue is a priority queue data structure where priorities can change over time and are only evaluated on demand. Two callbacks are required:

  • priority evaluates the actual priority of an item
  • maxPriority gives an upper estimate for the priority in any moment between now and the given absolute time

If the upper estimate is exceeded then Update should be called for that item. A global Refresh function should also be called periodically.

func NewLazyQueue added in v1.9.2

func NewLazyQueue[P cmp.Ordered, V any](setIndex SetIndexCallback[V], priority PriorityCallback[P, V], maxPriority MaxPriorityCallback[P, V], clock mclock.Clock, refreshPeriod time.Duration) *LazyQueue[P, V]

NewLazyQueue creates a new lazy queue

func (*LazyQueue[P, V]) Empty added in v1.9.2

func (q *LazyQueue[P, V]) Empty() bool

Empty checks whether the priority queue is empty.

func (*LazyQueue[P, V]) MultiPop added in v1.9.2

func (q *LazyQueue[P, V]) MultiPop(callback func(data V, priority P) bool)

MultiPop pops multiple items from the queue and is more efficient than calling Pop multiple times. Popped items are passed to the callback. MultiPop returns when the callback returns false or there are no more items to pop.

func (*LazyQueue[P, V]) Pop added in v1.9.2

func (q *LazyQueue[P, V]) Pop() (V, P)

Pop removes and returns the item with the greatest actual priority

func (*LazyQueue[P, V]) PopItem added in v1.9.2

func (q *LazyQueue[P, V]) PopItem() V

PopItem pops the item from the queue only, dropping the associated priority value.

func (*LazyQueue[P, V]) Push added in v1.9.2

func (q *LazyQueue[P, V]) Push(data V)

Push adds an item to the queue

func (*LazyQueue[P, V]) Refresh added in v1.9.2

func (q *LazyQueue[P, V]) Refresh()

Refresh performs queue re-evaluation if necessary

func (*LazyQueue[P, V]) Remove added in v1.9.2

func (q *LazyQueue[P, V]) Remove(index int) V

Remove removes the item with the given index.

func (*LazyQueue[P, V]) Reset added in v1.9.2

func (q *LazyQueue[P, V]) Reset()

Reset clears the contents of the queue

func (*LazyQueue[P, V]) Size added in v1.9.2

func (q *LazyQueue[P, V]) Size() int

Size returns the number of items in the priority queue.

func (*LazyQueue[P, V]) Update added in v1.9.2

func (q *LazyQueue[P, V]) Update(index int)

Update updates the upper priority estimate for the item with the given queue index

type MaxPriorityCallback added in v1.9.2

type MaxPriorityCallback[P cmp.Ordered, V any] func(data V, until mclock.AbsTime) P // estimated maximum priority callback

type PriorityCallback added in v1.9.2

type PriorityCallback[P cmp.Ordered, V any] func(data V) P // actual priority callback

type Prque

type Prque[P cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

Prque is a priority queue data structure.

func New

func New[P cmp.Ordered, V any](setIndex SetIndexCallback[V]) *Prque[P, V]

New creates a new priority queue.

func (*Prque[P, V]) Empty

func (p *Prque[P, V]) Empty() bool

Empty checks whether the priority queue is empty.

func (*Prque[P, V]) Peek added in v1.9.0

func (p *Prque[P, V]) Peek() (V, P)

Peek returns the value with the greatest priority but does not pop it off.

func (*Prque[P, V]) Pop

func (p *Prque[P, V]) Pop() (V, P)

Pop the value with the greatest priority off the stack and returns it. Currently no shrinking is done.

func (*Prque[P, V]) PopItem

func (p *Prque[P, V]) PopItem() V

PopItem pops only the item from the queue, dropping the associated priority value.

func (*Prque[P, V]) Push

func (p *Prque[P, V]) Push(data V, priority P)

Push a value with a given priority into the queue, expanding if necessary.

func (*Prque[P, V]) Remove

func (p *Prque[P, V]) Remove(i int) V

Remove removes the element with the given index.

func (*Prque[P, V]) Reset

func (p *Prque[P, V]) Reset()

Reset clears the contents of the priority queue.

func (*Prque[P, V]) Size

func (p *Prque[P, V]) Size() int

Size returns the number of element in the priority queue.

type SetIndexCallback added in v1.9.0

type SetIndexCallback[V any] func(data V, index int)

SetIndexCallback is called when the element is moved to a new index. Providing SetIndexCallback is optional, it is needed only if the application needs to delete elements other than the top one.

Jump to

Keyboard shortcuts

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