diff --git a/libcxx/docs/Status/PSTLPaper.csv b/libcxx/docs/Status/PSTLPaper.csv --- a/libcxx/docs/Status/PSTLPaper.csv +++ b/libcxx/docs/Status/PSTLPaper.csv @@ -62,7 +62,7 @@ | `[alg.set.operations] `_,std::set_intersection,Nikolas Klauser,|Not Started| | `[alg.set.operations] `_,std::set_symmetric_difference,Nikolas Klauser,|Not Started| | `[alg.set.operations] `_,std::set_union,Nikolas Klauser,|Not Started| -| `[alg.sort] `_,std::sort,Nikolas Klauser,|Not Started| +| `[alg.sort] `_,std::sort,Nikolas Klauser,|Complete| | `[alg.partitions] `_,std::stable_partition,Nikolas Klauser,|Not Started| | `[alg.sort] `_,std::stable_sort,Nikolas Klauser,|Complete| | `[alg.swap] `_,std::swap_ranges,Nikolas Klauser,|Not Started| diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -93,6 +93,7 @@ __algorithm/pstl_generate.h __algorithm/pstl_merge.h __algorithm/pstl_replace.h + __algorithm/pstl_sort.h __algorithm/pstl_stable_sort.h __algorithm/pstl_transform.h __algorithm/push_heap.h diff --git a/libcxx/include/__algorithm/pstl_backend.h b/libcxx/include/__algorithm/pstl_backend.h --- a/libcxx/include/__algorithm/pstl_backend.h +++ b/libcxx/include/__algorithm/pstl_backend.h @@ -147,6 +147,9 @@ _Pred __pred, const _Tp& __new_value); + template + void __pstl_sort(_Backend, _Iterator __first, _Iterator __last, _Comp __comp); + // TODO: Complete this list */ diff --git a/libcxx/include/__algorithm/pstl_sort.h b/libcxx/include/__algorithm/pstl_sort.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/pstl_sort.h @@ -0,0 +1,52 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_PSTL_SORT_H +#define _LIBCPP___ALGORITHM_PSTL_SORT_H + +#include <__algorithm/pstl_backend.h> +#include <__algorithm/pstl_frontend_dispatch.h> +#include <__algorithm/pstl_stable_sort.h> +#include <__config> +#include <__functional/operations.h> +#include <__type_traits/is_execution_policy.h> +#include <__type_traits/remove_cvref.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +template +void __pstl_sort(); + +template , + class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>, + enable_if_t, int> = 0> +_LIBCPP_HIDE_FROM_ABI void +sort(_ExecutionPolicy&& __policy, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp = {}) { + std::__pstl_frontend_dispatch( + _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_sort), + [&__policy](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) { + std::stable_sort(__policy, __g_first, __g_last, __g_comp); + }, + std::move(__first), + std::move(__last), + std::move(__comp)); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_SORT_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -1818,6 +1818,7 @@ #include <__algorithm/pstl_generate.h> #include <__algorithm/pstl_merge.h> #include <__algorithm/pstl_replace.h> +#include <__algorithm/pstl_sort.h> #include <__algorithm/pstl_stable_sort.h> #include <__algorithm/pstl_transform.h> #include <__algorithm/push_heap.h> diff --git a/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp b/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp --- a/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp +++ b/libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp @@ -216,6 +216,22 @@ return {}; } +bool pstl_sort_called = false; + +template +void __pstl_sort(TestBackend, RandomAccessIterator, RandomAccessIterator, Comp) { + assert(!pstl_sort_called); + pstl_sort_called = true; +} + +bool pstl_stable_sort_called = false; + +template +void __pstl_stable_sort(TestBackend, RandomAccessIterator, RandomAccessIterator, Comp) { + assert(!pstl_stable_sort_called); + pstl_stable_sort_called = true; +} + bool pstl_unary_transform_reduce_called = false; template @@ -303,6 +319,10 @@ assert(std::pstl_reduce_with_init_called); (void)std::reduce(TestPolicy{}, std::begin(a), std::end(a)); assert(std::pstl_reduce_without_init_called); + (void)std::sort(TestPolicy{}, std::begin(a), std::end(a)); + assert(std::pstl_sort_called); + (void)std::stable_sort(TestPolicy{}, std::begin(a), std::end(a)); + assert(std::pstl_stable_sort_called); (void)std::transform_reduce(TestPolicy{}, std::begin(a), std::end(a), 0, pred, pred); assert(std::pstl_unary_transform_reduce_called); (void)std::transform_reduce(TestPolicy{}, std::begin(a), std::end(a), std::begin(a), 0, pred, pred); diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/pstl.sort.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/pstl.sort.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/pstl.sort.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// UNSUPPORTED: libcpp-has-no-incomplete-pstl + +// template +// void sort(ExecutionPolicy&& exec, +// RandomAccessIterator first, RandomAccessIterator last); +// +// template +// void sort(ExecutionPolicy&& exec, +// RandomAccessIterator first, RandomAccessIterator last, +// Compare comp); + +#include +#include +#include +#include +#include + +#include "test_execution_policies.h" +#include "test_iterators.h" + +template +struct Test { + template + void operator()(ExecutionPolicy&& policy) { + { // simple test + int in[] = {1, 2, 3, 2, 6, 4}; + int expected[] = {1, 2, 2, 3, 4, 6}; + std::sort(policy, Iter(std::begin(in)), Iter(std::end(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // empty range works + int in[] = {1, 2, 3, 2, 6, 4}; + int expected[] = {1, 2, 3, 2, 6, 4}; + std::sort(policy, Iter(std::begin(in)), Iter(std::begin(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // single element range works + int in[] = {1}; + int expected[] = {1}; + std::sort(policy, Iter(std::begin(in)), Iter(std::begin(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // two element range works + int in[] = {2, 1}; + int expected[] = {1, 2}; + std::sort(policy, Iter(std::begin(in)), Iter(std::end(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // three element range works + int in[] = {2, 1, 4}; + int expected[] = {1, 2, 4}; + std::sort(policy, Iter(std::begin(in)), Iter(std::end(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // longer range works + int in[] = {2, 1, 4, 4, 7, 2, 4, 1, 6}; + int expected[] = {1, 1, 2, 2, 4, 4, 4, 6, 7}; + std::sort(policy, Iter(std::begin(in)), Iter(std::end(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // already sorted + int in[] = {1, 1, 2, 2, 4, 4, 4, 6, 7}; + int expected[] = {1, 1, 2, 2, 4, 4, 4, 6, 7}; + std::sort(policy, Iter(std::begin(in)), Iter(std::end(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // reversed + int in[] = {7, 6, 4, 4, 4, 2, 2, 1, 1}; + int expected[] = {1, 1, 2, 2, 4, 4, 4, 6, 7}; + std::sort(policy, Iter(std::begin(in)), Iter(std::end(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // repeating pattern + int in[] = {1, 2, 3, 1, 2, 3, 1, 2, 3}; + int expected[] = {1, 1, 1, 2, 2, 2, 3, 3, 3}; + std::sort(policy, Iter(std::begin(in)), Iter(std::end(in))); + assert(std::equal(std::begin(in), std::end(in), std::begin(expected))); + } + { // large range + std::vector vec(300); + std::iota(std::begin(vec), std::end(vec), 0); + auto expected = vec; + std::shuffle(std::begin(vec), std::end(vec), std::mt19937_64(std::random_device{}())); + std::sort(Iter(std::data(vec)), Iter(std::data(vec) + std::size(vec))); + assert(vec == expected); + } + } +}; + +int main(int, char**) { + types::for_each(types::random_access_iterator_list{}, TestIteratorWithPolicies{}); + + return 0; +}