Hash.h
Go to the documentation of this file.00001
00002
00003 #ifndef GAUDIKERNEL_HASH_H
00004 #define GAUDIKERNEL_HASH_H 1
00005
00006
00007
00008
00009
00010 #include <functional>
00011
00012
00013
00014 #include "boost/functional/hash.hpp"
00015
00016 namespace GaudiUtils
00017 {
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 template <class T>
00030 struct GenericHash : public std::unary_function<T,std::size_t>
00031 {
00032
00034 inline std::size_t operator() ( const T& key ) const {
00035 std::size_t res = 0 ;
00036 std::size_t len = sizeof(T) ;
00037 const char* p = reinterpret_cast<const char*>( &key );
00038 while( len-- ) { res = ( res << 1 ) ^ *p; ++p; }
00039 return res;
00040 }
00041
00042 };
00043
00044
00097 template <class T>
00098 struct Hash : public std::unary_function<T,std::size_t>
00099 {
00100
00102 inline std::size_t operator() ( const T& key ) const;
00103
00104 };
00105
00107 template <class T>
00108 struct Hash<T*> : public std::unary_function<const T*,std::size_t>
00109 {
00110
00112 inline std::size_t operator() ( const T* key ) const;
00113
00114 };
00115
00117 template <class T, unsigned N>
00118 struct Hash<T(&)[N]> : public std::unary_function<T(&)[N],std::size_t>
00119 {
00120
00122 inline std::size_t operator() ( T (&key) [N] ) const
00123 { return boost::hash_range ( key , key + N ) ; }
00124
00125 } ;
00127 template <class T, unsigned N>
00128 struct Hash<const T(&)[N]> : public std::unary_function<const T(&)[N],std::size_t>
00129 {
00130
00132 inline std::size_t operator() ( const T (&key) [N] ) const
00133 { return boost::hash_range ( key , key + N ) ; }
00134
00135 } ;
00136
00138 template<class T>
00139 struct Hash<const T> : public Hash<T> {} ;
00141 template<class T>
00142 struct Hash<const T*> : public Hash<T*> {} ;
00144 template<class T>
00145 struct Hash<T&> : public Hash<T> {} ;
00147 template<class T>
00148 struct Hash<const T&> : public Hash<T> {} ;
00149
00151 template <class T>
00152 inline std::size_t Hash<T>::operator() ( const T& key ) const
00153 {
00154 using namespace boost ;
00155 return hash_value ( key ) ;
00156 }
00158 template <class T>
00159 inline std::size_t Hash<T*>::operator() ( const T* key ) const
00160 {
00161 using namespace boost ;
00162 return hash_value ( key ) ;
00163 }
00165 template <>
00166 inline std::size_t Hash<char*>::operator() ( const char* key ) const
00167 {
00168 std::size_t seed = 0 ;
00169 while ( key ) { boost::hash_combine ( seed , *key ) ; ++key ; }
00170 return seed ;
00171 }
00172
00173
00174 }
00175
00176
00177
00178 #endif // GAUDIKERNEL_HASH_H
00179