advance.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 void
  33. __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 void
  39. __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 void
  49. __advance(_RandIter& __i, typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag) {
  50. __i += __n;
  51. }
  52. template < class _InputIter,
  53. class _Distance,
  54. class _IntegralDistance = decltype(std::__convert_to_integral(std::declval<_Distance>())),
  55. __enable_if_t<is_integral<_IntegralDistance>::value, int> = 0>
  56. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void advance(_InputIter& __i, _Distance __orig_n) {
  57. typedef typename iterator_traits<_InputIter>::difference_type _Difference;
  58. _Difference __n = static_cast<_Difference>(std::__convert_to_integral(__orig_n));
  59. // Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.
  60. _LIBCPP_ASSERT_PEDANTIC(__n >= 0 || __has_bidirectional_iterator_category<_InputIter>::value,
  61. "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
  62. std::__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 static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) {
  72. while (__n > 0) {
  73. --__n;
  74. ++__i;
  75. }
  76. }
  77. template <class _Ip>
  78. _LIBCPP_HIDE_FROM_ABI static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) {
  79. while (__n < 0) {
  80. ++__n;
  81. --__i;
  82. }
  83. }
  84. public:
  85. // Preconditions: If `I` does not model `bidirectional_iterator`, `n` is not negative.
  86. template <input_or_output_iterator _Ip>
  87. _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const {
  88. // Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.
  89. _LIBCPP_ASSERT_PEDANTIC(
  90. __n >= 0 || bidirectional_iterator<_Ip>, "If `n < 0`, then `bidirectional_iterator<I>` must be true.");
  91. // If `I` models `random_access_iterator`, equivalent to `i += n`.
  92. if constexpr (random_access_iterator<_Ip>) {
  93. __i += __n;
  94. return;
  95. } else if constexpr (bidirectional_iterator<_Ip>) {
  96. // Otherwise, if `n` is non-negative, increments `i` by `n`.
  97. __advance_forward(__i, __n);
  98. // Otherwise, decrements `i` by `-n`.
  99. __advance_backward(__i, __n);
  100. return;
  101. } else {
  102. // Otherwise, if `n` is non-negative, increments `i` by `n`.
  103. __advance_forward(__i, __n);
  104. return;
  105. }
  106. }
  107. // Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel)
  108. // denotes a range.
  109. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  110. _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp ___bound_sentinel) const {
  111. // If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
  112. if constexpr (assignable_from<_Ip&, _Sp>) {
  113. __i = std::move(___bound_sentinel);
  114. }
  115. // Otherwise, if `S` and `I` model `sized_sentinel_for<S, I>`, equivalent to `ranges::advance(i, bound_sentinel -
  116. // 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
  131. // `same_as<I, S>`.
  132. // Returns: `n - M`, where `M` is the difference between the ending and starting position.
  133. template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
  134. _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Ip>
  135. operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp ___bound_sentinel) const {
  136. // Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.
  137. _LIBCPP_ASSERT_PEDANTIC((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>),
  138. "If `n < 0`, then `bidirectional_iterator<I> && same_as<I, S>` must be true.");
  139. // If `S` and `I` model `sized_sentinel_for<S, I>`:
  140. if constexpr (sized_sentinel_for<_Sp, _Ip>) {
  141. // If |n| >= |bound_sentinel - i|, equivalent to `ranges::advance(i, bound_sentinel)`.
  142. // __magnitude_geq(a, b) returns |a| >= |b|, assuming they have the same sign.
  143. auto __magnitude_geq = [](auto __a, auto __b) { return __a == 0 ? __b == 0 : __a > 0 ? __a >= __b : __a <= __b; };
  144. if (const auto __m = ___bound_sentinel - __i; __magnitude_geq(__n, __m)) {
  145. (*this)(__i, ___bound_sentinel);
  146. return __n - __m;
  147. }
  148. // Otherwise, equivalent to `ranges::advance(i, n)`.
  149. (*this)(__i, __n);
  150. return 0;
  151. } else {
  152. // Otherwise, if `n` is non-negative, while `bool(i != bound_sentinel)` is true, increments `i` but at
  153. // most `n` times.
  154. while (__i != ___bound_sentinel && __n > 0) {
  155. ++__i;
  156. --__n;
  157. }
  158. // Otherwise, while `bool(i != bound_sentinel)` is true, decrements `i` but at most `-n` times.
  159. if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) {
  160. while (__i != ___bound_sentinel && __n < 0) {
  161. --__i;
  162. ++__n;
  163. }
  164. }
  165. return __n;
  166. }
  167. __libcpp_unreachable();
  168. }
  169. };
  170. } // namespace __advance
  171. inline namespace __cpo {
  172. inline constexpr auto advance = __advance::__fn{};
  173. } // namespace __cpo
  174. } // namespace ranges
  175. #endif // _LIBCPP_STD_VER >= 20
  176. _LIBCPP_END_NAMESPACE_STD
  177. _LIBCPP_POP_MACROS
  178. #endif // _LIBCPP___ITERATOR_ADVANCE_H