Make hb::shared_ptr hashable

pull/3627/head
Behdad Esfahbod 2 years ago
parent 3817bdfd7f
commit 0b35940a72
  1. 16
      src/hb-algs.hh
  2. 1
      src/hb-cplusplus.hh
  3. 9
      src/test-map.cc

@ -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)))
template <typename T,
hb_enable_if (std::is_integral<T>::value)> constexpr auto
impl (const T& v, hb_priority<0>) const HB_AUTO_RETURN
(
hb_enable_if (std::is_integral<T>::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 <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:

@ -51,6 +51,7 @@ struct shared_ptr
{
using v = vtable<T>;
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; }

@ -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<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;
}

Loading…
Cancel
Save