[Bf-blender-cvs] [f4633cf46c0] master: BLI: simplify copy constructor of vector

Jacques Lucke noreply at git.blender.org
Tue Jul 7 19:16:48 CEST 2020


Commit: f4633cf46c075c65d8238e3cb81de6dac95610a8
Author: Jacques Lucke
Date:   Tue Jul 7 19:16:31 2020 +0200
Branches: master
https://developer.blender.org/rBf4633cf46c075c65d8238e3cb81de6dac95610a8

BLI: simplify copy constructor of vector

===================================================================

M	source/blender/blenlib/BLI_vector.hh

===================================================================

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<T> values) : Vector()
+  Vector(Span<T> 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<uint OtherInlineBufferCapacity>
   Vector(const Vector<T, OtherInlineBufferCapacity, Allocator> &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<uint OtherInlineBufferCapacity>
-  void init_copy_from_other_vector(const Vector<T, OtherInlineBufferCapacity, Allocator> &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<typename T, uint InlineBufferCapacity = 20>
-using ScopedVector = Vector<T, InlineBufferCapacity, GuardedAllocator>;
-
 } /* namespace blender */
 
 #endif /* __BLI_VECTOR_HH__ */



More information about the Bf-blender-cvs mailing list