181ad6265SDimitry Andric //===----------------------------------------------------------------------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric
981ad6265SDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H
1081ad6265SDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H
1181ad6265SDimitry Andric
1281ad6265SDimitry Andric #include <__config>
1381ad6265SDimitry Andric #include <__functional/identity.h>
1481ad6265SDimitry Andric #include <__functional/invoke.h>
1581ad6265SDimitry Andric #include <__iterator/concepts.h>
1681ad6265SDimitry Andric #include <__iterator/projected.h>
1781ad6265SDimitry Andric #include <__ranges/access.h>
1881ad6265SDimitry Andric #include <__ranges/concepts.h>
1981ad6265SDimitry Andric #include <__ranges/dangling.h>
2081ad6265SDimitry Andric #include <__utility/move.h>
2181ad6265SDimitry Andric
2281ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2381ad6265SDimitry Andric # pragma GCC system_header
2481ad6265SDimitry Andric #endif
2581ad6265SDimitry Andric
26*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
27*b3edf446SDimitry Andric #include <__undef_macros>
28*b3edf446SDimitry Andric
2906c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3081ad6265SDimitry Andric
3181ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3281ad6265SDimitry Andric
3381ad6265SDimitry Andric namespace ranges {
3481ad6265SDimitry Andric
3581ad6265SDimitry Andric template <class _Iter, class _Sent, class _Type, class _Proj, class _Pred>
3606c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Iter
__replace_if_impl(_Iter __first,_Sent __last,_Pred & __pred,const _Type & __new_value,_Proj & __proj)3706c3fb27SDimitry Andric __replace_if_impl(_Iter __first, _Sent __last, _Pred& __pred, const _Type& __new_value, _Proj& __proj) {
3881ad6265SDimitry Andric for (; __first != __last; ++__first) {
3981ad6265SDimitry Andric if (std::invoke(__pred, std::invoke(__proj, *__first)))
4081ad6265SDimitry Andric *__first = __new_value;
4181ad6265SDimitry Andric }
4281ad6265SDimitry Andric return __first;
4381ad6265SDimitry Andric }
4481ad6265SDimitry Andric
4581ad6265SDimitry Andric namespace __replace_if {
4681ad6265SDimitry Andric struct __fn {
4706c3fb27SDimitry Andric template <input_iterator _Iter,
4806c3fb27SDimitry Andric sentinel_for<_Iter> _Sent,
4981ad6265SDimitry Andric class _Type,
5081ad6265SDimitry Andric class _Proj = identity,
5181ad6265SDimitry Andric indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
5281ad6265SDimitry Andric requires indirectly_writable<_Iter, const _Type&>
5306c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Iter
operator__fn5406c3fb27SDimitry Andric operator()(_Iter __first, _Sent __last, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) const {
5581ad6265SDimitry Andric return ranges::__replace_if_impl(std::move(__first), std::move(__last), __pred, __new_value, __proj);
5681ad6265SDimitry Andric }
5781ad6265SDimitry Andric
5881ad6265SDimitry Andric template <input_range _Range,
5981ad6265SDimitry Andric class _Type,
6081ad6265SDimitry Andric class _Proj = identity,
6181ad6265SDimitry Andric indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
6281ad6265SDimitry Andric requires indirectly_writable<iterator_t<_Range>, const _Type&>
6381ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range>
operator__fn6481ad6265SDimitry Andric operator()(_Range&& __range, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) const {
6581ad6265SDimitry Andric return ranges::__replace_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __new_value, __proj);
6681ad6265SDimitry Andric }
6781ad6265SDimitry Andric };
6881ad6265SDimitry Andric } // namespace __replace_if
6981ad6265SDimitry Andric
7081ad6265SDimitry Andric inline namespace __cpo {
7181ad6265SDimitry Andric inline constexpr auto replace_if = __replace_if::__fn{};
7281ad6265SDimitry Andric } // namespace __cpo
7381ad6265SDimitry Andric } // namespace ranges
7481ad6265SDimitry Andric
7581ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
7681ad6265SDimitry Andric
7706c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
7881ad6265SDimitry Andric
79*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
80*b3edf446SDimitry Andric
8181ad6265SDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H
82