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___CXX03___ALGORITHM_MOVE_H
10 #define _LIBCPP___CXX03___ALGORITHM_MOVE_H
11
12 #include <__cxx03/__algorithm/copy_move_common.h>
13 #include <__cxx03/__algorithm/for_each_segment.h>
14 #include <__cxx03/__algorithm/iterator_operations.h>
15 #include <__cxx03/__algorithm/min.h>
16 #include <__cxx03/__config>
17 #include <__cxx03/__iterator/segmented_iterator.h>
18 #include <__cxx03/__type_traits/common_type.h>
19 #include <__cxx03/__type_traits/is_constructible.h>
20 #include <__cxx03/__utility/move.h>
21 #include <__cxx03/__utility/pair.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 <__cxx03/__undef_macros>
29
30 _LIBCPP_BEGIN_NAMESPACE_STD
31
32 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
33 inline _LIBCPP_HIDE_FROM_ABI pair<_InIter, _OutIter> __move(_InIter __first, _Sent __last, _OutIter __result);
34
35 template <class _AlgPolicy>
36 struct __move_impl {
37 template <class _InIter, class _Sent, class _OutIter>
operator__move_impl38 _LIBCPP_HIDE_FROM_ABI pair<_InIter, _OutIter> operator()(_InIter __first, _Sent __last, _OutIter __result) const {
39 while (__first != __last) {
40 *__result = _IterOps<_AlgPolicy>::__iter_move(__first);
41 ++__first;
42 ++__result;
43 }
44 return std::make_pair(std::move(__first), std::move(__result));
45 }
46
47 template <class _InIter, class _OutIter>
48 struct _MoveSegment {
49 using _Traits = __segmented_iterator_traits<_InIter>;
50
51 _OutIter& __result_;
52
_MoveSegment__move_impl::_MoveSegment53 _LIBCPP_HIDE_FROM_ABI explicit _MoveSegment(_OutIter& __result) : __result_(__result) {}
54
55 _LIBCPP_HIDE_FROM_ABI void
operator__move_impl::_MoveSegment56 operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {
57 __result_ = std::__move<_AlgPolicy>(__lfirst, __llast, std::move(__result_)).second;
58 }
59 };
60
61 template <class _InIter, class _OutIter, __enable_if_t<__is_segmented_iterator<_InIter>::value, int> = 0>
operator__move_impl62 _LIBCPP_HIDE_FROM_ABI pair<_InIter, _OutIter> operator()(_InIter __first, _InIter __last, _OutIter __result) const {
63 std::__for_each_segment(__first, __last, _MoveSegment<_InIter, _OutIter>(__result));
64 return std::make_pair(__last, std::move(__result));
65 }
66
67 template <class _InIter,
68 class _OutIter,
69 __enable_if_t<__has_random_access_iterator_category<_InIter>::value &&
70 !__is_segmented_iterator<_InIter>::value && __is_segmented_iterator<_OutIter>::value,
71 int> = 0>
operator__move_impl72 _LIBCPP_HIDE_FROM_ABI pair<_InIter, _OutIter> operator()(_InIter __first, _InIter __last, _OutIter __result) const {
73 using _Traits = __segmented_iterator_traits<_OutIter>;
74 using _DiffT = typename common_type<__iter_diff_t<_InIter>, __iter_diff_t<_OutIter> >::type;
75
76 if (__first == __last)
77 return std::make_pair(std::move(__first), std::move(__result));
78
79 auto __local_first = _Traits::__local(__result);
80 auto __segment_iterator = _Traits::__segment(__result);
81 while (true) {
82 auto __local_last = _Traits::__end(__segment_iterator);
83 auto __size = std::min<_DiffT>(__local_last - __local_first, __last - __first);
84 auto __iters = std::__move<_AlgPolicy>(__first, __first + __size, __local_first);
85 __first = std::move(__iters.first);
86
87 if (__first == __last)
88 return std::make_pair(std::move(__first), _Traits::__compose(__segment_iterator, std::move(__iters.second)));
89
90 __local_first = _Traits::__begin(++__segment_iterator);
91 }
92 }
93
94 // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
95 template <class _In, class _Out, __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
operator__move_impl96 _LIBCPP_HIDE_FROM_ABI pair<_In*, _Out*> operator()(_In* __first, _In* __last, _Out* __result) const {
97 return std::__copy_trivial_impl(__first, __last, __result);
98 }
99 };
100
101 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
__move(_InIter __first,_Sent __last,_OutIter __result)102 inline _LIBCPP_HIDE_FROM_ABI pair<_InIter, _OutIter> __move(_InIter __first, _Sent __last, _OutIter __result) {
103 return std::__copy_move_unwrap_iters<__move_impl<_AlgPolicy> >(
104 std::move(__first), std::move(__last), std::move(__result));
105 }
106
107 template <class _InputIterator, class _OutputIterator>
108 inline _LIBCPP_HIDE_FROM_ABI _OutputIterator
move(_InputIterator __first,_InputIterator __last,_OutputIterator __result)109 move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
110 static_assert(is_copy_constructible<_InputIterator>::value, "Iterators has to be copy constructible.");
111 static_assert(is_copy_constructible<_OutputIterator>::value, "The output iterator has to be copy constructible.");
112
113 return std::__move<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__result)).second;
114 }
115
116 _LIBCPP_END_NAMESPACE_STD
117
118 _LIBCPP_POP_MACROS
119
120 #endif // _LIBCPP___CXX03___ALGORITHM_MOVE_H
121