Commit cfc0a59d authored by Frithjof Schulze's avatar Frithjof Schulze Committed by Robert Griesemer

container/heap: Simplify the example.

Using append simplifies the code and makes it work if
the initial capacity of the slice is smaller than the
number of items pushed.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/6869060
parent 2cb715a8
...@@ -37,15 +37,10 @@ func (pq PriorityQueue) Swap(i, j int) { ...@@ -37,15 +37,10 @@ func (pq PriorityQueue) Swap(i, j int) {
func (pq *PriorityQueue) Push(x interface{}) { func (pq *PriorityQueue) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length, // Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents. // not just its contents.
// To simplify indexing expressions in these methods, we save a copy of the n := len(*pq)
// slice object. We could instead write (*pq)[i].
a := *pq
n := len(a)
a = a[0 : n+1]
item := x.(*Item) item := x.(*Item)
item.index = n item.index = n
a[n] = item *pq = append(*pq, item)
*pq = a
} }
func (pq *PriorityQueue) Pop() interface{} { func (pq *PriorityQueue) Pop() interface{} {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment