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