Skip to content

Commit d51a84b

Browse files
committed
[libc++][PSTL] Implement std::stable_sort
Reviewed By: #libc, ldionne Spies: ldionne, libcxx-commits Differential Revision: https://reviews.llvm.org/D151573
1 parent 722832e commit d51a84b

File tree

11 files changed

+260
-0
lines changed

11 files changed

+260
-0
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ set(files
8080
__algorithm/pstl_backends/cpu_backends/for_each.h
8181
__algorithm/pstl_backends/cpu_backends/merge.h
8282
__algorithm/pstl_backends/cpu_backends/serial.h
83+
__algorithm/pstl_backends/cpu_backends/stable_sort.h
8384
__algorithm/pstl_backends/cpu_backends/thread.h
8485
__algorithm/pstl_backends/cpu_backends/transform.h
8586
__algorithm/pstl_backends/cpu_backends/transform_reduce.h
@@ -89,6 +90,7 @@ set(files
8990
__algorithm/pstl_for_each.h
9091
__algorithm/pstl_frontend_dispatch.h
9192
__algorithm/pstl_merge.h
93+
__algorithm/pstl_stable_sort.h
9294
__algorithm/pstl_transform.h
9395
__algorithm/push_heap.h
9496
__algorithm/ranges_adjacent_find.h

libcxx/include/__algorithm/pstl_backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ A PSTL parallel backend is a tag type to which the following functions are assoc
3232
template <class _ExecutionPolicy, class _Iterator, class _Predicate>
3333
_Iterator __pstl_find_if(_Backend, _Iterator __first, _Iterator __last, _Predicate __pred);
3434
35+
template <class _ExecutionPolicy, class _RandomAccessIterator, class _Comp>
36+
void __pstl_stable_sort(_Backend, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp);
37+
3538
template <class _ExecutionPolicy, class _InIterator, class _OutIterator, class _UnaryOperation>
3639
_OutIterator __pstl_transform(_InIterator __first, _InIterator __last, _OutIterator __result, _UnaryOperation __op);
3740

libcxx/include/__algorithm/pstl_backends/cpu_backend.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
_Compare __comp,
3838
_LeafMerge __leaf_merge);
3939
40+
template <class _RandomAccessIterator, class _Comp, class _LeafSort>
41+
void __parallel_stable_sort(_RandomAccessIterator __first,
42+
_RandomAccessIterator __last,
43+
_Comp __comp,
44+
_LeafSort __leaf_sort);
45+
4046
TODO: Document the parallel backend
4147
*/
4248

@@ -46,6 +52,7 @@
4652
#include <__algorithm/pstl_backends/cpu_backends/find_if.h>
4753
#include <__algorithm/pstl_backends/cpu_backends/for_each.h>
4854
#include <__algorithm/pstl_backends/cpu_backends/merge.h>
55+
#include <__algorithm/pstl_backends/cpu_backends/stable_sort.h>
4956
#include <__algorithm/pstl_backends/cpu_backends/transform.h>
5057
#include <__algorithm/pstl_backends/cpu_backends/transform_reduce.h>
5158

libcxx/include/__algorithm/pstl_backends/cpu_backends/serial.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <__config>
1414
#include <__utility/move.h>
15+
#include <cstddef>
1516

1617
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1718
# pragma GCC system_header
@@ -35,6 +36,12 @@ __parallel_transform_reduce(_Index __first, _Index __last, _UnaryOp, _Tp __init,
3536
return __reduce(std::move(__first), std::move(__last), std::move(__init));
3637
}
3738

39+
template <class _RandomAccessIterator, class _Compare, class _LeafSort>
40+
_LIBCPP_HIDE_FROM_ABI void __parallel_stable_sort(
41+
_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort) {
42+
__leaf_sort(__first, __last, __comp);
43+
}
44+
3845
_LIBCPP_HIDE_FROM_ABI inline void __cancel_execution() {}
3946

4047
template <class _RandomAccessIterator1,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
10+
#define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H
11+
12+
#include <__algorithm/pstl_backends/cpu_backends/backend.h>
13+
#include <__algorithm/stable_sort.h>
14+
#include <__config>
15+
#include <__type_traits/is_execution_policy.h>
16+
#include <__utility/terminate_on_exception.h>
17+
18+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19+
# pragma GCC system_header
20+
#endif
21+
22+
#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
23+
24+
_LIBCPP_BEGIN_NAMESPACE_STD
25+
26+
template <class _ExecutionPolicy, class _RandomAccessIterator, class _Comp>
27+
_LIBCPP_HIDE_FROM_ABI void
28+
__pstl_stable_sort(__cpu_backend_tag, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) {
29+
if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy>) {
30+
std::__terminate_on_exception([&] {
31+
__par_backend::__parallel_stable_sort(
32+
__first, __last, __comp, [](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
33+
std::stable_sort(__g_first, __g_last, __g_comp);
34+
});
35+
});
36+
} else {
37+
std::stable_sort(__first, __last, __comp);
38+
}
39+
}
40+
41+
_LIBCPP_END_NAMESPACE_STD
42+
43+
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
44+
45+
#endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_STABLE_SORT_H

