Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
ZipView.hpp
Go to the documentation of this file.
1
8#ifndef RTYPE_ECS_ZIPVIEW_HPP_
9#define RTYPE_ECS_ZIPVIEW_HPP_
10
12#include <tuple>
13#include <vector>
14#include <algorithm>
15#include <iterator>
16#include <span>
17
18namespace rtp::ecs {
19
25 template <class... Containers>
26 class ZipView {
27 public:
31 using tuple_arrays_t = std::tuple<Containers...>;
32
37 class Iterator {
38 public:
39 using iterator_category = std::forward_iterator_tag;
40 using value_type = std::tuple<typename std::remove_reference_t<Containers>::value_type&...>;
41 using difference_type = std::ptrdiff_t;
42 using pointer = void;
44
46 std::span<const Entity> entities,
47 size_t index)
48 : _arrays(arrays), _entities(entities), _index(index)
49 {
51 }
52
54 ++_index;
56 return *this;
57 }
58
60 Iterator tmp = *this;
61 ++(*this);
62 return tmp;
63 }
64
65 bool operator==(const Iterator& other) const {
66 return _index == other._index;
67 }
68
69 bool operator!=(const Iterator& other) const {
70 return _index != other._index;
71 }
72
73 [[nodiscard]]
76 return std::apply([e](auto&&... args) {
77 return std::forward_as_tuple(args[e]...);
78 }, _arrays);
79 }
80
81 private:
83 std::span<const Entity> _entities;
84 size_t _index;
89 void skipInvalid() {
90 while (_index < _entities.size()) {
92
93 bool all_present = std::apply([e](auto&&... args) {
94 return (... && args.has(e));
95 }, _arrays);
96
97 if (all_present) {
98 return;
99 }
100
101 ++_index;
102 }
103 }
104 };
105
110 ZipView(Containers... arrays) : _arrays(std::forward_as_tuple(arrays...)) {
111 size_t min_size = std::get<0>(_arrays).size();
112 _smallest_idx = 0;
113 findSmallest<0>(min_size);
114 }
115
121 const auto entities = getEntitiesFromSmallest(_smallest_idx);
122 return Iterator(_arrays, entities, 0);
123 }
124
130 const auto entities = getEntitiesFromSmallest(_smallest_idx);
131 return Iterator(_arrays, entities, entities.size());
132 }
133
134 private:
136 size_t _smallest_idx = 0;
143 template<size_t I>
144 void findSmallest(size_t& min_size) {
145 if constexpr (I < sizeof...(Containers)) {
146 size_t current_size = std::get<I>(_arrays).size();
147 if (current_size < min_size) {
148 min_size = current_size;
149 _smallest_idx = I;
150 }
151 findSmallest<I + 1>(min_size);
152 }
153 }
154
160 std::span<const Entity> getEntitiesFromSmallest(size_t index) {
161 std::span<const Entity> result{};
162 size_t current = 0;
163
164 std::apply([&](auto&&... args) {
165 ((current++ == index ? result = args.entities() : std::span<const Entity>{}), ...);
166 }, _arrays);
167
168 return result;
169 }
170 };
171}
172
173#endif // RTYPE_ECS_ZIPVIEW_HPP_
Sparse array container for efficient component storage in ECS.
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
The actual iterator performing the intersection logic.
Definition ZipView.hpp:37
bool operator==(const Iterator &other) const
Definition ZipView.hpp:65
std::tuple< typename std::remove_reference_t< Containers >::value_type &... > value_type
Definition ZipView.hpp:40
tuple_arrays_t & _arrays
Tuple of references to SparseArrays.
Definition ZipView.hpp:82
size_t _index
Current index in the entity list.
Definition ZipView.hpp:84
Iterator(tuple_arrays_t &arrays, std::span< const Entity > entities, size_t index)
Definition ZipView.hpp:45
std::forward_iterator_tag iterator_category
Definition ZipView.hpp:39
std::span< const Entity > _entities
Entities of the leader SparseArray.
Definition ZipView.hpp:83
void skipInvalid()
Skip to the next valid entity that has all components.
Definition ZipView.hpp:89
std::ptrdiff_t difference_type
Definition ZipView.hpp:41
bool operator!=(const Iterator &other) const
Definition ZipView.hpp:69
A view that iterates over entities possessing all specified components.
Definition ZipView.hpp:26
ZipView(Containers... arrays)
Constructor for ZipView.
Definition ZipView.hpp:110
Iterator end()
Get iterator to the end of the zipped view.
Definition ZipView.hpp:129
size_t _smallest_idx
Index of the SparseArray with the fewest entities.
Definition ZipView.hpp:136
void findSmallest(size_t &min_size)
Find the index of the SparseArray with the smallest size.
Definition ZipView.hpp:144
std::span< const Entity > getEntitiesFromSmallest(size_t index)
Get the entity list from the SparseArray at the specified index.
Definition ZipView.hpp:160
tuple_arrays_t _arrays
Tuple of references to SparseArrays.
Definition ZipView.hpp:135
std::tuple< Containers... > tuple_arrays_t
Tuple type holding references to all SparseArrays.
Definition ZipView.hpp:31
Iterator begin()
Get iterator to the beginning of the zipped view.
Definition ZipView.hpp:120
File : RenderSystem.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
STL namespace.