27 #ifndef sigproc_circularBuffer
28 #define sigproc_circularBuffer
70 template<
typename _derivedT,
typename _storedT,
typename _indexT>
176 return *
static_cast<derivedT *
>(
this);
181 return *
static_cast<const derivedT *
>(
this);
186 template<
class derivedT,
typename storedT,
typename indexT>
191 template<
class derivedT,
typename storedT,
typename indexT>
197 template<
class derivedT,
typename storedT,
typename indexT>
204 m_maxEntries = maxEnt;
205 m_buffer.reserve(m_maxEntries);
207 derived().setMaxEntries(maxEnt);
213 template<
class derivedT,
typename storedT,
typename indexT>
219 template<
class derivedT,
typename storedT,
typename indexT>
222 return m_buffer.size();
225 template<
class derivedT,
typename storedT,
typename indexT>
228 if( m_buffer.size() < m_maxEntries )
230 m_buffer.push_back(newEnt);
231 m_latest = m_buffer.size()-1;
233 derived().setWrapStartup();
237 m_buffer[m_nextEntry] = newEnt;
238 m_latest = m_nextEntry;
240 if(m_nextEntry >= m_buffer.size()) m_nextEntry = 0;
248 template<
class derivedT,
typename storedT,
typename indexT>
254 template<
class derivedT,
typename storedT,
typename indexT>
260 template<
class derivedT,
typename storedT,
typename indexT>
266 template<
class derivedT,
typename storedT,
typename indexT>
269 return derived().at(m_nextEntry, idx);
272 template<
class derivedT,
typename storedT,
typename indexT>
275 return derived().at(m_nextEntry, idx);
278 template<
class derivedT,
typename storedT,
typename indexT>
283 return derived().at(refEntry, idx);
286 template<
class derivedT,
typename storedT,
typename indexT>
291 return derived().at(refEntry, idx);
298 template<
typename _storedT,
typename _indexT>
337 m_wrapEntry = this->
m_buffer.size();
357 if(idx > this->m_wrapEntry)
return this->
m_buffer[refEntry + idx - this->
m_buffer.size()];
358 else return this->
m_buffer[refEntry + idx];
372 if(idx > this->m_wrapEntry)
return this->
m_buffer[refEntry + idx - this->
m_buffer.size()];
373 else return this->
m_buffer[refEntry + idx];
382 template<
typename _storedT,
typename _indexT>
455 template<
typename _storedT,
typename _indexT>
517 return this->
m_buffer[m_indices[refEntry + idx]];
531 return this->
m_buffer[m_indices[refEntry + idx]];
CRTP base class for all circular buffers, providing the underlying memory management and accessors.
storedT & at(indexT refEntry, indexT idx)
Get the entry at a given index relative a fixed reference entry.
storedT & operator[](indexT idx)
Get the entry at a given index.
circularBufferBase(indexT maxEnt)
Sizing constructor.
indexT maxEntries()
Get the maximum size of the buffer.
circularBufferBase()
Default c'tor.
std::vector< storedT > m_buffer
The circular buffer storage.
void maxEntries(indexT maxEnt)
Set the maximum size of the buffer.
indexT size() const
Get the number of entries.
const storedT & at(indexT refEntry, indexT idx) const
Get the entry at a given index relative a fixed start entry, const version.
indexT earliest()
Returns the index of the earliest entry.
indexT latest()
Returns the index of the latest entry.
const storedT & operator[](indexT idx) const
indexT m_maxEntries
The maximum number of entries to allow in the buffer before wrapping.
_derivedT derivedT
The child class.
uint64_t m_mono
A monotonic counter, which is incremented for each new entry, and reset on call to maxEntries.
_storedT storedT
The type stored in the circular buffer.
_indexT indexT
The index type, also used for sizes.
indexT m_latest
Index into m_buff of the latest entry. This is the newest entry in the buffer.
indexT m_nextEntry
Index into m_buffer of the next entry. This is the oldest entry in the buffer.
void nextEntry(const storedT &newEnt)
Add the next entry to the circular buffer.
Circular buffer which wraps with an if statement (branching) [faster than mod, less memory than index...
_storedT storedT
The maximum number of entries to allow in the buffer before wrapping.
void setMaxEntries(indexT maxEnt)
Interface implementation for maxEntries.
void setWrapStartup()
Interface implementation for wrapping setup during the startup phase.
circularBufferBranch()
Default c'tor.
storedT & at(indexT refEntry, indexT idx)
Interface implementation for entry access.
void setWrap()
Interface implementation for wrapping setup after the startup phase.
const storedT & at(indexT refEntry, indexT idx) const
Interface implementation for entry access, const version.
circularBufferBranch(indexT maxEnt)
Sizing constructor.
_indexT indexT
The index type, also used for sizes.
Circular buffer which wraps with a pre-populated indices array [generally fastest].
std::vector< size_t > m_indices
Vector of indices for fast indexing into parent's m_buffer.
circularBufferIndex()
Default c'tor.
const storedT & at(indexT refEntry, indexT idx) const
Interface implementation for entry access, const version.
_indexT indexT
The index type, also used for sizes.
circularBufferIndex(indexT maxEnt)
Sizing constructor.
void setWrap()
Interface implementation for wrapping setup after the startup phase.
_storedT storedT
The maximum number of entries to allow in the buffer before wrapping.
storedT & at(indexT refEntry, indexT idx)
Interface implementation for entry access.
void setWrapStartup()
Interface implementation for wrapping setup during the startup phase.
void setMaxEntries(indexT maxEnt)
Interface implementation for maxEntries.
Circular buffer which wraps with the mod opoerator [very slow].
_storedT storedT
The maximum number of entries to allow in the buffer before wrapping.
void setWrapStartup()
Interface implementation for wrapping setup during the startup phase.
_indexT indexT
The index type, also used for sizes.
const storedT & at(indexT refEntry, indexT idx) const
Interface implementation for entry access, const version.
storedT & at(indexT refEntry, indexT idx)
Interface implementation for entry access.
circularBufferMod()
Default c'tor.
circularBufferMod(indexT maxEnt)
Sizing constructor.
void setWrap()
Interface implementation for wrapping setup after the startup phase.
void setMaxEntries(indexT maxEnt)
Interface implementation for maxEntries.