00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef GIGA_GIGAHASH_H
00017 #define GIGA_GIGAHASH_H 1
00018
00019 #include <string>
00020
00033 template <class TYPE>
00034 struct GiGaHash : public std::unary_function<TYPE,size_t>
00035 {
00036 size_t operator() ( const TYPE& key ) const
00037 {
00038 size_t res = 0 ;
00039 size_t len = sizeof(TYPE) ;
00040 const char* p = reinterpret_cast<const char*>( &key );
00041 while( len-- ) { res = ( res << 1 ) ^ *p++ ; }
00042 return res;
00043 };
00044 };
00045
00046 template <>
00047 struct GiGaHash<std::string>
00048 : public std::unary_function<std::string,size_t>
00049 {
00050 size_t operator() ( const std::string& key ) const
00051 {
00052 typedef std::string::const_iterator CI;
00053 size_t res = 0 ;
00054 CI end = key.end () ;
00055 for( CI p = key.begin(); end != p ; ++p ) { res = ( res << 1 )^*p ;}
00056 return res ;
00057 };
00058 };
00059
00060 #ifndef WIN32
00061 #if defined (__GNUC__) && ( __GNUC__<= 2 )
00062 template<>
00063 struct std::hash<std::string>
00064 : public GiGaHash<std::string>{};
00065 #else
00066 namespace __gnu_cxx {
00067 template<>
00068 struct hash<std::string>
00069 : public GiGaHash<std::string>{};
00070 }
00071 #endif
00072 #endif
00073
00074
00075
00076
00077 #endif // GIGA_GIGAHASH_H
00078