jbates@chromium.org | 68c7aea | 2012-09-01 09:55:09 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_HASH_H_ |
| 6 | #define BASE_HASH_H_ |
| 7 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
mgiuca@chromium.org | e1be639 | 2014-03-03 16:59:27 +0900 | [diff] [blame] | 11 | #include <limits> |
jbates@chromium.org | 68c7aea | 2012-09-01 09:55:09 +0900 | [diff] [blame] | 12 | #include <string> |
davidben | f8b29b7 | 2016-01-22 10:41:41 +0900 | [diff] [blame] | 13 | #include <utility> |
jbates@chromium.org | 68c7aea | 2012-09-01 09:55:09 +0900 | [diff] [blame] | 14 | |
| 15 | #include "base/base_export.h" |
mgiuca@chromium.org | e1be639 | 2014-03-03 16:59:27 +0900 | [diff] [blame] | 16 | #include "base/logging.h" |
Brett Wilson | cc2b577 | 2017-09-02 05:23:53 +0900 | [diff] [blame] | 17 | #include "base/strings/string16.h" |
jbates@chromium.org | 68c7aea | 2012-09-01 09:55:09 +0900 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | |
Brett Wilson | cc2b577 | 2017-09-02 05:23:53 +0900 | [diff] [blame] | 21 | // Computes a hash of a memory buffer. This hash function is subject to change |
| 22 | // in the future, so use only for temporary in-memory structures. If you need |
| 23 | // to persist a change on disk or between computers, use PersistentHash(). |
| 24 | // |
mgiuca@chromium.org | e1be639 | 2014-03-03 16:59:27 +0900 | [diff] [blame] | 25 | // WARNING: This hash function should not be used for any cryptographic purpose. |
Brett Wilson | cc2b577 | 2017-09-02 05:23:53 +0900 | [diff] [blame] | 26 | BASE_EXPORT uint32_t Hash(const void* data, size_t length); |
| 27 | BASE_EXPORT uint32_t Hash(const std::string& str); |
| 28 | BASE_EXPORT uint32_t Hash(const string16& str); |
jbates@chromium.org | 68c7aea | 2012-09-01 09:55:09 +0900 | [diff] [blame] | 29 | |
Brett Wilson | cc2b577 | 2017-09-02 05:23:53 +0900 | [diff] [blame] | 30 | // Computes a hash of a memory buffer. This hash function must not change so |
| 31 | // that code can use the hashed values for persistent storage purposes or |
| 32 | // sending across the network. If a new persistent hash function is desired, a |
| 33 | // new version will have to be added in addition. |
| 34 | // |
mgiuca@chromium.org | e1be639 | 2014-03-03 16:59:27 +0900 | [diff] [blame] | 35 | // WARNING: This hash function should not be used for any cryptographic purpose. |
Brett Wilson | cc2b577 | 2017-09-02 05:23:53 +0900 | [diff] [blame] | 36 | BASE_EXPORT uint32_t PersistentHash(const void* data, size_t length); |
| 37 | BASE_EXPORT uint32_t PersistentHash(const std::string& str); |
jbates@chromium.org | 68c7aea | 2012-09-01 09:55:09 +0900 | [diff] [blame] | 38 | |
Brett Wilson | cc0c832 | 2017-09-07 08:38:57 +0900 | [diff] [blame] | 39 | // Hash pairs of 32-bit or 64-bit numbers. |
| 40 | BASE_EXPORT size_t HashInts32(uint32_t value1, uint32_t value2); |
| 41 | BASE_EXPORT size_t HashInts64(uint64_t value1, uint64_t value2); |
davidben | f8b29b7 | 2016-01-22 10:41:41 +0900 | [diff] [blame] | 42 | |
| 43 | template <typename T1, typename T2> |
| 44 | inline size_t HashInts(T1 value1, T2 value2) { |
| 45 | // This condition is expected to be compile-time evaluated and optimised away |
| 46 | // in release builds. |
| 47 | if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t))) |
| 48 | return HashInts64(value1, value2); |
| 49 | |
| 50 | return HashInts32(value1, value2); |
| 51 | } |
| 52 | |
Brett Wilson | 9b4d85b | 2017-09-20 07:23:37 +0900 | [diff] [blame] | 53 | // A templated hasher for pairs of integer types. Example: |
| 54 | // |
| 55 | // using MyPair = std::pair<int32_t, int32_t>; |
| 56 | // std::unordered_set<MyPair, base::IntPairHash<MyPair>> set; |
davidben | f8b29b7 | 2016-01-22 10:41:41 +0900 | [diff] [blame] | 57 | template <typename T> |
| 58 | struct IntPairHash; |
| 59 | |
| 60 | template <typename Type1, typename Type2> |
| 61 | struct IntPairHash<std::pair<Type1, Type2>> { |
| 62 | size_t operator()(std::pair<Type1, Type2> value) const { |
| 63 | return HashInts(value.first, value.second); |
| 64 | } |
| 65 | }; |
| 66 | |
jbates@chromium.org | 68c7aea | 2012-09-01 09:55:09 +0900 | [diff] [blame] | 67 | } // namespace base |
| 68 | |
| 69 | #endif // BASE_HASH_H_ |