[Bf-blender-cvs] [cb355564722] functions: support slicing ArrayRef with range

Jacques Lucke noreply at git.blender.org
Wed Sep 4 19:43:12 CEST 2019


Commit: cb3555647224cdb735ebac27adbffe4d01f68061
Author: Jacques Lucke
Date:   Wed Sep 4 12:11:44 2019 +0200
Branches: functions
https://developer.blender.org/rBcb3555647224cdb735ebac27adbffe4d01f68061

support slicing ArrayRef with range

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

M	source/blender/blenlib/BLI_array_ref.hpp
M	source/blender/blenlib/BLI_range.hpp
M	source/blender/blenlib/intern/BLI_range.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 cf1bafad8ff..273690ddcc8 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -42,6 +42,7 @@
 
 #include "BLI_utildefines.h"
 #include "BLI_memory.hpp"
+#include "BLI_range.hpp"
 
 namespace BLI {
 
@@ -86,6 +87,11 @@ template<typename T> class ArrayRef {
     return ArrayRef(m_start + start, length);
   }
 
+  ArrayRef slice(Range<uint> range) const
+  {
+    return this->slice(range.start(), range.size());
+  }
+
   /**
    * Return a new ArrayRef with n elements removed from the beginning.
    * Asserts that the array contains enough elements.
diff --git a/source/blender/blenlib/BLI_range.hpp b/source/blender/blenlib/BLI_range.hpp
index 752311a213e..9d792cf1cc8 100644
--- a/source/blender/blenlib/BLI_range.hpp
+++ b/source/blender/blenlib/BLI_range.hpp
@@ -24,14 +24,16 @@
 #pragma once
 
 #include <cmath>
+#include <algorithm>
 
 #include "BLI_utildefines.h"
-#include "BLI_array_ref.hpp"
 
 #define RANGE_AS_ARRAY_REF_MAX_LEN 10000
 
 namespace BLI {
 
+template<typename T> class ArrayRef;
+
 template<typename T> class Range {
  private:
   T m_start = 0;
diff --git a/source/blender/blenlib/intern/BLI_range.cpp b/source/blender/blenlib/intern/BLI_range.cpp
index d71814cc39c..fd9098c3b8a 100644
--- a/source/blender/blenlib/intern/BLI_range.cpp
+++ b/source/blender/blenlib/intern/BLI_range.cpp
@@ -15,6 +15,7 @@
  */
 
 #include "BLI_range.hpp"
+#include "BLI_array_ref.hpp"
 
 namespace BLI {
 
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index 6eea85e2bed..fefaf494d7f 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -2,6 +2,7 @@
 #include "BLI_array_ref.hpp"
 #include "BLI_vector.hpp"
 
+using BLI::Range;
 using IntVector = BLI::Vector<int>;
 using IntArrayRef = BLI::ArrayRef<int>;
 using MutableIntArrayRef = BLI::MutableArrayRef<int>;
@@ -93,6 +94,15 @@ TEST(array_ref, SliceEmpty)
   EXPECT_EQ(slice.size(), 0);
 }
 
+TEST(array_ref, SliceRange)
+{
+  IntVector a = {1, 2, 3, 4, 5};
+  auto slice = IntArrayRef(a).slice(Range<uint>(2, 4));
+  EXPECT_EQ(slice.size(), 2);
+  EXPECT_EQ(slice[0], 3);
+  EXPECT_EQ(slice[1], 4);
+}
+
 TEST(array_ref, Contains)
 {
   IntVector a = {4, 5, 6, 7};



More information about the Bf-blender-cvs mailing list