Skip to content

Commit

Permalink
feat(ecs): implement symmetric relation querying
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Jan 24, 2024
1 parent 54e1a97 commit f7db4ff
Show file tree
Hide file tree
Showing 4 changed files with 563 additions and 72 deletions.
18 changes: 18 additions & 0 deletions core/include/cubos/core/ecs/query/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ namespace cubos::core::ecs
/// @brief Relation data type.
DataTypeId dataType;

/// @brief Whether the link is symmetric.
bool isSymmetric;

/// @brief From target index.
int fromTarget;

Expand All @@ -80,8 +83,23 @@ namespace cubos::core::ecs
/// @brief Tables which match the link, found through calls to @ref update().
std::vector<SparseRelationTableId> tables;

/// @brief Tables which match the reverse link, found through calls to @ref update().
///
/// Only filled if @ref isSymmetric is true.
std::vector<SparseRelationTableId> reverseTables;

/// @brief Whether each of the tables in @ref reverseTables is already in @ref tables.
///
/// Only filled if @ref isSymmetric is true.
std::vector<bool> reverseTablesSeen;

/// @brief How many tables have already been seen through @ref SparseRelationTableRegistry::collect.
std::size_t seenCount{0};

/// @brief Gets the nth table in the link, as if the reverseTables vector was appended to the end of the
/// tables vector.
/// @param index Table index.
SparseRelationTableId table(std::size_t index) const;
};

World& mWorld;
Expand Down
12 changes: 4 additions & 8 deletions core/include/cubos/core/ecs/table/sparse_relation/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,15 @@ namespace cubos::core::ecs
/// @param index Entity index.
void erase(ArchetypeId archetype, uint32_t index);

/// @brief Collects new tables which match the given filter.
/// @param[out] tables Vector to insert the table identifiers into.
/// @brief Calls the given function for each new table.
/// @param counter Counter previously returned by this function. Zero should be used for the first call.
/// @param filter Function which receives a table identifier and returns a boolean.
/// @param func Function which receives a table identifier.
/// @return Counter to be passed to this function in a future call.
std::size_t collect(std::vector<SparseRelationTableId>& tables, std::size_t counter, auto filter)
std::size_t forEach(std::size_t counter, auto func)
{
for (; counter < mIds.size(); ++counter)
{
if (filter(mIds[counter]))
{
tables.emplace_back(mIds[counter]);
}
func(mIds[counter]);
}

return counter;
Expand Down
Loading

0 comments on commit f7db4ff

Please sign in to comment.