BoosterSeat
0.1
A C++ library that includes common utilities that are used in other projects.
|
Moving average calculator. More...
#include <rolling_average.hpp>
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... | |
T | 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_ = {} |
T | sum_ = 0 |
size_t | window_size_ |
Moving average calculator.
This class calculates the moving average over a rolling window.
|
inline |
Construct a RollingAverage object.
window_size | - The size of the rolling window |
|
default |
Default destructor.
|
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.
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().
|
inline |
This method returns the rolling average.
References bst::RollingAverage< T >::queue_, and bst::RollingAverage< T >::sum_.
Referenced by TEST().
|
inline |
Set the window size. If the new window size is smaller than the current queue size, the queue will be truncated.
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().
|
inline |
Remove all elements from the queue and reset the sum to zero.
References bst::RollingAverage< T >::queue_, and bst::RollingAverage< T >::sum_.
|
inline |
Get the number of elements in the queue. This number may be less than the window size.
References bst::RollingAverage< T >::queue_.
Referenced by TEST().
|
inline |
Get the window size.
References bst::RollingAverage< T >::window_size_.
|
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().
|
private |
|
private |
|
private |