[vector] Add initializer from iterable

This commit is contained in:
Behdad Esfahbod 2021-11-01 20:18:49 -06:00
parent 18a078f968
commit ee75e716c1
2 changed files with 37 additions and 0 deletions

View File

@ -47,6 +47,12 @@ struct hb_vector_t
for (auto&& i : l) for (auto&& i : l)
push (i); push (i);
} }
template <typename Iterable,
hb_requires (hb_is_iterable (Iterable))>
hb_vector_t (const Iterable &o) : hb_vector_t ()
{
hb_copy (o, *this);
}
hb_vector_t (const hb_vector_t &o) : hb_vector_t () hb_vector_t (const hb_vector_t &o) : hb_vector_t ()
{ {
alloc (o.length); alloc (o.length);

View File

@ -26,6 +26,7 @@
#include "hb.hh" #include "hb.hh"
#include "hb-algs.hh" #include "hb-algs.hh"
#include "hb-set.hh"
static char * static char *
@ -91,6 +92,35 @@ main (int argc, char **argv)
assert (++hb_inc (x) == 3); assert (++hb_inc (x) == 3);
assert (x == 3); assert (x == 3);
/* Test initializing vector from iterable. */
{
hb_set_t s;
s.add (18);
s.add (12);
hb_vector_t<int> v (s);
assert (v.length == 2);
assert (v[0] == 12);
assert (v[1] == 18);
}
/* Test initializing vector from iterator. */
{
hb_set_t s;
s.add (18);
s.add (12);
hb_vector_t<int> v (hb_iter (s));
assert (v.length == 2);
assert (v[0] == 12);
assert (v[1] == 18);
}
/* Test initializing vector from initializer list and swapping. */
{ {
hb_vector_t<int> v1 {1, 2, 3}; hb_vector_t<int> v1 {1, 2, 3};
hb_vector_t<int> v2 {4, 5}; hb_vector_t<int> v2 {4, 5};
@ -101,6 +131,7 @@ main (int argc, char **argv)
assert (v2[2] == 3); assert (v2[2] == 3);
} }
/* Test initializing sorted-vector from initializer list and swapping. */
{ {
hb_sorted_vector_t<int> v1 {1, 2, 3}; hb_sorted_vector_t<int> v1 {1, 2, 3};
hb_sorted_vector_t<int> v2 {4, 5}; hb_sorted_vector_t<int> v2 {4, 5};