-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentities.go
75 lines (61 loc) · 2 KB
/
entities.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
package attribution
import (
"fmt"
"math/big"
"strings"
)
// A Touchpoint represents a contributing entity in a ContributionSet.
type Touchpoint struct {
Name string // name of the touchpoint
}
// Touchpoints represents a list of touchpoints.
// It implements the sort.Interface interface.
type Touchpoints []Touchpoint
// String provides a string represenation of Touchpoints.
func (touchpoints Touchpoints) String() string {
names := []string{}
for _, touchpoint := range touchpoints {
names = append(names, fmt.Sprintf("{%s}", touchpoint.Name))
}
return "[" + strings.Join(names, " ") + "]"
}
// Len returns the length of Touchpoints.
func (touchpoints Touchpoints) Len() int {
return len(touchpoints)
}
// Less provides a strict order on Touchpoints.
func (touchpoints Touchpoints) Less(i, j int) bool {
return strings.Compare(touchpoints[i].Name, touchpoints[j].Name) == -1
}
// Swap swaps the order of two elements of Touchpoints.
func (touchpoints Touchpoints) Swap(i, j int) {
touchpoint := touchpoints[i]
touchpoints[i] = touchpoints[j]
touchpoints[j] = touchpoint
}
// A Contribution consists of an ordered list of touchpoints together with their combined value.
type Contribution struct {
Touchpoints Touchpoints
Value big.Float
}
func (contribution Contribution) String() string {
return fmt.Sprintf("{%s %s}", contribution.Touchpoints, contribution.Value.String())
}
func (contribution Contribution) Set() ContributionSet {
touchpoints := make(map[Touchpoint]struct{})
for _, touchpoint := range contribution.Touchpoints {
touchpoints[touchpoint] = struct{}{}
}
return ContributionSet{
Touchpoints: touchpoints,
Value: contribution.Value,
}
}
// A ContributionSet consists of an unordered set of touchpoints together with their combined value.
type ContributionSet struct {
Touchpoints map[Touchpoint]struct{}
Value big.Float
}
func (contribution ContributionSet) String() string {
return fmt.Sprintf("{%s %s}", contribution.Touchpoints, contribution.Value.String())
}