forked from Nordstrom/choices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
108 lines (91 loc) · 2.7 KB
/
value.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
// Copyright 2016 Andrew O'Neill, Nordstrom
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package choices
import (
"encoding/json"
"fmt"
)
// ValueType are the different types of Values a Param can have. This is used
// for parsing params in storage.
type ValueType int
// Constants for ValueTypes
const (
ValueTypeBad ValueType = iota
ValueTypeUniform
ValueTypeWeighted
)
// choice is the interface Param Choices must implement. They take a hash value
// and return the string that represents the value or an error.
type choice interface {
Choice(i uint64) (string, error)
}
// Uniform is a way to select from a list of Choices with uniform probability.
type Uniform struct {
Choices []string
}
// Choice implements the choice interface for Uniform choices.
func (u *Uniform) Choice(i uint64) (string, error) {
choice := int(i % uint64(len(u.Choices)))
return u.Choices[choice], nil
}
// MarshalJSON implements the json.Marshaler interface for Uniform choices.
func (u *Uniform) MarshalJSON() ([]byte, error) {
var aux = struct {
Choices []string `json:"choices"`
}{
Choices: u.Choices,
}
return json.Marshal(aux)
}
// Weighted is a way to select from a list of Choices with probability ratio
// supplied in Weights.
type Weighted struct {
Choices []weightedChoice
}
type weightedChoice struct {
name string
weight float64
}
func (w *weightedChoice) MarshalJSON() ([]byte, error) {
var aux = struct {
Name string `json:"name"`
Weight float64 `json:"weight"`
}{
Name: w.name,
Weight: w.weight,
}
return json.Marshal(aux)
}
// Choice implements the choice interface for Weighted choices.
func (w *Weighted) Choice(i uint64) (string, error) {
selection := make([]float64, len(w.Choices))
cumSum := 0.0
for ii, v := range w.Choices {
cumSum += v.weight
selection[ii] = cumSum
}
choice := uniform(i, 0, cumSum)
for ii, v := range selection {
if choice <= v {
return w.Choices[ii].name, nil
}
}
return "", fmt.Errorf("no selection was made")
}
// MarshalJSON implements the json.Marshaler interface for Weighted choices.
func (w *Weighted) MarshalJSON() ([]byte, error) {
var aux = struct {
Choices []weightedChoice `json:"choices"`
}{
Choices: w.Choices,
}
return json.Marshal(aux)
}