remove_if.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ACTION_REMOVE_IF_HPP
  14. #define RANGES_V3_ACTION_REMOVE_IF_HPP
  15. #include <utility>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/action/action.hpp>
  18. #include <range/v3/action/erase.hpp>
  19. #include <range/v3/algorithm/remove_if.hpp>
  20. #include <range/v3/functional/bind_back.hpp>
  21. #include <range/v3/functional/identity.hpp>
  22. #include <range/v3/range/traits.hpp>
  23. #include <range/v3/utility/static_const.hpp>
  24. #include <range/v3/detail/prologue.hpp>
  25. namespace ranges
  26. {
  27. // TODO Look at all the special cases handled by erase_if in Library Fundamentals 2
  28. /// \addtogroup group-actions
  29. /// @{
  30. namespace actions
  31. {
  32. struct remove_if_fn
  33. {
  34. template(typename C, typename P = identity)(
  35. requires (!range<C>))
  36. constexpr auto operator()(C pred, P proj = P{}) const
  37. {
  38. return make_action_closure(
  39. bind_back(remove_if_fn{}, std::move(pred), std::move(proj)));
  40. }
  41. template(typename Rng, typename C, typename P = identity)(
  42. requires forward_range<Rng> AND
  43. erasable_range<Rng &, iterator_t<Rng>, iterator_t<Rng>> AND
  44. permutable<iterator_t<Rng>> AND
  45. indirect_unary_predicate<C, projected<iterator_t<Rng>, P>>)
  46. Rng operator()(Rng && rng, C pred, P proj = P{}) const
  47. {
  48. auto it = ranges::remove_if(rng, std::move(pred), std::move(proj));
  49. ranges::erase(rng, it, ranges::end(rng));
  50. return static_cast<Rng &&>(rng);
  51. }
  52. };
  53. /// \relates actions::remove_if_fn
  54. RANGES_INLINE_VARIABLE(remove_if_fn, remove_if)
  55. } // namespace actions
  56. /// @}
  57. } // namespace ranges
  58. #include <range/v3/detail/epilogue.hpp>
  59. #endif