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 _LIBCPP_PUSH_MACROS 30 #include <__undef_macros> 31 32 #if _LIBCPP_STD_VER >= 20 33 34 _LIBCPP_BEGIN_NAMESPACE_STD 35 36 namespace ranges { 37 38 template <class _InIter, class _OutIter1, class _OutIter2> 39 using partition_copy_result = in_out_out_result<_InIter, _OutIter1, _OutIter2>; 40 41 namespace __partition_copy { 42 43 struct __fn { 44 // TODO(ranges): delegate to the classic algorithm. 45 template <class _InIter, class _Sent, class _OutIter1, class _OutIter2, class _Proj, class _Pred> 46 _LIBCPP_HIDE_FROM_ABI constexpr static partition_copy_result<__remove_cvref_t<_InIter>, 47 __remove_cvref_t<_OutIter1>, 48 __remove_cvref_t<_OutIter2> > 49 __partition_copy_fn_impl( 50 _InIter&& __first, 51 _Sent&& __last, 52 _OutIter1&& __out_true, 53 _OutIter2&& __out_false, 54 _Pred& __pred, 55 _Proj& __proj) { 56 for (; __first != __last; ++__first) { 57 if (std::invoke(__pred, std::invoke(__proj, *__first))) { 58 *__out_true = *__first; 59 ++__out_true; 60 61 } else { 62 *__out_false = *__first; 63 ++__out_false; 64 } 65 } 66 67 return {std::move(__first), std::move(__out_true), std::move(__out_false)}; 68 } 69 70 template <input_iterator _InIter, 71 sentinel_for<_InIter> _Sent, 72 weakly_incrementable _OutIter1, 73 weakly_incrementable _OutIter2, 74 class _Proj = identity, 75 indirect_unary_predicate<projected<_InIter, _Proj>> _Pred> 76 requires indirectly_copyable<_InIter, _OutIter1> && indirectly_copyable<_InIter, _OutIter2> 77 _LIBCPP_HIDE_FROM_ABI constexpr partition_copy_result<_InIter, _OutIter1, _OutIter2> operator()( 78 _InIter __first, _Sent __last, _OutIter1 __out_true, _OutIter2 __out_false, _Pred __pred, _Proj __proj = {}) 79 const { 80 return __partition_copy_fn_impl( 81 std::move(__first), std::move(__last), std::move(__out_true), std::move(__out_false), __pred, __proj); 82 } 83 84 template <input_range _Range, 85 weakly_incrementable _OutIter1, 86 weakly_incrementable _OutIter2, 87 class _Proj = identity, 88 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred> 89 requires indirectly_copyable<iterator_t<_Range>, _OutIter1> && indirectly_copyable<iterator_t<_Range>, _OutIter2> 90 _LIBCPP_HIDE_FROM_ABI constexpr partition_copy_result<borrowed_iterator_t<_Range>, _OutIter1, _OutIter2> 91 operator()(_Range&& __range, _OutIter1 __out_true, _OutIter2 __out_false, _Pred __pred, _Proj __proj = {}) const { 92 return __partition_copy_fn_impl( 93 ranges::begin(__range), ranges::end(__range), std::move(__out_true), std::move(__out_false), __pred, __proj); 94 } 95 }; 96 97 } // namespace __partition_copy 98 99 inline namespace __cpo { 100 inline constexpr auto partition_copy = __partition_copy::__fn{}; 101 } // namespace __cpo 102 } // namespace ranges 103 104 _LIBCPP_END_NAMESPACE_STD 105 106 #endif // _LIBCPP_STD_VER >= 20 107 108 _LIBCPP_POP_MACROS 109 110 #endif // _LIBCPP___ALGORITHM_RANGES_PARTITION_COPY_H 111