client_golang/prometheus/priority_queue.go

49 lines
865 B
Go
Raw Normal View History

// Copyright (c) 2013, Prometheus Team
// All rights reserved.
2012-05-20 01:59:25 +04:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2012-05-20 01:59:25 +04:00
package prometheus
2012-05-20 01:59:25 +04:00
type item struct {
2012-05-20 01:59:25 +04:00
Priority int64
Value interface{}
2012-05-20 01:59:25 +04:00
index int
}
type priorityQueue []*item
2012-05-20 01:59:25 +04:00
func (q priorityQueue) Len() int {
2012-05-20 01:59:25 +04:00
return len(q)
}
func (q priorityQueue) Less(i, j int) bool {
2012-05-20 01:59:25 +04:00
return q[i].Priority > q[j].Priority
}
func (q priorityQueue) Swap(i, j int) {
2012-05-20 01:59:25 +04:00
q[i], q[j] = q[j], q[i]
q[i].index = i
q[j].index = j
}
func (q *priorityQueue) Push(x interface{}) {
2012-05-20 01:59:25 +04:00
queue := *q
size := len(queue)
queue = queue[0 : size+1]
item := x.(*item)
2012-05-20 01:59:25 +04:00
item.index = size
queue[size] = item
*q = queue
}
func (q *priorityQueue) Pop() interface{} {
2012-05-20 01:59:25 +04:00
queue := *q
size := len(queue)
item := queue[size-1]
item.index = -1
*q = queue[0 : size-1]
return item
}