fasdf gfaha / CircularBuffer
Committer:
UCSBRobotics
Date:
Sun Aug 05 08:15:32 2012 +0000
Revision:
1:b24970e4c038
Parent:
0:787a5401f57e
Child:
2:4080c1770d51
Added the ability to write to arbitrary locations in the buffer and overloaded the [] operator.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UCSBRobotics 1:b24970e4c038 1 #ifndef CIRCULARBUFFER_H
UCSBRobotics 1:b24970e4c038 2 #define CIRCULARBUFFER_H
UCSBRobotics 1:b24970e4c038 3
UCSBRobotics 1:b24970e4c038 4
UCSBRobotics 1:b24970e4c038 5
UCSBRobotics 1:b24970e4c038 6 template <typename T, int S>
UCSBRobotics 1:b24970e4c038 7 class CircularBuffer
UCSBRobotics 1:b24970e4c038 8 {
UCSBRobotics 1:b24970e4c038 9 public:
UCSBRobotics 1:b24970e4c038 10 CircularBuffer() { currentIndex = 0; }
UCSBRobotics 1:b24970e4c038 11 T read(int index);
UCSBRobotics 1:b24970e4c038 12 T& operator[](int index);
UCSBRobotics 1:b24970e4c038 13 void write(T value, int index);
UCSBRobotics 1:b24970e4c038 14 void push(T value);
UCSBRobotics 1:b24970e4c038 15 void revert(int amount);
UCSBRobotics 1:b24970e4c038 16
UCSBRobotics 1:b24970e4c038 17 protected:
UCSBRobotics 1:b24970e4c038 18 T data[S];
UCSBRobotics 1:b24970e4c038 19 int currentIndex;
UCSBRobotics 1:b24970e4c038 20 };
UCSBRobotics 1:b24970e4c038 21
UCSBRobotics 1:b24970e4c038 22
UCSBRobotics 1:b24970e4c038 23
UCSBRobotics 1:b24970e4c038 24 template <typename T, int S>
UCSBRobotics 1:b24970e4c038 25 inline T CircularBuffer<T, S>::read(int index)
UCSBRobotics 1:b24970e4c038 26 {
UCSBRobotics 1:b24970e4c038 27 int realIndex = (currentIndex - index) % S;
UCSBRobotics 1:b24970e4c038 28 if (realIndex < 0) realIndex += S;
UCSBRobotics 1:b24970e4c038 29 return data[realIndex];
UCSBRobotics 1:b24970e4c038 30 }
UCSBRobotics 1:b24970e4c038 31
UCSBRobotics 1:b24970e4c038 32
UCSBRobotics 1:b24970e4c038 33
UCSBRobotics 1:b24970e4c038 34 template <typename T, int S>
UCSBRobotics 1:b24970e4c038 35 inline T& CircularBuffer<T, S>::operator[](int index)
UCSBRobotics 1:b24970e4c038 36 {
UCSBRobotics 1:b24970e4c038 37 int realIndex = (currentIndex - index) % S;
UCSBRobotics 1:b24970e4c038 38 if (realIndex < 0) realIndex += S;
UCSBRobotics 1:b24970e4c038 39 return data[realIndex];
UCSBRobotics 1:b24970e4c038 40 }
UCSBRobotics 1:b24970e4c038 41
UCSBRobotics 1:b24970e4c038 42
UCSBRobotics 1:b24970e4c038 43
UCSBRobotics 1:b24970e4c038 44 template <typename T, int S>
UCSBRobotics 1:b24970e4c038 45 inline void CircularBuffer<T, S>::write(T value, int index)
UCSBRobotics 1:b24970e4c038 46 {
UCSBRobotics 1:b24970e4c038 47 int realIndex = (currentIndex - index) % S;
UCSBRobotics 1:b24970e4c038 48 if (realIndex < 0) realIndex += S;
UCSBRobotics 1:b24970e4c038 49 data[realIndex] = value;
UCSBRobotics 1:b24970e4c038 50 }
UCSBRobotics 1:b24970e4c038 51
UCSBRobotics 1:b24970e4c038 52
UCSBRobotics 1:b24970e4c038 53
UCSBRobotics 1:b24970e4c038 54 template <typename T, int S>
UCSBRobotics 1:b24970e4c038 55 inline void CircularBuffer<T, S>::push(T value)
UCSBRobotics 1:b24970e4c038 56 {
UCSBRobotics 1:b24970e4c038 57 currentIndex = ++currentIndex % S;
UCSBRobotics 1:b24970e4c038 58 data[currentIndex] = value;
UCSBRobotics 1:b24970e4c038 59 }
UCSBRobotics 1:b24970e4c038 60
UCSBRobotics 1:b24970e4c038 61
UCSBRobotics 1:b24970e4c038 62
UCSBRobotics 1:b24970e4c038 63 template <typename T, int S>
UCSBRobotics 1:b24970e4c038 64 inline void CircularBuffer<T, S>::revert(int amount)
UCSBRobotics 1:b24970e4c038 65 {
UCSBRobotics 1:b24970e4c038 66 currentIndex = currentIndex >= amount ? currentIndex - amount : currentIndex - amount + S;
UCSBRobotics 1:b24970e4c038 67 }
UCSBRobotics 1:b24970e4c038 68
UCSBRobotics 0:787a5401f57e 69 #endif // CIRCULARBUFFER_H