[libcxx][nfc] moves std `advance`, `next`, and `prev` into their headers

Differential Revision: https://reviews.llvm.org/D103329
This commit is contained in:
Christopher Di Bella 2021-05-28 17:53:54 +00:00
parent e4034881c3
commit bf92bdad77
4 changed files with 56 additions and 76 deletions

View File

@ -29,6 +29,41 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_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>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_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>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_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 = typename enable_if<is_integral<decltype(_VSTD::__convert_to_integral(declval<_Distance>()))>::value>::type>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 void advance(_InputIter& __i, _Distance __orig_n) {
typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
_IntegralSize __n = __orig_n;
_LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
"Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
_VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
}
#if !defined(_LIBCPP_HAS_NO_RANGES)
namespace ranges {

View File

@ -25,6 +25,17 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
typename enable_if<__is_cpp17_input_iterator<_InputIter>::value, _InputIter>::type
next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
_LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
"Attempt to next(it, n) with negative n on a non-bidirectional iterator");
_VSTD::advance(__x, __n);
return __x;
}
#if !defined(_LIBCPP_HAS_NO_RANGES)
namespace ranges {

View File

@ -25,6 +25,16 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
typename enable_if<__is_cpp17_input_iterator<_InputIter>::value, _InputIter>::type
prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
_LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
"Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
_VSTD::advance(__x, -__n);
return __x;
}
#if !defined(_LIBCPP_HAS_NO_RANGES)
namespace ranges {

View File

@ -589,48 +589,6 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 iterator
typedef _Category iterator_category;
};
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_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>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_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>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_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 = typename enable_if<is_integral<decltype(_VSTD::__convert_to_integral(declval<_Distance>()))>::value>::type>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
void advance(_InputIter& __i, _Distance __orig_n)
{
typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
_IntegralSize __n = __orig_n;
_LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
"Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
_VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
}
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
typename iterator_traits<_InputIter>::difference_type
@ -658,40 +616,6 @@ distance(_InputIter __first, _InputIter __last)
return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
}
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
typename enable_if
<
__is_cpp17_input_iterator<_InputIter>::value,
_InputIter
>::type
next(_InputIter __x,
typename iterator_traits<_InputIter>::difference_type __n = 1)
{
_LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
"Attempt to next(it, n) with negative n on a non-bidirectional iterator");
_VSTD::advance(__x, __n);
return __x;
}
template <class _InputIter>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
typename enable_if
<
__is_cpp17_input_iterator<_InputIter>::value,
_InputIter
>::type
prev(_InputIter __x,
typename iterator_traits<_InputIter>::difference_type __n = 1)
{
_LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
"Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
_VSTD::advance(__x, -__n);
return __x;
}
template <class _Tp, class = void>
struct __is_stashing_iterator : false_type {};