3 #ifndef DUNE_COMMON_HASH_HH
4 #define DUNE_COMMON_HASH_HH
14 #include <tr1/functional>
19 #include <boost/version.hpp>
22 #if BOOST_VERSION >= 103400
23 #define HAVE_BOOST_HASH 1
26 #if !HAVE_STD_HASH && !HAVE_TR1_HASH
28 #include <boost/functional/hash.hpp>
30 #endif // !HAVE_STD_HASH && !HAVE_TR1_HASH
31 #endif // BOOST_VERSION >= 103400
32 #endif // HAVE_DUNE_BOOST
126 #define DUNE_DEFINE_HASH(template_args,type)
135 #define DUNE_HASH_TEMPLATE_ARGS(...)
143 #define DUNE_HASH_TYPE(...)
145 #else // DOXYGEN - hide all the ugly implementation
157 #define HAVE_DUNE_HASH 1
169 #define DUNE_DEFINE_STD_HASH(template_args,type) \
172 template<template_args> \
175 std::size_t operator()(const type& arg) const \
177 return hash_value(arg); \
183 #else // HAVE_STD_HASH
186 #define DUNE_DEFINE_STD_HASH(template_args,type)
188 #endif // HAVE_STD_HASH
199 #ifndef HAVE_DUNE_HASH
203 #define HAVE_DUNE_HASH 1
208 using std::tr1::hash;
212 #endif // HAVE_DUNE_HASH
217 #define DUNE_DEFINE_TR1_HASH(template_args,type) \
221 template<template_args> \
224 std::size_t operator()(const type& arg) const \
226 return hash_value(arg); \
233 #else // HAVE_TR1_HASH
236 #define DUNE_DEFINE_TR1_HASH(template_args,type)
238 #endif // HAVE_TR1_HASH
246 #if HAVE_STD_HASH || HAVE_TR1_HASH
258 #define DUNE_HASH_TEMPLATE_ARGS(...) (__VA_ARGS__)
262 #define DUNE_HASH_TYPE(...) (__VA_ARGS__)
266 #define DUNE_HASH_EXPAND_VA_ARGS(...) __VA_ARGS__
269 #define DUNE_DEFINE_HASH(template_args,type) \
270 DUNE_DEFINE_STD_HASH(DUNE_HASH_EXPAND_VA_ARGS template_args, DUNE_HASH_EXPAND_VA_ARGS type) \
271 DUNE_DEFINE_TR1_HASH(DUNE_HASH_EXPAND_VA_ARGS template_args,DUNE_HASH_EXPAND_VA_ARGS type) \
274 #else // HAVE_STD_HASH || HAVE_TR1_HASH
278 #define DUNE_DEFINE_HASH(template_args,type)
285 #define DUNE_HASH_TEMPLATE_ARGS
288 #define DUNE_HASH_TYPE
290 #endif // HAVE_STD_HASH || HAVE_TR1_HASH
298 #if !HAVE_DUNE_HASH && HAVE_BOOST_HASH
302 #define HAVE_DUNE_HASH 1
314 #endif // !HAVE_DUNE_HASH && HAVE_BOOST_HASH
324 #if HAVE_DUNE_HASH || defined(DOXYGEN)
350 template<
int sizeof_
size_t>
351 struct hash_combiner;
356 struct hash_combiner<8>
359 template<
typename typeof_
size_t,
typename T>
360 void operator()(typeof_size_t& seed,
const T& arg)
const
362 dune_static_assert(
sizeof(typeof_size_t)==8,
"hash_combiner::operator() instantiated with nonmatching type and size");
378 const typeof_size_t kMul = 0x9ddfea08eb382d69ULL;
379 typeof_size_t h = hasher(arg);
380 typeof_size_t a = (seed ^ h) * kMul;
382 typeof_size_t b = (h ^ a) * kMul;
393 struct hash_combiner<4>
396 template<
typename typeof_
size_t,
typename T>
397 void operator()(typeof_size_t& seed,
const T& arg)
const
399 dune_static_assert(
sizeof(typeof_size_t)==4,
"hash_combiner::operator() instantiated with nonmatching type and size");
409 const typeof_size_t c1 = 0xcc9e2d51;
410 const typeof_size_t c2 = 0x1b873593;
411 const typeof_size_t c3 = 0xe6546b64;
412 typeof_size_t h = hasher(arg);
413 typeof_size_t a = seed * c1;
414 a = (a >> 17) | (a << (32 - 17));
417 h = (h >> 19) | (h << (32 - 19));
435 hash_combiner<sizeof(std::size_t)>()(seed,arg);
448 template<
typename It>
451 std::size_t seed = 0;
452 for (; first != last; ++first)
468 template<
typename It>
471 for (; first != last; ++first)
479 #endif // HAVE_DUNE_HASH || defined(DOXYGEN)
481 #endif // DUNE_COMMON_HASH_HH
std::size_t operator()(const T &t) const
Calculates the hash of t.
Definition: hash.hh:69
std::size_t hash_range(It first, It last)
Hashes all elements in the range [first,last) and returns the combined hash.
Definition: hash.hh:449
void hash_combine(std::size_t &seed, const T &arg)
Calculates the hash value of arg and combines it in-place with seed.
Definition: hash.hh:433
T t
Definition: alignment.hh:38
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
Traits for type conversions and type information.
Fallback implementation of the C++0x static_assert feature.
Functor for hashing objects of type T.
Definition: hash.hh:65