| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <http://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 <type_traits> |
| 10 |
#include <iterator> |
| 11 |
#include <ranges> |
| 12 |
#include <utility> |
| 13 |
|
| 14 |
/* These could be empty tag types, but defining them from |
| 15 |
* std::integral_constant<bool, V> allows the deduction guide for |
| 16 |
* std::integral_constant<$integer_type, V> to handle them. If these |
| 17 |
* were done as tag types, a separate deduction guide would be required. |
| 18 |
*/ |
| 19 |
using xrange_ascending = std::true_type; |
| 20 |
using xrange_descending = std::false_type; |
| 21 |
|
| 22 |
namespace detail { |
| 23 |
|
| 24 |
template <typename B, typename E, typename step_type> |
| 25 |
struct xrange_check_constant_endpoints : std::true_type |
| 26 |
{ |
| 27 |
/* By default, endpoints are not constant and are not checked. */ |
| 28 |
}; |
| 29 |
|
| 30 |
template <typename Tb, Tb b, typename Te, Te e> |
| 31 |
struct xrange_check_constant_endpoints<std::integral_constant<Tb, b>, std::integral_constant<Te, e>, xrange_ascending> : std::true_type |
| 32 |
{ |
| 33 |
/* If both endpoints are constant, a static_assert can validate that |
| 34 |
* the range is not empty. |
| 35 |
*/ |
| 36 |
static_assert(b < e, "range is always empty due to value of `b` versus value of `e`"); |
| 37 |
}; |
| 38 |
|
| 39 |
template <typename Tb, Tb b, typename Te, Te e> |
| 40 |
struct xrange_check_constant_endpoints<std::integral_constant<Tb, b>, std::integral_constant<Te, e>, xrange_descending> : std::true_type |
| 41 |
{ |
| 42 |
static_assert(e < b, "range is always empty due to value of `b` versus value of `e`"); |
| 43 |
}; |
| 44 |
|
| 45 |
template <typename Tb, Tb b, typename Te, Te e, typename Tstep, Tstep step> |
| 46 |
struct xrange_check_constant_endpoints<std::integral_constant<Tb, b>, std::integral_constant<Te, e>, std::integral_constant<Tstep, step>> : |
| 47 |
xrange_check_constant_endpoints< |
| 48 |
std::integral_constant<Tb, b>, |
| 49 |
std::integral_constant<Te, e>, |
| 50 |
typename std::conditional<(step > 0), xrange_ascending, xrange_descending>::type |
| 51 |
> |
| 52 |
{ |
| 53 |
/* The implementation of xrange_iterator::operator++ moves the |
| 54 |
* current position by exactly the step value, every time. If |
| 55 |
* std::abs(step) is greater than 1, then some values will be |
| 56 |
* stepped over. Assert that skipping values will not cause |
| 57 |
* operator++ to skip over the end value. If it did, the range |
| 58 |
* would continue until wraparound terminated it, the program |
| 59 |
* abandoned reading the range, or the program crashed due to out of |
| 60 |
* range values. |
| 61 |
*/ |
| 62 |
static_assert((step > 0 ? ((e - b) % step) : ((b - e) % -step)) == 0, "step size will overstep end value"); |
| 63 |
}; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/* For the general case, store a copy of the value, |
| 68 |
* and provide an implicit conversion. |
| 69 |
*/ |
| 70 |
template <typename T, bool begin> |
| 71 |
class xrange_endpoint |
| 72 |
{ |
| 73 |
public: |
| 74 |
using value_type = T; |
| 75 |
/* The value is never mutated, but must be mutable to satisfy |
| 76 |
* std::ranges::range<xrange<...>>. |
| 77 |
*/ |
| 78 |
value_type value{}; |
| 79 |
constexpr operator value_type() const |
| 80 |
{ |
| 81 |
return value; |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
/* For the special case that the value is a std::integral_constant, |
| 86 |
* inherit from it so that the Empty Base Optimization can apply. |
| 87 |
*/ |
| 88 |
template <typename T, T V, bool begin> |
| 89 |
class xrange_endpoint<std::integral_constant<T, V>, begin> : public std::integral_constant<T, V> |
| 90 |
{ |
| 91 |
public: |
| 92 |
constexpr xrange_endpoint() = default; |
| 93 |
constexpr xrange_endpoint(const std::integral_constant<T, V> &) |
| 94 |
{ |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
template <typename index_type, typename step_type> |
| 99 |
class xrange_iterator |
| 100 |
{ |
| 101 |
index_type m_idx{}; |
| 102 |
public: |
| 103 |
using difference_type = std::ptrdiff_t; |
| 104 |
using iterator_category = std::forward_iterator_tag; |
| 105 |
using value_type = index_type; |
| 106 |
using pointer = value_type *; |
| 107 |
using reference = value_type &; |
| 108 |
constexpr xrange_iterator() = default; // default constructible required by std::semiregular |
| 109 |
constexpr xrange_iterator(const index_type i) : |
| 110 |
m_idx(i) |
| 111 |
{ |
| 112 |
} |
| 113 |
[[nodiscard]] |
| 114 |
difference_type operator-(const xrange_iterator &i) const |
| 115 |
requires( |
| 116 |
requires(index_type i) { |
| 117 |
i - i; |
| 118 |
} |
| 119 |
) |
| 120 |
{ |
| 121 |
return m_idx - i.m_idx; |
| 122 |
} |
| 123 |
[[nodiscard]] |
| 124 |
index_type operator*() const |
| 125 |
{ |
| 126 |
return m_idx; |
| 127 |
} |
| 128 |
xrange_iterator &operator++() |
| 129 |
requires( |
| 130 |
std::same_as<step_type, xrange_ascending> && |
| 131 |
requires(index_type i) { |
| 132 |
++ i; |
| 133 |
} |
| 134 |
) |
| 135 |
{ |
| 136 |
++ m_idx; |
| 137 |
return *this; |
| 138 |
} |
| 139 |
xrange_iterator &operator++() |
| 140 |
requires( |
| 141 |
std::same_as<step_type, xrange_descending> && |
| 142 |
requires(index_type i) { |
| 143 |
-- i; |
| 144 |
} |
| 145 |
) |
| 146 |
{ |
| 147 |
-- m_idx; |
| 148 |
return *this; |
| 149 |
} |
| 150 |
xrange_iterator &operator++() |
| 151 |
requires( |
| 152 |
!std::same_as<step_type, xrange_ascending> && |
| 153 |
!std::same_as<step_type, xrange_descending> && |
| 154 |
requires(index_type i) { |
| 155 |
i += step_type::value; |
| 156 |
} |
| 157 |
) |
| 158 |
{ |
| 159 |
m_idx += step_type::value; |
| 160 |
return *this; |
| 161 |
} |
| 162 |
[[nodiscard]] |
| 163 |
xrange_iterator operator++(int) |
| 164 |
requires( |
| 165 |
requires(xrange_iterator i) { |
| 166 |
++ i; |
| 167 |
} |
| 168 |
) |
| 169 |
{ |
| 170 |
auto r = *this; |
| 171 |
++ *this; |
| 172 |
return r; |
| 173 |
} |
| 174 |
[[nodiscard]] |
| 175 |
constexpr bool operator==(const xrange_iterator &i) const = default; |
| 176 |
template <typename end_index_type> |
| 177 |
[[nodiscard]] |
| 178 |
constexpr bool operator==(const xrange_endpoint<end_index_type, false> &i) const |
| 179 |
{ |
| 180 |
return m_idx == i.value; |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
/* This provides an approximation of the functionality of the Python2 |
| 185 |
* xrange object. Python3 renamed it to `range`. The older name is |
| 186 |
* kept because it is easier to find with grep. |
| 187 |
*/ |
| 188 |
template <typename index_type, typename B = index_type, typename E = index_type, typename step_type = xrange_ascending> |
| 189 |
class xrange : |
| 190 |
protected xrange_endpoint<B, true>, |
| 191 |
protected xrange_endpoint<E, false> |
| 192 |
{ |
| 193 |
protected: |
| 194 |
using begin_type = xrange_endpoint<B, true>; |
| 195 |
using end_type = xrange_endpoint<E, false>; |
| 196 |
using iterator = xrange_iterator<index_type, step_type>; |
| 197 |
/* This static_assert has no message, since the value is always |
| 198 |
* true. Use of static_assert forces instantiation of the type, |
| 199 |
* which has a static_assert that checks the values and displays a |
| 200 |
* message on failure. |
| 201 |
*/ |
| 202 |
static_assert(detail::xrange_check_constant_endpoints<B, E, step_type>::value); |
| 203 |
static_assert(!std::is_reference<E>::value, "xrange<E> must be a value, not a reference"); |
| 204 |
static constexpr B init_begin(B b, E e) |
| 205 |
{ |
| 206 |
if constexpr (std::is_convertible<E, B>::value) |
| 207 |
{ |
| 208 |
if constexpr (std::is_same<step_type, xrange_ascending>::value || (!std::is_same<step_type, xrange_descending>::value && step_type::value > 0)) |
| 209 |
{ |
| 210 |
#ifdef DXX_CONSTANT_TRUE |
| 211 |
(DXX_CONSTANT_TRUE(!(b < e)) && (DXX_ALWAYS_ERROR_FUNCTION("begin never less than end"), 0)); |
| 212 |
#endif |
| 213 |
if (!(b < e)) |
| 214 |
return e; |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
#ifdef DXX_CONSTANT_TRUE |
| 219 |
(DXX_CONSTANT_TRUE(!(e < b)) && (DXX_ALWAYS_ERROR_FUNCTION("end never less than begin"), 0)); |
| 220 |
#endif |
| 221 |
if (!(e < b)) |
| 222 |
return e; |
| 223 |
} |
| 224 |
} |
| 225 |
else |
| 226 |
(void)e; |
| 227 |
return b; |
| 228 |
} |
| 229 |
private: |
| 230 |
/* gcc-15 rejects `using end_type::operator typename end_type::value_type;` |
| 231 |
* Use `using end_type::operator end_value_type` with a local type as an |
| 232 |
* indirection in, rather than looking up the type from the base. |
| 233 |
* |
| 234 |
* - compiles in gcc-12, gcc-13, gcc-14 |
| 235 |
* - compiles in clang-15, clang-16, clang-17 |
| 236 |
* - fails to compile in gcc-15 |
| 237 |
*/ |
| 238 |
using end_value_type = typename end_type::value_type; |
| 239 |
public: |
| 240 |
using end_type::value; |
| 241 |
using end_type::operator end_value_type; |
| 242 |
constexpr xrange() = default; |
| 243 |
constexpr xrange(B b, E e) : |
| 244 |
begin_type{init_begin(std::move(b), e)}, end_type{std::move(e)} |
| 245 |
{ |
| 246 |
} |
| 247 |
/* Allow, but ignore, a third argument with the step size. The |
| 248 |
* argument must be here for class template argument deduction to |
| 249 |
* work. |
| 250 |
*/ |
| 251 |
constexpr xrange(B b, E e, step_type) : |
| 252 |
xrange{std::move(b), std::move(e)} |
| 253 |
{ |
| 254 |
} |
| 255 |
constexpr xrange(E e) : |
| 256 |
begin_type{}, end_type{std::move(e)} |
| 257 |
{ |
| 258 |
static_assert( |
| 259 |
/* For the general case, delegate to `std::is_unsigned_v`. |
| 260 |
* xrange requires that the type not be a reference, so there is no |
| 261 |
* need to use `std::remove_reference<E>` here. |
| 262 |
*/ |
| 263 |
requires { requires std::is_unsigned_v<E>; } or |
| 264 |
( |
| 265 |
/* For the special case that the value is a |
| 266 |
* `std::integral_constant`, examine the underlying integer |
| 267 |
* type. |
| 268 |
* `std::is_unsigned_v<std::integral_constant<unsigned, N>>` is |
| 269 |
* false, but this requires expression should yield true in |
| 270 |
* that case. |
| 271 |
*/ |
| 272 |
requires { E::value; typename E::value_type; } and |
| 273 |
requires { requires std::is_same_v<std::integral_constant<typename E::value_type, E::value>, E>; } and |
| 274 |
requires { requires std::is_unsigned_v<typename E::value_type>; } |
| 275 |
), "xrange(E) requires unsigned E; use xrange(B, E) if E must be signed"); |
| 276 |
} |
| 277 |
iterator begin() const |
| 278 |
{ |
| 279 |
return iterator(static_cast<const begin_type &>(*this)); |
| 280 |
} |
| 281 |
end_type end() const |
| 282 |
{ |
| 283 |
return *this; |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
template <typename index_type, typename B, typename E, typename step_type> |
| 288 |
inline constexpr bool std::ranges::enable_borrowed_range<xrange<index_type, B, E, step_type>> = true; |
| 289 |
|
| 290 |
/* Disallow building an `xrange` with a reference to mutable `e` as the |
| 291 |
* end term. When `e` is mutable, the loop might change `e` during |
| 292 |
* iteration, so using `xrange` could be wrong. If the loop does not |
| 293 |
* change `e`, store it in a const qualified variable, which will select |
| 294 |
* the next overload down instead. |
| 295 |
*/ |
| 296 |
template <typename Tb, typename Te> |
| 297 |
requires(!std::is_const<Te>::value) |
| 298 |
xrange(Tb &&b, Te &e) -> xrange<Tb, Tb, Te &>; // provokes a static_assert failure in the constructor |
| 299 |
|
| 300 |
template <typename Tb, typename Te> |
| 301 |
xrange(Tb &&b, Te &&e) -> xrange< |
| 302 |
typename std::common_type<Tb, Te>::type, |
| 303 |
typename std::remove_const<typename std::remove_reference<Tb>::type>::type, |
| 304 |
typename std::remove_const<typename std::remove_reference<Te>::type>::type |
| 305 |
>; |
| 306 |
|
| 307 |
template <typename Tb, typename Te, typename Tstep, Tstep step> |
| 308 |
xrange(Tb &&b, Te &&e, std::integral_constant<Tstep, step>) -> xrange< |
| 309 |
typename std::common_type<Tb, Te>::type, |
| 310 |
typename std::remove_const<typename std::remove_reference<Tb>::type>::type, |
| 311 |
typename std::remove_const<typename std::remove_reference<Te>::type>::type, |
| 312 |
std::integral_constant<Tstep, step> |
| 313 |
>; |
| 314 |
|
| 315 |
template <typename Te> |
| 316 |
xrange(const Te &) -> xrange<Te, std::integral_constant<typename std::common_type<Te, unsigned>::type, 0u>>; |
| 317 |
|
| 318 |
template <typename Te> |
| 319 |
xrange(Te &) -> xrange< |
| 320 |
Te, |
| 321 |
std::integral_constant<typename std::common_type<typename std::remove_const<Te>::type, unsigned>::type, 0u>, |
| 322 |
Te & |
| 323 |
>; |
| 324 |
|
| 325 |
template <typename Te, Te e> |
| 326 |
xrange(std::integral_constant<Te, e>) -> xrange<Te, std::integral_constant<Te, Te(0)>, std::integral_constant<Te, e>>; |