From e8844943a7d52e730c6df1c36462c6d44573f642 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 15 May 2012 16:04:06 +1000 Subject: [PATCH] Add count, sum, and mean for stats accumulator --- stats.cpp | 25 +++++++++++++++++++------ stats.hpp | 4 ++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/stats.cpp b/stats.cpp index c32b2dee..58e85d01 100644 --- a/stats.cpp +++ b/stats.cpp @@ -25,24 +25,31 @@ template util::stats::accumulator::accumulator (): - min (std::numeric_limits::max ()), - max (std::numeric_limits::min ()) + count (0), + min (std::numeric_limits::max ()), + max (std::numeric_limits::min ()), + sum (0) { ; } template void util::stats::accumulator::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 void util::stats::accumulator::add (const accumulator &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::range (void) const { return max - min; } +template +T +util::stats::accumulator::mean (void) const + { return sum / count; } + + template struct util::stats::accumulator; template struct util::stats::accumulator; diff --git a/stats.hpp b/stats.hpp index 57a93c0e..5323b351 100644 --- a/stats.hpp +++ b/stats.hpp @@ -34,10 +34,14 @@ namespace util { void add (T); void add (const accumulator &); + size_t count; + T min; T max; + T sum; T range (void) const; + T mean (void) const; }; template