The Gaudi Framework  v29r0 (ff2e7097)
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 <cstddef>
9 #include <numeric>
10 // ============================================================================
11 // Boost
12 // ============================================================================
13 #include "boost/functional/hash.hpp"
14 // ============================================================================
15 namespace GaudiUtils
16 {
18  // To enable the generic hash for a user defined class, it is enough to add
19  // few lines to the .cpp files that needs them
20  // @code
21  // #include "GaudiKernel/Hash.h"
22  // class MyType;
23  // namespace GaudiUtils {
24  // template <>
25  // struct Hash<MyType>: public GenericHash<MyType> {};
26  // }
27  // @endcode
28  template <class T>
29  struct GenericHash {
30  // ========================================================================
32  inline std::size_t operator()( const T& key ) const
33  {
34  const char* p = reinterpret_cast<const char*>( &key );
35  return std::accumulate( p, p + sizeof( T ), std::size_t{0},
36  []( std::size_t res, const char& c ) { return ( res << 1 ) ^ c; } );
37  }
38  // ========================================================================
39  };
40 
41  // ==========================================================================
94  template <class T>
95  struct Hash {
96  // ========================================================================
98  inline std::size_t operator()( const T& key ) const;
99  // ========================================================================
100  };
101  // ==========================================================================
103  template <class T>
104  struct Hash<T*> {
105  // ========================================================================
107  inline std::size_t operator()( const T* key ) const;
108  // ========================================================================
109  };
110  // ==========================================================================
112  template <class T, unsigned N>
113  struct Hash<T ( & )[N]> {
114  // ========================================================================
116  inline std::size_t operator()( T ( &key )[N] ) const { return boost::hash_range( key, key + N ); }
117  // ========================================================================
118  };
120  template <class T, unsigned N>
121  struct Hash<const T ( & )[N]> {
122  // ========================================================================
124  inline std::size_t operator()( const T ( &key )[N] ) const { return boost::hash_range( key, key + N ); }
125  // ========================================================================
126  };
127  // ==========================================================================
129  template <class T>
130  struct Hash<const T> : public Hash<T> {
131  };
133  template <class T>
134  struct Hash<const T*> : public Hash<T*> {
135  };
137  template <class T>
138  struct Hash<T&> : public Hash<T> {
139  };
141  template <class T>
142  struct Hash<const T&> : public Hash<T> {
143  };
144  // ==========================================================================
146  template <class T>
147  inline std::size_t Hash<T>::operator()( const T& key ) const
148  {
149  using namespace boost;
150  return hash_value( key );
151  }
153  template <class T>
154  inline std::size_t Hash<T*>::operator()( const T* key ) const
155  {
156  using namespace boost;
157  return hash_value( key );
158  }
160  template <>
161  inline std::size_t Hash<char*>::operator()( const char* key ) const
162  {
163  std::size_t seed = 0;
164  while ( key ) {
165  boost::hash_combine( seed, *key );
166  ++key;
167  }
168  return seed;
169  }
170 
171  // ==========================================================================
172 } // end of namespace GaudiUtils
173 // ============================================================================
174 // The END
175 // ============================================================================
176 #endif // GAUDIKERNEL_HASH_H
177 // ============================================================================
std::size_t operator()(T(&key)[N]) const
the hash-function
Definition: Hash.h:116
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:207
int N
Definition: IOTest.py:101
the partial specialization for pointers
Definition: Hash.h:104
Simple hash function.
Definition: Hash.h:95
Generic hash implementation (for easy migration to the new Hash class).
Definition: Hash.h:29
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:147
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:140
T accumulate(T...args)
std::size_t operator()(const T(&key)[N]) const
the hash-function
Definition: Hash.h:124