-#include <algorithm>
-#include <cmath>
-#include <tr1/functional>
-#include <numeric>
-
-#include "statistics.h"
-
-statistics::statistics()
- : up2date(false)
-{
-}
-
-void statistics::reset()
-{
- values.clear();
- up2date = false;
-}
-
-void statistics::push(double val)
-{
- values.push_back(val);
- up2date = false;
-}
-
-size_t statistics::get_count()
-{
- return values.size();
-}
-
-double statistics::get_sum()
-{
- update();
- return sum;
-}
-
-double statistics::get_avg()
-{
- update();
- return avg;
-}
-
-double statistics::get_stddev()
-{
- update();
- return stddev;
-}
-
-void statistics::update()
-{
- if (up2date)
- return;
-
- if (values.empty()) {
- sum = 0.0;
- avg = stddev = 0.0 / 0.0;
- }
-
- using std::tr1::bind;
- using std::tr1::placeholders::_1;
-
- unsigned n = values.size();
- sum = std::accumulate(values.begin(), values.end(), 0.0);
- avg = sum / n;
-
- std::vector<double> diff(values);
- std::transform(diff.begin(), diff.end(), diff.begin(),
- bind(std::minus<double>(), _1, avg));
- double epsilon = std::accumulate(diff.begin(), diff.end(), 0.0);
- double square_sum = std::inner_product(diff.begin(), diff.end(),
- diff.begin(), 0.0);
- double variance = (square_sum - (epsilon * epsilon) / n) / n;
- stddev = sqrt(variance);
-
- up2date = true;
-}