1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_RANGES_COPY_IF_H
10 #define _LIBCPP___ALGORITHM_RANGES_COPY_IF_H
12 #include <__cxx03/__algorithm/in_out_result.h>
13 #include <__cxx03/__config>
14 #include <__cxx03/__functional/identity.h>
15 #include <__cxx03/__functional/invoke.h>
16 #include <__cxx03/__iterator/concepts.h>
17 #include <__cxx03/__iterator/projected.h>
18 #include <__cxx03/__ranges/access.h>
19 #include <__cxx03/__ranges/concepts.h>
20 #include <__cxx03/__ranges/dangling.h>
21 #include <__cxx03/__utility/move.h>
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 # pragma GCC system_header
28 #include <__cxx03/__undef_macros>
30 #if _LIBCPP_STD_VER >= 20
32 _LIBCPP_BEGIN_NAMESPACE_STD
36 template <class _Ip
, class _Op
>
37 using copy_if_result
= in_out_result
<_Ip
, _Op
>;
41 template <class _InIter
, class _Sent
, class _OutIter
, class _Proj
, class _Pred
>
42 _LIBCPP_HIDE_FROM_ABI
static constexpr copy_if_result
<_InIter
, _OutIter
>
43 __copy_if_impl(_InIter __first
, _Sent __last
, _OutIter __result
, _Pred
& __pred
, _Proj
& __proj
) {
44 for (; __first
!= __last
; ++__first
) {
45 if (std::invoke(__pred
, std::invoke(__proj
, *__first
))) {
50 return {std::move(__first
), std::move(__result
)};
53 template <input_iterator _Iter
,
54 sentinel_for
<_Iter
> _Sent
,
55 weakly_incrementable _OutIter
,
56 class _Proj
= identity
,
57 indirect_unary_predicate
<projected
<_Iter
, _Proj
>> _Pred
>
58 requires indirectly_copyable
<_Iter
, _OutIter
>
59 _LIBCPP_HIDE_FROM_ABI
constexpr copy_if_result
<_Iter
, _OutIter
>
60 operator()(_Iter __first
, _Sent __last
, _OutIter __result
, _Pred __pred
, _Proj __proj
= {}) const {
61 return __copy_if_impl(std::move(__first
), std::move(__last
), std::move(__result
), __pred
, __proj
);
64 template <input_range _Range
,
65 weakly_incrementable _OutIter
,
66 class _Proj
= identity
,
67 indirect_unary_predicate
<projected
<iterator_t
<_Range
>, _Proj
>> _Pred
>
68 requires indirectly_copyable
<iterator_t
<_Range
>, _OutIter
>
69 _LIBCPP_HIDE_FROM_ABI
constexpr copy_if_result
<borrowed_iterator_t
<_Range
>, _OutIter
>
70 operator()(_Range
&& __r
, _OutIter __result
, _Pred __pred
, _Proj __proj
= {}) const {
71 return __copy_if_impl(ranges::begin(__r
), ranges::end(__r
), std::move(__result
), __pred
, __proj
);
74 } // namespace __copy_if
76 inline namespace __cpo
{
77 inline constexpr auto copy_if
= __copy_if::__fn
{};
81 _LIBCPP_END_NAMESPACE_STD
83 #endif // _LIBCPP_STD_VER >= 20
87 #endif // _LIBCPP___ALGORITHM_RANGES_COPY_IF_H