Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ringbuffer.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2010 by Robot Group Leipzig *
3  * martius@informatik.uni-leipzig.de *
4  * der@informatik.uni-leipzig.de *
5  * *
6  * ANY COMMERCIAL USE FORBIDDEN! *
7  * LICENSE: *
8  * This work is licensed under the Creative Commons *
9  * Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of *
10  * this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
11  * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, *
12  * San Francisco, California, 94105, USA. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17  * *
18  * *
19  * *
20  ***************************************************************************/
21 #ifndef __RINGBUFFER_H
22 #define __RINGBUFFER_H
23 
24 #include <vector>
25 #include <assert.h>
26 
27 template <typename T>
28 class RingBuffer {
29 public:
31  :buffersize(0)
32  { }
33 
34  RingBuffer(int size)
35  : buffersize(size) {
36  buffer.resize(size);
37  }
38 
39  /// sets size of buffer and initializes buffer elements.
40  void init(int size, const T& t){
41  buffersize = size;
42  buffer.resize(size, t);
43  }
44 
45  int getBufferSize() const {
46  return buffersize;
47  }
48 
49  /** returns object at index.
50  Index can be larger than buffersize, it will be wrapped. Negative index means 0.
51  */
52  T& get(int index){
53  assert(buffersize>0);
54  if(index < 0) index=0;
55  return buffer[index%buffersize];
56  }
57 
58  const T& get(int index) const {
59  assert(buffersize>0);
60  if(index < 0) index=0;
61  return buffer[index%buffersize];
62  }
63 
64  /// see get()
65  T& operator[] (int index){
66  return get(index);
67  }
68  /// see get()
69  const T& operator[] (int index) const {
70  return get(index);
71  }
72 
73 protected:
74  std::vector<T> buffer;
76 };
77 
78 #endif
RingBuffer()
Definition: ringbuffer.h:30
void init(int size, const T &t)
sets size of buffer and initializes buffer elements.
Definition: ringbuffer.h:40
std::vector< T > buffer
Definition: ringbuffer.h:74
RingBuffer(int size)
Definition: ringbuffer.h:34
matrix::Matrix t(const std::vector< matrix::Matrix > &data, int time)
Definition: datafunc.h:102
int buffersize
Definition: ringbuffer.h:75
T & operator[](int index)
see get()
Definition: ringbuffer.h:65
int getBufferSize() const
Definition: ringbuffer.h:45
const int T
integer constant for use with exp function and (^) operator to transpose the matrix ...
Definition: matrix.cpp:21
Definition: ringbuffer.h:28