-
Notifications
You must be signed in to change notification settings - Fork 0
/
agg.go
105 lines (85 loc) · 1.37 KB
/
agg.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
package gosim
import (
"math"
)
type Agg interface {
Update(ival int64, fval float64)
Get() (int64, float64)
Gather(a Agg)
Reset()
}
type cntSum struct {
cnt int64
sum float64
}
func newSum() *cntSum {
var c cntSum
c.Reset()
return &c
}
func (c *cntSum) Update(ival int64, fval float64) {
c.cnt += ival
c.sum += fval
}
func (c *cntSum) Gather(a Agg) {
i, f := a.Get()
c.cnt += i
c.sum += f
}
func (c *cntSum) Get() (int64, float64) {
return c.cnt, c.sum
}
func (c *cntSum) Reset() {
c.cnt = 0
c.sum = 0
}
type cntMin struct {
cnt int64
min float64
}
func newMin() *cntMin {
var c cntMin
c.Reset()
return &c
}
func (c *cntMin) Update(ival int64, fval float64) {
c.cnt += ival
c.min = math.Min(c.min, fval)
}
func (c *cntMin) Get() (int64, float64) {
return c.cnt, c.min
}
func (c *cntMin) Gather(a Agg) {
i, f := a.Get()
c.cnt += i
c.min += math.Min(c.min, f)
}
func (c *cntMin) Reset() {
c.cnt = 0
c.min = math.Inf(1)
}
type cntMax struct {
cnt int64
max float64
}
func newMax() *cntMax {
var c cntMax
c.Reset()
return &c
}
func (c *cntMax) Update(ival int64, fval float64) {
c.cnt += ival
c.max = math.Max(c.max, fval)
}
func (c *cntMax) Gather(a Agg) {
i, f := a.Get()
c.cnt += i
c.max += math.Max(c.max, f)
}
func (c *cntMax) Get() (int64, float64) {
return c.cnt, c.max
}
func (c *cntMax) Reset() {
c.cnt = 0
c.max = math.Inf(-1)
}