GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
CircularBuffer.h
Go to the documentation of this file.
1 
14 /*
15  GRT MIT License
16  Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
17 
18  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
19  and associated documentation files (the "Software"), to deal in the Software without restriction,
20  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
22  subject to the following conditions:
23 
24  The above copyright notice and this permission notice shall be included in all copies or substantial
25  portions of the Software.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
28  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
30  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32  */
33 
34 #ifndef GRT_CIRCULAR_BUFFER_HEADER
35 #define GRT_CIRCULAR_BUFFER_HEADER
36 #include <iostream>
37 #include <vector>
38 #include "ErrorLog.h"
39 using namespace std;
40 
41 namespace GRT{
42 
43 template <class T>
45  public:
46 
51  errorLog.setProceedingText("[ERROR: CircularBuffer]");
52  bufferSize = 0;
53  numValuesInBuffer = 0;
54  readPtr = 0;
55  writePtr = 0;
56  bufferInit = false;
57  }
58 
65  errorLog.setProceedingText("[ERROR: CircularBuffer]");
66  this->bufferSize = 0;
67  this->numValuesInBuffer = 0;
68  this->readPtr = 0;
69  this->writePtr = 0;
70  this->bufferInit = false;
71 
72  if( rhs.bufferInit ){
73  this->bufferInit = rhs.bufferInit;
74  this->bufferSize = rhs.bufferSize;
75  this->numValuesInBuffer = rhs.numValuesInBuffer;
76  this->buffer.resize( rhs.bufferSize );
77  for(unsigned int i=0; i<rhs.bufferSize; i++){
78  this->buffer[i] = rhs.buffer[ i ];
79  }
80  this->readPtr = rhs.readPtr;
81  this->writePtr = rhs.writePtr;
82  }
83  }
84 
90  CircularBuffer(unsigned int bufferSize){
91  errorLog.setProceedingText("[ERROR: CircularBuffer]");
92  bufferInit = false;
93  resize(bufferSize);
94  }
95 
99  virtual ~CircularBuffer(){
100  if( bufferInit ){
101  clear();
102  }
103  }
104 
112  if( this != &rhs ){
113  this->clear();
114 
115  if( rhs.bufferInit ){
116  this->bufferInit = rhs.bufferInit;
117  this->bufferSize = rhs.bufferSize;
118  this->numValuesInBuffer = rhs.numValuesInBuffer;
119  this->buffer.resize( rhs.bufferSize );
120  for(unsigned int i=0; i<rhs.bufferSize; i++){
121  this->buffer[i] = rhs.buffer[ i ];
122  }
123  this->readPtr = rhs.readPtr;
124  this->writePtr = rhs.writePtr;
125  }
126  }
127  return *this;
128  }
129 
136  inline T& operator[](const unsigned int &index){
137  return buffer[ (readPtr + index) % bufferSize ];
138  }
139 
146  inline const T& operator[](const unsigned int &index) const{
147  return buffer[ (readPtr + index) % bufferSize ];
148  }
149 
156  inline T& operator()(const unsigned int &index){
157  return buffer[ index ];
158  }
159 
166  inline const T& operator()(const unsigned int &index) const{
167  return buffer[ index ];
168  }
169 
176  bool resize(const unsigned int newBufferSize){
177  return resize(newBufferSize,T());
178  }
179 
187  bool resize(const unsigned int newBufferSize,const T &defaultValue){
188 
189  //Cleanup the old memory
190  clear();
191 
192  if( newBufferSize == 0 ) return false;
193 
194  //Setup the memory for the new buffer
195  bufferSize = newBufferSize;
196  buffer.resize(bufferSize,defaultValue);
197  numValuesInBuffer = 0;
198  readPtr = 0;
199  writePtr = 0;
200 
201  //Flag that the filter has been initialised
202  bufferInit = true;
203 
204  return true;
205  }
206 
213  bool push_back(const T &value){
214 
215  if( !bufferInit ){
216  errorLog << "Can't push_back value to circular buffer as the buffer has not been initialized!" << endl;
217  return false;
218  }
219 
220  //Add the value to the buffer
221  buffer[ writePtr ] = value;
222 
223  //Update the write pointer
224  writePtr++;
225  writePtr = writePtr % bufferSize;
226 
227  //Check if the buffer is full
228  if( ++numValuesInBuffer > bufferSize ){
229  numValuesInBuffer = bufferSize;
230 
231  //Only update the read pointer if the buffer has been filled
232  readPtr++;
233  readPtr = readPtr % bufferSize;
234  }
235 
236  return true;
237  }
238 
245  bool setAllValues(const T &value){
246  if( !bufferInit ){
247  return false;
248  }
249 
250  for(unsigned int i=0; i<bufferSize; i++){
251  buffer[i] = value;
252  }
253 
254  return true;
255  }
256 
261  bool reset(){
262  numValuesInBuffer = 0;
263  readPtr = 0;
264  writePtr = 0;
265  return true;
266  }
267 
271  void clear(){
272  numValuesInBuffer = 0;
273  readPtr = 0;
274  writePtr = 0;
275  buffer.clear();
276  bufferInit = false;
277  }
278 
284  vector< T > getDataAsVector() const{
285  if( bufferInit ){
286  vector< T > data( bufferSize );
287  for(unsigned int i=0; i<bufferSize; i++){
288  data[i] = (*this)[i];
289  }
290  return data;
291  }
292  return vector< T >();
293  }
294 
300  bool getInit() const { return bufferInit; }
301 
307  bool getBufferFilled() const { return bufferInit ? numValuesInBuffer==bufferSize : false; }
308 
314  unsigned int getSize() const { return bufferInit ? bufferSize : 0; }
315 
321  unsigned int getNumValuesInBuffer() const { return bufferInit ? numValuesInBuffer : 0; }
322 
328  unsigned int getReadPointerPosition() const { return bufferInit ? readPtr : 0; }
329 
335  unsigned int getWritePointerPosition() const { return bufferInit ? writePtr : 0; }
336 
337  T getBack() const {
338  if( !bufferInit ) return T();
339  return buffer[ (readPtr + numValuesInBuffer - 1) % bufferSize ];
340  }
341 
342 protected:
343  unsigned int bufferSize;
344  unsigned int numValuesInBuffer;
345  unsigned int readPtr;
346  unsigned int writePtr;
347  vector< T > buffer;
348  bool bufferInit;
349 
350  ErrorLog errorLog;
351 };
352 
353 }//End of namespace GRT
354 
355 #endif //GRT_CIRCULAR_BUFFER_HEADER
T & operator[](const unsigned int &index)
bool resize(const unsigned int newBufferSize, const T &defaultValue)
virtual ~CircularBuffer()
CircularBuffer(const CircularBuffer &rhs)
Definition: AdaBoost.cpp:25
bool push_back(const T &value)
bool setAllValues(const T &value)
bool getInit() const
bool resize(const unsigned int newBufferSize)
unsigned int getSize() const
vector< T > getDataAsVector() const
unsigned int getNumValuesInBuffer() const
bool getBufferFilled() const
unsigned int getReadPointerPosition() const
CircularBuffer & operator=(const CircularBuffer &rhs)
const T & operator[](const unsigned int &index) const
unsigned int getWritePointerPosition() const
CircularBuffer(unsigned int bufferSize)
T & operator()(const unsigned int &index)
const T & operator()(const unsigned int &index) const