remove.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-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_ALGORITHM_REMOVE_HPP
  14. #define RANGES_V3_ALGORITHM_REMOVE_HPP
  15. #include <meta/meta.hpp>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/algorithm/find.hpp>
  18. #include <range/v3/functional/identity.hpp>
  19. #include <range/v3/functional/invoke.hpp>
  20. #include <range/v3/iterator/concepts.hpp>
  21. #include <range/v3/iterator/operations.hpp>
  22. #include <range/v3/iterator/traits.hpp>
  23. #include <range/v3/range/access.hpp>
  24. #include <range/v3/range/concepts.hpp>
  25. #include <range/v3/range/dangling.hpp>
  26. #include <range/v3/range/traits.hpp>
  27. #include <range/v3/utility/static_const.hpp>
  28. #include <range/v3/detail/prologue.hpp>
  29. namespace ranges
  30. {
  31. /// \addtogroup group-algorithms
  32. /// @{
  33. RANGES_FUNC_BEGIN(remove)
  34. /// \brief function template \c remove
  35. template(typename I, typename S, typename T, typename P = identity)(
  36. requires permutable<I> AND sentinel_for<S, I> AND
  37. indirect_relation<equal_to, projected<I, P>, T const *>)
  38. constexpr I RANGES_FUNC(remove)(I first, S last, T const & val, P proj = P{})
  39. {
  40. first = find(std::move(first), last, val, ranges::ref(proj));
  41. if(first != last)
  42. {
  43. for(I i = next(first); i != last; ++i)
  44. {
  45. if(!(invoke(proj, *i) == val))
  46. {
  47. *first = iter_move(i);
  48. ++first;
  49. }
  50. }
  51. }
  52. return first;
  53. }
  54. /// \overload
  55. template(typename Rng, typename T, typename P = identity)(
  56. requires forward_range<Rng> AND permutable<iterator_t<Rng>> AND
  57. indirect_relation<equal_to, projected<iterator_t<Rng>, P>, T const *>)
  58. constexpr borrowed_iterator_t<Rng> //
  59. RANGES_FUNC(remove)(Rng && rng, T const & val, P proj = P{})
  60. {
  61. return (*this)(begin(rng), end(rng), val, std::move(proj));
  62. }
  63. RANGES_FUNC_END(remove)
  64. namespace cpp20
  65. {
  66. using ranges::remove;
  67. }
  68. /// @}
  69. } // namespace ranges
  70. #include <range/v3/detail/epilogue.hpp>
  71. #endif