ranges_replace.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef _LIBCPP___ALGORITHM_RANGES_REPLACE_H
  9. #define _LIBCPP___ALGORITHM_RANGES_REPLACE_H
  10. #include <__algorithm/ranges_replace_if.h>
  11. #include <__config>
  12. #include <__functional/identity.h>
  13. #include <__functional/ranges_operations.h>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/projected.h>
  16. #include <__ranges/access.h>
  17. #include <__ranges/concepts.h>
  18. #include <__ranges/dangling.h>
  19. #include <__utility/move.h>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. #if _LIBCPP_STD_VER >= 20
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. namespace ranges {
  26. namespace __replace {
  27. struct __fn {
  28. template <input_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type1, class _Type2, class _Proj = identity>
  29. requires indirectly_writable<_Iter, const _Type2&> &&
  30. indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type1*>
  31. _LIBCPP_HIDE_FROM_ABI constexpr _Iter operator()(
  32. _Iter __first, _Sent __last, const _Type1& __old_value, const _Type2& __new_value, _Proj __proj = {}) const {
  33. auto __pred = [&](const auto& __val) { return __val == __old_value; };
  34. return ranges::__replace_if_impl(std::move(__first), std::move(__last), __pred, __new_value, __proj);
  35. }
  36. template <input_range _Range, class _Type1, class _Type2, class _Proj = identity>
  37. requires indirectly_writable<iterator_t<_Range>, const _Type2&> &&
  38. indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type1*>
  39. _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range>
  40. operator()(_Range&& __range, const _Type1& __old_value, const _Type2& __new_value, _Proj __proj = {}) const {
  41. auto __pred = [&](auto&& __val) { return __val == __old_value; };
  42. return ranges::__replace_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __new_value, __proj);
  43. }
  44. };
  45. } // namespace __replace
  46. inline namespace __cpo {
  47. inline constexpr auto replace = __replace::__fn{};
  48. } // namespace __cpo
  49. } // namespace ranges
  50. _LIBCPP_END_NAMESPACE_STD
  51. #endif // _LIBCPP_STD_VER >= 20
  52. #endif // _LIBCPP___ALGORITHM_RANGES_REPLACE_H