-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcandidate.go
51 lines (39 loc) · 1.04 KB
/
candidate.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
49
package fingerprint
import "fmt"
type CandidateFilter func(candidate) bool
type candidate struct {
Freq float64
Pxx float64
}
type candidates []candidate
func (c candidates) String() string {
var s string
for _, v := range c {
s += fmt.Sprintf("%9.2f (%.2f)\t", v.Freq, v.Pxx)
}
return s
}
func (cs candidates) filter(f CandidateFilter) (fc candidates) {
fc = make(candidates, 0, 5)
for _, c := range cs {
if (f(c)) {
fc = append(fc, c)
}
}
return
}
func NewCandidates(Pxx, freqs []float64) (c candidates) {
c = make(candidates, 0)
for i, x := range freqs {
c = append(c, candidate{Freq: x, Pxx: Pxx[i]})
}
return
}
type ByPxx []candidate
func (a ByPxx) Len() int { return len(a) }
func (a ByPxx) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByPxx) Less(i, j int) bool { return a[i].Pxx < a[j].Pxx }
type ByFreq []candidate
func (a ByFreq) Len() int { return len(a) }
func (a ByFreq) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByFreq) Less(i, j int) bool { return a[i].Freq < a[j].Freq }