-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunk_by_view.hpp
232 lines (195 loc) · 6.75 KB
/
chunk_by_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
// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
/*!\file
* \author Enrico Seiler <enrico.seiler AT fu-berlin.de>
* \brief Provides seqan::stl::views::chunk_by.
*/
// File might be included from multiple libraries.
#ifndef SEQAN_STD_CHUNK_BY_VIEW
#define SEQAN_STD_CHUNK_BY_VIEW
#include <algorithm>
#include <cassert>
#include <ranges>
#ifdef __cpp_lib_ranges_chunk_by
namespace seqan::stl::views
{
using std::ranges::views::chunk_by;
} // namespace seqan::stl::views
#else
# include "all_view.hpp"
# include "concepts.hpp"
# include "detail/adaptor_from_functor.hpp"
# include "detail/compiler_definitions.hpp"
# include "detail/movable_box.hpp"
# include "detail/non_propagating_cache.hpp"
namespace seqan::stl::ranges
{
template <std::ranges::forward_range V,
std::indirect_binary_predicate<std::ranges::iterator_t<V>, std::ranges::iterator_t<V>> Pred>
requires std::ranges::view<V> && std::is_object_v<Pred>
class chunk_by_view : public std::ranges::view_interface<chunk_by_view<V, Pred>>
{
private:
V base_ = V();
seqan::stl::detail::movable_box<Pred> pred_;
class iterator;
seqan::stl::detail::non_propagating_cache<std::ranges::iterator_t<V>> current_;
constexpr std::ranges::iterator_t<V> find_next(std::ranges::iterator_t<V> current)
{
assert(pred_.has_value());
auto pred = [this]<typename lhs_t, typename rhs_t>(lhs_t && lhs, rhs_t && rhs)
{
// Call pred with two arguments, return logical not.
return !bool((*pred_)(std::forward<lhs_t>(lhs), std::forward<rhs_t>(rhs)));
};
auto it = std::ranges::adjacent_find(current, std::ranges::end(base_), pred);
return std::ranges::next(it, 1, std::ranges::end(base_));
}
constexpr std::ranges::iterator_t<V> find_prev(std::ranges::iterator_t<V> current)
requires std::ranges::bidirectional_range<V>
{
assert(pred_.has_value());
auto pred = [this]<typename lhs_t, typename rhs_t>(lhs_t && lhs, rhs_t && rhs)
{
// Call pred with two (reversed) arguments, return logical not.
return !bool((*pred_)(std::forward<rhs_t>(rhs), std::forward<lhs_t>(lhs)));
};
auto rbegin = std::make_reverse_iterator(current);
auto rend = std::make_reverse_iterator(std::ranges::begin(base_));
assert(rbegin != rend);
auto it = std::ranges::adjacent_find(rbegin, rend, pred).base();
return std::ranges::prev(it, 1, std::ranges::begin(base_));
}
public:
chunk_by_view()
requires std::default_initializable<V> && std::default_initializable<Pred>
= default;
constexpr explicit chunk_by_view(V base, Pred pred) : base_{std::move(base)}, pred_{std::move(pred)}
{}
constexpr V base() const &
requires std::copy_constructible<V>
{
return base_;
}
constexpr V base() &&
{
return std::move(base_);
}
constexpr Pred const & pred() const
{
return *pred_;
}
constexpr iterator begin()
{
assert(pred_.has_value());
std::ranges::iterator_t<V> it;
if (current_.has_value())
{
it = current_.value();
}
else
{
it = find_next(std::ranges::begin(base_));
current_ = it;
}
return iterator{*this, std::ranges::begin(base_), it};
}
constexpr auto end()
{
if constexpr (std::ranges::common_range<V>)
{
return iterator{*this, std::ranges::end(base_), std::ranges::end(base_)};
}
else
{
return std::default_sentinel;
}
}
};
template <class R, class Pred>
chunk_by_view(R &&, Pred) -> chunk_by_view<seqan::stl::views::all_t<R>, Pred>;
template <std::ranges::forward_range V,
std::indirect_binary_predicate<std::ranges::iterator_t<V>, std::ranges::iterator_t<V>> Pred>
requires std::ranges::view<V> && std::is_object_v<Pred>
class chunk_by_view<V, Pred>::iterator
{
private:
chunk_by_view * parent_ = nullptr;
std::ranges::iterator_t<V> current_ = std::ranges::iterator_t<V>{};
std::ranges::iterator_t<V> next_ = std::ranges::iterator_t<V>{};
constexpr iterator(chunk_by_view & parent, std::ranges::iterator_t<V> current, std::ranges::iterator_t<V> next) :
parent_{std::addressof(parent)},
current_{std::move(current)},
next_{std::move(next)}
{}
friend chunk_by_view;
public:
using value_type = std::ranges::subrange<std::ranges::iterator_t<V>>;
using difference_type = std::ranges::range_difference_t<V>;
using iterator_category = std::input_iterator_tag;
using iterator_concept = std::conditional_t<std::ranges::bidirectional_range<V>, //
std::bidirectional_iterator_tag,
std::forward_iterator_tag>;
iterator() = default;
constexpr value_type operator*() const
{
assert(current_ != next_);
return std::ranges::subrange{current_, next_};
}
constexpr iterator & operator++()
{
assert(current_ != next_);
current_ = next_;
next_ = parent_->find_next(current_);
return *this;
}
constexpr iterator operator++(int)
{
auto tmp = *this;
++*this;
return tmp;
}
constexpr iterator & operator--()
requires std::ranges::bidirectional_range<V>
{
next_ = current_;
current_ = parent_->find_prev(next_);
return *this;
}
constexpr iterator operator--(int)
requires std::ranges::bidirectional_range<V>
{
auto tmp = *this;
--*this;
return tmp;
}
friend constexpr bool operator==(iterator const & x, iterator const & y)
{
return x.current_ == y.current_;
}
friend constexpr bool operator==(iterator const & x, std::default_sentinel_t)
{
return x.current_ == x.next_;
}
};
struct chunk_by_fn
{
template <typename Predicate>
constexpr auto operator()(Predicate && pred) const
{
return seqan::stl::detail::adaptor_from_functor{*this, std::forward<Predicate>(pred)};
}
template <seqan::stl::ranges::viewable_range Range, typename Predicate>
constexpr auto operator()(Range && range, Predicate && pred) const
{
return chunk_by_view{std::forward<Range>(range), std::forward<Predicate>(pred)};
}
};
} // namespace seqan::stl::ranges
namespace seqan::stl::views
{
inline constexpr auto chunk_by = seqan::stl::ranges::chunk_by_fn{};
} // namespace seqan::stl::views
#endif // ifdef __cpp_lib_ranges_chunk_by
#endif // SEQAN_STD_CHUNK_BY_VIEW