19#ifndef __PONGASOFT_UTILS_COLLECTION_CIRCULAR_BUFFER_H__
20#define __PONGASOFT_UTILS_COLLECTION_CIRCULAR_BUFFER_H__
57 inline T
getAt(
int offset)
const
62 inline void setAt(
int offset, T e)
78 inline void init(T initValue)
80 for(
int i = 0; i <
fSize; ++i)
90 if(adjStartOffset + iSize <
fSize)
92 memcpy(oBuffer, &
fBuf[adjStartOffset], iSize *
sizeof(T));
96 int i = adjStartOffset;
97 for(
int k = 0; k < iSize; k++)
130 template<
typename U,
class BinaryPredicate>
131 inline U
fold(
int startOffset,
int endOffsetNotIncluded, U initValue, BinaryPredicate &op)
const
133 if(startOffset == endOffsetNotIncluded)
136 U resultValue = initValue;
140 if(startOffset < endOffsetNotIncluded)
142 int size = endOffsetNotIncluded - startOffset;
145 resultValue = op(resultValue,
fBuf[i]);
154 int size = startOffset - endOffsetNotIncluded;
157 resultValue = op(resultValue,
fBuf[i]);
171 template<
typename U,
class BinaryPredicate>
172 inline U
fold(
int endOffsetNotIncluded, U initValue, BinaryPredicate &op)
const
174 return fold(0, endOffsetNotIncluded, initValue, op);
180 template<
typename U,
class BinaryPredicate>
181 inline U
fold(U initValue, BinaryPredicate &op)
const
192 template<
typename U,
class BinaryPredicateWithIndex>
193 inline U
foldWithIndex(
int startOffset,
int endOffsetNotIncluded, U initValue, BinaryPredicateWithIndex &op)
const
195 if(startOffset == endOffsetNotIncluded)
198 U resultValue = initValue;
201 int index = startOffset;
203 if(startOffset < endOffsetNotIncluded)
205 int size = endOffsetNotIncluded - startOffset;
208 resultValue = op(index, resultValue,
fBuf[i]);
218 int size = startOffset - endOffsetNotIncluded;
221 resultValue = op(index, resultValue,
fBuf[i]);
236 template<
typename U,
class BinaryPredicateWithIndex>
237 inline U
foldWithIndex(
int endOffsetNotIncluded, U initValue, BinaryPredicateWithIndex &op)
const
239 return foldWithIndex(0, endOffsetNotIncluded, initValue, op);
245 template<
typename U,
class BinaryPredicateWithIndex>
269 while(index >=
fSize)
int fStart
Definition CircularBuffer.h:277
U foldWithIndex(U initValue, BinaryPredicateWithIndex &op) const
Shortcut for entire buffer (starting at startOffset 0).
Definition CircularBuffer.h:246
int adjustIndexFromOffset(int offset) const
Definition CircularBuffer.h:252
U foldWithIndex(int startOffset, int endOffsetNotIncluded, U initValue, BinaryPredicateWithIndex &op) const
Similar to fold but BinaryPredicateWithIndex is also provided the index (starting at startOffset).
Definition CircularBuffer.h:193
void setAt(int offset, T e)
Definition CircularBuffer.h:62
void copyToBuffer(int startOffset, T *oBuffer, int iSize)
Definition CircularBuffer.h:86
U fold(int endOffsetNotIncluded, U initValue, BinaryPredicate &op) const
Shortcut with startOffset 0.
Definition CircularBuffer.h:172
T getAt(int offset) const
Definition CircularBuffer.h:57
int getSize() const
Definition CircularBuffer.h:52
void push(T e)
Definition CircularBuffer.h:72
int adjustIndex(int index) const
Definition CircularBuffer.h:260
U fold(U initValue, BinaryPredicate &op) const
Shortcut for entire buffer (starting at startOffset 0).
Definition CircularBuffer.h:181
CircularBuffer(CircularBuffer const &iOther)
Definition CircularBuffer.h:40
void incrementHead()
Definition CircularBuffer.h:67
U fold(int startOffset, int endOffsetNotIncluded, U initValue, BinaryPredicate &op) const
"standard" implementation of the fold algorithm starting at startOffset and ending at endOffsetNotInc...
Definition CircularBuffer.h:131
int fSize
Definition CircularBuffer.h:275
void init(T initValue)
Definition CircularBuffer.h:78
U foldWithIndex(int endOffsetNotIncluded, U initValue, BinaryPredicateWithIndex &op) const
Shortcut with startOffset = 0.
Definition CircularBuffer.h:237
~CircularBuffer()
Definition CircularBuffer.h:46
CircularBuffer(int iSize)
Definition CircularBuffer.h:33
T * fBuf
Definition CircularBuffer.h:276
Definition CircularBuffer.h:27
Definition CircularBuffer.h:26