Skip to content

Commit b4e55ec

Browse files
committed
Code cleanup
1 parent c9144be commit b4e55ec

14 files changed

Lines changed: 690 additions & 582 deletions

File tree

distr/flecs.c

Lines changed: 313 additions & 284 deletions
Large diffs are not rendered by default.

distr/flecs.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10850,6 +10850,24 @@ void* ecs_field_w_size(
1085010850
size_t size,
1085110851
int8_t index);
1085210852

10853+
/** Get data for a field matched through component inheritance.
10854+
* This operation is like ecs_field_w_size(), but is used when a field is
10855+
* matched on a component that is derived from the queried (base) type. Because
10856+
* the stored (derived) component can be larger than the requested (base) type,
10857+
* the returned pointer is computed using the stride of the stored component. To
10858+
* iterate the values, use the stride returned by ecs_field_stride().
10859+
*
10860+
* @param it The iterator.
10861+
* @param size The size of the field type.
10862+
* @param index The index of the field.
10863+
* @return A pointer to the data of the field.
10864+
*/
10865+
FLECS_API
10866+
void* ecs_base_field_w_size(
10867+
const ecs_iter_t *it,
10868+
size_t size,
10869+
int8_t index);
10870+
1085310871
/** Get data for a field at a specified row.
1085410872
* This operation should be used instead of ecs_field_w_size() for sparse
1085510873
* component fields. This operation should be called for each returned row in a
@@ -12445,6 +12463,13 @@ int ecs_value_move_ctor(
1244512463
#define ecs_field(it, T, index)\
1244612464
(ECS_CAST(T*, ecs_field_w_size(it, sizeof(T), index)))
1244712465

12466+
/** Get field data for a component matched through component inheritance.
12467+
* Use this instead of ecs_field() when a field is matched on a component that
12468+
* is derived from the queried (base) type. The data is iterated with the stride
12469+
* returned by ecs_field_stride(). */
12470+
#define ecs_base_field(it, T, index)\
12471+
(ECS_CAST(T*, ecs_base_field_w_size(it, sizeof(T), index)))
12472+
1244812473
/** Get field data for a self-owned component. */
1244912474
#define ecs_field_self(it, T, index)\
1245012475
(ECS_CAST(T*, ecs_field_self_w_size(it, sizeof(T), index)))
@@ -27870,7 +27895,7 @@ struct iter {
2787027895
}
2787127896

2787227897
return flecs::base_field<A>(
27873-
static_cast<T*>(ecs_field_w_size(iter_, sizeof(A), index)),
27898+
static_cast<T*>(ecs_base_field_w_size(iter_, sizeof(A), index)),
2787427899
ecs_field_stride(iter_, index), count, is_shared);
2787527900
}
2787627901

