The Sparta Modeling Framework
Loading...
Searching...
No Matches
TupleHashCompute.hpp
1// <TupleHashCompute> -*- C++ -*-
2
3
12#pragma once
13
14namespace hashtuple {
15 template<typename TT>
16
18 struct hash {
19 size_t operator()(TT const& tt) const {
20 return std::hash<TT>()(tt);
21 }
22 };
23
24 namespace {
25 template<typename T>
26 inline void hash_combine(std::size_t& seed, T const& v) {
27 seed ^= hashtuple::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
28 }
29
31 // a std::tuple and second implicit template parameter is defaulted
32 // to the size of parameter pack contained in that tuple.
33 template<typename Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
34 struct HashValueImpl {
35 static void apply(size_t& seed, Tuple const& tuple) {
36
38 HashValueImpl<Tuple, Index -1>::apply(seed, tuple);
39 hash_combine(seed, std::get<Index>(tuple));
40 }
41 };
42
44 // has been popped.
45 template<typename Tuple>
46 struct HashValueImpl<Tuple, 0> {
47 static void apply(size_t& seed, Tuple const& tuple) {
48 hash_combine(seed, std::get<0>(tuple));
49 }
50 };
51 }
52
54 // is a std::tuple of parameter pack.
55 template<typename... TT>
56 struct hash<std::tuple<TT...>> {
57 size_t operator()(std::tuple<TT...> const& tt) const {
58 size_t seed = 0;
59
61 // std::tuple of parameter pack.
62 HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
63 return seed;
64 }
65 };
66} // namespace hashtuple
size_t operator()(std::tuple< TT... > const &tt) const
Base struct for types which have std::hash() defined.