| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 BASE_HASH_H_ | 5 #ifndef BASE_HASH_H_ |
| 6 #define BASE_HASH_H_ | 6 #define BASE_HASH_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/base_export.h" | 15 #include "base/base_export.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 | 19 |
| 20 // WARNING: This hash function should not be used for any cryptographic purpose. | 20 // WARNING: This hash function should not be used for any cryptographic purpose. |
| 21 BASE_EXPORT uint32_t SuperFastHash(const char* data, int len); | 21 BASE_EXPORT uint32_t SuperFastHash(const char* data, size_t length); |
| 22 | 22 |
| 23 // Computes a hash of a memory buffer |data| of a given |length|. | 23 // Computes a hash of a memory buffer |data| of a given |length|. |
| 24 // WARNING: This hash function should not be used for any cryptographic purpose. | 24 // WARNING: This hash function should not be used for any cryptographic purpose. |
| 25 inline uint32_t Hash(const char* data, size_t length) { | 25 inline uint32_t Hash(const char* data, size_t length) { |
| 26 if (length > static_cast<size_t>(std::numeric_limits<int>::max())) { | 26 return SuperFastHash(data, length); |
| 27 NOTREACHED(); | |
| 28 return 0; | |
| 29 } | |
| 30 return SuperFastHash(data, static_cast<int>(length)); | |
| 31 } | 27 } |
| 32 | 28 |
| 33 // Computes a hash of a string |str|. | 29 // Computes a hash of a string |str|. |
| 34 // WARNING: This hash function should not be used for any cryptographic purpose. | 30 // WARNING: This hash function should not be used for any cryptographic purpose. |
| 35 inline uint32_t Hash(const std::string& str) { | 31 inline uint32_t Hash(const std::string& str) { |
| 36 return Hash(str.data(), str.size()); | 32 return Hash(str.data(), str.size()); |
| 37 } | 33 } |
| 38 | 34 |
| 39 // Implement hashing for pairs of at-most 32 bit integer values. | 35 // Implement hashing for pairs of at-most 32 bit integer values. |
| 40 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using | 36 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 template <typename Type1, typename Type2> | 109 template <typename Type1, typename Type2> |
| 114 struct IntPairHash<std::pair<Type1, Type2>> { | 110 struct IntPairHash<std::pair<Type1, Type2>> { |
| 115 size_t operator()(std::pair<Type1, Type2> value) const { | 111 size_t operator()(std::pair<Type1, Type2> value) const { |
| 116 return HashInts(value.first, value.second); | 112 return HashInts(value.first, value.second); |
| 117 } | 113 } |
| 118 }; | 114 }; |
| 119 | 115 |
| 120 } // namespace base | 116 } // namespace base |
| 121 | 117 |
| 122 #endif // BASE_HASH_H_ | 118 #endif // BASE_HASH_H_ |
| OLD | NEW |