All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Hash.h
Go to the documentation of this file.
1 // $Id: Hash.h,v 1.6 2007/05/23 18:05:15 marcocle Exp $
2 // ============================================================================
3 #ifndef GAUDIKERNEL_HASH_H
4 #define GAUDIKERNEL_HASH_H 1
5 // ============================================================================
6 // Include files
7 // ============================================================================
8 // STD & STL
9 // ============================================================================
10 #include <functional>
11 // ============================================================================
12 // Boost
13 // ============================================================================
14 #include "boost/functional/hash.hpp"
15 // ============================================================================
16 namespace GaudiUtils
17 {
19  // To enable the generic hash for a user defined class, it is enough to add
20  // few lines to the .cpp files that needs them
21  // @code
22  // #include "GaudiKernel/Hash.h"
23  // class MyType;
24  // namespace GaudiUtils {
25  // template <>
26  // struct Hash<MyType>: public GenericHash<MyType> {};
27  // }
28  // @endcode
29  template <class T>
30  struct GenericHash : public std::unary_function<T,std::size_t>
31  {
32  // ========================================================================
34  inline std::size_t operator() ( const T& key ) const {
35  std::size_t res = 0 ;
36  std::size_t len = sizeof(T) ;
37  const char* p = reinterpret_cast<const char*>( &key );
38  while( len-- ) { res = ( res << 1 ) ^ *p; ++p; }
39  return res;
40  }
41  // ========================================================================
42  };
43 
44  // ==========================================================================
97  template <class T>
98  struct Hash : public std::unary_function<T,std::size_t>
99  {
100  // ========================================================================
102  inline std::size_t operator() ( const T& key ) const;
103  // ========================================================================
104  };
105  // ==========================================================================
107  template <class T>
108  struct Hash<T*> : public std::unary_function<const T*,std::size_t>
109  {
110  // ========================================================================
112  inline std::size_t operator() ( const T* key ) const;
113  // ========================================================================
114  };
115  // ==========================================================================
117  template <class T, unsigned N>
118  struct Hash<T(&)[N]> : public std::unary_function<T(&)[N],std::size_t>
119  {
120  // ========================================================================
122  inline std::size_t operator() ( T (&key) [N] ) const
123  { return boost::hash_range ( key , key + N ) ; }
124  // ========================================================================
125  } ;
127  template <class T, unsigned N>
128  struct Hash<const T(&)[N]> : public std::unary_function<const T(&)[N],std::size_t>
129  {
130  // ========================================================================
132  inline std::size_t operator() ( const T (&key) [N] ) const
133  { return boost::hash_range ( key , key + N ) ; }
134  // ========================================================================
135  } ;
136  // ==========================================================================
138  template<class T>
139  struct Hash<const T> : public Hash<T> {} ;
141  template<class T>
142  struct Hash<const T*> : public Hash<T*> {} ;
144  template<class T>
145  struct Hash<T&> : public Hash<T> {} ;
147  template<class T>
148  struct Hash<const T&> : public Hash<T> {} ;
149  // ==========================================================================
151  template <class T>
152  inline std::size_t Hash<T>::operator() ( const T& key ) const
153  {
154  using namespace boost ;
155  return hash_value ( key ) ;
156  }
158  template <class T>
159  inline std::size_t Hash<T*>::operator() ( const T* key ) const
160  {
161  using namespace boost ;
162  return hash_value ( key ) ;
163  }
165  template <>
166  inline std::size_t Hash<char*>::operator() ( const char* key ) const
167  {
168  std::size_t seed = 0 ;
169  while ( key ) { boost::hash_combine ( seed , *key ) ; ++key ; }
170  return seed ;
171  }
172 
173  // ==========================================================================
174 } // end of namespace GaudiUtils
175 // ============================================================================
176 // The END
177 // ============================================================================
178 #endif // GAUDIKERNEL_HASH_H
179 // ============================================================================
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition: StringKey.h:216
int N
Definition: IOTest.py:90
the partial specialization for pointers
Definition: Hash.h:108
Simple hash function.
Definition: Hash.h:98
Generic hash implementation (for easy migration to the new Hash class).
Definition: Hash.h:30
std::size_t operator()(const T &key) const
the generic hash function
Definition: Hash.h:34
std::size_t operator()(const T &key) const
the hash-function
Definition: Hash.h:152