Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members
FastPointerHashTable.hpp
00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: FastPointerHashTable.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_FASTPOINTERHASHTABLE_HPP 00044 #define VITOOKI_PS_FASTPOINTERHASHTABLE_HPP 00045 00046 #include <assert.h> 00060 template<class T> class FastPointerHashTable 00061 { 00062 T invalid; 00063 T* data; 00065 unsigned int size; 00066 unsigned int* keys; 00067 00068 public: 00069 FastPointerHashTable(unsigned int siz) { 00070 invalid=NULL; 00071 size=siz; 00072 data=new T[size]; 00073 memset(data,0,sizeof(T)*size); // init to NULL 00074 keys=new unsigned int[size]; 00075 memset(keys,0u,sizeof(unsigned int)*size); // init to 0 00076 // 0 is an invalid key for all elements but the first (due to the modulo mapping) 00077 keys[0]=1u; 00078 }; 00079 00080 ~FastPointerHashTable() { 00081 freeAllElements(); 00082 delete[] data; delete[] keys; 00083 }; 00084 00088 void put(T& elem,unsigned int key) { 00089 unsigned int pos=key%size; 00090 assert(elem != NULL); 00091 if(elem==NULL) 00092 return; 00093 // if we receive another element that maps to the same pos but has 00094 // a different key then we delete it! 00095 if(data[pos]) 00096 delete data[pos]; 00097 data[pos]=elem; 00098 keys[pos]=key; 00099 }; 00100 00105 T& get(unsigned int key) { 00106 unsigned int pos=key%size; 00107 if(keys[pos]==key) 00108 return data[pos]; 00109 return invalid; 00110 }; 00111 00112 void freeElement(const unsigned int key) { 00113 unsigned int pos=key%size; 00114 if(keys[pos]==key) { 00115 keys[pos]=key+1; // invalid key which can never be found at this position due to modulo operation 00116 if(data[pos]) 00117 delete data[pos]; 00118 data[pos]=NULL; 00119 } 00120 }; 00121 00122 void freeAllElements() { 00123 for(unsigned int i=0;i<size;i++) { 00124 if(data[i]) { 00125 delete data[i]; 00126 data[i]=NULL; 00127 } 00128 keys[i]=i+1; 00129 } 00130 }; 00131 00132 void unlinkElement(const unsigned int key) { 00133 unsigned int pos=key%size; 00134 if(keys[pos]==key) { 00135 keys[pos]=key+1; // invalid key which can never be found at this position due to modulo operation 00136 data[pos]=NULL; 00137 } 00138 }; 00139 }; 00140 #endif