[libcxx-commits] [libcxx] 5d7f346 - [libc++][PSTL] Implement std::rotate_copy
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 24 05:02:42 PDT 2023
Author: Nikolas Klauser
Date: 2023-10-24T14:02:37+02:00
New Revision: 5d7f346bd398fa1d7d9b7480a2996a39bd9062e1
URL: https://github.com/llvm/llvm-project/commit/5d7f346bd398fa1d7d9b7480a2996a39bd9062e1
DIFF: https://github.com/llvm/llvm-project/commit/5d7f346bd398fa1d7d9b7480a2996a39bd9062e1.diff
LOG: [libc++][PSTL] Implement std::rotate_copy
Reviewed By: #libc, ldionne
Spies: ldionne, libcxx-commits
Differential Revision: https://reviews.llvm.org/D155025
Added:
libcxx/include/__algorithm/pstl_rotate_copy.h
libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.exception_handling.pass.cpp
libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp
Modified:
libcxx/docs/Status/PSTLPaper.csv
libcxx/include/CMakeLists.txt
libcxx/include/__algorithm/pstl_backend.h
libcxx/include/algorithm
libcxx/test/libcxx/algorithms/pstl.robust_against_customization_points_not_working.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/docs/Status/PSTLPaper.csv b/libcxx/docs/Status/PSTLPaper.csv
index 1ca0a6ed3539b92..f8cf950847c62cf 100644
--- a/libcxx/docs/Status/PSTLPaper.csv
+++ b/libcxx/docs/Status/PSTLPaper.csv
@@ -55,7 +55,7 @@ Section,Description,Assignee,Complete
| `[alg.reverse] <https://wg21.link/alg.reverse>`_,std::reverse,Nikolas Klauser,|Not Started|
| `[alg.reverse] <https://wg21.link/alg.reverse>`_,std::reverse_copy,Nikolas Klauser,|Not Started|
| `[alg.rotate] <https://wg21.link/alg.rotate>`_,std::rotate,Nikolas Klauser,|Not Started|
-| `[alg.rotate] <https://wg21.link/alg.rotate>`_,std::rotate_copy,Nikolas Klauser,|Not Started|
+| `[alg.rotate] <https://wg21.link/alg.rotate>`_,std::rotate_copy,Nikolas Klauser,|Complete|
| `[alg.search] <https://wg21.link/alg.search>`_,std::search,Nikolas Klauser,|Not Started|
| `[alg.search] <https://wg21.link/alg.search>`_,std::search_n,Nikolas Klauser,|Not Started|
| `[alg.set.operations] <https://wg21.link/alg.set.operations>`_,std::set_
diff erence,Nikolas Klauser,|Not Started|
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index e425af4fda8d6b5..1f36ff6b3896e46 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -96,6 +96,7 @@ set(files
__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
index 1f5c13d49eda1bc..dcb1c99e7962e63 100644
--- a/libcxx/include/__algorithm/pstl_backend.h
+++ b/libcxx/include/__algorithm/pstl_backend.h
@@ -170,6 +170,10 @@ implemented, all the algorithms will eventually forward to the basis algorithms
_Pred __pred,
const _Tp& __new_value);
+ template <class _ExecutionPolicy, class _Iterator, class _OutIterator>
+ optional<_Iterator> __pstl_rotate_copy(
+ _Backend, _Iterator __first, _Iterator __middle, _Iterator __last, _OutIterator __result);
+
template <class _ExecutionPolicy, class _Iterator, class _Comp>
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
index 000000000000000..ea2cf6db1716b6f
--- /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 <class>
+void __pstl_rotate_copy();
+
+template <class _ExecutionPolicy,
+ class _ForwardIterator,
+ class _ForwardOutIterator,
+ class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
+ enable_if_t<is_execution_policy_v<_RawPolicy>, 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 <class _ExecutionPolicy,
+ class _ForwardIterator,
+ class _ForwardOutIterator,
+ class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
+ enable_if_t<is_execution_policy_v<_RawPolicy>, 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
index ac335cb39e829c9..7dcf396b683db4f 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -1835,6 +1835,7 @@ template <class BidirectionalIterator, class Compare>
#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
index 071fc50b2fe540c..25aed98fe136683 100644
--- 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 @@ __pstl_replace_copy_if(TestBackend, ForwardIterator, ForwardIterator, ForwardOut
return __empty{};
}
+bool pstl_rotate_copy_called = false;
+
+template <class, class ForwardIterator, class ForwardOutIterator>
+optional<ForwardOutIterator>
+__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 <class, class ForwardIterator, class ForwardOutIterator, class UnaryOperation>
@@ -380,6 +390,8 @@ int main(int, char**) {
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
index 000000000000000..d0b77661334d77e
--- /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 <algorithm>
+
+#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
index 000000000000000..989313cc6d68d5c
--- /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
+
+// <algorithm>
+
+// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
+// ForwardIterator2
+// rotate_copy(ExecutionPolicy&& exec,
+// ForwardIterator1 first, ForwardIterator1 middle, ForwardIterator1 last,
+// ForwardIterator2 result);
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <numeric>
+#include <vector>
+
+#include "test_macros.h"
+#include "test_execution_policies.h"
+#include "test_iterators.h"
+
+template <class Iter>
+struct Test {
+ template <class Policy>
+ 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<decltype(ret), Iter>);
+ 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<int, 0> in = {};
+ std::array<int, 0> 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<decltype(ret), Iter>);
+ 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<decltype(ret), Iter>);
+ 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<decltype(ret), Iter>);
+ assert(base(ret) == out + 2);
+
+ int expected[] = {2, 1};
+ assert(std::equal(out, out + 2, expected));
+ }
+ { // rotating a large range works
+ std::vector<int> data(100);
+ std::iota(data.begin(), data.end(), 0);
+ for (int i = 0; i != 100; ++i) { // check all permutations
+ auto copy = data;
+ std::vector<int> 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<int*>{}, TestIteratorWithPolicies<Test>{});
+
+ return 0;
+}
More information about the libcxx-commits
mailing list