pstl_replace.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include <optional>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_PUSH_MACROS
  24. #include <__undef_macros>
  25. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. template <class>
  28. void __pstl_replace_if();
  29. template <class _ExecutionPolicy,
  30. class _ForwardIterator,
  31. class _Pred,
  32. class _Tp,
  33. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  34. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  35. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
  36. __replace_if(_ExecutionPolicy&& __policy,
  37. _ForwardIterator&& __first,
  38. _ForwardIterator&& __last,
  39. _Pred&& __pred,
  40. const _Tp& __new_value) noexcept {
  41. return std::__pstl_frontend_dispatch(
  42. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace_if, _RawPolicy),
  43. [&__policy](
  44. _ForwardIterator&& __g_first, _ForwardIterator&& __g_last, _Pred&& __g_pred, const _Tp& __g_new_value) {
  45. std::for_each(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __element) {
  46. if (__g_pred(__element))
  47. __element = __g_new_value;
  48. });
  49. return optional<__empty>{__empty{}};
  50. },
  51. std::move(__first),
  52. std::move(__last),
  53. std::move(__pred),
  54. __new_value);
  55. }
  56. template <class _ExecutionPolicy,
  57. class _ForwardIterator,
  58. class _Pred,
  59. class _Tp,
  60. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  61. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  62. _LIBCPP_HIDE_FROM_ABI void
  63. replace_if(_ExecutionPolicy&& __policy,
  64. _ForwardIterator __first,
  65. _ForwardIterator __last,
  66. _Pred __pred,
  67. const _Tp& __new_value) {
  68. auto __res = std::__replace_if(__policy, std::move(__first), std::move(__last), std::move(__pred), __new_value);
  69. if (!__res)
  70. std::__throw_bad_alloc();
  71. }
  72. template <class>
  73. void __pstl_replace();
  74. template <class _ExecutionPolicy,
  75. class _ForwardIterator,
  76. class _Tp,
  77. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  78. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  79. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
  80. __replace(_ExecutionPolicy&& __policy,
  81. _ForwardIterator __first,
  82. _ForwardIterator __last,
  83. const _Tp& __old_value,
  84. const _Tp& __new_value) noexcept {
  85. return std::__pstl_frontend_dispatch(
  86. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace, _RawPolicy),
  87. [&__policy](
  88. _ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_old_value, const _Tp& __g_new_value) {
  89. return std::__replace_if(
  90. __policy,
  91. std::move(__g_first),
  92. std::move(__g_last),
  93. [&](__iter_reference<_ForwardIterator> __element) { return __element == __g_old_value; },
  94. __g_new_value);
  95. },
  96. std::move(__first),
  97. std::move(__last),
  98. __old_value,
  99. __new_value);
  100. }
  101. template <class _ExecutionPolicy,
  102. class _ForwardIterator,
  103. class _Tp,
  104. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  105. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  106. _LIBCPP_HIDE_FROM_ABI void
  107. replace(_ExecutionPolicy&& __policy,
  108. _ForwardIterator __first,
  109. _ForwardIterator __last,
  110. const _Tp& __old_value,
  111. const _Tp& __new_value) {
  112. if (!std::__replace(__policy, std::move(__first), std::move(__last), __old_value, __new_value))
  113. std::__throw_bad_alloc();
  114. }
  115. template <class>
  116. void __pstl_replace_copy_if();
  117. template <class _ExecutionPolicy,
  118. class _ForwardIterator,
  119. class _ForwardOutIterator,
  120. class _Pred,
  121. class _Tp,
  122. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  123. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  124. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty> __replace_copy_if(
  125. _ExecutionPolicy&& __policy,
  126. _ForwardIterator&& __first,
  127. _ForwardIterator&& __last,
  128. _ForwardOutIterator&& __result,
  129. _Pred&& __pred,
  130. const _Tp& __new_value) {
  131. return std::__pstl_frontend_dispatch(
  132. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace_copy_if, _RawPolicy),
  133. [&__policy](_ForwardIterator __g_first,
  134. _ForwardIterator __g_last,
  135. _ForwardOutIterator __g_result,
  136. _Pred __g_pred,
  137. const _Tp& __g_new_value) -> optional<__empty> {
  138. if (!std::__transform(
  139. __policy, __g_first, __g_last, __g_result, [&](__iter_reference<_ForwardIterator> __element) {
  140. return __g_pred(__element) ? __g_new_value : __element;
  141. }))
  142. return nullopt;
  143. return __empty{};
  144. },
  145. std::move(__first),
  146. std::move(__last),
  147. std::move(__result),
  148. std::move(__pred),
  149. __new_value);
  150. }
  151. template <class _ExecutionPolicy,
  152. class _ForwardIterator,
  153. class _ForwardOutIterator,
  154. class _Pred,
  155. class _Tp,
  156. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  157. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  158. _LIBCPP_HIDE_FROM_ABI void replace_copy_if(
  159. _ExecutionPolicy&& __policy,
  160. _ForwardIterator __first,
  161. _ForwardIterator __last,
  162. _ForwardOutIterator __result,
  163. _Pred __pred,
  164. const _Tp& __new_value) {
  165. if (!std::__replace_copy_if(
  166. __policy, std::move(__first), std::move(__last), std::move(__result), std::move(__pred), __new_value))
  167. std::__throw_bad_alloc();
  168. }
  169. template <class>
  170. void __pstl_replace_copy();
  171. template <class _ExecutionPolicy,
  172. class _ForwardIterator,
  173. class _ForwardOutIterator,
  174. class _Tp,
  175. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  176. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  177. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty> __replace_copy(
  178. _ExecutionPolicy&& __policy,
  179. _ForwardIterator&& __first,
  180. _ForwardIterator&& __last,
  181. _ForwardOutIterator&& __result,
  182. const _Tp& __old_value,
  183. const _Tp& __new_value) noexcept {
  184. return std::__pstl_frontend_dispatch(
  185. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace_copy, _RawPolicy),
  186. [&__policy](_ForwardIterator __g_first,
  187. _ForwardIterator __g_last,
  188. _ForwardOutIterator __g_result,
  189. const _Tp& __g_old_value,
  190. const _Tp& __g_new_value) {
  191. return std::__replace_copy_if(
  192. __policy,
  193. std::move(__g_first),
  194. std::move(__g_last),
  195. std::move(__g_result),
  196. [&](__iter_reference<_ForwardIterator> __element) { return __element == __g_old_value; },
  197. __g_new_value);
  198. },
  199. std::move(__first),
  200. std::move(__last),
  201. std::move(__result),
  202. __old_value,
  203. __new_value);
  204. }
  205. template <class _ExecutionPolicy,
  206. class _ForwardIterator,
  207. class _ForwardOutIterator,
  208. class _Tp,
  209. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  210. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  211. _LIBCPP_HIDE_FROM_ABI void replace_copy(
  212. _ExecutionPolicy&& __policy,
  213. _ForwardIterator __first,
  214. _ForwardIterator __last,
  215. _ForwardOutIterator __result,
  216. const _Tp& __old_value,
  217. const _Tp& __new_value) {
  218. if (!std::__replace_copy(
  219. __policy, std::move(__first), std::move(__last), std::move(__result), __old_value, __new_value))
  220. std::__throw_bad_alloc();
  221. }
  222. _LIBCPP_END_NAMESPACE_STD
  223. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  224. _LIBCPP_POP_MACROS
  225. #endif // _LIBCPP___ALGORITHM_PSTL_REPLACE_H