-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.go
82 lines (73 loc) · 1.67 KB
/
combine.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
package traces
// Combine returns a new Series that is the result of appling function f to
// all the points in all the series in the list.
//
// Helper functions are provided for adding, subtracting, any-ing and all-ing.
// For example, to add the series s0, s1 and s2:
//
// sum := traces.Combine(traces.Sum, s0, s1, s2)
//
func Combine(f func(...int64) int64, list ...*Series) *Series {
points := make(map[int64]int64)
unsorted := make([]int64, 0)
valueSet := make([]int64, len(list))
for _, s := range list {
for key := range s.points {
if _, ok := points[key]; !ok {
for i, t0 := range list {
valueSet[i] = t0.Get(key)
}
points[key] = f(valueSet...)
unsorted = append(unsorted, key)
}
}
}
return &Series{
points: points,
sorted: make([]int64, 0),
unsorted: unsorted,
}
}
// Sum adds all the vals. Use with the Combine function.
func Sum(vals ...int64) int64 {
var sum int64
for _, val := range vals {
sum += val
}
return sum
}
// Diff subtracts from the first value all the remaining values. Use with
// the Combine function.
func Diff(vals ...int64) int64 {
var diff int64
for i, val := range vals {
if i == 0 {
diff = val
} else {
diff -= val
}
}
return diff
}
// Any returns one if any of the values are nonzero and zero otherwise.
// Use with the Combine function.
func Any(vals ...int64) int64 {
var any int64
for _, val := range vals {
if val != 0 {
any = 1
}
}
return any
}
// All returns one if all of the values are nonzero and zero otherwise.
// Use with the Combine function.
func All(vals ...int64) int64 {
var all int64 = 1
for _, val := range vals {
if val == 0 {
all = 0
}
}
return all
}