diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -67,7 +67,7 @@ Merge,set_union,Not assigned,n/a,Not started Permutation,remove,Not assigned,n/a,Not started Permutation,remove_if,Not assigned,n/a,Not started -Permutation,reverse,Not assigned,n/a,Not started +Permutation,reverse,Nikolas Klauser,`D125752 `_,✅ Permutation,rotate,Not assigned,n/a,Not started Permutation,shuffle,Not assigned,n/a,Not started Permutation,unique,Not assigned,n/a,Not started diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -85,6 +85,7 @@ __algorithm/ranges_minmax.h __algorithm/ranges_minmax_element.h __algorithm/ranges_mismatch.h + __algorithm/ranges_reverse.h __algorithm/ranges_swap_ranges.h __algorithm/ranges_transform.h __algorithm/remove.h diff --git a/libcxx/include/__algorithm/ranges_reverse.h b/libcxx/include/__algorithm/ranges_reverse.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_reverse.h @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// 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_REVERSE_H +#define _LIBCPP___ALGORITHM_RANGES_REVERSE_H + +#include <__config> +#include <__iterator/concepts.h> +#include <__iterator/iter_swap.h> +#include <__iterator/next.h> +#include <__iterator/permutable.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/dangling.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __reverse { +struct __fn { + + template _Sent> + requires permutable<_Iter> + _LIBCPP_HIDE_FROM_ABI constexpr + _Iter operator()(_Iter __first, _Sent __last) const { + if constexpr (random_access_iterator<_Iter>) { + if (__first == __last) + return __first; + + auto __end = ranges::next(__first, __last); + auto __ret = __end; + + while (__first < --__end) { + ranges::iter_swap(__first, __end); + ++__first; + } + return __ret; + } else { + auto __end = ranges::next(__first, __last); + auto __ret = __end; + + while (__first != __end) { + if (__first == --__end) + break; + + ranges::iter_swap(__first, __end); + ++__first; + } + return __ret; + } + } + + template + requires permutable> + _LIBCPP_HIDE_FROM_ABI constexpr + borrowed_iterator_t<_Range> operator()(_Range&& __range) const { + return (*this)(ranges::begin(__range), ranges::end(__range)); + } + +}; +} // namespace __reverse + +inline namespace __cpo { + inline constexpr auto reverse = __reverse::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_REVERSE_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -274,6 +274,15 @@ indirect_unary_predicate, Proj>> Pred> constexpr bool ranges::is_partitioned(R&& r, Pred pred, Proj proj = {}); // since C++20 + + template S> + requires permutable + constexpr I ranges::reverse(I first, S last); // since C++20 + + template + requires permutable> + constexpr borrowed_iterator_t ranges::reverse(R&& r); // since C++20 + } constexpr bool // constexpr in C++20 @@ -1009,6 +1018,7 @@ #include <__algorithm/ranges_minmax.h> #include <__algorithm/ranges_minmax_element.h> #include <__algorithm/ranges_mismatch.h> +#include <__algorithm/ranges_reverse.h> #include <__algorithm/ranges_swap_ranges.h> #include <__algorithm/ranges_transform.h> #include <__algorithm/remove.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -318,6 +318,7 @@ module ranges_minmax { private header "__algorithm/ranges_minmax.h" } module ranges_minmax_element { private header "__algorithm/ranges_minmax_element.h" } module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } + module ranges_reverse { private header "__algorithm/ranges_reverse.h" } module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" } module ranges_transform { private header "__algorithm/ranges_transform.h" } module remove { private header "__algorithm/remove.h" } diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -122,6 +122,7 @@ #include <__algorithm/ranges_minmax.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax.h'}} #include <__algorithm/ranges_minmax_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax_element.h'}} #include <__algorithm/ranges_mismatch.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_mismatch.h'}} +#include <__algorithm/ranges_reverse.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_reverse.h'}} #include <__algorithm/ranges_swap_ranges.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}} #include <__algorithm/ranges_transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_transform.h'}} #include <__algorithm/remove.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/remove.h'}} diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/ranges.reverse.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/ranges.reverse.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/ranges.reverse.pass.cpp @@ -0,0 +1,120 @@ +//===----------------------------------------------------------------------===// +// +// 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, c++17 +// UNSUPPORTED: libcpp-has-no-incomplete-ranges + +// + +// template S> +// requires permutable +// constexpr I ranges::reverse(I first, S last); +// template +// requires permutable> +// constexpr borrowed_iterator_t ranges::reverse(R&& r); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +template > +concept HasReverseIt = requires (Iter first, Sent last) { std::ranges::reverse(first, last); }; + +static_assert(HasReverseIt); +static_assert(!HasReverseIt); +static_assert(!HasReverseIt); +static_assert(!HasReverseIt); +static_assert(!HasReverseIt); + + +template +concept HasReverseR = requires (Range range) { std::ranges::reverse(range); }; + +static_assert(HasReverseR>); +static_assert(!HasReverseR); +static_assert(!HasReverseR); +static_assert(!HasReverseR); +static_assert(!HasReverseR); + +template +constexpr void test(std::array value, std::array expected) { + { + auto val = value; + std::same_as decltype(auto) ret = std::ranges::reverse(Iter(val.data()), Sent(Iter(val.data() + val.size()))); + assert(val == expected); + assert(base(ret) == val.data() + val.size()); + } + { + auto val = value; + auto range = std::ranges::subrange(Iter(val.data()), Sent(Iter(val.data() + val.size()))); + std::same_as decltype(auto) ret = std::ranges::reverse(range); + assert(val == expected); + assert(base(ret) == val.data() + val.size()); + } +} + +template +constexpr void test_iterators() { + // simple test + test({1, 2, 3, 4}, {4, 3, 2, 1}); + // check that an odd number of elements works + test({1, 2, 3, 4, 5, 6, 7}, {7, 6, 5, 4, 3, 2, 1}); + // check that an empty range works + test({}, {}); + // check that a single element works + test({5}, {5}); +} + +struct SwapCounter { + int* counter; + constexpr SwapCounter(int* counter_) : counter(counter_) {} + friend constexpr void swap(SwapCounter& lhs, SwapCounter&) { ++*lhs.counter; } +}; + +constexpr bool test() { + test_iterators>(); + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators, sentinel_wrapper>>(); + test_iterators(); + + // check that std::ranges::dangling is returned + { + [[maybe_unused]] std::same_as auto ret = std::ranges::reverse(std::array {1, 2, 3, 4}); + } + + { + { + int counter = 0; + SwapCounter a[] = {&counter, &counter, &counter, &counter}; + std::ranges::reverse(a); + assert(counter == 2); + } + { + int counter = 0; + SwapCounter a[] = {&counter, &counter, &counter, &counter}; + std::ranges::reverse(a, a + 4); + assert(counter == 2); + } + } + + return true; +} + +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 --- 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 @@ -125,7 +125,7 @@ //static_assert(test(std::ranges::replace_copy, a, a, 42, 43)); //static_assert(test(std::ranges::replace_copy_if, a, a, odd, 43)); //static_assert(test(std::ranges::replace_if, a, odd, 43)); -//static_assert(test(std::ranges::reverse, a)); +static_assert(test(std::ranges::reverse, a)); //static_assert(test(std::ranges::reverse_copy, a, a)); //static_assert(test(std::ranges::rotate, a, a+5)); //static_assert(test(std::ranges::rotate_copy, a, a+5, a)); diff --git a/libcxx/test/support/almost_satisfies_types.h b/libcxx/test/support/almost_satisfies_types.h --- a/libcxx/test/support/almost_satisfies_types.h +++ b/libcxx/test/support/almost_satisfies_types.h @@ -139,4 +139,85 @@ static_assert(!std::movable); static_assert(!std::weakly_incrementable); +class BidirectionalIteratorNotDerivedFrom { +public: + using difference_type = long; + using value_type = int; + using iterator_category = std::forward_iterator_tag; + + BidirectionalIteratorNotDerivedFrom& operator++(); + BidirectionalIteratorNotDerivedFrom operator++(int); + BidirectionalIteratorNotDerivedFrom& operator--(); + BidirectionalIteratorNotDerivedFrom operator--(int); + int& operator*() const; + + bool operator==(const BidirectionalIteratorNotDerivedFrom&) const = default; +}; + +using BidirectionalRangeNotDerivedFrom = UncheckedRange; + +static_assert(std::forward_iterator); +static_assert(!std::bidirectional_iterator); +static_assert(!std::ranges::bidirectional_range); + +class BidirectionalIteratorNotDecrementable { +public: + using difference_type = long; + using value_type = int; + using iterator_category = std::bidirectional_iterator_tag; + + BidirectionalIteratorNotDecrementable& operator++(); + BidirectionalIteratorNotDecrementable operator++(int); + int& operator*() const; + + bool operator==(const BidirectionalIteratorNotDecrementable&) const = default; +}; + +using BidirectionalRangeNotDecrementable = UncheckedRange; + +static_assert(std::forward_iterator); +static_assert(!std::bidirectional_iterator); +static_assert(!std::ranges::bidirectional_range); + +class PermutableNotForwardIterator { +public: + using difference_type = long; + using value_type = int; + using iterator_category = std::input_iterator_tag; + + PermutableNotForwardIterator& operator++(); + void operator++(int); + int& operator*() const; +}; + +using PermutableRangeNotForwardIterator = UncheckedRange; + +static_assert(std::input_iterator); +static_assert(!std::forward_iterator); +static_assert(!std::permutable); + +class PermutableNotSwappable { +public: + class NotSwappable { + NotSwappable(NotSwappable&&) = delete; + }; + + using difference_type = long; + using value_type = NotSwappable; + using iterator_category = std::contiguous_iterator_tag; + + PermutableNotSwappable& operator++(); + PermutableNotSwappable operator++(int); + NotSwappable& operator*() const; + + bool operator==(const PermutableNotSwappable&) const = default; +}; + +using PermutableRangeNotSwappable = UncheckedRange; + +static_assert(std::input_iterator); +static_assert(std::forward_iterator); +static_assert(!std::permutable); +static_assert(!std::indirectly_swappable); + #endif // ALMOST_SATISFIES_TYPES_H