[llvm] r290553 - [ADT] Add a boring std::partition wrapper similar to our std::remove_if

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 26 15:10:40 PST 2016


Author: chandlerc
Date: Mon Dec 26 17:10:40 2016
New Revision: 290553

URL: http://llvm.org/viewvc/llvm-project?rev=290553&view=rev
Log:
[ADT] Add a boring std::partition wrapper similar to our std::remove_if
wrapper.

Modified:
    llvm/trunk/include/llvm/ADT/STLExtras.h
    llvm/trunk/unittests/ADT/STLExtrasTest.cpp

Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=290553&r1=290552&r2=290553&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Mon Dec 26 17:10:40 2016
@@ -808,6 +808,13 @@ OutputIt transform(R &&Range, OutputIt d
   return std::transform(std::begin(Range), std::end(Range), d_first, P);
 }
 
+/// Provide wrappers to std::partition which take ranges instead of having to
+/// pass begin/end explicitly.
+template <typename R, typename UnaryPredicate>
+auto partition(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range)) {
+  return std::partition(std::begin(Range), std::end(Range), P);
+}
+
 //===----------------------------------------------------------------------===//
 //     Extra additions to <memory>
 //===----------------------------------------------------------------------===//

Modified: llvm/trunk/unittests/ADT/STLExtrasTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/STLExtrasTest.cpp?rev=290553&r1=290552&r2=290553&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/STLExtrasTest.cpp (original)
+++ llvm/trunk/unittests/ADT/STLExtrasTest.cpp Mon Dec 26 17:10:40 2016
@@ -276,4 +276,25 @@ TEST(STLExtrasTest, ConcatRange) {
     Test.push_back(i);
   EXPECT_EQ(Expected, Test);
 }
+
+TEST(STLExtrasTest, PartitionAdaptor) {
+  std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  auto I = partition(V, [](int i) { return i % 2 == 0; });
+  ASSERT_EQ(V.begin() + 4, I);
+
+  // Sort the two halves as partition may have messed with the order.
+  std::sort(V.begin(), I);
+  std::sort(I, V.end());
+
+  EXPECT_EQ(2, V[0]);
+  EXPECT_EQ(4, V[1]);
+  EXPECT_EQ(6, V[2]);
+  EXPECT_EQ(8, V[3]);
+  EXPECT_EQ(1, V[4]);
+  EXPECT_EQ(3, V[5]);
+  EXPECT_EQ(5, V[6]);
+  EXPECT_EQ(7, V[7]);
+}
+
 }




More information about the llvm-commits mailing list