Debian Codegraph @

common/main/d_enumerate.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 #pragma once
8
9 #include <iterator>
10 #include "dxxsconf.h"
11 #include <tuple>
12 #include <type_traits>
13 #include <ranges>
14
15 namespace d_enumerate {
16
17 namespace detail {
18
19 /* In the general case, `value_type` is a tuple of the index and the
20 * result of the underlying sequence.
21 *
22 * In the special case that `result_type` is a tuple, construct a
23 * modified tuple instead of using a tuple of (index, inner tuple).
24 * This allows simpler structured bindings.
25 */
26 template <typename index_type, typename result_type>
27 struct adjust_iterator_dereference_type : std::false_type
28 {
29 using value_type = std::tuple<index_type, result_type>;
30 };
31
32 template <typename index_type, typename... T>
33 struct adjust_iterator_dereference_type<index_type, std::tuple<T...>> : std::true_type
34 {
35 using value_type = std::tuple<index_type, T...>;
36 };
37
38 /* Retrieve the member typedef `index_type` if one exists. Otherwise,
39 * use `std::size_t` as a fallback.
40 */
41 std::size_t array_index_type(...);
42
43 /* Add, then remove, a reference to the `index_type`. If `index_type`
44 * is an integer or enum type, this produces `index_type` again. If
45 * `index_type` is `void`, then this overload refers to
46 * `std::remove_reference<void &>::type`, which is ill-formed, and the
47 * overload is removed by SFINAE. This allows target types to use
48 * `using index_type = void` to decline to specify an `index_type`,
49 * which may be convenient in templates that conditionally define an
50 * `index_type` based on their template parameters.
51 */
52 template <typename T>
53 typename std::remove_reference<typename T::index_type &>::type array_index_type(T *);
54
55 }
56
57 }
58
59 template <typename sentinel_type>
60 class enumerated_sentinel
61 {
62 public:
63 sentinel_type m_sentinel;
64 };
65
66 template <typename range_index_type, std::input_or_output_iterator range_iterator_type, std::sentinel_for<range_iterator_type> sentinel_type>
67 class enumerated_iterator
68 {
69 using adjust_iterator_dereference_type = d_enumerate::detail::adjust_iterator_dereference_type<range_index_type, typename std::remove_cv<decltype(*std::declval<range_iterator_type>())>::type>;
70 range_iterator_type m_iter{};
71 range_index_type m_idx{};
72 public:
73 using index_type = range_index_type;
74 using iterator_category = typename std::iterator_traits<range_iterator_type>::iterator_category;
75 using value_type = typename adjust_iterator_dereference_type::value_type;
76 using difference_type = std::ptrdiff_t;
77 using pointer = value_type *;
78 using reference = value_type &;
79 constexpr enumerated_iterator() = default;
80 constexpr enumerated_iterator(range_iterator_type &&iter, index_type idx) :
81 m_iter{std::move(iter)}, m_idx{std::move(idx)}
82 {
83 }
84 value_type operator*() const
85 {
86 if constexpr (adjust_iterator_dereference_type::value)
87 return std::tuple_cat(std::tuple<index_type>(m_idx), *m_iter);
88 else
89 return {m_idx, *m_iter};
90 }
91 value_type operator[](const difference_type d) const
92 requires(std::random_access_iterator<range_iterator_type>)
93 {
94 return *(*this + d);
95 }
96 enumerated_iterator &operator++()
97 {
98 ++ m_iter;
99 if constexpr (std::is_enum<index_type>::value)
100 /* An enum type is not required to have operator++()
101 * defined, and may deliberately omit it to avoid allowing
102 * the value to be incremented in the typical use case. For
103 * this situation, an increment is desired, so emulate
104 * operator++() by casting to the underlying integer
105 * type, adding 1, and casting back.
106 */
107 m_idx = static_cast<index_type>(1u + static_cast<typename std::underlying_type<index_type>::type>(m_idx));
108 else
109 ++ m_idx;
110 return *this;
111 }
112 enumerated_iterator operator++(int)
113 {
114 auto result{*this};
115 ++ * this;
116 return result;
117 }
118 enumerated_iterator &operator--()
119 requires(std::bidirectional_iterator<range_iterator_type>)
120 {
121 -- m_iter;
122 if constexpr (std::is_enum<index_type>::value)
123 m_idx = static_cast<index_type>(static_cast<typename std::underlying_type<index_type>::type>(m_idx) - 1u);
124 else
125 -- m_idx;
126 return *this;
127 }
128 enumerated_iterator operator--(int)
129 requires(std::bidirectional_iterator<range_iterator_type>)
130 {
131 auto result{*this};
132 -- * this;
133 return result;
134 }
135 enumerated_iterator &operator+=(const difference_type d)
136 requires(std::random_access_iterator<range_iterator_type>)
137 {
138 std::advance(m_iter, d);
139 if constexpr (std::is_enum<index_type>::value)
140 m_idx = static_cast<index_type>(static_cast<typename std::underlying_type<index_type>::type>(m_idx) + d);
141 else
142 m_idx += d;
143 return *this;
144 }
145 enumerated_iterator &operator-=(const difference_type d)
146 requires(std::random_access_iterator<range_iterator_type>)
147 {
148 std::advance(m_iter, -d);
149 if constexpr (std::is_enum<index_type>::value)
150 m_idx = static_cast<index_type>(static_cast<typename std::underlying_type<index_type>::type>(m_idx) - d);
151 else
152 m_idx -= d;
153 return *this;
154 }
155 difference_type operator-(const enumerated_iterator &rhs) const
156 requires(std::random_access_iterator<range_iterator_type>)
157 {
158 return {m_iter - rhs.m_iter};
159 }
160 bool operator==(const enumerated_iterator &i) const = default;
161 constexpr auto operator<=>(const enumerated_iterator &i) const
162 {
163 /* Check the iterator, but skip the index. The index is tracked
164 * internally and not mutable by callers.
165 *
166 * `enumerated_iterator` instances are meant be compared only if both
167 * instances come from the same `enumerate` range. Comparing two
168 * instances derived from different ranges is undefined.
169 *
170 * If two instances come from the same `enumerate` range, then equality
171 * of their `m_iter` implies equality of their `m_idx`, and inequality
172 * of `m_iter` implies the same inequality for `m_idx`, so comparing
173 * the `m_idx` is redundant.
174 *
175 * If two instances come from different `enumerate` ranges, both
176 * `enumerate` ranges track the same underlying range, and the ranges
177 * have the same starting `m_idx`, then for this purpose, those ranges
178 * are equivalent and the preceding paragraph applies.
179 *
180 * If two instances come from different `enumerate` ranges, and the
181 * `enumerate` ranges do not track the same underlying range, then
182 * comparing their iterators is undefined, so ignoring their `m_idx` is
183 * irrelevant.
184 *
185 * If two instances come from different `enumerate` ranges, the
186 * `enumerate` ranges track the same underlying range, and the
187 * `enumerate` ranges have different starting `m_idx`, then this
188 * comparison can report the iterators as equal when they are not.
189 * Since such comparison is undefined, omit the test to save on logic
190 * that is unnecessary in all defined cases.
191 */
192 return m_iter <=> i.m_iter;
193 }
194 constexpr bool operator==(const enumerated_sentinel<sentinel_type> &i) const
195 {
196 return m_iter == i.m_sentinel;
197 }
198 };
199
200 template <typename range_index_type, std::random_access_iterator range_iterator_type, typename sentinel_type>
201 auto operator+(const enumerated_iterator<range_index_type, range_iterator_type, sentinel_type> &i, const std::ptrdiff_t d)
202 {
203 auto r{i};
204 r += d;
205 return r;
206 }
207
208 template <typename range_index_type, std::random_access_iterator range_iterator_type, typename sentinel_type>
209 auto operator+(const std::ptrdiff_t d, const enumerated_iterator<range_index_type, range_iterator_type, sentinel_type> &i)
210 {
211 auto r{i};
212 r += d;
213 return r;
214 }
215
216 template <typename range_index_type, std::random_access_iterator range_iterator_type, typename sentinel_type>
217 auto operator-(const enumerated_iterator<range_index_type, range_iterator_type, sentinel_type> &i, const std::ptrdiff_t d)
218 {
219 auto r{i};
220 r -= d;
221 return r;
222 }
223
224 template <std::input_or_output_iterator range_iterator_type, std::sentinel_for<range_iterator_type> range_sentinel_type, typename range_index_type>
225 class enumerate : std::ranges::subrange<range_iterator_type, range_sentinel_type>
226 {
227 using base_type = std::ranges::subrange<range_iterator_type, range_sentinel_type>;
228 using enumerated_iterator_type = enumerated_iterator<
229 range_index_type,
230 range_iterator_type,
231 range_sentinel_type>;
232 [[no_unique_address]]
233 range_index_type m_idx;
234 public:
235 using base_type::size;
236 using index_type = range_index_type;
237 template <std::ranges::borrowed_range range_type>
238 /* Require that range_type satisfy borrowed_range since the storage
239 * owned by the range must exist until the `enumerate` object is
240 * fully consumed. If `range_type` does not satisfy borrowed range,
241 * then its storage may cease to exist after this constructor returns.
242 */
243 enumerate(range_type &&t, index_type i = index_type{}) :
244 base_type{std::forward<range_type>(t)}, m_idx{std::move(i)}
245 {
246 }
247 enumerated_iterator_type begin() const
248 {
249 return {this->base_type::begin(), m_idx};
250 }
251 enumerated_sentinel<range_sentinel_type> end() const
252 {
253 return {this->base_type::end()};
254 }
255 };
256
257 template <std::input_or_output_iterator range_iterator_type, std::sentinel_for<range_iterator_type> range_sentinel_type, typename index_type>
258 inline constexpr bool std::ranges::enable_borrowed_range<enumerate<range_iterator_type, range_sentinel_type, index_type>> = true;
259
260 template <std::ranges::borrowed_range range_type, typename index_type = decltype(d_enumerate::detail::array_index_type(static_cast<typename std::remove_reference<range_type>::type *>(nullptr)))>
261 enumerate(range_type &&r, index_type start = {/* value ignored for deduction guide */}) -> enumerate</* range_iterator_type = */ decltype(std::ranges::begin(std::declval<range_type &>())), /* range_sentinel_type = */ decltype(std::ranges::end(std::declval<range_type &>())), index_type>;