[llvm] r263455 - [ADT] Add a pop_back_val method to the SparseSet container.

Quentin Colombet via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 14 11:10:41 PDT 2016


Author: qcolombet
Date: Mon Mar 14 13:10:41 2016
New Revision: 263455

URL: http://llvm.org/viewvc/llvm-project?rev=263455&view=rev
Log:
[ADT] Add a pop_back_val method to the SparseSet container.
The next commit will use it.

Modified:
    llvm/trunk/include/llvm/ADT/SparseSet.h
    llvm/trunk/unittests/ADT/SparseSetTest.cpp

Modified: llvm/trunk/include/llvm/ADT/SparseSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SparseSet.h?rev=263455&r1=263454&r2=263455&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SparseSet.h (original)
+++ llvm/trunk/include/llvm/ADT/SparseSet.h Mon Mar 14 13:10:41 2016
@@ -263,6 +263,11 @@ public:
     return *insert(ValueT(Key)).first;
   }
 
+  ValueT pop_back_val() {
+    // Sparse does not need to be cleared, see find().
+    return Dense.pop_back_val();
+  }
+
   /// erase - Erases an existing element identified by a valid iterator.
   ///
   /// This invalidates all iterators, but erase() returns an iterator pointing

Modified: llvm/trunk/unittests/ADT/SparseSetTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SparseSetTest.cpp?rev=263455&r1=263454&r2=263455&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SparseSetTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SparseSetTest.cpp Mon Mar 14 13:10:41 2016
@@ -183,4 +183,24 @@ TEST(SparseSetTest, AltStructSet) {
   EXPECT_FALSE(Set.erase(5));
   EXPECT_TRUE(Set.erase(6));
 }
+
+TEST(SparseSetTest, PopBack) {
+  USet Set;
+  const unsigned UpperBound = 300;
+  Set.setUniverse(UpperBound);
+  for (unsigned i = 0; i < UpperBound; ++i)
+    Set.insert(i);
+
+  // Make sure pop back returns the values in the reverse order we
+  // inserted them.
+  unsigned Expected = UpperBound;
+  while (!Set.empty())
+    ASSERT_TRUE(--Expected == Set.pop_back_val());
+
+  // Insert again the same elements in the sparse set and make sure
+  // each insertion actually inserts the elements. I.e., check
+  // that the underlying data structure are properly cleared.
+  for (unsigned i = 0; i < UpperBound; ++i)
+    ASSERT_TRUE(Set.insert(i).second);
+}
 } // namespace




More information about the llvm-commits mailing list