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