Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Heapable ¶
Heapable reflects an interface that an item must implement in order to be placed into a priority queue. If the priority queue does not require modifications (i.e. updates or removals), these can be implemented as no-ops.
type PriorityQueue ¶
type PriorityQueue struct {
// contains filtered or unexported fields
}
PriorityQueue implements a priority queue of Heapable items. Each item must implement the Heapable interface and is responsible for giving each item priority respective to every other item. The main benefit of this implementation is to abstract away any direct heap usage.
func NewPriorityQueue ¶
func NewPriorityQueue() *PriorityQueue
NewPriorityQueue returns a reference to a new initialized PriorityQueue.
func (*PriorityQueue) Pop ¶
func (pq *PriorityQueue) Pop() (res Heapable, err error)
Pop removes a Sortable item from the priority queue with the highest priority and returns it. The complexity is logarithmic. If the index is beyond the size of the priority queue, an error is returned.
func (*PriorityQueue) Push ¶
func (pq *PriorityQueue) Push(h Heapable)
Push adds a Sortable item to the priority queue. The complexity is logarithmic.
func (*PriorityQueue) Remove ¶
func (pq *PriorityQueue) Remove(h Heapable) (err error)
Remove removes a Heapable item from the priority queue. The item is retrieved by fetching it's index. Items are then "fixed" into their appropriate order once the item is removed. The complexity is logarithmic. If the index is beyond the size of the priority queue, an error is returned.
func (*PriorityQueue) Size ¶
func (pq *PriorityQueue) Size() int
Size returns the size of the priority queue.
func (*PriorityQueue) Update ¶
func (pq *PriorityQueue) Update(h Heapable) (err error)
Update re-establishes the priority queue ordering after the Heapable element has changed its value. It is up to the caller to update the element accordingly. The complexity is logarithmic. If the index is beyond the size of the priority queue, an error is returned.