shift_right.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_SHIFT_RIGHT_H
  9. #define _LIBCPP___ALGORITHM_SHIFT_RIGHT_H
  10. #include <__algorithm/move.h>
  11. #include <__algorithm/move_backward.h>
  12. #include <__algorithm/swap_ranges.h>
  13. #include <__config>
  14. #include <__iterator/iterator_traits.h>
  15. #include <__utility/swap.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_PUSH_MACROS
  20. #include <__undef_macros>
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. #if _LIBCPP_STD_VER >= 20
  23. template <class _ForwardIterator>
  24. inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
  25. shift_right(_ForwardIterator __first,
  26. _ForwardIterator __last,
  27. typename iterator_traits<_ForwardIterator>::difference_type __n) {
  28. if (__n == 0) {
  29. return __first;
  30. }
  31. if constexpr (__has_random_access_iterator_category<_ForwardIterator>::value) {
  32. decltype(__n) __d = __last - __first;
  33. if (__n >= __d) {
  34. return __last;
  35. }
  36. _ForwardIterator __m = __first + (__d - __n);
  37. return std::move_backward(__first, __m, __last);
  38. } else if constexpr (__has_bidirectional_iterator_category<_ForwardIterator>::value) {
  39. _ForwardIterator __m = __last;
  40. for (; __n > 0; --__n) {
  41. if (__m == __first) {
  42. return __last;
  43. }
  44. --__m;
  45. }
  46. return std::move_backward(__first, __m, __last);
  47. } else {
  48. _ForwardIterator __ret = __first;
  49. for (; __n > 0; --__n) {
  50. if (__ret == __last) {
  51. return __last;
  52. }
  53. ++__ret;
  54. }
  55. // We have an __n-element scratch space from __first to __ret.
  56. // Slide an __n-element window [__trail, __lead) from left to right.
  57. // We're essentially doing swap_ranges(__first, __ret, __trail, __lead)
  58. // over and over; but once __lead reaches __last we needn't bother
  59. // to save the values of elements [__trail, __last).
  60. auto __trail = __first;
  61. auto __lead = __ret;
  62. while (__trail != __ret) {
  63. if (__lead == __last) {
  64. std::move(__first, __trail, __ret);
  65. return __ret;
  66. }
  67. ++__trail;
  68. ++__lead;
  69. }
  70. _ForwardIterator __mid = __first;
  71. while (true) {
  72. if (__lead == __last) {
  73. __trail = std::move(__mid, __ret, __trail);
  74. std::move(__first, __mid, __trail);
  75. return __ret;
  76. }
  77. swap(*__mid, *__trail);
  78. ++__mid;
  79. ++__trail;
  80. ++__lead;
  81. if (__mid == __ret) {
  82. __mid = __first;
  83. }
  84. }
  85. }
  86. }
  87. #endif // _LIBCPP_STD_VER >= 20
  88. _LIBCPP_END_NAMESPACE_STD
  89. _LIBCPP_POP_MACROS
  90. #endif // _LIBCPP___ALGORITHM_SHIFT_RIGHT_H