blob: 55c7cf4c4d7ab1e190d63d901909e97c8aa94642 [file] [log] [blame]
Ryan Keane07dc4022020-04-01 10:16:10 -07001// Copyright 2020 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 UTIL_HASHING_H_
6#define UTIL_HASHING_H_
7
8namespace openscreen {
9
10// Computes the aggregate hash of the provided hashable objects.
11// Seed must initially use a large prime between 2^63 and 2^64 as a starting
12// value, or the result of a previous call to this function.
13template <typename... T>
14uint64_t ComputeAggregateHash(uint64_t seed, const T&... objs) {
15 auto hash_combiner = [](uint64_t seed, uint64_t hash_value) -> uint64_t {
16 static const uint64_t kMultiplier = UINT64_C(0x9ddfea08eb382d69);
17 uint64_t a = (hash_value ^ seed) * kMultiplier;
18 a ^= (a >> 47);
19 uint64_t b = (seed ^ a) * kMultiplier;
20 b ^= (b >> 47);
21 b *= kMultiplier;
22 return b;
23 };
24
25 uint64_t result = seed;
26 std::vector<uint64_t> hashes{std::hash<T>()(objs)...};
27 for (uint64_t hash : hashes) {
28 result = hash_combiner(result, hash);
29 }
30 return result;
31}
32
33template <typename... T>
34uint64_t ComputeAggregateHash(const T&... objs) {
35 // This value is taken from absl::Hash implementation.
36 constexpr uint64_t default_seed = UINT64_C(0xc3a5c85c97cb3127);
37 return ComputeAggregateHash(default_seed, objs...);
38}
39
40struct PairHash {
41 template <typename TFirst, typename TSecond>
42 size_t operator()(const std::pair<TFirst, TSecond>& pair) const {
43 return ComputeAggregateHash(pair.first, pair.second);
44 }
45};
46
47} // namespace openscreen
48
49#endif // UTIL_HASHING_H_