[ADT] Add an llvm::erase_if utility to make the standard erase+remove_if

pattern easier to write.

Differential Revision: https://reviews.llvm.org/D28120

llvm-svn: 290555
This commit is contained in:
Chandler Carruth 2016-12-26 23:30:44 +00:00
parent c9cf7fc7a4
commit cc44ab63b6
2 changed files with 23 additions and 0 deletions

View File

@ -815,6 +815,18 @@ auto partition(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range)) {
return std::partition(std::begin(Range), std::end(Range), P);
}
/// Provide a container algorithm similar to C++ Library Fundamentals v2's
/// `erase_if` which is equivalent to:
///
/// C.erase(remove_if(C, pred), C.end());
///
/// This version works for any container with an erase method call accepting
/// two iterators.
template <typename Container, typename UnaryPredicate>
void erase_if(Container &C, UnaryPredicate P) {
C.erase(remove_if(C, P), C.end());
}
//===----------------------------------------------------------------------===//
// Extra additions to <memory>
//===----------------------------------------------------------------------===//

View File

@ -297,4 +297,15 @@ TEST(STLExtrasTest, PartitionAdaptor) {
EXPECT_EQ(7, V[7]);
}
TEST(STLExtrasTest, EraseIf) {
std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
erase_if(V, [](int i) { return i % 2 == 0; });
EXPECT_EQ(4u, V.size());
EXPECT_EQ(1, V[0]);
EXPECT_EQ(3, V[1]);
EXPECT_EQ(5, V[2]);
EXPECT_EQ(7, V[3]);
}
}