[Bf-blender-cvs] [034639a877d] functions: initial vector slicing API

Jacques Lucke noreply at git.blender.org
Wed May 1 15:27:24 CEST 2019


Commit: 034639a877de1f28f283bb54a015e0b7ee352788
Author: Jacques Lucke
Date:   Wed May 1 10:57:26 2019 +0200
Branches: functions
https://developer.blender.org/rB034639a877de1f28f283bb54a015e0b7ee352788

initial vector slicing API

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

M	source/blender/blenlib/BLI_small_vector.hpp
M	tests/gtests/blenlib/BLI_small_vector_test.cc

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

diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 0a775148e58..8b00996e96b 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -1,6 +1,8 @@
 #pragma once
 
 #include "BLI_utildefines.h"
+#include "BLI_array_ref.hpp"
+
 #include "MEM_guardedalloc.h"
 #include <cstdlib>
 #include <cstring>
@@ -181,6 +183,25 @@ template<typename T, uint N = 4> class SmallVector {
     return this->index(value) != -1;
   }
 
+  ArrayRef<T> slice_start(uint end_exlusive) const
+  {
+    return this->slice(0, end_exlusive);
+  }
+
+  ArrayRef<T> slice_end(uint start_inclusive) const
+  {
+    return this->slice(start_inclusive, this->size());
+  }
+
+  ArrayRef<T> slice(uint start_inclusive, uint end_exclusive) const
+  {
+    BLI_assert(start_inclusive <= this->size());
+    BLI_assert(end_exclusive <= this->size());
+    BLI_assert(start_inclusive <= end_exclusive);
+    uint length = end_exclusive - start_inclusive;
+    return ArrayRef<T>(this->begin() + start_inclusive, length);
+  }
+
   static bool all_equal(const SmallVector &a, const SmallVector &b)
   {
     if (a.size() != b.size()) {
@@ -239,7 +260,7 @@ template<typename T, uint N = 4> class SmallVector {
 
   bool is_index_in_range(uint index) const
   {
-    return index >= 0 && index < this->size();
+    return index < this->size();
   }
 
   T *element_ptr(uint index) const
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc b/tests/gtests/blenlib/BLI_small_vector_test.cc
index 78f2e9b0db9..82c6bfef1f3 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -207,4 +207,53 @@ TEST(small_vector, AllEqual_True)
   IntVector b = {4, 5, 6};
   bool result = IntVector::all_equal(a, b);
   EXPECT_TRUE(result);
-}
\ No newline at end of file
+}
+
+TEST(small_vector, SliceStart)
+{
+  IntVector a = {4, 5, 6, 7};
+  auto slice = a.slice_start(2);
+  EXPECT_EQ(slice.size(), 2);
+  EXPECT_EQ(slice[0], 4);
+  EXPECT_EQ(slice[1], 5);
+}
+
+TEST(small_vector, SliceStartEmpty)
+{
+  IntVector a = {4, 5, 6, 7};
+  auto slice = a.slice_start(0);
+  EXPECT_EQ(slice.size(), 0);
+}
+
+TEST(small_vector, SliceEnd)
+{
+  IntVector a = {4, 5, 6, 7};
+  auto slice = a.slice_end(1);
+  EXPECT_EQ(slice.size(), 3);
+  EXPECT_EQ(slice[0], 5);
+  EXPECT_EQ(slice[1], 6);
+  EXPECT_EQ(slice[2], 7);
+}
+
+TEST(small_vector, SliceEndEmpty)
+{
+  IntVector a = {4, 5, 6, 7};
+  auto slice = a.slice_end(4);
+  EXPECT_EQ(slice.size(), 0);
+}
+
+TEST(small_vector, Slice)
+{
+  IntVector a = {4, 5, 6, 7};
+  auto slice = a.slice(1, 3);
+  EXPECT_EQ(slice.size(), 2);
+  EXPECT_EQ(slice[0], 5);
+  EXPECT_EQ(slice[1], 6);
+}
+
+TEST(small_vector, SliceEmpty)
+{
+  IntVector a = {4, 5, 6, 7};
+  auto slice = a.slice(2, 2);
+  EXPECT_EQ(slice.size(), 0);
+}



More information about the Bf-blender-cvs mailing list