aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@behdad.org>2021-11-01 22:45:11 -0600
committerBehdad Esfahbod <behdad@behdad.org>2021-11-01 22:45:11 -0600
commita03b9b14c988aa04ba3ee1e5d187b65e7b24015a (patch)
treef70c6e81f163a5abe31d60b27b57c1c327f815d8
parent3604f5f2484b47c0693896f8a8e48dfe46dddef1 (diff)
downloadharfbuzz_ng-a03b9b14c988aa04ba3ee1e5d187b65e7b24015a.tar.gz
[map] Add initializer_list and swap()
-rw-r--r--src/hb-map.hh25
-rw-r--r--src/test-map.cc10
-rw-r--r--src/test-set.cc6
3 files changed, 36 insertions, 5 deletions
diff --git a/src/hb-map.hh b/src/hb-map.hh
index d7b20f3e2..45bd3cb36 100644
--- a/src/hb-map.hh
+++ b/src/hb-map.hh
@@ -47,6 +47,12 @@ struct hb_hashmap_t
hb_hashmap_t& operator= (const hb_hashmap_t&& o) { hb_copy (o, *this); return *this; }
hb_hashmap_t& operator= (hb_hashmap_t&& o) { hb_swap (*this, o); return *this; }
+ hb_hashmap_t (std::initializer_list<hb_pair_t<K, V>> lst) : hb_hashmap_t ()
+ {
+ for (auto&& item : lst)
+ set (item.first, item.second);
+ }
+
static_assert (hb_is_integral (K) || hb_is_pointer (K), "");
static_assert (hb_is_integral (V) || hb_is_pointer (V), "");
@@ -336,7 +342,22 @@ struct hb_hashmap_t
struct hb_map_t : hb_hashmap_t<hb_codepoint_t,
hb_codepoint_t,
HB_MAP_VALUE_INVALID,
- HB_MAP_VALUE_INVALID> {};
-
+ HB_MAP_VALUE_INVALID>
+{
+ using hashmap = hb_hashmap_t<hb_codepoint_t,
+ hb_codepoint_t,
+ HB_MAP_VALUE_INVALID,
+ HB_MAP_VALUE_INVALID>;
+
+ hb_map_t () = default;
+ ~hb_map_t () = default;
+ hb_map_t (hb_map_t& o) = default;
+ hb_map_t& operator= (const hb_map_t& other) = default;
+ hb_map_t& operator= (hb_map_t&& other) = default;
+ hb_map_t (std::initializer_list<hb_pair_t<hb_codepoint_t, hb_codepoint_t>> lst) : hashmap (lst) {}
+ template <typename Iterable,
+ hb_requires (hb_is_iterable (Iterable))>
+ hb_map_t (const Iterable &o) : hashmap (o) {}
+};
#endif /* HB_MAP_HH */
diff --git a/src/test-map.cc b/src/test-map.cc
index b685530bd..2d0305809 100644
--- a/src/test-map.cc
+++ b/src/test-map.cc
@@ -64,5 +64,15 @@ main (int argc, char **argv)
v = hb_map_t {};
}
+ /* Test initializing from initializer list and swapping. */
+ {
+ using pair_t = hb_pair_t<hb_codepoint_t, hb_codepoint_t>;
+ hb_map_t v1 {pair_t{1,2}, pair_t{4,5}};
+ hb_map_t v2 {pair_t{3,4}};
+ hb_swap (v1, v2);
+ assert (v1.get_population () == 1);
+ assert (v2.get_population () == 2);
+ }
+
return 0;
}
diff --git a/src/test-set.cc b/src/test-set.cc
index 5c28beb34..286f1a997 100644
--- a/src/test-set.cc
+++ b/src/test-set.cc
@@ -60,7 +60,7 @@ main (int argc, char **argv)
assert (v.get_population () == 2);
}
- /* Test initializing set from iterable. */
+ /* Test initializing from iterable. */
{
hb_set_t s;
@@ -72,7 +72,7 @@ main (int argc, char **argv)
assert (v.get_population () == 2);
}
- /* Test initializing set from iterator. */
+ /* Test initializing from iterator. */
{
hb_set_t s;
@@ -84,7 +84,7 @@ main (int argc, char **argv)
assert (v.get_population () == 2);
}
- /* Test initializing set from initializer list and swapping. */
+ /* Test initializing from initializer list and swapping. */
{
hb_set_t v1 {1, 2, 3};
hb_set_t v2 {4, 5};