You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

copy_if.hpp 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_COPY_IF_HPP
  14. #define RANGES_V3_ALGORITHM_COPY_IF_HPP
  15. #include <functional>
  16. #include <utility>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/algorithm/result_types.hpp>
  19. #include <range/v3/functional/identity.hpp>
  20. #include <range/v3/functional/invoke.hpp>
  21. #include <range/v3/iterator/concepts.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. namespace ranges
  29. {
  30. /// \addtogroup group-algorithms
  31. /// @{
  32. template<typename I, typename O>
  33. using copy_if_result = detail::in_out_result<I, O>;
  34. RANGES_BEGIN_NIEBLOID(copy_if)
  35. /// \brief function template \c copy_if
  36. template<typename I, typename S, typename O, typename F, typename P = identity>
  37. auto RANGES_FUN_NIEBLOID(copy_if)(I first, S last, O out, F pred, P proj = P{}) //
  38. ->CPP_ret(copy_if_result<I, O>)( //
  39. requires input_iterator<I> && sentinel_for<S, I> &&
  40. weakly_incrementable<O> && indirect_unary_predicate<F, projected<I, P>> &&
  41. indirectly_copyable<I, O>)
  42. {
  43. for(; first != last; ++first)
  44. {
  45. auto && x = *first;
  46. if(invoke(pred, invoke(proj, x)))
  47. {
  48. *out = (decltype(x) &&)x;
  49. ++out;
  50. }
  51. }
  52. return {first, out};
  53. }
  54. /// \overload
  55. template<typename Rng, typename O, typename F, typename P = identity>
  56. auto RANGES_FUN_NIEBLOID(copy_if)(Rng && rng, O out, F pred, P proj = P{})
  57. ->CPP_ret(copy_if_result<safe_iterator_t<Rng>, O>)( //
  58. requires input_range<Rng> && weakly_incrementable<O> &&
  59. indirect_unary_predicate<F, projected<iterator_t<Rng>, P>> &&
  60. indirectly_copyable<iterator_t<Rng>, O>)
  61. {
  62. return (*this)(
  63. begin(rng), end(rng), std::move(out), std::move(pred), std::move(proj));
  64. }
  65. RANGES_END_NIEBLOID(copy_if)
  66. namespace cpp20
  67. {
  68. using ranges::copy_if;
  69. using ranges::copy_if_result;
  70. } // namespace cpp20
  71. /// @}
  72. } // namespace ranges
  73. #endif // include guard