Skip to content

Commit 4aa83d8

Browse files
committed
test: add unit tests for ArchetypeChunk functionality
1 parent 16c81db commit 4aa83d8

File tree

2 files changed

+409
-26
lines changed

2 files changed

+409
-26
lines changed

include/Freyr/Containers/ArchetypeChunk.hpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
#include "Freyr/Core/TaskManager.hpp"
66
#include "Freyr/Meta/Iteration.hpp"
77

8-
namespace FREYR_NAMESPACE
8+
namespace
9+
FREYR_NAMESPACE
910
{
1011
class Archetype;
1112

1213
class ArchetypeChunk
1314
{
14-
public:
15+
public:
1516
explicit ArchetypeChunk(std::string* internalName,
1617
SparseSet<ComponentEntry>* registeredComponents,
1718
const Ref<FreyrOptions>& freyrOptions,
@@ -77,7 +78,7 @@ namespace FREYR_NAMESPACE
7778
template <typename T>
7879
void RemoveComponent(const Entity entity)
7980
{
80-
GetComponentArray<T>()->RemoveData(entity);
81+
GetComponentArray<T>()->Remove(entity, mRegisteredEntities.lastIndex());
8182
}
8283

8384
template <typename T>
@@ -89,7 +90,7 @@ namespace FREYR_NAMESPACE
8990
template <typename... Ts>
9091
std::tuple<Ts&...> GetComponents(const Entity entity)
9192
{
92-
return std::tuple<Ts&...>(GetComponentArray<Ts>()->GetData(entity)...);
93+
return std::tuple<Ts&...>(GetComponentArray<Ts>()->GetComponent(mRegisteredEntities.getIndex(entity))...);
9394
}
9495

9596
template <typename... Components>
@@ -141,7 +142,7 @@ namespace FREYR_NAMESPACE
141142
"EntityCount",
142143
mRegisteredEntities.size());
143144
auto tuple = std::make_tuple(GetComponentArray<Components>()...);
144-
#if __cpp_lib_parallel_algorithm >= 201603L
145+
#if __cpp_lib_parallel_algorithm >= 201603L
145146

146147
std::for_each(std::execution::par,
147148
mRegisteredEntities.begin(),
@@ -152,26 +153,26 @@ namespace FREYR_NAMESPACE
152153
std::get<ComponentArray<Components>*>(tuple)->GetComponent(
153154
mRegisteredEntities.getIndex(entity))...);
154155
});
155-
#else
156+
#else
156157

157158
std::for_each(mRegisteredEntities.begin(), mRegisteredEntities.end(), [&](const auto& entity) {
158159
function(entity,
159160
index + mRegisteredEntities.getIndex(entity),
160161
std::get<ComponentArray<Components>*>(tuple)->GetComponent(
161162
mRegisteredEntities.getIndex(entity))...);
162163
});
163-
#endif
164+
#endif
164165
FREYR_PROFILING_END("FREYR", perfetto::Track((uint64_t) this));
165166
}
166167

167168
template <typename... Components>
168-
void Map(auto&& mapFunction,
169-
Entity index,
170-
std::vector<decltype(mapFunction(*(new Entity {}), *(new Components {})...))>& buffer)
169+
void Map(auto&& mapFunction,
170+
Entity index,
171+
std::vector<decltype(mapFunction(*(new Entity{}), *(new Components{})...))>& buffer)
171172
{
172173
auto tuple = std::make_tuple(GetComponentArray<Components>()...);
173174

174-
#if __cpp_lib_parallel_algorithm >= 201603L
175+
#if __cpp_lib_parallel_algorithm >= 201603L
175176
std::for_each(std::execution::par,
176177
mRegisteredEntities.begin(),
177178
mRegisteredEntities.end(),
@@ -181,14 +182,14 @@ namespace FREYR_NAMESPACE
181182
std::get<ComponentArray<Components>*>(tuple)->GetComponent(
182183
mRegisteredEntities.getIndex(entity))...);
183184
});
184-
#else
185+
#else
185186
std::for_each(mRegisteredEntities.begin(), mRegisteredEntities.end(), [&](const auto& entity) {
186187
buffer[index + mRegisteredEntities.getIndex(entity)] =
187188
mapFunction(entity,
188189
std::get<ComponentArray<Components>*>(tuple)->GetComponent(
189190
mRegisteredEntities.getIndex(entity))...);
190191
});
191-
#endif
192+
#endif
192193
}
193194

