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