xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/move.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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_MOVE_H
10 #define _LIBCPP___ALGORITHM_MOVE_H
11 
12 #include <__algorithm/copy_move_common.h>
13 #include <__algorithm/iterator_operations.h>
14 #include <__algorithm/min.h>
15 #include <__config>
16 #include <__iterator/segmented_iterator.h>
17 #include <__type_traits/common_type.h>
18 #include <__type_traits/is_copy_constructible.h>
19 #include <__utility/move.h>
20 #include <__utility/pair.h>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
32 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
33 __move(_InIter __first, _Sent __last, _OutIter __result);
34 
35 template <class _AlgPolicy>
36 struct __move_loop {
37   template <class _InIter, class _Sent, class _OutIter>
38   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
39   operator()(_InIter __first, _Sent __last, _OutIter __result) const {
40     while (__first != __last) {
41       *__result = _IterOps<_AlgPolicy>::__iter_move(__first);
42       ++__first;
43       ++__result;
44     }
45     return std::make_pair(std::move(__first), std::move(__result));
46   }
47 
48   template <class _InIter, class _OutIter, __enable_if_t<__is_segmented_iterator<_InIter>::value, int> = 0>
49   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
50   operator()(_InIter __first, _InIter __last, _OutIter __result) const {
51     using _Traits = __segmented_iterator_traits<_InIter>;
52     auto __sfirst = _Traits::__segment(__first);
53     auto __slast  = _Traits::__segment(__last);
54     if (__sfirst == __slast) {
55       auto __iters = std::__move<_AlgPolicy>(_Traits::__local(__first), _Traits::__local(__last), std::move(__result));
56       return std::make_pair(__last, std::move(__iters.second));
57     }
58 
59     __result = std::__move<_AlgPolicy>(_Traits::__local(__first), _Traits::__end(__sfirst), std::move(__result)).second;
60     ++__sfirst;
61     while (__sfirst != __slast) {
62       __result =
63           std::__move<_AlgPolicy>(_Traits::__begin(__sfirst), _Traits::__end(__sfirst), std::move(__result)).second;
64       ++__sfirst;
65     }
66     __result =
67         std::__move<_AlgPolicy>(_Traits::__begin(__sfirst), _Traits::__local(__last), std::move(__result)).second;
68     return std::make_pair(__last, std::move(__result));
69   }
70 
71   template <class _InIter,
72             class _OutIter,
73             __enable_if_t<__is_cpp17_random_access_iterator<_InIter>::value &&
74                               !__is_segmented_iterator<_InIter>::value && __is_segmented_iterator<_OutIter>::value,
75                           int> = 0>
76   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
77   operator()(_InIter __first, _InIter __last, _OutIter __result) {
78     using _Traits = __segmented_iterator_traits<_OutIter>;
79     using _DiffT  = typename common_type<__iter_diff_t<_InIter>, __iter_diff_t<_OutIter> >::type;
80 
81     if (__first == __last)
82       return std::make_pair(std::move(__first), std::move(__result));
83 
84     auto __local_first      = _Traits::__local(__result);
85     auto __segment_iterator = _Traits::__segment(__result);
86     while (true) {
87       auto __local_last = _Traits::__end(__segment_iterator);
88       auto __size       = std::min<_DiffT>(__local_last - __local_first, __last - __first);
89       auto __iters      = std::__move<_AlgPolicy>(__first, __first + __size, __local_first);
90       __first           = std::move(__iters.first);
91 
92       if (__first == __last)
93         return std::make_pair(std::move(__first), _Traits::__compose(__segment_iterator, std::move(__iters.second)));
94 
95       __local_first = _Traits::__begin(++__segment_iterator);
96     }
97   }
98 };
99 
100 struct __move_trivial {
101   // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
102   template <class _In, class _Out,
103             __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
104   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
105   operator()(_In* __first, _In* __last, _Out* __result) const {
106     return std::__copy_trivial_impl(__first, __last, __result);
107   }
108 };
109 
110 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
111 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
112 __move(_InIter __first, _Sent __last, _OutIter __result) {
113   return std::__dispatch_copy_or_move<_AlgPolicy, __move_loop<_AlgPolicy>, __move_trivial>(
114       std::move(__first), std::move(__last), std::move(__result));
115 }
116 
117 template <class _InputIterator, class _OutputIterator>
118 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
119 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
120   static_assert(is_copy_constructible<_InputIterator>::value, "Iterators has to be copy constructible.");
121   static_assert(is_copy_constructible<_OutputIterator>::value, "The output iterator has to be copy constructible.");
122 
123   return std::__move<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__result)).second;
124 }
125 
126 _LIBCPP_END_NAMESPACE_STD
127 
128 _LIBCPP_POP_MACROS
129 
130 #endif // _LIBCPP___ALGORITHM_MOVE_H
131