Add count, sum, and mean for stats accumulator

This commit is contained in:
Danny Robson 2012-05-15 16:04:06 +10:00
parent ee6fb59e99
commit e8844943a7
2 changed files with 23 additions and 6 deletions

View File

@ -25,24 +25,31 @@
template <typename T>
util::stats::accumulator<T>::accumulator ():
min (std::numeric_limits<T>::max ()),
max (std::numeric_limits<T>::min ())
count (0),
min (std::numeric_limits<T>::max ()),
max (std::numeric_limits<T>::min ()),
sum (0)
{ ; }
template <typename T>
void
util::stats::accumulator<T>::add (T val) {
min = std::min (val, min);
max = std::max (val, max);
min = std::min (val, min);
max = std::max (val, max);
sum += val;
++count;
}
template <typename T>
void
util::stats::accumulator<T>::add (const accumulator<T> &rhs) {
min = std::min (rhs.min, min);
max = std::max (rhs.max, max);
min = std::min (rhs.min, min);
max = std::max (rhs.max, max);
sum += rhs.sum;
count += rhs.count;
}
@ -52,6 +59,12 @@ util::stats::accumulator<T>::range (void) const
{ return max - min; }
template <typename T>
T
util::stats::accumulator<T>::mean (void) const
{ return sum / count; }
template struct util::stats::accumulator<double>;
template struct util::stats::accumulator<float>;

View File

@ -34,10 +34,14 @@ namespace util {
void add (T);
void add (const accumulator<T> &);
size_t count;
T min;
T max;
T sum;
T range (void) const;
T mean (void) const;
};
template <typename T>