4#ifndef GREM_EXECUTION_ENTITY_TABLE_HPP
5#define GREM_EXECUTION_ENTITY_TABLE_HPP
26template <
typename Row,
typename Allocator = std::allocator<Row>>
29template <
typename... Components>
33template <
typename... Components>
34struct is_entity_range<
Columns<Components...>> : std::true_type {};
36template <
typename... Components>
37struct entity_range_components_and_exclusions<
Columns<Components...>> {
44template <
typename Component>
45struct columns_extract_components;
47template <component Component>
48struct columns_extract_components<Component> {
55template <component Component>
56struct columns_extract_components<const Component> {
63struct ColumnsSentinel {
67template <
typename IncludedComponentsList>
70template <
typename... IncludedComponents>
71class ColumnsIterator<meta::TypeList<IncludedComponents...>> {
73 using difference_type = ptrdiff_t;
74 using value_type =
Tuple<
const size_t, IncludedComponents...>;
75 using reference =
Tuple<
const size_t&, IncludedComponents&...>;
76 using iterator_category = std::random_access_iterator_tag;
81 [[nodiscard]]
constexpr reference* operator->() noexcept {
86 ColumnsIterator() noexcept = default;
88 constexpr ColumnsIterator(
size_t rowIndex, const
Array<
void*, sizeof...(IncludedComponents)>& componentArrays) noexcept
90 , componentArrays(componentArrays) {}
92 [[nodiscard]]
constexpr reference
operator*()
const {
93 return [&]<
size_t... Indices>(std::index_sequence<Indices...>) -> reference {
94 return reference{rowIndex,
static_cast<IncludedComponents*
>(componentArrays[Indices])[rowIndex]...};
95 }(std::make_index_sequence<
sizeof...(IncludedComponents)>{});
98 [[nodiscard]]
constexpr pointer operator->()
const {
99 return pointer{**
this};
102 [[nodiscard]]
constexpr reference operator[](difference_type n)
const {
106 constexpr ColumnsIterator& operator++() {
111 constexpr ColumnsIterator& operator--() {
116 constexpr ColumnsIterator operator++(
int) {
117 return ColumnsIterator{rowIndex++, componentArrays};
120 constexpr ColumnsIterator operator--(
int) {
121 return ColumnsIterator{rowIndex--, componentArrays};
124 constexpr ColumnsIterator&
operator+=(difference_type n) {
129 constexpr ColumnsIterator&
operator-=(difference_type n) {
134 [[nodiscard]]
constexpr bool operator==(ColumnsSentinel other)
const {
135 return rowIndex == other.rowsEnd;
138 [[nodiscard]]
friend constexpr ColumnsIterator
operator+(
const ColumnsIterator& a, difference_type b) {
139 return ColumnsIterator{a.rowIndex + b, a.componentArrays};
142 [[nodiscard]]
friend constexpr ColumnsIterator
operator+(difference_type a,
const ColumnsIterator& b) {
143 return ColumnsIterator{a + b.rowIndex, b.componentArrays};
146 [[nodiscard]]
friend constexpr ColumnsIterator
operator-(
const ColumnsIterator& a, difference_type b) {
147 return ColumnsIterator{a.rowIndex - b, a.componentArrays};
150 [[nodiscard]]
friend constexpr difference_type
operator-(
const ColumnsIterator& a,
const ColumnsIterator& b) {
151 return static_cast<difference_type
>(a.rowIndex - b.rowIndex);
154 [[nodiscard]]
friend constexpr bool operator==(
const ColumnsIterator& a,
const ColumnsIterator& b) {
155 return a.rowIndex == b.rowIndex;
158 [[nodiscard]]
friend constexpr auto operator<=>(
const ColumnsIterator& a,
const ColumnsIterator& b) {
159 return a.rowIndex <=> b.rowIndex;
164 Array<
void*,
sizeof...(IncludedComponents)> componentArrays;
174template <
typename... Components>
189 using iterator = detail::ColumnsIterator<IncludedComponents>;
210 : rowIndex(first.rowIndex)
211 , rowsEnd(last.rowIndex)
212 , componentArrays(first.componentArrays) {}
215 return iterator{rowIndex, componentArrays};
228 return rowsEnd - rowIndex;
236 [[nodiscard]]
constexpr bool empty() const noexcept {
237 return rowIndex == rowsEnd;
250 return rowsEnd - rowIndex;
254 template <
typename Row,
typename Allocator>
260 , componentArrays(componentArrays) {}
280template <
typename... Components,
typename Allocator>
283 using ComponentTable =
Table<
Tuple<Components...>>;
288 using typename ComponentTable::allocator_type;
289 using typename ComponentTable::const_iterator;
290 using typename ComponentTable::const_pointer;
291 using typename ComponentTable::const_reference;
292 using typename ComponentTable::const_reverse_iterator;
293 using typename ComponentTable::difference_type;
294 using typename ComponentTable::iterator;
295 using typename ComponentTable::pointer;
296 using typename ComponentTable::reference;
297 using typename ComponentTable::reverse_iterator;
298 using typename ComponentTable::size_type;
299 using typename ComponentTable::value_type;
301 using ComponentTable::at;
302 using ComponentTable::column;
303 using ComponentTable::get;
304 using ComponentTable::Table;
305 using ComponentTable::operator[];
306 using ComponentTable::back;
307 using ComponentTable::begin;
308 using ComponentTable::capacity;
309 using ComponentTable::cbegin;
310 using ComponentTable::cend;
311 using ComponentTable::clear;
312 using ComponentTable::crbegin;
313 using ComponentTable::crend;
314 using ComponentTable::emplace_back;
315 using ComponentTable::empty;
316 using ComponentTable::end;
317 using ComponentTable::front;
318 using ComponentTable::max_size;
319 using ComponentTable::pop_back;
320 using ComponentTable::push_back;
321 using ComponentTable::rbegin;
322 using ComponentTable::rend;
323 using ComponentTable::reserve;
324 using ComponentTable::resize;
325 using ComponentTable::shrink_to_fit;
326 using ComponentTable::size;
327 using ComponentTable::swap;
340 template <component T>
342 if (rowIndex >= size()) {
343 throw std::out_of_range{
"Component not found for entity."};
345 return this->
template get<T>(rowIndex);
359 template <component T>
361 if (rowIndex >= size()) {
362 throw std::out_of_range{
"Component not found for entity."};
364 return this->
template get<T>(rowIndex);
384 template <
typename... Cs>
386 using EntityRange =
Columns<Cs...>;
387 return getEntitiesImplementation<EntityRange>(
typename EntityRange::IncludedComponents{});
407 template <
typename... Cs>
409 using EntityRange =
Columns<Cs...>;
410 return const_cast<EntityTable*
>(
this)->getEntitiesImplementation<EntityRange>(
typename EntityRange::IncludedComponents{});
420 template <
typename... Cs>
432 template <
typename... Cs>
462 template <
typename... Cs>
464 using EntityRange =
Columns<Cs...>;
465 return getEntitiesChunkImplementation<EntityRange>(chunkIndex, chunkCount,
typename EntityRange::IncludedComponents{});
493 template <
typename... Cs>
496 using EntityRange =
Columns<Cs...>;
497 return const_cast<EntityTable*
>(
this)->getEntitiesChunkImplementation<EntityRange>(chunkIndex, chunkCount,
typename EntityRange::IncludedComponents{});
501 template <
typename EntityRange,
typename... IncludedComponents>
506 template <
typename EntityRange,
typename... IncludedComponents>
507 [[nodiscard]] EntityRange getEntitiesChunkImplementation(
size_t chunkIndex,
size_t chunkCount, meta::TypeList<IncludedComponents...>)
noexcept {
509 const size_t chunkSize = (size() + chunkCount - 1) / chunkCount;
510 const size_t chunkBegin = chunkIndex * chunkSize;
512 const size_t chunkEnd =
min(chunkBegin + chunkSize, size());
517template <std::
size_t Index,
typename Row,
typename Allocator>
522template <std::
size_t Index,
typename Row,
typename Allocator>
527template <
typename T,
typename Row,
typename Allocator>
529 return t.template
get<T>(rowIndex);
532template <
typename T,
typename Row,
typename Allocator>
534 return t.template
get<T>(rowIndex);
537template <
typename T,
typename Row,
typename Allocator>
542template <
typename T,
typename Row,
typename Allocator>
547template <std::
size_t ColumnIndex,
typename Row,
typename Allocator>
552template <std::
size_t ColumnIndex,
typename Row,
typename Allocator>
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
View of the rows in a specific set of columns in an EntityTable.
Definition EntityTable.hpp:175
constexpr bool empty() const noexcept
Check if the range is empty.
Definition EntityTable.hpp:236
detail::ColumnsSentinel sentinel
Definition EntityTable.hpp:190
typename iterator::value_type value_type
Definition EntityTable.hpp:191
ptrdiff_t difference_type
Definition EntityTable.hpp:194
meta::type_list_concat_t< typename detail::columns_extract_components< Components >::IncludedComponents... > IncludedComponents
List of components required by the range, provided as a meta::TypeList.
Definition EntityTable.hpp:184
friend class EntityTable
Definition EntityTable.hpp:255
size_t size_type
Definition EntityTable.hpp:193
meta::type_list_concat_t< typename detail::columns_extract_components< Components >::ImmutableComponents... > ImmutableComponents
List of const components required by the range, provided as a meta::TypeList.
Definition EntityTable.hpp:181
constexpr size_type getCandidateCount() const noexcept
Get an upper bound estimate on the number of rows in the range, which is equal to the exact number of...
Definition EntityTable.hpp:249
meta::type_list_concat_t< typename detail::columns_extract_components< Components >::ExcludedComponents... > ExcludedComponents
List of components excluded by the range, provided as a meta::TypeList.
Definition EntityTable.hpp:187
detail::ColumnsIterator< IncludedComponents > iterator
Definition EntityTable.hpp:189
constexpr size_type size() const noexcept
Get the number of rows in the range.
Definition EntityTable.hpp:227
constexpr Columns() noexcept=default
Construct an empty entity range.
typename iterator::reference reference
Definition EntityTable.hpp:192
constexpr sentinel end() const noexcept
Definition EntityTable.hpp:218
constexpr iterator begin() const noexcept
Definition EntityTable.hpp:214
meta::type_list_concat_t< typename detail::columns_extract_components< Components >::MutableComponents... > MutableComponents
List of non-const components required by the range, provided as a meta::TypeList.
Definition EntityTable.hpp:178
Columns< Cs... > getEntities() noexcept
Get a view of all rows of a specific set of columns in the table.
Definition EntityTable.hpp:385
Columns< Cs... > getEntitiesChunk(size_t chunkIndex, size_t chunkCount) const noexcept
Get a specific chunk of the rows of a specific set of columns in the table.
Definition EntityTable.hpp:494
Columns< Cs... > getEntities() const noexcept
Get a view of all rows of a specific set of columns in the table.
Definition EntityTable.hpp:408
const T & getComponent(size_t rowIndex) const
Get a specific component of a row in the table.
Definition EntityTable.hpp:360
Columns< Cs... > getEntitiesChunk(size_t chunkIndex, size_t chunkCount) noexcept
Get a specific chunk of the rows of a specific set of columns in the table.
Definition EntityTable.hpp:463
T & getComponent(size_t rowIndex)
Get a specific component of a row in the table.
Definition EntityTable.hpp:341
Definition EntityTable.hpp:27
Concept that checks if a given type is a valid component type that can be stored and associated with ...
Definition component.hpp:21
constexpr Span< T > column(EntityTable< Row, Allocator > &t)
Definition EntityTable.hpp:538
constexpr decltype(auto) get(EntityTable< Row, Allocator > &t, std::size_t rowIndex)
Definition EntityTable.hpp:518
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
constexpr Offset2D & operator+=(Offset2D &a, Offset2D b) noexcept
Definition extents.hpp:646
constexpr Offset2D operator-(Offset2D a) noexcept
Definition extents.hpp:664
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
Tuple(Ts...) -> Tuple< Ts... >
constexpr Offset2D operator*(Offset2D a, int32_t b) noexcept
Definition extents.hpp:692
constexpr Offset2D & operator-=(Offset2D &a, Extent2D b) noexcept
Definition extents.hpp:658
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
constexpr auto operator+(const ConstantString< CharT, LengthA > &a, const ConstantString< CharT, LengthB > &b)
Definition ConstantString.hpp:656