diff options
Diffstat (limited to 'src/pkg/container/heap')
-rw-r--r-- | src/pkg/container/heap/example_pq_test.go | 22 | ||||
-rw-r--r-- | src/pkg/container/heap/heap.go | 4 |
2 files changed, 14 insertions, 12 deletions
diff --git a/src/pkg/container/heap/example_pq_test.go b/src/pkg/container/heap/example_pq_test.go index 8cbeb8d70..7017095cb 100644 --- a/src/pkg/container/heap/example_pq_test.go +++ b/src/pkg/container/heap/example_pq_test.go @@ -52,13 +52,12 @@ func (pq *PriorityQueue) Pop() interface{} { // 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) + heap.Fix(pq, item.index) } -// This example inserts some items into a PriorityQueue, manipulates an item, +// This example creates a PriorityQueue with some items, adds and manipulates an item, // and then removes the items in priority order. func Example_priorityQueue() { // Some items and their priorities. @@ -66,28 +65,31 @@ func Example_priorityQueue() { "banana": 3, "apple": 2, "pear": 4, } - // Create a priority queue and put the items in it. - pq := &PriorityQueue{} - heap.Init(pq) + // Create a priority queue, put the items in it, and + // establish the priority queue (heap) invariants. + pq := make(PriorityQueue, len(items)) + i := 0 for value, priority := range items { - item := &Item{ + pq[i] = &Item{ value: value, priority: priority, + index: i, } - heap.Push(pq, item) + i++ } + heap.Init(&pq) // Insert a new item and then modify its priority. item := &Item{ value: "orange", priority: 1, } - heap.Push(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) + item := heap.Pop(&pq).(*Item) fmt.Printf("%.2d:%s ", item.priority, item.value) } // Output: diff --git a/src/pkg/container/heap/heap.go b/src/pkg/container/heap/heap.go index 52c8507b4..c467a1191 100644 --- a/src/pkg/container/heap/heap.go +++ b/src/pkg/container/heap/heap.go @@ -22,7 +22,7 @@ import "sort" // min-heap with the following invariants (established after // Init has been called or if the data is empty or sorted): // -// !h.Less(j, i) for 0 <= i < h.Len() and j = 2*i+1 or 2*i+2 and j < h.Len() +// !h.Less(j, i) for 0 <= i < h.Len() and 2*i+1 <= j <= 2*i+2 and j < h.Len() // // Note that Push and Pop in this interface are for package heap's // implementation to call. To add and remove things from the heap, @@ -78,7 +78,7 @@ func Remove(h Interface, i int) interface{} { return h.Pop() } -// Fix reestablishes the heap ordering after the element at index i has changed its value. +// Fix re-establishes the heap ordering after the element at index i has changed its value. // Changing the value of the element at index i and then calling Fix is equivalent to, // but less expensive than, calling Remove(h, i) followed by a Push of the new value. // The complexity is O(log(n)) where n = h.Len(). |