[map] Make hb_map_t hashable

This commit is contained in:
Behdad Esfahbod 2022-05-19 13:38:52 -06:00
parent ad17699089
commit 561e02fefb
2 changed files with 19 additions and 10 deletions

View File

@ -239,6 +239,14 @@ struct hb_hashmap_t
bool is_empty () const { return population == 0; } bool is_empty () const { return population == 0; }
explicit operator bool () const { return !is_empty (); } explicit operator bool () const { return !is_empty (); }
unsigned hash () const
{
unsigned h = 0;
for (auto pair : iter ())
h ^= hb_hash (pair.first) ^ hb_hash (pair.second);
return h;
}
bool is_equal (const hb_hashmap_t &other) const bool is_equal (const hb_hashmap_t &other) const
{ {
if (population != other.population) return false; if (population != other.population) return false;

View File

@ -29,6 +29,7 @@
#include <string> #include <string>
static const std::string invalid{"invalid"}; static const std::string invalid{"invalid"};
static const hb_map_t invalid_map{};
static const hb_set_t invalid_set{}; static const hb_set_t invalid_set{};
static const hb_vector_t<unsigned> invalid_vector{}; static const hb_vector_t<unsigned> invalid_vector{};
@ -152,29 +153,29 @@ main (int argc, char **argv)
} }
} }
#if 0
/* Test hashing maps. */ /* Test hashing maps. */
{ {
using pair = hb_pair_t<hb_codepoint_t, hb_codepoint_t>;
hb_hashmap_t<hb_map_t, hb_map_t, const hb_map_t *, const hb_map_t *, &invalid_map, &invalid_map> m1; hb_hashmap_t<hb_map_t, hb_map_t, const hb_map_t *, const hb_map_t *, &invalid_map, &invalid_map> m1;
hb_hashmap_t<hb_map_t, hb_map_t, std::nullptr_t, std::nullptr_t, nullptr, nullptr> m2; hb_hashmap_t<hb_map_t, hb_map_t, std::nullptr_t, std::nullptr_t, nullptr, nullptr> m2;
m1.map (hb_map_t (), hb_map_t ()); m1.set (hb_map_t (), hb_map_t {});
m2.map (hb_map_t (), hb_map_t ()); m2.set (hb_map_t (), hb_map_t {});
// m1.map (hb_map_t (), hb_map_t {1}); m1.set (hb_map_t (), hb_map_t {pair (1u, 2u)});
// m2.map (hb_map_t (), hb_map_t {1}); m2.set (hb_map_t (), hb_map_t {pair (1u, 2u)});
// m1.map (hb_map_t {1}, hb_map_t {2}); m1.set (hb_map_t {pair (1u, 2u)}, hb_map_t {pair (2u, 3u)});
// m2.map (hb_map_t {1}, hb_map_t {2}); m2.set (hb_map_t {pair (1u, 2u)}, hb_map_t {pair (2u, 3u)});
/* Cannot override empty map. */ /* Cannot override empty map. */
assert (m1.get (hb_map_t ()) == hb_map_t ()); assert (m1.get (hb_map_t ()) == hb_map_t ());
assert (m2.get (hb_map_t ()) == hb_map_t ()); assert (m2.get (hb_map_t ()) == hb_map_t ());
// assert (m1.get (hb_map_t {1}) == hb_map_t {2}); assert (m1.get (hb_map_t {pair (1u, 2u)}) == hb_map_t {pair (2u, 3u)});
// assert (m2.get (hb_map_t {1}) == hb_map_t {2}); assert (m2.get (hb_map_t {pair (1u, 2u)}) == hb_map_t {pair (2u, 3u)});
} }
#endif
/* Test hashing sets. */ /* Test hashing sets. */
{ {