[Bf-blender-cvs] [e9eb08fea16] master: BLI: support equality operator on Span and Vector

Jacques Lucke noreply at git.blender.org
Wed Mar 17 16:46:28 CET 2021


Commit: e9eb08fea16ab033bec52e9b7355f6b52245e152
Author: Jacques Lucke
Date:   Wed Mar 17 16:38:49 2021 +0100
Branches: master
https://developer.blender.org/rBe9eb08fea16ab033bec52e9b7355f6b52245e152

BLI: support equality operator on Span and Vector

This is quite convenient sometimes and is available for `std::vector` as well.

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

M	source/blender/blenlib/BLI_span.hh
M	source/blender/blenlib/BLI_vector.hh
M	source/blender/blenlib/tests/BLI_span_test.cc

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

diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh
index f4960df8ab9..fcc6d6f754b 100644
--- a/source/blender/blenlib/BLI_span.hh
+++ b/source/blender/blenlib/BLI_span.hh
@@ -417,6 +417,19 @@ template<typename T> class Span {
     return Span<NewT>(reinterpret_cast<const NewT *>(data_), new_size);
   }
 
+  friend bool operator==(const Span<T> a, const Span<T> b)
+  {
+    if (a.size() != b.size()) {
+      return false;
+    }
+    return std::equal(a.begin(), a.end(), b.begin());
+  }
+
+  friend bool operator!=(const Span<T> a, const Span<T> b)
+  {
+    return !(a == b);
+  }
+
   /**
    * A debug utility to print the content of the Span. Every element will be printed on a
    * separate line using the given callback.
diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh
index fe6d54ae9e5..eefacd5d64f 100644
--- a/source/blender/blenlib/BLI_vector.hh
+++ b/source/blender/blenlib/BLI_vector.hh
@@ -879,6 +879,16 @@ class Vector {
     return IndexRange(this->size());
   }
 
+  friend bool operator==(const Vector &a, const Vector &b)
+  {
+    return a.as_span() == b.as_span();
+  }
+
+  friend bool operator!=(const Vector &a, const Vector &b)
+  {
+    return !(a == b);
+  }
+
   /**
    * Print some debug information about the vector.
    */
diff --git a/source/blender/blenlib/tests/BLI_span_test.cc b/source/blender/blenlib/tests/BLI_span_test.cc
index f611529b47e..5e3c6a1b680 100644
--- a/source/blender/blenlib/tests/BLI_span_test.cc
+++ b/source/blender/blenlib/tests/BLI_span_test.cc
@@ -404,4 +404,21 @@ TEST(span, ImplicitConversions)
   BLI_STATIC_ASSERT((!std::is_convertible_v<Span<int *>, MutableSpan<const int *>>), "");
 }
 
+TEST(span, Comparison)
+{
+  std::array<int, 3> a = {3, 4, 5};
+  std::array<int, 4> b = {3, 4, 5, 6};
+
+  EXPECT_FALSE(Span(a) == Span(b));
+  EXPECT_FALSE(Span(b) == Span(a));
+  EXPECT_TRUE(Span(a) == Span(b).take_front(3));
+  EXPECT_TRUE(Span(a) == Span(a));
+  EXPECT_TRUE(Span(b) == Span(b));
+
+  EXPECT_TRUE(Span(a) != Span(b));
+  EXPECT_TRUE(Span(b) != Span(a));
+  EXPECT_FALSE(Span(a) != Span(b).take_front(3));
+  EXPECT_FALSE(Span(a) != Span(a));
+}
+
 }  // namespace blender::tests



More information about the Bf-blender-cvs mailing list