Skip to content

Commit 59be1d2

Browse files
committed
Tweaked: Pass esc iterators by reference
1 parent b421fe9 commit 59be1d2

File tree

8 files changed

+184
-184
lines changed

8 files changed

+184
-184
lines changed

README.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ const bool hasWheel = w.has(car, wheel);
304304
305305
// Check if entities hidden behind the iterator have Velocity (via iterator).
306306
ecs::Query q = w.query().any<Position, Velocity>();
307-
q.each([&](ecs::Iter iter) {
308-
const bool hasPosition = iter.has<Position>();
309-
const bool hasVelocity = iter.has<Velocity>();
307+
q.each([&](ecs::Iter& it) {
308+
const bool hasPosition = it.has<Position>();
309+
const bool hasVelocity = it.has<Velocity>();
310310
...
311311
});
312312
```
@@ -319,9 +319,9 @@ auto v = w.add<Velocity>().entity;
319319

320320
// Check if entities hidden behind the iterator have Velocity (via iterator).
321321
ecs::Query q = w.query().any(p).any(v);
322-
q.each([&](ecs::Iter iter) {
323-
const bool hasPosition = iter.has(p);
324-
const bool hasVelocity = iter.has(v);
322+
q.each([&](ecs::Iter& it) {
323+
const bool hasPosition = it.has(p);
324+
const bool hasVelocity = it.has(v);
325325
...
326326
});
327327
```
@@ -622,33 +622,33 @@ There are three types of iterators:
622622
ecs::Query q = w.query();
623623
q.all<Position&, Velocity>();
624624

625-
q.each([](ecs::IterAll iter) {
626-
auto p = iter.view_mut<Position>(); // Read-write access to Position
627-
auto v = iter.view<Velocity>(); // Read-only access to Velocity
625+
q.each([](ecs::IterAll& it) {
626+
auto p = it.view_mut<Position>(); // Read-write access to Position
627+
auto v = it.view<Velocity>(); // Read-only access to Velocity
628628

629629
// Iterate over all enabled entities and update their x-axis position
630-
GAIA_EACH(iter) {
631-
if (!iter.enabled(i))
630+
GAIA_EACH(it) {
631+
if (!it.enabled(i))
632632
return;
633633
p[i].x += 1.f;
634634
}
635635

636636
// Iterate over all entities and update their position based on their velocity
637-
GAIA_EACH(iter) {
637+
GAIA_EACH(it) {
638638
p[i].x += v[i].x * dt;
639639
p[i].y += v[i].y * dt;
640640
p[i].z += v[i].z * dt;
641641
}
642642
});
643643
```
644644

645-
***GAIA_EACH(iter)*** is simply a shortcut for
645+
***GAIA_EACH(it)*** is simply a shortcut for
646646
```
647-
for (uint32_t i=0; i<iter.size(); ++i)
647+
for (uint32_t i=0; i<it.size(); ++i)
648648
```
649649
A similar macro exists where you can specify the variable name. It is called ***GAIA_EACH_(iter,k)***
650650
```
651-
for (uint32_t k=0; k<iter.size(); ++k)
651+
for (uint32_t k=0; k<it.size(); ++k)
652652
```
653653

654654
### Constraints
@@ -685,21 +685,21 @@ q.arr(entities, ecs::Query::Constraint::AcceptAll);
685685
// Fills the array with only e1 because e1 is disabled.
686686
q.arr(entities, ecs::Query::Constraint::DisabledOnly);
687687

688-
q.each([](ecs::Iter iter) {
689-
auto p = iter.view_mut<Position>(); // Read-Write access to Position
688+
q.each([](ecs::Iter& it) {
689+
auto p = it.view_mut<Position>(); // Read-Write access to Position
690690
// Iterates over enabled entities
691-
GAIA_EACH(iter) p[i] = {}; // reset the position of each enabled entity
691+
GAIA_EACH(it) p[i] = {}; // reset the position of each enabled entity
692692
});
693-
q.each([](ecs::IterDisabled iter) {
694-
auto p = iter.view_mut<Position>(); // Read-Write access to Position
693+
q.each([](ecs::IterDisabled& it) {
694+
auto p = it.view_mut<Position>(); // Read-Write access to Position
695695
// Iterates over disabled entities
696-
GAIA_EACH(iter) p[i] = {}; // reset the position of each disabled entity
696+
GAIA_EACH(it) p[i] = {}; // reset the position of each disabled entity
697697
});
698-
q.each([](ecs::IterAll iter) {
699-
auto p = iter.view_mut<Position>(); // Read-Write access to Position
698+
q.each([](ecs::IterAll& it) {
699+
auto p = it.view_mut<Position>(); // Read-Write access to Position
700700
// Iterates over all entities
701-
GAIA_EACH(iter) {
702-
if (iter.enabled(i)) {
701+
GAIA_EACH(it) {
702+
if (it.enabled(i)) {
703703
p[i] = {}; // reset the position of each enabled entity
704704
}
705705
}
@@ -716,7 +716,7 @@ struct Disabled {};
716716
e.add<Disabled>(); // disable entity
717717

718718
ecs::Query q = w.query().all<Position, Disabled>;
719-
q.each([&](ecs::Iter iter){
719+
q.each([&](ecs::Iter& it){
720720
// Processes all disabled entities
721721
});
722722

@@ -1118,25 +1118,25 @@ struct VelocitySoA {
11181118
...
11191119

11201120
ecs::Query q = w.query().all<PositionSoA&, VelocitySoA>;
1121-
q.each([](ecs::Iter iter) {
1121+
q.each([](ecs::Iter& it) {
11221122
// Position
1123-
auto vp = iter.view_mut<PositionSoA>(); // read-write access to PositionSoA
1123+
auto vp = it.view_mut<PositionSoA>(); // read-write access to PositionSoA
11241124
auto px = vp.set<0>(); // continuous block of "x" from PositionSoA
11251125
auto py = vp.set<1>(); // continuous block of "y" from PositionSoA
11261126
auto pz = vp.set<2>(); // continuous block of "z" from PositionSoA
11271127

11281128
// Velocity
1129-
auto vv = iter.view<VelocitySoA>(); // read-only access to VelocitySoA
1129+
auto vv = it.view<VelocitySoA>(); // read-only access to VelocitySoA
11301130
auto vx = vv.get<0>(); // continuous block of "x" from VelocitySoA
11311131
auto vy = vv.get<1>(); // continuous block of "y" from VelocitySoA
11321132
auto vz = vv.get<2>(); // continuous block of "z" from VelocitySoA
11331133

11341134
// Handle x coordinates
1135-
GAIA_EACH(iter) px[i] += vx[i] * dt;
1135+
GAIA_EACH(it) px[i] += vx[i] * dt;
11361136
// Handle y coordinates
1137-
GAIA_EACH(iter) py[i] += vy[i] * dt;
1137+
GAIA_EACH(it) py[i] += vy[i] * dt;
11381138
// Handle z coordinates
1139-
GAIA_EACH(iter) pz[i] += vz[i] * dt;
1139+
GAIA_EACH(it) pz[i] += vz[i] * dt;
11401140

11411141
/*
11421142
You can even use SIMD intrinsics now without a worry.
@@ -1146,14 +1146,14 @@ q.each([](ecs::Iter iter) {
11461146
The code bellow uses x86 SIMD intrinsics.
11471147
uint32_t i = 0;
11481148
// Process SSE-sized blocks first
1149-
for (; i < iter.size(); i+=4) {
1149+
for (; i < it.size(); i+=4) {
11501150
const auto pVec = _mm_load_ps(px.data() + i);
11511151
const auto vVec = _mm_load_ps(vx.data() + i);
11521152
const auto respVec = _mm_fmadd_ps(vVec, dtVec, pVec);
11531153
_mm_store_ps(px.data() + i, respVec);
11541154
}
11551155
// Process the rest of the elements
1156-
for (; i < iter.size(); ++i) {
1156+
for (; i < it.size(); ++i) {
11571157
...
11581158
}
11591159
*/

include/gaia/ecs/query.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ namespace gaia {
447447

448448
ChunkSpanMut chunkSpan((Chunk**)&chunks[chunkOffset], batchSize);
449449
for (auto* pChunk: chunkSpan) {
450-
Iter iter(*pChunk);
451-
if (iter.size() == 0)
450+
Iter it(*pChunk);
451+
if (it.size() == 0)
452452
continue;
453453

454454
if constexpr (HasFilters) {
@@ -493,24 +493,24 @@ namespace gaia {
493493
GAIA_FORCEINLINE void
494494
run_query_on_chunk(Chunk& chunk, Func func, [[maybe_unused]] core::func_type_list<T...> types) {
495495
if constexpr (sizeof...(T) > 0) {
496-
Iter iter(chunk);
496+
Iter it(chunk);
497497

498498
// Pointers to the respective component types in the chunk, e.g
499499
// q.each([&](Position& p, const Velocity& v) {...}
500500
// Translates to:
501-
// auto p = iter.view_mut_inter<Position, true>();
502-
// auto v = iter.view_inter<Velocity>();
503-
auto dataPointerTuple = std::make_tuple(iter.template view_auto<T>()...);
501+
// auto p = it.view_mut_inter<Position, true>();
502+
// auto v = it.view_inter<Velocity>();
503+
auto dataPointerTuple = std::make_tuple(it.template view_auto<T>()...);
504504

505505
// Iterate over each entity in the chunk.
506506
// Translates to:
507-
// GAIA_EACH(iter) func(p[i], v[i]);
507+
// GAIA_EACH(it) func(p[i], v[i]);
508508

509-
GAIA_EACH(iter) func(std::get<decltype(iter.template view_auto<T>())>(dataPointerTuple)[i]...);
509+
GAIA_EACH(it) func(std::get<decltype(it.template view_auto<T>())>(dataPointerTuple)[i]...);
510510
} else {
511511
// No functor parameters. Do an empty loop.
512-
Iter iter(chunk);
513-
GAIA_EACH(iter) func();
512+
Iter it(chunk);
513+
GAIA_EACH(it) func();
514514
}
515515
}
516516

@@ -529,11 +529,11 @@ namespace gaia {
529529

530530
const auto& chunks = pArchetype->chunks();
531531
const bool isNotEmpty = core::has_if(chunks, [&](Chunk* pChunk) {
532-
Iter iter(*pChunk);
532+
Iter it(*pChunk);
533533
if constexpr (UseFilters)
534-
return iter.size() > 0 && match_filters(*pChunk, queryInfo);
534+
return it.size() > 0 && match_filters(*pChunk, queryInfo);
535535
else
536-
return iter.size() > 0;
536+
return it.size() > 0;
537537
});
538538

539539
if (isNotEmpty)
@@ -555,8 +555,8 @@ namespace gaia {
555555

556556
const auto& chunks = pArchetype->chunks();
557557
for (auto* pChunk: chunks) {
558-
Iter iter(*pChunk);
559-
const auto entityCnt = iter.size();
558+
Iter it(*pChunk);
559+
const auto entityCnt = it.size();
560560
if (entityCnt == 0)
561561
continue;
562562

@@ -586,8 +586,8 @@ namespace gaia {
586586

587587
const auto& chunks = pArchetype->chunks();
588588
for (auto* pChunk: chunks) {
589-
Iter iter(*pChunk);
590-
if (iter.size() == 0)
589+
Iter it(*pChunk);
590+
if (it.size() == 0)
591591
continue;
592592

593593
// Filters
@@ -596,8 +596,8 @@ namespace gaia {
596596
continue;
597597
}
598598

599-
const auto dataView = iter.template view<ContainerItemType>();
600-
GAIA_EACH(iter) outArray.push_back(dataView[i]);
599+
const auto dataView = it.template view<ContainerItemType>();
600+
GAIA_EACH(it) outArray.push_back(dataView[i]);
601601
}
602602
}
603603
}

single_include/gaia.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20747,8 +20747,8 @@ namespace gaia {
2074720747

2074820748
ChunkSpanMut chunkSpan((Chunk**)&chunks[chunkOffset], batchSize);
2074920749
for (auto* pChunk: chunkSpan) {
20750-
Iter iter(*pChunk);
20751-
if (iter.size() == 0)
20750+
Iter it(*pChunk);
20751+
if (it.size() == 0)
2075220752
continue;
2075320753

2075420754
if constexpr (HasFilters) {
@@ -20793,24 +20793,24 @@ namespace gaia {
2079320793
GAIA_FORCEINLINE void
2079420794
run_query_on_chunk(Chunk& chunk, Func func, [[maybe_unused]] core::func_type_list<T...> types) {
2079520795
if constexpr (sizeof...(T) > 0) {
20796-
Iter iter(chunk);
20796+
Iter it(chunk);
2079720797

2079820798
// Pointers to the respective component types in the chunk, e.g
2079920799
// q.each([&](Position& p, const Velocity& v) {...}
2080020800
// Translates to:
20801-
// auto p = iter.view_mut_inter<Position, true>();
20802-
// auto v = iter.view_inter<Velocity>();
20803-
auto dataPointerTuple = std::make_tuple(iter.template view_auto<T>()...);
20801+
// auto p = it.view_mut_inter<Position, true>();
20802+
// auto v = it.view_inter<Velocity>();
20803+
auto dataPointerTuple = std::make_tuple(it.template view_auto<T>()...);
2080420804

2080520805
// Iterate over each entity in the chunk.
2080620806
// Translates to:
20807-
// GAIA_EACH(iter) func(p[i], v[i]);
20807+
// GAIA_EACH(it) func(p[i], v[i]);
2080820808

20809-
GAIA_EACH(iter) func(std::get<decltype(iter.template view_auto<T>())>(dataPointerTuple)[i]...);
20809+
GAIA_EACH(it) func(std::get<decltype(it.template view_auto<T>())>(dataPointerTuple)[i]...);
2081020810
} else {
2081120811
// No functor parameters. Do an empty loop.
20812-
Iter iter(chunk);
20813-
GAIA_EACH(iter) func();
20812+
Iter it(chunk);
20813+
GAIA_EACH(it) func();
2081420814
}
2081520815
}
2081620816

@@ -20829,11 +20829,11 @@ namespace gaia {
2082920829

2083020830
const auto& chunks = pArchetype->chunks();
2083120831
const bool isNotEmpty = core::has_if(chunks, [&](Chunk* pChunk) {
20832-
Iter iter(*pChunk);
20832+
Iter it(*pChunk);
2083320833
if constexpr (UseFilters)
20834-
return iter.size() > 0 && match_filters(*pChunk, queryInfo);
20834+
return it.size() > 0 && match_filters(*pChunk, queryInfo);
2083520835
else
20836-
return iter.size() > 0;
20836+
return it.size() > 0;
2083720837
});
2083820838

2083920839
if (isNotEmpty)
@@ -20855,8 +20855,8 @@ namespace gaia {
2085520855

2085620856
const auto& chunks = pArchetype->chunks();
2085720857
for (auto* pChunk: chunks) {
20858-
Iter iter(*pChunk);
20859-
const auto entityCnt = iter.size();
20858+
Iter it(*pChunk);
20859+
const auto entityCnt = it.size();
2086020860
if (entityCnt == 0)
2086120861
continue;
2086220862

@@ -20886,8 +20886,8 @@ namespace gaia {
2088620886

2088720887
const auto& chunks = pArchetype->chunks();
2088820888
for (auto* pChunk: chunks) {
20889-
Iter iter(*pChunk);
20890-
if (iter.size() == 0)
20889+
Iter it(*pChunk);
20890+
if (it.size() == 0)
2089120891
continue;
2089220892

2089320893
// Filters
@@ -20896,8 +20896,8 @@ namespace gaia {
2089620896
continue;
2089720897
}
2089820898

20899-
const auto dataView = iter.template view<ContainerItemType>();
20900-
GAIA_EACH(iter) outArray.push_back(dataView[i]);
20899+
const auto dataView = it.template view<ContainerItemType>();
20900+
GAIA_EACH(it) outArray.push_back(dataView[i]);
2090120901
}
2090220902
}
2090320903
}

0 commit comments

Comments
 (0)