FireSTARR
Loading...
Searching...
No Matches
RangeIterator.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_RANGEITERATOR_H
3#define FS_RANGEITERATOR_H
4#include "stdafx.h"
5#include "Log.h"
6// HACK: don't use until logging causing issues with constexpr is fixed
7#undef DEBUG_ITERATOR
8namespace fs
9{
10template <class T = MathSize>
12{
13 friend class iterable;
14
15public:
16 using iterator_category = std::bidirectional_iterator_tag;
17 using difference_type = int;
18 using value_type = T;
20 const value_type start,
21 const value_type end,
22 const value_type increment,
23 const bool inclusive = true
24 )
25 : start_(start), end_(end), increment_(increment), inclusive_(inclusive)
26 {
27#ifdef DEBUG_ITERATOR
28 logging::verbose(
29 "Range is from {:f} to {:f} with step {:f} {:s}",
30 start_,
31 end_,
32 increment_,
33 inclusive_ ? "inclusive" : "exclusive"
34 );
35#endif
36 }
37 constexpr RangeIterator() = default;
38 constexpr RangeIterator(const RangeIterator& rhs) = default;
39 constexpr RangeIterator(RangeIterator&& rhs) = default;
40 RangeIterator& operator=(const RangeIterator& rhs) = default;
41 RangeIterator& operator=(RangeIterator&& rhs) = default;
42
43public:
44 constexpr inline value_type operator*() const { return start_ + step_ * increment_; }
45#ifndef DEBUG_ITERATOR
46 constexpr
47#endif
48 inline RangeIterator&
49 operator++()
50 {
51#ifdef DEBUG_ITERATOR
52 logging::check_fatal(*(*this) < start_, [&]() {
53 return std::format(
54 "operator++() {:g} less than start value {:g} ({:+g}) for step {:d}",
55 *(*this),
56 start_,
57 (*(*this) - start_),
58 step_
59 );
60 });
61#endif
62 step_++;
63#ifdef DEBUG_ITERATOR
64 logging::check_fatal(*this > end(), [&]() {
65 return std::format(
66 "operator++() {:g} more than end value {:g} ({:+g}) for step {:d}",
67 *(*this),
68 *end(),
69 (*(*this) - *end()),
70 step_
71 );
72 });
73#endif
74 return *this;
75 }
76#ifndef DEBUG_ITERATOR
77 constexpr
78#endif
79 inline RangeIterator
80 operator++(int)
81 {
82#ifdef DEBUG_ITERATOR
83 logging::check_fatal(*(*this) < start_, [&]() {
84 return std::format(
85 "operator++(int) {:g} less than start value {:g} ({:+g}) for step {:d}",
86 *(*this),
87 start_,
88 step_
89 );
90 });
91#endif
92 RangeIterator oTmp = *(*this);
93 *(*this) += increment_;
94#ifdef DEBUG_ITERATOR
95 logging::check_fatal(*this > end(), [&]() {
96 return std::format(
97 "operator++(int) {:g} more than end value {:g} ({:+g}) for step {:d}",
98 *(*this),
99 *end(),
100 step_
101 );
102 });
103#endif
104 return oTmp;
105 }
106#ifndef DEBUG_ITERATOR
107 constexpr
108#endif
109 inline RangeIterator&
110 operator--()
111 {
112#ifdef DEBUG_ITERATOR
113 logging::check_fatal(*this > end(), [&]() {
114 return std::format(
115 "operator--() {:g} more than end value {:g} ({:+g}) for step {:d}", *(*this), *end(), step_
116 );
117 });
118#endif
119 *(*this) -= increment_;
120#ifdef DEBUG_ITERATOR
121 logging::check_fatal(*(*this) < start_, [&]() {
122 return std::format(
123 "operator--() {:g} less than start value {:g} ({:+g}) for step {:d}",
124 *(*this),
125 start_,
126 step_
127 );
128 });
129#endif
130 return *this;
131 }
132#ifndef DEBUG_ITERATOR
133 constexpr
134#endif
135 inline RangeIterator
136 operator--(int)
137 {
138 RangeIterator oTmp = *(*this);
139#ifdef DEBUG_ITERATOR
140 logging::check_fatal(*this > end(), [&]() {
141 return std::format(
142 "operator--(int) {:g} more than end value {:g} ({:+g}) for step {:d}",
143 *(*this),
144 *end(),
145 step_
146 );
147 });
148#endif
149 *(*this) -= increment_;
150#ifdef DEBUG_ITERATOR
151 logging::check_fatal(*(*this) < start_, [&]() {
152 return std::format(
153 "operator--(int) {:g} less than start value {:g} ({:+g}) for step {:d}",
154 *(*this),
155 start_,
156 step_
157 );
158 });
159#endif
160 return oTmp;
161 }
162 constexpr inline difference_type operator-(const RangeIterator& rhs) const
163 {
164 return static_cast<difference_type>(step_ - rhs.step_);
165 }
166 auto operator<=>(const RangeIterator<value_type>& rhs) const = default;
167 // inline bool operator==(const RangeIterator& rhs) const { return *(*this) == rhs; }
168 // inline bool operator!=(const RangeIterator& rhs) const { return !(*(*this) == rhs); }
169 // inline bool operator>(const RangeIterator& rhs) const { return *(*this) < *rhs; }
170 // inline bool operator<(const RangeIterator& rhs) const { return *(*this) > *rhs; }
171 // inline bool operator>=(const RangeIterator& rhs) const { return *(*this) <= *rhs; }
172 // inline bool operator<=(const RangeIterator& rhs) const { return *(*this) >= *rhs; }
173 constexpr auto begin() const { return RangeIterator<value_type>(this, start_); }
174 constexpr auto end() const
175 {
176 // if inclusive then end is slightly past end value so end is included
178 this, end_ + (inclusive_ ? increment_ : static_cast<value_type>(0))
179 );
180 }
181 difference_type size() const { return end() - begin(); }
182
183private:
184 constexpr RangeIterator(const RangeIterator* rhs, const T value)
185 : start_(rhs->start_), end_(rhs->end_), increment_(rhs->increment_),
186 step_(static_cast<difference_type>((value - start_) / increment_)),
187 inclusive_(rhs->inclusive_)
188 { }
189 value_type start_{};
190 value_type end_{};
191 value_type increment_{};
192 size_t step_{0};
193 bool inclusive_{};
194};
195static_assert(
196 std::bidirectional_iterator<RangeIterator<MathSize>>,
197 "iterator must be an iterator!"
198);
199static_assert(std::bidirectional_iterator<RangeIterator<int>>, "iterator must be an iterator!");
200inline auto range(
201 const MathSize start,
202 const MathSize end,
203 const MathSize step,
204 const bool inclusive = true
205)
206{
207 std::ignore = inclusive;
208 auto r = RangeIterator(start, end, step);
209 return vector<MathSize>{r.begin(), r.end()};
210}
211inline auto range_int(const int start, const int end, const int step, const bool inclusive = true)
212{
213 std::ignore = inclusive;
214 auto r = RangeIterator<int>(start, end, step);
215 return vector<int>{r.begin(), r.end()};
216}
217}
218namespace fs::testing
219{
220template <class T>
221auto check_range(
222 const char* name_fct,
223 const char* name_param,
224 const auto fct_a,
225 const auto fct_b,
226 const MathSize epsilon,
227 const T& it
228)
229{
230 logging::debug("Checking {:s}", name_fct);
231 for (auto v : it)
232 {
233 const auto msg = std::format("{} ({} = {})", name_fct, name_param, v);
234 logging::check_tolerance(epsilon, fct_a(v), fct_b(v), msg.c_str());
235 logging::verbose("{:s}", msg);
236 }
237}
238}
239#endif
Definition RangeIterator.h:12