|
9 | 9 | #ifndef _LIBCPP___ALGORITHM_COPY_H |
10 | 10 | #define _LIBCPP___ALGORITHM_COPY_H |
11 | 11 |
|
12 | | -#include <__algorithm/unwrap_iter.h> |
13 | | -#include <__algorithm/unwrap_range.h> |
| 12 | +#include <__algorithm/copy_move_common.h> |
| 13 | +#include <__algorithm/iterator_operations.h> |
14 | 14 | #include <__config> |
15 | | -#include <__iterator/iterator_traits.h> |
16 | | -#include <__iterator/reverse_iterator.h> |
17 | | -#include <__type_traits/enable_if.h> |
18 | | -#include <__type_traits/is_copy_constructible.h> |
19 | | -#include <__type_traits/is_same.h> |
20 | | -#include <__type_traits/is_trivially_copy_assignable.h> |
21 | | -#include <__type_traits/is_trivially_copyable.h> |
22 | | -#include <__type_traits/remove_const.h> |
23 | 15 | #include <__utility/move.h> |
24 | 16 | #include <__utility/pair.h> |
25 | | -#include <cstring> |
26 | 17 |
|
27 | 18 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
28 | 19 | # pragma GCC system_header |
29 | 20 | #endif |
30 | 21 |
|
31 | 22 | _LIBCPP_BEGIN_NAMESPACE_STD |
32 | 23 |
|
33 | | -// copy |
| 24 | +struct __copy_loop { |
| 25 | + template <class _InIter, class _Sent, class _OutIter> |
| 26 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> |
| 27 | + operator()(_InIter __first, _Sent __last, _OutIter __result) const { |
| 28 | + while (__first != __last) { |
| 29 | + *__result = *__first; |
| 30 | + ++__first; |
| 31 | + ++__result; |
| 32 | + } |
34 | 33 |
|
35 | | -template <class _InIter, class _Sent, class _OutIter> |
36 | | -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 |
37 | | -pair<_InIter, _OutIter> __copy_impl(_InIter __first, _Sent __last, _OutIter __result) { |
38 | | - while (__first != __last) { |
39 | | - *__result = *__first; |
40 | | - ++__first; |
41 | | - ++__result; |
| 34 | + return std::make_pair(std::move(__first), std::move(__result)); |
42 | 35 | } |
43 | | - return pair<_InIter, _OutIter>(std::move(__first), std::move(__result)); |
44 | | -} |
| 36 | +}; |
45 | 37 |
|
46 | | -template <class _InValueT, |
47 | | - class _OutValueT, |
48 | | - class = __enable_if_t<is_same<__remove_const_t<_InValueT>, _OutValueT>::value |
49 | | - && is_trivially_copy_assignable<_OutValueT>::value> > |
50 | | -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 |
51 | | -pair<_InValueT*, _OutValueT*> __copy_impl(_InValueT* __first, _InValueT* __last, _OutValueT* __result) { |
52 | | - if (__libcpp_is_constant_evaluated() |
53 | | -// TODO: Remove this once GCC supports __builtin_memmove during constant evaluation |
54 | | -#ifndef _LIBCPP_COMPILER_GCC |
55 | | - && !is_trivially_copyable<_InValueT>::value |
56 | | -#endif |
57 | | - ) |
58 | | - return std::__copy_impl<_InValueT*, _InValueT*, _OutValueT*>(__first, __last, __result); |
59 | | - const size_t __n = static_cast<size_t>(__last - __first); |
60 | | - if (__n > 0) |
61 | | - ::__builtin_memmove(__result, __first, __n * sizeof(_OutValueT)); |
62 | | - return std::make_pair(__first + __n, __result + __n); |
63 | | -} |
| 38 | +struct __copy_trivial { |
| 39 | + // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer. |
| 40 | + template <class _In, class _Out, |
| 41 | + __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0> |
| 42 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> |
| 43 | + operator()(_In* __first, _In* __last, _Out* __result) const { |
| 44 | + return std::__copy_trivial_impl(__first, __last, __result); |
| 45 | + } |
| 46 | +}; |
64 | 47 |
|
65 | | -template <class _InIter, class _OutIter, |
66 | | - __enable_if_t<is_same<__remove_const_t<__iter_value_type<_InIter> >, __iter_value_type<_OutIter> >::value |
67 | | - && __is_cpp17_contiguous_iterator<typename _InIter::iterator_type>::value |
68 | | - && __is_cpp17_contiguous_iterator<typename _OutIter::iterator_type>::value |
69 | | - && is_trivially_copy_assignable<__iter_value_type<_OutIter> >::value |
70 | | - && __is_reverse_iterator<_InIter>::value |
71 | | - && __is_reverse_iterator<_OutIter>::value, int> = 0> |
72 | | -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 |
| 48 | +template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> |
73 | 49 | pair<_InIter, _OutIter> |
74 | | -__copy_impl(_InIter __first, _InIter __last, _OutIter __result) { |
75 | | - auto __first_base = std::__unwrap_iter(__first.base()); |
76 | | - auto __last_base = std::__unwrap_iter(__last.base()); |
77 | | - auto __result_base = std::__unwrap_iter(__result.base()); |
78 | | - auto __result_first = __result_base - (__first_base - __last_base); |
79 | | - std::__copy_impl(__last_base, __first_base, __result_first); |
80 | | - return std::make_pair(__last, _OutIter(std::__rewrap_iter(__result.base(), __result_first))); |
81 | | -} |
82 | | - |
83 | | -template <class _InIter, class _Sent, class _OutIter, |
84 | | - __enable_if_t<!(is_copy_constructible<_InIter>::value |
85 | | - && is_copy_constructible<_Sent>::value |
86 | | - && is_copy_constructible<_OutIter>::value), int> = 0 > |
87 | | -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 |
88 | | -pair<_InIter, _OutIter> __copy(_InIter __first, _Sent __last, _OutIter __result) { |
89 | | - return std::__copy_impl(std::move(__first), std::move(__last), std::move(__result)); |
90 | | -} |
91 | | - |
92 | | -template <class _InIter, class _Sent, class _OutIter, |
93 | | - __enable_if_t<is_copy_constructible<_InIter>::value |
94 | | - && is_copy_constructible<_Sent>::value |
95 | | - && is_copy_constructible<_OutIter>::value, int> = 0> |
96 | 50 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 |
97 | | -pair<_InIter, _OutIter> __copy(_InIter __first, _Sent __last, _OutIter __result) { |
98 | | - auto __range = std::__unwrap_range(__first, __last); |
99 | | - auto __ret = std::__copy_impl(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__result)); |
100 | | - return std::make_pair( |
101 | | - std::__rewrap_range<_Sent>(__first, __ret.first), std::__rewrap_iter(__result, __ret.second)); |
| 51 | +__copy(_InIter __first, _Sent __last, _OutIter __result) { |
| 52 | + return std::__dispatch_copy_or_move<_AlgPolicy, __copy_loop, __copy_trivial>( |
| 53 | + std::move(__first), std::move(__last), std::move(__result)); |
102 | 54 | } |
103 | 55 |
|
104 | 56 | template <class _InputIterator, class _OutputIterator> |
105 | 57 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 |
106 | 58 | _OutputIterator |
107 | 59 | copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) { |
108 | | - return std::__copy(__first, __last, __result).second; |
| 60 | + return std::__copy<_ClassicAlgPolicy>(__first, __last, __result).second; |
109 | 61 | } |
110 | 62 |
|
111 | 63 | _LIBCPP_END_NAMESPACE_STD |
|
0 commit comments