178fbb8b73932cdb9e0afd11774250b6f7f1b6e5
[senf.git] / senf / Utils / StatisticAccumulator.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief StatisticAccumulator public header */
25
26 #ifndef HH_SENF_Utils_StatisticAccumulator_
27 #define HH_SENF_Utils_StatisticAccumulator_ 1
28
29 // Custom includes
30 #include <math.h>
31 #include <boost/cstdint.hpp>
32 ///////////////////////////////hh.p////////////////////////////////////////
33 namespace senf {
34
35      struct StatisticsData
36      {
37          StatisticsData( float min_, float avg_, float max_, float stddev_, boost::uint32_t count_)
38              : min(min_), avg(avg_), max(max_), stddev(stddev_), count(count_){
39          };
40          StatisticsData( StatisticsData const & other)
41              : min(other.min), avg(other.avg), max(other.max), stddev(other.stddev), count(other.count){
42          };
43          StatisticsData()
44              : min(0.0), avg(0.0), max(0.0), stddev(0.0), count(0){
45          };
46
47          float min;
48          float avg;
49          float max;
50          float stddev;
51          boost::uint32_t count;
52      };
53
54      /** \brief Accumulate measurement values
55
56          The accumulator mainly do the prelimenary work for the senf::Statistic class.
57          It accumulates certain values with in an interval to be used by senf::Statistics
58
59          \li the senf::Statistics class
60          \li statistics sources
61
62          This class provides the average, standard deviation, min, max values over one
63          interval, means until clear() is called. It rather directly calculates the results
64          then collection all single values provided by calling accumulate().
65          \see senf::Statistics to process accumulated values
66          \ingroup senf_statistics
67       */
68     template <class T>
69     class StatisticAccumulator
70     {
71     public:
72         StatisticAccumulator();
73 //        virtual ~StatisticAccumulator();
74
75         void clear();
76         ///< Reset accumulated values.
77         /**< This member reset all values. */
78         void accumulate(T value);
79         ///< Gather value to be accumulated.
80         /**< This method accumulates a value.
81              \param[in] value to be accumulated value*/
82         void setLastAvg(T value);
83         ///< If no real data was collected, this method specifies the min/avg/max value to be returned.
84         /**< This method specifies the the min/avg/max value to be returned if no real data was colelcted
85              \param[in] value to be returned*/
86
87     public:
88         T     min() const;
89         ///< Returns current minimal value.
90         /**< This method returns the minimal value of the current accumulation.*/
91         T     max() const;
92         ///< Returns current maximal value.
93         /**< This method returns the maximal value of the current accumulation.*/
94         float avg() const;
95         ///< Returns average value.
96         /**< This method returns the average value of the current accumulation.*/
97         float last_avg() const;
98         ///< Returns former average value.
99         /**< This method returns the average value of the former accumulation period.*/
100         float stddev() const;
101         ///< Returns standard deviation value.
102         /**< This method returns the standard deviation value of the current accumulation.*/
103         boost::uint32_t count() const;
104         ///< Returns count of accumulated values.
105         /**< This method returns count of accumulated values of the current accumulation.*/
106         void data( StatisticsData & data_) const;
107         ///< Returns the accumulated data as a tuple
108         /**< This method returns the accumulated information as a tuple.*/  
109
110     private:
111         T sum_squared_;
112         T sum_;
113         T min_;
114         T max_;
115         float last_avg_;
116         boost::uint32_t count_;
117
118
119     };
120
121
122     typedef StatisticAccumulator<int> StatisticAccumulatorInt;
123     typedef StatisticAccumulator<float> StatisticAccumulatorFloat;
124
125 }
126 ///////////////////////////////hh.e////////////////////////////////////////
127 //#include "StatisticAccumulator.cci"
128 #include "StatisticAccumulator.ct"
129 #include "StatisticAccumulator.cti"
130 #endif
131