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/iterator_operations.h> 13 #include <__algorithm/unwrap_iter.h> 14 #include <__config> 15 #include <__iterator/iterator_traits.h> 16 #include <__iterator/reverse_iterator.h> 17 #include <__utility/move.h> 18 #include <__utility/pair.h> 19 #include <cstring> 20 #include <type_traits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 # pragma GCC system_header 24 #endif 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 28 // move 29 30 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> 31 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 32 pair<_InIter, _OutIter> __move_impl(_InIter __first, _Sent __last, _OutIter __result) { 33 while (__first != __last) { 34 *__result = _IterOps<_AlgPolicy>::__iter_move(__first); 35 ++__first; 36 ++__result; 37 } 38 return std::make_pair(std::move(__first), std::move(__result)); 39 } 40 41 template <class _AlgPolicy, 42 class _InType, 43 class _OutType, 44 class = __enable_if_t<is_same<typename remove_const<_InType>::type, _OutType>::value 45 && is_trivially_move_assignable<_OutType>::value> > 46 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 47 pair<_InType*, _OutType*> __move_impl(_InType* __first, _InType* __last, _OutType* __result) { 48 if (__libcpp_is_constant_evaluated() 49 // TODO: Remove this once GCC supports __builtin_memmove during constant evaluation 50 #ifndef _LIBCPP_COMPILER_GCC 51 && !is_trivially_copyable<_InType>::value 52 #endif 53 ) 54 return std::__move_impl<_AlgPolicy, _InType*, _InType*, _OutType*>(__first, __last, __result); 55 const size_t __n = static_cast<size_t>(__last - __first); 56 ::__builtin_memmove(__result, __first, __n * sizeof(_OutType)); 57 return std::make_pair(__first + __n, __result + __n); 58 } 59 60 template <class> 61 struct __is_trivially_move_assignable_unwrapped_impl : false_type {}; 62 63 template <class _Type> 64 struct __is_trivially_move_assignable_unwrapped_impl<_Type*> : is_trivially_move_assignable<_Type> {}; 65 66 template <class _Iter> 67 struct __is_trivially_move_assignable_unwrapped 68 : __is_trivially_move_assignable_unwrapped_impl<decltype(std::__unwrap_iter<_Iter>(std::declval<_Iter>()))> {}; 69 70 template <class _AlgPolicy, 71 class _InIter, 72 class _OutIter, 73 __enable_if_t<is_same<typename remove_const<typename iterator_traits<_InIter>::value_type>::type, 74 typename iterator_traits<_OutIter>::value_type>::value 75 && __is_cpp17_contiguous_iterator<_InIter>::value 76 && __is_cpp17_contiguous_iterator<_OutIter>::value 77 && is_trivially_move_assignable<__iter_value_type<_OutIter> >::value, int> = 0> 78 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14 79 pair<reverse_iterator<_InIter>, reverse_iterator<_OutIter> > 80 __move_impl(reverse_iterator<_InIter> __first, 81 reverse_iterator<_InIter> __last, 82 reverse_iterator<_OutIter> __result) { 83 auto __first_base = std::__unwrap_iter(__first.base()); 84 auto __last_base = std::__unwrap_iter(__last.base()); 85 auto __result_base = std::__unwrap_iter(__result.base()); 86 auto __result_first = __result_base - (__first_base - __last_base); 87 std::__move_impl<_AlgPolicy>(__last_base, __first_base, __result_first); 88 return std::make_pair(__last, reverse_iterator<_OutIter>(std::__rewrap_iter(__result.base(), __result_first))); 89 } 90 91 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> 92 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 93 __enable_if_t<is_copy_constructible<_InIter>::value 94 && is_copy_constructible<_Sent>::value 95 && is_copy_constructible<_OutIter>::value, pair<_InIter, _OutIter> > 96 __move(_InIter __first, _Sent __last, _OutIter __result) { 97 auto __ret = std::__move_impl<_AlgPolicy>( 98 std::__unwrap_iter(__first), std::__unwrap_iter(__last), std::__unwrap_iter(__result)); 99 return std::make_pair(std::__rewrap_iter(__first, __ret.first), std::__rewrap_iter(__result, __ret.second)); 100 } 101 102 template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter> 103 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 104 __enable_if_t<!is_copy_constructible<_InIter>::value 105 || !is_copy_constructible<_Sent>::value 106 || !is_copy_constructible<_OutIter>::value, pair<_InIter, _OutIter> > 107 __move(_InIter __first, _Sent __last, _OutIter __result) { 108 return std::__move_impl<_AlgPolicy>(std::move(__first), std::move(__last), std::move(__result)); 109 } 110 111 template <class _InputIterator, class _OutputIterator> 112 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 113 _OutputIterator move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) { 114 return std::__move<_ClassicAlgPolicy>(__first, __last, __result).second; 115 } 116 117 _LIBCPP_END_NAMESPACE_STD 118 119 #endif // _LIBCPP___ALGORITHM_MOVE_H 120