diff options
Diffstat (limited to 'src/pkg/container/heap')
-rw-r--r-- | src/pkg/container/heap/example_intheap_test.go | 43 | ||||
-rw-r--r-- | src/pkg/container/heap/example_pq_test.go | 95 | ||||
-rw-r--r-- | src/pkg/container/heap/example_test.go | 105 | ||||
-rw-r--r-- | src/pkg/container/heap/heap.go | 10 | ||||
-rw-r--r-- | src/pkg/container/heap/heap_test.go | 16 |
5 files changed, 157 insertions, 112 deletions
diff --git a/src/pkg/container/heap/example_intheap_test.go b/src/pkg/container/heap/example_intheap_test.go new file mode 100644 index 000000000..e718cbc58 --- /dev/null +++ b/src/pkg/container/heap/example_intheap_test.go @@ -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 +} diff --git a/src/pkg/container/heap/example_pq_test.go b/src/pkg/container/heap/example_pq_test.go new file mode 100644 index 000000000..8cbeb8d70 --- /dev/null +++ b/src/pkg/container/heap/example_pq_test.go @@ -0,0 +1,95 @@ +// 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 a priority queue built using the heap interface. +package heap_test + +import ( + "container/heap" + "fmt" +) + +// An Item is something we manage in a priority queue. +type Item struct { + value string // The value of the item; arbitrary. + priority int // The priority of the item in the queue. + // The index is needed by update and is maintained by the heap.Interface methods. + index int // The index of the item in the heap. +} + +// A PriorityQueue implements heap.Interface and holds Items. +type PriorityQueue []*Item + +func (pq PriorityQueue) Len() int { return len(pq) } + +func (pq PriorityQueue) Less(i, j int) bool { + // We want Pop to give us the highest, not lowest, priority so we use greater than here. + return pq[i].priority > pq[j].priority +} + +func (pq PriorityQueue) Swap(i, j int) { + pq[i], pq[j] = pq[j], pq[i] + pq[i].index = i + pq[j].index = j +} + +func (pq *PriorityQueue) Push(x interface{}) { + n := len(*pq) + item := x.(*Item) + item.index = n + *pq = append(*pq, item) +} + +func (pq *PriorityQueue) Pop() interface{} { + old := *pq + n := len(old) + item := old[n-1] + item.index = -1 // for safety + *pq = old[0 : n-1] + return item +} + +// 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 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, + } + + // Create a priority queue and put the items in it. + pq := &PriorityQueue{} + heap.Init(pq) + for value, priority := range items { + item := &Item{ + value: value, + priority: priority, + } + heap.Push(pq, item) + } + + // Insert a new item and then modify its priority. + item := &Item{ + value: "orange", + priority: 1, + } + 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: + // 05:orange 04:pear 03:banana 02:apple +} diff --git a/src/pkg/container/heap/example_test.go b/src/pkg/container/heap/example_test.go deleted file mode 100644 index 2050bc835..000000000 --- a/src/pkg/container/heap/example_test.go +++ /dev/null @@ -1,105 +0,0 @@ -// 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 a priority queue built using the heap interface. -package heap_test - -import ( - "container/heap" - "fmt" -) - -// An Item is something we manage in a priority queue. -type Item struct { - value string // The value of the item; arbitrary. - priority int // The priority of the item in the queue. - // The index is needed by changePriority and is maintained by the heap.Interface methods. - index int // The index of the item in the heap. -} - -// A PriorityQueue implements heap.Interface and holds Items. -type PriorityQueue []*Item - -func (pq PriorityQueue) Len() int { return len(pq) } - -func (pq PriorityQueue) Less(i, j int) bool { - // We want Pop to give us the highest, not lowest, priority so we use greater than here. - return pq[i].priority > pq[j].priority -} - -func (pq PriorityQueue) Swap(i, j int) { - pq[i], pq[j] = pq[j], pq[i] - pq[i].index = i - pq[j].index = j -} - -func (pq *PriorityQueue) Push(x interface{}) { - // Push and Pop use pointer receivers because they modify the slice's length, - // not just its contents. - // To simplify indexing expressions in these methods, we save a copy of the - // slice object. We could instead write (*pq)[i]. - a := *pq - n := len(a) - a = a[0 : n+1] - item := x.(*Item) - item.index = n - a[n] = item - *pq = a -} - -func (pq *PriorityQueue) Pop() interface{} { - a := *pq - n := len(a) - item := a[n-1] - item.index = -1 // for safety - *pq = a[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) { - heap.Remove(pq, item.index) - 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, - } - 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++ { - item := &Item{ - value: values[i], - priority: priorities[i], - } - heap.Push(&pq, item) - } - // 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) - 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 -} diff --git a/src/pkg/container/heap/heap.go b/src/pkg/container/heap/heap.go index 67018e6ba..c37e50e3c 100644 --- a/src/pkg/container/heap/heap.go +++ b/src/pkg/container/heap/heap.go @@ -4,13 +4,13 @@ // Package heap provides heap operations for any type that implements // heap.Interface. A heap is a tree with the property that each node is the -// highest-valued node in its subtree. +// minimum-valued node in its subtree. // // A heap is a common way to implement a priority queue. To build a priority // 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 @@ -79,7 +79,7 @@ func Remove(h Interface, i int) interface{} { func up(h Interface, j int) { for { i := (j - 1) / 2 // parent - if i == j || h.Less(i, j) { + if i == j || !h.Less(j, i) { break } h.Swap(i, j) @@ -90,14 +90,14 @@ func up(h Interface, j int) { func down(h Interface, i, n int) { for { j1 := 2*i + 1 - if j1 >= n { + if j1 >= n || j1 < 0 { // j1 < 0 after int overflow break } j := j1 // left child if j2 := j1 + 1; j2 < n && !h.Less(j1, j2) { j = j2 // = 2*i + 2 // right child } - if h.Less(i, j) { + if !h.Less(j, i) { break } h.Swap(i, j) diff --git a/src/pkg/container/heap/heap_test.go b/src/pkg/container/heap/heap_test.go index cb31ef6d3..274d587d8 100644 --- a/src/pkg/container/heap/heap_test.go +++ b/src/pkg/container/heap/heap_test.go @@ -2,10 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package heap_test +package heap import ( - . "container/heap" "testing" ) @@ -170,3 +169,16 @@ func TestRemove2(t *testing.T) { } } } + +func BenchmarkDup(b *testing.B) { + const n = 10000 + h := make(myHeap, n) + for i := 0; i < b.N; i++ { + for j := 0; j < n; j++ { + Push(&h, 0) // all elements are the same + } + for h.Len() > 0 { + Pop(&h) + } + } +} |