Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members
RingBuffer.hpp
00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: RingBuffer.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 #ifndef VITOOKI_PS_RINGBUFFER_HPP 00044 #define VITOOKI_PS_RINGBUFFER_HPP 00045 00057 template<class T> class RingBuffer{ 00058 T invalid; 00059 T* data; 00060 int* keys; 00061 int startPos; 00062 int endPos; 00063 int size; 00064 public: 00065 RingBuffer(int siz) { 00066 size=siz; 00067 data=new T[size]; 00068 keys=new int[size]; 00069 startPos=endPos=0; 00070 // set to NULL 00071 memset(data,0,size*sizeof(T)); 00072 }; 00073 00075 ~RingBuffer() { 00076 delete[] keys;delete[] data; 00077 }; 00078 00079 void put(T& elem,int key){ 00080 if(startPos==0 && 0==endPos) { 00081 // this happens exactly only once! During the first insert. 00082 // in all other cases endPos will be always larger than startPos 00083 data[startPos]=elem; 00084 keys[startPos]=key; 00085 endPos++; 00086 } 00087 else { 00088 assert(endPos-startPos<=size); // > should never happen 00089 if(endPos-startPos==size) { 00090 // we are full, overwrite object 00091 startPos++; 00092 } 00093 // now insert the element 00094 data[endPos%size]=elem; 00095 keys[endPos%size]=key; 00096 endPos++; 00097 } 00098 }; 00099 00104 T get(int key, bool* found) { 00105 assert(found); 00106 // endpos always points to the next insert pos 00107 for(int i=startPos;i<endPos;i++) { 00108 if(keys[i%size]==key) { 00109 keys[i%size]=-1; 00110 *found=true; 00111 return data[i%size]; 00112 } 00113 } 00114 *found=false; 00115 return invalid; 00116 }; 00117 }; 00118 00119 #endif