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