diff --git a/CMakeLists.txt b/CMakeLists.txt index e019db8..d2fec37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -project(UECS VERSION 0.11.0) +project(UECS VERSION 0.11.1) message(STATUS "[Project] ${PROJECT_NAME}") include(cmake/InitUCMake.cmake) diff --git a/include/UECS/CmptLocator.h b/include/UECS/CmptLocator.h index 133cc1e..04a8a84 100644 --- a/include/UECS/CmptLocator.h +++ b/include/UECS/CmptLocator.h @@ -23,18 +23,12 @@ namespace Ubpa::UECS { size_t HashCode() const noexcept { return hashCode; } - const std::set& LastFrameCmptTypes() const noexcept { return lastFrameCmptTypes; } - const std::set& WriteCmptTypes() const noexcept { return writeCmptTypes; } - const std::set& LatestCmptTypes() const noexcept { return latestCmptTypes; } const std::set& CmptTypes() const noexcept { return cmptTypes; } bool operator==(const CmptLocator& rhs) const noexcept; private: size_t GenHashCode() const noexcept; - std::set lastFrameCmptTypes; - std::set writeCmptTypes; - std::set latestCmptTypes; std::set cmptTypes; size_t hashCode; diff --git a/include/UECS/CmptPtr.h b/include/UECS/CmptPtr.h index 0b09b2a..d64aec9 100644 --- a/include/UECS/CmptPtr.h +++ b/include/UECS/CmptPtr.h @@ -12,11 +12,13 @@ namespace Ubpa::UECS { template CmptPtr(Cmpt* p) : type{ CmptType::Of }, p{ p }{} - CmptType Type() const noexcept { return type; } - // unchecked void* Ptr() const noexcept { return p; } + CmptType Type() const noexcept { return type; } + + bool Valid() const noexcept { return p != nullptr; } + // unchecked template Cmpt* As() const noexcept { return reinterpret_cast(p); } diff --git a/include/UECS/CmptType.h b/include/UECS/CmptType.h index 9b6b8af..1fb202f 100644 --- a/include/UECS/CmptType.h +++ b/include/UECS/CmptType.h @@ -26,6 +26,7 @@ namespace Ubpa::UECS { template // non-tagged constexpr bool Is() const noexcept { return hashcode == TypeID; } + // only compare hash constexpr bool operator<(const CmptType& rhs) const noexcept { return hashcode < rhs.hashcode; } constexpr bool operator==(const CmptType& rhs) const noexcept { return hashcode == rhs.hashcode; } constexpr bool operator!=(const CmptType& rhs) const noexcept { return hashcode != rhs.hashcode; } diff --git a/include/UECS/Schedule.h b/include/UECS/Schedule.h index 34a0e8a..5a44228 100644 --- a/include/UECS/Schedule.h +++ b/include/UECS/Schedule.h @@ -8,7 +8,7 @@ #include -namespace Ubpa::UECS::detail::Schedule_ { +namespace Ubpa::UECS::detail { struct Compiler; } @@ -64,7 +64,7 @@ namespace Ubpa::UECS { std::vector writeSysFuncs; std::vector latestSysFuncs; }; - friend struct detail::Schedule_::Compiler; + friend struct detail::Compiler; std::unordered_map GenCmptSysFuncsMap() const; SysFuncGraph GenSysFuncGraph() const; diff --git a/include/UECS/SingletonLocator.h b/include/UECS/SingletonLocator.h index 88ce9f2..a9f8341 100644 --- a/include/UECS/SingletonLocator.h +++ b/include/UECS/SingletonLocator.h @@ -19,15 +19,9 @@ namespace Ubpa::UECS { template SingletonLocator& Combine(); - const std::set& LastFrameSingletonTypes() const noexcept { return lastFrameSingletonTypes; } - const std::set& WriteSingletonTypes() const noexcept { return writeSingletonTypes; } - const std::set& LatestSingletonTypes() const noexcept { return latestSingletonTypes; } const std::set& SingletonTypes() const noexcept { return singletonTypes; } private: - std::set lastFrameSingletonTypes; - std::set writeSingletonTypes; - std::set latestSingletonTypes; std::set singletonTypes; }; } diff --git a/include/UECS/detail/CmptsLocator.inl b/include/UECS/detail/CmptsLocator.inl index 123611a..5ab749f 100644 --- a/include/UECS/detail/CmptsLocator.inl +++ b/include/UECS/detail/CmptsLocator.inl @@ -26,9 +26,6 @@ namespace Ubpa::UECS { template CmptLocator& CmptLocator::Combine() { CmptLocator funcLocator = Generate(); - lastFrameCmptTypes = SetUnion(lastFrameCmptTypes, funcLocator.lastFrameCmptTypes); - writeCmptTypes = SetUnion(writeCmptTypes, funcLocator.writeCmptTypes); - latestCmptTypes = SetUnion(latestCmptTypes, funcLocator.latestCmptTypes); cmptTypes = SetUnion(cmptTypes, funcLocator.cmptTypes); return *this; } diff --git a/include/UECS/detail/SingletonLocator.inl b/include/UECS/detail/SingletonLocator.inl index a6ad208..df6f9e7 100644 --- a/include/UECS/detail/SingletonLocator.inl +++ b/include/UECS/detail/SingletonLocator.inl @@ -26,9 +26,6 @@ namespace Ubpa::UECS { template SingletonLocator& SingletonLocator::Combine() { SingletonLocator funcLocator = Generate(); - lastFrameSingletonTypes = SetUnion(lastFrameSingletonTypes, funcLocator.lastFrameSingletonTypes); - writeSingletonTypes = SetUnion(writeSingletonTypes, funcLocator.writeSingletonTypes); - latestSingletonTypes = SetUnion(latestSingletonTypes, funcLocator.latestSingletonTypes); singletonTypes = SetUnion(singletonTypes, funcLocator.singletonTypes); return *this; } diff --git a/src/core/CmptLocator.cpp b/src/core/CmptLocator.cpp index 807cb35..0070237 100644 --- a/src/core/CmptLocator.cpp +++ b/src/core/CmptLocator.cpp @@ -9,25 +9,8 @@ using namespace std; CmptLocator::CmptLocator(const CmptType* types, size_t num) { assert(types != nullptr && num > 0); - for (size_t i = 0; i < num; i++) { - switch (types[i].GetAccessMode()) - { - case Ubpa::UECS::AccessMode::LAST_FRAME: - lastFrameCmptTypes.insert(types[i]); - break; - case Ubpa::UECS::AccessMode::WRITE: - writeCmptTypes.insert(types[i]); - break; - case Ubpa::UECS::AccessMode::LATEST: - latestCmptTypes.insert(types[i]); - break; - default: - assert(false); - break; - } - } - cmptTypes = SetUnion(lastFrameCmptTypes, writeCmptTypes); - cmptTypes = SetUnion(cmptTypes, latestCmptTypes); + for (size_t i = 0; i < num; i++) + cmptTypes.insert(types[i]); hashCode = GenHashCode(); } @@ -37,13 +20,11 @@ CmptLocator::CmptLocator() size_t CmptLocator::GenHashCode() const noexcept { size_t rst = TypeID; - for (auto type : cmptTypes) + for (const auto& type : cmptTypes) rst = hash_combine(rst, type.HashCode()); return rst; } bool CmptLocator::operator==(const CmptLocator& rhs) const noexcept { - return lastFrameCmptTypes == rhs.lastFrameCmptTypes - && writeCmptTypes == rhs.writeCmptTypes - && latestCmptTypes == rhs.latestCmptTypes; + return cmptTypes == rhs.cmptTypes; } diff --git a/src/core/Schedule.cpp b/src/core/Schedule.cpp index 87e060b..d704148 100644 --- a/src/core/Schedule.cpp +++ b/src/core/Schedule.cpp @@ -7,7 +7,7 @@ using namespace Ubpa::UECS; using namespace std; -namespace Ubpa::UECS::detail::Schedule_ { +namespace Ubpa::UECS::detail { struct NoneGroup { NoneGroup() = default; NoneGroup(SystemFunc* func) @@ -213,21 +213,40 @@ void Schedule::Clear() { unordered_map Schedule::GenCmptSysFuncsMap() const { unordered_map rst; for (const auto& [hashcode, sysFunc] : sysFuncs) { - const auto& cmptsLocator = sysFunc->entityQuery.locator; - for (const auto& type : cmptsLocator.LastFrameCmptTypes()) - rst[type].lastFrameSysFuncs.push_back(sysFunc); - for (const auto& type : cmptsLocator.WriteCmptTypes()) - rst[type].writeSysFuncs.push_back(sysFunc); - for (const auto& type : cmptsLocator.LatestCmptTypes()) - rst[type].latestSysFuncs.push_back(sysFunc); - - const auto& singletonsLocator = sysFunc->singletonLocator; - for (const auto& type : singletonsLocator.LastFrameSingletonTypes()) - rst[type].lastFrameSysFuncs.push_back(sysFunc); - for (const auto& type : singletonsLocator.WriteSingletonTypes()) - rst[type].writeSysFuncs.push_back(sysFunc); - for (const auto& type : singletonsLocator.LatestSingletonTypes()) - rst[type].latestSysFuncs.push_back(sysFunc); + for (const auto& type : sysFunc->entityQuery.locator.CmptTypes()) { + switch (type.GetAccessMode()) + { + case Ubpa::UECS::AccessMode::LAST_FRAME: + rst[type].lastFrameSysFuncs.push_back(sysFunc); + break; + case Ubpa::UECS::AccessMode::WRITE: + rst[type].writeSysFuncs.push_back(sysFunc); + break; + case Ubpa::UECS::AccessMode::LATEST: + rst[type].latestSysFuncs.push_back(sysFunc); + break; + default: + assert(false); + break; + } + } + for (const auto& type : sysFunc->singletonLocator.SingletonTypes()) { + switch (type.GetAccessMode()) + { + case Ubpa::UECS::AccessMode::LAST_FRAME_SINGLETON: + rst[type].lastFrameSysFuncs.push_back(sysFunc); + break; + case Ubpa::UECS::AccessMode::WRITE_SINGLETON: + rst[type].writeSysFuncs.push_back(sysFunc); + break; + case Ubpa::UECS::AccessMode::LATEST_SINGLETON: + rst[type].latestSysFuncs.push_back(sysFunc); + break; + default: + assert(false); + break; + } + } if (sysFunc->GetMode() == SystemFunc::Mode::Chunk) { const auto& filter = sysFunc->entityQuery.filter; @@ -296,7 +315,7 @@ SysFuncGraph Schedule::GenSysFuncGraph() const { auto cmptSysFuncsMap = GenCmptSysFuncsMap(); // [gen groupMap] - unordered_map groupMap; + unordered_map groupMap; for (const auto& [hashcode, sysFunc] : sysFuncs) groupMap.emplace(sysFunc, sysFunc); @@ -323,14 +342,14 @@ SysFuncGraph Schedule::GenSysFuncGraph() const { // [gen graph] - edge - time point for (const auto& [type, cmptSysFuncs] : cmptSysFuncsMap) - detail::Schedule_::Compiler::SetPrePostEdge(graph, cmptSysFuncs, groupMap); + detail::Compiler::SetPrePostEdge(graph, cmptSysFuncs, groupMap); // [gen graph] - edge - none group for (const auto& [type, cmptSysFuncs] : cmptSysFuncsMap) { if (cmptSysFuncs.writeSysFuncs.empty()) continue; - auto sortedGroup = detail::Schedule_::Compiler::GenSortNoneGroup(graph, cmptSysFuncs, groupMap); + auto sortedGroup = detail::Compiler::GenSortNoneGroup(graph, cmptSysFuncs, groupMap); for (size_t i = 0; i < sortedGroup.size() - 1; i++) { const auto& gx = sortedGroup[i]; diff --git a/src/core/SingletonLocator.cpp b/src/core/SingletonLocator.cpp index 51f7c93..c0134be 100644 --- a/src/core/SingletonLocator.cpp +++ b/src/core/SingletonLocator.cpp @@ -9,26 +9,8 @@ using namespace std; SingletonLocator::SingletonLocator(const CmptType* types, size_t num) { assert(types != nullptr && num > 0); - for (size_t i = 0; i < num; i++) { - switch (types[i].GetAccessMode()) - { - case Ubpa::UECS::AccessMode::LAST_FRAME_SINGLETON: - lastFrameSingletonTypes.insert(types[i]); - break; - case Ubpa::UECS::AccessMode::WRITE_SINGLETON: - writeSingletonTypes.insert(types[i]); - break; - case Ubpa::UECS::AccessMode::LATEST_SINGLETON: - latestSingletonTypes.insert(types[i]); - break; - default: - assert(false); - break; - } - } - - singletonTypes = SetUnion(lastFrameSingletonTypes, writeSingletonTypes); - singletonTypes = SetUnion(singletonTypes, latestSingletonTypes); + for (size_t i = 0; i < num; i++) + singletonTypes.insert(types[i]); } SingletonLocator::SingletonLocator() {} diff --git a/src/core/World.cpp b/src/core/World.cpp index c3d9cec..bac31c4 100644 --- a/src/core/World.cpp +++ b/src/core/World.cpp @@ -137,9 +137,9 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { cmptTypes.insert(singleton); } - for (auto cmptType : cmptTypes) { + for (const auto& cmptType : cmptTypes) { auto cmptIdx = registry.RegisterNode(queryCmptName(cmptType)); - cmptType2idx[cmptType] = cmptIdx; + cmptType2idx.emplace(cmptType, cmptIdx); if (AccessMode_IsSingleton(cmptType.GetAccessMode())) subgraph_singleton.AddNode(cmptIdx); else @@ -152,18 +152,26 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { subgraph_sys.AddNode(sysIdx); - const auto& cmptlocator = sysFunc->entityQuery.locator; - for (const auto& cmptType : cmptlocator.LastFrameCmptTypes()) { - auto edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); - subgraph_lastframe.AddEdge(edgeIdx); - } - for (const auto& cmptType : cmptlocator.WriteCmptTypes()) { - auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]); - subgraph_write.AddEdge(edgeIdx); - } - for (const auto& cmptType : cmptlocator.LatestCmptTypes()) { - auto edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); - subgraph_latest.AddEdge(edgeIdx); + for (const auto& cmptType : sysFunc->entityQuery.locator.CmptTypes()) { + size_t edgeIdx; + switch (cmptType.GetAccessMode()) + { + case Ubpa::UECS::AccessMode::LAST_FRAME: + edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); + subgraph_lastframe.AddEdge(edgeIdx); + break; + case Ubpa::UECS::AccessMode::WRITE: + edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]); + subgraph_write.AddEdge(edgeIdx); + break; + case Ubpa::UECS::AccessMode::LATEST: + edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx); + subgraph_latest.AddEdge(edgeIdx); + break; + default: + assert(false); + break; + } } const auto& filter = sysFunc->entityQuery.filter; @@ -238,18 +246,26 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { subgraph_none.AddEdge(edgeIdx); } - const auto& singletonlocator = sysFunc->singletonLocator; - for (const auto& singleton : singletonlocator.LastFrameSingletonTypes()) { - auto edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx); - subgraph_lastframe.AddEdge(edgeIdx); - } - for (const auto& singleton : singletonlocator.WriteSingletonTypes()) { - auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[singleton]); - subgraph_write.AddEdge(edgeIdx); - } - for (const auto& singleton : singletonlocator.LatestSingletonTypes()) { - auto edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx); - subgraph_latest.AddEdge(edgeIdx); + for (const auto& singleton : sysFunc->singletonLocator.SingletonTypes()) { + size_t edgeIdx; + switch (singleton.GetAccessMode()) + { + case Ubpa::UECS::AccessMode::LAST_FRAME_SINGLETON: + edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx); + subgraph_lastframe.AddEdge(edgeIdx); + break; + case Ubpa::UECS::AccessMode::WRITE_SINGLETON: + edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[singleton]); + subgraph_write.AddEdge(edgeIdx); + break; + case Ubpa::UECS::AccessMode::LATEST_SINGLETON: + edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx); + subgraph_latest.AddEdge(edgeIdx); + break; + default: + assert(false); + break; + } } } diff --git a/src/test/16_singleton/main.cpp b/src/test/16_singleton/main.cpp index d3bda26..9a1d5c7 100644 --- a/src/test/16_singleton/main.cpp +++ b/src/test/16_singleton/main.cpp @@ -29,7 +29,6 @@ class MoverSystem : public System { }; int main() { - constexpr auto mode = AccessModeOf>; RTDCmptTraits::Instance().Register ();