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@3:1a33490e1990, 2012-08-06 (annotated)
- Committer:
- UCSBRobotics
- Date:
- Mon Aug 06 06:13:11 2012 +0000
- Revision:
- 3:1a33490e1990
- Parent:
- 2:4080c1770d51
- Child:
- 4:13b626890ad4
Added comments and switched to standard mbed brace style (K&R)
Who changed what in which revision?
| User | Revision | Line number | New 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 | 3:1a33490e1990 | 7 | class CircularBuffer { |
| UCSBRobotics | 1:b24970e4c038 | 8 | public: |
| UCSBRobotics | 3:1a33490e1990 | 9 | CircularBuffer() { |
| UCSBRobotics | 3:1a33490e1990 | 10 | headIndex = 0; |
| UCSBRobotics | 3:1a33490e1990 | 11 | } |
| UCSBRobotics | 1:b24970e4c038 | 12 | T read(int index); |
| UCSBRobotics | 1:b24970e4c038 | 13 | T& operator[](int index); |
| UCSBRobotics | 1:b24970e4c038 | 14 | void write(T value, int index); |
| UCSBRobotics | 1:b24970e4c038 | 15 | void push(T value); |
| UCSBRobotics | 1:b24970e4c038 | 16 | void revert(int amount); |
| UCSBRobotics | 3:1a33490e1990 | 17 | |
| UCSBRobotics | 1:b24970e4c038 | 18 | protected: |
| UCSBRobotics | 1:b24970e4c038 | 19 | T data[S]; |
| UCSBRobotics | 3:1a33490e1990 | 20 | int headIndex; |
| UCSBRobotics | 2:4080c1770d51 | 21 | inline int getRealIndex(int i); |
| UCSBRobotics | 1:b24970e4c038 | 22 | }; |
| UCSBRobotics | 1:b24970e4c038 | 23 | |
| UCSBRobotics | 1:b24970e4c038 | 24 | |
| UCSBRobotics | 1:b24970e4c038 | 25 | |
| UCSBRobotics | 3:1a33490e1990 | 26 | /* getRealIndex() |
| UCSBRobotics | 3:1a33490e1990 | 27 | * |
| UCSBRobotics | 3:1a33490e1990 | 28 | * Translates head-based indices into absolute indices. |
| UCSBRobotics | 3:1a33490e1990 | 29 | * Private function used by other members. |
| UCSBRobotics | 3:1a33490e1990 | 30 | */ |
| UCSBRobotics | 1:b24970e4c038 | 31 | template <typename T, int S> |
| UCSBRobotics | 3:1a33490e1990 | 32 | inline int CircularBuffer<T, S>::getRealIndex(int index) { |
| UCSBRobotics | 3:1a33490e1990 | 33 | int realIndex = (headIndex - index) % S; |
| UCSBRobotics | 2:4080c1770d51 | 34 | if (realIndex < 0) realIndex += S; |
| UCSBRobotics | 2:4080c1770d51 | 35 | return realIndex; |
| UCSBRobotics | 2:4080c1770d51 | 36 | } |
| UCSBRobotics | 2:4080c1770d51 | 37 | |
| UCSBRobotics | 2:4080c1770d51 | 38 | |
| UCSBRobotics | 2:4080c1770d51 | 39 | |
| UCSBRobotics | 3:1a33490e1990 | 40 | /* read(int index) |
| UCSBRobotics | 3:1a33490e1990 | 41 | * |
| UCSBRobotics | 3:1a33490e1990 | 42 | * Returns the object stored at a given index. |
| UCSBRobotics | 3:1a33490e1990 | 43 | */ |
| UCSBRobotics | 2:4080c1770d51 | 44 | template <typename T, int S> |
| UCSBRobotics | 3:1a33490e1990 | 45 | inline T CircularBuffer<T, S>::read(int index) { |
| UCSBRobotics | 2:4080c1770d51 | 46 | return data[getRealIndex(index)]; |
| UCSBRobotics | 1:b24970e4c038 | 47 | } |
| UCSBRobotics | 1:b24970e4c038 | 48 | |
| UCSBRobotics | 1:b24970e4c038 | 49 | |
| UCSBRobotics | 1:b24970e4c038 | 50 | |
| UCSBRobotics | 3:1a33490e1990 | 51 | /* operator[int index] |
| UCSBRobotics | 3:1a33490e1990 | 52 | * |
| UCSBRobotics | 3:1a33490e1990 | 53 | * Returns a reference to the object stored at a given index. |
| UCSBRobotics | 3:1a33490e1990 | 54 | */ |
| UCSBRobotics | 1:b24970e4c038 | 55 | template <typename T, int S> |
| UCSBRobotics | 3:1a33490e1990 | 56 | inline T& CircularBuffer<T, S>::operator[](int index) { |
| UCSBRobotics | 2:4080c1770d51 | 57 | return data[getRealIndex(index)]; |
| UCSBRobotics | 1:b24970e4c038 | 58 | } |
| UCSBRobotics | 1:b24970e4c038 | 59 | |
| UCSBRobotics | 1:b24970e4c038 | 60 | |
| UCSBRobotics | 1:b24970e4c038 | 61 | |
| UCSBRobotics | 3:1a33490e1990 | 62 | /* write(T value, int index) |
| UCSBRobotics | 3:1a33490e1990 | 63 | * |
| UCSBRobotics | 3:1a33490e1990 | 64 | * Sets the value of the object stored at the given index. |
| UCSBRobotics | 3:1a33490e1990 | 65 | */ |
| UCSBRobotics | 1:b24970e4c038 | 66 | template <typename T, int S> |
| UCSBRobotics | 3:1a33490e1990 | 67 | inline void CircularBuffer<T, S>::write(T value, int index) { |
| UCSBRobotics | 2:4080c1770d51 | 68 | data[getRealIndex(index)] = value; |
| UCSBRobotics | 1:b24970e4c038 | 69 | } |
| UCSBRobotics | 1:b24970e4c038 | 70 | |
| UCSBRobotics | 1:b24970e4c038 | 71 | |
| UCSBRobotics | 1:b24970e4c038 | 72 | |
| UCSBRobotics | 3:1a33490e1990 | 73 | /* push(T value) |
| UCSBRobotics | 3:1a33490e1990 | 74 | * |
| UCSBRobotics | 3:1a33490e1990 | 75 | * Pushes the given value to the head of the buffer. |
| UCSBRobotics | 3:1a33490e1990 | 76 | * Moves the head forward. |
| UCSBRobotics | 3:1a33490e1990 | 77 | */ |
| UCSBRobotics | 1:b24970e4c038 | 78 | template <typename T, int S> |
| UCSBRobotics | 3:1a33490e1990 | 79 | inline void CircularBuffer<T, S>::push(T value) { |
| UCSBRobotics | 3:1a33490e1990 | 80 | headIndex = ++headIndex % S; |
| UCSBRobotics | 3:1a33490e1990 | 81 | data[headIndex] = value; |
| UCSBRobotics | 1:b24970e4c038 | 82 | } |
| UCSBRobotics | 1:b24970e4c038 | 83 | |
| UCSBRobotics | 1:b24970e4c038 | 84 | |
| UCSBRobotics | 1:b24970e4c038 | 85 | |
| UCSBRobotics | 3:1a33490e1990 | 86 | /* revert(int amount) |
| UCSBRobotics | 3:1a33490e1990 | 87 | * |
| UCSBRobotics | 3:1a33490e1990 | 88 | * Pushes the head back by a given amount. |
| UCSBRobotics | 3:1a33490e1990 | 89 | */ |
| UCSBRobotics | 1:b24970e4c038 | 90 | template <typename T, int S> |
| UCSBRobotics | 3:1a33490e1990 | 91 | inline void CircularBuffer<T, S>::revert(int amount) { |
| UCSBRobotics | 3:1a33490e1990 | 92 | headIndex = headIndex >= amount ? headIndex - amount : headIndex - amount + S; |
| UCSBRobotics | 1:b24970e4c038 | 93 | } |
| UCSBRobotics | 1:b24970e4c038 | 94 | |
| UCSBRobotics | 2:4080c1770d51 | 95 | |
| UCSBRobotics | 2:4080c1770d51 | 96 | |
| UCSBRobotics | 0:787a5401f57e | 97 | #endif // CIRCULARBUFFER_H |
