Debian Codegraph @

mfbt/PairHash.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /* Utilities for hashing pairs. */
8
9 #ifndef mozilla_PairHash_h
10 #define mozilla_PairHash_h
11
12 #include "mozilla/CompactPair.h"
13 #include "mozilla/HashFunctions.h"
14
15 #include <utility> // std::pair
16
17 namespace mozilla {
18
19 /**
20 * The HashPair overloads below do just what you'd expect.
21 *
22 * These functions support hash of std::pair<T,U> and mozilla::CompactPair<T,u>
23 * where type T and U both support AddToHash.
24 */
25 template <typename U, typename V>
26 [[nodiscard]] inline HashNumber HashPair(const std::pair<U, V>& pair) {
27 // Pair hash combines the hash of each member
28 return HashGeneric(pair.first, pair.second);
29 }
30
31 template <typename U, typename V>
32 [[nodiscard]] inline HashNumber HashCompactPair(const CompactPair<U, V>& pair) {
33 // Pair hash combines the hash of each member
34 return HashGeneric(pair.first(), pair.second());
35 }
36
37 /**
38 * Hash policy for std::pair compatible with HashTable
39 */
40 template <typename T, typename U>
41 struct PairHasher {
42 using Key = std::pair<T, U>;
43 using Lookup = Key;
44
45 static HashNumber hash(const Lookup& aLookup) { return HashPair(aLookup); }
46
47 static bool match(const Key& aKey, const Lookup& aLookup) {
48 return aKey == aLookup;
49 }
50
51 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
52 };
53
54 /**
55 * Hash policy for mozilla::CompactPair compatible with HashTable
56 */
57 template <typename T, typename U>
58 struct CompactPairHasher {
59 using Key = CompactPair<T, U>;
60 using Lookup = Key;
61
62 static HashNumber hash(const Lookup& aLookup) {
63 return HashCompactPair(aLookup);
64 }
65
66 static bool match(const Key& aKey, const Lookup& aLookup) {
67 return aKey == aLookup;
68 }
69
70 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
71 };
72
73 } // namespace mozilla
74
75 #endif /* mozilla_PairHash_h */