From aa6182453b29b2d1cc974756fe0ae4220a846bf4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 1 Nov 2021 18:36:57 -0600 Subject: [PATCH] [vector] Add initializer_list constructor & tests --- src/hb-vector.hh | 15 +++++++++++++-- src/test-algs.cc | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 44c174d07..d3a5dffac 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -31,6 +31,8 @@ #include "hb-array.hh" #include "hb-null.hh" +#include + template struct hb_vector_t @@ -39,9 +41,14 @@ struct hb_vector_t static constexpr unsigned item_size = hb_static_size (Type); hb_vector_t () { init (); } - hb_vector_t (const hb_vector_t &o) + hb_vector_t (std::initializer_list l) : hb_vector_t () + { + alloc (l.size ()); + for (auto&& i : l) + push (i); + } + hb_vector_t (const hb_vector_t &o) : hb_vector_t () { - init (); alloc (o.length); hb_copy (o, *this); } @@ -302,6 +309,10 @@ struct hb_vector_t template struct hb_sorted_vector_t : hb_vector_t { + hb_sorted_vector_t () : hb_vector_t () {} + hb_sorted_vector_t (std::initializer_list l) : hb_vector_t (l) {} + hb_sorted_vector_t (hb_sorted_vector_t& o) : hb_vector_t (o) {} + hb_sorted_array_t< Type> as_array () { return hb_sorted_array (this->arrayZ, this->length); } hb_sorted_array_t as_array () const { return hb_sorted_array (this->arrayZ, this->length); } diff --git a/src/test-algs.cc b/src/test-algs.cc index f8b8ff666..0f6b1fbd0 100644 --- a/src/test-algs.cc +++ b/src/test-algs.cc @@ -91,5 +91,25 @@ main (int argc, char **argv) assert (++hb_inc (x) == 3); assert (x == 3); + { + hb_vector_t v1 {1, 2, 3}; + hb_vector_t v2 {4, 5}; + hb_swap (v1, v2); + assert (v1.length == 2); + assert (v1[0] == 4); + assert (v2.length == 3); + assert (v2[2] == 3); + } + + { + hb_sorted_vector_t v1 {1, 2, 3}; + hb_sorted_vector_t v2 {4, 5}; + hb_swap (v1, v2); + assert (v1.length == 2); + assert (v1[0] == 4); + assert (v2.length == 3); + assert (v2[2] == 3); + } + return 0; }