prque

package
v1.13.8 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

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

type LazyQueue[P constraints.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

func NewLazyQueue[P constraints.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

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

Empty checks whether the priority queue is empty.

func (*LazyQueue[P, V]) MultiPop

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

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

Pop removes and returns the item with the greatest actual priority

func (*LazyQueue[P, V]) PopItem

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

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

Push adds an item to the queue

func (*LazyQueue[P, V]) Refresh

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

Refresh performs queue re-evaluation if necessary

func (*LazyQueue[P, V]) Remove

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

Remove removes the item with the given index.

func (*LazyQueue[P, V]) Reset

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

Reset clears the contents of the queue

func (*LazyQueue[P, V]) Size

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

Size returns the number of items in the priority queue.

func (*LazyQueue[P, V]) Update

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

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

type MaxPriorityCallback

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

type PriorityCallback

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

type Prque

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

Priority queue data structure.

func New

func New[P constraints.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

Checks whether the priority queue is empty.

func (*Prque[P, V]) Peek

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)

Pops 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

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)

Pushes 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()

Clears the contents of the priority queue.

func (*Prque[P, V]) Size

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

Returns the number of element in the priority queue.

type SetIndexCallback

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