[map] Add is_equal() / towards making hb_map_t hashable

New API:
+ hb_map_is_equal()
This commit is contained in:
Behdad Esfahbod 2022-05-19 13:36:12 -06:00
parent 14a24d8e3f
commit ad17699089
5 changed files with 64 additions and 0 deletions

View File

@ -459,6 +459,7 @@ hb_map_destroy
hb_map_get
hb_map_get_empty
hb_map_get_population
hb_map_is_equal
hb_map_get_user_data
hb_map_has
hb_map_is_empty

View File

@ -289,3 +289,23 @@ hb_map_get_population (const hb_map_t *map)
{
return map->get_population ();
}
/**
* hb_map_is_equal:
* @map: A map
* @other: Another map
*
* Tests whether @map and @other are equal (contain the same
* elements).
*
* Return value: %true if the two maps are equal, %false otherwise.
*
* Since: REPLACEME
**/
hb_bool_t
hb_map_is_equal (const hb_map_t *map,
const hb_map_t *other)
{
return map->is_equal (*other);
}

View File

@ -91,6 +91,10 @@ hb_map_is_empty (const hb_map_t *map);
HB_EXTERN unsigned int
hb_map_get_population (const hb_map_t *map);
HB_EXTERN hb_bool_t
hb_map_is_equal (const hb_map_t *map,
const hb_map_t *other);
HB_EXTERN void
hb_map_set (hb_map_t *map,
hb_codepoint_t key,

View File

@ -42,6 +42,7 @@ template <typename K, typename V,
struct hb_hashmap_t
{
hb_hashmap_t () { init (); }
hb_hashmap_t (std::nullptr_t) : hb_hashmap_t () {}
~hb_hashmap_t () { fini (); }
hb_hashmap_t (const hb_hashmap_t& o) : hb_hashmap_t () { resize (population); hb_copy (o, *this); }
@ -238,6 +239,19 @@ struct hb_hashmap_t
bool is_empty () const { return population == 0; }
explicit operator bool () const { return !is_empty (); }
bool is_equal (const hb_hashmap_t &other) const
{
if (population != other.population) return false;
for (auto pair : iter ())
if (get (pair.first) != pair.second)
return false;
return true;
}
bool operator == (const hb_hashmap_t &other) const { return is_equal (other); }
bool operator != (const hb_hashmap_t &other) const { return !is_equal (other); }
unsigned int get_population () const { return population; }
/*
@ -394,6 +408,7 @@ struct hb_map_t : hb_hashmap_t<hb_codepoint_t,
~hb_map_t () = default;
hb_map_t () : hashmap () {}
hb_map_t (std::nullptr_t) : hb_map_t () {}
hb_map_t (const hb_map_t &o) : hashmap ((hashmap &) o) {}
hb_map_t (hb_map_t &&o) : hashmap (std::move ((hashmap &) o)) {}
hb_map_t& operator= (const hb_map_t&) = default;

View File

@ -152,6 +152,30 @@ main (int argc, char **argv)
}
}
#if 0
/* Test hashing maps. */
{
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;
m1.map (hb_map_t (), hb_map_t ());
m2.map (hb_map_t (), hb_map_t ());
// m1.map (hb_map_t (), hb_map_t {1});
// m2.map (hb_map_t (), hb_map_t {1});
// m1.map (hb_map_t {1}, hb_map_t {2});
// m2.map (hb_map_t {1}, hb_map_t {2});
/* Cannot override empty map. */
assert (m1.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 (m2.get (hb_map_t {1}) == hb_map_t {2});
}
#endif
/* Test hashing sets. */
{
hb_hashmap_t<hb_set_t, hb_set_t, const hb_set_t *, const hb_set_t *, &invalid_set, &invalid_set> m1;