Caffe2 - C++ API
A deep learning, cross platform ML framework
hash.h
1 #pragma once
2 
3 #include <functional>
4 #include <vector>
5 
6 namespace torch {
7 
8 // NOTE: hash_combine is based on implementation from Boost
9 //
10 // Boost Software License - Version 1.0 - August 17th, 2003
11 //
12 // Permission is hereby granted, free of charge, to any person or organization
13 // obtaining a copy of the software and accompanying documentation covered by
14 // this license (the "Software") to use, reproduce, display, distribute,
15 // execute, and transmit the Software, and to prepare derivative works of the
16 // Software, and to permit third-parties to whom the Software is furnished to
17 // do so, all subject to the following:
18 //
19 // The copyright notices in the Software and this entire statement, including
20 // the above license grant, this restriction and the following disclaimer,
21 // must be included in all copies of the Software, in whole or in part, and
22 // all derivative works of the Software, unless such copies or derivative
23 // works are solely in the form of machine-executable object code generated by
24 // a source language processor.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 // DEALINGS IN THE SOFTWARE.
33 
34 inline size_t hash_combine(size_t seed, size_t value) {
35  return seed ^ (value + 0x9e3779b9 + (seed << 6u) + (seed >> 2u));
36 }
37 
39 // torch::hash implementation
41 
42 namespace _hash_detail {
43 
44 // Use template argument deduction to shorten calls to torch::hash
45 template<typename T>
46 size_t simple_get_hash(const T& o);
47 
48 template<typename T, typename V>
49 using type_if_not_enum = typename std::enable_if<!std::is_enum<T>::value, V>::type;
50 
51 // Use SFINAE to dispatch to std::hash if possible, cast enum types to int automatically,
52 // and fall back to T::hash otherwise.
53 // NOTE: C++14 added support for hashing enum types to the standard, and some compilers
54 // implement it even when C++14 flags aren't specified. This is why we have to disable
55 // this overload if T is an enum type (and use the one below in this case).
56 template<typename T>
57 auto dispatch_hash(const T& o) -> decltype(std::hash<T>()(o), type_if_not_enum<T, size_t>()) {
58  return std::hash<T>()(o);
59 }
60 
61 template<typename T>
62 typename std::enable_if<std::is_enum<T>::value, size_t>::type dispatch_hash(const T& o) {
63  using R = typename std::underlying_type<T>::type;
64  return std::hash<R>()(static_cast<R>(o));
65 }
66 
67 template<typename T>
68 auto dispatch_hash(const T& o) -> decltype(T::hash(o), size_t()) {
69  return T::hash(o);
70 }
71 
72 } // namespace _hash_detail
73 
74 // Hasher struct
75 template<typename T>
76 struct hash {
77  size_t operator()(const T& o) const {
78  return _hash_detail::dispatch_hash(o);
79  };
80 };
81 
82 // Specialization for std::tuple
83 template<typename... Types>
84 struct hash<std::tuple<Types...>> {
85  template<size_t idx, typename... Ts>
86  struct tuple_hash {
87  size_t operator()(const std::tuple<Ts...>& t) const {
88  return hash_combine(_hash_detail::simple_get_hash(std::get<idx>(t)),
89  tuple_hash<idx-1, Ts...>()(t));
90  }
91  };
92 
93  template<typename... Ts>
94  struct tuple_hash<0, Ts...> {
95  size_t operator()(const std::tuple<Ts...>& t) const {
96  return _hash_detail::simple_get_hash(std::get<0>(t));
97  }
98  };
99 
100  size_t operator()(const std::tuple<Types...>& t) const {
101  return tuple_hash<sizeof...(Types)-1, Types...>()(t);
102  }
103 };
104 
105 // Specialization for std::vector
106 template<typename T>
107 struct hash<std::vector<T>> {
108  size_t operator()(const std::vector<T>& v) const {
109  size_t seed = 0;
110  for (const auto & elem : v) {
111  seed = hash_combine(seed, _hash_detail::simple_get_hash(elem));
112  }
113  return seed;
114  }
115 };
116 
117 namespace _hash_detail {
118 
119 template<typename T>
120 size_t simple_get_hash(const T& o) {
121  return torch::hash<T>()(o);
122 }
123 
124 } // namespace _hash_detail
125 
126 // Use this function to actually hash multiple things in one line.
127 // Dispatches to torch::hash, so it can hash containers.
128 // Example:
129 //
130 // static size_t hash(const MyStruct& s) {
131 // return get_hash(s.member1, s.member2, s.member3);
132 // }
133 template<typename... Types>
134 size_t get_hash(const Types&... args) {
135  return torch::hash<decltype(std::tie(args...))>()(std::tie(args...));
136 }
137 
138 } // namespace torch
Definition: jit_type.h:17