-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathavgmax.go
158 lines (131 loc) · 3.11 KB
/
avgmax.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2019, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package minmax
import "fmt"
//gosl: start minmax
const (
MaxFloat32 float32 = 3.402823466e+38
MinFloat32 float32 = 1.175494351e-38
)
// AvgMax holds average and max statistics
type AvgMax32 struct {
Avg float32
Max float32
// sum for computing average
Sum float32
// index of max item
MaxIndex int32
// number of items in sum
N int32
pad, pad1, pad2 int32
}
// Init initializes prior to new updates
func (am *AvgMax32) Init() {
am.Avg = 0
am.Sum = 0
am.N = 0
am.Max = -MaxFloat32
am.MaxIndex = -1
}
// UpdateVal updates stats from given value
func (am *AvgMax32) UpdateValue(val float32, idx int32) {
am.Sum += val
am.N++
if val > am.Max {
am.Max = val
am.MaxIndex = idx
}
}
// UpdateFromOther updates these values from other AvgMax32 values
func (am *AvgMax32) UpdateFromOther(oSum, oMax float32, oN, oMaxIndex int32) {
am.Sum += oSum
am.N += oN
if oMax > am.Max {
am.Max = oMax
am.MaxIndex = oMaxIndex
}
}
// CalcAvg computes the average given the current Sum and N values
func (am *AvgMax32) CalcAvg() {
if am.N > 0 {
am.Avg = am.Sum / float32(am.N)
} else {
am.Avg = am.Sum
am.Max = am.Avg // prevents Max from being -MaxFloat..
}
}
//gosl: end minmax
func (am *AvgMax32) String() string {
return fmt.Sprintf("{Avg: %g, Max: %g, Sum: %g, MaxIndex: %d, N: %d}", am.Avg, am.Max, am.Sum, am.MaxIndex, am.N)
}
// UpdateFrom updates these values from other AvgMax32 values
func (am *AvgMax32) UpdateFrom(oth *AvgMax32) {
am.UpdateFromOther(oth.Sum, oth.Max, oth.N, oth.MaxIndex)
am.Sum += oth.Sum
am.N += oth.N
if oth.Max > am.Max {
am.Max = oth.Max
am.MaxIndex = oth.MaxIndex
}
}
// CopyFrom copies from other AvgMax32
func (am *AvgMax32) CopyFrom(oth *AvgMax32) {
*am = *oth
}
///////////////////////////////////////////////////////////////////////////
// 64
// AvgMax holds average and max statistics
type AvgMax64 struct {
Avg float64
Max float64
// sum for computing average
Sum float64
// index of max item
MaxIndex int32
// number of items in sum
N int32
}
// Init initializes prior to new updates
func (am *AvgMax64) Init() {
am.Avg = 0
am.Sum = 0
am.N = 0
am.Max = -MaxFloat64
am.MaxIndex = -1
}
// UpdateVal updates stats from given value
func (am *AvgMax64) UpdateValue(val float64, idx int) {
am.Sum += val
am.N++
if val > am.Max {
am.Max = val
am.MaxIndex = int32(idx)
}
}
// CalcAvg computes the average given the current Sum and N values
func (am *AvgMax64) CalcAvg() {
if am.N > 0 {
am.Avg = am.Sum / float64(am.N)
} else {
am.Avg = am.Sum
am.Max = am.Avg // prevents Max from being -MaxFloat..
}
}
// UpdateFrom updates these values from other AvgMax64
func (am *AvgMax64) UpdateFrom(oth *AvgMax64) {
am.Sum += oth.Sum
am.N += oth.N
if oth.Max > am.Max {
am.Max = oth.Max
am.MaxIndex = oth.MaxIndex
}
}
// CopyFrom copies from other AvgMax64
func (am *AvgMax64) CopyFrom(oth *AvgMax64) {
am.Avg = oth.Avg
am.Max = oth.Max
am.MaxIndex = oth.MaxIndex
am.Sum = oth.Sum
am.N = oth.N
}