added a setLastAvg() method to report a 'substitute' min/avg/max if no real data...
[senf.git] / senf / Utils / StatisticAccumulator.ct
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 non-inline template implementation  */
25
26 // Custom includes
27
28 #define prefix_
29 ///////////////////////////////ct.p////////////////////////////////////////
30
31 ///////////////////////////////////////////////////////////////////////////
32 // senf::StatisticAccumulator<T>
33
34 template <class T>
35 prefix_ senf::StatisticAccumulator<T>::StatisticAccumulator( )
36     : sum_squared_(0),
37       sum_(0),
38       min_(0),
39       max_(0),
40       last_avg_(0),
41       count_(0)
42 {
43 }
44
45 //template <class T>
46 //prefix_ senf::StatisticAccumulator<T>::~StatisticAccumulator()
47 //{ }
48
49 template <class T>
50 prefix_ float senf::StatisticAccumulator<T>::stddev()
51     const
52 {
53     if (count_ == 0) {
54         return NAN;
55     }
56     else if (count_ == 1) {
57         return 0.0;
58     }
59     float _avg (avg());
60     return sqrt( ( float(sum_squared_) / float( count_) ) - (_avg * _avg) );
61 }
62
63 template <class T>
64 prefix_ void senf::StatisticAccumulator<T>::setLastAvg( T value)
65 {
66     last_avg_ = value;        
67 }
68
69 template <class T>
70 prefix_ void senf::StatisticAccumulator<T>::accumulate( T value)
71 {
72     if (count_ == 0) {
73         min_ = max_ = sum_ = value;
74         sum_squared_ = value * value;
75         count_++;
76         return;
77     }
78     sum_ += value;
79     sum_squared_ += value * value;
80     count_++;
81     if (value < min_)
82         min_ = value;
83     else if (value > max_)
84         max_ = value;
85 }
86
87 template <class T>
88 prefix_ void senf::StatisticAccumulator<T>::clear()
89 {
90     if( count_ > 0){
91       last_avg_ = avg();
92       count_ = 0;
93       sum_squared_ = 0;
94       sum_ = min_ = max_ = 0;
95     }
96 }
97
98 template <class T>
99 prefix_ void senf::StatisticAccumulator<T>::data( StatisticsData &data_) const
100 {
101     if( count_ == 0){
102         data_.min = data_.avg = data_.max = last_avg_;
103         data_.stddev = 0.0;
104         data_.count = 0;
105     }
106     else{
107       data_.min = (float) min_;
108       data_.avg = avg();        
109       data_.max = (float) max_;
110       data_.stddev = stddev();
111       data_.count = count_;
112   }
113 }
114
115
116 ///////////////////////////////ct.e////////////////////////////////////////
117 #undef prefix_