diff --git a/priority_queue.go b/priority_queue.go index f4091f6..0863b57 100644 --- a/priority_queue.go +++ b/priority_queue.go @@ -20,17 +20,18 @@ } func (pq *priorityQueue) Push(x interface{}) { - s := *pq + n := len(*pq) bar := x.(*Bar) - bar.index = len(s) - s = append(s, bar) - *pq = s + bar.index = n + *pq = append(*pq, bar) } func (pq *priorityQueue) Pop() interface{} { - s := *pq - *pq = s[0 : len(s)-1] - bar := s[len(s)-1] + old := *pq + n := len(old) + bar := old[n-1] + old[n-1] = nil // avoid memory leak bar.index = -1 // for safety + *pq = old[:n-1] return bar }