Skip to content

[ADT] Add DenseSet::insert_range #131567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

kazutakahirata
Copy link
Contributor

This pach adds DenseSet::insert_range, named after
std::map::insert_range from C++23. The intent is to allow a heavy
operation as the argument like:

Set.insert_range(Map[Key]);

Without insert_range, we would have to do:

Set.insert(Map[Key].begin(), Map[Key].end());

or:

auto &M = Map[Key];
Set.insert(M.begin(), M.end());

Neither is elegant.

This pach adds DenseSet::insert_range, named after
std::map::insert_range from C++23.  The intent is to allow a heavy
operation as the argument like:

  Set.insert_range(Map[Key]);

Without insert_range, we would have to do:

  Set.insert(Map[Key].begin(), Map[Key].end());

or:

  auto &M = Map[Key];
  Set.insert(M.begin(), M.end());

Neither is elegant.
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

This pach adds DenseSet::insert_range, named after
std::map::insert_range from C++23. The intent is to allow a heavy
operation as the argument like:

Set.insert_range(Map[Key]);

Without insert_range, we would have to do:

Set.insert(Map[Key].begin(), Map[Key].end());

or:

auto &M = Map[Key];
Set.insert(M.begin(), M.end());

Neither is elegant.


Full diff: https://github.com/llvm/llvm-project/pull/131567.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/DenseSet.h (+5)
  • (modified) llvm/unittests/ADT/DenseSetTest.cpp (+11)
diff --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h
index a307bd8c9198b..ac766e778df0d 100644
--- a/llvm/include/llvm/ADT/DenseSet.h
+++ b/llvm/include/llvm/ADT/DenseSet.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_ADT_DENSESET_H
 #define LLVM_ADT_DENSESET_H
 
+#include "llvm/ADT/ADL.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/Support/MathExtras.h"
@@ -237,6 +238,10 @@ class DenseSetImpl {
     for (; I != E; ++I)
       insert(*I);
   }
+
+  template <typename Range> void insert_range(Range &&R) {
+    insert(adl_begin(R), adl_end(R));
+  }
 };
 
 /// Equality comparison for DenseSet.
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index f14542eafc251..14cef2ab678f9 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -32,6 +32,17 @@ TEST(DenseSetTest, DoubleEntrySetTest) {
   EXPECT_EQ(0u, set.count(2));
 }
 
+TEST(DenseSetTest, InsertRange) {
+  llvm::DenseSet<unsigned> set1;
+  llvm::DenseSet<unsigned> set2;
+  set1.insert(1);
+  set1.insert(2);
+  set1.insert(3);
+  constexpr unsigned Args[] = {3, 1, 2};
+  set2.insert_range(Args);
+  EXPECT_EQ(set1, set2);
+}
+
 struct TestDenseSetInfo {
   static inline unsigned getEmptyKey() { return ~0; }
   static inline unsigned getTombstoneKey() { return ~0U - 1; }

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.

Can't we use 'append_range'?

@kazutakahirata
Copy link
Contributor Author

Can't we use 'append_range'?

We cannot quite use append_range with std::map or DenseSet because it is implemented like so:

template <typename Container, typename Range>
void append_range(Container &C, Range &&R) {
  C.insert(C.end(), adl_begin(R), adl_end(R));
}

Note that neither std::map::insert nor DenseSet accepts C.end().

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.

If the right .insert is not available, then this makes sense to me.

@kazutakahirata kazutakahirata merged commit 4ce1d1f into llvm:main Mar 17, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_denseset_insert_range branch March 17, 2025 20:19
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.

3 participants