Mercurial > hg > CbC > CbC_llvm
comparison libcxx/include/__algorithm/shift.h @ 221:79ff65ed7e25
LLVM12 Original
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 15 Jun 2021 19:15:29 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
220:42394fc6a535 | 221:79ff65ed7e25 |
---|---|
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_H | |
10 #define _LIBCPP___ALGORITHM_SHIFT_H | |
11 | |
12 #include <__config> | |
13 #include <__algorithm/move.h> | |
14 #include <__iterator/iterator_traits.h> | |
15 | |
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | |
17 #pragma GCC system_header | |
18 #endif | |
19 | |
20 _LIBCPP_PUSH_MACROS | |
21 #include <__undef_macros> | |
22 | |
23 _LIBCPP_BEGIN_NAMESPACE_STD | |
24 | |
25 // shift_left, shift_right | |
26 | |
27 #if _LIBCPP_STD_VER > 17 | |
28 | |
29 template <class _ForwardIterator> | |
30 inline _LIBCPP_INLINE_VISIBILITY constexpr | |
31 _ForwardIterator | |
32 shift_left(_ForwardIterator __first, _ForwardIterator __last, | |
33 typename iterator_traits<_ForwardIterator>::difference_type __n) | |
34 { | |
35 if (__n == 0) { | |
36 return __last; | |
37 } | |
38 | |
39 _ForwardIterator __m = __first; | |
40 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) { | |
41 if (__n >= __last - __first) { | |
42 return __first; | |
43 } | |
44 __m += __n; | |
45 } else { | |
46 for (; __n > 0; --__n) { | |
47 if (__m == __last) { | |
48 return __first; | |
49 } | |
50 ++__m; | |
51 } | |
52 } | |
53 return _VSTD::move(__m, __last, __first); | |
54 } | |
55 | |
56 template <class _ForwardIterator> | |
57 inline _LIBCPP_INLINE_VISIBILITY constexpr | |
58 _ForwardIterator | |
59 shift_right(_ForwardIterator __first, _ForwardIterator __last, | |
60 typename iterator_traits<_ForwardIterator>::difference_type __n) | |
61 { | |
62 if (__n == 0) { | |
63 return __first; | |
64 } | |
65 | |
66 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) { | |
67 decltype(__n) __d = __last - __first; | |
68 if (__n >= __d) { | |
69 return __last; | |
70 } | |
71 _ForwardIterator __m = __first + (__d - __n); | |
72 return _VSTD::move_backward(__first, __m, __last); | |
73 } else if constexpr (__is_cpp17_bidirectional_iterator<_ForwardIterator>::value) { | |
74 _ForwardIterator __m = __last; | |
75 for (; __n > 0; --__n) { | |
76 if (__m == __first) { | |
77 return __last; | |
78 } | |
79 --__m; | |
80 } | |
81 return _VSTD::move_backward(__first, __m, __last); | |
82 } else { | |
83 _ForwardIterator __ret = __first; | |
84 for (; __n > 0; --__n) { | |
85 if (__ret == __last) { | |
86 return __last; | |
87 } | |
88 ++__ret; | |
89 } | |
90 | |
91 // We have an __n-element scratch space from __first to __ret. | |
92 // Slide an __n-element window [__trail, __lead) from left to right. | |
93 // We're essentially doing swap_ranges(__first, __ret, __trail, __lead) | |
94 // over and over; but once __lead reaches __last we needn't bother | |
95 // to save the values of elements [__trail, __last). | |
96 | |
97 auto __trail = __first; | |
98 auto __lead = __ret; | |
99 while (__trail != __ret) { | |
100 if (__lead == __last) { | |
101 _VSTD::move(__first, __trail, __ret); | |
102 return __ret; | |
103 } | |
104 ++__trail; | |
105 ++__lead; | |
106 } | |
107 | |
108 _ForwardIterator __mid = __first; | |
109 while (true) { | |
110 if (__lead == __last) { | |
111 __trail = _VSTD::move(__mid, __ret, __trail); | |
112 _VSTD::move(__first, __mid, __trail); | |
113 return __ret; | |
114 } | |
115 swap(*__mid, *__trail); | |
116 ++__mid; | |
117 ++__trail; | |
118 ++__lead; | |
119 if (__mid == __ret) { | |
120 __mid = __first; | |
121 } | |
122 } | |
123 } | |
124 } | |
125 | |
126 #endif // _LIBCPP_STD_VER > 17 | |
127 | |
128 _LIBCPP_END_NAMESPACE_STD | |
129 | |
130 _LIBCPP_POP_MACROS | |
131 | |
132 #endif // _LIBCPP___ALGORITHM_SHIFT_H |