[llvm] [llvm][ADT] Add wrappers to `std::fill` (PR #146681)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 2 05:45:48 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Longsheng Mou (CoTinker)
<details>
<summary>Changes</summary>
This PR adds `llvm::fill` that accepts a range instead of start/end iterator.
---
Full diff: https://github.com/llvm/llvm-project/pull/146681.diff
2 Files Affected:
- (modified) llvm/include/llvm/ADT/STLExtras.h (+6)
- (modified) llvm/unittests/ADT/STLExtrasTest.cpp (+12)
``````````diff
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index eea06cfb99ba2..b23188cbdadeb 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1759,6 +1759,12 @@ bool none_of(R &&Range, UnaryPredicate P) {
return std::none_of(adl_begin(Range), adl_end(Range), P);
}
+/// Provide wrappers to std::fill which take ranges instead of having to pass
+/// begin/end explicitly.
+template <typename R, typename T> void fill(R &&Range, T &&Value) {
+ std::fill(adl_begin(Range), adl_end(Range), std::forward<T>(Value));
+}
+
/// Provide wrappers to std::find which take ranges instead of having to pass
/// begin/end explicitly.
template <typename R, typename T> auto find(R &&Range, const T &Val) {
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 286cfa745fd14..14f5da1ff9549 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1591,6 +1591,18 @@ TEST(STLExtrasTest, Includes) {
}
}
+TEST(STLExtrasTest, Fill) {
+ std::vector<int> V1 = {1, 2, 3};
+ std::vector<int> V2;
+ int Val = 4;
+ auto SameToVal = [&](int V) { return V == Val; };
+ fill(V1, Val);
+ EXPECT_TRUE(llvm::all_of(V1, SameToVal));
+ V2.resize(5);
+ fill(V2, Val);
+ EXPECT_TRUE(llvm::all_of(V2, SameToVal));
+}
+
struct Foo;
struct Bar {};
``````````
</details>
https://github.com/llvm/llvm-project/pull/146681
More information about the llvm-commits
mailing list