[Bf-blender-cvs] [7704e6a678a] master: Cleanup: bring operator overloads closer together

Jacques Lucke noreply at git.blender.org
Fri Jul 3 14:31:33 CEST 2020


Commit: 7704e6a678ab324b479797dc8d8f8d63b109956f
Author: Jacques Lucke
Date:   Fri Jul 3 14:31:26 2020 +0200
Branches: master
https://developer.blender.org/rB7704e6a678ab324b479797dc8d8f8d63b109956f

Cleanup: bring operator overloads closer together

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

M	source/blender/blenlib/BLI_array.hh
M	source/blender/blenlib/BLI_float3.hh
M	source/blender/blenlib/BLI_float4x4.hh
M	source/blender/blenlib/BLI_vector.hh
M	source/blender/blenlib/BLI_vector_set.hh

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

diff --git a/source/blender/blenlib/BLI_array.hh b/source/blender/blenlib/BLI_array.hh
index 8569d3d1af8..ee4e9702779 100644
--- a/source/blender/blenlib/BLI_array.hh
+++ b/source/blender/blenlib/BLI_array.hh
@@ -201,6 +201,18 @@ class Array {
     return *this;
   }
 
+  T &operator[](uint index)
+  {
+    BLI_assert(index < size_);
+    return data_[index];
+  }
+
+  const T &operator[](uint index) const
+  {
+    BLI_assert(index < size_);
+    return data_[index];
+  }
+
   operator Span<T>() const
   {
     return Span<T>(data_, size_);
@@ -221,18 +233,6 @@ class Array {
     return *this;
   }
 
-  T &operator[](uint index)
-  {
-    BLI_assert(index < size_);
-    return data_[index];
-  }
-
-  const T &operator[](uint index) const
-  {
-    BLI_assert(index < size_);
-    return data_[index];
-  }
-
   /**
    * Returns the number of elements in the array.
    */
diff --git a/source/blender/blenlib/BLI_float3.hh b/source/blender/blenlib/BLI_float3.hh
index 7ef4f1b4973..0edee600ef6 100644
--- a/source/blender/blenlib/BLI_float3.hh
+++ b/source/blender/blenlib/BLI_float3.hh
@@ -58,56 +58,6 @@ struct float3 {
     return &x;
   }
 
-  float normalize_and_get_length()
-  {
-    return normalize_v3(*this);
-  }
-
-  float3 normalized() const
-  {
-    float3 result;
-    normalize_v3_v3(result, *this);
-    return result;
-  }
-
-  float length() const
-  {
-    return len_v3(*this);
-  }
-
-  float length_squared() const
-  {
-    return len_squared_v3(*this);
-  }
-
-  void reflect(const float3 &normal)
-  {
-    *this = this->reflected(normal);
-  }
-
-  float3 reflected(const float3 &normal) const
-  {
-    float3 result;
-    reflect_v3_v3v3(result, *this, normal);
-    return result;
-  }
-
-  static float3 safe_divide(const float3 &a, const float3 &b)
-  {
-    float3 result;
-    result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x;
-    result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y;
-    result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z;
-    return result;
-  }
-
-  void invert()
-  {
-    x = -x;
-    y = -y;
-    z = -z;
-  }
-
   friend float3 operator+(const float3 &a, const float3 &b)
   {
     return {a.x + b.x, a.y + b.y, a.z + b.z};
@@ -178,6 +128,56 @@ struct float3 {
     return stream;
   }
 
+  float normalize_and_get_length()
+  {
+    return normalize_v3(*this);
+  }
+
+  float3 normalized() const
+  {
+    float3 result;
+    normalize_v3_v3(result, *this);
+    return result;
+  }
+
+  float length() const
+  {
+    return len_v3(*this);
+  }
+
+  float length_squared() const
+  {
+    return len_squared_v3(*this);
+  }
+
+  void reflect(const float3 &normal)
+  {
+    *this = this->reflected(normal);
+  }
+
+  float3 reflected(const float3 &normal) const
+  {
+    float3 result;
+    reflect_v3_v3v3(result, *this, normal);
+    return result;
+  }
+
+  static float3 safe_divide(const float3 &a, const float3 &b)
+  {
+    float3 result;
+    result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x;
+    result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y;
+    result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z;
+    return result;
+  }
+
+  void invert()
+  {
+    x = -x;
+    y = -y;
+    z = -z;
+  }
+
   static float dot(const float3 &a, const float3 &b)
   {
     return a.x * b.x + a.y * b.y + a.z * b.z;
diff --git a/source/blender/blenlib/BLI_float4x4.hh b/source/blender/blenlib/BLI_float4x4.hh
index 0abfb751ebf..1e9bd12b12b 100644
--- a/source/blender/blenlib/BLI_float4x4.hh
+++ b/source/blender/blenlib/BLI_float4x4.hh
@@ -46,23 +46,6 @@ struct float4x4 {
     return (const float *)this;
   }
 
-  float4x4 inverted() const
-  {
-    float result[4][4];
-    invert_m4_m4(result, values);
-    return result;
-  }
-
-  /**
-   * Matrix inversion can be implemented more efficiently for affine matrices.
-   */
-  float4x4 inverted_affine() const
-  {
-    BLI_assert(values[0][3] == 0.0f && values[1][3] == 0.0f && values[2][3] == 0.0f &&
-               values[3][3] == 1.0f);
-    return this->inverted();
-  }
-
   friend float4x4 operator*(const float4x4 &a, const float4x4 &b)
   {
     float4x4 result;
@@ -86,6 +69,23 @@ struct float4x4 {
     return m * float3(v);
   }
 
+  float4x4 inverted() const
+  {
+    float result[4][4];
+    invert_m4_m4(result, values);
+    return result;
+  }
+
+  /**
+   * Matrix inversion can be implemented more efficiently for affine matrices.
+   */
+  float4x4 inverted_affine() const
+  {
+    BLI_assert(values[0][3] == 0.0f && values[1][3] == 0.0f && values[2][3] == 0.0f &&
+               values[3][3] == 1.0f);
+    return this->inverted();
+  }
+
   struct float3x3_ref {
     const float4x4 &data;
 
diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh
index 1d161c419c8..50283d7b2b5 100644
--- a/source/blender/blenlib/BLI_vector.hh
+++ b/source/blender/blenlib/BLI_vector.hh
@@ -263,26 +263,6 @@ class Vector {
     }
   }
 
-  operator Span<T>() const
-  {
-    return Span<T>(begin_, this->size());
-  }
-
-  operator MutableSpan<T>()
-  {
-    return MutableSpan<T>(begin_, this->size());
-  }
-
-  Span<T> as_span() const
-  {
-    return *this;
-  }
-
-  MutableSpan<T> as_mutable_span()
-  {
-    return *this;
-  }
-
   Vector &operator=(const Vector &other)
   {
     if (this == &other) {
@@ -309,6 +289,42 @@ class Vector {
     return *this;
   }
 
+  /**
+   * Get the value at the given index. This invokes undefined behavior when the index is out of
+   * bounds.
+   */
+  const T &operator[](uint index) const
+  {
+    BLI_assert(index < this->size());
+    return begin_[index];
+  }
+
+  T &operator[](uint index)
+  {
+    BLI_assert(index < this->size());
+    return begin_[index];
+  }
+
+  operator Span<T>() const
+  {
+    return Span<T>(begin_, this->size());
+  }
+
+  operator MutableSpan<T>()
+  {
+    return MutableSpan<T>(begin_, this->size());
+  }
+
+  Span<T> as_span() const
+  {
+    return *this;
+  }
+
+  MutableSpan<T> as_mutable_span()
+  {
+    return *this;
+  }
+
   /**
    * Make sure that enough memory is allocated to hold min_capacity elements.
    * This won't necessarily make an allocation when min_capacity is small.
@@ -673,22 +689,6 @@ class Vector {
     return this->first_index_of_try(value) != -1;
   }
 
-  /**
-   * Get the value at the given index. This invokes undefined behavior when the index is out of
-   * bounds.
-   */
-  const T &operator[](uint index) const
-  {
-    BLI_assert(index < this->size());
-    return begin_[index];
-  }
-
-  T &operator[](uint index)
-  {
-    BLI_assert(index < this->size());
-    return begin_[index];
-  }
-
   /**
    * Get access to the underlying array.
    */
diff --git a/source/blender/blenlib/BLI_vector_set.hh b/source/blender/blenlib/BLI_vector_set.hh
index 6d7fd101b65..2ee1113e221 100644
--- a/source/blender/blenlib/BLI_vector_set.hh
+++ b/source/blender/blenlib/BLI_vector_set.hh
@@ -235,6 +235,31 @@ class VectorSet {
     return *this;
   }
 
+  /**
+   * Get the key stored at the given position in the vector.
+   */
+  const Key &operator[](uint32_t index) const
+  {
+    BLI_assert(index <= this->size());
+    return keys_[index];
+  }
+
+  operator Span<Key>() const
+  {
+    return Span<Key>(keys_, this->size());
+  }
+
+  /**
+   * Get an Span referencing the keys vector. The referenced memory buffer is only valid as
+   * long as the vector set is not changed.
+   *
+   * The keys must not be changed, because this would change their hash value.
+   */
+  Span<Key> as_span() const
+  {
+    return *this;
+  }
+
   /**
    * Add a new key to the vector set. This invokes undefined behavior when the key is in the set
    * already. When you know for certain that a key is not in the set yet, use this method for
@@ -402,31 +427,6 @@ class VectorSet {
     return keys_ + this->size();
   }
 
-  /**
-   * Get the key stored at the given position in the vector.
-   */
-  const Key &operator[](uint32_t index) const
-  {
-    BLI_assert(index <= this->size());
-    return keys_[index];
-  }
-
-  operator Span<Key>() const
-  {
-    return Span<Key>(keys_, this->size());
-  }
-
-  /**
-   * Get an Span referencing the keys vector. The referenced memory buffer is only valid as
-   * long as the vector set is not changed.
-   *
-   * The keys must not be changed, because this would change their hash value.
-   */
-  Span<Key> as_span() const
-  {
-    return *this;
-  }
-
   /**
    * Print common statistics like size and collision count. This is useful for debugging purposes.
    */



More information about the Bf-blender-cvs mailing list