| 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_REPLACE_COPY_IF_H |
| 10 |
#define _LIBCPP___ALGORITHM_RANGES_REPLACE_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 _InIter, class _OutIter> |
| 37 |
using replace_copy_if_result = in_out_result<_InIter, _OutIter>; |
| 38 |
|
| 39 |
template <class _InIter, class _Sent, class _OutIter, class _Pred, class _Type, class _Proj> |
| 40 |
_LIBCPP_HIDE_FROM_ABI constexpr replace_copy_if_result<_InIter, _OutIter> __replace_copy_if_impl( |
| 41 |
_InIter __first, _Sent __last, _OutIter __result, _Pred& __pred, const _Type& __new_value, _Proj& __proj) { |
| 42 |
while (__first != __last) { |
| 43 |
if (std::invoke(__pred, std::invoke(__proj, *__first))) |
| 44 |
*__result = __new_value; |
| 45 |
else |
| 46 |
*__result = *__first; |
| 47 |
|
| 48 |
++__first; |
| 49 |
++__result; |
| 50 |
} |
| 51 |
|
| 52 |
return {std::move(__first), std::move(__result)}; |
| 53 |
} |
| 54 |
|
| 55 |
namespace __replace_copy_if { |
| 56 |
|
| 57 |
struct __fn { |
| 58 |
template <input_iterator _InIter, |
| 59 |
sentinel_for<_InIter> _Sent, |
| 60 |
class _Type, |
| 61 |
output_iterator<const _Type&> _OutIter, |
| 62 |
class _Proj = identity, |
| 63 |
indirect_unary_predicate<projected<_InIter, _Proj>> _Pred> |
| 64 |
requires indirectly_copyable<_InIter, _OutIter> |
| 65 |
_LIBCPP_HIDE_FROM_ABI constexpr replace_copy_if_result<_InIter, _OutIter> operator()( |
| 66 |
_InIter __first, _Sent __last, _OutIter __result, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) |
| 67 |
const { |
| 68 |
return ranges::__replace_copy_if_impl( |
| 69 |
std::move(__first), std::move(__last), std::move(__result), __pred, __new_value, __proj); |
| 70 |
} |
| 71 |
|
| 72 |
template <input_range _Range, |
| 73 |
class _Type, |
| 74 |
output_iterator<const _Type&> _OutIter, |
| 75 |
class _Proj = identity, |
| 76 |
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred> |
| 77 |
requires indirectly_copyable<iterator_t<_Range>, _OutIter> |
| 78 |
_LIBCPP_HIDE_FROM_ABI constexpr replace_copy_if_result<borrowed_iterator_t<_Range>, _OutIter> |
| 79 |
operator()(_Range&& __range, _OutIter __result, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) const { |
| 80 |
return ranges::__replace_copy_if_impl( |
| 81 |
ranges::begin(__range), ranges::end(__range), std::move(__result), __pred, __new_value, __proj); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
} // namespace __replace_copy_if |
| 86 |
|
| 87 |
inline namespace __cpo { |
| 88 |
inline constexpr auto replace_copy_if = __replace_copy_if::__fn{}; |
| 89 |
} // namespace __cpo |
| 90 |
} // namespace ranges |
| 91 |
|
| 92 |
_LIBCPP_END_NAMESPACE_STD |
| 93 |
|
| 94 |
#endif // _LIBCPP_STD_VER >= 20 |
| 95 |
|
| 96 |
_LIBCPP_POP_MACROS |
| 97 |
|
| 98 |
#endif // _LIBCPP___ALGORITHM_RANGES_REPLACE_COPY_IF_H |