From 96a5bc950c7bd0b2cbd7c8b80f2c4c1f9843f39b Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 24 Feb 2015 20:00:13 +0100 Subject: [PATCH] Added HashCombine function Former-commit-id: 852deae33143138f2841e226690bfef94d3ba2b9 --- include/Nazara/Core/Algorithm.hpp | 2 ++ include/Nazara/Core/Algorithm.inl | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/Nazara/Core/Algorithm.hpp b/include/Nazara/Core/Algorithm.hpp index f094c8976..26c9efbcf 100644 --- a/include/Nazara/Core/Algorithm.hpp +++ b/include/Nazara/Core/Algorithm.hpp @@ -8,8 +8,10 @@ #define NAZARA_ALGORITHM_CORE_HPP #include +#include #include +template void NzHashCombine(std::size_t& seed, const T& v); template void NzUnpackTuple(F func, const std::tuple& t); #include diff --git a/include/Nazara/Core/Algorithm.inl b/include/Nazara/Core/Algorithm.inl index 5a8bbeccf..fa1a388ab 100644 --- a/include/Nazara/Core/Algorithm.inl +++ b/include/Nazara/Core/Algorithm.inl @@ -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 +void NzHashCombine(std::size_t& seed, const T& v) +{ + const std::size_t kMul = 0x9ddfea08eb382d69ULL; + + std::hash 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 void NzUnpackTuple(F func, const std::tuple& t) {