1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_RANGES_PARTITION_COPY_H
10 #define _LIBCPP___ALGORITHM_RANGES_PARTITION_COPY_H
11 
12 #include <__algorithm/in_out_out_result.h>
13 #include <__config>
14 #include <__functional/identity.h>
15 #include <__functional/invoke.h>
16 #include <__iterator/concepts.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__iterator/projected.h>
19 #include <__ranges/access.h>
20 #include <__ranges/concepts.h>
21 #include <__ranges/dangling.h>
22 #include <__type_traits/remove_cvref.h>
23 #include <__utility/move.h>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 #if _LIBCPP_STD_VER >= 20
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 namespace ranges {
34 
35 template <class _InIter, class _OutIter1, class _OutIter2>
36 using partition_copy_result = in_out_out_result<_InIter, _OutIter1, _OutIter2>;
37 
38 namespace __partition_copy {
39 
40 struct __fn {
41   // TODO(ranges): delegate to the classic algorithm.
42   template <class _InIter, class _Sent, class _OutIter1, class _OutIter2, class _Proj, class _Pred>
43   _LIBCPP_HIDE_FROM_ABI constexpr static partition_copy_result<__remove_cvref_t<_InIter>,
44                                                                __remove_cvref_t<_OutIter1>,
45                                                                __remove_cvref_t<_OutIter2> >
46   __partition_copy_fn_impl(
47       _InIter&& __first,
48       _Sent&& __last,
49       _OutIter1&& __out_true,
50       _OutIter2&& __out_false,
51       _Pred& __pred,
52       _Proj& __proj) {
53     for (; __first != __last; ++__first) {
54       if (std::invoke(__pred, std::invoke(__proj, *__first))) {
55         *__out_true = *__first;
56         ++__out_true;
57 
58       } else {
59         *__out_false = *__first;
60         ++__out_false;
61       }
62     }
63 
64     return {std::move(__first), std::move(__out_true), std::move(__out_false)};
65   }
66 
67   template <input_iterator _InIter,
68             sentinel_for<_InIter> _Sent,
69             weakly_incrementable _OutIter1,
70             weakly_incrementable _OutIter2,
71             class _Proj = identity,
72             indirect_unary_predicate<projected<_InIter, _Proj>> _Pred>
73     requires indirectly_copyable<_InIter, _OutIter1> && indirectly_copyable<_InIter, _OutIter2>
74   _LIBCPP_HIDE_FROM_ABI constexpr partition_copy_result<_InIter, _OutIter1, _OutIter2> operator()(
75       _InIter __first, _Sent __last, _OutIter1 __out_true, _OutIter2 __out_false, _Pred __pred, _Proj __proj = {})
76       const {
77     return __partition_copy_fn_impl(
78         std::move(__first), std::move(__last), std::move(__out_true), std::move(__out_false), __pred, __proj);
79   }
80 
81   template <input_range _Range,
82             weakly_incrementable _OutIter1,
83             weakly_incrementable _OutIter2,
84             class _Proj = identity,
85             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
86     requires indirectly_copyable<iterator_t<_Range>, _OutIter1> && indirectly_copyable<iterator_t<_Range>, _OutIter2>
87   _LIBCPP_HIDE_FROM_ABI constexpr partition_copy_result<borrowed_iterator_t<_Range>, _OutIter1, _OutIter2>
88   operator()(_Range&& __range, _OutIter1 __out_true, _OutIter2 __out_false, _Pred __pred, _Proj __proj = {}) const {
89     return __partition_copy_fn_impl(
90         ranges::begin(__range), ranges::end(__range), std::move(__out_true), std::move(__out_false), __pred, __proj);
91   }
92 };
93 
94 } // namespace __partition_copy
95 
96 inline namespace __cpo {
97 inline constexpr auto partition_copy = __partition_copy::__fn{};
98 } // namespace __cpo
99 } // namespace ranges
100 
101 _LIBCPP_END_NAMESPACE_STD
102 
103 #endif // _LIBCPP_STD_VER >= 20
104 
105 #endif // _LIBCPP___ALGORITHM_RANGES_PARTITION_COPY_H
106