The code has been extracted from
github.com/daviddengcn/go-villa
.
The package provides a type-safe alternative to container/heap
from the
standard library.
The package only requires that the type being used implements sort.Interface
.
The pop and push operations are provided by the package. The trick is that they
do not return/take the deleted/inserted element but assume the last position in
the underlying sort.Interface
is where to put/find it.
The example below leverages the implementation of sort.Interface
provided by
the standard sort
package and creates short wrappers with a more elegant
interface.
type IntHeap []int
func (h *IntHeap) Pop() int {
heap.PopToLast(sort.IntSlice(*h))
res := (*h)[len(*h) - 1]
*h = (*h)[:len(*h) - 1]
return res
}
func (h *IntHeap) Push(x int) {
*h = append(*h, x)
heap.PushLast(sort.IntSlice(*h))
}
The library is provided under the 2-clause BSD license (see LICENSE).
Copyright for github.com/szabba/heap
(c) 2014, Karol Marcjan
All rights reserved.
Copyright for github.com/daviddengcn/go-villa
(c) 2013, Yi DENG
All right reserved.