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@2:4080c1770d51, 2012-08-05 (annotated)
- Committer:
 - UCSBRobotics
 - Date:
 - Sun Aug 05 22:18:39 2012 +0000
 - Revision:
 - 2:4080c1770d51
 - Parent:
 - 1:b24970e4c038
 - Child:
 - 3:1a33490e1990
 
Moved duplicated index calculating code into a private function
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 | 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 | 2:4080c1770d51 | 20 | inline int getRealIndex(int i); | 
| UCSBRobotics | 1:b24970e4c038 | 21 | }; | 
| UCSBRobotics | 1:b24970e4c038 | 22 | |
| UCSBRobotics | 1:b24970e4c038 | 23 | |
| UCSBRobotics | 1:b24970e4c038 | 24 | |
| UCSBRobotics | 1:b24970e4c038 | 25 | template <typename T, int S> | 
| UCSBRobotics | 2:4080c1770d51 | 26 | inline int CircularBuffer<T, S>::getRealIndex(int index) | 
| UCSBRobotics | 2:4080c1770d51 | 27 | { | 
| UCSBRobotics | 2:4080c1770d51 | 28 | int realIndex = (currentIndex - index) % S; | 
| UCSBRobotics | 2:4080c1770d51 | 29 | if (realIndex < 0) realIndex += S; | 
| UCSBRobotics | 2:4080c1770d51 | 30 | return realIndex; | 
| UCSBRobotics | 2:4080c1770d51 | 31 | } | 
| UCSBRobotics | 2:4080c1770d51 | 32 | |
| UCSBRobotics | 2:4080c1770d51 | 33 | |
| UCSBRobotics | 2:4080c1770d51 | 34 | |
| UCSBRobotics | 2:4080c1770d51 | 35 | template <typename T, int S> | 
| UCSBRobotics | 1:b24970e4c038 | 36 | inline T CircularBuffer<T, S>::read(int index) | 
| UCSBRobotics | 1:b24970e4c038 | 37 | { | 
| UCSBRobotics | 2:4080c1770d51 | 38 | return data[getRealIndex(index)]; | 
| UCSBRobotics | 1:b24970e4c038 | 39 | } | 
| UCSBRobotics | 1:b24970e4c038 | 40 | |
| UCSBRobotics | 1:b24970e4c038 | 41 | |
| UCSBRobotics | 1:b24970e4c038 | 42 | |
| UCSBRobotics | 1:b24970e4c038 | 43 | template <typename T, int S> | 
| UCSBRobotics | 1:b24970e4c038 | 44 | inline T& CircularBuffer<T, S>::operator[](int index) | 
| UCSBRobotics | 1:b24970e4c038 | 45 | { | 
| 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 | 1:b24970e4c038 | 51 | template <typename T, int S> | 
| UCSBRobotics | 1:b24970e4c038 | 52 | inline void CircularBuffer<T, S>::write(T value, int index) | 
| UCSBRobotics | 1:b24970e4c038 | 53 | { | 
| UCSBRobotics | 2:4080c1770d51 | 54 | data[getRealIndex(index)] = value; | 
| UCSBRobotics | 1:b24970e4c038 | 55 | } | 
| UCSBRobotics | 1:b24970e4c038 | 56 | |
| UCSBRobotics | 1:b24970e4c038 | 57 | |
| UCSBRobotics | 1:b24970e4c038 | 58 | |
| UCSBRobotics | 1:b24970e4c038 | 59 | template <typename T, int S> | 
| UCSBRobotics | 1:b24970e4c038 | 60 | inline void CircularBuffer<T, S>::push(T value) | 
| UCSBRobotics | 1:b24970e4c038 | 61 | { | 
| UCSBRobotics | 1:b24970e4c038 | 62 | currentIndex = ++currentIndex % S; | 
| UCSBRobotics | 1:b24970e4c038 | 63 | data[currentIndex] = value; | 
| UCSBRobotics | 1:b24970e4c038 | 64 | } | 
| UCSBRobotics | 1:b24970e4c038 | 65 | |
| UCSBRobotics | 1:b24970e4c038 | 66 | |
| UCSBRobotics | 1:b24970e4c038 | 67 | |
| UCSBRobotics | 1:b24970e4c038 | 68 | template <typename T, int S> | 
| UCSBRobotics | 1:b24970e4c038 | 69 | inline void CircularBuffer<T, S>::revert(int amount) | 
| UCSBRobotics | 1:b24970e4c038 | 70 | { | 
| UCSBRobotics | 1:b24970e4c038 | 71 | currentIndex = currentIndex >= amount ? currentIndex - amount : currentIndex - amount + S; | 
| UCSBRobotics | 1:b24970e4c038 | 72 | } | 
| UCSBRobotics | 1:b24970e4c038 | 73 | |
| UCSBRobotics | 2:4080c1770d51 | 74 | |
| UCSBRobotics | 2:4080c1770d51 | 75 | |
| UCSBRobotics | 0:787a5401f57e | 76 | #endif // CIRCULARBUFFER_H | 
