[libcxx-commits] [libcxx] 9d90531 - [libc++][ranges] Implement std::ranges::swap_ranges()
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Feb 10 07:01:50 PST 2022
Author: Nikolas Klauser
Date: 2022-02-10T16:01:45+01:00
New Revision: 9d90531904983c1268cea6d56bfbc1ab2743ec23
URL: https://github.com/llvm/llvm-project/commit/9d90531904983c1268cea6d56bfbc1ab2743ec23
DIFF: https://github.com/llvm/llvm-project/commit/9d90531904983c1268cea6d56bfbc1ab2743ec23.diff
LOG: [libc++][ranges] Implement std::ranges::swap_ranges()
Implement `std::ranges::swap_ranges()`
Reviewed By: Quuxplusone, #libc, ldionne
Spies: ldionne, mgorny, jloser, libcxx-commits
Differential Revision: https://reviews.llvm.org/D116303
Added:
libcxx/include/__algorithm/ranges_swap_ranges.h
libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_swap_ranges.module.verify.cpp
libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp
Modified:
libcxx/docs/Status/RangesAlgorithms.csv
libcxx/include/CMakeLists.txt
libcxx/include/__algorithm/swap_ranges.h
libcxx/include/algorithm
libcxx/include/module.modulemap
libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index 155e6b880fb7e..95940b4932b5f 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -53,7 +53,7 @@ Write,replace,Not assigned,n/a,Not started
Write,replace_if,Not assigned,n/a,Not started
Write,replace_copy,Not assigned,n/a,Not started
Write,replace_copy_if,Not assigned,n/a,Not started
-Write,swap_ranges,Not assigned,n/a,Not started
+Write,swap_ranges,Nikolas Klauser,`D116303 <https://llvm.org/D116303>`_,Complete
Write,reverse_copy,Not assigned,n/a,Not started
Write,rotate_copy,Not assigned,n/a,Not started
Write,sample,Not assigned,n/a,Not started
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 8137afb470f1b..ba7d096feb233 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -63,6 +63,7 @@ set(files
__algorithm/pop_heap.h
__algorithm/prev_permutation.h
__algorithm/push_heap.h
+ __algorithm/ranges_swap_ranges.h
__algorithm/remove.h
__algorithm/remove_copy.h
__algorithm/remove_copy_if.h
diff --git a/libcxx/include/__algorithm/ranges_swap_ranges.h b/libcxx/include/__algorithm/ranges_swap_ranges.h
new file mode 100644
index 0000000000000..cced873836db7
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_swap_ranges.h
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_RANGES_SWAP_RANGES_H
+#define _LIBCPP___ALGORITHM_RANGES_SWAP_RANGES_H
+
+#include <__algorithm/in_in_result.h>
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__iterator/iter_swap.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/dangling.h>
+#include <__utility/move.h>
+#include <type_traits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#ifndef _LIBCPP_HAS_NO_CONCEPTS
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+
+template <class _I1, class _I2>
+using swap_ranges_result = in_in_result<_I1, _I2>;
+
+namespace __swap_ranges {
+struct __fn {
+ template <input_iterator _I1, sentinel_for<_I1> _S1,
+ input_iterator _I2, sentinel_for<_I2> _S2>
+ requires indirectly_swappable<_I1, _I2>
+ _LIBCPP_HIDE_FROM_ABI constexpr swap_ranges_result<_I1, _I2>
+ operator()(_I1 __first1, _S1 __last1, _I2 __first2, _S2 __last2) const {
+ while (__first1 != __last1 && __first2 != __last2) {
+ ranges::iter_swap(__first1, __first2);
+ ++__first1;
+ ++__first2;
+ }
+ return {_VSTD::move(__first1), _VSTD::move(__first2)};
+ }
+
+ template <input_range _R1, input_range _R2>
+ requires indirectly_swappable<iterator_t<_R1>, iterator_t<_R2>>
+ _LIBCPP_HIDE_FROM_ABI constexpr
+ swap_ranges_result<borrowed_iterator_t<_R1>, borrowed_iterator_t<_R2>>
+ operator()(_R1&& __r1, _R2&& __r2) const {
+ return operator()(ranges::begin(__r1), ranges::end(__r1),
+ ranges::begin(__r2), ranges::end(__r2));
+ }
+};
+} // namespace __swap_ranges
+
+inline namespace __cpo {
+ inline constexpr auto swap_ranges = __swap_ranges::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_HAS_NO_RANGES
+
+#endif // _LIBCPP___ALGORITHM_RANGES_SWAP_RANGES_H
diff --git a/libcxx/include/__algorithm/swap_ranges.h b/libcxx/include/__algorithm/swap_ranges.h
index b17ee896b183b..0422265bb4be4 100644
--- a/libcxx/include/__algorithm/swap_ranges.h
+++ b/libcxx/include/__algorithm/swap_ranges.h
@@ -11,7 +11,6 @@
#include <__config>
#include <__utility/swap.h>
-#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 815f28e5ceb1b..21c077637b959 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -199,6 +199,16 @@ template <class ForwardIterator1, class ForwardIterator2>
constexpr ForwardIterator2 // constexpr in C++20
swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
+template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2>
+ requires indirectly_swappable<I1, I2>
+ constexpr ranges::swap_ranges_result<I1, I2>
+ ranges::swap_ranges(I1 first1, S1 last1, I2 first2, S2 last2);
+
+template<input_range R1, input_range R2>
+ requires indirectly_swappable<iterator_t<R1>, iterator_t<R2>>
+ constexpr ranges::swap_ranges_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
+ ranges::swap_ranges(R1&& r1, R2&& r2);
+
template <class ForwardIterator1, class ForwardIterator2>
constexpr void // constexpr in C++20
iter_swap(ForwardIterator1 a, ForwardIterator2 b);
@@ -739,6 +749,7 @@ template<class InputIterator, class OutputIterator>
#include <__algorithm/pop_heap.h>
#include <__algorithm/prev_permutation.h>
#include <__algorithm/push_heap.h>
+#include <__algorithm/ranges_swap_ranges.h>
#include <__algorithm/remove.h>
#include <__algorithm/remove_copy.h>
#include <__algorithm/remove_copy_if.h>
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 5ef2c1c92ade7..528118f97fe42 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -284,6 +284,7 @@ module std [system] {
module pop_heap { private header "__algorithm/pop_heap.h" }
module prev_permutation { private header "__algorithm/prev_permutation.h" }
module push_heap { private header "__algorithm/push_heap.h" }
+ module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" }
module remove { private header "__algorithm/remove.h" }
module remove_copy { private header "__algorithm/remove_copy.h" }
module remove_copy_if { private header "__algorithm/remove_copy_if.h" }
diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_swap_ranges.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_swap_ranges.module.verify.cpp
new file mode 100644
index 0000000000000..edd4c30f5f43a
--- /dev/null
+++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_swap_ranges.module.verify.cpp
@@ -0,0 +1,15 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: modules-build
+
+// WARNING: This test was generated by 'generate_private_header_tests.py'
+// and should not be edited manually.
+
+// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}}
+#include <__algorithm/ranges_swap_ranges.h>
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp
new file mode 100644
index 0000000000000..fd50678c99c80
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp
@@ -0,0 +1,214 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: libcpp-has-no-incomplete-ranges
+
+// template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2>
+// requires indirectly_swappable<I1, I2>
+// constexpr ranges::swap_ranges_result<I1, I2>
+// ranges::swap_ranges(I1 first1, S1 last1, I2 first2, S2 last2);
+// template<input_range R1, input_range R2>
+// requires indirectly_swappable<iterator_t<R1>, iterator_t<R2>>
+// constexpr ranges::swap_ranges_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
+// ranges::swap_ranges(R1&& r1, R2&& r2);
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <ranges>
+
+#include "test_iterators.h"
+
+constexpr void test_
diff erent_lengths() {
+ using Expected = std::ranges::swap_ranges_result<int*, int*>;
+ int i[3] = {1, 2, 3};
+ int j[1] = {4};
+ std::same_as<Expected> auto r = std::ranges::swap_ranges(i, i + 3, j, j + 1);
+ assert(r.in1 == i + 1);
+ assert(r.in2 == j + 1);
+ assert(i[0] == 4);
+ assert(i[1] == 2);
+ assert(i[2] == 3);
+ assert(j[0] == 1);
+ std::same_as<Expected> auto r2 = std::ranges::swap_ranges(i, j);
+ assert(r2.in1 == i + 1);
+ assert(r2.in2 == j + 1);
+ assert(i[0] == 1);
+ assert(i[1] == 2);
+ assert(i[2] == 3);
+ assert(j[0] == 4);
+ std::same_as<Expected> auto r3 = std::ranges::swap_ranges(j, j + 1, i, i + 3);
+ assert(r3.in1 == j + 1);
+ assert(r3.in2 == i + 1);
+ assert(i[0] == 4);
+ assert(i[1] == 2);
+ assert(i[2] == 3);
+ assert(j[0] == 1);
+ std::same_as<Expected> auto r4 = std::ranges::swap_ranges(j, i);
+ assert(r4.in1 == j + 1);
+ assert(r4.in2 == i + 1);
+ assert(i[0] == 1);
+ assert(i[1] == 2);
+ assert(i[2] == 3);
+ assert(j[0] == 4);
+}
+
+constexpr void test_range() {
+ std::array r1 = {1, 2, 3};
+ std::array r2 = {4, 5, 6};
+
+
+ std::same_as<std::ranges::in_in_result<int*, int*>> auto r = std::ranges::swap_ranges(r1, r2);
+ assert(r.in1 == r1.end());
+ assert(r.in2 == r2.end());
+
+ assert((r1 == std::array{4, 5, 6}));
+ assert((r2 == std::array{1, 2, 3}));
+}
+
+constexpr void test_borrowed_input_range() {
+ {
+ int r1[] = {1, 2, 3};
+ int r2[] = {4, 5, 6};
+ std::ranges::swap_ranges(std::views::all(r1), r2);
+ assert(r1[0] == 4);
+ assert(r1[1] == 5);
+ assert(r1[2] == 6);
+ assert(r2[0] == 1);
+ assert(r2[1] == 2);
+ assert(r2[2] == 3);
+ }
+ {
+ int r1[] = {1, 2, 3};
+ int r2[] = {4, 5, 6};
+ std::ranges::swap_ranges(r1, std::views::all(r2));
+ assert(r1[0] == 4);
+ assert(r1[1] == 5);
+ assert(r1[2] == 6);
+ assert(r2[0] == 1);
+ assert(r2[1] == 2);
+ assert(r2[2] == 3);
+ }
+ {
+ int r1[] = {1, 2, 3};
+ int r2[] = {4, 5, 6};
+ std::ranges::swap_ranges(std::views::all(r1), std::views::all(r2));
+ assert(r1[0] == 4);
+ assert(r1[1] == 5);
+ assert(r1[2] == 6);
+ assert(r2[0] == 1);
+ assert(r2[1] == 2);
+ assert(r2[2] == 3);
+ }
+}
+
+constexpr void test_sentinel() {
+ int i[3] = {1, 2, 3};
+ int j[3] = {4, 5, 6};
+ using It = cpp17_input_iterator<int*>;
+ using Sent = sentinel_wrapper<It>;
+ using Expected = std::ranges::swap_ranges_result<It, It>;
+ std::same_as<Expected> auto r =
+ std::ranges::swap_ranges(It(i), Sent(It(i + 3)), It(j), Sent(It(j + 3)));
+ assert(base(r.in1) == i + 3);
+ assert(base(r.in2) == j + 3);
+ assert(i[0] == 4);
+ assert(i[1] == 5);
+ assert(i[2] == 6);
+ assert(j[0] == 1);
+ assert(j[1] == 2);
+ assert(j[2] == 3);
+}
+
+template <class Iter1, class Iter2>
+constexpr void test_iterators() {
+ using Expected = std::ranges::swap_ranges_result<Iter1, Iter2>;
+ int i[3] = {1, 2, 3};
+ int j[3] = {4, 5, 6};
+ std::same_as<Expected> auto r =
+ std::ranges::swap_ranges(Iter1(i), sentinel_wrapper(Iter1(i + 3)), Iter2(j), sentinel_wrapper(Iter2(j + 3)));
+ assert(base(r.in1) == i + 3);
+ assert(base(r.in2) == j + 3);
+ assert(i[0] == 4);
+ assert(i[1] == 5);
+ assert(i[2] == 6);
+ assert(j[0] == 1);
+ assert(j[1] == 2);
+ assert(j[2] == 3);
+}
+
+constexpr void test_rval_range() {
+ {
+ using Expected = std::ranges::swap_ranges_result<int*, std::ranges::dangling>;
+ std::array<int, 3> r = {1, 2, 3};
+ std::same_as<Expected> auto a = std::ranges::swap_ranges(r, std::array{4, 5, 6});
+ assert((r == std::array{4, 5, 6}));
+ assert(a.in1 == r.begin() + 3);
+ }
+ {
+ std::array<int, 3> r = {1, 2, 3};
+ using Expected = std::ranges::swap_ranges_result<std::ranges::dangling, int*>;
+ std::same_as<Expected> auto b = std::ranges::swap_ranges(std::array{4, 5, 6}, r);
+ assert((r == std::array{4, 5, 6}));
+ assert(b.in2 == r.begin() + 3);
+ }
+}
+
+constexpr bool test() {
+ test_range();
+
+ test_iterators<cpp20_input_iterator<int*>, cpp20_input_iterator<int*>>();
+ test_iterators<cpp20_input_iterator<int*>, forward_iterator<int*>>();
+ test_iterators<cpp20_input_iterator<int*>, bidirectional_iterator<int*>>();
+ test_iterators<cpp20_input_iterator<int*>, random_access_iterator<int*>>();
+ test_iterators<cpp20_input_iterator<int*>, int*>();
+
+ test_iterators<forward_iterator<int*>, cpp20_input_iterator<int*>>();
+ test_iterators<forward_iterator<int*>, forward_iterator<int*>>();
+ test_iterators<forward_iterator<int*>, bidirectional_iterator<int*>>();
+ test_iterators<forward_iterator<int*>, random_access_iterator<int*>>();
+ test_iterators<forward_iterator<int*>, int*>();
+
+ test_iterators<bidirectional_iterator<int*>, cpp20_input_iterator<int*>>();
+ test_iterators<bidirectional_iterator<int*>, forward_iterator<int*>>();
+ test_iterators<bidirectional_iterator<int*>, bidirectional_iterator<int*>>();
+ test_iterators<bidirectional_iterator<int*>, random_access_iterator<int*>>();
+ test_iterators<bidirectional_iterator<int*>, int*>();
+
+ test_iterators<random_access_iterator<int*>, cpp20_input_iterator<int*>>();
+ test_iterators<random_access_iterator<int*>, forward_iterator<int*>>();
+ test_iterators<random_access_iterator<int*>, bidirectional_iterator<int*>>();
+ test_iterators<random_access_iterator<int*>, random_access_iterator<int*>>();
+ test_iterators<random_access_iterator<int*>, int*>();
+
+ test_iterators<int*, cpp20_input_iterator<int*>>();
+ test_iterators<int*, forward_iterator<int*>>();
+ test_iterators<int*, bidirectional_iterator<int*>>();
+ test_iterators<int*, random_access_iterator<int*>>();
+ test_iterators<int*, int*>();
+
+ test_sentinel();
+ test_
diff erent_lengths();
+ test_borrowed_input_range();
+ test_rval_range();
+
+ return true;
+}
+
+static_assert(std::same_as<std::ranges::swap_ranges_result<int, char>, std::ranges::in_in_result<int, char>>);
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
index 3a7146e55104d..ecbcc83872ba0 100644
--- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
+++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
@@ -142,7 +142,7 @@ int a[10];
//static_assert(test(std::ranges::stable_partition, a, odd));
//static_assert(test(std::ranges::stable_sort, a));
//static_assert(test(std::ranges::starts_with, a, a));
-//static_assert(test(std::ranges::swap_ranges, a, a));
+static_assert(test(std::ranges::swap_ranges, a, a));
//static_assert(test(std::ranges::transform, a, a, triple));
//static_assert(test(std::ranges::unique, a));
//static_assert(test(std::ranges::unique_copy, a, a));
More information about the libcxx-commits
mailing list