2020-07-26 17:09:05 +08:00
|
|
|
package stat
|
|
|
|
|
|
|
|
import "container/heap"
|
|
|
|
|
|
|
|
type taskHeap []Task
|
|
|
|
|
|
|
|
func (h *taskHeap) Len() int {
|
|
|
|
return len(*h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskHeap) Less(i, j int) bool {
|
|
|
|
return (*h)[i].Duration < (*h)[j].Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskHeap) Swap(i, j int) {
|
|
|
|
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
|
|
|
|
}
|
|
|
|
|
2023-01-24 16:32:02 +08:00
|
|
|
func (h *taskHeap) Push(x any) {
|
2020-07-26 17:09:05 +08:00
|
|
|
*h = append(*h, x.(Task))
|
|
|
|
}
|
|
|
|
|
2023-01-24 16:32:02 +08:00
|
|
|
func (h *taskHeap) Pop() any {
|
2020-07-26 17:09:05 +08:00
|
|
|
old := *h
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
|
|
|
*h = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func topK(all []Task, k int) []Task {
|
|
|
|
h := new(taskHeap)
|
|
|
|
heap.Init(h)
|
|
|
|
|
|
|
|
for _, each := range all {
|
|
|
|
if h.Len() < k {
|
|
|
|
heap.Push(h, each)
|
|
|
|
} else if (*h)[0].Duration < each.Duration {
|
|
|
|
heap.Pop(h)
|
|
|
|
heap.Push(h, each)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *h
|
|
|
|
}
|