| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef _LIBCUDACXX___ITERATOR_ADVANCE_H |
| #define _LIBCUDACXX___ITERATOR_ADVANCE_H |
|
|
| #ifndef __cuda_std__ |
| # include <__config> |
| #endif |
|
|
| #include "../__assert" |
| #include "../__concepts/assignable.h" |
| #include "../__concepts/same_as.h" |
| #include "../__iterator/concepts.h" |
| #include "../__iterator/incrementable_traits.h" |
| #include "../__iterator/iterator_traits.h" |
| #include "../__utility/convert_to_integral.h" |
| #include "../__utility/move.h" |
| #include "../cstdlib" |
|
|
| #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| # pragma GCC system_header |
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| # pragma clang system_header |
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| # pragma system_header |
| #endif |
|
|
| _LIBCUDACXX_BEGIN_NAMESPACE_STD |
|
|
| template <class _InputIter> |
| _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX14 void |
| __advance(_InputIter& __i, typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag) |
| { |
| for (; __n > 0; --__n) |
| { |
| ++__i; |
| } |
| } |
|
|
| template <class _BiDirIter> |
| _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX14 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> |
| _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX14 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(_CUDA_VSTD::__convert_to_integral(_CUDA_VSTD::declval<_Distance>())), |
| class = __enable_if_t<is_integral<_IntegralDistance>::value> > |
| _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX14 void advance(_InputIter& __i, _Distance __orig_n) |
| { |
| typedef typename iterator_traits<_InputIter>::difference_type _Difference; |
| _Difference __n = static_cast<_Difference>(_CUDA_VSTD::__convert_to_integral(__orig_n)); |
| _LIBCUDACXX_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value, |
| "Attempt to advance(it, n) with negative n on a non-bidirectional iterator"); |
| _CUDA_VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category()); |
| } |
|
|
| _LIBCUDACXX_END_NAMESPACE_STD |
|
|
| #if _LIBCUDACXX_STD_VER > 14 && !defined(_LICBUDACXX_COMPILER_MSVC_2017) |
|
|
| |
|
|
| _LIBCUDACXX_BEGIN_NAMESPACE_RANGES |
| _LIBCUDACXX_BEGIN_NAMESPACE_CPO(__advance) |
| struct __fn |
| { |
| private: |
| template <class _Ip> |
| _LIBCUDACXX_INLINE_VISIBILITY static constexpr void __advance_forward(_Ip& __i, iter_difference_t<_Ip> __n) |
| { |
| while (__n > 0) |
| { |
| --__n; |
| ++__i; |
| } |
| } |
|
|
| template <class _Ip> |
| _LIBCUDACXX_INLINE_VISIBILITY static constexpr void __advance_backward(_Ip& __i, iter_difference_t<_Ip> __n) |
| { |
| while (__n < 0) |
| { |
| ++__n; |
| --__i; |
| } |
| } |
|
|
| template <class _Iter_difference> |
| _LIBCUDACXX_INLINE_VISIBILITY static constexpr auto |
| __magnitude_geq(_Iter_difference __a, _Iter_difference __b) noexcept |
| { |
| return __a == 0 ? __b == 0 : |
| __a > 0 ? __a >= __b |
| : __a <= __b; |
| }; |
|
|
| public: |
| |
|
|
| _LIBCUDACXX_TEMPLATE(class _Ip) |
| _LIBCUDACXX_REQUIRES(input_or_output_iterator<_Ip>) |
| _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr void |
| operator()(_Ip& __i, iter_difference_t<_Ip> __n) const |
| { |
| _LIBCUDACXX_ASSERT(__n >= 0 || bidirectional_iterator<_Ip>, |
| "If `n < 0`, then `bidirectional_iterator<I>` must be true."); |
|
|
| |
| 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; |
| } |
| } |
|
|
| _LIBCUDACXX_TEMPLATE(class _Ip, class _Sp) |
| _LIBCUDACXX_REQUIRES(input_or_output_iterator<_Ip> _LIBCUDACXX_AND sentinel_for<_Sp, _Ip>) |
| _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const |
| { |
| |
| if constexpr (assignable_from<_Ip&, _Sp>) |
| { |
| __i = _CUDA_VSTD::move(__bound_sentinel); |
| } |
| |
| |
| else if constexpr (sized_sentinel_for<_Sp, _Ip>) |
| { |
| (*this)(__i, __bound_sentinel - __i); |
| } |
| |
| else |
| { |
| while (__i != __bound_sentinel) |
| { |
| ++__i; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| _LIBCUDACXX_TEMPLATE(class _Ip, class _Sp) |
| _LIBCUDACXX_REQUIRES(input_or_output_iterator<_Ip> _LIBCUDACXX_AND sentinel_for<_Sp, _Ip>) |
| _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_HIDE_FROM_ABI constexpr iter_difference_t<_Ip> |
| operator()(_Ip& __i, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const |
| { |
| _LIBCUDACXX_ASSERT((__n >= 0) || (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>), |
| "If `n < 0`, then `bidirectional_iterator<I> && same_as<I, S>` must be true."); |
| |
| if constexpr (sized_sentinel_for<_Sp, _Ip>) |
| { |
| |
| |
| const auto __M = __bound_sentinel - __i; |
| if (__magnitude_geq(__n, __M)) |
| { |
| (*this)(__i, __bound_sentinel); |
| return __n - __M; |
| } |
|
|
| |
| (*this)(__i, __n); |
| return 0; |
| } |
| else |
| { |
| |
| |
| while (__i != __bound_sentinel && __n > 0) |
| { |
| ++__i; |
| --__n; |
| } |
|
|
| |
| if constexpr (bidirectional_iterator<_Ip> && same_as<_Ip, _Sp>) |
| { |
| while (__i != __bound_sentinel && __n < 0) |
| { |
| --__i; |
| ++__n; |
| } |
| } |
| return __n; |
| } |
| _LIBCUDACXX_UNREACHABLE(); |
| } |
| }; |
| _LIBCUDACXX_END_NAMESPACE_CPO |
|
|
| inline namespace __cpo |
| { |
| _LIBCUDACXX_CPO_ACCESSIBILITY auto advance = __advance::__fn{}; |
| } |
|
|
| _LIBCUDACXX_END_NAMESPACE_RANGES |
|
|
| #endif |
|
|
| #endif |
|
|