From f4633cf46c075c65d8238e3cb81de6dac95610a8 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 7 Jul 2020 19:16:31 +0200 Subject: BLI: simplify copy constructor of vector --- source/blender/blenlib/BLI_vector.hh | 44 ++++-------------------------------- 1 file changed, 4 insertions(+), 40 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh index e4f5afe661d..f608290f5df 100644 --- a/source/blender/blenlib/BLI_vector.hh +++ b/source/blender/blenlib/BLI_vector.hh @@ -118,7 +118,7 @@ class Vector { * Create an empty vector. * This does not do any memory allocation. */ - Vector() + Vector(Allocator allocator = {}) : allocator_(allocator) { begin_ = inline_buffer_; end_ = begin_; @@ -159,7 +159,7 @@ class Vector { /** * Create a vector from an array ref. The values in the vector are copy constructed. */ - Vector(Span values) : Vector() + Vector(Span values, Allocator allocator = {}) : Vector(allocator) { const uint size = values.size(); this->reserve(size); @@ -198,9 +198,8 @@ class Vector { * Create a copy of another vector. The other vector will not be changed. If the other vector has * less than InlineBufferCapacity elements, no allocation will be made. */ - Vector(const Vector &other) : allocator_(other.allocator_) + Vector(const Vector &other) : Vector(other.as_span(), other.allocator_) { - this->init_copy_from_other_vector(other); } /** @@ -209,9 +208,8 @@ class Vector { */ template Vector(const Vector &other) - : allocator_(other.allocator_) + : Vector(other.as_span(), other.allocator_) { - this->init_copy_from_other_vector(other); } /** @@ -799,44 +797,10 @@ class Vector { end_ = begin_ + size; capacity_end_ = begin_ + new_capacity; } - - /** - * Initialize all properties, except for allocator_, which has to be initialized beforehand. - */ - template - void init_copy_from_other_vector(const Vector &other) - { - allocator_ = other.allocator_; - - const uint size = other.size(); - uint capacity; - - if (size <= InlineBufferCapacity) { - begin_ = inline_buffer_; - capacity = InlineBufferCapacity; - } - else { - begin_ = (T *)allocator_.allocate(sizeof(T) * size, alignof(T), AT); - capacity = size; - } - - end_ = begin_ + size; - capacity_end_ = begin_ + capacity; - - uninitialized_copy_n(other.data(), size, begin_); - UPDATE_VECTOR_SIZE(this); - } }; #undef UPDATE_VECTOR_SIZE -/** - * Use when the vector is used in the local scope of a function. It has a larger inline storage by - * default to make allocations less likely. - */ -template -using ScopedVector = Vector; - } /* namespace blender */ #endif /* __BLI_VECTOR_HH__ */ -- cgit v1.2.3