libcxx/include/__algorithm/pstl_backends/cpu_backends/thread.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <__assert>
1313
#include <__config>
1414
#include <__utility/move.h>
15+
#include <cstddef>
1516

1617
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1718
# pragma GCC system_header
@@ -38,6 +39,12 @@ __parallel_transform_reduce(_Index __first, _Index __last, _UnaryOp, _Tp __init,
3839
return __reduce(std::move(__first), std::move(__last), std::move(__init));
3940
}
4041

42+
template <class _RandomAccessIterator, class _Compare, class _LeafSort>
43+
_LIBCPP_HIDE_FROM_ABI void __parallel_stable_sort(
44+
_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort) {
45+
__leaf_sort(__first, __last, __comp);
46+
}
47+
4148
_LIBCPP_HIDE_FROM_ABI inline void __cancel_execution() {}
4249

4350
template <class _RandomAccessIterator1,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H
10+
#define _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H
11+
12+
#include <__algorithm/pstl_backend.h>
13+
#include <__config>
14+
#include <__functional/operations.h>
15+
#include <__type_traits/is_execution_policy.h>
16+
#include <__type_traits/remove_cvref.h>
17+
18+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19+
# pragma GCC system_header
20+
#endif
21+
22+
#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
23+
24+
_LIBCPP_BEGIN_NAMESPACE_STD
25+
26+
template <class _ExecutionPolicy,
27+
class _RandomAccessIterator,
28+
class _Comp = less<>,
29+
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
30+
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
31+
_LIBCPP_HIDE_FROM_ABI void
32+
stable_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp = {}) {
33+
using _Backend = typename __select_backend<_RawPolicy>::type;
34+
std::__pstl_stable_sort<_RawPolicy>(_Backend{}, std::move(__first), std::move(__last), std::move(__comp));
35+
}
36+
37+
_LIBCPP_END_NAMESPACE_STD
38+
39+
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
40+
41+
#endif // _LIBCPP___ALGORITHM_PSTL_STABLE_SORT_H

libcxx/include/algorithm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,7 @@ template <class BidirectionalIterator, class Compare>
18061806
#include <__algorithm/pstl_find.h>
18071807
#include <__algorithm/pstl_for_each.h>
18081808
#include <__algorithm/pstl_merge.h>
1809+
#include <__algorithm/pstl_stable_sort.h>
18091810
#include <__algorithm/pstl_transform.h>
18101811
#include <__algorithm/push_heap.h>
18111812
#include <__algorithm/ranges_adjacent_find.h>

