From 2b2ed5072128df9dc262fac1a5e40635a6ff71b3 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Wed, 25 Jun 2014 18:50:46 +0200 Subject: [PATCH] add a hash combine implementation that has some minor performance guarantees --- Util/StdHashExtensions.h | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Util/StdHashExtensions.h b/Util/StdHashExtensions.h index 29663f1d3..d2f2fc4fe 100644 --- a/Util/StdHashExtensions.h +++ b/Util/StdHashExtensions.h @@ -32,13 +32,41 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +// this is largely inspired by boost's hash combine +template +inline void hash_combine(std::size_t &seed, const T& val) +{ + seed ^= std::hash()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2); +} + +template +inline void hash_val(std::size_t &seed, const T& val) +{ + hash_combine(seed, val); +} + +template +inline void hash_val(std::size_t &seed, const T& val, const Types& ... args) +{ + hash_combine(seed, val); + hash_val(seed, args ...); +} + +template +inline std::size_t hash_val( const Types&... args) +{ + std::size_t seed = 0; + hash_val(seed, args...); + return seed; +} + namespace std { template <> struct hash> { size_t operator()(const std::pair &pair) const { - return std::hash()(pair.first) ^ std::hash()(pair.second); + return hash_val(pair.first, pair.second); } }; }