Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CircularBuffer.h
- Committer:
- UCSBRobotics
- Date:
- 2012-08-05
- Revision:
- 0:787a5401f57e
- Child:
- 1:b24970e4c038
File content as of revision 0:787a5401f57e:
#ifndef CIRCULARBUFFER_H
#define CIRCULARBUFFER_H
template <typename T, int S>
class CircularBuffer
{
public:
CircularBuffer() { index = 0; }
T value(int i);
void push(T value);
void revert(int amount);
protected:
T data[S];
int index;
};
template <typename T, int S>
inline T CircularBuffer<T, S>::value(int i)
{
int realIndex = (index - i) % S;
if (realIndex < 0) realIndex += S;
return data[realIndex];
}
template <typename T, int S>
inline void CircularBuffer<T, S>::push(T value)
{
index = ++index % S;
data[index] = value;
}
template <typename T, int S>
inline void CircularBuffer<T, S>::revert(int amount)
{
index = index >= amount ? index - amount : index - amount + S;
}
#endif // CIRCULARBUFFER_H
