libcruft-util/stats.cpp

81 lines
1.8 KiB
C++
Raw Normal View History

2012-05-14 16:11:09 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2012-05-14 16:11:09 +10:00
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/
#include "stats.hpp"
#include <limits>
2018-03-22 16:10:06 +11:00
#include <ostream>
2012-05-14 16:11:09 +10:00
template <typename T>
cruft::stats::accumulator<T>::accumulator () {
reset ();
}
template <typename T>
void
cruft::stats::accumulator<T>::reset (void) {
count = 0;
min = std::numeric_limits<T>::max ();
max = std::numeric_limits<T>::min ();
sum = 0;
}
2012-05-14 16:11:09 +10:00
template <typename T>
void
cruft::stats::accumulator<T>::add (T val) {
min = std::min (val, min);
max = std::max (val, max);
sum += val;
++count;
2012-05-14 16:11:09 +10:00
}
template <typename T>
void
cruft::stats::accumulator<T>::add (const accumulator<T> &rhs) {
min = std::min (rhs.min, min);
max = std::max (rhs.max, max);
sum += rhs.sum;
count += rhs.count;
2012-05-14 16:11:09 +10:00
}
template <typename T>
T
cruft::stats::accumulator<T>::range (void) const
2012-05-14 16:11:09 +10:00
{ return max - min; }
template <typename T>
T
cruft::stats::accumulator<T>::mean (void) const
{ return sum / count; }
template struct cruft::stats::accumulator<uint64_t>;
template struct cruft::stats::accumulator<double>;
template struct cruft::stats::accumulator<float>;
2012-05-14 16:11:09 +10:00
template <typename T>
std::ostream&
cruft::stats::operator<< (std::ostream &os, const accumulator<T> &rhs) {
2012-05-14 16:11:09 +10:00
os << "(min: " << rhs.min << ", max: " << rhs.max << ")";
return os;
}
template std::ostream& cruft::stats::operator<< (std::ostream&, const accumulator<uint64_t> &);
template std::ostream& cruft::stats::operator<< (std::ostream&, const accumulator<double> &);
template std::ostream& cruft::stats::operator<< (std::ostream&, const accumulator<float > &);
2012-05-14 16:11:09 +10:00