~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/container/heap/example_pq_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
// This example demonstrates a priority queue built using the heap interface.
 
6
package heap_test
 
7
 
 
8
import (
 
9
        "container/heap"
 
10
        "fmt"
 
11
)
 
12
 
 
13
// An Item is something we manage in a priority queue.
 
14
type Item struct {
 
15
        value    string // The value of the item; arbitrary.
 
16
        priority int    // The priority of the item in the queue.
 
17
        // The index is needed by update and is maintained by the heap.Interface methods.
 
18
        index int // The index of the item in the heap.
 
19
}
 
20
 
 
21
// A PriorityQueue implements heap.Interface and holds Items.
 
22
type PriorityQueue []*Item
 
23
 
 
24
func (pq PriorityQueue) Len() int { return len(pq) }
 
25
 
 
26
func (pq PriorityQueue) Less(i, j int) bool {
 
27
        // We want Pop to give us the highest, not lowest, priority so we use greater than here.
 
28
        return pq[i].priority > pq[j].priority
 
29
}
 
30
 
 
31
func (pq PriorityQueue) Swap(i, j int) {
 
32
        pq[i], pq[j] = pq[j], pq[i]
 
33
        pq[i].index = i
 
34
        pq[j].index = j
 
35
}
 
36
 
 
37
func (pq *PriorityQueue) Push(x interface{}) {
 
38
        n := len(*pq)
 
39
        item := x.(*Item)
 
40
        item.index = n
 
41
        *pq = append(*pq, item)
 
42
}
 
43
 
 
44
func (pq *PriorityQueue) Pop() interface{} {
 
45
        old := *pq
 
46
        n := len(old)
 
47
        item := old[n-1]
 
48
        item.index = -1 // for safety
 
49
        *pq = old[0 : n-1]
 
50
        return item
 
51
}
 
52
 
 
53
// update modifies the priority and value of an Item in the queue.
 
54
func (pq *PriorityQueue) update(item *Item, value string, priority int) {
 
55
        heap.Remove(pq, item.index)
 
56
        item.value = value
 
57
        item.priority = priority
 
58
        heap.Push(pq, item)
 
59
}
 
60
 
 
61
// This example inserts some items into a PriorityQueue, manipulates an item,
 
62
// and then removes the items in priority order.
 
63
func Example_priorityQueue() {
 
64
        // Some items and their priorities.
 
65
        items := map[string]int{
 
66
                "banana": 3, "apple": 2, "pear": 4,
 
67
        }
 
68
 
 
69
        // Create a priority queue and put the items in it.
 
70
        pq := &PriorityQueue{}
 
71
        heap.Init(pq)
 
72
        for value, priority := range items {
 
73
                item := &Item{
 
74
                        value:    value,
 
75
                        priority: priority,
 
76
                }
 
77
                heap.Push(pq, item)
 
78
        }
 
79
 
 
80
        // Insert a new item and then modify its priority.
 
81
        item := &Item{
 
82
                value:    "orange",
 
83
                priority: 1,
 
84
        }
 
85
        heap.Push(pq, item)
 
86
        pq.update(item, item.value, 5)
 
87
 
 
88
        // Take the items out; they arrive in decreasing priority order.
 
89
        for pq.Len() > 0 {
 
90
                item := heap.Pop(pq).(*Item)
 
91
                fmt.Printf("%.2d:%s ", item.priority, item.value)
 
92
        }
 
93
        // Output:
 
94
        // 05:orange 04:pear 03:banana 02:apple
 
95
}