[Bf-blender-cvs] [aec147ec741] functions-experimental-refactor: new ArrayRef.first_index method

Jacques Lucke noreply at git.blender.org
Tue Oct 29 16:54:53 CET 2019


Commit: aec147ec7413d80bea7112d2a5862d7afa7970a1
Author: Jacques Lucke
Date:   Tue Oct 29 16:52:38 2019 +0100
Branches: functions-experimental-refactor
https://developer.blender.org/rBaec147ec7413d80bea7112d2a5862d7afa7970a1

new ArrayRef.first_index method

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

M	source/blender/blenlib/BLI_array_ref.h
M	tests/gtests/blenlib/BLI_array_ref_test.cc

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

diff --git a/source/blender/blenlib/BLI_array_ref.h b/source/blender/blenlib/BLI_array_ref.h
index 0455ed0a7e6..c68814a3a6b 100644
--- a/source/blender/blenlib/BLI_array_ref.h
+++ b/source/blender/blenlib/BLI_array_ref.h
@@ -278,6 +278,17 @@ template<typename T> class ArrayRef {
     return false;
   }
 
+  uint first_index(const T &search_value) const
+  {
+    for (uint i = 0; i < m_size; i++) {
+      if (m_start[i] == search_value) {
+        return i;
+      }
+    }
+    BLI_assert(false);
+    return 0;
+  }
+
   /**
    * Get a new array ref to the same underlying memory buffer. No conversions are done.
    * Asserts when the sizes of the types don't match.
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index d202ca66148..002086583eb 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -274,3 +274,13 @@ TEST(array_ref, ContainsPtr)
   EXPECT_FALSE(a_ref.contains_ptr(&a[0] - 1));
   EXPECT_FALSE(a_ref.contains_ptr(&other));
 }
+
+TEST(array_ref, FirstIndex)
+{
+  std::array<int, 5> a = {4, 5, 4, 2, 5};
+  IntArrayRef a_ref(a);
+
+  EXPECT_EQ(a_ref.first_index(4), 0);
+  EXPECT_EQ(a_ref.first_index(5), 1);
+  EXPECT_EQ(a_ref.first_index(2), 3);
+}



More information about the Bf-blender-cvs mailing list