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/. */
7 /* Utilities for hashing pairs. */
9 #ifndef mozilla_PairHash_h
10 #define mozilla_PairHash_h
12 #include "mozilla/CompactPair.h"
13 #include "mozilla/HashFunctions.h"
15 #include <utility> // std::pair
20 * The HashPair overloads below do just what you'd expect.
22 * These functions support hash of std::pair<T,U> and mozilla::CompactPair<T,u>
23 * where type T and U both support AddToHash.
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
);
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());
38 * Hash policy for std::pair compatible with HashTable
40 template <typename T
, typename U
>
42 using Key
= std::pair
<T
, U
>;
45 static HashNumber
hash(const Lookup
& aLookup
) { return HashPair(aLookup
); }
47 static bool match(const Key
& aKey
, const Lookup
& aLookup
) {
48 return aKey
== aLookup
;
51 static void rekey(Key
& aKey
, const Key
& aNewKey
) { aKey
= aNewKey
; }
55 * Hash policy for mozilla::CompactPair compatible with HashTable
57 template <typename T
, typename U
>
58 struct CompactPairHasher
{
59 using Key
= CompactPair
<T
, U
>;
62 static HashNumber
hash(const Lookup
& aLookup
) {
63 return HashCompactPair(aLookup
);
66 static bool match(const Key
& aKey
, const Lookup
& aLookup
) {
67 return aKey
== aLookup
;
70 static void rekey(Key
& aKey
, const Key
& aNewKey
) { aKey
= aNewKey
; }
73 } // namespace mozilla
75 #endif /* mozilla_PairHash_h */