-
Notifications
You must be signed in to change notification settings - Fork 29
/
adjacent.hpp
292 lines (237 loc) · 8.67 KB
/
adjacent.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_OP_ADJACENT_HPP_INCLUDED
#define FLUX_OP_ADJACENT_HPP_INCLUDED
#include <flux/core.hpp>
#include <flux/op/begin_end.hpp>
#include <flux/op/reverse.hpp>
#include <flux/op/zip.hpp>
#include <flux/source/iota.hpp>
#include <array>
namespace flux {
namespace detail {
template <typename Base, distance_t N>
struct adjacent_sequence_traits_base {
protected:
struct cursor_type {
std::array<cursor_t<Base>, N> arr{};
friend constexpr auto operator==(cursor_type const& lhs, cursor_type const& rhs)
-> bool
{
return lhs.arr.back() == rhs.arr.back();
}
friend constexpr auto operator<=>(cursor_type const& lhs, cursor_type const& rhs)
-> std::strong_ordering
requires ordered_cursor<cursor_t<Base>>
{
return lhs.arr.back() <=> rhs.arr.back();
}
};
public:
static inline constexpr bool is_infinite = infinite_sequence<Base>;
static constexpr auto first(auto& self) -> cursor_type
{
cursor_type out{flux::first(self.base_), };
for (auto i : flux::iota(std::size_t{1}, std::size_t{N})) {
out.arr[i] = out.arr[i - 1];
if (!flux::is_last(self.base_, out.arr[i])) {
flux::inc(self.base_, out.arr[i]);
}
}
return out;
}
static constexpr auto is_last(auto& self, cursor_type const& cur) -> bool
{
return flux::is_last(self.base_, cur.arr.back());
}
static constexpr auto inc(auto& self, cursor_type& cur) -> void
{
std::apply([&](auto&... curs) {
(flux::inc(self.base_, curs), ...);
}, cur.arr);
}
static constexpr auto last(auto& self) -> cursor_type
requires (bidirectional_sequence<Base> && bounded_sequence<Base>)
{
cursor_type out{};
out.arr.back() = flux::last(self.base_);
auto const first = flux::first(self.base_);
for (auto i : flux::iota(std::size_t{0}, std::size_t{N}-1).reverse()) {
out.arr[i] = out.arr[i + 1];
if (out.arr[i] != first) {
flux::dec(self.base_, out.arr[i]);
}
}
return out;
}
static constexpr auto dec(auto& self, cursor_type& cur) -> void
requires bidirectional_sequence<Base>
{
std::apply([&self](auto&... curs) {
(flux::dec(self.base_, curs), ...);
}, cur.arr);
}
static constexpr auto inc(auto& self, cursor_type& cur, distance_t offset) -> void
requires random_access_sequence<Base>
{
std::apply([&self, offset](auto&... curs) {
(flux::inc(self.base_, curs, offset), ...);
}, cur.arr);
}
static constexpr auto distance(auto& self, cursor_type const& from, cursor_type const& to)
-> distance_t
requires random_access_sequence<Base>
{
return flux::distance(self.base_, from.arr.back(), to.arr.back());
}
static constexpr auto size(auto& self) -> distance_t
requires sized_sequence<Base>
{
auto s = (flux::size(self.base_) - N) + 1;
return (std::max)(s, distance_t{0});
}
};
template <typename Base, distance_t N>
struct adjacent_adaptor : inline_sequence_base<adjacent_adaptor<Base, N>> {
private:
Base base_;
friend struct adjacent_sequence_traits_base<Base, N>;
public:
constexpr explicit adjacent_adaptor(decays_to<Base> auto&& base)
: base_(FLUX_FWD(base))
{}
struct flux_sequence_traits : adjacent_sequence_traits_base<Base, N> {
private:
using cursor_type = adjacent_sequence_traits_base<Base, N>::cursor_type;
template <auto& ReadFn>
static constexpr auto do_read(auto& self, cursor_type const& cur)
{
return std::apply([&](auto const&... curs) {
return pair_or_tuple_t<decltype(ReadFn(self.base_, curs))...>(
ReadFn(self.base_, curs)...);
}, cur.arr);
}
template <std::size_t I>
using base_value_t = value_t<Base>;
template <std::size_t... Is>
static auto make_value_type(std::index_sequence<Is...>) -> pair_or_tuple_t<base_value_t<Is>...>;
public:
using value_type = decltype(make_value_type(std::make_index_sequence<N>{}));
static constexpr auto read_at(auto& self, cursor_type const& cur)
-> decltype(do_read<flux::read_at>(self, cur))
{
return do_read<flux::read_at>(self, cur);
}
static constexpr auto move_at(auto& self, cursor_type const& cur)
-> decltype(do_read<flux::move_at>( self, cur))
{
return do_read<flux::move_at>( self, cur);
}
static constexpr auto read_at_unchecked(auto& self, cursor_type const& cur)
-> decltype(do_read<flux::read_at_unchecked>(self, cur))
{
return do_read<flux::read_at_unchecked>(self, cur);
}
static constexpr auto move_at_unchecked(auto& self, cursor_type const& cur)
-> decltype(do_read<flux::move_at_unchecked>(self, cur))
{
return do_read<flux::move_at_unchecked>(self, cur);
}
};
};
template <typename Func, typename E, distance_t N>
struct adjacent_invocable_helper {
template <std::size_t I>
using repeater = E;
static inline constexpr bool value = []<std::size_t... Is> (std::index_sequence<Is...>) consteval {
return std::regular_invocable<Func, repeater<Is>...>;
}(std::make_index_sequence<N>{});
};
template <typename Func, typename E, distance_t N>
concept adjacent_invocable = adjacent_invocable_helper<Func, E, N>::value;
template <typename Base, distance_t N, typename Func>
struct adjacent_map_adaptor : inline_sequence_base<adjacent_map_adaptor<Base, N, Func>> {
private:
Base base_;
Func func_;
friend struct adjacent_sequence_traits_base<Base, N>;
public:
constexpr explicit adjacent_map_adaptor(decays_to<Base> auto&& base, Func&& func)
: base_(FLUX_FWD(base)),
func_(std::move(func))
{}
struct flux_sequence_traits : adjacent_sequence_traits_base<Base, N> {
template <typename Self>
static constexpr auto read_at(Self& self, cursor_t<Self> const& cur)
-> decltype(auto)
requires adjacent_invocable<decltype((self.func_)), element_t<decltype((self.base_))>, N>
{
return std::apply([&](auto const&... curs) {
return std::invoke(self.func_, flux::read_at(self.base_, curs)...);
}, cur.arr);
}
};
};
template <distance_t N>
struct adjacent_fn {
template <adaptable_sequence Seq>
requires multipass_sequence<Seq>
[[nodiscard]]
constexpr auto operator()(Seq&& seq) const -> multipass_sequence auto
{
return adjacent_adaptor<std::decay_t<Seq>, N>(FLUX_FWD(seq));
}
};
template <distance_t N>
struct adjacent_map_fn {
template <adaptable_sequence Seq, typename Func>
requires multipass_sequence<Seq> &&
adjacent_invocable<Func, element_t<Seq>, N>
[[nodiscard]]
constexpr auto operator()(Seq&& seq, Func func) const -> multipass_sequence auto
{
return adjacent_map_adaptor<std::decay_t<Seq>, N, Func>(
FLUX_FWD(seq), std::move(func));
}
};
} // namespace detail
FLUX_EXPORT
template <distance_t N>
requires (N > 0)
inline constexpr auto adjacent = detail::adjacent_fn<N>{};
FLUX_EXPORT inline constexpr auto pairwise = adjacent<2>;
FLUX_EXPORT
template <distance_t N>
requires (N > 0)
inline constexpr auto adjacent_map = detail::adjacent_map_fn<N>{};
FLUX_EXPORT inline constexpr auto pairwise_map = adjacent_map<2>;
template <typename D>
template <distance_t N>
constexpr auto inline_sequence_base<D>::adjacent() &&
requires multipass_sequence<D>
{
return flux::adjacent<N>(std::move(derived()));
}
template <typename D>
constexpr auto inline_sequence_base<D>::pairwise() &&
requires multipass_sequence<D>
{
return flux::pairwise(std::move(derived()));
}
template <typename D>
template <distance_t N, typename Func>
requires multipass_sequence<D>
constexpr auto inline_sequence_base<D>::adjacent_map(Func func) &&
{
return flux::adjacent_map<N>(std::move(derived()), std::move(func));
}
template <typename D>
template <typename Func>
requires multipass_sequence<D>
constexpr auto inline_sequence_base<D>::pairwise_map(Func func) &&
{
return flux::pairwise_map(std::move(derived()), std::move(func));
}
} // namespace flux
#endif // FLUX_OP_ADJACENT_HPP_INCLUDED