MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin_FixedCircularBuffer.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
12#include <array>
13namespace marvin::containers {
38 template <FloatType T, size_t N>
39 class FixedCircularBuffer final {
40 public:
45 reset();
46 }
47
52 void push(T toPush) {
53 m_buffer[m_writeIndex++] = toPush;
54 while (m_writeIndex >= static_cast<int>(N)) {
55 m_writeIndex -= static_cast<int>(N);
56 }
57 }
58
65 [[nodiscard]] T peek(size_t offset) const {
66 auto actualIndex = (m_writeIndex - static_cast<int>(offset));
67 while (actualIndex < 0) {
68 actualIndex += static_cast<int>(N);
69 }
70 const auto res = m_buffer[static_cast<size_t>(actualIndex)];
71 return res;
72 }
73
77 void reset() {
78 std::fill(m_buffer.begin(), m_buffer.end(), static_cast<T>(0.0));
79 m_writeIndex = 0;
80 }
81
82 private:
83 std::array<T, N> m_buffer;
84 int m_writeIndex{ 0 };
85 };
86} // namespace marvin::containers
FixedCircularBuffer()
Definition marvin_FixedCircularBuffer.h:44
T peek(size_t offset) const
Definition marvin_FixedCircularBuffer.h:65
void push(T toPush)
Definition marvin_FixedCircularBuffer.h:52
void reset()
Definition marvin_FixedCircularBuffer.h:77
Views, wrappers and custom containers.
Definition marvin_BufferView.h:15