--- /dev/null
+// 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.
+package heap_test
+
+import (
+ "container/heap"
+ "fmt"
+)
+
+// An IntHeap is a min-heap of ints.
+type IntHeap []int
+
+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.
+ *h = append(*h, x.(int))
+}
+
+func (h *IntHeap) Pop() interface{} {
+ old := *h
+ n := len(old)
+ x := old[n-1]
+ *h = old[0 : n-1]
+ return x
+}
+
+// This example inserts several ints into an IntHeap and removes them in order of priority.
+func Example_intHeap() {
+ h := &IntHeap{2, 1, 5}
+ heap.Init(h)
+ heap.Push(h, 3)
+ for h.Len() > 0 {
+ fmt.Printf("%d ", heap.Pop(h))
+ }
+ // Output: 1 2 3 5
+}
}
func (pq *PriorityQueue) Push(x interface{}) {
- // Push and Pop use pointer receivers because they modify the slice's length,
- // not just its contents.
n := len(*pq)
item := x.(*Item)
item.index = n
}
func (pq *PriorityQueue) Pop() interface{} {
- a := *pq
- n := len(a)
- item := a[n-1]
+ old := *pq
+ n := len(old)
+ item := old[n-1]
item.index = -1 // for safety
- *pq = a[0 : n-1]
+ *pq = old[0 : n-1]
return item
}
-// update is not used by the example but shows how to take the top item from
-// the queue, update its priority and value, and put it back.
-func (pq *PriorityQueue) update(value string, priority int) {
- item := heap.Pop(pq).(*Item)
- item.value = value
- item.priority = priority
- heap.Push(pq, item)
-}
-
-// changePriority is not used by the example but shows how to change the
-// priority of an arbitrary item.
-func (pq *PriorityQueue) changePriority(item *Item, priority int) {
+// update modifies the priority and value of an Item in the queue.
+func (pq *PriorityQueue) update(item *Item, value string, priority int) {
heap.Remove(pq, item.index)
+ item.value = value
item.priority = priority
heap.Push(pq, item)
}
-// This example pushes 10 items into a PriorityQueue and takes them out in
-// order of priority.
-func Example() {
- const nItem = 10
- // Random priorities for the items (a permutation of 0..9, times 11)).
- priorities := [nItem]int{
- 77, 22, 44, 55, 11, 88, 33, 99, 00, 66,
+// This example inserts some items into a PriorityQueue, manipulates an item,
+// and then removes the items in priority order.
+func Example_priorityQueue() {
+ // Some items and their priorities.
+ items := map[string]int{
+ "banana": 3, "apple": 2, "pear": 4,
}
- values := [nItem]string{
- "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
- }
- // Create a priority queue and put some items in it.
- pq := make(PriorityQueue, 0, nItem)
- for i := 0; i < cap(pq); i++ {
+
+ // Create a priority queue and put the items in it.
+ pq := &PriorityQueue{}
+ heap.Init(pq)
+ for value, priority := range items {
item := &Item{
- value: values[i],
- priority: priorities[i],
+ value: value,
+ priority: priority,
}
- heap.Push(&pq, item)
+ heap.Push(pq, item)
+ }
+
+ // Insert a new item and then modify its priority.
+ item := &Item{
+ value: "orange",
+ priority: 1,
}
- // Take the items out; should arrive in decreasing priority order.
- // For example, the highest priority (99) is the seventh item, so output starts with 99:"seven".
- for i := 0; i < nItem; i++ {
- item := heap.Pop(&pq).(*Item)
+ heap.Push(pq, item)
+ pq.update(item, item.value, 5)
+
+ // Take the items out; they arrive in decreasing priority order.
+ for pq.Len() > 0 {
+ item := heap.Pop(pq).(*Item)
fmt.Printf("%.2d:%s ", item.priority, item.value)
}
// Output:
- // 99:seven 88:five 77:zero 66:nine 55:three 44:two 33:six 22:one 11:four 00:eight
+ // 05:orange 04:pear 03:banana 02:apple
}