OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ | |
7 | |
8 #include <functional> | |
9 #include <type_traits> | |
10 #include <unordered_map> | |
yzshen1
2016/09/21 23:17:52
some of the includes are not needed, please remove
tibell
2016/09/22 05:17:23
Done.
| |
11 #include <vector> | |
12 | |
13 #include "base/optional.h" | |
14 #include "mojo/public/cpp/bindings/lib/template_util.h" | |
15 | |
16 namespace mojo { | |
17 namespace internal { | |
18 | |
19 template <typename T> | |
20 size_t HashCombine(size_t seed, const T& value) { | |
21 // Based on proposal in: | |
22 // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf | |
23 return seed ^ (std::hash<T>()(value) + (seed << 6) + (seed >> 2)); | |
24 } | |
25 | |
26 template <typename T> | |
27 struct HasHashMethod { | |
28 template <typename U> | |
29 static char Test(decltype(&U::Hash)); | |
30 template <typename U> | |
31 static int Test(...); | |
32 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | |
33 | |
34 private: | |
35 EnsureTypeIsComplete<T> check_t_; | |
36 }; | |
37 | |
38 template <typename T, bool has_hash_method = HasHashMethod<T>::value> | |
39 struct HashTraits; | |
40 | |
41 template <typename T> | |
42 size_t Hash(size_t seed, const T& value); | |
43 | |
44 template <typename T> | |
45 struct HashTraits<T, true> { | |
46 static size_t Hash(size_t seed, const T& value) { return value.Hash(seed); } | |
47 }; | |
48 | |
49 template <typename T> | |
50 struct HashTraits<T, false> { | |
51 static size_t Hash(size_t seed, const T& value) { | |
52 return HashCombine(seed, value); | |
53 } | |
54 }; | |
55 | |
56 template <typename T> | |
57 size_t Hash(size_t seed, const T& value) { | |
58 return HashTraits<T>::Hash(seed, value); | |
59 } | |
60 | |
61 } // namespace internal | |
62 } // namespace mojo | |
63 | |
64 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ | |
OLD | NEW |