pstl_find.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_FIND_H
  9. #define _LIBCPP___ALGORITHM_PSTL_FIND_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/find.h>
  12. #include <__algorithm/pstl_backend.h>
  13. #include <__algorithm/pstl_frontend_dispatch.h>
  14. #include <__config>
  15. #include <__iterator/cpp17_iterator_concepts.h>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/is_execution_policy.h>
  18. #include <__type_traits/remove_cvref.h>
  19. #include <__utility/move.h>
  20. #include <optional>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_PUSH_MACROS
  25. #include <__undef_macros>
  26. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. template <class _ExecutionPolicy,
  29. class _ForwardIterator,
  30. class _Predicate,
  31. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  32. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  33. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardIterator>>
  34. __find_if(_ExecutionPolicy&&, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) noexcept {
  35. using _Backend = typename __select_backend<_RawPolicy>::type;
  36. return std::__pstl_find_if<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__pred));
  37. }
  38. template <class _ExecutionPolicy,
  39. class _ForwardIterator,
  40. class _Predicate,
  41. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  42. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  43. _LIBCPP_HIDE_FROM_ABI _ForwardIterator
  44. find_if(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
  45. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  46. auto __res = std::__find_if(__policy, std::move(__first), std::move(__last), std::move(__pred));
  47. if (!__res)
  48. std::__throw_bad_alloc();
  49. return *std::move(__res);
  50. }
  51. template <class>
  52. void __pstl_find_if_not();
  53. template <class _ExecutionPolicy,
  54. class _ForwardIterator,
  55. class _Predicate,
  56. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  57. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  58. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardIterator>>
  59. __find_if_not(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) {
  60. return std::__pstl_frontend_dispatch(
  61. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find_if_not, _RawPolicy),
  62. [&](_ForwardIterator&& __g_first, _ForwardIterator&& __g_last, _Predicate&& __g_pred)
  63. -> optional<__remove_cvref_t<_ForwardIterator>> {
  64. return std::__find_if(
  65. __policy, __g_first, __g_last, [&](__iter_reference<__remove_cvref_t<_ForwardIterator>> __value) {
  66. return !__g_pred(__value);
  67. });
  68. },
  69. std::move(__first),
  70. std::move(__last),
  71. std::move(__pred));
  72. }
  73. template <class _ExecutionPolicy,
  74. class _ForwardIterator,
  75. class _Predicate,
  76. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  77. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  78. _LIBCPP_HIDE_FROM_ABI _ForwardIterator
  79. find_if_not(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
  80. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  81. auto __res = std::__find_if_not(__policy, std::move(__first), std::move(__last), std::move(__pred));
  82. if (!__res)
  83. std::__throw_bad_alloc();
  84. return *std::move(__res);
  85. }
  86. template <class>
  87. void __pstl_find();
  88. template <class _ExecutionPolicy,
  89. class _ForwardIterator,
  90. class _Tp,
  91. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  92. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  93. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardIterator>>
  94. __find(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) noexcept {
  95. return std::__pstl_frontend_dispatch(
  96. _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find, _RawPolicy),
  97. [&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value) -> optional<_ForwardIterator> {
  98. return std::find_if(
  99. __policy, __g_first, __g_last, [&](__iter_reference<__remove_cvref_t<_ForwardIterator>> __element) {
  100. return __element == __g_value;
  101. });
  102. },
  103. std::move(__first),
  104. std::move(__last),
  105. __value);
  106. }
  107. template <class _ExecutionPolicy,
  108. class _ForwardIterator,
  109. class _Tp,
  110. class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
  111. enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
  112. _LIBCPP_HIDE_FROM_ABI _ForwardIterator
  113. find(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
  114. _LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator);
  115. auto __res = std::__find(__policy, std::move(__first), std::move(__last), __value);
  116. if (!__res)
  117. std::__throw_bad_alloc();
  118. return *std::move(__res);
  119. }
  120. _LIBCPP_END_NAMESPACE_STD
  121. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
  122. _LIBCPP_POP_MACROS
  123. #endif // _LIBCPP___ALGORITHM_PSTL_FIND_H