-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitlist.go
48 lines (39 loc) · 940 Bytes
/
hitlist.go
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
package main
import (
"sort"
)
// Pair is a k/v bucket for HitList.
type Pair struct {
Key string
Value int
}
// HitList is a map for sorted data with struct value types.
type HitList map[int]Pair
// CutTopN returns top n hit sections from an interval data.
func CutTopN(h HitList, n uint) HitList {
if len(h) <= int(n) {
return h
}
out := HitList{}
for i := 0; i < int(n); i++ {
out[i] = h[i]
}
return out
}
// RankByHits sorts HitList in reverse order based on Pair Value.
func RankByHits(sectionHits map[string]int) HitList {
pl := make(HitList, len(sectionHits))
if len(sectionHits) == 0 {
return pl
}
i := 0
for k, v := range sectionHits {
pl[i] = Pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
func (p HitList) Len() int { return len(p) }
func (p HitList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p HitList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }