BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
bst::RollingAverage< T > Class Template Reference

Moving average calculator. More...

#include <rolling_average.hpp>

Collaboration diagram for bst::RollingAverage< T >:

Public Member Functions

 RollingAverage (size_t window_size)
 Construct a RollingAverage object. More...
 
 ~RollingAverage ()=default
 Default destructor. More...
 
void addValue (T value)
 Add a value to the rolling average. If the queue size exceeds the window size, the oldest value will be removed from the queue and the sum will be decremented by that value. More...
 
void clear ()
 Remove all elements from the queue and reset the sum to zero. More...
 
getAverage () const
 This method returns the rolling average. More...
 
size_t getNumElements () const
 Get the number of elements in the queue. This number may be less than the window size. More...
 
size_t getWindowSize () const
 Get the window size. More...
 
void removeOldest ()
 Remove the oldest element from the queue and update the sum. Safe to call if the queue is empty. More...
 
void setWindowSize (size_t new_window_size)
 Set the window size. If the new window size is smaller than the current queue size, the queue will be truncated. More...
 

Private Attributes

std::queue< T > queue_ = {}
 
sum_ = 0
 
size_t window_size_
 

Detailed Description

template<typename T = double>
class bst::RollingAverage< T >

Moving average calculator.

This class calculates the moving average over a rolling window.

Constructor & Destructor Documentation

◆ RollingAverage()

template<typename T = double>
bst::RollingAverage< T >::RollingAverage ( size_t  window_size)
inline

Construct a RollingAverage object.

Parameters
window_size- The size of the rolling window

◆ ~RollingAverage()

template<typename T = double>
bst::RollingAverage< T >::~RollingAverage ( )
default

Default destructor.

Member Function Documentation

◆ addValue()

template<typename T = double>
void bst::RollingAverage< T >::addValue ( value)
inline

Add a value to the rolling average. If the queue size exceeds the window size, the oldest value will be removed from the queue and the sum will be decremented by that value.

Parameters
value- The value to add to the rolling average

References bst::RollingAverage< T >::queue_, bst::RollingAverage< T >::sum_, and bst::RollingAverage< T >::window_size_.

Referenced by TEST().

Here is the caller graph for this function:

◆ getAverage()

template<typename T = double>
T bst::RollingAverage< T >::getAverage ( ) const
inline

This method returns the rolling average.

Returns
T - the rolling average

References bst::RollingAverage< T >::queue_, and bst::RollingAverage< T >::sum_.

Referenced by TEST().

Here is the caller graph for this function:

◆ setWindowSize()

template<typename T = double>
void bst::RollingAverage< T >::setWindowSize ( size_t  new_window_size)
inline

Set the window size. If the new window size is smaller than the current queue size, the queue will be truncated.

Parameters
new_window_size- The new window size

References bst::RollingAverage< T >::queue_, bst::RollingAverage< T >::sum_, and bst::RollingAverage< T >::window_size_.

Referenced by TEST().

Here is the caller graph for this function:

◆ clear()

template<typename T = double>
void bst::RollingAverage< T >::clear ( )
inline

Remove all elements from the queue and reset the sum to zero.

References bst::RollingAverage< T >::queue_, and bst::RollingAverage< T >::sum_.

◆ getNumElements()

template<typename T = double>
size_t bst::RollingAverage< T >::getNumElements ( ) const
inline

Get the number of elements in the queue. This number may be less than the window size.

Returns
size_t - The number of elements in the queue

References bst::RollingAverage< T >::queue_.

Referenced by TEST().

Here is the caller graph for this function:

◆ getWindowSize()

template<typename T = double>
size_t bst::RollingAverage< T >::getWindowSize ( ) const
inline

Get the window size.

Returns
size_t - The window size

References bst::RollingAverage< T >::window_size_.

◆ removeOldest()

template<typename T = double>
void bst::RollingAverage< T >::removeOldest ( )
inline

Remove the oldest element from the queue and update the sum. Safe to call if the queue is empty.

References bst::RollingAverage< T >::queue_, and bst::RollingAverage< T >::sum_.

Referenced by TEST().

Here is the caller graph for this function:

Member Data Documentation

◆ queue_

◆ window_size_

◆ sum_