g++/final: don't add extra code to check for buffer overflows (-fno-stack-protector)
[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 //   Mathias Kretschmer <mtk@berlios.de>
27 //   Jens Moedeker <jmo@berlios.de>
28
29 /** \file
30     \brief StatisticAccumulator non-inline template implementation  */
31
32 // Custom includes
33
34 #define prefix_
35 ///////////////////////////////ct.p////////////////////////////////////////
36
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::StatisticAccumulator<T>
39
40 template <class T>
41 prefix_ senf::StatisticAccumulator<T>::StatisticAccumulator()
42     : sum_squared_(0), sum_(0), min_(0), max_(0), last_avg_(0), count_(0)
43 { }
44
45 template <class T>
46 prefix_ float senf::StatisticAccumulator<T>::stddev()
47     const
48 {
49     if (count_ == 0) {
50         return NAN;
51     }
52     else if (count_ == 1) {
53         return 0.0;
54     }
55     float _avg (avg());
56     return sqrt( ( float(sum_squared_) / float( count_) ) - (_avg * _avg) );
57 }
58
59 template <class T>
60 prefix_ void senf::StatisticAccumulator<T>::setLastAvg( T value)
61 {
62     last_avg_ = value;        
63 }
64
65 template <class T>
66 prefix_ void senf::StatisticAccumulator<T>::accumulate( T value)
67 {
68     if (count_ == 0) {
69         min_ = max_ = sum_ = value;
70         sum_squared_ = value * value;
71         count_++;
72         return;
73     }
74     sum_ += value;
75     sum_squared_ += value * value;
76     count_++;
77     if (value < min_)
78         min_ = value;
79     else if (value > max_)
80         max_ = value;
81 }
82
83 template <class T>
84 prefix_ void senf::StatisticAccumulator<T>::clear()
85 {
86     if (count_ > 0) {
87         last_avg_ = avg();
88         count_ = 0;
89         sum_squared_ = 0;
90         sum_ = min_ = max_ = 0;
91     }
92 }
93
94 template <class T>
95 prefix_ void senf::StatisticAccumulator<T>::data( StatisticsData &data_) const
96 {
97     if (count_ == 0) {
98         data_.min = data_.avg = data_.max = last_avg_;
99         data_.stddev = 0.0;
100         data_.count = 0;
101     } else {
102         data_.min = (float) min_;
103         data_.avg = avg();
104         data_.max = (float) max_;
105         data_.stddev = stddev();
106         data_.count = count_;
107     }
108 }
109
110
111 ///////////////////////////////ct.e////////////////////////////////////////
112 #undef prefix_