PointerRingBuffer.hpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: PointerRingBuffer.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_POINTERRINGBUFFER_HPP 00044 #define VITOOKI_PS_POINTERRINGBUFFER_HPP 00045 00058 template<class T> class PointerRingBuffer{ 00059 T* data; 00060 int* keys; 00061 int startPos; 00062 int endPos; 00063 int size; 00064 public: 00065 PointerRingBuffer(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 ~PointerRingBuffer() { 00076 flushBuffer(); 00077 delete[] keys;delete[] data; 00078 }; 00079 00080 void flushBuffer() { 00081 for(int i=startPos;i<endPos;i++) { 00082 if(data[i%size]) { 00083 delete data[i%size]; 00084 } 00085 } 00086 startPos=endPos=0; 00087 memset(data,0,size*sizeof(T)); 00088 }; 00089 00090 void put(T& elem,int key){ 00091 if(startPos==0 && 0==endPos) { 00092 // this happens exactly only once! During the first insert. 00093 // in all other cases endPos will be always larger than startPos 00094 data[startPos]=elem; 00095 keys[startPos]=key; 00096 endPos++; 00097 } 00098 else { 00099 assert(endPos-startPos<=size); // > should never happen 00100 if(endPos-startPos==size) { 00101 // we are full, delete object? 00102 if(data[startPos%size]) { 00103 delete data[startPos%size]; 00104 data[startPos%size]=NULL; 00105 } 00106 startPos++; 00107 } 00108 // now insert the element 00109 data[endPos%size]=elem; 00110 keys[endPos%size]=key; 00111 endPos++; 00112 } 00113 }; 00114 00118 T get(int key) { 00119 // endpos always points to the next insert pos 00120 for(int i=startPos;i<endPos;i++) { 00121 if(keys[i%size]==key) { 00122 T dummy=data[i%size]; 00123 data[i%size]=NULL; 00124 keys[i%size]=0; 00125 return dummy; 00126 } 00127 } 00128 return NULL; 00129 }; 00130 }; 00131 #endif