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