194195
template <typename... Components>
@@ -206,21 +207,21 @@ namespace FREYR_NAMESPACE
206207
entities.size());
207208
auto tuple = std::make_tuple(GetComponentArray<Components>()...);
208209

209-
#if __cpp_lib_execution >= 201603L
210+
#if __cpp_lib_execution >= 201603L
210211
std::for_each(std::execution::seq, entities.begin(), entities.end(), [&](const auto& entity) {
211212
if (!mRegisteredEntities.contains(entity))
212213
return;
213214
function(entity,
214215
std::get<ComponentArray<Components>*>(tuple)->GetComponent(
215216
mRegisteredEntities.getIndex(entity))...);
216217
});
217-
#else
218+
#else
218219
std::for_each(entities.begin(), entities.end(), [&](const auto& entity) {
219220
if (!mRegisteredEntities.contains(entity))
220221
return;
221222
function(entity, std::get<ComponentArray<Components>*>(tuple)->GetComponent(entity)...);
222223
});
223-
#endif
224+
#endif
224225
FREYR_PROFILING_END("FREYR", perfetto::Track((uint64_t) this));
225226
}
226227

@@ -242,21 +243,21 @@ namespace FREYR_NAMESPACE
242243
"EntityCount",
243244
entities.size());
244245

245-
#if __cpp_lib_parallel_algorithm >= 201603L
246+
#if __cpp_lib_parallel_algorithm >= 201603L
246247
std::for_each(std::execution::par, entities.begin(), entities.end(), [&](const auto& entity) {
247248
if (!mRegisteredEntities.contains(entity))
248249
return;
249250

250-
function(entity, GetComponentArray<Components>()->GetData(entity)...);
251+
function(entity, GetComponentArray<Components>()->GetComponent(entity)...);
251252
});
252-
#else
253+
#else
253254
std::for_each(entities.begin(), entities.end(), [&](const auto& entity) {
254255
if (!mRegisteredEntities.contains(entity))
255256
return;
256257

257-
function(entity, GetComponentArray<Components>()->GetData(entity)...);
258+
function(entity, GetComponentArray<Components>()->GetComponent(entity)...);
258259
});
259-
#endif
260+
#endif
260261
FREYR_PROFILING_END("FREYR", perfetto::Track((uint64_t) this));
261262
}
262263

@@ -288,9 +289,9 @@ namespace FREYR_NAMESPACE
288289

289290
void Swap(const Entity a, const Entity b)
290291
{
291-
for (auto component : *mRegisteredComponents)
292+
for (const auto componentArray : mComponentArrays)
292293
{
293-
mComponentArrays[component]->Swap(a, b);
294+
componentArray->Swap(mRegisteredEntities.getIndex(a), mRegisteredEntities.getIndex(b));
294295
}
295296

296297
mRegisteredEntities.swap(a, b);
@@ -327,7 +328,7 @@ namespace FREYR_NAMESPACE
327328
}
328329
}
329330

330-
protected:
331+
protected:
331332
void InternalRemoveEntity(Entity entity)
332333
{
333334
for (const auto componentArray : mComponentArrays)
@@ -359,7 +360,7 @@ namespace FREYR_NAMESPACE
359360
NextTask();
360361
}
361362

362-
private:
363+
private:
363364
friend class Archetype;
364365

365366
Ref<FreyrOptions> mFreyrOptions;
@@ -375,4 +376,4 @@ namespace FREYR_NAMESPACE
375376
std::string* mInternalName;
376377
SparseSet<IComponentArray*> mComponentArrays;
377378
};
378-
} // namespace FREYR_NAMESPACE
379+
} // namespace FREYR_NAMESPACE

0 commit comments

Comments
 (0)