[map] Add initializer_list and swap()

This commit is contained in:
Behdad Esfahbod 2021-11-01 22:45:11 -06:00
parent 3604f5f248
commit a03b9b14c9
3 changed files with 35 additions and 4 deletions

View File

@ -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 */

View File

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

View File

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