9 #ifndef circular_buffer_H 10 #define circular_buffer_H 38 if (m_size<=2)
throw std::invalid_argument(
"size must be >2");
46 m_data[m_next_write++]=d;
47 if (m_next_write==m_size) m_next_write=0;
49 if (m_next_write==m_next_read)
50 throw std::out_of_range(
"push: circular_buffer is full");
57 m_data[m_next_write++]=d;
58 if (m_next_write==m_size) m_next_write=0;
60 if (m_next_write==m_next_read)
61 throw std::out_of_range(
"push: circular_buffer is full");
69 push(*array_elements++);
76 if (m_next_read==m_next_write)
77 throw std::out_of_range(
"pop: circular_buffer is empty");
79 const size_t i = m_next_read++;
80 if (m_next_read==m_size) m_next_read=0;
87 void pop(T &out_val) {
88 if (m_next_read==m_next_write)
89 throw std::out_of_range(
"pop: circular_buffer is empty");
91 out_val=m_data[m_next_read++];
92 if (m_next_read==m_size) m_next_read=0;
105 if (m_next_read==m_next_write)
throw std::out_of_range(
"peek: circular_buffer is empty");
111 if (index>=this->
size())
throw std::out_of_range(
"peek: seek out of range");
112 return m_data[(m_next_read + index)%m_size];
121 if (peek_read==m_next_write)
throw std::out_of_range(
"peek: circular_buffer is empty");
122 T val =m_data[peek_read++];
123 if (peek_read==m_size) peek_read=0;
131 if (m_next_write>=m_next_read)
151 m_next_write = m_next_read = 0;
void clear()
Delete all the stored data, if any.
void peek_many(T *out_array, size_t count) const
Like peek(), for multiple elements, storing a number of elements into a user-provided array...
size_t size() const
Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size ...
circular_buffer(const size_t size)
void pop_many(T *out_array, size_t count)
Pop a number of elements into a user-provided array.
void pop(T &out_val)
Retrieve an element from the buffer.
size_t available() const
The maximum number of elements that can be written ("push") without rising an overflow error...
T pop()
Retrieve an element from the buffer.
A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
T peek(size_t index) const
Like peek(), but seeking ahead in the buffer (index=0 means the immediate next element, index=1 the following one, etc.) If trying to read passing the number of available elements.
T peek() const
Peek (see without modifying) what is to be read from the buffer if pop() was to be called...
void push(T d)
Insert a copy of the given element in the buffer.
size_t capacity() const
Return the maximum capacity of the buffer.
void push_many(T *array_elements, size_t count)
Insert an array of elements in the buffer.
void push_ref(const T &d)
Insert a reference of the given element in the buffer.