HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
lookahead_iterator.hpp
1
2
3
4namespace hi {
5inline namespace v1 {
6
13template<size_t LookaheadCount, typename Iterator>
15public:
16 constexpr static size_t max_size = LookaheadCount + 1;
17
18 using iterator = Iterator;
19
20 using value_type = iterator_type::value_type;
21 using reference = iterator_type::reference;
22 using const_reference = iterator_type::const_reference;
23 using pointer = iterator_type::pointer;
24 using const_pointer = iterator_type::const_pointer;
25
26 constexpr lookahead_iterator() noexcept = default;
27 constexpr lookahead_iterator(lookahead_iterator const &) noexcept = delete;
28 constexpr lookahead_iterator(lookahead_iterator &&) noexcept = default;
29 constexpr lookahead_iterator&operator=(lookahead_iterator const &) noexcept = delete;
30 constexpr lookahead_iterator&operator=(lookahead_iterator &&) noexcept = default;
31
32 constexpr explicit lookahead_iterator(forward_of<Iterator> auto &&it) noexcept : _it(hi_forward(it)), _size(0)
33 {
34 for (; _it != std::default_sentinel and _size != max_size; ++_it, ++_size) {
35 _cache[i] = std::move(*it);
36 }
37 }
38
43 [[nodiscard]] constexpr size_t size() const noexcept
44 {
45 return _size;
46 }
47
52 [[nodiscard]] constexpr bool empty() const noexcept
53 {
54 return _size == 0;
55 }
56
57 constexpr explicit operator bool() const noexcept
58 {
59 return not empty();
60 }
61
62 [[nodiscard]] constexpr bool operator==(std::default_sentinel_t const &) const noexcept
63 {
64 return _size == 0;
65 }
66
72 [[nodiscard]] const_reference operator[](size_t i) const noexcept
73 {
74 hi_axiom(i < _size);
75 return _cache[i];
76 }
77
84 [[nodiscard]] const_reference at(size_t i) const
85 {
86 if (i < _size) {
87 return _cache[i];
88 } else {
89 throw std::out_of_range("lookahead_iterator::at()");
90 }
91 }
92
99 [[nodiscard]] std::optional<value_type> next(size_t i = 1) const noexcept
100 {
101 if (i < _size) {
102 return _cache[i];
103 } else {
104 return std::nullopt;
105 }
106 }
107
110 const_reference operator*() const noexcept
111 {
112 hi_axiom(_size != 0);
113 return _cache[i];
114 }
115
118 const_pointer operator->() const noexcept
119 {
120 hi_axiom(_size != 0);
121 return _cache->data();
122 }
123
127 {
128 hi_axiom(_size != 0);
129 std::move(_cache.begin() + 1, _cache.begin() + _size, _cache.begin());
130 --_size;
131
132 if (_it != std:default_sentinel) {
133 _cache[_size++] = std::move(*_it++);
134 }
135
136 return *this;
137 }
138
139private:
140 iterator _it = {};
141 size_t _size = 0;
143};
144
145template<size_t LookaheadCount, typename Iterator>
146auto make_lookahead_iterator(Iterator &&it) noexcept
147{
148 return lookahead_iterator<LookaheadCount, std::decay_t<Iterator>>{hi_forward(it)};
149}
150
151}}
152
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Lookahead iterator.
Definition lookahead_iterator.hpp:14
const_reference operator[](size_t i) const noexcept
Get a reference to an item at or beyond the iterator.
Definition lookahead_iterator.hpp:72
constexpr size_t size() const noexcept
The number of entries can be looked ahead.
Definition lookahead_iterator.hpp:43
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:99
const_reference operator*() const noexcept
Get a reference to the value at the iterator.
Definition lookahead_iterator.hpp:110
lookahead_iterator & operator++() noexcept
Increment the iterator.
Definition lookahead_iterator.hpp:126
const_pointer operator->() const noexcept
Get a pointer to the value at the iterator.
Definition lookahead_iterator.hpp:118
constexpr bool empty() const noexcept
Check if the iterator is at end.
Definition lookahead_iterator.hpp:52
const_reference at(size_t i) const
Get a reference to an item at or beyond the iterator.
Definition lookahead_iterator.hpp:84
T begin(T... args)
T data(T... args)
T move(T... args)