]> Cypherpunks repositories - gostls13.git/commitdiff
container/heap: split example into two
authorCaleb Spare <cespare@gmail.com>
Thu, 31 Jan 2013 07:14:29 +0000 (23:14 -0800)
committerRuss Cox <rsc@golang.org>
Thu, 31 Jan 2013 07:14:29 +0000 (23:14 -0800)
This adds a simple IntHeap example, and modifies the more complex
PriorityQueue example to make use of the index field it maintains.

Fixes #4331.

R=rsc, adg
CC=golang-dev
https://golang.org/cl/7068048

src/pkg/container/heap/example_intheap_test.go [new file with mode: 0644]
src/pkg/container/heap/example_pq_test.go [moved from src/pkg/container/heap/example_test.go with 50% similarity]
src/pkg/container/heap/heap.go

diff --git a/src/pkg/container/heap/example_intheap_test.go b/src/pkg/container/heap/example_intheap_test.go
new file mode 100644 (file)
index 0000000..e718cbc
--- /dev/null
@@ -0,0 +1,43 @@
+// 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
+}
similarity index 50%
rename from src/pkg/container/heap/example_test.go
rename to src/pkg/container/heap/example_pq_test.go
index 70f654a00792a4956e52ba5a5667971120e86466..0f91de138e58bdf03d0b55d2f3de07f95ecaf5bb 100644 (file)
@@ -35,8 +35,6 @@ func (pq PriorityQueue) Swap(i, j int) {
 }
 
 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
@@ -44,57 +42,54 @@ func (pq *PriorityQueue) Push(x interface{}) {
 }
 
 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
 }
index bbaf40a989d5c26378b8a12792ce4a80476df309..d17a30aec3b1bc252cbe5601ceac52850dcfb7f6 100644 (file)
@@ -10,7 +10,7 @@
 // queue, implement the Heap interface with the (negative) priority as the
 // ordering for the Less method, so Push adds items while Pop removes the
 // highest-priority item from the queue. The Examples include such an
-// implementation; the file example_test.go has the complete source.
+// implementation; the file example_pq_test.go has the complete source.
 //
 package heap