dReal4
hash.h
1 #pragma once
2 
3 #include <cstddef>
4 #include <functional>
5 #include <iostream>
6 #include <map>
7 #include <set>
8 #include <utility>
9 #include <vector>
10 
11 namespace dreal {
12 namespace drake {
13 
14 /** Combines a given hash value @p seed and a hash of parameter @p v. */
15 template <class T>
16 size_t hash_combine(size_t seed, const T& v);
17 
18 template <class T, class... Rest>
19 size_t hash_combine(size_t seed, const T& v, Rest... rest) {
20  return hash_combine(hash_combine(seed, v), rest...);
21 }
22 
23 /** Computes the combined hash value of the elements of an iterator range. */
24 template <typename It>
25 size_t hash_range(It first, It last) {
26  size_t seed{};
27  for (; first != last; ++first) {
28  seed = hash_combine(seed, *first);
29  }
30  return seed;
31 }
32 
33 /** Computes the hash value of @p v using std::hash. */
34 template <class T>
35 struct hash_value {
36  size_t operator()(const T& v) const { return std::hash<T>{}(v); }
37 };
38 
39 /** Computes the hash value of a pair @p p. */
40 template <class T1, class T2>
41 struct hash_value<std::pair<T1, T2>> {
42  size_t operator()(const std::pair<T1, T2>& p) const {
43  return hash_combine(0, p.first, p.second);
44  }
45 };
46 
47 /** Computes the hash value of a vector @p vec. */
48 template <class T>
49 struct hash_value<std::vector<T>> {
50  size_t operator()(const std::vector<T>& vec) const {
51  return hash_range(vec.begin(), vec.end());
52  }
53 };
54 
55 /** Computes the hash value of a set @p s. */
56 template <class T>
57 struct hash_value<std::set<T>> {
58  size_t operator()(const std::set<T>& s) const {
59  return hash_range(s.begin(), s.end());
60  }
61 };
62 
63 /** Computes the hash value of a map @p map. */
64 template <class T1, class T2>
65 struct hash_value<std::map<T1, T2>> {
66  size_t operator()(const std::map<T1, T2>& map) const {
67  return hash_range(map.begin(), map.end());
68  }
69 };
70 
71 /** Combines two hash values into one. The following code is public domain
72  * according to http://www.burtleburtle.net/bob/hash/doobs.html. */
73 template <class T>
74 inline size_t hash_combine(size_t seed, const T& v) {
75  seed ^= hash_value<T>{}(v) + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
76  return seed;
77 }
78 } // namespace drake
79 } // namespace dreal
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
STL namespace.
Computes the hash value of v using std::hash.
Definition: hash.h:35