SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
Utils::Averaged< T > Class Template Reference

Averaged template class provides an average value from a fixed size array. More...

#include <Averaged.h>

Public Member Functions

 Averaged ()
 
 Averaged (int numValues, T initValue=0)
 
void init (int numValues, T initValue)
 Initializes the average value array to a given value. More...
 
void set (T value)
 Sets the current value in the value array and builds the average. More...
 
average ()
 
size_t size ()
 

Private Attributes

float _oneOverNumValues {}
 multiplier instead of divider More...
 
vector< T > _values
 value array More...
 
int _currentValueIndex {}
 current value index within _values More...
 
_sum
 sum of all values More...
 
_average
 average value More...
 

Detailed Description

template<class T>
class Utils::Averaged< T >

Averaged template class provides an average value from a fixed size array.

The Average template class provides a simple moving average value continuously averaged from a fixed size vector. The template class can be used for any template type T that provides the following operators: =, -, +, T* float

Definition at line 31 of file Averaged.h.

Constructor & Destructor Documentation

◆ Averaged() [1/2]

template<class T >
Utils::Averaged< T >::Averaged ( )
inline

Definition at line 34 of file Averaged.h.

34 : _currentValueIndex(0), _sum(0), _average(0) {}
T _sum
sum of all values
Definition: Averaged.h:81
T _average
average value
Definition: Averaged.h:82
int _currentValueIndex
current value index within _values
Definition: Averaged.h:80

◆ Averaged() [2/2]

template<class T >
Utils::Averaged< T >::Averaged ( int  numValues,
initValue = 0 
)
inline

Definition at line 35 of file Averaged.h.

36  {
37  init(numValues, initValue);
38  }
void init(int numValues, T initValue)
Initializes the average value array to a given value.
Definition: Averaged.h:41

Member Function Documentation

◆ average()

template<class T >
T Utils::Averaged< T >::average ( )
inline

Definition at line 74 of file Averaged.h.

74 { return _average; }

◆ init()

template<class T >
void Utils::Averaged< T >::init ( int  numValues,
initValue 
)
inline

Initializes the average value array to a given value.

Definition at line 41 of file Averaged.h.

42  {
43  assert(numValues > 0 && "Num. of values must be greater than zero");
44  _values.clear();
45  _values.resize(numValues, initValue);
46  _oneOverNumValues = 1.0f / (float)_values.size();
47  _sum = initValue * numValues;
48  _average = initValue;
50  }
vector< T > _values
value array
Definition: Averaged.h:79
float _oneOverNumValues
multiplier instead of divider
Definition: Averaged.h:78

◆ set()

template<class T >
void Utils::Averaged< T >::set ( value)
inline

Sets the current value in the value array and builds the average.

Definition at line 53 of file Averaged.h.

54  {
55  assert(_values.size() > 0 && "_value vector not initialized");
56 
57  // Shortcut for no averaging
58  if (_values.size() == 1)
59  _sum = _average = value;
60  else
61  {
62  if (_currentValueIndex == _values.size())
64 
65  // Correct the sum continuously
67  _values[_currentValueIndex] = value;
69  _average = _sum * _oneOverNumValues; // avoid division
71  }
72  }

◆ size()

template<class T >
size_t Utils::Averaged< T >::size ( )
inline

Definition at line 75 of file Averaged.h.

75 { return _values.size(); }

Member Data Documentation

◆ _average

template<class T >
T Utils::Averaged< T >::_average
private

average value

Definition at line 82 of file Averaged.h.

◆ _currentValueIndex

template<class T >
int Utils::Averaged< T >::_currentValueIndex {}
private

current value index within _values

Definition at line 80 of file Averaged.h.

◆ _oneOverNumValues

template<class T >
float Utils::Averaged< T >::_oneOverNumValues {}
private

multiplier instead of divider

Definition at line 78 of file Averaged.h.

◆ _sum

template<class T >
T Utils::Averaged< T >::_sum
private

sum of all values

Definition at line 81 of file Averaged.h.

◆ _values

template<class T >
vector<T> Utils::Averaged< T >::_values
private

value array

Definition at line 79 of file Averaged.h.


The documentation for this class was generated from the following file: