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 
12 
13 namespace drake {
14 
16 template <class T>
17 size_t hash_combine(size_t seed, const T& v);
18 
19 template <class T, class... Rest>
20 size_t hash_combine(size_t seed, const T& v, Rest... rest) {
21  return hash_combine(hash_combine(seed, v), rest...);
22 }
23 
25 template <typename It>
26 size_t hash_range(It first, It last) {
27  size_t seed{};
28  for (; first != last; ++first) {
29  seed = hash_combine(seed, *first);
30  }
31  return seed;
32 }
33 
35 template <class T>
36 struct hash_value {
37  size_t operator()(const T& v) { return std::hash<T>{}(v); }
38 };
39 
41 template <class T1, class T2>
42 struct hash_value<std::pair<T1, T2>> {
43  size_t operator()(const std::pair<T1, T2>& p) {
44  return hash_combine(0, p.first, p.second);
45  }
46 };
47 
49 template <class T>
50 struct hash_value<std::vector<T>> {
51  size_t operator()(const std::vector<T>& vec) {
52  return hash_range(vec.begin(), vec.end());
53  }
54 };
55 
57 template <class T>
58 struct hash_value<std::set<T>> {
59  size_t operator()(const std::set<T>& s) {
60  return hash_range(s.begin(), s.end());
61  }
62 };
63 
65 template <class T1, class T2>
66 struct hash_value<std::map<T1, T2>> {
67  size_t operator()(const std::map<T1, T2>& map) {
68  return hash_range(map.begin(), map.end());
69  }
70 };
71 
74 template <class T>
75 inline size_t hash_combine(size_t seed, const T& v) {
76  seed ^= hash_value<T>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
77  return seed;
78 }
79 } // 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:75
Definition: automotive_demo.cc:88
size_t operator()(const std::map< T1, T2 > &map)
Definition: hash.h:67
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:26
std::vector< double > vector
Definition: translator_test.cc:20
Computes the hash value of v using std::hash.
Definition: hash.h:36
int v
Definition: rgbd_camera_test.cc:165
Provides Drake&#39;s assertion implementation.
size_t operator()(const std::pair< T1, T2 > &p)
Definition: hash.h:43
size_t operator()(const std::set< T > &s)
Definition: hash.h:59
size_t operator()(const std::vector< T > &vec)
Definition: hash.h:51
size_t operator()(const T &v)
Definition: hash.h:37