Codebase list golang-github-vbauerster-mpb / e4a12c6
pq refactoring follow container/heap example from std doc. Vladimir Bauer 2 years ago
1 changed file(s) with 8 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
1919 }
2020
2121 func (pq *priorityQueue) Push(x interface{}) {
22 s := *pq
22 n := len(*pq)
2323 bar := x.(*Bar)
24 bar.index = len(s)
25 s = append(s, bar)
26 *pq = s
24 bar.index = n
25 *pq = append(*pq, bar)
2726 }
2827
2928 func (pq *priorityQueue) Pop() interface{} {
30 s := *pq
31 *pq = s[0 : len(s)-1]
32 bar := s[len(s)-1]
29 old := *pq
30 n := len(old)
31 bar := old[n-1]
32 old[n-1] = nil // avoid memory leak
3333 bar.index = -1 // for safety
34 *pq = old[:n-1]
3435 return bar
3536 }