shift_right.h 3.1 KB

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