aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/vbauerster/mpb/v7/priority_queue.go
blob: 152482e9ac9a134f9897af3dc01b2db0e5ba2842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package mpb

// A priorityQueue implements heap.Interface
type priorityQueue []*Bar

func (pq priorityQueue) Len() int { return len(pq) }

func (pq priorityQueue) Less(i, j int) bool {
	// less priority pops first
	return pq[i].priority > pq[j].priority
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *priorityQueue) Push(x interface{}) {
	s := *pq
	bar := x.(*Bar)
	bar.index = len(s)
	s = append(s, bar)
	*pq = s
}

func (pq *priorityQueue) Pop() interface{} {
	s := *pq
	*pq = s[0 : len(s)-1]
	bar := s[len(s)-1]
	bar.index = -1 // for safety
	return bar
}