simplecircularbuffer.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. class SimpleCircularBuffer<Class T>
  2. {
  3. private int m_Pointer = 0;
  4. private int m_Size = 0;
  5. private ref array<T> m_Samples = new array<T>();
  6. /**
  7. \brief Initialize Simple Circular Buffer Cyclic Buffer
  8. \param pSize \p size of the buffer
  9. \param pDefaultValue \p initial value stored in buffer
  10. */
  11. void SimpleCircularBuffer(int pSize, T pDefaultValue = 0)
  12. {
  13. m_Size = pSize;
  14. for (int i = 0; i < m_Size; i++)
  15. {
  16. m_Samples.Insert(pDefaultValue);
  17. }
  18. }
  19. /**
  20. \brief Add new value to buffer
  21. \param newSample \p value that will be added to buffer
  22. \return average value from the buffer
  23. @code
  24. ref SimpleCircularBuffer<float> m_Buffer = SimpleCircularBuffer<float>(2, -1);
  25. m_Buffer.Add(0.1);
  26. @endcode
  27. */
  28. void Add(T newSample)
  29. {
  30. m_Samples[m_Pointer++] = newSample;
  31. if (m_Pointer == m_Size)
  32. {
  33. m_Pointer = 0;
  34. }
  35. }
  36. /**
  37. \brief Returns value from given index
  38. \param pIndex \p size of the buffer
  39. */
  40. T Get(T pIndex)
  41. {
  42. return m_Samples[pIndex];
  43. }
  44. /**
  45. \brief Returns array of values stored in buffer
  46. \param pIndex \p size of the buffer
  47. */
  48. array<T> GetValues()
  49. {
  50. return m_Samples;
  51. }
  52. }