GestureRecognitionToolkit  Version: 0.2.0
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 "GRTTypedefs.h"
39 #include "ErrorLog.h"
40 
41 GRT_BEGIN_NAMESPACE
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!" << std::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 
285  GRT_DEPRECATED_MSG("Use getData() instead!", std::vector< T > getDataAsVector() const );
286 
294  Vector< T > getData( const bool rawBuffer = false ) const{
295  if( bufferInit ){
296 
297  if( rawBuffer ){
298  return buffer; //Here we return the entire data buffer
299  }else{ //Here we return only valid elements and in order from oldest to newest
300  Vector< T > data( numValuesInBuffer );
301  for(unsigned int i=0; i<numValuesInBuffer; i++){
302  data[i] = (*this)[i]; //Gets the ordered element
303  }
304  return data;
305  }
306  }
307  return Vector< T >();
308  }
309 
315  bool getInit() const { return bufferInit; }
316 
322  bool getBufferFilled() const { return bufferInit ? numValuesInBuffer==bufferSize : false; }
323 
329  unsigned int getSize() const { return bufferInit ? bufferSize : 0; }
330 
336  unsigned int getNumValuesInBuffer() const { return bufferInit ? numValuesInBuffer : 0; }
337 
343  unsigned int getReadPointerPosition() const { return bufferInit ? readPtr : 0; }
344 
350  unsigned int getWritePointerPosition() const { return bufferInit ? writePtr : 0; }
351 
357  T getBack() const {
358  if( !bufferInit ) return T();
359  return buffer[ (readPtr + numValuesInBuffer - 1) % bufferSize ];
360  }
361 
362 protected:
363  bool bufferInit;
364  unsigned int bufferSize;
365  unsigned int numValuesInBuffer;
366  unsigned int readPtr;
367  unsigned int writePtr;
368  Vector< T > buffer;
369 
370  ErrorLog errorLog;
371 };
372 
373 //Deprecated function
374 template< class T > std::vector< T > CircularBuffer< T >::getDataAsVector() const{ return getData(); };
375 
376 GRT_END_NAMESPACE
377 
378 #endif //GRT_CIRCULAR_BUFFER_HEADER
bool push_back(const T &value)
const T & operator[](const unsigned int &index) const
bool getBufferFilled() const
CircularBuffer & operator=(const CircularBuffer &rhs)
T getBack() const
CircularBuffer(const CircularBuffer &rhs)
GRT_DEPRECATED_MSG("Use getData() instead!", std::vector< T > getDataAsVector() const )
unsigned int getWritePointerPosition() const
bool getInit() const
virtual ~CircularBuffer()
T & operator[](const unsigned int &index)
unsigned int getNumValuesInBuffer() const
unsigned int getReadPointerPosition() const
CircularBuffer(unsigned int bufferSize)
bool resize(const unsigned int newBufferSize, const T &defaultValue)
const T & operator()(const unsigned int &index) const
bool setAllValues(const T &value)
Definition: Vector.h:41
T & operator()(const unsigned int &index)
Vector< T > getData(const bool rawBuffer=false) const
unsigned int getSize() const
bool resize(const unsigned int newBufferSize)