xref: /llvm-project/libcxx/include/__cxx03/__algorithm/move.h (revision ce7771902dc50d900de639d499a60486b83f70e0)
1e78f53d1SNikolas Klauser //===----------------------------------------------------------------------===//
2e78f53d1SNikolas Klauser //
3e78f53d1SNikolas Klauser // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e78f53d1SNikolas Klauser // See https://llvm.org/LICENSE.txt for license information.
5e78f53d1SNikolas Klauser // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e78f53d1SNikolas Klauser //
7e78f53d1SNikolas Klauser //===----------------------------------------------------------------------===//
8e78f53d1SNikolas Klauser 
9*ce777190SNikolas Klauser #ifndef _LIBCPP___CXX03___ALGORITHM_MOVE_H
10*ce777190SNikolas Klauser #define _LIBCPP___CXX03___ALGORITHM_MOVE_H
11e78f53d1SNikolas Klauser 
1273fbae83SNikolas Klauser #include <__cxx03/__algorithm/copy_move_common.h>
1373fbae83SNikolas Klauser #include <__cxx03/__algorithm/for_each_segment.h>
1473fbae83SNikolas Klauser #include <__cxx03/__algorithm/iterator_operations.h>
1573fbae83SNikolas Klauser #include <__cxx03/__algorithm/min.h>
1673fbae83SNikolas Klauser #include <__cxx03/__config>
1773fbae83SNikolas Klauser #include <__cxx03/__iterator/segmented_iterator.h>
1873fbae83SNikolas Klauser #include <__cxx03/__type_traits/common_type.h>
1973fbae83SNikolas Klauser #include <__cxx03/__type_traits/is_constructible.h>
2073fbae83SNikolas Klauser #include <__cxx03/__utility/move.h>
2173fbae83SNikolas Klauser #include <__cxx03/__utility/pair.h>
22e78f53d1SNikolas Klauser 
23e78f53d1SNikolas Klauser #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24e78f53d1SNikolas Klauser #  pragma GCC system_header
25e78f53d1SNikolas Klauser #endif
26e78f53d1SNikolas Klauser 
27e78f53d1SNikolas Klauser _LIBCPP_PUSH_MACROS
2873fbae83SNikolas Klauser #include <__cxx03/__undef_macros>
29e78f53d1SNikolas Klauser 
30e78f53d1SNikolas Klauser _LIBCPP_BEGIN_NAMESPACE_STD
31e78f53d1SNikolas Klauser 
32e78f53d1SNikolas Klauser template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
33e78f53d1SNikolas Klauser inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
34e78f53d1SNikolas Klauser __move(_InIter __first, _Sent __last, _OutIter __result);
35e78f53d1SNikolas Klauser 
36e78f53d1SNikolas Klauser template <class _AlgPolicy>
37e78f53d1SNikolas Klauser struct __move_impl {
38e78f53d1SNikolas Klauser   template <class _InIter, class _Sent, class _OutIter>
39e78f53d1SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
40e78f53d1SNikolas Klauser   operator()(_InIter __first, _Sent __last, _OutIter __result) const {
41e78f53d1SNikolas Klauser     while (__first != __last) {
42e78f53d1SNikolas Klauser       *__result = _IterOps<_AlgPolicy>::__iter_move(__first);
43e78f53d1SNikolas Klauser       ++__first;
44e78f53d1SNikolas Klauser       ++__result;
45e78f53d1SNikolas Klauser     }
46e78f53d1SNikolas Klauser     return std::make_pair(std::move(__first), std::move(__result));
47e78f53d1SNikolas Klauser   }
48e78f53d1SNikolas Klauser 
49e78f53d1SNikolas Klauser   template <class _InIter, class _OutIter>
50e78f53d1SNikolas Klauser   struct _MoveSegment {
51e78f53d1SNikolas Klauser     using _Traits = __segmented_iterator_traits<_InIter>;
52e78f53d1SNikolas Klauser 
53e78f53d1SNikolas Klauser     _OutIter& __result_;
54e78f53d1SNikolas Klauser 
55e78f53d1SNikolas Klauser     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit _MoveSegment(_OutIter& __result)
56e78f53d1SNikolas Klauser         : __result_(__result) {}
57e78f53d1SNikolas Klauser 
58e78f53d1SNikolas Klauser     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
59e78f53d1SNikolas Klauser     operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {
60e78f53d1SNikolas Klauser       __result_ = std::__move<_AlgPolicy>(__lfirst, __llast, std::move(__result_)).second;
61e78f53d1SNikolas Klauser     }
62e78f53d1SNikolas Klauser   };
63e78f53d1SNikolas Klauser 
64e78f53d1SNikolas Klauser   template <class _InIter, class _OutIter, __enable_if_t<__is_segmented_iterator<_InIter>::value, int> = 0>
65e78f53d1SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
66e78f53d1SNikolas Klauser   operator()(_InIter __first, _InIter __last, _OutIter __result) const {
67e78f53d1SNikolas Klauser     std::__for_each_segment(__first, __last, _MoveSegment<_InIter, _OutIter>(__result));
68e78f53d1SNikolas Klauser     return std::make_pair(__last, std::move(__result));
69e78f53d1SNikolas Klauser   }
70e78f53d1SNikolas Klauser 
71e78f53d1SNikolas Klauser   template <class _InIter,
72e78f53d1SNikolas Klauser             class _OutIter,
73e78f53d1SNikolas Klauser             __enable_if_t<__has_random_access_iterator_category<_InIter>::value &&
74e78f53d1SNikolas Klauser                               !__is_segmented_iterator<_InIter>::value && __is_segmented_iterator<_OutIter>::value,
75e78f53d1SNikolas Klauser                           int> = 0>
76e78f53d1SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
77e78f53d1SNikolas Klauser   operator()(_InIter __first, _InIter __last, _OutIter __result) const {
78e78f53d1SNikolas Klauser     using _Traits = __segmented_iterator_traits<_OutIter>;
79e78f53d1SNikolas Klauser     using _DiffT  = typename common_type<__iter_diff_t<_InIter>, __iter_diff_t<_OutIter> >::type;
80e78f53d1SNikolas Klauser 
81e78f53d1SNikolas Klauser     if (__first == __last)
82e78f53d1SNikolas Klauser       return std::make_pair(std::move(__first), std::move(__result));
83e78f53d1SNikolas Klauser 
84e78f53d1SNikolas Klauser     auto __local_first      = _Traits::__local(__result);
85e78f53d1SNikolas Klauser     auto __segment_iterator = _Traits::__segment(__result);
86e78f53d1SNikolas Klauser     while (true) {
87e78f53d1SNikolas Klauser       auto __local_last = _Traits::__end(__segment_iterator);
88e78f53d1SNikolas Klauser       auto __size       = std::min<_DiffT>(__local_last - __local_first, __last - __first);
89e78f53d1SNikolas Klauser       auto __iters      = std::__move<_AlgPolicy>(__first, __first + __size, __local_first);
90e78f53d1SNikolas Klauser       __first           = std::move(__iters.first);
91e78f53d1SNikolas Klauser 
92e78f53d1SNikolas Klauser       if (__first == __last)
93e78f53d1SNikolas Klauser         return std::make_pair(std::move(__first), _Traits::__compose(__segment_iterator, std::move(__iters.second)));
94e78f53d1SNikolas Klauser 
95e78f53d1SNikolas Klauser       __local_first = _Traits::__begin(++__segment_iterator);
96e78f53d1SNikolas Klauser     }
97e78f53d1SNikolas Klauser   }
98e78f53d1SNikolas Klauser 
99e78f53d1SNikolas Klauser   // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
100e78f53d1SNikolas Klauser   template <class _In, class _Out, __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
101e78f53d1SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
102e78f53d1SNikolas Klauser   operator()(_In* __first, _In* __last, _Out* __result) const {
103e78f53d1SNikolas Klauser     return std::__copy_trivial_impl(__first, __last, __result);
104e78f53d1SNikolas Klauser   }
105e78f53d1SNikolas Klauser };
106e78f53d1SNikolas Klauser 
107e78f53d1SNikolas Klauser template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
108e78f53d1SNikolas Klauser inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
109e78f53d1SNikolas Klauser __move(_InIter __first, _Sent __last, _OutIter __result) {
110e78f53d1SNikolas Klauser   return std::__copy_move_unwrap_iters<__move_impl<_AlgPolicy> >(
111e78f53d1SNikolas Klauser       std::move(__first), std::move(__last), std::move(__result));
112e78f53d1SNikolas Klauser }
113e78f53d1SNikolas Klauser 
114e78f53d1SNikolas Klauser template <class _InputIterator, class _OutputIterator>
115e78f53d1SNikolas Klauser inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
116e78f53d1SNikolas Klauser move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
117e78f53d1SNikolas Klauser   static_assert(is_copy_constructible<_InputIterator>::value, "Iterators has to be copy constructible.");
118e78f53d1SNikolas Klauser   static_assert(is_copy_constructible<_OutputIterator>::value, "The output iterator has to be copy constructible.");
119e78f53d1SNikolas Klauser 
120e78f53d1SNikolas Klauser   return std::__move<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__result)).second;
121e78f53d1SNikolas Klauser }
122e78f53d1SNikolas Klauser 
123e78f53d1SNikolas Klauser _LIBCPP_END_NAMESPACE_STD
124e78f53d1SNikolas Klauser 
125e78f53d1SNikolas Klauser _LIBCPP_POP_MACROS
126e78f53d1SNikolas Klauser 
127*ce777190SNikolas Klauser #endif // _LIBCPP___CXX03___ALGORITHM_MOVE_H
128