Main MRPT website > C++ reference for MRPT 1.3.2
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-2015, 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;
47  if (m_next_write==m_size) m_next_write=0;
48 
49  if (m_next_write==m_next_read)
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;
58  if (m_next_write==m_size) m_next_write=0;
59 
60  if (m_next_write==m_next_read)
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() {
76  if (m_next_read==m_next_write)
77  throw std::out_of_range("pop: circular_buffer is empty");
78 
79  const size_t i = m_next_read++;
80  if (m_next_read==m_size) m_next_read=0;
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) {
88  if (m_next_read==m_next_write)
89  throw std::out_of_range("pop: circular_buffer is empty");
90 
91  out_val=m_data[m_next_read++];
92  if (m_next_read==m_size) m_next_read=0;
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  */
98  void pop_many(T *out_array, size_t count) {
99  while (count--)
100  pop(*out_array++);
101  }
102 
103  /** Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size of the internal buffer)
104  * \sa capacity
105  */
106  size_t size() const {
107  if (m_next_write>=m_next_read)
108  return m_next_write-m_next_read;
109  else return m_next_write + (m_size-m_next_read);
110  }
111 
112  /** Return the maximum capacity of the buffer.
113  * \sa size
114  */
115  size_t capacity() const {
116  return m_size;
117  }
118 
119  /** The maximum number of elements that can be written ("push") without rising an overflow error.
120  */
121  size_t available() const {
122  return (capacity()-size())-1;
123  }
124 
125  /** Delete all the stored data, if any. */
126  void clear() {
127  m_next_write = m_next_read = 0;
128  }
129 
130  }; // end class circular_buffer
131 
132  } // End of namespace
133 } // End of namespace
134 #endif
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.
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.
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.



Page generated by Doxygen 1.8.11 for MRPT 1.3.2 SVN: at Wed May 25 02:34:21 UTC 2016