xref: /llvm-project/libcxx/include/__algorithm/ranges_partition_copy.h (revision 4f15267d3dd797a15901fe9352f0d5fa121b9095)
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 <__utility/move.h>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 #if _LIBCPP_STD_VER >= 20
29 
30 _LIBCPP_BEGIN_NAMESPACE_STD
31 
32 namespace ranges {
33 
34 template <class _InIter, class _OutIter1, class _OutIter2>
35 using partition_copy_result = in_out_out_result<_InIter, _OutIter1, _OutIter2>;
36 
37 namespace __partition_copy {
38 
39 struct __fn {
40 
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
44   static partition_copy_result<
45       __remove_cvref_t<_InIter>, __remove_cvref_t<_OutIter1>, __remove_cvref_t<_OutIter2>
46   > __partition_copy_fn_impl( _InIter&& __first, _Sent&& __last, _OutIter1&& __out_true, _OutIter2&& __out_false,
47       _Pred& __pred, _Proj& __proj) {
48     for (; __first != __last; ++__first) {
49       if (std::invoke(__pred, std::invoke(__proj, *__first))) {
50         *__out_true = *__first;
51         ++__out_true;
52 
53       } else {
54         *__out_false = *__first;
55         ++__out_false;
56       }
57     }
58 
59     return {std::move(__first), std::move(__out_true), std::move(__out_false)};
60   }
61 
62   template <input_iterator _InIter, sentinel_for<_InIter> _Sent,
63             weakly_incrementable _OutIter1, weakly_incrementable _OutIter2,
64             class _Proj = identity, indirect_unary_predicate<projected<_InIter, _Proj>> _Pred>
65   requires indirectly_copyable<_InIter, _OutIter1> && indirectly_copyable<_InIter, _OutIter2>
66   _LIBCPP_HIDE_FROM_ABI constexpr
67   partition_copy_result<_InIter, _OutIter1, _OutIter2>
68   operator()(_InIter __first, _Sent __last, _OutIter1 __out_true, _OutIter2 __out_false,
69              _Pred __pred, _Proj __proj = {}) const {
70     return __partition_copy_fn_impl(
71         std::move(__first), std::move(__last), std::move(__out_true), std::move(__out_false), __pred, __proj);
72   }
73 
74   template <input_range _Range, weakly_incrementable _OutIter1, weakly_incrementable _OutIter2,
75             class _Proj = identity, indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
76   requires indirectly_copyable<iterator_t<_Range>, _OutIter1> && indirectly_copyable<iterator_t<_Range>, _OutIter2>
77   _LIBCPP_HIDE_FROM_ABI constexpr
78   partition_copy_result<borrowed_iterator_t<_Range>, _OutIter1, _OutIter2>
79   operator()(_Range&& __range, _OutIter1 __out_true, _OutIter2 __out_false, _Pred __pred, _Proj __proj = {}) const {
80     return __partition_copy_fn_impl(
81         ranges::begin(__range), ranges::end(__range), std::move(__out_true), std::move(__out_false), __pred, __proj);
82   }
83 
84 };
85 
86 } // namespace __partition_copy
87 
88 inline namespace __cpo {
89   inline constexpr auto partition_copy = __partition_copy::__fn{};
90 } // namespace __cpo
91 } // namespace ranges
92 
93 _LIBCPP_END_NAMESPACE_STD
94 
95 #endif // _LIBCPP_STD_VER >= 20
96 
97 #endif // _LIBCPP___ALGORITHM_RANGES_PARTITION_COPY_H
98