-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_view.hpp
273 lines (241 loc) · 11.2 KB
/
split_view.hpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <ranges>
#include <algorithm>
#include <iterator>
template<class F, int = (F(), 0)>
constexpr bool is_constexpr(F) { return true; }
constexpr bool is_constexpr(...) { return false; }
template<class R>
concept simple_view = std::ranges::view<R> && std::ranges::range<const R> &&
std::same_as<std::ranges::iterator_t<R>, std::ranges::iterator_t<const R>> &&
std::same_as<std::ranges::sentinel_t<R>, std::ranges::sentinel_t<const R>>;
template<bool Const, class T>
using maybe_const = std::conditional_t<Const, const T, T>;
template<std::ranges::input_range V, std::ranges::forward_range Pattern>
requires std::ranges::view<V> && std::ranges::view<Pattern> &&
std::indirectly_comparable<std::ranges::iterator_t<V>, std::ranges::iterator_t<Pattern>,
std::ranges::equal_to> &&
(std::ranges::forward_range<V> || (std::ranges::sized_range<V> &&
(is_constexpr)([]{ Pattern::size(); }) && Pattern::size() <= 1))
class split_view : public std::ranges::view_interface<split_view<V, Pattern>> {
private:
[[no_unique_address]] V base_ = V();
[[no_unique_address]] Pattern pattern_ = Pattern();
[[no_unique_address]]
std::conditional_t<!std::ranges::forward_range<V>,
std::ranges::iterator_t<V>, std::ranges::dangling> current_ = decltype(current_)();
template<bool> struct outer_iterator;
template<bool> struct inner_iterator;
friend constexpr bool operator==(const inner_iterator<true>& x, std::default_sentinel_t);
friend constexpr bool operator==(const inner_iterator<false>& x, std::default_sentinel_t);
template<bool Const> struct outer_iterator {
private:
template<bool> friend struct outer_iterator;
template<bool> friend struct inner_iterator;
friend constexpr bool operator==(const inner_iterator<Const>& x, std::default_sentinel_t);
using Parent = maybe_const<Const, split_view>;
using Base = maybe_const<Const, V>;
Parent* parent_ = nullptr;
[[no_unique_address]]
std::conditional_t<std::ranges::forward_range<V>,
std::ranges::iterator_t<Base>, std::ranges::dangling> current_ = decltype(current_)();
public:
constexpr auto& get_current_() {
if constexpr (std::ranges::forward_range<V>)
return this->current_;
else
return parent_->current_;
}
constexpr auto& get_current_() const {
if constexpr (std::ranges::forward_range<V>)
return this->current_;
else
return parent_->current_;
}
public:
using iterator_concept =
std::conditional_t<std::ranges::forward_range<Base>,
std::forward_iterator_tag, std::input_iterator_tag>;
using iterator_category = std::input_iterator_tag;
struct value_type : std::ranges::view_interface<value_type> {
private:
outer_iterator i_ = outer_iterator();
public:
value_type() = default;
constexpr explicit value_type(outer_iterator i) : i_(std::move(i)) {}
constexpr inner_iterator<Const> begin() const requires std::copyable<outer_iterator> {
return inner_iterator<Const>{i_};
}
constexpr inner_iterator<Const> begin() requires (!std::copyable<outer_iterator>) {
return inner_iterator<Const>{std::move(i_)};
}
constexpr std::default_sentinel_t end() const {
return std::default_sentinel;
}
};
using difference_type = std::ranges::range_difference_t<Base>;
outer_iterator() = default;
constexpr explicit outer_iterator(Parent& parent)
requires (!std::ranges::forward_range<Base>)
: parent_(std::addressof(parent)) {}
constexpr explicit outer_iterator(Parent& parent, std::ranges::iterator_t<Base> current)
requires std::ranges::forward_range<Base>
: parent_(std::addressof(parent)), current_(std::move(current)) {}
constexpr outer_iterator(outer_iterator<!Const> i)
requires Const && std::convertible_to<std::ranges::iterator_t<V>,
std::ranges::iterator_t<Base>>
: parent_(i.parent_), current_(std::move(i.current_)) {}
constexpr value_type operator*() const { return value_type{*this}; }
constexpr outer_iterator& operator++() {
const auto end = std::ranges::end(parent_->base_);
if (get_current_() == end)
return *this;
const auto [pbegin, pend] = std::ranges::subrange{parent_->pattern_};
if (pbegin == pend)
++get_current_();
else if constexpr (!std::ranges::forward_range<Base>) {
get_current_() = std::ranges::find(std::move(get_current_()), end, *pbegin);
if (get_current_() != end) {
++get_current_();
}
} else {
do {
auto [b, p] =
std::ranges::mismatch(std::move(get_current_()), end, pbegin, pend);
if (p == pend) {
get_current_() = std::move(b);
break;
}
} while (++get_current_() != end);
}
return *this;
}
constexpr decltype(auto) operator++(int) {
if constexpr (std::ranges::forward_range<Base>) {
auto tmp = *this;
++*this;
return tmp;
} else {
++*this;
}
}
friend constexpr bool operator==(const outer_iterator& x, const outer_iterator& y)
requires std::ranges::forward_range<Base> {
return x.current_ == y.current_;
}
friend constexpr bool operator==(const outer_iterator& x, std::default_sentinel_t) {
return x.get_current_() == std::ranges::end(x.parent_->base_);
}
};
template<bool Const>
struct inner_iterator {
private:
using Base = maybe_const<Const, V>;
outer_iterator<Const> i_ = decltype(i_)();
bool incremented_ = false;
public:
using iterator_concept = typename outer_iterator<Const>::iterator_concept;
using iterator_category = std::conditional_t<
std::derived_from<
typename std::iterator_traits<std::ranges::iterator_t<Base>>::iterator_category,
std::forward_iterator_tag>,
std::forward_iterator_tag,
typename std::iterator_traits<std::ranges::iterator_t<Base>>::iterator_category>;
using value_type = std::ranges::range_value_t<Base>;
using difference_type = std::ranges::range_difference_t<Base>;
inner_iterator() = default;
constexpr explicit inner_iterator(outer_iterator<Const> i) : i_(std::move(i)) {}
constexpr decltype(auto) operator*() const { return *i_.get_current_(); }
constexpr inner_iterator& operator++() {
incremented_ = true;
if constexpr (!std::ranges::forward_range<Base>) {
if constexpr (Pattern::size() == 0) {
return *this;
}
}
++i_.get_current_();
return *this;
}
constexpr decltype(auto) operator++(int) {
if constexpr (std::ranges::forward_range<V>) {
auto tmp = *this;
++*this;
return tmp;
} else {
++*this;
}
}
friend constexpr bool operator==(const inner_iterator& x, const inner_iterator& y)
requires std::ranges::forward_range<Base> {
return x.i_.get_current_() == y.i_.get_current_();
}
friend constexpr bool operator==(const inner_iterator& x, std::default_sentinel_t) {
auto [pcur, pend] = std::ranges::subrange{x.i_.parent_->pattern_};
auto end = std::ranges::end(x.i_.parent_->base_);
if constexpr (!std::ranges::forward_range<Base>) {
const auto& cur = x.i_.get_current_();
if (cur == end) return true;
if (pcur == pend) return x.incremented_;
return *cur == *pcur;
} else {
auto cur = x.i_.get_current_();
if (cur == end) return true;
if (pcur == pend) return x.incremented_;
do {
if (*cur != *pcur) return false;
if (++pcur == pend) return true;
} while (++cur != end);
return false;
}
}
friend constexpr decltype(auto) iter_move(const inner_iterator& i)
noexcept(noexcept(std::ranges::iter_move(i.i_.get_current_()))) {
return std::ranges::iter_move(i.i_.get_current_());
}
friend constexpr void iter_swap(const inner_iterator& x, const inner_iterator& y)
noexcept(noexcept(std::ranges::iter_swap(x.i_.get_current_(), y.i_.get_current_())))
requires std::indirectly_swappable<std::ranges::iterator_t<Base>> {
std::ranges::iter_swap(x.i_.get_current_(), y.i_.get_current_());
}
};
public:
split_view() = default;
constexpr split_view(V base, Pattern pattern)
: base_(std::move(base)), pattern_(std::move(pattern)) {}
template<std::ranges::input_range R>
requires std::constructible_from<V, std::views::all_t<R>> &&
std::constructible_from<Pattern,
std::ranges::single_view<std::ranges::range_value_t<R>>>
constexpr split_view(R&& r, std::ranges::range_value_t<R> e)
: base_(std::views::all(std::forward<R>(r))),
pattern_(std::ranges::single_view{std::move(e)}) {}
constexpr V base() const& requires std::copy_constructible<V> { return base_; }
constexpr V base() && { return std::move(base_); }
constexpr auto begin() {
if constexpr (std::ranges::forward_range<V>)
return outer_iterator<simple_view<V>>{*this, std::ranges::begin(base_)};
else {
current_ = std::ranges::begin(base_);
return outer_iterator<false>{*this};
}
}
constexpr auto begin() const requires std::ranges::forward_range<V> &&
std::ranges::forward_range<const V> {
return outer_iterator<true>{*this, std::ranges::begin(base_)};
}
constexpr auto end() requires std::ranges::forward_range<V> &&
std::ranges::common_range<V> {
return outer_iterator<simple_view<V>>{*this, std::ranges::end(base_)};
}
constexpr auto end() const {
if constexpr (std::ranges::forward_range<V> &&
std::ranges::forward_range<const V> && std::ranges::common_range<const V>)
return outer_iterator<true>{*this, std::ranges::end(base_)};
else
return std::default_sentinel;
}
};
template<class R, class P>
split_view(R&&, P&&) -> split_view<std::views::all_t<R>, std::views::all_t<P>>;
template<std::ranges::input_range R>
split_view(R&&, std::ranges::range_value_t<R>)
-> split_view<std::views::all_t<R>, std::ranges::single_view<std::ranges::range_value_t<R>>>;