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