PLearn 0.1
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__)     // 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) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1000)
00053 //              GCC 3.0 or ICC < 10.0
00054 #               define __NMSPACE__ std
00055 #  else                                            // GCC 3.1 or later
00056 using namespace __gnu_cxx;
00057 #               define __NMSPACE__ __gnu_cxx
00058 #  endif
00059 
00060 //using namespace __NMSPACE__;
00061 
00062 // Three original cases for hash specialization and declaration:
00063 
00064 // In file pl_hash_fun:
00065 // template<>
00066 // struct hash<float>
00067 // {
00068 //   size_t operator()(float x) const { return PLearn::hashval(x); }
00069 // };
00070 // NOW IS: SET_HASH_WITH_FUNCTION
00071 
00072 // -------------------------------------------------------------------
00073 
00074 // In file pl_hash_fun:
00075 // template<>
00076 // struct hash<string>
00077 // {
00078 //   size_t operator()(const string& __s) const { return hash<const char*>()(__s.c_str()); }
00079 // };
00080 // NOW IS: SET_HASH_WITH_INHERITANCE
00081 
00082 // -------------------------------------------------------------------
00083 
00084 // In file TMat_maths_impl.h:
00085 // template<class T>
00086 // struct hash<PLearn::TVec<T> >
00087 // {
00088 //   size_t operator()(PLearn::TVec<T> v) const { return hash<T>()(sumsquare(v));} 
00089 // };
00090 // NOW IS: SET_HASH_FUNCTION
00091 
00092 // -------------------------------------------------------------------
00093 // -------------------------------------------------------------------
00094 // -------------------------------------------------------------------
00095 
00099 
00100 // WARNING: paramName should be included in hashFunc!   
00101 // Example: 
00102 //     SET_HASH_WITH_FUNCTION(float, val, PLearn::hashval(val))
00103 // --> Will return (see the function operator() below): 
00104 //     size_t operator()(const float& val) const
00105 //     { 
00106 //        return (size_t)(PLearn::hashval(val));
00107 //     } 
00108 #define SET_HASH_WITH_FUNCTION(type, paramName, hashFunc)           \
00109   namespace __NMSPACE__ {                                           \
00110           template<>                                                      \
00111           struct hash< type >                                             \
00112           {                                                               \
00113                   size_t operator()(const type& paramName) const                \
00114                   {                                                             \
00115                           return (size_t)(hashFunc);                                  \
00116                   }                                                             \
00117           };                                                              \
00118   }
00119 
00120 #define SET_HASH_WITH_FUNCTION_NOCONSTREF(type, paramName, hashFunc) \
00121   namespace __NMSPACE__ {                                           \
00122           template<>                                                      \
00123           struct hash< type >                                             \
00124           {                                                               \
00125                   size_t operator()(type paramName) const                       \
00126                   {                                                             \
00127                           return (size_t)(hashFunc);                                  \
00128                   }                                                             \
00129           };                                                              \
00130   }
00131 
00132 // Example: 
00133 //     SET_HASH_WITH_INHERITANCE(std::string, const char*, val, val.c_str())
00134 // --> Will return (see the function operator() below): 
00135 //   size_t operator()(const std::string& val) const 
00136 //   { 
00137 //     return hash<const char*>()val.c_str()); 
00138 //   } 
00139 #define SET_HASH_WITH_INHERITANCE(type, originalType, paramName, hashFunc)              \
00140   namespace __NMSPACE__ {                                                     \
00141           template<>                                                                \
00142           struct hash< type >                                                       \
00143           {                                                                         \
00144                   size_t operator()(const type& paramName) const                          \
00145       {                                                                       \
00146                           return hash< originalType >()(hashFunc);                              \
00147                   }                                                                       \
00148           };                                                                        \
00149   }
00150 
00151 
00152 // WARNING: paramName should be included in hashFunc!   
00153 // Example: 
00154 //     SET_HASH_FUNCTION(PLearn::TVec<T>, T, val, sqrt(val))
00155 // --> Will return:
00156 //   template <class T >
00157 //   struct hash< PLearn::TVec<T> >
00158 //   {
00159 //      size_t operator()(const PLearn::TVec<T>& val) const 
00160 //      {                                                               
00161 //         return hash< T >()(sqrt(val));                                                                                       
00162 //      }       
00163 //   }
00164 // See below: size_t operator()(const type& paramName) const 
00165 #define SET_HASH_FUNCTION(type, templateClass, paramName, hashFunc)  \
00166   namespace __NMSPACE__ {                                            \
00167           template<class templateClass >                                   \
00168           struct hash< type >                                              \
00169           {                                                                \
00170                   size_t operator()(const type& paramName) const                 \
00171                   {                                                              \
00172                           return hash< templateClass >()(hashFunc);                    \
00173                   }                                                              \
00174           };                                                               \
00175   }
00176 #endif // __GNUC__
00177 
00181 
00182 #if (defined(WIN32) || defined(__INTEL_COMPILER)) && !defined(_MINGW_) && !defined(__GNUC__) // MinGW runs under gcc...
00183 
00184 #include <hash_set>
00185 #include <hash_map>
00186 
00187 // WARNING: paramName should be included in hashFunc!   
00188 // Example: 
00189 //     SET_HASH_WITH_FUNCTION(float, val, PLearn::hashval(val))
00190 // --> Will return (see the function operator() below): 
00191 //     size_t operator()(const float& val) const
00192 //     { 
00193 //        return (size_t)(PLearn::hashval(val));
00194 //     } 
00195 
00196 #define SET_HASH_WITH_FUNCTION(type, paramName, hashFunc)     \
00197   namespace stdext {                                          \
00198         template<class _Kty>                                        \
00199         class hash_compare<type, std::less<_Kty> >                  \
00200         {                                                           \
00201         public:                                                     \
00202                 enum                                                      \
00203                 {       /* parameters for hash table */                         \
00204                         bucket_size = 4,        /* 0 < bucket_size */                 \
00205                         min_buckets = 8};       /* min_buckets = 2 ^^ N, 0 < N */     \
00206                                                               \
00207                         hash_compare()                                          \
00208                                 : comp()                                              \
00209                         {       /* construct with default comparator */               \
00210                         }                                                       \
00211                                                               \
00212                         hash_compare(std::less<_Kty> _Pred)                     \
00213                                 : comp(_Pred)                                         \
00214                         {       /* construct with _Pred comparator */                 \
00215                         }                                                       \
00216                                                               \
00217                         size_t operator()(const type& paramName) const          \
00218                         {       /* hash _Keyval to size_t value */                    \
00219                                 return (size_t)(hashFunc);                            \
00220                         }                                                       \
00221                                                               \
00222                         bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const\
00223                         {       /* test if _Keyval1 ordered before _Keyval2 */        \
00224                                 return (comp(_Keyval1, _Keyval2));                    \
00225                         }                                                       \
00226                                                               \
00227         protected:                                                  \
00228                 std::less<_Kty> comp; /* the comparator object */         \
00229                                                               \
00230         };                                                          \
00231   } // end of namespace stdext
00232 
00233 // WARNING: paramName should be included in hashFunc!   
00234 //     SET_HASH_WITH_INHERITANCE(std::string, const char*, val, val.c_str())
00235 // --> Will return (see the function operator() below): 
00236 //   size_t operator()(const std::string& val) const 
00237 //   { 
00238 //     return stdext::hash_compare<const char*>()val.c_str()); 
00239 //   } 
00240 #define SET_HASH_WITH_INHERITANCE(type, originalType, paramName, hashFunc)\
00241   namespace stdext {                                                      \
00242   template<class _Kty>                                                    \
00243   class hash_compare<type, std::less<_Kty> >                              \
00244   {                                                                       \
00245   public:                                                                 \
00246     enum                                                                  \
00247     {   /* parameters for hash table */                                     \
00248       bucket_size = 4,  /* 0 < bucket_size */                             \
00249       min_buckets = 8}; /* min_buckets = 2 ^^ N, 0 < N */                 \
00250                                                                           \
00251       hash_compare()                                                      \
00252         : comp()                                                          \
00253                         {       /* construct with default comparator */ }                         \
00254                                                                           \
00255       hash_compare(std::less<_Kty> _Pred)                                 \
00256         : comp(_Pred)                                                     \
00257                         {       /* construct with _Pred comparator */ }                           \
00258                                                                           \
00259       size_t operator()(const type& paramName) const                      \
00260       { /* hash _Keyval to size_t value */                                \
00261         return hash_compare< originalType >()(hashFunc);                  \
00262       }                                                                   \
00263                                                                           \
00264       bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const   \
00265       { /* test if _Keyval1 ordered before _Keyval2 */                    \
00266         return (comp(_Keyval1, _Keyval2));                                \
00267       }                                                                   \
00268                                                                           \
00269   protected:                                                              \
00270     std::less<_Kty> comp;       /* the comparator object */                     \
00271                                                                           \
00272   }; }
00273 
00274 // WARNING: paramName should be included in hashFunc!   
00275 // Example: 
00276 //     SET_HASH_FUNCTION(PLearn::TVec<T>, T, val, sqrt(val))
00277 // --> Will return:
00278 //   template <class T >
00279 //   class stdext::hash_compare<PLearn::TVec<T>, std::less< T > >                               
00280 //   {
00281 //   //// Some other stuffs...
00282 //      size_t operator()(const PLearn::TVec<T>& val) const 
00283 //      {                                                               
00284 //          return stdext::hash_compare< T >()(sqrt(val)); 
00285 //      }       
00286 //   //// Some other stuffs...
00287 //   }
00288 // See below: size_t operator()(const type& paramName) const 
00289 #define SET_HASH_FUNCTION(type, templateClass, paramName, hashFunc)       \
00290   namespace stdext {                                                      \
00291         template<class templateClass >                                          \
00292         class hash_compare<type, std::less< templateClass > >                   \
00293         {                                                                       \
00294         public:                                                                 \
00295                 enum                                                                                \
00296                 {       /* parameters for hash table */                                                 \
00297                         bucket_size = 4,        /* 0 < bucket_size */                             \
00298                         min_buckets = 8};       /* min_buckets = 2 ^^ N, 0 < N */                 \
00299                                                                           \
00300                         hash_compare()                                                      \
00301                                 : comp()                                                          \
00302                         {       /* construct with default comparator */                           \
00303                         }                                                                   \
00304                                                                           \
00305                         hash_compare(std::less< templateClass > _Pred)                      \
00306                                 : comp(_Pred)                                                     \
00307                         {       /* construct with _Pred comparator */                             \
00308                         }                                                                   \
00309                                                                           \
00310                         size_t operator()(const type& paramName) const                      \
00311                         {       /* hash _Keyval to size_t value */                                \
00312                                 return hash_compare< templateClass >()(hashFunc);                 \
00313                         }                                                                   \
00314                                                                           \
00315                         bool operator()(const templateClass& _Keyval1, const templateClass& _Keyval2) const     \
00316                         {       /* test if _Keyval1 ordered before _Keyval2 */                    \
00317                                 return (comp(_Keyval1, _Keyval2));                                \
00318                         }                                                                   \
00319                                                                           \
00320         protected:                                                              \
00321                 std::less< templateClass > comp;        /* the comparator object */         \
00322                                                                           \
00323   }; }
00324 
00325 #endif // WIN32 
00326 
00327 #endif // ms_hash_wrapper_H
00328 
00329 
00330 /*
00331   Local Variables:
00332   mode:c++
00333   c-basic-offset:4
00334   c-file-style:"stroustrup"
00335   c-file-offsets:((innamespace . 0)(inline-open . 0))
00336   indent-tabs-mode:nil
00337   fill-column:79
00338   End:
00339 */
00340 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines