6 #ifndef CIRCULARBUFFER_HPP_INCLUDE
7 #define CIRCULARBUFFER_HPP_INCLUDE
92 const type&
value(
const int index)
const;
106 std::vector<type>
make_vector(
const unsigned int start,
const unsigned int end)
const;
109 std::vector<type> m_buffer;
111 unsigned long m_head;
113 unsigned long m_count;
118 template <
class type>
125 template <
class type>
135 template <
class type>
141 template <
class type>
147 template <
class type>
153 template <
class type>
160 template <
class type>
164 if (size < m_count && m_max_size > 0) {
165 int size_diff = m_count - size;
166 std::vector<type> temp;
167 for (
size_t idx = size_diff; idx < m_count; ++idx) {
168 temp.push_back(value(idx));
171 m_buffer.resize(size);
176 m_buffer.resize(size);
182 template <
class type>
185 if (m_max_size < 1) {
188 if (m_count < m_max_size) {
189 m_buffer.at(m_count) = value;
193 m_buffer.at(m_head) = value;
194 m_head = ((m_head + 1) % m_max_size);
198 template <
class type>
201 if (index >=
static_cast<int>(m_count) || index < -
static_cast<int>(m_count)) {
202 throw Exception(std::string(
"CircularBuffer::value(): index [") + std::to_string(index) +
"] is out of bounds",
206 const int new_index = m_count + index;
207 return m_buffer.at((m_head + new_index) % m_max_size);
209 return m_buffer.at((m_head + index) % m_max_size);
213 template <
class type>
216 std::vector<type> result(size());
218 std::copy(m_buffer.begin(), m_buffer.begin() + m_count, result.begin());
221 std::copy(m_buffer.begin() + m_head, m_buffer.end(), result.begin());
222 std::copy(m_buffer.begin(), m_buffer.begin() + m_head, result.end() - m_head);
227 template <
class type>
230 if (idx_start >= (
unsigned int)size()) {
233 if (idx_end > (
unsigned int)size()) {
236 if (idx_end <= idx_start) {
240 int slice_length = idx_end - idx_start;
241 std::vector<type> result(slice_length);
243 unsigned int start=(m_head + idx_start) % capacity();
244 unsigned int end=(((m_head + idx_end) - 1) % capacity()) + 1;
247 std::copy(m_buffer.begin() + start, m_buffer.begin() + end, result.begin());
250 std::copy(m_buffer.begin() + start, m_buffer.end(), result.begin());
251 std::copy(m_buffer.begin(), m_buffer.begin() + end, result.begin() + capacity() - start);
Templated container for a circular buffer implementation. The CircularBuffer container implements a f...
Definition: CircularBuffer.hpp:24
virtual ~CircularBuffer()
CircularBuffer destructor, virtual.
Definition: CircularBuffer.hpp:136
std::vector< type > make_vector(void) const
Create a vector from the entire circular buffer contents.
Definition: CircularBuffer.hpp:214
int capacity(void) const
Capacity of the buffer.
Definition: CircularBuffer.hpp:148
int size(void) const
Size of the buffer contents.
Definition: CircularBuffer.hpp:142
const type & value(const int index) const
Returns a constant reference to the value from the buffer.
Definition: CircularBuffer.hpp:199
void clear(void)
Clears all entries from the buffer.
Definition: CircularBuffer.hpp:154
CircularBuffer()
Definition: CircularBuffer.hpp:119
void set_capacity(const unsigned int size)
Re-size the circular buffer.
Definition: CircularBuffer.hpp:161
std::vector< type > make_vector(const unsigned int start, const unsigned int end) const
Create a vector slice from the circular buffer contents.
Definition: CircularBuffer.hpp:228
void insert(const type value)
Insert a value into the buffer.
Definition: CircularBuffer.hpp:183
CircularBuffer(unsigned int size)
Constructor for the CircularBuffer template.
Definition: CircularBuffer.hpp:126
Class for all GEOPM-specific exceptions.
Definition: Exception.hpp:48
@ GEOPM_ERROR_INVALID
Definition: geopm_error.h:20
@ GEOPM_ERROR_RUNTIME
Definition: geopm_error.h:18