Main MRPT website > C++ reference for MRPT 1.5.7
circular_buffer.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef circular_buffer_H
10 #define circular_buffer_H
11 
12 #include <vector>
13 #include <stdexcept>
14 
15 namespace mrpt
16 {
17  namespace utils
18  {
19  /** A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the underlying storage.
20  * \ingroup stlext_grp
21  * \note Defined in #include <mrpt/utils/circular_buffer.h>
22  */
23  template <typename T>
25  {
26  private:
27  std::vector<T> m_data;
28  const size_t m_size;
30 
31  public:
32  circular_buffer(const size_t size) :
33  m_data(size),
34  m_size(size),
35  m_next_read(0),
36  m_next_write(0)
37  {
38  if (m_size<=2) throw std::invalid_argument("size must be >2");
39  }
40  //virtual ~circular_buffer() { }
41 
42  /** Insert a copy of the given element in the buffer.
43  * \exception std::out_of_range If the buffer run out of space.
44  */
45  void push(T d) {
46  size_t new_idx = m_next_write + 1;
47  if (new_idx == m_size) new_idx = 0;
48  if (new_idx == m_next_read)
49  throw std::out_of_range("push: circular_buffer is full");
50  m_data[m_next_write] = d;
51  m_next_write = new_idx;
52  }
53 
54  /** Insert a reference of the given element in the buffer.
55  * \exception std::out_of_range If the buffer run out of space.
56  */
57  void push_ref(const T &d) {
58  m_data[m_next_write++]=d;
60 
62  throw std::out_of_range("push: circular_buffer is full");
63  }
64 
65  /** Insert an array of elements in the buffer.
66  * \exception std::out_of_range If the buffer run out of space.
67  */
68  void push_many(T *array_elements, size_t count) {
69  while (count--)
70  push(*array_elements++);
71  }
72 
73  /** Retrieve an element from the buffer.
74  * \exception std::out_of_range If the buffer is empty.
75  */
76  T pop() {
78  throw std::out_of_range("pop: circular_buffer is empty");
79 
80  const size_t i = m_next_read++;
82  return m_data[i];
83  }
84 
85  /** Retrieve an element from the buffer.
86  * \exception std::out_of_range If the buffer is empty.
87  */
88  void pop(T &out_val) {
90  throw std::out_of_range("pop: circular_buffer is empty");
91 
92  out_val=m_data[m_next_read++];
94  }
95 
96  /** Pop a number of elements into a user-provided array.
97  * \exception std::out_of_range If the buffer has less elements than requested. */
98  void pop_many(T *out_array, size_t count) {
99  while (count--)
100  pop(*out_array++);
101  }
102 
103  /** Peek (see without modifying) what is to be read from the buffer if pop() was to be called.
104  * \exception std::out_of_range If the buffer is empty. */
105  T peek() const {
106  if (m_next_read==m_next_write) throw std::out_of_range("peek: circular_buffer is empty");
107  return m_data[m_next_read];
108  }
109  /** Like peek(), but seeking ahead in the buffer (index=0 means the immediate next element, index=1 the following one, etc.)
110  * \exception std::out_of_range If trying to read passing the number of available elements. */
111  T peek(size_t index) const {
112  if (index>=this->size()) throw std::out_of_range("peek: seek out of range");
113  return m_data[(m_next_read + index)%m_size];
114  }
115 
116  /** Like peek(), for multiple elements, storing a number of elements into a user-provided array.
117  * \exception std::out_of_range If the buffer has less elements than requested. */
118  void peek_many(T *out_array, size_t count) const {
119  size_t peek_read = m_next_read;
120  while (count--)
121  {
122  if (peek_read==m_next_write) throw std::out_of_range("peek: circular_buffer is empty");
123  T val =m_data[peek_read++];
124  if (peek_read==m_size) peek_read=0;
125  *out_array++ = val;
126  }
127  }
128 
129  /** Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size of the internal buffer)
130  * \sa capacity */
131  size_t size() const {
133  return m_next_write-m_next_read;
134  else return m_next_write + (m_size-m_next_read);
135  }
136 
137  /** Return the maximum capacity of the buffer.
138  * \sa size
139  */
140  size_t capacity() const {
141  return m_size;
142  }
143 
144  /** The maximum number of elements that can be written ("push") without rising an overflow error.
145  */
146  size_t available() const {
147  return (capacity()-size())-1;
148  }
149 
150  /** Delete all the stored data, if any. */
151  void clear() {
153  }
154 
155  }; // end class circular_buffer
156 
157  } // End of namespace
158 } // End of namespace
159 #endif
GLuint GLuint GLsizei count
Definition: glext.h:3512
T peek() const
Peek (see without modifying) what is to be read from the buffer if pop() was to be called...
size_t available() const
The maximum number of elements that can be written ("push") without rising an overflow error...
void clear()
Delete all the stored data, if any.
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.
GLuint index
Definition: glext.h:3891
int val
Definition: mrpt_jpeglib.h:953
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.
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.
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.)
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...
GLsizeiptr size
Definition: glext.h:3779
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.



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019