HashTuple.h
Go to the documentation of this file.
1 // From https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set
2 // Maybe we should put something like this in a standard header?
3 namespace std
4 {
5  namespace
6  {
7  template<class T> inline void hash_combine(std::size_t& seed, const T& v)
8  {
9  seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
10  }
11 
12  // Recursive template code derived from Matthieu M.
14  struct HashValueImpl
15  {
16  static void apply(size_t& seed, const Tuple& tuple)
17  {
18  HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
19  hash_combine(seed, std::get<Index>(tuple));
20  }
21  };
22 
23  template<class Tuple> struct HashValueImpl<Tuple, 0>
24  {
25  static void apply(size_t& seed, const Tuple& tuple)
26  {
27  hash_combine(seed, std::get<0>(tuple));
28  }
29  };
30  }
31 
32  template<class... TT> struct hash<std::tuple<TT...>>
33  {
34  size_t operator()(const std::tuple<TT...>& tt) const
35  {
36  size_t seed = 0;
37  HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
38  return seed;
39  }
40  };
41 }
size_t operator()(const std::tuple< TT... > &tt) const
Definition: HashTuple.h:34
STL namespace.
void * Tuple
Definition: DBFolder.h:13
Definition: type_traits.h:61