| 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_SWAP_RANGES_H |
| 10 | #define _LIBCPP___PSTL_BACKENDS_STATPART_SWAP_RANGES_H |
| 11 | |
| 12 | #include <__algorithm/swap_ranges.h> |
| 13 | #include <__config> |
| 14 | #include <__execution/is_execution_policy.h> |
| 15 | #include <__iterator/concepts.h> |
| 16 | #include <__iterator/iterator_traits.h> |
| 17 | #include <__optional/nullopt_t.h> |
| 18 | #include <__optional/optional.h> |
| 19 | #include <__pstl/backend_fwd.h> |
| 20 | #include <__pstl/backends/statpart/basis.h> |
| 21 | #include <__utility/empty.h> |
| 22 | #include <__utility/move.h> |
| 23 | |
| 24 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 25 | # pragma GCC system_header |
| 26 | #endif |
| 27 | |
| 28 | _LIBCPP_PUSH_MACROS |
| 29 | #include <__undef_macros> |
| 30 | |
| 31 | #if _LIBCPP_STD_VER >= 17 |
| 32 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 33 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 34 | namespace __pstl { |
| 35 | |
| 36 | template <class _ExecutionPolicy> |
| 37 | struct __swap_ranges<__statpart_backend_tag, _ExecutionPolicy> { |
| 38 | template <class _Policy, class _ForwardIterator1, class _ForwardIterator2> |
| 39 | _LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator2> operator()( |
| 40 | _Policy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) const noexcept { |
| 41 | if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && |
| 42 | __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value && |
| 43 | __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value) { |
| 44 | auto __res = __statpart::__for_each_iter_pair( |
| 45 | __first1, |
| 46 | __last1, |
| 47 | __first2, |
| 48 | [](_ForwardIterator1 __brick_first1, _ForwardIterator1 __brick_last1, _ForwardIterator2 __brick_first2) { |
| 49 | std::swap_ranges(std::move(__brick_first1), std::move(__brick_last1), std::move(__brick_first2)); |
| 50 | }); |
| 51 | if (!__res) { |
| 52 | return std::nullopt; // Failed to run the algorithm, propagate the error. |
| 53 | } |
| 54 | return __first2 + (__last1 - __first1); |
| 55 | } else { |
| 56 | // Non-random access iterators cannot be processed in parallel, fall back to the sequential implementation. |
| 57 | return std::swap_ranges(std::move(__first1), std::move(__last1), std::move(__first2)); |
| 58 | } |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | } // namespace __pstl |
| 63 | _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS |
| 64 | _LIBCPP_END_NAMESPACE_STD |
| 65 | |
| 66 | #endif // _LIBCPP_STD_VER >= 17 |
| 67 | |
| 68 | _LIBCPP_POP_MACROS |
| 69 | |
| 70 | #endif // _LIBCPP___PSTL_BACKENDS_STATPART_SWAP_RANGES_H |
| 71 | |