Saga
Saga Game Engine
Loading...
Searching...
No Matches
tupleHash.h
Go to the documentation of this file.
1#pragma once
2
3#include <tuple>
4
5namespace std{
6 namespace
7 {
8 // Code from boost
9 // Reciprocal of the golden ratio helps spread entropy
10 // and handles duplicates.
11 // See Mike Seymour in magic-numbers-in-boosthash-combine:
12 // https://stackoverflow.com/questions/4948780
13
14 template <class T>
15 inline void hash_combine(std::size_t& seed, T const& v)
16 {
17 seed ^= hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
18 }
19
20 // Recursive template code derived from Matthieu M.
21 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
23 {
24 static void apply(size_t& seed, Tuple const& tuple)
25 {
27 hash_combine(seed, get<Index>(tuple));
28 }
29 };
30
31 template <class Tuple>
32 struct HashValueImpl<Tuple,0>
33 {
34 static void apply(size_t& seed, Tuple const& tuple)
35 {
36 hash_combine(seed, get<0>(tuple));
37 }
38 };
39 }
40
41 template <typename ... TT>
42 struct hash<std::tuple<TT...>>
43 {
44 size_t
45 operator()(std::tuple<TT...> const& tt) const
46 {
47 size_t seed = 0;
48 HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
49 return seed;
50 }
51
52 };
53}
void hash_combine(std::size_t &seed, T const &v)
Definition: tupleHash.h:15
Definition: tupleHash.h:5
static void apply(size_t &seed, Tuple const &tuple)
Definition: tupleHash.h:34
static void apply(size_t &seed, Tuple const &tuple)
Definition: tupleHash.h:24
size_t operator()(std::tuple< TT... > const &tt) const
Definition: tupleHash.h:45