diff --git a/libs/ecs/include/psemek/ecs/detail/ordered_component_hash.hpp b/libs/ecs/include/psemek/ecs/detail/ordered_component_hash.hpp new file mode 100644 index 00000000..ef5a9be5 --- /dev/null +++ b/libs/ecs/include/psemek/ecs/detail/ordered_component_hash.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +namespace psemek::ecs::detail +{ + + template + struct ordered_component_hasher + { + std::size_t result = 0xb6113227befa663bull; + + void operator()(util::uuid const & uuid) + { + auto hash = std::hash{}(uuid); + if (!With) + hash = ~hash; + + util::hash_combine(result, hash); + } + }; + + template + std::size_t ordered_component_hash(UUIDs const & uuids) + { + ordered_component_hasher hasher; + for (auto const & uuid : uuids) + hasher(uuid); + return hasher.result; + } + + template + std::size_t ordered_component_hash(WithUUIDs const & with_uuids, WithoutUUIDs const & without_uuids) + { + return ordered_component_hash(with_uuids) ^ ordered_component_hash(without_uuids); + } + +} diff --git a/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp b/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp index f4b700ff..66dee4e9 100644 --- a/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp +++ b/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -14,7 +14,7 @@ namespace psemek::ecs::detail { std::size_t operator()(std::pair, util::span> const & uuids) const { - return component_hash(uuids.first, uuids.second); + return ordered_component_hash(uuids.first, uuids.second); } std::size_t operator()(std::shared_ptr const & cache) const @@ -55,7 +55,7 @@ namespace psemek::ecs::detail 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); + cache->hash = ordered_component_hash(with_uuids, without_uuids); table_container.apply([&](table & table){ cache->add(&table); diff --git a/libs/ecs/include/psemek/ecs/detail/table.hpp b/libs/ecs/include/psemek/ecs/detail/table.hpp index 6e4c3ae9..181b101f 100644 --- a/libs/ecs/include/psemek/ecs/detail/table.hpp +++ b/libs/ecs/include/psemek/ecs/detail/table.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include diff --git a/libs/ecs/include/psemek/ecs/detail/table_container.hpp b/libs/ecs/include/psemek/ecs/detail/table_container.hpp index f10ae2e0..de660612 100644 --- a/libs/ecs/include/psemek/ecs/detail/table_container.hpp +++ b/libs/ecs/include/psemek/ecs/detail/table_container.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -11,7 +11,7 @@ namespace psemek::ecs::detail { std::size_t operator()(util::span const & uuids) const { - return component_hash(uuids); + return unordered_component_hash(uuids); } std::size_t operator()(std::unique_ptr const & table) const diff --git a/libs/ecs/include/psemek/ecs/detail/component_hash.hpp b/libs/ecs/include/psemek/ecs/detail/unordered_component_hash.hpp similarity index 61% rename from libs/ecs/include/psemek/ecs/detail/component_hash.hpp rename to libs/ecs/include/psemek/ecs/detail/unordered_component_hash.hpp index f74bf8c0..ad756cc5 100644 --- a/libs/ecs/include/psemek/ecs/detail/component_hash.hpp +++ b/libs/ecs/include/psemek/ecs/detail/unordered_component_hash.hpp @@ -7,7 +7,7 @@ namespace psemek::ecs::detail { template - struct component_hasher + struct unordered_component_hasher { std::size_t result = 0xcd5694d2b3f3443eull; @@ -21,18 +21,18 @@ namespace psemek::ecs::detail }; template - std::size_t component_hash(UUIDs const & uuids) + std::size_t unordered_component_hash(UUIDs const & uuids) { - component_hasher hasher; + unordered_component_hasher hasher; for (auto const & uuid : uuids) hasher(uuid); return hasher.result; } template - std::size_t component_hash(WithUUIDs const & with_uuids, WithoutUUIDs const & without_uuids) + std::size_t unordered_component_hash(WithUUIDs const & with_uuids, WithoutUUIDs const & without_uuids) { - return component_hash(with_uuids) ^ component_hash(without_uuids); + return unordered_component_hash(with_uuids) ^ unordered_component_hash(without_uuids); } } diff --git a/libs/ecs/source/detail/table.cpp b/libs/ecs/source/detail/table.cpp index a0b2fd5e..2011e881 100644 --- a/libs/ecs/source/detail/table.cpp +++ b/libs/ecs/source/detail/table.cpp @@ -1,12 +1,12 @@ #include -#include +#include namespace psemek::ecs::detail { table::table(std::vector> columns) { - component_hasher hasher; + unordered_component_hasher hasher; for (std::size_t i = 0; i < columns.size(); ++i) { auto const & column = columns[i];