Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Matthews <guitargeek>2021-09-15 19:13:10 +0300
committerHans Goudey <h.goudey@me.com>2021-09-15 19:13:10 +0300
commita6adb7ecaef38e419d9268074193942669be6e7f (patch)
tree449c8dc305fb5c5233bde31fdd988899213b50e7 /source/blender/blenlib
parent09f14b38f2d0c22b396dcd6c15bf959631ed1bfd (diff)
BLI: Add a reverse method to MutableSpan
Add a method that allows a MutableSpan to reverse itself. This reverses the data in the original span object. This is a first step in extracting some functionality from nodes and making it more general. Differential Revision: https://developer.blender.org/D12485
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/BLI_span.hh10
-rw-r--r--source/blender/blenlib/tests/BLI_span_test.cc23
2 files changed, 33 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh
index e04295b0e51..5adb47ba0b0 100644
--- a/source/blender/blenlib/BLI_span.hh
+++ b/source/blender/blenlib/BLI_span.hh
@@ -644,6 +644,16 @@ template<typename T> class MutableSpan {
}
/**
+ * Reverse the data in the MutableSpan.
+ */
+ constexpr void reverse()
+ {
+ for (const int i : IndexRange(size_ / 2)) {
+ std::swap(data_[size_ - 1 - i], data_[i]);
+ }
+ }
+
+ /**
* Returns an (immutable) Span that references the same array. This is usually not needed,
* due to implicit conversions. However, sometimes automatic type deduction needs some help.
*/
diff --git a/source/blender/blenlib/tests/BLI_span_test.cc b/source/blender/blenlib/tests/BLI_span_test.cc
index 4d23a53c08a..fb88fb63e53 100644
--- a/source/blender/blenlib/tests/BLI_span_test.cc
+++ b/source/blender/blenlib/tests/BLI_span_test.cc
@@ -362,6 +362,29 @@ TEST(span, ReverseIterator)
EXPECT_EQ_ARRAY(reversed_vec.data(), Span({7, 6, 5, 4}).data(), 4);
}
+TEST(span, ReverseMutableSpan)
+{
+ std::array<int, 0> src0 = {};
+ MutableSpan<int> span0 = src0;
+ span0.reverse();
+ EXPECT_EQ_ARRAY(span0.data(), Span<int>({}).data(), 0);
+
+ std::array<int, 1> src1 = {4};
+ MutableSpan<int> span1 = src1;
+ span1.reverse();
+ EXPECT_EQ_ARRAY(span1.data(), Span<int>({4}).data(), 1);
+
+ std::array<int, 2> src2 = {4, 5};
+ MutableSpan<int> span2 = src2;
+ span2.reverse();
+ EXPECT_EQ_ARRAY(span2.data(), Span<int>({5, 4}).data(), 2);
+
+ std::array<int, 5> src5 = {4, 5, 6, 7, 8};
+ MutableSpan<int> span5 = src5;
+ span5.reverse();
+ EXPECT_EQ_ARRAY(span5.data(), Span<int>({8, 7, 6, 5, 4}).data(), 5);
+}
+
TEST(span, MutableReverseIterator)
{
std::array<int, 4> src = {4, 5, 6, 7};