| 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 | |
| 9 | #ifndef _LIBCPP___PSTL_CPU_ALGOS_MISMATCH_H |
| 10 | #define _LIBCPP___PSTL_CPU_ALGOS_MISMATCH_H |
| 11 | |
| 12 | #include <__algorithm/min.h> |
| 13 | #include <__algorithm/mismatch.h> |
| 14 | #include <__config> |
| 15 | #include <__functional/operations.h> |
| 16 | #include <__iterator/concepts.h> |
| 17 | #include <__iterator/iterator_traits.h> |
| 18 | #include <__optional/nullopt_t.h> |
| 19 | #include <__optional/optional.h> |
| 20 | #include <__pstl/backend_fwd.h> |
| 21 | #include <__pstl/cpu_algos/cpu_traits.h> |
| 22 | #include <__pstl/cpu_algos/find_if.h> |
| 23 | #include <__type_traits/is_execution_policy.h> |
| 24 | #include <__utility/move.h> |
| 25 | #include <__utility/pair.h> |
| 26 | |
| 27 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 28 | # pragma GCC system_header |
| 29 | #endif |
| 30 | |
| 31 | _LIBCPP_PUSH_MACROS |
| 32 | #include <__undef_macros> |
| 33 | |
| 34 | #if _LIBCPP_STD_VER >= 17 |
| 35 | |
| 36 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 37 | namespace __pstl { |
| 38 | |
| 39 | template <class _Backend, class _RawExecutionPolicy> |
| 40 | struct __cpu_parallel_mismatch { |
| 41 | template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate> |
| 42 | _LIBCPP_HIDE_FROM_ABI optional<pair<_ForwardIterator1, _ForwardIterator2>> |
| 43 | operator()(_Policy&&, |
| 44 | _ForwardIterator1 __first1, |
| 45 | _ForwardIterator1 __last1, |
| 46 | _ForwardIterator2 __first2, |
| 47 | _ForwardIterator2 __last2, |
| 48 | _Predicate __pred) const noexcept { |
| 49 | if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> && |
| 50 | __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value && |
| 51 | __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value) { |
| 52 | // Look for a mismatch only in the prefix of the two ranges. |
| 53 | auto __n = std::min(__last1 - __first1, __last2 - __first2); |
| 54 | // Find a position in the first range where the predicate is false against the corresponding position in the |
| 55 | // second range. |
| 56 | auto __res = __pstl::__parallel_find<_Backend>( |
| 57 | __first1, |
| 58 | __first1 + __n, |
| 59 | [&__pred, __first1, __first2](_ForwardIterator1 __brick_first1, _ForwardIterator1 __brick_last1) { |
| 60 | // Run the sequential mismatch algorithm on these ranges: |
| 61 | // [__brick_first1, __brick_last1) and |
| 62 | // [__first2 + (__brick_first1 - __first1), __first2 + (__brick_last1 - __first1)) |
| 63 | auto __brick_first2 = __first2 + (__brick_first1 - __first1); |
| 64 | return std::mismatch(std::move(__brick_first1), std::move(__brick_last1), std::move(__brick_first2), __pred) |
| 65 | .first; |
| 66 | }, |
| 67 | less<>{}, // `less` here means the lowest index among the mismatches |
| 68 | true // `true` here means we want the first mismatch, not the last |
| 69 | ); |
| 70 | if (!__res) { |
| 71 | return std::nullopt; // Failed to run the algorithm, propagate the error. |
| 72 | } |
| 73 | auto __idx = *__res - __first1; |
| 74 | return pair<_ForwardIterator1, _ForwardIterator2>{std::move(*__res), __first2 + __idx}; |
| 75 | } else { |
| 76 | // Non-random access iterators cannot be processed in parallel, fall back to the sequential implementation. |
| 77 | // Unsequenced execution is also implicitly covered by the sequential implementation. |
| 78 | return std::mismatch( |
| 79 | std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), std::move(__pred)); |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | } // namespace __pstl |
| 85 | _LIBCPP_END_NAMESPACE_STD |
| 86 | |
| 87 | #endif // _LIBCPP_STD_VER >= 17 |
| 88 | |
| 89 | _LIBCPP_POP_MACROS |
| 90 | |
| 91 | #endif // _LIBCPP___PSTL_CPU_ALGOS_MISMATCH_H |
| 92 | |