[llvm] [llvm][ADT] Add wrappers to `std::fill` (PR #146681)

Longsheng Mou via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 2 09:08:55 PDT 2025


https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/146681

>From df0e75fc690efc177a26cd7dc05828344226e1de Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Wed, 2 Jul 2025 20:40:28 +0800
Subject: [PATCH 1/2] [llvm][ADT] Add wrappers to `std::fill`

This PR adds  `llvm::fill` that accepts a range instead of start/end iterator.
---
 llvm/include/llvm/ADT/STLExtras.h    |  6 ++++++
 llvm/unittests/ADT/STLExtrasTest.cpp | 12 ++++++++++++
 2 files changed, 18 insertions(+)

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..c50a406bd563e 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 IsSameAsVal = [&](int V) { return V == Val; };
+  fill(V1, Val);
+  EXPECT_TRUE(llvm::all_of(V1, IsSameAsVal));
+  V2.resize(5);
+  fill(V2, Val);
+  EXPECT_TRUE(llvm::all_of(V2, IsSameAsVal));
+}
+
 struct Foo;
 struct Bar {};
 

>From 300a85c1fcbbf688345bd20ed1aaef1e44288111 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 3 Jul 2025 00:07:06 +0800
Subject: [PATCH 2/2] Use `testing::ElementsAre` instead of `all_of`

---
 llvm/unittests/ADT/STLExtrasTest.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index c50a406bd563e..9fda0d912a2f5 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1595,12 +1595,11 @@ TEST(STLExtrasTest, Fill) {
   std::vector<int> V1 = {1, 2, 3};
   std::vector<int> V2;
   int Val = 4;
-  auto IsSameAsVal = [&](int V) { return V == Val; };
   fill(V1, Val);
-  EXPECT_TRUE(llvm::all_of(V1, IsSameAsVal));
-  V2.resize(5);
+  EXPECT_THAT(V1, ElementsAre(Val, Val, Val));
+  V2.resize(4);
   fill(V2, Val);
-  EXPECT_TRUE(llvm::all_of(V2, IsSameAsVal));
+  EXPECT_THAT(V2, ElementsAre(Val, Val, Val, Val));
 }
 
 struct Foo;



More information about the llvm-commits mailing list