Skip to content

Conversation

@CoTinker
Copy link
Contributor

@CoTinker CoTinker commented Jul 2, 2025

This PR adds llvm::fill that accepts a range instead of begin/end iterator.

@llvmbot
Copy link
Member

llvmbot commented Jul 2, 2025

@llvm/pr-subscribers-llvm-adt

Author: Longsheng Mou (CoTinker)

Changes

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 --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 {};
 

This PR adds  `llvm::fill` that accepts a range instead of start/end iterator.
Copy link
Contributor

@jurahul jurahul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you also plan to add uninitialized_fill?

@CoTinker
Copy link
Contributor Author

CoTinker commented Jul 2, 2025

Do you also plan to add uninitialized_fill?

Sorry, currently, I have no plans to add uninitialized_fill, as it is seldom used.

@CoTinker CoTinker merged commit 6553753 into llvm:main Jul 3, 2025
9 checks passed
@CoTinker CoTinker deleted the fill branch July 3, 2025 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants