xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/ranges_copy_if.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
181ad6265SDimitry Andric //===----------------------------------------------------------------------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_COPY_IF_H
1081ad6265SDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_COPY_IF_H
1181ad6265SDimitry Andric 
1281ad6265SDimitry Andric #include <__algorithm/in_out_result.h>
1381ad6265SDimitry Andric #include <__config>
1481ad6265SDimitry Andric #include <__functional/identity.h>
1581ad6265SDimitry Andric #include <__functional/invoke.h>
1681ad6265SDimitry Andric #include <__iterator/concepts.h>
1781ad6265SDimitry Andric #include <__iterator/projected.h>
1881ad6265SDimitry Andric #include <__ranges/access.h>
1981ad6265SDimitry Andric #include <__ranges/concepts.h>
2081ad6265SDimitry Andric #include <__ranges/dangling.h>
2181ad6265SDimitry Andric #include <__utility/move.h>
2281ad6265SDimitry Andric 
2381ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2481ad6265SDimitry Andric #  pragma GCC system_header
2581ad6265SDimitry Andric #endif
2681ad6265SDimitry Andric 
27*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
28*b3edf446SDimitry Andric #include <__undef_macros>
29*b3edf446SDimitry Andric 
3006c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3181ad6265SDimitry Andric 
3281ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3381ad6265SDimitry Andric 
3481ad6265SDimitry Andric namespace ranges {
3581ad6265SDimitry Andric 
3681ad6265SDimitry Andric template <class _Ip, class _Op>
3781ad6265SDimitry Andric using copy_if_result = in_out_result<_Ip, _Op>;
3881ad6265SDimitry Andric 
3981ad6265SDimitry Andric namespace __copy_if {
4081ad6265SDimitry Andric struct __fn {
4181ad6265SDimitry Andric   template <class _InIter, class _Sent, class _OutIter, class _Proj, class _Pred>
4206c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr copy_if_result<_InIter, _OutIter>
__copy_if_impl__fn4381ad6265SDimitry Andric   __copy_if_impl(_InIter __first, _Sent __last, _OutIter __result, _Pred& __pred, _Proj& __proj) {
4481ad6265SDimitry Andric     for (; __first != __last; ++__first) {
4581ad6265SDimitry Andric       if (std::invoke(__pred, std::invoke(__proj, *__first))) {
4681ad6265SDimitry Andric         *__result = *__first;
4781ad6265SDimitry Andric         ++__result;
4881ad6265SDimitry Andric       }
4981ad6265SDimitry Andric     }
5081ad6265SDimitry Andric     return {std::move(__first), std::move(__result)};
5181ad6265SDimitry Andric   }
5281ad6265SDimitry Andric 
5306c3fb27SDimitry Andric   template <input_iterator _Iter,
5406c3fb27SDimitry Andric             sentinel_for<_Iter> _Sent,
5506c3fb27SDimitry Andric             weakly_incrementable _OutIter,
5606c3fb27SDimitry Andric             class _Proj = identity,
5781ad6265SDimitry Andric             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
5881ad6265SDimitry Andric     requires indirectly_copyable<_Iter, _OutIter>
5906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr copy_if_result<_Iter, _OutIter>
operator__fn6081ad6265SDimitry Andric   operator()(_Iter __first, _Sent __last, _OutIter __result, _Pred __pred, _Proj __proj = {}) const {
6181ad6265SDimitry Andric     return __copy_if_impl(std::move(__first), std::move(__last), std::move(__result), __pred, __proj);
6281ad6265SDimitry Andric   }
6381ad6265SDimitry Andric 
6406c3fb27SDimitry Andric   template <input_range _Range,
6506c3fb27SDimitry Andric             weakly_incrementable _OutIter,
6606c3fb27SDimitry Andric             class _Proj = identity,
6781ad6265SDimitry Andric             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
6881ad6265SDimitry Andric     requires indirectly_copyable<iterator_t<_Range>, _OutIter>
6906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr copy_if_result<borrowed_iterator_t<_Range>, _OutIter>
operator__fn7081ad6265SDimitry Andric   operator()(_Range&& __r, _OutIter __result, _Pred __pred, _Proj __proj = {}) const {
7181ad6265SDimitry Andric     return __copy_if_impl(ranges::begin(__r), ranges::end(__r), std::move(__result), __pred, __proj);
7281ad6265SDimitry Andric   }
7381ad6265SDimitry Andric };
7481ad6265SDimitry Andric } // namespace __copy_if
7581ad6265SDimitry Andric 
7681ad6265SDimitry Andric inline namespace __cpo {
7781ad6265SDimitry Andric inline constexpr auto copy_if = __copy_if::__fn{};
7881ad6265SDimitry Andric } // namespace __cpo
7981ad6265SDimitry Andric } // namespace ranges
8081ad6265SDimitry Andric 
8181ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
8281ad6265SDimitry Andric 
8306c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
8481ad6265SDimitry Andric 
85*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
86*b3edf446SDimitry Andric 
8781ad6265SDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_COPY_IF_H
88