move_backward.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_BACKWARD_H
  9. #define _LIBCPP___ALGORITHM_MOVE_BACKWARD_H
  10. #include <__algorithm/copy_move_common.h>
  11. #include <__algorithm/iterator_operations.h>
  12. #include <__algorithm/min.h>
  13. #include <__config>
  14. #include <__iterator/segmented_iterator.h>
  15. #include <__type_traits/common_type.h>
  16. #include <__type_traits/is_copy_constructible.h>
  17. #include <__utility/move.h>
  18. #include <__utility/pair.h>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. template <class _AlgPolicy, class _BidirectionalIterator1, class _Sentinel, class _BidirectionalIterator2>
  26. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator1, _BidirectionalIterator2>
  27. __move_backward(_BidirectionalIterator1 __first, _Sentinel __last, _BidirectionalIterator2 __result);
  28. template <class _AlgPolicy>
  29. struct __move_backward_loop {
  30. template <class _InIter, class _Sent, class _OutIter>
  31. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
  32. operator()(_InIter __first, _Sent __last, _OutIter __result) const {
  33. auto __last_iter = _IterOps<_AlgPolicy>::next(__first, __last);
  34. auto __original_last_iter = __last_iter;
  35. while (__first != __last_iter) {
  36. *--__result = _IterOps<_AlgPolicy>::__iter_move(--__last_iter);
  37. }
  38. return std::make_pair(std::move(__original_last_iter), std::move(__result));
  39. }
  40. template <class _InIter, class _OutIter, __enable_if_t<__is_segmented_iterator<_InIter>::value, int> = 0>
  41. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
  42. operator()(_InIter __first, _InIter __last, _OutIter __result) const {
  43. using _Traits = __segmented_iterator_traits<_InIter>;
  44. auto __sfirst = _Traits::__segment(__first);
  45. auto __slast = _Traits::__segment(__last);
  46. if (__sfirst == __slast) {
  47. auto __iters =
  48. std::__move_backward<_AlgPolicy>(_Traits::__local(__first), _Traits::__local(__last), std::move(__result));
  49. return std::make_pair(__last, __iters.second);
  50. }
  51. __result =
  52. std::__move_backward<_AlgPolicy>(_Traits::__begin(__slast), _Traits::__local(__last), std::move(__result))
  53. .second;
  54. --__slast;
  55. while (__sfirst != __slast) {
  56. __result =
  57. std::__move_backward<_AlgPolicy>(_Traits::__begin(__slast), _Traits::__end(__slast), std::move(__result))
  58. .second;
  59. --__slast;
  60. }
  61. __result = std::__move_backward<_AlgPolicy>(_Traits::__local(__first), _Traits::__end(__slast), std::move(__result))
  62. .second;
  63. return std::make_pair(__last, std::move(__result));
  64. }
  65. template <class _InIter,
  66. class _OutIter,
  67. __enable_if_t<__has_random_access_iterator_category<_InIter>::value &&
  68. !__is_segmented_iterator<_InIter>::value && __is_segmented_iterator<_OutIter>::value,
  69. int> = 0>
  70. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
  71. operator()(_InIter __first, _InIter __last, _OutIter __result) const {
  72. using _Traits = __segmented_iterator_traits<_OutIter>;
  73. using _DiffT = typename common_type<__iter_diff_t<_InIter>, __iter_diff_t<_OutIter> >::type;
  74. // When the range contains no elements, __result might not be a valid iterator
  75. if (__first == __last)
  76. return std::make_pair(__first, __result);
  77. auto __orig_last = __last;
  78. auto __local_last = _Traits::__local(__result);
  79. auto __segment_iterator = _Traits::__segment(__result);
  80. while (true) {
  81. auto __local_first = _Traits::__begin(__segment_iterator);
  82. auto __size = std::min<_DiffT>(__local_last - __local_first, __last - __first);
  83. auto __iter = std::__move_backward<_AlgPolicy>(__last - __size, __last, __local_last).second;
  84. __last -= __size;
  85. if (__first == __last)
  86. return std::make_pair(std::move(__orig_last), _Traits::__compose(__segment_iterator, std::move(__iter)));
  87. __local_last = _Traits::__end(--__segment_iterator);
  88. }
  89. }
  90. };
  91. struct __move_backward_trivial {
  92. // At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
  93. template <class _In, class _Out,
  94. __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
  95. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
  96. operator()(_In* __first, _In* __last, _Out* __result) const {
  97. return std::__copy_backward_trivial_impl(__first, __last, __result);
  98. }
  99. };
  100. template <class _AlgPolicy, class _BidirectionalIterator1, class _Sentinel, class _BidirectionalIterator2>
  101. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator1, _BidirectionalIterator2>
  102. __move_backward(_BidirectionalIterator1 __first, _Sentinel __last, _BidirectionalIterator2 __result) {
  103. static_assert(std::is_copy_constructible<_BidirectionalIterator1>::value &&
  104. std::is_copy_constructible<_BidirectionalIterator1>::value, "Iterators must be copy constructible.");
  105. return std::__dispatch_copy_or_move<_AlgPolicy, __move_backward_loop<_AlgPolicy>, __move_backward_trivial>(
  106. std::move(__first), std::move(__last), std::move(__result));
  107. }
  108. template <class _BidirectionalIterator1, class _BidirectionalIterator2>
  109. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator2
  110. move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, _BidirectionalIterator2 __result) {
  111. return std::__move_backward<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__result)).second;
  112. }
  113. _LIBCPP_END_NAMESPACE_STD
  114. _LIBCPP_POP_MACROS
  115. #endif // _LIBCPP___ALGORITHM_MOVE_BACKWARD_H