1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_HASH_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_HASH_H_
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker
11*635a8641SAndroid Build Coastguard Worker #include <limits>
12*635a8641SAndroid Build Coastguard Worker #include <string>
13*635a8641SAndroid Build Coastguard Worker #include <utility>
14*635a8641SAndroid Build Coastguard Worker
15*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/strings/string16.h"
18*635a8641SAndroid Build Coastguard Worker
19*635a8641SAndroid Build Coastguard Worker namespace base {
20*635a8641SAndroid Build Coastguard Worker
21*635a8641SAndroid Build Coastguard Worker // Computes a hash of a memory buffer. This hash function is subject to change
22*635a8641SAndroid Build Coastguard Worker // in the future, so use only for temporary in-memory structures. If you need
23*635a8641SAndroid Build Coastguard Worker // to persist a change on disk or between computers, use PersistentHash().
24*635a8641SAndroid Build Coastguard Worker //
25*635a8641SAndroid Build Coastguard Worker // WARNING: This hash function should not be used for any cryptographic purpose.
26*635a8641SAndroid Build Coastguard Worker BASE_EXPORT uint32_t Hash(const void* data, size_t length);
27*635a8641SAndroid Build Coastguard Worker BASE_EXPORT uint32_t Hash(const std::string& str);
28*635a8641SAndroid Build Coastguard Worker BASE_EXPORT uint32_t Hash(const string16& str);
29*635a8641SAndroid Build Coastguard Worker
30*635a8641SAndroid Build Coastguard Worker // Computes a hash of a memory buffer. This hash function must not change so
31*635a8641SAndroid Build Coastguard Worker // that code can use the hashed values for persistent storage purposes or
32*635a8641SAndroid Build Coastguard Worker // sending across the network. If a new persistent hash function is desired, a
33*635a8641SAndroid Build Coastguard Worker // new version will have to be added in addition.
34*635a8641SAndroid Build Coastguard Worker //
35*635a8641SAndroid Build Coastguard Worker // WARNING: This hash function should not be used for any cryptographic purpose.
36*635a8641SAndroid Build Coastguard Worker BASE_EXPORT uint32_t PersistentHash(const void* data, size_t length);
37*635a8641SAndroid Build Coastguard Worker BASE_EXPORT uint32_t PersistentHash(const std::string& str);
38*635a8641SAndroid Build Coastguard Worker
39*635a8641SAndroid Build Coastguard Worker // Hash pairs of 32-bit or 64-bit numbers.
40*635a8641SAndroid Build Coastguard Worker BASE_EXPORT size_t HashInts32(uint32_t value1, uint32_t value2);
41*635a8641SAndroid Build Coastguard Worker BASE_EXPORT size_t HashInts64(uint64_t value1, uint64_t value2);
42*635a8641SAndroid Build Coastguard Worker
43*635a8641SAndroid Build Coastguard Worker template <typename T1, typename T2>
HashInts(T1 value1,T2 value2)44*635a8641SAndroid Build Coastguard Worker inline size_t HashInts(T1 value1, T2 value2) {
45*635a8641SAndroid Build Coastguard Worker // This condition is expected to be compile-time evaluated and optimised away
46*635a8641SAndroid Build Coastguard Worker // in release builds.
47*635a8641SAndroid Build Coastguard Worker if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t)))
48*635a8641SAndroid Build Coastguard Worker return HashInts64(value1, value2);
49*635a8641SAndroid Build Coastguard Worker
50*635a8641SAndroid Build Coastguard Worker return HashInts32(value1, value2);
51*635a8641SAndroid Build Coastguard Worker }
52*635a8641SAndroid Build Coastguard Worker
53*635a8641SAndroid Build Coastguard Worker // A templated hasher for pairs of integer types. Example:
54*635a8641SAndroid Build Coastguard Worker //
55*635a8641SAndroid Build Coastguard Worker // using MyPair = std::pair<int32_t, int32_t>;
56*635a8641SAndroid Build Coastguard Worker // std::unordered_set<MyPair, base::IntPairHash<MyPair>> set;
57*635a8641SAndroid Build Coastguard Worker template <typename T>
58*635a8641SAndroid Build Coastguard Worker struct IntPairHash;
59*635a8641SAndroid Build Coastguard Worker
60*635a8641SAndroid Build Coastguard Worker template <typename Type1, typename Type2>
61*635a8641SAndroid Build Coastguard Worker struct IntPairHash<std::pair<Type1, Type2>> {
62*635a8641SAndroid Build Coastguard Worker size_t operator()(std::pair<Type1, Type2> value) const {
63*635a8641SAndroid Build Coastguard Worker return HashInts(value.first, value.second);
64*635a8641SAndroid Build Coastguard Worker }
65*635a8641SAndroid Build Coastguard Worker };
66*635a8641SAndroid Build Coastguard Worker
67*635a8641SAndroid Build Coastguard Worker } // namespace base
68*635a8641SAndroid Build Coastguard Worker
69*635a8641SAndroid Build Coastguard Worker #endif // BASE_HASH_H_
70