Main MRPT website > C++ reference for MRPT 1.5.5
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  m_data[m_next_write++]=d;
48 
50  throw std::out_of_range("push: circular_buffer is full");
51  }
52 
53  /** Insert a reference of the given element in the buffer.
54  * \exception std::out_of_range If the buffer run out of space.
55  */
56  void push_ref(const T &d) {
57  m_data[m_next_write++]=d;
59 
61  throw std::out_of_range("push: circular_buffer is full");
62  }
63 
64  /** Insert an array of elements in the buffer.
65  * \exception std::out_of_range If the buffer run out of space.
66  */
67  void push_many(T *array_elements, size_t count) {
68  while (count--)
69  push(*array_elements++);
70  }
71 
72  /** Retrieve an element from the buffer.
73  * \exception std::out_of_range If the buffer is empty.
74  */
75  T pop() {
77  throw std::out_of_range("pop: circular_buffer is empty");
78 
79  const size_t i = m_next_read++;
81  return m_data[i];
82  }
83 
84  /** Retrieve an element from the buffer.
85  * \exception std::out_of_range If the buffer is empty.
86  */
87  void pop(T &out_val) {
89  throw std::out_of_range("pop: circular_buffer is empty");
90 
91  out_val=m_data[m_next_read++];
93  }
94 
95  /** Pop a number of elements into a user-provided array.
96  * \exception std::out_of_range If the buffer has less elements than requested. */
97  void pop_many(T *out_array, size_t count) {
98  while (count--)
99  pop(*out_array++);
100  }
101 
102  /** Peek (see without modifying) what is to be read from the buffer if pop() was to be called.
103  * \exception std::out_of_range If the buffer is empty. */
104  T peek() const {
105  if (m_next_read==m_next_write) throw std::out_of_range("peek: circular_buffer is empty");
106  return m_data[m_next_read];
107  }
108  /** Like peek(), but seeking ahead in the buffer (index=0 means the immediate next element, index=1 the following one, etc.)
109  * \exception std::out_of_range If trying to read passing the number of available elements. */
110  T peek(size_t index) const {
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];
113  }
114 
115  /** Like peek(), for multiple elements, storing a number of elements into a user-provided array.
116  * \exception std::out_of_range If the buffer has less elements than requested. */
117  void peek_many(T *out_array, size_t count) const {
118  size_t peek_read = m_next_read;
119  while (count--)
120  {
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;
124  *out_array++ = val;
125  }
126  }
127 
128  /** Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size of the internal buffer)
129  * \sa capacity */
130  size_t size() const {
132  return m_next_write-m_next_read;
133  else return m_next_write + (m_size-m_next_read);
134  }
135 
136  /** Return the maximum capacity of the buffer.
137  * \sa size
138  */
139  size_t capacity() const {
140  return m_size;
141  }
142 
143  /** The maximum number of elements that can be written ("push") without rising an overflow error.
144  */
145  size_t available() const {
146  return (capacity()-size())-1;
147  }
148 
149  /** Delete all the stored data, if any. */
150  void clear() {
152  }
153 
154  }; // end class circular_buffer
155 
156  } // End of namespace
157 } // End of namespace
158 #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.5 Git: e06b63dbf Fri Dec 1 14:41:11 2017 +0100 at lun oct 28 01:31:35 CET 2019