| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef _LIBCPP___ITERATOR_ADVANCE_H |
| #define _LIBCPP___ITERATOR_ADVANCE_H |
|
|
| #include <__assert> |
| #include <__concepts/assignable.h> |
| #include <__concepts/same_as.h> |
| #include <__config> |
| #include <__iterator/concepts.h> |
| #include <__iterator/incrementable_traits.h> |
| #include <__iterator/iterator_traits.h> |
| #include <__type_traits/enable_if.h> |
| #include <__type_traits/is_integral.h> |
| #include <__utility/convert_to_integral.h> |
| #include <__utility/declval.h> |
| #include <__utility/move.h> |
| #include <__utility/unreachable.h> |
| #include <limits> |
|
|
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| # pragma GCC system_header |
| #endif |
|
|
| _LIBCPP_PUSH_MACROS |
| #include <__undef_macros> |
|
|
| _LIBCPP_BEGIN_NAMESPACE_STD |
|
|
| template <class _InputIter> |
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void |
| __advance(_InputIter& __i, typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag) { |
| for (; __n > 0; --__n) |
| ++__i; |
| } |
|
|
| template <class _BiDirIter> |
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void |
| __advance(_BiDirIter& __i, typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag) { |
| if (__n >= 0) |
| for (; __n > 0; --__n) |
| ++__i; |
| else |
| for (; __n < 0; ++__n) |
| --__i; |
| } |
|
|
| template <class _RandIter> |
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void |
| __advance(_RandIter& __i, typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag) { |
| __i += __n; |
| } |
|
|
| template < class _InputIter, |
| class _Distance, |
| class _IntegralDistance = decltype(std::__convert_to_integral(std::declval<_Distance>())), |
| __enable_if_t<is_integral<_IntegralDistance>::value, int> = 0> |
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void advance(_InputIter& __i, _Distance __orig_n) { |
| typedef typename iterator_traits<_InputIter>::difference_type _Difference; |
| _Difference __n = static_cast<_Difference>(std::__convert_to_integral(__orig_n)); |
| _LIBCPP_ASSERT_PEDANTIC(__has_bidirectional_iterator_category<_InputIter>::value || __n >= 0, |
| "std::advance: Can only pass a negative `n` with a bidirectional_iterator."); |
| std::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category()); |
| } |
|
|
| #if _LIBCPP_STD_VER >= 20 |
|
|
| |
|
|
| namespace ranges { |
| struct __advance { |
| private: |
| template <class _Ip> |
| _LIBCPP_HIDE_FROM_ABI static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) { |
| while (__n > 0) { |
| --__n; |
| ++__i; |
| } |
| } |
|
|
| template <class _Ip> |
| _LIBCPP_HIDE_FROM_ABI static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) { |
| while (__n < 0) { |
| ++__n; |
| --__i; |
| } |
| } |
|
|
| public: |
| |
| template <input_or_output_iterator _Ip> |
| _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, iter_difference_t<_Ip> __n) const { |
| _LIBCPP_ASSERT_PEDANTIC(bidirectional_iterator<_Ip> || __n >= 0, |
| "ranges::advance: Can only pass a negative `n` with a bidirectional_iterator."); |
|
|
| |
| if constexpr (random_access_iterator<_Ip>) { |
| __i += __n; |
| return; |
| } else if constexpr (bidirectional_iterator<_Ip>) { |
| |
| __advance_forward(__i, __n); |
| |
| __advance_backward(__i, __n); |
| return; |
| } else { |
| |
| __advance_forward(__i, __n); |
| return; |
| } |
| } |
|
|
| |
| |
| template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp> |
| _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const { |
| |
| if constexpr (assignable_from<_Ip&, _Sp>) { |
| __i = std::move(__bound_sentinel); |
| } |
| |
| |
| else if constexpr (sized_sentinel_for<_Sp, _Ip>) { |
| (*this)(__i, __bound_sentinel - __i); |
| } |
| |
| else { |
| while (__i != __bound_sentinel) { |
| ++__i; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp> |
| _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Ip> |
| operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const { |
| _LIBCPP_ASSERT_PEDANTIC( |
| (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) || (__n >= 0), |
| "ranges::advance: Can only pass a negative `n` with a bidirectional_iterator coming from a common_range."); |
| |
| if constexpr (sized_sentinel_for<_Sp, _Ip>) { |
| |
| |
| auto __magnitude_geq = [](auto __a, auto __b) { return __a == 0 ? __b == 0 : __a > 0 ? __a >= __b : __a <= __b; }; |
| if (const auto __m = __bound_sentinel - __i; __magnitude_geq(__n, __m)) { |
| (*this)(__i, __bound_sentinel); |
| return __n - __m; |
| } |
|
|
| |
| (*this)(__i, __n); |
| return 0; |
| } else { |
| |
| |
| while (__n > 0 && __i != __bound_sentinel) { |
| ++__i; |
| --__n; |
| } |
|
|
| |
| if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) { |
| while (__n < 0 && __i != __bound_sentinel) { |
| --__i; |
| ++__n; |
| } |
| } |
| return __n; |
| } |
|
|
| __libcpp_unreachable(); |
| } |
| }; |
|
|
| inline namespace __cpo { |
| inline constexpr auto advance = __advance{}; |
| } |
| } |
|
|
| #endif |
|
|
| _LIBCPP_END_NAMESPACE_STD |
|
|
| _LIBCPP_POP_MACROS |
|
|
| #endif |
|
|