From cfc0a59d6e4e11b08b7b7085e6da031643879b0a Mon Sep 17 00:00:00 2001 From: Frithjof Schulze Date: Tue, 4 Dec 2012 14:11:33 -0800 Subject: [PATCH] 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 --- src/pkg/container/heap/example_test.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) 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{} {