@@ -31903,7 +31928,7 @@ struct field_ptrs {
3190331928
fields_[index].is_ref = true;
3190431929
fields_[index].index = static_cast<int8_t>(index);
3190531930
} else {
31906-
fields_[index].ptr = ecs_field_w_size(iter, sizeof(A),
31931+
fields_[index].ptr = ecs_base_field_w_size(iter, sizeof(A),
3190731932
static_cast<int8_t>(index));
3190831933
fields_[index].is_ref = iter->sources[index] != 0;
3190931934
}

include/flecs.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5820,6 +5820,24 @@ void* ecs_field_w_size(
58205820
size_t size,
58215821
int8_t index);
58225822

5823+
/** Get data for a field matched through component inheritance.
5824+
* This operation is like ecs_field_w_size(), but is used when a field is
5825+
* matched on a component that is derived from the queried (base) type. Because
5826+
* the stored (derived) component can be larger than the requested (base) type,
5827+
* the returned pointer is computed using the stride of the stored component. To
5828+
* iterate the values, use the stride returned by ecs_field_stride().
5829+
*
5830+
* @param it The iterator.
5831+
* @param size The size of the field type.
5832+
* @param index The index of the field.
5833+
* @return A pointer to the data of the field.
5834+
*/
5835+
FLECS_API
5836+
void* ecs_base_field_w_size(
5837+
const ecs_iter_t *it,
5838+
size_t size,
5839+
int8_t index);
5840+
58235841
/** Get data for a field at a specified row.
58245842
* This operation should be used instead of ecs_field_w_size() for sparse
58255843
* component fields. This operation should be called for each returned row in a

include/flecs/addons/cpp/delegate.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct field_ptrs {
7373
fields_[index].is_ref = true;
7474
fields_[index].index = static_cast<int8_t>(index);
7575
} else {
76-
fields_[index].ptr = ecs_field_w_size(iter, sizeof(A),
76+
fields_[index].ptr = ecs_base_field_w_size(iter, sizeof(A),
7777
static_cast<int8_t>(index));
7878
fields_[index].is_ref = iter->sources[index] != 0;
7979
}

include/flecs/addons/cpp/iter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ struct iter {
554554
}
555555

556556
return flecs::base_field<A>(
557-
static_cast<T*>(ecs_field_w_size(iter_, sizeof(A), index)),
557+
static_cast<T*>(ecs_base_field_w_size(iter_, sizeof(A), index)),
558558
ecs_field_stride(iter_, index), count, is_shared);
559559
}
560560

include/flecs/addons/flecs_c.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,13 @@
727727
#define ecs_field(it, T, index)\
728728
(ECS_CAST(T*, ecs_field_w_size(it, sizeof(T), index)))
729729

730+
/** Get field data for a component matched through component inheritance.
731+
* Use this instead of ecs_field() when a field is matched on a component that
732+
* is derived from the queried (base) type. The data is iterated with the stride
733+
* returned by ecs_field_stride(). */
734+
#define ecs_base_field(it, T, index)\
735+
(ECS_CAST(T*, ecs_base_field_w_size(it, sizeof(T), index)))
736+
730737
/** Get field data for a self-owned component. */
731738
#define ecs_field_self(it, T, index)\
732739
(ECS_CAST(T*, ecs_field_self_w_size(it, sizeof(T), index)))

src/iter.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,43 @@ void* ecs_field_w_size(
123123
return it->ptrs[index];
124124
}
125125

126+
int16_t column = it->columns[index];
127+
if (column >= 0) {
128+
ecs_column_t *col = &it->table->data.columns[column];
129+
ecs_assert((ecs_size_t)size == col->ti->size,
130+
ECS_INVALID_PARAMETER, NULL);
131+
return ECS_ELEM(col->data, (ecs_size_t)size, it->offset);
132+
}
133+
134+
return flecs_field_shared(it, size, index);
135+
error:
136+
return NULL;
137+
}
138+
139+
void* ecs_base_field_w_size(
140+
const ecs_iter_t *it,
141+
size_t size,
142+
int8_t index)
143+
{
144+
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER,
145+
"operation invalid before calling next()");
146+
ecs_check(index >= 0, ECS_INVALID_PARAMETER,
147+
"invalid field index %d", index);
148+
ecs_check(index < it->field_count, ECS_INVALID_PARAMETER,
149+
"field index %d out of bounds", index);
150+
ecs_check(size != 0, ECS_INVALID_PARAMETER,
151+
"missing size for field %d", index);
152+
ecs_check(ecs_field_size(it, index) == size ||
153+
!ecs_field_size(it, index),
154+
ECS_INVALID_PARAMETER,
155+
"mismatching size for field %d (expected '%s')",
156+
index,
157+
flecs_errstr(ecs_id_str(it->world, it->ids[index])));
158+
159+
if (it->ptrs) {
160+
return it->ptrs[index];
161+
}
162+
126163
int16_t column = it->columns[index];
127164
if (column >= 0) {
128165
ecs_column_t *col = &it->table->data.columns[column];

src/query/engine/engine.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,9 @@ ecs_id_t flecs_query_op_get_id(
110110
const ecs_query_run_ctx_t *ctx);
111111

112112
int16_t flecs_query_next_column(
113-
ecs_table_t *table,
114-
ecs_id_t id,
115-
int32_t column);
116-
117-
int16_t flecs_query_next_inherited_column(
118113
const ecs_world_t *world,
119114
ecs_table_t *table,
120-
ecs_id_t base,
115+
ecs_id_t id,
121116
int32_t column);
122117

123118
void flecs_query_it_set_tr(

src/query/engine/eval.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@ bool flecs_query_dispatch(
1717
bool redo,
1818
ecs_query_run_ctx_t *ctx);
1919

20-
static
21-
int16_t flecs_query_next_column_w_inherited(
22-
const ecs_world_t *world,
23-
ecs_table_t *table,
24-
const ecs_table_record_t *tr,
25-
ecs_id_t id,
26-
int32_t column)
27-
{
28-
if (!ecs_id_match(table->type.array[tr->index], tr->hdr.cr->id)) {
29-
return flecs_query_next_inherited_column(world, table, id, column);
30-
}
31-
return flecs_query_next_column(table, id, column);
32-
}
33-
3420
bool flecs_query_select_w_id(
3521
const ecs_query_op_t *op,
3622
bool redo,
@@ -77,8 +63,8 @@ bool flecs_query_select_w_id(
7763
tr = (const ecs_table_record_t*)op_ctx->it.cur;
7864
ecs_assert(tr != NULL, ECS_INTERNAL_ERROR, NULL);
7965
table = tr->hdr.table;
80-
op_ctx->column = flecs_query_next_column_w_inherited(
81-
ctx->world, table, tr, cr->id, op_ctx->column);
66+
op_ctx->column = flecs_query_next_column(
67+
ctx->world, table, cr->id, op_ctx->column);
8268
op_ctx->remaining --;
8369
}
8470

@@ -142,9 +128,8 @@ bool flecs_query_with(
142128
return false;
143129
}
144130

145-
tr = (const ecs_table_record_t*)op_ctx->it.cur;
146-
op_ctx->column = flecs_query_next_column_w_inherited(
147-
ctx->world, table, tr, cr->id, op_ctx->column);
131+
op_ctx->column = flecs_query_next_column(
132+
ctx->world, table, cr->id, op_ctx->column);
148133
ecs_assert(op_ctx->column != -1, ECS_INTERNAL_ERROR, NULL);
149134
}
150135

src/query/engine/eval_utils.c

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -330,104 +330,17 @@ ecs_id_t flecs_query_op_get_id(
330330
}
331331

332332
int16_t flecs_query_next_column(
333+
const ecs_world_t *world,
333334
ecs_table_t *table,
334335
ecs_id_t id,
335336
int32_t column)
336337
{
337-
if (!ECS_IS_PAIR(id) || (ECS_PAIR_FIRST(id) != EcsWildcard)) {
338-
column = column + 1;
339-
} else {
340-
ecs_assert(column >= 0, ECS_INTERNAL_ERROR, NULL);
341-
column = ecs_search_offset(NULL, table, column + 1, id, NULL);
342-
ecs_assert(column != -1, ECS_INTERNAL_ERROR, NULL);
343-
}
338+
ecs_assert(column >= 0, ECS_INTERNAL_ERROR, NULL);
339+
column = ecs_search_offset(world, table, column + 1, id, NULL);
340+
ecs_assert(column != -1, ECS_INTERNAL_ERROR, NULL);
344341
return flecs_ito(int16_t, column);
345342
}
346343

347-
static
348-
bool flecs_component_inherits_from(
349-
const ecs_world_t *world,
350-
ecs_entity_t component,
351-
ecs_entity_t base,
352-
int32_t depth)
353-
{
354-
if (depth >= FLECS_DAG_DEPTH_MAX) {
355-
return false;
356-
}
357-
358-
ecs_record_t *r = flecs_entities_get(world, component);
359-
if (!r) {
360-
return false;
361-
}
362-
363-
ecs_table_t *table = r->table;
364-
if (!table || !(table->flags & EcsTableHasIsA)) {
365-
return false;
366-
}
367-
368-
const ecs_table_record_t *tr_isa = flecs_component_get_table(
369-
world->cr_isa_wildcard, table);
370-
if (!tr_isa) {
371-
return false;
372-
}
373-
374-
ecs_id_t *ids = table->type.array;
375-
int32_t i = tr_isa->index, end = i + tr_isa->count;
376-
for (; i < end; i ++) {
377-
ecs_entity_t b = ecs_pair_second(world, ids[i]);
378-
if (b == base) {
379-
return true;
380-
}
381-
if (flecs_component_inherits_from(world, b, base, depth + 1)) {
382-
return true;
383-
}
384-
}
385-
386-
return false;
387-
}
388-
389-
int16_t flecs_query_next_inherited_column(
390-
const ecs_world_t *world,
391-
ecs_table_t *table,
392-
ecs_id_t base,
393-
int32_t column)
394-
{
395-
ecs_id_t *ids = table->type.array;
396-
int32_t i, count = table->type.count;
397-
398-
if (ECS_IS_PAIR(base)) {
399-
ecs_entity_t base_rel = ecs_pair_first(world, base);
400-
ecs_entity_t tgt = ECS_PAIR_SECOND(base);
401-
bool any_tgt = (tgt == EcsWildcard) || (tgt == EcsAny);
402-
for (i = column + 1; i < count; i ++) {
403-
ecs_id_t id = ids[i];
404-
if (!ECS_IS_PAIR(id)) {
405-
continue;
406-
}
407-
if (!any_tgt && (ECS_PAIR_SECOND(id) != tgt)) {
408-
continue;
409-
}
410-
if (flecs_component_inherits_from(
411-
world, ecs_pair_first(world, id), base_rel, 0))
412-
{
413-
return flecs_ito(int16_t, i);
414-
}
415-
}
416-
} else {
417-
for (i = column + 1; i < count; i ++) {
418-
ecs_id_t id = ids[i];
419-
if (id & ECS_ID_FLAGS_MASK) {
420-
continue;
421-
}
422-
if (flecs_component_inherits_from(world, id, base, 0)) {
423-
return flecs_ito(int16_t, i);
424-
}
425-
}
426-
}
427-
428-
ecs_abort(ECS_INTERNAL_ERROR, NULL);
429-
}
430-
431344
void flecs_query_it_set_tr(
432345
ecs_iter_t *it,
433346
int32_t field_index,

0 commit comments

Comments
 (0)