00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __RingBuffer_h_
00010 #define __RingBuffer_h_
00011
00012
00013
00014
00015
00016
00017 template <class V, int n> class RingBuffer
00018 {
00019 public:
00020
00021 RingBuffer() {init();}
00022
00023
00024
00025
00026 void init () {current = n - 1; numberOfEntries = 0;}
00027
00028
00029
00030
00031
00032 void add (const V& v)
00033 {
00034 current++;
00035 if (current==n) current=0;
00036 if (++numberOfEntries >= n) numberOfEntries = n;
00037 buffer[current] = v;
00038 }
00039
00040
00041
00042
00043
00044
00045 V& getEntry (int i)
00046 {
00047 int j = current - i;
00048 j %= n;
00049 if (j < 0) j += n;
00050 return buffer[j];
00051 }
00052
00053
00054
00055
00056
00057
00058 void updateEntry(const V& v, int i)
00059 {
00060 int j = current - i;
00061 j %= n;
00062 if (j < 0) j += n;
00063 buffer[j] = v;
00064 }
00065
00066
00067
00068
00069
00070
00071 V& operator[] (int i)
00072 {
00073 return getEntry(i);
00074 }
00075
00076
00077
00078
00079
00080
00081 const V& operator[] (int i) const
00082 {
00083 return buffer[i > current ? n + current - i : current - i];
00084 }
00085
00086 int getNumberOfEntries() const
00087 {
00088 return numberOfEntries;
00089 }
00090
00091 private:
00092 int current;
00093 int numberOfEntries;
00094 V buffer[n];
00095 };
00096
00097 #endif // __RingBuffer_h_