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