Skip to content

Commit

Permalink
#1375 Yield wildcard records for any terms
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens authored Sep 26, 2024
1 parent 4db1bc1 commit dd39321
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 48 deletions.
46 changes: 23 additions & 23 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13849,7 +13849,8 @@ void flecs_emit(
if (count) {
storage_i = tr->column;
bool is_sparse = idr->flags & EcsIdIsSparse;
if (storage_i != -1 || is_sparse) {

if (!ecs_id_is_wildcard(id) && (storage_i != -1 || is_sparse)) {
void *ptr;
ecs_size_t size = idr->type_info->size;

Expand Down Expand Up @@ -36230,7 +36231,7 @@ void flecs_table_init_columns(
int16_t *s2t = &table->column_map[ids_count];

for (i = 0; i < ids_count; i ++) {
ecs_id_t id = table->type.array[i];
ecs_id_t id = ids[i];
ecs_table_record_t *tr = &records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
const ecs_type_info_t *ti = idr->type_info;
Expand All @@ -36250,17 +36251,21 @@ void flecs_table_init_columns(
table->component_map[id] = flecs_ito(int16_t, cur + 1);
}

if (ECS_IS_PAIR(ids[i])) {
ecs_table_record_t *wc_tr = flecs_id_record_get_table(
idr->parent, table);
if (wc_tr->index == tr->index) {
wc_tr->column = tr->column;
}
}

table->flags |= flecs_type_info_flags(ti);
cur ++;
}

int32_t record_count = table->_->record_count;
for (; i < record_count; i ++) {
ecs_table_record_t *tr = &records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
ecs_id_t id = idr->id;

if (ecs_id_is_wildcard(id)) {
ecs_table_record_t *first_tr = &records[tr->index];
tr->column = first_tr->column;
}
}
}

/* Initialize table storage */
Expand Down Expand Up @@ -68951,7 +68956,7 @@ bool flecs_query_with(
{
ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);
ecs_id_record_t *idr = op_ctx->idr;
const ecs_table_record_t *tr;
ecs_table_record_t *tr;

ecs_table_t *table = flecs_query_get_table(op, &op->src, EcsQuerySrc, ctx);
if (!table) {
Expand All @@ -68974,6 +68979,7 @@ bool flecs_query_with(

op_ctx->column = flecs_ito(int16_t, tr->index);
op_ctx->remaining = flecs_ito(int16_t, tr->count);
op_ctx->it.cur = &tr->hdr;
} else {
ecs_assert((op_ctx->remaining + op_ctx->column - 1) < table->type.count,
ECS_INTERNAL_ERROR, NULL);
Expand Down Expand Up @@ -69120,16 +69126,6 @@ bool flecs_query_self_up(
}
}

static
bool flecs_query_select_any(
const ecs_query_op_t *op,
bool redo,
const ecs_query_run_ctx_t *ctx)
{
return flecs_query_select_w_id(op, redo, ctx, EcsAny,
(EcsTableNotQueryable|EcsTableIsPrefab|EcsTableIsDisabled));
}

static
bool flecs_query_and_any(
const ecs_query_op_t *op,
Expand All @@ -69153,8 +69149,9 @@ bool flecs_query_and_any(
remaining = 0;
}

ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);

if (!redo) {
ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);
if (match_flags & EcsTermMatchAny && op_ctx->remaining) {
op_ctx->remaining = flecs_ito(int16_t, remaining);
}
Expand All @@ -69165,6 +69162,8 @@ bool flecs_query_and_any(
ctx->it->ids[field] = flecs_query_op_get_id(op, ctx);
}

ctx->it->trs[field] = (ecs_table_record_t*)op_ctx->it.cur;

return result;
}

Expand All @@ -69178,7 +69177,8 @@ bool flecs_query_only_any(
if (flecs_ref_is_written(op, &op->src, EcsQuerySrc, written)) {
return flecs_query_and_any(op, redo, ctx);
} else {
return flecs_query_select_any(op, redo, ctx);
return flecs_query_select_w_id(op, redo, ctx, EcsAny,
(EcsTableNotQueryable|EcsTableIsPrefab|EcsTableIsDisabled));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/observable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,8 @@ void flecs_emit(
if (count) {
storage_i = tr->column;
bool is_sparse = idr->flags & EcsIdIsSparse;
if (storage_i != -1 || is_sparse) {

if (!ecs_id_is_wildcard(id) && (storage_i != -1 || is_sparse)) {
void *ptr;
ecs_size_t size = idr->type_info->size;

Expand Down
21 changes: 8 additions & 13 deletions src/query/engine/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bool flecs_query_with(
{
ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);
ecs_id_record_t *idr = op_ctx->idr;
const ecs_table_record_t *tr;
ecs_table_record_t *tr;

ecs_table_t *table = flecs_query_get_table(op, &op->src, EcsQuerySrc, ctx);
if (!table) {
Expand All @@ -118,6 +118,7 @@ bool flecs_query_with(

op_ctx->column = flecs_ito(int16_t, tr->index);
op_ctx->remaining = flecs_ito(int16_t, tr->count);
op_ctx->it.cur = &tr->hdr;
} else {
ecs_assert((op_ctx->remaining + op_ctx->column - 1) < table->type.count,
ECS_INTERNAL_ERROR, NULL);
Expand Down Expand Up @@ -264,16 +265,6 @@ bool flecs_query_self_up(
}
}

static
bool flecs_query_select_any(
const ecs_query_op_t *op,
bool redo,
const ecs_query_run_ctx_t *ctx)
{
return flecs_query_select_w_id(op, redo, ctx, EcsAny,
(EcsTableNotQueryable|EcsTableIsPrefab|EcsTableIsDisabled));
}

static
bool flecs_query_and_any(
const ecs_query_op_t *op,
Expand All @@ -297,8 +288,9 @@ bool flecs_query_and_any(
remaining = 0;
}

ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);

if (!redo) {
ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);
if (match_flags & EcsTermMatchAny && op_ctx->remaining) {
op_ctx->remaining = flecs_ito(int16_t, remaining);
}
Expand All @@ -309,6 +301,8 @@ bool flecs_query_and_any(
ctx->it->ids[field] = flecs_query_op_get_id(op, ctx);
}

ctx->it->trs[field] = (ecs_table_record_t*)op_ctx->it.cur;

return result;
}

Expand All @@ -322,7 +316,8 @@ bool flecs_query_only_any(
if (flecs_ref_is_written(op, &op->src, EcsQuerySrc, written)) {
return flecs_query_and_any(op, redo, ctx);
} else {
return flecs_query_select_any(op, redo, ctx);
return flecs_query_select_w_id(op, redo, ctx, EcsAny,
(EcsTableNotQueryable|EcsTableIsPrefab|EcsTableIsDisabled));
}
}

Expand Down
22 changes: 13 additions & 9 deletions src/storage/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void flecs_table_init_columns(
int16_t *s2t = &table->column_map[ids_count];

for (i = 0; i < ids_count; i ++) {
ecs_id_t id = table->type.array[i];
ecs_id_t id = ids[i];
ecs_table_record_t *tr = &records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
const ecs_type_info_t *ti = idr->type_info;
Expand All @@ -172,17 +172,21 @@ void flecs_table_init_columns(
table->component_map[id] = flecs_ito(int16_t, cur + 1);
}

if (ECS_IS_PAIR(ids[i])) {
ecs_table_record_t *wc_tr = flecs_id_record_get_table(
idr->parent, table);
if (wc_tr->index == tr->index) {
wc_tr->column = tr->column;
}
}

table->flags |= flecs_type_info_flags(ti);
cur ++;
}

int32_t record_count = table->_->record_count;
for (; i < record_count; i ++) {
ecs_table_record_t *tr = &records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
ecs_id_t id = idr->id;

if (ecs_id_is_wildcard(id)) {
ecs_table_record_t *first_tr = &records[tr->index];
tr->column = first_tr->column;
}
}
}

/* Initialize table storage */
Expand Down
12 changes: 11 additions & 1 deletion test/query/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,17 @@
"ref_fields_up_src",
"ref_fields_self_up_src",
"0_src_match_nothing",
"0_terms_match_nothing"
"0_terms_match_nothing",
"any_record",
"pair_rel_any_record",
"pair_tgt_any_record",
"pair_any_any_record",
"written_any_record",
"written_pair_rel_any_record",
"written_pair_tgt_any_record",
"written_pair_any_any_record",
"pair_rel_any_record_component",
"pair_tgt_any_record_component"
]
}, {
"id": "Combinations",
Expand Down
Loading

0 comments on commit dd39321

Please sign in to comment.