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_H 10 #define _LIBCPP___ALGORITHM_RANGES_REPLACE_COPY_H 11 12 #include <__algorithm/in_out_result.h> 13 #include <__algorithm/ranges_replace_copy_if.h> 14 #include <__config> 15 #include <__functional/identity.h> 16 #include <__functional/invoke.h> 17 #include <__functional/ranges_operations.h> 18 #include <__iterator/concepts.h> 19 #include <__iterator/projected.h> 20 #include <__ranges/access.h> 21 #include <__ranges/concepts.h> 22 #include <__ranges/dangling.h> 23 #include <__utility/move.h> 24 25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 26 # pragma GCC system_header 27 #endif 28 29 #if _LIBCPP_STD_VER > 17 30 31 _LIBCPP_BEGIN_NAMESPACE_STD 32 33 namespace ranges { 34 35 template <class _InIter, class _OutIter> 36 using replace_copy_result = in_out_result<_InIter, _OutIter>; 37 38 namespace __replace_copy { 39 40 struct __fn { 41 template <input_iterator _InIter, 42 sentinel_for<_InIter> _Sent, 43 class _OldType, 44 class _NewType, 45 output_iterator<const _NewType&> _OutIter, 46 class _Proj = identity> 47 requires indirectly_copyable<_InIter, _OutIter> && 48 indirect_binary_predicate<ranges::equal_to, projected<_InIter, _Proj>, const _OldType*> 49 _LIBCPP_HIDE_FROM_ABI constexpr replace_copy_result<_InIter, _OutIter> 50 operator()(_InIter __first, 51 _Sent __last, 52 _OutIter __result, 53 const _OldType& __old_value, 54 const _NewType& __new_value, 55 _Proj __proj = {}) const { 56 auto __pred = [&](const auto& __value) { return __value == __old_value; }; 57 return ranges::__replace_copy_if_impl( 58 std::move(__first), std::move(__last), std::move(__result), __pred, __new_value, __proj); 59 } 60 61 template <input_range _Range, 62 class _OldType, 63 class _NewType, 64 output_iterator<const _NewType&> _OutIter, 65 class _Proj = identity> 66 requires indirectly_copyable<iterator_t<_Range>, _OutIter> && 67 indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _OldType*> 68 _LIBCPP_HIDE_FROM_ABI constexpr replace_copy_result<borrowed_iterator_t<_Range>, _OutIter> 69 operator()(_Range&& __range, 70 _OutIter __result, 71 const _OldType& __old_value, 72 const _NewType& __new_value, 73 _Proj __proj = {}) const { 74 auto __pred = [&](const auto& __value) { return __value == __old_value; }; 75 return ranges::__replace_copy_if_impl( 76 ranges::begin(__range), ranges::end(__range), std::move(__result), __pred, __new_value, __proj); 77 } 78 }; 79 80 } // namespace __replace_copy 81 82 inline namespace __cpo { 83 inline constexpr auto replace_copy = __replace_copy::__fn{}; 84 } // namespace __cpo 85 } // namespace ranges 86 87 _LIBCPP_END_NAMESPACE_STD 88 89 #endif // _LIBCPP_STD_VER > 17 90 91 #endif // _LIBCPP___ALGORITHM_RANGES_REPLACE_COPY_H 92