A simple and generic priority queue implementation in Golang.
Any such type that implements the Heapable
interface may be used in a PriorityQueue
.
The underlying element essentially must know how to give priority when compared to another
element of the same type.
If the priority queue does not require modifications (i.e. updates or removals), the type
implementing Heapable
does not need to support indexing.
type myType struct {
priority int
}
func (mt *myType) Index() (i int) { return }
func (mt *myType) SetIndex(_ int) {}
func (mt *myType) Priority(other interface{}) bool {
if t, ok := other.(*myType); ok {
return th.priority > t.priority
}
return false
}
pq := NewPriorityQueue()
pq.Push(&myType{priority: 1})
pq.Push(&myType{priority: 3})
pq.Push(&myType{priority: 2})
for pq.Size() != 0 {
res, err := pq.Pop()
// ...
}
$ go test -v ./...
- Fork it
- Create your feature branch (
git checkout -b feature/my-new-feature
) - Commit your changes (
git commit -m 'Add some feature'
) - Push to the branch (
git push origin feature/my-new-feature
) - Create a new Pull Request