-
Notifications
You must be signed in to change notification settings - Fork 36
/
frequency.go
120 lines (103 loc) · 2.63 KB
/
frequency.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"strconv"
"strings"
)
// Frequency represents the Response frequency to use for a given Channel code prefix.
type Frequency struct {
Prefix string
Value float64
}
// NewFrequency builds a Frequency based on an encoded text string of the form <prefix>:<value>
func NewFrequency(str string) (Frequency, error) {
parts := strings.SplitN(str, ":", 2)
switch n := len(parts); {
case n > 1:
v, err := strconv.ParseFloat(strings.TrimSpace(parts[1]), 64)
if err != nil {
return Frequency{}, err
}
return Frequency{Prefix: strings.TrimSpace(parts[0]), Value: v}, nil
case n > 0:
v, err := strconv.ParseFloat(strings.TrimSpace(parts[0]), 64)
if err != nil {
return Frequency{}, err
}
return Frequency{Value: v}, nil
default:
return Frequency{}, nil
}
}
func (f Frequency) String() string {
if len(f.Prefix) > 0 {
return fmt.Sprintf("%s:%g", f.Prefix, f.Value)
}
return fmt.Sprintf("%g", f.Value)
}
func (f Frequency) MarshalText() ([]byte, error) {
return []byte(f.String()), nil
}
func (f *Frequency) UnmarshalText(data []byte) error {
v, err := NewFrequency(string(data))
if err != nil {
return err
}
*f = v
return nil
}
type Frequencies map[string]float64
func NewFrequencies(freqs ...Frequency) Frequencies {
res := make(map[string]float64)
for _, f := range freqs {
res[f.Prefix] = f.Value
}
return res
}
// Get gets the value associated with the given key.
// If there are no values associated with the key, Get
// returns zero and false, otherwise it returns the value and true.
func (f Frequencies) Get(key string) (float64, bool) {
if f == nil {
return 0.0, false
}
v, ok := f[key]
return v, ok
}
// Set sets the key to value. It replaces any existing values.
func (f Frequencies) Set(key string, value float64) {
f[key] = value
}
// Del deletes the value associated with key.
func (f Frequencies) Del(key string) {
delete(f, key)
}
// Has checks whether a given key is set.
func (f Frequencies) Has(key string) bool {
_, ok := f[key]
return ok
}
// Find checks the code against the prefixes and returns a value with the longest prefixes. For multiple matches the returned value will be matched alphabetically.
// If no matches are found then zero and false are returned, otherwise, the value and true will be returned.
func (f Frequencies) Find(code string) (float64, bool) {
match := -1
var key string
var value float64
for k, v := range f {
if !strings.HasPrefix(code, k) {
continue
}
if len(k) < match {
continue
}
if len(k) == match && k < key {
continue
}
match, key = len(k), k
value = v
}
if match < 0 {
return 0.0, false
}
return value, true
}