OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ |
7 | 7 |
8 #include <functional> | |
8 #include <type_traits> | 9 #include <type_traits> |
9 #include <unordered_map> | 10 #include <unordered_map> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/optional.h" | 13 #include "base/optional.h" |
13 #include "mojo/public/cpp/bindings/lib/template_util.h" | 14 #include "mojo/public/cpp/bindings/lib/template_util.h" |
14 | 15 |
15 namespace mojo { | 16 namespace mojo { |
16 namespace internal { | 17 namespace internal { |
17 | 18 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 } | 149 } |
149 return true; | 150 return true; |
150 } | 151 } |
151 }; | 152 }; |
152 | 153 |
153 template <typename T> | 154 template <typename T> |
154 bool Equals(const T& a, const T& b) { | 155 bool Equals(const T& a, const T& b) { |
155 return EqualsTraits<T>::Equals(a, b); | 156 return EqualsTraits<T>::Equals(a, b); |
156 } | 157 } |
157 | 158 |
159 template <typename T> | |
160 size_t HashCombine(size_t seed, const T& value) { | |
yzshen1
2016/09/20 23:44:39
Does it make sense to move hash-related functions
tibell
2016/09/21 07:10:53
Done.
| |
161 // Based on proposal in: | |
162 // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf | |
163 return seed ^ (std::hash<T>()(value) + (seed << 6) + (seed >> 2)); | |
164 } | |
165 | |
166 template <typename T> | |
167 struct HasHashMethod { | |
168 template <typename U> | |
169 static char Test(decltype(&U::Hash)); | |
170 template <typename U> | |
171 static int Test(...); | |
172 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | |
173 | |
174 private: | |
175 EnsureTypeIsComplete<T> check_t_; | |
176 }; | |
177 | |
178 template <typename T, bool has_hash_method = HasHashMethod<T>::value> | |
179 struct HashTraits; | |
180 | |
181 template <typename T> | |
182 size_t Hash(size_t seed, const T& value); | |
183 | |
184 template <typename T> | |
185 struct HashTraits<T, true> { | |
186 static size_t Hash(size_t seed, const T& value) { return value.Hash(seed); } | |
187 }; | |
188 | |
189 template <typename T> | |
190 struct HashTraits<T, false> { | |
191 static size_t Hash(size_t seed, const T& value) { | |
192 return HashCombine(seed, value); | |
193 } | |
194 }; | |
195 | |
196 template <typename T> | |
197 struct HashTraits<base::Optional<T>, false> { | |
Sam McNally
2016/09/21 04:57:58
If we're still disallowing nullable structs, array
tibell
2016/09/21 07:10:53
Done. Deleted.
| |
198 static size_t Hash(size_t seed, const base::Optional<T>& value) { | |
199 return !value ? HashCombine(seed, 0) : internal::Hash(seed, *value); | |
yzshen1
2016/09/20 23:44:39
nit: is "internal::" necessary here?
tibell
2016/09/21 07:10:53
Done.
| |
200 } | |
201 }; | |
202 | |
203 template <typename T> | |
204 struct HashTraits<std::vector<T>, false> { | |
205 static size_t Hash(size_t seed, const std::vector<T>& value) { | |
206 for (const auto& element : value) { | |
207 seed = Hash(seed, element); | |
208 } | |
209 return seed; | |
210 } | |
211 }; | |
212 | |
213 template <typename K, typename V> | |
214 struct HashTraits<std::unordered_map<K, V>, false> { | |
215 static size_t Hash(size_t seed, const std::unordered_map<K, V>& value) { | |
216 for (const auto& element : value) { | |
217 seed = Hash(seed, element.first); | |
218 seed = Hash(seed, element.second); | |
219 } | |
220 return seed; | |
221 } | |
222 }; | |
223 | |
224 template <typename T> | |
225 size_t Hash(size_t seed, const T& value) { | |
226 return HashTraits<T>::Hash(seed, value); | |
227 } | |
228 | |
158 } // namespace internal | 229 } // namespace internal |
159 } // namespace mojo | 230 } // namespace mojo |
160 | 231 |
161 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ | 232 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ |
OLD | NEW |