File indexing completed on 2026-05-03 08:13:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H
0010 #define _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H
0011
0012 #include <__config>
0013 #include <__functional/identity.h>
0014 #include <__functional/invoke.h>
0015 #include <__iterator/concepts.h>
0016 #include <__iterator/projected.h>
0017 #include <__ranges/access.h>
0018 #include <__ranges/concepts.h>
0019 #include <__ranges/dangling.h>
0020 #include <__utility/move.h>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_PUSH_MACROS
0027 #include <__undef_macros>
0028
0029 #if _LIBCPP_STD_VER >= 20
0030
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032
0033 namespace ranges {
0034
0035 template <class _Iter, class _Sent, class _Type, class _Proj, class _Pred>
0036 _LIBCPP_HIDE_FROM_ABI constexpr _Iter
0037 __replace_if_impl(_Iter __first, _Sent __last, _Pred& __pred, const _Type& __new_value, _Proj& __proj) {
0038 for (; __first != __last; ++__first) {
0039 if (std::invoke(__pred, std::invoke(__proj, *__first)))
0040 *__first = __new_value;
0041 }
0042 return __first;
0043 }
0044
0045 struct __replace_if {
0046 template <input_iterator _Iter,
0047 sentinel_for<_Iter> _Sent,
0048 class _Type,
0049 class _Proj = identity,
0050 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
0051 requires indirectly_writable<_Iter, const _Type&>
0052 _LIBCPP_HIDE_FROM_ABI constexpr _Iter
0053 operator()(_Iter __first, _Sent __last, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) const {
0054 return ranges::__replace_if_impl(std::move(__first), std::move(__last), __pred, __new_value, __proj);
0055 }
0056
0057 template <input_range _Range,
0058 class _Type,
0059 class _Proj = identity,
0060 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
0061 requires indirectly_writable<iterator_t<_Range>, const _Type&>
0062 _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range>
0063 operator()(_Range&& __range, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) const {
0064 return ranges::__replace_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __new_value, __proj);
0065 }
0066 };
0067
0068 inline namespace __cpo {
0069 inline constexpr auto replace_if = __replace_if{};
0070 }
0071 }
0072
0073 _LIBCPP_END_NAMESPACE_STD
0074
0075 #endif
0076
0077 _LIBCPP_POP_MACROS
0078
0079 #endif