| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <algorithm> |
| 10 |
|
| 11 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 12 |
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 13 |
|
| 14 |
// template<bidirectional_iterator I1, sentinel_for<I1> S1, bidirectional_iterator I2> |
| 15 |
// requires indirectly_copyable<I1, I2> |
| 16 |
// constexpr ranges::copy_backward_result<I1, I2> |
| 17 |
// ranges::copy_backward(I1 first, S1 last, I2 result); |
| 18 |
// template<bidirectional_range R, bidirectional_iterator I> |
| 19 |
// requires indirectly_copyable<iterator_t<R>, I> |
| 20 |
// constexpr ranges::copy_backward_result<borrowed_iterator_t<R>, I> |
| 21 |
// ranges::copy_backward(R&& r, I result); |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <array> |
| 25 |
#include <cassert> |
| 26 |
#include <deque> |
| 27 |
#include <ranges> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
#include "almost_satisfies_types.h" |
| 31 |
#include "test_iterators.h" |
| 32 |
|
| 33 |
template <class In, class Out = In, class Sent = sentinel_wrapper<In>> |
| 34 |
concept HasCopyBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::copy_backward(in, sent, out); }; |
| 35 |
|
| 36 |
static_assert(HasCopyBackwardIt<int*>); |
| 37 |
static_assert(!HasCopyBackwardIt<InputIteratorNotDerivedFrom>); |
| 38 |
static_assert(!HasCopyBackwardIt<InputIteratorNotIndirectlyReadable>); |
| 39 |
static_assert(!HasCopyBackwardIt<InputIteratorNotInputOrOutputIterator>); |
| 40 |
static_assert(!HasCopyBackwardIt<int*, WeaklyIncrementableNotMovable>); |
| 41 |
struct NotIndirectlyCopyable {}; |
| 42 |
static_assert(!HasCopyBackwardIt<int*, NotIndirectlyCopyable*>); |
| 43 |
static_assert(!HasCopyBackwardIt<int*, int*, SentinelForNotSemiregular>); |
| 44 |
static_assert(!HasCopyBackwardIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>); |
| 45 |
|
| 46 |
template <class Range, class Out> |
| 47 |
concept HasCopyBackwardR = requires(Range range, Out out) { std::ranges::copy_backward(range, out); }; |
| 48 |
|
| 49 |
static_assert(HasCopyBackwardR<std::array<int, 10>, int*>); |
| 50 |
static_assert(!HasCopyBackwardR<InputRangeNotDerivedFrom, int*>); |
| 51 |
static_assert(!HasCopyBackwardR<InputRangeNotIndirectlyReadable, int*>); |
| 52 |
static_assert(!HasCopyBackwardR<InputRangeNotInputOrOutputIterator, int*>); |
| 53 |
static_assert(!HasCopyBackwardR<WeaklyIncrementableNotMovable, int*>); |
| 54 |
static_assert(!HasCopyBackwardR<UncheckedRange<NotIndirectlyCopyable*>, int*>); |
| 55 |
static_assert(!HasCopyBackwardR<InputRangeNotSentinelSemiregular, int*>); |
| 56 |
static_assert(!HasCopyBackwardR<InputRangeNotSentinelEqualityComparableWith, int*>); |
| 57 |
|
| 58 |
static_assert(std::is_same_v<std::ranges::copy_result<int, long>, std::ranges::in_out_result<int, long>>); |
| 59 |
|
| 60 |
template <class In, class Out, class Sent> |
| 61 |
constexpr void test_iterators() { |
| 62 |
{ // simple test |
| 63 |
{ |
| 64 |
std::array in {1, 2, 3, 4}; |
| 65 |
std::array<int, 4> out; |
| 66 |
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = |
| 67 |
std::ranges::copy_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size())); |
| 68 |
assert(in == out); |
| 69 |
assert(base(ret.in) == in.data() + in.size()); |
| 70 |
assert(base(ret.out) == out.data()); |
| 71 |
} |
| 72 |
{ |
| 73 |
std::array in {1, 2, 3, 4}; |
| 74 |
std::array<int, 4> out; |
| 75 |
auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); |
| 76 |
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = |
| 77 |
std::ranges::copy_backward(range, Out(out.data() + out.size())); |
| 78 |
assert(in == out); |
| 79 |
assert(base(ret.in) == in.data() + in.size()); |
| 80 |
assert(base(ret.out) == out.data()); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
{ // check that an empty range works |
| 85 |
{ |
| 86 |
std::array<int, 0> in; |
| 87 |
std::array<int, 0> out; |
| 88 |
auto ret = |
| 89 |
std::ranges::copy_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size())); |
| 90 |
assert(base(ret.in) == in.data() + in.size()); |
| 91 |
assert(base(ret.out) == out.data()); |
| 92 |
} |
| 93 |
{ |
| 94 |
std::array<int, 0> in; |
| 95 |
std::array<int, 0> out; |
| 96 |
auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); |
| 97 |
auto ret = std::ranges::copy_backward(range, Out(out.data())); |
| 98 |
assert(base(ret.in) == in.data() + in.size()); |
| 99 |
assert(base(ret.out) == out.data()); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
template <class InContainer, class OutContainer, class In, class Out, class Sent = In> |
| 105 |
constexpr void test_containers() { |
| 106 |
{ |
| 107 |
InContainer in {1, 2, 3, 4}; |
| 108 |
OutContainer out(4); |
| 109 |
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = |
| 110 |
std::ranges::copy_backward(In(in.begin()), Sent(In(in.end())), Out(out.end())); |
| 111 |
assert(std::ranges::equal(in, out)); |
| 112 |
assert(base(ret.in) == in.end()); |
| 113 |
assert(base(ret.out) == out.begin()); |
| 114 |
} |
| 115 |
{ |
| 116 |
InContainer in {1, 2, 3, 4}; |
| 117 |
OutContainer out(4); |
| 118 |
auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end()))); |
| 119 |
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::copy_backward(range, Out(out.end())); |
| 120 |
assert(std::ranges::equal(in, out)); |
| 121 |
assert(base(ret.in) == in.end()); |
| 122 |
assert(base(ret.out) == out.begin()); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
template <class Iter, class Sent> |
| 127 |
constexpr void test_join_view() { |
| 128 |
auto to_subranges = std::views::transform([](auto& vec) { |
| 129 |
return std::ranges::subrange(Iter(vec.begin()), Sent(Iter(vec.end()))); |
| 130 |
}); |
| 131 |
|
| 132 |
{ // segmented -> contiguous |
| 133 |
std::vector<std::vector<int>> vectors = {}; |
| 134 |
auto range = vectors | to_subranges; |
| 135 |
std::vector<std::ranges::subrange<Iter, Sent>> subrange_vector(range.begin(), range.end()); |
| 136 |
std::array<int, 0> arr; |
| 137 |
|
| 138 |
std::ranges::copy_backward(subrange_vector | std::views::join, arr.end()); |
| 139 |
assert(std::ranges::equal(arr, std::array<int, 0>{})); |
| 140 |
} |
| 141 |
{ // segmented -> contiguous |
| 142 |
std::vector<std::vector<int>> vectors = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10}, {}}; |
| 143 |
auto range = vectors | to_subranges; |
| 144 |
std::vector<std::ranges::subrange<Iter, Sent>> subrange_vector(range.begin(), range.end()); |
| 145 |
std::array<int, 10> arr; |
| 146 |
|
| 147 |
std::ranges::copy_backward(subrange_vector | std::views::join, arr.end()); |
| 148 |
assert(std::ranges::equal(arr, std::array{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})); |
| 149 |
} |
| 150 |
{ // contiguous -> segmented |
| 151 |
std::vector<std::vector<int>> vectors = {{0, 0, 0, 0}, {0, 0}, {0, 0, 0, 0}, {}}; |
| 152 |
auto range = vectors | to_subranges; |
| 153 |
std::vector<std::ranges::subrange<Iter, Sent>> subrange_vector(range.begin(), range.end()); |
| 154 |
std::array arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 155 |
|
| 156 |
std::ranges::copy_backward(arr, (subrange_vector | std::views::join).end()); |
| 157 |
assert(std::ranges::equal(subrange_vector | std::views::join, std::array{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})); |
| 158 |
} |
| 159 |
{ // segmented -> segmented |
| 160 |
std::vector<std::vector<int>> vectors = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10}, {}}; |
| 161 |
auto range1 = vectors | to_subranges; |
| 162 |
std::vector<std::ranges::subrange<Iter, Sent>> subrange_vector(range1.begin(), range1.end()); |
| 163 |
std::vector<std::vector<int>> to_vectors = {{0, 0, 0, 0}, {0, 0, 0, 0}, {}, {0, 0}}; |
| 164 |
auto range2 = to_vectors | to_subranges; |
| 165 |
std::vector<std::ranges::subrange<Iter, Sent>> to_subrange_vector(range2.begin(), range2.end()); |
| 166 |
|
| 167 |
std::ranges::copy_backward(subrange_vector | std::views::join, (to_subrange_vector | std::views::join).end()); |
| 168 |
assert(std::ranges::equal(to_subrange_vector | std::views::join, std::array{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
template <class> |
| 173 |
constexpr bool is_proxy_iterator = false; |
| 174 |
|
| 175 |
template <class Iter> |
| 176 |
constexpr bool is_proxy_iterator<ProxyIterator<Iter>> = true; |
| 177 |
|
| 178 |
template <template <class> class InIter, template <class> class OutIter> |
| 179 |
constexpr void test_sentinels() { |
| 180 |
test_iterators<InIter<int*>, OutIter<int*>, InIter<int*>>(); |
| 181 |
test_iterators<InIter<int*>, OutIter<int*>, sentinel_wrapper<InIter<int*>>>(); |
| 182 |
test_iterators<InIter<int*>, OutIter<int*>, sized_sentinel<InIter<int*>>>(); |
| 183 |
|
| 184 |
if constexpr (!std::is_same_v<InIter<int*>, contiguous_iterator<int*>> && |
| 185 |
!std::is_same_v<OutIter<int*>, contiguous_iterator<int*>> && |
| 186 |
!std::is_same_v<InIter<int*>, ContiguousProxyIterator<int*>> && |
| 187 |
!std::is_same_v<OutIter<int*>, ContiguousProxyIterator<int*>>) { |
| 188 |
if (!std::is_constant_evaluated()) { |
| 189 |
test_containers<std::deque<int>, |
| 190 |
std::deque<int>, |
| 191 |
InIter<std::deque<int>::iterator>, |
| 192 |
OutIter<std::deque<int>::iterator>>(); |
| 193 |
test_containers<std::deque<int>, |
| 194 |
std::vector<int>, |
| 195 |
InIter<std::deque<int>::iterator>, |
| 196 |
OutIter<std::vector<int>::iterator>>(); |
| 197 |
test_containers<std::vector<int>, |
| 198 |
std::deque<int>, |
| 199 |
InIter<std::vector<int>::iterator>, |
| 200 |
OutIter<std::deque<int>::iterator>>(); |
| 201 |
test_containers<std::vector<int>, |
| 202 |
std::vector<int>, |
| 203 |
InIter<std::vector<int>::iterator>, |
| 204 |
OutIter<std::vector<int>::iterator>>(); |
| 205 |
} |
| 206 |
if constexpr (!is_proxy_iterator<InIter<int*>>) |
| 207 |
test_join_view<InIter<std::vector<int>::iterator>, InIter<std::vector<int>::iterator>>(); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
template <template <class> class Out> |
| 212 |
constexpr void test_in_iterators() { |
| 213 |
test_sentinels<bidirectional_iterator, Out>(); |
| 214 |
test_sentinels<random_access_iterator, Out>(); |
| 215 |
test_sentinels<contiguous_iterator, Out>(); |
| 216 |
test_sentinels<std::type_identity_t, Out>(); |
| 217 |
} |
| 218 |
|
| 219 |
template <template <class> class Out> |
| 220 |
constexpr void test_proxy_in_iterators() { |
| 221 |
test_sentinels<BidirectionalProxyIterator, Out>(); |
| 222 |
test_sentinels<RandomAccessProxyIterator, Out>(); |
| 223 |
test_sentinels<ContiguousProxyIterator, Out>(); |
| 224 |
test_sentinels<ProxyIterator, Out>(); |
| 225 |
} |
| 226 |
|
| 227 |
constexpr bool test() { |
| 228 |
test_in_iterators<bidirectional_iterator>(); |
| 229 |
test_in_iterators<random_access_iterator>(); |
| 230 |
test_in_iterators<contiguous_iterator>(); |
| 231 |
test_in_iterators<std::type_identity_t>(); |
| 232 |
|
| 233 |
test_proxy_in_iterators<BidirectionalProxyIterator>(); |
| 234 |
test_proxy_in_iterators<RandomAccessProxyIterator>(); |
| 235 |
test_proxy_in_iterators<ContiguousProxyIterator>(); |
| 236 |
|
| 237 |
{ // check that ranges::dangling is returned |
| 238 |
std::array<int, 4> out; |
| 239 |
std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret = |
| 240 |
std::ranges::copy_backward(std::array {1, 2, 3, 4}, out.data() + out.size()); |
| 241 |
assert(ret.out == out.data()); |
| 242 |
assert((out == std::array{1, 2, 3, 4})); |
| 243 |
} |
| 244 |
|
| 245 |
{ // check that an iterator is returned with a borrowing range |
| 246 |
std::array in {1, 2, 3, 4}; |
| 247 |
std::array<int, 4> out; |
| 248 |
std::same_as<std::ranges::in_out_result<std::array<int, 4>::iterator, int*>> auto ret = |
| 249 |
std::ranges::copy_backward(std::views::all(in), out.data() + out.size()); |
| 250 |
assert(ret.in == in.end()); |
| 251 |
assert(ret.out == out.data()); |
| 252 |
assert(in == out); |
| 253 |
} |
| 254 |
|
| 255 |
{ // check that every element is copied exactly once |
| 256 |
struct CopyOnce { |
| 257 |
bool copied = false; |
| 258 |
constexpr CopyOnce() = default; |
| 259 |
constexpr CopyOnce(const CopyOnce& other) = delete; |
| 260 |
constexpr CopyOnce& operator=(const CopyOnce& other) { |
| 261 |
assert(!other.copied); |
| 262 |
copied = true; |
| 263 |
return *this; |
| 264 |
} |
| 265 |
}; |
| 266 |
{ |
| 267 |
std::array<CopyOnce, 4> in {}; |
| 268 |
std::array<CopyOnce, 4> out {}; |
| 269 |
auto ret = std::ranges::copy_backward(in.begin(), in.end(), out.end()); |
| 270 |
assert(ret.in == in.end()); |
| 271 |
assert(ret.out == out.begin()); |
| 272 |
assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; })); |
| 273 |
} |
| 274 |
{ |
| 275 |
std::array<CopyOnce, 4> in {}; |
| 276 |
std::array<CopyOnce, 4> out {}; |
| 277 |
auto ret = std::ranges::copy_backward(in, out.end()); |
| 278 |
assert(ret.in == in.end()); |
| 279 |
assert(ret.out == out.begin()); |
| 280 |
assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; })); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
{ // check that the range is copied backwards |
| 285 |
struct OnlyBackwardsCopyable { |
| 286 |
OnlyBackwardsCopyable* next = nullptr; |
| 287 |
bool canCopy = false; |
| 288 |
OnlyBackwardsCopyable() = default; |
| 289 |
constexpr OnlyBackwardsCopyable& operator=(const OnlyBackwardsCopyable&) { |
| 290 |
assert(canCopy); |
| 291 |
if (next != nullptr) |
| 292 |
next->canCopy = true; |
| 293 |
return *this; |
| 294 |
} |
| 295 |
}; |
| 296 |
{ |
| 297 |
std::array<OnlyBackwardsCopyable, 3> in {}; |
| 298 |
std::array<OnlyBackwardsCopyable, 3> out {}; |
| 299 |
out[1].next = &out[0]; |
| 300 |
out[2].next = &out[1]; |
| 301 |
out[2].canCopy = true; |
| 302 |
auto ret = std::ranges::copy_backward(in, out.end()); |
| 303 |
assert(ret.in == in.end()); |
| 304 |
assert(ret.out == out.begin()); |
| 305 |
assert(out[0].canCopy); |
| 306 |
assert(out[1].canCopy); |
| 307 |
assert(out[2].canCopy); |
| 308 |
} |
| 309 |
{ |
| 310 |
std::array<OnlyBackwardsCopyable, 3> in {}; |
| 311 |
std::array<OnlyBackwardsCopyable, 3> out {}; |
| 312 |
out[1].next = &out[0]; |
| 313 |
out[2].next = &out[1]; |
| 314 |
out[2].canCopy = true; |
| 315 |
auto ret = std::ranges::copy_backward(in.begin(), in.end(), out.end()); |
| 316 |
assert(ret.in == in.end()); |
| 317 |
assert(ret.out == out.begin()); |
| 318 |
assert(out[0].canCopy); |
| 319 |
assert(out[1].canCopy); |
| 320 |
assert(out[2].canCopy); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return true; |
| 325 |
} |
| 326 |
|
| 327 |
int main(int, char**) { |
| 328 |
test(); |
| 329 |
static_assert(test()); |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |