]> granicus.if.org Git - llvm/commitdiff
[ADT] Add a boring std::partition wrapper similar to our std::remove_if
authorChandler Carruth <chandlerc@gmail.com>
Mon, 26 Dec 2016 23:10:40 +0000 (23:10 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Mon, 26 Dec 2016 23:10:40 +0000 (23:10 +0000)
wrapper.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290553 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/STLExtras.h
unittests/ADT/STLExtrasTest.cpp

index 09483823f4b6af4fd10c8c8fed7afea10a3e6f96..43cfaa28770d24a849d2404a3e1b57d4b19b6885 100644 (file)
@@ -808,6 +808,13 @@ OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P) {
   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>
 //===----------------------------------------------------------------------===//
index d3bef6a2e0570284ff83f5725d794f1338691809..28e0ebb53f6c1196e03330a3ddbf1d04df49e9b9 100644 (file)
@@ -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]);
+}
+
 }