From: Frithjof Schulze Date: Tue, 4 Dec 2012 22:11:33 +0000 (-0800) Subject: container/heap: Simplify the example. X-Git-Tag: go1.1rc2~1720 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=cfc0a59d6e4e11b08b7b7085e6da031643879b0a;p=gostls13.git 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 --- diff --git a/src/pkg/container/heap/example_test.go b/src/pkg/container/heap/example_test.go index 2050bc8359..70f654a007 100644 --- a/src/pkg/container/heap/example_test.go +++ b/src/pkg/container/heap/example_test.go @@ -37,15 +37,10 @@ func (pq PriorityQueue) Swap(i, j int) { 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] + n := len(*pq) item := x.(*Item) item.index = n - a[n] = item - *pq = a + *pq = append(*pq, item) } func (pq *PriorityQueue) Pop() interface{} {