Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

ms_hash_wrapper.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // ms_hash_wrapper Works as a wrapper between MS (VS .NET 2003) hashing functions 00004 // and GCC plearn (gcc inspired) hashing specializations. 00005 // Copyright (C) 2004 Norman Casagrande 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 // Wrappers for the has functions. See below for instructions. 00037 // IMPORTANT: These macros MUST be outside of any namespace!!!!!!!!! 00038 00039 00040 #ifndef ms_hash_wrapper_H 00041 #define ms_hash_wrapper_H 00042 00043 #if defined(__GNUC__) && !defined(__INTEL_COMPILER) // Look below after the macros for the end of this 00044 00045 #if __GNUC__ < 3 00046 #error GNUC < 3 is not supported!! 00047 #endif 00048 00049 # include <ext/hash_set> //to get stl_hash_fun.h ... (template<> class hash) 00050 # include <ext/hash_map> //to get stl_hash_fun.h ... (template<> class hash) 00051 00052 # if __GNUC__ == 3 && __GNUC_MINOR__ == 0 // GCC 3.0 00053 # define __NMSPACE__ std 00054 # else // GCC 3.1 or later 00055 using namespace __gnu_cxx; 00056 # define __NMSPACE__ __gnu_cxx 00057 # endif 00058 00059 //using namespace __NMSPACE__; 00060 00061 // Three original cases for hash specialization and declaration: 00062 00063 // In file pl_hash_fun: 00064 // template<> 00065 // struct hash<float> 00066 // { 00067 // size_t operator()(float x) const { return PLearn::hashval(x); } 00068 // }; 00069 // NOW IS: SET_HASH_WITH_FUNCTION 00070 00071 // ------------------------------------------------------------------- 00072 00073 // In file pl_hash_fun: 00074 // template<> 00075 // struct hash<string> 00076 // { 00077 // size_t operator()(const string& __s) const { return hash<const char*>()(__s.c_str()); } 00078 // }; 00079 // NOW IS: SET_HASH_WITH_INHERITANCE 00080 00081 // ------------------------------------------------------------------- 00082 00083 // In file TMat_maths_impl.h: 00084 // template<class T> 00085 // struct hash<PLearn::TVec<T> > 00086 // { 00087 // size_t operator()(PLearn::TVec<T> v) const { return hash<T>()(sumsquare(v));} 00088 // }; 00089 // NOW IS: SET_HASH_FUNCTION 00090 00091 // ------------------------------------------------------------------- 00092 // ------------------------------------------------------------------- 00093 // ------------------------------------------------------------------- 00094 00098 00099 // WARNING: paramName should be included in hashFunc! 00100 // Example: 00101 // SET_HASH_WITH_FUNCTION(float, val, PLearn::hashval(val)) 00102 // --> Will return (see the function operator() below): 00103 // size_t operator()(const float& val) const 00104 // { 00105 // return (size_t)(PLearn::hashval(val)); 00106 // } 00107 #define SET_HASH_WITH_FUNCTION(type, paramName, hashFunc) \ 00108 namespace __NMSPACE__ { \ 00109 template<> \ 00110 struct hash< type > \ 00111 { \ 00112 size_t operator()(type paramName) const \ 00113 { \ 00114 return (size_t)(hashFunc); \ 00115 } \ 00116 }; \ 00117 } 00118 00119 // Example: 00120 // SET_HASH_WITH_INHERITANCE(std::string, const char*, val, val.c_str()) 00121 // --> Will return (see the function operator() below): 00122 // size_t operator()(const std::string& val) const 00123 // { 00124 // return hash<const char*>()val.c_str()); 00125 // } 00126 #define SET_HASH_WITH_INHERITANCE(type, originalType, paramName, hashFunc) \ 00127 namespace __NMSPACE__ { \ 00128 template<> \ 00129 struct hash< type > \ 00130 { \ 00131 size_t operator()(type paramName) const \ 00132 { \ 00133 return hash< originalType >()(hashFunc); \ 00134 } \ 00135 }; \ 00136 } 00137 00138 00139 // WARNING: paramName should be included in hashFunc! 00140 // Example: 00141 // SET_HASH_FUNCTION(PLearn::TVec<T>, T, val, sqrt(val)) 00142 // --> Will return: 00143 // template <class T > 00144 // struct hash< PLearn::TVec<T> > 00145 // { 00146 // size_t operator()(PLearn::TVec<T>& val) const 00147 // { 00148 // return hash< T >()(sqrt(val)); 00149 // } 00150 // } 00151 // See below: size_t operator()(const type& paramName) const 00152 #define SET_HASH_FUNCTION(type, templateClass, paramName, hashFunc) \ 00153 namespace __NMSPACE__ { \ 00154 template<class templateClass > \ 00155 struct hash< type > \ 00156 { \ 00157 size_t operator()(type paramName) const \ 00158 { \ 00159 return hash< templateClass >()(hashFunc); \ 00160 } \ 00161 }; \ 00162 } 00163 #endif // __GNUC__ 00164 00168 00169 #if (defined(WIN32) || defined(__INTEL_COMPILER)) && !defined(_MINGW_) && !defined(__GNUC__) // MinGW runs under gcc... 00170 00171 #include <hash_set> 00172 #include <hash_map> 00173 00174 // WARNING: paramName should be included in hashFunc! 00175 // Example: 00176 // SET_HASH_WITH_FUNCTION(float, val, PLearn::hashval(val)) 00177 // --> Will return (see the function operator() below): 00178 // size_t operator()(const float& val) const 00179 // { 00180 // return (size_t)(PLearn::hashval(val)); 00181 // } 00182 00183 #define SET_HASH_WITH_FUNCTION(type, paramName, hashFunc) \ 00184 namespace stdext { \ 00185 template<class _Kty> \ 00186 class hash_compare<type, std::less<_Kty> > \ 00187 { \ 00188 public: \ 00189 enum \ 00190 { /* parameters for hash table */ \ 00191 bucket_size = 4, /* 0 < bucket_size */ \ 00192 min_buckets = 8}; /* min_buckets = 2 ^^ N, 0 < N */ \ 00193 \ 00194 hash_compare() \ 00195 : comp() \ 00196 { /* construct with default comparator */ \ 00197 } \ 00198 \ 00199 hash_compare(std::less<_Kty> _Pred) \ 00200 : comp(_Pred) \ 00201 { /* construct with _Pred comparator */ \ 00202 } \ 00203 \ 00204 size_t operator()(const type& paramName) const \ 00205 { /* hash _Keyval to size_t value */ \ 00206 return (size_t)(hashFunc); \ 00207 } \ 00208 \ 00209 bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const\ 00210 { /* test if _Keyval1 ordered before _Keyval2 */ \ 00211 return (comp(_Keyval1, _Keyval2)); \ 00212 } \ 00213 \ 00214 protected: \ 00215 std::less<_Kty> comp; /* the comparator object */ \ 00216 \ 00217 }; \ 00218 } // end of namespace stdext 00219 00220 // WARNING: paramName should be included in hashFunc! 00221 // SET_HASH_WITH_INHERITANCE(std::string, const char*, val, val.c_str()) 00222 // --> Will return (see the function operator() below): 00223 // size_t operator()(const std::string& val) const 00224 // { 00225 // return stdext::hash_compare<const char*>()val.c_str()); 00226 // } 00227 #define SET_HASH_WITH_INHERITANCE(type, originalType, paramName, hashFunc)\ 00228 namespace stdext { \ 00229 template<class _Kty> \ 00230 class hash_compare<type, std::less<_Kty> > \ 00231 { \ 00232 public: \ 00233 enum \ 00234 { /* parameters for hash table */ \ 00235 bucket_size = 4, /* 0 < bucket_size */ \ 00236 min_buckets = 8}; /* min_buckets = 2 ^^ N, 0 < N */ \ 00237 \ 00238 hash_compare() \ 00239 : comp() \ 00240 { /* construct with default comparator */ } \ 00241 \ 00242 hash_compare(std::less<_Kty> _Pred) \ 00243 : comp(_Pred) \ 00244 { /* construct with _Pred comparator */ } \ 00245 \ 00246 size_t operator()(const type& paramName) const \ 00247 { /* hash _Keyval to size_t value */ \ 00248 return hash_compare< originalType >()(hashFunc); \ 00249 } \ 00250 \ 00251 bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const \ 00252 { /* test if _Keyval1 ordered before _Keyval2 */ \ 00253 return (comp(_Keyval1, _Keyval2)); \ 00254 } \ 00255 \ 00256 protected: \ 00257 std::less<_Kty> comp; /* the comparator object */ \ 00258 \ 00259 }; } 00260 00261 // WARNING: paramName should be included in hashFunc! 00262 // Example: 00263 // SET_HASH_FUNCTION(PLearn::TVec<T>, T, val, sqrt(val)) 00264 // --> Will return: 00265 // template <class T > 00266 // class stdext::hash_compare<PLearn::TVec<T>, std::less< T > > 00267 // { 00268 // //// Some other stuffs... 00269 // size_t operator()(PLearn::TVec<T>& val) const 00270 // { 00271 // return stdext::hash_compare< T >()(sqrt(val)); 00272 // } 00273 // //// Some other stuffs... 00274 // } 00275 // See below: size_t operator()(const type& paramName) const 00276 #define SET_HASH_FUNCTION(type, templateClass, paramName, hashFunc) \ 00277 namespace stdext { \ 00278 template<class templateClass > \ 00279 class hash_compare<type, std::less< templateClass > > \ 00280 { \ 00281 public: \ 00282 enum \ 00283 { /* parameters for hash table */ \ 00284 bucket_size = 4, /* 0 < bucket_size */ \ 00285 min_buckets = 8}; /* min_buckets = 2 ^^ N, 0 < N */ \ 00286 \ 00287 hash_compare() \ 00288 : comp() \ 00289 { /* construct with default comparator */ \ 00290 } \ 00291 \ 00292 hash_compare(std::less< templateClass > _Pred) \ 00293 : comp(_Pred) \ 00294 { /* construct with _Pred comparator */ \ 00295 } \ 00296 \ 00297 size_t operator()(const type& paramName) const \ 00298 { /* hash _Keyval to size_t value */ \ 00299 return hash_compare< templateClass >()(hashFunc); \ 00300 } \ 00301 \ 00302 bool operator()(const templateClass& _Keyval1, const templateClass& _Keyval2) const \ 00303 { /* test if _Keyval1 ordered before _Keyval2 */ \ 00304 return (comp(_Keyval1, _Keyval2)); \ 00305 } \ 00306 \ 00307 protected: \ 00308 std::less< templateClass > comp; /* the comparator object */ \ 00309 \ 00310 }; } 00311 00312 #endif // WIN32 00313 00314 00318 00319 #if defined(__GNUC__) && defined(__INTEL_COMPILER) 00320 00321 #include <hash_set> 00322 #include <hash_map> 00323 00324 // WARNING: paramName should be included in hashFunc! 00325 // Example: 00326 // SET_HASH_WITH_FUNCTION(float, val, PLearn::hashval(val)) 00327 // --> Will return (see the function operator() below): 00328 // size_t operator()(const float& val) const 00329 // { 00330 // return (size_t)(PLearn::hashval(val)); 00331 // } 00332 00333 #define SET_HASH_WITH_FUNCTION(type, paramName, hashFunc) \ 00334 namespace std { \ 00335 template<class _Kty> \ 00336 class hash_compare<type, std::less<_Kty> > \ 00337 { \ 00338 public: \ 00339 enum \ 00340 { /* parameters for hash table */ \ 00341 bucket_size = 4, /* 0 < bucket_size */ \ 00342 min_buckets = 8}; /* min_buckets = 2 ^^ N, 0 < N */ \ 00343 \ 00344 hash_compare() \ 00345 : comp() \ 00346 { /* construct with default comparator */ \ 00347 } \ 00348 \ 00349 hash_compare(std::less<_Kty> _Pred) \ 00350 : comp(_Pred) \ 00351 { /* construct with _Pred comparator */ \ 00352 } \ 00353 \ 00354 size_t operator()(const type& paramName) const \ 00355 { /* hash _Keyval to size_t value */ \ 00356 return (size_t)(hashFunc); \ 00357 } \ 00358 \ 00359 bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const\ 00360 { /* test if _Keyval1 ordered before _Keyval2 */ \ 00361 return (comp(_Keyval1, _Keyval2)); \ 00362 } \ 00363 \ 00364 protected: \ 00365 std::less<_Kty> comp; /* the comparator object */ \ 00366 \ 00367 }; \ 00368 } // end of namespace std 00369 00370 // WARNING: paramName should be included in hashFunc! 00371 // SET_HASH_WITH_INHERITANCE(std::string, const char*, val, val.c_str()) 00372 // --> Will return (see the function operator() below): 00373 // size_t operator()(const std::string& val) const 00374 // { 00375 // return stdext::hash_compare<const char*>()val.c_str()); 00376 // } 00377 #define SET_HASH_WITH_INHERITANCE(type, originalType, paramName, hashFunc)\ 00378 namespace std { \ 00379 template<class _Kty> \ 00380 class hash_compare<type, std::less<_Kty> > \ 00381 { \ 00382 public: \ 00383 enum \ 00384 { /* parameters for hash table */ \ 00385 bucket_size = 4, /* 0 < bucket_size */ \ 00386 min_buckets = 8}; /* min_buckets = 2 ^^ N, 0 < N */ \ 00387 \ 00388 hash_compare() \ 00389 : comp() \ 00390 { /* construct with default comparator */ } \ 00391 \ 00392 hash_compare(std::less<_Kty> _Pred) \ 00393 : comp(_Pred) \ 00394 { /* construct with _Pred comparator */ } \ 00395 \ 00396 size_t operator()(const type& paramName) const \ 00397 { /* hash _Keyval to size_t value */ \ 00398 return hash_compare< originalType >()(hashFunc); \ 00399 } \ 00400 \ 00401 bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const \ 00402 { /* test if _Keyval1 ordered before _Keyval2 */ \ 00403 return (comp(_Keyval1, _Keyval2)); \ 00404 } \ 00405 \ 00406 protected: \ 00407 std::less<_Kty> comp; /* the comparator object */ \ 00408 \ 00409 }; } 00410 00411 // WARNING: paramName should be included in hashFunc! 00412 // Example: 00413 // SET_HASH_FUNCTION(PLearn::TVec<T>, T, val, sqrt(val)) 00414 // --> Will return: 00415 // template <class T > 00416 // class stdext::hash_compare<PLearn::TVec<T>, std::less< T > > 00417 // { 00418 // //// Some other stuffs... 00419 // size_t operator()(PLearn::TVec<T>& val) const 00420 // { 00421 // return stdext::hash_compare< T >()(sqrt(val)); 00422 // } 00423 // //// Some other stuffs... 00424 // } 00425 // See below: size_t operator()(const type& paramName) const 00426 #define SET_HASH_FUNCTION(type, templateClass, paramName, hashFunc) \ 00427 namespace std { \ 00428 template<class templateClass > \ 00429 class hash_compare<type, std::less< templateClass > > \ 00430 { \ 00431 public: \ 00432 enum \ 00433 { /* parameters for hash table */ \ 00434 bucket_size = 4, /* 0 < bucket_size */ \ 00435 min_buckets = 8}; /* min_buckets = 2 ^^ N, 0 < N */ \ 00436 \ 00437 hash_compare() \ 00438 : comp() \ 00439 { /* construct with default comparator */ \ 00440 } \ 00441 \ 00442 hash_compare(std::less< templateClass > _Pred) \ 00443 : comp(_Pred) \ 00444 { /* construct with _Pred comparator */ \ 00445 } \ 00446 \ 00447 size_t operator()(const type& paramName) const \ 00448 { /* hash _Keyval to size_t value */ \ 00449 return hash_compare< templateClass >()(hashFunc); \ 00450 } \ 00451 \ 00452 bool operator()(const templateClass& _Keyval1, const templateClass& _Keyval2) const \ 00453 { /* test if _Keyval1 ordered before _Keyval2 */ \ 00454 return (comp(_Keyval1, _Keyval2)); \ 00455 } \ 00456 \ 00457 protected: \ 00458 std::less< templateClass > comp; /* the comparator object */ \ 00459 \ 00460 }; } 00461 00462 #endif // WIN32 00463 00464 #endif // ms_hash_wrapper_H

Generated on Tue Aug 17 15:59:04 2004 for PLearn by doxygen 1.3.7