forked from OSchip/llvm-project
[libc++] Implement ranges::{reverse, rotate}_copy
Reviewed By: var-const, #libc Spies: huixie90, libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D127211
This commit is contained in:
parent
b19d3ee712
commit
7d426a392f
|
@ -54,8 +54,8 @@ Write,replace_if,Nikolas Klauser,`D126283 <https://llvm.org/D126283>`_,✅
|
|||
Write,replace_copy,Nikolas Klauser,n/a,Not started
|
||||
Write,replace_copy_if,Nikolas Klauser,n/a,Not started
|
||||
Write,swap_ranges,Nikolas Klauser,`D116303 <https://llvm.org/D116303>`_,✅
|
||||
Write,reverse_copy,Nikolas Klauser,`D127211 <https://llvm.org/D127211>`_,Under review
|
||||
Write,rotate_copy,Nikolas Klauser,`D127211 <https://llvm.org/D127211>`_,Under review
|
||||
Write,reverse_copy,Nikolas Klauser,`D127211 <https://llvm.org/D127211>`_,✅
|
||||
Write,rotate_copy,Nikolas Klauser,`D127211 <https://llvm.org/D127211>`_,✅
|
||||
Write,sample,Not assigned,n/a,Not started
|
||||
Write,unique_copy,Not assigned,n/a,Not started
|
||||
Write,partition_copy,Not assigned,n/a,Not started
|
||||
|
|
|
|
@ -112,6 +112,8 @@ set(files
|
|||
__algorithm/ranges_replace.h
|
||||
__algorithm/ranges_replace_if.h
|
||||
__algorithm/ranges_reverse.h
|
||||
__algorithm/ranges_reverse_copy.h
|
||||
__algorithm/ranges_rotate_copy.h
|
||||
__algorithm/ranges_set_difference.h
|
||||
__algorithm/ranges_set_intersection.h
|
||||
__algorithm/ranges_sort.h
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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_COPY_H
|
||||
#define _LIBCPP___ALGORITHM_RANGES_REVERSE_COPY_H
|
||||
|
||||
#include <__algorithm/in_out_result.h>
|
||||
#include <__algorithm/ranges_copy.h>
|
||||
#include <__config>
|
||||
#include <__iterator/concepts.h>
|
||||
#include <__iterator/next.h>
|
||||
#include <__iterator/reverse_iterator.h>
|
||||
#include <__ranges/access.h>
|
||||
#include <__ranges/concepts.h>
|
||||
#include <__ranges/dangling.h>
|
||||
#include <__ranges/subrange.h>
|
||||
#include <__utility/move.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 {
|
||||
|
||||
template <class _InIter, class _OutIter>
|
||||
using reverse_copy_result = in_out_result<_InIter, _OutIter>;
|
||||
|
||||
namespace __reverse_copy {
|
||||
struct __fn {
|
||||
|
||||
template <bidirectional_iterator _InIter, sentinel_for<_InIter> _Sent, weakly_incrementable _OutIter>
|
||||
requires indirectly_copyable<_InIter, _OutIter>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
reverse_copy_result<_InIter, _OutIter> operator()(_InIter __first, _Sent __last, _OutIter __result) const {
|
||||
return (*this)(subrange(std::move(__first), std::move(__last)), std::move(__result));
|
||||
}
|
||||
|
||||
template <bidirectional_range _Range, weakly_incrementable _OutIter>
|
||||
requires indirectly_copyable<iterator_t<_Range>, _OutIter>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
reverse_copy_result<borrowed_iterator_t<_Range>, _OutIter> operator()(_Range&& __range, _OutIter __result) const {
|
||||
auto __ret = ranges::copy(std::__reverse_range(__range), std::move(__result));
|
||||
return {ranges::next(ranges::begin(__range), ranges::end(__range)), std::move(__ret.out)};
|
||||
}
|
||||
|
||||
};
|
||||
} // namespace __reverse_copy
|
||||
|
||||
inline namespace __cpo {
|
||||
inline constexpr auto reverse_copy = __reverse_copy::__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_COPY_H
|
|
@ -0,0 +1,68 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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_ROTATE_COPY_H
|
||||
#define _LIBCPP___ALGORITHM_RANGES_ROTATE_COPY_H
|
||||
|
||||
#include <__algorithm/in_out_result.h>
|
||||
#include <__algorithm/ranges_copy.h>
|
||||
#include <__config>
|
||||
#include <__iterator/concepts.h>
|
||||
#include <__iterator/reverse_iterator.h>
|
||||
#include <__ranges/access.h>
|
||||
#include <__ranges/concepts.h>
|
||||
#include <__ranges/dangling.h>
|
||||
#include <__utility/move.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 {
|
||||
|
||||
template <class _InIter, class _OutIter>
|
||||
using rotate_copy_result = in_out_result<_InIter, _OutIter>;
|
||||
|
||||
namespace __rotate_copy {
|
||||
struct __fn {
|
||||
|
||||
template <bidirectional_iterator _InIter, sentinel_for<_InIter> _Sent, weakly_incrementable _OutIter>
|
||||
requires indirectly_copyable<_InIter, _OutIter>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
rotate_copy_result<_InIter, _OutIter>
|
||||
operator()(_InIter __first, _InIter __middle, _Sent __last, _OutIter __result) const {
|
||||
auto __res1 = ranges::copy(__middle, __last, std::move(__result));
|
||||
auto __res2 = ranges::copy(__first, __middle, std::move(__res1.out));
|
||||
return {std::move(__res1.in), std::move(__res2.out)};
|
||||
}
|
||||
|
||||
template <bidirectional_range _Range, weakly_incrementable _OutIter>
|
||||
requires indirectly_copyable<iterator_t<_Range>, _OutIter>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr
|
||||
rotate_copy_result<borrowed_iterator_t<_Range>, _OutIter>
|
||||
operator()(_Range&& __range, iterator_t<_Range> __middle, _OutIter __result) const {
|
||||
return (*this)(ranges::begin(__range), std::move(__middle), ranges::end(__range), std::move(__result));
|
||||
}
|
||||
|
||||
};
|
||||
} // namespace __rotate_copy
|
||||
|
||||
inline namespace __cpo {
|
||||
inline constexpr auto rotate_copy = __rotate_copy::__fn{};
|
||||
} // namespace __cpo
|
||||
} // namespace ranges
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
||||
|
||||
#endif // _LIBCPP___ALGORITHM_RANGES_ROTATE_COPY_H
|
|
@ -15,15 +15,20 @@
|
|||
#include <__compare/three_way_comparable.h>
|
||||
#include <__concepts/convertible_to.h>
|
||||
#include <__config>
|
||||
#include <__iterator/advance.h>
|
||||
#include <__iterator/concepts.h>
|
||||
#include <__iterator/incrementable_traits.h>
|
||||
#include <__iterator/iter_move.h>
|
||||
#include <__iterator/iter_swap.h>
|
||||
#include <__iterator/iterator.h>
|
||||
#include <__iterator/iterator_traits.h>
|
||||
#include <__iterator/next.h>
|
||||
#include <__iterator/prev.h>
|
||||
#include <__iterator/readable_traits.h>
|
||||
#include <__memory/addressof.h>
|
||||
#include <__ranges/access.h>
|
||||
#include <__ranges/concepts.h>
|
||||
#include <__ranges/subrange.h>
|
||||
#include <__utility/move.h>
|
||||
#include <type_traits>
|
||||
|
||||
|
@ -365,6 +370,16 @@ struct __rewrap_iter_impl<_ReverseWrapper<_OrigIter>, _UnwrappedIter> {
|
|||
}
|
||||
};
|
||||
|
||||
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
||||
template <ranges::bidirectional_range _Range>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr ranges::
|
||||
subrange<reverse_iterator<ranges::iterator_t<_Range>>, reverse_iterator<ranges::iterator_t<_Range>>>
|
||||
__reverse_range(_Range&& __range) {
|
||||
auto __first = ranges::begin(__range);
|
||||
return {std::make_reverse_iterator(ranges::next(__first, ranges::end(__range))), std::make_reverse_iterator(__first)};
|
||||
}
|
||||
#endif
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
#endif // _LIBCPP___ITERATOR_REVERSE_ITERATOR_H
|
||||
|
|
|
@ -585,9 +585,9 @@ namespace ranges {
|
|||
requires permutable<iterator_t<R>>
|
||||
constexpr borrowed_subrange_t<R>
|
||||
ranges::remove_if(R&& r, Pred pred, Proj proj = {}); // since C++20
|
||||
|
||||
|
||||
template<class I, class O>
|
||||
using set_difference_result = in_out_result<I, O>; // since C++20
|
||||
using set_difference_result = in_out_result<I, O>; // since C++20
|
||||
|
||||
template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
|
||||
weakly_incrementable O, class Comp = ranges::less,
|
||||
|
@ -623,6 +623,32 @@ namespace ranges {
|
|||
set_intersection(R1&& r1, R2&& r2, O result,
|
||||
Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20
|
||||
|
||||
template <class _InIter, class _OutIter>
|
||||
using reverse_copy_result = in_out_result<_InIter, _OutIter>; // since C++20
|
||||
|
||||
template<bidirectional_iterator I, sentinel_for<I> S, weakly_incrementable O>
|
||||
requires indirectly_copyable<I, O>
|
||||
constexpr ranges::reverse_copy_result<I, O>
|
||||
ranges::reverse_copy(I first, S last, O result); // since C++20
|
||||
|
||||
template<bidirectional_range R, weakly_incrementable O>
|
||||
requires indirectly_copyable<iterator_t<R>, O>
|
||||
constexpr ranges::reverse_copy_result<borrowed_iterator_t<R>, O>
|
||||
ranges::reverse_copy(R&& r, O result); // since C++20
|
||||
|
||||
template <class _InIter, class _OutIter>
|
||||
using rotate_copy_result = in_out_result<_InIter, _OutIter>; // since C++20
|
||||
|
||||
template<forward_iterator I, sentinel_for<I> S, weakly_incrementable O>
|
||||
requires indirectly_copyable<I, O>
|
||||
constexpr ranges::rotate_copy_result<I, O>
|
||||
ranges::rotate_copy(I first, I middle, S last, O result); // since C++20
|
||||
|
||||
template<forward_range R, weakly_incrementable O>
|
||||
requires indirectly_copyable<iterator_t<R>, O>
|
||||
constexpr ranges::rotate_copy_result<borrowed_iterator_t<R>, O>
|
||||
ranges::rotate_copy(R&& r, iterator_t<R> middle, O result); // since C++20
|
||||
|
||||
}
|
||||
|
||||
constexpr bool // constexpr in C++20
|
||||
|
@ -1396,6 +1422,8 @@ template <class BidirectionalIterator, class Compare>
|
|||
#include <__algorithm/ranges_replace.h>
|
||||
#include <__algorithm/ranges_replace_if.h>
|
||||
#include <__algorithm/ranges_reverse.h>
|
||||
#include <__algorithm/ranges_reverse_copy.h>
|
||||
#include <__algorithm/ranges_rotate_copy.h>
|
||||
#include <__algorithm/ranges_set_difference.h>
|
||||
#include <__algorithm/ranges_set_intersection.h>
|
||||
#include <__algorithm/ranges_sort.h>
|
||||
|
|
|
@ -351,6 +351,8 @@ module std [system] {
|
|||
module ranges_replace { private header "__algorithm/ranges_replace.h" }
|
||||
module ranges_replace_if { private header "__algorithm/ranges_replace_if.h" }
|
||||
module ranges_reverse { private header "__algorithm/ranges_reverse.h" }
|
||||
module ranges_reverse_copy { private header "__algorithm/ranges_reverse_copy.h" }
|
||||
module ranges_rotate_copy { private header "__algorithm/ranges_rotate_copy.h" }
|
||||
module ranges_set_difference { private header "__algorithm/ranges_set_difference.h" }
|
||||
module ranges_set_intersection { private header "__algorithm/ranges_set_intersection.h" }
|
||||
module ranges_sort { private header "__algorithm/ranges_sort.h" }
|
||||
|
|
|
@ -149,6 +149,8 @@ END-SCRIPT
|
|||
#include <__algorithm/ranges_replace.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_replace.h'}}
|
||||
#include <__algorithm/ranges_replace_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_replace_if.h'}}
|
||||
#include <__algorithm/ranges_reverse.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_reverse.h'}}
|
||||
#include <__algorithm/ranges_reverse_copy.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_reverse_copy.h'}}
|
||||
#include <__algorithm/ranges_rotate_copy.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_rotate_copy.h'}}
|
||||
#include <__algorithm/ranges_set_difference.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_set_difference.h'}}
|
||||
#include <__algorithm/ranges_set_intersection.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_set_intersection.h'}}
|
||||
#include <__algorithm/ranges_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_sort.h'}}
|
||||
|
|
|
@ -6,6 +6,7 @@ cstddef
|
|||
cstdint
|
||||
cstdlib
|
||||
cstring
|
||||
initializer_list
|
||||
iosfwd
|
||||
limits
|
||||
type_traits
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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<bidirectional_iterator I, sentinel_for<I> S, weakly_incrementable O>
|
||||
// requires indirectly_copyable<I, O>
|
||||
// constexpr ranges::reverse_copy_result<I, O>
|
||||
// ranges::reverse_copy(I first, S last, O result);
|
||||
// template<bidirectional_range R, weakly_incrementable O>
|
||||
// requires indirectly_copyable<iterator_t<R>, O>
|
||||
// constexpr ranges::reverse_copy_result<borrowed_iterator_t<R>, O>
|
||||
// ranges::reverse_copy(R&& r, O result);
|
||||
|
||||
// <algorithm>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <ranges>
|
||||
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "test_iterators.h"
|
||||
|
||||
template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>>
|
||||
concept HasReverseCopyIt = requires(Iter first, Sent last, Out out) { std::ranges::reverse_copy(first, last, out); };
|
||||
|
||||
template <class Range, class Out = int*>
|
||||
concept HasReverseCopyR = requires(Range range, Out out) { std::ranges::reverse_copy(range, out); };
|
||||
|
||||
static_assert(HasReverseCopyIt<int*>);
|
||||
static_assert(!HasReverseCopyIt<BidirectionalIteratorNotDerivedFrom>);
|
||||
static_assert(!HasReverseCopyIt<BidirectionalIteratorNotDecrementable>);
|
||||
static_assert(!HasReverseCopyIt<int*, SentinelForNotSemiregular>);
|
||||
static_assert(!HasReverseCopyIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
|
||||
static_assert(!HasReverseCopyIt<int*, OutputIteratorNotIndirectlyWritable>);
|
||||
static_assert(!HasReverseCopyIt<int*, OutputIteratorNotInputOrOutputIterator>);
|
||||
|
||||
static_assert(HasReverseCopyR<UncheckedRange<int*>>);
|
||||
static_assert(!HasReverseCopyR<BidirectionalRangeNotDerivedFrom>);
|
||||
static_assert(!HasReverseCopyR<BidirectionalRangeNotDecrementable>);
|
||||
static_assert(!HasReverseCopyR<UncheckedRange<int*, SentinelForNotSemiregular>>);
|
||||
static_assert(!HasReverseCopyR<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>);
|
||||
static_assert(!HasReverseCopyR<UncheckedRange<int*>, OutputIteratorNotInputOrOutputIterator>);
|
||||
|
||||
static_assert(std::is_same_v<std::ranges::reverse_copy_result<int, int>, std::ranges::in_out_result<int, int>>);
|
||||
|
||||
template <class Iter, class OutIter, class Sent, int N>
|
||||
constexpr void test(std::array<int, N> value, std::array<int, N> expected) {
|
||||
{
|
||||
std::array<int, N> out;
|
||||
std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
|
||||
std::ranges::reverse_copy(Iter(value.data()),
|
||||
Sent(Iter(value.data() + value.size())),
|
||||
OutIter(out.data()));
|
||||
assert(base(ret.in) == value.data() + value.size());
|
||||
assert(base(ret.out) == out.data() + out.size());
|
||||
assert(out == expected);
|
||||
}
|
||||
{
|
||||
std::array<int, N> out;
|
||||
auto range = std::ranges::subrange(Iter(value.data()), Sent(Iter(value.data() + value.size())));
|
||||
std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
|
||||
std::ranges::reverse_copy(range, OutIter(out.data()));
|
||||
assert(base(ret.in) == value.data() + value.size());
|
||||
assert(base(ret.out) == out.data() + out.size());
|
||||
assert(out == expected);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Iter, class OutIter, class Sent>
|
||||
constexpr void test_iterators() {
|
||||
// simple test
|
||||
test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, {4, 3, 2, 1});
|
||||
|
||||
// check that an empty range works
|
||||
test<Iter, OutIter, Sent, 0>({}, {});
|
||||
|
||||
// check that a single element range works
|
||||
test<Iter, OutIter, Sent, 1>({1}, {1});
|
||||
|
||||
// check that a two element range works
|
||||
test<Iter, OutIter, Sent, 2>({1, 2}, {2, 1});
|
||||
}
|
||||
|
||||
template <class Iter, class Sent = Iter>
|
||||
constexpr void test_out_iterators() {
|
||||
test_iterators<Iter, cpp20_output_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, forward_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, bidirectional_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, random_access_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, contiguous_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, int*, Sent>();
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
test_out_iterators<bidirectional_iterator<int*>>();
|
||||
test_out_iterators<random_access_iterator<int*>>();
|
||||
test_out_iterators<contiguous_iterator<int*>>();
|
||||
test_out_iterators<int*>();
|
||||
test_out_iterators<const int*>();
|
||||
|
||||
{
|
||||
struct AssignmentCounter {
|
||||
int* counter;
|
||||
|
||||
constexpr AssignmentCounter(int* counter_) : counter(counter_) {}
|
||||
constexpr AssignmentCounter& operator=(const AssignmentCounter&) { ++*counter; return *this; }
|
||||
};
|
||||
|
||||
{
|
||||
int c = 0;
|
||||
AssignmentCounter a[] = {&c, &c, &c, &c};
|
||||
AssignmentCounter b[] = {&c, &c, &c, &c};
|
||||
std::ranges::reverse_copy(a, a + 4, b);
|
||||
assert(c == 4);
|
||||
}
|
||||
{
|
||||
int c = 0;
|
||||
AssignmentCounter a[] = {&c, &c, &c, &c};
|
||||
AssignmentCounter b[] = {&c, &c, &c, &c};
|
||||
std::ranges::reverse_copy(a, b);
|
||||
assert(c == 4);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,154 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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<forward_iterator I, sentinel_for<I> S, weakly_incrementable O>
|
||||
// requires indirectly_copyable<I, O>
|
||||
// constexpr ranges::rotate_copy_result<I, O>
|
||||
// ranges::rotate_copy(I first, I middle, S last, O result);
|
||||
// template<forward_range R, weakly_incrementable O>
|
||||
// requires indirectly_copyable<iterator_t<R>, O>
|
||||
// constexpr ranges::rotate_copy_result<borrowed_iterator_t<R>, O>
|
||||
// ranges::rotate_copy(R&& r, iterator_t<R> middle, O result);
|
||||
|
||||
// <algorithm>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <ranges>
|
||||
|
||||
#include "almost_satisfies_types.h"
|
||||
#include "test_iterators.h"
|
||||
|
||||
template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>>
|
||||
concept HasRotateCopyIt = requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };
|
||||
|
||||
template <class Range, class Out = int*>
|
||||
concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); };
|
||||
|
||||
static_assert(HasRotateCopyIt<int*>);
|
||||
static_assert(!HasRotateCopyIt<BidirectionalIteratorNotDerivedFrom>);
|
||||
static_assert(!HasRotateCopyIt<BidirectionalIteratorNotDecrementable>);
|
||||
static_assert(!HasRotateCopyIt<int*, SentinelForNotSemiregular>);
|
||||
static_assert(!HasRotateCopyIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
|
||||
static_assert(!HasRotateCopyIt<int*, OutputIteratorNotIndirectlyWritable>);
|
||||
static_assert(!HasRotateCopyIt<int*, OutputIteratorNotInputOrOutputIterator>);
|
||||
|
||||
static_assert(HasRotateCopyR<UncheckedRange<int*>>);
|
||||
static_assert(!HasRotateCopyR<BidirectionalRangeNotDerivedFrom>);
|
||||
static_assert(!HasRotateCopyR<BidirectionalRangeNotDecrementable>);
|
||||
static_assert(!HasRotateCopyR<UncheckedRange<int*, SentinelForNotSemiregular>>);
|
||||
static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>);
|
||||
static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotInputOrOutputIterator>);
|
||||
|
||||
static_assert(std::is_same_v<std::ranges::rotate_copy_result<int, int>, std::ranges::in_out_result<int, int>>);
|
||||
|
||||
template <class Iter, class OutIter, class Sent, int N>
|
||||
constexpr void test(std::array<int, N> value, size_t middle, std::array<int, N> expected) {
|
||||
{
|
||||
std::array<int, N> out;
|
||||
std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
|
||||
std::ranges::rotate_copy(Iter(value.data()),
|
||||
Iter(value.data() + middle),
|
||||
Sent(Iter(value.data() + value.size())),
|
||||
OutIter(out.data()));
|
||||
assert(base(ret.in) == value.data() + value.size());
|
||||
assert(base(ret.out) == out.data() + out.size());
|
||||
assert(out == expected);
|
||||
}
|
||||
{
|
||||
std::array<int, N> out;
|
||||
auto range = std::ranges::subrange(Iter(value.data()), Sent(Iter(value.data() + value.size())));
|
||||
std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
|
||||
std::ranges::rotate_copy(range, Iter(value.data() + middle), OutIter(out.data()));
|
||||
assert(base(ret.in) == value.data() + value.size());
|
||||
assert(base(ret.out) == out.data() + out.size());
|
||||
assert(out == expected);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Iter, class OutIter, class Sent>
|
||||
constexpr void test_iterators() {
|
||||
// simple test
|
||||
test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2});
|
||||
|
||||
// check that an empty range works
|
||||
test<Iter, OutIter, Sent, 0>({}, 0, {});
|
||||
|
||||
// check that a single element range works
|
||||
test<Iter, OutIter, Sent, 1>({1}, 0, {1});
|
||||
|
||||
// check that a two element range works
|
||||
test<Iter, OutIter, Sent, 2>({1, 2}, 1, {2, 1});
|
||||
|
||||
// rotate on the first element
|
||||
test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 0, {1, 2, 3, 4, 5, 6, 7});
|
||||
|
||||
// rotate on the second element
|
||||
test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 1, {2, 3, 4, 5, 6, 7, 1});
|
||||
|
||||
// rotate on the last element
|
||||
test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 6, {7, 1, 2, 3, 4, 5, 6});
|
||||
|
||||
// rotate on the one-past-the-end pointer
|
||||
test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
|
||||
}
|
||||
|
||||
template <class Iter, class Sent = Iter>
|
||||
constexpr void test_out_iterators() {
|
||||
test_iterators<Iter, cpp20_output_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, forward_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, bidirectional_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, random_access_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, contiguous_iterator<int*>, Sent>();
|
||||
test_iterators<Iter, int*, Sent>();
|
||||
}
|
||||
|
||||
constexpr bool test() {
|
||||
test_out_iterators<bidirectional_iterator<int*>>();
|
||||
test_out_iterators<random_access_iterator<int*>>();
|
||||
test_out_iterators<contiguous_iterator<int*>>();
|
||||
test_out_iterators<int*>();
|
||||
test_out_iterators<const int*>();
|
||||
|
||||
{
|
||||
struct AssignmentCounter {
|
||||
int* counter;
|
||||
|
||||
constexpr AssignmentCounter(int* counter_) : counter(counter_) {}
|
||||
constexpr AssignmentCounter& operator=(const AssignmentCounter&) { ++*counter; return *this; }
|
||||
};
|
||||
|
||||
{
|
||||
int c = 0;
|
||||
AssignmentCounter a[] = {&c, &c, &c, &c};
|
||||
AssignmentCounter b[] = {&c, &c, &c, &c};
|
||||
std::ranges::rotate_copy(a, a + 2, a + 4, b);
|
||||
assert(c == 4);
|
||||
}
|
||||
{
|
||||
int c = 0;
|
||||
AssignmentCounter a[] = {&c, &c, &c, &c};
|
||||
AssignmentCounter b[] = {&c, &c, &c, &c};
|
||||
std::ranges::rotate_copy(a, a + 2, b);
|
||||
assert(c == 4);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test();
|
||||
static_assert(test());
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -126,9 +126,9 @@ static_assert(test(std::ranges::replace, 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_copy, a, 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));
|
||||
static_assert(test(std::ranges::rotate_copy, a, a+5, a));
|
||||
//static_assert(test(std::ranges::sample, a, a, 5));
|
||||
//static_assert(test(std::ranges::search, a, a));
|
||||
//static_assert(test(std::ranges::search_n, a, 10, 42));
|
||||
|
|
Loading…
Reference in New Issue