-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopkqueue.go
More file actions
58 lines (53 loc) · 1.39 KB
/
topkqueue.go
File metadata and controls
58 lines (53 loc) · 1.39 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package pqueuespan
// TopKQueue maintains a fixed-size queue of items
// with k highest priorities.
type TopKQueue struct {
PQueueSpan
k int
}
func NewTopKQueue(k int) *TopKQueue {
return &TopKQueue{
*NewPQueueSpan(MINPQ),
k,
}
}
// DryPush checks whether a Push with the given priority
// will result in a materialized insertion to the
// TopKQueue
func (pq *TopKQueue) DryPush(lowerBound, upperBound float64) bool {
if pq.Size() < pq.k {
return true
}
_, bottom := pq.Head()
//if bottom < priority {
if bottom.lowerBound == lowerBound {
return bottom.upperBound < upperBound
//return true
}
return bottom.lowerBound < lowerBound
//return false
}
// Push pushes a new item to the TopKQueue, but does not
// actually insert the item into the queue unless its
// priority qualifies for the top-k
func (pq *TopKQueue) Push(value interface{}, lowerBound, upperBound float64) {
if !pq.DryPush(lowerBound, upperBound) {
return
}
if pq.Size() == pq.k {
pq.Pop()
}
pq.PQueueSpan.Push(value, lowerBound, upperBound)
}
func (pq *TopKQueue) Descending() (values []interface{}, lowerBounds, upperBounds []float64) {
values = make([]interface{}, pq.Size())
lowerBounds = make([]float64, pq.Size())
upperBounds = make([]float64, pq.Size())
for i := len(values) - 1; i >= 0; i-- {
v, p := pq.Pop()
values[i] = v
lowerBounds[i] = p.lowerBound
upperBounds[i] = p.upperBound
}
return
}