[Bf-blender-cvs] [1f52afb3fd4] functions: new Vector.remove_first_occurence_and_reorder method

Jacques Lucke noreply at git.blender.org
Sat Nov 23 18:05:12 CET 2019


Commit: 1f52afb3fd4eee534c518dd5dd77cad5e1356cb1
Author: Jacques Lucke
Date:   Sat Nov 23 17:25:41 2019 +0100
Branches: functions
https://developer.blender.org/rB1f52afb3fd4eee534c518dd5dd77cad5e1356cb1

new Vector.remove_first_occurence_and_reorder method

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

M	source/blender/blenlib/BLI_vector.h
M	tests/gtests/blenlib/BLI_vector_test.cc

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

diff --git a/source/blender/blenlib/BLI_vector.h b/source/blender/blenlib/BLI_vector.h
index b2fc53a4fac..d07a4e0dc82 100644
--- a/source/blender/blenlib/BLI_vector.h
+++ b/source/blender/blenlib/BLI_vector.h
@@ -450,6 +450,12 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
     UPDATE_VECTOR_SIZE(this);
   }
 
+  void remove_first_occurrence_and_reorder(const T &value)
+  {
+    uint index = this->index(value);
+    this->remove_and_reorder(index);
+  }
+
   /**
    * Do a linear search to find the value in the vector.
    * When found, return the first index, otherwise return -1.
diff --git a/tests/gtests/blenlib/BLI_vector_test.cc b/tests/gtests/blenlib/BLI_vector_test.cc
index 69015a96582..c2ea3f10ff6 100644
--- a/tests/gtests/blenlib/BLI_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_vector_test.cc
@@ -349,6 +349,22 @@ TEST(vector, RemoveReorder)
   EXPECT_TRUE(vec.empty());
 }
 
+TEST(vector, RemoveFirstOccurrenceAndReorder)
+{
+  IntVector vec = {4, 5, 6, 7};
+  vec.remove_first_occurrence_and_reorder(5);
+  EXPECT_EQ(vec[0], 4);
+  EXPECT_EQ(vec[1], 7);
+  EXPECT_EQ(vec[2], 6);
+  vec.remove_first_occurrence_and_reorder(6);
+  EXPECT_EQ(vec[0], 4);
+  EXPECT_EQ(vec[1], 7);
+  vec.remove_first_occurrence_and_reorder(4);
+  EXPECT_EQ(vec[0], 7);
+  vec.remove_first_occurrence_and_reorder(7);
+  EXPECT_EQ(vec.size(), 0);
+}
+
 TEST(vector, AllEqual_False)
 {
   IntVector a = {1, 2, 3};



More information about the Bf-blender-cvs mailing list