The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
18namespace 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 };
103
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 };
109
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 };
115
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 }
133
134
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 }
139
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
Generic hash implementation (for easy migration to the new Hash class).
Definition Hash.h:31
std::size_t operator()(const T &key) const
the generic hash function
Definition Hash.h:33
Simple hash function.
Definition Hash.h:93
std::size_t operator()(const T *key) const
the hash-function
Definition Hash.h:135
std::size_t operator()(T(&key)[N]) const
the hash-function
Definition Hash.h:107
std::size_t operator()(const T(&key)[N]) const
the hash-function
Definition Hash.h:113
std::size_t operator()(const T &key) const
the hash-function
Definition Hash.h:129