ecs::iterator class allows for iterating over multiple containers (arrays ahah) at the same time by zipping them together.It enables simultaneous access to corresponding elements from different containers.
This is useful in entity-component systems when you need to operate on multiple components in parallel.
namespace ecs {
template <typename... Arrays>
class iterator {
public:
iterator(std::tuple<Arrays &...> arrays, size_t pos) : _arrays(arrays), _pos(pos) {}
bool operator==(const iterator &other) const
{
return _pos == other._pos;
}
bool operator!=(const iterator &other) const
{
return _pos != other._pos;
}
void operator++()
{
++_pos;
}
auto operator*()
{
return mul_args_helper(std::make_index_sequence<sizeof...(Arrays)>());
}
private:
template <std::size_t... I>
auto mul_args_helper(std::index_sequence<I...>)
{
return std::tuple<std::add_lvalue_reference_t<decltype(std::get<I>(_arrays)[_pos])>...>{std::get<I>(_arrays
)[_pos]...};
}
std::tuple<Arrays &...> _arrays;
size_t _pos;
};
} // namespace ecs
iterator(std::tuple<Arrays &...> arrays, size_t pos)
pos).arrays: A tuple holding references to multiple arrays to iterate over.pos: The starting position of the iterator in the arrays.bool operator==(const iterator &other) const
true if the iterators point to the same position in the arrays.bool operator!=(const iterator &other) const
true if the iterators point to different positions in the arrays.void operator++()
auto operator*()
std::tuple<Arrays &...> _arrays
size_t _pos
template <std::size_t... I> auto mul_args_helper(std::index_sequence<I...>)
_pos).