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