pstl_find.h 5.7 KB

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