-
Notifications
You must be signed in to change notification settings - Fork 2
/
to.hpp
232 lines (190 loc) · 7.43 KB
/
to.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::ranges::to.
*/
// File might be included from multiple libraries.
#ifndef SEQAN_STD_TO
#define SEQAN_STD_TO
#include <ranges>
#ifdef __cpp_lib_ranges_to_container
namespace seqan::stl
{
using std::from_range_t;
inline constexpr from_range_t from_range{};
} // namespace seqan::stl
namespace seqan::stl::ranges
{
using std::ranges::to;
} // namespace seqan::stl::ranges
#else
# include <algorithm>
# include "detail/adaptor_from_functor.hpp"
namespace seqan::stl
{
struct from_range_t
{
explicit from_range_t() = default;
};
inline constexpr from_range_t from_range{};
} // namespace seqan::stl
namespace seqan::stl::detail::to
{
// clang-format off
template <class Container>
constexpr bool reservable_container =
std::ranges::sized_range<Container> &&
requires (Container & c, std::ranges::range_size_t<Container> n)
{
c.reserve(n);
{ c.capacity() } -> std::same_as<decltype(n)>;
{ c.max_size() } -> std::same_as<decltype(n)>;
};
template <class Container, class Ref>
constexpr bool container_insertable =
requires (Container & c, Ref && ref)
{
requires
(
requires { c.push_back(std::forward<Ref>(ref)); } ||
requires { c.insert(c.end(), std::forward<Ref>(ref)); }
);
};
// clang-format on
template <class Ref, class Container>
constexpr auto container_inserter(Container & c)
{
if constexpr (requires { c.push_back(std::declval<Ref>()); })
return std::back_inserter(c);
else
return std::inserter(c, c.end());
}
template <std::ranges::input_range R>
struct input_iterator
{
using iterator_category = std::input_iterator_tag;
using value_type = std::ranges::range_value_t<R>;
using difference_type = std::ptrdiff_t;
using pointer = std::add_pointer_t<std::ranges::range_reference_t<R>>;
using reference = std::ranges::range_reference_t<R>;
reference operator*() const;
pointer operator->() const;
input_iterator & operator++();
input_iterator operator++(int);
bool operator==(input_iterator const &) const;
};
template <typename It>
concept has_input_iterator_category =
std::derived_from<typename std::iterator_traits<It>::iterator_category, std::input_iterator_tag>;
} // namespace seqan::stl::detail::to
namespace seqan::stl::ranges
{
template <class C, std::ranges::input_range R, class... Args>
requires (!std::ranges::view<C>)
constexpr C to(R && r, Args &&... args)
{
if constexpr (!std::ranges::input_range<C>
|| std::convertible_to<std::ranges::range_reference_t<R>, std::ranges::range_value_t<C>>)
{
if constexpr (std::constructible_from<C, R, Args...>)
return C(std::forward<R>(r), std::forward<Args>(args)...);
else if constexpr (std::constructible_from<C, seqan::stl::from_range_t, R, Args...>)
return C(seqan::stl::from_range, std::forward<R>(r), std::forward<Args>(args)...);
else if constexpr (std::ranges::common_range<R>
&& seqan::stl::detail::to::has_input_iterator_category<std::ranges::iterator_t<R>>
&& std::
constructible_from<C, std::ranges::iterator_t<R>, std::ranges::sentinel_t<R>, Args...>)
return C(std::ranges::begin(r), std::ranges::end(r), std::forward<Args>(args)...);
else if constexpr (std::constructible_from<C, Args...>
&& seqan::stl::detail::to::container_insertable<C, std::ranges::range_reference_t<R>>)
{
C c(std::forward<Args>(args)...);
if constexpr (std::ranges::sized_range<R> && seqan::stl::detail::to::reservable_container<C>)
c.reserve(static_cast<std::ranges::range_size_t<C>>(std::ranges::size(r)));
std::ranges::copy(r, seqan::stl::detail::to::container_inserter<std::ranges::range_reference_t<R>>(c));
return c;
}
}
else if constexpr (std::ranges::input_range<std::ranges::range_reference_t<R>>)
return to<C>(r
| std::views::transform(
[](auto && elem)
{
return to<std::ranges::range_value_t<C>>(std::forward<decltype(elem)>(elem));
}),
std::forward<Args>(args)...);
else
__builtin_unreachable();
}
template <template <class...> class C, std::ranges::input_range R, class... Args>
constexpr auto to(R && r, Args &&... args)
{
if constexpr (requires { C(std::declval<R>(), std::declval<Args>()...); })
return to<decltype(C(std::declval<R>(), std::declval<Args>()...))>(std::forward<R>(r),
std::forward<Args>(args)...);
else if constexpr (requires { C(seqan::stl::from_range, std::declval<R>(), std::declval<Args>()...); })
return to<decltype(C(seqan::stl::from_range, std::declval<R>(), std::declval<Args>()...))>(
std::forward<R>(r),
std::forward<Args>(args)...);
else if constexpr (requires {
C(std::declval<seqan::stl::detail::to::input_iterator<R>>(),
std::declval<seqan::stl::detail::to::input_iterator<R>>(),
std::declval<Args>()...);
})
return to<decltype(C(std::declval<seqan::stl::detail::to::input_iterator<R>>(),
std::declval<seqan::stl::detail::to::input_iterator<R>>(),
std::declval<Args>()...))>(std::forward<R>(r), std::forward<Args>(args)...);
else
__builtin_unreachable();
}
} // namespace seqan::stl::ranges
namespace seqan::stl::detail::to
{
template <class C>
requires (!std::ranges::view<C>)
struct to_fn1
{
template <class... Args>
constexpr auto operator()(Args &&... args) const
{
return seqan::stl::detail::adaptor_from_functor{*this, std::forward<Args>(args)...};
}
template <std::ranges::input_range R, class... Args>
constexpr auto operator()(R && r, Args &&... args) const
{
return seqan::stl::ranges::to<C>(std::forward<R>(r), std::forward<Args>(args)...);
}
};
template <template <class...> class C>
struct to_fn2
{
template <class... Args>
constexpr auto operator()(Args &&... args) const
{
return seqan::stl::detail::adaptor_from_functor{*this, std::forward<Args>(args)...};
}
template <std::ranges::input_range R, class... Args>
constexpr auto operator()(R && r, Args &&... args) const
{
return seqan::stl::ranges::to<C>(std::forward<R>(r), std::forward<Args>(args)...);
}
};
} // namespace seqan::stl::detail::to
namespace seqan::stl::ranges
{
template <class C, class... Args>
requires (!std::ranges::view<C>)
constexpr auto to(Args &&... args)
{
return seqan::stl::detail::to::to_fn1<C>{}(std::forward<Args>(args)...);
}
template <template <class...> class C, class... Args>
constexpr auto to(Args &&... args)
{
return seqan::stl::detail::to::to_fn2<C>{}(std::forward<Args>(args)...);
}
} // namespace seqan::stl::ranges
#endif // ifdef __cpp_lib_ranges_to_container
#endif // SEQAN_STD_TO