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 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 28 29 _LIBCPP_BEGIN_NAMESPACE_STD 30 31 namespace ranges { 32 33 template<class _Ip, class _Op> 34 using copy_if_result = in_out_result<_Ip, _Op>; 35 36 namespace __copy_if { 37 struct __fn { 38 39 template <class _InIter, class _Sent, class _OutIter, class _Proj, class _Pred> 40 _LIBCPP_HIDE_FROM_ABI static constexpr 41 copy_if_result <_InIter, _OutIter> 42 __copy_if_impl(_InIter __first, _Sent __last, _OutIter __result, _Pred& __pred, _Proj& __proj) { 43 for (; __first != __last; ++__first) { 44 if (std::invoke(__pred, std::invoke(__proj, *__first))) { 45 *__result = *__first; 46 ++__result; 47 } 48 } 49 return {std::move(__first), std::move(__result)}; 50 } 51 52 template <input_iterator _Iter, sentinel_for<_Iter> _Sent, weakly_incrementable _OutIter, class _Proj = identity, 53 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred> 54 requires indirectly_copyable<_Iter, _OutIter> 55 _LIBCPP_HIDE_FROM_ABI constexpr 56 copy_if_result<_Iter, _OutIter> 57 operator()(_Iter __first, _Sent __last, _OutIter __result, _Pred __pred, _Proj __proj = {}) const { 58 return __copy_if_impl(std::move(__first), std::move(__last), std::move(__result), __pred, __proj); 59 } 60 61 template <input_range _Range, weakly_incrementable _OutIter, class _Proj = identity, 62 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred> 63 requires indirectly_copyable<iterator_t<_Range>, _OutIter> 64 _LIBCPP_HIDE_FROM_ABI constexpr 65 copy_if_result<borrowed_iterator_t<_Range>, _OutIter> 66 operator()(_Range&& __r, _OutIter __result, _Pred __pred, _Proj __proj = {}) const { 67 return __copy_if_impl(ranges::begin(__r), ranges::end(__r), std::move(__result), __pred, __proj); 68 } 69 }; 70 } // namespace __copy_if 71 72 inline namespace __cpo { 73 inline constexpr auto copy_if = __copy_if::__fn{}; 74 } // namespace __cpo 75 } // namespace ranges 76 77 _LIBCPP_END_NAMESPACE_STD 78 79 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 80 81 #endif // _LIBCPP___ALGORITHM_RANGES_COPY_IF_H 82