I have used the container/heap
package to implement a priority queue. One thing bothers me though. What should the behaviour of the interface.Pop()
method be if the heap is empty? I don't see anything mentioned in the documentation and the source code doesn't seem to be expecting this situation:
// Pop removes the minimum element (according to Less) from the heap
// and returns it. The complexity is O(log(n)) where n = h.Len().
// It is equivalent to Remove(h, 0).
//
func Pop(h Interface) interface{} {
n := h.Len() - 1
h.Swap(0, n)
down(h, 0, n)
return h.Pop()
}
Clearly, if h.Len()
is 0
this is not going to work well. Is this simply meant to panic
or is the user expected to always check whether there are any items left?