From 403384998a6bb5f428e15ced5503206b45032b25 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 8 Jul 2020 22:27:25 +0200 Subject: BLI: improve constructors and conversions to span This allows us to avoid many calls to `as_span()` methods. I will remove those in the next commit. Furthermore, constructors of Vector and Array can convert from one type to another now. I tested these changes on Linux with gcc and on Windows. --- source/blender/blenlib/BLI_vector.hh | 43 ++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'source/blender/blenlib/BLI_vector.hh') diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh index f608290f5df..df885588d9b 100644 --- a/source/blender/blenlib/BLI_vector.hh +++ b/source/blender/blenlib/BLI_vector.hh @@ -141,9 +141,19 @@ class Vector { */ Vector(uint size, const T &value) : Vector() { + this->resize(size, value); + } + + /** + * Create a vector from an array ref. The values in the vector are copy constructed. + */ + template> * = nullptr> + Vector(Span values, Allocator allocator = {}) : Vector(allocator) + { + const uint size = values.size(); this->reserve(size); this->increase_size_by_unchecked(size); - blender::uninitialized_fill_n(begin_, size, value); + uninitialized_convert_n(values.data(), size, begin_); } /** @@ -152,24 +162,21 @@ class Vector { * This allows you to write code like: * Vector vec = {3, 4, 5}; */ - Vector(const std::initializer_list &values) : Vector(Span(values)) + template> * = nullptr> + Vector(const std::initializer_list &values) : Vector(Span(values)) { } - /** - * Create a vector from an array ref. The values in the vector are copy constructed. - */ - Vector(Span values, Allocator allocator = {}) : Vector(allocator) + template> * = nullptr> + Vector(const std::array &values) : Vector(Span(values)) { - const uint size = values.size(); - this->reserve(size); - this->increase_size_by_unchecked(size); - blender::uninitialized_copy_n(values.data(), size, begin_); } /** - * Create a vector from any container. It must be possible to use the container in a range-for - * loop. + * Create a vector from any container. It must be possible to use the container in a + * range-for loop. */ template static Vector FromContainer(const ContainerT &container) { @@ -313,6 +320,18 @@ class Vector { return MutableSpan(begin_, this->size()); } + template> * = nullptr> + operator Span() const + { + return Span(begin_, this->size()); + } + + template> * = nullptr> + operator MutableSpan() + { + return MutableSpan(begin_, this->size()); + } + Span as_span() const { return *this; -- cgit v1.2.3