1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___ITERATOR_ADVANCE_H
11 #define _LIBCPP___ITERATOR_ADVANCE_H
12
13 #include <__assert>
14 #include <__concepts/assignable.h>
15 #include <__concepts/same_as.h>
16 #include <__config>
17 #include <__iterator/concepts.h>
18 #include <__iterator/incrementable_traits.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__type_traits/enable_if.h>
21 #include <__type_traits/is_integral.h>
22 #include <__utility/convert_to_integral.h>
23 #include <__utility/declval.h>
24 #include <__utility/move.h>
25 #include <__utility/unreachable.h>
26 #include <limits>
27
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
30 #endif
31
32 _LIBCPP_BEGIN_NAMESPACE_STD
33
34 template <class _InputIter>
35 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
__advance(_InputIter & __i,typename iterator_traits<_InputIter>::difference_type __n,input_iterator_tag)36 void __advance(_InputIter& __i, typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag) {
37 for (; __n > 0; --__n)
38 ++__i;
39 }
40
41 template <class _BiDirIter>
42 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
__advance(_BiDirIter & __i,typename iterator_traits<_BiDirIter>::difference_type __n,bidirectional_iterator_tag)43 void __advance(_BiDirIter& __i, typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag) {
44 if (__n >= 0)
45 for (; __n > 0; --__n)
46 ++__i;
47 else
48 for (; __n < 0; ++__n)
49 --__i;
50 }
51
52 template <class _RandIter>
53 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
__advance(_RandIter & __i,typename iterator_traits<_RandIter>::difference_type __n,random_access_iterator_tag)54 void __advance(_RandIter& __i, typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag) {
55 __i += __n;
56 }
57
58 template <
59 class _InputIter, class _Distance,
60 class _IntegralDistance = decltype(_VSTD::__convert_to_integral(std::declval<_Distance>())),
61 class = __enable_if_t<is_integral<_IntegralDistance>::value> >
62 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
advance(_InputIter & __i,_Distance __orig_n)63 void advance(_InputIter& __i, _Distance __orig_n) {
64 typedef typename iterator_traits<_InputIter>::difference_type _Difference;
65 _Difference __n = static_cast<_Difference>(_VSTD::__convert_to_integral(__orig_n));
66 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
67 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
68 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
69 }
70
71 #if _LIBCPP_STD_VER >= 20
72
73 // [range.iter.op.advance]
74
75 namespace ranges {
76 namespace __advance {
77
78 struct __fn {
79 private:
80 template <class _Ip>
81 _LIBCPP_HIDE_FROM_ABI
__advance_forward__fn82 static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) {
83 while (__n > 0) {
84 --__n;
85 ++__i;
86 }
87 }
88
89 template <class _Ip>
90 _LIBCPP_HIDE_FROM_ABI
__advance_backward__fn91 static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) {
92 while (__n < 0) {
93 ++__n;
94 --__i;
95 }
96 }
97
98 public:
99 // Preconditions: If `I` does not model `bidirectional_iterator`, `n` is not negative.
100 template <input_or_output_iterator _Ip>
101 _LIBCPP_HIDE_FROM_ABI
operator__fn102 constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const {
103 _LIBCPP_ASSERT(__n >= 0 || bidirectional_iterator<_Ip>,
104 "If `n < 0`, then `bidirectional_iterator<I>` must be true.");
105
106 // If `I` models `random_access_iterator`, equivalent to `i += n`.
107 if constexpr (random_access_iterator<_Ip>) {
108 __i += __n;
109 return;
110 } else if constexpr (bidirectional_iterator<_Ip>) {
111 // Otherwise, if `n` is non-negative, increments `i` by `n`.
112 __advance_forward(__i, __n);
113 // Otherwise, decrements `i` by `-n`.
114 __advance_backward(__i, __n);
115 return;
116 } else {
117 // Otherwise, if `n` is non-negative, increments `i` by `n`.
118 __advance_forward(__i, __n);
119 return;
120 }
121 }
122
123 // Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel) denotes a range.
124 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
operator__fn125 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
126 // If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
127 if constexpr (assignable_from<_Ip&, _Sp>) {
128 __i = _VSTD::move(__bound_sentinel);
129 }
130 // Otherwise, if `S` and `I` model `sized_sentinel_for<S, I>`, equivalent to `ranges::advance(i, bound_sentinel - i)`.
131 else if constexpr (sized_sentinel_for<_Sp, _Ip>) {
132 (*this)(__i, __bound_sentinel - __i);
133 }
134 // Otherwise, while `bool(i != bound_sentinel)` is true, increments `i`.
135 else {
136 while (__i != __bound_sentinel) {
137 ++__i;
138 }
139 }
140 }
141
142 // Preconditions:
143 // * If `n > 0`, [i, bound_sentinel) denotes a range.
144 // * If `n == 0`, [i, bound_sentinel) or [bound_sentinel, i) denotes a range.
145 // * If `n < 0`, [bound_sentinel, i) denotes a range, `I` models `bidirectional_iterator`, and `I` and `S` model `same_as<I, S>`.
146 // Returns: `n - M`, where `M` is the difference between the ending and starting position.
147 template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
operator__fn148 _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Ip> operator()(_Ip& __i, iter_difference_t<_Ip> __n,
149 _Sp __bound_sentinel) const {
150 _LIBCPP_ASSERT((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>),
151 "If `n < 0`, then `bidirectional_iterator<I> && same_as<I, S>` must be true.");
152 // If `S` and `I` model `sized_sentinel_for<S, I>`:
153 if constexpr (sized_sentinel_for<_Sp, _Ip>) {
154 // If |n| >= |bound_sentinel - i|, equivalent to `ranges::advance(i, bound_sentinel)`.
155 // __magnitude_geq(a, b) returns |a| >= |b|, assuming they have the same sign.
156 auto __magnitude_geq = [](auto __a, auto __b) {
157 return __a == 0 ? __b == 0 :
158 __a > 0 ? __a >= __b :
159 __a <= __b;
160 };
161 if (const auto __m = __bound_sentinel - __i; __magnitude_geq(__n, __m)) {
162 (*this)(__i, __bound_sentinel);
163 return __n - __m;
164 }
165
166 // Otherwise, equivalent to `ranges::advance(i, n)`.
167 (*this)(__i, __n);
168 return 0;
169 } else {
170 // Otherwise, if `n` is non-negative, while `bool(i != bound_sentinel)` is true, increments `i` but at
171 // most `n` times.
172 while (__i != __bound_sentinel && __n > 0) {
173 ++__i;
174 --__n;
175 }
176
177 // Otherwise, while `bool(i != bound_sentinel)` is true, decrements `i` but at most `-n` times.
178 if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) {
179 while (__i != __bound_sentinel && __n < 0) {
180 --__i;
181 ++__n;
182 }
183 }
184 return __n;
185 }
186
187 __libcpp_unreachable();
188 }
189 };
190
191 } // namespace __advance
192
193 inline namespace __cpo {
194 inline constexpr auto advance = __advance::__fn{};
195 } // namespace __cpo
196 } // namespace ranges
197
198 #endif // _LIBCPP_STD_VER >= 20
199
200 _LIBCPP_END_NAMESPACE_STD
201
202 #endif // _LIBCPP___ITERATOR_ADVANCE_H
203