| Fling Engine 0.00.1 Fling Engine is a game engine written in Vulkan

MovingAverage.hpp
Go to the documentation of this file.
1 #pragma once
2
3 #include "FlingTypes.h"
4
5 namespace Fling
6 {
10 template<typename T, size_t t_Size>
11 class MovingAverage
12 {
13 public:
14
15 MovingAverage();
16 ~MovingAverage() = default;
17
19 void Push(T t_Element);
20
22 T GetAverage() const;
23
24 private:
26 size_t m_CurrentIndex = {};
27
29 size_t m_Count = {};
30
32 size_t m_MaxSize = {};
33
35 T m_Buffer [t_Size] = {};
36 };
37
38 template<typename T, size_t t_Size>
39 inline MovingAverage<T, t_Size>::MovingAverage()
40 : m_MaxSize(t_Size)
41 {
42 }
43
44 template<typename T, size_t t_Size>
45 inline void MovingAverage<T, t_Size>::Push(T t_Element)
46 {
47 ++m_Count;
48 const UINT32 index = m_CurrentIndex++;
49 m_Buffer[index & (m_MaxSize - 1u)] = t_Element;
50 }
51
52 template<typename T, size_t t_Size>
53 inline T MovingAverage<T, t_Size>::GetAverage() const
54 {
55 T Average = {};
56
57 size_t Range = (m_Count < m_MaxSize ? m_Count : m_MaxSize);
58 for (size_t i = 0; i < Range; ++i)
59 {
60 Average += m_Buffer[i];
61 }
62
63 return (Average / static_cast<T>(Range));
64 }
65 } // namespace Fling
Fling::MovingAverage::Push
void Push(T t_Element)
Push a value onto the buffer to be calculated.
Definition: MovingAverage.hpp:45
Fling::MovingAverage::~MovingAverage
~MovingAverage()=default
Fling::MovingAverage::m_CurrentIndex
size_t m_CurrentIndex
Index for keeping track of what part of the buffer we should be inserting into.
Definition: MovingAverage.hpp:26
Fling::MovingAverage::m_Count
size_t m_Count
Current count of things pushed to this average.
Definition: MovingAverage.hpp:29
Fling::MovingAverage::m_MaxSize
size_t m_MaxSize
Max sample size in this moving average.
Definition: MovingAverage.hpp:32
Fling::MovingAverage::GetAverage
T GetAverage() const
Get the average (Sum / num elements)
Definition: MovingAverage.hpp:53
UINT32
uint32_t UINT32
Definition: FlingTypes.h:13
Fling
Definition: Engine.h:29
Fling::MovingAverage::MovingAverage
MovingAverage()
Definition: MovingAverage.hpp:39
Fling::MovingAverage
A moving average can be used to calculate things like FPS.
Definition: MovingAverage.hpp:11
FlingTypes.h
Fling::MovingAverage::m_Buffer
T m_Buffer[t_Size]
Buffer of samples used to calculate the moving average.
Definition: MovingAverage.hpp:35