Gaudi Framework, version v23r4

Home   Generated: Mon Sep 17 2012

Hash.h

Go to the documentation of this file.
00001 // $Id: Hash.h,v 1.6 2007/05/23 18:05:15 marcocle Exp $
00002 // ============================================================================
00003 #ifndef GAUDIKERNEL_HASH_H
00004 #define GAUDIKERNEL_HASH_H 1
00005 // ============================================================================
00006 // Include files
00007 // ============================================================================
00008 // STD & STL
00009 // ============================================================================
00010 #include <functional>
00011 // ============================================================================
00012 // Boost
00013 // ============================================================================
00014 #include "boost/functional/hash.hpp"
00015 // ============================================================================
00016 namespace GaudiUtils
00017 {
00019   //  To enable the generic hash for a user defined class, it is enough to add
00020   //  few lines to the .cpp files that needs them
00021   //  @code
00022   //  #include "GaudiKernel/Hash.h"
00023   //  class MyType;
00024   //  namespace GaudiUtils {
00025   //    template <>
00026   //    struct Hash<MyType>: public GenericHash<MyType> {};
00027   //  }
00028   //  @endcode
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 } // end of namespace GaudiUtils
00175 // ============================================================================
00176 // The END
00177 // ============================================================================
00178 #endif // GAUDIKERNEL_HASH_H
00179 // ============================================================================
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Sep 17 2012 13:49:30 for Gaudi Framework, version v23r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004