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