Skip to content

Commit 88632e4

Browse files
committed
[libc++] Refactor __less
This simplifies the usage of `__less` by making the class not depend on the types compared, but instead the `operator()`. We can't remove the template completely because we explicitly instantiate `std::__sort` with `__less<T>`. Reviewed By: ldionne, #libc Spies: arichardson, EricWF, libcxx-commits, mgrang Differential Revision: https://reviews.llvm.org/D145285
1 parent 0aa4af7 commit 88632e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+142
-128
lines changed

libcxx/include/__algorithm/binary_search.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
3737
bool
3838
binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3939
{
40-
return std::binary_search(__first, __last, __value,
41-
__less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
40+
return std::binary_search(__first, __last, __value, __less<>());
4241
}
4342

4443
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/clamp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ _LIBCPP_INLINE_VISIBILITY constexpr
3737
const _Tp&
3838
clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
3939
{
40-
return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
40+
return _VSTD::clamp(__v, __lo, __hi, __less<>());
4141
}
4242
#endif
4343

libcxx/include/__algorithm/comp.h

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,17 @@ struct __equal_to {
2929
template <class _Lhs, class _Rhs>
3030
struct __is_trivial_equality_predicate<__equal_to, _Lhs, _Rhs> : true_type {};
3131

32-
template <class _T1, class _T2 = _T1>
33-
struct __less
34-
{
35-
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
36-
bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
37-
38-
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
39-
bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
40-
41-
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
42-
bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
43-
44-
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
45-
bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
46-
};
47-
48-
template <class _T1>
49-
struct __less<_T1, _T1>
50-
{
51-
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
52-
bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
53-
};
54-
55-
template <class _T1>
56-
struct __less<const _T1, _T1>
57-
{
58-
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
59-
bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
60-
};
61-
62-
template <class _T1>
63-
struct __less<_T1, const _T1>
64-
{
65-
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
66-
bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
32+
// The definition is required because __less is part of the ABI, but it's empty
33+
// because all comparisons should be transparent.
34+
template <class _T1 = void, class _T2 = _T1>
35+
struct __less {};
36+
37+
template <>
38+
struct __less<void, void> {
39+
template <class _Tp, class _Up>
40+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __lhs, const _Up& __rhs) const {
41+
return __lhs < __rhs;
42+
}
6743
};
6844

6945
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/equal_range.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __valu
7575
template <class _ForwardIterator, class _Tp>
7676
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator, _ForwardIterator>
7777
equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
78-
return std::equal_range(
79-
std::move(__first),
80-
std::move(__last),
81-
__value,
82-
__less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
78+
return std::equal_range(std::move(__first), std::move(__last), __value, __less<>());
8379
}
8480

8581
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/includes.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@ _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
6161
template <class _InputIterator1, class _InputIterator2>
6262
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
6363
includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
64-
return std::includes(
65-
std::move(__first1),
66-
std::move(__last1),
67-
std::move(__first2),
68-
std::move(__last2),
69-
__less<typename iterator_traits<_InputIterator1>::value_type,
70-
typename iterator_traits<_InputIterator2>::value_type>());
64+
return std::includes(std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __less<>());
7165
}
7266

7367
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/inplace_merge.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ inline _LIBCPP_HIDE_FROM_ABI
246246
void
247247
inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
248248
{
249-
std::inplace_merge(std::move(__first), std::move(__middle), std::move(__last),
250-
__less<typename iterator_traits<_BidirectionalIterator>::value_type>());
249+
std::inplace_merge(std::move(__first), std::move(__middle), std::move(__last), __less<>());
251250
}
252251

253252
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/is_heap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
3636
bool
3737
is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
3838
{
39-
return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
39+
return _VSTD::is_heap(__first, __last, __less<>());
4040
}
4141

4242
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/is_heap_until.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ template<class _RandomAccessIterator>
5858
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandomAccessIterator
5959
is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
6060
{
61-
return _VSTD::__is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
61+
return _VSTD::__is_heap_until(__first, __last, __less<>());
6262
}
6363

6464
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/is_sorted.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
3636
bool
3737
is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3838
{
39-
return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
39+
return _VSTD::is_sorted(__first, __last, __less<>());
4040
}
4141

4242
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/is_sorted_until.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ template<class _ForwardIterator>
4848
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
4949
is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
5050
{
51-
return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
51+
return _VSTD::is_sorted_until(__first, __last, __less<>());
5252
}
5353

5454
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)