~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): Serge Hallyn
  • Date: 2014-11-18 15:12:26 UTC
  • mfrom: (14.2.12 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141118151226-zug7vn93mn3dtiz3
Tags: 2:1.3.2-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - Support co-installability with gccgo-go tool:
    - d/rules,golang-go.install: Rename bin/go -> bin/golang-go
    - d/golang-go.{postinst,prerm}: Install/remove /usr/bin/go using
      alternatives.
  - d/copyright: Amendments for full compiliance with copyright format.
  - d/control: Demote golang-go.tools to Suggests to support Ubuntu MIR.
  - dropped patches (now upstream):
    - d/p/issue27650045_40001_50001.diff
    - d/p/issue28050043_60001_70001.diff
    - d/p/issue54790044_100001_110001.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
 
53
53
// update modifies the priority and value of an Item in the queue.
54
54
func (pq *PriorityQueue) update(item *Item, value string, priority int) {
55
 
        heap.Remove(pq, item.index)
56
55
        item.value = value
57
56
        item.priority = priority
58
 
        heap.Push(pq, item)
 
57
        heap.Fix(pq, item.index)
59
58
}
60
59
 
61
 
// This example inserts some items into a PriorityQueue, manipulates an item,
 
60
// This example creates a PriorityQueue with some items, adds and manipulates an item,
62
61
// and then removes the items in priority order.
63
62
func Example_priorityQueue() {
64
63
        // Some items and their priorities.
66
65
                "banana": 3, "apple": 2, "pear": 4,
67
66
        }
68
67
 
69
 
        // Create a priority queue and put the items in it.
70
 
        pq := &PriorityQueue{}
71
 
        heap.Init(pq)
 
68
        // Create a priority queue, put the items in it, and
 
69
        // establish the priority queue (heap) invariants.
 
70
        pq := make(PriorityQueue, len(items))
 
71
        i := 0
72
72
        for value, priority := range items {
73
 
                item := &Item{
 
73
                pq[i] = &Item{
74
74
                        value:    value,
75
75
                        priority: priority,
 
76
                        index:    i,
76
77
                }
77
 
                heap.Push(pq, item)
 
78
                i++
78
79
        }
 
80
        heap.Init(&pq)
79
81
 
80
82
        // Insert a new item and then modify its priority.
81
83
        item := &Item{
82
84
                value:    "orange",
83
85
                priority: 1,
84
86
        }
85
 
        heap.Push(pq, item)
 
87
        heap.Push(&pq, item)
86
88
        pq.update(item, item.value, 5)
87
89
 
88
90
        // Take the items out; they arrive in decreasing priority order.
89
91
        for pq.Len() > 0 {
90
 
                item := heap.Pop(pq).(*Item)
 
92
                item := heap.Pop(&pq).(*Item)
91
93
                fmt.Printf("%.2d:%s ", item.priority, item.value)
92
94
        }
93
95
        // Output: