|
| 1 | +// Copyright David Stone 2021. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +module; |
| 7 | + |
| 8 | +#include <operators/arrow.hpp> |
| 9 | + |
| 10 | +export module containers.algorithms.generate_until; |
| 11 | + |
| 12 | +export import containers.common_iterator_functions; |
| 13 | +import containers.stored_function; |
| 14 | + |
| 15 | +import bounded; |
| 16 | +import numeric_traits; |
| 17 | +import std_module; |
| 18 | + |
| 19 | +using namespace bounded::literal; |
| 20 | + |
| 21 | +namespace containers { |
| 22 | + |
| 23 | +template<typename Function> |
| 24 | +struct generate_until_sentinel { |
| 25 | + explicit constexpr generate_until_sentinel(Function & is_done_): |
| 26 | + m_is_done(is_done_) |
| 27 | + { |
| 28 | + } |
| 29 | + constexpr auto is_done() const -> bool { |
| 30 | + return std::invoke(m_is_done); |
| 31 | + } |
| 32 | +private: |
| 33 | + [[no_unique_address]] stored_function<Function> m_is_done; |
| 34 | +}; |
| 35 | + |
| 36 | +template<typename Function> |
| 37 | +struct generate_until_iterator { |
| 38 | + using iterator_category = std::input_iterator_tag; |
| 39 | + // TODO: ??? |
| 40 | + using difference_type = decltype(bounded::integer(std::int64_t())); |
| 41 | + |
| 42 | + explicit constexpr generate_until_iterator(Function & generator): |
| 43 | + m_generator(generator) |
| 44 | + { |
| 45 | + } |
| 46 | + generate_until_iterator(generate_until_iterator &&) = default; |
| 47 | + generate_until_iterator(generate_until_iterator const &) = delete; |
| 48 | + auto operator=(generate_until_iterator &&) & -> generate_until_iterator & = default; |
| 49 | + auto operator=(generate_until_iterator const &) & -> generate_until_iterator & = delete; |
| 50 | + |
| 51 | + constexpr auto operator*() const -> decltype(auto) { |
| 52 | + return std::invoke(m_generator); |
| 53 | + } |
| 54 | + OPERATORS_ARROW_DEFINITIONS |
| 55 | + |
| 56 | + template<typename UntilFunction> |
| 57 | + friend constexpr auto operator==(generate_until_iterator const &, generate_until_sentinel<UntilFunction> const rhs) -> bool { |
| 58 | + return rhs.is_done(); |
| 59 | + } |
| 60 | + |
| 61 | + friend constexpr auto operator+(generate_until_iterator it, bounded::constant_t<1>) -> generate_until_iterator { |
| 62 | + return it; |
| 63 | + } |
| 64 | +private: |
| 65 | + [[no_unique_address]] stored_function<Function> m_generator; |
| 66 | +}; |
| 67 | + |
| 68 | +export template<std::invocable Function, bounded::invocable_r<bool> UntilFunction> |
| 69 | +struct generate_until { |
| 70 | + constexpr generate_until(Function generator, UntilFunction until): |
| 71 | + m_generator(std::move(generator)), |
| 72 | + m_until(std::move(until)) |
| 73 | + { |
| 74 | + } |
| 75 | + |
| 76 | + constexpr auto begin() const requires std::invocable<Function const &> { |
| 77 | + return generate_until_iterator(m_generator); |
| 78 | + } |
| 79 | + constexpr auto begin() { |
| 80 | + return generate_until_iterator(m_generator); |
| 81 | + } |
| 82 | + constexpr auto end() const requires std::invocable<UntilFunction const &> { |
| 83 | + return generate_until_sentinel(m_until); |
| 84 | + } |
| 85 | + constexpr auto end() { |
| 86 | + return generate_until_sentinel(m_until); |
| 87 | + } |
| 88 | +private: |
| 89 | + [[no_unique_address]] Function m_generator; |
| 90 | + [[no_unique_address]] UntilFunction m_until; |
| 91 | +}; |
| 92 | + |
| 93 | +} // namespace containers |
| 94 | + |
| 95 | +constexpr auto empty_generator = containers::generate_until( |
| 96 | + [] -> int { std::unreachable(); }, |
| 97 | + [] { return true; } |
| 98 | +); |
| 99 | +static_assert(empty_generator.begin() == empty_generator.end()); |
| 100 | + |
| 101 | +static_assert([] { |
| 102 | + for ([[maybe_unused]] auto const _ : empty_generator) { |
| 103 | + } |
| 104 | + return true; |
| 105 | +}()); |
| 106 | + |
| 107 | +static_assert([] { |
| 108 | + int x = 2; |
| 109 | + auto const g = containers::generate_until( |
| 110 | + [&] { return x *= 2; }, |
| 111 | + [&] { return x > 4; } |
| 112 | + ); |
| 113 | + for ([[maybe_unused]] auto const _ : g) { |
| 114 | + } |
| 115 | + return x == 8; |
| 116 | +}()); |
| 117 | + |
0 commit comments