| 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___ALGORITHM_COPY_H |
| 10 | #define _LIBCPP___ALGORITHM_COPY_H |
| 11 | |
| 12 | #include <__algorithm/copy_move_common.h> |
| 13 | #include <__algorithm/for_each_segment.h> |
| 14 | #include <__algorithm/min.h> |
| 15 | #include <__algorithm/specialized_algorithms.h> |
| 16 | #include <__config> |
| 17 | #include <__iterator/iterator_traits.h> |
| 18 | #include <__iterator/segmented_iterator.h> |
| 19 | #include <__type_traits/common_type.h> |
| 20 | #include <__type_traits/enable_if.h> |
| 21 | #include <__utility/move.h> |
| 22 | #include <__utility/pair.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 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 32 | |
| 33 | template <class _InputIterator, class _OutputIterator> |
| 34 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator |
| 35 | copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result); |
| 36 | |
| 37 | template <class _InIter, class _Sent, class _OutIter> |
| 38 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> __copy(_InIter, _Sent, _OutIter); |
| 39 | |
| 40 | struct __copy_impl { |
| 41 | template <class _InIter, |
| 42 | class _Sent, |
| 43 | class _OutIter, |
| 44 | __enable_if_t<!__specialized_algorithm<_Algorithm::__copy, |
| 45 | __iterator_pair<_InIter, _Sent>, |
| 46 | __single_iterator<_OutIter> >::__has_algorithm, |
| 47 | int> = 0> |
| 48 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> |
| 49 | operator()(_InIter __first, _Sent __last, _OutIter __result) const { |
| 50 | while (__first != __last) { |
| 51 | *__result = *__first; |
| 52 | ++__first; |
| 53 | ++__result; |
| 54 | } |
| 55 | |
| 56 | return std::make_pair(std::move(__first), std::move(__result)); |
| 57 | } |
| 58 | |
| 59 | template <class _InIter, |
| 60 | class _Sent, |
| 61 | class _OutIter, |
| 62 | __enable_if_t<__specialized_algorithm<_Algorithm::__copy, |
| 63 | __iterator_pair<_InIter, _Sent>, |
| 64 | __single_iterator<_OutIter> >::__has_algorithm, |
| 65 | int> = 0> |
| 66 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 static pair<_InIter, _OutIter> |
| 67 | operator()(_InIter __first, _Sent __last, _OutIter __result) { |
| 68 | return __specialized_algorithm<_Algorithm::__copy, __iterator_pair<_InIter, _Sent>, __single_iterator<_OutIter> >()( |
| 69 | std::move(__first), std::move(__last), std::move(__result)); |
| 70 | } |
| 71 | |
| 72 | template <class _InIter, class _OutIter, __enable_if_t<__is_segmented_iterator_v<_InIter>, int> = 0> |
| 73 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> |
| 74 | operator()(_InIter __first, _InIter __last, _OutIter __result) const { |
| 75 | using __local_iterator = typename __segmented_iterator_traits<_InIter>::__local_iterator; |
| 76 | std::__for_each_segment(__first, __last, [&__result](__local_iterator __lfirst, __local_iterator __llast) { |
| 77 | __result = std::__copy(std::move(__lfirst), std::move(__llast), std::move(__result)).second; |
| 78 | }); |
| 79 | return std::make_pair(__last, std::move(__result)); |
| 80 | } |
| 81 | |
| 82 | template <class _InIter, |
| 83 | class _OutIter, |
| 84 | __enable_if_t<__has_random_access_iterator_category<_InIter>::value && |
| 85 | !__is_segmented_iterator_v<_InIter> && __is_segmented_iterator_v<_OutIter>, |
| 86 | int> = 0> |
| 87 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> |
| 88 | operator()(_InIter __first, _InIter __last, _OutIter __result) const { |
| 89 | using _Traits = __segmented_iterator_traits<_OutIter>; |
| 90 | using _DiffT = |
| 91 | typename common_type<__iterator_difference_type<_InIter>, __iterator_difference_type<_OutIter> >::type; |
| 92 | |
| 93 | if (__first == __last) |
| 94 | return std::make_pair(std::move(__first), std::move(__result)); |
| 95 | |
| 96 | auto __local_first = _Traits::__local(__result); |
| 97 | auto __segment_iterator = _Traits::__segment(__result); |
| 98 | while (true) { |
| 99 | auto __local_last = _Traits::__end(__segment_iterator); |
| 100 | auto __size = std::min<_DiffT>(__local_last - __local_first, __last - __first); |
| 101 | auto __iters = std::__copy(__first, __first + __size, __local_first); |
| 102 | __first = std::move(__iters.first); |
| 103 | |
| 104 | if (__first == __last) |
| 105 | return std::make_pair(std::move(__first), _Traits::__compose(__segment_iterator, std::move(__iters.second))); |
| 106 | |
| 107 | __local_first = _Traits::__begin(++__segment_iterator); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer. |
| 112 | template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0> |
| 113 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> |
| 114 | operator()(_In* __first, _In* __last, _Out* __result) const { |
| 115 | return std::__copy_trivial_impl(__first, __last, __result); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | template <class _InIter, class _Sent, class _OutIter> |
| 120 | pair<_InIter, _OutIter> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 |
| 121 | __copy(_InIter __first, _Sent __last, _OutIter __result) { |
| 122 | return std::__copy_move_unwrap_iters<__copy_impl>(std::move(__first), std::move(__last), std::move(__result)); |
| 123 | } |
| 124 | |
| 125 | template <class _InputIterator, class _OutputIterator> |
| 126 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator |
| 127 | copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) { |
| 128 | return std::__copy(__first, __last, __result).second; |
| 129 | } |
| 130 | |
| 131 | _LIBCPP_END_NAMESPACE_STD |
| 132 | |
| 133 | _LIBCPP_POP_MACROS |
| 134 | |
| 135 | #endif // _LIBCPP___ALGORITHM_COPY_H |
| 136 | |