advance.h 6.8 KB

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