Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Hash.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 
13 #include <cstddef>
14 #include <numeric>
15 
16 #include <boost/functional/hash.hpp>
17 
18 namespace GaudiUtils {
20  // To enable the generic hash for a user defined class, it is enough to add
21  // few lines to the .cpp files that needs them
22  // @code
23  // #include <GaudiKernel/Hash.h>
24  // class MyType;
25  // namespace GaudiUtils {
26  // template <>
27  // struct Hash<MyType>: public GenericHash<MyType> {};
28  // }
29  // @endcode
30  template <class T>
31  struct GenericHash {
33  inline std::size_t operator()( const T& key ) const {
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 
92  template <class T>
93  struct Hash {
95  inline std::size_t operator()( const T& key ) const;
96  };
98  template <class T>
99  struct Hash<T*> {
101  inline std::size_t operator()( const T* key ) const;
102  };
104  template <class T, unsigned N>
105  struct Hash<T ( & )[N]> {
107  inline std::size_t operator()( T ( &key )[N] ) const { return boost::hash_range( key, key + N ); }
108  };
110  template <class T, unsigned N>
111  struct Hash<const T ( & )[N]> {
113  inline std::size_t operator()( const T ( &key )[N] ) const { return boost::hash_range( key, key + N ); }
114  };
116  template <class T>
117  struct Hash<const T> : public Hash<T> {};
119  template <class T>
120  struct Hash<const T*> : public Hash<T*> {};
122  template <class T>
123  struct Hash<T&> : public Hash<T> {};
125  template <class T>
126  struct Hash<const T&> : public Hash<T> {};
128  template <class T>
129  inline std::size_t Hash<T>::operator()( const T& key ) const {
130  using namespace boost;
131  return hash_value( key );
132  }
134  template <class T>
135  inline std::size_t Hash<T*>::operator()( const T* key ) const {
136  using namespace boost;
137  return hash_value( key );
138  }
140  template <>
141  inline std::size_t Hash<char*>::operator()( const char* key ) const {
142  std::size_t seed = 0;
143  while ( key ) {
144  boost::hash_combine( seed, *key );
145  ++key;
146  }
147  return seed;
148  }
149 } // namespace GaudiUtils
GaudiUtils::GenericHash
Generic hash implementation (for easy migration to the new Hash class).
Definition: Hash.h:31
IOTest.N
N
Definition: IOTest.py:112
gaudirun.c
c
Definition: gaudirun.py:525
GaudiUtils::GenericHash::operator()
std::size_t operator()(const T &key) const
the generic hash function
Definition: Hash.h:33
GaudiUtils::Hash< T * >
the partial specialization for pointers
Definition: Hash.h:99
GaudiUtils::Hash::operator()
std::size_t operator()(const T &key) const
the hash-function
Definition: Hash.h:129
GaudiUtils::Hash< const T(&)[N]>::operator()
std::size_t operator()(const T(&key)[N]) const
the hash-function
Definition: Hash.h:113
Gaudi::hash_value
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition: StringKey.h:153
GaudiUtils::Hash
Definition: Hash.h:93
OffloadAtlasMCRecoScenario.seed
seed
Definition: OffloadAtlasMCRecoScenario.py:52
GaudiUtils::Hash< T(&)[N]>::operator()
std::size_t operator()(T(&key)[N]) const
the hash-function
Definition: Hash.h:107
GaudiUtils
Definition: Allocator.h:62
Gaudi::Accumulators::accumulate
void accumulate(Counter &counter, const Container &container, Fun f=Identity{})
A helper function for accumulating data from a container into a counter This is internally using buff...
Definition: Accumulators.h:1227
ProduceConsume.key
key
Definition: ProduceConsume.py:84