[Bf-blender-cvs] [16b2b33d019] master: BLI: Add "first" method to MutableSpan and Vector

Hans Goudey noreply at git.blender.org
Sun Apr 25 22:06:49 CEST 2021


Commit: 16b2b33d0194e79fabca7139658d61701c9d8915
Author: Hans Goudey
Date:   Sun Apr 25 15:06:38 2021 -0500
Branches: master
https://developer.blender.org/rB16b2b33d0194e79fabca7139658d61701c9d8915

BLI: Add "first" method to MutableSpan and Vector

This is convenient because having a uniform interface is nice, and
because of the similarity to "last".

Differential Revision: https://developer.blender.org/D11076

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

M	source/blender/blenlib/BLI_span.hh
M	source/blender/blenlib/BLI_vector.hh

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

diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh
index c32ba0826df..c3876d4eaf8 100644
--- a/source/blender/blenlib/BLI_span.hh
+++ b/source/blender/blenlib/BLI_span.hh
@@ -661,6 +661,16 @@ template<typename T> class MutableSpan {
     return IndexRange(size_);
   }
 
+  /**
+   * Return a reference to the first element in the array. This invokes undefined behavior when the
+   * array is empty.
+   */
+  constexpr T &first() const
+  {
+    BLI_assert(size_ > 0);
+    return data_[0];
+  }
+
   /**
    * Returns a reference to the last element. This invokes undefined behavior when the array is
    * empty.
diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh
index d08a5c65c52..3ffd5309a04 100644
--- a/source/blender/blenlib/BLI_vector.hh
+++ b/source/blender/blenlib/BLI_vector.hh
@@ -444,7 +444,7 @@ class Vector {
     this->append_as(std::move(value));
   }
   /* This is similar to `std::vector::emplace_back`. */
-  template<typename... ForwardValue> void append_as(ForwardValue &&... value)
+  template<typename... ForwardValue> void append_as(ForwardValue &&...value)
   {
     this->ensure_space_for_one();
     this->append_unchecked_as(std::forward<ForwardValue>(value)...);
@@ -486,7 +486,7 @@ class Vector {
   {
     this->append_unchecked_as(std::move(value));
   }
-  template<typename... ForwardT> void append_unchecked_as(ForwardT &&... value)
+  template<typename... ForwardT> void append_unchecked_as(ForwardT &&...value)
   {
     BLI_assert(end_ < capacity_end_);
     new (end_) T(std::forward<ForwardT>(value)...);
@@ -668,6 +668,21 @@ class Vector {
     return *(end_ - 1);
   }
 
+  /**
+   * Return a reference to the first element in the vector.
+   * This invokes undefined behavior when the vector is empty.
+   */
+  const T &first() const
+  {
+    BLI_assert(this->size() > 0);
+    return *begin_;
+  }
+  T &first()
+  {
+    BLI_assert(this->size() > 0);
+    return *begin_;
+  }
+
   /**
    * Return how many values are currently stored in the vector.
    */



More information about the Bf-blender-cvs mailing list