Skip to content

Commit b118d50

Browse files
committed
Cleanup code
1 parent a057b2a commit b118d50

3 files changed

Lines changed: 285 additions & 186 deletions

File tree

distr/flecs.c

Lines changed: 95 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -43297,6 +43297,83 @@ void flecs_table_register_inherited(
4329743297
}
4329843298
}
4329943299

43300+
/* Copy records to array for tables with inherited (base) components.
43301+
* Inherited records are inserted right after the table's own id records so
43302+
* that the wildcard records remain at the end. Non-wildcard inherited records
43303+
* (the base component ids) are placed first, followed by the existing wildcard
43304+
* records and the inherited wildcard records. This ensures the extended_type
43305+
* array (component ids + base ids) mirrors the order of the records array and
43306+
* excludes wildcards. */
43307+
static
43308+
ecs_table_record_t* flecs_table_init_inherited_records(
43309+
ecs_world_t *world,
43310+
ecs_table_t *table,
43311+
ecs_vec_t *records,
43312+
int32_t inherited_start,
43313+
int32_t inherited_count,
43314+
int32_t *inherited_id_count_out,
43315+
int32_t *inherited_wc_count_out)
43316+
{
43317+
int32_t i, dst_record_count = ecs_vec_count(records);
43318+
ecs_table_record_t *vec_tr = ecs_vec_first_t(records, ecs_table_record_t);
43319+
43320+
int32_t inherited_wc_count = 0;
43321+
for (i = 0; i < inherited_count; i ++) {
43322+
if (ecs_id_is_wildcard(vec_tr[inherited_start + i].hdr.cr->id)) {
43323+
inherited_wc_count ++;
43324+
}
43325+
}
43326+
int32_t inherited_id_count = inherited_count - inherited_wc_count;
43327+
43328+
ecs_table_record_t *dst_tr = flecs_walloc_n(world, ecs_table_record_t,
43329+
dst_record_count);
43330+
ecs_os_memcpy_n(dst_tr, vec_tr, ecs_table_record_t, inherited_start);
43331+
43332+
int32_t at = inherited_start;
43333+
for (i = 0; i < inherited_count; i ++) {
43334+
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
43335+
if (!ecs_id_is_wildcard(itr->hdr.cr->id)) {
43336+
dst_tr[at ++] = *itr;
43337+
}
43338+
}
43339+
43340+
int32_t aux_count = dst_record_count - inherited_start - inherited_count;
43341+
ecs_os_memcpy_n(&dst_tr[at], &vec_tr[inherited_start + inherited_count],
43342+
ecs_table_record_t, aux_count);
43343+
at += aux_count;
43344+
43345+
for (i = 0; i < inherited_count; i ++) {
43346+
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
43347+
if (ecs_id_is_wildcard(itr->hdr.cr->id)) {
43348+
dst_tr[at ++] = *itr;
43349+
}
43350+
}
43351+
43352+
/* Build extended type with component ids + non-wildcard base ids */
43353+
if (inherited_id_count) {
43354+
int32_t dst_count = table->type.count;
43355+
int32_t et_count = dst_count + inherited_id_count;
43356+
ecs_type_t *et = flecs_walloc_t(world, ecs_type_t);
43357+
et->count = et_count;
43358+
et->array = flecs_walloc_n(world, ecs_id_t, et_count);
43359+
for (i = 0; i < dst_count; i ++) {
43360+
et->array[i] = table->type.array[i];
43361+
}
43362+
for (i = 0; i < inherited_id_count; i ++) {
43363+
ecs_id_t base_id = dst_tr[dst_count + i].hdr.cr->id;
43364+
et->array[dst_count + i] = base_id;
43365+
table->bloom_filter = flecs_table_bloom_filter_add(
43366+
table->bloom_filter, base_id);
43367+
}
43368+
table->_->extended_type = et;
43369+
}
43370+
43371+
*inherited_id_count_out = inherited_id_count;
43372+
*inherited_wc_count_out = inherited_wc_count;
43373+
43374+
return dst_tr;
43375+
}
43376+
4330043377
/* Main table initialization function */
4330143378
void flecs_table_init(
4330243379
ecs_world_t *world,
@@ -43530,70 +43607,24 @@ void flecs_table_init(
4353043607
table->bloom_filter, ecs_pair(EcsChildOf, 0));
4353143608
}
4353243609

