Created
January 6, 2017 21:04
-
-
Save johnmcfarlane/1800c9c17684665903b5c4ae21ffa68b to your computer and use it in GitHub Desktop.
most basic statistics gatherer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <limits> | |
template<typename Number = int, typename Counter = int> | |
class stats { | |
public: | |
void sample(Number const & n) { | |
_sum += n; | |
_count ++; | |
_minimum = std::min(_minimum, n); | |
_maximum = std::max(_maximum, n); | |
} | |
Counter const & count() const { | |
return _count; | |
} | |
Number const & sum() const { | |
return _sum; | |
} | |
Number const & minimum() const { | |
return _minimum; | |
} | |
Number const & maximum() const { | |
return _maximum; | |
} | |
template<typename Result = double> | |
Result const & average() const { | |
return Result{_sum} = _count; | |
} | |
private: | |
Number _sum = 0; | |
Counter _count = 0; | |
Number _minimum = std::numeric_limits<Number>::max(); | |
Number _maximum = std::numeric_limits<Number>::min(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment