[llvm] 4379d22 - [ADT] Add DenseSet(llvm::from_t, Range) (#131832)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 18:17:48 PDT 2025


Author: Kazu Hirata
Date: 2025-03-22T18:17:45-07:00
New Revision: 4379d22013185221e09ec0c2cbbaf006323f64b5

URL: https://github.com/llvm/llvm-project/commit/4379d22013185221e09ec0c2cbbaf006323f64b5
DIFF: https://github.com/llvm/llvm-project/commit/4379d22013185221e09ec0c2cbbaf006323f64b5.diff

LOG: [ADT] Add DenseSet(llvm::from_t, Range) (#131832)

This patch adds a constructor of the form:

  DenseSet Set(llvm::from_range, Range)

while forward-porting std::from_range from c++23.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/DenseSet.h
    llvm/include/llvm/ADT/STLForwardCompat.h
    llvm/unittests/ADT/DenseSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h
index ac766e778df0d..5642f1400bc39 100644
--- a/llvm/include/llvm/ADT/DenseSet.h
+++ b/llvm/include/llvm/ADT/DenseSet.h
@@ -17,6 +17,7 @@
 #include "llvm/ADT/ADL.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/type_traits.h"
 #include <cstddef>
@@ -78,6 +79,10 @@ class DenseSetImpl {
     insert(Elems.begin(), Elems.end());
   }
 
+  template <typename Range>
+  DenseSetImpl(llvm::from_range_t, Range &&R)
+      : DenseSetImpl(adl_begin(R), adl_end(R)) {}
+
   bool empty() const { return TheMap.empty(); }
   size_type size() const { return TheMap.size(); }
   size_t getMemorySize() const { return TheMap.getMemorySize(); }

diff  --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h
index 6afe3610b257f..10c274ea8e23e 100644
--- a/llvm/include/llvm/ADT/STLForwardCompat.h
+++ b/llvm/include/llvm/ADT/STLForwardCompat.h
@@ -67,6 +67,12 @@ template <typename Enum>
   return static_cast<std::underlying_type_t<Enum>>(E);
 }
 
+// A tag for constructors accepting ranges.
+struct from_range_t {
+  explicit from_range_t() = default;
+};
+inline constexpr from_range_t from_range{};
+
 } // namespace llvm
 
 #endif // LLVM_ADT_STLFORWARDCOMPAT_H

diff  --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index 8fc2bab44b124..5a8ee592ddfc7 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -33,6 +33,24 @@ TEST(DenseSetTest, DoubleEntrySetTest) {
   EXPECT_EQ(0u, set.count(2));
 }
 
+TEST(DenseSetTest, CtorRange) {
+  constexpr unsigned Args[] = {3, 1, 2};
+  llvm::DenseSet<unsigned> set(llvm::from_range, Args);
+  EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
+}
+
+TEST(DenseSetTest, CtorRangeImplicitConversion) {
+  constexpr char Args[] = {3, 1, 2};
+  llvm::DenseSet<unsigned> set(llvm::from_range, Args);
+  EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
+}
+
+TEST(SmallDenseSetTest, CtorRange) {
+  constexpr unsigned Args[] = {9, 7, 8};
+  llvm::SmallDenseSet<unsigned> set(llvm::from_range, Args);
+  EXPECT_THAT(set, ::testing::UnorderedElementsAre(7, 8, 9));
+}
+
 TEST(DenseSetTest, InsertRange) {
   llvm::DenseSet<unsigned> set;
   constexpr unsigned Args[] = {3, 1, 2};


        


More information about the llvm-commits mailing list