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_COPY_IF_H 10 #define _LIBCPP___ALGORITHM_RANGES_COPY_IF_H 11 12 #include <__algorithm/in_out_result.h> 13 #include <__config> 14 #include <__functional/identity.h> 15 #include <__functional/invoke.h> 16 #include <__iterator/concepts.h> 17 #include <__iterator/projected.h> 18 #include <__ranges/access.h> 19 #include <__ranges/concepts.h> 20 #include <__ranges/dangling.h> 21 #include <__utility/move.h> 22 23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 24 # pragma GCC system_header 25 #endif 26 27 _LIBCPP_PUSH_MACROS 28 #include <__undef_macros> 29 30 #if _LIBCPP_STD_VER >= 20 31 32 _LIBCPP_BEGIN_NAMESPACE_STD 33 34 namespace ranges { 35 36 template <class _Ip, class _Op> 37 using copy_if_result = in_out_result<_Ip, _Op>; 38 39 namespace __copy_if { 40 struct __fn { 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))) { 46 *__result = *__first; 47 ++__result; 48 } 49 } 50 return {std::move(__first), std::move(__result)}; 51 } 52 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); 62 } 63 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); 72 } 73 }; 74 } // namespace __copy_if 75 76 inline namespace __cpo { 77 inline constexpr auto copy_if = __copy_if::__fn{}; 78 } // namespace __cpo 79 } // namespace ranges 80 81 _LIBCPP_END_NAMESPACE_STD 82 83 #endif // _LIBCPP_STD_VER >= 20 84 85 _LIBCPP_POP_MACROS 86 87 #endif // _LIBCPP___ALGORITHM_RANGES_COPY_IF_H 88