Add simple stat accumulator class

This commit is contained in:
Danny Robson 2012-05-14 16:11:09 +10:00
parent 83a30c9d94
commit faa44b24ef
3 changed files with 119 additions and 0 deletions

View File

@ -82,6 +82,8 @@ UTIL_FILES = \
signal.cpp \ signal.cpp \
signal.hpp \ signal.hpp \
si.hpp \ si.hpp \
stats.cpp \
stats.hpp \
stream.cpp \ stream.cpp \
stream.hpp \ stream.hpp \
string.cpp \ string.cpp \

69
stats.cpp Normal file
View File

@ -0,0 +1,69 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/
#include "stats.hpp"
#include <cmath>
#include <limits>
template <typename T>
util::stats::accumulator<T>::accumulator ():
min (std::numeric_limits<T>::max ()),
max (std::numeric_limits<T>::min ())
{ ; }
template <typename T>
void
util::stats::accumulator<T>::add (T val) {
min = std::min (val, min);
max = std::max (val, max);
}
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);
}
template <typename T>
T
util::stats::accumulator<T>::range (void) const
{ return max - min; }
template struct util::stats::accumulator<double>;
template struct util::stats::accumulator<float>;
template <typename T>
std::ostream&
util::stats::operator<< (std::ostream &os, const accumulator<T> &rhs) {
os << "(min: " << rhs.min << ", max: " << rhs.max << ")";
return os;
}
template std::ostream& util::stats::operator<< (std::ostream&, const accumulator<double> &);
template std::ostream& util::stats::operator<< (std::ostream&, const accumulator<float > &);

48
stats.hpp Normal file
View File

@ -0,0 +1,48 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_STATS_HPP
#define __UTIL_STATS_HPP
#include <iostream>
namespace util {
namespace stats {
// Undefined until at least one value has been added.
template <typename T>
struct accumulator {
accumulator ();
void add (T);
void add (const accumulator<T> &);
T min;
T max;
T range (void) const;
};
template <typename T>
std::ostream& operator<< (std::ostream&, const accumulator<T>&);
}
}
#endif