00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __RingBufferWithSum_h_
00010 #define __RingBufferWithSum_h_
00011
00012 #include <limits.h>
00013
00014
00015
00016
00017
00018
00019
00020 template <int n> class RingBufferWithSum
00021 {
00022 public:
00023
00024 RingBufferWithSum() {init();}
00025
00026
00027
00028
00029 void init () {current = n - 1; numberOfEntries = 0; sum = 0;}
00030
00031
00032
00033
00034
00035 void add (int value)
00036 {
00037 if(numberOfEntries == n) sum -= getEntry(numberOfEntries - 1);
00038 sum += value;
00039 current++;
00040 if (current==n) current=0;
00041 if (++numberOfEntries >= n) numberOfEntries = n;
00042 buffer[current] = value;
00043 }
00044
00045
00046
00047
00048
00049
00050 int getEntry (int i)
00051 {
00052 int j = current - i;
00053 j %= n;
00054 if (j < 0) j += n;
00055 return buffer[j];
00056 }
00057
00058 int getSum()
00059 {
00060 return sum;
00061 }
00062
00063 int getMinimum()
00064 {
00065 int min = INT_MAX;
00066 for(int i = 0; i < numberOfEntries;i++)
00067 {
00068 if(buffer[i] < min) min = buffer[i];
00069 }
00070 return min;
00071 }
00072
00073
00074
00075
00076
00077
00078 int operator[] (int i)
00079 {
00080 return getEntry(i);
00081 }
00082
00083
00084
00085
00086
00087
00088 int operator[] (int i) const
00089 {
00090 return buffer[i > current ? n + current - i : current - i];
00091 }
00092
00093 int getNumberOfEntries() const
00094 {
00095 return numberOfEntries;
00096 }
00097
00098 private:
00099 int current;
00100 int numberOfEntries;
00101 int buffer[n];
00102
00103 int sum;
00104 };
00105
00106 #endif // __RingBufferWithSum_h_
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139