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,14 +58,13 @@ 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> -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) { + && __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<_InIter, _OutIter> +__copy_impl(_InIter __first, _InIter __last, _OutIter __result) { auto __first_base = std::__unwrap_iter(__first.base()).first; auto __last_base = std::__unwrap_iter(__last.base()).first; auto __result_un = std::__unwrap_iter(__result.base()); @@ -74,7 +73,7 @@ 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_un.second, __result_first))); + return std::make_pair(__last, _OutIter(std::__rewrap_iter(__result_un.second, __result_first))); } template 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 @@ -27,9 +27,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,24 +328,161 @@ } #endif +#if _LIBCPP_STD_VER <= 17 template -using _ReverseWrapper = reverse_iterator >; +using _AlgRevIter = reverse_iterator<_Iter>; +#else -template -struct __unwrap_iter_impl<_ReverseWrapper<_Iter>, __b> { +// reverse_iterator breaks some C++20-hostile iterators. This class is essentially a copy-paste of reverse_iterator +// with the problematic parts removed to make it usable in the classic STL algorithms in C++20. +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 _Iter base() const { return __iter_; } + _LIBCPP_HIDE_FROM_ABI constexpr reference operator*() const { + auto __tmp = __iter_; + return *--__tmp; + } + + _LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const { + if constexpr (is_pointer_v<_Iter>) { + return std::prev(__iter_); + } else { + return std::prev(__iter_).operator->(); + } + } + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter& operator++() { + --__iter_; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter operator++(int) { + auto __tmp = *this; + --__iter_; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter& operator--() { + ++__iter_; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter operator--(int) { + auto __tmp = *this; + ++__iter_; + return __tmp; + } + + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter& operator+=(difference_type __n) { + __iter_ -= __n; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter& operator-=(difference_type __n) { + __iter_ += __n; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter operator+(difference_type __n) const { + return _AlgRevIter(__iter_ - __n); + } + + _LIBCPP_HIDE_FROM_ABI constexpr _AlgRevIter operator-(difference_type __n) const { + return _AlgRevIter(__iter_ + __n); + } + + _LIBCPP_HIDE_FROM_ABI constexpr difference_type operator-(const _AlgRevIter& __other) const { + return __other.__iter_ - __iter_; + } + + _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