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