10#include "../utility/utility.hpp"
11#include "../macros.hpp"
19hi_export_module(hikogui.parser.lookahead_iterator);
21hi_export
namespace hi {
30template<
size_t LookaheadCount,
typename It, std::sentinel_for<It> ItEnd = std::default_sentinel_t>
33 static_assert(std::has_single_bit(LookaheadCount),
"LookaheadCount must be a power of two.");
35 constexpr static size_t max_size = LookaheadCount;
38 using reference = value_type
const &;
39 using pointer = value_type
const *;
45 using value_type = value_type;
46 using reference = reference;
47 using pointer = pointer;
49 constexpr proxy(
proxy const &)
noexcept =
default;
51 constexpr proxy &operator=(
proxy const &)
noexcept =
default;
52 constexpr proxy &operator=(
proxy &&)
noexcept =
default;
55 constexpr reference operator*()
const noexcept
60 constexpr pointer operator->()
const noexcept
75 constexpr explicit
lookahead_iterator(It first, ItEnd last) noexcept : _it(first), _last(last), _size(0)
77 while (_size != max_size and _it != last) {
78 add_one_to_lookahead();
86 [[nodiscard]]
constexpr size_t size() const noexcept
95 [[nodiscard]]
constexpr bool empty() const noexcept
100 constexpr explicit operator bool() const noexcept
105 [[nodiscard]]
constexpr bool operator==(std::default_sentinel_t)
const noexcept
115 [[nodiscard]]
constexpr reference
operator[](
size_t i)
const noexcept
118 return _lookahead[(_tail + i) % max_size];
127 [[nodiscard]]
constexpr reference
at(
size_t i)
const
142 [[nodiscard]]
constexpr std::optional<value_type>
next(
size_t i = 1) const noexcept
155 hi_axiom(_size != 0);
163 hi_axiom(_size != 0);
171 hi_axiom(_size != 0);
174 add_one_to_lookahead();
181 for (
auto i = 0_uz; i != n; ++i) {
189 auto r = proxy{**
this};
202 constexpr void add_one_to_lookahead() noexcept
204 hi_axiom(_size < max_size);
207 _lookahead[(_tail + _size) % max_size] = *_it;
214static_assert(std::movable<lookahead_iterator<2, std::string::iterator, std::string::iterator>>);
215static_assert(std::is_same_v<std::iterator_traits<lookahead_iterator<2, std::string::iterator, std::string::iterator>>::value_type,
char>);
216static_assert(std::input_or_output_iterator<lookahead_iterator<2, std::string::iterator, std::string::iterator>>);
217static_assert(std::weakly_incrementable<lookahead_iterator<2, std::string::iterator, std::string::iterator>>);
226template<
size_t LookaheadCount,
typename It, std::sentinel_for<It> ItEnd = std::default_sentinel_t>
239template<
size_t LookaheadCount, std::ranges::range Range>
242 return make_lookahead_iterator<LookaheadCount>(std::ranges::begin(range), std::ranges::end(range));
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
auto make_lookahead_iterator(It first, ItEnd last=std::default_sentinel) noexcept
Create a lookahead_iterator from a forward iterator.
Definition lookahead_iterator.hpp:227
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Lookahead iterator.
Definition lookahead_iterator.hpp:31
constexpr lookahead_iterator & operator++() noexcept
Increment the iterator.
Definition lookahead_iterator.hpp:169
constexpr size_t size() const noexcept
The number of entries can be looked ahead.
Definition lookahead_iterator.hpp:86
constexpr pointer operator->() const noexcept
Get a pointer to the value at the iterator.
Definition lookahead_iterator.hpp:161
constexpr reference at(size_t i) const
Get a reference to an item at or beyond the iterator.
Definition lookahead_iterator.hpp:127
constexpr std::optional< value_type > next(size_t i=1) const noexcept
Get a reference to an item at or beyond the iterator.
Definition lookahead_iterator.hpp:142
constexpr reference operator[](size_t i) const noexcept
Get a reference to an item at or beyond the iterator.
Definition lookahead_iterator.hpp:115
constexpr reference operator*() const noexcept
Get a reference to the value at the iterator.
Definition lookahead_iterator.hpp:153
constexpr bool empty() const noexcept
Check if the iterator is at end.
Definition lookahead_iterator.hpp:95
Definition lookahead_iterator.hpp:43