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_HASH_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ |
7 | 7 |
8 #include <cstring> | 8 #include <cstring> |
9 #include <functional> | 9 #include <functional> |
10 #include <type_traits> | 10 #include <type_traits> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
| 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 |
18 template <typename T> | 19 template <typename T> |
19 size_t HashCombine(size_t seed, const T& value) { | 20 size_t HashCombine(size_t seed, const T& value) { |
20 // Based on proposal in: | 21 // Based on proposal in: |
21 // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf | 22 // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf |
22 return seed ^ (std::hash<T>()(value) + (seed << 6) + (seed >> 2)); | 23 return seed ^ (std::hash<T>()(value) + (seed << 6) + (seed >> 2)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 struct HashTraits<std::vector<T>, false> { | 57 struct HashTraits<std::vector<T>, false> { |
57 static size_t Hash(size_t seed, const std::vector<T>& value) { | 58 static size_t Hash(size_t seed, const std::vector<T>& value) { |
58 for (const auto& element : value) { | 59 for (const auto& element : value) { |
59 seed = HashCombine(seed, element); | 60 seed = HashCombine(seed, element); |
60 } | 61 } |
61 return seed; | 62 return seed; |
62 } | 63 } |
63 }; | 64 }; |
64 | 65 |
65 template <typename T> | 66 template <typename T> |
| 67 struct HashTraits<base::Optional<std::vector<T>>, false> { |
| 68 static size_t Hash(size_t seed, const base::Optional<std::vector<T>>& value) { |
| 69 if (!value) |
| 70 return HashCombine(seed, 0); |
| 71 |
| 72 return Hash(seed, *value); |
| 73 } |
| 74 }; |
| 75 |
| 76 template <typename T> |
66 size_t Hash(size_t seed, const T& value) { | 77 size_t Hash(size_t seed, const T& value) { |
67 return HashTraits<T>::Hash(seed, value); | 78 return HashTraits<T>::Hash(seed, value); |
68 } | 79 } |
69 | 80 |
70 } // namespace internal | 81 } // namespace internal |
71 } // namespace mojo | 82 } // namespace mojo |
72 | 83 |
73 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ | 84 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_HASH_UTIL_H_ |
OLD | NEW |