ed64e8917e8b47e06728b0cae6500473f9fb365d
[senf.git] / senf / Utils / StatisticAccumulator.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27 /** \file
28     \brief StatisticAccumulator public header */
29
30 #ifndef HH_SENF_Utils_StatisticAccumulator_
31 #define HH_SENF_Utils_StatisticAccumulator_ 1
32
33 // Custom includes
34 #include <math.h>
35 #include <boost/cstdint.hpp>
36 #include <senf/Utils/Format.hh>
37 ///////////////////////////////hh.p////////////////////////////////////////
38 namespace senf {
39
40      struct StatisticsData
41      {
42          StatisticsData(float min_, float avg_, float max_, float stddev_, boost::uint32_t count_)
43              : min(min_), avg(avg_), max(max_), stddev(stddev_), count(count_) {};
44          StatisticsData(StatisticsData const & other)
45              : min(other.min), avg(other.avg), max(other.max), stddev(other.stddev), count(other.count) {};
46          StatisticsData()
47              : min(0.0), avg(0.0), max(0.0), stddev(0.0), count(0) {};
48
49          float min;
50          float avg;
51          float max;
52          float stddev;
53          boost::uint32_t count;
54      };
55
56      std::ostream & operator<<(std::ostream & os, StatisticsData const & _data);
57
58      /** \brief Accumulate measurement values
59
60          The accumulator mainly do the prelimenary work for the senf::Statistic class.
61          It accumulates certain values with in an interval to be used by senf::Statistics
62
63          \li the senf::Statistics class
64          \li statistics sources
65
66          This class provides the average, standard deviation, min, max values over one
67          interval, means until clear() is called. It rather directly calculates the results
68          then collection all single values provided by calling accumulate().
69          \see senf::Statistics to process accumulated values
70          \ingroup senf_statistics
71       */
72     template <class T>
73     class StatisticAccumulator
74     {
75     public:
76         StatisticAccumulator();
77 //        virtual ~StatisticAccumulator();
78
79         void clear();
80         ///< Reset accumulated values.
81         /**< This member reset all values. */
82         void accumulate(T value);
83         ///< Gather value to be accumulated.
84         /**< This method accumulates a value.
85              \param[in] value to be accumulated value*/
86         void setLastAvg(T value);
87         ///< If no real data was collected, this method specifies the min/avg/max value to be returned.
88         /**< This method specifies the the min/avg/max value to be returned if no real data was colelcted
89              \param[in] value to be returned*/
90
91     public:
92         T     min() const;
93         ///< Returns current minimal value.
94         /**< This method returns the minimal value of the current accumulation.*/
95         T     max() const;
96         ///< Returns current maximal value.
97         /**< This method returns the maximal value of the current accumulation.*/
98         float avg() const;
99         ///< Returns average value.
100         /**< This method returns the average value of the current accumulation.*/
101         float last_avg() const;
102         ///< Returns former average value.
103         /**< This method returns the average value of the former accumulation period.*/
104         float stddev() const;
105         ///< Returns standard deviation value.
106         /**< This method returns the standard deviation value of the current accumulation.*/
107         boost::uint32_t count() const;
108         ///< Returns count of accumulated values.
109         /**< This method returns count of accumulated values of the current accumulation.*/
110         void data( StatisticsData & data_) const;
111         ///< Returns the accumulated data as a tuple
112         /**< This method returns the accumulated information as a tuple.*/  
113
114     private:
115         T sum_squared_;
116         T sum_;
117         T min_;
118         T max_;
119         float last_avg_;
120         boost::uint32_t count_;
121
122
123     };
124
125
126     typedef StatisticAccumulator<int> StatisticAccumulatorInt;
127     typedef StatisticAccumulator<float> StatisticAccumulatorFloat;
128
129 }
130 ///////////////////////////////hh.e////////////////////////////////////////
131 //#include "StatisticAccumulator.cci"
132 #include "StatisticAccumulator.ct"
133 #include "StatisticAccumulator.cti"
134 #endif
135