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