From 4f6079138d74c1958c6345de28a08a8816e0c4af Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Jan 2023 13:58:46 -0700 Subject: [PATCH] [map] Add hb_map_update() --- docs/harfbuzz-sections.txt | 1 + src/hb-map.cc | 16 ++++++++++++++++ src/hb-map.h | 4 ++++ src/hb-map.hh | 7 +++++++ src/test-map.cc | 16 ++++++++++++++-- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index e30590f59..25dafd51b 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -546,6 +546,7 @@ hb_map_get_population hb_map_is_empty hb_map_is_equal hb_map_hash +hb_map_update hb_map_next HB_MAP_VALUE_INVALID hb_map_t diff --git a/src/hb-map.cc b/src/hb-map.cc index 71cff4dba..6b23d3115 100644 --- a/src/hb-map.cc +++ b/src/hb-map.cc @@ -341,6 +341,22 @@ hb_map_hash (const hb_map_t *map) return map->hash (); } +/** + * hb_map_update: + * @map: A map + * @other: Another map + * + * Add the contents of @other to @map. + * + * Since: REPLACEME + **/ +HB_EXTERN void +hb_map_update (hb_map_t *map, + const hb_map_t *other) +{ + map->update (*other); +} + /** * hb_map_next: * @map: A map diff --git a/src/hb-map.h b/src/hb-map.h index 9387ec2ce..923dd853f 100644 --- a/src/hb-map.h +++ b/src/hb-map.h @@ -118,6 +118,10 @@ HB_EXTERN hb_bool_t hb_map_has (const hb_map_t *map, hb_codepoint_t key); +HB_EXTERN void +hb_map_update (hb_map_t *map, + const hb_map_t *other); + /* Pass -1 in for idx to get started. */ HB_EXTERN hb_bool_t hb_map_next (const hb_map_t *map, diff --git a/src/hb-map.hh b/src/hb-map.hh index 9d6fbec8b..0482c22cd 100644 --- a/src/hb-map.hh +++ b/src/hb-map.hh @@ -308,6 +308,13 @@ struct hb_hashmap_t unsigned int get_population () const { return population; } + void update (const hb_hashmap_t &other) + { + if (unlikely (!successful)) return; + + hb_copy (other, *this); + } + /* * Iterator */ diff --git a/src/test-map.cc b/src/test-map.cc index bda3548e8..195b485c0 100644 --- a/src/test-map.cc +++ b/src/test-map.cc @@ -314,7 +314,7 @@ main (int argc, char **argv) hb_codepoint_t k; hb_codepoint_t v; unsigned pop = 0; - for (signed i; + for (signed i = -1; m.next (&i, &k, &v);) { pop++; @@ -326,7 +326,19 @@ main (int argc, char **argv) else if (k == 6) assert (v == 8); else assert (false); } - assert (pop = m.get_population ()); + assert (pop == m.get_population ()); + } + /* Test update */ + { + hb_map_t m1, m2; + m1.set (1, 2); + m1.set (2, 4); + m2.set (1, 3); + + m1.update (m2); + assert (m1.get_population () == 2); + assert (m1[1] == 3); + assert (m1[2] == 4); } return 0;