Created
July 13, 2013 12:48
std::hash specialization for std::tuple from StackOverflow.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef common_tuple_hash_h | |
#define common_tuple_hash_h | |
#include <tuple> | |
namespace std{ | |
namespace | |
{ | |
template <class t> | |
inline void hash_combine(std::size_t& seed, t const& v) | |
{ | |
seed ^= hash<t>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); | |
} | |
template <class tuple, | |
size_t index = std::tuple_size<tuple>::value - 1> | |
struct hashvalueimpl | |
{ | |
static void apply(size_t& seed, tuple const& tuple) | |
{ | |
hashvalueimpl<tuple, index-1>::apply(seed, tuple); | |
hash_combine(seed, get<index>(tuple)); | |
} | |
}; | |
template <class tuple> | |
struct hashvalueimpl<tuple,0> | |
{ | |
static void apply(size_t& seed, tuple const& tuple) | |
{ | |
hash_combine(seed, get<0>(tuple)); | |
} | |
}; | |
} | |
template <typename ... tt> | |
struct hash<std::tuple<tt...>> | |
{ | |
size_t | |
operator()(std::tuple<tt...> const& tt) const | |
{ | |
size_t seed = 0; | |
hashvalueimpl<std::tuple<tt...> >::apply(seed, tt); | |
return seed; | |
} | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment