From 0b35940a72aaa4575e4dd1f8991abb037bc2a0ed Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Jun 2022 15:10:19 -0600 Subject: [PATCH] Make hb::shared_ptr hashable --- src/hb-algs.hh | 16 +++++++++++----- src/hb-cplusplus.hh | 1 + src/test-map.cc | 9 +++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 6ffe0e22e..c5fa0bcbc 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -246,12 +246,18 @@ struct impl (const T& v, hb_priority<1>) const HB_RETURN (uint32_t, std::hash>{} (hb_deref (v))) template ::value)> constexpr auto - impl (const T& v, hb_priority<0>) const HB_AUTO_RETURN - ( + hb_enable_if (std::is_integral::value)> constexpr uint32_t + impl (const T& v, hb_priority<0>) const + { /* Knuth's multiplicative method: */ - (uint32_t) v * 2654435761u - ) + return (uint32_t) v * 2654435761u; + } + + template constexpr uint32_t + impl (const hb::shared_ptr& v, hb_priority<0>) const + { + return (uint32_t) (intptr_t) v.get () * 2654435761u; + } public: diff --git a/src/hb-cplusplus.hh b/src/hb-cplusplus.hh index 122c6920e..39b150fea 100644 --- a/src/hb-cplusplus.hh +++ b/src/hb-cplusplus.hh @@ -51,6 +51,7 @@ struct shared_ptr { using v = vtable; + shared_ptr (std::nullptr_t) : p (nullptr) {} explicit shared_ptr (T *p = nullptr) : p (p) {} shared_ptr (const shared_ptr &o) : p (v::reference (o.p)) {} shared_ptr (shared_ptr &&o) : p (o.p) { o.p = nullptr; } diff --git a/src/test-map.cc b/src/test-map.cc index 3f81e8c3a..7d437a9cc 100644 --- a/src/test-map.cc +++ b/src/test-map.cc @@ -222,5 +222,14 @@ main (int argc, char **argv) assert (m2.get (vector_t {1}) == vector_t {2}); } + /* Test hb::shared_ptr. */ + { + hb_hashmap_t, hb::shared_ptr, std::nullptr_t, std::nullptr_t, nullptr, nullptr> m; + + hb_hash (hb::shared_ptr ()); + m.get (hb::shared_ptr ()); + m.get (hb::shared_ptr (hb_set_get_empty ())); + } + return 0; }