Horizon
copy_if.hpp
Go to the documentation of this file.
1 // 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 
16 #include <functional>
17 #include <utility>
18 
19 #include <range/v3/range_fwd.hpp>
20 
21 #include <range/v3/algorithm/result_types.hpp>
30 #include <range/v3/utility/static_const.hpp>
31 
32 #include <range/v3/detail/prologue.hpp>
33 
34 namespace ranges
35 {
38  template<typename I, typename O>
39  using copy_if_result = detail::in_out_result<I, O>;
40 
41  RANGES_FUNC_BEGIN(copy_if)
42 
43 
44  template(typename I, typename S, typename O, typename F, typename P = identity)(
45  requires input_iterator<I> AND sentinel_for<S, I> AND
47  indirect_unary_predicate<F, projected<I, P>> AND
48  indirectly_copyable<I, O>)
49  constexpr copy_if_result<I, O> //
50  RANGES_FUNC(copy_if)(I first, S last, O out, F pred, P proj = P{}) //
51  {
52  for(; first != last; ++first)
53  {
54  auto && x = *first;
55  if(invoke(pred, invoke(proj, x)))
56  {
57  *out = (decltype(x) &&)x;
58  ++out;
59  }
60  }
61  return {first, out};
62  }
63 
65  template(typename Rng, typename O, typename F, typename P = identity)(
66  requires input_range<Rng> AND weakly_incrementable<O> AND
67  indirect_unary_predicate<F, projected<iterator_t<Rng>, P>> AND
69  constexpr copy_if_result<borrowed_iterator_t<Rng>, O> //
70  RANGES_FUNC(copy_if)(Rng && rng, O out, F pred, P proj = P{})
71  {
72  return (*this)(
73  begin(rng), end(rng), std::move(out), std::move(pred), std::move(proj));
74  }
75 
76  RANGES_FUNC_END(copy_if)
77 
78  namespace cpp20
79  {
80  using ranges::copy_if;
81  using ranges::copy_if_result;
82  } // namespace cpp20
84 } // namespace ranges
85 
86 #include <range/v3/detail/epilogue.hpp>
87 
88 #endif
template(typename Rng, typename O, typename F, typename P=identity)(requires input_range< Rng > AND weakly_incrementable< O > AND indirect_unary_predicate< F
This is an overloaded member function, provided for convenience. It differs from the above function o...
CPP_concept sentinel_for
\concept sentinel_for
Definition: concepts.hpp:306
CPP_concept indirect_unary_predicate
\concept indirect_unary_predicate
Definition: concepts.hpp:632
CPP_concept input_iterator
\concept input_iterator
Definition: concepts.hpp:362
CPP_concept indirectly_copyable
\concept indirectly_copyable
Definition: concepts.hpp:782
CPP_concept weakly_incrementable
\concept weakly_incrementable
Definition: concepts.hpp:268
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition: meta.hpp:541
front< Pair > first
Retrieve the first element of the pair Pair.
Definition: meta.hpp:2251
Definition: identity.hpp:25