pstl_equal.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_EQUAL_H
  9. #define _LIBCPP___ALGORITHM_PSTL_EQUAL_H
  10. #include <__algorithm/equal.h>
  11. #include <__algorithm/pstl_frontend_dispatch.h>
  12. #include <__config>
  13. #include <__functional/operations.h>
  14. #include <__iterator/iterator_traits.h>
  15. #include <__numeric/pstl_transform_reduce.h>
  16. #include <__utility/move.h>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class>
  25. void __pstl_equal();
  26. template <class _ExecutionPolicy,
  27. class _ForwardIterator1,
  28. class _ForwardIterator2,
  29. class _Pred,
  30. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  31. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  32. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
  33. __equal(_ExecutionPolicy&& __policy,
  34. _ForwardIterator1&& __first1,
  35. _ForwardIterator1&& __last1,
  36. _ForwardIterator2&& __first2,
  37. _Pred&& __pred) noexcept {
  38. return std::__pstl_frontend_dispatch(
  39. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal, _RawPolicy),
  40. [&__policy](
  41. _ForwardIterator1 __g_first1, _ForwardIterator1 __g_last1, _ForwardIterator2 __g_first2, _Pred __g_pred) {
  42. return std::__transform_reduce(
  43. __policy,
  44. std::move(__g_first1),
  45. std::move(__g_last1),
  46. std::move(__g_first2),
  47. true,
  48. std::logical_and{},
  49. std::move(__g_pred));
  50. },
  51. std::move(__first1),
  52. std::move(__last1),
  53. std::move(__first2),
  54. std::move(__pred));
  55. }
  56. template <class _ExecutionPolicy,
  57. class _ForwardIterator1,
  58. class _ForwardIterator2,
  59. class _Pred,
  60. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  61. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  62. _LIBCPP_HIDE_FROM_ABI bool
  63. equal(_ExecutionPolicy&& __policy,
  64. _ForwardIterator1 __first1,
  65. _ForwardIterator1 __last1,
  66. _ForwardIterator2 __first2,
  67. _Pred __pred) {
  68. auto __res = std::__equal(__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__pred));
  69. if (!__res)
  70. std::__throw_bad_alloc();
  71. return *__res;
  72. }
  73. template <class _ExecutionPolicy,
  74. class _ForwardIterator1,
  75. class _ForwardIterator2,
  76. enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
  77. _LIBCPP_HIDE_FROM_ABI bool
  78. equal(_ExecutionPolicy&& __policy, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
  79. return std::equal(__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::equal_to{});
  80. }
  81. template <class _ExecutionPolicy,
  82. class _ForwardIterator1,
  83. class _ForwardIterator2,
  84. class _Pred,
  85. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  86. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  87. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
  88. __equal(_ExecutionPolicy&& __policy,
  89. _ForwardIterator1&& __first1,
  90. _ForwardIterator1&& __last1,
  91. _ForwardIterator2&& __first2,
  92. _ForwardIterator2&& __last2,
  93. _Pred&& __pred) noexcept {
  94. return std::__pstl_frontend_dispatch(
  95. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_equal, _RawPolicy),
  96. [&__policy](_ForwardIterator1 __g_first1,
  97. _ForwardIterator1 __g_last1,
  98. _ForwardIterator2 __g_first2,
  99. _ForwardIterator2 __g_last2,
  100. _Pred __g_pred) -> optional<bool> {
  101. if constexpr (__has_random_access_iterator_category<_ForwardIterator1>::value &&
  102. __has_random_access_iterator_category<_ForwardIterator2>::value) {
  103. if (__g_last1 - __g_first1 != __g_last2 - __g_first2)
  104. return false;
  105. return std::__equal(
  106. __policy, std::move(__g_first1), std::move(__g_last1), std::move(__g_first2), std::move(__g_pred));
  107. } else {
  108. (void)__policy; // Avoid unused lambda capture warning
  109. return std::equal(
  110. std::move(__g_first1),
  111. std::move(__g_last1),
  112. std::move(__g_first2),
  113. std::move(__g_last2),
  114. std::move(__g_pred));
  115. }
  116. },
  117. std::move(__first1),
  118. std::move(__last1),
  119. std::move(__first2),
  120. std::move(__last2),
  121. std::move(__pred));
  122. }
  123. template <class _ExecutionPolicy,
  124. class _ForwardIterator1,
  125. class _ForwardIterator2,
  126. class _Pred,
  127. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  128. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  129. _LIBCPP_HIDE_FROM_ABI bool
  130. equal(_ExecutionPolicy&& __policy,
  131. _ForwardIterator1 __first1,
  132. _ForwardIterator1 __last1,
  133. _ForwardIterator2 __first2,
  134. _ForwardIterator2 __last2,
  135. _Pred __pred) {
  136. auto __res = std::__equal(
  137. __policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), std::move(__pred));
  138. if (!__res)
  139. std::__throw_bad_alloc();
  140. return *__res;
  141. }
  142. template <class _ExecutionPolicy,
  143. class _ForwardIterator1,
  144. class _ForwardIterator2,
  145. enable_if_t<is_execution_policy_v<__remove_cvref_t<_ExecutionPolicy>>, int> = 0>
  146. _LIBCPP_HIDE_FROM_ABI bool
  147. equal(_ExecutionPolicy&& __policy,
  148. _ForwardIterator1 __first1,
  149. _ForwardIterator1 __last1,
  150. _ForwardIterator2 __first2,
  151. _ForwardIterator2 __last2) {
  152. return std::equal(
  153. __policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), std::equal_to{});
  154. }
  155. _LIBCPP_END_NAMESPACE_STD
  156. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  157. _LIBCPP_POP_MACROS
  158. #endif // _LIBCPP___ALGORITHM_PSTL_EQUAL_H