From 0dabd85e3b42ddb3fb91fc02e6add955624ff535 Mon Sep 17 00:00:00 2001 From: "A.J. Beamon" Date: Wed, 26 Jan 2022 15:01:16 -0800 Subject: [PATCH] Add a hash function for Optional. Add a default boost hash function for any type that is not boost hashable but can be hashed using std::hash. --- flow/Arena.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/flow/Arena.h b/flow/Arena.h index a29f07f1e4..013578dc61 100644 --- a/flow/Arena.h +++ b/flow/Arena.h @@ -31,6 +31,7 @@ #include "flow/ObjectSerializerTraits.h" #include "flow/FileIdentifier.h" #include +#include #include #include #include @@ -282,8 +283,10 @@ public: bool operator<(Optional const& o) const { return impl < o.impl; } void reset() { impl.reset(); } + size_t hash() const { return hashFunc(impl); } private: + static inline std::hash> hashFunc{}; std::optional impl; }; @@ -651,6 +654,26 @@ struct hash> { }; } // namespace std +namespace std { +template +struct hash> { + std::size_t operator()(Optional const& val) const { return val.hash(); } +}; +} // namespace std + +template > +struct boost_hashable : std::false_type {}; + +template +struct boost_hashable()))>> : std::true_type {}; + +// Using boost hash functions on types that depend on member hashes (e.g. std::pair) expect the members +// to be boost hashable. This provides a default boost hash function based on std::hash. +template +std::enable_if_t::value, std::size_t> hash_value(const T& v) { + return std::hash{}(v); +} + template <> struct TraceableString { static const char* begin(StringRef value) { return reinterpret_cast(value.begin()); }