@@ -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 */
4330143378void 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) {
0 commit comments