Make hb::shared_ptr hashable

This commit is contained in:
Behdad Esfahbod 2022-06-01 15:10:19 -06:00
parent 3817bdfd7f
commit 0b35940a72
3 changed files with 21 additions and 5 deletions

View File

@ -246,12 +246,18 @@ struct
impl (const T& v, hb_priority<1>) const HB_RETURN (uint32_t, std::hash<hb_decay<decltype (hb_deref (v))>>{} (hb_deref (v))) impl (const T& v, hb_priority<1>) const HB_RETURN (uint32_t, std::hash<hb_decay<decltype (hb_deref (v))>>{} (hb_deref (v)))
template <typename T, template <typename T,
hb_enable_if (std::is_integral<T>::value)> constexpr auto hb_enable_if (std::is_integral<T>::value)> constexpr uint32_t
impl (const T& v, hb_priority<0>) const HB_AUTO_RETURN impl (const T& v, hb_priority<0>) const
( {
/* Knuth's multiplicative method: */ /* Knuth's multiplicative method: */
(uint32_t) v * 2654435761u return (uint32_t) v * 2654435761u;
) }
template <typename T> constexpr uint32_t
impl (const hb::shared_ptr<T>& v, hb_priority<0>) const
{
return (uint32_t) (intptr_t) v.get () * 2654435761u;
}
public: public:

View File

@ -51,6 +51,7 @@ struct shared_ptr
{ {
using v = vtable<T>; using v = vtable<T>;
shared_ptr (std::nullptr_t) : p (nullptr) {}
explicit shared_ptr (T *p = nullptr) : p (p) {} explicit shared_ptr (T *p = nullptr) : p (p) {}
shared_ptr (const shared_ptr &o) : p (v::reference (o.p)) {} shared_ptr (const shared_ptr &o) : p (v::reference (o.p)) {}
shared_ptr (shared_ptr &&o) : p (o.p) { o.p = nullptr; } shared_ptr (shared_ptr &&o) : p (o.p) { o.p = nullptr; }

View File

@ -222,5 +222,14 @@ main (int argc, char **argv)
assert (m2.get (vector_t {1}) == vector_t {2}); assert (m2.get (vector_t {1}) == vector_t {2});
} }
/* Test hb::shared_ptr. */
{
hb_hashmap_t<hb::shared_ptr<hb_set_t>, hb::shared_ptr<hb_set_t>, std::nullptr_t, std::nullptr_t, nullptr, nullptr> m;
hb_hash (hb::shared_ptr<hb_set_t> ());
m.get (hb::shared_ptr<hb_set_t> ());
m.get (hb::shared_ptr<hb_set_t> (hb_set_get_empty ()));
}
return 0; return 0;
} }