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

Tools/RingBuffer.h

Go to the documentation of this file.
00001 /**
00002  * @file RingBuffer.h
00003  *
00004  * Declaration of class RingBuffer
00005  *
00006  * @author Max Risler
00007  */
00008 
00009 #ifndef __RingBuffer_h_
00010 #define __RingBuffer_h_
00011 
00012 /**
00013  * @class RingBuffer
00014  *
00015  * template class for cyclic buffering of the last n values of Type V
00016  */
00017 template <class V, int n> class RingBuffer
00018 {
00019   public:
00020     /** Constructor */
00021     RingBuffer() {init();}
00022 
00023     /**
00024      * initializes the Ringbuffer
00025      */
00026     void init () {current = n - 1; numberOfEntries = 0;}
00027 
00028     /**
00029      * adds an entry to the buffer
00030      * \param v value to be added
00031      */
00032     void add (const V& v) 
00033     {
00034       current++;
00035       if (current==n) current=0;
00036       if (++numberOfEntries >= n) numberOfEntries = n;
00037       buffer[current] = v;
00038     }
00039 
00040     /**
00041      * returns an entry
00042      * \param i index of entry counting from last added (last=0,...)
00043      * \return a reference to the buffer entry
00044      */
00045     V& getEntry (int i)
00046     {
00047       int j = current - i;
00048       j %= n;
00049       if (j < 0) j += n;
00050       return buffer[j];
00051    }
00052 
00053    /**
00054      * returns an entry
00055      * \param v the value the entry i shall be updated with
00056      * \param i index of entry counting from last added (last=0,...)
00057      */
00058     void updateEntry(const V& v, int i)
00059     {
00060       int j = current - i;
00061       j %= n;
00062       if (j < 0) j += n;
00063       buffer[j] = v;
00064    }
00065 
00066     /**
00067      * returns an entry
00068      * \param i index of entry counting from last added (last=0,...)
00069      * \return a reference to the buffer entry
00070      */
00071     V& operator[] (int i)
00072     {
00073       return getEntry(i);
00074     }
00075 
00076     /**
00077      * returns a constant entry.
00078      * \param i index of entry counting from last added (last=0,...)
00079      * \return a reference to the buffer entry
00080      */
00081     const V& operator[] (int i) const
00082     {
00083       return buffer[i > current ? n + current - i : current - i];
00084     }
00085 
00086     int getNumberOfEntries() const
00087     {
00088       return numberOfEntries;
00089     }
00090 
00091   private:
00092     int current;
00093     int numberOfEntries;
00094     V buffer[n];
00095 };
00096 
00097 #endif // __RingBuffer_h_
00098 
00099 /*
00100  * Change log :
00101  * 
00102  * $Log: RingBuffer.h,v $
00103  * Revision 1.2  2004/05/22 22:52:05  juengel
00104  * Renamed ballP_osition to ballModel.
00105  *
00106  * Revision 1.1.1.1  2004/05/22 17:35:55  cvsadm
00107  * created new repository GT2004_WM
00108  *
00109  * Revision 1.1  2003/10/07 10:13:21  cvsadm
00110  * Created GT2004 (M.J.)
00111  *
00112  * Revision 1.2  2003/08/08 11:37:30  dueffert
00113  * doxygen docu corrected
00114  *
00115  * Revision 1.1.1.1  2003/07/02 09:40:28  cvsadm
00116  * created new repository for the competitions in Padova from the 
00117  * tamara CVS (Tuesday 2:00 pm)
00118  *
00119  * removed unused solutions
00120  *
00121  * Revision 1.6  2003/06/24 11:36:12  jhoffman
00122  * added "updateEntry()"
00123  *
00124  * Revision 1.5  2003/04/06 20:56:39  risler
00125  * bug fix: wrong array-index was used in getEntry and operator[]
00126  *
00127  * Revision 1.4  2003/03/12 22:25:35  roefer
00128  * Access functions speeded up
00129  *
00130  * Revision 1.3  2002/11/27 13:51:55  dueffert
00131  * doxygen docu corrected
00132  *
00133  * Revision 1.2  2002/11/19 15:43:03  dueffert
00134  * doxygen comments corrected
00135  *
00136  * Revision 1.1  2002/09/10 15:53:58  cvsadm
00137  * Created new project GT2003 (M.L.)
00138  * - Cleaned up the /Src/DataTypes directory
00139  * - Removed challenge related source code
00140  * - Removed processing of incoming audio data
00141  * - Renamed AcousticMessage to SoundRequest
00142  *
00143  * Revision 1.2  2002/06/20 00:40:22  Thomas Röfer
00144  * WLan crash removed
00145  *
00146  * Revision 1.1.1.1  2002/05/10 12:40:32  cvsadm
00147  * Moved GT2002 Project from ute to tamara.
00148  *
00149  * Revision 1.4  2002/04/25 20:29:58  roefer
00150  * New BallPercept and BallP_osition, GTMath errors in SimGT2002 fixed
00151  *
00152  * Revision 1.3  2002/02/25 19:27:49  juengel
00153  * no message
00154  *
00155  * Revision 1.2  2002/02/21 16:29:43  risler
00156  * added constructor
00157  *
00158  * Revision 1.1  2002/02/03 09:32:54  risler
00159  * RingBuffer from DefaultSensorDataProcessor now is seperate class
00160  *
00161  *
00162  */

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