Simplify ecs::query_cache_container: store caches directly instead of storing nodes with uuid duplicates
This commit is contained in:
parent
daa2b9c49a
commit
3359aaa62d
2 changed files with 24 additions and 42 deletions
|
|
@ -22,6 +22,9 @@ namespace psemek::ecs::detail
|
|||
{
|
||||
std::vector<util::uuid> with_uuids;
|
||||
std::vector<util::uuid> without_uuids;
|
||||
|
||||
std::size_t hash;
|
||||
|
||||
std::list<query_cache_entry> entries;
|
||||
|
||||
using callback_factory = util::function<table_callback(std::vector<std::uint32_t> const &)>;
|
||||
|
|
|
|||
|
|
@ -10,14 +10,6 @@
|
|||
namespace psemek::ecs::detail
|
||||
{
|
||||
|
||||
struct query_cache_data
|
||||
{
|
||||
std::size_t hash;
|
||||
std::vector<util::uuid> with_uuids;
|
||||
std::vector<util::uuid> without_uuids;
|
||||
std::shared_ptr<query_cache> cache;
|
||||
};
|
||||
|
||||
struct query_cache_hash
|
||||
{
|
||||
std::size_t operator()(std::pair<util::span<util::uuid const>, util::span<util::uuid const>> const & uuids) const
|
||||
|
|
@ -25,34 +17,28 @@ namespace psemek::ecs::detail
|
|||
return component_hash(uuids.first, uuids.second);
|
||||
}
|
||||
|
||||
std::size_t operator()(std::unique_ptr<query_cache_data> const & set) const
|
||||
std::size_t operator()(std::shared_ptr<query_cache> const & cache) const
|
||||
{
|
||||
return set->hash;
|
||||
return cache->hash;
|
||||
}
|
||||
};
|
||||
|
||||
struct query_cache_equal
|
||||
{
|
||||
bool operator()(std::pair<util::span<util::uuid const>, util::span<util::uuid const>> const & uuids, std::unique_ptr<query_cache_data> const & set) const
|
||||
bool operator()(std::pair<util::span<util::uuid const>, util::span<util::uuid const>> const & uuids, std::shared_ptr<query_cache> const & cache) const
|
||||
{
|
||||
if (uuids.first.size() != set->with_uuids.size())
|
||||
if (!std::equal(uuids.first.begin(), uuids.first.end(), cache->with_uuids.begin(), cache->with_uuids.end()))
|
||||
return false;
|
||||
|
||||
if (uuids.second.size() != set->without_uuids.size())
|
||||
return false;
|
||||
|
||||
if (!std::equal(uuids.first.begin(), uuids.first.end(), set->with_uuids.begin()))
|
||||
return false;
|
||||
|
||||
if (!std::equal(uuids.second.begin(), uuids.second.end(), set->without_uuids.begin()))
|
||||
if (!std::equal(uuids.second.begin(), uuids.second.end(), cache->without_uuids.begin(), cache->without_uuids.end()))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator()(std::unique_ptr<query_cache_data> const & set1, std::unique_ptr<query_cache_data> const & set2) const
|
||||
bool operator()(std::shared_ptr<query_cache> const & cache1, std::shared_ptr<query_cache> const & cache2) const
|
||||
{
|
||||
return set1.get() == set2.get();
|
||||
return cache1.get() == cache2.get();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -64,44 +50,37 @@ namespace psemek::ecs::detail
|
|||
auto it = caches_.find(std::pair{with_uuids, without_uuids});
|
||||
if (it == caches_.end())
|
||||
{
|
||||
auto value = std::make_unique<query_cache_data>();
|
||||
value->cache = std::make_shared<query_cache>();
|
||||
for (auto const & uuid : with_uuids)
|
||||
{
|
||||
value->with_uuids.push_back(uuid);
|
||||
value->cache->with_uuids.push_back(uuid);
|
||||
}
|
||||
for (auto const & uuid : without_uuids)
|
||||
{
|
||||
value->without_uuids.push_back(uuid);
|
||||
value->cache->without_uuids.push_back(uuid);
|
||||
}
|
||||
value->hash = component_hash(with_uuids, without_uuids);
|
||||
auto cache = std::make_shared<query_cache>();
|
||||
|
||||
cache->with_uuids.assign(with_uuids.begin(), with_uuids.end());
|
||||
cache->without_uuids.assign(without_uuids.begin(), without_uuids.end());
|
||||
|
||||
cache->hash = component_hash(with_uuids, without_uuids);
|
||||
|
||||
table_container.apply([&](table & table){
|
||||
value->cache->add(&table);
|
||||
cache->add(&table);
|
||||
}, with_uuids, without_uuids);
|
||||
|
||||
it = caches_.insert(std::move(value)).first;
|
||||
it = caches_.insert(std::move(cache)).first;
|
||||
}
|
||||
return it->get()->cache;
|
||||
return *it;
|
||||
}
|
||||
|
||||
template <typename Function, typename ContainsUUID>
|
||||
void apply(Function && function, ContainsUUID && contains_uuid)
|
||||
{
|
||||
for (auto & cache_data : caches_)
|
||||
for (auto & cache : caches_)
|
||||
{
|
||||
bool good = true;
|
||||
|
||||
for (auto const & uuid : cache_data->with_uuids)
|
||||
for (auto const & uuid : cache->with_uuids)
|
||||
if (!contains_uuid(uuid))
|
||||
{
|
||||
good = false;
|
||||
break;
|
||||
}
|
||||
|
||||
for (auto const & uuid : cache_data->without_uuids)
|
||||
for (auto const & uuid : cache->without_uuids)
|
||||
if (contains_uuid(uuid))
|
||||
{
|
||||
good = false;
|
||||
|
|
@ -111,7 +90,7 @@ namespace psemek::ecs::detail
|
|||
if (!good)
|
||||
continue;
|
||||
|
||||
function(*(cache_data->cache));
|
||||
function(*cache);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -121,7 +100,7 @@ namespace psemek::ecs::detail
|
|||
}
|
||||
|
||||
private:
|
||||
util::hash_set<std::unique_ptr<query_cache_data>, query_cache_hash, query_cache_equal> caches_;
|
||||
util::hash_set<std::shared_ptr<query_cache>, query_cache_hash, query_cache_equal> caches_;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue