-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculates.go
74 lines (68 loc) · 1.3 KB
/
calculates.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
package windows
import "math"
type WinCalculateFunc func(rw *RollingWindow) (valSum float64, total int)
func GetRollingWindowSum(rw *RollingWindow) (valSum float64, total int) {
rw.Cal(func(b *Item) {
valSum += b.Val
total += b.Total
})
return
}
func GetRollingWindowAvg(rw *RollingWindow) (valSum float64, total int) {
rw.Cal(func(b *Item) {
valSum += b.Val
total += b.Total
})
valSum = valSum / float64(rw.GetStaticItemNum())
return
}
func GetRollingWindowMax(rw *RollingWindow) (float64, int) {
var max *float64
var _max float64
var total int
var cnt int
rw.Cal(func(b *Item) {
if max == nil {
_max = b.Val
max = &_max
} else {
_max = math.Max(*max, b.Val)
max = &_max
}
total += b.Total
cnt += 1
})
if max == nil {
return 0, total
}
if cnt < rw.GetStaticItemNum() {
_max = math.Max(*max, 0)
max = &_max
}
return *max, total
}
func GetRollingWindowMin(rw *RollingWindow) (float64, int) {
var min *float64
var _min float64
var total int
var cnt int
rw.Cal(func(b *Item) {
if min == nil {
_min = b.Val
min = &_min
} else {
_min = math.Min(*min, b.Val)
min = &_min
}
total += b.Total
cnt += 1
})
if min == nil {
return 0, total
}
if cnt < rw.GetStaticItemNum() {
_min = math.Min(*min, 0)
min = &_min
}
return *min, total
}