[map] Add iterable constructor

This commit is contained in:
Behdad Esfahbod 2021-11-01 22:47:26 -06:00
parent a03b9b14c9
commit 94158316d9
2 changed files with 30 additions and 0 deletions

View File

@ -52,6 +52,12 @@ struct hb_hashmap_t
for (auto&& item : lst)
set (item.first, item.second);
}
template <typename Iterable,
hb_requires (hb_is_iterable (Iterable))>
hb_hashmap_t (const Iterable &o) : hb_hashmap_t ()
{
hb_copy (o, *this);
}
static_assert (hb_is_integral (K) || hb_is_pointer (K), "");
static_assert (hb_is_integral (V) || hb_is_pointer (V), "");

View File

@ -64,6 +64,30 @@ main (int argc, char **argv)
v = hb_map_t {};
}
/* Test initializing from iterable. */
{
hb_map_t s;
s.set (1, 2);
s.set (3, 4);
hb_map_t v (s);
assert (v.get_population () == 2);
}
/* Test initializing from iterator. */
{
hb_map_t s;
s.set (1, 2);
s.set (3, 4);
hb_map_t v (hb_iter (s));
assert (v.get_population () == 2);
}
/* Test initializing from initializer list and swapping. */
{
using pair_t = hb_pair_t<hb_codepoint_t, hb_codepoint_t>;