xref: /freebsd/contrib/llvm-project/libcxx/include/__iterator/advance.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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 <__function_like.h>
16 #include <__iterator/concepts.h>
17 #include <__iterator/incrementable_traits.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__utility/move.h>
20 #include <cstdlib>
21 #include <concepts>
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_PUSH_MACROS
30 #include <__undef_macros>
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 template <class _InputIter>
35 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
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_AFTER_CXX14
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_AFTER_CXX14
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(declval<_Distance>())),
61     class = _EnableIf<is_integral<_IntegralDistance>::value> >
62 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
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 !defined(_LIBCPP_HAS_NO_RANGES)
72 
73 namespace ranges {
74 // [range.iter.op.advance]
75 struct __advance_fn final : private __function_like {
76 private:
77   template <class _Tp>
78   _LIBCPP_HIDE_FROM_ABI
79   static constexpr _Tp __magnitude_geq(_Tp __a, _Tp __b) noexcept {
80     return __a < 0 ? (__a <= __b) : (__a >= __b);
81   }
82 
83   template <class _Ip>
84   _LIBCPP_HIDE_FROM_ABI
85   static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) {
86     while (__n > 0) {
87       --__n;
88       ++__i;
89     }
90   }
91 
92   template <class _Ip>
93   _LIBCPP_HIDE_FROM_ABI
94   static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) {
95     while (__n < 0) {
96       ++__n;
97       --__i;
98     }
99   }
100 
101 public:
102   constexpr explicit __advance_fn(__tag __x) noexcept : __function_like(__x) {}
103 
104   // Preconditions: If `I` does not model `bidirectional_iterator`, `n` is not negative.
105   template <input_or_output_iterator _Ip>
106   _LIBCPP_HIDE_FROM_ABI
107   constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const {
108     _LIBCPP_ASSERT(__n >= 0 || bidirectional_iterator<_Ip>,
109                    "If `n < 0`, then `bidirectional_iterator<I>` must be true.");
110 
111     // If `I` models `random_access_iterator`, equivalent to `i += n`.
112     if constexpr (random_access_iterator<_Ip>) {
113       __i += __n;
114       return;
115     } else if constexpr (bidirectional_iterator<_Ip>) {
116       // Otherwise, if `n` is non-negative, increments `i` by `n`.
117       __advance_forward(__i, __n);
118       // Otherwise, decrements `i` by `-n`.
119       __advance_backward(__i, __n);
120       return;
121     } else {
122       // Otherwise, if `n` is non-negative, increments `i` by `n`.
123       __advance_forward(__i, __n);
124       return;
125     }
126   }
127 
128   // Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound) denotes a range.
129   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
130   _LIBCPP_HIDE_FROM_ABI
131   constexpr void operator()(_Ip& __i, _Sp __bound) const {
132     // If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound)`.
133     if constexpr (assignable_from<_Ip&, _Sp>) {
134       __i = _VSTD::move(__bound);
135     }
136     // Otherwise, if `S` and `I` model `sized_sentinel_for<S, I>`, equivalent to `ranges::advance(i, bound - i)`.
137     else if constexpr (sized_sentinel_for<_Sp, _Ip>) {
138       (*this)(__i, __bound - __i);
139     }
140     // Otherwise, while `bool(i != bound)` is true, increments `i`.
141     else {
142       while (__i != __bound) {
143         ++__i;
144       }
145     }
146   }
147 
148   // Preconditions:
149   //   * If `n > 0`, [i, bound) denotes a range.
150   //   * If `n == 0`, [i, bound) or [bound, i) denotes a range.
151   //   * If `n < 0`, [bound, i) denotes a range, `I` models `bidirectional_iterator`, and `I` and `S` model `same_as<I, S>`.
152   // Returns: `n - M`, where `M` is the difference between the the ending and starting position.
153   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
154   _LIBCPP_HIDE_FROM_ABI
155   constexpr iter_difference_t<_Ip> operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp __bound) const {
156     _LIBCPP_ASSERT((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>),
157                    "If `n < 0`, then `bidirectional_iterator<I> && same_as<I, S>` must be true.");
158     // If `S` and `I` model `sized_sentinel_for<S, I>`:
159     if constexpr (sized_sentinel_for<_Sp, _Ip>) {
160       // If |n| >= |bound - i|, equivalent to `ranges::advance(i, bound)`.
161       if (const auto __M = __bound - __i; __magnitude_geq(__n, __M)) {
162         (*this)(__i, __bound);
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)` is true, increments `i` but at
171       // most `n` times.
172       while (__i != __bound && __n > 0) {
173         ++__i;
174         --__n;
175       }
176 
177       // Otherwise, while `bool(i != bound)` is true, decrements `i` but at most `-n` times.
178       if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) {
179         while (__i != __bound && __n < 0) {
180           --__i;
181           ++__n;
182         }
183       }
184       return __n;
185     }
186 
187     _LIBCPP_UNREACHABLE();
188   }
189 };
190 
191 inline constexpr auto advance = __advance_fn(__function_like::__tag());
192 } // namespace ranges
193 
194 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
195 
196 _LIBCPP_END_NAMESPACE_STD
197 
198 _LIBCPP_POP_MACROS
199 
200 #endif // _LIBCPP___ITERATOR_ADVANCE_H
201