ANNA Suite  2020b
Multipurpose development suite for Telco applications
Average.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_util_Average_hpp
10 #define anna_core_util_Average_hpp
11 
14 #include <anna/core/functions.hpp>
15 
16 namespace anna {
17 
24 template <class T> class Average {
25 public:
31  Average(const char* name, const char* measure = NULL) :
32  a_name(name), a_accumulator(0), a_n(0), a_measure(measure) {;}
33 
38  bool isEmpty() const { return (a_n == 0); }
39 
45  bool isZero() const { return a_n == 0 || a_accumulator == 0; }
46 
51  int size() const { return a_n; }
52 
57  T getAccumulator() const { return a_accumulator; }
58 
64  T value() const { return (isEmpty() == true) ? T(0) : (a_accumulator / a_n); }
65 
69  void clear() { a_accumulator = 0; a_n = 0; }
70 
76  void setValue(const T& value, const unsigned int _n) {
77  a_accumulator = value;
78  a_n = _n;
79  }
80 
86  operator T() const { return value(); }
87 
94  {
95  a_accumulator = value;
96  a_n = 1;
97  return *this;
98  }
99 
106  {
107  a_accumulator = other.a_accumulator;
108  a_n = other.a_n;
109  return *this;
110  }
111 
118  {
119  const T backup(a_accumulator);
120 
121  if((a_accumulator += v) < backup) {
122  a_accumulator = v;
123  a_n = 1;
124  } else
125  a_n ++;
126 
127  return *this;
128  }
129 
136  {
137  if(a_accumulator > v && a_n > 1) {
138  a_accumulator -= v;
139  a_n --;
140  } else {
141  a_accumulator = 0;
142  a_n = 0;
143  }
144 
145  return *this;
146  }
147 
152  std::string asString() const
153  {
154  std::string msg(a_name);
155  msg += " { Accumulate: ";
156  msg += functions::asString(a_accumulator);
157 
158  if(a_measure != NULL)
159  msg += " " + std::string(a_measure);
160 
161  msg += functions::asString(" | N: %u | avg: ", a_n);
162  msg += functions::asString(value());
163 
164  if(a_measure != NULL)
165  msg += " " + std::string(a_measure);
166 
167  return msg += " }";
168  }
169 
170 private:
171  const char* a_name;
172  const char* a_measure;
173  T a_accumulator;
174  unsigned int a_n;
175 };
176 
177 }
178 
179 #endif
180 
181 
182 
T getAccumulator() const
Definition: Average.hpp:57
T value() const
Definition: Average.hpp:64
Average< T > & operator+=(const T &v)
Definition: Average.hpp:117
Definition: Average.hpp:24
static std::string asString(const int number)
Average(const char *name, const char *measure=NULL)
Definition: Average.hpp:31
int size() const
Definition: Average.hpp:51
bool isZero() const
Definition: Average.hpp:45
void setValue(const T &value, const unsigned int _n)
Definition: Average.hpp:76
Average< T > & operator-=(const T v)
Definition: Average.hpp:135
Definition: app.hpp:12
bool isEmpty() const
Definition: Average.hpp:38
std::string asString() const
Definition: Average.hpp:152
void clear()
Definition: Average.hpp:69
Average< T > & operator=(const T value)
Definition: Average.hpp:93