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