43533-
/* Now that all records have been added, copy them to array. Inherited
43534-
* records are inserted right after the table's own id records so that the
43535-
* wildcard records remain at the end. Non-wildcard inherited records (the
43536-
* base component ids) are placed first, followed by the existing wildcard
43537-
* records and the inherited wildcard records. This ensures the
43538-
* extended_type array (component ids + base ids) mirrors the order of the
43539-
* records array and excludes wildcards. */
43610+
/* Now that all records have been added, copy them to array */
4354043611
int32_t i, dst_record_count = ecs_vec_count(records);
43541-
ecs_table_record_t *vec_tr = ecs_vec_first_t(records, ecs_table_record_t);
43542-
43543-
int32_t inherited_wc_count = 0;
43544-
for (i = 0; i < inherited_count; i ++) {
43545-
if (ecs_id_is_wildcard(vec_tr[inherited_start + i].hdr.cr->id)) {
43546-
inherited_wc_count ++;
43547-
}
43548-
}
43549-
int32_t inherited_id_count = inherited_count - inherited_wc_count;
43550-
43551-
ecs_table_record_t *dst_tr = flecs_walloc_n(world, ecs_table_record_t,
43552-
dst_record_count);
43553-
ecs_os_memcpy_n(dst_tr, vec_tr, ecs_table_record_t, inherited_start);
43554-
43555-
int32_t at = inherited_start;
43556-
for (i = 0; i < inherited_count; i ++) {
43557-
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
43558-
if (!ecs_id_is_wildcard(itr->hdr.cr->id)) {
43559-
dst_tr[at ++] = *itr;
43560-
}
43561-
}
43562-
43563-
int32_t aux_count = dst_record_count - inherited_start - inherited_count;
43564-
ecs_os_memcpy_n(&dst_tr[at], &vec_tr[inherited_start + inherited_count],
43565-
ecs_table_record_t, aux_count);
43566-
at += aux_count;
43567-
43568-
for (i = 0; i < inherited_count; i ++) {
43569-
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
43570-
if (ecs_id_is_wildcard(itr->hdr.cr->id)) {
43571-
dst_tr[at ++] = *itr;
43572-
}
43612+
int32_t inherited_id_count = 0, inherited_wc_count = 0;
43613+
ecs_table_record_t *dst_tr;
43614+
if (inherited_count) {
43615+
dst_tr = flecs_table_init_inherited_records(world, table, records,
43616+
inherited_start, inherited_count,
43617+
&inherited_id_count, &inherited_wc_count);
43618+
} else {
43619+
dst_tr = flecs_walloc_n(world, ecs_table_record_t, dst_record_count);
43620+
ecs_os_memcpy_n(dst_tr, ecs_vec_first_t(records, ecs_table_record_t),
43621+
ecs_table_record_t, dst_record_count);
4357343622
}
4357443623

4357543624
table->_->record_count = flecs_ito(int16_t, dst_record_count);
4357643625
table->_->records = dst_tr;
4357743626
int32_t column_count = 0;
4357843627

43579-
/* Build extended type with component ids + non-wildcard base ids */
43580-
if (inherited_id_count) {
43581-
int32_t et_count = dst_count + inherited_id_count;
43582-
ecs_type_t *et = flecs_walloc_t(world, ecs_type_t);
43583-
et->count = et_count;
43584-
et->array = flecs_walloc_n(world, ecs_id_t, et_count);
43585-
for (i = 0; i < dst_count; i ++) {
43586-
et->array[i] = dst_ids[i];
43587-
}
43588-
for (i = 0; i < inherited_id_count; i ++) {
43589-
ecs_id_t base_id = dst_tr[dst_count + i].hdr.cr->id;
43590-
et->array[dst_count + i] = base_id;
43591-
table->bloom_filter = flecs_table_bloom_filter_add(
43592-
table->bloom_filter, base_id);
43593-
}
43594-
table->_->extended_type = et;
43595-
}
43596-
4359743628
ecs_table_record_t *isa_tr = NULL;
4359843629

4359943630
/* Register & patch up records */
@@ -43621,9 +43652,9 @@ void flecs_table_init(
4362143652
/* Claim component record so it stays alive as long as the table exists */
4362243653
flecs_component_claim(world, cr);
4362343654

43624-
bool inherited = (i >= inherited_start &&
43655+
bool inherited = inherited_count && ((i >= inherited_start &&
4362543656
i < (inherited_start + inherited_id_count)) ||
43626-
(i >= (dst_record_count - inherited_wc_count));
43657+
(i >= (dst_record_count - inherited_wc_count)));
4362743658
if (inherited) {
4362843659
/* Propagate table event flags for inherited base components, so
4362943660
* that OnTableCreate/OnTableDelete events are emitted for tables
@@ -43669,9 +43700,11 @@ void flecs_table_init(
4366943700
/* Set column indices of inherited records to the column of the id they
4367043701
* were inherited from (inherited wildcard records are initialized by
4367143702
* flecs_table_init_columns). */
43672-
for (i = 0; i < inherited_id_count; i ++) {
43673-
ecs_table_record_t *itr = &dst_tr[inherited_start + i];
43674-
itr->column = dst_tr[itr->index].column;
43703+
if (inherited_id_count) {
43704+
for (i = 0; i < inherited_id_count; i ++) {
43705+
ecs_table_record_t *itr = &dst_tr[inherited_start + i];
43706+
itr->column = dst_tr[itr->index].column;
43707+
}
4367543708
}
4367643709

4367743710
if (childof_cr) {

distr/flecs_no_addons.c

Lines changed: 95 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -35142,6 +35142,83 @@ void flecs_table_register_inherited(
3514235142
}
3514335143
}
3514435144

35145+
/* Copy records to array for tables with inherited (base) components.
35146+
* Inherited records are inserted right after the table's own id records so
35147+
* that the wildcard records remain at the end. Non-wildcard inherited records
35148+
* (the base component ids) are placed first, followed by the existing wildcard
35149+
* records and the inherited wildcard records. This ensures the extended_type
35150+
* array (component ids + base ids) mirrors the order of the records array and
35151+
* excludes wildcards. */
35152+
static
35153+
ecs_table_record_t* flecs_table_init_inherited_records(
35154+
ecs_world_t *world,
35155+
ecs_table_t *table,
35156+
ecs_vec_t *records,
35157+
int32_t inherited_start,
35158+
int32_t inherited_count,
35159+
int32_t *inherited_id_count_out,
35160+
int32_t *inherited_wc_count_out)
35161+
{
35162+
int32_t i, dst_record_count = ecs_vec_count(records);
35163+
ecs_table_record_t *vec_tr = ecs_vec_first_t(records, ecs_table_record_t);
35164+
35165+
int32_t inherited_wc_count = 0;
35166+
for (i = 0; i < inherited_count; i ++) {
35167+
if (ecs_id_is_wildcard(vec_tr[inherited_start + i].hdr.cr->id)) {
35168+
inherited_wc_count ++;
35169+
}
35170+
}
35171+
int32_t inherited_id_count = inherited_count - inherited_wc_count;
35172+
35173+
ecs_table_record_t *dst_tr = flecs_walloc_n(world, ecs_table_record_t,
35174+
dst_record_count);
35175+
ecs_os_memcpy_n(dst_tr, vec_tr, ecs_table_record_t, inherited_start);
35176+
35177+
int32_t at = inherited_start;
35178+
for (i = 0; i < inherited_count; i ++) {
35179+
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
35180+
if (!ecs_id_is_wildcard(itr->hdr.cr->id)) {
35181+
dst_tr[at ++] = *itr;
35182+
}
35183+
}
35184+
35185+
int32_t aux_count = dst_record_count - inherited_start - inherited_count;
35186+
ecs_os_memcpy_n(&dst_tr[at], &vec_tr[inherited_start + inherited_count],
35187+
ecs_table_record_t, aux_count);
35188+
at += aux_count;
35189+
35190+
for (i = 0; i < inherited_count; i ++) {
35191+
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
35192+
if (ecs_id_is_wildcard(itr->hdr.cr->id)) {
35193+
dst_tr[at ++] = *itr;
35194+
}
35195+
}
35196+
35197+
/* Build extended type with component ids + non-wildcard base ids */
35198+
if (inherited_id_count) {
35199+
int32_t dst_count = table->type.count;
35200+
int32_t et_count = dst_count + inherited_id_count;
35201+
ecs_type_t *et = flecs_walloc_t(world, ecs_type_t);
35202+
et->count = et_count;
35203+
et->array = flecs_walloc_n(world, ecs_id_t, et_count);
35204+
for (i = 0; i < dst_count; i ++) {
35205+
et->array[i] = table->type.array[i];
35206+
}
35207+
for (i = 0; i < inherited_id_count; i ++) {
35208+
ecs_id_t base_id = dst_tr[dst_count + i].hdr.cr->id;
35209+
et->array[dst_count + i] = base_id;
35210+
table->bloom_filter = flecs_table_bloom_filter_add(
35211+
table->bloom_filter, base_id);
35212+
}
35213+
table->_->extended_type = et;
35214+
}
35215+
35216+
*inherited_id_count_out = inherited_id_count;
35217+
*inherited_wc_count_out = inherited_wc_count;
35218+
35219+
return dst_tr;
35220+
}
35221+
3514535222
/* Main table initialization function */
3514635223
void flecs_table_init(
3514735224
ecs_world_t *world,
@@ -35375,70 +35452,24 @@ void flecs_table_init(
3537535452
table->bloom_filter, ecs_pair(EcsChildOf, 0));
3537635453
}
3537735454

35378-
/* Now that all records have been added, copy them to array. Inherited
35379-
* records are inserted right after the table's own id records so that the
35380-
* wildcard records remain at the end. Non-wildcard inherited records (the
35381-
* base component ids) are placed first, followed by the existing wildcard
35382-
* records and the inherited wildcard records. This ensures the
35383-
* extended_type array (component ids + base ids) mirrors the order of the
35384-
* records array and excludes wildcards. */
35455+
/* Now that all records have been added, copy them to array */
3538535456
int32_t i, dst_record_count = ecs_vec_count(records);
35386-
ecs_table_record_t *vec_tr = ecs_vec_first_t(records, ecs_table_record_t);
35387-
35388-
int32_t inherited_wc_count = 0;
35389-
for (i = 0; i < inherited_count; i ++) {
35390-
if (ecs_id_is_wildcard(vec_tr[inherited_start + i].hdr.cr->id)) {
35391-
inherited_wc_count ++;
35392-
}
35393-
}
35394-
int32_t inherited_id_count = inherited_count - inherited_wc_count;
35395-
35396-
ecs_table_record_t *dst_tr = flecs_walloc_n(world, ecs_table_record_t,
35397-
dst_record_count);
35398-
ecs_os_memcpy_n(dst_tr, vec_tr, ecs_table_record_t, inherited_start);
35399-
35400-
int32_t at = inherited_start;
35401-
for (i = 0; i < inherited_count; i ++) {
35402-
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
35403-
if (!ecs_id_is_wildcard(itr->hdr.cr->id)) {
35404-
dst_tr[at ++] = *itr;
35405-
}
35406-
}
35407-
35408-
int32_t aux_count = dst_record_count - inherited_start - inherited_count;
35409-
ecs_os_memcpy_n(&dst_tr[at], &vec_tr[inherited_start + inherited_count],
35410-
ecs_table_record_t, aux_count);
35411-
at += aux_count;
35412-
35413-
for (i = 0; i < inherited_count; i ++) {
35414-
ecs_table_record_t *itr = &vec_tr[inherited_start + i];
35415-
if (ecs_id_is_wildcard(itr->hdr.cr->id)) {
35416-
dst_tr[at ++] = *itr;
35417-
}
35457+
int32_t inherited_id_count = 0, inherited_wc_count = 0;
35458+
ecs_table_record_t *dst_tr;
35459+
if (inherited_count) {
35460+
dst_tr = flecs_table_init_inherited_records(world, table, records,
35461+
inherited_start, inherited_count,
35462+
&inherited_id_count, &inherited_wc_count);
35463+
} else {
35464+
dst_tr = flecs_walloc_n(world, ecs_table_record_t, dst_record_count);
35465+
ecs_os_memcpy_n(dst_tr, ecs_vec_first_t(records, ecs_table_record_t),
35466+
ecs_table_record_t, dst_record_count);
3541835467
}
3541935468

3542035469
table->_->record_count = flecs_ito(int16_t, dst_record_count);
3542135470
table->_->records = dst_tr;
3542235471
int32_t column_count = 0;
3542335472

35424-
/* Build extended type with component ids + non-wildcard base ids */
35425-
if (inherited_id_count) {
35426-
int32_t et_count = dst_count + inherited_id_count;
35427-
ecs_type_t *et = flecs_walloc_t(world, ecs_type_t);
35428-
et->count = et_count;
35429-
et->array = flecs_walloc_n(world, ecs_id_t, et_count);
35430-
for (i = 0; i < dst_count; i ++) {
35431-
et->array[i] = dst_ids[i];
35432-
}
35433-
for (i = 0; i < inherited_id_count; i ++) {
35434-
ecs_id_t base_id = dst_tr[dst_count + i].hdr.cr->id;
35435-
et->array[dst_count + i] = base_id;
35436-
table->bloom_filter = flecs_table_bloom_filter_add(
35437-
table->bloom_filter, base_id);
35438-
}
35439-
table->_->extended_type = et;
35440-
}
35441-
3544235473
ecs_table_record_t *isa_tr = NULL;
3544335474

3544435475
/* Register & patch up records */
@@ -35466,9 +35497,9 @@ void flecs_table_init(
3546635497
/* Claim component record so it stays alive as long as the table exists */
3546735498
flecs_component_claim(world, cr);
3546835499

35469-
bool inherited = (i >= inherited_start &&
35500+
bool inherited = inherited_count && ((i >= inherited_start &&
3547035501
i < (inherited_start + inherited_id_count)) ||
35471-
(i >= (dst_record_count - inherited_wc_count));
35502+
(i >= (dst_record_count - inherited_wc_count)));
3547235503
if (inherited) {
3547335504
/* Propagate table event flags for inherited base components, so
3547435505
* that OnTableCreate/OnTableDelete events are emitted for tables
@@ -35514,9 +35545,11 @@ void flecs_table_init(
3551435545
/* Set column indices of inherited records to the column of the id they
3551535546
* were inherited from (inherited wildcard records are initialized by
3551635547
* flecs_table_init_columns). */
35517-
for (i = 0; i < inherited_id_count; i ++) {
35518-
ecs_table_record_t *itr = &dst_tr[inherited_start + i];
35519-
itr->column = dst_tr[itr->index].column;
35548+
if (inherited_id_count) {
35549+
for (i = 0; i < inherited_id_count; i ++) {
35550+
ecs_table_record_t *itr = &dst_tr[inherited_start + i];
35551+
itr->column = dst_tr[itr->index].column;
35552+
}
3552035553
}
3552135554

3552235555
if (childof_cr) {

0 commit comments

Comments
 (0)