[Bf-blender-cvs] [35dcbf506de] functions: fill an array partially

Jacques Lucke noreply at git.blender.org
Thu Jul 11 17:15:08 CEST 2019


Commit: 35dcbf506de5c83af72942abe9cbe22a773c9f0f
Author: Jacques Lucke
Date:   Thu Jul 11 09:53:39 2019 +0200
Branches: functions
https://developer.blender.org/rB35dcbf506de5c83af72942abe9cbe22a773c9f0f

fill an array partially

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

M	source/blender/blenlib/BLI_array_ref.hpp
M	source/blender/simulations/bparticles/simulate.cpp
M	tests/gtests/blenlib/BLI_array_ref_test.cc

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

diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index d17f1e8662d..1c97ea7b537 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -128,6 +128,16 @@ template<typename T> class ArrayRef {
     std::fill_n(m_start, m_size, element);
   }
 
+  /**
+   * Replace a subset of all elements with the given value.
+   */
+  void fill_indices(ArrayRef<uint> indices, const T &element)
+  {
+    for (uint i : indices) {
+      m_start[i] = element;
+    }
+  }
+
   /**
    * Copy the values from another array into the references array.
    */
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 891f1e8458a..03c4d8b0a25 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -33,10 +33,8 @@ BLI_NOINLINE static void find_next_event_per_particle(ParticleSet particles,
                                                       ArrayRef<float> r_time_factors_to_next_event,
                                                       VectorAdaptor<uint> &r_pindices_with_event)
 {
-  for (uint pindex : particles.pindices()) {
-    r_next_event_indices[pindex] = -1;
-    r_time_factors_to_next_event[pindex] = 1.0f;
-  }
+  r_next_event_indices.fill_indices(particles.pindices(), -1);
+  r_time_factors_to_next_event.fill_indices(particles.pindices(), 1.0f);
 
   for (uint event_index = 0; event_index < events.size(); event_index++) {
     SmallVector<uint> triggered_pindices;
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index ace7c3f91f2..6559ec67409 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -179,6 +179,18 @@ TEST(array_ref, Fill)
   EXPECT_EQ(a[4], 1);
 }
 
+TEST(array_ref, FillIndices)
+{
+  std::array<int, 5> a = {0, 0, 0, 0, 0};
+  IntArrayRef a_ref(a);
+  a_ref.fill_indices({0, 2, 3}, 1);
+  EXPECT_EQ(a[0], 1);
+  EXPECT_EQ(a[1], 0);
+  EXPECT_EQ(a[2], 1);
+  EXPECT_EQ(a[3], 1);
+  EXPECT_EQ(a[4], 0);
+}
+
 TEST(array_ref, CopyFrom)
 {
   std::array<int, 3> a = {3, 4, 5};



More information about the Bf-blender-cvs mailing list