-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc++] Implement part of P2562R1: constexpr ranges::stable_sort
#128860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
frederick-vs-ja
merged 5 commits into
llvm:main
from
frederick-vs-ja:constexpr-ranges-stable_sort
Mar 6, 2025
Merged
[libc++] Implement part of P2562R1: constexpr ranges::stable_sort
#128860
frederick-vs-ja
merged 5 commits into
llvm:main
from
frederick-vs-ja:constexpr-ranges-stable_sort
Mar 6, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-libcxx Author: A. Jiang (frederick-vs-ja) ChangesDrive-by: Unblocks test coverage for Fixes #119395. Full diff: https://github.com/llvm/llvm-project/pull/128860.diff 6 Files Affected:
diff --git a/libcxx/include/__algorithm/ranges_stable_sort.h b/libcxx/include/__algorithm/ranges_stable_sort.h
index 9c7df80ae9872..6e17d0d0c7ec4 100644
--- a/libcxx/include/__algorithm/ranges_stable_sort.h
+++ b/libcxx/include/__algorithm/ranges_stable_sort.h
@@ -41,7 +41,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
namespace ranges {
struct __stable_sort {
template <class _Iter, class _Sent, class _Comp, class _Proj>
- _LIBCPP_HIDE_FROM_ABI static _Iter __stable_sort_fn_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
+ _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX26 _Iter
+ __stable_sort_fn_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
auto __last_iter = ranges::next(__first, __last);
auto&& __projected_comp = std::__make_projected(__comp, __proj);
@@ -52,13 +53,14 @@ struct __stable_sort {
template <random_access_iterator _Iter, sentinel_for<_Iter> _Sent, class _Comp = ranges::less, class _Proj = identity>
requires sortable<_Iter, _Comp, _Proj>
- _LIBCPP_HIDE_FROM_ABI _Iter operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Iter
+ operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
return __stable_sort_fn_impl(std::move(__first), std::move(__last), __comp, __proj);
}
template <random_access_range _Range, class _Comp = ranges::less, class _Proj = identity>
requires sortable<iterator_t<_Range>, _Comp, _Proj>
- _LIBCPP_HIDE_FROM_ABI borrowed_iterator_t<_Range>
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 borrowed_iterator_t<_Range>
operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
return __stable_sort_fn_impl(ranges::begin(__r), ranges::end(__r), __comp, __proj);
}
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 7b4cb8e496196..8ef2ba84e6cc5 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -422,11 +422,12 @@ namespace ranges {
template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
class Proj = identity>
requires sortable<I, Comp, Proj>
- I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
+ constexpr I // constexpr in C++26
+ ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
template<random_access_range R, class Comp = ranges::less, class Proj = identity>
requires sortable<iterator_t<R>, Comp, Proj>
- borrowed_iterator_t<R>
+ constexpr borrowed_iterator_t<R> // constexpr in C++26
ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
index c8f76b20c6825..5dedd54a4c009 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
@@ -13,11 +13,12 @@
// template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
// class Proj = identity>
// requires sortable<I, Comp, Proj>
-// I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
+// constexpr I // constexpr since C++26
+// ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
//
// template<random_access_range R, class Comp = ranges::less, class Proj = identity>
// requires sortable<iterator_t<R>, Comp, Proj>
-// borrowed_iterator_t<R>
+// constexpr borrowed_iterator_t<R> // constexpr since C++26
// ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
#include <algorithm>
@@ -57,7 +58,7 @@ static_assert(!HasStableSortR<UncheckedRange<int*>, BadComparator>);
static_assert(!HasStableSortR<UncheckedRange<const int*>>); // Doesn't satisfy `sortable`.
template <class Iter, class Sent, std::size_t N>
-void test_one(std::array<int, N> input, std::array<int, N> expected) {
+TEST_CONSTEXPR_CXX26 void test_one(std::array<int, N> input, std::array<int, N> expected) {
{ // (iterator, sentinel) overload.
auto sorted = input;
auto b = Iter(sorted.data());
@@ -81,7 +82,7 @@ void test_one(std::array<int, N> input, std::array<int, N> expected) {
}
template <class Iter, class Sent>
-void test_iterators_2() {
+TEST_CONSTEXPR_CXX26 void test_iterators_2() {
// Empty sequence.
test_one<Iter, Sent, 0>({}, {});
// 1-element sequence.
@@ -105,25 +106,25 @@ void test_iterators_2() {
}
template <class Iter>
-void test_iterators_1() {
+TEST_CONSTEXPR_CXX26 void test_iterators_1() {
test_iterators_2<Iter, Iter>();
test_iterators_2<Iter, sentinel_wrapper<Iter>>();
}
-void test_iterators() {
+TEST_CONSTEXPR_CXX26 void test_iterators() {
test_iterators_1<random_access_iterator<int*>>();
test_iterators_1<contiguous_iterator<int*>>();
test_iterators_1<int*>();
}
-void test() {
+TEST_CONSTEXPR_CXX26 bool test() {
test_iterators();
struct OrderedValue {
int value;
double original_order;
bool operator==(const OrderedValue&) const = default;
- auto operator<=>(const OrderedValue& rhs) const { return value <=> rhs.value; }
+ TEST_CONSTEXPR_CXX26 auto operator<=>(const OrderedValue& rhs) const { return value <=> rhs.value; }
};
{ // The sort is stable (equivalent elements remain in the same order).
@@ -214,24 +215,24 @@ void test() {
{ // `std::invoke` is used in the implementation.
struct S {
int i;
- S(int i_) : i(i_) {}
+ TEST_CONSTEXPR_CXX26 S(int i_) : i(i_) {}
- bool comparator(const S& rhs) const { return i < rhs.i; }
- const S& projection() const { return *this; }
+ TEST_CONSTEXPR_CXX26 bool comparator(const S& rhs) const { return i < rhs.i; }
+ TEST_CONSTEXPR_CXX26 const S& projection() const { return *this; }
bool operator==(const S&) const = default;
};
{
std::array in = {S{2}, S{3}, S{1}};
- auto last = std::ranges::stable_sort(in.begin(), in.end(), &S::comparator, &S::projection);
+ auto last = std::ranges::stable_sort(in.begin(), in.end(), &S::comparator, &S::projection);
assert((in == std::array{S{1}, S{2}, S{3}}));
assert(last == in.end());
}
{
std::array in = {S{2}, S{3}, S{1}};
- auto last = std::ranges::stable_sort(in, &S::comparator, &S::projection);
+ auto last = std::ranges::stable_sort(in, &S::comparator, &S::projection);
assert((in == std::array{S{1}, S{2}, S{3}}));
assert(last == in.end());
}
@@ -242,8 +243,6 @@ void test() {
std::ranges::stable_sort(std::array{1, 2, 3});
}
- // TODO: Enable the tests once the implementation switched to use iter_move/iter_swap
- /*
{ // ProxyIterator
{
std::array in = {2, 1, 3};
@@ -260,12 +259,15 @@ void test() {
assert((in == std::array{1, 2, 3}));
}
}
- */
+
+ return true;
}
int main(int, char**) {
test();
- // Note: `stable_sort` is not `constexpr`.
+#if TEST_STD_VER >= 26
+ static_assert(test());
+#endif
return 0;
}
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
index 0624a6c2d49c7..299478da22072 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
@@ -200,8 +200,12 @@ constexpr bool test_all() {
if (!std::is_constant_evaluated())
dangling_1st(std::ranges::stable_partition, in, unary_pred);
dangling_1st(std::ranges::sort, in);
+#if TEST_STD_VER < 26
if (!std::is_constant_evaluated())
+#endif
+ {
dangling_1st(std::ranges::stable_sort, in);
+ }
dangling_1st(std::ranges::partial_sort, in, mid);
dangling_1st(std::ranges::nth_element, in, mid);
if (!std::is_constant_evaluated())
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
index acd7640b418eb..a01cdf1d3a179 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
@@ -167,8 +167,12 @@ constexpr bool test_all() {
if (!std::is_constant_evaluated())
test(std::ranges::stable_partition, in, &Foo::unary_pred, &Bar::val);
test(std::ranges::sort, in, &Foo::binary_pred, &Bar::val);
+#if TEST_STD_VER < 26
if (!std::is_constant_evaluated())
+#endif
+ {
test(std::ranges::stable_sort, in, &Foo::binary_pred, &Bar::val);
+ }
test_mid(std::ranges::partial_sort, in, mid, &Foo::binary_pred, &Bar::val);
test_mid(std::ranges::nth_element, in, mid, &Foo::binary_pred, &Bar::val);
if (!std::is_constant_evaluated())
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index ca1433b778751..d85052c51973a 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -170,8 +170,12 @@ constexpr void run_tests() {
if (!std::is_constant_evaluated())
test(std::ranges::stable_partition, in, unary_pred);
test(std::ranges::sort, in);
+#if TEST_STD_VER < 26
if (!std::is_constant_evaluated())
+#endif
+ {
test(std::ranges::stable_sort, in);
+ }
test_mid(std::ranges::partial_sort, in, mid);
test_mid(std::ranges::nth_element, in, mid);
if (!std::is_constant_evaluated())
|
17f461e to
1df42f0
Compare
Drive-by: Enables test coverage for `ranges::stable_sort` with proxy iterators.
1df42f0 to
f4ec08c
Compare
mordante
approved these changes
Mar 3, 2025
Member
mordante
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM, modulo one nit.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Drive-by: Enables test coverage for
ranges::stable_sortwith proxy iterators, and changes "constexpr in" to "constexpr since" in comments in<algorithm>Fixes #119395.