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_
00107 
00108 /*
00109  * Change log :
00110  * 
00111  * $Log: RingBufferWithSum.h,v $
00112  * Revision 1.1.1.1  2004/05/22 17:35:55  cvsadm
00113  * created new repository GT2004_WM
00114  *
00115  * Revision 1.2  2003/10/29 13:17:16  juengel
00116  * bug fixed
00117  *
00118  * Revision 1.1  2003/10/07 10:13:21  cvsadm
00119  * Created GT2004 (M.J.)
00120  *
00121  * Revision 1.2  2003/08/08 11:37:30  dueffert
00122  * doxygen docu corrected
00123  *
00124  * Revision 1.1.1.1  2003/07/02 09:40:28  cvsadm
00125  * created new repository for the competitions in Padova from the 
00126  * tamara CVS (Tuesday 2:00 pm)
00127  *
00128  * removed unused solutions
00129  *
00130  * Revision 1.3  2003/06/27 15:09:14  juengel
00131  * Added getMinimum().
00132  *
00133  * Revision 1.2  2003/06/21 09:48:36  dueffert
00134  * useless const warning removed
00135  *
00136  * Revision 1.1  2003/06/20 17:56:21  juengel
00137  * Added RingBufferWithSum.
00138  *
00139  */

Generated on Thu Sep 23 19:57:41 2004 for GT2004 by doxygen 1.3.6