- Code: Select all
namespace
{
// Code from boost
// Reciprocal of the golden ratio helps spread entropy
// and handles duplicates.
// See Mike Seymour in magic-numbers-in-boosthash-combine:
// http://stackoverflow.com/questions/4948780
template <class T>
inline void hash_combine(uint& seed, T const& v)
{
seed ^= qHash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
// Recursive template code derived from Matthieu M.
template <class Tuple, uint Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl
{
static void apply(uint& seed, Tuple const& tuple)
{
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple>
struct HashValueImpl<Tuple, 0>
{
static void apply(uint& seed, Tuple const& tuple)
{
hash_combine(seed, std::get<0>(tuple));
}
};
}
template <typename ... TT>
inline uint qHash(std::tuple<TT...> const& tt)
{
uint seed = 0;
HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
return seed;
}