diff --git a/libcxx/include/set b/libcxx/include/set --- a/libcxx/include/set +++ b/libcxx/include/set @@ -210,27 +210,31 @@ template bool operator< (const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator!=(const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator> (const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator>=(const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator<=(const set& x, - const set& y); + const set& y); // removed in C++20 + +template + synth-three-way-result operator<=>(const set& x, + const set& y); // since C++20 // specialized algorithms: template @@ -435,27 +439,31 @@ template bool operator< (const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator!=(const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator> (const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator>=(const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator<=(const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 + +template + synth-three-way-result operator<=>(const multiset& x, + const multiset& y); // since C++20 // specialized algorithms: template @@ -473,6 +481,7 @@ #include <__algorithm/equal.h> #include <__algorithm/lexicographical_compare.h> +#include <__algorithm/lexicographical_compare_three_way.h> #include <__assert> // all public C++ headers provide the assertion handler #include <__config> #include <__functional/is_transparent.h> @@ -982,6 +991,8 @@ return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin()); } +#if _LIBCPP_STD_VER <= 17 + template inline _LIBCPP_INLINE_VISIBILITY bool @@ -1027,6 +1038,17 @@ return !(__y < __x); } +#else // _LIBCPP_STD_VER <= 17 + +template +inline _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Key> +operator<=>(const set<_Key, _Allocator>& __x, const set<_Key, _Allocator>& __y) { + return std::lexicographical_compare_three_way( + __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Key, _Key>); +} + +#endif // _LIBCPP_STD_VER <= 17 + // specialized algorithms: template inline _LIBCPP_INLINE_VISIBILITY @@ -1518,6 +1540,8 @@ return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin()); } +#if _LIBCPP_STD_VER <= 17 + template inline _LIBCPP_INLINE_VISIBILITY bool @@ -1563,6 +1587,17 @@ return !(__y < __x); } +#else // _LIBCPP_STD_VER <= 17 + +template +inline _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Key> +operator<=>(const multiset<_Key, _Allocator>& __x, const multiset<_Key, _Allocator>& __y) { + return std::lexicographical_compare_three_way( + __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Key, _Key>); +} + +#endif // _LIBCPP_STD_VER <= 17 + template inline _LIBCPP_INLINE_VISIBILITY void diff --git a/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.pass.cpp b/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// class multiset + +// template +// synth-three-way-result operator<=>(const multiset& x, +// const multiset& y); + +#include +#include + +#include "test_container_comparisons.h" + +int main(int, char**) { + assert(test_ordered_set_container_spaceship()); + // `std::multiset` is not constexpr, so no `static_assert` test here. + return 0; +} diff --git a/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.pass.cpp b/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// class set + +// template +// synth-three-way-result operator<=>(const set& x, +// const set& y); + +#include +#include + +#include "test_container_comparisons.h" + +int main(int, char**) { + assert((test_ordered_set_container_spaceship())); + // `std::set` is not constexpr, so no `static_assert` test here. + return 0; +} diff --git a/libcxx/test/support/test_container_comparisons.h b/libcxx/test/support/test_container_comparisons.h --- a/libcxx/test/support/test_container_comparisons.h +++ b/libcxx/test/support/test_container_comparisons.h @@ -10,6 +10,8 @@ #ifndef TEST_CONTAINER_COMPARISONS #define TEST_CONTAINER_COMPARISONS +#include + #include "test_comparisons.h" // Implementation detail of `test_sequence_container_spaceship` @@ -71,7 +73,7 @@ test_sequence_container_spaceship_with_type(); test_sequence_container_spaceship_with_type(); - // `LessAndEqComp` does not have `operator<=>`. ordering is sythesized based on `operator<` + // `LessAndEqComp` does not have `operator<=>`. Ordering is sythesized based on `operator<` test_sequence_container_spaceship_with_type(); // Thanks to SFINAE, the following is not a compiler error but returns `false` @@ -183,7 +185,7 @@ } } -// Tests the `operator<=>` on ordered containers +// Tests the `operator<=>` on ordered map containers template
typename Container> constexpr bool test_ordered_map_container_spaceship() { // The container should fulfil `std::three_way_comparable` @@ -195,7 +197,7 @@ test_ordered_map_container_spaceship_with_type(); test_ordered_map_container_spaceship_with_type(); - // `LessAndEqComp` does not have `operator<=>`. ordering is sythesized based on `operator<` + // `LessAndEqComp` does not have `operator<=>`. Ordering is sythesized based on `operator<` test_ordered_map_container_spaceship_with_type(); // Thanks to SFINAE, the following is not a compiler error but returns `false` @@ -205,4 +207,76 @@ return true; } +// Implementation detail of `test_ordered_set_container_spaceship` +template
typename Container, typename Elem, typename Order> +constexpr void test_ordered_set_spaceship_with_type() { + // Empty containers + { + Container l1; + Container l2; + assert(testOrder(l1, l2, Order::equivalent)); + } + // Identical contents + { + Container l1{1, 1, 2}; + Container l2{1, 1, 2}; + assert(testOrder(l1, l2, Order::equivalent)); + } + // Less, due to contained values + { + Container l1{1, 1, 2, 3}; + Container l2{1, 2, 2, 4}; + assert(testOrder(l1, l2, Order::less)); + } + // Greater, due to contained values + { + Container l1{1, 2, 2, 4}; + Container l2{1, 1, 2, 3}; + assert(testOrder(l1, l2, Order::greater)); + } + // Shorter list + { + Container l1{1, 1, 2, 2}; + Container l2{1, 1, 2, 2, 3}; + assert(testOrder(l1, l2, Order::less)); + } + // Longer list + { + Container l1{1, 1, 2, 2, 3}; + Container l2{1, 1, 2, 2}; + assert(testOrder(l1, l2, Order::greater)); + } + // Unordered + if constexpr (std::is_same_v) { + Container l1{1, std::numeric_limits::min()}; + Container l2{1, 2}; + assert(testOrder(l1, l2, Order::unordered)); + } +} + +// Tests the `operator<=>` on ordered set containers +template
typename Container> +constexpr bool test_ordered_set_container_spaceship() { + // The container should fulfil `std::three_way_comparable` + static_assert(std::three_way_comparable>); + + // Test different comparison categories + test_ordered_set_spaceship_with_type(); + test_ordered_set_spaceship_with_type(); + test_ordered_set_spaceship_with_type(); + // `set` doesn't support `std::partial_ordering` + if constexpr (!std::is_same_v, std::set>) { + test_ordered_set_spaceship_with_type(); + } + + // `LessAndEqComp` does not have `operator<=>`. Ordering is sythesized based on `operator<` + test_ordered_set_spaceship_with_type(); + + // Thanks to SFINAE, the following is not a compiler error but returns `false` + struct NonComparable {}; + static_assert(!std::three_way_comparable>); + + return true; +} + #endif