]> Cypherpunks repositories - gostls13.git/commitdiff
container/heap: Simplify the example.
authorFrithjof Schulze <schulze@math.uni-hannover.de>
Tue, 4 Dec 2012 22:11:33 +0000 (14:11 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 4 Dec 2012 22:11:33 +0000 (14:11 -0800)
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

src/pkg/container/heap/example_test.go

index 2050bc835915d1d3dab107af03354637d0fb37ce..70f654a00792a4956e52ba5a5667971120e86466 100644 (file)
@@ -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{} {