[libc++] Eliminate needless `add_lvalue_reference` from <algorithm> helpers. NFCI.

When `_Compare` is a function parameter already (so it's not `void`
and it's not an abominable function type), `add_lvalue_reference_t<_Compare>`
is simply a synonym for `_Compare&`. We don't need to pull in `<type_traits>`
and instantiate a template trait to figure that out.

Differential Revision: https://reviews.llvm.org/D108400
This commit is contained in:
Arthur O'Dwyer 2021-08-19 15:06:21 -04:00
parent fafe5a6f44
commit ca7926bd79
14 changed files with 24 additions and 47 deletions

View File

@ -10,7 +10,6 @@
#define _LIBCPP___ALGORITHM_COMP_REF_TYPE_H
#include <__config>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -73,7 +72,7 @@ struct __comp_ref_type {
// Pass the comparator by lvalue reference. Or in debug mode, using a
// debugging wrapper that stores a reference.
#ifndef _LIBCPP_DEBUG
typedef typename add_lvalue_reference<_Comp>::type type;
typedef _Comp& type;
#else
typedef __debug_less<_Comp> type;
#endif

View File

@ -14,7 +14,6 @@
#include <__algorithm/comp.h>
#include <__iterator/distance.h>
#include <__iterator/iterator_traits.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -61,14 +60,14 @@ __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _Random
if (_VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
return false;
return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
typename add_lvalue_reference<_BinaryPredicate>::type>(__first1, __last1, __first2, __pred);
_BinaryPredicate&>(__first1, __last1, __first2, __pred);
}
template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
_BinaryPredicate __pred) {
return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>(
return _VSTD::__equal<_BinaryPredicate&>(
__first1, __last1, __first2, __last2, __pred, typename iterator_traits<_InputIterator1>::iterator_category(),
typename iterator_traits<_InputIterator2>::iterator_category());
}

View File

@ -13,7 +13,6 @@
#include <__config>
#include <__algorithm/comp.h>
#include <__iterator/iterator_traits.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -134,7 +133,7 @@ template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredica
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate __pred) {
return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>(
return _VSTD::__find_end<_BinaryPredicate&>(
__first1, __last1, __first2, __last2, __pred, typename iterator_traits<_ForwardIterator1>::iterator_category(),
typename iterator_traits<_ForwardIterator2>::iterator_category());
}

View File

@ -15,7 +15,6 @@
#include <__iterator/distance.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/next.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -137,15 +136,14 @@ _LIBCPP_CONSTEXPR_AFTER_CXX17 bool __is_permutation(_RandomAccessIterator1 __fir
if (_VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
return false;
return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
typename add_lvalue_reference<_BinaryPredicate>::type>(__first1, __last1, __first2,
__pred);
_BinaryPredicate&>(__first1, __last1, __first2, __pred);
}
template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
_ForwardIterator2 __last2, _BinaryPredicate __pred) {
return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>(
return _VSTD::__is_permutation<_BinaryPredicate&>(
__first1, __last1, __first2, __last2, __pred, typename iterator_traits<_ForwardIterator1>::iterator_category(),
typename iterator_traits<_ForwardIterator2>::iterator_category());
}

View File

@ -51,8 +51,7 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_ForwardIterator
lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
{
typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
return _VSTD::__lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
return _VSTD::__lower_bound<_Compare&>(__first, __last, __value_, __comp);
}
template <class _ForwardIterator, class _Tp>

View File

@ -13,7 +13,6 @@
#include <__iterator/iterator_traits.h>
#include <__utility/swap.h>
#include <utility> // pair
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -77,8 +76,8 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_ForwardIterator
partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
{
return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
(__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
return _VSTD::__partition<_Predicate&>(
__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
}
_LIBCPP_END_NAMESPACE_STD

View File

@ -12,7 +12,6 @@
#include <__config>
#include <__algorithm/find_if.h>
#include <utility>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -27,8 +26,7 @@ template <class _ForwardIterator, class _Predicate>
_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
{
__first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
(__first, __last, __pred);
__first = _VSTD::find_if<_ForwardIterator, _Predicate&>(__first, __last, __pred);
if (__first != __last)
{
_ForwardIterator __i = __first;

View File

@ -13,7 +13,6 @@
#include <__algorithm/comp.h>
#include <__config>
#include <__iterator/iterator_traits.h>
#include <type_traits>
#include <utility>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@ -101,7 +100,7 @@ template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredica
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate __pred) {
return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>(
return _VSTD::__search<_BinaryPredicate&>(
__first1, __last1, __first2, __last2, __pred,
typename iterator_traits<_ForwardIterator1>::iterator_category(),
typename iterator_traits<_ForwardIterator2>::iterator_category()).first;

View File

@ -13,7 +13,7 @@
#include <__config>
#include <__algorithm/comp.h>
#include <__iterator/iterator_traits.h>
#include <type_traits>
#include <type_traits> // __convert_to_integral
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -97,7 +97,7 @@ _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator __search_n(_RandomAccessIter
template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator search_n(
_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_, _BinaryPredicate __pred) {
return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>(
return _VSTD::__search_n<_BinaryPredicate&>(
__first, __last, _VSTD::__convert_to_integral(__count), __value_, __pred,
typename iterator_traits<_ForwardIterator>::iterator_category());
}

View File

@ -17,7 +17,6 @@
#include <__algorithm/unwrap_iter.h>
#include <__utility/swap.h>
#include <memory>
#include <type_traits> // swap
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -131,9 +130,7 @@ __selection_sort(_BidirectionalIterator __first, _BidirectionalIterator __last,
_BidirectionalIterator __lm1 = __last;
for (--__lm1; __first != __lm1; ++__first)
{
_BidirectionalIterator __i = _VSTD::min_element<_BidirectionalIterator,
typename add_lvalue_reference<_Compare>::type>
(__first, __last, __comp);
_BidirectionalIterator __i = _VSTD::min_element<_BidirectionalIterator, _Compare&>(__first, __last, __comp);
if (__i != __first)
swap(*__first, *__i);
}

View File

@ -85,8 +85,7 @@ __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate
// recurse on [__first, __m), *__first know to be false
// F?????????????????
// f m l
typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
_ForwardIterator __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
_ForwardIterator __first_false = _VSTD::__stable_partition<_Predicate&>(__first, __m, __pred, __len2, __p, __fit);
// TTTFFFFF??????????
// f ff m l
// recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
@ -101,7 +100,7 @@ __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate
}
// TTTFFFFFTTTF??????
// f ff m m1 l
__second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
__second_false = _VSTD::__stable_partition<_Predicate&>(__m1, __last, __pred, __len_half, __p, __fit);
__second_half_done:
// TTTFFFFFTTTTTFFFFF
// f ff m sf l
@ -137,8 +136,7 @@ __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate
__p = _VSTD::get_temporary_buffer<value_type>(__len);
__h.reset(__p.first);
}
return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
(__first, __last, __pred, __len, __p, forward_iterator_tag());
return _VSTD::__stable_partition<_Predicate&>(__first, __last, __pred, __len, __p, forward_iterator_tag());
}
template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
@ -222,8 +220,7 @@ __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last
}
// F???TFFF?????????T
// f m1 m l
typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
__first_false = _VSTD::__stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
__first_false = _VSTD::__stable_partition<_Predicate&>(__first, __m1, __pred, __len_half, __p, __bit);
__first_half_done:
// TTTFFFFF?????????T
// f ff m l
@ -240,7 +237,7 @@ __first_half_done:
}
// TTTFFFFFTTTF?????T
// f ff m m1 l
__second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
__second_false = _VSTD::__stable_partition<_Predicate&>(__m1, __last, __pred, __len_half, __p, __bit);
__second_half_done:
// TTTFFFFFTTTTTFFFFF
// f ff m sf l
@ -285,8 +282,7 @@ __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last
__p = _VSTD::get_temporary_buffer<value_type>(__len);
__h.reset(__p.first);
}
return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
(__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
return _VSTD::__stable_partition<_Predicate&>(__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
}
template <class _ForwardIterator, class _Predicate>
@ -294,8 +290,7 @@ inline _LIBCPP_INLINE_VISIBILITY
_ForwardIterator
stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
{
return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
(__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
return _VSTD::__stable_partition<_Predicate&>(__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
}
_LIBCPP_END_NAMESPACE_STD

View File

@ -14,7 +14,6 @@
#include <__algorithm/adjacent_find.h>
#include <__iterator/iterator_traits.h>
#include <__utility/move.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -31,8 +30,7 @@ template <class _ForwardIterator, class _BinaryPredicate>
_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
{
__first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
(__first, __last, __pred);
__first = _VSTD::adjacent_find<_ForwardIterator, _BinaryPredicate&>(__first, __last, __pred);
if (__first != __last)
{
// ... a a ? ...

View File

@ -13,7 +13,6 @@
#include <__algorithm/comp.h>
#include <__iterator/iterator_traits.h>
#include <utility>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@ -91,8 +90,7 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator
unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
{
return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
(__first, __last, __result, __pred,
return _VSTD::__unique_copy<_BinaryPredicate&>(__first, __last, __result, __pred,
typename iterator_traits<_InputIterator>::iterator_category(),
typename iterator_traits<_OutputIterator>::iterator_category());
}

View File

@ -51,8 +51,7 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_ForwardIterator
upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
{
typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
return _VSTD::__upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
return _VSTD::__upper_bound<_Compare&>(__first, __last, __value_, __comp);
}
template <class _ForwardIterator, class _Tp>