move.h 5.3 KB

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