Documentation
¶
Overview ¶
Package pq implements a priority queue data structure on top of container/heap. As an addition to regular operations, it allows an update of an items priority, allowing the queue to be used in graph search algorithms like Dijkstra's algorithm. Computational complexities of operations are mainly determined by container/heap. In addition, a map of items is maintained, allowing O(1) lookup needed for priority updates, which themselves are O(log n).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PriorityQueue ¶
type PriorityQueue[T comparable] struct { // contains filtered or unexported fields }
PriorityQueue[T] represents the queue of comparable type T.
func New ¶
func New[T comparable]() PriorityQueue[T]
New[T] initializes an empty priority queue of type T.
func (*PriorityQueue[T]) Insert ¶
func (p *PriorityQueue[T]) Insert(v T, priority float64)
Insert inserts a new element into the queue. No action is performed on duplicate elements.
func (*PriorityQueue[T]) Len ¶
func (p *PriorityQueue[T]) Len() int
Len returns the number of elements in the queue.
func (*PriorityQueue[T]) Peek ¶
func (p *PriorityQueue[T]) Peek() (T, bool)
Peek gets the element with the highest priority from the queue and returns it. The second return value indicates whether the queue is empty
func (*PriorityQueue[T]) Pop ¶
func (p *PriorityQueue[T]) Pop() (T, bool)
Pop removes the element with the highest priority from the queue and returns it. The second return value indicates whether the queue is empty
func (*PriorityQueue[T]) UpdatePriority ¶
func (p *PriorityQueue[T]) UpdatePriority(x T, newPriority float64)
UpdatePriority changes the priority of a given item. If the specified item is not present in the queue, no action is performed.