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:
authorJacques Lucke <jacques@blender.org>2020-07-20 14:02:10 +0300
committerJacques Lucke <jacques@blender.org>2020-07-20 14:02:10 +0300
commit579b1800534fa08950278169c45db4b3be09b42c (patch)
tree4bcce8c9a3c43ca2b7bc5826862470df36ad1651
parent8cbbdedaf4dfec9e320e7e2be58b75d256950df1 (diff)
BLI: add Vector/Array.fill methods
-rw-r--r--source/blender/blenlib/BLI_array.hh8
-rw-r--r--source/blender/blenlib/BLI_vector.hh8
-rw-r--r--tests/gtests/blenlib/BLI_array_test.cc12
-rw-r--r--tests/gtests/blenlib/BLI_vector_test.cc12
4 files changed, 40 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_array.hh b/source/blender/blenlib/BLI_array.hh
index c30893f1337..871038d7d85 100644
--- a/source/blender/blenlib/BLI_array.hh
+++ b/source/blender/blenlib/BLI_array.hh
@@ -269,6 +269,14 @@ class Array {
}
/**
+ * Copies the given value to every element in the array.
+ */
+ void fill(const T &value) const
+ {
+ initialized_fill_n(data_, size_, value);
+ }
+
+ /**
* Get a pointer to the beginning of the array.
*/
const T *data() const
diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh
index 66de8d2fbd1..11bb00c3f8d 100644
--- a/source/blender/blenlib/BLI_vector.hh
+++ b/source/blender/blenlib/BLI_vector.hh
@@ -703,6 +703,14 @@ class Vector {
}
/**
+ * Copies the given value to every element in the vector.
+ */
+ void fill(const T &value) const
+ {
+ initialized_fill_n(begin_, this->size(), value);
+ }
+
+ /**
* Get access to the underlying array.
*/
T *data()
diff --git a/tests/gtests/blenlib/BLI_array_test.cc b/tests/gtests/blenlib/BLI_array_test.cc
index 4e03321454c..c01ba26ffd7 100644
--- a/tests/gtests/blenlib/BLI_array_test.cc
+++ b/tests/gtests/blenlib/BLI_array_test.cc
@@ -161,4 +161,16 @@ TEST(array, NoInitializationSizeConstructor)
}
}
+TEST(array, Fill)
+{
+ Array<int> array(5);
+ array.fill(3);
+ EXPECT_EQ(array.size(), 5u);
+ EXPECT_EQ(array[0], 3);
+ EXPECT_EQ(array[1], 3);
+ EXPECT_EQ(array[2], 3);
+ EXPECT_EQ(array[3], 3);
+ EXPECT_EQ(array[4], 3);
+}
+
} // namespace blender
diff --git a/tests/gtests/blenlib/BLI_vector_test.cc b/tests/gtests/blenlib/BLI_vector_test.cc
index b203970ff8e..f581626d1ad 100644
--- a/tests/gtests/blenlib/BLI_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_vector_test.cc
@@ -624,4 +624,16 @@ TEST(vector, ConstructVoidPointerVector)
EXPECT_EQ(vec.size(), 3);
}
+TEST(vector, Fill)
+{
+ Vector<int> vec(5);
+ vec.fill(3);
+ EXPECT_EQ(vec.size(), 5u);
+ EXPECT_EQ(vec[0], 3);
+ EXPECT_EQ(vec[1], 3);
+ EXPECT_EQ(vec[2], 3);
+ EXPECT_EQ(vec[3], 3);
+ EXPECT_EQ(vec[4], 3);
+}
+
} // namespace blender