80 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H10#define _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H11 12#include <__config>13#include <__functional/identity.h>14#include <__functional/invoke.h>15#include <__iterator/concepts.h>16#include <__iterator/projected.h>17#include <__ranges/access.h>18#include <__ranges/concepts.h>19#include <__ranges/dangling.h>20#include <__utility/move.h>21 22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23# pragma GCC system_header24#endif25 26_LIBCPP_PUSH_MACROS27#include <__undef_macros>28 29#if _LIBCPP_STD_VER >= 2030 31_LIBCPP_BEGIN_NAMESPACE_STD32 33namespace ranges {34 35template <class _Iter, class _Sent, class _Type, class _Proj, class _Pred>36_LIBCPP_HIDE_FROM_ABI constexpr _Iter37__replace_if_impl(_Iter __first, _Sent __last, _Pred& __pred, const _Type& __new_value, _Proj& __proj) {38 for (; __first != __last; ++__first) {39 if (std::invoke(__pred, std::invoke(__proj, *__first)))40 *__first = __new_value;41 }42 return __first;43}44 45struct __replace_if {46 template <input_iterator _Iter,47 sentinel_for<_Iter> _Sent,48 class _Type,49 class _Proj = identity,50 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>51 requires indirectly_writable<_Iter, const _Type&>52 _LIBCPP_HIDE_FROM_ABI constexpr _Iter53 operator()(_Iter __first, _Sent __last, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) const {54 return ranges::__replace_if_impl(std::move(__first), std::move(__last), __pred, __new_value, __proj);55 }56 57 template <input_range _Range,58 class _Type,59 class _Proj = identity,60 indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>61 requires indirectly_writable<iterator_t<_Range>, const _Type&>62 _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range>63 operator()(_Range&& __range, _Pred __pred, const _Type& __new_value, _Proj __proj = {}) const {64 return ranges::__replace_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __new_value, __proj);65 }66};67 68inline namespace __cpo {69inline constexpr auto replace_if = __replace_if{};70} // namespace __cpo71} // namespace ranges72 73_LIBCPP_END_NAMESPACE_STD74 75#endif // _LIBCPP_STD_VER >= 2076 77_LIBCPP_POP_MACROS78 79#endif // _LIBCPP___ALGORITHM_RANGES_REPLACE_IF_H80