Skip to content

Commit

Permalink
span : API with const CmptType* types, size_t num
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Nov 12, 2020
1 parent a05e64b commit 08a1d19
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 113 deletions.
4 changes: 3 additions & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

- 0.14.3: `CmptsView`, `SingletonsView` use `Span`
- 0.14.3
- `CmptsView`, `SingletonsView` use `Span`
- API with `const CmptType* types, size_t num` use `Span<CmptType> types` as instead
- 0.14.2
- add `RandomAccessor` for random access other entity's component
- `SystemFunc` add the member
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/CmptLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Ubpa::UECS {

bool HasWriteCmptType() const noexcept;
private:
size_t GenHashCode() const noexcept;
void UpdateHashCode() noexcept;

CmptAccessTypeSet cmptTypes;

Expand Down
12 changes: 6 additions & 6 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Ubpa::UECS {
std::tuple<Entity, Cmpts*...> Create();

// use RTDCmptTraits
Entity Create(const CmptType* types, size_t num);
Entity Create(Span<const CmptType> types);

Entity Instantiate(Entity);

Expand All @@ -45,15 +45,15 @@ namespace Ubpa::UECS {
std::tuple<Cmpts*...> Attach(Entity);

// use RTDCmptTraits
void Attach(Entity, const CmptType* types, size_t num);
void Attach(Entity, Span<const CmptType> types);

template<typename Cmpt, typename... Args>
Cmpt* Emplace(Entity, Args&&...);

// use Detach(Entity, const CmptType*, size_t)
template<typename... Cmpts>
void Detach(Entity);
void Detach(Entity, const CmptType* types, size_t num);
void Detach(Entity, Span<const CmptType> types);

// use Have(Entity, CmptType)
template<typename Cmpt>
Expand Down Expand Up @@ -99,17 +99,17 @@ namespace Ubpa::UECS {
friend class World;
friend class Archetype;

static bool IsSet(const CmptType* types, size_t num) noexcept;
static bool IsSet(Span<const CmptType> types) noexcept;

template<typename... Cmpts>
Archetype* GetOrCreateArchetypeOf();
// types not contain Entity
Archetype* GetOrCreateArchetypeOf(const CmptType* types, size_t num);
Archetype* GetOrCreateArchetypeOf(Span<const CmptType> types);

// return original archetype
template<typename... Cmpts>
Archetype* AttachWithoutInit(Entity);
Archetype* AttachWithoutInit(Entity, const CmptType* types, size_t num);
Archetype* AttachWithoutInit(Entity, Span<const CmptType> types);

const std::set<Archetype*>& QueryArchetypes(const EntityQuery&) const;
mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;
Expand Down
6 changes: 1 addition & 5 deletions include/UECS/EntityQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

namespace Ubpa::UECS {
// ArchetypeFilter + CmptLocator
class EntityQuery {
public:
struct EntityQuery {
ArchetypeFilter filter;
CmptLocator locator;

EntityQuery(ArchetypeFilter filter = {}, CmptLocator locator = {})
: filter{ std::move(filter) }, locator{ std::move(locator) } {}

size_t HashCode() const noexcept { return hash_combine(filter.HashCode(), locator.HashCode()); }

bool operator==(const EntityQuery& query) const noexcept {
Expand Down
4 changes: 3 additions & 1 deletion include/UECS/SingletonLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include "CmptType.h"

#include <UContainer/Span.h>

#include <set>

namespace Ubpa::UECS {
class SingletonLocator {
public:
SingletonLocator(const CmptAccessType* types, size_t num);
SingletonLocator(Span<const CmptAccessType> types);
SingletonLocator() = default;

template<typename Func>
Expand Down
13 changes: 5 additions & 8 deletions include/UECS/detail/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include <UContainer/Pool.h>

#include <UTemplate/Typelist.h>
#include <UTemplate/TypeID.h>

#include <map>

namespace Ubpa::UECS {
class EntityMngr;
Expand All @@ -36,15 +33,15 @@ namespace Ubpa::UECS {
~Archetype();

// auto add Entity
static Archetype* New(RTDCmptTraits&, Pool<Chunk>* chunkPool, const CmptType* types, size_t num);
static Archetype* New(RTDCmptTraits&, Pool<Chunk>* chunkPool, Span<const CmptType> types);

// auto add Entity
template<typename... Cmpts>
static Archetype* Add(const Archetype* from);
static Archetype* Add(RTDCmptTraits&, const Archetype* from, const CmptType* types, size_t num);
static Archetype* Add(RTDCmptTraits&, const Archetype* from, Span<const CmptType> types);

// auto add Entity
static Archetype* Remove(const Archetype* from, const CmptType* types, size_t num);
static Archetype* Remove(const Archetype* from, Span<const CmptType> types);

// Entity + Components
std::tuple<std::vector<Entity*>, std::vector<std::vector<CmptAccessPtr>>, std::vector<size_t>>
Expand Down Expand Up @@ -94,7 +91,7 @@ namespace Ubpa::UECS {
size_t ChunkCapacity() const noexcept { return chunkCapacity; }

// add Entity
static CmptTypeSet GenCmptTypeSet(const CmptType* types, size_t num);
static CmptTypeSet GenCmptTypeSet(Span<const CmptType> types);
template<typename... Cmpts>
static CmptTypeSet GenCmptTypeSet();

Expand All @@ -104,7 +101,7 @@ namespace Ubpa::UECS {
void SetLayout();

size_t Offsetof(CmptType type) const { return type2offset.at(type); }
static bool NotContainEntity(const CmptType* types, size_t num) noexcept;
static bool NotContainEntity(Span<const CmptType> types) noexcept;

friend class EntityMngr;

Expand Down
4 changes: 2 additions & 2 deletions include/UECS/detail/Archetype.inl
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ namespace Ubpa::UECS {
"<Cmpts>... must be different");

constexpr std::array types = { CmptType::Of<Cmpts>... };
return GenCmptTypeSet(types.data(), types.size());
return GenCmptTypeSet(types);
}
else
return GenCmptTypeSet(nullptr, 0);
return GenCmptTypeSet({});
}
}
1 change: 0 additions & 1 deletion include/UECS/detail/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <cstdint>
#include <array>
#include <tuple>
#include <vector>

#include "../config.h"
Expand Down
12 changes: 5 additions & 7 deletions include/UECS/detail/CmptTypeSet.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include "Util.h"
#include "../EntityQuery.h"

#include <UTemplate/TypeID.h>
#include <UTemplate/TemplateList.h>
#include <UContainer/Span.h>

#include <set>

Expand All @@ -14,15 +12,15 @@ namespace Ubpa::UECS {

size_t HashCode() const noexcept;

void Insert(const CmptType* types, size_t num);
void Erase(const CmptType* types, size_t num) noexcept;
void Insert(Span<const CmptType> types);
void Erase(Span<const CmptType> types) noexcept;
bool Contains(CmptType type) const;

bool ContainsAll(const CmptType* types, size_t num) const;
bool ContainsAll(Span<const CmptType> types) const;
template<typename CmptTypeContainer>
bool ContainsAll(const CmptTypeContainer& types) const;

bool ContainsAny(const CmptType* types, size_t num) const;
bool ContainsAny(Span<const CmptType> types) const;
template<typename CmptTypeContainer>
bool ContainsAny(const CmptTypeContainer& types) const;

Expand Down
28 changes: 12 additions & 16 deletions include/UECS/detail/CmptTypeSet.inl
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
#pragma once

namespace Ubpa::UECS {
inline void CmptTypeSet::Insert(const CmptType* types, size_t num) {
assert(types || num == 0);
for (size_t i = 0; i < num; i++)
data.insert(types[i]);
inline void CmptTypeSet::Insert(Span<const CmptType> types) {
for(const auto& type : types)
data.insert(type);
}

inline void CmptTypeSet::Erase(const CmptType* types, size_t num) noexcept {
assert(types || num == 0);
for (size_t i = 0; i < num; i++)
data.erase(types[i]);
inline void CmptTypeSet::Erase(Span<const CmptType> types) noexcept {
for (const auto& type : types)
data.erase(type);
}

inline bool CmptTypeSet::Contains(CmptType type) const {
return data.find(type) != data.end();
}

inline bool CmptTypeSet::ContainsAll(const CmptType* types, size_t num) const {
assert(types || num == 0);
for (size_t i = 0; i < num; i++) {
if (!Contains(types[i]))
inline bool CmptTypeSet::ContainsAll(Span<const CmptType> types) const {
for (const auto& type : types) {
if (Contains(type))
return false;
}
return true;
Expand All @@ -35,10 +32,9 @@ namespace Ubpa::UECS {
return true;
}

inline bool CmptTypeSet::ContainsAny(const CmptType* types, size_t num) const {
assert(types || num == 0);
for (size_t i = 0; i < num; i++) {
if (Contains(types[i]))
inline bool CmptTypeSet::ContainsAny(Span<const CmptType> types) const {
for (const auto& type : types) {
if (!Contains(type))
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions include/UECS/detail/CmptsLocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Ubpa::UECS {
CmptLocator& CmptLocator::Combine() {
CmptLocator funcLocator = Generate<Func>();
cmptTypes = SetUnion(cmptTypes, funcLocator.cmptTypes);
UpdateHashCode();
return *this;
}
}
2 changes: 1 addition & 1 deletion include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace Ubpa::UECS {
template<typename... Cmpts>
void EntityMngr::Detach(Entity e) {
constexpr std::array types{ CmptType::Of<Cmpts>... };
Detach(e, types.data, types.size());
Detach(e, types);
}

template<typename Cmpt>
Expand Down
4 changes: 2 additions & 2 deletions include/UECS/detail/SingletonLocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace Ubpa::UECS::detail {
SingletonLocator GenerateSingletonLocator(TypeList<Singletons...>) {
if constexpr (sizeof...(Singletons) > 0) {
constexpr std::array types{ CmptAccessType::Of<Singletons>... };
return SingletonLocator{ types.data(), types.size() };
return SingletonLocator{ types };
}
else
return SingletonLocator{};
return SingletonLocator{{}};
}
}

Expand Down
47 changes: 23 additions & 24 deletions src/core/Archetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,47 +82,47 @@ void Archetype::SetLayout() {
type2offset.emplace(type, layout.offsets[i++]);
}

Archetype* Archetype::New(RTDCmptTraits& rtdCmptTraits, Pool<Chunk>* chunkPool, const CmptType* types, size_t num) {
assert(NotContainEntity(types, num));
Archetype* Archetype::New(RTDCmptTraits& rtdCmptTraits, Pool<Chunk>* chunkPool, Span<const CmptType> types) {
assert(NotContainEntity(types));

auto* rst = new Archetype{ chunkPool };
rst->types.Insert(types, num);
rst->types.Insert(types);
rst->types.data.insert(CmptType::Of<Entity>);
rst->cmptTraits.Register<Entity>();
for (size_t i = 0; i < num; i++)
rst->cmptTraits.Register(rtdCmptTraits, types[i]);
for(const auto& type : types)
rst->cmptTraits.Register(rtdCmptTraits, type);
rst->SetLayout();
return rst;
}

Archetype* Archetype::Add(RTDCmptTraits& rtdCmptTraits, const Archetype* from, const CmptType* types, size_t num) {
assert(NotContainEntity(types, num));
assert(!from->types.ContainsAll(types, num));
Archetype* Archetype::Add(RTDCmptTraits& rtdCmptTraits, const Archetype* from, Span<const CmptType> types) {
assert(NotContainEntity(types));
assert(!from->types.ContainsAll(types));

auto* rst = new Archetype{ from->chunkPool };

rst->types = from->types;
rst->cmptTraits = from->cmptTraits;
rst->types.Insert(types, num);
for (size_t i = 0; i < num; i++)
rst->cmptTraits.Register(rtdCmptTraits, types[i]);
rst->types.Insert(types);
for (const auto& type : types)
rst->cmptTraits.Register(rtdCmptTraits, type);

rst->SetLayout();

return rst;
}

Archetype* Archetype::Remove(const Archetype* from, const CmptType* types, size_t num) {
assert(NotContainEntity(types, num));
assert(from->types.ContainsAny(types, num));
Archetype* Archetype::Remove(const Archetype* from, Span<const CmptType> types) {
assert(NotContainEntity(types));
assert(from->types.ContainsAny(types));

auto* rst = new Archetype{ from->chunkPool };

rst->types = from->types;
rst->cmptTraits = from->cmptTraits;
rst->types.Erase(types, num);
for (size_t i = 0; i < num; i++)
rst->cmptTraits.Deregister(types[i]);
rst->types.Erase(types);
for (const auto& type : types)
rst->cmptTraits.Deregister(type);

rst->SetLayout();

Expand Down Expand Up @@ -309,18 +309,17 @@ size_t Archetype::EntityNumOfChunk(size_t chunkIdx) const noexcept {
return chunkCapacity;
}

CmptTypeSet Archetype::GenCmptTypeSet(const CmptType* types, size_t num) {
assert(NotContainEntity(types, num));
CmptTypeSet Archetype::GenCmptTypeSet(Span<const CmptType> types) {
assert(NotContainEntity(types));
CmptTypeSet typeset;
typeset.Insert(types, num);
typeset.Insert(types);
typeset.data.insert(CmptType::Of<Entity>);
return typeset;
}

bool Archetype::NotContainEntity(const CmptType* types, size_t num) noexcept {
assert(types || num == 0);
for (size_t i = 0; i < num; i++) {
if (types[i].Is<Entity>())
bool Archetype::NotContainEntity(Span<const CmptType> types) noexcept {
for (const auto& type : types) {
if (type.Is<Entity>())
return false;
}
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/core/CmptLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ CmptLocator::CmptLocator(const CmptAccessType* types, size_t num) {
for (size_t i = 0; i < num; i++)
cmptTypes.insert(types[i]);

hashCode = GenHashCode();
UpdateHashCode();
}

CmptLocator::CmptLocator()
: hashCode{ TypeID<CmptLocator> } {}

size_t CmptLocator::GenHashCode() const noexcept {
void CmptLocator::UpdateHashCode() noexcept {
size_t rst = TypeID<CmptLocator>;
for (const auto& type : cmptTypes)
rst = hash_combine(rst, type.HashCode());
return rst;
hashCode = rst;
}

bool CmptLocator::HasWriteCmptType() const noexcept {
Expand Down
Loading

0 comments on commit 08a1d19

Please sign in to comment.