Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Tools/RingBufferWithSum.h

Go to the documentation of this file.

    00001 /**
    00002  * @file RingBufferWithSum.h
    00003  *
    00004  * Declaration of class RingBufferWithSum
    00005  *
    00006  * @author Matthias Jüngel
    00007  */
    00008
    00009 #ifndef __RingBufferWithSum_h_
    00010 #define __RingBufferWithSum_h_
    00011
    00012 #include <limits.h>
    00013
    00014 /**
    00015  * @class RingBufferWithSum
    00016  *
    00017  * template class for cyclic buffering of the last n values of Type int
    00018  * and with a function that returns the sum of all entries
    00019  */
    00020 template <int n> class RingBufferWithSum
    00021 {
    00022   public:
    00023     /** Constructor */
    00024     RingBufferWithSum() {init();}
    00025
    00026     /**
    00027      * initializes the RingBufferWithSum
    00028      */
    00029     void init () {current = n - 1; numberOfEntries = 0; sum = 0;}
    00030
    00031     /**
    00032      * adds an entry to the buffer
    00033      * \param value value to be added
    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      * returns an entry
    00047      * \param i index of entry counting from last added (last=0,...)
    00048      * \return a reference to the buffer entry
    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      * returns an entry
    00075      * \param i index of entry counting from last added (last=0,...)
    00076      * \return a reference to the buffer entry
    00077      */
    00078     int operator[] (int i)
    00079     {
    00080       return getEntry(i);
    00081     }
    00082
    00083     /**
    00084      * returns a constant entry.
    00085      * \param i index of entry counting from last added (last=0,...)
    00086      * \return a reference to the buffer entry
    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_