| // Copyright 2012 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| // This example demonstrates an integer heap built using the heap interface. |
| // An IntHeap is a min-heap of ints. |
| func (h IntHeap) Len() int { return len(h) } |
| func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } |
| func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } |
| func (h *IntHeap) Push(x interface{}) { |
| // Push and Pop use pointer receivers because they modify the slice's length, |
| // not just its contents. |
| func (h *IntHeap) Pop() interface{} { |
| // This example inserts several ints into an IntHeap, checks the minimum, |
| // and removes them in order of priority. |
| fmt.Printf("minimum: %d\n", (*h)[0]) |
| fmt.Printf("%d ", heap.Pop(h)) |