hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
hash.H
Go to the documentation of this file.
1 /*
2  * hash : utilities for hashing data
3  */
4 
5 #ifndef HOBBES_UTIL_HASH_H_INCLUDED
6 #define HOBBES_UTIL_HASH_H_INCLUDED
7 
8 #include <functional>
9 #include <unordered_map>
10 #include <map>
11 #include <vector>
12 #include <tuple>
13 
14 namespace hobbes {
15 
16 template <typename T>
17  struct genHash {
18  inline size_t operator()(const T& x) const {
19  static std::hash<T> h;
20  return h(x);
21  }
22  };
23 
24 template <typename T>
25  inline void hashAppend(std::size_t& seed, const T& x) {
26  genHash<T> hasher;
27  seed ^= hasher(x) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
28  }
29 
30 // hash tuples
31 template <std::size_t i, typename ... Ts>
32  inline typename std::enable_if<i == sizeof...(Ts), void>::type hashTuple(std::size_t*, const std::tuple<Ts...>&) {
33  }
34 template <std::size_t i, typename ... Ts>
35  inline typename std::enable_if<i < sizeof...(Ts), void>::type hashTuple(std::size_t* r, const std::tuple<Ts...>& ms) {
36  hashAppend(*r, std::get<i>(ms));
37  hashTuple<i+1, Ts...>(r, ms);
38  }
39 template <typename ... Ts>
40  struct genHash<std::tuple<Ts...>> {
41  inline size_t operator()(const std::tuple<Ts...>& xs) const {
42  size_t r = 0;
43  hashTuple<0, Ts...>(&r, xs);
44  return r;
45  }
46  };
47 
48 // hash pairs
49 template <typename U, typename V>
50  struct genHash< std::pair<U, V> > {
51  inline size_t operator()(const std::pair<U, V>& x) const {
52  size_t r = 0;
53  hashAppend<U>(r, x.first);
54  hashAppend<V>(r, x.second);
55  return r;
56  }
57  };
58 
59 // hash vectors
60 template <typename T>
61  struct genHash< std::vector<T> > {
62  inline size_t operator()(const std::vector<T>& xs) const {
63  size_t r = 0;
64  hashAppend<size_t>(r, xs.size());
65  for (const auto& x : xs) {
66  hashAppend<T>(r, x);
67  }
68  return r;
69  }
70  };
71 
72 }
73 
74 #endif
75 
Definition: hash.H:17
size_t operator()(const T &x) const
Definition: hash.H:18
std::enable_if< i==sizeof...(Ts), void >::type hashTuple(std::size_t *, const std::tuple< Ts... > &)
Definition: hash.H:32
std::pair< std::string, std::string > pair
Definition: str.H:220
Definition: boot.H:7
size_t r(const reader::MetaData &md, size_t o, T *t)
Definition: storage.H:1730
void hashAppend(std::size_t &seed, const T &x)
Definition: hash.H:25
MonoTypePtr tuple(const MonoTypes &mtys=MonoTypes())
Definition: type.H:1068