StatisticAccumulator.ct
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief StatisticAccumulator non-inline template implementation */
16 
17 // Custom includes
18 #include <algorithm>
19 #include <limits>
20 
21 #define prefix_
22 ///////////////////////////////ct.p////////////////////////////////////////
23 
24 ///////////////////////////////////////////////////////////////////////////
25 // senf::StatisticAccumulator<T>
26 
27 template <class T>
28 prefix_ senf::StatisticAccumulator<T>::StatisticAccumulator(T const & value)
29  : sum_(0), sum_squared_(0), min_( std::numeric_limits<T>::max()), max_( std::numeric_limits<T>::lowest()), cnt_(0)
30 {
31  accumulate(value);
32 }
33 
34 template <class T>
35 prefix_ senf::StatisticAccumulator<T>::StatisticAccumulator()
36  : sum_(0), sum_squared_(0), min_( std::numeric_limits<T>::max()), max_( std::numeric_limits<T>::lowest()), cnt_(0)
37 {}
38 
39 template <class T>
40 prefix_ senf::StatisticAccumulator<T>::StatisticAccumulator(T const & sum, T const & sumSquared, T const & min, T const & max, unsigned count)
41  : sum_(sum), sum_squared_(sumSquared), min_(min), max_(max), cnt_(count)
42 {}
43 
44 template <class T>
45 prefix_ senf::StatisticAccumulator<T>::StatisticAccumulator(senf::StatisticsData const & data)
46  : sum_(T(data.avg) * data.cnt), sum_squared_(T(data.avg * data.avg) * data.cnt), min_(T(data.min)), max_(T(data.max)), cnt_(data.cnt)
47 {}
48 
49 
50 template <class T>
51 prefix_ float senf::StatisticAccumulator<T>::stddev()
52  const
53 {
54  if (cnt_ == 0)
55  return NAN;
56  if (cnt_ == 1)
57  return 0.0f;
58  float _avg (avg());
59  // std::max avoid rounding errors, that might cause a sqrt(<0)
60  return sqrtf(std::max(0.0f, (float(sum_squared_) / float( cnt_) - (_avg * _avg) )));
61 }
62 
63 template <class T>
64 prefix_ void senf::StatisticAccumulator<T>::clear()
65 {
66  cnt_ = 0;
67  sum_squared_ = 0;
68  sum_ = 0;
69  min_ = std::numeric_limits<T>::max();
70  max_ = std::numeric_limits<T>::lowest();
71 }
72 
73 template <class T>
74 prefix_ void senf::StatisticAccumulator<T>::data(StatisticsData & data_)
75  const
76 {
77  if (cnt_ == 0) {
78  data_.min = data_.avg = data_.max = NAN;
79  data_.stddev = NAN;
80  data_.cnt = 0;
81  } else {
82  data_.min = (float) min_;
83  data_.avg = avg();
84  data_.max = (float) max_;
85  data_.stddev = stddev();
86  data_.cnt = cnt_;
87  }
88 }
89 
90 template <class T>
91 prefix_ senf::StatisticsData senf::StatisticAccumulator<T>::data()
92  const
93 {
94  StatisticsData tmp;
95  data(tmp);
96  return tmp;
97 }
98 
99 template <class T>
100 prefix_ senf::StatisticAccumulator<T> senf::StatisticAccumulator<T>::operator+=(senf::StatisticAccumulator<T> const & other)
101 {
102  if (other.cnt_ > 0) {
103  if (cnt_ == 0) {
104  this->sum_ = other.sum_;
105  this->sum_squared_ = other.sum_squared_;
106  this->cnt_ = other.cnt_;
107  this->min_ = other.min_;
108  this->max_ = other.max_;
109  } else {
110  this->sum_ += other.sum_;
111  this->sum_squared_ += other.sum_squared_;
112  this->cnt_ += other.cnt_;
113  this->min_ = std::min(min_,other.min_);
114  this->max_ = std::max(max_,other.max_);
115  }
116  }
117  return *this;
118 }
119 
120 template <class T>
121 prefix_ senf::StatisticAccumulator<T> senf::StatisticAccumulator<T>::operator*=(const float & scale)
122 {
123  if (cnt_ > 0) {
124  this->sum_ *= scale;
125  this->sum_squared_ *= scale * scale;
126  this->min_ *= scale;
127  this->max_ *= scale;
128  }
129 
130  return *this;
131 }
132 
133 //
134 // EWMA
135 //
136 
137 template <class T>
138 prefix_ senf::StatisticsEWMA<T>::StatisticsEWMA(float alpha, T const & value)
139  : ewma_(value),
140  alpha_(alpha),
141  cnt_(0)
142 {
143 }
144 
145 template <class T>
146 prefix_ void senf::StatisticsEWMA<T>::clear(T const & value)
147 {
148  ewma_ = value;
149  cnt_ = 0;
150 }
151 
152 
153 ///////////////////////////////ct.e////////////////////////////////////////
154 #undef prefix_