Skip to content

Commit

Permalink
simplify {Cmpt|Singleton}Locator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 9, 2020
1 parent 4afcddc commit f42e4e3
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 112 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 0 additions & 6 deletions include/UECS/CmptLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ namespace Ubpa::UECS {

size_t HashCode() const noexcept { return hashCode; }

const std::set<CmptType>& LastFrameCmptTypes() const noexcept { return lastFrameCmptTypes; }
const std::set<CmptType>& WriteCmptTypes() const noexcept { return writeCmptTypes; }
const std::set<CmptType>& LatestCmptTypes() const noexcept { return latestCmptTypes; }
const std::set<CmptType>& CmptTypes() const noexcept { return cmptTypes; }

bool operator==(const CmptLocator& rhs) const noexcept;
private:
size_t GenHashCode() const noexcept;

std::set<CmptType> lastFrameCmptTypes;
std::set<CmptType> writeCmptTypes;
std::set<CmptType> latestCmptTypes;
std::set<CmptType> cmptTypes;

size_t hashCode;
Expand Down
6 changes: 4 additions & 2 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace Ubpa::UECS {
template<typename Cmpt>
CmptPtr(Cmpt* p) : type{ CmptType::Of<Cmpt> }, 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<typename Cmpt>
Cmpt* As() const noexcept { return reinterpret_cast<Cmpt*>(p); }
Expand Down
1 change: 1 addition & 0 deletions include/UECS/CmptType.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Ubpa::UECS {
template<typename Cmpt> // non-tagged
constexpr bool Is() const noexcept { return hashcode == TypeID<Cmpt>; }

// 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; }
Expand Down
4 changes: 2 additions & 2 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <map>

namespace Ubpa::UECS::detail::Schedule_ {
namespace Ubpa::UECS::detail {
struct Compiler;
}

Expand Down Expand Up @@ -64,7 +64,7 @@ namespace Ubpa::UECS {
std::vector<SystemFunc*> writeSysFuncs;
std::vector<SystemFunc*> latestSysFuncs;
};
friend struct detail::Schedule_::Compiler;
friend struct detail::Compiler;
std::unordered_map<CmptType, CmptSysFuncs> GenCmptSysFuncsMap() const;

SysFuncGraph GenSysFuncGraph() const;
Expand Down
6 changes: 0 additions & 6 deletions include/UECS/SingletonLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ namespace Ubpa::UECS {
template<typename Func>
SingletonLocator& Combine();

const std::set<CmptType>& LastFrameSingletonTypes() const noexcept { return lastFrameSingletonTypes; }
const std::set<CmptType>& WriteSingletonTypes() const noexcept { return writeSingletonTypes; }
const std::set<CmptType>& LatestSingletonTypes() const noexcept { return latestSingletonTypes; }
const std::set<CmptType>& SingletonTypes() const noexcept { return singletonTypes; }

private:
std::set<CmptType> lastFrameSingletonTypes;
std::set<CmptType> writeSingletonTypes;
std::set<CmptType> latestSingletonTypes;
std::set<CmptType> singletonTypes;
};
}
Expand Down
3 changes: 0 additions & 3 deletions include/UECS/detail/CmptsLocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace Ubpa::UECS {
template<typename Func>
CmptLocator& CmptLocator::Combine() {
CmptLocator funcLocator = Generate<Func>();
lastFrameCmptTypes = SetUnion(lastFrameCmptTypes, funcLocator.lastFrameCmptTypes);
writeCmptTypes = SetUnion(writeCmptTypes, funcLocator.writeCmptTypes);
latestCmptTypes = SetUnion(latestCmptTypes, funcLocator.latestCmptTypes);
cmptTypes = SetUnion(cmptTypes, funcLocator.cmptTypes);
return *this;
}
Expand Down
3 changes: 0 additions & 3 deletions include/UECS/detail/SingletonLocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace Ubpa::UECS {
template<typename Func>
SingletonLocator& SingletonLocator::Combine() {
SingletonLocator funcLocator = Generate<Func>();
lastFrameSingletonTypes = SetUnion(lastFrameSingletonTypes, funcLocator.lastFrameSingletonTypes);
writeSingletonTypes = SetUnion(writeSingletonTypes, funcLocator.writeSingletonTypes);
latestSingletonTypes = SetUnion(latestSingletonTypes, funcLocator.latestSingletonTypes);
singletonTypes = SetUnion(singletonTypes, funcLocator.singletonTypes);
return *this;
}
Expand Down
27 changes: 4 additions & 23 deletions src/core/CmptLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -37,13 +20,11 @@ CmptLocator::CmptLocator()

size_t CmptLocator::GenHashCode() const noexcept {
size_t rst = TypeID<CmptLocator>;
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;
}
57 changes: 38 additions & 19 deletions src/core/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -213,21 +213,40 @@ void Schedule::Clear() {
unordered_map<CmptType, Schedule::CmptSysFuncs> Schedule::GenCmptSysFuncsMap() const {
unordered_map<CmptType, Schedule::CmptSysFuncs> 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;
Expand Down Expand Up @@ -296,7 +315,7 @@ SysFuncGraph Schedule::GenSysFuncGraph() const {
auto cmptSysFuncsMap = GenCmptSysFuncsMap();

// [gen groupMap]
unordered_map<SystemFunc*, detail::Schedule_::NoneGroup> groupMap;
unordered_map<SystemFunc*, detail::NoneGroup> groupMap;
for (const auto& [hashcode, sysFunc] : sysFuncs)
groupMap.emplace(sysFunc, sysFunc);

Expand All @@ -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];
Expand Down
22 changes: 2 additions & 20 deletions src/core/SingletonLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
68 changes: 42 additions & 26 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/test/16_singleton/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class MoverSystem : public System {
};

int main() {
constexpr auto mode = AccessModeOf<const Singleton<Timer>>;
RTDCmptTraits::Instance().Register
<Timer, Velocity, Position>();

Expand Down

0 comments on commit f42e4e3

Please sign in to comment.