1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
//===----------------------------------------------------------------------===//
//
// 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
// std::views::drop
#include <ranges>
#include <array>
#include <cassert>
#include <concepts>
#include <span>
#include <string_view>
#include <utility>
#include "test_iterators.h"
template <class View, class T>
concept CanBePiped = requires (View&& view, T&& t) {
{ std::forward<View>(view) | std::forward<T>(t) };
};
struct SizedView : std::ranges::view_base {
int* begin_ = nullptr;
int* end_ = nullptr;
constexpr SizedView(int* begin, int* end) : begin_(begin), end_(end) {}
constexpr auto begin() const { return forward_iterator<int*>(begin_); }
constexpr auto end() const { return sized_sentinel<forward_iterator<int*>>(forward_iterator<int*>(end_)); }
};
static_assert(std::ranges::forward_range<SizedView>);
static_assert(std::ranges::sized_range<SizedView>);
static_assert(std::ranges::view<SizedView>);
struct SizedViewWithUnsizedSentinel : std::ranges::view_base {
using iterator = random_access_iterator<int*>;
using sentinel = sentinel_wrapper<random_access_iterator<int*>>;
int* begin_ = nullptr;
int* end_ = nullptr;
constexpr SizedViewWithUnsizedSentinel(int* begin, int* end) : begin_(begin), end_(end) {}
constexpr auto begin() const { return iterator(begin_); }
constexpr auto end() const { return sentinel(iterator(end_)); }
constexpr std::size_t size() const { return end_ - begin_; }
};
static_assert(std::ranges::random_access_range<SizedViewWithUnsizedSentinel>);
static_assert(std::ranges::sized_range<SizedViewWithUnsizedSentinel>);
static_assert(!std::sized_sentinel_for<SizedViewWithUnsizedSentinel::sentinel, SizedViewWithUnsizedSentinel::iterator>);
static_assert(std::ranges::view<SizedViewWithUnsizedSentinel>);
template <class T>
constexpr void test_small_range(const T& input) {
constexpr int N = 100;
auto size = std::ranges::size(input);
assert(size < N);
auto result = input | std::views::drop(N);
assert(result.empty());
}
constexpr bool test() {
constexpr int N = 8;
int buf[N] = {1, 2, 3, 4, 5, 6, 7, 8};
// Test that `std::views::drop` is a range adaptor.
{
using SomeView = SizedView;
// Test `view | views::drop`
{
SomeView view(buf, buf + N);
std::same_as<std::ranges::drop_view<SomeView>> decltype(auto) result = view | std::views::drop(3);
assert(result.base().begin_ == buf);
assert(result.base().end_ == buf + N);
assert(base(result.begin()) == buf + 3);
assert(base(base(result.end())) == buf + N);
assert(result.size() == 5);
}
// Test `adaptor | views::drop`
{
SomeView view(buf, buf + N);
auto f = [](int i) { return i; };
auto const partial = std::views::transform(f) | std::views::drop(3);
using Result = std::ranges::drop_view<std::ranges::transform_view<SomeView, decltype(f)>>;
std::same_as<Result> decltype(auto) result = partial(view);
assert(result.base().base().begin_ == buf);
assert(result.base().base().end_ == buf + N);
assert(base(result.begin().base()) == buf + 3);
assert(base(base(result.end().base())) == buf + N);
assert(result.size() == 5);
}
// Test `views::drop | adaptor`
{
SomeView view(buf, buf + N);
auto f = [](int i) { return i; };
auto const partial = std::views::drop(3) | std::views::transform(f);
using Result = std::ranges::transform_view<std::ranges::drop_view<SomeView>, decltype(f)>;
std::same_as<Result> decltype(auto) result = partial(view);
assert(result.base().base().begin_ == buf);
assert(result.base().base().end_ == buf + N);
assert(base(result.begin().base()) == buf + 3);
assert(base(base(result.end().base())) == buf + N);
assert(result.size() == 5);
}
// Check SFINAE friendliness
{
struct NotAView { };
static_assert(!std::is_invocable_v<decltype(std::views::drop)>);
static_assert(!std::is_invocable_v<decltype(std::views::drop), NotAView, int>);
static_assert( CanBePiped<SomeView&, decltype(std::views::drop(3))>);
static_assert( CanBePiped<int(&)[10], decltype(std::views::drop(3))>);
static_assert(!CanBePiped<int(&&)[10], decltype(std::views::drop(3))>);
static_assert(!CanBePiped<NotAView, decltype(std::views::drop(3))>);
static_assert(!CanBePiped<SomeView&, decltype(std::views::drop(/*n=*/NotAView{}))>);
}
}
{
static_assert(std::same_as<decltype(std::views::drop), decltype(std::ranges::views::drop)>);
}
// `views::drop(empty_view, n)` returns an `empty_view`.
{
using Result = std::ranges::empty_view<int>;
[[maybe_unused]] std::same_as<Result> decltype(auto) result = std::views::empty<int> | std::views::drop(3);
}
// `views::drop(span, n)` returns a `span`.
{
std::span<int> s(buf);
std::same_as<decltype(s)> decltype(auto) result = s | std::views::drop(5);
assert(result.size() == 3);
}
// `views::drop(span, n)` returns a `span` with a dynamic extent, regardless of the input `span`.
{
std::span<int, 8> s(buf);
std::same_as<std::span<int, std::dynamic_extent>> decltype(auto) result = s | std::views::drop(3);
assert(result.size() == 5);
}
// `views::drop(string_view, n)` returns a `string_view`.
{
{
std::string_view sv = "abcdef";
std::same_as<decltype(sv)> decltype(auto) result = sv | std::views::drop(2);
assert(result.size() == 4);
}
{
std::u32string_view sv = U"abcdef";
std::same_as<decltype(sv)> decltype(auto) result = sv | std::views::drop(2);
assert(result.size() == 4);
}
}
// `views::drop(iota_view, n)` returns an `iota_view`.
{
auto iota = std::views::iota(1, 8);
// The second template argument of the resulting `iota_view` is different because it has to be able to hold
// the `range_difference_t` of the input `iota_view`.
using Result = std::ranges::iota_view<int, int>;
std::same_as<Result> decltype(auto) result = iota | std::views::drop(3);
assert(result.size() == 4);
assert(*result.begin() == 4);
assert(*std::ranges::next(result.begin(), 3) == 7);
}
// `views::drop(subrange, n)` returns a `subrange` when `subrange::StoreSize == false`.
{
auto subrange = std::ranges::subrange(buf, buf + N);
LIBCPP_STATIC_ASSERT(!decltype(subrange)::_StoreSize);
using Result = std::ranges::subrange<int*>;
std::same_as<Result> decltype(auto) result = subrange | std::views::drop(3);
assert(result.size() == 5);
}
// `views::drop(subrange, n)` returns a `subrange` when `subrange::StoreSize == true`.
{
using View = SizedViewWithUnsizedSentinel;
View view(buf, buf + N);
using Subrange = std::ranges::subrange<View::iterator, View::sentinel, std::ranges::subrange_kind::sized>;
auto subrange = Subrange(view.begin(), view.end(), std::ranges::distance(view.begin(), view.end()));
LIBCPP_STATIC_ASSERT(decltype(subrange)::_StoreSize);
std::same_as<Subrange> decltype(auto) result = subrange | std::views::drop(3);
assert(result.size() == 5);
}
// `views::drop(subrange, n)` doesn't return a `subrange` if it's not a random access range.
{
SizedView v(buf, buf + N);
auto subrange = std::ranges::subrange(v.begin(), v.end());
using Result = std::ranges::drop_view<std::ranges::subrange<forward_iterator<int*>,
sized_sentinel<forward_iterator<int*>>>>;
std::same_as<Result> decltype(auto) result = subrange | std::views::drop(3);
assert(result.size() == 5);
}
// When the size of the input range `s` is shorter than `n`, an `empty_view` is returned.
{
test_small_range(std::span(buf));
test_small_range(std::string_view("abcdef"));
test_small_range(std::ranges::subrange(buf, buf + N));
test_small_range(std::views::iota(1, 8));
}
// Test that it's possible to call `std::views::drop` with any single argument as long as the resulting closure is
// never invoked. There is no good use case for it, but it's valid.
{
struct X { };
[[maybe_unused]] auto partial = std::views::drop(X{});
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|