Skip to content

Commit

Permalink
#124 clean up code for deriving table flags
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed May 22, 2022
1 parent 912c2ad commit 11dfc84
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 95 deletions.
80 changes: 80 additions & 0 deletions src/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,86 @@ void flecs_table_init_data(
}
}

static
void flecs_table_init_flags(
ecs_world_t *world,
ecs_table_t *table)
{
ecs_id_t *ids = table->type.array;
int32_t count = table->type.count;

/* Iterate components to initialize table flags */
int32_t i;
for (i = 0; i < count; i ++) {
ecs_id_t id = ids[i];

/* As we're iterating over the table components, also set the table
* flags. These allow us to quickly determine if the table contains
* data that needs to be handled in a special way. */

if (id <= EcsLastInternalComponentId) {
table->flags |= EcsTableHasBuiltins;
}

if (id == EcsModule) {
table->flags |= EcsTableHasBuiltins;
table->flags |= EcsTableHasModule;
} else if (id == EcsPrefab) {
table->flags |= EcsTableIsPrefab;
} else if (id == EcsDisabled) {
table->flags |= EcsTableIsDisabled;
} else {
ecs_entity_t role = id & ECS_ROLE_MASK;
if (role == ECS_PAIR) {
ecs_entity_t r = ECS_PAIR_FIRST(id);

table->flags |= EcsTableHasPairs;

if (r == EcsIsA) {
table->flags |= EcsTableHasIsA;
} else if (r == EcsChildOf) {
table->flags |= EcsTableHasChildOf;
ecs_entity_t obj = ecs_pair_second(world, id);
ecs_assert(obj != 0, ECS_INTERNAL_ERROR, NULL);

if (obj == EcsFlecs || obj == EcsFlecsCore ||
ecs_has_id(world, obj, EcsModule))
{
/* If table contains entities that are inside one of the
* builtin modules, it contains builtin entities */
table->flags |= EcsTableHasBuiltins;
table->flags |= EcsTableHasModule;
}
}
} else if (role == ECS_SWITCH) {
table->flags |= EcsTableHasSwitch;

if (!table->sw_column_count) {
table->sw_column_offset = flecs_ito(int16_t, i);
}
table->sw_column_count ++;
} else if (role == ECS_DISABLED) {
table->flags |= EcsTableHasDisabled;

if (!table->bs_column_count) {
table->bs_column_offset = flecs_ito(int16_t, i);
}
table->bs_column_count ++;
} else if (role == ECS_OVERRIDE) {
table->flags |= EcsTableHasOverrides;
}
}
}
}

void flecs_table_init(
ecs_world_t *world,
ecs_table_t *table,
ecs_table_t *from)
{
flecs_table_init_flags(world, table);
}

static
void notify_trigger(
ecs_world_t *world,
Expand Down
6 changes: 6 additions & 0 deletions src/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#ifndef FLECS_TABLE_H
#define FLECS_TABLE_H

/* Init table */
void flecs_table_init(
ecs_world_t *world,
ecs_table_t *table,
ecs_table_t *from);

/** Copy type. */
ecs_type_t flecs_type_copy(
const ecs_type_t *src);
Expand Down
98 changes: 3 additions & 95 deletions src/table_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,6 @@ void flecs_table_records_register(
id = ecs_pair(id, EcsWildcard);
register_table_for_id(world, table, id, i, 1, &table->records[r]);
r ++;

/* Keep track of how many switch/bitset columns there are */
if (role == ECS_SWITCH) {
if (!table->sw_column_count) {
table->sw_column_offset = flecs_ito(int16_t, i);
}
table->sw_column_count ++;
} else if (role == ECS_DISABLED) {
if (!table->bs_column_count) {
table->bs_column_offset = flecs_ito(int16_t, i);
}
table->bs_column_count ++;
}
}
}
}
Expand Down Expand Up @@ -641,87 +628,6 @@ bool flecs_table_records_update_empty(
return result;
}

static
void init_flags(
ecs_world_t *world,
ecs_table_t *table)
{
ecs_id_t *ids = table->type.array;
int32_t count = table->type.count;

/* Iterate components to initialize table flags */
int32_t i;
for (i = 0; i < count; i ++) {
ecs_id_t id = ids[i];

/* As we're iterating over the table components, also set the table
* flags. These allow us to quickly determine if the table contains
* data that needs to be handled in a special way. */

if (id <= EcsLastInternalComponentId) {
table->flags |= EcsTableHasBuiltins;
}

if (id == EcsModule) {
table->flags |= EcsTableHasBuiltins;
table->flags |= EcsTableHasModule;
}

if (id == EcsPrefab) {
table->flags |= EcsTableIsPrefab;
}

/* If table contains disabled entities, mark it as disabled */
if (id == EcsDisabled) {
table->flags |= EcsTableIsDisabled;
}

/* Does the table have pairs */
if (ECS_HAS_ROLE(id, PAIR)) {
table->flags |= EcsTableHasPairs;
}

/* Does table have IsA relations */
if (ECS_HAS_RELATION(id, EcsIsA)) {
table->flags |= EcsTableHasIsA;
}

/* Does table have ChildOf relations */
if (ECS_HAS_RELATION(id, EcsChildOf)) {
table->flags |= EcsTableHasChildOf;
}

/* Does table have switch columns */
if (ECS_HAS_ROLE(id, SWITCH)) {
table->flags |= EcsTableHasSwitch;
}

/* Does table support component disabling */
if (ECS_HAS_ROLE(id, DISABLED)) {
table->flags |= EcsTableHasDisabled;
}

if (ECS_HAS_ROLE(id, OVERRIDE)) {
table->flags |= EcsTableHasOverrides;
}

if (ECS_HAS_RELATION(id, EcsChildOf)) {
ecs_poly_assert(world, ecs_world_t);
ecs_entity_t obj = ecs_pair_second(world, id);
ecs_assert(obj != 0, ECS_INTERNAL_ERROR, NULL);

if (obj == EcsFlecs || obj == EcsFlecsCore ||
ecs_has_id(world, obj, EcsModule))
{
/* If table contains entities that are inside one of the builtin
* modules, it contains builtin entities */
table->flags |= EcsTableHasBuiltins;
table->flags |= EcsTableHasModule;
}
}
}
}

static
void init_table(
ecs_world_t *world,
Expand All @@ -735,7 +641,9 @@ void init_table(
table->generation = 0;

init_node(&table->node);
init_flags(world, table);

flecs_table_init(world, table, NULL);

flecs_table_records_register(world, table);
flecs_table_init_data(world, table);
}
Expand Down

0 comments on commit 11dfc84

Please sign in to comment.