[PATCH] D96134: [ADT] Allow SmallPtrSet to be used with a std::insert_iterator

Aaron Ballman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 5 06:34:44 PST 2021


aaron.ballman created this revision.
aaron.ballman added reviewers: dblaikie, echristo, chandlerc, rsmith.
Herald added a subscriber: dexonsmith.
aaron.ballman requested review of this revision.
Herald added a project: LLVM.

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. For example, it would be nice to be able to write:

  llvm::copy_if(Buffer, std::inserter(SomeSmallPtrSet, SomeSmallPtrSet.begin()), [](...) { return whatever(); });

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96134

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


Index: llvm/unittests/ADT/SmallPtrSetTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallPtrSetTest.cpp
+++ llvm/unittests/ADT/SmallPtrSetTest.cpp
@@ -14,6 +14,8 @@
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/Support/PointerLikeTypeTraits.h"
 #include "gtest/gtest.h"
+#include <algorithm>
+#include <iterator>
 
 using namespace llvm;
 
@@ -395,3 +397,16 @@
   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]};
+
+  // Ensure that we can use SmallPtrSet with std::inserter().
+  std::copy(std::begin(Buf), std::end(Buf), std::inserter(Set, Set.begin()));
+
+  // Ensure that all of the values were copied into the set.
+  for (const auto *Ptr : Buf)
+    EXPECT_TRUE(Set.contains(Ptr));
+}
Index: llvm/include/llvm/ADT/SmallPtrSet.h
===================================================================
--- llvm/include/llvm/ADT/SmallPtrSet.h
+++ llvm/include/llvm/ADT/SmallPtrSet.h
@@ -366,6 +366,13 @@
     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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96134.321740.patch
Type: text/x-patch
Size: 1676 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210205/414682d8/attachment.bin>


More information about the llvm-commits mailing list