fasdf gfaha / CircularBuffer
Committer:
UCSBRobotics
Date:
Sun Aug 05 07:52:40 2012 +0000
Revision:
0:787a5401f57e
Child:
1:b24970e4c038
Initial commit.; Basic circular buffer template is implemented and compiles.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UCSBRobotics 0:787a5401f57e 1 #ifndef CIRCULARBUFFER_H
UCSBRobotics 0:787a5401f57e 2 #define CIRCULARBUFFER_H
UCSBRobotics 0:787a5401f57e 3
UCSBRobotics 0:787a5401f57e 4
UCSBRobotics 0:787a5401f57e 5
UCSBRobotics 0:787a5401f57e 6 template <typename T, int S>
UCSBRobotics 0:787a5401f57e 7 class CircularBuffer
UCSBRobotics 0:787a5401f57e 8 {
UCSBRobotics 0:787a5401f57e 9 public:
UCSBRobotics 0:787a5401f57e 10 CircularBuffer() { index = 0; }
UCSBRobotics 0:787a5401f57e 11 T value(int i);
UCSBRobotics 0:787a5401f57e 12 void push(T value);
UCSBRobotics 0:787a5401f57e 13 void revert(int amount);
UCSBRobotics 0:787a5401f57e 14
UCSBRobotics 0:787a5401f57e 15 protected:
UCSBRobotics 0:787a5401f57e 16 T data[S];
UCSBRobotics 0:787a5401f57e 17 int index;
UCSBRobotics 0:787a5401f57e 18 };
UCSBRobotics 0:787a5401f57e 19
UCSBRobotics 0:787a5401f57e 20
UCSBRobotics 0:787a5401f57e 21
UCSBRobotics 0:787a5401f57e 22 template <typename T, int S>
UCSBRobotics 0:787a5401f57e 23 inline T CircularBuffer<T, S>::value(int i)
UCSBRobotics 0:787a5401f57e 24 {
UCSBRobotics 0:787a5401f57e 25 int realIndex = (index - i) % S;
UCSBRobotics 0:787a5401f57e 26 if (realIndex < 0) realIndex += S;
UCSBRobotics 0:787a5401f57e 27 return data[realIndex];
UCSBRobotics 0:787a5401f57e 28 }
UCSBRobotics 0:787a5401f57e 29
UCSBRobotics 0:787a5401f57e 30
UCSBRobotics 0:787a5401f57e 31
UCSBRobotics 0:787a5401f57e 32 template <typename T, int S>
UCSBRobotics 0:787a5401f57e 33 inline void CircularBuffer<T, S>::push(T value)
UCSBRobotics 0:787a5401f57e 34 {
UCSBRobotics 0:787a5401f57e 35 index = ++index % S;
UCSBRobotics 0:787a5401f57e 36 data[index] = value;
UCSBRobotics 0:787a5401f57e 37 }
UCSBRobotics 0:787a5401f57e 38
UCSBRobotics 0:787a5401f57e 39
UCSBRobotics 0:787a5401f57e 40
UCSBRobotics 0:787a5401f57e 41 template <typename T, int S>
UCSBRobotics 0:787a5401f57e 42 inline void CircularBuffer<T, S>::revert(int amount)
UCSBRobotics 0:787a5401f57e 43 {
UCSBRobotics 0:787a5401f57e 44 index = index >= amount ? index - amount : index - amount + S;
UCSBRobotics 0:787a5401f57e 45 }
UCSBRobotics 0:787a5401f57e 46
UCSBRobotics 0:787a5401f57e 47 #endif // CIRCULARBUFFER_H