PointerHashTable.hpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: HashTable.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_POINTERHASHTABLE_HPP 00044 #define VITOOKI_PS_POINTERHASHTABLE_HPP 00045 #include <assert.h> 00060 template<class T> class PointerHashTable 00061 { 00063 std::list<T> invalid; 00064 std::list<T>* data; 00066 int size; 00067 unsigned int* keys; 00068 00069 public: 00070 PointerHashTable(int siz) { 00071 size=siz; 00072 data=new std::list<T>[size]; 00073 keys=new unsigned int[size]; 00074 }; 00075 ~PointerHashTable() { 00076 for(unsigned int i=0;i<size;i++) { 00077 freeElementsForPos(i); 00078 } 00079 delete[] data; delete[] keys; 00080 }; 00081 00082 void put(T& elem,unsigned int key) { 00083 unsigned int pos=key%size; 00084 assert(elem != NULL); 00085 // if we receive another element that maps to the same pos but has 00086 // a different key then we delete it! 00087 if(!data[pos].empty() && keys[pos]!=key) 00088 freeElementsForKey(pos); 00089 00090 data[pos].push_back(elem); 00091 }; 00092 00093 std::list<T>& get(unsigned int key) { 00094 unsigned int pos=key%size; 00095 if(keys[pos]==key) 00096 return data[pos]; 00097 return invalid; 00098 }; 00099 00100 void unlinkElementsForKey(const unsigned int key) { 00101 unsigned int pos=key%size; 00102 if(!data[pos].empty()) { 00103 list<T>::iterator li; 00104 while(!data[pos].empty()) { 00105 data[pos].pop_front(); 00106 } 00107 } 00108 } 00109 void freeElementsForKey(const unsigned int key) { 00110 unsigned int pos=key%size; 00111 if(keys[pos]==key) 00112 freeElementsForPos(pos); 00113 } 00114 00115 protected: 00117 void freeElementsForPos(const unsigned int p) { 00118 unsigned int pos=p%size; 00119 if(!data[pos].empty()) { 00120 list<T>::iterator li; 00121 while(!data[pos].empty()) { 00122 li=data[pos].begin(); 00123 if(*li) 00124 delete (*li); 00125 data[pos].pop_front(); 00126 } 00127 } 00128 }; 00129 00130 }; 00131 #endif