diff --git a/libcxx/include/__algorithm/copy.h b/libcxx/include/__algorithm/copy.h --- a/libcxx/include/__algorithm/copy.h +++ b/libcxx/include/__algorithm/copy.h @@ -58,26 +58,26 @@ template >::type, __iter_value_type<_OutIter> >::value - && __is_cpp17_contiguous_iterator<_InIter>::value - && __is_cpp17_contiguous_iterator<_OutIter>::value - && is_trivially_copy_assignable<__iter_value_type<_OutIter> >::value, int> = 0> + && __is_cpp17_contiguous_iterator::value + && __is_cpp17_contiguous_iterator::value + && is_trivially_copy_assignable<__iter_value_type<_OutIter> >::value + && __is_reverse_iterator<_InIter>::value + && __is_reverse_iterator<_OutIter>::value, int> = 0> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 -pair, reverse_iterator<_OutIter> > -__copy_impl(reverse_iterator<_InIter> __first, - reverse_iterator<_InIter> __last, - reverse_iterator<_OutIter> __result) { +pair<_InIter, _OutIter> +__copy_impl(_InIter __first, _InIter __last, _OutIter __result) { auto __first_base = std::__unwrap_iter(__first.base()); auto __last_base = std::__unwrap_iter(__last.base()); auto __result_base = std::__unwrap_iter(__result.base()); auto __result_first = __result_base - (__first_base - __last_base); std::__copy_impl(__last_base, __first_base, __result_first); - return std::make_pair(__last, reverse_iterator<_OutIter>(std::__rewrap_iter(__result.base(), __result_first))); + return std::make_pair(__last, _OutIter(std::__rewrap_iter(__result.base(), __result_first))); } template ::value && is_copy_constructible<_Sent>::value - && is_copy_constructible<_OutIter>::value), int> = 0 > + && is_copy_constructible<_OutIter>::value), int> = 0> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 pair<_InIter, _OutIter> __copy(_InIter __first, _Sent __last, _OutIter __result) { return std::__copy_impl(std::move(__first), std::move(__last), std::move(__result)); diff --git a/libcxx/include/__algorithm/copy_backward.h b/libcxx/include/__algorithm/copy_backward.h --- a/libcxx/include/__algorithm/copy_backward.h +++ b/libcxx/include/__algorithm/copy_backward.h @@ -26,9 +26,9 @@ template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 pair<_Iter1, _Iter2> __copy_backward_impl(_Iter1 __first, _Sent1 __last, _Iter2 __result) { - auto __ret = std::__copy(reverse_iterator<_Iter1>(__last), - reverse_iterator<_Sent1>(__first), - reverse_iterator<_Iter2>(__result)); + auto __ret = std::__copy(_AlgRevIter<_Iter1>(__last), + _AlgRevIter<_Sent1>(__first), + _AlgRevIter<_Iter2>(__result)); return pair<_Iter1, _Iter2>(__ret.first.base(), __ret.second.base()); } diff --git a/libcxx/include/__algorithm/inplace_merge.h b/libcxx/include/__algorithm/inplace_merge.h --- a/libcxx/include/__algorithm/inplace_merge.h +++ b/libcxx/include/__algorithm/inplace_merge.h @@ -103,8 +103,8 @@ value_type* __p = __buff; for (_BidirectionalIterator __i = __middle; __i != __last; __d.template __incr(), (void) ++__i, (void) ++__p) ::new ((void*)__p) value_type(_VSTD::move(*__i)); - typedef reverse_iterator<_BidirectionalIterator> _RBi; - typedef reverse_iterator _Rv; + typedef _AlgRevIter<_BidirectionalIterator> _RBi; + typedef _AlgRevIter _Rv; typedef __invert<_Compare> _Inverted; _VSTD::__half_inplace_merge<_Inverted>(_Rv(__p), _Rv(__buff), _RBi(__middle), _RBi(__first), diff --git a/libcxx/include/__iterator/iterator_traits.h b/libcxx/include/__iterator/iterator_traits.h --- a/libcxx/include/__iterator/iterator_traits.h +++ b/libcxx/include/__iterator/iterator_traits.h @@ -489,6 +489,12 @@ typename add_const::value_type::first_type>::type, typename iterator_traits<_InputIterator>::value_type::second_type>; +template +using __iterator_category_type = typename iterator_traits<_Iter>::iterator_category; + +template +using __iterator_pointer_type = typename iterator_traits<_Iter>::pointer; + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP___ITERATOR_ITERATOR_TRAITS_H diff --git a/libcxx/include/__iterator/reverse_iterator.h b/libcxx/include/__iterator/reverse_iterator.h --- a/libcxx/include/__iterator/reverse_iterator.h +++ b/libcxx/include/__iterator/reverse_iterator.h @@ -192,6 +192,12 @@ #endif // _LIBCPP_STD_VER > 17 }; +template +struct __is_reverse_iterator : false_type {}; + +template +struct __is_reverse_iterator> : true_type {}; + template inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 bool @@ -322,23 +328,175 @@ } #endif +#if _LIBCPP_STD_VER <= 17 +template +using _AlgRevIter = reverse_iterator<_Iter>; +#else + +template +class _AlgRevIter { + _Iter __iter_; + +public: + static_assert(__is_cpp17_bidirectional_iterator<_Iter>::value || bidirectional_iterator<_Iter>); + + using iterator_type = _Iter; + using iterator_category = + _If<__is_cpp17_random_access_iterator<_Iter>::value, random_access_iterator_tag, __iterator_category_type<_Iter>>; + using pointer = __iterator_pointer_type<_Iter>; + using iterator_concept = + _If<__is_cpp17_random_access_iterator<_Iter>::value, random_access_iterator_tag, bidirectional_iterator_tag>; + using value_type = iter_value_t<_Iter>; + using difference_type = iter_difference_t<_Iter>; + using reference = iter_reference_t<_Iter>; + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter() = default; + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter(const _AlgRevIter&) = default; + _LIBCPP_HIDE_FROM_ABI constexpr explicit _AlgRevIter(_Iter __iter) : __iter_(__iter) {} + + _LIBCPP_HIDE_FROM_ABI constexpr auto base() const { return __iter_; } + _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() { + auto __tmp = __iter_; + return *--__tmp; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto operator->() const { + if constexpr (is_pointer_v<_Iter>) { + return std::prev(__iter_); + } else { + return std::prev(__iter_).operator->(); + } + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto& operator++() { + --__iter_; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto operator++(int) { + auto __tmp = *this; + --__iter_; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto& operator--() { + ++__iter_; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto operator--(int) { + auto __tmp = *this; + ++__iter_; + return __tmp; + } + + + _LIBCPP_HIDE_FROM_ABI constexpr auto& operator+=(difference_type __n) { + __iter_ -= __n; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto& operator-=(difference_type __n) { + __iter_ += __n; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto operator+(difference_type __n) const { return _AlgRevIter(__iter_ - __n); } + _LIBCPP_HIDE_FROM_ABI constexpr auto operator-(difference_type __n) const { return _AlgRevIter(__iter_ + __n); } + + _LIBCPP_HIDE_FROM_ABI constexpr auto operator[](difference_type __n) const { return *(*this + __n); } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const _AlgRevIter& __lhs, const _AlgRevIter& __rhs) { + return __lhs.base() == __rhs.base(); + } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator!=(const _AlgRevIter& __lhs, const _AlgRevIter& __rhs) { + return __lhs.base() != __rhs.base(); + } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const _AlgRevIter& __lhs, const _AlgRevIter& __rhs) { + return __lhs.base() > __rhs.base(); + } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const _AlgRevIter& __lhs, const _AlgRevIter& __rhs) { + return __lhs.base() < __rhs.base(); + } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const _AlgRevIter& __lhs, const _AlgRevIter& __rhs) { + return __lhs.base() >= __rhs.base(); + } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const _AlgRevIter& __lhs, const _AlgRevIter& __rhs) { + return __lhs.base() <= __rhs.base(); + } +}; + +template +struct __is_reverse_iterator<_AlgRevIter<_Iter>> : true_type {}; + +#endif // _LIBCPP_STD_VER <= 17 + +template +struct _IsReverseIterator : false_type {}; + template -using _ReverseWrapper = reverse_iterator >; +struct _IsReverseIterator> : true_type {}; + +#if _LIBCPP_STD_VER > 17 + +template +struct _IsReverseIterator<_AlgRevIter<_Iter>> : true_type {}; + +#endif // _LIBCPP_STD_VER > 17 + +template +using _ReverseWrapper1 = reverse_iterator>; template -struct __unwrap_iter_impl<_ReverseWrapper<_Iter>, __b> { +struct __unwrap_iter_impl<_ReverseWrapper1<_Iter>, __b> { static _LIBCPP_CONSTEXPR decltype(std::__unwrap_iter(std::declval<_Iter>())) - __apply(_ReverseWrapper<_Iter> __i) _NOEXCEPT { + __apply(_ReverseWrapper1<_Iter> __i) _NOEXCEPT { return std::__unwrap_iter(__i.base().base()); } }; -template -struct __rewrap_iter_impl<_ReverseWrapper<_OrigIter>, _UnwrappedIter> { - template - struct _ReverseWrapperCount { - static _LIBCPP_CONSTEXPR const size_t value = 1; - }; +#if _LIBCPP_STD_VER > 17 + +template +using _ReverseWrapper2 = reverse_iterator<_AlgRevIter<_Iter>>; + +template +struct __unwrap_iter_impl<_ReverseWrapper2<_Iter>, __b> { + static constexpr auto __apply(_ReverseWrapper2<_Iter> __i) noexcept { + return std::__unwrap_iter(__i.base().base()); + } +}; + +template +using _ReverseWrapper3 = _AlgRevIter>; + +template +struct __unwrap_iter_impl<_ReverseWrapper3<_Iter>, __b> { + static constexpr auto __apply(_ReverseWrapper3<_Iter> __i) noexcept { + return std::__unwrap_iter(__i.base().base()); + } +}; + +template +using _ReverseWrapper4 = _AlgRevIter<_AlgRevIter<_Iter>>; + +template +struct __unwrap_iter_impl<_ReverseWrapper4<_Iter>, __b> { + static constexpr auto __apply(_ReverseWrapper4<_Iter> __i) noexcept { + return std::__unwrap_iter(__i.base().base()); + } +}; +#endif + +template