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 ¶
- type LazyQueue
- func (q *LazyQueue[P, V]) Empty() bool
- func (q *LazyQueue[P, V]) MultiPop(callback func(data V, priority P) bool)
- func (q *LazyQueue[P, V]) Pop() (V, P)
- func (q *LazyQueue[P, V]) PopItem() V
- func (q *LazyQueue[P, V]) Push(data V)
- func (q *LazyQueue[P, V]) Refresh()
- func (q *LazyQueue[P, V]) Remove(index int) V
- func (q *LazyQueue[P, V]) Reset()
- func (q *LazyQueue[P, V]) Size() int
- func (q *LazyQueue[P, V]) Update(index int)
- type MaxPriorityCallback
- type PriorityCallback
- type Prque
- type SetIndexCallback
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]) MultiPop ¶
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]) Reset ¶
func (q *LazyQueue[P, V]) Reset()
Reset clears the contents of the queue
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]) 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.
type SetIndexCallback ¶
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.