[llvm] ec04e28 - Allow SmallPtrSet to be used with a std::insert_iterator

Aaron Ballman via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 5 13:13:08 PST 2021


Author: Aaron Ballman
Date: 2021-02-05T16:12:47-05:00
New Revision: ec04e2850adc044d84c840d7c48ebe3291ec47f7

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

LOG: Allow SmallPtrSet to be used with a std::insert_iterator

Currently, the SmallPtrSet type allows inserting elements but it does
not support inserting elements with a positional hint. The lack of this
signature means that you cannot use SmallPtrSet with
std::insert_iterator or std::inserter(), which makes some code
constructs more awkward. This adds an overload of insert() that can be
used in these scenarios.

The positional hint is unused by SmallPtrSet and the call is equivalent
to calling insert() without a hint.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SmallPtrSet.h
    llvm/unittests/ADT/SmallPtrSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 57dd8f6b695d..981b741669b0 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -366,6 +366,13 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
     return std::make_pair(makeIterator(p.first), p.second);
   }
 
+  /// Insert the given pointer with an iterator hint that is ignored. This is
+  /// identical to calling insert(Ptr), but allows SmallPtrSet to be used by
+  /// std::insert_iterator and std::inserter().
+  iterator insert(iterator, PtrType Ptr) {
+    return insert(Ptr).first;
+  }
+
   /// erase - If the set contains the specified pointer, remove it and return
   /// true, otherwise return false.
   bool erase(PtrType Ptr) {

diff  --git a/llvm/unittests/ADT/SmallPtrSetTest.cpp b/llvm/unittests/ADT/SmallPtrSetTest.cpp
index eacd62ffc6ff..6f3c94eed273 100644
--- a/llvm/unittests/ADT/SmallPtrSetTest.cpp
+++ b/llvm/unittests/ADT/SmallPtrSetTest.cpp
@@ -395,3 +395,16 @@ TEST(SmallPtrSetTest, Contains) {
   EXPECT_TRUE(Set.contains(&buf[1]));
   EXPECT_TRUE(Set.contains(&buf[2]));
 }
+
+TEST(SmallPtrSetTest, InsertIterator) {
+  SmallPtrSet<int *, 5> Set;
+  int Vals[5] = {11, 22, 33, 44, 55};
+  int *Buf[5] = {&Vals[0], &Vals[1], &Vals[2], &Vals[3], &Vals[4]};
+
+  for (int *Ptr : Buf)
+    Set.insert(Set.begin(), Ptr);
+
+  // Ensure that all of the values were copied into the set.
+  for (const auto *Ptr : Buf)
+    EXPECT_TRUE(Set.contains(Ptr));
+}


        


More information about the llvm-commits mailing list