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