18#include <seqan3/contrib/std/detail/movable_box.hpp>
27namespace seqan3::detail
47template <std::ranges::view urng_t,
typename fun_t,
bool or_throw,
bool and_consume>
48class view_take_until :
public std::ranges::view_interface<view_take_until<urng_t, fun_t, or_throw, and_consume>>
51 static_assert(std::invocable<fun_t, std::ranges::range_reference_t<urng_t>>,
52 "The functor type for detail::take_until must model"
53 "std::invocable<fun_t, std::ranges::range_reference_t<urng_t>>.");
54 static_assert(std::convertible_to<std::invoke_result_t<fun_t &&, std::ranges::range_reference_t<urng_t>>,
bool>,
55 "The result type of the functor for detail::take_until must be a boolean.");
61 seqan::stl::detail::movable_box_t<fun_t> fun;
64 static constexpr bool const_iterable =
69 template <
bool const_range>
70 using basic_iterator = seqan3::detail::maybe_const_iterator_t<const_range, urng_t>;
74 template <
bool const_range>
79 template <
bool const_range>
80 class basic_consume_iterator;
84 template <
bool const_range>
85 using basic_consume_sentinel = std::default_sentinel_t;
91 view_take_until() =
default;
92 constexpr view_take_until(view_take_until
const & rhs) =
default;
93 constexpr view_take_until(view_take_until && rhs) =
default;
94 constexpr view_take_until & operator=(view_take_until
const & rhs) =
default;
95 constexpr view_take_until & operator=(view_take_until && rhs) =
default;
96 ~view_take_until() =
default;
102 view_take_until(urng_t && _urange, fun_t && _fun) :
112 template <std::ranges::viewable_range rng_t>
113 requires std::constructible_from<urng_t, std::views::all_t<rng_t>>
114 view_take_until(rng_t && _urange, fun_t && _fun) :
135 auto begin() noexcept
137 if constexpr (and_consume && !std::ranges::forward_range<urng_t>)
138 return basic_consume_iterator<false>{
std::ranges::begin(urange), fun, std::ranges::end(urange)};
144 auto begin() const noexcept
145 requires const_iterable
147 if constexpr (and_consume && !std::ranges::forward_range<urng_t const>)
148 return basic_consume_iterator<true>{
std::ranges::cbegin(urange), fun, std::ranges::cend(urange)};
168 if constexpr (and_consume && !std::ranges::forward_range<urng_t>)
169 return basic_consume_sentinel<false>{};
171 return basic_sentinel<false>{std::ranges::end(urange), fun};
175 auto end() const noexcept
176 requires const_iterable
178 if constexpr (and_consume && !std::ranges::forward_range<urng_t const>)
179 return basic_consume_sentinel<true>{};
181 return basic_sentinel<true>{std::ranges::end(urange), fun};
188template <
typename urng_t,
typename fun_t,
bool or_throw = false,
bool and_consume = false>
189view_take_until(urng_t &&, fun_t &&) -> view_take_until<std::views::all_t<urng_t>, fun_t, or_throw, and_consume>;
191template <std::ranges::view urng_t,
typename fun_t,
bool or_throw,
bool and_consume>
192template <
bool const_range>
193class view_take_until<urng_t, fun_t, or_throw, and_consume>::basic_consume_iterator :
194 public inherited_iterator_base<basic_consume_iterator<const_range>,
195 seqan3::detail::maybe_const_iterator_t<const_range, urng_t>>
199 using underlying_iterator_t = seqan3::detail::maybe_const_iterator_t<const_range, urng_t>;
201 using base_t = inherited_iterator_base<basic_consume_iterator, underlying_iterator_t>;
204 seqan::stl::detail::movable_box_t<fun_t>
const * fun{
nullptr};
207 using underlying_sentinel_t = seqan3::detail::maybe_const_sentinel_t<const_range, urng_t>;
210 underlying_sentinel_t underlying_sentinel;
213 bool at_end_gracefully =
false;
220 constexpr basic_consume_iterator() =
default;
221 constexpr basic_consume_iterator(basic_consume_iterator
const & rhs) =
default;
222 constexpr basic_consume_iterator(basic_consume_iterator && rhs) =
default;
223 constexpr basic_consume_iterator & operator=(basic_consume_iterator
const & rhs) =
default;
224 constexpr basic_consume_iterator & operator=(basic_consume_iterator && rhs) =
default;
225 ~basic_consume_iterator() =
default;
228 basic_consume_iterator(underlying_iterator_t it,
229 seqan::stl::detail::movable_box_t<fun_t>
const & _fun,
230 underlying_sentinel_t sen)
noexcept(
noexcept(base_t{it})) :
231 base_t{std::move(it)},
233 underlying_sentinel{std::move(sen)}
235 if ((this->base() != underlying_sentinel) && fun->operator()(**
this))
237 at_end_gracefully =
true;
250 using pointer = detail::iter_pointer_t<underlying_iterator_t>;
259 basic_consume_iterator & operator++()
260 noexcept(noexcept(++
std::declval<base_t &>())
261 && noexcept(
std::declval<underlying_iterator_t &>() !=
std::declval<underlying_sentinel_t &>())
262 && noexcept(fun->operator()(
std::declval<reference>())))
264 base_t::operator++();
266 while ((this->base() != underlying_sentinel) && fun->operator()(**
this))
268 at_end_gracefully =
true;
269 base_t::operator++();
276 decltype(
auto)
operator++(
int)
noexcept(
noexcept(++std::declval<basic_consume_iterator &>())
277 && (std::same_as<
decltype(std::declval<underlying_iterator_t &>()++),
void>
278 || std::is_nothrow_copy_constructible_v<basic_consume_iterator>))
281 if constexpr (std::same_as<decltype(std::declval<underlying_iterator_t &>()++),
void>)
287 basic_consume_iterator cpy{*
this};
298 bool operator==(basic_consume_sentinel<const_range>
const &)
const
300 &&
noexcept(std::declval<underlying_iterator_t &>() != std::declval<underlying_sentinel_t &>())
301 &&
noexcept(fun->operator()(std::declval<reference>())))
303 if (at_end_gracefully)
306 if (this->base() == underlying_sentinel)
308 if constexpr (or_throw)
309 throw unexpected_end_of_input{
"Reached end of input before functor evaluated to true."};
314 return fun->operator()(**this);
318 friend bool operator==(basic_consume_sentinel<const_range>
const & lhs, basic_consume_iterator
const & rhs)
319 noexcept(
noexcept(rhs == lhs))
325 bool operator!=(basic_consume_sentinel<const_range>
const & rhs)
const
326 noexcept(
noexcept(std::declval<basic_consume_iterator &>() == rhs))
328 return !(*
this == rhs);
332 friend bool operator!=(basic_consume_sentinel<const_range>
const & lhs, basic_consume_iterator
const & rhs)
333 noexcept(
noexcept(rhs != lhs))
340template <std::ranges::view urng_t,
typename fun_t,
bool or_throw,
bool and_consume>
341template <
bool const_range>
342class view_take_until<urng_t, fun_t, or_throw, and_consume>::basic_sentinel
346 using underlying_sentinel_t = seqan3::detail::maybe_const_sentinel_t<const_range, urng_t>;
349 underlying_sentinel_t underlying_sentinel{};
352 seqan::stl::detail::movable_box_t<fun_t>
const * fun{
nullptr};
358 basic_sentinel() =
default;
359 basic_sentinel(basic_sentinel
const &) =
default;
360 basic_sentinel(basic_sentinel &&) =
default;
361 basic_sentinel & operator=(basic_sentinel
const &) =
default;
362 basic_sentinel & operator=(basic_sentinel &&) =
default;
363 ~basic_sentinel() =
default;
369 explicit basic_sentinel(underlying_sentinel_t underlying_sentinel,
370 seqan::stl::detail::movable_box_t<fun_t>
const & _fun) :
371 underlying_sentinel{
std::
move(underlying_sentinel)},
376 basic_sentinel(basic_sentinel<!const_range> other)
377 requires const_range && std::convertible_to<std::ranges::sentinel_t<urng_t>, underlying_sentinel_t>
378 : underlying_sentinel{std::move(other.underlying_sentinel)}, fun{other.fun}
387 friend bool operator==(basic_iterator<const_range>
const & lhs, basic_sentinel
const & rhs)
390 if (lhs == rhs.underlying_sentinel)
392 if constexpr (or_throw)
393 throw unexpected_end_of_input{
"Reached end of input before functor evaluated to true."};
398 return rhs.fun->operator()(*lhs);
402 friend bool operator==(basic_sentinel
const & lhs, basic_iterator<const_range>
const & rhs)
408 friend bool operator!=(basic_iterator<const_range>
const & lhs, basic_sentinel
const & rhs)
410 return !(lhs == rhs);
414 friend bool operator!=(basic_sentinel
const & lhs, basic_iterator<const_range>
const & rhs)
420 template <
bool other_const_range = !const_range>
421 requires (std::sentinel_for<underlying_sentinel_t, basic_iterator<other_const_range>>)
422 friend bool operator==(basic_iterator<other_const_range>
const & lhs, basic_sentinel
const & rhs)
425 if (lhs == rhs.underlying_sentinel)
427 if constexpr (or_throw)
428 throw unexpected_end_of_input{
"Reached end of input before functor evaluated to true."};
433 return rhs.fun->operator()(*lhs);
437 template <
bool other_const_range = !const_range>
438 requires (std::sentinel_for<underlying_sentinel_t, basic_iterator<other_const_range>>)
439 friend bool operator==(basic_sentinel
const & lhs, basic_iterator<other_const_range>
const & rhs)
445 template <
bool other_const_range = !const_range>
446 requires (std::sentinel_for<underlying_sentinel_t, basic_iterator<other_const_range>>)
447 friend bool operator!=(basic_iterator<other_const_range>
const & lhs, basic_sentinel
const & rhs)
449 return !(lhs == rhs);
453 template <
bool other_const_range = !const_range>
454 requires (std::sentinel_for<underlying_sentinel_t, basic_iterator<other_const_range>>)
455 friend bool operator!=(basic_sentinel
const & lhs, basic_iterator<other_const_range>
const & rhs)
469template <
bool or_throw,
bool and_consume>
473 template <
typename fun_t>
474 constexpr auto operator()(fun_t && fun)
const
476 return adaptor_from_functor{*
this, std::forward<fun_t>(fun)};
486 template <std::ranges::viewable_range urng_t,
typename fun_t>
487 constexpr auto operator()(urng_t && urange, fun_t && fun)
const
489 return view_take_until<std::views::all_t<urng_t>, fun_t, or_throw, and_consume>{
490 std::views::all(std::forward<urng_t>(urange)),
491 std::forward<fun_t>(fun)};
501namespace seqan3::detail
560inline constexpr auto take_until = take_until_fn<false, false>{};
574inline constexpr auto take_until_or_throw = take_until_fn<true, false>{};
588inline constexpr auto take_until_and_consume = take_until_fn<false, true>{};
602inline constexpr auto take_until_or_throw_and_consume = take_until_fn<true, true>{};
Provides seqan3::detail::adaptor_from_functor.
Provides various transformation traits used by the range module.
Provides the seqan3::detail::inherited_iterator_base template.
Specifies requirements of an input range type for which the const version of that type satisfies the ...
Provides exceptions used in the I/O module.
Provides various transformation traits for use on iterators.
SeqAn specific customisations in the standard namespace.
Additional non-standard concepts for ranges.