Added HashCombine function

Former-commit-id: 852deae33143138f2841e226690bfef94d3ba2b9
This commit is contained in:
Lynix 2015-02-24 20:00:13 +01:00
parent cadeae5e21
commit 96a5bc950c
2 changed files with 19 additions and 0 deletions

View File

@ -8,8 +8,10 @@
#define NAZARA_ALGORITHM_CORE_HPP #define NAZARA_ALGORITHM_CORE_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <functional>
#include <tuple> #include <tuple>
template<typename T> void NzHashCombine(std::size_t& seed, const T& v);
template<typename F, typename... ArgsT> void NzUnpackTuple(F func, const std::tuple<ArgsT...>& t); template<typename F, typename... ArgsT> void NzUnpackTuple(F func, const std::tuple<ArgsT...>& t);
#include <Nazara/Core/Algorithm.inl> #include <Nazara/Core/Algorithm.inl>

View File

@ -30,6 +30,23 @@ struct NzImplTupleUnpack<0>
} }
}; };
// Algorithme venant de CityHash par Google
// http://stackoverflow.com/questions/8513911/how-to-create-a-good-hash-combine-with-64-bit-output-inspired-by-boosthash-co
template<typename T>
void NzHashCombine(std::size_t& seed, const T& v)
{
const std::size_t kMul = 0x9ddfea08eb382d69ULL;
std::hash<T> hasher;
std::size_t a = (hasher(v) ^ seed) * kMul;
a ^= (a >> 47);
std::size_t b = (seed ^ a) * kMul;
b ^= (b >> 47);
seed = b * kMul;
}
template<typename F, typename... ArgsT> template<typename F, typename... ArgsT>
void NzUnpackTuple(F func, const std::tuple<ArgsT...>& t) void NzUnpackTuple(F func, const std::tuple<ArgsT...>& t)
{ {