[PATCH] D28120: [ADT] Add an llvm::erase_if utility to make the standard erase+remove_if pattern easier to write.

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 2 12:22:40 PST 2017


On Mon, Dec 26, 2016 at 3:28 PM Chandler Carruth via Phabricator via
llvm-commits <llvm-commits at lists.llvm.org> wrote:

> chandlerc created this revision.
> chandlerc added a reviewer: aaron.ballman.
> chandlerc added a subscriber: llvm-commits.
> Herald added a subscriber: mcrosier.
>
> What it says on the tin...
>
>
> https://reviews.llvm.org/D28120
>
> Files:
>   include/llvm/ADT/STLExtras.h
>   unittests/ADT/STLExtrasTest.cpp
>
>
> Index: unittests/ADT/STLExtrasTest.cpp
> ===================================================================
> --- unittests/ADT/STLExtrasTest.cpp
> +++ unittests/ADT/STLExtrasTest.cpp
> @@ -297,4 +297,15 @@
>    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]);
>

I'd suggest simplifying the test case a bit (making it smaller, at least -
doesn't seem like all 4 output values are interesting - probably only a
vector of length 3-4 would suffice, rather than 8. Less repetitious on the
EXPECTs too)

Up to you, though.


> +}
> +
>  }
> Index: include/llvm/ADT/STLExtras.h
> ===================================================================
> --- include/llvm/ADT/STLExtras.h
> +++ include/llvm/ADT/STLExtras.h
> @@ -815,6 +815,18 @@
>    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>
>
>  //===----------------------------------------------------------------------===//
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170102/14ffe85a/attachment.html>


More information about the llvm-commits mailing list