[Bf-blender-cvs] [fb26304aa29] functions: ArrayRef.contains_ptr
Jacques Lucke
noreply at git.blender.org
Mon Jul 22 18:13:05 CEST 2019
Commit: fb26304aa2996aaf03aed5b5885de3ff801e332f
Author: Jacques Lucke
Date: Mon Jul 22 14:18:43 2019 +0200
Branches: functions
https://developer.blender.org/rBfb26304aa2996aaf03aed5b5885de3ff801e332f
ArrayRef.contains_ptr
===================================================================
M source/blender/blenlib/BLI_array_ref.hpp
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 197fbc8b433..dbb2053b450 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -208,6 +208,15 @@ template<typename T> class ArrayRef {
return false;
}
+ /**
+ * Does a constant time check to see if the pointer is within the referenced array.
+ * Return true if it is, otherwise false.
+ */
+ bool contains_ptr(const T *ptr)
+ {
+ return (this->begin() <= ptr) && (ptr < this->end());
+ }
+
/**
* Does a linear search to count how often the value is in the array.
* Returns the number of occurences.
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index 6559ec67409..0dc47bc72cf 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -250,3 +250,16 @@ TEST(array_ref, Get)
EXPECT_EQ(a_ref.get(3, 42), 42);
EXPECT_EQ(a_ref.get(4, 42), 42);
}
+
+TEST(array_ref, ContainsPtr)
+{
+ std::array<int, 3> a = {5, 6, 7};
+ int other = 10;
+ IntArrayRef a_ref(a);
+ EXPECT_TRUE(a_ref.contains_ptr(&a[0] + 0));
+ EXPECT_TRUE(a_ref.contains_ptr(&a[0] + 1));
+ EXPECT_TRUE(a_ref.contains_ptr(&a[0] + 2));
+ EXPECT_FALSE(a_ref.contains_ptr(&a[0] + 3));
+ EXPECT_FALSE(a_ref.contains_ptr(&a[0] - 1));
+ EXPECT_FALSE(a_ref.contains_ptr(&other));
+}
More information about the Bf-blender-cvs
mailing list