[llvm] r287610 - [ADT] Add initializer list support to SmallPtrSet so that sets can be

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 21 19:27:43 PST 2016


Author: chandlerc
Date: Mon Nov 21 21:27:43 2016
New Revision: 287610

URL: http://llvm.org/viewvc/llvm-project?rev=287610&view=rev
Log:
[ADT] Add initializer list support to SmallPtrSet so that sets can be
easily initialized with some initial values.

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

Modified: llvm/trunk/include/llvm/ADT/SmallPtrSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallPtrSet.h?rev=287610&r1=287609&r2=287610&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallPtrSet.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallPtrSet.h Mon Nov 21 21:27:43 2016
@@ -22,6 +22,7 @@
 #include <cstddef>
 #include <cstring>
 #include <cstdlib>
+#include <initializer_list>
 #include <iterator>
 #include <utility>
 
@@ -336,6 +337,10 @@ public:
       insert(*I);
   }
 
+  void insert(std::initializer_list<PtrType> IL) {
+    insert(IL.begin(), IL.end());
+  }
+
   inline iterator begin() const {
     return iterator(CurArray, EndPointer());
   }
@@ -374,6 +379,11 @@ public:
     this->insert(I, E);
   }
 
+  SmallPtrSet(std::initializer_list<PtrType> IL)
+      : BaseT(SmallStorage, SmallSizePowTwo) {
+    this->insert(IL.begin(), IL.end());
+  }
+
   SmallPtrSet<PtrType, SmallSize> &
   operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
     if (&RHS != this)
@@ -388,6 +398,13 @@ public:
     return *this;
   }
 
+  SmallPtrSet<PtrType, SmallSize> &
+  operator=(std::initializer_list<PtrType> IL) {
+    this->clear();
+    this->insert(IL.begin(), IL.end());
+    return *this;
+  }
+
   /// swap - Swaps the elements of two sets.
   void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
     SmallPtrSetImplBase::swap(RHS);

Modified: llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp?rev=287610&r1=287609&r2=287610&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp Mon Nov 21 21:27:43 2016
@@ -21,10 +21,7 @@ TEST(SmallPtrSetTest, Assignment) {
   for (int i = 0; i < 8; ++i)
     buf[i] = 0;
 
-  SmallPtrSet<int *, 4> s1;
-  s1.insert(&buf[0]);
-  s1.insert(&buf[1]);
-
+  SmallPtrSet<int *, 4> s1 = {&buf[0], &buf[1]};
   SmallPtrSet<int *, 4> s2;
   (s2 = s1).insert(&buf[2]);
 
@@ -38,6 +35,15 @@ TEST(SmallPtrSetTest, Assignment) {
       EXPECT_TRUE(s1.count(&buf[i]));
     else
       EXPECT_FALSE(s1.count(&buf[i]));
+
+  // Assign and insert with initializer lists, and ones that contain both
+  // duplicates and out-of-order elements.
+  (s2 = {&buf[6], &buf[7], &buf[6]}).insert({&buf[5], &buf[4]});
+  for (int i = 0; i < 8; ++i)
+    if (i < 4)
+      EXPECT_FALSE(s2.count(&buf[i]));
+    else
+      EXPECT_TRUE(s2.count(&buf[i]));
 }
 
 TEST(SmallPtrSetTest, GrowthTest) {




More information about the llvm-commits mailing list