move.h 4.9 KB

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