19#ifndef __PONGASOFT_UTILS_COLLECTION_CIRCULAR_BUFFER_H__
20#define __PONGASOFT_UTILS_COLLECTION_CIRCULAR_BUFFER_H__
34template<
typename Op,
typename U,
typename T>
36 std::invocable<Op, U const&, T const&> &&
37 std::same_as<std::invoke_result_t<Op, U const&, T const&>, U>;
43template<
typename Op,
typename U,
typename T>
45 std::invocable<Op, int, U const&, T const&> &&
46 std::same_as<std::invoke_result_t<Op, int, U const&, T const&>, U>;
99 for(
int i = 0; i <
fSize; ++i)
109 if(adjStartOffset + iSize <
fSize)
111 memcpy(oBuffer, &
fBuf[adjStartOffset], iSize *
sizeof(T));
115 int i = adjStartOffset;
116 for(
int k = 0; k < iSize; k++)
118 oBuffer[k] =
fBuf[i];
143 template<
typename U, FoldOp<U, T> Op>
144 U
fold(
int startOffset,
int endOffsetNotIncluded, U initValue, Op &op)
const
146 if(startOffset == endOffsetNotIncluded)
149 U resultValue = initValue;
153 if(startOffset < endOffsetNotIncluded)
155 int size = endOffsetNotIncluded - startOffset;
158 resultValue = std::invoke(op, resultValue,
fBuf[i]);
167 int size = startOffset - endOffsetNotIncluded;
170 resultValue = std::invoke(op, resultValue,
fBuf[i]);
184 template<
typename U, FoldOp<U, T> Op>
185 U
fold(
int endOffsetNotIncluded, U initValue, Op &op)
const
187 return fold(0, endOffsetNotIncluded, initValue, op);
193 template<
typename U, FoldOp<U, T> Op>
194 U
fold(U initValue, Op &op)
const
204 template<
typename U, FoldOpWithIndex<U, T> Op>
205 U
foldWithIndex(
int startOffset,
int endOffsetNotIncluded, U initValue, Op &op)
const
207 if(startOffset == endOffsetNotIncluded)
210 U resultValue = initValue;
213 int index = startOffset;
215 if(startOffset < endOffsetNotIncluded)
217 int size = endOffsetNotIncluded - startOffset;
220 resultValue = std::invoke(op, index, resultValue,
fBuf[i]);
230 int size = startOffset - endOffsetNotIncluded;
233 resultValue = std::invoke(op, index, resultValue,
fBuf[i]);
248 template<
typename U, FoldOpWithIndex<U, T> Op>
251 return foldWithIndex(0, endOffsetNotIncluded, initValue, op);
257 template<
typename U, FoldOpWithIndex<U, T> Op>
281 while(index >=
fSize)
int fStart
Definition CircularBuffer.h:289
U foldWithIndex(int endOffsetNotIncluded, U initValue, Op &op) const
Shortcut with startOffset = 0.
Definition CircularBuffer.h:249
U foldWithIndex(int startOffset, int endOffsetNotIncluded, U initValue, Op &op) const
Similar to fold but Op is also provided the index (starting at startOffset).
Definition CircularBuffer.h:205
int adjustIndexFromOffset(int offset) const
Definition CircularBuffer.h:264
void setAt(int offset, T e)
Definition CircularBuffer.h:81
void copyToBuffer(int startOffset, T *oBuffer, int iSize)
Definition CircularBuffer.h:105
T getAt(int offset) const
Definition CircularBuffer.h:76
int getSize() const
Definition CircularBuffer.h:71
void push(T e)
Definition CircularBuffer.h:91
int adjustIndex(int index) const
Definition CircularBuffer.h:272
U fold(int endOffsetNotIncluded, U initValue, Op &op) const
Shortcut with startOffset 0.
Definition CircularBuffer.h:185
CircularBuffer(CircularBuffer const &iOther)
Definition CircularBuffer.h:59
void incrementHead()
Definition CircularBuffer.h:86
int fSize
Definition CircularBuffer.h:287
U foldWithIndex(U initValue, Op &op) const
Shortcut for entire buffer (starting at startOffset 0).
Definition CircularBuffer.h:258
void init(T initValue)
Definition CircularBuffer.h:97
~CircularBuffer()
Definition CircularBuffer.h:65
U fold(int startOffset, int endOffsetNotIncluded, U initValue, Op &op) const
"standard" implementation of the fold algorithm starting at startOffset and ending at endOffsetNotInc...
Definition CircularBuffer.h:144
CircularBuffer(int iSize)
Definition CircularBuffer.h:52
U fold(U initValue, Op &op) const
Shortcut for entire buffer (starting at startOffset 0).
Definition CircularBuffer.h:194
T * fBuf
Definition CircularBuffer.h:288
Defines the FoldOp concept which is a binary operation that takes two arguments of type U and T and r...
Definition CircularBuffer.h:35
Similar to the FoldOp concept but with an additional index argument.
Definition CircularBuffer.h:44
Definition CircularBuffer.h:28
Definition CircularBuffer.h:27