00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Neil 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_Hash.h ( UT Library, C++) 00015 * 00016 * COMMENTS: 00017 */ 00018 00019 #ifndef __UT_Hash__ 00020 #define __UT_Hash__ 00021 00022 #include "UT_API.h" 00023 #include <string.h> 00024 #include <stdlib.h> 00025 #include <malloc.h> 00026 00027 #include "UT_Math.h" 00028 #include "UT_String.h" 00029 #include "UT_SmallObject.h" 00030 00031 class UT_API UT_Hash 00032 : public UT_SmallObject<UT_Hash, 00033 UT_SMALLOBJECT_CLEANPAGES_DEFAULT, 00034 UT_SMALLOBJECT_PAGESIZE_DEFAULT, 00035 UT_SMALLOBJECT_THREADSAFE_ON> 00036 { 00037 public: 00038 virtual ~UT_Hash() { } 00039 00040 // return 0 on equality 00041 // 00042 // Often, the comparison function for a UT_Hash is more expensive 00043 // than computing the unsigned integer hash code. However, it's 00044 // possible for the compare() function to be called even when 00045 // there is no collision on the hash codes, therefore, if you 00046 // have the hash codes cached, its often efficient to do 00047 // a quick comparison of the hash codes before computing the 00048 // expensive compare function. 00049 virtual int compare(const UT_Hash &a) const = 0; 00050 00051 // Copies data from another UT_Hash into this one. 00052 virtual void copy(const UT_Hash &a) = 0; 00053 00054 // return hash value 00055 // should never change i.e. should rely on const data 00056 virtual unsigned hash() const = 0; 00057 00058 virtual UT_Hash* copy() const = 0; 00059 00060 virtual int64 getMemUsage() const { return 0; } 00061 }; 00062 00063 class UT_API UT_Hash_String : public UT_Hash { 00064 private: 00065 char *myData; 00066 public: 00067 UT_Hash_String( const char *s ) 00068 { 00069 myData = strdup( s ); 00070 } 00071 00072 ~UT_Hash_String() 00073 { 00074 free(myData); 00075 } 00076 00077 int compare( const UT_Hash & a) const 00078 { 00079 return strcmp(myData, ((const UT_Hash_String&)a).myData); 00080 } 00081 00082 void copy(const UT_Hash &a) 00083 { 00084 free(myData); 00085 myData = strdup(static_cast<const UT_Hash_String &>(a).myData); 00086 } 00087 00088 unsigned hash() const 00089 { 00090 return UT_String::hash(myData); 00091 } 00092 00093 UT_Hash* copy() const 00094 { 00095 return new UT_Hash_String( myData ); 00096 } 00097 00098 int64 getMemUsage() const 00099 { 00100 return (sizeof(*this) + (::strlen(myData) + 1) * sizeof(char)); 00101 } 00102 00103 const char* getString() const 00104 { 00105 return myData; 00106 } 00107 }; 00108 00109 class UT_API UT_Hash_Int : public UT_Hash { 00110 private: 00111 int myData; 00112 unsigned myKey; 00113 public: 00114 UT_Hash_Int( int a ) : myData( a ) 00115 { 00116 myKey = (unsigned) a; 00117 myKey = UTwang_inthash(myKey); 00118 } 00119 00120 ~UT_Hash_Int() 00121 { 00122 } 00123 00124 int compare( const UT_Hash & a) const 00125 { 00126 return !(myData==((const UT_Hash_Int&)a).myData); 00127 } 00128 00129 void copy(const UT_Hash &a) 00130 { 00131 myData = static_cast<const UT_Hash_Int &>(a).myData; 00132 myKey = static_cast<const UT_Hash_Int &>(a).myKey; 00133 } 00134 00135 unsigned hash() const 00136 { 00137 return myKey; 00138 } 00139 00140 UT_Hash* copy() const 00141 { 00142 return new UT_Hash_Int( myData ); 00143 } 00144 00145 int64 getMemUsage() const 00146 { 00147 return (sizeof(*this)); 00148 } 00149 00150 int getData() const { return myData; } 00151 }; 00152 00153 class UT_API UT_Hash_Int64 : public UT_Hash { 00154 private: 00155 int64 myData; 00156 uint64 myKey; 00157 public: 00158 UT_Hash_Int64( int64 a ) : myData( a ) 00159 { 00160 myKey = (uint64) a; 00161 myKey = UTwang_inthash64(myKey); 00162 } 00163 00164 ~UT_Hash_Int64() 00165 { 00166 } 00167 00168 int compare( const UT_Hash & a) const 00169 { 00170 return !(myData==((const UT_Hash_Int64&)a).myData); 00171 } 00172 00173 void copy(const UT_Hash &a) 00174 { 00175 myData = static_cast<const UT_Hash_Int64 &>(a).myData; 00176 myKey = static_cast<const UT_Hash_Int64 &>(a).myKey; 00177 } 00178 00179 unsigned hash() const 00180 { 00181 return myKey; 00182 } 00183 00184 UT_Hash* copy() const 00185 { 00186 return new UT_Hash_Int64( myData ); 00187 } 00188 00189 int64 getMemUsage() const 00190 { 00191 return (sizeof(*this)); 00192 } 00193 00194 int64 getData() const { return myData; } 00195 }; 00196 00197 class UT_API UT_Hash_Ptr : public UT_Hash { 00198 private: 00199 const void *myData; 00200 public: 00201 UT_Hash_Ptr( const void *p ) : myData(p) 00202 { 00203 } 00204 00205 ~UT_Hash_Ptr() 00206 { 00207 } 00208 00209 int compare(const UT_Hash &a) const 00210 { 00211 return !(myData==((const UT_Hash_Ptr&)a).myData); 00212 } 00213 00214 void copy(const UT_Hash &a) 00215 { 00216 myData = static_cast<const UT_Hash_Ptr &>(a).myData; 00217 } 00218 00219 unsigned hash() const 00220 { 00221 unsigned key; 00222 key = SYSpointerHash(myData); 00223 key = UTwang_inthash(key); 00224 return key; 00225 } 00226 00227 UT_Hash* copy() const 00228 { 00229 return new UT_Hash_Ptr(myData); 00230 } 00231 00232 int64 getMemUsage() const 00233 { 00234 return (sizeof(*this)); 00235 } 00236 00237 operator const void *() const 00238 { 00239 return myData; 00240 } 00241 operator void *() const 00242 { 00243 return const_cast<void *>(myData); 00244 } 00245 }; 00246 00247 class UT_API UT_Hash_Const_Ptr : public UT_Hash { 00248 private: 00249 const void *myData; 00250 public: 00251 UT_Hash_Const_Ptr( const void *p ) : myData(p) 00252 { 00253 } 00254 00255 ~UT_Hash_Const_Ptr() 00256 { 00257 } 00258 00259 int compare(const UT_Hash &a) const 00260 { 00261 return !(myData==((const UT_Hash_Const_Ptr&)a).myData); 00262 } 00263 00264 void copy(const UT_Hash &a) 00265 { 00266 myData = static_cast<const UT_Hash_Const_Ptr &>(a).myData; 00267 } 00268 00269 unsigned hash() const 00270 { 00271 unsigned key; 00272 key = SYSpointerHash(myData); 00273 key = UTwang_inthash(key); 00274 return key; 00275 } 00276 00277 UT_Hash* copy() const 00278 { 00279 return new UT_Hash_Const_Ptr(myData); 00280 } 00281 00282 int64 getMemUsage() const 00283 { 00284 return (sizeof(*this)); 00285 } 00286 }; 00287 00288 #endif 00289
1.5.9