1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_util_Average_hpp
38 #define anna_core_util_Average_hpp
40 #include <anna/core/util/Millisecond.hpp>
41 #include <anna/core//util/DelayMeter.hpp>
42 #include <anna/core/functions.hpp>
47 Template para calcular la media de una sucesion de valores numericos.
49 Internamente trabaja con Microsegundos porque los métodos que los proporcionan son
52 template <class T> class Average {
56 \param name Nombre logico de esta instancia.
57 \param measure Unidad de medida. Solo se usa a efecto de salida de datos.
59 Average(const char* name, const char* measure = NULL) :
60 a_name(name), a_accumulator(0), a_n(0), a_measure(measure) {;}
63 Devuelve el indicador de validez de esta media.
64 \return \em true Si la media no tiene ningun valor o \em false en caso contrario.
66 bool isEmpty() const throw() { return (a_n == 0); }
69 Devuelve \em true si el valor de la media es cero, bien por no tener valores o
70 bien porque todos ellos eran cero.
71 \return \em true el valor de la media es cero o \em false en otro caso.
73 bool isZero() const throw() { return a_n == 0 || a_accumulator == 0; }
76 Devuelve el numero de elementos de contiene la media.
77 \return el numero de elementos de contiene la media.
79 int size() const throw() { return a_n; }
82 Devuelve el valor acumulado.
83 \return el valor acumulado.
85 T getAccumulator() const throw() { return a_accumulator; }
88 Devuelve la media de la sucesion de valores numericos asociados a esta.
89 \return La media de la sucesion de valores numericos asociados a esta.
90 \warning Antes de invocar a este operador hay que verificar que #isEmpty devuelve \em false.
92 T value() const throw() { return (isEmpty() == true) ? T(0) : (a_accumulator / a_n); }
95 Inicializa el valor de la media.
97 void clear() throw() { a_accumulator = 0; a_n = 0; }
100 * Establece manualmente el valor de la estadística.
101 * \param value Valor que tomará el acumulador de este instancia.
102 * \param _n valor que tomará el conteador de esta instancia.
104 void setValue(const T& value, const unsigned int _n) throw() {
105 a_accumulator = value;
110 Devuelve la media de la sucesion de valores numericos asociados a esta.
111 \return La media de la sucesion de valores numericos asociados a esta.
112 \warning Antes de invocar a este operador hay que verificar que #isEmpty devuelve \em false.
114 operator T() const throw() { return value(); }
117 Inicializa el valor de esta media.
118 \param value Valor con el que iniciar la media.
119 \return La referencia a esta instancia.
121 Average<T>& operator = (const T value)
123 a_accumulator = value;
130 \param other Objeto del que obtener los valores.
131 \return La referencia a esta instancia.
133 Average<T>& operator = (const Average<T>& other)
135 a_accumulator = other.a_accumulator;
141 Incrementa la media con el valor recibido.
142 \param v Valor con el que incrementar la media.
143 \return La referencia a esta instancia.
145 Average<T>& operator += (const T& v)
147 const T backup(a_accumulator);
149 if((a_accumulator += v) < backup) {
159 Decrementa la media con el valor recibido.
160 \param v Valor con el que incrementar la media.
161 \return La referencia a esta instancia.
163 Average<T>& operator -= (const T v)
165 if(a_accumulator > v && a_n > 1) {
177 Devuelve una cadena con la informacion referente a esta clase.
178 \return Una cadena con la informacion referente a esta clase.
180 std::string asString() const
182 std::string msg(a_name);
183 msg += " { Accumulate: ";
184 msg += functions::asString(a_accumulator);
186 if(a_measure != NULL)
187 msg += " " + std::string(a_measure);
189 msg += functions::asString(" | N: %u | avg: ", a_n);
190 msg += functions::asString(value());
192 if(a_measure != NULL)
193 msg += " " + std::string(a_measure);
200 const char* a_measure;