Use an iterator template for enum sequence loops (#1228)

This commit is contained in:
Rangi
2023-11-12 03:19:19 -05:00
committed by GitHub
parent d390db5c57
commit e824e34526
9 changed files with 60 additions and 17 deletions

View File

@@ -110,7 +110,8 @@ static constexpr auto flipTable(std::integer_sequence<T, i...>) {
return byte;
}(i)...};
}
}
} // namespace detail
// Flipping tends to happen fairly often, so take a bite out of dcache to speed it up
static constexpr auto flipTable = detail::flipTable(std::make_integer_sequence<uint16_t, 256>());

View File

@@ -21,6 +21,6 @@ namespace packing {
std::tuple<DefaultInitVec<size_t>, size_t>
overloadAndRemove(std::vector<ProtoPalette> const &protoPalettes);
}
} // namespace packing
#endif // RGBDS_GFX_PAL_PACKING_HPP

View File

@@ -21,6 +21,6 @@ void grayscale(std::vector<Palette> &palettes,
std::array<std::optional<Rgba>, 0x8001> const &colors);
void rgb(std::vector<Palette> &palettes);
}
} // namespace sorting
#endif // RGBDS_GFX_PAL_SORTING_HPP

View File

@@ -13,6 +13,42 @@ static inline void report() {
puts(__PRETTY_FUNCTION__);
}
template<typename T>
class EnumSeqIterator {
T _value;
public:
explicit EnumSeqIterator(T value) : _value(value) {}
EnumSeqIterator &operator++() {
_value = (T)(_value + 1);
return *this;
}
auto operator*() const { return _value; }
friend auto operator==(EnumSeqIterator const &lhs, EnumSeqIterator const &rhs) {
return lhs._value == rhs._value;
}
friend auto operator!=(EnumSeqIterator const &lhs, EnumSeqIterator const &rhs) {
return lhs._value != rhs._value;
}
};
template<typename T>
class EnumSeq {
T _start;
T _stop;
public:
explicit EnumSeq(T stop) : _start((T)0), _stop(stop) {}
explicit EnumSeq(T start, T stop) : _start(start), _stop(stop) {}
EnumSeqIterator<T> begin() { return EnumSeqIterator(_start); }
EnumSeqIterator<T> end() { return EnumSeqIterator(_stop); }
};
// This is not a fully generic implementation; its current use cases only require for-loop behavior.
// We also assume that all iterators have the same length.
template<typename... Iters>
@@ -73,7 +109,7 @@ public:
template<typename T>
using Holder = std::conditional_t<std::is_lvalue_reference_v<T>, T,
std::remove_cv_t<std::remove_reference_t<T>>>;
}
} // namespace detail
// Does the same number of iterations as the first container's iterator!
template<typename... Containers>