| VRS - The Virtual Rendering System |
| version 3.3 |
00001 /********************************************************************** 00002 VRS - The Virtual Rendering System 00003 Copyright (C) 2001 Computer Graphics Systems Group at the 00004 Hasso-Plattner-Institute, Potsdam, Germany. 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 This library is distributed in the hope that it will be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 See the GNU Lesser General Public License for more details. 00013 You should have received a copy of the GNU Lesser+ General Public 00014 License along with this library; if not, write to the FreeSoftware 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA, 02111-1307, USA. 00016 **********************************************************************/ 00017 // $Id: hashtable.h 6189 2007-12-17 07:43:04Z Steffen_Schmidt $ 00018 // $Date: 2007-12-17 08:43:04 +0100 (Mon, 17 Dec 2007) $ 00019 // $Revision: 6189 $ 00020 // $State$ 00021 // $Author: Steffen_Schmidt $ 00022 // 00023 // $Log$ 00024 // Revision 1.21 2005/07/12 12:53:12 baumann 00025 // 64 bit issue fixed (reinterpret_cast) 00026 // 00027 // Revision 1.20 2005/06/10 14:43:32 baumann 00028 // added hash code for std::wstring 00029 // 00030 // Revision 1.19 2004/11/09 15:04:55 kirsch 00031 // further changes to class comments due to doxygen 00032 // 00033 // Revision 1.18 2004/11/03 10:58:28 saar 00034 // Class comment changed (Doxygen style) 00035 // 00036 // Revision 1.17 2004/05/27 11:56:48 kirsch 00037 // - allow dictionary with less than 113 value-slots 00038 // - do not use pointer to std::vector<T> anymore 00039 // 00040 // Revision 1.16 2004/03/12 16:28:39 baumann 00041 // macros VRS_NAMESPACE_BEGIN/_END expanded and removed 00042 // 00043 // Revision 1.15 2004/01/27 13:39:46 buchholz 00044 // HashValue for strings moved here from config.h 00045 // HashValue for pairs added 00046 // 00047 // Revision 1.14 2004/01/19 11:43:57 baumann 00048 // serialization mechanism improved 00049 // 00050 // Revision 1.13 2003/11/07 14:19:24 kirsch 00051 // removed leading underscore from include guards 00052 // 00053 // Revision 1.12 2003/07/10 11:07:56 baumann 00054 // code cleanup 00055 // 00056 // Revision 1.11 2002/11/26 15:02:18 baumann 00057 // typo fixed 00058 // 00059 // Revision 1.10 2002/11/26 14:41:51 baumann 00060 // HashValue() functions rewritten; most recursions removed 00061 // 00062 // Revision 1.9 2002/11/25 10:00:03 baumann 00063 // changed return type of HashValue() to UINT 00064 // 00065 // Revision 1.8 2002/08/06 16:24:01 baumann 00066 // VRS_API removed for template classes 00067 // 00068 // Revision 1.7 2002/07/03 15:52:50 baumann 00069 // several changes for iterator code 00070 // most iterators are now serializable again 00071 // 00072 // Revision 1.6 2002/07/03 09:58:05 baumann 00073 // serialization added for iterator 00074 // added hash value functions for most of the builtin types 00075 // perfromance improvements for iterator 00076 // 00077 // Revision 1.5 2002/07/02 15:23:48 baumann 00078 // non-persistent containers now have the prefix NonPersistent, e.g. NonPersistentArray<T> 00079 // persistent containers now have no prefix, e.g. Array<T> 00080 // 00081 // Revision 1.4 2002/07/02 06:12:34 baumann 00082 // VRS::Iterator<T> completely rewritten 00083 // 00084 // Revision 1.3 2002/06/28 07:08:14 baumann 00085 // new method isEmpty() added 00086 // 00087 // Revision 1.2 2002/04/09 10:00:40 kersting 00088 // added dtor 00089 // 00090 // Revision 1.1 2002/03/04 12:03:54 kersting 00091 // directory structure changes 00092 // 00093 // Revision 1.12 2002/03/03 21:44:24 kosta 00094 // VRS_TYPEINFO-macro rewritten 00095 // 00096 // Revision 1.11 2002/02/19 10:34:08 kosta 00097 // undone all changes since 2002-02-15 00098 // 00099 // Revision 1.8 2002/02/14 09:37:20 kosta 00100 // automatic serialization registration for template classes implemented 00101 // 00102 // Revision 1.7 2002/02/11 14:35:15 kosta 00103 // implementation improved 00104 // 00105 // Revision 1.6 2002/02/05 15:50:24 kosta 00106 // typo fixed 00107 // 00108 // Revision 1.5 2002/02/05 15:47:15 kosta 00109 // Hashtable<T> split into 2 classes: 00110 // Hashtable<T>> (which cannot be serialized) 00111 // PersistentHashtable<T> (which can be serialized) 00112 // 00113 // Revision 1.4 2002/02/04 11:54:37 kosta 00114 // persistency rewritten (for better namespace support) 00115 // 00116 // Revision 1.3 2002/01/15 16:25:53 kosta 00117 // macros rewritten for a better namespace support 00118 // 00119 // Revision 1.2 2001/11/13 16:36:30 kirsch 00120 // changed line feed to unix style (removed control-M) 00121 // 00122 // Revision 1.1.1.1 2001/06/08 08:09:20 kirsch 00123 // imported alpha-version by olli 00124 // 00125 00126 #ifndef VRS_HASHTABLE_H 00127 #define VRS_HASHTABLE_H 00128 00129 #include <vrs/color.h> 00130 #include <vrs/container/iterator.h> 00131 #include <vector> 00132 #include <utility> 00133 00134 namespace VRS { 00135 00136 template<typename T> class HashTableIterator; 00137 00139 template<typename T> 00140 class NonPersistentHashTable : public SharedObj { 00141 public: 00142 NonPersistentHashTable(unsigned int noOfElements = 113); 00157 bool contains(const T& t) const; 00162 const T* find(const T& t) const; 00167 unsigned int size() const; 00169 00170 bool isEmpty() const; 00172 00173 bool insert(const T& t); 00178 bool erase(const T& t); 00179 void clear(); 00183 Iterator<T>* newIterator() const; 00187 VRS_TYPEINFO(NonPersistentHashTable, SharedObj); 00188 VRS_SERIALIZABLE_ABSTRACT_CLASS(NonPersistentHashTable); // not supported (throws exception); use HashTable<T> instead 00189 00190 private: 00191 unsigned int getHashValue(const T& t) const; 00192 static unsigned int nextPrimeAfter(unsigned int); 00193 static bool isPrime(unsigned int); 00194 00195 std::vector<std::vector<T> > hash_; 00196 unsigned int count_; 00197 00198 protected: 00199 friend class HashTableIterator<T>; 00200 mutable Iterator<T>* iterator_; // no SO-ptr!!! will be set/unset by HashTableIterator's ctor/dtor 00201 00202 const T& get(unsigned int index) const; // used by the HashTableIterator 00203 mutable unsigned int cachedAccum_; // used for improving the get()-method 00204 mutable unsigned int cachedSlot_; // used for improving the get()-method 00205 }; 00206 00208 template<class T> 00209 class HashTable : public NonPersistentHashTable<T> { 00210 public: 00211 HashTable(unsigned int noOfElements = 0) : NonPersistentHashTable<T>(noOfElements) { } 00212 00213 VRS_TYPEINFO(HashTable, NonPersistentHashTable<T>); 00214 VRS_SERIALIZABLE(HashTable); 00215 }; 00216 00217 VRS_SERIALIZATION_REGISTRATION_TEMPLATE(HashTable) 00218 00219 // hash values for unsigned integral types 00220 inline unsigned int HashValue(unsigned long t) { return static_cast<unsigned int>(t); } 00221 inline unsigned int HashValue(unsigned int t) { return static_cast<unsigned int>(t); } 00222 inline unsigned int HashValue(unsigned short t) { return static_cast<unsigned int>(t); } 00223 inline unsigned int HashValue(unsigned char t) { return static_cast<unsigned int>(t); } 00224 00225 // hash values for signed integral types 00226 inline unsigned int HashValue(long t) { return static_cast<unsigned int>(t); } 00227 inline unsigned int HashValue(int t) { return static_cast<unsigned int>(t); } 00228 inline unsigned int HashValue(short t) { return static_cast<unsigned int>(t); } 00229 inline unsigned int HashValue(char t) { return static_cast<unsigned int>(t); } 00230 00231 // hash values for signed floating point types 00232 inline unsigned int HashValue(double t) { return static_cast<unsigned int>(t); } 00233 inline unsigned int HashValue(float t) { return static_cast<unsigned int>(t); } 00234 00235 // hash values for strings 00236 inline unsigned int HashValue(const std::string& str) { 00237 const unsigned int len = static_cast<unsigned int>(str.length()); 00238 if(len == 0) { return 0; } 00239 return ((str[0]<<24) | (str[len/3]<<16) | (str[2*len/3]<<8) | str[len-1]); 00240 } 00241 00242 // hash values for pointers 00243 template<class T> 00244 inline unsigned int HashValue(const T* t) { return static_cast<unsigned int>(t - static_cast<const T*>(NULL)); } 00245 template<class T> 00246 inline unsigned int HashValue(const SO<T>& t) { return HashValue(static_cast<T*>(t)); } 00247 00248 // hash values for combined types 00249 template<typename FIRST, typename SECOND> 00250 inline unsigned int HashValue(const std::pair<FIRST,SECOND>& t) { 00251 return ((7 * HashValue(t.first)) ^ (19 * HashValue(t.second))); 00252 } 00253 00254 #ifdef VRS_HAS_WSTRING 00255 inline unsigned int HashValue(const std::wstring& str) { 00256 const unsigned int len = static_cast<unsigned int>(str.length()); 00257 if(len == 0) { return 0; } 00258 return (((str[0]&0xFF)<<24) | ((str[len/3]&0xFF)<<16) | ((str[2*len/3]&0xFF)<<8) | (str[len-1]&0xFF)); 00259 } 00260 #endif // VRS_HAS_WSTRING 00261 00262 // hash value for colors 00263 inline unsigned int HashValue(const VRS::Color& color) { 00264 return (HashValue(color[0] * 255) + 00265 3 * HashValue(color[1] * 255) + 00266 5 * HashValue(color[2] * 255) + 00267 7 * HashValue(color[3] * 255)); 00268 } 00269 00270 } // namespace VRS 00271 00272 #include <vrs/container/hashtable.tli> 00273 00274 #endif // VRS_HASHTABLE_H