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

Tools/TRingBufferWithSum.h

Go to the documentation of this file.
00001 /**
00002  * @file TRingBufferWithSumLong.h
00003  *
00004  * Declaration of template class RingBufferWithSumLong
00005  *
00006  * @author Matthias Meyer
00007  */
00008 
00009 #ifndef __TRingBufferWithSum_h_
00010 #define __TRingBufferWithSum_h_
00011 
00012 #include <limits.h>
00013 
00014 /**
00015  * @class RingBufferWithSum
00016  */
00017 template <class C,int n> class TRingBufferWithSum
00018 {
00019   public:
00020     /** Constructor */
00021     TRingBufferWithSum() {init();}
00022 
00023     /**
00024      * initializes the RingBufferWithSum
00025      */
00026     void init () {current = n - 1; numberOfEntries = 0; sum = 0;}
00027 
00028     /**
00029      * adds an entry to the buffer
00030      * \param value value to be added
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      * returns an entry
00044      * \param i index of entry counting from last added (last=0,...)
00045      * \return a reference to the buffer entry
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];  // Initialize with first element
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      * returns an entry
00072      * \param i index of entry counting from last added (last=0,...)
00073      * \return a reference to the buffer entry
00074      */
00075     C operator[] (int i)
00076     {
00077       return getEntry(i);
00078     }
00079 
00080     /**
00081      * returns a constant entry.
00082      * \param i index of entry counting from last added (last=0,...)
00083      * \return a reference to the buffer entry
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 

Generated on Mon Mar 20 22:00:09 2006 for GT2005 by doxygen 1.3.6