pstl_replace.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_PSTL_REPLACE_H
  9. #define _LIBCPP___ALGORITHM_PSTL_REPLACE_H
  10. #include <__algorithm/pstl_backend.h>
  11. #include <__algorithm/pstl_for_each.h>
  12. #include <__algorithm/pstl_frontend_dispatch.h>
  13. #include <__algorithm/pstl_transform.h>
  14. #include <__config>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/remove_cvref.h>
  18. #include <__utility/move.h>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class>
  25. void __pstl_replace_if();
  26. template <class _ExecutionPolicy,
  27. class _ForwardIterator,
  28. class _Pred,
  29. class _Tp,
  30. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  31. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  32. _LIBCPP_HIDE_FROM_ABI void
  33. replace_if(_ExecutionPolicy&& __policy,
  34. _ForwardIterator __first,
  35. _ForwardIterator __last,
  36. _Pred __pred,
  37. const _Tp& __new_value) {
  38. std::__pstl_frontend_dispatch(
  39. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace_if, _RawPolicy),
  40. [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Pred __g_pred, const _Tp& __g_new_value) {
  41. std::for_each(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __element) {
  42. if (__g_pred(__element))
  43. __element = __g_new_value;
  44. });
  45. },
  46. std::move(__first),
  47. std::move(__last),
  48. std::move(__pred),
  49. __new_value);
  50. }
  51. template <class>
  52. void __pstl_replace();
  53. template <class _ExecutionPolicy,
  54. class _ForwardIterator,
  55. class _Tp,
  56. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  57. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  58. _LIBCPP_HIDE_FROM_ABI void
  59. replace(_ExecutionPolicy&& __policy,
  60. _ForwardIterator __first,
  61. _ForwardIterator __last,
  62. const _Tp& __old_value,
  63. const _Tp& __new_value) {
  64. std::__pstl_frontend_dispatch(
  65. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace, _RawPolicy),
  66. [&__policy](
  67. _ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_old_value, const _Tp& __g_new_value) {
  68. std::replace_if(
  69. __policy,
  70. std::move(__g_first),
  71. std::move(__g_last),
  72. [&](__iter_reference<_ForwardIterator> __element) { return __element == __g_old_value; },
  73. __g_new_value);
  74. },
  75. std::move(__first),
  76. std::move(__last),
  77. __old_value,
  78. __new_value);
  79. }
  80. template <class>
  81. void __pstl_replace_copy_if();
  82. template <class _ExecutionPolicy,
  83. class _ForwardIterator,
  84. class _ForwardOutIterator,
  85. class _Pred,
  86. class _Tp,
  87. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  88. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  89. _LIBCPP_HIDE_FROM_ABI void replace_copy_if(
  90. _ExecutionPolicy&& __policy,
  91. _ForwardIterator __first,
  92. _ForwardIterator __last,
  93. _ForwardOutIterator __result,
  94. _Pred __pred,
  95. const _Tp& __new_value) {
  96. std::__pstl_frontend_dispatch(
  97. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace_copy_if, _RawPolicy),
  98. [&__policy](_ForwardIterator __g_first,
  99. _ForwardIterator __g_last,
  100. _ForwardOutIterator __g_result,
  101. _Pred __g_pred,
  102. const _Tp& __g_new_value) {
  103. std::transform(__policy, __g_first, __g_last, __g_result, [&](__iter_reference<_ForwardIterator> __element) {
  104. return __g_pred(__element) ? __g_new_value : __element;
  105. });
  106. },
  107. std::move(__first),
  108. std::move(__last),
  109. std::move(__result),
  110. std::move(__pred),
  111. __new_value);
  112. }
  113. template <class>
  114. void __pstl_replace_copy();
  115. template <class _ExecutionPolicy,
  116. class _ForwardIterator,
  117. class _ForwardOutIterator,
  118. class _Tp,
  119. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  120. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  121. _LIBCPP_HIDE_FROM_ABI void replace_copy(
  122. _ExecutionPolicy&& __policy,
  123. _ForwardIterator __first,
  124. _ForwardIterator __last,
  125. _ForwardOutIterator __result,
  126. const _Tp& __old_value,
  127. const _Tp& __new_value) {
  128. std::__pstl_frontend_dispatch(
  129. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace_copy, _RawPolicy),
  130. [&__policy](_ForwardIterator __g_first,
  131. _ForwardIterator __g_last,
  132. _ForwardOutIterator __g_result,
  133. const _Tp& __g_old_value,
  134. const _Tp& __g_new_value) {
  135. return std::replace_copy_if(
  136. __policy,
  137. std::move(__g_first),
  138. std::move(__g_last),
  139. std::move(__g_result),
  140. [&](__iter_reference<_ForwardIterator> __element) { return __element == __g_old_value; },
  141. __g_new_value);
  142. },
  143. std::move(__first),
  144. std::move(__last),
  145. std::move(__result),
  146. __old_value,
  147. __new_value);
  148. }
  149. _LIBCPP_END_NAMESPACE_STD
  150. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  151. #endif // _LIBCPP___ALGORITHM_PSTL_REPLACE_H