Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Hash.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_HASH_H
2 #define GAUDIKERNEL_HASH_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <functional>
9 // ============================================================================
10 // Boost
11 // ============================================================================
12 #include "boost/functional/hash.hpp"
13 // ============================================================================
14 namespace GaudiUtils
15 {
17  // To enable the generic hash for a user defined class, it is enough to add
18  // few lines to the .cpp files that needs them
19  // @code
20  // #include "GaudiKernel/Hash.h"
21  // class MyType;
22  // namespace GaudiUtils {
23  // template <>
24  // struct Hash<MyType>: public GenericHash<MyType> {};
25  // }
26  // @endcode
27  template <class T>
28  struct GenericHash : public std::unary_function<T,std::size_t>
29  {
30  // ========================================================================
32  inline std::size_t operator() ( const T& key ) const {
33  std::size_t res = 0 ;
34  std::size_t len = sizeof(T) ;
35  const char* p = reinterpret_cast<const char*>( &key );
36  while( len-- ) { res = ( res << 1 ) ^ *p; ++p; }
37  return res;
38  }
39  // ========================================================================
40  };
41 
42  // ==========================================================================
95  template <class T>
96  struct Hash : public std::unary_function<T,std::size_t>
97  {
98  // ========================================================================
100  inline std::size_t operator() ( const T& key ) const;
101  // ========================================================================
102  };
103  // ==========================================================================
105  template <class T>
107  {
108  // ========================================================================
110  inline std::size_t operator() ( const T* key ) const;
111  // ========================================================================
112  };
113  // ==========================================================================
115  template <class T, unsigned N>
116  struct Hash<T(&)[N]> : public std::unary_function<T(&)[N],std::size_t>
117  {
118  // ========================================================================
120  inline std::size_t operator() ( T (&key) [N] ) const
121  { return boost::hash_range ( key , key + N ) ; }
122  // ========================================================================
123  } ;
125  template <class T, unsigned N>
126  struct Hash<const T(&)[N]> : public std::unary_function<const T(&)[N],std::size_t>
127  {
128  // ========================================================================
130  inline std::size_t operator() ( const T (&key) [N] ) const
131  { return boost::hash_range ( key , key + N ) ; }
132  // ========================================================================
133  } ;
134  // ==========================================================================
136  template<class T>
137  struct Hash<const T> : public Hash<T> {} ;
139  template<class T>
140  struct Hash<const T*> : public Hash<T*> {} ;
142  template<class T>
143  struct Hash<T&> : public Hash<T> {} ;
145  template<class T>
146  struct Hash<const T&> : public Hash<T> {} ;
147  // ==========================================================================
149  template <class T>
150  inline std::size_t Hash<T>::operator() ( const T& key ) const
151  {
152  using namespace boost ;
153  return hash_value ( key ) ;
154  }
156  template <class T>
157  inline std::size_t Hash<T*>::operator() ( const T* key ) const
158  {
159  using namespace boost ;
160  return hash_value ( key ) ;
161  }
163  template <>
164  inline std::size_t Hash<char*>::operator() ( const char* key ) const
165  {
166  std::size_t seed = 0 ;
167  while ( key ) { boost::hash_combine ( seed , *key ) ; ++key ; }
168  return seed ;
169  }
170 
171  // ==========================================================================
172 } // end of namespace GaudiUtils
173 // ============================================================================
174 // The END
175 // ============================================================================
176 #endif // GAUDIKERNEL_HASH_H
177 // ============================================================================
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
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:106
Simple hash function.
Definition: Hash.h:96
Generic hash implementation (for easy migration to the new Hash class).
Definition: Hash.h:28
std::size_t operator()(const T &key) const
the generic hash function
Definition: Hash.h:32
std::size_t operator()(const T &key) const
the hash-function
Definition: Hash.h:150
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:149