| 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_COPY_IF_H |
| 10 | #define _LIBCPP___ALGORITHM_COPY_IF_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__functional/identity.h> |
| 14 | #include <__type_traits/invoke.h> |
| 15 | #include <__utility/move.h> |
| 16 | #include <__utility/pair.h> |
| 17 | |
| 18 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 19 | # pragma GCC system_header |
| 20 | #endif |
| 21 | |
| 22 | _LIBCPP_PUSH_MACROS |
| 23 | #include <__undef_macros> |
| 24 | |
| 25 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 26 | |
| 27 | template <class _InIter, class _Sent, class _OutIter, class _Proj, class _Pred> |
| 28 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> |
| 29 | __copy_if(_InIter __first, _Sent __last, _OutIter __result, _Pred& __pred, _Proj& __proj) { |
| 30 | for (; __first != __last; ++__first) { |
| 31 | if (std::__invoke(__pred, std::__invoke(__proj, *__first))) { |
| 32 | *__result = *__first; |
| 33 | ++__result; |
| 34 | } |
| 35 | } |
| 36 | return std::make_pair(std::move(__first), std::move(__result)); |
| 37 | } |
| 38 | |
| 39 | template <class _InputIterator, class _OutputIterator, class _Predicate> |
| 40 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator |
| 41 | copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred) { |
| 42 | __identity __proj; |
| 43 | return std::__copy_if(__first, __last, __result, __pred, __proj).second; |
| 44 | } |
| 45 | |
| 46 | _LIBCPP_END_NAMESPACE_STD |
| 47 | |
| 48 | _LIBCPP_POP_MACROS |
| 49 | |
| 50 | #endif // _LIBCPP___ALGORITHM_COPY_IF_H |
| 51 | |