c0b83ae21c7e7541a36768f09ab805d65149b438
[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( T defaultvalue)
36     : defaultvalue_(defaultvalue),
37       sum_squared_(defaultvalue*defaultvalue),
38       sum_(defaultvalue),
39       min_(defaultvalue),
40       max_(defaultvalue),
41       last_avg_(float(defaultvalue)),
42       count_(0)
43 {
44 }
45
46 //template <class T>
47 //prefix_ senf::StatisticAccumulator<T>::~StatisticAccumulator()
48 //{ }
49
50 template <class T>
51 prefix_ float senf::StatisticAccumulator<T>::stddev()
52     const
53 {
54     if (count_ == 0) {
55         return NAN;
56     }
57     else if (count_ == 1) {
58         return 0.0;
59     }
60     float _avg (avg());
61     return sqrt( ( float(sum_squared_) / float( count_) ) - (_avg * _avg) );
62 }
63
64 template <class T>
65 prefix_ void senf::StatisticAccumulator<T>::accumulate( T value)
66 {
67     if (count_ == 0) {
68         min_ = max_ = sum_ = value;
69         sum_squared_ = value * value;
70         count_++;
71         return;
72     }
73     sum_ += value;
74     sum_squared_ += value * value;
75     count_++;
76     if (value < min_)
77         min_ = value;
78     else if (value > max_)
79         max_ = value;
80 }
81
82 template <class T>
83 prefix_ void senf::StatisticAccumulator<T>::clear()
84 {
85     last_avg_ = avg();
86     count_ = 0;
87     sum_squared_ = defaultvalue_*defaultvalue_;
88     sum_ = min_ = max_ = defaultvalue_;
89 }
90
91
92 ///////////////////////////////ct.e////////////////////////////////////////
93 #undef prefix_