24 #ifndef CIRCULAR_BUFFER
25 #define CIRCULAR_BUFFER
50 template <
class data_type>
60 std::vector<data_type>
readData(
size_t n);
87 template <
class data_type>
89 : mCurrentWritePosition(0),
90 mCurrentReadPosition(0),
91 mMaximumSize(maximum_size),
108 template <
class data_type>
111 mCurrentWritePosition = 0;
112 mCurrentReadPosition = 0;
127 template <
class data_type>
130 assert(mMaximumSize > 0);
131 assert(mCurrentWritePosition < mMaximumSize);
132 assert(mCurrentReadPosition < mMaximumSize);
134 mData[mCurrentWritePosition] = data;
136 mCurrentWritePosition = (mCurrentWritePosition+1) % mMaximumSize;
138 mCurrentSize = std::min(mCurrentSize + 1, mMaximumSize);
140 mCurrentReadPosition = (mCurrentWritePosition + mMaximumSize - mCurrentSize) % mMaximumSize;
158 template <
class data_type>
161 std::vector<data_type> data_out(mCurrentSize);
163 std::size_t part1Size = std::min(mCurrentReadPosition + mCurrentSize, mMaximumSize) - mCurrentReadPosition;
164 std::memcpy(data_out.data(), mData.data() + mCurrentReadPosition, part1Size *
sizeof(data_type));
167 std::size_t part2Size = mCurrentSize - part1Size;
168 std::memcpy(data_out.data() + part1Size, mData.data(), part2Size *
sizeof(data_type));
185 template <
class data_type>
188 std::vector<data_type> data_out(std::move(getOrderedData()));
189 if (data_out.size() > n) data_out.resize(n);
191 EptAssert(mCurrentSize >= data_out.size(),
"Do not read more data than existent.");
194 mCurrentReadPosition = (mCurrentReadPosition + data_out.size()) % mMaximumSize;
195 mCurrentSize = mCurrentSize - data_out.size();
212 template <
class data_type>
216 std::vector<data_type> old_data(std::move(getOrderedData()));
217 mMaximumSize = maximum_size;
219 mCurrentSize = std::min(old_data.size(), maximum_size);
221 mData.resize(mMaximumSize);
224 if (mMaximumSize == 0)
return;
227 mCurrentReadPosition = 0;
228 mCurrentWritePosition = mCurrentSize % mMaximumSize;
230 std::memcpy(mData.data(),
231 old_data.data() + (old_data.size() - mCurrentSize),
232 mCurrentSize *
sizeof(data_type));
236 #endif // CIRCULAR_BUFFER
std::size_t maximum_size() const
Return actual maximal size.
std::size_t mMaximumSize
Maximal size of the buffer.
std::vector< data_type > getOrderedData() const
Copy entire data in a time-ordered form.
std::size_t mCurrentSize
Current size of the buffer.
std::size_t mCurrentWritePosition
Current read position.
std::vector< data_type > mData
Internal cyclic data buffer.
CircularBuffer(std::size_t maximum_size=0)
Construct an empty buffer of maximal size zero.
std::vector< data_type > readData(size_t n)
Retrieve time-ordeded data with maximum size of n and remove if from the buffer.
Template class for a circular buffer.
void resize(std::size_t maximum_size)
Resize the buffer, shrink oldest data if necessary.
std::size_t size() const
Return current buffer size.
void push_back(const data_type &data)
Append a new data element to the buffer.
std::size_t mCurrentReadPosition
Current write position.
void clear()
Clear the buffer, keeping its maximal size.