iMSTK
Interactive Medical Simulation Toolkit
imstkAccumulationBuffer.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 #include <vector>
7 #include <imstkLogger.h>
8 
12 template<typename T>
14 {
15 public:
16  explicit AccumulationBuffer(size_t capacity = 1024) : m_capacity(capacity), m_data(capacity, {})
17  {
18  }
19 
21  size_t getCapacity() const
22  {
23  return m_capacity;
24  }
25 
27  void pushBack(T val)
28  {
29  m_sum += val;
30  m_sum -= m_data[m_index];
31  m_data[m_index] = val;
32  m_index = (m_index + 1) % m_capacity;
33  }
34 
36  T getAverage() const
37  {
38  return m_sum / static_cast<T>(m_capacity);
39  }
40 
41 private:
42  size_t m_capacity;
43  std::vector<T> m_data;
44  T m_sum = {};
45  size_t m_index = 0;
46 };
void pushBack(T val)
Pushes the value onto the buffer.
Class to accumulate values for determining a rolling average note that the buffer will be filled with...