[Bf-blender-cvs] [a61852a5e2e] functions: store size in IndexRange

Jacques Lucke noreply at git.blender.org
Thu Sep 5 19:11:39 CEST 2019


Commit: a61852a5e2ea42b43c5921efe4dc81726ff8ff41
Author: Jacques Lucke
Date:   Thu Sep 5 12:12:08 2019 +0200
Branches: functions
https://developer.blender.org/rBa61852a5e2ea42b43c5921efe4dc81726ff8ff41

store size in IndexRange

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

M	source/blender/blenlib/BLI_array_ref.hpp
M	source/blender/blenlib/BLI_chunked_range.hpp
M	source/blender/blenlib/BLI_range.hpp
M	source/blender/blenlib/intern/BLI_range.cpp
M	source/blender/functions/core/data_graph.hpp
M	source/blender/simulations/bparticles/action_interface.hpp
M	source/blender/simulations/bparticles/particle_allocator.cpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	tests/gtests/blenlib/BLI_array_ref_test.cc
M	tests/gtests/blenlib/BLI_range_test.cc

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

diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index 7d0a74f8575..36256e52ad0 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -81,10 +81,10 @@ template<typename T> class ArrayRef {
    * Return a continuous part of the array.
    * Asserts that the slice stays within the array.
    */
-  ArrayRef slice(uint start, uint length) const
+  ArrayRef slice(uint start, uint size) const
   {
-    BLI_assert(start + length <= this->size() || length == 0);
-    return ArrayRef(m_start + start, length);
+    BLI_assert(start + size <= this->size() || size == 0);
+    return ArrayRef(m_start + start, size);
   }
 
   ArrayRef slice(IndexRange range) const
diff --git a/source/blender/blenlib/BLI_chunked_range.hpp b/source/blender/blenlib/BLI_chunked_range.hpp
index 101f9676f94..124dbd05dd6 100644
--- a/source/blender/blenlib/BLI_chunked_range.hpp
+++ b/source/blender/blenlib/BLI_chunked_range.hpp
@@ -49,8 +49,8 @@ class ChunkedIndexRange {
   {
     BLI_assert(index < m_chunk_amount);
     uint start = m_total_range[index * m_chunk_size];
-    uint one_after_last = std::min<uint>(start + m_chunk_size, m_total_range.one_after_last());
-    return IndexRange(start, one_after_last);
+    uint size = std::min<uint>(m_chunk_size, m_total_range.one_after_last() - start);
+    return IndexRange(start, size);
   }
 };
 
diff --git a/source/blender/blenlib/BLI_range.hpp b/source/blender/blenlib/BLI_range.hpp
index f0eed080c81..d6083df6ffa 100644
--- a/source/blender/blenlib/BLI_range.hpp
+++ b/source/blender/blenlib/BLI_range.hpp
@@ -38,22 +38,17 @@ template<typename T> class ArrayRef;
 class IndexRange {
  private:
   uint m_start = 0;
-  uint m_one_after_last = 0;
+  uint m_size = 0;
 
  public:
   IndexRange() = default;
 
-  explicit IndexRange(uint one_after_last) : m_start(0), m_one_after_last(one_after_last)
+  explicit IndexRange(uint size) : m_start(0), m_size(size)
   {
   }
 
-  /**
-   * Construct a new range.
-   * Asserts when start is larger than one_after_last.
-   */
-  IndexRange(uint start, uint one_after_last) : m_start(start), m_one_after_last(one_after_last)
+  IndexRange(uint start, uint size) : m_start(start), m_size(size)
   {
-    BLI_assert(start <= one_after_last);
   }
 
   class Iterator {
@@ -89,7 +84,7 @@ class IndexRange {
 
   Iterator end() const
   {
-    return Iterator(m_one_after_last);
+    return Iterator(m_start + m_size);
   }
 
   /**
@@ -106,8 +101,7 @@ class IndexRange {
    */
   friend bool operator==(IndexRange a, IndexRange b)
   {
-    return (a.m_start == b.m_start && a.m_one_after_last == b.m_one_after_last) ||
-           (a.size() == 0 && b.size() == 0);
+    return (a.m_size == b.m_size) && (a.m_start == b.m_start || a.m_size == 0);
   }
 
   /**
@@ -115,7 +109,7 @@ class IndexRange {
    */
   uint size() const
   {
-    return m_one_after_last - m_start;
+    return m_size;
   }
 
   /**
@@ -123,7 +117,7 @@ class IndexRange {
    */
   IndexRange after(uint n) const
   {
-    return IndexRange(m_one_after_last, m_one_after_last + n);
+    return IndexRange(m_start + m_size, n);
   }
 
   /**
@@ -131,7 +125,7 @@ class IndexRange {
    */
   IndexRange before(uint n) const
   {
-    return IndexRange(m_start - n, m_start);
+    return IndexRange(m_start - n, n);
   }
 
   /**
@@ -151,7 +145,7 @@ class IndexRange {
   uint last() const
   {
     BLI_assert(this->size() > 0);
-    return m_one_after_last - 1;
+    return m_start + m_size - 1;
   }
 
   /**
@@ -159,7 +153,7 @@ class IndexRange {
    */
   uint one_after_last() const
   {
-    return m_one_after_last;
+    return m_start + m_size;
   }
 
   /**
@@ -175,14 +169,14 @@ class IndexRange {
    */
   bool contains(uint value) const
   {
-    return value >= m_start && value < m_one_after_last;
+    return value >= m_start && value < m_start + m_size;
   }
 
   IndexRange slice(uint start, uint size) const
   {
     uint new_start = m_start + start;
-    BLI_assert(new_start + size <= m_one_after_last || size == 0);
-    return IndexRange(new_start, new_start + size);
+    BLI_assert(new_start + size <= m_start + m_size || size == 0);
+    return IndexRange(new_start, size);
   }
 
   /**
diff --git a/source/blender/blenlib/intern/BLI_range.cpp b/source/blender/blenlib/intern/BLI_range.cpp
index 5b567b1656b..8e11fc474fc 100644
--- a/source/blender/blenlib/intern/BLI_range.cpp
+++ b/source/blender/blenlib/intern/BLI_range.cpp
@@ -24,7 +24,7 @@ static uint array[RANGE_AS_ARRAY_REF_MAX_LEN];
 
 ArrayRef<uint> IndexRange::as_array_ref() const
 {
-  BLI_assert(m_one_after_last <= RANGE_AS_ARRAY_REF_MAX_LEN);
+  BLI_assert(m_size <= RANGE_AS_ARRAY_REF_MAX_LEN);
   if (!array_is_initialized) {
     for (uint i = 0; i < RANGE_AS_ARRAY_REF_MAX_LEN; i++) {
       array[i] = i;
diff --git a/source/blender/functions/core/data_graph.hpp b/source/blender/functions/core/data_graph.hpp
index 1d50662f5a4..ba6bcb918c6 100644
--- a/source/blender/functions/core/data_graph.hpp
+++ b/source/blender/functions/core/data_graph.hpp
@@ -264,7 +264,7 @@ class DataGraph : public RefCounter {
   IndexRange input_ids_of_node(uint node_id) const
   {
     const Node &node = m_nodes[node_id];
-    return IndexRange(node.inputs_start, node.inputs_start + node.function->input_amount());
+    return IndexRange(node.inputs_start, node.function->input_amount());
   }
 
   DataSocketSequence<IndexRange> inputs_of_node(uint node_id) const
@@ -275,7 +275,7 @@ class DataGraph : public RefCounter {
   IndexRange output_ids_of_node(uint node_id) const
   {
     const Node &node = m_nodes[node_id];
-    return IndexRange(node.outputs_start, node.outputs_start + node.function->output_amount());
+    return IndexRange(node.outputs_start, node.function->output_amount());
   }
 
   DataSocketSequence<IndexRange> outputs_of_node(uint node_id) const
diff --git a/source/blender/simulations/bparticles/action_interface.hpp b/source/blender/simulations/bparticles/action_interface.hpp
index c67c5f5e4f3..fd2ccb78ef1 100644
--- a/source/blender/simulations/bparticles/action_interface.hpp
+++ b/source/blender/simulations/bparticles/action_interface.hpp
@@ -137,7 +137,7 @@ inline void Action::execute_from_emitter(AttributesRefGroup &new_particles,
   uint offset = 0;
   for (AttributesRef attributes : new_particles) {
     uint range_size = attributes.size();
-    IndexRange range(offset, offset + range_size);
+    IndexRange range(offset, range_size);
     offset += range_size;
 
     build_context(range, (void *)action_context);
@@ -198,7 +198,7 @@ inline void Action::execute_for_new_particles(AttributesRefGroup &new_particles,
   uint offset = 0;
   for (AttributesRef attributes : new_particles) {
     uint range_size = attributes.size();
-    action_context->update(IndexRange(offset, offset + range_size));
+    action_context->update(IndexRange(offset, range_size));
     offset += range_size;
 
     AttributesRef offsets(info, buffers, range_size);
diff --git a/source/blender/simulations/bparticles/particle_allocator.cpp b/source/blender/simulations/bparticles/particle_allocator.cpp
index ea2a289b89b..f3c04aa0320 100644
--- a/source/blender/simulations/bparticles/particle_allocator.cpp
+++ b/source/blender/simulations/bparticles/particle_allocator.cpp
@@ -39,7 +39,7 @@ void ParticleAllocator::allocate_block_ranges(StringRef particle_type_name,
     ParticlesBlock &block = this->get_non_full_block(particle_type_name);
 
     uint size_to_use = std::min(block.unused_amount(), remaining_size);
-    IndexRange range(block.active_amount(), block.active_amount() + size_to_use);
+    IndexRange range(block.active_amount(), size_to_use);
     block.active_amount() += size_to_use;
 
     r_blocks.append(&block);
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index 9df5009e96d..5754311f114 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -233,7 +233,7 @@ inline uint ParticlesContainer::count_active() const
 inline IndexRange ParticlesContainer::new_particle_ids(uint amount)
 {
   uint start = m_next_particle_id.fetch_add(amount);
-  return IndexRange(start, start + amount);
+  return IndexRange(start, amount);
 }
 
 inline AttributesInfo &ParticlesContainer::attributes_info()
@@ -320,7 +320,7 @@ inline AttributesRef ParticlesBlock::attributes_slice(IndexRange range)
 
 inline AttributesRef ParticlesBlock::attributes_slice(uint start, uint length)
 {
-  return this->attributes_slice(IndexRange(start, start + length));
+  return this->attributes_slice(IndexRange(start, length));
 }
 
 inline AttributesRef ParticlesBlock::attributes_all()
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index 78236d4d3ad..81964fc596f 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -97,7 +97,7 @@ TEST(array_ref, SliceEmpty)
 TEST(array_ref, SliceRange)
 {
   IntVector a = {1, 2, 3, 4, 5};
-  auto slice = IntArrayRef(a).slice(IndexRange(2, 4));
+  auto slice = IntArrayRef(a).slice(IndexRange(2, 2));
   EXPECT_EQ(slice.size(), 2);
   EXPECT_EQ(slice[0], 3);
   EXPECT_EQ(slice[1], 4);
diff --git a/tests/gtests/blenlib/BLI_range_test.cc b/tests/gtests/blenlib/BLI_range_test.cc
index 25aa8b7ded8..8e041528f09 100644
--- a/tests/gtests/blenlib/BLI_range_test.cc
+++ b/tests/gtests/blenlib/BLI_range_test.cc
@@ -22,7 +22,7 @@ TEST(index_range, DefaultConstructor)
 
 TEST(index_range, SingleElementRange)
 {
-  IndexRange range(4, 5);
+  IndexRange range(4, 1);
   EXPECT_EQ(range.size(), 1);
   EXPECT_EQ(*range.begin(), 4);
 
@@ -37,7 +37,7 @@ TEST(index_range, SingleElementRange)
 
 TEST(index_range, MultipleElementRange)
 {
-  IndexRange range(6, 10);
+  IndexRange range(6, 4);
   EXPECT_EQ(range.size(), 4);
 
   IntVector vector;
@@ -53,7 +53,7 @@ TEST(index_range, MultipleElementRange)
 
 TEST(index_range, SubscriptOperator)
 {
-  IndexRange range(5, 10);
+  IndexRange range(5, 5);
   EXPECT_EQ(range[0], 5);
   EXPECT_EQ(range[1], 6);
   EXPECT_EQ(

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list