Fawkes API  Fawkes Development Version
circular_buffer.h
1 
2 /***************************************************************************
3  * circual_buffer.h - Circular buffer
4  *
5  * Created: Fri Aug 15 12:00:42 2014
6  * Copyright 2014 Till Hofmann
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __CORE_UTILS_CIRCULAR_BUFFER_H_
25 #define __CORE_UTILS_CIRCULAR_BUFFER_H_
26 
27 #include <deque>
28 
29 namespace fawkes {
30 
31 /** @class CircularBuffer <core/utils/circular_buffer.h>
32  * Circular buffer with a fixed size.
33  * This class provides a a circular buffer.
34  * A circular buffer is a container with a fixed (maximum) size.
35  * It automatically maintains its size by removing elements from the front,
36  * if necessary. This implementation does not allow any element manipulation
37  * other than push_back() and pop_front(). All returned references to elements
38  * are constant.
39  *
40  * @ingroup FCL
41  * @author Till Hofmann
42  */
43 template <typename Type>
45 {
46  public:
47 
48  /** The size_type of the buffer */
49  typedef size_t size_type;
50  /** The CircularBuffer's iterator is a std::deque iterator */
51  typedef typename std::deque<Type>::const_iterator const_iterator;
52  /** iterator is also const, we don't want to manipulate any elements */
53  typedef const_iterator iterator;
54 
55  /** Constructor.
56  * @param n the maximum size of the buffer */
57  CircularBuffer(size_type n)
58  : __deque(),
59  __max_size(n)
60  {}
61 
62  /** Copy constructor.
63  * @param other CircularBuffer to copy
64  */
66  : __deque(other.get_deque()),
67  __max_size(other.get_max_size())
68  {}
69 
70  /** Destructor. */
71  virtual ~CircularBuffer()
72  {}
73 
74  /** Insert an element at the end of the buffer
75  * and delete the first element if necessary
76  * @param val the value to insert
77  */
78  virtual void push_back(const Type& val)
79  {
80  if (__deque.size() >= __max_size) {
81  __deque.pop_front();
82  }
83  __deque.push_back(val);
84  }
85 
86  /** Delete the first element */
87  virtual void pop_front()
88  {
89  __deque.pop_front();
90  }
91 
92  /** Get the maximum size of the buffer
93  * @return the maximum size
94  */
95  virtual size_type get_max_size() const
96  {
97  return __max_size;
98  }
99 
100  /** Get the deque used to store the elements
101  * @return the deque
102  */
103  virtual std::deque<Type> get_deque() const
104  {
105  return __deque;
106  }
107 
108  /** Element access
109  * @param n position of the element
110  * @return reference to the n-th element
111  */
112  virtual const Type & operator[](size_type n) const
113  {
114  return __deque[n];
115  }
116 
117  /** Element access
118  * @param n position of the element
119  * @return reference to the n-th element
120  */
121  virtual const Type & at(size_type n) const
122  {
123  return __deque.at(n);
124  }
125 
126  /** Access the first element in the buffer
127  * @return reference to the first element
128  */
129  virtual const Type & front() const
130  {
131  return __deque.front();
132  }
133 
134  /** Access the last element in the buffer
135  * @return reference to the last element
136  */
137  virtual const Type & back() const
138  {
139  return __deque.back();
140  }
141 
142  /** Get iterator to the beginning
143  * @return iterator
144  */
145  virtual const_iterator begin() const
146  {
147  return __deque.begin();
148  }
149 
150  /** Get iterator to the end
151  * @return iterator
152  */
153  virtual const_iterator end() const
154  {
155  return __deque.end();
156  }
157 
158  /** Get actual size of the buffer
159  * @return number of elements in the buffer
160  */
161  virtual size_type size() const
162  {
163  return __deque.size();
164  }
165 
166  protected:
167  /** The deque used to store the data */
168  std::deque<Type> __deque;
169  /** The maximum size of the circular buffer */
170  size_type __max_size;
171 
172 };
173 
174 } // end namespace fawkes
175 
176 #endif
virtual const Type & back() const
Access the last element in the buffer.
size_type __max_size
The maximum size of the circular buffer.
Fawkes library namespace.
virtual const Type & at(size_type n) const
Element access.
virtual const_iterator end() const
Get iterator to the end.
virtual std::deque< Type > get_deque() const
Get the deque used to store the elements.
const_iterator iterator
iterator is also const, we don&#39;t want to manipulate any elements
virtual size_type size() const
Get actual size of the buffer.
Circular buffer with a fixed size.
virtual ~CircularBuffer()
Destructor.
CircularBuffer(size_type n)
Constructor.
virtual const Type & front() const
Access the first element in the buffer.
virtual const_iterator begin() const
Get iterator to the beginning.
std::deque< Type > __deque
The deque used to store the data.
std::deque< Type >::const_iterator const_iterator
The CircularBuffer&#39;s iterator is a std::deque iterator.
virtual const Type & operator[](size_type n) const
Element access.
size_t size_type
The size_type of the buffer.
virtual size_type get_max_size() const
Get the maximum size of the buffer.
CircularBuffer(const CircularBuffer< Type > &other)
Copy constructor.
virtual void pop_front()
Delete the first element.
virtual void push_back(const Type &val)
Insert an element at the end of the buffer and delete the first element if necessary.