20 #ifndef ANTKEEPER_MATH_MOVING_AVERAGE_HPP
21 #define ANTKEEPER_MATH_MOVING_AVERAGE_HPP
63 if (m_sample_index < m_samples.size())
65 m_samples[m_sample_index] = value;
67 m_average = m_sum /
static_cast<sample_type>(m_sample_index);
71 sample_type& sample = m_samples[m_sample_index % m_samples.size()];
75 m_average = m_sum /
static_cast<sample_type>(m_samples.size());
108 if (
size > m_samples.size())
110 m_samples.resize(
size);
112 m_sample_index =
size;
118 return m_samples.data();
134 [[nodiscard]]
inline std::size_t
size() const noexcept
136 return std::min<std::size_t>(m_sample_index, m_samples.size());
140 [[nodiscard]]
inline constexpr std::size_t
capacity() const noexcept
142 return m_samples.size();
146 [[nodiscard]]
inline constexpr
bool empty() const noexcept
148 return !m_sample_index;
152 [[nodiscard]]
inline constexpr
bool full() const noexcept
154 return m_sample_index >= m_samples.size();
158 std::vector<sample_type> m_samples;
159 std::size_t m_sample_index{0};
Calculates a moving average.
std::size_t size() const noexcept
Returns the current number of samples.
sample_type sum() const noexcept
Returns the sum of all current samples.
void reset() noexcept
Resets the moving average.
constexpr sample_type * data() const noexcept
Returns a pointer to the sample data.
T sample_type
Type of value to average.
constexpr std::size_t capacity() const noexcept
Returns the maximum number of samples.
moving_average(std::size_t capacity)
Constructs a moving average.
void reserve(std::size_t capacity)
Changes the sample capacity of the moving average.
sample_type average() const noexcept
Returns the current moving average value.
constexpr bool empty() const noexcept
Return true if there are currently no samples in the average, false otherwise.
void resize(std::size_t size)
Changes the current number of samples of the moving average.
moving_average() noexcept=default
Constructs a moving average.
constexpr bool full() const noexcept
Return true if the number of samples in the average has reached its capacity, false otherwise.
Mathematical functions and data types.