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 @@ -55,7 +55,7 @@ | `[alg.reverse] `_,std::reverse,Nikolas Klauser,|Not Started| | `[alg.reverse] `_,std::reverse_copy,Nikolas Klauser,|Not Started| | `[alg.rotate] `_,std::rotate,Nikolas Klauser,|Not Started| -| `[alg.rotate] `_,std::rotate_copy,Nikolas Klauser,|Not Started| +| `[alg.rotate] `_,std::rotate_copy,Nikolas Klauser,|Complete| | `[alg.search] `_,std::search,Nikolas Klauser,|Not Started| | `[alg.search] `_,std::search_n,Nikolas Klauser,|Not Started| | `[alg.set.operations] `_,std::set_difference,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 @@ -96,6 +96,7 @@ __algorithm/pstl_merge.h __algorithm/pstl_move.h __algorithm/pstl_replace.h + __algorithm/pstl_rotate_copy.h __algorithm/pstl_sort.h __algorithm/pstl_stable_sort.h __algorithm/pstl_transform.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 @@ -170,6 +170,10 @@ _Pred __pred, const _Tp& __new_value); + template + optional<_Iterator> __pstl_rotate_copy( + _Backend, _Iterator __first, _Iterator __middle, _Iterator __last, _OutIterator __result); + template optional<__empty> __pstl_sort(_Backend, _Iterator __first, _Iterator __last, _Comp __comp); diff --git a/libcxx/include/__algorithm/pstl_rotate_copy.h b/libcxx/include/__algorithm/pstl_rotate_copy.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/pstl_rotate_copy.h @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// 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_ROTATE_COPY_H +#define _LIBCPP___ALGORITHM_PSTL_ROTATE_COPY_H + +#include <__algorithm/pstl_backend.h> +#include <__algorithm/pstl_copy.h> +#include <__algorithm/pstl_frontend_dispatch.h> +#include <__type_traits/is_execution_policy.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_rotate_copy(); + +template , + enable_if_t, int> = 0> +[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> +__rotate_copy(_ExecutionPolicy&& __policy, + _ForwardIterator&& __first, + _ForwardIterator&& __middle, + _ForwardIterator&& __last, + _ForwardOutIterator&& __result) noexcept { + return std::__pstl_frontend_dispatch( + _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_rotate_copy, _RawPolicy), + [&__policy](_ForwardIterator __g_first, + _ForwardIterator __g_middle, + _ForwardIterator __g_last, + _ForwardOutIterator __g_result) -> optional<_ForwardOutIterator> { + auto __result_mid = + std::__copy(__policy, _ForwardIterator(__g_middle), std::move(__g_last), std::move(__g_result)); + if (!__result_mid) + return nullopt; + return std::__copy(__policy, std::move(__g_first), std::move(__g_middle), *std::move(__result_mid)); + }, + std::move(__first), + std::move(__middle), + std::move(__last), + std::move(__result)); +} + +template , + enable_if_t, int> = 0> +_LIBCPP_HIDE_FROM_ABI _ForwardOutIterator rotate_copy( + _ExecutionPolicy&& __policy, + _ForwardIterator __first, + _ForwardIterator __middle, + _ForwardIterator __last, + _ForwardOutIterator __result) { + auto __res = + std::__rotate_copy(__policy, std::move(__first), std::move(__middle), std::move(__last), std::move(__result)); + if (!__res) + std::__throw_bad_alloc(); + return *__res; +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +#endif // _LIBCPP___ALGORITHM_PSTL_ROTATE_COPY_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -1835,6 +1835,7 @@ #include <__algorithm/pstl_merge.h> #include <__algorithm/pstl_move.h> #include <__algorithm/pstl_replace.h> +#include <__algorithm/pstl_rotate_copy.h> #include <__algorithm/pstl_sort.h> #include <__algorithm/pstl_stable_sort.h> #include <__algorithm/pstl_transform.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 @@ -228,6 +228,16 @@ return __empty{}; } +bool pstl_rotate_copy_called = false; + +template +optional +__pstl_rotate_copy(TestBackend, ForwardIterator, ForwardIterator, ForwardIterator, ForwardOutIterator res) { + assert(!pstl_rotate_copy_called); + pstl_rotate_copy_called = true; + return res; +} + bool pstl_unary_transform_called = false; template @@ -380,6 +390,8 @@ 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::rotate_copy(TestPolicy{}, std::begin(a), std::begin(a), std::end(a), std::begin(a)); + assert(std::pstl_rotate_copy_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)); diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.exception_handling.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.exception_handling.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.exception_handling.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// 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: no-exceptions +// REQUIRES: has-unix-headers + +// UNSUPPORTED: libcpp-has-no-incomplete-pstl + +// check that std::find(ExecutionPolicy), std::find_if(ExecutionPolicy) and std::find_if_not(ExecutionPolicy) terminate +// on user-thrown exceptions + +#include + +#include "check_assertion.h" +#include "test_execution_policies.h" +#include "test_iterators.h" + +int main(int, char**) { + test_execution_policies([](auto&& policy) { + EXPECT_STD_TERMINATE([&] { + try { + int a[] = {1, 2}; + int b[] = {1, 2}; + (void)std::rotate_copy( + policy, + util::throw_on_move_iterator(std::begin(a), 1), + util::throw_on_move_iterator(std::begin(a), 1), + util::throw_on_move_iterator(std::end(a), 1), + util::throw_on_move_iterator(std::begin(b), 1)); + } catch (const util::iterator_error&) { + assert(false); + } + std::terminate(); // make the test pass in case the algorithm didn't move the iterator + }); + }); +} diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp @@ -0,0 +1,98 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// ForwardIterator2 +// rotate_copy(ExecutionPolicy&& exec, +// ForwardIterator1 first, ForwardIterator1 middle, ForwardIterator1 last, +// ForwardIterator2 result); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_execution_policies.h" +#include "test_iterators.h" + +template +struct Test { + template + void operator()(Policy&& policy) { + { // simple test + int in[] = {1, 2, 3, 4}; + int out[std::size(in)]; + + decltype(auto) ret = std::rotate_copy(policy, Iter(in), Iter(in + 2), Iter(in + 4), Iter(out)); + static_assert(std::is_same_v); + assert(base(ret) == out + 4); + + int expected[] = {3, 4, 1, 2}; + assert(std::equal(out, out + 4, expected)); + } + { // rotating an empty range works + std::array in = {}; + std::array out = {}; + + decltype(auto) ret = + std::rotate_copy(policy, Iter(in.data()), Iter(in.data()), Iter(in.data()), Iter(out.data())); + static_assert(std::is_same_v); + assert(base(ret) == out.data()); + } + { // rotating an single-element range works + int in[] = {1}; + int out[std::size(in)]; + + decltype(auto) ret = std::rotate_copy(policy, Iter(in), Iter(in), Iter(in + 1), Iter(out)); + static_assert(std::is_same_v); + assert(base(ret) == out + 1); + + int expected[] = {1}; + assert(std::equal(out, out + 1, expected)); + } + { // rotating a two-element range works + int in[] = {1, 2}; + int out[std::size(in)]; + + decltype(auto) ret = std::rotate_copy(policy, Iter(in), Iter(in + 1), Iter(in + 2), Iter(out)); + static_assert(std::is_same_v); + assert(base(ret) == out + 2); + + int expected[] = {2, 1}; + assert(std::equal(out, out + 2, expected)); + } + { // rotating a large range works + std::vector data(100); + std::iota(data.begin(), data.end(), 0); + for (int i = 0; i != 100; ++i) { // check all permutations + auto copy = data; + std::vector out(100); + std::rotate_copy(Iter(data.data()), Iter(data.data() + i), Iter(data.data() + data.size()), Iter(out.data())); + assert(out[0] == i); + assert(std::adjacent_find(out.begin(), out.end(), [](int lhs, int rhs) { + return lhs == 99 ? rhs != 0 : lhs != rhs - 1; + }) == out.end()); + assert(copy == data); + } + } + } +}; + +int main(int, char**) { + types::for_each(types::forward_iterator_list{}, TestIteratorWithPolicies{}); + + return 0; +}