libcxx/include/module.modulemap.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ module std [system] {
345345
module pstl_backends_cpu_backends_serial {
346346
private header "__algorithm/pstl_backends/cpu_backends/serial.h"
347347
}
348+
module pstl_backends_cpu_backends_stable_sort {
349+
private header "__algorithm/pstl_backends/cpu_backends/stable_sort.h"
350+
}
348351
module pstl_backends_cpu_backends_thread {
349352
private header "__algorithm/pstl_backends/cpu_backends/thread.h"
350353
}

libcxx/test/libcxx/private_headers.verify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ END-SCRIPT
123123
#include <__algorithm/pstl_backends/cpu_backends/for_each.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/for_each.h'}}
124124
#include <__algorithm/pstl_backends/cpu_backends/merge.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/merge.h'}}
125125
#include <__algorithm/pstl_backends/cpu_backends/serial.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/serial.h'}}
126+
#include <__algorithm/pstl_backends/cpu_backends/stable_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/stable_sort.h'}}
126127
#include <__algorithm/pstl_backends/cpu_backends/thread.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/thread.h'}}
127128
#include <__algorithm/pstl_backends/cpu_backends/transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/transform.h'}}
128129
#include <__algorithm/pstl_backends/cpu_backends/transform_reduce.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pstl_backends/cpu_backends/transform_reduce.h'}}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// UNSUPPORTED: libcpp-has-no-incomplete-pstl
12+
13+
// <algorithm>
14+
15+
// template<class ExecutionPolicy, class RandomAccessIterator>
16+
// void stable_sort(ExecutionPolicy&& exec,
17+
// RandomAccessIterator first, RandomAccessIterator last);
18+
//
19+
// template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
20+
// void stable_sort(ExecutionPolicy&& exec,
21+
// RandomAccessIterator first, RandomAccessIterator last,
22+
// Compare comp);
23+
24+
#include <algorithm>
25+
#include <array>
26+
#include <atomic>
27+
#include <cassert>
28+
#include <vector>
29+
30+
#include "test_macros.h"
31+
#include "test_execution_policies.h"
32+
#include "test_iterators.h"
33+
34+
EXECUTION_POLICY_SFINAE_TEST(stable_sort);
35+
36+
static_assert(sfinae_test_stable_sort<int, int*, int*, bool (*)(int)>);
37+
static_assert(!sfinae_test_stable_sort<std::execution::parallel_policy, int*, int*, bool (*)(int, int)>);
38+
39+
struct OrderedValue {
40+
int value;
41+
double original_order;
42+
bool operator==(const OrderedValue& other) const { return other.value == value; }
43+
44+
auto operator<(const OrderedValue& rhs) const { return value < rhs.value; }
45+
auto operator>(const OrderedValue& rhs) const { return value > rhs.value; }
46+
};
47+
48+
template <class Iter, std::size_t N>
49+
void test_one(std::array<int, N> input, std::array<int, N> expected) {
50+
std::stable_sort(Iter(input.data()), Iter(input.data() + input.size()));
51+
assert(input == expected);
52+
}
53+
54+
template <class Iter>
55+
struct Test {
56+
template <class Policy>
57+
void operator()(Policy&& policy) {
58+
59+
// Empty sequence.
60+
test_one<Iter, 0>({}, {});
61+
// 1-element sequence.
62+
test_one<Iter, 1>({1}, {1});
63+
// 2-element sequence.
64+
test_one<Iter, 2>({2, 1}, {1, 2});
65+
// 3-element sequence.
66+
test_one<Iter, 3>({2, 1, 3}, {1, 2, 3});
67+
// Longer sequence.
68+
test_one<Iter, 8>({2, 1, 3, 6, 8, 4, 11, 5}, {1, 2, 3, 4, 5, 6, 8, 11});
69+
// Longer sequence with duplicates.
70+
test_one<Iter, 7>({2, 1, 3, 6, 2, 8, 6}, {1, 2, 2, 3, 6, 6, 8});
71+
// All elements are the same.
72+
test_one<Iter, 3>({1, 1, 1}, {1, 1, 1});
73+
// Already sorted.
74+
test_one<Iter, 5>({1, 2, 3, 4, 5}, {1, 2, 3, 4, 5});
75+
// Reverse-sorted.
76+
test_one<Iter, 5>({5, 4, 3, 2, 1}, {1, 2, 3, 4, 5});
77+
// Repeating pattern.
78+
test_one<Iter, 6>({1, 2, 1, 2, 1, 2}, {1, 1, 1, 2, 2, 2});
79+
80+
{ // The sort is stable (equivalent elements remain in the same order).
81+
using V = OrderedValue;
82+
using Array = std::array<V, 20>;
83+
Array in = {V{10, 10.1}, {12, 12.1}, {3, 3.1}, {5, 5.1}, {3, 3.2}, {3, 3.3}, {11, 11.1},
84+
{12, 12.2}, {4, 4.1}, {4, 4.2}, {4, 4.3}, {1, 1.1}, {6, 6.1}, {3, 3.4},
85+
{10, 10.2}, {8, 8.1}, {12, 12.3}, {1, 1.2}, {1, 1.3}, {5, 5.2}};
86+
Array expected = {V{1, 1.1}, {1, 1.2}, {1, 1.3}, {3, 3.1}, {3, 3.2}, {3, 3.3}, {3, 3.4},
87+
{4, 4.1}, {4, 4.2}, {4, 4.3}, {5, 5.1}, {5, 5.2}, {6, 6.1}, {8, 8.1},
88+
{10, 10.1}, {10, 10.2}, {11, 11.1}, {12, 12.1}, {12, 12.2}, {12, 12.3}};
89+
90+
std::stable_sort(policy, in.begin(), in.end());
91+
assert(in == expected);
92+
}
93+
94+
{ // A custom comparator works and is stable.
95+
using V = OrderedValue;
96+
using Array = std::array<V, 11>;
97+
98+
Array in = {
99+
V{1, 1.1},
100+
{2, 2.1},
101+
{2, 2.2},
102+
{3, 3.1},
103+
{2, 2.3},
104+
{3, 3.2},
105+
{4, 4.1},
106+
{5, 5.1},
107+
{2, 2.4},
108+
{5, 5.2},
109+
{1, 1.2}};
110+
Array expected = {
111+
V{5, 5.1},
112+
{5, 5.2},
113+
{4, 4.1},
114+
{3, 3.1},
115+
{3, 3.2},
116+
{2, 2.1},
117+
{2, 2.2},
118+
{2, 2.3},
119+
{2, 2.4},
120+
{1, 1.1},
121+
{1, 1.2}};
122+
123+
std::stable_sort(policy, in.begin(), in.end(), std::greater{});
124+
assert(in == expected);
125+
}
126+
}
127+
};
128+
129+
int main(int, char**) {
130+
types::for_each(types::random_access_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
131+
132+
#ifndef TEST_HAS_NO_EXCEPTIONS
133+
std::set_terminate(terminate_successful);
134+
int a[] = {1, 2};
135+
try {
136+
std::stable_sort(std::execution::par, std::begin(a), std::end(a), [](int, int) -> bool { throw int{}; });
137+
} catch (int) {
138+
assert(false);
139+
}
140+
#endif
141+
142+
return 0;
143+
}

0 commit comments

Comments
 (0)