1
0

heap.go 505 B

123456789101112131415161718192021
  1. package sheap
  2. type IntHeap []int64
  3. func (h IntHeap) Len() int { return len(h) }
  4. func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
  5. func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  6. func (h *IntHeap) Push(x interface{}) {
  7. // Push and Pop use pointer receivers because they modify the slice's length,
  8. // not just its contents.
  9. *h = append(*h, x.(int64))
  10. }
  11. func (h *IntHeap) Pop() interface{} {
  12. old := *h
  13. n := len(old)
  14. x := old[n-1]
  15. *h = old[0 : n-1]
  16. return x
  17. }