|
|
@ -22,7 +22,7 @@ import ( |
|
|
|
"container/heap" |
|
|
|
"container/heap" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Priority queue data structure.
|
|
|
|
// Prque is a priority queue data structure.
|
|
|
|
type Prque[P cmp.Ordered, V any] struct { |
|
|
|
type Prque[P cmp.Ordered, V any] struct { |
|
|
|
cont *sstack[P, V] |
|
|
|
cont *sstack[P, V] |
|
|
|
} |
|
|
|
} |
|
|
@ -32,7 +32,7 @@ func New[P cmp.Ordered, V any](setIndex SetIndexCallback[V]) *Prque[P, V] { |
|
|
|
return &Prque[P, V]{newSstack[P, V](setIndex)} |
|
|
|
return &Prque[P, V]{newSstack[P, V](setIndex)} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Pushes a value with a given priority into the queue, expanding if necessary.
|
|
|
|
// Push a value with a given priority into the queue, expanding if necessary.
|
|
|
|
func (p *Prque[P, V]) Push(data V, priority P) { |
|
|
|
func (p *Prque[P, V]) Push(data V, priority P) { |
|
|
|
heap.Push(p.cont, &item[P, V]{data, priority}) |
|
|
|
heap.Push(p.cont, &item[P, V]{data, priority}) |
|
|
|
} |
|
|
|
} |
|
|
@ -43,14 +43,14 @@ func (p *Prque[P, V]) Peek() (V, P) { |
|
|
|
return item.value, item.priority |
|
|
|
return item.value, item.priority |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Pops the value with the greatest priority off the stack and returns it.
|
|
|
|
// Pop the value with the greatest priority off the stack and returns it.
|
|
|
|
// Currently no shrinking is done.
|
|
|
|
// Currently no shrinking is done.
|
|
|
|
func (p *Prque[P, V]) Pop() (V, P) { |
|
|
|
func (p *Prque[P, V]) Pop() (V, P) { |
|
|
|
item := heap.Pop(p.cont).(*item[P, V]) |
|
|
|
item := heap.Pop(p.cont).(*item[P, V]) |
|
|
|
return item.value, item.priority |
|
|
|
return item.value, item.priority |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Pops only the item from the queue, dropping the associated priority value.
|
|
|
|
// PopItem pops only the item from the queue, dropping the associated priority value.
|
|
|
|
func (p *Prque[P, V]) PopItem() V { |
|
|
|
func (p *Prque[P, V]) PopItem() V { |
|
|
|
return heap.Pop(p.cont).(*item[P, V]).value |
|
|
|
return heap.Pop(p.cont).(*item[P, V]).value |
|
|
|
} |
|
|
|
} |
|
|
@ -60,17 +60,17 @@ func (p *Prque[P, V]) Remove(i int) V { |
|
|
|
return heap.Remove(p.cont, i).(*item[P, V]).value |
|
|
|
return heap.Remove(p.cont, i).(*item[P, V]).value |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Checks whether the priority queue is empty.
|
|
|
|
// Empty checks whether the priority queue is empty.
|
|
|
|
func (p *Prque[P, V]) Empty() bool { |
|
|
|
func (p *Prque[P, V]) Empty() bool { |
|
|
|
return p.cont.Len() == 0 |
|
|
|
return p.cont.Len() == 0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Returns the number of element in the priority queue.
|
|
|
|
// Size returns the number of element in the priority queue.
|
|
|
|
func (p *Prque[P, V]) Size() int { |
|
|
|
func (p *Prque[P, V]) Size() int { |
|
|
|
return p.cont.Len() |
|
|
|
return p.cont.Len() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Clears the contents of the priority queue.
|
|
|
|
// Reset clears the contents of the priority queue.
|
|
|
|
func (p *Prque[P, V]) Reset() { |
|
|
|
func (p *Prque[P, V]) Reset() { |
|
|
|
*p = *New[P, V](p.cont.setIndex) |
|
|
|
*p = *New[P, V](p.cont.setIndex) |
|
|
|
} |
|
|
|
} |
|
|
|