[Bf-blender-cvs] [f09f8ad61fc] functions: move slicing functionality to ArrayRef
Jacques Lucke
noreply at git.blender.org
Wed May 1 15:27:27 CEST 2019
Commit: f09f8ad61fc65c82a64bbae1dd3aad0a8f7fca06
Author: Jacques Lucke
Date: Wed May 1 11:39:05 2019 +0200
Branches: functions
https://developer.blender.org/rBf09f8ad61fc65c82a64bbae1dd3aad0a8f7fca06
move slicing functionality to ArrayRef
===================================================================
M source/blender/blenlib/BLI_array_ref.hpp
M source/blender/blenlib/BLI_small_vector.hpp
A tests/gtests/blenlib/BLI_array_ref_test.cc
M tests/gtests/blenlib/BLI_small_vector_test.cc
M tests/gtests/blenlib/CMakeLists.txt
===================================================================
diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index 9b7b6234674..d04a1b04a94 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include "BLI_small_vector.hpp"
+
namespace BLI {
template<typename T> class ArrayRef {
@@ -14,12 +16,34 @@ template<typename T> class ArrayRef {
{
}
- T *begin()
+ ArrayRef(SmallVector<T> &vector) : m_start(vector.begin()), m_size(vector.size())
+ {
+ }
+
+ ArrayRef slice(uint start, uint length) const
+ {
+ BLI_assert(start + length <= this->size());
+ return ArrayRef(m_start + start, length);
+ }
+
+ ArrayRef drop_front(uint n = 1) const
+ {
+ BLI_assert(n <= this->size());
+ return this->slice(n, this->size() - n);
+ }
+
+ ArrayRef drop_back(uint n = 1) const
+ {
+ BLI_assert(n <= this->size());
+ return this->slice(0, this->size() - n);
+ }
+
+ T *begin() const
{
return m_start;
}
- T *end()
+ T *end() const
{
return m_start + m_size;
}
diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 8b00996e96b..aba74d2850f 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -1,7 +1,6 @@
#pragma once
#include "BLI_utildefines.h"
-#include "BLI_array_ref.hpp"
#include "MEM_guardedalloc.h"
#include <cstdlib>
@@ -183,25 +182,6 @@ 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()) {
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
new file mode 100644
index 00000000000..5c71bb27542
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -0,0 +1,74 @@
+#include "testing/testing.h"
+#include "BLI_array_ref.hpp"
+
+using IntVector = BLI::SmallVector<int>;
+using IntArrayRef = BLI::ArrayRef<int>;
+
+TEST(array_ref, FromSmallVector)
+{
+ IntVector a = {1, 2, 3};
+ IntArrayRef a_ref = a;
+ EXPECT_EQ(a_ref.size(), 3);
+ EXPECT_EQ(a_ref[0], 1);
+ EXPECT_EQ(a_ref[1], 2);
+ EXPECT_EQ(a_ref[2], 3);
+}
+
+TEST(array_ref, IsReferencing)
+{
+ int array[] = {3, 5, 8};
+ IntArrayRef ref(array, ARRAY_SIZE(array));
+ EXPECT_EQ(ref.size(), 3);
+ EXPECT_EQ(ref[1], 5);
+ array[1] = 10;
+ EXPECT_EQ(ref[1], 10);
+}
+
+TEST(array_ref, DropBack)
+{
+ IntVector a = {4, 5, 6, 7};
+ auto slice = IntArrayRef(a).drop_back(2);
+ EXPECT_EQ(slice.size(), 2);
+ EXPECT_EQ(slice[0], 4);
+ EXPECT_EQ(slice[1], 5);
+}
+
+TEST(array_ref, DropBackAll)
+{
+ IntVector a = {4, 5, 6, 7};
+ auto slice = IntArrayRef(a).drop_back(a.size());
+ EXPECT_EQ(slice.size(), 0);
+}
+
+TEST(array_ref, DropFront)
+{
+ IntVector a = {4, 5, 6, 7};
+ auto slice = IntArrayRef(a).drop_front(1);
+ EXPECT_EQ(slice.size(), 3);
+ EXPECT_EQ(slice[0], 5);
+ EXPECT_EQ(slice[1], 6);
+ EXPECT_EQ(slice[2], 7);
+}
+
+TEST(array_ref, DropFrontAll)
+{
+ IntVector a = {4, 5, 6, 7};
+ auto slice = IntArrayRef(a).drop_front(a.size());
+ EXPECT_EQ(slice.size(), 0);
+}
+
+TEST(array_ref, Slice)
+{
+ IntVector a = {4, 5, 6, 7};
+ auto slice = IntArrayRef(a).slice(1, 2);
+ 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 = IntArrayRef(a).slice(2, 0);
+ EXPECT_EQ(slice.size(), 0);
+}
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc b/tests/gtests/blenlib/BLI_small_vector_test.cc
index 82c6bfef1f3..7dedd48e766 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -208,52 +208,3 @@ TEST(small_vector, AllEqual_True)
bool result = IntVector::all_equal(a, b);
EXPECT_TRUE(result);
}
-
-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);
-}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index 3da56bd460d..49e42a931e1 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -41,6 +41,7 @@ endif()
BLENDER_TEST(BLI_array_lookup "bf_blenlib")
BLENDER_TEST(BLI_array_store "bf_blenlib")
BLENDER_TEST(BLI_array_utils "bf_blenlib")
+BLENDER_TEST(BLI_array_ref "bf_blenlib")
BLENDER_TEST(BLI_expr_pylike_eval "bf_blenlib")
BLENDER_TEST(BLI_edgehash "bf_blenlib")
BLENDER_TEST(BLI_ghash "bf_blenlib")
More information about the Bf-blender-cvs
mailing list