9e5d106de941ac9780e049a0794fb24f8487c002
[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>::accumulate( T value)
65 {
66     if (count_ == 0) {
67         min_ = max_ = sum_ = value;
68         sum_squared_ = value * value;
69         count_++;
70         return;
71     }
72     sum_ += value;
73     sum_squared_ += value * value;
74     count_++;
75     if (value < min_)
76         min_ = value;
77     else if (value > max_)
78         max_ = value;
79 }
80
81 template <class T>
82 prefix_ void senf::StatisticAccumulator<T>::clear()
83 {
84     if( count_ > 0){
85       last_avg_ = avg();
86       count_ = 0;
87       sum_squared_ = 0;
88       sum_ = min_ = max_ = 0;
89     }
90 }
91
92 template <class T>
93 prefix_ void senf::StatisticAccumulator<T>::data( StatisticsData &data_) const
94 {
95     if( count_ == 0){
96         data_.min = data_.avg = data_.max = last_avg_;
97         data_.stddev = 0.0;
98         data_.count = 0;
99     }
100     else{
101       data_.min = (float) min_;
102       data_.avg = avg();        
103       data_.max = (float) max_;
104       data_.stddev = stddev();
105       data_.count = count_;
106   }
107 }
108
109
110 ///////////////////////////////ct.e////////////////////////////////////////
111 #undef prefix_