Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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 <cstddef>
9 #include <numeric>
10 // ============================================================================
11 // Boost
12 // ============================================================================
13 #include "boost/functional/hash.hpp"
14 // ============================================================================
15 namespace GaudiUtils {
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 {
29  // ========================================================================
31  inline std::size_t operator()( const T& key ) const {
32  const char* p = reinterpret_cast<const char*>( &key );
33  return std::accumulate( p, p + sizeof( T ), std::size_t{0},
34  []( std::size_t res, const char& c ) { return ( res << 1 ) ^ c; } );
35  }
36  // ========================================================================
37  };
38 
39  // ==========================================================================
92  template <class T>
93  struct Hash {
94  // ========================================================================
96  inline std::size_t operator()( const T& key ) const;
97  // ========================================================================
98  };
99  // ==========================================================================
101  template <class T>
102  struct Hash<T*> {
103  // ========================================================================
105  inline std::size_t operator()( const T* key ) const;
106  // ========================================================================
107  };
108  // ==========================================================================
110  template <class T, unsigned N>
111  struct Hash<T ( & )[N]> {
112  // ========================================================================
114  inline std::size_t operator()( T ( &key )[N] ) const { return boost::hash_range( key, key + N ); }
115  // ========================================================================
116  };
118  template <class T, unsigned N>
119  struct Hash<const T ( & )[N]> {
120  // ========================================================================
122  inline std::size_t operator()( const T ( &key )[N] ) const { return boost::hash_range( key, key + N ); }
123  // ========================================================================
124  };
125  // ==========================================================================
127  template <class T>
128  struct Hash<const T> : public Hash<T> {};
130  template <class T>
131  struct Hash<const T*> : public Hash<T*> {};
133  template <class T>
134  struct Hash<T&> : public Hash<T> {};
136  template <class T>
137  struct Hash<const T&> : public Hash<T> {};
138  // ==========================================================================
140  template <class T>
141  inline std::size_t Hash<T>::operator()( const T& key ) const {
142  using namespace boost;
143  return hash_value( key );
144  }
146  template <class T>
147  inline std::size_t Hash<T*>::operator()( const T* key ) const {
148  using namespace boost;
149  return hash_value( key );
150  }
152  template <>
153  inline std::size_t Hash<char*>::operator()( const char* key ) const {
154  std::size_t seed = 0;
155  while ( key ) {
156  boost::hash_combine( seed, *key );
157  ++key;
158  }
159  return seed;
160  }
161 
162  // ==========================================================================
163 } // end of namespace GaudiUtils
164 // ============================================================================
165 // The END
166 // ============================================================================
167 #endif // GAUDIKERNEL_HASH_H
std::size_t operator()(T(&key)[N]) const
the hash-function
Definition: Hash.h:114
int N
Definition: IOTest.py:99
the partial specialization for pointers
Definition: Hash.h:102
Simple hash function.
Definition: Hash.h:93
std::size_t hash_value(TupleID const &b)
Definition: TupleID.h:23
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:31
std::size_t operator()(const T &key) const
the hash-function
Definition: Hash.h:141
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
T accumulate(T...args)
std::size_t operator()(const T(&key)[N]) const
the hash-function
Definition: Hash.h:122