Skip to content

Instantly share code, notes, and snippets.

@douglasmakey
Created July 15, 2019 06:56
Show Gist options
  • Save douglasmakey/5afce081bb658a92a1aa5f26aff2bce5 to your computer and use it in GitHub Desktop.
Save douglasmakey/5afce081bb658a92a1aa5f26aff2bce5 to your computer and use it in GitHub Desktop.
package main
import hp "container/heap"
type path struct {
value int
nodes []string
}
type minPath []path
func (h minPath) Len() int { return len(h) }
func (h minPath) Less(i, j int) bool { return h[i].value < h[j].value }
func (h minPath) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *minPath) Push(x interface{}) {
*h = append(*h, x.(path))
}
func (h *minPath) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
type heap struct {
values *minPath
}
func newHeap() *heap {
return &heap{values: &minPath{}}
}
func (h *heap) push(p path) {
hp.Push(h.values, p)
}
func (h *heap) pop() path {
i := hp.Pop(h.values)
return i.(path)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment