advance.h 7.2 KB

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