From a089d91fda71e4c19c9c3c822abe86bbcd878dbc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Jun 2022 09:55:43 -0600 Subject: [PATCH] [hash] Adjust hash for shared_ptr, implement it for std::hash --- src/hb-algs.hh | 12 ++++++------ src/hb-cplusplus.hh | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 903815647..02c5a8697 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -240,17 +240,17 @@ struct private: template constexpr auto - impl (const T& v, hb_priority<2>) const HB_RETURN (uint32_t, hb_deref (v).hash ()) - - template constexpr auto - impl (const T& v, hb_priority<1>) const HB_RETURN (uint32_t, std::hash>{} (hb_deref (v))) + impl (const T& v, hb_priority<1>) const HB_RETURN (uint32_t, hb_deref (v).hash ()) template constexpr uint32_t - impl (const hb::shared_ptr& v, hb_priority<0>) const + impl (const hb::shared_ptr& v, hb_priority<1>) const { - return (uint32_t) (intptr_t) v.get () * 2654435761u; + return (uint32_t) (intptr_t) v.get ()->hash (); } + template constexpr auto + impl (const T& v, hb_priority<0>) const HB_RETURN (uint32_t, std::hash>{} (hb_deref (v))) + public: template constexpr auto diff --git a/src/hb-cplusplus.hh b/src/hb-cplusplus.hh index 9bda38c47..42c7d5624 100644 --- a/src/hb-cplusplus.hh +++ b/src/hb-cplusplus.hh @@ -88,8 +88,6 @@ struct shared_ptr template struct is_shared_ptr : std::false_type {}; template struct is_shared_ptr> : std::true_type {}; -// TODO Implement hash<> and atomic<> - template +struct std::hash> +{ + std::size_t operator()(const hb::shared_ptr& v) const noexcept + { + std::size_t h = std::hash{}(v.get ()); + return h; + } +}; + +// TODO Implement atomic<> + + #endif /* __cplusplus */ #endif /* HB_CPLUSPLUS_HH */