Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions libcxx/include/__utility/lazy_synth_three_way_comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ struct __eager_compare_result {
};

template <class _Comparator, class _LHS, class _RHS>
struct __lazy_synth_three_way_comparator<
_Comparator,
_LHS,
_RHS,
__enable_if_t<_And<__desugars_to<__less_tag, _Comparator, _LHS, _RHS>,
__has_default_three_way_comparator<_LHS, _RHS> >::value> > {
struct __lazy_synth_three_way_comparator<_Comparator,
_LHS,
_RHS,
__enable_if_t<_And<__desugars_to<__less_tag, _Comparator, _LHS, _RHS>,
__has_default_three_way_comparator<_LHS, _RHS> >::value> > {
// This lifetimebound annotation is technically incorrect, but other specializations actually capture the lifetime of
// the comparator.
_LIBCPP_HIDE_FROM_ABI __lazy_synth_three_way_comparator(_LIBCPP_CTOR_LIFETIMEBOUND const _Comparator&) {}
Expand All @@ -87,6 +86,23 @@ struct __lazy_synth_three_way_comparator<
}
};

template <class _Comparator, class _LHS, class _RHS>
struct __lazy_synth_three_way_comparator<_Comparator,
_LHS,
_RHS,
__enable_if_t<_And<__desugars_to<__greater_tag, _Comparator, _LHS, _RHS>,
__has_default_three_way_comparator<_LHS, _RHS> >::value> > {
// This lifetimebound annotation is technically incorrect, but other specializations actually capture the lifetime of
// the comparator.
_LIBCPP_HIDE_FROM_ABI __lazy_synth_three_way_comparator(_LIBCPP_CTOR_LIFETIMEBOUND const _Comparator&) {}

// Same comment as above.
_LIBCPP_HIDE_FROM_ABI static __eager_compare_result
operator()(_LIBCPP_LIFETIMEBOUND const _LHS& __lhs, _LIBCPP_LIFETIMEBOUND const _RHS& __rhs) {
return __eager_compare_result(-__default_three_way_comparator<_LHS, _RHS>()(__lhs, __rhs));
}
};

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___UTILITY_LAZY_SYNTH_THREE_WAY_COMPARATOR_H
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ int main(int, char**) {
assert(r == std::next(m.begin(), 8));
}
}
{ // Check with std::greater to ensure we're actually using the correct comparator
using Pair = std::pair<const int, int>;
using Map = std::map<int, int, std::greater<int> >;
Pair ar[] = {Pair(5, 5), Pair(6, 6), Pair(7, 7), Pair(8, 8), Pair(9, 9), Pair(10, 10), Pair(11, 11), Pair(12, 12)};
Map m(ar, ar + sizeof(ar) / sizeof(ar[0]));
assert(m.find(12) == std::next(m.begin(), 0));
assert(m.find(11) == std::next(m.begin(), 1));
assert(m.find(10) == std::next(m.begin(), 2));
assert(m.find(9) == std::next(m.begin(), 3));
assert(m.find(8) == std::next(m.begin(), 4));
assert(m.find(7) == std::next(m.begin(), 5));
assert(m.find(6) == std::next(m.begin(), 6));
assert(m.find(5) == std::next(m.begin(), 7));
assert(m.find(4) == std::next(m.begin(), 8));
assert(std::next(m.begin(), 8) == m.end());
}
#if TEST_STD_VER >= 11
{
typedef std::pair<const int, double> V;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ int main(int, char**) {
assert(r == m.end());
}
}
{
using Pair = std::pair<const int, int>;
using Map = std::multimap<int, int, std::greater<int> >;
Pair arr[] = {
Pair(5, 1), Pair(5, 2), Pair(5, 3), Pair(7, 1), Pair(7, 2), Pair(7, 3), Pair(9, 1), Pair(9, 2), Pair(9, 3)};
const Map m(arr, arr + sizeof(arr) / sizeof(arr[0]));
assert(iter_in_range(std::next(m.begin(), 6), std::next(m.begin(), 9), m.find(5)));
assert(m.find(6) == m.end());
assert(iter_in_range(std::next(m.begin(), 3), std::next(m.begin(), 6), m.find(7)));
assert(m.find(8) == m.end());
assert(iter_in_range(std::next(m.begin(), 0), std::next(m.begin(), 3), m.find(9)));
assert(m.find(10) == m.end());
}
#if TEST_STD_VER >= 11
{
typedef std::multimap<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
Expand Down
15 changes: 15 additions & 0 deletions libcxx/test/std/containers/associative/multiset/find.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ int main(int, char**) {
assert(r == std::next(m.begin(), 8));
}
}
{ // Check with std::greater to ensure we're actually using the correct comparator
using Set = std::multiset<int, std::greater<int> >;
int ar[] = {5, 6, 7, 8, 9, 10, 11, 12};
Set m(ar, ar + sizeof(ar) / sizeof(ar[0]));
assert(m.find(12) == std::next(m.begin(), 0));
assert(m.find(11) == std::next(m.begin(), 1));
assert(m.find(10) == std::next(m.begin(), 2));
assert(m.find(9) == std::next(m.begin(), 3));
assert(m.find(8) == std::next(m.begin(), 4));
assert(m.find(7) == std::next(m.begin(), 5));
assert(m.find(6) == std::next(m.begin(), 6));
assert(m.find(5) == std::next(m.begin(), 7));
assert(m.find(4) == std::next(m.begin(), 8));
assert(std::next(m.begin(), 8) == m.end());
}
#if TEST_STD_VER >= 11
{
typedef int V;
Expand Down
15 changes: 15 additions & 0 deletions libcxx/test/std/containers/associative/set/find.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ int main(int, char**) {
assert(r == std::next(m.begin(), 8));
}
}
{ // Check with std::greater to ensure we're actually using the correct comparator
using Set = std::set<int, std::greater<int> >;
int ar[] = {5, 6, 7, 8, 9, 10, 11, 12};
Set m(ar, ar + sizeof(ar) / sizeof(ar[0]));
assert(m.find(12) == std::next(m.begin(), 0));
assert(m.find(11) == std::next(m.begin(), 1));
assert(m.find(10) == std::next(m.begin(), 2));
assert(m.find(9) == std::next(m.begin(), 3));
assert(m.find(8) == std::next(m.begin(), 4));
assert(m.find(7) == std::next(m.begin(), 5));
assert(m.find(6) == std::next(m.begin(), 6));
assert(m.find(5) == std::next(m.begin(), 7));
assert(m.find(4) == std::next(m.begin(), 8));
assert(std::next(m.begin(), 8) == m.end());
}
#if TEST_STD_VER >= 11
{
typedef int V;
Expand Down
Loading