From e73ababe831bcc4b3a81b2aaaed6df36edf186b0 Mon Sep 17 00:00:00 2001 From: Joe Eli McIlvain Date: Mon, 25 Oct 2021 21:03:47 -0700 Subject: [PATCH 1/2] Synthesize `ctor_move_dtor` from `ctor` and `move_dtor` when necessary. When `ctor_move_dtor` is not set, but `move` and `dtor` are set, but `move_ctor` is not set, then use `move_dtor` to synthesize it. --- flecs.c | 48197 +++++++++++++++++++++++++------------------------- src/world.c | 19 +- 2 files changed, 24125 insertions(+), 24091 deletions(-) diff --git a/flecs.c b/flecs.c index 82ff34bc84..0a8d240c3c 100644 --- a/flecs.c +++ b/flecs.c @@ -2069,12870 +2069,13208 @@ void _assert_func( #endif -/* Count number of switch columns */ -static -int32_t switch_column_count( - ecs_table_t *table) -{ - int32_t i, sw_count = 0, count = ecs_vector_count(table->type); - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); +/* Roles */ +const ecs_id_t ECS_CASE = (ECS_ROLE | (0x7Cull << 56)); +const ecs_id_t ECS_SWITCH = (ECS_ROLE | (0x7Bull << 56)); +const ecs_id_t ECS_PAIR = (ECS_ROLE | (0x7Aull << 56)); +const ecs_id_t ECS_OVERRIDE = (ECS_ROLE | (0x75ull << 56)); +const ecs_id_t ECS_DISABLED = (ECS_ROLE | (0x74ull << 56)); - for (i = 0; i < count; i ++) { - ecs_id_t id = ids[i]; - if (ECS_HAS_ROLE(id, SWITCH)) { - if (!sw_count) { - table->sw_column_offset = i; - } - sw_count ++; - } - } +/** Builtin component ids */ +const ecs_entity_t ecs_id(EcsComponent) = 1; +const ecs_entity_t ecs_id(EcsComponentLifecycle) = 2; +const ecs_entity_t ecs_id(EcsType) = 3; +const ecs_entity_t ecs_id(EcsIdentifier) = 4; +const ecs_entity_t ecs_id(EcsTrigger) = 5; +const ecs_entity_t ecs_id(EcsQuery) = 6; +const ecs_entity_t ecs_id(EcsObserver) = 7; - return sw_count; -} +/* System module component ids */ +const ecs_entity_t ecs_id(EcsSystem) = 10; +const ecs_entity_t ecs_id(EcsTickSource) = 11; -/* Count number of bitset columns */ -static -int32_t bitset_column_count( - ecs_table_t *table) -{ - int32_t count = 0; - ecs_vector_each(table->type, ecs_entity_t, c_ptr, { - ecs_entity_t component = *c_ptr; +/** Pipeline module component ids */ +const ecs_entity_t ecs_id(EcsPipelineQuery) = 12; - if (ECS_HAS_ROLE(component, DISABLED)) { - if (!count) { - table->bs_column_offset = c_ptr_i; - } - count ++; - } - }); +/** Timer module component ids */ +const ecs_entity_t ecs_id(EcsTimer) = 13; +const ecs_entity_t ecs_id(EcsRateFilter) = 14; - return count; -} +/** Meta module component ids */ +const ecs_entity_t ecs_id(EcsMetaType) = 15; +const ecs_entity_t ecs_id(EcsMetaTypeSerialized) = 16; +const ecs_entity_t ecs_id(EcsPrimitive) = 17; +const ecs_entity_t ecs_id(EcsEnum) = 18; +const ecs_entity_t ecs_id(EcsBitmask) = 19; +const ecs_entity_t ecs_id(EcsMember) = 20; +const ecs_entity_t ecs_id(EcsStruct) = 21; +const ecs_entity_t ecs_id(EcsArray) = 22; +const ecs_entity_t ecs_id(EcsVector) = 23; -static -void init_storage_map( - ecs_table_t *table) -{ - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - if (!table->storage_table) { - return; - } +/* Core scopes & entities */ +const ecs_entity_t EcsWorld = ECS_HI_COMPONENT_ID + 0; +const ecs_entity_t EcsFlecs = ECS_HI_COMPONENT_ID + 1; +const ecs_entity_t EcsFlecsCore = ECS_HI_COMPONENT_ID + 2; +const ecs_entity_t EcsModule = ECS_HI_COMPONENT_ID + 3; +const ecs_entity_t EcsPrefab = ECS_HI_COMPONENT_ID + 4; +const ecs_entity_t EcsDisabled = ECS_HI_COMPONENT_ID + 5; - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - int32_t t, ids_count = ecs_vector_count(table->type); - ecs_id_t *storage_ids = ecs_vector_first(table->storage_type, ecs_id_t); - int32_t s, storage_ids_count = ecs_vector_count(table->storage_type); +/* Relation properties */ +const ecs_entity_t EcsWildcard = ECS_HI_COMPONENT_ID + 10; +const ecs_entity_t EcsThis = ECS_HI_COMPONENT_ID + 11; +const ecs_entity_t EcsTransitive = ECS_HI_COMPONENT_ID + 12; +const ecs_entity_t EcsInclusive = ECS_HI_COMPONENT_ID + 13; +const ecs_entity_t EcsFinal = ECS_HI_COMPONENT_ID + 14; +const ecs_entity_t EcsTag = ECS_HI_COMPONENT_ID + 15; - if (!ids_count) { - table->storage_map = NULL; - return; - } +/* Identifier tags */ +const ecs_entity_t EcsName = ECS_HI_COMPONENT_ID + 16; +const ecs_entity_t EcsSymbol = ECS_HI_COMPONENT_ID + 17; - table->storage_map = ecs_os_malloc_n( - int32_t, ids_count + storage_ids_count); +/* Relations */ +const ecs_entity_t EcsChildOf = ECS_HI_COMPONENT_ID + 20; +const ecs_entity_t EcsIsA = ECS_HI_COMPONENT_ID + 21; - int32_t *t2s = table->storage_map; - int32_t *s2t = &table->storage_map[ids_count]; +/* Events */ +const ecs_entity_t EcsOnAdd = ECS_HI_COMPONENT_ID + 30; +const ecs_entity_t EcsOnRemove = ECS_HI_COMPONENT_ID + 31; +const ecs_entity_t EcsOnSet = ECS_HI_COMPONENT_ID + 32; +const ecs_entity_t EcsUnSet = ECS_HI_COMPONENT_ID + 33; +const ecs_entity_t EcsOnDelete = ECS_HI_COMPONENT_ID + 34; +const ecs_entity_t EcsOnCreateTable = ECS_HI_COMPONENT_ID + 35; +const ecs_entity_t EcsOnDeleteTable = ECS_HI_COMPONENT_ID + 36; +const ecs_entity_t EcsOnTableEmpty = ECS_HI_COMPONENT_ID + 37; +const ecs_entity_t EcsOnTableNonEmpty = ECS_HI_COMPONENT_ID + 38; +const ecs_entity_t EcsOnCreateTrigger = ECS_HI_COMPONENT_ID + 39; +const ecs_entity_t EcsOnDeleteTrigger = ECS_HI_COMPONENT_ID + 40; +const ecs_entity_t EcsOnDeleteObservable = ECS_HI_COMPONENT_ID + 41; +const ecs_entity_t EcsOnComponentLifecycle = ECS_HI_COMPONENT_ID + 42; +const ecs_entity_t EcsOnDeleteObject = ECS_HI_COMPONENT_ID + 43; - for (s = 0, t = 0; (t < ids_count) && (s < storage_ids_count); ) { - ecs_id_t id = ids[t]; - ecs_id_t storage_id = storage_ids[s]; +/* Actions */ +const ecs_entity_t EcsRemove = ECS_HI_COMPONENT_ID + 50; +const ecs_entity_t EcsDelete = ECS_HI_COMPONENT_ID + 51; +const ecs_entity_t EcsThrow = ECS_HI_COMPONENT_ID + 52; - if (id == storage_id) { - t2s[t] = s; - s2t[s] = t; - } else { - t2s[t] = -1; - } +/* Misc */ +const ecs_entity_t EcsDefaultChildComponent = ECS_HI_COMPONENT_ID + 55; - /* Ids can never get ahead of storage id, as ids are a superset of the - * storage ids */ - ecs_assert(id <= storage_id, ECS_INTERNAL_ERROR, NULL); +/* Systems */ +const ecs_entity_t EcsMonitor = ECS_HI_COMPONENT_ID + 61; +const ecs_entity_t EcsInactive = ECS_HI_COMPONENT_ID + 63; +const ecs_entity_t EcsPipeline = ECS_HI_COMPONENT_ID + 64; +const ecs_entity_t EcsPreFrame = ECS_HI_COMPONENT_ID + 65; +const ecs_entity_t EcsOnLoad = ECS_HI_COMPONENT_ID + 66; +const ecs_entity_t EcsPostLoad = ECS_HI_COMPONENT_ID + 67; +const ecs_entity_t EcsPreUpdate = ECS_HI_COMPONENT_ID + 68; +const ecs_entity_t EcsOnUpdate = ECS_HI_COMPONENT_ID + 69; +const ecs_entity_t EcsOnValidate = ECS_HI_COMPONENT_ID + 70; +const ecs_entity_t EcsPostUpdate = ECS_HI_COMPONENT_ID + 71; +const ecs_entity_t EcsPreStore = ECS_HI_COMPONENT_ID + 72; +const ecs_entity_t EcsOnStore = ECS_HI_COMPONENT_ID + 73; +const ecs_entity_t EcsPostFrame = ECS_HI_COMPONENT_ID + 74; - t += (id <= storage_id); - s += (id == storage_id); - } +/* Meta primitive components (don't use low ids to save id space) */ +const ecs_entity_t EcsConstant = ECS_HI_COMPONENT_ID + 80; +const ecs_entity_t ecs_id(ecs_bool_t) = ECS_HI_COMPONENT_ID + 81; +const ecs_entity_t ecs_id(ecs_char_t) = ECS_HI_COMPONENT_ID + 82; +const ecs_entity_t ecs_id(ecs_byte_t) = ECS_HI_COMPONENT_ID + 83; +const ecs_entity_t ecs_id(ecs_u8_t) = ECS_HI_COMPONENT_ID + 84; +const ecs_entity_t ecs_id(ecs_u16_t) = ECS_HI_COMPONENT_ID + 85; +const ecs_entity_t ecs_id(ecs_u32_t) = ECS_HI_COMPONENT_ID + 86; +const ecs_entity_t ecs_id(ecs_u64_t) = ECS_HI_COMPONENT_ID + 87; +const ecs_entity_t ecs_id(ecs_uptr_t) = ECS_HI_COMPONENT_ID + 88; +const ecs_entity_t ecs_id(ecs_i8_t) = ECS_HI_COMPONENT_ID + 89; +const ecs_entity_t ecs_id(ecs_i16_t) = ECS_HI_COMPONENT_ID + 90; +const ecs_entity_t ecs_id(ecs_i32_t) = ECS_HI_COMPONENT_ID + 91; +const ecs_entity_t ecs_id(ecs_i64_t) = ECS_HI_COMPONENT_ID + 92; +const ecs_entity_t ecs_id(ecs_iptr_t) = ECS_HI_COMPONENT_ID + 93; +const ecs_entity_t ecs_id(ecs_f32_t) = ECS_HI_COMPONENT_ID + 94; +const ecs_entity_t ecs_id(ecs_f64_t) = ECS_HI_COMPONENT_ID + 95; +const ecs_entity_t ecs_id(ecs_string_t) = ECS_HI_COMPONENT_ID + 96; +const ecs_entity_t ecs_id(ecs_entity_t) = ECS_HI_COMPONENT_ID + 97; - /* Storage ids is always a subset of ids, so all should be iterated */ - ecs_assert(s == storage_ids_count, ECS_INTERNAL_ERROR, NULL); +/* Doc module components */ +const ecs_entity_t ecs_id(EcsDocDescription) =ECS_HI_COMPONENT_ID + 100; +const ecs_entity_t EcsDocBrief = ECS_HI_COMPONENT_ID + 101; +const ecs_entity_t EcsDocDetail = ECS_HI_COMPONENT_ID + 102; +const ecs_entity_t EcsDocLink = ECS_HI_COMPONENT_ID + 103; - /* Initialize remainder of type -> storage_type map */ - for (; (t < ids_count); t ++) { - t2s[t] = -1; - } -} +/* -- Private functions -- */ -static -void init_storage_table( - ecs_world_t *world, - ecs_table_t *table) +const ecs_stage_t* flecs_stage_from_readonly_world( + const ecs_world_t *world) { - int32_t i, count = ecs_vector_count(table->type); - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - ecs_ids_t storage_ids = { - .array = ecs_os_alloca_n(ecs_id_t, count) - }; - - for (i = 0; i < count; i ++) { - ecs_id_t id = ids[i]; - - if ((id == ecs_id(EcsComponent)) || - (ECS_PAIR_RELATION(id) == ecs_id(EcsIdentifier))) - { - storage_ids.array[storage_ids.count ++] = id; - continue; - } + ecs_assert(ecs_poly_is(world, ecs_world_t) || + ecs_poly_is(world, ecs_stage_t), + ECS_INTERNAL_ERROR, + NULL); - const EcsComponent *comp = flecs_component_from_id(world, id); - if (!comp || !comp->size) { - continue; - } + if (ecs_poly_is(world, ecs_world_t)) { + return &world->stage; - storage_ids.array[storage_ids.count ++] = id; + } else if (ecs_poly_is(world, ecs_stage_t)) { + return (ecs_stage_t*)world; } - if (storage_ids.count && storage_ids.count != count) { - table->storage_table = flecs_table_find_or_create(world, &storage_ids); - table->storage_type = table->storage_table->type; - ecs_assert(table->storage_table != NULL, ECS_INTERNAL_ERROR, NULL); - } else if (storage_ids.count) { - table->storage_table = table; - table->storage_type = table->storage_table->type; - ecs_assert(table->storage_table != NULL, ECS_INTERNAL_ERROR, NULL); - } - - if (!table->storage_map) { - init_storage_map(table); - } + return NULL; } -void flecs_table_init_data( - ecs_world_t *world, - ecs_table_t *table) +ecs_stage_t *flecs_stage_from_world( + ecs_world_t **world_ptr) { - init_storage_table(world, table); - - int32_t sw_count = table->sw_column_count = switch_column_count(table); - int32_t bs_count = table->bs_column_count = bitset_column_count(table); + ecs_world_t *world = *world_ptr; - ecs_data_t *storage = &table->storage; - ecs_type_t type = table->storage_type; + ecs_assert(ecs_poly_is(world, ecs_world_t) || + ecs_poly_is(world, ecs_stage_t), + ECS_INTERNAL_ERROR, + NULL); - int32_t i, count = ecs_vector_count(type); + if (ecs_poly_is(world, ecs_world_t)) { + ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); + return &world->stage; - /* Root tables don't have columns */ - if (!count && !sw_count && !bs_count) { - storage->columns = NULL; + } else if (ecs_poly_is(world, ecs_stage_t)) { + ecs_stage_t *stage = (ecs_stage_t*)world; + *world_ptr = stage->world; + return stage; } + + return NULL; +} - if (count) { - ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); - storage->columns = ecs_os_calloc_n(ecs_column_t, count); - - for (i = 0; i < count; i ++) { - ecs_entity_t id = ids[i]; - - /* Bootstrap components */ - if (id == ecs_id(EcsComponent)) { - storage->columns[i].size = ECS_SIZEOF(EcsComponent); - storage->columns[i].alignment = ECS_ALIGNOF(EcsComponent); - continue; - } else if (ECS_PAIR_RELATION(id) == ecs_id(EcsIdentifier)) { - storage->columns[i].size = ECS_SIZEOF(EcsIdentifier); - storage->columns[i].alignment = ECS_ALIGNOF(EcsIdentifier); - continue; - } - - const EcsComponent *component = flecs_component_from_id(world, id); - ecs_assert(component != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(component->size != 0, ECS_INTERNAL_ERROR, NULL); +/* Evaluate component monitor. If a monitored entity changed it will have set a + * flag in one of the world's component monitors. Queries can register + * themselves with component monitors to determine whether they need to rematch + * with tables. */ +static +void eval_component_monitor( + ecs_world_t *world) +{ + ecs_relation_monitor_t *rm = &world->monitors; - storage->columns[i].size = flecs_to_i16(component->size); - storage->columns[i].alignment = flecs_to_i16(component->alignment); - } + if (!rm->is_dirty) { + return; } - if (sw_count) { - ecs_entity_t *ids = ecs_vector_first(table->type, ecs_entity_t); - int32_t sw_offset = table->sw_column_offset; - storage->sw_columns = ecs_os_calloc_n(ecs_sw_column_t, sw_count); - - for (i = 0; i < sw_count; i ++) { - ecs_entity_t e = ids[i + sw_offset]; - ecs_assert(ECS_HAS_ROLE(e, SWITCH), ECS_INTERNAL_ERROR, NULL); - e = e & ECS_COMPONENT_MASK; - const EcsType *type_ptr = ecs_get(world, e, EcsType); - ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_type_t sw_type = type_ptr->normalized; + ecs_map_iter_t it = ecs_map_iter(rm->monitor_sets); + ecs_monitor_set_t *ms; - ecs_entity_t *sw_array = ecs_vector_first(sw_type, ecs_entity_t); - int32_t sw_array_count = ecs_vector_count(sw_type); + while ((ms = ecs_map_next(&it, ecs_monitor_set_t, NULL))) { + if (!ms->is_dirty) { + continue; + } - ecs_switch_t *sw = flecs_switch_new( - sw_array[0], - sw_array[sw_array_count - 1], - 0); + if (ms->monitors) { + ecs_map_iter_t mit = ecs_map_iter(ms->monitors); + ecs_monitor_t *m; + while ((m = ecs_map_next(&mit, ecs_monitor_t, NULL))) { + if (!m->is_dirty) { + continue; + } - storage->sw_columns[i].data = sw; - storage->sw_columns[i].type = sw_type; - } - } + ecs_vector_each(m->queries, ecs_query_t*, q_ptr, { + flecs_query_notify(world, *q_ptr, &(ecs_query_event_t) { + .kind = EcsQueryTableRematch + }); + }); - if (bs_count) { - storage->bs_columns = ecs_os_calloc_n(ecs_bs_column_t, bs_count); - for (i = 0; i < bs_count; i ++) { - flecs_bitset_init(&storage->bs_columns[i].data); + m->is_dirty = false; + } } - } -} - -static -ecs_flags32_t get_component_action_flags( - const ecs_type_info_t *c_info) -{ - ecs_flags32_t flags = 0; - if (c_info->lifecycle.ctor) { - flags |= EcsTableHasCtors; - } - if (c_info->lifecycle.dtor) { - flags |= EcsTableHasDtors; - } - if (c_info->lifecycle.copy) { - flags |= EcsTableHasCopy; + ms->is_dirty = false; } - if (c_info->lifecycle.move) { - flags |= EcsTableHasMove; - } - return flags; + rm->is_dirty = false; } -/* Check if table has instance of component, including pairs */ -static -bool has_component( +void flecs_monitor_mark_dirty( ecs_world_t *world, - ecs_type_t type, - ecs_entity_t component) + ecs_entity_t relation, + ecs_entity_t id) { - ecs_entity_t *entities = ecs_vector_first(type, ecs_entity_t); - int32_t i, count = ecs_vector_count(type); + ecs_assert(world->monitors.monitor_sets != NULL, ECS_INTERNAL_ERROR, NULL); - for (i = 0; i < count; i ++) { - if (component == ecs_get_typeid(world, entities[i])) { - return true; + /* Only flag if there are actually monitors registered, so that we + * don't waste cycles evaluating monitors if there's no interest */ + ecs_monitor_set_t *ms = ecs_map_get(world->monitors.monitor_sets, + ecs_monitor_set_t, relation); + if (ms && ms->monitors) { + ecs_monitor_t *m = ecs_map_get(ms->monitors, + ecs_monitor_t, id); + if (m) { + m->is_dirty = true; + ms->is_dirty = true; + world->monitors.is_dirty = true; } } - - return false; } -static -void notify_component_info( +void flecs_monitor_register( ecs_world_t *world, - ecs_table_t *table, - ecs_entity_t component) + ecs_entity_t relation, + ecs_entity_t id, + ecs_query_t *query) { - ecs_type_t table_type = table->storage_type; - if (!component || has_component(world, table_type, component)){ - int32_t column_count = ecs_vector_count(table_type); - ecs_assert(!component || column_count != 0, ECS_INTERNAL_ERROR, NULL); - - if (!column_count) { - return; - } - - if (!table->c_info) { - table->c_info = ecs_os_calloc( - ECS_SIZEOF(ecs_type_info_t*) * column_count); - } + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(query != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(world->monitors.monitor_sets != NULL, ECS_INTERNAL_ERROR, NULL); - /* Reset lifecycle flags before recomputing */ - table->flags &= ~EcsTableHasLifecycle; + ecs_monitor_set_t *ms = ecs_map_ensure( + world->monitors.monitor_sets, ecs_monitor_set_t, relation); + ecs_assert(ms != NULL, ECS_INTERNAL_ERROR, NULL); - /* Recompute lifecycle flags */ - ecs_entity_t *array = ecs_vector_first(table_type, ecs_entity_t); - int32_t i; - for (i = 0; i < column_count; i ++) { - ecs_id_t id = array[i]; - ecs_entity_t c; + if (!ms->monitors) { + ms->monitors = ecs_map_new(ecs_monitor_t, 1); + } - /* Hardcode components used in bootstrap */ - if (id == ecs_id(EcsComponent)) { - c = id; - } else if (ECS_PAIR_RELATION(id) == ecs_id(EcsIdentifier)) { - c = ecs_id(EcsIdentifier); - } else { - c = ecs_get_typeid(world, array[i]); - } - ecs_assert(c != 0, ECS_INTERNAL_ERROR, NULL); - - const ecs_type_info_t *c_info = flecs_get_c_info(world, c); - if (c_info) { - ecs_flags32_t flags = get_component_action_flags(c_info); - table->flags |= flags; - } + ecs_monitor_t *m = ecs_map_ensure(ms->monitors, ecs_monitor_t, id); + ecs_assert(m != NULL, ECS_INTERNAL_ERROR, NULL); - /* Store pointer to c_info for fast access */ - table->c_info[i] = (ecs_type_info_t*)c_info; - } - } + ecs_query_t **q = ecs_vector_add(&m->queries, ecs_query_t*); + *q = query; } static -void notify_trigger( - ecs_world_t *world, - ecs_table_t *table, - ecs_entity_t event) +void monitors_init( + ecs_relation_monitor_t *rm) { - (void)world; - - if (event == EcsOnAdd) { - table->flags |= EcsTableHasOnAdd; - } else if (event == EcsOnRemove) { - table->flags |= EcsTableHasOnRemove; - } else if (event == EcsOnSet) { - table->flags |= EcsTableHasOnSet; - } else if (event == EcsUnSet) { - table->flags |= EcsTableHasUnSet; - } + rm->monitor_sets = ecs_map_new(ecs_monitor_t, 0); + rm->is_dirty = false; } static -void run_on_remove( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data) +void monitors_fini( + ecs_relation_monitor_t *rm) { - int32_t count = ecs_vector_count(data->entities); - if (count) { - ecs_ids_t removed = { - .array = ecs_vector_first(table->type, ecs_id_t), - .count = ecs_vector_count(table->type) - }; + ecs_map_iter_t it = ecs_map_iter(rm->monitor_sets); + ecs_monitor_set_t *ms; - ecs_table_diff_t diff = { - .removed = removed, - .un_set = removed - }; - - flecs_notify_on_remove(world, table, NULL, 0, count, &diff); + while ((ms = ecs_map_next(&it, ecs_monitor_set_t, NULL))) { + if (ms->monitors) { + ecs_map_iter_t mit = ecs_map_iter(ms->monitors); + ecs_monitor_t *m; + while ((m = ecs_map_next(&mit, ecs_monitor_t, NULL))) { + ecs_vector_free(m->queries); + } + + ecs_map_free(ms->monitors); + } } -} -/* -- Private functions -- */ + ecs_map_free(rm->monitor_sets); +} -/* If table goes from 0 to >0 entities or from >0 entities to 0 entities notify - * queries. This allows systems associated with queries to move inactive tables - * out of the main loop. */ static -void table_activate( - ecs_world_t *world, - ecs_table_t *table, - bool activate) +void init_store( + ecs_world_t *world) { - ecs_vector_t *queries = table->queries; - ecs_query_t **buffer = ecs_vector_first(queries, ecs_query_t*); - int32_t i, count = ecs_vector_count(queries); - - for (i = 0; i < count; i ++) { - flecs_query_notify(world, buffer[i], &(ecs_query_event_t) { - .kind = activate ? EcsQueryTableNonEmpty : EcsQueryTableEmpty, - .table = table - }); - } + ecs_os_memset(&world->store, 0, ECS_SIZEOF(ecs_store_t)); + + /* Initialize entity index */ + world->store.entity_index = flecs_sparse_new(ecs_record_t); + flecs_sparse_set_id_source(world->store.entity_index, &world->stats.last_id); - flecs_table_set_empty(world, table); -} + /* Initialize root table */ + world->store.tables = flecs_sparse_new(ecs_table_t); -/* This function is called when a query is matched with a table. A table keeps - * a list of tables that match so that they can be notified when the table - * becomes empty / non-empty. */ -static -void register_query( - ecs_world_t *world, - ecs_table_t *table, - ecs_query_t *query) -{ - (void)world; -#ifndef NDEBUG - /* Sanity check if query has already been added */ - int32_t i, count = ecs_vector_count(table->queries); - for (i = 0; i < count; i ++) { - ecs_query_t **q = ecs_vector_get(table->queries, ecs_query_t*, i); - ecs_assert(*q != query, ECS_INTERNAL_ERROR, NULL); - } -#endif + /* Initialize table map */ + world->store.table_map = flecs_table_hashmap_new(); - ecs_query_t **q = ecs_vector_add(&table->queries, ecs_query_t*); - if (q) *q = query; + /* Initialize one root table per stage */ + flecs_init_root_table(world); } -/* This function is called when a query is unmatched with a table. This can - * happen for queries that have shared components expressions in their signature - * and those shared components changed (for example, a base removed a comp). */ static -void unregister_query( - ecs_world_t *world, - ecs_table_t *table, - ecs_query_t *query) +void clean_tables( + ecs_world_t *world) { - (void)world; + int32_t i, count = flecs_sparse_count(world->store.tables); - int32_t i, count = ecs_vector_count(table->queries); for (i = 0; i < count; i ++) { - ecs_query_t **q = ecs_vector_get(table->queries, ecs_query_t*, i); - if (*q == query) { - break; - } + ecs_table_t *t = flecs_sparse_get_dense(world->store.tables, ecs_table_t, i); + flecs_table_free(world, t); } - /* Query must have been registered with table */ - ecs_assert(i != count, ECS_INTERNAL_ERROR, NULL); - - /* Remove query */ - ecs_vector_remove(table->queries, ecs_query_t*, i); -} - -static -void ctor_component( - ecs_world_t *world, - ecs_type_info_t *cdata, - ecs_column_t *column, - ecs_entity_t *entities, - int32_t row, - int32_t count) -{ - /* A new component is constructed */ - ecs_xtor_t ctor; - if (cdata && (ctor = cdata->lifecycle.ctor)) { - void *ctx = cdata->lifecycle.ctx; - int16_t size = column->size; - int16_t alignment = column->alignment; - - void *ptr = ecs_vector_get_t(column->data, size, alignment, row); + /* Free table types separately so that if application destructors rely on + * a type it's still valid. */ + for (i = 0; i < count; i ++) { + ecs_table_t *t = flecs_sparse_get_dense(world->store.tables, ecs_table_t, i); + flecs_table_free_type(t); + } - ctor(world, cdata->component, entities, ptr, - flecs_to_size_t(size), count, ctx); + /* Clear the root table */ + if (count) { + flecs_table_reset(world, &world->store.root); } } static -void dtor_component( - ecs_world_t *world, - ecs_type_info_t *cdata, - ecs_column_t *column, - ecs_entity_t *entities, - int32_t row, - int32_t count) -{ - if (!count) { - return; - } - - /* An old component is destructed */ - ecs_xtor_t dtor; - if (cdata && (dtor = cdata->lifecycle.dtor)) { - void *ctx = cdata->lifecycle.ctx; - int16_t size = column->size; - int16_t alignment = column->alignment; - - ecs_assert(column->data != NULL, ECS_INTERNAL_ERROR, NULL); - void *ptr = ecs_vector_get_t(column->data, size, alignment, row); - ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - - dtor(world, cdata->component, &entities[row], ptr, - flecs_to_size_t(size), count, ctx); - } +void fini_store(ecs_world_t *world) { + clean_tables(world); + flecs_sparse_free(world->store.tables); + flecs_table_free(world, &world->store.root); + flecs_sparse_clear(world->store.entity_index); + flecs_hashmap_free(world->store.table_map); } static -void dtor_all_components( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t row, - int32_t count, - bool update_entity_index, - bool is_delete) -{ - /* Can't delete and not update the entity index */ - ecs_assert(!is_delete || update_entity_index, ECS_INTERNAL_ERROR, NULL); +void log_addons(void) { + ecs_trace("addons included in build:"); + ecs_log_push(); + #ifdef FLECS_CPP + ecs_trace("FLECS_CPP"); + #endif + #ifdef FLECS_MODULE + ecs_trace("FLECS_MODULE"); + #endif + #ifdef FLECS_PARSER + ecs_trace("FLECS_PARSER"); + #endif + #ifdef FLECS_PLECS + ecs_trace("FLECS_PLECS"); + #endif + #ifdef FLECS_RULES + ecs_trace("FLECS_RULES"); + #endif + #ifdef FLECS_SNAPSHOT + ecs_trace("FLECS_SNAPSHOT"); + #endif + #ifdef FLECS_STATS + ecs_trace("FLECS_STATS"); + #endif + #ifdef FLECS_SYSTEM + ecs_trace("FLECS_SYSTEM"); + #endif + #ifdef FLECS_PIPELINE + ecs_trace("FLECS_PIPELINE"); + #endif + #ifdef FLECS_TIMER + ecs_trace("FLECS_TIMER"); + #endif + #ifdef FLECS_META + ecs_trace("FLECS_META"); + #endif + #ifdef FLECS_META_C + ecs_trace("FLECS_META_C"); + #endif + #ifdef FLECS_EXPR + ecs_trace("FLECS_EXPR"); + #endif + #ifdef FLECS_JSON + ecs_trace("FLECS_JSON"); + #endif + #ifdef FLECS_DOC + ecs_trace("FLECS_DOC"); + #endif + #ifdef FLECS_COREDOC + ecs_trace("FLECS_COREDOC"); + #endif + #ifdef FLECS_LOG + ecs_trace("FLECS_LOG"); + #endif + ecs_log_pop(); +} - ecs_record_t **records = ecs_vector_first(data->record_ptrs, ecs_record_t*); - ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); - int32_t i, c, end = row + count; - int32_t column_count = ecs_vector_count(table->storage_type); +/* -- Public functions -- */ - (void)records; +ecs_world_t *ecs_mini(void) { + ecs_os_init(); - /* If table has components with destructors, iterate component columns */ - if (table->flags & EcsTableHasDtors) { - /* Prevent the storage from getting modified while deleting */ - ecs_defer_begin(world); + /* Log information about current build & OS API config */ - /* Throw up a lock just to be sure */ - table->lock = true; + ecs_trace("#[bold]bootstrapping world"); + ecs_log_push(); - /* Iterate entities first, then components. This ensures that only one - * entity is invalidated at a time, which ensures that destructors can - * safely access other entities. */ - for (i = row; i < end; i ++) { - for (c = 0; c < column_count; c++) { - ecs_column_t *column = &data->columns[c]; - dtor_component(world, table->c_info[c], column, entities, i, 1); - } + ecs_trace("tracing enabled, call ecs_log_set_level(-1) to disable"); - /* Update entity index after invoking destructors so that entity can - * be safely used in destructor callbacks. */ - if (update_entity_index) { - ecs_entity_t e = entities[i]; - ecs_assert(!e || ecs_is_valid(world, e), - ECS_INTERNAL_ERROR, NULL); - ecs_assert(!e || records[i] == ecs_eis_get(world, e), - ECS_INTERNAL_ERROR, NULL); - ecs_assert(!e || records[i]->table == table, - ECS_INTERNAL_ERROR, NULL); + if (!ecs_os_has_heap()) { + ecs_abort(ECS_MISSING_OS_API, NULL); + } - if (is_delete) { - ecs_eis_delete(world, e); - ecs_assert(ecs_is_valid(world, e) == false, - ECS_INTERNAL_ERROR, NULL); - } else { - // If this is not a delete, clear the entity index record - ecs_record_t r = {NULL, 0}; - ecs_eis_set(world, e, &r); - } - } else { - /* This should only happen in rare cases, such as when the data - * cleaned up is not part of the world (like with snapshots) */ - } - } + if (!ecs_os_has_threading()) { + ecs_trace("threading unavailable, to use threads set OS API first (see examples)"); + } - table->lock = false; - - ecs_defer_end(world); + if (!ecs_os_has_time()) { + ecs_trace("time management not available"); + } - /* If table does not have destructors, just update entity index */ - } else if (update_entity_index) { - if (is_delete) { - for (i = row; i < end; i ++) { - ecs_entity_t e = entities[i]; - ecs_assert(!e || ecs_is_valid(world, e), ECS_INTERNAL_ERROR, NULL); - ecs_assert(!e || records[i] == ecs_eis_get(world, e), - ECS_INTERNAL_ERROR, NULL); - ecs_assert(!e || records[i]->table == table, - ECS_INTERNAL_ERROR, NULL); + log_addons(); - ecs_eis_delete(world, e); - ecs_assert(!ecs_is_valid(world, e), ECS_INTERNAL_ERROR, NULL); - } - } else { - for (i = row; i < end; i ++) { - ecs_entity_t e = entities[i]; - ecs_assert(!e || ecs_is_valid(world, e), ECS_INTERNAL_ERROR, NULL); - ecs_assert(!e || records[i] == ecs_eis_get(world, e), - ECS_INTERNAL_ERROR, NULL); - ecs_assert(!e || records[i]->table == table, - ECS_INTERNAL_ERROR, NULL); - ecs_record_t r = {NULL, 0}; - ecs_eis_set(world, e, &r); - } - } - } -} +#ifndef NDEBUG + ecs_trace("debug build, rebuild with NDEBUG for improved performance"); +#else + ecs_trace("#[green]release#[reset] build"); +#endif -static -void fini_data( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - bool do_on_remove, - bool update_entity_index, - bool is_delete, - bool deactivate) -{ - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_world_t *world = ecs_os_calloc(sizeof(ecs_world_t)); + ecs_assert(world != NULL, ECS_OUT_OF_MEMORY, NULL); + ecs_poly_init(world, ecs_world_t); - if (!data) { - return; - } + world->self = world; - if (do_on_remove) { - run_on_remove(world, table, data); - } + world->type_info = flecs_sparse_new(ecs_type_info_t); + world->id_index = ecs_map_new(ecs_id_record_t, 8); + flecs_observable_init(&world->observable); - int32_t count = flecs_table_data_count(data); - if (count) { - dtor_all_components(world, table, data, 0, count, - update_entity_index, is_delete); - } + world->queries = flecs_sparse_new(ecs_query_t); + world->triggers = flecs_sparse_new(ecs_trigger_t); + world->observers = flecs_sparse_new(ecs_observer_t); + world->fini_tasks = ecs_vector_new(ecs_entity_t, 0); - /* Sanity check */ - ecs_assert(ecs_vector_count(data->record_ptrs) == - ecs_vector_count(data->entities), ECS_INTERNAL_ERROR, NULL); + world->aliases = flecs_string_hashmap_new(ecs_entity_t); + world->symbols = flecs_string_hashmap_new(ecs_entity_t); + world->type_handles = ecs_map_new(ecs_entity_t, 0); - ecs_column_t *columns = data->columns; - if (columns) { - int32_t c, column_count = ecs_vector_count(table->storage_type); - for (c = 0; c < column_count; c ++) { - /* Sanity check */ - ecs_assert(!columns[c].data || (ecs_vector_count(columns[c].data) == - ecs_vector_count(data->entities)), ECS_INTERNAL_ERROR, NULL); + world->stats.time_scale = 1.0; + + monitors_init(&world->monitors); - ecs_vector_free(columns[c].data); - } - ecs_os_free(columns); - data->columns = NULL; + if (ecs_os_has_time()) { + ecs_os_get_time(&world->world_start_time); } - ecs_sw_column_t *sw_columns = data->sw_columns; - if (sw_columns) { - int32_t c, column_count = table->sw_column_count; - for (c = 0; c < column_count; c ++) { - flecs_switch_free(sw_columns[c].data); - } - ecs_os_free(sw_columns); - data->sw_columns = NULL; - } + flecs_stage_init(world, &world->stage); + ecs_set_stages(world, 1); - ecs_bs_column_t *bs_columns = data->bs_columns; - if (bs_columns) { - int32_t c, column_count = table->bs_column_count; - for (c = 0; c < column_count; c ++) { - flecs_bitset_deinit(&bs_columns[c].data); - } - ecs_os_free(bs_columns); - data->bs_columns = NULL; - } + init_store(world); + ecs_trace("table store initialized"); - ecs_vector_free(data->entities); - ecs_vector_free(data->record_ptrs); + flecs_bootstrap(world); - data->entities = NULL; - data->record_ptrs = NULL; + ecs_trace("world ready!"); + ecs_log_pop(); - if (deactivate && count) { - table_activate(world, table, false); - } + return world; } -/* Cleanup, no OnRemove, don't update entity index, don't deactivate table */ -void flecs_table_clear_data( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data) -{ - fini_data(world, table, data, false, false, false, false); -} +ecs_world_t *ecs_init(void) { + ecs_world_t *world = ecs_mini(); -/* Cleanup, no OnRemove, clear entity index, deactivate table */ -void flecs_table_clear_entities_silent( - ecs_world_t *world, - ecs_table_t *table) -{ - fini_data(world, table, &table->storage, false, true, false, true); +#ifdef FLECS_MODULE_H + ecs_trace("#[bold]import addons"); + ecs_log_push(); + ecs_trace("use ecs_mini to create world without importing addons"); +#ifdef FLECS_SYSTEM_H + ECS_IMPORT(world, FlecsSystem); +#endif +#ifdef FLECS_PIPELINE_H + ECS_IMPORT(world, FlecsPipeline); +#endif +#ifdef FLECS_TIMER_H + ECS_IMPORT(world, FlecsTimer); +#endif +#ifdef FLECS_META_H + ECS_IMPORT(world, FlecsMeta); +#endif +#ifdef FLECS_DOC_H + ECS_IMPORT(world, FlecsDoc); +#endif +#ifdef FLECS_COREDOC_H + ECS_IMPORT(world, FlecsCoreDoc); +#endif + ecs_trace("addons imported!"); + ecs_log_pop(); +#endif + + return world; } -/* Cleanup, run OnRemove, clear entity index, deactivate table */ -void flecs_table_clear_entities( - ecs_world_t *world, - ecs_table_t *table) +#define ARG(short, long, action)\ + if (i < argc) {\ + if (argv[i][0] == '-') {\ + if (argv[i][1] == '-') {\ + if (long && !strcmp(&argv[i][2], long ? long : "")) {\ + action;\ + parsed = true;\ + }\ + } else {\ + if (short && argv[i][1] == short) {\ + action;\ + parsed = true;\ + }\ + }\ + }\ + } + +ecs_world_t* ecs_init_w_args( + int argc, + char *argv[]) { - fini_data(world, table, &table->storage, true, true, false, true); + (void)argc; + (void)argv; + return ecs_init(); } -/* Cleanup, run OnRemove, delete from entity index, deactivate table */ -void flecs_table_delete_entities( - ecs_world_t *world, - ecs_table_t *table) +void ecs_quit( + ecs_world_t *world) { - fini_data(world, table, &table->storage, true, true, true, true); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + flecs_stage_from_world(&world); + world->should_quit = true; } -/* Unset all components in table. This function is called before a table is - * deleted, and invokes all UnSet handlers, if any */ -void flecs_table_remove_actions( - ecs_world_t *world, - ecs_table_t *table) +bool ecs_should_quit( + const ecs_world_t *world) { - (void)world; - run_on_remove(world, table, &table->storage); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + world = ecs_get_world(world); + return world->should_quit; } -/* Free table resources. */ -void flecs_table_free( +void flecs_notify_tables( ecs_world_t *world, - ecs_table_t *table) + ecs_id_t id, + ecs_table_event_t *event) { - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - (void)world; - -#ifndef NDEBUG - char *expr = ecs_type_str(world, table->type); - ecs_dbg_1("table #[green][%s]#[normal] deleted", expr); - ecs_os_free(expr); -#endif + ecs_poly_assert(world, ecs_world_t); - /* Cleanup data, no OnRemove, delete from entity index, don't deactivate */ - fini_data(world, table, &table->storage, false, true, true, false); + /* If no id is specified, broadcast to all tables */ + if (!id) { + ecs_sparse_t *tables = world->store.tables; + int32_t i, count = flecs_sparse_count(tables); + for (i = 0; i < count; i ++) { + ecs_table_t *table = flecs_sparse_get_dense(tables, ecs_table_t, i); + flecs_table_notify(world, table, event); + } - flecs_table_clear_edges(world, table); + /* If id is specified, only broadcast to tables with id */ + } else { + ecs_id_record_t *idr = flecs_get_id_record(world, id); + if (!idr) { + return; + } - flecs_unregister_table(world, table); - - ecs_vector_free(table->queries); - ecs_os_free(table->dirty_state); - ecs_os_free(table->storage_map); + const ecs_table_record_t *tables = flecs_id_record_tables(idr); + int32_t i, count = flecs_id_record_count(idr); + for (i = 0; i < count; i ++) { + flecs_table_notify(world, tables[i].table, event); + } - if (table->c_info) { - ecs_os_free(table->c_info); + tables = flecs_id_record_empty_tables(idr); + count = flecs_id_record_empty_count(idr); + for (i = 0; i < count; i ++) { + flecs_table_notify(world, tables[i].table, event); + } } +} - table->id = 0; +void ecs_default_ctor( + ecs_world_t *world, ecs_entity_t component, const ecs_entity_t *entity_ptr, + void *ptr, size_t size, int32_t count, void *ctx) +{ + (void)world; (void)component; (void)entity_ptr; (void)ctx; + ecs_os_memset(ptr, 0, flecs_from_size_t(size) * count); } -/* Free table type. Do this separately from freeing the table as types can be - * in use by application destructors. */ -void flecs_table_free_type( - ecs_table_t *table) +static +void default_copy_ctor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, const void *src_ptr, + size_t size, int32_t count, void *ctx) { - ecs_vector_free((ecs_vector_t*)table->type); + callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); + callbacks->copy(world, component, dst_entity, src_entity, dst_ptr, src_ptr, + size, count, ctx); } -/* Reset a table to its initial state. */ -void flecs_table_reset( - ecs_world_t *world, - ecs_table_t *table) +static +void default_move_ctor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) { - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - flecs_table_clear_edges(world, table); + callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); + callbacks->move(world, component, dst_entity, src_entity, dst_ptr, src_ptr, + size, count, ctx); } static -void mark_table_dirty( - ecs_table_t *table, - int32_t index) +void default_ctor_w_move_w_dtor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) { - if (table->dirty_state) { - table->dirty_state[index] ++; - } + callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); + callbacks->move(world, component, dst_entity, src_entity, dst_ptr, src_ptr, + size, count, ctx); + callbacks->dtor(world, component, src_entity, src_ptr, size, count, ctx); } -void flecs_table_mark_dirty( - ecs_world_t *world, - ecs_table_t *table, - ecs_entity_t component) +static +void default_move_ctor_w_dtor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) { - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + callbacks->move_ctor(world, component, callbacks, dst_entity, src_entity, + dst_ptr, src_ptr, size, count, ctx); + callbacks->dtor(world, component, src_entity, src_ptr, size, count, ctx); +} - if (table->dirty_state) { - int32_t index = ecs_type_match(world, table->storage_table, - table->storage_type, 0, component, 0, 0, 0, NULL, NULL); - ecs_assert(index != -1, ECS_INTERNAL_ERROR, NULL); - table->dirty_state[index] ++; - } +static +void default_ctor_w_move_dtor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) +{ + callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); + callbacks->move_dtor(world, component, callbacks, dst_entity, src_entity, + dst_ptr, src_ptr, size, count, ctx); } static -void move_switch_columns( - ecs_table_t *new_table, - ecs_data_t *new_data, - int32_t new_index, - ecs_table_t *old_table, - ecs_data_t *old_data, - int32_t old_index, - int32_t count) +void default_move( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) { - int32_t i_old = 0, old_column_count = old_table->sw_column_count; - int32_t i_new = 0, new_column_count = new_table->sw_column_count; + callbacks->move(world, component, dst_entity, src_entity, + dst_ptr, src_ptr, size, count, ctx); +} - if (!old_column_count || !new_column_count) { - return; - } +static +void default_dtor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) +{ + (void)callbacks; + (void)src_entity; - ecs_sw_column_t *old_columns = old_data->sw_columns; - ecs_sw_column_t *new_columns = new_data->sw_columns; + /* When there is no move, destruct the destination component & memcpy the + * component to dst. The src component does not have to be destructed when + * a component has a trivial move. */ + callbacks->dtor(world, component, dst_entity, dst_ptr, size, count, ctx); + ecs_os_memcpy(dst_ptr, src_ptr, flecs_from_size_t(size) * count); +} - ecs_type_t new_type = new_table->type; - ecs_type_t old_type = old_table->type; +static +void default_move_w_dtor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) +{ + /* If a component has a move, the move will take care of memcpying the data + * and destroying any data in dst. Because this is not a trivial move, the + * src component must also be destructed. */ + callbacks->move(world, component, dst_entity, src_entity, + dst_ptr, src_ptr, size, count, ctx); + callbacks->dtor(world, component, src_entity, src_ptr, size, count, ctx); +} - int32_t offset_new = new_table->sw_column_offset; - int32_t offset_old = old_table->sw_column_offset; +void ecs_set_component_actions_w_id( + ecs_world_t *world, + ecs_entity_t component, + EcsComponentLifecycle *lifecycle) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + flecs_stage_from_world(&world); - ecs_id_t *new_ids = ecs_vector_first(new_type, ecs_id_t); - ecs_id_t *old_ids = ecs_vector_first(old_type, ecs_id_t); + const EcsComponent *component_ptr = ecs_get(world, component, EcsComponent); - for (; (i_new < new_column_count) && (i_old < old_column_count);) { - ecs_entity_t new_id = new_ids[i_new + offset_new]; - ecs_entity_t old_id = old_ids[i_old + offset_old]; + /* Cannot register lifecycle actions for things that aren't a component */ + ecs_assert(component_ptr != NULL, ECS_INVALID_PARAMETER, NULL); - if (new_id == old_id) { - ecs_switch_t *old_switch = old_columns[i_old].data; - ecs_switch_t *new_switch = new_columns[i_new].data; + /* Cannot register lifecycle actions for components with size 0 */ + ecs_assert(component_ptr->size != 0, ECS_INVALID_PARAMETER, NULL); - flecs_switch_ensure(new_switch, new_index + count); + ecs_type_info_t *c_info = flecs_get_or_create_c_info(world, component); + ecs_assert(c_info != NULL, ECS_INTERNAL_ERROR, NULL); - int i; - for (i = 0; i < count; i ++) { - uint64_t value = flecs_switch_get(old_switch, old_index + i); - flecs_switch_set(new_switch, new_index + i, value); + if (c_info->lifecycle_set) { + ecs_assert(c_info->component == component, ECS_INTERNAL_ERROR, NULL); + ecs_assert(c_info->lifecycle.ctor == lifecycle->ctor, + ECS_INCONSISTENT_COMPONENT_ACTION, NULL); + ecs_assert(c_info->lifecycle.dtor == lifecycle->dtor, + ECS_INCONSISTENT_COMPONENT_ACTION, NULL); + ecs_assert(c_info->lifecycle.copy == lifecycle->copy, + ECS_INCONSISTENT_COMPONENT_ACTION, NULL); + ecs_assert(c_info->lifecycle.move == lifecycle->move, + ECS_INCONSISTENT_COMPONENT_ACTION, NULL); + } else { + c_info->component = component; + c_info->lifecycle = *lifecycle; + c_info->lifecycle_set = true; + c_info->size = component_ptr->size; + c_info->alignment = component_ptr->alignment; + + /* If no constructor is set, invoking any of the other lifecycle actions + * is not safe as they will potentially access uninitialized memory. For + * ease of use, if no constructor is specified, set a default one that + * initializes the component to 0. */ + if (!lifecycle->ctor && + (lifecycle->dtor || lifecycle->copy || lifecycle->move)) + { + c_info->lifecycle.ctor = ecs_default_ctor; + } + + /* Set default copy ctor, move ctor and merge */ + if (lifecycle->copy && !lifecycle->copy_ctor) { + c_info->lifecycle.copy_ctor = default_copy_ctor; + } + + if (lifecycle->move && !lifecycle->move_ctor) { + c_info->lifecycle.move_ctor = default_move_ctor; + } + + if (!lifecycle->ctor_move_dtor) { + if (lifecycle->move) { + if (lifecycle->dtor) { + if (lifecycle->move_ctor) { + /* If an explicit move ctor has been set, use callback + * that uses the move ctor vs. using a ctor+move */ + c_info->lifecycle.ctor_move_dtor = + default_move_ctor_w_dtor; + } else if (lifecycle->move_dtor) { + /* If no explicit move_dtor has been set, use callback + * that uses the move dtor vs. using a move+dtor */ + c_info->lifecycle.ctor_move_dtor = + default_ctor_w_move_dtor; + } else { + /* If neither move_ctor nor move_dtor has been set, use + * combination of ctor + move + dtor */ + c_info->lifecycle.ctor_move_dtor = + default_ctor_w_move_w_dtor; + } + } else { + /* If no dtor has been set, this is just a move ctor */ + c_info->lifecycle.ctor_move_dtor = + c_info->lifecycle.move_ctor; + } } } - i_new += new_id <= old_id; - i_old += new_id >= old_id; + if (!lifecycle->move_dtor) { + if (lifecycle->move) { + if (lifecycle->dtor) { + c_info->lifecycle.move_dtor = default_move_w_dtor; + } else { + c_info->lifecycle.move_dtor = default_move; + } + } else { + if (lifecycle->dtor) { + c_info->lifecycle.move_dtor = default_dtor; + } + } + } + + /* Broadcast to all tables since we need to register a ctor for every + * table that uses the component as itself, as predicate or as object. + * The latter is what makes selecting the right set of tables complex, + * as it depends on the predicate of a pair whether the object is used + * as the component type or not. + * A more selective approach requires a more expressive notification + * framework. */ + flecs_notify_tables(world, 0, &(ecs_table_event_t) { + .kind = EcsTableComponentInfo, + .component = component + }); } } -static -void move_bitset_columns( - ecs_table_t *new_table, - ecs_data_t *new_data, - int32_t new_index, - ecs_table_t *old_table, - ecs_data_t *old_data, - int32_t old_index, - int32_t count) +bool ecs_component_has_actions( + const ecs_world_t *world, + ecs_entity_t component) { - int32_t i_old = 0, old_column_count = old_table->bs_column_count; - int32_t i_new = 0, new_column_count = new_table->bs_column_count; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + world = ecs_get_world(world); - if (!old_column_count || !new_column_count) { - return; - } + const ecs_type_info_t *c_info = flecs_get_c_info(world, component); + return (c_info != NULL) && c_info->lifecycle_set; +} - ecs_bs_column_t *old_columns = old_data->bs_columns; - ecs_bs_column_t *new_columns = new_data->bs_columns; +void ecs_atfini( + ecs_world_t *world, + ecs_fini_action_t action, + void *ctx) +{ + ecs_poly_assert(world, ecs_world_t); - ecs_type_t new_type = new_table->type; - ecs_type_t old_type = old_table->type; + ecs_assert(action != NULL, ECS_INTERNAL_ERROR, NULL); - int32_t offset_new = new_table->bs_column_offset; - int32_t offset_old = old_table->bs_column_offset; + ecs_action_elem_t *elem = ecs_vector_add(&world->fini_actions, + ecs_action_elem_t); + ecs_assert(elem != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); - ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); + elem->action = action; + elem->ctx = ctx; +} - for (; (i_new < new_column_count) && (i_old < old_column_count);) { - ecs_entity_t new_component = new_components[i_new + offset_new]; - ecs_entity_t old_component = old_components[i_old + offset_old]; +void ecs_run_post_frame( + ecs_world_t *world, + ecs_fini_action_t action, + void *ctx) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(action != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_stage_t *stage = flecs_stage_from_world(&world); - if (new_component == old_component) { - ecs_bitset_t *old_bs = &old_columns[i_old].data; - ecs_bitset_t *new_bs = &new_columns[i_new].data; + ecs_action_elem_t *elem = ecs_vector_add(&stage->post_frame_actions, + ecs_action_elem_t); + ecs_assert(elem != NULL, ECS_INTERNAL_ERROR, NULL); - flecs_bitset_ensure(new_bs, new_index + count); + elem->action = action; + elem->ctx = ctx; +} - int i; - for (i = 0; i < count; i ++) { - uint64_t value = flecs_bitset_get(old_bs, old_index + i); - flecs_bitset_set(new_bs, new_index + i, value); - } - } +/* Unset data in tables */ +static +void fini_unset_tables( + ecs_world_t *world) +{ + ecs_sparse_t *tables = world->store.tables; + int32_t i, count = flecs_sparse_count(tables); - i_new += new_component <= old_component; - i_old += new_component >= old_component; + for (i = 0; i < count; i ++) { + ecs_table_t *table = flecs_sparse_get_dense(tables, ecs_table_t, i); + flecs_table_remove_actions(world, table); } } +/* Invoke fini actions */ static -void grow_column( - ecs_world_t *world, - ecs_entity_t *entities, - ecs_column_t *column, - ecs_type_info_t *c_info, - int32_t to_add, - int32_t new_size, - bool construct) +void fini_actions( + ecs_world_t *world) { - ecs_vector_t *vec = column->data; - int16_t alignment = column->alignment; - - int32_t size = column->size; - int32_t count = ecs_vector_count(vec); - int32_t old_size = ecs_vector_size(vec); - int32_t new_count = count + to_add; - bool can_realloc = new_size != old_size; - - ecs_assert(new_size >= new_count, ECS_INTERNAL_ERROR, NULL); - - /* If the array could possibly realloc and the component has a move action - * defined, move old elements manually */ - ecs_move_t move; - if (c_info && count && can_realloc && (move = c_info->lifecycle.move)) { - ecs_xtor_t ctor = c_info->lifecycle.ctor; - ecs_assert(ctor != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_vector_each(world->fini_actions, ecs_action_elem_t, elem, { + elem->action(world, elem->ctx); + }); - /* Create new vector */ - ecs_vector_t *new_vec = ecs_vector_new_t(size, alignment, new_size); - ecs_vector_set_count_t(&new_vec, size, alignment, new_count); + ecs_vector_free(world->fini_actions); +} - void *old_buffer = ecs_vector_first_t( - vec, size, alignment); +/* Cleanup component lifecycle callbacks & systems */ +static +void fini_component_lifecycle( + ecs_world_t *world) +{ + flecs_sparse_free(world->type_info); +} - void *new_buffer = ecs_vector_first_t( - new_vec, size, alignment); +/* Cleanup queries */ +static +void fini_queries( + ecs_world_t *world) +{ + int32_t i, count = flecs_sparse_count(world->queries); + for (i = 0; i < count; i ++) { + ecs_query_t *query = flecs_sparse_get_dense(world->queries, ecs_query_t, 0); + ecs_query_fini(query); + } + flecs_sparse_free(world->queries); +} - /* First construct elements (old and new) in new buffer */ - ctor(world, c_info->component, entities, new_buffer, - flecs_to_size_t(size), construct ? new_count : count, - c_info->lifecycle.ctx); - - /* Move old elements */ - move(world, c_info->component, entities, entities, - new_buffer, old_buffer, flecs_to_size_t(size), count, - c_info->lifecycle.ctx); +static +void fini_observers( + ecs_world_t *world) +{ + flecs_sparse_free(world->observers); +} - /* Free old vector */ - ecs_vector_free(vec); - column->data = new_vec; - } else { - /* If array won't realloc or has no move, simply add new elements */ - if (can_realloc) { - ecs_vector_set_size_t(&vec, size, alignment, new_size); - } +/* Cleanup stages */ +static +void fini_stages( + ecs_world_t *world) +{ + flecs_stage_deinit(world, &world->stage); + ecs_set_stages(world, 0); +} - void *elem = ecs_vector_addn_t(&vec, size, alignment, to_add); +/* Cleanup id index */ +static +void fini_id_index( + ecs_world_t *world) +{ + ecs_map_iter_t it = ecs_map_iter(world->id_index); + ecs_id_record_t *idr; + while ((idr = ecs_map_next(&it, ecs_id_record_t, NULL))) { + ecs_table_cache_fini(&idr->cache); + ecs_map_free(idr->add_refs); + ecs_map_free(idr->remove_refs); + } - ecs_xtor_t ctor; - if (construct && c_info && (ctor = c_info->lifecycle.ctor)) { - /* If new elements need to be constructed and component has a - * constructor, construct */ - ctor(world, c_info->component, &entities[count], elem, - flecs_to_size_t(size), to_add, c_info->lifecycle.ctx); - } + ecs_map_free(world->id_index); +} - column->data = vec; +/* Cleanup aliases & symbols */ +static +void fini_aliases( + ecs_hashmap_t *map) +{ + flecs_hashmap_iter_t it = flecs_hashmap_iter(*map); + ecs_hashed_string_t *key; + while (flecs_hashmap_next_w_key(&it, ecs_hashed_string_t, &key, ecs_entity_t)) { + ecs_os_free(key->value); } - - ecs_assert(ecs_vector_size(column->data) == new_size, - ECS_INTERNAL_ERROR, NULL); + + flecs_hashmap_free(*map); } +/* Cleanup misc structures */ static -int32_t grow_data( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t to_add, - int32_t size, - const ecs_entity_t *ids) +void fini_misc( + ecs_world_t *world) { - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_map_free(world->type_handles); + ecs_vector_free(world->fini_tasks); + monitors_fini(&world->monitors); +} - int32_t cur_count = flecs_table_data_count(data); - int32_t column_count = ecs_vector_count(table->storage_type); - int32_t sw_column_count = table->sw_column_count; - int32_t bs_column_count = table->bs_column_count; - ecs_column_t *columns = data->columns; - ecs_sw_column_t *sw_columns = data->sw_columns; - ecs_bs_column_t *bs_columns = data->bs_columns; +/* The destroyer of worlds */ +int ecs_fini( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); + ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); - /* Add record to record ptr array */ - ecs_vector_set_size(&data->record_ptrs, ecs_record_t*, size); - ecs_record_t **r = ecs_vector_addn(&data->record_ptrs, ecs_record_t*, to_add); - ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL); - if (ecs_vector_size(data->record_ptrs) > size) { - size = ecs_vector_size(data->record_ptrs); - } + ecs_trace("#[bold]shutting down world"); + ecs_log_push(); - /* Add entity to column with entity ids */ - ecs_vector_set_size(&data->entities, ecs_entity_t, size); - ecs_entity_t *e = ecs_vector_addn(&data->entities, ecs_entity_t, to_add); - ecs_assert(e != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ecs_vector_size(data->entities) == size, ECS_INTERNAL_ERROR, NULL); - - /* Initialize entity ids and record ptrs */ - int32_t i; - if (ids) { - for (i = 0; i < to_add; i ++) { - e[i] = ids[i]; - } - } else { - ecs_os_memset(e, 0, ECS_SIZEOF(ecs_entity_t) * to_add); - } - ecs_os_memset(r, 0, ECS_SIZEOF(ecs_record_t*) * to_add); + world->is_fini = true; - /* Add elements to each column array */ - ecs_type_info_t **c_info_array = table->c_info; - ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); - for (i = 0; i < column_count; i ++) { - ecs_column_t *column = &columns[i]; - ecs_assert(column->size != 0, ECS_INTERNAL_ERROR, NULL); + /* Operations invoked during UnSet/OnRemove/destructors are deferred and + * will be discarded after world cleanup */ + ecs_defer_begin(world); - ecs_type_info_t *c_info = NULL; - if (c_info_array) { - c_info = c_info_array[i]; - } + /* Run UnSet/OnRemove actions for components while the store is still + * unmodified by cleanup. */ + fini_unset_tables(world); + + /* Run fini actions (simple callbacks ran when world is deleted) before + * destroying the storage */ + fini_actions(world); - grow_column(world, entities, column, c_info, to_add, size, true); - ecs_assert(ecs_vector_size(columns[i].data) == size, - ECS_INTERNAL_ERROR, NULL); - } + /* This will destroy all entities and components. After this point no more + * user code is executed. */ + fini_store(world); - /* Add elements to each switch column */ - for (i = 0; i < sw_column_count; i ++) { - ecs_switch_t *sw = sw_columns[i].data; - flecs_switch_addn(sw, to_add); - } + /* Purge deferred operations from the queue. This discards operations but + * makes sure that any resources in the queue are freed */ + flecs_defer_purge(world, &world->stage); - /* Add elements to each bitset column */ - for (i = 0; i < bs_column_count; i ++) { - ecs_bitset_t *bs = &bs_columns[i].data; - flecs_bitset_addn(bs, to_add); + /* Entity index is kept alive until this point so that user code can do + * validity checks on entity ids, even though after store cleanup the index + * will be empty, so all entity ids are invalid. */ + flecs_sparse_free(world->store.entity_index); + + if (world->locking_enabled) { + ecs_os_mutex_free(world->mutex); } - /* If the table is monitored indicate that there has been a change */ - mark_table_dirty(table, 0); - - if (!world->is_readonly && !cur_count) { - table_activate(world, table, true); - } + ecs_trace("table store deinitialized"); - table->alloc_count ++; + fini_stages(world); - /* Return index of first added entity */ - return cur_count; -} + fini_component_lifecycle(world); -static -void fast_append( - ecs_column_t *columns, - int32_t column_count) -{ - /* Add elements to each column array */ - int32_t i; - for (i = 0; i < column_count; i ++) { - ecs_column_t *column = &columns[i]; - int16_t size = column->size; - if (size) { - int16_t alignment = column->alignment; - ecs_vector_add_t(&column->data, size, alignment); - } - } -} + fini_queries(world); -int32_t flecs_table_append( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - ecs_entity_t entity, - ecs_record_t *record, - bool construct) -{ - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + fini_observers(world); - /* Get count & size before growing entities array. This tells us whether the - * arrays will realloc */ - int32_t count = ecs_vector_count(data->entities); - int32_t size = ecs_vector_size(data->entities); - int32_t column_count = ecs_vector_count(table->storage_type); - ecs_column_t *columns = table->storage.columns; - - /* Grow buffer with entity ids, set new element to new entity */ - ecs_entity_t *e = ecs_vector_add(&data->entities, ecs_entity_t); - ecs_assert(e != NULL, ECS_INTERNAL_ERROR, NULL); - *e = entity; + fini_id_index(world); - /* Keep track of alloc count. This allows references to check if cached - * pointers need to be updated. */ - table->alloc_count += (count == size); + flecs_observable_fini(&world->observable); - /* Add record ptr to array with record ptrs */ - ecs_record_t **r = ecs_vector_add(&data->record_ptrs, ecs_record_t*); - ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL); - *r = record; - - /* If the table is monitored indicate that there has been a change */ - mark_table_dirty(table, 0); + flecs_sparse_free(world->triggers); - /* If this is the first entity in this table, signal queries so that the - * table moves from an inactive table to an active table. */ - if (!world->is_readonly && !count) { - table_activate(world, table, true); - } + fini_aliases(&world->aliases); + + fini_aliases(&world->symbols); + + fini_misc(world); - ecs_assert(count >= 0, ECS_INTERNAL_ERROR, NULL); + flecs_increase_timer_resolution(0); - /* Fast path: no switch columns, no lifecycle actions */ - if (!(table->flags & EcsTableIsComplex)) { - fast_append(columns, column_count); - return count; - } + /* End of the world */ + ecs_poly_free(world, ecs_world_t); - int32_t sw_column_count = table->sw_column_count; - int32_t bs_column_count = table->bs_column_count; - ecs_sw_column_t *sw_columns = table->storage.sw_columns; - ecs_bs_column_t *bs_columns = table->storage.bs_columns; + ecs_os_fini(); - ecs_type_info_t **c_info_array = table->c_info; - ecs_entity_t *entities = ecs_vector_first( - data->entities, ecs_entity_t); + ecs_trace("world destroyed, bye!"); + ecs_log_pop(); - /* Reobtain size to ensure that the columns have the same size as the - * entities and record vectors. This keeps reasoning about when allocations - * occur easier. */ - size = ecs_vector_size(data->entities); + return 0; +} - /* Grow component arrays with 1 element */ - int32_t i; - for (i = 0; i < column_count; i ++) { - ecs_column_t *column = &columns[i]; - ecs_assert(column->size != 0, ECS_INTERNAL_ERROR, NULL); +void ecs_dim( + ecs_world_t *world, + int32_t entity_count) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_eis_set_size(world, entity_count + ECS_HI_COMPONENT_ID); +} - ecs_type_info_t *c_info = NULL; - if (c_info_array) { - c_info = c_info_array[i]; - } +void flecs_eval_component_monitors( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + eval_component_monitor(world); +} - grow_column(world, entities, column, c_info, 1, size, construct); - - ecs_assert( - ecs_vector_size(columns[i].data) == ecs_vector_size(data->entities), - ECS_INTERNAL_ERROR, NULL); - - ecs_assert( - ecs_vector_count(columns[i].data) == ecs_vector_count(data->entities), - ECS_INTERNAL_ERROR, NULL); - } +void ecs_measure_frame_time( + ecs_world_t *world, + bool enable) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(ecs_os_has_time(), ECS_MISSING_OS_API, NULL); - /* Add element to each switch column */ - for (i = 0; i < sw_column_count; i ++) { - ecs_assert(sw_columns != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_switch_t *sw = sw_columns[i].data; - flecs_switch_add(sw); + if (world->stats.target_fps == 0.0f || enable) { + world->measure_frame_time = enable; } +} - /* Add element to each bitset column */ - for (i = 0; i < bs_column_count; i ++) { - ecs_assert(bs_columns != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_bitset_t *bs = &bs_columns[i].data; - flecs_bitset_addn(bs, 1); - } +void ecs_measure_system_time( + ecs_world_t *world, + bool enable) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(ecs_os_has_time(), ECS_MISSING_OS_API, NULL); + world->measure_system_time = enable; +} - return count; +/* Increase timer resolution based on target fps */ +static +void set_timer_resolution( + FLECS_FLOAT fps) +{ + if(fps >= 60.0f) flecs_increase_timer_resolution(1); + else flecs_increase_timer_resolution(0); } -static -void fast_delete_last( - ecs_column_t *columns, - int32_t column_count) +void ecs_set_target_fps( + ecs_world_t *world, + FLECS_FLOAT fps) { - int i; - for (i = 0; i < column_count; i ++) { - ecs_column_t *column = &columns[i]; - ecs_vector_remove_last(column->data); - } + ecs_poly_assert(world, ecs_world_t); + ecs_assert(ecs_os_has_time(), ECS_MISSING_OS_API, NULL); + + ecs_measure_frame_time(world, true); + world->stats.target_fps = fps; + set_timer_resolution(fps); } -static -void fast_delete( - ecs_column_t *columns, - int32_t column_count, - int32_t index) +void* ecs_get_context( + const ecs_world_t *world) { - int i; - for (i = 0; i < column_count; i ++) { - ecs_column_t *column = &columns[i]; - int16_t size = column->size; - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + world = ecs_get_world(world); + return world->context; +} - int16_t alignment = column->alignment; - ecs_vector_remove_t(column->data, size, alignment, index); - } +void ecs_set_context( + ecs_world_t *world, + void *context) +{ + ecs_poly_assert(world, ecs_world_t); + world->context = context; } -void flecs_table_delete( +void ecs_set_entity_range( ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t index, - bool destruct) + ecs_entity_t id_start, + ecs_entity_t id_end) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_poly_assert(world, ecs_world_t); + ecs_assert(!id_end || id_end > id_start, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!id_end || id_end > world->stats.last_id, ECS_INVALID_PARAMETER, NULL); - ecs_vector_t *v_entities = data->entities; - int32_t count = ecs_vector_count(v_entities); + if (world->stats.last_id < id_start) { + world->stats.last_id = id_start - 1; + } - ecs_assert(count > 0, ECS_INTERNAL_ERROR, NULL); - count --; - ecs_assert(index <= count, ECS_INTERNAL_ERROR, NULL); + world->stats.min_id = id_start; + world->stats.max_id = id_end; +} - /* Move last entity id to index */ - ecs_entity_t *entities = ecs_vector_first(v_entities, ecs_entity_t); - ecs_entity_t entity_to_move = entities[count]; - ecs_entity_t entity_to_delete = entities[index]; - entities[index] = entity_to_move; - ecs_vector_remove_last(v_entities); +bool ecs_enable_range_check( + ecs_world_t *world, + bool enable) +{ + ecs_poly_assert(world, ecs_world_t); + bool old_value = world->range_check_enabled; + world->range_check_enabled = enable; + return old_value; +} - /* Move last record ptr to index */ - ecs_vector_t *v_records = data->record_ptrs; - ecs_assert(count < ecs_vector_count(v_records), ECS_INTERNAL_ERROR, NULL); +int32_t ecs_get_threads( + ecs_world_t *world) +{ + return ecs_vector_count(world->worker_stages); +} - ecs_record_t **records = ecs_vector_first(v_records, ecs_record_t*); - ecs_record_t *record_to_move = records[count]; - records[index] = record_to_move; - ecs_vector_remove_last(v_records); +bool ecs_enable_locking( + ecs_world_t *world, + bool enable) +{ + ecs_poly_assert(world, ecs_world_t); - /* Update record of moved entity in entity index */ - if (index != count) { - if (record_to_move) { - if (record_to_move->row >= 0) { - record_to_move->row = index + 1; - } else { - record_to_move->row = -(index + 1); - } - ecs_assert(record_to_move->table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(record_to_move->table == table, ECS_INTERNAL_ERROR, NULL); + if (enable) { + if (!world->locking_enabled) { + world->mutex = ecs_os_mutex_new(); + world->thr_sync = ecs_os_mutex_new(); + world->thr_cond = ecs_os_cond_new(); + } + } else { + if (world->locking_enabled) { + ecs_os_mutex_free(world->mutex); + ecs_os_mutex_free(world->thr_sync); + ecs_os_cond_free(world->thr_cond); } - } - - /* If the table is monitored indicate that there has been a change */ - mark_table_dirty(table, 0); - - /* If table is empty, deactivate it */ - if (!count) { - table_activate(world, table, false); } - /* Destruct component data */ - ecs_type_info_t **c_info_array = table->c_info; - ecs_column_t *columns = data->columns; - int32_t column_count = ecs_vector_count(table->storage_type); - int32_t i; + bool old = world->locking_enabled; + world->locking_enabled = enable; + return old; +} - /* If this is a table without lifecycle callbacks or special columns, take - * fast path that just remove an element from the array(s) */ - if (!(table->flags & EcsTableIsComplex)) { - if (index == count) { - fast_delete_last(columns, column_count); - } else { - fast_delete(columns, column_count, index); - } +void ecs_lock( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); + ecs_os_mutex_lock(world->mutex); +} - return; - } +void ecs_unlock( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); + ecs_os_mutex_unlock(world->mutex); +} - /* Last element, destruct & remove */ - if (index == count) { - /* If table has component destructors, invoke */ - if (destruct && (table->flags & EcsTableHasDtors)) { - ecs_assert(c_info_array != NULL, ECS_INTERNAL_ERROR, NULL); - - for (i = 0; i < column_count; i ++) { - ecs_type_info_t *c_info = c_info_array[i]; - ecs_xtor_t dtor; - if (c_info && (dtor = c_info->lifecycle.dtor)) { - ecs_size_t size = c_info->size; - ecs_size_t alignment = c_info->alignment; - dtor(world, c_info->component, &entity_to_delete, - ecs_vector_last_t(columns[i].data, size, alignment), - flecs_to_size_t(size), 1, c_info->lifecycle.ctx); - } - } - } +void ecs_begin_wait( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); + ecs_os_mutex_lock(world->thr_sync); + ecs_os_cond_wait(world->thr_cond, world->thr_sync); +} - fast_delete_last(columns, column_count); +void ecs_end_wait( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); + ecs_os_mutex_unlock(world->thr_sync); +} - /* Not last element, move last element to deleted element & destruct */ - } else { - /* If table has component destructors, invoke */ - if (destruct && (table->flags & (EcsTableHasDtors | EcsTableHasMove))) { - ecs_assert(c_info_array != NULL, ECS_INTERNAL_ERROR, NULL); +const ecs_type_info_t* flecs_get_c_info( + const ecs_world_t *world, + ecs_entity_t component) +{ + ecs_poly_assert(world, ecs_world_t); - for (i = 0; i < column_count; i ++) { - ecs_column_t *column = &columns[i]; - ecs_size_t size = column->size; - ecs_size_t align = column->alignment; - ecs_vector_t *vec = column->data; - void *dst = ecs_vector_get_t(vec, size, align, index); - void *src = ecs_vector_last_t(vec, size, align); - - ecs_type_info_t *c_info = c_info_array[i]; - ecs_move_ctor_t move_dtor; - if (c_info && (move_dtor = c_info->lifecycle.move_dtor)) { - move_dtor(world, c_info->component, &c_info->lifecycle, - &entity_to_move, &entity_to_delete, dst, src, - flecs_to_size_t(size), 1, c_info->lifecycle.ctx); - } else { - ecs_os_memcpy(dst, src, size); - } + ecs_assert(component != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!(component & ECS_ROLE_MASK), ECS_INTERNAL_ERROR, NULL); - ecs_vector_remove_last(vec); - } + return flecs_sparse_get(world->type_info, ecs_type_info_t, component); +} - } else { - fast_delete(columns, column_count, index); - } - } +ecs_type_info_t* flecs_get_or_create_c_info( + ecs_world_t *world, + ecs_entity_t component) +{ + ecs_poly_assert(world, ecs_world_t); - /* Remove elements from switch columns */ - ecs_sw_column_t *sw_columns = data->sw_columns; - int32_t sw_column_count = table->sw_column_count; - for (i = 0; i < sw_column_count; i ++) { - flecs_switch_remove(sw_columns[i].data, index); + const ecs_type_info_t *c_info = flecs_get_c_info(world, component); + ecs_type_info_t *c_info_mut = NULL; + if (!c_info) { + c_info_mut = flecs_sparse_ensure( + world->type_info, ecs_type_info_t, component); + ecs_assert(c_info_mut != NULL, ECS_INTERNAL_ERROR, NULL); + } else { + c_info_mut = (ecs_type_info_t*)c_info; } - /* Remove elements from bitset columns */ - ecs_bs_column_t *bs_columns = data->bs_columns; - int32_t bs_column_count = table->bs_column_count; - for (i = 0; i < bs_column_count; i ++) { - flecs_bitset_remove(&bs_columns[i].data, index); - } + return c_info_mut; } static -void fast_move( - ecs_table_t *new_table, - ecs_data_t *new_data, - int32_t new_index, - ecs_table_t *old_table, - ecs_data_t *old_data, - int32_t old_index) +FLECS_FLOAT insert_sleep( + ecs_world_t *world, + ecs_time_t *stop) { - ecs_type_t new_type = new_table->storage_type; - ecs_type_t old_type = old_table->storage_type; - - int32_t i_new = 0, new_column_count = ecs_vector_count(new_table->storage_type); - int32_t i_old = 0, old_column_count = ecs_vector_count(old_table->storage_type); - ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); - ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); + ecs_poly_assert(world, ecs_world_t); - ecs_column_t *old_columns = old_data->columns; - ecs_column_t *new_columns = new_data->columns; + ecs_time_t start = *stop; + FLECS_FLOAT delta_time = (FLECS_FLOAT)ecs_time_measure(stop); + if (world->stats.target_fps == (FLECS_FLOAT)0.0) { + return delta_time; + } - for (; (i_new < new_column_count) && (i_old < old_column_count);) { - ecs_entity_t new_component = new_components[i_new]; - ecs_entity_t old_component = old_components[i_old]; + FLECS_FLOAT target_delta_time = + ((FLECS_FLOAT)1.0 / (FLECS_FLOAT)world->stats.target_fps); - if (new_component == old_component) { - ecs_column_t *new_column = &new_columns[i_new]; - ecs_column_t *old_column = &old_columns[i_old]; - int16_t size = new_column->size; - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + /* Calculate the time we need to sleep by taking the measured delta from the + * previous frame, and subtracting it from target_delta_time. */ + FLECS_FLOAT sleep = target_delta_time - delta_time; - int16_t alignment = new_column->alignment; - void *dst = ecs_vector_get_t( - new_column->data, size, alignment, new_index); - void *src = ecs_vector_get_t( - old_column->data, size, alignment, old_index); + /* Pick a sleep interval that is 20 times lower than the time one frame + * should take. This means that this function at most iterates 20 times in + * a busy loop */ + FLECS_FLOAT sleep_time = sleep / (FLECS_FLOAT)4.0; - ecs_assert(dst != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(src != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_os_memcpy(dst, src, size); + do { + /* Only call sleep when sleep_time is not 0. On some platforms, even + * a sleep with a timeout of 0 can cause stutter. */ + if (sleep_time != 0) { + ecs_sleepf((double)sleep_time); } - i_new += new_component <= old_component; - i_old += new_component >= old_component; - } + ecs_time_t now = start; + delta_time = (FLECS_FLOAT)ecs_time_measure(&now); + } while ((target_delta_time - delta_time) > + (sleep_time / (FLECS_FLOAT)2.0)); + + return delta_time; } -void flecs_table_move( +static +FLECS_FLOAT start_measure_frame( ecs_world_t *world, - ecs_entity_t dst_entity, - ecs_entity_t src_entity, - ecs_table_t *new_table, - ecs_data_t *new_data, - int32_t new_index, - ecs_table_t *old_table, - ecs_data_t *old_data, - int32_t old_index, - bool construct) + FLECS_FLOAT user_delta_time) { - ecs_assert(new_table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(old_table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!new_table->lock, ECS_LOCKED_STORAGE, NULL); - ecs_assert(!old_table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_poly_assert(world, ecs_world_t); - ecs_assert(old_index >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(new_index >= 0, ECS_INTERNAL_ERROR, NULL); + FLECS_FLOAT delta_time = 0; - ecs_assert(old_data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(new_data != NULL, ECS_INTERNAL_ERROR, NULL); + if (world->measure_frame_time || (user_delta_time == 0)) { + ecs_time_t t = world->frame_start_time; + do { + if (world->frame_start_time.sec) { + delta_time = insert_sleep(world, &t); - if (!((new_table->flags | old_table->flags) & EcsTableIsComplex)) { - fast_move(new_table, new_data, new_index, old_table, old_data, old_index); - return; + ecs_time_measure(&t); + } else { + ecs_time_measure(&t); + if (world->stats.target_fps != 0) { + delta_time = (FLECS_FLOAT)1.0 / world->stats.target_fps; + } else { + /* Best guess */ + delta_time = (FLECS_FLOAT)1.0 / (FLECS_FLOAT)60.0; + } + } + + /* Keep trying while delta_time is zero */ + } while (delta_time == 0); + + world->frame_start_time = t; + + /* Keep track of total time passed in world */ + world->stats.world_time_total_raw += (FLECS_FLOAT)delta_time; } - move_switch_columns( - new_table, new_data, new_index, old_table, old_data, old_index, 1); + return (FLECS_FLOAT)delta_time; +} - move_bitset_columns( - new_table, new_data, new_index, old_table, old_data, old_index, 1); +static +void stop_measure_frame( + ecs_world_t* world) +{ + ecs_poly_assert(world, ecs_world_t); - bool same_entity = dst_entity == src_entity; + if (world->measure_frame_time) { + ecs_time_t t = world->frame_start_time; + world->stats.frame_time_total += (FLECS_FLOAT)ecs_time_measure(&t); + } +} - ecs_type_t new_type = new_table->storage_type; - ecs_type_t old_type = old_table->storage_type; +FLECS_FLOAT ecs_frame_begin( + ecs_world_t *world, + FLECS_FLOAT user_delta_time) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(world->is_readonly == false, ECS_INVALID_OPERATION, NULL); - int32_t i_new = 0, new_column_count = ecs_vector_count(new_table->storage_type); - int32_t i_old = 0, old_column_count = ecs_vector_count(old_table->storage_type); - ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); - ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); + ecs_assert(user_delta_time != 0 || ecs_os_has_time(), ECS_MISSING_OS_API, "get_time"); - ecs_column_t *old_columns = old_data->columns; - ecs_column_t *new_columns = new_data->columns; + if (world->locking_enabled) { + ecs_lock(world); + } - for (; (i_new < new_column_count) && (i_old < old_column_count);) { - ecs_entity_t new_component = new_components[i_new]; - ecs_entity_t old_component = old_components[i_old]; + /* Start measuring total frame time */ + FLECS_FLOAT delta_time = start_measure_frame(world, user_delta_time); + if (user_delta_time == 0) { + user_delta_time = delta_time; + } - if (new_component == old_component) { - ecs_column_t *new_column = &new_columns[i_new]; - ecs_column_t *old_column = &old_columns[i_old]; - int16_t size = new_column->size; - int16_t alignment = new_column->alignment; + world->stats.delta_time_raw = user_delta_time; + world->stats.delta_time = user_delta_time * world->stats.time_scale; - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + /* Keep track of total scaled time passed in world */ + world->stats.world_time_total += world->stats.delta_time; - void *dst = ecs_vector_get_t( - new_column->data, size, alignment, new_index); - void *src = ecs_vector_get_t( - old_column->data, size, alignment, old_index); + flecs_eval_component_monitors(world); - ecs_assert(dst != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(src != NULL, ECS_INTERNAL_ERROR, NULL); + return world->stats.delta_time; +} - ecs_type_info_t *cdata = new_table->c_info[i_new]; - if (same_entity) { - ecs_move_ctor_t callback; - if (cdata && (callback = cdata->lifecycle.ctor_move_dtor)) { - void *ctx = cdata->lifecycle.ctx; - /* ctor + move + dtor */ - callback(world, new_component, &cdata->lifecycle, - &dst_entity, &src_entity, - dst, src, flecs_to_size_t(size), 1, ctx); - } else { - ecs_os_memcpy(dst, src, size); - } - } else { - ecs_copy_ctor_t copy; - if (cdata && (copy = cdata->lifecycle.copy_ctor)) { - void *ctx = cdata->lifecycle.ctx; - copy(world, new_component, &cdata->lifecycle, - &dst_entity, &src_entity, - dst, src, flecs_to_size_t(size), 1, ctx); - } else { - ecs_os_memcpy(dst, src, size); - } - } - } else { - if (new_component < old_component) { - if (construct) { - ctor_component(world, new_table->c_info[i_new], - &new_columns[i_new], &dst_entity, new_index, 1); - } - } else { - dtor_component(world, old_table->c_info[i_old], - &old_columns[i_old], &src_entity, old_index, 1); - } - } +void ecs_frame_end( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(world->is_readonly == false, ECS_INVALID_OPERATION, NULL); - i_new += new_component <= old_component; - i_old += new_component >= old_component; - } + world->stats.frame_count_total ++; - if (construct) { - for (; (i_new < new_column_count); i_new ++) { - ctor_component(world, new_table->c_info[i_new], - &new_columns[i_new], &dst_entity, new_index, 1); - } - } + ecs_vector_each(world->worker_stages, ecs_stage_t, stage, { + flecs_stage_merge_post_frame(world, stage); + }); - for (; (i_old < old_column_count); i_old ++) { - dtor_component(world, old_table->c_info[i_old], - &old_columns[i_old], &src_entity, old_index, 1); + if (world->locking_enabled) { + ecs_unlock(world); + + ecs_os_mutex_lock(world->thr_sync); + ecs_os_cond_broadcast(world->thr_cond); + ecs_os_mutex_unlock(world->thr_sync); } + + stop_measure_frame(world); } -int32_t flecs_table_appendn( +const ecs_world_info_t* ecs_get_world_info( + const ecs_world_t *world) +{ + world = ecs_get_world(world); + return &world->stats; +} + +void flecs_notify_queries( ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t to_add, - const ecs_entity_t *ids) + ecs_query_event_t *event) { - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_poly_assert(world, ecs_world_t); - int32_t cur_count = flecs_table_data_count(data); - return grow_data(world, table, data, to_add, cur_count + to_add, ids); + int32_t i, count = flecs_sparse_count(world->queries); + for (i = 0; i < count; i ++) { + ecs_query_t *query = flecs_sparse_get_dense( + world->queries, ecs_query_t, i); + if (query->flags & EcsQueryIsSubquery) { + continue; + } + + flecs_query_notify(world, query, event); + } } -void flecs_table_set_size( +void flecs_delete_table( ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t size) + ecs_table_t *table) { - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_poly_assert(world, ecs_world_t); - int32_t cur_count = flecs_table_data_count(data); + /* Notify queries that table is to be removed */ + flecs_notify_queries( + world, &(ecs_query_event_t){ + .kind = EcsQueryTableUnmatch, + .table = table + }); - if (cur_count < size) { - grow_data(world, table, data, 0, size, NULL); - } -} + uint64_t id = table->id; -int32_t flecs_table_data_count( - const ecs_data_t *data) -{ - return data ? ecs_vector_count(data->entities) : 0; + /* Free resources associated with table */ + flecs_table_free(world, table); + flecs_table_free_type(table); + + /* Remove table from sparse set */ + ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL); + flecs_sparse_remove(world->store.tables, id); } static -void swap_switch_columns( +void register_table( + ecs_world_t *world, ecs_table_t *table, - ecs_data_t *data, - int32_t row_1, - int32_t row_2) + ecs_id_t id, + int32_t column) { - int32_t i = 0, column_count = table->sw_column_count; - if (!column_count) { - return; - } + ecs_id_record_t *idr = flecs_ensure_id_record(world, id); + ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_sw_column_t *columns = data->sw_columns; + ecs_table_record_t *tr = ecs_table_cache_get( + &idr->cache, ecs_table_record_t, table); + if (tr) { + /* Tables can be registered multiple times for wildcard caches */ + tr->count ++; + } else { + tr = ecs_table_cache_insert(&idr->cache, ecs_table_record_t, table); + tr->table = table; + tr->column = column; + tr->count = 1; + } - for (i = 0; i < column_count; i ++) { - ecs_switch_t *sw = columns[i].data; - flecs_switch_swap(sw, row_1, row_2); + /* Set flags if triggers are registered for table */ + if (flecs_triggers_for_id(world, id, EcsOnAdd)) { + table->flags |= EcsTableHasOnAdd; + } + if (flecs_triggers_for_id(world, id, EcsOnRemove)) { + table->flags |= EcsTableHasOnRemove; + } + if (flecs_triggers_for_id(world, id, EcsOnSet)) { + table->flags |= EcsTableHasOnSet; + } + if (flecs_triggers_for_id(world, id, EcsUnSet)) { + table->flags |= EcsTableHasUnSet; } } static -void swap_bitset_columns( +void unregister_table( + ecs_world_t *world, ecs_table_t *table, - ecs_data_t *data, - int32_t row_1, - int32_t row_2) + ecs_id_t id, + int32_t column) { - int32_t i = 0, column_count = table->bs_column_count; - if (!column_count) { + (void)column; + + ecs_id_record_t *idr = flecs_get_id_record(world, id); + if (!idr) { return; } - ecs_bs_column_t *columns = data->bs_columns; + ecs_table_cache_remove(&idr->cache, ecs_table_record_t, table); - for (i = 0; i < column_count; i ++) { - ecs_bitset_t *bs = &columns[i].data; - flecs_bitset_swap(bs, row_1, row_2); + if (ecs_table_cache_is_empty(&idr->cache)) { + flecs_clear_id_record(world, id); } } -void flecs_table_swap( +static +void set_empty( ecs_world_t *world, ecs_table_t *table, - ecs_data_t *data, - int32_t row_1, - int32_t row_2) -{ - (void)world; + ecs_id_t id, + int32_t column) +{ + (void)column; - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(row_1 >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(row_2 >= 0, ECS_INTERNAL_ERROR, NULL); - - if (row_1 == row_2) { - return; + ecs_id_record_t *idr = flecs_get_id_record(world, id); + if (idr) { + ecs_table_cache_set_empty(&idr->cache, table, + ecs_table_count(table) == 0); } +} - /* If the table is monitored indicate that there has been a change */ - mark_table_dirty(table, 0); +static +void for_each_id( + ecs_world_t *world, + ecs_table_t *table, + void(*action)(ecs_world_t*, ecs_table_t*, ecs_id_t, int32_t), + bool set_watch) +{ + int32_t i, count = ecs_vector_count(table->type); + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); + bool has_childof = false; + + for (i = 0; i < count; i ++) { + ecs_entity_t id = ids[i]; - ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); - ecs_entity_t e1 = entities[row_1]; - ecs_entity_t e2 = entities[row_2]; + /* This check ensures that legacy CHILDOF works */ + if (ECS_HAS_RELATION(id, EcsChildOf)) { + has_childof = true; + } - ecs_record_t **record_ptrs = ecs_vector_first(data->record_ptrs, ecs_record_t*); - ecs_record_t *record_ptr_1 = record_ptrs[row_1]; - ecs_record_t *record_ptr_2 = record_ptrs[row_2]; + action(world, table, ecs_strip_generation(id), i); + action(world, table, EcsWildcard, i); - ecs_assert(record_ptr_1 != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(record_ptr_2 != NULL, ECS_INTERNAL_ERROR, NULL); + if (ECS_HAS_ROLE(id, PAIR)) { + ecs_entity_t pred_w_wildcard = ecs_pair( + ECS_PAIR_RELATION(id), EcsWildcard); + action(world, table, pred_w_wildcard, i); - /* Keep track of whether entity is watched */ - bool watched_1 = record_ptr_1->row < 0; - bool watched_2 = record_ptr_2->row < 0; + ecs_entity_t obj_w_wildcard = ecs_pair( + EcsWildcard, ECS_PAIR_OBJECT(id)); + action(world, table, obj_w_wildcard, i); - /* Swap entities & records */ - entities[row_1] = e2; - entities[row_2] = e1; - record_ptr_1->row = flecs_row_to_record(row_2, watched_1); - record_ptr_2->row = flecs_row_to_record(row_1, watched_2); - record_ptrs[row_1] = record_ptr_2; - record_ptrs[row_2] = record_ptr_1; + ecs_entity_t all_wildcard = ecs_pair(EcsWildcard, EcsWildcard); + action(world, table, all_wildcard, i); - swap_switch_columns(table, data, row_1, row_2); - swap_bitset_columns(table, data, row_1, row_2); + if (set_watch) { + flecs_set_watch(world, ecs_pair_relation(world, id)); + flecs_set_watch(world, ecs_pair_object(world, id)); + } + } else { + if (id & ECS_ROLE_MASK) { + id &= ECS_COMPONENT_MASK; + action(world, table, ecs_pair(id, EcsWildcard), i); + } - ecs_column_t *columns = data->columns; - if (!columns) { - return; + if (set_watch) { + flecs_set_watch(world, id); + } + } } - /* Swap columns */ - int32_t i, column_count = ecs_vector_count(table->storage_type); - - for (i = 0; i < column_count; i ++) { - int16_t size = columns[i].size; - int16_t alignment = columns[i].alignment; + if (!has_childof) { + action(world, table, ecs_pair(EcsChildOf, 0), 0); + } +} - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); +void flecs_register_table( + ecs_world_t *world, + ecs_table_t *table) +{ + for_each_id(world, table, register_table, true); +} - void *ptr = ecs_vector_first_t(columns[i].data, size, alignment); - void *tmp = ecs_os_alloca(size); +void flecs_unregister_table( + ecs_world_t *world, + ecs_table_t *table) +{ + for_each_id(world, table, unregister_table, false); - void *el_1 = ECS_OFFSET(ptr, size * row_1); - void *el_2 = ECS_OFFSET(ptr, size * row_2); + ecs_ids_t key = { + .array = ecs_vector_first(table->type, ecs_id_t), + .count = ecs_vector_count(table->type) + }; - ecs_os_memcpy(tmp, el_1, size); - ecs_os_memcpy(el_1, el_2, size); - ecs_os_memcpy(el_2, tmp, size); - } + flecs_hashmap_remove(world->store.table_map, &key, ecs_table_t*); } -static -void merge_vector( - ecs_vector_t **dst_out, - ecs_vector_t *src, - int16_t size, - int16_t alignment) +void flecs_table_set_empty( + ecs_world_t *world, + ecs_table_t *table) { - ecs_vector_t *dst = *dst_out; - int32_t dst_count = ecs_vector_count(dst); + for_each_id(world, table, set_empty, false); +} - if (!dst_count) { - if (dst) { - ecs_vector_free(dst); - } +ecs_id_record_t* flecs_ensure_id_record( + ecs_world_t *world, + ecs_id_t id) +{ + ecs_id_record_t *idr = ecs_map_ensure(world->id_index, ecs_id_record_t, + ecs_strip_generation(id)); - *dst_out = src; - - /* If the new table is not empty, copy the contents from the - * src into the dst. */ - } else { - int32_t src_count = ecs_vector_count(src); - ecs_vector_set_count_t(&dst, size, alignment, dst_count + src_count); - - void *dst_ptr = ecs_vector_first_t(dst, size, alignment); - void *src_ptr = ecs_vector_first_t(src, size, alignment); + if (!ecs_table_cache_is_initialized(&idr->cache)) { + ecs_table_cache_init(&idr->cache, ecs_table_record_t, world, NULL); + } - dst_ptr = ECS_OFFSET(dst_ptr, size * dst_count); - - ecs_os_memcpy(dst_ptr, src_ptr, size * src_count); + return idr; +} - ecs_vector_free(src); - *dst_out = dst; - } +ecs_id_record_t* flecs_get_id_record( + const ecs_world_t *world, + ecs_id_t id) +{ + return ecs_map_get(world->id_index, ecs_id_record_t, + ecs_strip_generation(id)); } -static -void merge_column( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t column_id, - ecs_vector_t *src) +ecs_table_record_t* flecs_get_table_record( + const ecs_world_t *world, + const ecs_table_t *table, + ecs_id_t id) { - ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); - ecs_type_info_t *c_info = table->c_info[column_id]; - ecs_column_t *column = &data->columns[column_id]; - ecs_vector_t *dst = column->data; - int16_t size = column->size; - int16_t alignment = column->alignment; - int32_t dst_count = ecs_vector_count(dst); - - if (!dst_count) { - if (dst) { - ecs_vector_free(dst); - } - - column->data = src; - - /* If the new table is not empty, copy the contents from the - * src into the dst. */ - } else { - int32_t src_count = ecs_vector_count(src); - ecs_vector_set_count_t(&dst, size, alignment, dst_count + src_count); - column->data = dst; - - /* Construct new values */ - if (c_info) { - ctor_component( - world, c_info, column, entities, dst_count, src_count); - } - - void *dst_ptr = ecs_vector_first_t(dst, size, alignment); - void *src_ptr = ecs_vector_first_t(src, size, alignment); + ecs_id_record_t* idr = flecs_get_id_record(world, id); + if (!idr) { + return NULL; + } - dst_ptr = ECS_OFFSET(dst_ptr, size * dst_count); - - /* Move values into column */ - ecs_move_t move; - if (c_info && (move = c_info->lifecycle.move)) { - move(world, c_info->component, entities, entities, - dst_ptr, src_ptr, flecs_to_size_t(size), src_count, - c_info->lifecycle.ctx); - } else { - ecs_os_memcpy(dst_ptr, src_ptr, size * src_count); - } + return ecs_table_cache_get(&idr->cache, ecs_table_record_t, table); +} - ecs_vector_free(src); +void flecs_register_add_ref( + ecs_world_t *world, + const ecs_table_t *table, + ecs_id_t id) +{ + ecs_id_record_t *idr = flecs_ensure_id_record(world, id); + if (!idr->add_refs) { + idr->add_refs = ecs_map_new(ecs_table_t*, 1); } + + ecs_table_t **ptr = ecs_map_ensure( + idr->add_refs, ecs_table_t*, table->id); + ptr[0] = (ecs_table_t*)table; } -static -void merge_table_data( +void flecs_register_remove_ref( ecs_world_t *world, - ecs_table_t *new_table, - ecs_table_t *old_table, - int32_t old_count, - int32_t new_count, - ecs_data_t *old_data, - ecs_data_t *new_data) + const ecs_table_t *table, + ecs_id_t id) { - ecs_type_t new_type = new_table->storage_type; - ecs_type_t old_type = old_table->storage_type; - int32_t i_new = 0, new_column_count = ecs_vector_count(new_type); - int32_t i_old = 0, old_column_count = ecs_vector_count(old_type); - ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); - ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); + ecs_id_record_t *idr = flecs_ensure_id_record(world, id); + if (!idr->remove_refs) { + idr->remove_refs = ecs_map_new(ecs_table_t*, 1); + } - ecs_column_t *old_columns = old_data->columns; - ecs_column_t *new_columns = new_data->columns; + ecs_table_t **ptr = ecs_map_ensure( + idr->remove_refs, ecs_table_t*, table->id); + ptr[0] = (ecs_table_t*)table; +} - if (!new_columns && !new_data->entities) { - new_columns = new_data->columns; +void flecs_clear_id_record( + ecs_world_t *world, + ecs_id_t id) +{ + if (world->is_fini) { + return; } - ecs_assert(!new_column_count || new_columns, ECS_INTERNAL_ERROR, NULL); - - if (!old_count) { + ecs_id_record_t *idr_ptr = flecs_get_id_record(world, id); + if (!idr_ptr) { return; } - /* Merge entities */ - merge_vector(&new_data->entities, old_data->entities, ECS_SIZEOF(ecs_entity_t), - ECS_ALIGNOF(ecs_entity_t)); - old_data->entities = NULL; - ecs_entity_t *entities = ecs_vector_first(new_data->entities, ecs_entity_t); - - ecs_assert(ecs_vector_count(new_data->entities) == old_count + new_count, - ECS_INTERNAL_ERROR, NULL); - - /* Merge entity index record pointers */ - merge_vector(&new_data->record_ptrs, old_data->record_ptrs, - ECS_SIZEOF(ecs_record_t*), ECS_ALIGNOF(ecs_record_t*)); - old_data->record_ptrs = NULL; - - for (; (i_new < new_column_count) && (i_old < old_column_count); ) { - ecs_entity_t new_component = new_components[i_new]; - ecs_entity_t old_component = old_components[i_old]; - int16_t size = new_columns[i_new].size; - int16_t alignment = new_columns[i_new].alignment; - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); - - if (new_component == old_component) { - merge_column(world, new_table, new_data, i_new, - old_columns[i_old].data); - old_columns[i_old].data = NULL; - - /* Mark component column as dirty */ - mark_table_dirty(new_table, i_new + 1); - - i_new ++; - i_old ++; - } else if (new_component < old_component) { - /* New column does not occur in old table, make sure vector is large - * enough. */ - ecs_column_t *column = &new_columns[i_new]; - ecs_vector_set_count_t(&column->data, size, alignment, - old_count + new_count); + ecs_id_record_t idr = *idr_ptr; - /* Construct new values */ - ecs_type_info_t *c_info = new_table->c_info[i_new]; - if (c_info) { - ctor_component(world, c_info, column, - entities, 0, old_count + new_count); - } - - i_new ++; - } else if (new_component > old_component) { - ecs_column_t *column = &old_columns[i_old]; - - /* Destruct old values */ - ecs_type_info_t *c_info = old_table->c_info[i_old]; - if (c_info) { - dtor_component(world, c_info, column, - entities, 0, old_count); - } + ecs_map_remove(world->id_index, ecs_strip_generation(id)); - /* Old column does not occur in new table, remove */ - ecs_vector_free(column->data); - column->data = NULL; + ecs_table_cache_fini_delete_all(world, &idr.cache, ecs_table_record_t); - i_old ++; - } + /* Remove add & remove references to id from tables */ + ecs_table_t *table; + ecs_map_iter_t it = ecs_map_iter(idr.add_refs); + while ((table = ecs_map_next_ptr(&it, ecs_table_t*, NULL))) { + flecs_table_clear_add_edge(table, id); } - move_switch_columns( - new_table, new_data, new_count, old_table, old_data, 0, old_count); - - /* Initialize remaining columns */ - for (; i_new < new_column_count; i_new ++) { - ecs_column_t *column = &new_columns[i_new]; - int16_t size = column->size; - int16_t alignment = column->alignment; - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + it = ecs_map_iter(idr.remove_refs); + while ((table = ecs_map_next_ptr(&it, ecs_table_t*, NULL))) { + flecs_table_clear_remove_edge(table, id); + } - ecs_vector_set_count_t(&column->data, size, alignment, - old_count + new_count); + ecs_map_free(idr.add_refs); + ecs_map_free(idr.remove_refs); +} - /* Construct new values */ - ecs_type_info_t *c_info = new_table->c_info[i_new]; - if (c_info) { - ctor_component(world, c_info, column, - entities, 0, old_count + new_count); - } +const ecs_table_record_t* flecs_id_record_table( + ecs_id_record_t *idr, + ecs_table_t *table) +{ + if (!idr) { + return NULL; } - - /* Destroy remaining columns */ - for (; i_old < old_column_count; i_old ++) { - ecs_column_t *column = &old_columns[i_old]; - - /* Destruct old values */ - ecs_type_info_t *c_info = old_table->c_info[i_old]; - if (c_info) { - dtor_component(world, c_info, column, entities, - 0, old_count); - } - - /* Old column does not occur in new table, remove */ - ecs_vector_free(column->data); - column->data = NULL; - } - - /* Mark entity column as dirty */ - mark_table_dirty(new_table, 0); + return ecs_table_cache_get(&idr->cache, ecs_table_record_t, table); } -int32_t ecs_table_count( - const ecs_table_t *table) +const ecs_table_record_t* flecs_id_record_tables( + const ecs_id_record_t *idr) { - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - return flecs_table_data_count(&table->storage); + if (!idr) { + return NULL; + } + return ecs_table_cache_tables(&idr->cache, ecs_table_record_t); } -void flecs_table_merge( - ecs_world_t *world, - ecs_table_t *new_table, - ecs_table_t *old_table, - ecs_data_t *new_data, - ecs_data_t *old_data) +const ecs_table_record_t* flecs_id_record_empty_tables( + const ecs_id_record_t *idr) { - ecs_assert(old_table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!old_table->lock, ECS_LOCKED_STORAGE, NULL); - - bool move_data = false; - - /* If there is nothing to merge to, just clear the old table */ - if (!new_table) { - flecs_table_clear_data(world, old_table, old_data); - return; - } else { - ecs_assert(!new_table->lock, ECS_LOCKED_STORAGE, NULL); + if (!idr) { + return NULL; } + return ecs_table_cache_empty_tables(&idr->cache, ecs_table_record_t); +} - /* If there is no data to merge, drop out */ - if (!old_data) { - return; +int32_t flecs_id_record_count( + const ecs_id_record_t *idr) +{ + if (!idr) { + return 0; } + return ecs_table_cache_count(&idr->cache); +} - if (!new_data) { - new_data = &new_table->storage; - if (new_table == old_table) { - move_data = true; - } +int32_t flecs_id_record_empty_count( + const ecs_id_record_t *idr) +{ + if (!idr) { + return 0; } + return ecs_table_cache_empty_count(&idr->cache); +} - ecs_entity_t *old_entities = ecs_vector_first(old_data->entities, ecs_entity_t); - int32_t old_count = ecs_vector_count(old_data->entities); - int32_t new_count = ecs_vector_count(new_data->entities); +#ifdef FLECS_SYSTEMS_H +#ifndef FLECS_SYSTEM_PRIVATE_H +#define FLECS_SYSTEM_PRIVATE_H - ecs_record_t **old_records = ecs_vector_first( - old_data->record_ptrs, ecs_record_t*); - /* First, update entity index so old entities point to new type */ - int32_t i; - for(i = 0; i < old_count; i ++) { - ecs_record_t *record; - if (new_table != old_table) { - record = old_records[i]; - ecs_assert(record != NULL, ECS_INTERNAL_ERROR, NULL); - } else { - record = ecs_eis_ensure(world, old_entities[i]); - } +typedef struct EcsSystem { + ecs_iter_action_t action; /* Callback to be invoked for matching it */ - bool is_monitored = record->row < 0; - record->row = flecs_row_to_record(new_count + i, is_monitored); - record->table = new_table; - } + ecs_entity_t entity; /* Entity id of system, used for ordering */ + ecs_query_t *query; /* System query */ + ecs_system_status_action_t status_action; /* Status action */ + ecs_entity_t tick_source; /* Tick source associated with system */ + + int32_t invoke_count; /* Number of times system is invoked */ + FLECS_FLOAT time_spent; /* Time spent on running system */ + FLECS_FLOAT time_passed; /* Time passed since last invocation */ - /* Merge table columns */ - if (move_data) { - *new_data = *old_data; - } else { - merge_table_data(world, new_table, old_table, old_count, new_count, - old_data, new_data); - } + ecs_entity_t self; /* Entity associated with system */ - new_table->alloc_count ++; + void *ctx; /* Userdata for system */ + void *status_ctx; /* User data for status action */ + void *binding_ctx; /* Optional language binding context */ - if (!new_count && old_count) { - table_activate(world, new_table, true); - } -} + ecs_ctx_free_t ctx_free; + ecs_ctx_free_t status_ctx_free; + ecs_ctx_free_t binding_ctx_free; +} EcsSystem; -void flecs_table_replace_data( +/* Invoked when system becomes active / inactive */ +void ecs_system_activate( ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data) -{ - int32_t prev_count = 0; - ecs_data_t *table_data = &table->storage; - ecs_assert(!data || data != table_data, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - - prev_count = ecs_vector_count(table_data->entities); - run_on_remove(world, table, table_data); - flecs_table_clear_data(world, table, table_data); + ecs_entity_t system, + bool activate, + const EcsSystem *system_data); - if (data) { - table->storage = *data; - } else { - flecs_table_init_data(world, table); - } +/* Internal function to run a system */ +ecs_entity_t ecs_run_intern( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t system, + EcsSystem *system_data, + int32_t stage_current, + int32_t stage_count, + FLECS_FLOAT delta_time, + int32_t offset, + int32_t limit, + void *param); - int32_t count = ecs_table_count(table); +#endif +#endif - if (!prev_count && count) { - table_activate(world, table, true); - } else if (prev_count && !count) { - table_activate(world, table, false); - } +static +void compute_group_id( + ecs_query_t *query, + ecs_query_table_match_t *match) +{ + ecs_assert(match != NULL, ECS_INTERNAL_ERROR, NULL); - table->alloc_count ++; -} + if (query->group_by) { + ecs_table_t *table = match->table; + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); -int32_t* flecs_table_get_dirty_state( - ecs_table_t *table) -{ - ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - - if (!table->dirty_state) { - int32_t column_count = ecs_vector_count(table->storage_type); - table->dirty_state = ecs_os_calloc_n( int32_t, column_count + 1); - ecs_assert(table->dirty_state != NULL, ECS_INTERNAL_ERROR, NULL); + match->group_id = query->group_by(query->world, table->type, + query->group_by_id, query->group_by_ctx); + } else { + match->group_id = 0; } - return table->dirty_state; } -int32_t* flecs_table_get_monitor( - ecs_table_t *table) +static +ecs_query_table_list_t* get_group( + ecs_query_t *query, + uint64_t group_id) { - int32_t *dirty_state = flecs_table_get_dirty_state(table); - ecs_assert(dirty_state != NULL, ECS_INTERNAL_ERROR, NULL); - - int32_t column_count = ecs_vector_count(table->storage_type); - return ecs_os_memdup(dirty_state, (column_count + 1) * ECS_SIZEOF(int32_t)); + return ecs_map_get(query->groups, ecs_query_table_list_t, group_id); } -void flecs_table_notify( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_event_t *event) +static +ecs_query_table_list_t* ensure_group( + ecs_query_t *query, + uint64_t group_id) { - if (world->is_fini) { - return; - } - - switch(event->kind) { - case EcsTableQueryMatch: - register_query(world, table, event->query); - break; - case EcsTableQueryUnmatch: - unregister_query(world, table, event->query); - break; - case EcsTableComponentInfo: - notify_component_info(world, table, event->component); - break; - case EcsTableTriggerMatch: - notify_trigger(world, table, event->event); - break; - } + return ecs_map_ensure(query->groups, ecs_query_table_list_t, group_id); } -void ecs_table_lock( - ecs_world_t *world, - ecs_table_t *table) +/* Find the last node of the group after which this group should be inserted */ +static +ecs_query_table_node_t* find_group_insertion_node( + ecs_query_t *query, + uint64_t group_id) { - if (ecs_poly_is(world, ecs_world_t) && !world->is_readonly) { - table->lock ++; - } -} + /* Grouping must be enabled */ + ecs_assert(query->group_by != NULL, ECS_INTERNAL_ERROR, NULL); -void ecs_table_unlock( - ecs_world_t *world, - ecs_table_t *table) -{ - if (ecs_poly_is(world, ecs_world_t) && !world->is_readonly) { - table->lock --; - ecs_assert(table->lock >= 0, ECS_INVALID_OPERATION, NULL); - } -} + ecs_map_iter_t it = ecs_map_iter(query->groups); + ecs_query_table_list_t *list, *closest_list = NULL; + uint64_t id, closest_id = 0; -bool ecs_table_has_module( - ecs_table_t *table) -{ - return table->flags & EcsTableHasModule; -} + /* Find closest smaller group id */ + while ((list = ecs_map_next(&it, ecs_query_table_list_t, &id))) { + if (id >= group_id) { + continue; + } -ecs_column_t *ecs_table_column_for_id( - const ecs_world_t *world, - const ecs_table_t *table, - ecs_id_t id) -{ - ecs_table_t *storage_table = table->storage_table; - if (!storage_table) { - return NULL; - } + if (!list->last) { + ecs_assert(list->first == NULL, ECS_INTERNAL_ERROR, NULL); + continue; + } - ecs_table_record_t *tr = flecs_get_table_record(world, storage_table, id); - if (tr) { - return &table->storage.columns[tr->column]; + if (!closest_list || ((group_id - id) < (group_id - closest_id))) { + closest_id = id; + closest_list = list; + } } - return NULL; + if (closest_list) { + return closest_list->last; + } else { + return NULL; /* Group should be first in query */ + } } -ecs_type_t ecs_table_get_type( - const ecs_table_t *table) +/* Initialize group with first node */ +static +void create_group( + ecs_query_t *query, + ecs_query_table_node_t *node) { - return table->type; -} + ecs_query_table_match_t *match = node->match; + uint64_t group_id = match->group_id; -ecs_type_t ecs_table_get_storage_type( - const ecs_table_t *table) -{ - return table->storage_type; + /* If query has grouping enabled & this is a new/empty group, find + * the insertion point for the group */ + ecs_query_table_node_t *insert_after = find_group_insertion_node( + query, group_id); + + if (!insert_after) { + /* This group should appear first in the query list */ + ecs_query_table_node_t *query_first = query->list.first; + if (query_first) { + /* If this is not the first match for the query, insert before it */ + node->next = query_first; + query_first->prev = node; + query->list.first = node; + } else { + /* If this is the first match of the query, initialize its list */ + ecs_assert(query->list.last == NULL, ECS_INTERNAL_ERROR, NULL); + query->list.first = node; + query->list.last = node; + } + } else { + ecs_assert(query->list.first != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(query->list.last != NULL, ECS_INTERNAL_ERROR, NULL); + + /* This group should appear after another group */ + ecs_query_table_node_t *insert_before = insert_after->next; + node->prev = insert_after; + insert_after->next = node; + node->next = insert_before; + if (insert_before) { + insert_before->prev = node; + } else { + ecs_assert(query->list.last == insert_after, + ECS_INTERNAL_ERROR, NULL); + + /* This group should appear last in the query list */ + query->list.last = node; + } + } } -int32_t ecs_table_storage_count( - const ecs_table_t *table) +static +void remove_group( + ecs_query_t *query, + uint64_t group_id) { - return ecs_vector_count(table->storage_type); + ecs_map_remove(query->groups, group_id); } -int32_t ecs_table_type_to_storage_index( - const ecs_table_t *table, - int32_t index) +/* Find the list the node should be part of */ +static +ecs_query_table_list_t* get_node_list( + ecs_query_t *query, + ecs_query_table_node_t *node) { - ecs_assert(index < ecs_vector_count(table->type), - ECS_INVALID_PARAMETER, NULL); - int32_t *storage_map = table->storage_map; - if (storage_map) { - return storage_map[index]; + ecs_query_table_match_t *match = node->match; + if (query->group_by) { + return get_group(query, match->group_id); } else { - return -1; + return &query->list; } } -int32_t ecs_table_storage_to_type_index( - const ecs_table_t *table, - int32_t index) -{ - ecs_assert(index < ecs_vector_count(table->storage_type), - ECS_INVALID_PARAMETER, NULL); - ecs_assert(table->storage_map != NULL, ECS_INVALID_PARAMETER, NULL); - int32_t offset = ecs_vector_count(table->type); - return table->storage_map[offset + index]; -} - -ecs_record_t* ecs_record_find( - ecs_world_t *world, - ecs_entity_t entity) +/* Find or create the list the node should be part of */ +static +ecs_query_table_list_t* ensure_node_list( + ecs_query_t *query, + ecs_query_table_node_t *node) { - ecs_record_t *r = ecs_eis_get(world, entity); - if (r) { - return r; + ecs_query_table_match_t *match = node->match; + if (query->group_by) { + return ensure_group(query, match->group_id); } else { - return NULL; + return &query->list; } } -void* ecs_record_get_column( - ecs_record_t *r, - int32_t column, - size_t c_size) +/* Remove node from list */ +static +void remove_table_node( + ecs_query_t *query, + ecs_query_table_node_t *node) { - (void)c_size; - ecs_table_t *table = r->table; - - ecs_assert(column < ecs_vector_count(table->storage_type), - ECS_INVALID_PARAMETER, NULL); - - ecs_column_t *c = &table->storage.columns[column]; - ecs_assert(c != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_query_table_node_t *prev = node->prev; + ecs_query_table_node_t *next = node->next; - int16_t size = c->size; - ecs_assert(!flecs_from_size_t(c_size) || - flecs_from_size_t(c_size) == c->size, - ECS_INVALID_PARAMETER, NULL); + ecs_assert(prev != node, ECS_INTERNAL_ERROR, NULL); + ecs_assert(next != node, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!prev || prev != next, ECS_INTERNAL_ERROR, NULL); - void *array = ecs_vector_first_t(c->data, c->size, c->alignment); - bool is_watched; - int32_t row = flecs_record_to_row(r->row, &is_watched); + ecs_query_table_list_t *list = get_node_list(query, node); - return ECS_OFFSET(array, size * row); -} -#include + if (!list || !list->first) { + /* If list contains no nodes, the node must be empty */ + ecs_assert(!list || list->last == NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(prev == NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(next == NULL, ECS_INTERNAL_ERROR, NULL); + return; + } -static const char* mixin_kind_str[] = { - [EcsMixinBase] = "base (should never be requested by application)", - [EcsMixinWorld] = "world", - [EcsMixinObservable] = "observable", - [EcsMixinMax] = "max (should never be requested by application)" -}; + ecs_assert(prev != NULL || query->list.first == node, + ECS_INTERNAL_ERROR, NULL); + ecs_assert(next != NULL || query->list.last == node, + ECS_INTERNAL_ERROR, NULL); -ecs_mixins_t ecs_world_t_mixins = { - .type_name = "ecs_world_t", - .elems = { - [EcsMixinWorld] = offsetof(ecs_world_t, self), - [EcsMixinObservable] = offsetof(ecs_world_t, observable) + if (prev) { + prev->next = next; } -}; - -ecs_mixins_t ecs_stage_t_mixins = { - .type_name = "ecs_stage_t", - .elems = { - [EcsMixinBase] = offsetof(ecs_stage_t, world), - [EcsMixinWorld] = offsetof(ecs_stage_t, world) + if (next) { + next->prev = prev; } -}; -ecs_mixins_t ecs_query_t_mixins = { - .type_name = "ecs_query_t", - .elems = { - [EcsMixinWorld] = offsetof(ecs_query_t, world) - } -}; + ecs_assert(list->count > 0, ECS_INTERNAL_ERROR, NULL); + list->count --; -static -void* get_mixin( - const ecs_poly_t *poly, - ecs_mixin_kind_t kind) -{ - ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(kind < EcsMixinMax, ECS_INVALID_PARAMETER, NULL); - - const ecs_header_t *hdr = poly; - ecs_assert(hdr != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(hdr->magic == ECS_OBJECT_MAGIC, ECS_INVALID_PARAMETER, NULL); + if (query->group_by) { + ecs_query_table_match_t *match = node->match; + uint64_t group_id = match->group_id; - const ecs_mixins_t *mixins = hdr->mixins; - if (!mixins) { - /* Object has no mixins */ - goto not_found; - } + /* Make sure query.list is updated if this is the first or last group */ + if (query->list.first == node) { + ecs_assert(prev == NULL, ECS_INTERNAL_ERROR, NULL); + query->list.first = next; + prev = next; + } + if (query->list.last == node) { + ecs_assert(next == NULL, ECS_INTERNAL_ERROR, NULL); + query->list.last = prev; + next = prev; + } - ecs_size_t offset = mixins->elems[kind]; - if (offset == 0) { - /* Object has mixins but not the requested one. Try to find the mixin - * in the poly's base */ - goto find_in_base; - } + ecs_assert(query->list.count > 0, ECS_INTERNAL_ERROR, NULL); + query->list.count --; - /* Object has mixin, return its address */ - return ECS_OFFSET(hdr, offset); + /* Make sure group list only contains nodes that belong to the group */ + if (prev && prev->match->group_id != group_id) { + /* The previous node belonged to another group */ + prev = next; + } + if (next && next->match->group_id != group_id) { + /* The next node belonged to another group */ + next = prev; + } -find_in_base: - if (offset) { - /* If the poly has a base, try to find the mixin in the base */ - ecs_poly_t *base = *(ecs_poly_t**)ECS_OFFSET(hdr, offset); - if (base) { - return get_mixin(base, kind); + /* Do check again, in case both prev & next belonged to another group */ + if (prev && prev->match->group_id != group_id) { + /* There are no more matches left in this group */ + remove_group(query, group_id); + list = NULL; } } - -not_found: - /* Mixin wasn't found for poly */ - return NULL; -} -static -void* assert_mixin( - const ecs_poly_t *poly, - ecs_mixin_kind_t kind) -{ - void *ptr = get_mixin(poly, kind); - if (!ptr) { - const ecs_header_t *header = poly; - const ecs_mixins_t *mixins = header->mixins; - ecs_err("%s not available for type %s", - mixin_kind_str[kind], - mixins ? mixins->type_name : "unknown"); - ecs_os_abort(); + if (list) { + if (list->first == node) { + list->first = next; + } + if (list->last == node) { + list->last = prev; + } } - return ptr; -} - -void _ecs_poly_init( - ecs_poly_t *poly, - int32_t type, - ecs_size_t size, - ecs_mixins_t *mixins) -{ - ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); + node->prev = NULL; + node->next = NULL; - ecs_header_t *hdr = poly; - ecs_os_memset(poly, 0, size); +#ifdef FLECS_SYSTEMS_H + if (query->list.first == NULL && query->system && !query->world->is_fini) { + ecs_system_activate(query->world, query->system, false, NULL); + } +#endif - hdr->magic = ECS_OBJECT_MAGIC; - hdr->type = type; - hdr->mixins = mixins; + query->match_count ++; } -void _ecs_poly_fini( - ecs_poly_t *poly, - int32_t type) +/* Add node to list */ +static +void insert_table_node( + ecs_query_t *query, + ecs_query_table_node_t *node) { - ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); - (void)type; + /* Node should not be part of an existing list */ + ecs_assert(node->prev == NULL && node->next == NULL, + ECS_INTERNAL_ERROR, NULL); - ecs_header_t *hdr = poly; + /* If this is the first match, activate system */ +#ifdef FLECS_SYSTEMS_H + if (!query->list.first && query->system) { + ecs_system_activate(query->world, query->system, true, NULL); + } +#endif - /* Don't deinit poly that wasn't initialized */ - ecs_assert(hdr->magic == ECS_OBJECT_MAGIC, ECS_INVALID_PARAMETER, NULL); - ecs_assert(hdr->type == type, ECS_INVALID_PARAMETER, NULL); - hdr->magic = 0; -} + compute_group_id(query, node->match); -#define assert_object(cond, file, line)\ - _ecs_assert((cond), ECS_INVALID_PARAMETER, #cond, file, line, NULL);\ - assert(cond) + ecs_query_table_list_t *list = ensure_node_list(query, node); + if (list->last) { + ecs_assert(query->list.first != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(query->list.last != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(list->first != NULL, ECS_INTERNAL_ERROR, NULL); -#ifndef NDEBUG -void _ecs_poly_assert( - const ecs_poly_t *poly, - int32_t type, - const char *file, - int32_t line) -{ - assert_object(poly != NULL, file, line); - - const ecs_header_t *hdr = poly; - assert_object(hdr->magic == ECS_OBJECT_MAGIC, file, line); - assert_object(hdr->type == type, file, line); -} -#endif + ecs_query_table_node_t *last = list->last; + ecs_query_table_node_t *last_next = last->next; -bool _ecs_poly_is( - const ecs_poly_t *poly, - int32_t type) -{ - ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); + node->prev = last; + node->next = last_next; + last->next = node; - const ecs_header_t *hdr = poly; - ecs_assert(hdr->magic == ECS_OBJECT_MAGIC, ECS_INVALID_PARAMETER, NULL); - return hdr->type == type; -} + if (last_next) { + last_next->prev = node; + } -ecs_observable_t* ecs_get_observable( - const ecs_poly_t *poly) -{ - return (ecs_observable_t*)assert_mixin(poly, EcsMixinObservable); -} + list->last = node; -const ecs_world_t* ecs_get_world( - const ecs_poly_t *poly) -{ - return *(ecs_world_t**)assert_mixin(poly, EcsMixinWorld); -} + if (query->group_by) { + /* Make sure to update query list if this is the last group */ + if (query->list.last == last) { + query->list.last = node; + } + } + } else { + ecs_assert(list->first == NULL, ECS_INTERNAL_ERROR, NULL); + list->first = node; + list->last = node; -static -const ecs_entity_t* new_w_data( - ecs_world_t *world, - ecs_table_t *table, - const ecs_entity_t *entities, - ecs_ids_t *component_ids, - int32_t count, - void **c_info, - bool move, - int32_t *row_out, - ecs_table_diff_t *diff); + if (query->group_by) { + /* Initialize group with its first node */ + create_group(query, node); + } + } -static -void* get_component_w_index( - ecs_table_t *table, - int32_t column_index, - int32_t row) -{ - ecs_assert(column_index < ecs_table_storage_count(table), - ECS_NOT_A_COMPONENT, NULL); + if (query->group_by) { + query->list.count ++; + } - ecs_column_t *column = &table->storage.columns[column_index]; + list->count ++; + query->match_count ++; - /* If size is 0, component does not have a value. This is likely caused by - * an application trying to call ecs_get with a tag. */ - int32_t size = column->size; - ecs_assert(size != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(node->prev != node, ECS_INTERNAL_ERROR, NULL); + ecs_assert(node->next != node, ECS_INTERNAL_ERROR, NULL); - void *ptr = ecs_vector_first_t(column->data, size, column->alignment); - return ECS_OFFSET(ptr, size * row); + ecs_assert(list->first != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(list->last != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(list->last == node, ECS_INTERNAL_ERROR, NULL); + ecs_assert(query->list.first != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(query->list.last != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(query->list.first->prev == NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(query->list.last->next == NULL, ECS_INTERNAL_ERROR, NULL); } static -void* get_component( - const ecs_world_t *world, - ecs_table_t *table, - int32_t row, - ecs_id_t id) +ecs_query_table_match_t* cache_add( + ecs_query_table_t *elem) { - if (!table->storage_table) { - ecs_assert(ecs_type_index_of(table->type, 0, id) == -1, - ECS_NOT_A_COMPONENT, NULL); - return NULL; - } + ecs_query_table_match_t *result = ecs_os_calloc_t(ecs_query_table_match_t); + ecs_query_table_node_t *node = &result->node; - ecs_table_record_t *tr = flecs_get_table_record( - world, table->storage_table, id); - if (!tr) { - ecs_assert(ecs_type_index_of(table->type, 0, id) == -1, - ECS_NOT_A_COMPONENT, NULL); - return NULL; + node->match = result; + if (!elem->first) { + elem->first = result; + elem->last = result; + } else { + ecs_assert(elem->last != NULL, ECS_INTERNAL_ERROR, NULL); + elem->last->next_match = result; + elem->last = result; } - return get_component_w_index(table, tr->column, row); + return result; } +/* Builtin group_by callback for Cascade terms. + * This function traces the hierarchy depth of an entity type by following a + * relation upwards (to its 'parents') for as long as those parents have the + * specified component id. + * The result of the function is the number of parents with the provided + * component for a given relation. */ static -void* get_base_component( - const ecs_world_t *world, - ecs_table_t *table, - ecs_id_t id, - ecs_id_record_t *table_index, - ecs_id_record_t *table_index_isa, - int32_t recur_depth) +uint64_t group_by_cascade( + ecs_world_t *world, + ecs_type_t type, + ecs_entity_t component, + void *ctx) { - /* Cycle detected in IsA relation */ - ecs_assert(recur_depth < ECS_MAX_RECURSION, ECS_INVALID_PARAMETER, NULL); - - /* Table (and thus entity) does not have component, look for base */ - if (!(table->flags & EcsTableHasIsA)) { - return NULL; - } + uint64_t result = 0; + int32_t i, count = ecs_vector_count(type); + ecs_entity_t *array = ecs_vector_first(type, ecs_entity_t); + ecs_term_t *term = ctx; + ecs_entity_t relation = term->subj.set.relation; - /* Exclude Name */ - if (id == ecs_pair(ecs_id(EcsIdentifier), EcsName)) { - return NULL; - } + /* Cascade needs a relation to calculate depth from */ + ecs_assert(relation != 0, ECS_INVALID_PARAMETER, NULL); - /* Should always be an id record for IsA, otherwise a table with a - * HasBase flag set should not exist. */ - if (!table_index_isa) { - ecs_id_record_t *idr = flecs_get_id_record(world, ecs_pair(EcsIsA, EcsWildcard)); - ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL); - table_index_isa = idr; - } + /* Should only be used with cascade terms */ + ecs_assert(term->subj.set.mask & EcsCascade, + ECS_INVALID_PARAMETER, NULL); - /* Table should always be in the table index for (IsA, *), otherwise the - * HasBase flag should not have been set */ - const ecs_table_record_t *tr_isa = flecs_id_record_table( - table_index_isa, table); - ecs_assert(tr_isa != NULL, ECS_INTERNAL_ERROR, NULL); + /* Iterate back to front as relations are more likely to occur near the + * end of a type. */ + for (i = count - 1; i >= 0; i --) { + /* Find relation & relation object in entity type */ + if (ECS_HAS_RELATION(array[i], relation)) { + ecs_type_t obj_type = ecs_get_type(world, + ecs_pair_object(world, array[i])); + int32_t j, c_count = ecs_vector_count(obj_type); + ecs_entity_t *c_array = ecs_vector_first(obj_type, ecs_entity_t); - ecs_type_t type = table->type; - ecs_id_t *ids = ecs_vector_first(type, ecs_id_t); - int32_t i = tr_isa->column, end = tr_isa->count + tr_isa->column; - void *ptr = NULL; + /* Iterate object type, check if it has the specified component */ + for (j = 0; j < c_count; j ++) { + /* If it has the component, it is part of the tree matched by + * the query, increase depth */ + if (c_array[j] == component) { + result ++; - do { - ecs_id_t pair = ids[i ++]; - ecs_entity_t base = ecs_pair_object(world, pair); - - ecs_record_t *r = ecs_eis_get(world, base); - if (!r) { - continue; - } + /* Recurse to test if the object has matching parents */ + result += group_by_cascade(world, obj_type, component, ctx); + break; + } + } - table = r->table; - if (!table) { - continue; - } + if (j != c_count) { + break; + } - const ecs_table_record_t *tr = flecs_id_record_table( - table_index, table); - if (!tr) { - ptr = get_base_component(world, table, id, table_index, - table_index_isa, recur_depth + 1); - } else { - bool is_monitored; - int32_t row = flecs_record_to_row(r->row, &is_monitored); - ptr = get_component_w_index(table, tr->column, row); + /* If the id doesn't have a role set, we'll find no more relations */ + } else if (!(array[i] & ECS_ROLE_MASK)) { + break; } - } while (!ptr && (i < end)); + } - return ptr; + return result; } -/* Utility to compute actual row from row in record */ +#ifndef NDEBUG + static -int32_t set_row_info( - ecs_entity_info_t *info, - int32_t row) +const char* query_name( + ecs_world_t *world, + ecs_query_t *q) { - return info->row = flecs_record_to_row(row, &info->is_watched); + if (q->system) { + return ecs_get_name(world, q->system); + } else if (q->filter.name) { + return q->filter.name; + } else { + return q->filter.expr; + } } -/* Utility to set info from main stage record */ +#endif + static -void set_info_from_record( - ecs_entity_info_t *info, - ecs_record_t *record) +int get_comp_and_src( + ecs_world_t *world, + ecs_query_t *query, + int32_t t, + ecs_table_t *table_arg, + ecs_entity_t *component_out, + ecs_entity_t *entity_out, + bool *match_out) { - ecs_assert(record != NULL, ECS_INTERNAL_ERROR, NULL); - - info->record = record; + ecs_entity_t component = 0, entity = 0; - ecs_table_t *table = record->table; + ecs_term_t *terms = query->filter.terms; + int32_t term_count = query->filter.term_count; + ecs_term_t *term = &terms[t]; + ecs_term_id_t *subj = &term->subj; + ecs_oper_kind_t op = term->oper; - set_row_info(info, record->row); + *match_out = true; - info->table = table; - if (!info->table) { - return; + if (op == EcsNot) { + entity = subj->entity; } - info->data = &table->storage; + if (!subj->entity) { + component = term->id; + } else { + ecs_table_t *table = table_arg; + if (subj->entity != EcsThis) { + table = ecs_get_table(world, subj->entity); + } - ecs_assert(ecs_vector_count(table->storage.entities) > info->row, - ECS_INTERNAL_ERROR, NULL); -} + ecs_type_t type = NULL; + if (table) { + type = table->type; + } -static -const ecs_type_info_t *get_c_info( - ecs_world_t *world, - ecs_entity_t component) -{ - ecs_entity_t real_id = ecs_get_typeid(world, component); - if (real_id) { - return flecs_get_c_info(world, real_id); - } else { - return NULL; - } -} + if (op == EcsOr) { + for (; t < term_count; t ++) { + term = &terms[t]; -static -void ids_merge( - ecs_ids_t *ids, - ecs_ids_t *add) -{ - if (!add || !add->count) { - return; - } - - int32_t new_count = ids->count + add->count; - if (new_count >= ids->size) { - ids->size = flecs_next_pow_of_2(new_count); - ecs_id_t *arr = ecs_os_malloc(ids->size * ECS_SIZEOF(ecs_id_t)); - ecs_os_memcpy_n(arr, ids->array, ecs_id_t, ids->count); + /* Keep iterating until the next non-OR expression */ + if (term->oper != EcsOr) { + t --; + break; + } - if (ids->count >= ECS_MAX_ADD_REMOVE) { - ecs_os_free(ids->array); - } - - ids->array = arr; - } + if (!component) { + ecs_entity_t source = 0; + int32_t result = ecs_type_match(world, table, type, + 0, term->id, subj->set.relation, subj->set.min_depth, + subj->set.max_depth, &source, NULL); - ecs_os_memcpy_n(&ids->array[ids->count], add->array, ecs_id_t, add->count); - ids->count += add->count; -} + if (result != -1) { + component = term->id; + } -#define ECS_TABLE_DIFF_INIT {\ - .added = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ - .removed = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ - .on_set = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ - .un_set = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ -} + if (source) { + entity = source; + } + } + } + } else { + component = term->id; -static -void diff_append( - ecs_table_diff_t *dst, - ecs_table_diff_t *src) -{ - ids_merge(&dst->added, &src->added); - ids_merge(&dst->removed, &src->removed); - ids_merge(&dst->on_set, &src->on_set); - ids_merge(&dst->un_set, &src->un_set); -} + ecs_entity_t source = 0; + bool result = ecs_type_match(world, table, type, 0, component, + subj->set.relation, subj->set.min_depth, subj->set.max_depth, + &source, NULL) != -1; -static -void diff_free( - ecs_table_diff_t *diff) -{ - if (diff->added.count > ECS_MAX_ADD_REMOVE) { - ecs_os_free(diff->added.array); - } - if (diff->removed.count > ECS_MAX_ADD_REMOVE) { - ecs_os_free(diff->removed.array); - } - if (diff->on_set.count > ECS_MAX_ADD_REMOVE) { - ecs_os_free(diff->on_set.array); + *match_out = result; + + if (op == EcsNot) { + result = !result; + } + + /* Optional terms may not have the component. *From terms contain + * the id of a type of which the contents must match, but the type + * itself does not need to match. */ + if (op == EcsOptional || op == EcsAndFrom || op == EcsOrFrom || + op == EcsNotFrom) + { + result = true; + } + + /* Table has already been matched, so unless column is optional + * any components matched from the table must be available. */ + if (table == table_arg) { + ecs_assert(result == true, ECS_INTERNAL_ERROR, NULL); + } + + if (source) { + entity = source; + } + } + + if (subj->entity != EcsThis) { + entity = subj->entity; + } } - if (diff->un_set.count > ECS_MAX_ADD_REMOVE) { - ecs_os_free(diff->un_set.array); + + if (entity == EcsThis) { + entity = 0; } -} -static -ecs_table_t* table_append( - ecs_world_t *world, - ecs_table_t *table, - ecs_id_t id, - ecs_table_diff_t *diff) -{ - ecs_table_diff_t temp_diff; - table = flecs_table_traverse_add(world, table, &id, &temp_diff); - ecs_assert(table != NULL, ECS_INVALID_PARAMETER, NULL); - diff_append(diff, &temp_diff); - return table; + *component_out = component; + *entity_out = entity; + + return t; } +typedef struct pair_offset_t { + int32_t index; + int32_t count; +} pair_offset_t; + +/* Get index for specified pair. Take into account that a pair can be matched + * multiple times per table, by keeping an offset of the last found index */ static -void notify( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_t *other_table, - int32_t row, - int32_t count, - ecs_entity_t event, - ecs_ids_t *ids) +int32_t get_pair_index( + ecs_type_t table_type, + ecs_id_t pair, + int32_t column_index, + pair_offset_t *pair_offsets, + int32_t count) { - ecs_emit(world, &(ecs_event_desc_t) { - .event = event, - .ids = ids, - .payload_kind = EcsPayloadTable, - .payload.table = { - .table = table, - .other_table = other_table, - .offset = row, - .count = count - } - }); -} + int32_t result; -static -void instantiate( - ecs_world_t *world, - ecs_entity_t base, - ecs_table_t *table, - ecs_data_t *data, - int32_t row, - int32_t count); + /* The count variable keeps track of the number of times a pair has been + * matched with the current table. Compare the count to check if the index + * was already resolved for this iteration */ + if (pair_offsets[column_index].count == count) { + /* If it was resolved, return the last stored index. Subtract one as the + * index is offset by one, to ensure we're not getting stuck on the same + * index. */ + result = pair_offsets[column_index].index - 1; + } else { + /* First time for this iteration that the pair index is resolved, look + * it up in the type. */ + result = ecs_type_index_of(table_type, + pair_offsets[column_index].index, pair); + pair_offsets[column_index].index = result + 1; + pair_offsets[column_index].count = count; + } + + return result; +} static -void instantiate_children( +int32_t get_component_index( ecs_world_t *world, - ecs_entity_t base, ecs_table_t *table, - ecs_data_t *data, - int32_t row, - int32_t count, - ecs_table_t *child_table) + ecs_type_t table_type, + ecs_entity_t *component_out, + int32_t column_index, + ecs_oper_kind_t op, + pair_offset_t *pair_offsets, + int32_t count) { - if (!ecs_table_count(child_table)) { - return; - } - - ecs_type_t type = child_table->type; - ecs_data_t *child_data = &child_table->storage; + int32_t result = 0; + ecs_entity_t component = *component_out; - ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); - int32_t type_count = ecs_vector_count(type); + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - /* Instantiate child table for each instance */ + if (component) { + /* If requested component is a case, find the corresponding switch to + * lookup in the table */ + if (ECS_HAS_ROLE(component, CASE)) { + result = flecs_table_switch_from_case( + world, table, component); + ecs_assert(result != -1, ECS_INTERNAL_ERROR, NULL); - /* Create component array for creating the table */ - ecs_ids_t components = { - .array = ecs_os_alloca_n(ecs_entity_t, type_count + 1) - }; + result += table->sw_column_offset; + } else + if (ECS_HAS_ROLE(component, PAIR)) { + ecs_entity_t rel = ECS_PAIR_RELATION(component); + ecs_entity_t obj = ECS_PAIR_OBJECT(component); - void **component_data = ecs_os_alloca_n(void*, type_count + 1); + /* Both the relationship and the object of the pair must be set */ + ecs_assert(rel != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(obj != 0, ECS_INVALID_PARAMETER, NULL); - /* Copy in component identifiers. Find the base index in the component - * array, since we'll need this to replace the base with the instance id */ - int j, i, childof_base_index = -1, pos = 0; - for (i = 0; i < type_count; i ++) { - ecs_id_t id = ids[i]; + if (rel == EcsWildcard || obj == EcsWildcard) { + ecs_assert(pair_offsets != NULL, ECS_INTERNAL_ERROR, NULL); - /* Make sure instances don't have EcsPrefab */ - if (id == EcsPrefab) { - continue; - } + /* Get index of pair. Start looking from the last pair index + * as this may not be the first instance of the pair. */ + result = get_pair_index( + table_type, component, column_index, pair_offsets, count); + + if (result != -1) { + /* If component of current column is a pair, get the actual + * pair type for the table, so the system can see which + * component the pair was applied to */ + ecs_entity_t *pair = ecs_vector_get( + table_type, ecs_entity_t, result); + *component_out = *pair; - /* Keep track of the element that creates the ChildOf relationship with - * the prefab parent. We need to replace this element to make sure the - * created children point to the instance and not the prefab */ - if (ECS_HAS_RELATION(id, EcsChildOf) && (ECS_PAIR_OBJECT(id) == base)) { - childof_base_index = pos; - } + /* Check if the pair is a tag or whether it has data */ + if (ecs_get(world, rel, EcsComponent) == NULL) { + /* If pair has no data associated with it, use the + * component to which the pair has been added */ + component = ECS_PAIR_OBJECT(*pair); + } else { + component = rel; + } + } + } else { - int32_t storage_index = ecs_table_type_to_storage_index(child_table, i); - if (storage_index != -1) { - ecs_column_t *column = &child_data->columns[storage_index]; - component_data[pos] = ecs_vector_first_t( - column->data, column->size, column->alignment); + /* If the low part is a regular entity (component), then + * this query exactly matches a single pair instance. In + * this case we can simply do a lookup of the pair + * identifier in the table type. */ + result = ecs_type_index_of(table_type, 0, component); + } } else { - component_data[pos] = NULL; + /* Get column index for component */ + result = ecs_type_index_of(table_type, 0, component); } - components.array[pos] = id; - pos ++; - } - - /* Table must contain children of base */ - ecs_assert(childof_base_index != -1, ECS_INTERNAL_ERROR, NULL); + /* If column is found, add one to the index, as column zero in + * a table is reserved for entity id's */ + if (result != -1) { + result ++; + } - /* If children are added to a prefab, make sure they are prefabs too */ - if (table->flags & EcsTableIsPrefab) { - components.array[pos] = EcsPrefab; - component_data[pos] = NULL; - pos ++; + /* ecs_table_column_offset may return -1 if the component comes + * from a prefab. If so, the component will be resolved as a + * reference (see below) */ } - components.count = pos; + if (op == EcsAndFrom || op == EcsOrFrom || op == EcsNotFrom) { + result = 0; + } else if (op == EcsOptional) { + /* If table doesn't have the field, mark it as no data */ + if (!ecs_type_has_id(world, table_type, component, false)) { + result = 0; + } + } - /* Instantiate the prefab child table for each new instance */ - ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); - int32_t child_count = ecs_vector_count(child_data->entities); + return result; +} - for (i = row; i < count + row; i ++) { - ecs_entity_t instance = entities[i]; - ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; - ecs_table_t *i_table = NULL; - - /* Replace ChildOf element in the component array with instance id */ - components.array[childof_base_index] = ecs_pair(EcsChildOf, instance); +static +ecs_vector_t* add_ref( + ecs_world_t *world, + ecs_query_t *query, + ecs_vector_t *references, + ecs_term_t *term, + ecs_entity_t component, + ecs_entity_t entity) +{ + ecs_ref_t *ref = ecs_vector_add(&references, ecs_ref_t); + ecs_term_id_t *subj = &term->subj; - /* Find or create table */ - for (j = 0; j < components.count; j ++) { - i_table = table_append(world, i_table, components.array[j], &diff); - } + if (!(subj->set.mask & EcsCascade)) { + ecs_assert(entity != 0, ECS_INTERNAL_ERROR, NULL); + } + + *ref = (ecs_ref_t){0}; + ref->entity = entity; + ref->component = component; - ecs_assert(i_table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ecs_vector_count(i_table->type) == components.count, - ECS_INTERNAL_ERROR, NULL); + const EcsComponent *c_info = flecs_component_from_id(world, component); + if (c_info) { + if (c_info->size && subj->entity != 0) { + if (entity) { + ecs_get_ref_w_id(world, ref, entity, component); + } - /* The instance is trying to instantiate from a base that is also - * its parent. This would cause the hierarchy to instantiate itself - * which would cause infinite recursion. */ - ecs_entity_t *children = ecs_vector_first( - child_data->entities, ecs_entity_t); -#ifndef NDEBUG - for (j = 0; j < child_count; j ++) { - ecs_entity_t child = children[j]; - ecs_assert(child != instance, ECS_INVALID_PARAMETER, NULL); + query->flags |= EcsQueryHasRefs; } -#endif - - /* Create children */ - int32_t child_row; - new_w_data(world, i_table, NULL, &components, child_count, - component_data, false, &child_row, &diff); - diff_free(&diff); + } - /* If prefab child table has children itself, recursively instantiate */ - ecs_data_t *i_data = &i_table->storage; - for (j = 0; j < child_count; j ++) { - ecs_entity_t child = children[j]; - instantiate(world, child, i_table, i_data, child_row + j, 1); - } - } + return references; } static -void instantiate( - ecs_world_t *world, - ecs_entity_t base, - ecs_table_t *table, - ecs_data_t *data, - int32_t row, - int32_t count) +int32_t get_pair_count( + ecs_type_t type, + ecs_entity_t pair) { - ecs_table_t *base_table = ecs_get_table(world, ecs_get_alive(world, base)); - if (!base_table || !(base_table->flags & EcsTableIsPrefab)) { - /* Don't instantiate children from base entities that aren't prefabs */ - return; - } - - /* If base is a parent, instantiate children of base for instances */ - const ecs_id_record_t *idr = flecs_get_id_record( - world, ecs_pair(EcsChildOf, base)); - - const ecs_table_record_t *tables = flecs_id_record_tables(idr); - int32_t i, table_count = flecs_id_record_count(idr); - for (i = 0; i < table_count; i ++) { - instantiate_children( - world, base, table, data, row, count, tables[i].table); + int32_t i = -1, result = 0; + while (-1 != (i = ecs_type_index_of(type, i + 1, pair))) { + result ++; } -} -static -bool override_component( - ecs_world_t *world, - ecs_entity_t component, - ecs_type_t type, - ecs_data_t *data, - ecs_column_t *column, - int32_t row, - int32_t count); + return result; +} +/* For each pair that the query subscribes for, count the occurrences in the + * table. Cardinality of subscribed for pairs must be the same as in the table + * or else the table won't match. */ static -bool override_from_base( - ecs_world_t *world, - ecs_entity_t base, - ecs_entity_t component, - ecs_data_t *data, - ecs_column_t *column, - int32_t row, - int32_t count) +int32_t count_pairs( + const ecs_query_t *query, + ecs_type_t type) { - ecs_assert(component != 0, ECS_INTERNAL_ERROR, NULL); - - ecs_entity_info_t base_info; - ecs_assert(component != 0, ECS_INTERNAL_ERROR, NULL); - - if (!flecs_get_info(world, base, &base_info) || !base_info.table) { - return false; - } + ecs_term_t *terms = query->filter.terms; + int32_t i, count = query->filter.term_count; + int32_t first_count = 0, pair_count = 0; - void *base_ptr = get_component( - world, base_info.table, base_info.row, component); - if (base_ptr) { - int16_t data_size = column->size; - void *data_array = ecs_vector_first_t( - column->data, column->size, column->alignment); - void *data_ptr = ECS_OFFSET(data_array, data_size * row); + for (i = 0; i < count; i ++) { + ecs_term_t *term = &terms[i]; - component = ecs_get_typeid(world, component); - const ecs_type_info_t *cdata = flecs_get_c_info(world, component); - int32_t index; + if (!ECS_HAS_ROLE(term->id, PAIR)) { + continue; + } - ecs_copy_t copy = cdata ? cdata->lifecycle.copy : NULL; - if (copy) { - ecs_entity_t *entities = ecs_vector_first( - data->entities, ecs_entity_t); + if (term->subj.entity != EcsThis) { + continue; + } - void *ctx = cdata->lifecycle.ctx; - for (index = 0; index < count; index ++) { - copy(world, component, &entities[row], &base, - data_ptr, base_ptr, flecs_to_size_t(data_size), 1, ctx); - data_ptr = ECS_OFFSET(data_ptr, data_size); + if (ecs_id_is_wildcard(term->id)) { + pair_count = get_pair_count(type, term->id); + if (!first_count) { + first_count = pair_count; + } else { + if (first_count != pair_count) { + /* The pairs that this query subscribed for occur in the + * table but don't have the same cardinality. Ignore the + * table. This could typically happen for empty tables along + * a path in the table graph. */ + return -1; + } } - } else { - for (index = 0; index < count; index ++) { - ecs_os_memcpy(data_ptr, base_ptr, data_size); - data_ptr = ECS_OFFSET(data_ptr, data_size); - } } - - return true; - } else { - /* If component not found on base, check if base itself inherits */ - ecs_type_t base_type = base_info.table->type; - return override_component(world, component, base_type, data, column, - row, count); } + + return first_count; } static -bool override_component( +ecs_type_t get_term_type( ecs_world_t *world, - ecs_entity_t component, - ecs_type_t type, - ecs_data_t *data, - ecs_column_t *column, - int32_t row, - int32_t count) + ecs_term_t *term, + ecs_entity_t component) { - ecs_entity_t *type_array = ecs_vector_first(type, ecs_entity_t); - int32_t i, type_count = ecs_vector_count(type); - - /* Walk prefabs */ - i = type_count - 1; - do { - ecs_entity_t e = type_array[i]; - - if (!(e & ECS_ROLE_MASK)) { - break; - } - - if (ECS_HAS_RELATION(e, EcsIsA)) { - if (override_from_base(world, ecs_pair_object(world, e), component, - data, column, row, count)) - { - return true; - } - } - } while (--i >= 0); + ecs_oper_kind_t oper = term->oper; + ecs_assert(oper == EcsAndFrom || oper == EcsOrFrom || oper == EcsNotFrom, + ECS_INTERNAL_ERROR, NULL); + (void)oper; - return false; + const EcsType *type = ecs_get(world, component, EcsType); + if (type) { + return type->normalized; + } else { + return ecs_get_type(world, component); + } } +/** Add table to system, compute offsets for system components in table it */ static -void components_override( +void add_table( ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t row, - int32_t count, - ecs_ids_t *added) + ecs_query_t *query, + ecs_table_t *table) { - ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); - - ecs_column_t *columns = data->columns; - ecs_type_t type = table->type; - ecs_table_t *storage_table = table->storage_table; + ecs_type_t table_type = NULL; + ecs_term_t *terms = query->filter.terms; + int32_t t, c, term_count = query->filter.term_count; - int i; - for (i = 0; i < added->count; i ++) { - ecs_entity_t id = added->array[i]; + if (table) { + table_type = table->type; + } - if (ECS_HAS_RELATION(id, EcsIsA)) { - ecs_entity_t base = ECS_PAIR_OBJECT(id); + int32_t pair_cur = 0, pair_count = count_pairs(query, table_type); + + /* If the query has pairs, we need to account for the fact that a table may + * have multiple components to which the pair is applied, which means the + * table has to be registered with the query multiple times, with different + * table columns. If so, allocate a small array for each pair in which the + * last added table index of the pair is stored, so that in the next + * iteration we can start the search from the correct offset type. */ + pair_offset_t *pair_offsets = NULL; + if (pair_count) { + pair_offsets = ecs_os_calloc( + ECS_SIZEOF(pair_offset_t) * term_count); + } - /* Cannot inherit from base if base is final */ - ecs_assert(!ecs_has_id(world, ecs_get_alive(world, base), EcsFinal), - ECS_INVALID_PARAMETER, NULL); + /* From here we recurse */ + ecs_query_table_match_t *table_data; + ecs_vector_t *references = NULL; - ecs_assert(base != 0, ECS_INVALID_PARAMETER, NULL); + ecs_query_table_t *qt = ecs_table_cache_insert( + &query->cache, ecs_query_table_t, table); - if (!world->stage.base) { - /* Setting base prevents instantiating the hierarchy multiple - * times. The instantiate function recursively iterates the - * hierarchy to instantiate children. While this is happening, - * new tables are created which end up calling this function, - * which would call instantiate multiple times for the same - * level in the hierarchy. */ - world->stage.base = base; - instantiate(world, base, table, data, row, count); - world->stage.base = 0; - } - } +add_pair: + table_data = cache_add(qt); + table_data->table = table; + if (table) { + table_type = table->type; + } - if (!storage_table) { - continue; - } + if (term_count) { + /* Array that contains the system column to table column mapping */ + table_data->columns = ecs_os_calloc_n(int32_t, query->filter.term_count_actual); + ecs_assert(table_data->columns != NULL, ECS_OUT_OF_MEMORY, NULL); - ecs_table_record_t *tr = flecs_get_table_record( - world, storage_table, id); - if (!tr) { - continue; - } + /* Store the components of the matched table. In the case of OR expressions, + * components may differ per matched table. */ + table_data->ids = ecs_os_calloc_n(ecs_entity_t, query->filter.term_count_actual); + ecs_assert(table_data->ids != NULL, ECS_OUT_OF_MEMORY, NULL); - ecs_column_t *column = &columns[tr->column]; - if (!column->size) { - continue; - } + /* Cache subject (source) entity ids for components */ + table_data->subjects = ecs_os_calloc_n(ecs_entity_t, query->filter.term_count_actual); + ecs_assert(table_data->subjects != NULL, ECS_OUT_OF_MEMORY, NULL); - override_component(world, id, type, data, column, row, count); + /* Cache subject (source) entity ids for components */ + table_data->sizes = ecs_os_calloc_n(ecs_size_t, query->filter.term_count_actual); + ecs_assert(table_data->sizes != NULL, ECS_OUT_OF_MEMORY, NULL); } -} - -static -void set_switch( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t row, - int32_t count, - ecs_ids_t *entities, - bool reset) -{ - ecs_entity_t *array = entities->array; - int32_t i, comp_count = entities->count; - - for (i = 0; i < comp_count; i ++) { - ecs_entity_t e = array[i]; - - if (ECS_HAS_ROLE(e, CASE)) { - e = e & ECS_COMPONENT_MASK; - ecs_entity_t sw_case = 0; - if (!reset) { - sw_case = e; - ecs_assert(sw_case != 0, ECS_INTERNAL_ERROR, NULL); - } + /* Walk columns parsed from the system signature */ + c = 0; + for (t = 0; t < term_count; t ++) { + ecs_term_t *term = &terms[t]; + ecs_term_id_t subj = term->subj; + ecs_entity_t entity = 0, component = 0; + ecs_oper_kind_t op = term->oper; - int32_t sw_index = flecs_table_switch_from_case(world, table, e); - ecs_assert(sw_index != -1, ECS_INTERNAL_ERROR, NULL); - ecs_switch_t *sw = data->sw_columns[sw_index].data; - ecs_assert(sw != NULL, ECS_INTERNAL_ERROR, NULL); - - int32_t r; - for (r = 0; r < count; r ++) { - flecs_switch_set(sw, row + r, sw_case); - } + if (op == EcsNot) { + subj.entity = 0; } - } -} -static -void ecs_components_switch( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - int32_t row, - int32_t count, - ecs_ids_t *added, - ecs_ids_t *removed) -{ - if (added) { - set_switch(world, table, data, row, count, added, false); - } - if (removed) { - set_switch(world, table, data, row, count, removed, true); - } -} + /* Get actual component and component source for current column */ + bool match; + t = get_comp_and_src(world, query, t, table, &component, &entity, &match); -static -int32_t new_entity( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_info_t *info, - ecs_table_t *new_table, - ecs_table_diff_t *diff, - bool construct) -{ - ecs_record_t *record = info->record; - ecs_data_t *new_data = &new_table->storage; - int32_t new_row; + /* This column does not retrieve data from a static entity */ + if (!entity && subj.entity) { + int32_t index = get_component_index(world, table, table_type, + &component, c, op, pair_offsets, pair_cur + 1); - if (!record) { - record = ecs_eis_ensure(world, entity); - } + if (index == -1) { + if (op == EcsOptional && subj.set.mask == EcsSelf) { + index = 0; + } + } else { + if (op == EcsOptional && !(subj.set.mask & EcsSelf)) { + index = 0; + } + } - new_row = flecs_table_append( - world, new_table, new_data, entity, record, construct); + table_data->columns[c] = index; - record->table = new_table; - record->row = flecs_row_to_record(new_row, info->is_watched); + /* If the column is a case, we should only iterate the entities in + * the column for this specific case. Add a sparse column with the + * case id so we can find the correct entities when iterating */ + if (ECS_HAS_ROLE(component, CASE)) { + flecs_sparse_column_t *sc = ecs_vector_add( + &table_data->sparse_columns, flecs_sparse_column_t); + sc->signature_column_index = t; + sc->sw_case = component & ECS_COMPONENT_MASK; + sc->sw_column = NULL; + } - ecs_assert( - ecs_vector_count(new_data[0].entities) > new_row, - ECS_INTERNAL_ERROR, NULL); + /* If table has a disabled bitmask for components, check if there is + * a disabled column for the queried for component. If so, cache it + * in a vector as the iterator will need to skip the entity when the + * component is disabled. */ + if (index && (table && table->flags & EcsTableHasDisabled)) { + ecs_entity_t bs_id = + (component & ECS_COMPONENT_MASK) | ECS_DISABLED; + int32_t bs_index = ecs_type_index_of(table->type, 0, bs_id); + if (bs_index != -1) { + flecs_bitset_column_t *elem = ecs_vector_add( + &table_data->bitset_columns, flecs_bitset_column_t); + elem->column_index = bs_index; + elem->bs_column = NULL; + } + } + } - if (new_table->flags & EcsTableHasAddActions) { - flecs_notify_on_add( - world, new_table, NULL, new_data, new_row, 1, diff, true); - } + ecs_entity_t type_id = ecs_get_typeid(world, component); + if (!type_id && !(ECS_ROLE_MASK & component)) { + type_id = component; + } - info->data = new_data; + if (entity || table_data->columns[c] == -1 || subj.set.mask & EcsCascade) { + if (type_id) { + references = add_ref(world, query, references, term, + component, entity); + table_data->columns[c] = -ecs_vector_count(references); + } - return new_row; -} + table_data->subjects[c] = entity; + flecs_set_watch(world, entity); -static -int32_t move_entity( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_info_t *info, - ecs_table_t *src_table, - ecs_data_t *src_data, - int32_t src_row, - ecs_table_t *dst_table, - ecs_table_diff_t *diff, - bool construct) -{ - ecs_data_t *dst_data = &dst_table->storage; - ecs_assert(src_data != dst_data, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ecs_is_alive(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(src_table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(src_data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(src_row >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ecs_vector_count(src_data->entities) > src_row, - ECS_INTERNAL_ERROR, NULL); + if (!match) { + ecs_ref_t *ref = ecs_vector_last(references, ecs_ref_t); + ref->entity = 0; + } + } - ecs_record_t *record = info->record; - ecs_assert(!record || record == ecs_eis_get(world, entity), - ECS_INTERNAL_ERROR, NULL); + if (type_id) { + const EcsComponent *cptr = ecs_get(world, type_id, EcsComponent); + if (!cptr || !cptr->size) { + int32_t column = table_data->columns[c]; + if (column < 0) { + ecs_ref_t *r = ecs_vector_get( + references, ecs_ref_t, -column - 1); + r->component = 0; + } + } - int32_t dst_row = flecs_table_append(world, dst_table, dst_data, entity, - record, false); + if (cptr) { + table_data->sizes[c] = cptr->size; + } else { + table_data->sizes[c] = 0; + } + } else { + table_data->sizes[c] = 0; + } - ecs_assert(ecs_vector_count(src_data->entities) > src_row, - ECS_INTERNAL_ERROR, NULL); - /* Copy entity & components from src_table to dst_table */ - if (src_table->type) { - flecs_notify_on_remove( - world, src_table, dst_table, src_row, 1, diff); + if (ECS_HAS_ROLE(component, SWITCH)) { + table_data->sizes[c] = ECS_SIZEOF(ecs_entity_t); + } else if (ECS_HAS_ROLE(component, CASE)) { + table_data->sizes[c] = ECS_SIZEOF(ecs_entity_t); + } + table_data->ids[c] = component; - flecs_table_move(world, entity, entity, dst_table, dst_data, dst_row, - src_table, src_data, src_row, construct); + c ++; } - /* Update entity index & delete old data after running remove actions */ - record->table = dst_table; - record->row = flecs_row_to_record(dst_row, info->is_watched); + if (references) { + ecs_size_t ref_size = ECS_SIZEOF(ecs_ref_t) * ecs_vector_count(references); + table_data->references = ecs_os_malloc(ref_size); + ecs_os_memcpy(table_data->references, + ecs_vector_first(references, ecs_ref_t), ref_size); + ecs_vector_free(references); + references = NULL; + } - flecs_table_delete(world, src_table, src_data, src_row, false); + /* Insert match to iteration list if table is not empty */ + if (!table || ecs_table_count(table) != 0) { + ecs_assert(table == qt->hdr.table, ECS_INTERNAL_ERROR, NULL); + insert_table_node(query, &table_data->node); + } - /* If components were added, invoke add actions */ - if (src_table != dst_table || diff->added.count) { - if (diff->added.count && (dst_table->flags & EcsTableHasAddActions)) { - flecs_notify_on_add(world, dst_table, src_table, dst_data, - dst_row, 1, diff, true); - } + /* Use tail recursion when adding table for multiple pairs */ + pair_cur ++; + if (pair_cur < pair_count) { + goto add_pair; } - info->data = dst_data; + if (table && !(query->flags & EcsQueryIsSubquery)) { + flecs_table_notify(world, table, &(ecs_table_event_t){ + .kind = EcsTableQueryMatch, + .query = query + }); + } - return dst_row; + if (pair_offsets) { + ecs_os_free(pair_offsets); + } } static -void delete_entity( - ecs_world_t *world, - ecs_table_t *src_table, - ecs_data_t *src_data, - int32_t src_row, - ecs_table_diff_t *diff) +bool match_term( + const ecs_world_t *world, + const ecs_table_t *table, + ecs_term_t *term) { - if (src_table) { - /* Invoke remove actions before deleting */ - if (src_table->flags & EcsTableHasRemoveActions) { - flecs_notify_on_remove(world, src_table, NULL, src_row, 1, diff); - } + ecs_term_id_t *subj = &term->subj; + + /* If term has no subject, there's nothing to match */ + if (!subj->entity) { + return true; } - flecs_table_delete(world, src_table, src_data, src_row, true); + if (term->subj.entity != EcsThis) { + table = ecs_get_table(world, subj->entity); + } + + return ecs_type_match( + world, table, table->type, 0, term->id, subj->set.relation, + subj->set.min_depth, subj->set.max_depth, NULL, NULL) != -1; } -/* Updating component monitors is a relatively expensive operation that only - * happens for entities that are monitored. The approach balances the amount of - * processing between the operation on the entity vs the amount of work that - * needs to be done to rematch queries, as a simple brute force approach does - * not scale when there are many tables / queries. Therefore we need to do a bit - * of bookkeeping that is more intelligent than simply flipping a flag */ -static -void update_component_monitor_w_array( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_t relation, - ecs_ids_t *entities) +/* Match table with query */ +bool flecs_query_match( + const ecs_world_t *world, + const ecs_table_t *table, + const ecs_query_t *query) { - if (!entities) { - return; + if (!(query->flags & EcsQueryNeedsTables)) { + return false; } - int i; - for (i = 0; i < entities->count; i ++) { - ecs_entity_t id = entities->array[i]; - if (ECS_HAS_ROLE(id, PAIR)) { - ecs_entity_t rel = ECS_PAIR_RELATION(id); - - /* If a relationship has changed, check if it could have impacted - * the shape of the graph for that relationship. If so, mark the - * relationship as dirty */ - if (rel != relation && flecs_get_id_record(world, ecs_pair(rel, entity))) { - update_component_monitor_w_array(world, entity, rel, entities); - } + ecs_type_t table_type = table->type; - } - - if (ECS_HAS_RELATION(id, EcsIsA)) { - /* If an IsA relationship is added to a monitored entity (can - * be either a parent or a base) component monitors need to be - * evaluated for the components of the prefab. */ - ecs_entity_t base = ecs_pair_object(world, id); - ecs_type_t type = ecs_get_type(world, base); - ecs_ids_t base_entities = flecs_type_to_ids(type); + /* Don't match disabled entities */ + if (!(query->flags & EcsQueryMatchDisabled) && ecs_type_has_id( + world, table_type, EcsDisabled, true)) + { + return false; + } - /* This evaluates the component monitor for all components of the - * base entity. If the base entity contains IsA relationships - * these will be evaluated recursively as well. */ - update_component_monitor_w_array( - world, entity, relation, &base_entities); - } else { - flecs_monitor_mark_dirty(world, relation, id); - } + /* Don't match prefab entities */ + if (!(query->flags & EcsQueryMatchPrefab) && ecs_type_has_id( + world, table_type, EcsPrefab, true)) + { + return false; } -} -static -void update_component_monitors( - ecs_world_t *world, - ecs_entity_t entity, - ecs_ids_t *added, - ecs_ids_t *removed) -{ - update_component_monitor_w_array(world, entity, 0, added); - update_component_monitor_w_array(world, entity, 0, removed); -} + /* Check if pair cardinality matches pairs in query, if any */ + if (count_pairs(query, table->type) == -1) { + return false; + } -static -void commit( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_info_t *info, - ecs_table_t *dst_table, - ecs_table_diff_t *diff, - bool construct) -{ - ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); - - ecs_table_t *src_table = info->table; - if (src_table == dst_table) { - /* If source and destination table are the same no action is needed * - * However, if a component was added in the process of traversing a - * table, this suggests that a case switch could have occured. */ - if (((diff->added.count) || (diff->removed.count)) && - src_table && src_table->flags & EcsTableHasSwitch) - { - ecs_components_switch( - world, src_table, info->data, info->row, 1, - &diff->added, &diff->removed); - } + ecs_term_t *terms = query->filter.terms; + int32_t i, term_count = query->filter.term_count; - return; - } + for (i = 0; i < term_count; i ++) { + ecs_term_t *term = &terms[i]; + ecs_oper_kind_t oper = term->oper; - if (src_table) { - ecs_data_t *src_data = info->data; - ecs_assert(dst_table != NULL, ECS_INTERNAL_ERROR, NULL); + if (oper == EcsAnd) { + if (!match_term(world, table, term)) { + return false; + } - if (dst_table->type) { - info->row = move_entity(world, entity, info, src_table, - src_data, info->row, dst_table, diff, construct); - info->table = dst_table; - } else { - delete_entity(world, src_table, src_data, info->row, diff); + } else if (oper == EcsNot) { + if (match_term(world, table, term)) { + return false; + } - ecs_eis_set(world, entity, &(ecs_record_t){ - NULL, (info->is_watched == true) * -1 - }); - } - } else { - if (dst_table->type) { - info->row = new_entity( - world, entity, info, dst_table, diff, construct); - info->table = dst_table; - } - } + } else if (oper == EcsOr) { + bool match = false; - /* If the entity is being watched, it is being monitored for changes and - * requires rematching systems when components are added or removed. This - * ensures that systems that rely on components from containers or prefabs - * update the matched tables when the application adds or removes a - * component from, for example, a container. */ - if (info->is_watched) { - update_component_monitors(world, entity, &diff->added, &diff->removed); + for (; i < term_count; i ++) { + term = &terms[i]; + if (term->oper != EcsOr) { + i --; + break; + } + + if (!match && match_term( world, table, term)) { + match = true; + } + } + + if (!match) { + return false; + } + + } else if (oper == EcsAndFrom || oper == EcsOrFrom || oper == EcsNotFrom) { + ecs_type_t type = get_term_type((ecs_world_t*)world, term, term->id); + int32_t match_count = 0, j, count = ecs_vector_count(type); + ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); + + for (j = 0; j < count; j ++) { + ecs_term_t tmp_term = *term; + tmp_term.oper = EcsAnd; + tmp_term.id = ids[j]; + tmp_term.pred.entity = ids[j]; + + if (match_term(world, table, &tmp_term)) { + match_count ++; + } + } + + if (oper == EcsAndFrom && match_count != count) { + return false; + } + if (oper == EcsOrFrom && match_count == 0) { + return false; + } + if (oper == EcsNotFrom && match_count != 0) { + return false; + } + } } - if ((!src_table || !src_table->type) && world->range_check_enabled) { - ecs_assert(!world->stats.max_id || entity <= world->stats.max_id, ECS_OUT_OF_RANGE, 0); - ecs_assert(entity >= world->stats.min_id, ECS_OUT_OF_RANGE, 0); - } + return true; } +/** Match existing tables against system (table is created before system) */ static -void new( +void match_tables( ecs_world_t *world, - ecs_entity_t entity, - ecs_ids_t *to_add) + ecs_query_t *query) { - ecs_entity_info_t info = {0}; - int32_t i, count = to_add->count; - ecs_table_t *table = &world->store.root; - - ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; - for (i = 0; i < count; i ++) { - table = table_append(world, table, to_add->array[i], &diff); - } + int32_t i, count = flecs_sparse_count(world->store.tables); - new_entity(world, entity, &info, table, &diff, true); + for (i = 0; i < count; i ++) { + ecs_table_t *table = flecs_sparse_get_dense( + world->store.tables, ecs_table_t, i); - diff_free(&diff); + if (flecs_query_match(world, table, query)) { + add_table(world, query, table); + } + } } +#define ELEM(ptr, size, index) ECS_OFFSET(ptr, size * index) + static -const ecs_entity_t* new_w_data( +int32_t qsort_partition( ecs_world_t *world, ecs_table_t *table, - const ecs_entity_t *entities, - ecs_ids_t *component_ids, - int32_t count, - void **component_data, - bool is_move, - int32_t *row_out, - ecs_table_diff_t *diff) + ecs_data_t *data, + ecs_entity_t *entities, + void *ptr, + int32_t elem_size, + int32_t lo, + int32_t hi, + ecs_order_by_action_t compare) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(count != 0, ECS_INTERNAL_ERROR, NULL); - - int32_t sparse_count = 0; - if (!entities) { - sparse_count = ecs_eis_count(world); - entities = flecs_sparse_new_ids(world->store.entity_index, count); - } + int32_t p = (hi + lo) / 2; + void *pivot = ELEM(ptr, elem_size, p); + ecs_entity_t pivot_e = entities[p]; + int32_t i = lo - 1, j = hi + 1; + void *el; - ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); +repeat: + { + do { + i ++; + el = ELEM(ptr, elem_size, i); + } while ( compare(entities[i], el, pivot_e, pivot) < 0); - if (!table) { - return entities; - } + do { + j --; + el = ELEM(ptr, elem_size, j); + } while ( compare(entities[j], el, pivot_e, pivot) > 0); - ecs_type_t type = table->type; - if (!type) { - return entities; - } + if (i >= j) { + return j; + } - ecs_ids_t component_array = { 0 }; - if (!component_ids) { - component_ids = &component_array; - component_array.array = ecs_vector_first(type, ecs_entity_t); - component_array.count = ecs_vector_count(type); - } + flecs_table_swap(world, table, data, i, j); - ecs_data_t *data = &table->storage; - int32_t row = flecs_table_appendn(world, table, data, count, entities); - - /* Update entity index. */ - int i; - ecs_record_t **record_ptrs = ecs_vector_first(data->record_ptrs, ecs_record_t*); - for (i = 0; i < count; i ++) { - record_ptrs[row + i] = ecs_eis_set(world, entities[i], - &(ecs_record_t){ - .table = table, - .row = row + i + 1 - }); + if (p == i) { + pivot = ELEM(ptr, elem_size, j); + pivot_e = entities[j]; + } else if (p == j) { + pivot = ELEM(ptr, elem_size, i); + pivot_e = entities[i]; + } + + goto repeat; } +} - flecs_defer_none(world, &world->stage); +static +void qsort_array( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + ecs_entity_t *entities, + void *ptr, + int32_t size, + int32_t lo, + int32_t hi, + ecs_order_by_action_t compare) +{ + if ((hi - lo) < 1) { + return; + } - flecs_notify_on_add(world, table, NULL, data, row, count, diff, - component_data == NULL); + int32_t p = qsort_partition( + world, table, data, entities, ptr, size, lo, hi, compare); - if (component_data) { - /* Set components that we're setting in the component mask so the init - * actions won't call OnSet triggers for them. This ensures we won't - * call OnSet triggers multiple times for the same component */ - int32_t c_i; - for (c_i = 0; c_i < component_ids->count; c_i ++) { - void *src_ptr = component_data[c_i]; - if (!src_ptr) { - continue; - } + qsort_array(world, table, data, entities, ptr, size, lo, p, compare); - /* Find component in storage type */ - ecs_entity_t id = component_ids->array[c_i]; - ecs_column_t *column = ecs_table_column_for_id(world, table, id); - ecs_assert(column != NULL, ECS_INTERNAL_ERROR, NULL); + qsort_array(world, table, data, entities, ptr, size, p + 1, hi, compare); +} - int16_t size = column->size; - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); +static +void sort_table( + ecs_world_t *world, + ecs_table_t *table, + int32_t column_index, + ecs_order_by_action_t compare) +{ + ecs_data_t *data = &table->storage; + if (!data->entities) { + /* Nothing to sort */ + return; + } - int16_t alignment = column->alignment; - void *ptr = ecs_vector_first_t(column->data, size, alignment); - ptr = ECS_OFFSET(ptr, size * row); + int32_t count = flecs_table_data_count(data); + if (count < 2) { + return; + } - const ecs_type_info_t *cdata = get_c_info(world, id); - ecs_copy_t copy; - ecs_move_t move; - if (cdata && is_move && (move = cdata->lifecycle.move)) { - ecs_entity_t *eids = ecs_vector_first(data->entities, ecs_entity_t); - move(world, id, eids, eids, ptr, src_ptr, - flecs_to_size_t(size), count, cdata->lifecycle.ctx); - } else if (cdata && !is_move && (copy = cdata->lifecycle.copy)) { - ecs_entity_t *eids = ecs_vector_first(data->entities, ecs_entity_t); - copy(world, id, eids, eids, ptr, src_ptr, - flecs_to_size_t(size), count, cdata->lifecycle.ctx); - } else { - ecs_os_memcpy(ptr, src_ptr, size * count); - } - }; + ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); - flecs_notify_on_set(world, table, row, count, NULL, true); - flecs_notify_on_set(world, table, row, count, &diff->on_set, false); + void *ptr = NULL; + int32_t size = 0; + if (column_index != -1) { + ecs_column_t *column = &data->columns[column_index]; + size = column->size; + ptr = ecs_vector_first_t(column->data, size, column->alignment); } - flecs_defer_flush(world, &world->stage); + qsort_array(world, table, data, entities, ptr, size, 0, count - 1, compare); +} - if (row_out) { - *row_out = row; - } +/* Helper struct for building sorted table ranges */ +typedef struct sort_helper_t { + ecs_query_table_match_t *match; + ecs_entity_t *entities; + const void *ptr; + int32_t row; + int32_t elem_size; + int32_t count; + bool shared; +} sort_helper_t; - if (sparse_count) { - entities = flecs_sparse_ids(world->store.entity_index); - return &entities[sparse_count]; +static +const void* ptr_from_helper( + sort_helper_t *helper) +{ + ecs_assert(helper->row < helper->count, ECS_INTERNAL_ERROR, NULL); + ecs_assert(helper->elem_size >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(helper->row >= 0, ECS_INTERNAL_ERROR, NULL); + if (helper->shared) { + return helper->ptr; } else { - return entities; + return ELEM(helper->ptr, helper->elem_size, helper->row); } } static -void add_id_w_info( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_info_t *info, - ecs_id_t id, - bool construct) +ecs_entity_t e_from_helper( + sort_helper_t *helper) { - ecs_table_diff_t diff; - - ecs_table_t *src_table = info->table; - ecs_table_t *dst_table = flecs_table_traverse_add( - world, src_table, &id, &diff); - - commit(world, entity, info, dst_table, &diff, construct); + if (helper->row < helper->count) { + return helper->entities[helper->row]; + } else { + return 0; + } } static -void add_id( - ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) +void build_sorted_table_range( + ecs_query_t *query, + ecs_query_table_list_t *list) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_stage_t *stage = flecs_stage_from_world(&world); - - if (flecs_defer_add(world, stage, entity, id)) { + ecs_world_t *world = query->world; + ecs_entity_t component = query->order_by_component; + ecs_order_by_action_t compare = query->order_by; + + if (!list->count) { return; } - ecs_entity_info_t info; - flecs_get_info(world, entity, &info); + int to_sort = 0; + sort_helper_t *helper = ecs_os_malloc_n(sort_helper_t, list->count); + ecs_query_table_node_t *cur, *end = list->last->next; + for (cur = list->first; cur != end; cur = cur->next) { + ecs_query_table_match_t *match = cur->match; + ecs_table_t *table = match->table; + ecs_data_t *data = &table->storage; + ecs_vector_t *entities; + if (!(entities = data->entities) || !ecs_table_count(table)) { + continue; + } - ecs_table_diff_t diff; - ecs_table_t *src_table = info.table; - ecs_table_t *dst_table = flecs_table_traverse_add( - world, src_table, &id, &diff); + int32_t index = ecs_type_index_of(table->type, 0, component); + if (index != -1) { + ecs_column_t *column = &data->columns[index]; + int16_t size = column->size; + int16_t align = column->alignment; + helper[to_sort].ptr = ecs_vector_first_t(column->data, size, align); + helper[to_sort].elem_size = size; + helper[to_sort].shared = false; + } else if (component) { + /* Find component in prefab */ + ecs_entity_t base; + ecs_type_match(world, table, table->type, 0, component, + EcsIsA, 1, 0, &base, NULL); - commit(world, entity, &info, dst_table, &diff, true); + /* If a base was not found, the query should not have allowed using + * the component for sorting */ + ecs_assert(base != 0, ECS_INTERNAL_ERROR, NULL); - flecs_defer_flush(world, stage); -} + const EcsComponent *cptr = ecs_get(world, component, EcsComponent); + ecs_assert(cptr != NULL, ECS_INTERNAL_ERROR, NULL); -static -void remove_id( - ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_stage_t *stage = flecs_stage_from_world(&world); + helper[to_sort].ptr = ecs_get_id(world, base, component); + helper[to_sort].elem_size = cptr->size; + helper[to_sort].shared = true; + } else { + helper[to_sort].ptr = NULL; + helper[to_sort].elem_size = 0; + helper[to_sort].shared = false; + } - if (flecs_defer_remove(world, stage, entity, id)) { - return; + helper[to_sort].match = match; + helper[to_sort].entities = ecs_vector_first(entities, ecs_entity_t); + helper[to_sort].row = 0; + helper[to_sort].count = ecs_table_count(table); + to_sort ++; } - ecs_entity_info_t info; - flecs_get_info(world, entity, &info); + bool proceed; + do { + int32_t j, min = 0; + proceed = true; - ecs_table_diff_t diff; - ecs_table_t *src_table = info.table; - ecs_table_t *dst_table = flecs_table_traverse_remove( - world, src_table, &id, &diff); + ecs_entity_t e1; + while (!(e1 = e_from_helper(&helper[min]))) { + min ++; + if (min == to_sort) { + proceed = false; + break; + } + } - commit(world, entity, &info, dst_table, &diff, true); + if (!proceed) { + break; + } - flecs_defer_flush(world, stage); -} + for (j = min + 1; j < to_sort; j++) { + ecs_entity_t e2 = e_from_helper(&helper[j]); + if (!e2) { + continue; + } -static -void *get_mutable( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_t component, - ecs_entity_info_t *info, - bool *is_added) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(component != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert((component & ECS_COMPONENT_MASK) == component || - ECS_HAS_ROLE(component, PAIR), ECS_INVALID_PARAMETER, NULL); + const void *ptr1 = ptr_from_helper(&helper[min]); + const void *ptr2 = ptr_from_helper(&helper[j]); - void *dst = NULL; - if (flecs_get_info(world, entity, info) && info->table) { - dst = get_component(world, info->table, info->row, component); + if (compare(e1, ptr1, e2, ptr2) > 0) { + min = j; + e1 = e_from_helper(&helper[min]); + } + } + + sort_helper_t *cur_helper = &helper[min]; + if (!cur || cur->match != cur_helper->match) { + cur = ecs_vector_add(&query->table_slices, ecs_query_table_node_t); + ecs_assert(cur != NULL, ECS_INTERNAL_ERROR, NULL); + cur->match = cur_helper->match; + cur->offset = cur_helper->row; + cur->count = 1; + } else { + cur->count ++; + } + + cur_helper->row ++; + } while (proceed); + + /* Iterate through the vector of slices to set the prev/next ptrs. This + * can't be done while building the vector, as reallocs may occur */ + int32_t i, count = ecs_vector_count(query->table_slices); + ecs_query_table_node_t *nodes = ecs_vector_first( + query->table_slices, ecs_query_table_node_t); + for (i = 0; i < count; i ++) { + nodes[i].prev = &nodes[i - 1]; + nodes[i].next = &nodes[i + 1]; } - if (!dst) { - ecs_table_t *table = info->table; - add_id_w_info(world, entity, info, component, true); - flecs_get_info(world, entity, info); - ecs_assert(info->table != NULL, ECS_INTERNAL_ERROR, NULL); + nodes[0].prev = NULL; + nodes[i - 1].next = NULL; - ecs_assert(info->table->storage_table != NULL, ECS_INTERNAL_ERROR, NULL); - dst = get_component(world, info->table, info->row, component); + ecs_os_free(helper); +} - if (is_added) { - *is_added = table != info->table; - } +static +void build_sorted_tables( + ecs_query_t *query) +{ + ecs_vector_clear(query->table_slices); - return dst; - } else { - if (is_added) { - *is_added = false; - } + if (query->group_by) { + /* Populate sorted node list in grouping order */ + ecs_query_table_node_t *cur = query->list.first; + if (cur) { + do { + /* Find list for current group */ + ecs_query_table_match_t *match = cur->match; + ecs_assert(match != NULL, ECS_INTERNAL_ERROR, NULL); + uint64_t group_id = match->group_id; + ecs_query_table_list_t *list = ecs_map_get(query->groups, + ecs_query_table_list_t, group_id); + ecs_assert(list != NULL, ECS_INTERNAL_ERROR, NULL); - return dst; + /* Sort tables in current group */ + build_sorted_table_range(query, list); + + /* Find next group to sort */ + cur = list->last->next; + } while (cur); + } + } else { + build_sorted_table_range(query, &query->list); } } +static +bool tables_dirty( + ecs_query_t *query) +{ + bool is_dirty = false; -/* -- Private functions -- */ + ecs_vector_t *vec = query->cache.tables; + ecs_query_table_t *tables = ecs_vector_first(vec, ecs_query_table_t); + int32_t i, count = ecs_vector_count(vec); -void flecs_notify_on_add( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_t *other_table, - ecs_data_t *data, - int32_t row, - int32_t count, - ecs_table_diff_t *diff, - bool run_on_set) -{ - ecs_assert(diff != NULL, ECS_INTERNAL_ERROR, NULL); + for (i = 0; i < count; i ++) { + ecs_query_table_t *qt = &tables[i]; + ecs_table_t *table = qt->hdr.table; - if (table->flags & EcsTableHasIsA) { - components_override(world, table, data, row, count, &diff->added); - } + if (!qt->monitor) { + qt->monitor = flecs_table_get_monitor(table); + is_dirty = true; + } - if (table->flags & EcsTableHasSwitch) { - ecs_components_switch( - world, table, data, row, count, &diff->added, NULL); + int32_t *dirty_state = flecs_table_get_dirty_state(table); + int32_t t, storage_count = ecs_table_storage_count(table); + for (t = 0; t < storage_count + 1; t ++) { + is_dirty = is_dirty || (dirty_state[t] != qt->monitor[t]); + } } - if (table->flags & EcsTableHasOnAdd) { - notify(world, table, other_table, row, count, EcsOnAdd, &diff->added); - } + is_dirty = is_dirty || (query->match_count != query->prev_match_count); - if (run_on_set) { - notify(world, table, other_table, row, count, EcsOnSet, &diff->on_set); - } + return is_dirty; } -void flecs_notify_on_remove( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_t *other_table, - int32_t row, - int32_t count, - ecs_table_diff_t *diff) +static +void tables_reset_dirty( + ecs_query_t *query) { - ecs_assert(diff != NULL, ECS_INTERNAL_ERROR, NULL); + query->prev_match_count = query->match_count; - if (count) { - notify(world, table, other_table, row, count, EcsUnSet, &diff->un_set); + ecs_vector_t *vec = query->cache.tables; + ecs_query_table_t *tables = ecs_vector_first(vec, ecs_query_table_t); + int32_t i, count = ecs_vector_count(vec); - if (table->flags & EcsTableHasOnRemove) { - notify(world, table, other_table, row, count, EcsOnRemove, - &diff->removed); + for (i = 0; i < count; i ++) { + ecs_query_table_t *qt = &tables[i]; + ecs_table_t *table = qt->hdr.table; + + if (!qt->monitor) { + /* If one table doesn't have a monitor, none of the tables will have + * a monitor, so early out. */ + return; } - if (table->flags & EcsTableHasIsA) { - notify(world, table, other_table, row, count, EcsOnSet, &diff->on_set); + int32_t *dirty_state = flecs_table_get_dirty_state(table); + int32_t t, storage_count = ecs_table_storage_count(table); + for (t = 0; t < storage_count + 1; t ++) { + qt->monitor[t] = dirty_state[t]; } } } -void flecs_notify_on_set( +static +void sort_tables( ecs_world_t *world, - ecs_table_t *table, - int32_t row, - int32_t count, - ecs_ids_t *ids, - bool owned) + ecs_query_t *query) { - ecs_data_t *data = &table->storage; + ecs_order_by_action_t compare = query->order_by; + if (!compare) { + return; + } + + ecs_entity_t order_by_component = query->order_by_component; - ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); - ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(row < ecs_vector_count(data->entities), - ECS_INTERNAL_ERROR, NULL); - ecs_assert((row + count) <= ecs_vector_count(data->entities), - ECS_INTERNAL_ERROR, NULL); - entities = ECS_OFFSET(entities, ECS_SIZEOF(ecs_entity_t) * row); + /* Iterate over non-empty tables. Don't bother with empty tables as they + * have nothing to sort */ - ecs_ids_t local_ids; - if (!ids) { - local_ids.array = ecs_vector_first(table->storage_type, ecs_id_t); - local_ids.count = ecs_vector_count(table->storage_type); - ids = &local_ids; - } + bool tables_sorted = false; + ecs_vector_t *vec = query->cache.tables; + ecs_query_table_t *tables = ecs_vector_first(vec, ecs_query_table_t); + int32_t i, count = ecs_vector_count(vec); - if (owned) { - int i; - for (i = 0; i < ids->count; i ++) { - ecs_id_t id = ids->array[i]; - const ecs_type_info_t *info = get_c_info(world, id); - ecs_on_set_t on_set; - if (info && (on_set = info->lifecycle.on_set)) { - ecs_column_t *c = ecs_table_column_for_id(world, table, id); - ecs_size_t size = c->size; - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + for (i = 0; i < count; i ++) { + ecs_query_table_t *qt = &tables[i]; + ecs_table_t *table = qt->hdr.table; - void *ptr = ecs_vector_get_t(c->data, size, c->alignment, row); - on_set(world, id, entities, ptr, flecs_to_size_t(size), - count, info->lifecycle.ctx); + /* If no monitor had been created for the table yet, create it now */ + bool is_dirty = false; + if (!qt->monitor) { + qt->monitor = flecs_table_get_monitor(table); + + /* A new table is always dirty */ + is_dirty = true; + } + + int32_t *dirty_state = flecs_table_get_dirty_state(table); + is_dirty = is_dirty || (dirty_state[0] != qt->monitor[0]); + + int32_t index = -1; + if (order_by_component) { + /* Get index of sorted component. We only care if the component we're + * sorting on has changed or if entities have been added / re(moved) */ + index = ecs_type_index_of(table->type, 0, order_by_component); + if (index != -1) { + ecs_assert(index < ecs_vector_count(table->type), + ECS_INTERNAL_ERROR, NULL); + is_dirty = is_dirty || (dirty_state[index + 1] != qt->monitor[index + 1]); + } else { + /* Table does not contain component which means the sorted + * component is shared. Table does not need to be sorted */ + continue; } + } + + /* Check both if entities have moved (element 0) or if the component + * we're sorting on has changed (index + 1) */ + if (is_dirty) { + /* Sort the table */ + sort_table(world, table, index, compare); + tables_sorted = true; } } - /* Run OnSet notifications */ - if (table->flags & EcsTableHasOnSet) { - notify(world, table, NULL, row, count, EcsOnSet, ids); + if (tables_sorted || query->match_count != query->prev_match_count) { + build_sorted_tables(query); + query->match_count ++; /* Increase version if tables changed */ } } -bool flecs_get_info( - const ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_info_t *info) +static +bool has_refs( + ecs_query_t *query) { - info->table = NULL; - info->record = NULL; - info->data = NULL; - info->is_watched = false; + ecs_term_t *terms = query->filter.terms; + int32_t i, count = query->filter.term_count; - if (entity & ECS_ROLE) { - return false; - } - - ecs_record_t *record = ecs_eis_get(world, entity); + for (i = 0; i < count; i ++) { + ecs_term_t *term = &terms[i]; + ecs_term_id_t *subj = &term->subj; - if (!record) { - return false; + if (term->oper == EcsNot && !subj->entity) { + /* Special case: if oper kind is Not and the query contained a + * shared expression, the expression is translated to FromEmpty to + * prevent resolving the ref */ + return true; + } else if (subj->entity && (subj->entity != EcsThis || subj->set.mask != EcsSelf)) { + /* If entity is not this, or if it can be substituted by other + * entities, the query can have references. */ + return true; + } } - set_info_from_record(info, record); - - return true; + return false; } -int32_t flecs_record_to_row( - int32_t row, - bool *is_watched_out) +static +bool has_pairs( + ecs_query_t *query) { - bool is_watched = row < 0; - row = row * -(is_watched * 2 - 1) - 1 * (row != 0); - *is_watched_out = is_watched; - return row; -} + ecs_term_t *terms = query->filter.terms; + int32_t i, count = query->filter.term_count; -int32_t flecs_row_to_record( - int32_t row, - bool is_watched) -{ - return (row + 1) * -(is_watched * 2 - 1); -} + for (i = 0; i < count; i ++) { + if (ecs_id_is_wildcard(terms[i].id)) { + return true; + } + } -ecs_ids_t flecs_type_to_ids( - ecs_type_t type) -{ - return (ecs_ids_t){ - .array = ecs_vector_first(type, ecs_entity_t), - .count = ecs_vector_count(type) - }; + return false; } -void flecs_set_watch( +static +void register_monitors( ecs_world_t *world, - ecs_entity_t entity) -{ - (void)world; + ecs_query_t *query) +{ + ecs_term_t *terms = query->filter.terms; + int32_t i, count = query->filter.term_count; - ecs_record_t *record = ecs_eis_get(world, entity); - if (!record) { - ecs_record_t new_record = {.row = -1, .table = NULL}; - ecs_eis_set(world, entity, &new_record); - } else { - if (record->row > 0) { - record->row *= -1; + for (i = 0; i < count; i++) { + ecs_term_t *term = &terms[i]; + ecs_term_id_t *subj = &term->subj; - } else if (record->row == 0) { - /* If entity is empty, there is no index to change the sign of. In - * this case, set the index to -1, and assign an empty type. */ - record->row = -1; - record->table = NULL; + /* If component is requested with EcsCascade register component as a + * parent monitor. Parent monitors keep track of whether an entity moved + * in the hierarchy, which potentially requires the query to reorder its + * tables. + * Also register a regular component monitor for EcsCascade columns. + * This ensures that when the component used in the EcsCascade column + * is added or removed tables are updated accordingly*/ + if (subj->set.mask & EcsSuperSet && subj->set.mask & EcsCascade && + subj->set.relation != EcsIsA) + { + if (term->oper != EcsOr) { + if (term->subj.set.relation != EcsIsA) { + flecs_monitor_register( + world, term->subj.set.relation, term->id, query); + } + flecs_monitor_register(world, 0, term->id, query); + } + + /* FromAny also requires registering a monitor, as FromAny columns can + * be matched with prefabs. The only term kinds that do not require + * registering a monitor are FromOwned and FromEmpty. */ + } else if ((subj->set.mask & EcsSuperSet) || (subj->entity != EcsThis)){ + if (term->oper != EcsOr) { + flecs_monitor_register(world, 0, term->id, query); + } } - } + }; } - -/* -- Public functions -- */ - -bool ecs_commit( +static +void process_signature( ecs_world_t *world, - ecs_entity_t entity, - ecs_record_t *record, - ecs_table_t *table, - ecs_ids_t *added, - ecs_ids_t *removed) + ecs_query_t *query) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_term_t *terms = query->filter.terms; + int32_t i, count = query->filter.term_count; - ecs_table_t *src_table = NULL; - if (!record) { - record = ecs_eis_get(world, entity); - src_table = record->table; - } + for (i = 0; i < count; i ++) { + ecs_term_t *term = &terms[i]; + ecs_term_id_t *pred = &term->pred; + ecs_term_id_t *subj = &term->subj; + ecs_term_id_t *obj = &term->obj; + ecs_oper_kind_t op = term->oper; + ecs_inout_kind_t inout = term->inout; - ecs_entity_info_t info = {0}; - if (record) { - set_info_from_record(&info, record); - } + (void)pred; + (void)obj; - ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; - if (added) { - diff.added = *added; - } - if (removed) { - diff.added = *removed; - } - - commit(world, entity, &info, table, &diff, true); + /* Queries do not support variables */ + ecs_assert(pred->var != EcsVarIsVariable, + ECS_UNSUPPORTED, NULL); + ecs_assert(subj->entity == EcsThis || subj->var != EcsVarIsVariable, + ECS_UNSUPPORTED, NULL); + ecs_assert(obj->var != EcsVarIsVariable, + ECS_UNSUPPORTED, NULL); - return src_table != table; -} + /* If self is not included in set, always start from depth 1 */ + if (!subj->set.min_depth && !(subj->set.mask & EcsSelf)) { + subj->set.min_depth = 1; + } -ecs_entity_t ecs_new_id( - ecs_world_t *world) -{ - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + if (inout != EcsIn) { + query->flags |= EcsQueryHasOutColumns; + } - const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); + if (op == EcsOptional) { + query->flags |= EcsQueryHasOptional; + } - /* It is possible that the world passed to this function is a stage, so - * make sure we have the actual world. Cast away const since this is one of - * the few functions that may modify the world while it is in readonly mode, - * since it is thread safe (uses atomic inc when in threading mode) */ - ecs_world_t *unsafe_world = (ecs_world_t*)ecs_get_world(world); + if (!(query->flags & EcsQueryMatchDisabled)) { + if (op == EcsAnd || op == EcsOr || op == EcsOptional) { + if (term->id == EcsDisabled) { + query->flags |= EcsQueryMatchDisabled; + } + } + } - ecs_entity_t entity; + if (!(query->flags & EcsQueryMatchPrefab)) { + if (op == EcsAnd || op == EcsOr || op == EcsOptional) { + if (term->id == EcsPrefab) { + query->flags |= EcsQueryMatchPrefab; + } + } + } - int32_t stage_count = ecs_get_stage_count(unsafe_world); - if (stage->asynchronous || (ecs_os_has_threading() && stage_count > 1)) { - /* Can't atomically increase number above max int */ - ecs_assert( - unsafe_world->stats.last_id < UINT_MAX, ECS_INTERNAL_ERROR, NULL); + if (subj->entity == EcsThis) { + query->flags |= EcsQueryNeedsTables; + } - entity = (ecs_entity_t)ecs_os_ainc( - (int32_t*)&unsafe_world->stats.last_id); - } else { - entity = ecs_eis_recycle(unsafe_world); + if (subj->set.mask & EcsCascade && term->oper == EcsOptional) { + /* Query can only have one cascade column */ + ecs_assert(query->cascade_by == 0, ECS_INVALID_PARAMETER, NULL); + query->cascade_by = i + 1; + } + + if (subj->entity && subj->entity != EcsThis && + subj->set.mask == EcsSelf) + { + flecs_set_watch(world, term->subj.entity); + } } - ecs_assert(!unsafe_world->stats.max_id || - ecs_entity_t_lo(entity) <= unsafe_world->stats.max_id, - ECS_OUT_OF_RANGE, NULL); + query->flags |= (ecs_flags32_t)(has_refs(query) * EcsQueryHasRefs); + query->flags |= (ecs_flags32_t)(has_pairs(query) * EcsQueryHasTraits); - return entity; + if (!(query->flags & EcsQueryIsSubquery)) { + register_monitors(world, query); + } } -ecs_entity_t ecs_set_with( +static +bool match_table( ecs_world_t *world, - ecs_id_t id) + ecs_query_t *query, + ecs_table_t *table) { - ecs_stage_t *stage = flecs_stage_from_world(&world); - ecs_id_t prev = stage->with; - stage->with = id; - return prev; + if (flecs_query_match(world, table, query)) { + add_table(world, query, table); + return true; + } + return false; } -ecs_entity_t ecs_get_with( - const ecs_world_t *world) +/** When a table becomes empty remove it from the query list, or vice versa. */ +static +void update_table( + ecs_query_t *query, + ecs_table_t *table, + bool empty) { - const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); - return stage->with; -} + int32_t prev_count = ecs_query_table_count(query); + ecs_table_cache_set_empty(&query->cache, table, empty); + int32_t cur_count = ecs_query_table_count(query); -ecs_entity_t ecs_new_low_id( - ecs_world_t *world) -{ - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + if (prev_count != cur_count) { + ecs_query_table_t *qt = ecs_table_cache_get( + &query->cache, ecs_query_table_t, table); + ecs_assert(qt != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_query_table_match_t *cur, *next; - /* It is possible that the world passed to this function is a stage, so - * make sure we have the actual world. Cast away const since this is one of - * the few functions that may modify the world while it is in readonly mode, - * but only if single threaded. */ - ecs_world_t *unsafe_world = (ecs_world_t*)ecs_get_world(world); + for (cur = qt->first; cur != NULL; cur = next) { + next = cur->next_match; - if (unsafe_world->is_readonly) { - /* Can't issue new comp id while iterating when in multithreaded mode */ - ecs_assert(ecs_get_stage_count(world) <= 1, - ECS_INVALID_WHILE_ITERATING, NULL); + if (empty) { + ecs_assert(ecs_table_count(table) == 0, + ECS_INTERNAL_ERROR, NULL); + + remove_table_node(query, &cur->node); + } else { + ecs_assert(ecs_table_count(table) != 0, + ECS_INTERNAL_ERROR, NULL); + insert_table_node(query, &cur->node); + } + } } - ecs_entity_t id; + ecs_assert(cur_count || query->list.first == NULL, + ECS_INTERNAL_ERROR, NULL); +} - if (unsafe_world->stats.last_component_id < ECS_HI_COMPONENT_ID) { - do { - id = unsafe_world->stats.last_component_id ++; - } while (ecs_exists(unsafe_world, id) && id < ECS_HI_COMPONENT_ID); +static +void add_subquery( + ecs_world_t *world, + ecs_query_t *parent, + ecs_query_t *subquery) +{ + ecs_query_t **elem = ecs_vector_add(&parent->subqueries, ecs_query_t*); + *elem = subquery; + + ecs_table_cache_t *cache = &parent->cache; + ecs_vector_t *tables = cache->tables, *empty = cache->empty_tables; + + ecs_query_table_t *elems = ecs_vector_first(tables, ecs_query_table_t); + int32_t i, count = ecs_vector_count(tables); + for (i = 0; i < count; i ++) { + match_table(world, subquery, elems[i].hdr.table); } - - if (unsafe_world->stats.last_component_id >= ECS_HI_COMPONENT_ID) { - /* If the low component ids are depleted, return a regular entity id */ - id = ecs_new_id(unsafe_world); + + elems = ecs_vector_first(empty, ecs_query_table_t); + count = ecs_vector_count(empty); + for (i = 0; i < count; i ++) { + match_table(world, subquery, elems[i].hdr.table); } +} - return id; +static +void notify_subqueries( + ecs_world_t *world, + ecs_query_t *query, + ecs_query_event_t *event) +{ + if (query->subqueries) { + ecs_query_t **queries = ecs_vector_first(query->subqueries, ecs_query_t*); + int32_t i, count = ecs_vector_count(query->subqueries); + + ecs_query_event_t sub_event = *event; + sub_event.parent_query = query; + + for (i = 0; i < count; i ++) { + ecs_query_t *sub = queries[i]; + flecs_query_notify(world, sub, &sub_event); + } + } } -ecs_entity_t ecs_new_w_id( +static +void resolve_cascade_subject_for_table( ecs_world_t *world, - ecs_id_t id) + ecs_query_t *query, + const ecs_table_t *table, + ecs_type_t table_type, + ecs_query_table_match_t *table_data) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t term_index = query->cascade_by - 1; + ecs_term_t *term = &query->filter.terms[term_index]; - ecs_stage_t *stage = flecs_stage_from_world(&world); - ecs_entity_t entity = ecs_new_id(world); + ecs_assert(table_data->references != 0, + ECS_INTERNAL_ERROR, NULL); - ecs_id_t ids[3]; - ecs_ids_t to_add = { .array = ids, .count = 0 }; + /* Obtain reference index */ + int32_t *column_indices = table_data->columns; + int32_t ref_index = -column_indices[term_index] - 1; - if (id) { - ids[to_add.count ++] = id; + /* Obtain pointer to the reference data */ + ecs_ref_t *references = table_data->references; + + /* Find source for component */ + ecs_entity_t subject; + ecs_type_match(world, table, table_type, 0, term->id, + term->subj.set.relation, 1, 0, &subject, NULL); + + /* If container was found, update the reference */ + if (subject) { + ecs_ref_t *ref = &references[ref_index]; + ecs_assert(ref->component == term->id, ECS_INTERNAL_ERROR, NULL); + + references[ref_index].entity = ecs_get_alive(world, subject); + table_data->subjects[term_index] = subject; + ecs_get_ref_w_id(world, ref, subject, term->id); + } else { + references[ref_index].entity = 0; + table_data->subjects[term_index] = 0; } - ecs_id_t with = stage->with; - if (with) { - ids[to_add.count ++] = with; + if (ecs_table_count(table)) { + /* The subject (or depth of the subject) may have changed, so reinsert + * the node to make sure it's in the right group */ + remove_table_node(query, &table_data->node); + insert_table_node(query, &table_data->node); } +} - ecs_entity_t scope = stage->scope; - if (scope) { - if (!id || !ECS_HAS_RELATION(id, EcsChildOf)) { - ids[to_add.count ++] = ecs_pair(EcsChildOf, scope); - } +static +void resolve_cascade_subject( + ecs_world_t *world, + ecs_query_t *query, + ecs_query_table_t *elem, + const ecs_table_t *table, + ecs_type_t table_type) +{ + ecs_query_table_match_t *cur; + for (cur = elem->first; cur != NULL; cur = cur->next_match) { + resolve_cascade_subject_for_table( + world, query, table, table_type, cur); } +} - if (to_add.count) { - if (flecs_defer_new(world, stage, entity, to_add.array[0])) { - int i; - for (i = 1; i < to_add.count; i ++) { - flecs_defer_add(world, stage, entity, to_add.array[i]); - } - return entity; - } +/* Remove table */ +static +void remove_table( + ecs_poly_t *poly, + void *ptr) +{ + ecs_poly_assert(poly, ecs_query_t); - new(world, entity, &to_add); - } else { - if (flecs_defer_new(world, stage, entity, 0)) { - return entity; + ecs_query_t *query = poly; + ecs_query_table_t *elem = ptr; + ecs_query_table_match_t *cur, *next; + + for (cur = elem->first; cur != NULL; cur = next) { + ecs_os_free(cur->columns); + ecs_os_free(cur->ids); + ecs_os_free(cur->subjects); + ecs_os_free(cur->sizes); + ecs_os_free(cur->references); + ecs_os_free(cur->sparse_columns); + ecs_os_free(cur->bitset_columns); + + if (!elem->hdr.empty) { + remove_table_node(query, &cur->node); } - ecs_eis_set(world, entity, &(ecs_record_t){ 0 }); + next = cur->next_match; + + ecs_os_free(cur); } - flecs_defer_flush(world, stage); + ecs_os_free(elem->monitor); - return entity; + elem->first = NULL; } -#ifdef FLECS_PARSER +static +void unmatch_table( + ecs_query_t *query, + ecs_table_t *table) +{ + ecs_table_cache_remove(&query->cache, ecs_query_table_t, table); +} -/* Traverse table graph by either adding or removing identifiers parsed from the - * passed in expression. */ static -ecs_table_t *traverse_from_expr( +void rematch_table( ecs_world_t *world, - ecs_table_t *table, - const char *name, - const char *expr, - ecs_table_diff_t *diff, - bool replace_and, - bool *error) + ecs_query_t *query, + ecs_table_t *table) { - const char *ptr = expr; - if (ptr) { - ecs_term_t term = {0}; - while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))){ - if (!ecs_term_is_initialized(&term)) { - break; - } + ecs_query_table_t *match = ecs_table_cache_get( + &query->cache, ecs_query_table_t, table); - if (ecs_term_finalize(world, name, &term)) { - if (error) { - *error = true; - } - return NULL; - } + if (flecs_query_match(world, table, query)) { + /* If the table matches, and it is not currently matched, add */ + if (match == NULL) { + add_table(world, query, table); - if (!ecs_term_is_trivial(&term)) { - if (error) { - *error = true; - } - ecs_parser_error(name, expr, (ptr - expr), - "invalid non-trivial term in add expression"); - return NULL; - } + /* If table still matches and has cascade column, reevaluate the + * sources of references. This may have changed in case + * components were added/removed to container entities */ + } else if (query->cascade_by) { + resolve_cascade_subject(world, query, match, table, table->type); - if (term.oper == EcsAnd || !replace_and) { - /* Regular AND expression */ - table = table_append(world, table, term.id, diff); - } else if (term.oper == EcsAndFrom) { - /* Add all components from the specified type */ - const EcsType *t = ecs_get(world, term.id, EcsType); - if (!t) { - if (error) { - *error = true; - } - ecs_parser_error(name, expr, (ptr - expr), - "expected type for AND role"); - return NULL; - } - - ecs_id_t *ids = ecs_vector_first(t->normalized, ecs_id_t); - int32_t i, count = ecs_vector_count(t->normalized); - for (i = 0; i < count; i ++) { - table = table_append(world, table, ids[i], diff); - } + /* If query has optional columns, it is possible that a column that + * previously had data no longer has data, or vice versa. Do a full + * rematch to make sure data is consistent. */ + } else if (query->flags & EcsQueryHasOptional) { + unmatch_table(query, table); + if (!(query->flags & EcsQueryIsSubquery)) { + flecs_table_notify(world, table, &(ecs_table_event_t){ + .kind = EcsTableQueryUnmatch, + .query = query + }); } - - ecs_term_fini(&term); + add_table(world, query, table); } - - if (!ptr) { - if (error) { - *error = true; + } else { + /* Table no longer matches, remove */ + if (match != NULL) { + unmatch_table(query, table); + if (!(query->flags & EcsQueryIsSubquery)) { + flecs_table_notify(world, table, &(ecs_table_event_t){ + .kind = EcsTableQueryUnmatch, + .query = query + }); } - return NULL; + notify_subqueries(world, query, &(ecs_query_event_t){ + .kind = EcsQueryTableUnmatch, + .table = table + }); } } - - return table; } -/* Add/remove components based on the parsed expression. This operation is - * slower than traverse_from_expr, but safe to use from a deferred context. */ static -void defer_from_expr( +bool satisfy_constraints( ecs_world_t *world, - ecs_entity_t entity, - const char *name, - const char *expr, - bool is_add, - bool replace_and) + const ecs_filter_t *filter) { - const char *ptr = expr; - if (ptr) { - ecs_term_t term = {0}; - while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))) { - if (!ecs_term_is_initialized(&term)) { - break; - } + ecs_term_t *terms = filter->terms; + int32_t i, count = filter->term_count; - if (ecs_term_finalize(world, name, &term)) { - return; - } + for (i = 0; i < count; i ++) { + ecs_term_t *term = &terms[i]; + ecs_term_id_t *subj = &term->subj; + ecs_oper_kind_t oper = term->oper; - if (!ecs_term_is_trivial(&term)) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid non-trivial term in add expression"); - return; - } + if (oper == EcsOptional) { + continue; + } - if (term.oper == EcsAnd || !replace_and) { - /* Regular AND expression */ - if (is_add) { - ecs_add_id(world, entity, term.id); - } else { - ecs_remove_id(world, entity, term.id); - } - } else if (term.oper == EcsAndFrom) { - /* Add all components from the specified type */ - const EcsType *t = ecs_get(world, term.id, EcsType); - if (!t) { - ecs_parser_error(name, expr, (ptr - expr), - "expected type for AND role"); - return; + if (subj->entity != EcsThis && subj->set.mask & EcsSelf) { + ecs_type_t type = ecs_get_type(world, subj->entity); + + if (ecs_type_has_id(world, type, term->id, false)) { + if (oper == EcsNot) { + return false; } - - ecs_id_t *ids = ecs_vector_first(t->normalized, ecs_id_t); - int32_t i, count = ecs_vector_count(t->normalized); - for (i = 0; i < count; i ++) { - if (is_add) { - ecs_add_id(world, entity, ids[i]); - } else { - ecs_remove_id(world, entity, ids[i]); - } + } else { + if (oper != EcsNot) { + return false; } } - - ecs_term_fini(&term); } } + + return true; } -#endif -/* If operation is not deferred, add components by finding the target - * table and moving the entity towards it. */ -static -int traverse_add( +/* Rematch system with tables after a change happened to a watched entity */ +static +void rematch_tables( ecs_world_t *world, - ecs_entity_t result, - const char *name, - const ecs_entity_desc_t *desc, - ecs_entity_t scope, - ecs_id_t with, - bool new_entity, - bool name_assigned) + ecs_query_t *query, + ecs_query_t *parent_query) { - const char *sep = desc->sep; - - /* Find existing table */ - ecs_entity_info_t info = {0}; - ecs_table_t *src_table = NULL, *table = NULL; - if (!new_entity) { - if (flecs_get_info(world, result, &info)) { - table = info.table; + if (parent_query) { + ecs_query_table_t *tables = ecs_vector_first( + parent_query->cache.tables, ecs_query_table_t); + int32_t i, count = ecs_vector_count(parent_query->cache.tables); + for (i = 0; i < count; i ++) { + rematch_table(world, query, tables[i].hdr.table); } - } - - /* Find destination table */ - ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; + tables = ecs_vector_first( + parent_query->cache.empty_tables, ecs_query_table_t); + count = ecs_vector_count(parent_query->cache.empty_tables); + for (i = 0; i < count; i ++) { + rematch_table(world, query, tables[i].hdr.table); + } + } else { + ecs_sparse_t *tables = world->store.tables; + int32_t i, count = flecs_sparse_count(tables); - /* If this is a new entity without a name, add the scope. If a name is - * provided, the scope will be added by the add_path_w_sep function */ - if (new_entity) { - if (new_entity && scope && !name && !name_assigned) { - table = table_append( - world, table, ecs_pair(EcsChildOf, scope), &diff); - } - if (with) { - table = table_append(world, table, with, &diff); + for (i = 0; i < count; i ++) { + /* Is the system currently matched with the table? */ + ecs_table_t *table = flecs_sparse_get_dense(tables, ecs_table_t, i); + rematch_table(world, query, table); } } - /* If a name is provided but not yet assigned, add the Name component */ - if (name && !name_assigned) { - table = table_append(world, table, - ecs_pair(ecs_id(EcsIdentifier), EcsName), &diff); - } + /* Enable/disable system if constraints are (not) met. If the system is + * already dis/enabled this operation has no side effects. */ + query->constraints_satisfied = satisfy_constraints(world, &query->filter); +} - /* Add components from the 'add' id array */ - int32_t i = 0; - ecs_id_t id; - const ecs_id_t *ids = desc->add; - while ((i < ECS_MAX_ADD_REMOVE) && (id = ids[i ++])) { - table = table_append(world, table, id, &diff); - } +static +void remove_subquery( + ecs_query_t *parent, + ecs_query_t *sub) +{ + ecs_assert(parent != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(sub != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(parent->subqueries != NULL, ECS_INTERNAL_ERROR, NULL); - /* Add components from the 'add_expr' expression */ - if (desc->add_expr) { -#ifdef FLECS_PARSER - bool error = false; - table = traverse_from_expr( - world, table, name, desc->add_expr, &diff, true, &error); - if (error) { - return -1; + int32_t i, count = ecs_vector_count(parent->subqueries); + ecs_query_t **sq = ecs_vector_first(parent->subqueries, ecs_query_t*); + + for (i = 0; i < count; i ++) { + if (sq[i] == sub) { + break; } -#else - ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); -#endif } - /* Commit entity to destination table */ - if (src_table != table) { - commit(world, result, &info, table, &diff, true); - } + ecs_vector_remove(parent->subqueries, ecs_query_t*, i); +} - /* Set name */ - if (name && !name_assigned) { - ecs_add_path_w_sep(world, result, scope, name, sep, NULL); - ecs_assert(ecs_get_name(world, result) != NULL, - ECS_INTERNAL_ERROR, NULL); - } +/* -- Private API -- */ - if (desc->symbol) { - const char *sym = ecs_get_symbol(world, result); - if (sym) { - ecs_assert(!ecs_os_strcmp(desc->symbol, sym), - ECS_INCONSISTENT_NAME, desc->symbol); - } else { - ecs_set_symbol(world, result, desc->symbol); +void flecs_query_notify( + ecs_world_t *world, + ecs_query_t *query, + ecs_query_event_t *event) +{ + bool notify = true; + + switch(event->kind) { + case EcsQueryTableMatch: + /* Creation of new table */ + if (match_table(world, query, event->table)) { + if (query->subqueries) { + notify_subqueries(world, query, event); + } } + notify = false; + break; + case EcsQueryTableUnmatch: + /* Deletion of table */ + unmatch_table(query, event->table); + break; + case EcsQueryTableRematch: + /* Rematch tables of query */ + rematch_tables(world, query, event->parent_query); + break; + case EcsQueryTableEmpty: + /* Table is empty, deactivate */ + update_table(query, event->table, true); + break; + case EcsQueryTableNonEmpty: + /* Table is non-empty, activate */ + update_table(query, event->table, false); + break; + case EcsQueryOrphan: + ecs_assert(query->flags & EcsQueryIsSubquery, ECS_INTERNAL_ERROR, NULL); + query->flags |= EcsQueryIsOrphaned; + query->parent = NULL; + break; } - diff_free(&diff); - - return 0; + if (notify) { + notify_subqueries(world, query, event); + } } -/* When in deferred mode, we need to add/remove components one by one using - * the regular operations. */ -static -void deferred_add_remove( +static +void query_order_by( ecs_world_t *world, - ecs_entity_t entity, - const char *name, - const ecs_entity_desc_t *desc, - ecs_entity_t scope, - ecs_id_t with, - bool new_entity, - bool name_assigned) + ecs_query_t *query, + ecs_entity_t order_by_component, + ecs_order_by_action_t order_by) { - const char *sep = desc->sep; + ecs_assert(query != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!(query->flags & EcsQueryIsOrphaned), ECS_INVALID_PARAMETER, NULL); + ecs_assert(query->flags & EcsQueryNeedsTables, ECS_INVALID_PARAMETER, NULL); - /* If this is a new entity without a name, add the scope. If a name is - * provided, the scope will be added by the add_path_w_sep function */ - if (new_entity) { - if (new_entity && scope && !name && !name_assigned) { - ecs_add_id(world, entity, ecs_pair(EcsChildOf, scope)); - } + query->order_by_component = order_by_component; + query->order_by = order_by; - if (with) { - ecs_add_id(world, entity, with); - } - } + ecs_vector_free(query->table_slices); + query->table_slices = NULL; - /* Add components from the 'add' id array */ - int32_t i = 0; - ecs_id_t id; - const ecs_id_t *ids = desc->add; - while ((i < ECS_MAX_ADD_REMOVE) && (id = ids[i ++])) { - ecs_add_id(world, entity, id); - } + sort_tables(world, query); - /* Add components from the 'add_expr' expression */ - if (desc->add_expr) { -#ifdef FLECS_PARSER - defer_from_expr(world, entity, name, desc->add_expr, true, true); -#else - ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); -#endif + if (!query->table_slices) { + build_sorted_tables(query); } +} - /* Set name */ - if (name && !name_assigned) { - ecs_add_path_w_sep(world, entity, scope, name, sep, NULL); - } +static +void query_group_by( + ecs_query_t *query, + ecs_entity_t sort_component, + ecs_group_by_action_t group_by) +{ + /* Cannot change grouping once a query has been created */ + ecs_assert(query->group_by_id == 0, ECS_INVALID_OPERATION, NULL); + ecs_assert(query->group_by == 0, ECS_INVALID_OPERATION, NULL); - /* Currently it's not supported to set the symbol from a deferred context */ - if (desc->symbol) { - const char *sym = ecs_get_symbol(world, entity); - ecs_assert(!ecs_os_strcmp(sym, desc->symbol), ECS_UNSUPPORTED, NULL); - (void)sym; - } + query->group_by_id = sort_component; + query->group_by = group_by; + query->groups = ecs_map_new(ecs_query_table_list_t, 16); } -ecs_entity_t ecs_entity_init( + +/* -- Public API -- */ + +ecs_query_t* ecs_query_init( ecs_world_t *world, - const ecs_entity_desc_t *desc) + const ecs_query_desc_t *desc) { ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(desc != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); - ecs_stage_t *stage = flecs_stage_from_world(&world); - ecs_entity_t scope = ecs_get_scope(world); - ecs_id_t with = ecs_get_with(world); + ecs_query_t *result = flecs_sparse_add(world->queries, ecs_query_t); + ecs_poly_init(result, ecs_query_t); - const char *name = desc->name; - const char *sep = desc->sep; - if (!sep) { - sep = "."; + result->id = flecs_sparse_last_id(world->queries); + + if (ecs_filter_init(world, &result->filter, &desc->filter)) { + flecs_sparse_remove(world->queries, result->id); + return NULL; } - const char *root_sep = desc->root_sep; + result->world = world; + result->system = desc->system; + result->prev_match_count = -1; - bool new_entity = false; - bool name_assigned = false; + ecs_table_cache_init( + &result->cache, ecs_query_table_t, result, remove_table); - /* Remove optional prefix from name. Entity names can be derived from - * language identifiers, such as components (typenames) and systems - * function names). Because C does not have namespaces, such identifiers - * often encode the namespace as a prefix. - * To ensure interoperability between C and C++ (and potentially other - * languages with namespacing) the entity must be stored without this prefix - * and with the proper namespace, which is what the name_prefix is for */ - const char *prefix = world->name_prefix; - if (name && prefix) { - ecs_size_t len = ecs_os_strlen(prefix); - if (!ecs_os_strncmp(name, prefix, len) && - (isupper(name[len]) || name[len] == '_')) - { - if (name[len] == '_') { - name = name + len + 1; - } else { - name = name + len; - } - } + process_signature(world, result); + + /* Group before matching so we won't have to move tables around later */ + int32_t cascade_by = result->cascade_by; + if (cascade_by) { + query_group_by(result, result->filter.terms[cascade_by - 1].id, + group_by_cascade); + result->group_by_ctx = &result->filter.terms[cascade_by - 1]; } - /* Find or create entity */ - ecs_entity_t result = desc->entity; - if (!result) { - if (name) { - result = ecs_lookup_path_w_sep( - world, scope, name, sep, root_sep, false); - if (result) { - name_assigned = true; - } - } + if (desc->group_by) { + /* Can't have a cascade term and group by at the same time, as cascade + * uses the group_by mechanism */ + ecs_assert(!result->cascade_by, ECS_INVALID_PARAMETER, NULL); + query_group_by(result, desc->group_by_id, desc->group_by); + result->group_by_ctx = desc->group_by_ctx; + result->group_by_ctx_free = desc->group_by_ctx_free; + } - if (!result) { - if (desc->use_low_id) { - result = ecs_new_low_id(world); - } else { - result = ecs_new_id(world); - } - new_entity = true; - ecs_assert(ecs_get_type(world, result) == NULL, - ECS_INTERNAL_ERROR, NULL); - } - } else { - ecs_assert(ecs_is_valid(world, result), ECS_INVALID_PARAMETER, NULL); + if (desc->parent != NULL) { + result->flags |= EcsQueryIsSubquery; + } - name_assigned = ecs_has_pair( - world, result, ecs_id(EcsIdentifier), EcsName); - if (name && name_assigned) { - /* If entity has name, verify that name matches */ - char *path = ecs_get_path_w_sep(world, scope, result, sep, NULL); - if (path) { - if (ecs_os_strcmp(path, name)) { - /* Mismatching name */ - ecs_os_free(path); - return 0; - } - ecs_os_free(path); + /* If a system is specified, ensure that if there are any subjects in the + * filter that refer to the system, the component is added */ + if (desc->system) { + int32_t t, term_count = result->filter.term_count; + ecs_term_t *terms = result->filter.terms; + + for (t = 0; t < term_count; t ++) { + ecs_term_t *term = &terms[t]; + if (term->subj.entity == desc->system) { + ecs_add_id(world, desc->system, term->id); } - } + } } - ecs_assert(name_assigned == ecs_has_pair( - world, result, ecs_id(EcsIdentifier), EcsName), - ECS_INTERNAL_ERROR, NULL); + ecs_dbg_1("query #[green]%s#[reset] created with expression #[red]%s", + query_name(world, result), result->filter.expr); - if (stage->defer) { - deferred_add_remove((ecs_world_t*)stage, result, name, desc, - scope, with, new_entity, name_assigned); - } else { - if (traverse_add(world, result, name, desc, - scope, with, new_entity, name_assigned)) - { - return 0; + ecs_log_push(); + + if (!desc->parent) { + if (result->flags & EcsQueryNeedsTables) { + match_tables(world, result); + } else { + /* Add stub table that resolves references (if any) so everything is + * preprocessed when the query is evaluated. */ + add_table(world, result, NULL); } + } else { + add_subquery(world, desc->parent, result); + result->parent = desc->parent; + } + + if (desc->order_by) { + query_order_by( + world, result, desc->order_by_component, desc->order_by); } + result->constraints_satisfied = satisfy_constraints(world, &result->filter); + + ecs_log_pop(); + return result; } -const ecs_entity_t* ecs_bulk_init( - ecs_world_t *world, - const ecs_bulk_desc_t *desc) +void ecs_query_fini( + ecs_query_t *query) { - const ecs_entity_t *entities = desc->entities; - int32_t count = desc->count; + ecs_poly_assert(query, ecs_query_t); + ecs_world_t *world = query->world; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - int32_t sparse_count = 0; - if (!entities) { - sparse_count = ecs_eis_count(world); - entities = flecs_sparse_new_ids(world->store.entity_index, count); - ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); - } else { - int i; - for (i = 0; i < count; i ++) { - ecs_ensure(world, entities[i]); + if (query->group_by_ctx_free) { + if (query->group_by_ctx) { + query->group_by_ctx_free(query->group_by_ctx); } } - ecs_ids_t ids; + if ((query->flags & EcsQueryIsSubquery) && + !(query->flags & EcsQueryIsOrphaned)) + { + remove_subquery(query->parent, query); + } - ecs_table_t *table = desc->table; - ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; - if (!table) { - int32_t i = 0; - ecs_id_t id; - while ((id = desc->ids[i])) { - table = table_append(world, table, id, &diff); - i ++; - } + notify_subqueries(world, query, &(ecs_query_event_t){ + .kind = EcsQueryOrphan + }); - ids.array = (ecs_id_t*)desc->ids; - ids.count = i; - } else { - diff.added.array = ecs_vector_first(table->type, ecs_id_t); - diff.added.count = ecs_vector_count(table->type); + ecs_vector_each(query->cache.empty_tables, ecs_query_table_t, table, { + if (!(query->flags & EcsQueryIsSubquery)) { + flecs_table_notify(world, table->hdr.table, &(ecs_table_event_t){ + .kind = EcsTableQueryUnmatch, + .query = query + }); + } + }); - ids = diff.added; - } + ecs_vector_each(query->cache.tables, ecs_query_table_t, table, { + if (!(query->flags & EcsQueryIsSubquery)) { + flecs_table_notify(world, table->hdr.table, &(ecs_table_event_t){ + .kind = EcsTableQueryUnmatch, + .query = query + }); + } + }); - new_w_data( - world, table, entities, &ids, count, desc->data, true, NULL, &diff); + ecs_table_cache_fini(&query->cache); + ecs_map_free(query->groups); - if (!sparse_count) { - return entities; - } else { - /* Refetch entity ids, in case the underlying array was reallocated */ - entities = flecs_sparse_ids(world->store.entity_index); - return &entities[sparse_count]; - } + ecs_vector_free(query->subqueries); + ecs_vector_free(query->table_slices); + ecs_filter_fini(&query->filter); + + ecs_poly_fini(query, ecs_query_t); + + /* Remove query from storage */ + flecs_sparse_remove(world->queries, query->id); } -ecs_entity_t ecs_component_init( - ecs_world_t *world, - const ecs_component_desc_t *desc) +const ecs_filter_t* ecs_query_get_filter( + ecs_query_t *query) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - world = (ecs_world_t*)ecs_get_world(world); - - bool is_readonly = world->is_readonly; - bool is_deferred = ecs_is_deferred(world); - int32_t defer_count = 0; - ecs_vector_t *defer_queue = NULL; - ecs_stage_t *stage = NULL; - - /* If world is readonly or deferring is enabled, component registration can - * still happen directly on the main storage, but only if the application - * is singlethreaded. */ - if (is_readonly || is_deferred) { - ecs_assert(ecs_get_stage_count(world) <= 1, - ECS_INVALID_WHILE_ITERATING, NULL); + ecs_poly_assert(query, ecs_query_t); + return &query->filter; +} - /* Silence readonly warnings */ - world->is_readonly = false; +/* Create query iterator */ +ecs_iter_t ecs_query_iter_page( + const ecs_world_t *stage, + ecs_query_t *query, + int32_t offset, + int32_t limit) +{ + ecs_poly_assert(query, ecs_query_t); + ecs_assert(!(query->flags & EcsQueryIsOrphaned), ECS_INVALID_PARAMETER, NULL); - /* Hack around safety checks (this ought to look ugly) */ - ecs_world_t *temp_world = world; - stage = flecs_stage_from_world(&temp_world); - defer_count = stage->defer; - defer_queue = stage->defer_queue; - stage->defer = 0; - stage->defer_queue = NULL; - } + ecs_world_t *world = (ecs_world_t*)ecs_get_world(stage); - ecs_entity_desc_t entity_desc = desc->entity; - entity_desc.use_low_id = true; - if (!entity_desc.symbol) { - entity_desc.symbol = entity_desc.name; - } + sort_tables(world, query); - ecs_entity_t e = desc->entity.entity; - ecs_entity_t result = ecs_entity_init(world, &entity_desc); - if (!result) { - return 0; + if (!world->is_readonly && query->flags & EcsQueryHasRefs) { + flecs_eval_component_monitors(world); } - bool added = false; - EcsComponent *ptr = ecs_get_mut(world, result, EcsComponent, &added); + tables_reset_dirty(query); - if (added) { - ptr->size = flecs_from_size_t(desc->size); - ptr->alignment = flecs_from_size_t(desc->alignment); - if (!ptr->size) { - ecs_trace("#[green]tag#[reset] %s registered", - ecs_get_name(world, result)); - } else { - ecs_trace("#[green]component#[reset] %s registered", - ecs_get_name(world, result)); - } + int32_t table_count; + if (query->table_slices) { + table_count = ecs_vector_count(query->table_slices); } else { - if (ptr->size != flecs_from_size_t(desc->size)) { - ecs_abort(ECS_INVALID_COMPONENT_SIZE, desc->entity.name); - } - if (ptr->alignment != flecs_from_size_t(desc->alignment)) { - ecs_abort(ECS_INVALID_COMPONENT_ALIGNMENT, desc->entity.name); - } - } - - ecs_modified(world, result, EcsComponent); - - if (e > world->stats.last_component_id && e < ECS_HI_COMPONENT_ID) { - world->stats.last_component_id = e + 1; + table_count = ecs_vector_count(query->cache.tables); } - /* Ensure components cannot be deleted */ - ecs_add_pair(world, result, EcsOnDelete, EcsThrow); + ecs_query_iter_t it = { + .query = query, + .node = query->list.first, + .page_iter = { + .offset = offset, + .limit = limit, + .remaining = limit + } + }; - if (is_readonly || is_deferred) { - /* Restore readonly state / defer count */ - world->is_readonly = is_readonly; - stage->defer = defer_count; - stage->defer_queue = defer_queue; + if (query->order_by && query->list.count) { + it.node = ecs_vector_first(query->table_slices, ecs_query_table_node_t); } - ecs_assert(result != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ecs_has(world, result, EcsComponent), ECS_INTERNAL_ERROR, NULL); + return (ecs_iter_t){ + .real_world = world, + .world = (ecs_world_t*)stage, + .terms = query->filter.terms, + .term_count = query->filter.term_count_actual, + .table_count = table_count, + .iter.query = it, + .next = ecs_query_next + }; +} - return result; +ecs_iter_t ecs_query_iter( + const ecs_world_t *world, + ecs_query_t *query) +{ + ecs_poly_assert(query, ecs_query_t); + return ecs_query_iter_page(world, query, 0, 0); } -ecs_entity_t ecs_type_init( - ecs_world_t *world, - const ecs_type_desc_t *desc) +static +int ecs_page_iter_next( + ecs_page_iter_t *it, + ecs_page_cursor_t *cur) { - ecs_entity_t result = ecs_entity_init(world, &desc->entity); - if (!result) { - return 0; + int32_t offset = it->offset; + int32_t limit = it->limit; + if (!(offset || limit)) { + return cur->count == 0; } - ecs_table_t *table = NULL, *normalized = NULL; - ecs_table_diff_t temp_diff, diff = ECS_TABLE_DIFF_INIT; + int32_t count = cur->count; + int32_t remaining = it->remaining; - /* Find destination table (and type) */ + if (offset) { + if (offset > count) { + /* No entities to iterate in current table */ + it->offset -= count; + return 1; + } else { + cur->first += offset; + count = cur->count -= offset; + it->offset = 0; + } + } - /* Add components from the 'add' id array */ - int32_t i = 0; - ecs_id_t id; - const ecs_id_t *ids = desc->ids; - while ((i < ECS_MAX_ADD_REMOVE) && (id = ids[i ++])) { - normalized = flecs_table_traverse_add( - world, normalized, &id, &temp_diff); - table = flecs_table_traverse_add(world, table, &id, NULL); - ecs_assert(table != NULL, ECS_INVALID_PARAMETER, NULL); - diff_append(&diff, &temp_diff); + if (remaining) { + if (remaining > count) { + it->remaining -= count; + } else { + count = cur->count = remaining; + it->remaining = 0; + } + } else if (limit) { + /* Limit hit: no more entities left to iterate */ + return -1; } - /* If expression is set, add it to the table */ - if (desc->ids_expr) { -#ifdef FLECS_PARSER - bool error = false; + return count == 0; +} - normalized = traverse_from_expr(world, normalized, desc->entity.name, - desc->ids_expr, &diff, true, &error); - if (error) { - return 0; - } +static +int find_smallest_column( + ecs_table_t *table, + ecs_query_table_match_t *table_data, + ecs_vector_t *sparse_columns) +{ + flecs_sparse_column_t *sparse_column_array = + ecs_vector_first(sparse_columns, flecs_sparse_column_t); + int32_t i, count = ecs_vector_count(sparse_columns); + int32_t min = INT_MAX, index = 0; - table = traverse_from_expr(world, table, desc->entity.name, - desc->ids_expr, &diff, false, &error); - if (error) { - return 0; - } -#else - ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); -#endif - } + for (i = 0; i < count; i ++) { + /* The array with sparse queries for the matched table */ + flecs_sparse_column_t *sparse_column = &sparse_column_array[i]; - diff_free(&diff); + /* Pointer to the switch column struct of the table */ + ecs_sw_column_t *sc = sparse_column->sw_column; - ecs_type_t type = NULL; - ecs_type_t normalized_type = NULL; - - if (table) { - type = table->type; - } - if (normalized) { - normalized_type = normalized->type; - } + /* If the sparse column pointer hadn't been retrieved yet, do it now */ + if (!sc) { + /* Get the table column index from the signature column index */ + int32_t table_column_index = table_data->columns[ + sparse_column->signature_column_index]; - bool add = false; - EcsType *type_ptr = ecs_get_mut(world, result, EcsType, &add); - if (add) { - type_ptr->type = type; - type_ptr->normalized = normalized_type; + /* Translate the table column index to switch column index */ + table_column_index -= table->sw_column_offset; + ecs_assert(table_column_index >= 1, ECS_INTERNAL_ERROR, NULL); - /* This will allow the type to show up in debug tools */ - if (type) { - ecs_map_set(world->type_handles, (uintptr_t)type, &result); + /* Get the sparse column */ + ecs_data_t *data = &table->storage; + sc = sparse_column->sw_column = + &data->sw_columns[table_column_index - 1]; } - ecs_modified(world, result, EcsType); - } else { - if (type_ptr->type != type) { - ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); + /* Find the smallest column */ + ecs_switch_t *sw = sc->data; + int32_t case_count = flecs_switch_case_count(sw, sparse_column->sw_case); + if (case_count < min) { + min = case_count; + index = i + 1; } - if (type_ptr->normalized != normalized_type) { - ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); - } } - return result; + return index; } -const ecs_entity_t* ecs_bulk_new_w_id( - ecs_world_t *world, - ecs_id_t id, - int32_t count) +static +int sparse_column_next( + ecs_table_t *table, + ecs_query_table_match_t *matched_table, + ecs_vector_t *sparse_columns, + ecs_query_iter_t *iter, + ecs_page_cursor_t *cur) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_stage_t *stage = flecs_stage_from_world(&world); + bool first_iteration = false; + int32_t sparse_smallest; - const ecs_entity_t *ids; - if (flecs_defer_bulk_new(world, stage, count, id, &ids)) { - return ids; + if (!(sparse_smallest = iter->sparse_smallest)) { + sparse_smallest = iter->sparse_smallest = find_smallest_column( + table, matched_table, sparse_columns); + first_iteration = true; } - ecs_table_t *table = &world->store.root; - ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; - - if (id) { - table = table_append(world, table, id, &diff); - } + sparse_smallest -= 1; - ids = new_w_data(world, table, NULL, NULL, count, NULL, false, NULL, &diff); - flecs_defer_flush(world, stage); + flecs_sparse_column_t *columns = ecs_vector_first( + sparse_columns, flecs_sparse_column_t); + flecs_sparse_column_t *column = &columns[sparse_smallest]; + ecs_switch_t *sw, *sw_smallest = column->sw_column->data; + ecs_entity_t case_smallest = column->sw_case; - return ids; -} + /* Find next entity to iterate in sparse column */ + int32_t first; + if (first_iteration) { + first = flecs_switch_first(sw_smallest, case_smallest); + } else { + first = flecs_switch_next(sw_smallest, iter->sparse_first); + } -void ecs_clear( - ecs_world_t *world, - ecs_entity_t entity) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + if (first == -1) { + goto done; + } - ecs_stage_t *stage = flecs_stage_from_world(&world); - if (flecs_defer_clear(world, stage, entity)) { - return; - } + /* Check if entity matches with other sparse columns, if any */ + int32_t i, count = ecs_vector_count(sparse_columns); + do { + for (i = 0; i < count; i ++) { + if (i == sparse_smallest) { + /* Already validated this one */ + continue; + } - ecs_entity_info_t info; - info.table = NULL; + column = &columns[i]; + sw = column->sw_column->data; - flecs_get_info(world, entity, &info); + if (flecs_switch_get(sw, first) != column->sw_case) { + first = flecs_switch_next(sw_smallest, first); + if (first == -1) { + goto done; + } + } + } + } while (i != count); - ecs_table_t *table = info.table; - if (table) { - ecs_table_diff_t diff = { - .removed = flecs_type_to_ids(table->type), - .un_set = flecs_type_to_ids(table->storage_type) - }; + cur->first = iter->sparse_first = first; + cur->count = 1; - delete_entity(world, table, &table->storage, info.row, &diff); - info.record->table = NULL; - info.record->row = 0; - } + return 0; +done: + /* Iterated all elements in the sparse list, we should move to the + * next matched table. */ + iter->sparse_smallest = 0; + iter->sparse_first = 0; - flecs_defer_flush(world, stage); + return -1; } -static -void on_delete_any_w_entity( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_t action); +#define BS_MAX ((uint64_t)0xFFFFFFFFFFFFFFFF) static -void throw_invalid_delete( - ecs_world_t *world, - ecs_id_t id) +int bitset_column_next( + ecs_table_t *table, + ecs_vector_t *bitset_columns, + ecs_query_iter_t *iter, + ecs_page_cursor_t *cur) { - ecs_abort(ECS_INVALID_DELETE, ecs_id_str(world, id)); -} + /* Precomputed single-bit test */ + static const uint64_t bitmask[64] = { + (uint64_t)1 << 0, (uint64_t)1 << 1, (uint64_t)1 << 2, (uint64_t)1 << 3, + (uint64_t)1 << 4, (uint64_t)1 << 5, (uint64_t)1 << 6, (uint64_t)1 << 7, + (uint64_t)1 << 8, (uint64_t)1 << 9, (uint64_t)1 << 10, (uint64_t)1 << 11, + (uint64_t)1 << 12, (uint64_t)1 << 13, (uint64_t)1 << 14, (uint64_t)1 << 15, + (uint64_t)1 << 16, (uint64_t)1 << 17, (uint64_t)1 << 18, (uint64_t)1 << 19, + (uint64_t)1 << 20, (uint64_t)1 << 21, (uint64_t)1 << 22, (uint64_t)1 << 23, + (uint64_t)1 << 24, (uint64_t)1 << 25, (uint64_t)1 << 26, (uint64_t)1 << 27, + (uint64_t)1 << 28, (uint64_t)1 << 29, (uint64_t)1 << 30, (uint64_t)1 << 31, + (uint64_t)1 << 32, (uint64_t)1 << 33, (uint64_t)1 << 34, (uint64_t)1 << 35, + (uint64_t)1 << 36, (uint64_t)1 << 37, (uint64_t)1 << 38, (uint64_t)1 << 39, + (uint64_t)1 << 40, (uint64_t)1 << 41, (uint64_t)1 << 42, (uint64_t)1 << 43, + (uint64_t)1 << 44, (uint64_t)1 << 45, (uint64_t)1 << 46, (uint64_t)1 << 47, + (uint64_t)1 << 48, (uint64_t)1 << 49, (uint64_t)1 << 50, (uint64_t)1 << 51, + (uint64_t)1 << 52, (uint64_t)1 << 53, (uint64_t)1 << 54, (uint64_t)1 << 55, + (uint64_t)1 << 56, (uint64_t)1 << 57, (uint64_t)1 << 58, (uint64_t)1 << 59, + (uint64_t)1 << 60, (uint64_t)1 << 61, (uint64_t)1 << 62, (uint64_t)1 << 63 + }; -static -void remove_from_table( - ecs_world_t *world, - ecs_table_t *src_table, - ecs_id_t id, - int32_t column, - int32_t column_count) -{ - ecs_table_diff_t temp_diff, diff = ECS_TABLE_DIFF_INIT; - ecs_table_t *dst_table = src_table; - ecs_id_t *ids = ecs_vector_first(src_table->type, ecs_id_t); + /* Precomputed test to verify if remainder of block is set (or not) */ + static const uint64_t bitmask_remain[64] = { + BS_MAX, BS_MAX - (BS_MAX >> 63), BS_MAX - (BS_MAX >> 62), + BS_MAX - (BS_MAX >> 61), BS_MAX - (BS_MAX >> 60), BS_MAX - (BS_MAX >> 59), + BS_MAX - (BS_MAX >> 58), BS_MAX - (BS_MAX >> 57), BS_MAX - (BS_MAX >> 56), + BS_MAX - (BS_MAX >> 55), BS_MAX - (BS_MAX >> 54), BS_MAX - (BS_MAX >> 53), + BS_MAX - (BS_MAX >> 52), BS_MAX - (BS_MAX >> 51), BS_MAX - (BS_MAX >> 50), + BS_MAX - (BS_MAX >> 49), BS_MAX - (BS_MAX >> 48), BS_MAX - (BS_MAX >> 47), + BS_MAX - (BS_MAX >> 46), BS_MAX - (BS_MAX >> 45), BS_MAX - (BS_MAX >> 44), + BS_MAX - (BS_MAX >> 43), BS_MAX - (BS_MAX >> 42), BS_MAX - (BS_MAX >> 41), + BS_MAX - (BS_MAX >> 40), BS_MAX - (BS_MAX >> 39), BS_MAX - (BS_MAX >> 38), + BS_MAX - (BS_MAX >> 37), BS_MAX - (BS_MAX >> 36), BS_MAX - (BS_MAX >> 35), + BS_MAX - (BS_MAX >> 34), BS_MAX - (BS_MAX >> 33), BS_MAX - (BS_MAX >> 32), + BS_MAX - (BS_MAX >> 31), BS_MAX - (BS_MAX >> 30), BS_MAX - (BS_MAX >> 29), + BS_MAX - (BS_MAX >> 28), BS_MAX - (BS_MAX >> 27), BS_MAX - (BS_MAX >> 26), + BS_MAX - (BS_MAX >> 25), BS_MAX - (BS_MAX >> 24), BS_MAX - (BS_MAX >> 23), + BS_MAX - (BS_MAX >> 22), BS_MAX - (BS_MAX >> 21), BS_MAX - (BS_MAX >> 20), + BS_MAX - (BS_MAX >> 19), BS_MAX - (BS_MAX >> 18), BS_MAX - (BS_MAX >> 17), + BS_MAX - (BS_MAX >> 16), BS_MAX - (BS_MAX >> 15), BS_MAX - (BS_MAX >> 14), + BS_MAX - (BS_MAX >> 13), BS_MAX - (BS_MAX >> 12), BS_MAX - (BS_MAX >> 11), + BS_MAX - (BS_MAX >> 10), BS_MAX - (BS_MAX >> 9), BS_MAX - (BS_MAX >> 8), + BS_MAX - (BS_MAX >> 7), BS_MAX - (BS_MAX >> 6), BS_MAX - (BS_MAX >> 5), + BS_MAX - (BS_MAX >> 4), BS_MAX - (BS_MAX >> 3), BS_MAX - (BS_MAX >> 2), + BS_MAX - (BS_MAX >> 1) + }; - /* If id is pair but the column pointed to is not a pair, the record is - * pointing to an instance of the id that has a (non-PAIR) role. */ - bool is_pair = ECS_HAS_ROLE(id, PAIR); - bool is_role = is_pair && !ECS_HAS_ROLE(ids[column], PAIR); - ecs_assert(!is_role || ((ids[column] & ECS_ROLE_MASK) != 0), - ECS_INTERNAL_ERROR, NULL); - bool is_wildcard = ecs_id_is_wildcard(id); + int32_t i, count = ecs_vector_count(bitset_columns); + flecs_bitset_column_t *columns = ecs_vector_first( + bitset_columns, flecs_bitset_column_t); + int32_t bs_offset = table->bs_column_offset; - int32_t i, count = ecs_vector_count(src_table->type), removed_count = 0; - ecs_entity_t entity = ECS_PAIR_RELATION(id); + int32_t first = iter->bitset_first; + int32_t last = 0; - for (i = column; i < count; i ++) { - ecs_id_t e = ids[i]; + for (i = 0; i < count; i ++) { + flecs_bitset_column_t *column = &columns[i]; + ecs_bs_column_t *bs_column = columns[i].bs_column; - if (is_role) { - if ((e & ECS_COMPONENT_MASK) != entity) { - continue; - } - } else if (is_wildcard && !ecs_id_match(e, id)) { - continue; + if (!bs_column) { + int32_t index = column->column_index; + ecs_assert((index - bs_offset >= 0), ECS_INTERNAL_ERROR, NULL); + bs_column = &table->storage.bs_columns[index - bs_offset]; + columns[i].bs_column = bs_column; } - - dst_table = flecs_table_traverse_remove(world, dst_table, &e, &temp_diff); - diff_append(&diff, &temp_diff); - removed_count ++; - if (removed_count == column_count) { - break; + ecs_bitset_t *bs = &bs_column->data; + int32_t bs_elem_count = bs->count; + int32_t bs_block = first >> 6; + int32_t bs_block_count = ((bs_elem_count - 1) >> 6) + 1; + + if (bs_block >= bs_block_count) { + goto done; } - } - ecs_assert(dst_table != NULL, ECS_INTERNAL_ERROR, NULL); + uint64_t *data = bs->data; + int32_t bs_start = first & 0x3F; - if (!dst_table->type) { - /* If this removes all components, clear table */ - flecs_table_clear_entities(world, src_table); - } else { - /* Otherwise, merge table into dst_table */ - if (dst_table != src_table) { - ecs_data_t *src_data = &src_table->storage; - int32_t src_count = ecs_table_count(src_table); - if (diff.removed.count) { - flecs_notify_on_remove(world, src_table, NULL, - 0, src_count, &diff); + /* Step 1: find the first non-empty block */ + uint64_t v = data[bs_block]; + uint64_t remain = bitmask_remain[bs_start]; + while (!(v & remain)) { + /* If no elements are remaining, move to next block */ + if ((++bs_block) >= bs_block_count) { + /* No non-empty blocks left */ + goto done; } - flecs_table_merge(world, dst_table, src_table, - &dst_table->storage, src_data); + bs_start = 0; + remain = BS_MAX; /* Test the full block */ + v = data[bs_block]; } - } - diff_free(&diff); -} + /* Step 2: find the first non-empty element in the block */ + while (!(v & bitmask[bs_start])) { + bs_start ++; -static -void delete_objects( - ecs_world_t *world, - ecs_table_t *table) -{ - ecs_data_t *data = &table->storage; - if (data) { - ecs_entity_t *entities = ecs_vector_first( - data->entities, ecs_entity_t); + /* Block was not empty, so bs_start must be smaller than 64 */ + ecs_assert(bs_start < 64, ECS_INTERNAL_ERROR, NULL); + } + + /* Step 3: Find number of contiguous enabled elements after start */ + int32_t bs_end = bs_start, bs_block_end = bs_block; + + remain = bitmask_remain[bs_end]; + while ((v & remain) == remain) { + bs_end = 0; + bs_block_end ++; - int32_t i, count = ecs_vector_count(data->entities); - for (i = 0; i < count; i ++) { - ecs_entity_t e = entities[i]; - ecs_record_t *r = flecs_sparse_get( - world->store.entity_index, ecs_record_t, e); - - /* If row is negative, it means the entity is being monitored. Only - * monitored entities can have delete actions */ - if (r && r->row < 0) { - /* Make row positive which prevents infinite recursion in case - * of cyclic delete actions */ - r->row = (-r->row); + if (bs_block_end == bs_block_count) { + break; + } - /* Run delete actions for objects */ - on_delete_any_w_entity(world, entities[i], 0); - } + v = data[bs_block_end]; + remain = BS_MAX; /* Test the full block */ } - /* Clear components from table (invokes destructors, OnRemove) */ - flecs_table_delete_entities(world, table); - } -} + /* Step 4: find remainder of enabled elements in current block */ + if (bs_block_end != bs_block_count) { + while ((v & bitmask[bs_end])) { + bs_end ++; + } + } -static -void on_delete_object_action( - ecs_world_t *world, - ecs_id_t id, - ecs_entity_t action) -{ - ecs_id_record_t *idr = flecs_get_id_record(world, id); - if (idr) { - const ecs_table_record_t *tables = flecs_id_record_tables(idr); - int32_t i, count = flecs_id_record_count(idr); + /* Block was not 100% occupied, so bs_start must be smaller than 64 */ + ecs_assert(bs_end < 64, ECS_INTERNAL_ERROR, NULL); - for (i = 0; i < count; i ++) { - const ecs_table_record_t *tr = &tables[i]; - ecs_table_t *table = tr->table; + /* Step 5: translate to element start/end and make sure that each column + * range is a subset of the previous one. */ + first = bs_block * 64 + bs_start; + int32_t cur_last = bs_block_end * 64 + bs_end; + + /* No enabled elements found in table */ + if (first == cur_last) { + goto done; + } - if (!ecs_table_count(table)) { + /* If multiple bitsets are evaluated, make sure each subsequent range + * is equal or a subset of the previous range */ + if (i) { + /* If the first element of a subsequent bitset is larger than the + * previous last value, start over. */ + if (first >= last) { + i = -1; continue; } - ecs_id_t *rel_id = ecs_vector_get(table->type, ecs_id_t, tr->column); - ecs_assert(rel_id != NULL, ECS_INTERNAL_ERROR, NULL); - - ecs_entity_t rel = ECS_PAIR_RELATION(*rel_id); - /* delete_object_action should be invoked for relations */ - ecs_assert(rel != 0, ECS_INTERNAL_ERROR, NULL); - - /* Get the record for the relation, to find the delete action */ - ecs_id_record_t *idrr; - if (!action) { - idrr = flecs_get_id_record(world, rel); - if (idrr) { - action =idrr->on_delete_object; - } - } - - if (!action || action == EcsRemove) { - remove_from_table(world, table, id, tr->column, tr->count); - i = 0; count = flecs_id_record_count(idr); - } else if (action == EcsDelete) { - delete_objects(world, table); - } else if (action == EcsThrow) { - throw_invalid_delete(world, id); + /* Make sure the last element of the range doesn't exceed the last + * element of the previous range. */ + if (cur_last > last) { + cur_last = last; } } + + last = cur_last; + int32_t elem_count = last - first; - flecs_clear_id_record(world, id); + /* Make sure last element doesn't exceed total number of elements in + * the table */ + if (elem_count > bs_elem_count) { + elem_count = bs_elem_count; + } + + cur->first = first; + cur->count = elem_count; + iter->bitset_first = first; } + + /* Keep track of last processed element for iteration */ + iter->bitset_first = last; + + return 0; +done: + return -1; } static -void on_delete_id_action( - ecs_world_t *world, - ecs_id_t id, - ecs_entity_t action) +void mark_columns_dirty( + ecs_query_t *query, + ecs_query_table_match_t *table_data) { - ecs_id_record_t *idr = flecs_get_id_record(world, id); - if (idr) { - if (!action) { - action = idr->on_delete; - } - - if (action == EcsThrow) { - throw_invalid_delete(world, id); - } - - const ecs_table_record_t *tables = flecs_id_record_tables(idr); - int32_t i, count = flecs_id_record_count(idr); + ecs_table_t *table = table_data->table; - for (i = 0; i < count ; i ++) { - const ecs_table_record_t *tr = &tables[i]; - ecs_table_t *table = tr->table; + if (table && table->dirty_state) { + ecs_term_t *terms = query->filter.terms; + int32_t c = 0, i, count = query->filter.term_count; + for (i = 0; i < count; i ++) { + ecs_term_t *term = &terms[i]; + ecs_term_id_t *subj = &term->subj; - if (!action || action == EcsRemove) { - remove_from_table(world, table, id, tr->column, tr->count); - } else if (action == EcsDelete) { - delete_objects(world, table); - } - } + if (term->inout != EcsIn && (term->inout != EcsInOutDefault || + (subj->entity == EcsThis && subj->set.mask == EcsSelf))) + { + int32_t index = table_data->columns[c]; + if (index > 0) { + int32_t storage_index = ecs_table_type_to_storage_index( + table, index - 1); + if (storage_index >= 0) { + table->dirty_state[storage_index + 1] ++; + } + } + } - flecs_clear_id_record(world, id); - } -} + if (terms[i].oper == EcsOr) { + do { + i ++; + } while ((i < count) && terms[i].oper == EcsOr); + } -static -void on_delete_action( - ecs_world_t *world, - ecs_id_t id, - ecs_entity_t action) -{ - if (ecs_id_is_wildcard(id)) { - /* If id is wildcard, check if the relation or object is a wildcard. - * Relation wildcard ids are implemented differently as relations - * with the same object aren't guaranteed to occupy neighboring - * elements in the type, other wildcards with the same relation. */ - if (ECS_PAIR_RELATION(id) == EcsWildcard) { - on_delete_object_action(world, id, action); - } else { - on_delete_id_action(world, id, action); + c ++; } - } else { - /* If the id is not a wildcard it's simple, as a table can only have - * at most one instance of the id */ - on_delete_id_action(world, id, action); } } -static -void on_delete_any_w_entity( - ecs_world_t *world, - ecs_id_t id, - ecs_entity_t action) +/* Return next table */ +bool ecs_query_next( + ecs_iter_t *it) { - /* Make sure any references to the entity are cleaned up */ - on_delete_action(world, id, action); - on_delete_action(world, ecs_pair(id, EcsWildcard), action); - on_delete_action(world, ecs_pair(EcsWildcard, id), action); -} + ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(it->next == ecs_query_next, ECS_INVALID_PARAMETER, NULL); -void ecs_delete_with( - ecs_world_t *world, - ecs_id_t id) -{ - ecs_stage_t *stage = flecs_stage_from_world(&world); - if (flecs_defer_on_delete_action(world, stage, id, EcsDelete)) { - return; - } + ecs_query_iter_t *iter = &it->iter.query; + ecs_page_iter_t *piter = &iter->page_iter; + ecs_query_t *query = iter->query; + ecs_world_t *world = query->world; + (void)world; - on_delete_action(world, id, EcsDelete); + it->is_valid = true; - flecs_defer_flush(world, stage); -} + ecs_poly_assert(world, ecs_world_t); -void ecs_remove_all( - ecs_world_t *world, - ecs_id_t id) -{ - ecs_stage_t *stage = flecs_stage_from_world(&world); - if (flecs_defer_on_delete_action(world, stage, id, EcsRemove)) { - return; + if (!query->constraints_satisfied) { + goto done; } + + ecs_page_cursor_t cur; + int32_t prev_count = it->total_count; - on_delete_action(world, id, EcsRemove); - - flecs_defer_flush(world, stage); -} - -void ecs_delete( - ecs_world_t *world, - ecs_entity_t entity) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); - - ecs_stage_t *stage = flecs_stage_from_world(&world); - if (flecs_defer_delete(world, stage, entity)) { - return; - } + ecs_query_table_node_t *node, *next; + for (node = iter->node; node != NULL; node = next) { + ecs_query_table_match_t *match = node->match; + ecs_table_t *table = match->table; - ecs_record_t *r = flecs_sparse_get( - world->store.entity_index, ecs_record_t, entity); - if (r) { - ecs_entity_info_t info = {0}; - set_info_from_record(&info, r); + next = node->next; - ecs_table_t *table = info.table; - uint64_t table_id = 0; if (table) { - table_id = table->id; - } + cur.first = node->offset; + cur.count = node->count; + if (!cur.count) { + cur.count = ecs_table_count(table); - if (info.is_watched) { - /* Make row positive which prevents infinite recursion in case - * of cyclic delete actions */ - r->row = (-r->row); + /* List should never contain empty tables */ + ecs_assert(cur.count != 0, ECS_INTERNAL_ERROR, NULL); + } - /* Ensure that the store contains no dangling references to the - * deleted entity (as a component, or as part of a relation) */ - on_delete_any_w_entity(world, entity, 0); + ecs_vector_t *bitset_columns = match->bitset_columns; + ecs_vector_t *sparse_columns = match->sparse_columns; - /* Refetch data. In case of circular relations, the entity may have - * moved to a different table. */ - set_info_from_record(&info, r); - - table = info.table; - if (table) { - table_id = table->id; - } else { - table_id = 0; + if (bitset_columns) { + if (bitset_column_next(table, bitset_columns, iter, + &cur) == -1) + { + /* No more enabled components for table */ + continue; + } else { + next = node; + } } - if (r->table) { - ecs_ids_t to_remove = flecs_type_to_ids(r->table->type); - update_component_monitors(world, entity, NULL, &to_remove); + if (sparse_columns) { + if (sparse_column_next(table, match, + sparse_columns, iter, &cur) == -1) + { + /* No more elements in sparse column */ + continue; + } else { + next = node; + } } - } - ecs_assert(!table_id || table, ECS_INTERNAL_ERROR, NULL); + int ret = ecs_page_iter_next(piter, &cur); + if (ret < 0) { + goto done; + } else if (ret > 0) { + continue; + } - /* If entity has components, remove them. Check if table is still alive, - * as delete actions could have deleted the table already. */ - if (table_id && flecs_sparse_is_alive(world->store.tables, table_id)) { - ecs_table_diff_t diff = { - .removed = flecs_type_to_ids(table->type), - .un_set = flecs_type_to_ids(table->storage_type) - }; + ecs_entity_t *entity_buffer = ecs_vector_first( + table->storage.entities, ecs_entity_t); + it->entities = &entity_buffer[cur.first]; + it->offset = cur.first; + it->count = cur.count; + it->total_count = cur.count; + } - delete_entity(world, table, info.data, info.row, &diff); - r->table = NULL; + it->table = match->table; + if (it->table) { + it->type = it->table->type; } - r->row = 0; + it->ids = match->ids; + it->columns = match->columns; + it->subjects = match->subjects; + it->sizes = match->sizes; + it->references = match->references; + it->frame_offset += prev_count; - /* Remove (and invalidate) entity after executing handlers */ - flecs_sparse_remove(world->store.entity_index, entity); - } + flecs_iter_init(it); - flecs_defer_flush(world, stage); -} + flecs_iter_populate_data(world, it, it->ptrs, NULL); -void ecs_add_id( - ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - add_id(world, entity, id); -} + if (query->flags & EcsQueryHasOutColumns) { + if (table) { + mark_columns_dirty(query, match); + } + } -void ecs_remove_id( - ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - remove_id(world, entity, id); -} + iter->node = next; -ecs_entity_t ecs_clone( - ecs_world_t *world, - ecs_entity_t dst, - ecs_entity_t src, - bool copy_value) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(src != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, src), ECS_INVALID_PARAMETER, NULL); + goto yield; + } - ecs_stage_t *stage = flecs_stage_from_world(&world); +done: + flecs_iter_fini(it); + return false; - if (!dst) { - dst = ecs_new_id(world); - } +yield: + return true; +} - if (flecs_defer_clone(world, stage, dst, src, copy_value)) { - return dst; - } +bool ecs_query_next_worker( + ecs_iter_t *it, + int32_t current, + int32_t total) +{ + int32_t per_worker, first, prev_offset = it->offset; + ecs_world_t *world = it->world; - ecs_entity_info_t src_info; - bool found = flecs_get_info(world, src, &src_info); - ecs_table_t *src_table = src_info.table; + do { + if (!ecs_query_next(it)) { + return false; + } - if (!found || !src_table) { - goto done; - } + int32_t count = it->count; + per_worker = count / total; + first = per_worker * current; - ecs_type_t src_type = src_table->type; - ecs_table_diff_t diff = {.added = flecs_type_to_ids(src_type)}; + count -= per_worker * total; - ecs_entity_info_t dst_info = {0}; - dst_info.row = new_entity(world, dst, &dst_info, src_table, &diff, true); + if (count) { + if (current < count) { + per_worker ++; + first += current; + } else { + first += count; + } + } - if (copy_value) { - flecs_table_move(world, dst, src, src_table, dst_info.data, - dst_info.row, src_table, src_info.data, src_info.row, true); + if (!per_worker && !(it->iter.query.query->flags & EcsQueryNeedsTables)) { + if (current == 0) { + flecs_iter_populate_data(world, it, it->ptrs, NULL); + return true; + } else { + return false; + } + } + } while (!per_worker); - flecs_notify_on_set(world, src_table, dst_info.row, 1, NULL, true); - } + it->frame_offset -= prev_offset; + it->count = per_worker; + it->offset += first; + it->entities = &it->entities[first]; + it->frame_offset += first; + flecs_iter_populate_data(world, it, it->ptrs, NULL); -done: - flecs_defer_flush(world, stage); - return dst; + return true; } -const void* ecs_get_id( - const ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) +bool ecs_query_changed( + ecs_query_t *query) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(flecs_stage_from_readonly_world(world)->asynchronous == false, - ECS_INVALID_PARAMETER, NULL); - - world = ecs_get_world(world); + ecs_poly_assert(query, ecs_query_t); + ecs_assert(!(query->flags & EcsQueryIsOrphaned), ECS_INVALID_PARAMETER, NULL); + return tables_dirty(query); +} - ecs_record_t *r = ecs_eis_get(world, entity); - if (!r) { - return NULL; - } +bool ecs_query_orphaned( + ecs_query_t *query) +{ + ecs_poly_assert(query, ecs_query_t); + return query->flags & EcsQueryIsOrphaned; +} - ecs_table_t *table = r->table; - if (!table) { - return NULL; - } +/* -- Public API -- */ - ecs_id_record_t *idr = flecs_get_id_record(world, id); - if (!idr) { - return NULL; - } +static +bool has_case( + const ecs_world_t *world, + ecs_entity_t sw_case, + ecs_entity_t e) +{ + const EcsType *type_ptr = ecs_get(world, e & ECS_COMPONENT_MASK, EcsType); + ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + return ecs_type_has_id(world, type_ptr->normalized, sw_case, false); +} - const ecs_table_record_t *tr = NULL; - ecs_table_t *storage_table = table->storage_table; - if (storage_table) { - tr = flecs_id_record_table(idr, storage_table); +static +bool match_id( + const ecs_world_t *world, + ecs_entity_t id, + ecs_entity_t match_with) +{ + if (ECS_HAS_ROLE(match_with, CASE)) { + ecs_entity_t sw_case = match_with & ECS_COMPONENT_MASK; + if (ECS_HAS_ROLE(id, SWITCH) && has_case(world, sw_case, id)) { + return 1; + } else { + return 0; + } } else { - /* If the entity does not have a storage table (has no data) but it does - * have the id, the id must be a tag, and getting a tag is illegal. */ - ecs_assert(!ecs_owns_id(world, entity, id), ECS_NOT_A_COMPONENT, NULL); - } - - if (!tr) { - return get_base_component(world, table, id, idr, NULL, 0); + return ecs_id_match(id, match_with); } - - bool is_monitored; - int32_t row = flecs_record_to_row(r->row, &is_monitored); - - return get_component_w_index(table, tr->column, row); } -const void* ecs_get_ref_w_id( +static +int32_t search_type( const ecs_world_t *world, - ecs_ref_t *ref, - ecs_entity_t entity, - ecs_id_t id) + const ecs_table_t *table, + ecs_type_t type, + int32_t offset, + ecs_id_t id, + ecs_entity_t rel, + int32_t min_depth, + int32_t max_depth, + int32_t depth, + ecs_entity_t *subject_out, + int32_t *count_out) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ref != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!entity || !ref->entity || entity == ref->entity, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!id || !ref->component || id == ref->component, ECS_INVALID_PARAMETER, NULL); - ecs_record_t *record = ref->record; - - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); - - entity |= ref->entity; + if (!id) { + return -1; + } - if (!record) { - record = ecs_eis_get(world, entity); + if (!type) { + return -1; } - if (!record || !record->table) { - return NULL; + if (max_depth && depth > max_depth) { + return -1; } - ecs_table_t *table = record->table; + int32_t i, count = ecs_vector_count(type); + ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); - if (ref->record == record && - ref->table == table && - ref->row == record->row && - ref->alloc_count == table->alloc_count) - { - return ref->ptr; + if (depth >= min_depth) { + if (table && !offset && !(ECS_HAS_ROLE(id, CASE))) { + ecs_table_record_t *tr = flecs_get_table_record(world, table, id); + if (tr) { + if (count_out) { + *count_out = tr->count; + } + return tr->column; + } + } else { + for (i = offset; i < count; i ++) { + if (match_id(world, ids[i], id)) { + return i; + } + } + } } - id |= ref->component; + if (rel && id != EcsPrefab && id != EcsDisabled && + ECS_PAIR_RELATION(id) != ecs_id(EcsIdentifier) && + ECS_PAIR_RELATION(id) != EcsChildOf) + { + for (i = 0; i < count; i ++) { + ecs_entity_t e = ids[i]; + if (!ECS_HAS_RELATION(e, rel)) { + continue; + } - int32_t row = record->row; + ecs_entity_t obj = ecs_pair_object(world, e); + ecs_assert(obj != 0, ECS_INTERNAL_ERROR, NULL); - ref->entity = entity; - ref->component = id; - ref->table = table; - ref->row = record->row; - ref->alloc_count = table->alloc_count; + ecs_table_t *obj_table = ecs_get_table(world, obj); + if (!obj_table) { + continue; + } - if (table) { - bool is_monitored; - row = flecs_record_to_row(row, &is_monitored); - ref->ptr = get_component(world, table, row, id); - } + if ((search_type(world, obj_table, obj_table->type, 0, id, + rel, min_depth, max_depth, depth + 1, subject_out, NULL) != -1)) + { + if (subject_out && !*subject_out) { + *subject_out = obj; + } + return i; - ref->record = record; + /* If the id could not be found on the object and the relationship + * is not IsA, try substituting the object type with IsA */ + } else if (rel != EcsIsA) { + if (search_type(world, obj_table, obj_table->type, 0, + id, EcsIsA, 1, 0, 0, subject_out, NULL) != -1) + { + if (subject_out && !*subject_out) { + *subject_out = obj; + } + return i; + } + } + } + } - return ref->ptr; + return -1; } -void* ecs_get_mut_id( - ecs_world_t *world, - ecs_entity_t entity, +bool ecs_type_has_id( + const ecs_world_t *world, + ecs_type_t type, ecs_id_t id, - bool *is_added) + bool owned) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - - ecs_stage_t *stage = flecs_stage_from_world(&world); - void *result; - - if (flecs_defer_set( - world, stage, EcsOpMut, entity, id, 0, NULL, &result, is_added)) - { - return result; - } - - ecs_entity_info_t info; - result = get_mutable(world, entity, id, &info, is_added); - - /* Store table so we can quickly check if returned pointer is still valid */ - ecs_table_t *table = info.record->table; - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + return search_type(world, NULL, type, 0, id, owned ? 0 : EcsIsA, 0, 0, 0, NULL, NULL) != -1; +} - /* Keep track of alloc count of table, since even if the entity has not - * moved, other entities could have been added to the table which could - * reallocate arrays. Also store the row, as the entity could have - * reallocated. */ - int32_t alloc_count = table->alloc_count; - int32_t row = info.record->row; - - flecs_defer_flush(world, stage); +int32_t ecs_type_index_of( + ecs_type_t type, + int32_t offset, + ecs_id_t id) +{ + return search_type(NULL, NULL, type, offset, id, 0, 0, 0, 0, NULL, NULL); +} - /* Ensure that after flushing, the pointer is still valid. Flushing may - * trigger callbacks, which could do anything with the entity */ - if (table != info.record->table || - alloc_count != info.record->table->alloc_count || - row != info.record->row) - { - if (flecs_get_info(world, entity, &info) && info.table) { - result = get_component(world, info.table, info.row, id); - } else { - /* A trigger has removed the component we just added. This is not - * allowed, an application should always be able to assume that - * get_mut returns a valid pointer. */ - ecs_assert(false, ECS_INVALID_OPERATION, NULL); - } +int32_t ecs_type_match( + const ecs_world_t *world, + const ecs_table_t *table, + ecs_type_t type, + int32_t offset, + ecs_id_t id, + ecs_entity_t rel, + int32_t min_depth, + int32_t max_depth, + ecs_entity_t *subject_out, + int32_t *count_out) +{ + if (subject_out) { + *subject_out = 0; } - - return result; + return search_type(world, table, type, offset, id, rel, min_depth, max_depth, 0, subject_out, count_out); } -void* ecs_emplace_id( - ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) +char* ecs_type_str( + const ecs_world_t *world, + ecs_type_t type) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - ecs_assert(!ecs_has_id(world, entity, id), ECS_INVALID_PARAMETER, NULL); + if (!type) { + return ecs_os_strdup(""); + } - ecs_stage_t *stage = flecs_stage_from_world(&world); - void *result; + ecs_vector_t *chbuf = ecs_vector_new(char, 32); + char *dst; - if (flecs_defer_set( - world, stage, EcsOpMut, entity, id, 0, NULL, &result, NULL)) - { - return result; - } + ecs_entity_t *entities = ecs_vector_first(type, ecs_entity_t); + int32_t i, count = ecs_vector_count(type); - ecs_entity_info_t info = {0}; - flecs_get_info(world, entity, &info); + for (i = 0; i < count; i ++) { + ecs_entity_t e = entities[i]; + char buffer[256]; + ecs_size_t len; - add_id_w_info(world, entity, &info, id, false /* Add without ctor */); + if (i) { + *(char*)ecs_vector_add(&chbuf, char) = ','; + } - void *ptr = get_component(world, info.table, info.row, id); + if (e == 1) { + ecs_os_strcpy(buffer, "EcsComponent"); + len = ecs_os_strlen("EcsComponent"); + } else { + len = flecs_from_size_t(ecs_id_str_w_buf(world, e, buffer, 256)); + } - flecs_defer_flush(world, stage); + dst = ecs_vector_addn(&chbuf, char, len); + ecs_os_memcpy(dst, buffer, len); + } - return ptr; + *(char*)ecs_vector_add(&chbuf, char) = '\0'; + + char* result = ecs_os_strdup(ecs_vector_first(chbuf, char)); + ecs_vector_free(chbuf); + return result; } -void ecs_modified_id( - ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) +static +ecs_vector_t* sort_and_dedup( + ecs_vector_t *result) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + /* Sort vector */ + ecs_vector_sort(result, ecs_id_t, flecs_entity_compare_qsort); - ecs_stage_t *stage = flecs_stage_from_world(&world); + /* Ensure vector doesn't contain duplicates */ + ecs_id_t *ids = ecs_vector_first(result, ecs_id_t); + int32_t i, offset = 0, count = ecs_vector_count(result); - if (flecs_defer_modified(world, stage, entity, id)) { - return; - } + for (i = 0; i < count; i ++) { + if (i && ids[i] == ids[i - 1]) { + offset ++; + } - /* If the entity does not have the component, calling ecs_modified is - * invalid. The assert needs to happen after the defer statement, as the - * entity may not have the component when this function is called while - * operations are being deferred. */ - ecs_assert(ecs_has_id(world, entity, id), - ECS_INVALID_PARAMETER, NULL); + if (i + offset >= count) { + break; + } - ecs_entity_info_t info = {0}; - if (flecs_get_info(world, entity, &info)) { - ecs_ids_t ids = { .array = &id, .count = 1 }; - flecs_notify_on_set(world, info.table, info.row, 1, &ids, true); + ids[i] = ids[i + offset]; } - flecs_table_mark_dirty(world, info.table, id); - - flecs_defer_flush(world, stage); + ecs_vector_set_count(&result, ecs_id_t, i - offset); + + return result; } +/** Parse callback that adds type to type identifier */ static -ecs_entity_t assign_ptr_w_id( +ecs_vector_t* expr_to_ids( ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id, - size_t size, - void *ptr, - bool is_move, - bool notify) + const char *name, + const char *expr) { - ecs_stage_t *stage = flecs_stage_from_world(&world); +#ifdef FLECS_PARSER + ecs_vector_t *result = NULL; + const char *ptr = expr; + ecs_term_t term = {0}; - if (!entity) { - entity = ecs_new_id(world); - ecs_entity_t scope = stage->scope; - if (scope) { - ecs_add_pair(world, entity, EcsChildOf, scope); - } + if (!ptr) { + return NULL; } - if (flecs_defer_set(world, stage, EcsOpSet, entity, id, - flecs_from_size_t(size), ptr, NULL, NULL)) - { - return entity; - } + while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))) { + if (term.name) { + ecs_parser_error(name, expr, (ptr - expr), + "column names not supported in type expression"); + goto error; + } - ecs_entity_info_t info; + if (term.oper != EcsAnd && term.oper != EcsAndFrom) { + ecs_parser_error(name, expr, (ptr - expr), + "operator other than AND not supported in type expression"); + goto error; + } - void *dst = get_mutable(world, entity, id, &info, NULL); + if (ecs_term_finalize(world, name, &term)) { + goto error; + } - /* This can no longer happen since we defer operations */ - ecs_assert(dst != NULL, ECS_INTERNAL_ERROR, NULL); + if (term.subj.entity == 0) { + /* Empty term */ + goto done; + } - if (ptr) { - ecs_entity_t real_id = ecs_get_typeid(world, id); - const ecs_type_info_t *cdata = get_c_info(world, real_id); - if (cdata) { - if (is_move) { - ecs_move_t move = cdata->lifecycle.move; - if (move) { - move(world, real_id, &entity, &entity, dst, ptr, size, 1, - cdata->lifecycle.ctx); - } else { - ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); - } - } else { - ecs_copy_t copy = cdata->lifecycle.copy; - if (copy) { - copy(world, real_id, &entity, &entity, dst, ptr, size, 1, - cdata->lifecycle.ctx); - } else { - ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); - } - } - } else { - ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); + if (term.subj.set.mask != EcsSelf) { + ecs_parser_error(name, expr, (ptr - expr), + "source modifiers not supported for type expressions"); + goto error; } - } else { - memset(dst, 0, size); - } - flecs_table_mark_dirty(world, info.table, id); + if (term.subj.entity != EcsThis) { + ecs_parser_error(name, expr, (ptr - expr), + "subject other than this not supported in type expression"); + goto error; + } - if (notify) { - ecs_ids_t ids = { .array = &id, .count = 1 }; - flecs_notify_on_set(world, info.table, info.row, 1, &ids, true); - } + if (term.oper == EcsAndFrom) { + term.role = ECS_AND; + } - flecs_defer_flush(world, stage); + ecs_id_t* elem = ecs_vector_add(&result, ecs_id_t); + *elem = term.id | term.role; - return entity; -} + ecs_term_fini(&term); + } -ecs_entity_t ecs_set_id( - ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id, - size_t size, - const void *ptr) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!entity || ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + result = sort_and_dedup(result); - /* Safe to cast away const: function won't modify if move arg is false */ - return assign_ptr_w_id( - world, entity, id, size, (void*)ptr, false, true); +done: + return result; +error: + ecs_term_fini(&term); + ecs_vector_free(result); + return NULL; +#else + (void)world; + (void)name; + (void)expr; + ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); + return NULL; +#endif } -ecs_entity_t ecs_get_case( - const ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_t sw_id) +/* Create normalized type. A normalized type resolves all elements with an + * AND flag and appends them to the resulting type, where the default type + * maintains the original type hierarchy. */ +static +ecs_vector_t* ids_to_normalized_ids( + ecs_world_t *world, + ecs_vector_t *ids) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, sw_id), ECS_INVALID_PARAMETER, NULL); + ecs_vector_t *result = NULL; - world = ecs_get_world(world); + ecs_entity_t *array = ecs_vector_first(ids, ecs_id_t); + int32_t i, count = ecs_vector_count(ids); - ecs_entity_info_t info; - ecs_table_t *table; - if (!flecs_get_info(world, entity, &info) || !(table = info.table)) { - return 0; - } + for (i = 0; i < count; i ++) { + ecs_entity_t e = array[i]; + if (ECS_HAS_ROLE(e, AND)) { + ecs_entity_t entity = ECS_PAIR_OBJECT(e); - sw_id = sw_id | ECS_SWITCH; + const EcsType *type_ptr = ecs_get(world, entity, EcsType); + ecs_assert(type_ptr != NULL, ECS_INVALID_PARAMETER, + "flag must be applied to type"); - ecs_type_t type = table->type; - int32_t index = ecs_type_index_of(type, 0, sw_id); - if (index == -1) { - return 0; + ecs_vector_each(type_ptr->normalized, ecs_id_t, c_ptr, { + ecs_entity_t *el = ecs_vector_add(&result, ecs_id_t); + *el = *c_ptr; + }) + } else { + ecs_entity_t *el = ecs_vector_add(&result, ecs_id_t); + *el = e; + } } - index -= table->sw_column_offset; - ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); - - /* Data cannot be NULl, since entity is stored in the table */ - ecs_assert(info.data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_switch_t *sw = info.data->sw_columns[index].data; - return flecs_switch_get(sw, info.row); + return sort_and_dedup(result); } -void ecs_enable_component_w_id( +static +ecs_table_t* table_from_ids( ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id, - bool enable) + ecs_vector_t *ids) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - - ecs_stage_t *stage = flecs_stage_from_world(&world); + ecs_ids_t ids_array = flecs_type_to_ids(ids); + ecs_table_t *result = flecs_table_find_or_create(world, &ids_array); + return result; +} - if (flecs_defer_enable( - world, stage, entity, id, enable)) - { - return; - } else { - /* Operations invoked by enable/disable should not be deferred */ - stage->defer --; +/* If a name prefix is set with ecs_set_name_prefix, check if the entity name + * has the prefix, and if so remove it. This enables using prefixed names in C + * for components / systems while storing a canonical / language independent + * identifier. */ +const char* flecs_name_from_symbol( + ecs_world_t *world, + const char *type_name) +{ + const char *prefix = world->name_prefix; + if (type_name && prefix) { + ecs_size_t len = ecs_os_strlen(prefix); + if (!ecs_os_strncmp(type_name, prefix, len) && + (isupper(type_name[len]) || type_name[len] == '_')) + { + if (type_name[len] == '_') { + return type_name + len + 1; + } else { + return type_name + len; + } + } } - ecs_entity_info_t info; - flecs_get_info(world, entity, &info); + return type_name; +} - ecs_entity_t bs_id = (id & ECS_COMPONENT_MASK) | ECS_DISABLED; - - ecs_table_t *table = info.table; - int32_t index = -1; - if (table) { - index = ecs_type_index_of(table->type, 0, bs_id); - } +/* -- Public functions -- */ - if (index == -1) { - ecs_add_id(world, entity, bs_id); - ecs_enable_component_w_id(world, entity, id, enable); - return; +ecs_type_t ecs_type_from_str( + ecs_world_t *world, + const char *expr) +{ + ecs_vector_t *ids = expr_to_ids(world, NULL, expr); + if (!ids) { + return NULL; } - index -= table->bs_column_offset; - ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_vector_t *normalized_ids = ids_to_normalized_ids(world, ids); + ecs_vector_free(ids); - /* Data cannot be NULl, since entity is stored in the table */ - ecs_assert(info.data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_bitset_t *bs = &info.data->bs_columns[index].data; - ecs_assert(bs != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_table_t *table = table_from_ids(world, normalized_ids); + ecs_vector_free(normalized_ids); - flecs_bitset_set(bs, info.row, enable); + return table->type; } -bool ecs_is_component_enabled_w_id( - const ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) +ecs_table_t* ecs_table_from_str( + ecs_world_t *world, + const char *expr) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); - - ecs_entity_info_t info; - ecs_table_t *table; - if (!flecs_get_info(world, entity, &info) || !(table = info.table)) { - return false; - } - - ecs_entity_t bs_id = (id & ECS_COMPONENT_MASK) | ECS_DISABLED; + ecs_poly_assert(world, ecs_world_t); - ecs_type_t type = table->type; - int32_t index = ecs_type_index_of(type, 0, bs_id); - if (index == -1) { - /* If table does not have DISABLED column for component, component is - * always enabled, if the entity has it */ - return ecs_has_id(world, entity, id); + ecs_vector_t *ids = expr_to_ids(world, NULL, expr); + if (!ids) { + return NULL; } - index -= table->bs_column_offset; - ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); - - /* Data cannot be NULl, since entity is stored in the table */ - ecs_assert(info.data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_bitset_t *bs = &info.data->bs_columns[index].data; + ecs_table_t *result = table_from_ids(world, ids); + ecs_vector_free(ids); - return flecs_bitset_get(bs, info.row); + return result; } -bool ecs_has_id( - const ecs_world_t *world, - ecs_entity_t entity, - ecs_id_t id) +/* Count number of switch columns */ +static +int32_t switch_column_count( + ecs_table_t *table) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - ecs_assert(id != 0, ECS_INVALID_PARAMETER, NULL); + int32_t i, sw_count = 0, count = ecs_vector_count(table->type); + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - if (!id) { - return true; + for (i = 0; i < count; i ++) { + ecs_id_t id = ids[i]; + if (ECS_HAS_ROLE(id, SWITCH)) { + if (!sw_count) { + table->sw_column_offset = i; + } + sw_count ++; + } } - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); - - if (ECS_HAS_ROLE(id, CASE)) { - ecs_entity_info_t info; - ecs_table_t *table; - if (!flecs_get_info(world, entity, &info) || !(table = info.table)) { - return false; - } + return sw_count; +} - int32_t index = flecs_table_switch_from_case(world, table, id); - ecs_assert(index < table->sw_column_count, ECS_INTERNAL_ERROR, NULL); - - ecs_data_t *data = info.data; - ecs_switch_t *sw = data->sw_columns[index].data; - ecs_entity_t value = flecs_switch_get(sw, info.row); +/* Count number of bitset columns */ +static +int32_t bitset_column_count( + ecs_table_t *table) +{ + int32_t count = 0; + ecs_vector_each(table->type, ecs_entity_t, c_ptr, { + ecs_entity_t component = *c_ptr; - return value == (id & ECS_COMPONENT_MASK); - } else { - ecs_table_t *table = ecs_get_table(world, entity); - if (!table) { - return false; + if (ECS_HAS_ROLE(component, DISABLED)) { + if (!count) { + table->bs_column_offset = c_ptr_i; + } + count ++; } + }); - return ecs_type_match( - world, table, table->type, 0, id, EcsIsA, 0, 0, NULL, NULL) != -1; - } + return count; } -ecs_entity_t ecs_get_object( - const ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_t rel, - int32_t index) +static +void init_storage_map( + ecs_table_t *table) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(rel != 0, ECS_INVALID_PARAMETER, NULL); - - world = ecs_get_world(world); - - if (!entity) { - return 0; + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + if (!table->storage_table) { + return; } - ecs_table_t *table = ecs_get_table(world, entity); - if (!table) { - return 0; - } + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); + int32_t t, ids_count = ecs_vector_count(table->type); + ecs_id_t *storage_ids = ecs_vector_first(table->storage_type, ecs_id_t); + int32_t s, storage_ids_count = ecs_vector_count(table->storage_type); - ecs_id_t wc = ecs_pair(rel, EcsWildcard); - ecs_table_record_t *tr = flecs_get_table_record(world, table, wc); - if (!tr) { - return 0; + if (!ids_count) { + table->storage_map = NULL; + return; } - if (index >= tr->count) { - return 0; - } + table->storage_map = ecs_os_malloc_n( + int32_t, ids_count + storage_ids_count); - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - return ecs_pair_object(world, ids[tr->column + index]); -} + int32_t *t2s = table->storage_map; + int32_t *s2t = &table->storage_map[ids_count]; -ecs_entity_t ecs_get_object_for_id( - const ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_t rel, - ecs_id_t id) -{ - ecs_table_t *table = ecs_get_table(world, entity); - ecs_entity_t subject = 0; + for (s = 0, t = 0; (t < ids_count) && (s < storage_ids_count); ) { + ecs_id_t id = ids[t]; + ecs_id_t storage_id = storage_ids[s]; - if (rel) { - int32_t column = ecs_type_match( - world, table, table->type, 0, id, rel, 0, 0, &subject, NULL); - if (column == -1) { - return 0; + if (id == storage_id) { + t2s[t] = s; + s2t[s] = t; + } else { + t2s[t] = -1; } - } else { - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - int32_t i, count = ecs_vector_count(table->type); - for (i = 0; i < count; i ++) { - ecs_id_t ent = ids[i]; - if (ent & ECS_ROLE_MASK) { - /* Skip ids with pairs, roles since 0 was provided for rel */ - break; - } + /* Ids can never get ahead of storage id, as ids are a superset of the + * storage ids */ + ecs_assert(id <= storage_id, ECS_INTERNAL_ERROR, NULL); - if (ecs_has_id(world, ent, id)) { - subject = ent; - break; - } - } + t += (id <= storage_id); + s += (id == storage_id); } - if (subject == 0) { - return entity; - } else { - return subject; + /* Storage ids is always a subset of ids, so all should be iterated */ + ecs_assert(s == storage_ids_count, ECS_INTERNAL_ERROR, NULL); + + /* Initialize remainder of type -> storage_type map */ + for (; (t < ids_count); t ++) { + t2s[t] = -1; } } -const char* ecs_get_name( - const ecs_world_t *world, - ecs_entity_t entity) +static +void init_storage_table( + ecs_world_t *world, + ecs_table_t *table) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + int32_t i, count = ecs_vector_count(table->type); + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); + ecs_ids_t storage_ids = { + .array = ecs_os_alloca_n(ecs_id_t, count) + }; - const EcsIdentifier *ptr = ecs_get_pair( - world, entity, EcsIdentifier, EcsName); + for (i = 0; i < count; i ++) { + ecs_id_t id = ids[i]; - if (ptr) { - return ptr->value; - } else { - return NULL; - } -} + if ((id == ecs_id(EcsComponent)) || + (ECS_PAIR_RELATION(id) == ecs_id(EcsIdentifier))) + { + storage_ids.array[storage_ids.count ++] = id; + continue; + } -const char* ecs_get_symbol( - const ecs_world_t *world, - ecs_entity_t entity) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + const EcsComponent *comp = flecs_component_from_id(world, id); + if (!comp || !comp->size) { + continue; + } - const EcsIdentifier *ptr = ecs_get_pair( - world, entity, EcsIdentifier, EcsSymbol); + storage_ids.array[storage_ids.count ++] = id; + } + + if (storage_ids.count && storage_ids.count != count) { + table->storage_table = flecs_table_find_or_create(world, &storage_ids); + table->storage_type = table->storage_table->type; + ecs_assert(table->storage_table != NULL, ECS_INTERNAL_ERROR, NULL); + } else if (storage_ids.count) { + table->storage_table = table; + table->storage_type = table->storage_table->type; + ecs_assert(table->storage_table != NULL, ECS_INTERNAL_ERROR, NULL); + } - if (ptr) { - return ptr->value; - } else { - return NULL; + if (!table->storage_map) { + init_storage_map(table); } } -ecs_entity_t ecs_set_name( +void flecs_table_init_data( ecs_world_t *world, - ecs_entity_t entity, - const char *name) + ecs_table_t *table) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + init_storage_table(world, table); - if (!entity) { - entity = ecs_new_id(world); - } - - ecs_set_pair(world, entity, EcsIdentifier, EcsName, {.value = (char*)name}); + int32_t sw_count = table->sw_column_count = switch_column_count(table); + int32_t bs_count = table->bs_column_count = bitset_column_count(table); - return entity; -} + ecs_data_t *storage = &table->storage; + ecs_type_t type = table->storage_type; -ecs_entity_t ecs_set_symbol( - ecs_world_t *world, - ecs_entity_t entity, - const char *name) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t i, count = ecs_vector_count(type); - if (!entity) { - entity = ecs_new_id(world); + /* Root tables don't have columns */ + if (!count && !sw_count && !bs_count) { + storage->columns = NULL; } - - ecs_set_pair(world, entity, EcsIdentifier, EcsSymbol, { - .value = (char*)name - }); - return entity; -} + if (count) { + ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); + storage->columns = ecs_os_calloc_n(ecs_column_t, count); -ecs_id_t ecs_make_pair( - ecs_entity_t relation, - ecs_entity_t object) -{ - return ecs_pair(relation, object); -} + for (i = 0; i < count; i ++) { + ecs_entity_t id = ids[i]; -bool ecs_is_valid( - const ecs_world_t *world, - ecs_entity_t entity) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + /* Bootstrap components */ + if (id == ecs_id(EcsComponent)) { + storage->columns[i].size = ECS_SIZEOF(EcsComponent); + storage->columns[i].alignment = ECS_ALIGNOF(EcsComponent); + continue; + } else if (ECS_PAIR_RELATION(id) == ecs_id(EcsIdentifier)) { + storage->columns[i].size = ECS_SIZEOF(EcsIdentifier); + storage->columns[i].alignment = ECS_ALIGNOF(EcsIdentifier); + continue; + } - /* 0 is not a valid entity id */ - if (!entity) { - return false; - } + const EcsComponent *component = flecs_component_from_id(world, id); + ecs_assert(component != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(component->size != 0, ECS_INTERNAL_ERROR, NULL); - /* Entities should not contain data in dead zone bits */ - if (entity & ~0xFF00FFFFFFFFFFFF) { - return false; + storage->columns[i].size = flecs_to_i16(component->size); + storage->columns[i].alignment = flecs_to_i16(component->alignment); + } } - - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); - /* When checking roles and/or pairs, the generation count may have been - * stripped away. Just test if the entity is 0 or not. */ - if (ECS_HAS_ROLE(entity, PAIR)) { - ecs_entity_t lo = ECS_PAIR_OBJECT(entity); - ecs_entity_t hi = ECS_PAIR_RELATION(entity); - return lo != 0 && hi != 0; - } else - if (entity & ECS_ROLE) { - return ecs_entity_t_lo(entity) != 0; - } + if (sw_count) { + ecs_entity_t *ids = ecs_vector_first(table->type, ecs_entity_t); + int32_t sw_offset = table->sw_column_offset; + storage->sw_columns = ecs_os_calloc_n(ecs_sw_column_t, sw_count); - /* An id may not yet exist in the world which does not mean it cannot be - * used as an entity identifier. An example is when a hard-coded entity id - * is used. However, if the entity id does exist in the world, it must be - * alive. */ - return !ecs_exists(world, entity) || ecs_is_alive(world, entity); -} + for (i = 0; i < sw_count; i ++) { + ecs_entity_t e = ids[i + sw_offset]; + ecs_assert(ECS_HAS_ROLE(e, SWITCH), ECS_INTERNAL_ERROR, NULL); + e = e & ECS_COMPONENT_MASK; + const EcsType *type_ptr = ecs_get(world, e, EcsType); + ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_type_t sw_type = type_ptr->normalized; -ecs_id_t ecs_strip_generation( - ecs_entity_t e) -{ - /* If this is not a pair, erase the generation bits */ - if (!(e & ECS_ROLE_MASK)) { - e &= ~ECS_GENERATION_MASK; + ecs_entity_t *sw_array = ecs_vector_first(sw_type, ecs_entity_t); + int32_t sw_array_count = ecs_vector_count(sw_type); + + ecs_switch_t *sw = flecs_switch_new( + sw_array[0], + sw_array[sw_array_count - 1], + 0); + + storage->sw_columns[i].data = sw; + storage->sw_columns[i].type = sw_type; + } } - return e; + if (bs_count) { + storage->bs_columns = ecs_os_calloc_n(ecs_bs_column_t, bs_count); + for (i = 0; i < bs_count; i ++) { + flecs_bitset_init(&storage->bs_columns[i].data); + } + } } -bool ecs_is_alive( - const ecs_world_t *world, - ecs_entity_t entity) +static +ecs_flags32_t get_component_action_flags( + const ecs_type_info_t *c_info) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); + ecs_flags32_t flags = 0; - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); + if (c_info->lifecycle.ctor) { + flags |= EcsTableHasCtors; + } + if (c_info->lifecycle.dtor) { + flags |= EcsTableHasDtors; + } + if (c_info->lifecycle.copy) { + flags |= EcsTableHasCopy; + } + if (c_info->lifecycle.move) { + flags |= EcsTableHasMove; + } - return ecs_eis_is_alive(world, entity); + return flags; } -ecs_entity_t ecs_get_alive( - const ecs_world_t *world, - ecs_entity_t entity) +/* Check if table has instance of component, including pairs */ +static +bool has_component( + ecs_world_t *world, + ecs_type_t type, + ecs_entity_t component) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - - if (!entity) { - return 0; - } + ecs_entity_t *entities = ecs_vector_first(type, ecs_entity_t); + int32_t i, count = ecs_vector_count(type); - if (ecs_is_alive(world, entity)) { - return entity; + for (i = 0; i < count; i ++) { + if (component == ecs_get_typeid(world, entities[i])) { + return true; + } } + + return false; +} - /* Make sure id does not have generation. This guards against accidentally - * "upcasting" a not alive identifier to a alive one. */ - ecs_assert((uint32_t)entity == entity, ECS_INVALID_PARAMETER, NULL); +static +void notify_component_info( + ecs_world_t *world, + ecs_table_t *table, + ecs_entity_t component) +{ + ecs_type_t table_type = table->storage_type; + if (!component || has_component(world, table_type, component)){ + int32_t column_count = ecs_vector_count(table_type); + ecs_assert(!component || column_count != 0, ECS_INTERNAL_ERROR, NULL); - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); + if (!column_count) { + return; + } + + if (!table->c_info) { + table->c_info = ecs_os_calloc( + ECS_SIZEOF(ecs_type_info_t*) * column_count); + } - ecs_entity_t current = ecs_eis_get_current(world, entity); - if (!current || !ecs_is_alive(world, current)) { - return 0; - } + /* Reset lifecycle flags before recomputing */ + table->flags &= ~EcsTableHasLifecycle; - return current; -} + /* Recompute lifecycle flags */ + ecs_entity_t *array = ecs_vector_first(table_type, ecs_entity_t); + int32_t i; + for (i = 0; i < column_count; i ++) { + ecs_id_t id = array[i]; + ecs_entity_t c; -void ecs_ensure( - ecs_world_t *world, - ecs_entity_t entity) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); + /* Hardcode components used in bootstrap */ + if (id == ecs_id(EcsComponent)) { + c = id; + } else if (ECS_PAIR_RELATION(id) == ecs_id(EcsIdentifier)) { + c = ecs_id(EcsIdentifier); + } else { + c = ecs_get_typeid(world, array[i]); + } + ecs_assert(c != 0, ECS_INTERNAL_ERROR, NULL); + + const ecs_type_info_t *c_info = flecs_get_c_info(world, c); + if (c_info) { + ecs_flags32_t flags = get_component_action_flags(c_info); + table->flags |= flags; + } - if (ecs_eis_is_alive(world, entity)) { - /* Nothing to be done, already alive */ - return; + /* Store pointer to c_info for fast access */ + table->c_info[i] = (ecs_type_info_t*)c_info; + } } - - /* Ensure id exists. The underlying datastructure will verify that the - * generation count matches the provided one. */ - ecs_eis_ensure(world, entity); } -bool ecs_exists( - const ecs_world_t *world, - ecs_entity_t entity) +static +void notify_trigger( + ecs_world_t *world, + ecs_table_t *table, + ecs_entity_t event) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); - - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); + (void)world; - return ecs_eis_exists(world, entity); + if (event == EcsOnAdd) { + table->flags |= EcsTableHasOnAdd; + } else if (event == EcsOnRemove) { + table->flags |= EcsTableHasOnRemove; + } else if (event == EcsOnSet) { + table->flags |= EcsTableHasOnSet; + } else if (event == EcsUnSet) { + table->flags |= EcsTableHasUnSet; + } } -ecs_table_t* ecs_get_table( - const ecs_world_t *world, - ecs_entity_t entity) +static +void run_on_remove( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); + int32_t count = ecs_vector_count(data->entities); + if (count) { + ecs_ids_t removed = { + .array = ecs_vector_first(table->type, ecs_id_t), + .count = ecs_vector_count(table->type) + }; - ecs_record_t *record = ecs_eis_get(world, entity); - ecs_table_t *table; - if (record && (table = record->table)) { - return table; + ecs_table_diff_t diff = { + .removed = removed, + .un_set = removed + }; + + flecs_notify_on_remove(world, table, NULL, 0, count, &diff); } - - return NULL; } -ecs_table_t* ecs_get_storage_table( - const ecs_world_t *world, - ecs_entity_t entity) +/* -- Private functions -- */ + +/* If table goes from 0 to >0 entities or from >0 entities to 0 entities notify + * queries. This allows systems associated with queries to move inactive tables + * out of the main loop. */ +static +void table_activate( + ecs_world_t *world, + ecs_table_t *table, + bool activate) { - ecs_table_t *table = ecs_get_table(world, entity); - if (table) { - return table->storage_table; + ecs_vector_t *queries = table->queries; + ecs_query_t **buffer = ecs_vector_first(queries, ecs_query_t*); + int32_t i, count = ecs_vector_count(queries); + + for (i = 0; i < count; i ++) { + flecs_query_notify(world, buffer[i], &(ecs_query_event_t) { + .kind = activate ? EcsQueryTableNonEmpty : EcsQueryTableEmpty, + .table = table + }); } - return NULL; + flecs_table_set_empty(world, table); } -ecs_type_t ecs_get_type( - const ecs_world_t *world, - ecs_entity_t entity) +/* This function is called when a query is matched with a table. A table keeps + * a list of tables that match so that they can be notified when the table + * becomes empty / non-empty. */ +static +void register_query( + ecs_world_t *world, + ecs_table_t *table, + ecs_query_t *query) { - ecs_table_t *table = ecs_get_table(world, entity); - if (table) { - return table->type; + (void)world; +#ifndef NDEBUG + /* Sanity check if query has already been added */ + int32_t i, count = ecs_vector_count(table->queries); + for (i = 0; i < count; i ++) { + ecs_query_t **q = ecs_vector_get(table->queries, ecs_query_t*, i); + ecs_assert(*q != query, ECS_INTERNAL_ERROR, NULL); } +#endif - return NULL; + ecs_query_t **q = ecs_vector_add(&table->queries, ecs_query_t*); + if (q) *q = query; } -ecs_id_t ecs_get_typeid( - const ecs_world_t *world, - ecs_id_t id) +/* This function is called when a query is unmatched with a table. This can + * happen for queries that have shared components expressions in their signature + * and those shared components changed (for example, a base removed a comp). */ +static +void unregister_query( + ecs_world_t *world, + ecs_table_t *table, + ecs_query_t *query) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + (void)world; - if (ECS_HAS_ROLE(id, PAIR)) { - /* Make sure we're not working with a stage */ - world = ecs_get_world(world); + int32_t i, count = ecs_vector_count(table->queries); + for (i = 0; i < count; i ++) { + ecs_query_t **q = ecs_vector_get(table->queries, ecs_query_t*, i); + if (*q == query) { + break; + } + } - ecs_entity_t rel = ecs_get_alive(world, ECS_PAIR_RELATION(id)); + /* Query must have been registered with table */ + ecs_assert(i != count, ECS_INTERNAL_ERROR, NULL); - /* If relation is marked as a tag, it never has data. Return relation */ - if (ecs_has_id(world, rel, EcsTag)) { - return 0; - } + /* Remove query */ + ecs_vector_remove(table->queries, ecs_query_t*, i); +} - const EcsComponent *ptr = ecs_get(world, rel, EcsComponent); - if (ptr && ptr->size != 0) { - return rel; - } else { - ecs_entity_t obj = ecs_get_alive(world, ECS_PAIR_OBJECT(id)); - ptr = ecs_get(world, obj, EcsComponent); - - if (ptr && ptr->size != 0) { - return obj; - } +static +void ctor_component( + ecs_world_t *world, + ecs_type_info_t *cdata, + ecs_column_t *column, + ecs_entity_t *entities, + int32_t row, + int32_t count) +{ + /* A new component is constructed */ + ecs_xtor_t ctor; + if (cdata && (ctor = cdata->lifecycle.ctor)) { + void *ctx = cdata->lifecycle.ctx; + int16_t size = column->size; + int16_t alignment = column->alignment; - /* Neither relation nor object have data */ - return 0; - } + void *ptr = ecs_vector_get_t(column->data, size, alignment, row); - } else if (id & ECS_ROLE_MASK) { - return 0; - } else { - const EcsComponent *ptr = ecs_get(world, id, EcsComponent); - if (!ptr || !ptr->size) { - return 0; - } + ctor(world, cdata->component, entities, ptr, + flecs_to_size_t(size), count, ctx); } - - return id; } -int32_t ecs_count_id( - const ecs_world_t *world, - ecs_entity_t id) +static +void dtor_component( + ecs_world_t *world, + ecs_type_info_t *cdata, + ecs_column_t *column, + ecs_entity_t *entities, + int32_t row, + int32_t count) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - - if (!id) { - return 0; + if (!count) { + return; } + + /* An old component is destructed */ + ecs_xtor_t dtor; + if (cdata && (dtor = cdata->lifecycle.dtor)) { + void *ctx = cdata->lifecycle.ctx; + int16_t size = column->size; + int16_t alignment = column->alignment; - int32_t count = 0; + ecs_assert(column->data != NULL, ECS_INTERNAL_ERROR, NULL); + void *ptr = ecs_vector_get_t(column->data, size, alignment, row); + ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_iter_t it = ecs_term_iter(world, &(ecs_term_t) { .id = id }); - while (ecs_term_next(&it)) { - count += it.count; + dtor(world, cdata->component, &entities[row], ptr, + flecs_to_size_t(size), count, ctx); } - - return count; } -bool ecs_defer_begin( - ecs_world_t *world) +static +void dtor_all_components( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + int32_t row, + int32_t count, + bool update_entity_index, + bool is_delete) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_stage_t *stage = flecs_stage_from_world(&world); - return flecs_defer_none(world, stage); -} + /* Can't delete and not update the entity index */ + ecs_assert(!is_delete || update_entity_index, ECS_INTERNAL_ERROR, NULL); -bool ecs_defer_end( - ecs_world_t *world) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_stage_t *stage = flecs_stage_from_world(&world); - return flecs_defer_flush(world, stage); -} + ecs_record_t **records = ecs_vector_first(data->record_ptrs, ecs_record_t*); + ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); + int32_t i, c, end = row + count; + int32_t column_count = ecs_vector_count(table->storage_type); -static -size_t append_to_str( - char **buffer, - const char *str, - size_t bytes_left, - size_t *required) -{ - char *ptr = NULL; - if (buffer) { - ptr = *buffer; - } + (void)records; - size_t len = strlen(str); - size_t to_write; - if (bytes_left < len) { - to_write = bytes_left; - bytes_left = 0; - } else { - to_write = len; - bytes_left -= len; - } - - if (to_write && ptr) { - ecs_os_memcpy(ptr, str, to_write); - } + /* If table has components with destructors, iterate component columns */ + if (table->flags & EcsTableHasDtors) { + /* Prevent the storage from getting modified while deleting */ + ecs_defer_begin(world); - (*required) += len; + /* Throw up a lock just to be sure */ + table->lock = true; - if (buffer) { - (*buffer) += to_write; - } + /* Iterate entities first, then components. This ensures that only one + * entity is invalidated at a time, which ensures that destructors can + * safely access other entities. */ + for (i = row; i < end; i ++) { + for (c = 0; c < column_count; c++) { + ecs_column_t *column = &data->columns[c]; + dtor_component(world, table->c_info[c], column, entities, i, 1); + } - return bytes_left; -} + /* Update entity index after invoking destructors so that entity can + * be safely used in destructor callbacks. */ + if (update_entity_index) { + ecs_entity_t e = entities[i]; + ecs_assert(!e || ecs_is_valid(world, e), + ECS_INTERNAL_ERROR, NULL); + ecs_assert(!e || records[i] == ecs_eis_get(world, e), + ECS_INTERNAL_ERROR, NULL); + ecs_assert(!e || records[i]->table == table, + ECS_INTERNAL_ERROR, NULL); -const char* ecs_role_str( - ecs_entity_t entity) -{ - if (ECS_HAS_ROLE(entity, PAIR)) { - return "PAIR"; - } else - if (ECS_HAS_ROLE(entity, DISABLED)) { - return "DISABLED"; - } else - if (ECS_HAS_ROLE(entity, XOR)) { - return "XOR"; - } else - if (ECS_HAS_ROLE(entity, OR)) { - return "OR"; - } else - if (ECS_HAS_ROLE(entity, AND)) { - return "AND"; - } else - if (ECS_HAS_ROLE(entity, NOT)) { - return "NOT"; - } else - if (ECS_HAS_ROLE(entity, SWITCH)) { - return "SWITCH"; - } else - if (ECS_HAS_ROLE(entity, CASE)) { - return "CASE"; - } else - if (ECS_HAS_ROLE(entity, OVERRIDE)) { - return "OVERRIDE"; - } else { - return "UNKNOWN"; + if (is_delete) { + ecs_eis_delete(world, e); + ecs_assert(ecs_is_valid(world, e) == false, + ECS_INTERNAL_ERROR, NULL); + } else { + // If this is not a delete, clear the entity index record + ecs_record_t r = {NULL, 0}; + ecs_eis_set(world, e, &r); + } + } else { + /* This should only happen in rare cases, such as when the data + * cleaned up is not part of the world (like with snapshots) */ + } + } + + table->lock = false; + + ecs_defer_end(world); + + /* If table does not have destructors, just update entity index */ + } else if (update_entity_index) { + if (is_delete) { + for (i = row; i < end; i ++) { + ecs_entity_t e = entities[i]; + ecs_assert(!e || ecs_is_valid(world, e), ECS_INTERNAL_ERROR, NULL); + ecs_assert(!e || records[i] == ecs_eis_get(world, e), + ECS_INTERNAL_ERROR, NULL); + ecs_assert(!e || records[i]->table == table, + ECS_INTERNAL_ERROR, NULL); + + ecs_eis_delete(world, e); + ecs_assert(!ecs_is_valid(world, e), ECS_INTERNAL_ERROR, NULL); + } + } else { + for (i = row; i < end; i ++) { + ecs_entity_t e = entities[i]; + ecs_assert(!e || ecs_is_valid(world, e), ECS_INTERNAL_ERROR, NULL); + ecs_assert(!e || records[i] == ecs_eis_get(world, e), + ECS_INTERNAL_ERROR, NULL); + ecs_assert(!e || records[i]->table == table, + ECS_INTERNAL_ERROR, NULL); + ecs_record_t r = {NULL, 0}; + ecs_eis_set(world, e, &r); + } + } } } -size_t ecs_id_str_w_buf( - const ecs_world_t *world, - ecs_id_t id, - char *buffer, - size_t buffer_len) +static +void fini_data( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + bool do_on_remove, + bool update_entity_index, + bool is_delete, + bool deactivate) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - world = ecs_get_world(world); + if (!data) { + return; + } - char *ptr = buffer; - char **pptr = NULL; - if (ptr) { - pptr = &ptr; + if (do_on_remove) { + run_on_remove(world, table, data); } - size_t bytes_left = buffer_len - 1, required = 0; - if (id & ECS_ROLE_MASK && !ECS_HAS_ROLE(id, PAIR)) { - const char *role = ecs_role_str(id); - bytes_left = append_to_str(pptr, role, bytes_left, &required); - bytes_left = append_to_str(pptr, "|", bytes_left, &required); + int32_t count = flecs_table_data_count(data); + if (count) { + dtor_all_components(world, table, data, 0, count, + update_entity_index, is_delete); } - if (ECS_HAS_ROLE(id, PAIR)) { - ecs_entity_t rel = ECS_PAIR_RELATION(id); - ecs_entity_t obj = ECS_PAIR_OBJECT(id); + /* Sanity check */ + ecs_assert(ecs_vector_count(data->record_ptrs) == + ecs_vector_count(data->entities), ECS_INTERNAL_ERROR, NULL); - ecs_entity_t e; - if ((e = ecs_get_alive(world, rel))) { - rel = e; + ecs_column_t *columns = data->columns; + if (columns) { + int32_t c, column_count = ecs_vector_count(table->storage_type); + for (c = 0; c < column_count; c ++) { + /* Sanity check */ + ecs_assert(!columns[c].data || (ecs_vector_count(columns[c].data) == + ecs_vector_count(data->entities)), ECS_INTERNAL_ERROR, NULL); + + ecs_vector_free(columns[c].data); } - if ((e = ecs_get_alive(world, obj))) { - obj = e; + ecs_os_free(columns); + data->columns = NULL; + } + + ecs_sw_column_t *sw_columns = data->sw_columns; + if (sw_columns) { + int32_t c, column_count = table->sw_column_count; + for (c = 0; c < column_count; c ++) { + flecs_switch_free(sw_columns[c].data); } + ecs_os_free(sw_columns); + data->sw_columns = NULL; + } - char *rel_str = ecs_get_fullpath(world, rel); - bytes_left = append_to_str(pptr, "(", bytes_left, &required); - bytes_left = append_to_str(pptr, rel_str, bytes_left, &required); - ecs_os_free(rel_str); - bytes_left = append_to_str(pptr, ",", bytes_left, &required); + ecs_bs_column_t *bs_columns = data->bs_columns; + if (bs_columns) { + int32_t c, column_count = table->bs_column_count; + for (c = 0; c < column_count; c ++) { + flecs_bitset_deinit(&bs_columns[c].data); + } + ecs_os_free(bs_columns); + data->bs_columns = NULL; + } - char *obj_str = ecs_get_fullpath(world, obj); - bytes_left = append_to_str(pptr, obj_str, bytes_left, &required); - ecs_os_free(obj_str); + ecs_vector_free(data->entities); + ecs_vector_free(data->record_ptrs); - append_to_str(pptr, ")", bytes_left, &required); - } else { - ecs_entity_t e = id & ECS_COMPONENT_MASK; - char *path = ecs_get_fullpath(world, e); - append_to_str(pptr, path, bytes_left, &required); - ecs_os_free(path); - } + data->entities = NULL; + data->record_ptrs = NULL; - if (ptr) { - ptr[0] = '\0'; + if (deactivate && count) { + table_activate(world, table, false); } - - return required; } -char* ecs_id_str( - const ecs_world_t *world, - ecs_id_t id) +/* Cleanup, no OnRemove, don't update entity index, don't deactivate table */ +void flecs_table_clear_data( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data) { - size_t size = ecs_id_str_w_buf(world, id, NULL, 0); - char *result = ecs_os_malloc(flecs_from_size_t(size) + 1); - ecs_id_str_w_buf(world, id, result, size + 1); - return result; + fini_data(world, table, data, false, false, false, false); } -static -void flush_bulk_new( +/* Cleanup, no OnRemove, clear entity index, deactivate table */ +void flecs_table_clear_entities_silent( ecs_world_t *world, - ecs_defer_op_t *op) + ecs_table_t *table) { - ecs_entity_t *entities = op->is._n.entities; + fini_data(world, table, &table->storage, false, true, false, true); +} - if (op->id) { - int i, count = op->is._n.count; - for (i = 0; i < count; i ++) { - add_id(world, entities[i], op->id); - } - } +/* Cleanup, run OnRemove, clear entity index, deactivate table */ +void flecs_table_clear_entities( + ecs_world_t *world, + ecs_table_t *table) +{ + fini_data(world, table, &table->storage, true, true, false, true); +} - ecs_os_free(entities); +/* Cleanup, run OnRemove, delete from entity index, deactivate table */ +void flecs_table_delete_entities( + ecs_world_t *world, + ecs_table_t *table) +{ + fini_data(world, table, &table->storage, true, true, true, true); } -static -void free_value( +/* Unset all components in table. This function is called before a table is + * deleted, and invokes all UnSet handlers, if any */ +void flecs_table_remove_actions( ecs_world_t *world, - ecs_entity_t *entities, - ecs_id_t id, - void *value, - int32_t count) + ecs_table_t *table) { - ecs_entity_t real_id = ecs_get_typeid(world, id); - const ecs_type_info_t *info = flecs_get_c_info(world, real_id); - ecs_xtor_t dtor; - - if (info && (dtor = info->lifecycle.dtor)) { - ecs_size_t size = info->size; - void *ptr; - int i; - for (i = 0, ptr = value; i < count; i ++, ptr = ECS_OFFSET(ptr, size)) { - dtor(world, id, &entities[i], ptr, flecs_to_size_t(size), 1, - info->lifecycle.ctx); - } - } + (void)world; + run_on_remove(world, table, &table->storage); } -static -void discard_op( +/* Free table resources. */ +void flecs_table_free( ecs_world_t *world, - ecs_defer_op_t *op) + ecs_table_t *table) { - if (op->kind != EcsOpBulkNew) { - void *value = op->is._1.value; - if (value) { - free_value(world, &op->is._1.entity, op->id, op->is._1.value, 1); - ecs_os_free(value); - } - } else { - ecs_os_free(op->is._n.entities); + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + (void)world; + +#ifndef NDEBUG + char *expr = ecs_type_str(world, table->type); + ecs_dbg_1("table #[green][%s]#[normal] deleted", expr); + ecs_os_free(expr); +#endif + + /* Cleanup data, no OnRemove, delete from entity index, don't deactivate */ + fini_data(world, table, &table->storage, false, true, true, false); + + flecs_table_clear_edges(world, table); + + flecs_unregister_table(world, table); + + ecs_vector_free(table->queries); + ecs_os_free(table->dirty_state); + ecs_os_free(table->storage_map); + + if (table->c_info) { + ecs_os_free(table->c_info); } + + table->id = 0; } -static -bool is_entity_valid( +/* Free table type. Do this separately from freeing the table as types can be + * in use by application destructors. */ +void flecs_table_free_type( + ecs_table_t *table) +{ + ecs_vector_free((ecs_vector_t*)table->type); +} + +/* Reset a table to its initial state. */ +void flecs_table_reset( ecs_world_t *world, - ecs_entity_t e) + ecs_table_t *table) { - if (ecs_exists(world, e) && !ecs_is_alive(world, e)) { - return false; - } - return true; + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + flecs_table_clear_edges(world, table); } static -bool remove_invalid( +void mark_table_dirty( + ecs_table_t *table, + int32_t index) +{ + if (table->dirty_state) { + table->dirty_state[index] ++; + } +} + +void flecs_table_mark_dirty( ecs_world_t *world, - ecs_id_t *id_out) + ecs_table_t *table, + ecs_entity_t component) { - ecs_id_t id = *id_out; + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - if (ECS_HAS_ROLE(id, PAIR)) { - ecs_entity_t rel = ecs_pair_relation(world, id); - if (!rel || !is_entity_valid(world, rel)) { - /* After relation is deleted we can no longer see what its - * delete action was, so pretend this never happened */ - *id_out = 0; - return true; - } else { - ecs_entity_t obj = ecs_pair_object(world, id); - if (!obj || !is_entity_valid(world, obj)) { - /* Check the relation's policy for deleted objects */ - ecs_id_record_t *idr = flecs_get_id_record(world, rel); - if (!idr || (idr->on_delete_object == EcsRemove)) { - *id_out = 0; - return true; - } else { - if (idr->on_delete_object == EcsDelete) { - /* Entity should be deleted, don't bother checking - * other ids */ - return false; - } else if (idr->on_delete_object == EcsThrow) { - /* If policy is throw this object should not have - * been deleted */ - throw_invalid_delete(world, id); - } - } - } - } - - } else { - id &= ECS_COMPONENT_MASK; - if (!is_entity_valid(world, id)) { - /* After relation is deleted we can no longer see what its - * delete action was, so pretend this never happened */ - *id_out = 0; - return true; - } + if (table->dirty_state) { + int32_t index = ecs_type_match(world, table->storage_table, + table->storage_type, 0, component, 0, 0, 0, NULL, NULL); + ecs_assert(index != -1, ECS_INTERNAL_ERROR, NULL); + table->dirty_state[index] ++; } - - return true; } -/* Leave safe section. Run all deferred commands. */ -bool flecs_defer_flush( - ecs_world_t *world, - ecs_stage_t *stage) +static +void move_switch_columns( + ecs_table_t *new_table, + ecs_data_t *new_data, + int32_t new_index, + ecs_table_t *old_table, + ecs_data_t *old_data, + int32_t old_index, + int32_t count) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t i_old = 0, old_column_count = old_table->sw_column_count; + int32_t i_new = 0, new_column_count = new_table->sw_column_count; - if (!--stage->defer) { - /* Set to NULL. Processing deferred commands can cause additional - * commands to get enqueued (as result of reactive systems). Make sure - * that the original array is not reallocated, as this would complicate - * processing the queue. */ - ecs_vector_t *defer_queue = stage->defer_queue; - stage->defer_queue = NULL; + if (!old_column_count || !new_column_count) { + return; + } - if (defer_queue) { - ecs_defer_op_t *ops = ecs_vector_first(defer_queue, ecs_defer_op_t); - int32_t i, count = ecs_vector_count(defer_queue); - - for (i = 0; i < count; i ++) { - ecs_defer_op_t *op = &ops[i]; - ecs_entity_t e = op->is._1.entity; - if (op->kind == EcsOpBulkNew) { - e = 0; - } + ecs_sw_column_t *old_columns = old_data->sw_columns; + ecs_sw_column_t *new_columns = new_data->sw_columns; - /* If entity is no longer alive, this could be because the queue - * contained both a delete and a subsequent add/remove/set which - * should be ignored. */ - if (e && !ecs_is_alive(world, e) && ecs_eis_exists(world, e)) { - ecs_assert(op->kind != EcsOpNew && op->kind != EcsOpClone, - ECS_INTERNAL_ERROR, NULL); - world->discard_count ++; - discard_op(world, op); - continue; - } + ecs_type_t new_type = new_table->type; + ecs_type_t old_type = old_table->type; - switch(op->kind) { - case EcsOpNew: - case EcsOpAdd: - ecs_assert(op->id != 0, ECS_INTERNAL_ERROR, NULL); - if (remove_invalid(world, &op->id)) { - if (op->id) { - world->add_count ++; - add_id(world, e, op->id); - } - } else { - ecs_delete(world, e); - } - break; - case EcsOpRemove: - remove_id(world, e, op->id); - break; - case EcsOpClone: - ecs_clone(world, e, op->id, op->is._1.clone_value); - break; - case EcsOpSet: - assign_ptr_w_id(world, e, - op->id, flecs_to_size_t(op->is._1.size), - op->is._1.value, true, true); - break; - case EcsOpMut: - assign_ptr_w_id(world, e, - op->id, flecs_to_size_t(op->is._1.size), - op->is._1.value, true, false); - break; - case EcsOpModified: - ecs_modified_id(world, e, op->id); - break; - case EcsOpDelete: { - ecs_delete(world, e); - break; - } - case EcsOpClear: - ecs_clear(world, e); - break; - case EcsOpOnDeleteAction: - on_delete_action(world, op->id, e); - break; - case EcsOpEnable: - ecs_enable_component_w_id(world, e, op->id, true); - break; - case EcsOpDisable: - ecs_enable_component_w_id(world, e, op->id, false); - break; - case EcsOpBulkNew: - flush_bulk_new(world, op); - continue; - } + int32_t offset_new = new_table->sw_column_offset; + int32_t offset_old = old_table->sw_column_offset; - if (op->is._1.value) { - ecs_os_free(op->is._1.value); - } - } + ecs_id_t *new_ids = ecs_vector_first(new_type, ecs_id_t); + ecs_id_t *old_ids = ecs_vector_first(old_type, ecs_id_t); - if (stage->defer_queue) { - ecs_vector_free(stage->defer_queue); - } + for (; (i_new < new_column_count) && (i_old < old_column_count);) { + ecs_entity_t new_id = new_ids[i_new + offset_new]; + ecs_entity_t old_id = old_ids[i_old + offset_old]; - /* Restore defer queue */ - ecs_vector_clear(defer_queue); - stage->defer_queue = defer_queue; + if (new_id == old_id) { + ecs_switch_t *old_switch = old_columns[i_old].data; + ecs_switch_t *new_switch = new_columns[i_new].data; + + flecs_switch_ensure(new_switch, new_index + count); + + int i; + for (i = 0; i < count; i ++) { + uint64_t value = flecs_switch_get(old_switch, old_index + i); + flecs_switch_set(new_switch, new_index + i, value); + } } - return true; + i_new += new_id <= old_id; + i_old += new_id >= old_id; } - - return false; } -/* Delete operations from queue without executing them. */ -bool flecs_defer_purge( - ecs_world_t *world, - ecs_stage_t *stage) +static +void move_bitset_columns( + ecs_table_t *new_table, + ecs_data_t *new_data, + int32_t new_index, + ecs_table_t *old_table, + ecs_data_t *old_data, + int32_t old_index, + int32_t count) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t i_old = 0, old_column_count = old_table->bs_column_count; + int32_t i_new = 0, new_column_count = new_table->bs_column_count; - if (!--stage->defer) { - ecs_vector_t *defer_queue = stage->defer_queue; - stage->defer_queue = NULL; + if (!old_column_count || !new_column_count) { + return; + } - if (defer_queue) { - ecs_defer_op_t *ops = ecs_vector_first(defer_queue, ecs_defer_op_t); - int32_t i, count = ecs_vector_count(defer_queue); - for (i = 0; i < count; i ++) { - discard_op(world, &ops[i]); - } + ecs_bs_column_t *old_columns = old_data->bs_columns; + ecs_bs_column_t *new_columns = new_data->bs_columns; - if (stage->defer_queue) { - ecs_vector_free(stage->defer_queue); - } + ecs_type_t new_type = new_table->type; + ecs_type_t old_type = old_table->type; - /* Restore defer queue */ - ecs_vector_clear(defer_queue); - stage->defer_queue = defer_queue; - } + int32_t offset_new = new_table->bs_column_offset; + int32_t offset_old = old_table->bs_column_offset; - return true; - } + ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); + ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); - return false; -} + for (; (i_new < new_column_count) && (i_old < old_column_count);) { + ecs_entity_t new_component = new_components[i_new + offset_new]; + ecs_entity_t old_component = old_components[i_old + offset_old]; -static -ecs_defer_op_t* new_defer_op(ecs_stage_t *stage) { - ecs_defer_op_t *result = ecs_vector_add(&stage->defer_queue, ecs_defer_op_t); - ecs_os_memset(result, 0, ECS_SIZEOF(ecs_defer_op_t)); - return result; + if (new_component == old_component) { + ecs_bitset_t *old_bs = &old_columns[i_old].data; + ecs_bitset_t *new_bs = &new_columns[i_new].data; + + flecs_bitset_ensure(new_bs, new_index + count); + + int i; + for (i = 0; i < count; i ++) { + uint64_t value = flecs_bitset_get(old_bs, old_index + i); + flecs_bitset_set(new_bs, new_index + i, value); + } + } + + i_new += new_component <= old_component; + i_old += new_component >= old_component; + } } static -bool defer_add_remove( +void grow_column( ecs_world_t *world, - ecs_stage_t *stage, - ecs_defer_op_kind_t op_kind, - ecs_entity_t entity, - ecs_id_t id) + ecs_entity_t *entities, + ecs_column_t *column, + ecs_type_info_t *c_info, + int32_t to_add, + int32_t new_size, + bool construct) { - if (stage->defer) { - if (!id) { - return true; + ecs_vector_t *vec = column->data; + int16_t alignment = column->alignment; + + int32_t size = column->size; + int32_t count = ecs_vector_count(vec); + int32_t old_size = ecs_vector_size(vec); + int32_t new_count = count + to_add; + bool can_realloc = new_size != old_size; + + ecs_assert(new_size >= new_count, ECS_INTERNAL_ERROR, NULL); + + /* If the array could possibly realloc and the component has a move action + * defined, move old elements manually */ + ecs_move_t move; + if (c_info && count && can_realloc && (move = c_info->lifecycle.move)) { + ecs_xtor_t ctor = c_info->lifecycle.ctor; + ecs_assert(ctor != NULL, ECS_INTERNAL_ERROR, NULL); + + /* Create new vector */ + ecs_vector_t *new_vec = ecs_vector_new_t(size, alignment, new_size); + ecs_vector_set_count_t(&new_vec, size, alignment, new_count); + + void *old_buffer = ecs_vector_first_t( + vec, size, alignment); + + void *new_buffer = ecs_vector_first_t( + new_vec, size, alignment); + + /* First construct elements (old and new) in new buffer */ + ctor(world, c_info->component, entities, new_buffer, + flecs_to_size_t(size), construct ? new_count : count, + c_info->lifecycle.ctx); + + /* Move old elements */ + move(world, c_info->component, entities, entities, + new_buffer, old_buffer, flecs_to_size_t(size), count, + c_info->lifecycle.ctx); + + /* Free old vector */ + ecs_vector_free(vec); + column->data = new_vec; + } else { + /* If array won't realloc or has no move, simply add new elements */ + if (can_realloc) { + ecs_vector_set_size_t(&vec, size, alignment, new_size); } - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = op_kind; - op->id = id; - op->is._1.entity = entity; + void *elem = ecs_vector_addn_t(&vec, size, alignment, to_add); - if (op_kind == EcsOpNew) { - world->new_count ++; - } else if (op_kind == EcsOpAdd) { - world->add_count ++; - } else if (op_kind == EcsOpRemove) { - world->remove_count ++; + ecs_xtor_t ctor; + if (construct && c_info && (ctor = c_info->lifecycle.ctor)) { + /* If new elements need to be constructed and component has a + * constructor, construct */ + ctor(world, c_info->component, &entities[count], elem, + flecs_to_size_t(size), to_add, c_info->lifecycle.ctx); } - return true; - } else { - stage->defer ++; + column->data = vec; } - - return false; + + ecs_assert(ecs_vector_size(column->data) == new_size, + ECS_INTERNAL_ERROR, NULL); } static -void merge_stages( +int32_t grow_data( ecs_world_t *world, - bool force_merge) + ecs_table_t *table, + ecs_data_t *data, + int32_t to_add, + int32_t size, + const ecs_entity_t *ids) { - bool is_stage = ecs_poly_is(world, ecs_stage_t); - ecs_stage_t *stage = flecs_stage_from_world(&world); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); - bool measure_frame_time = world->measure_frame_time; + int32_t cur_count = flecs_table_data_count(data); + int32_t column_count = ecs_vector_count(table->storage_type); + int32_t sw_column_count = table->sw_column_count; + int32_t bs_column_count = table->bs_column_count; + ecs_column_t *columns = data->columns; + ecs_sw_column_t *sw_columns = data->sw_columns; + ecs_bs_column_t *bs_columns = data->bs_columns; - ecs_time_t t_start; - if (measure_frame_time) { - ecs_os_get_time(&t_start); + /* Add record to record ptr array */ + ecs_vector_set_size(&data->record_ptrs, ecs_record_t*, size); + ecs_record_t **r = ecs_vector_addn(&data->record_ptrs, ecs_record_t*, to_add); + ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL); + if (ecs_vector_size(data->record_ptrs) > size) { + size = ecs_vector_size(data->record_ptrs); } - if (is_stage) { - /* Check for consistency if force_merge is enabled. In practice this - * function will never get called with force_merge disabled for just - * a single stage. */ - if (force_merge || stage->auto_merge) { - ecs_defer_end((ecs_world_t*)stage); + /* Add entity to column with entity ids */ + ecs_vector_set_size(&data->entities, ecs_entity_t, size); + ecs_entity_t *e = ecs_vector_addn(&data->entities, ecs_entity_t, to_add); + ecs_assert(e != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ecs_vector_size(data->entities) == size, ECS_INTERNAL_ERROR, NULL); + + /* Initialize entity ids and record ptrs */ + int32_t i; + if (ids) { + for (i = 0; i < to_add; i ++) { + e[i] = ids[i]; } } else { - /* Merge stages. Only merge if the stage has auto_merging turned on, or - * if this is a forced merge (like when ecs_merge is called) */ - int32_t i, count = ecs_get_stage_count(world); - for (i = 0; i < count; i ++) { - ecs_stage_t *s = (ecs_stage_t*)ecs_get_stage(world, i); - ecs_poly_assert(s, ecs_stage_t); - if (force_merge || s->auto_merge) { - ecs_defer_end((ecs_world_t*)s); - } + ecs_os_memset(e, 0, ECS_SIZEOF(ecs_entity_t) * to_add); + } + ecs_os_memset(r, 0, ECS_SIZEOF(ecs_record_t*) * to_add); + + /* Add elements to each column array */ + ecs_type_info_t **c_info_array = table->c_info; + ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); + for (i = 0; i < column_count; i ++) { + ecs_column_t *column = &columns[i]; + ecs_assert(column->size != 0, ECS_INTERNAL_ERROR, NULL); + + ecs_type_info_t *c_info = NULL; + if (c_info_array) { + c_info = c_info_array[i]; } + + grow_column(world, entities, column, c_info, to_add, size, true); + ecs_assert(ecs_vector_size(columns[i].data) == size, + ECS_INTERNAL_ERROR, NULL); } - flecs_eval_component_monitors(world); + /* Add elements to each switch column */ + for (i = 0; i < sw_column_count; i ++) { + ecs_switch_t *sw = sw_columns[i].data; + flecs_switch_addn(sw, to_add); + } - if (measure_frame_time) { - world->stats.merge_time_total += - (FLECS_FLOAT)ecs_time_measure(&t_start); + /* Add elements to each bitset column */ + for (i = 0; i < bs_column_count; i ++) { + ecs_bitset_t *bs = &bs_columns[i].data; + flecs_bitset_addn(bs, to_add); } - world->stats.merge_count_total ++; + /* If the table is monitored indicate that there has been a change */ + mark_table_dirty(table, 0); - /* If stage is asynchronous, deferring is always enabled */ - if (stage->asynchronous) { - ecs_defer_begin((ecs_world_t*)stage); + if (!world->is_readonly && !cur_count) { + table_activate(world, table, true); } -} -static -void do_auto_merge( - ecs_world_t *world) -{ - merge_stages(world, false); -} + table->alloc_count ++; -static -void do_manual_merge( - ecs_world_t *world) -{ - merge_stages(world, true); + /* Return index of first added entity */ + return cur_count; } -bool flecs_defer_none( - ecs_world_t *world, - ecs_stage_t *stage) +static +void fast_append( + ecs_column_t *columns, + int32_t column_count) { - (void)world; - return (++ stage->defer) == 1; + /* Add elements to each column array */ + int32_t i; + for (i = 0; i < column_count; i ++) { + ecs_column_t *column = &columns[i]; + int16_t size = column->size; + if (size) { + int16_t alignment = column->alignment; + ecs_vector_add_t(&column->data, size, alignment); + } + } } -bool flecs_defer_modified( +int32_t flecs_table_append( ecs_world_t *world, - ecs_stage_t *stage, + ecs_table_t *table, + ecs_data_t *data, ecs_entity_t entity, - ecs_id_t id) + ecs_record_t *record, + bool construct) { - (void)world; - if (stage->defer) { - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = EcsOpModified; - op->id = id; - op->is._1.entity = entity; - return true; - } else { - stage->defer ++; - } - - return false; -} + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); -bool flecs_defer_clone( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t entity, - ecs_entity_t src, - bool clone_value) -{ - (void)world; - if (stage->defer) { - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = EcsOpClone; - op->id = src; - op->is._1.entity = entity; - op->is._1.clone_value = clone_value; - return true; - } else { - stage->defer ++; - } + /* Get count & size before growing entities array. This tells us whether the + * arrays will realloc */ + int32_t count = ecs_vector_count(data->entities); + int32_t size = ecs_vector_size(data->entities); + int32_t column_count = ecs_vector_count(table->storage_type); + ecs_column_t *columns = table->storage.columns; - return false; -} + /* Grow buffer with entity ids, set new element to new entity */ + ecs_entity_t *e = ecs_vector_add(&data->entities, ecs_entity_t); + ecs_assert(e != NULL, ECS_INTERNAL_ERROR, NULL); + *e = entity; -bool flecs_defer_delete( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t entity) -{ - (void)world; - if (stage->defer) { - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = EcsOpDelete; - op->is._1.entity = entity; - world->delete_count ++; - return true; - } else { - stage->defer ++; + /* Keep track of alloc count. This allows references to check if cached + * pointers need to be updated. */ + table->alloc_count += (count == size); + + /* Add record ptr to array with record ptrs */ + ecs_record_t **r = ecs_vector_add(&data->record_ptrs, ecs_record_t*); + ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL); + *r = record; + + /* If the table is monitored indicate that there has been a change */ + mark_table_dirty(table, 0); + + /* If this is the first entity in this table, signal queries so that the + * table moves from an inactive table to an active table. */ + if (!world->is_readonly && !count) { + table_activate(world, table, true); + } + + ecs_assert(count >= 0, ECS_INTERNAL_ERROR, NULL); + + /* Fast path: no switch columns, no lifecycle actions */ + if (!(table->flags & EcsTableIsComplex)) { + fast_append(columns, column_count); + return count; } - return false; -} -bool flecs_defer_clear( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t entity) -{ - (void)world; - if (stage->defer) { - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = EcsOpClear; - op->is._1.entity = entity; - world->clear_count ++; - return true; - } else { - stage->defer ++; + int32_t sw_column_count = table->sw_column_count; + int32_t bs_column_count = table->bs_column_count; + ecs_sw_column_t *sw_columns = table->storage.sw_columns; + ecs_bs_column_t *bs_columns = table->storage.bs_columns; + + ecs_type_info_t **c_info_array = table->c_info; + ecs_entity_t *entities = ecs_vector_first( + data->entities, ecs_entity_t); + + /* Reobtain size to ensure that the columns have the same size as the + * entities and record vectors. This keeps reasoning about when allocations + * occur easier. */ + size = ecs_vector_size(data->entities); + + /* Grow component arrays with 1 element */ + int32_t i; + for (i = 0; i < column_count; i ++) { + ecs_column_t *column = &columns[i]; + ecs_assert(column->size != 0, ECS_INTERNAL_ERROR, NULL); + + ecs_type_info_t *c_info = NULL; + if (c_info_array) { + c_info = c_info_array[i]; + } + + grow_column(world, entities, column, c_info, 1, size, construct); + + ecs_assert( + ecs_vector_size(columns[i].data) == ecs_vector_size(data->entities), + ECS_INTERNAL_ERROR, NULL); + + ecs_assert( + ecs_vector_count(columns[i].data) == ecs_vector_count(data->entities), + ECS_INTERNAL_ERROR, NULL); } - return false; + + /* Add element to each switch column */ + for (i = 0; i < sw_column_count; i ++) { + ecs_assert(sw_columns != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_switch_t *sw = sw_columns[i].data; + flecs_switch_add(sw); + } + + /* Add element to each bitset column */ + for (i = 0; i < bs_column_count; i ++) { + ecs_assert(bs_columns != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_bitset_t *bs = &bs_columns[i].data; + flecs_bitset_addn(bs, 1); + } + + return count; } -bool flecs_defer_on_delete_action( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_id_t id, - ecs_entity_t action) +static +void fast_delete_last( + ecs_column_t *columns, + int32_t column_count) { - (void)world; - if (stage->defer) { - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = EcsOpOnDeleteAction; - op->id = id; - op->is._1.entity = action; - world->clear_count ++; - return true; - } else { - stage->defer ++; + int i; + for (i = 0; i < column_count; i ++) { + ecs_column_t *column = &columns[i]; + ecs_vector_remove_last(column->data); } - return false; } -bool flecs_defer_enable( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t entity, - ecs_id_t id, - bool enable) +static +void fast_delete( + ecs_column_t *columns, + int32_t column_count, + int32_t index) { - (void)world; - if (stage->defer) { - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = enable ? EcsOpEnable : EcsOpDisable; - op->is._1.entity = entity; - op->id = id; - return true; - } else { - stage->defer ++; + int i; + for (i = 0; i < column_count; i ++) { + ecs_column_t *column = &columns[i]; + int16_t size = column->size; + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + + int16_t alignment = column->alignment; + ecs_vector_remove_t(column->data, size, alignment, index); } - return false; } -bool flecs_defer_bulk_new( +void flecs_table_delete( ecs_world_t *world, - ecs_stage_t *stage, - int32_t count, - ecs_id_t id, - const ecs_entity_t **ids_out) + ecs_table_t *table, + ecs_data_t *data, + int32_t index, + bool destruct) { - if (stage->defer) { - ecs_entity_t *ids = ecs_os_malloc(count * ECS_SIZEOF(ecs_entity_t)); - world->bulk_new_count ++; + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - /* Use ecs_new_id as this is thread safe */ - int i; - for (i = 0; i < count; i ++) { - ids[i] = ecs_new_id(world); - } + ecs_vector_t *v_entities = data->entities; + int32_t count = ecs_vector_count(v_entities); - *ids_out = ids; + ecs_assert(count > 0, ECS_INTERNAL_ERROR, NULL); + count --; + ecs_assert(index <= count, ECS_INTERNAL_ERROR, NULL); - /* Store data in op */ - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = EcsOpBulkNew; - op->id = id; - op->is._n.entities = ids; - op->is._n.count = count; + /* Move last entity id to index */ + ecs_entity_t *entities = ecs_vector_first(v_entities, ecs_entity_t); + ecs_entity_t entity_to_move = entities[count]; + ecs_entity_t entity_to_delete = entities[index]; + entities[index] = entity_to_move; + ecs_vector_remove_last(v_entities); - return true; - } else { - stage->defer ++; - } + /* Move last record ptr to index */ + ecs_vector_t *v_records = data->record_ptrs; + ecs_assert(count < ecs_vector_count(v_records), ECS_INTERNAL_ERROR, NULL); - return false; -} + ecs_record_t **records = ecs_vector_first(v_records, ecs_record_t*); + ecs_record_t *record_to_move = records[count]; + records[index] = record_to_move; + ecs_vector_remove_last(v_records); -bool flecs_defer_new( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t entity, - ecs_id_t id) -{ - return defer_add_remove(world, stage, EcsOpNew, entity, id); -} + /* Update record of moved entity in entity index */ + if (index != count) { + if (record_to_move) { + if (record_to_move->row >= 0) { + record_to_move->row = index + 1; + } else { + record_to_move->row = -(index + 1); + } + ecs_assert(record_to_move->table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(record_to_move->table == table, ECS_INTERNAL_ERROR, NULL); + } + } -bool flecs_defer_add( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t entity, - ecs_id_t id) -{ - return defer_add_remove(world, stage, EcsOpAdd, entity, id); -} + /* If the table is monitored indicate that there has been a change */ + mark_table_dirty(table, 0); -bool flecs_defer_remove( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t entity, - ecs_id_t id) -{ - return defer_add_remove(world, stage, EcsOpRemove, entity, id); -} + /* If table is empty, deactivate it */ + if (!count) { + table_activate(world, table, false); + } -bool flecs_defer_set( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_defer_op_kind_t op_kind, - ecs_entity_t entity, - ecs_id_t id, - ecs_size_t size, - const void *value, - void **value_out, - bool *is_added) -{ - if (stage->defer) { - world->set_count ++; - if (!size) { - const EcsComponent *cptr = flecs_component_from_id(world, id); - ecs_assert(cptr != NULL, ECS_INVALID_PARAMETER, NULL); - size = cptr->size; + /* Destruct component data */ + ecs_type_info_t **c_info_array = table->c_info; + ecs_column_t *columns = data->columns; + int32_t column_count = ecs_vector_count(table->storage_type); + int32_t i; + + /* If this is a table without lifecycle callbacks or special columns, take + * fast path that just remove an element from the array(s) */ + if (!(table->flags & EcsTableIsComplex)) { + if (index == count) { + fast_delete_last(columns, column_count); + } else { + fast_delete(columns, column_count, index); } - ecs_defer_op_t *op = new_defer_op(stage); - op->kind = op_kind; - op->id = id; - op->is._1.entity = entity; - op->is._1.size = size; - op->is._1.value = ecs_os_malloc(size); + return; + } - if (!value) { - value = ecs_get_id(world, entity, id); - if (is_added) { - *is_added = value == NULL; + /* Last element, destruct & remove */ + if (index == count) { + /* If table has component destructors, invoke */ + if (destruct && (table->flags & EcsTableHasDtors)) { + ecs_assert(c_info_array != NULL, ECS_INTERNAL_ERROR, NULL); + + for (i = 0; i < column_count; i ++) { + ecs_type_info_t *c_info = c_info_array[i]; + ecs_xtor_t dtor; + if (c_info && (dtor = c_info->lifecycle.dtor)) { + ecs_size_t size = c_info->size; + ecs_size_t alignment = c_info->alignment; + dtor(world, c_info->component, &entity_to_delete, + ecs_vector_last_t(columns[i].data, size, alignment), + flecs_to_size_t(size), 1, c_info->lifecycle.ctx); + } } } - const ecs_type_info_t *c_info = NULL; - ecs_entity_t real_id = ecs_get_typeid(world, id); - if (real_id) { - c_info = flecs_get_c_info(world, real_id); - } + fast_delete_last(columns, column_count); - if (value) { - ecs_copy_ctor_t copy; - if (c_info && (copy = c_info->lifecycle.copy_ctor)) { - copy(world, id, &c_info->lifecycle, &entity, &entity, - op->is._1.value, value, flecs_to_size_t(size), 1, - c_info->lifecycle.ctx); - } else { - ecs_os_memcpy(op->is._1.value, value, size); - } - } else { - ecs_xtor_t ctor; - if (c_info && (ctor = c_info->lifecycle.ctor)) { - ctor(world, id, &entity, op->is._1.value, - flecs_to_size_t(size), 1, c_info->lifecycle.ctx); + /* Not last element, move last element to deleted element & destruct */ + } else { + /* If table has component destructors, invoke */ + if (destruct && (table->flags & (EcsTableHasDtors | EcsTableHasMove))) { + ecs_assert(c_info_array != NULL, ECS_INTERNAL_ERROR, NULL); + + for (i = 0; i < column_count; i ++) { + ecs_column_t *column = &columns[i]; + ecs_size_t size = column->size; + ecs_size_t align = column->alignment; + ecs_vector_t *vec = column->data; + void *dst = ecs_vector_get_t(vec, size, align, index); + void *src = ecs_vector_last_t(vec, size, align); + + ecs_type_info_t *c_info = c_info_array[i]; + ecs_move_ctor_t move_dtor; + if (c_info && (move_dtor = c_info->lifecycle.move_dtor)) { + move_dtor(world, c_info->component, &c_info->lifecycle, + &entity_to_move, &entity_to_delete, dst, src, + flecs_to_size_t(size), 1, c_info->lifecycle.ctx); + } else { + ecs_os_memcpy(dst, src, size); + } + + ecs_vector_remove_last(vec); } - } - if (value_out) { - *value_out = op->is._1.value; + } else { + fast_delete(columns, column_count, index); } + } - return true; - } else { - stage->defer ++; + /* Remove elements from switch columns */ + ecs_sw_column_t *sw_columns = data->sw_columns; + int32_t sw_column_count = table->sw_column_count; + for (i = 0; i < sw_column_count; i ++) { + flecs_switch_remove(sw_columns[i].data, index); } - return false; + /* Remove elements from bitset columns */ + ecs_bs_column_t *bs_columns = data->bs_columns; + int32_t bs_column_count = table->bs_column_count; + for (i = 0; i < bs_column_count; i ++) { + flecs_bitset_remove(&bs_columns[i].data, index); + } } -void flecs_stage_merge_post_frame( - ecs_world_t *world, - ecs_stage_t *stage) +static +void fast_move( + ecs_table_t *new_table, + ecs_data_t *new_data, + int32_t new_index, + ecs_table_t *old_table, + ecs_data_t *old_data, + int32_t old_index) { - /* Execute post frame actions */ - ecs_vector_each(stage->post_frame_actions, ecs_action_elem_t, action, { - action->action(world, action->ctx); - }); + ecs_type_t new_type = new_table->storage_type; + ecs_type_t old_type = old_table->storage_type; - ecs_vector_free(stage->post_frame_actions); - stage->post_frame_actions = NULL; -} + int32_t i_new = 0, new_column_count = ecs_vector_count(new_table->storage_type); + int32_t i_old = 0, old_column_count = ecs_vector_count(old_table->storage_type); + ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); + ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); -void flecs_stage_init( - ecs_world_t *world, - ecs_stage_t *stage) -{ - ecs_poly_assert(world, ecs_world_t); + ecs_column_t *old_columns = old_data->columns; + ecs_column_t *new_columns = new_data->columns; - ecs_poly_init(stage, ecs_stage_t); - stage->world = world; - stage->thread_ctx = world; - stage->auto_merge = true; - stage->asynchronous = false; -} + for (; (i_new < new_column_count) && (i_old < old_column_count);) { + ecs_entity_t new_component = new_components[i_new]; + ecs_entity_t old_component = old_components[i_old]; -void flecs_stage_deinit( - ecs_world_t *world, - ecs_stage_t *stage) -{ - (void)world; - ecs_poly_assert(world, ecs_world_t); - ecs_poly_assert(stage, ecs_stage_t); + if (new_component == old_component) { + ecs_column_t *new_column = &new_columns[i_new]; + ecs_column_t *old_column = &old_columns[i_old]; + int16_t size = new_column->size; + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); - /* Make sure stage has no unmerged data */ - ecs_assert(ecs_vector_count(stage->defer_queue) == 0, - ECS_INVALID_PARAMETER, NULL); + int16_t alignment = new_column->alignment; + void *dst = ecs_vector_get_t( + new_column->data, size, alignment, new_index); + void *src = ecs_vector_get_t( + old_column->data, size, alignment, old_index); - ecs_poly_fini(stage, ecs_stage_t); + ecs_assert(dst != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(src != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_os_memcpy(dst, src, size); + } - ecs_vector_free(stage->defer_queue); + i_new += new_component <= old_component; + i_old += new_component >= old_component; + } } -void ecs_set_stages( +void flecs_table_move( ecs_world_t *world, - int32_t stage_count) + ecs_entity_t dst_entity, + ecs_entity_t src_entity, + ecs_table_t *new_table, + ecs_data_t *new_data, + int32_t new_index, + ecs_table_t *old_table, + ecs_data_t *old_data, + int32_t old_index, + bool construct) { - ecs_poly_assert(world, ecs_world_t); - - ecs_stage_t *stages; - int32_t i, count = ecs_vector_count(world->worker_stages); + ecs_assert(new_table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(old_table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!new_table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_assert(!old_table->lock, ECS_LOCKED_STORAGE, NULL); - if (count && count != stage_count) { - stages = ecs_vector_first(world->worker_stages, ecs_stage_t); + ecs_assert(old_index >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(new_index >= 0, ECS_INTERNAL_ERROR, NULL); - for (i = 0; i < count; i ++) { - /* If stage contains a thread handle, ecs_set_threads was used to - * create the stages. ecs_set_threads and ecs_set_stages should not - * be mixed. */ - ecs_poly_assert(&stages[i], ecs_stage_t); - ecs_assert(stages[i].thread == 0, ECS_INVALID_OPERATION, NULL); - flecs_stage_deinit(world, &stages[i]); - } + ecs_assert(old_data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(new_data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_vector_free(world->worker_stages); + if (!((new_table->flags | old_table->flags) & EcsTableIsComplex)) { + fast_move(new_table, new_data, new_index, old_table, old_data, old_index); + return; } - - if (stage_count) { - world->worker_stages = ecs_vector_new(ecs_stage_t, stage_count); - for (i = 0; i < stage_count; i ++) { - ecs_stage_t *stage = ecs_vector_add( - &world->worker_stages, ecs_stage_t); - flecs_stage_init(world, stage); - stage->id = 1 + i; /* 0 is reserved for main/temp stage */ + move_switch_columns( + new_table, new_data, new_index, old_table, old_data, old_index, 1); - /* Set thread_ctx to stage, as this stage might be used in a - * multithreaded context */ - stage->thread_ctx = (ecs_world_t*)stage; - } - } else { - /* Set to NULL to prevent double frees */ - world->worker_stages = NULL; - } + move_bitset_columns( + new_table, new_data, new_index, old_table, old_data, old_index, 1); - /* Regardless of whether the stage was just initialized or not, when the - * ecs_set_stages function is called, all stages inherit the auto_merge - * property from the world */ - for (i = 0; i < stage_count; i ++) { - ecs_stage_t *stage = (ecs_stage_t*)ecs_get_stage(world, i); - stage->auto_merge = world->stage.auto_merge; - } -} - -int32_t ecs_get_stage_count( - const ecs_world_t *world) -{ - world = ecs_get_world(world); - return ecs_vector_count(world->worker_stages); -} - -int32_t ecs_get_stage_id( - const ecs_world_t *world) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + bool same_entity = dst_entity == src_entity; - if (ecs_poly_is(world, ecs_stage_t)) { - ecs_stage_t *stage = (ecs_stage_t*)world; + ecs_type_t new_type = new_table->storage_type; + ecs_type_t old_type = old_table->storage_type; - /* Index 0 is reserved for main stage */ - return stage->id - 1; - } else if (ecs_poly_is(world, ecs_world_t)) { - return 0; - } else { - ecs_abort(ECS_INTERNAL_ERROR, NULL); - } -} + int32_t i_new = 0, new_column_count = ecs_vector_count(new_table->storage_type); + int32_t i_old = 0, old_column_count = ecs_vector_count(old_table->storage_type); + ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); + ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); -ecs_world_t* ecs_get_stage( - const ecs_world_t *world, - int32_t stage_id) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(ecs_vector_count(world->worker_stages) > stage_id, - ECS_INVALID_PARAMETER, NULL); + ecs_column_t *old_columns = old_data->columns; + ecs_column_t *new_columns = new_data->columns; - return (ecs_world_t*)ecs_vector_get( - world->worker_stages, ecs_stage_t, stage_id); -} + for (; (i_new < new_column_count) && (i_old < old_column_count);) { + ecs_entity_t new_component = new_components[i_new]; + ecs_entity_t old_component = old_components[i_old]; -bool ecs_staging_begin( - ecs_world_t *world) -{ - ecs_poly_assert(world, ecs_world_t); + if (new_component == old_component) { + ecs_column_t *new_column = &new_columns[i_new]; + ecs_column_t *old_column = &old_columns[i_old]; + int16_t size = new_column->size; + int16_t alignment = new_column->alignment; - int32_t i, count = ecs_get_stage_count(world); - for (i = 0; i < count; i ++) { - ecs_defer_begin(ecs_get_stage(world, i)); - } + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); - bool is_readonly = world->is_readonly; + void *dst = ecs_vector_get_t( + new_column->data, size, alignment, new_index); + void *src = ecs_vector_get_t( + old_column->data, size, alignment, old_index); - /* From this point on, the world is "locked" for mutations, and it is only - * allowed to enqueue commands from stages */ - world->is_readonly = true; + ecs_assert(dst != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(src != NULL, ECS_INTERNAL_ERROR, NULL); - return is_readonly; -} + ecs_type_info_t *cdata = new_table->c_info[i_new]; + if (same_entity) { + ecs_move_ctor_t callback; + if (cdata && (callback = cdata->lifecycle.ctor_move_dtor)) { + void *ctx = cdata->lifecycle.ctx; + /* ctor + move + dtor */ + callback(world, new_component, &cdata->lifecycle, + &dst_entity, &src_entity, + dst, src, flecs_to_size_t(size), 1, ctx); + } else { + ecs_os_memcpy(dst, src, size); + } + } else { + ecs_copy_ctor_t copy; + if (cdata && (copy = cdata->lifecycle.copy_ctor)) { + void *ctx = cdata->lifecycle.ctx; + copy(world, new_component, &cdata->lifecycle, + &dst_entity, &src_entity, + dst, src, flecs_to_size_t(size), 1, ctx); + } else { + ecs_os_memcpy(dst, src, size); + } + } + } else { + if (new_component < old_component) { + if (construct) { + ctor_component(world, new_table->c_info[i_new], + &new_columns[i_new], &dst_entity, new_index, 1); + } + } else { + dtor_component(world, old_table->c_info[i_old], + &old_columns[i_old], &src_entity, old_index, 1); + } + } -void ecs_staging_end( - ecs_world_t *world) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(world->is_readonly == true, ECS_INVALID_OPERATION, NULL); + i_new += new_component <= old_component; + i_old += new_component >= old_component; + } - /* After this it is safe again to mutate the world directly */ - world->is_readonly = false; + if (construct) { + for (; (i_new < new_column_count); i_new ++) { + ctor_component(world, new_table->c_info[i_new], + &new_columns[i_new], &dst_entity, new_index, 1); + } + } - do_auto_merge(world); + for (; (i_old < old_column_count); i_old ++) { + dtor_component(world, old_table->c_info[i_old], + &old_columns[i_old], &src_entity, old_index, 1); + } } -void ecs_merge( - ecs_world_t *world) +int32_t flecs_table_appendn( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + int32_t to_add, + const ecs_entity_t *ids) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_poly_is(world, ecs_world_t) || - ecs_poly_is(world, ecs_stage_t), ECS_INVALID_PARAMETER, NULL); - do_manual_merge(world); + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + + int32_t cur_count = flecs_table_data_count(data); + return grow_data(world, table, data, to_add, cur_count + to_add, ids); } -void ecs_set_automerge( +void flecs_table_set_size( ecs_world_t *world, - bool auto_merge) + ecs_table_t *table, + ecs_data_t *data, + int32_t size) { - /* If a world is provided, set auto_merge globally for the world. This - * doesn't actually do anything (the main stage never merges) but it serves - * as the default for when stages are created. */ - if (ecs_poly_is(world, ecs_world_t)) { - world->stage.auto_merge = auto_merge; + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - /* Propagate change to all stages */ - int i, stage_count = ecs_get_stage_count(world); - for (i = 0; i < stage_count; i ++) { - ecs_stage_t *stage = (ecs_stage_t*)ecs_get_stage(world, i); - stage->auto_merge = auto_merge; - } + int32_t cur_count = flecs_table_data_count(data); - /* If a stage is provided, override the auto_merge value for the individual - * stage. This allows an application to control per-stage which stage should - * be automatically merged and which one shouldn't */ - } else { - ecs_poly_assert(world, ecs_stage_t); - ecs_stage_t *stage = (ecs_stage_t*)world; - stage->auto_merge = auto_merge; + if (cur_count < size) { + grow_data(world, table, data, 0, size, NULL); } } -bool ecs_stage_is_readonly( - const ecs_world_t *stage) +int32_t flecs_table_data_count( + const ecs_data_t *data) { - const ecs_world_t *world = ecs_get_world(stage); + return data ? ecs_vector_count(data->entities) : 0; +} - if (ecs_poly_is(stage, ecs_stage_t)) { - if (((ecs_stage_t*)stage)->asynchronous) { - return false; - } +static +void swap_switch_columns( + ecs_table_t *table, + ecs_data_t *data, + int32_t row_1, + int32_t row_2) +{ + int32_t i = 0, column_count = table->sw_column_count; + if (!column_count) { + return; } - if (world->is_readonly) { - if (ecs_poly_is(stage, ecs_world_t)) { - return true; - } - } else { - if (ecs_poly_is(stage, ecs_stage_t)) { - return true; - } - } + ecs_sw_column_t *columns = data->sw_columns; - return false; + for (i = 0; i < column_count; i ++) { + ecs_switch_t *sw = columns[i].data; + flecs_switch_swap(sw, row_1, row_2); + } } -ecs_world_t* ecs_async_stage_new( - ecs_world_t *world) +static +void swap_bitset_columns( + ecs_table_t *table, + ecs_data_t *data, + int32_t row_1, + int32_t row_2) { - ecs_stage_t *stage = ecs_os_calloc(sizeof(ecs_stage_t)); - flecs_stage_init(world, stage); - - stage->id = -1; - stage->auto_merge = false; - stage->asynchronous = true; + int32_t i = 0, column_count = table->bs_column_count; + if (!column_count) { + return; + } - ecs_defer_begin((ecs_world_t*)stage); + ecs_bs_column_t *columns = data->bs_columns; - return (ecs_world_t*)stage; + for (i = 0; i < column_count; i ++) { + ecs_bitset_t *bs = &columns[i].data; + flecs_bitset_swap(bs, row_1, row_2); + } } -void ecs_async_stage_free( - ecs_world_t *world) -{ - ecs_poly_assert(world, ecs_stage_t); - ecs_stage_t *stage = (ecs_stage_t*)world; - ecs_assert(stage->asynchronous == true, ECS_INVALID_PARAMETER, NULL); - flecs_stage_deinit(stage->world, stage); - ecs_os_free(stage); -} +void flecs_table_swap( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + int32_t row_1, + int32_t row_2) +{ + (void)world; -bool ecs_stage_is_async( - ecs_world_t *stage) -{ - if (!stage) { - return false; - } + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(row_1 >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(row_2 >= 0, ECS_INTERNAL_ERROR, NULL); - if (!ecs_poly_is(stage, ecs_stage_t)) { - return false; + if (row_1 == row_2) { + return; } - return ((ecs_stage_t*)stage)->asynchronous; -} + /* If the table is monitored indicate that there has been a change */ + mark_table_dirty(table, 0); -bool ecs_is_deferred( - const ecs_world_t *world) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); - return stage->defer != 0; -} + ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); + ecs_entity_t e1 = entities[row_1]; + ecs_entity_t e2 = entities[row_2]; -/** Resize the vector buffer */ -static -ecs_vector_t* resize( - ecs_vector_t *vector, - int16_t offset, - int32_t size) -{ - ecs_vector_t *result = ecs_os_realloc(vector, offset + size); - ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, 0); - return result; -} + ecs_record_t **record_ptrs = ecs_vector_first(data->record_ptrs, ecs_record_t*); + ecs_record_t *record_ptr_1 = record_ptrs[row_1]; + ecs_record_t *record_ptr_2 = record_ptrs[row_2]; -/* -- Public functions -- */ + ecs_assert(record_ptr_1 != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(record_ptr_2 != NULL, ECS_INTERNAL_ERROR, NULL); -ecs_vector_t* _ecs_vector_new( - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count) -{ - ecs_assert(elem_size != 0, ECS_INTERNAL_ERROR, NULL); - - ecs_vector_t *result = - ecs_os_malloc(offset + elem_size * elem_count); - ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); + /* Keep track of whether entity is watched */ + bool watched_1 = record_ptr_1->row < 0; + bool watched_2 = record_ptr_2->row < 0; - result->count = 0; - result->size = elem_count; -#ifndef NDEBUG - result->elem_size = elem_size; -#endif - return result; -} + /* Swap entities & records */ + entities[row_1] = e2; + entities[row_2] = e1; + record_ptr_1->row = flecs_row_to_record(row_2, watched_1); + record_ptr_2->row = flecs_row_to_record(row_1, watched_2); + record_ptrs[row_1] = record_ptr_2; + record_ptrs[row_2] = record_ptr_1; -ecs_vector_t* _ecs_vector_from_array( - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count, - void *array) -{ - ecs_assert(elem_size != 0, ECS_INTERNAL_ERROR, NULL); + swap_switch_columns(table, data, row_1, row_2); + swap_bitset_columns(table, data, row_1, row_2); + + ecs_column_t *columns = data->columns; + if (!columns) { + return; + } + + /* Swap columns */ + int32_t i, column_count = ecs_vector_count(table->storage_type); - ecs_vector_t *result = - ecs_os_malloc(offset + elem_size * elem_count); - ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); + for (i = 0; i < column_count; i ++) { + int16_t size = columns[i].size; + int16_t alignment = columns[i].alignment; - ecs_os_memcpy(ECS_OFFSET(result, offset), array, elem_size * elem_count); + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); - result->count = elem_count; - result->size = elem_count; -#ifndef NDEBUG - result->elem_size = elem_size; -#endif - return result; -} + void *ptr = ecs_vector_first_t(columns[i].data, size, alignment); + void *tmp = ecs_os_alloca(size); -void ecs_vector_free( - ecs_vector_t *vector) -{ - ecs_os_free(vector); -} + void *el_1 = ECS_OFFSET(ptr, size * row_1); + void *el_2 = ECS_OFFSET(ptr, size * row_2); -void ecs_vector_clear( - ecs_vector_t *vector) -{ - if (vector) { - vector->count = 0; - } + ecs_os_memcpy(tmp, el_1, size); + ecs_os_memcpy(el_1, el_2, size); + ecs_os_memcpy(el_2, tmp, size); + } } -void _ecs_vector_zero( - ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset) +static +void merge_vector( + ecs_vector_t **dst_out, + ecs_vector_t *src, + int16_t size, + int16_t alignment) { - void *array = ECS_OFFSET(vector, offset); - ecs_os_memset(array, 0, elem_size * vector->count); -} + ecs_vector_t *dst = *dst_out; + int32_t dst_count = ecs_vector_count(dst); -void ecs_vector_assert_size( - ecs_vector_t *vector, - ecs_size_t elem_size) -{ - (void)elem_size; + if (!dst_count) { + if (dst) { + ecs_vector_free(dst); + } + + *dst_out = src; - if (vector) { - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + /* If the new table is not empty, copy the contents from the + * src into the dst. */ + } else { + int32_t src_count = ecs_vector_count(src); + ecs_vector_set_count_t(&dst, size, alignment, dst_count + src_count); + + void *dst_ptr = ecs_vector_first_t(dst, size, alignment); + void *src_ptr = ecs_vector_first_t(src, size, alignment); + + dst_ptr = ECS_OFFSET(dst_ptr, size * dst_count); + + ecs_os_memcpy(dst_ptr, src_ptr, size * src_count); + + ecs_vector_free(src); + *dst_out = dst; } } -void* _ecs_vector_addn( - ecs_vector_t **array_inout, - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count) +static +void merge_column( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + int32_t column_id, + ecs_vector_t *src) { - ecs_assert(array_inout != NULL, ECS_INTERNAL_ERROR, NULL); - - if (elem_count == 1) { - return _ecs_vector_add(array_inout, elem_size, offset); - } - - ecs_vector_t *vector = *array_inout; - if (!vector) { - vector = _ecs_vector_new(elem_size, offset, 1); - *array_inout = vector; - } + ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); + ecs_type_info_t *c_info = table->c_info[column_id]; + ecs_column_t *column = &data->columns[column_id]; + ecs_vector_t *dst = column->data; + int16_t size = column->size; + int16_t alignment = column->alignment; + int32_t dst_count = ecs_vector_count(dst); - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + if (!dst_count) { + if (dst) { + ecs_vector_free(dst); + } - int32_t max_count = vector->size; - int32_t old_count = vector->count; - int32_t new_count = old_count + elem_count; + column->data = src; + + /* If the new table is not empty, copy the contents from the + * src into the dst. */ + } else { + int32_t src_count = ecs_vector_count(src); + ecs_vector_set_count_t(&dst, size, alignment, dst_count + src_count); + column->data = dst; - if ((new_count - 1) >= max_count) { - if (!max_count) { - max_count = elem_count; + /* Construct new values */ + if (c_info) { + ctor_component( + world, c_info, column, entities, dst_count, src_count); + } + + void *dst_ptr = ecs_vector_first_t(dst, size, alignment); + void *src_ptr = ecs_vector_first_t(src, size, alignment); + + dst_ptr = ECS_OFFSET(dst_ptr, size * dst_count); + + /* Move values into column */ + ecs_move_t move; + if (c_info && (move = c_info->lifecycle.move)) { + move(world, c_info->component, entities, entities, + dst_ptr, src_ptr, flecs_to_size_t(size), src_count, + c_info->lifecycle.ctx); } else { - while (max_count < new_count) { - max_count *= 2; - } + ecs_os_memcpy(dst_ptr, src_ptr, size * src_count); } - vector = resize(vector, offset, max_count * elem_size); - vector->size = max_count; - *array_inout = vector; + ecs_vector_free(src); } - - vector->count = new_count; - - return ECS_OFFSET(vector, offset + elem_size * old_count); } -void* _ecs_vector_add( - ecs_vector_t **array_inout, - ecs_size_t elem_size, - int16_t offset) +static +void merge_table_data( + ecs_world_t *world, + ecs_table_t *new_table, + ecs_table_t *old_table, + int32_t old_count, + int32_t new_count, + ecs_data_t *old_data, + ecs_data_t *new_data) { - ecs_assert(array_inout != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_vector_t *vector = *array_inout; - int32_t count, size; + ecs_type_t new_type = new_table->storage_type; + ecs_type_t old_type = old_table->storage_type; + int32_t i_new = 0, new_column_count = ecs_vector_count(new_type); + int32_t i_old = 0, old_column_count = ecs_vector_count(old_type); + ecs_entity_t *new_components = ecs_vector_first(new_type, ecs_entity_t); + ecs_entity_t *old_components = ecs_vector_first(old_type, ecs_entity_t); - if (vector) { - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - count = vector->count; - size = vector->size; + ecs_column_t *old_columns = old_data->columns; + ecs_column_t *new_columns = new_data->columns; - if (count >= size) { - size *= 2; - if (!size) { - size = 2; + if (!new_columns && !new_data->entities) { + new_columns = new_data->columns; + } + + ecs_assert(!new_column_count || new_columns, ECS_INTERNAL_ERROR, NULL); + + if (!old_count) { + return; + } + + /* Merge entities */ + merge_vector(&new_data->entities, old_data->entities, ECS_SIZEOF(ecs_entity_t), + ECS_ALIGNOF(ecs_entity_t)); + old_data->entities = NULL; + ecs_entity_t *entities = ecs_vector_first(new_data->entities, ecs_entity_t); + + ecs_assert(ecs_vector_count(new_data->entities) == old_count + new_count, + ECS_INTERNAL_ERROR, NULL); + + /* Merge entity index record pointers */ + merge_vector(&new_data->record_ptrs, old_data->record_ptrs, + ECS_SIZEOF(ecs_record_t*), ECS_ALIGNOF(ecs_record_t*)); + old_data->record_ptrs = NULL; + + for (; (i_new < new_column_count) && (i_old < old_column_count); ) { + ecs_entity_t new_component = new_components[i_new]; + ecs_entity_t old_component = old_components[i_old]; + int16_t size = new_columns[i_new].size; + int16_t alignment = new_columns[i_new].alignment; + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + + if (new_component == old_component) { + merge_column(world, new_table, new_data, i_new, + old_columns[i_old].data); + old_columns[i_old].data = NULL; + + /* Mark component column as dirty */ + mark_table_dirty(new_table, i_new + 1); + + i_new ++; + i_old ++; + } else if (new_component < old_component) { + /* New column does not occur in old table, make sure vector is large + * enough. */ + ecs_column_t *column = &new_columns[i_new]; + ecs_vector_set_count_t(&column->data, size, alignment, + old_count + new_count); + + /* Construct new values */ + ecs_type_info_t *c_info = new_table->c_info[i_new]; + if (c_info) { + ctor_component(world, c_info, column, + entities, 0, old_count + new_count); + } + + i_new ++; + } else if (new_component > old_component) { + ecs_column_t *column = &old_columns[i_old]; + + /* Destruct old values */ + ecs_type_info_t *c_info = old_table->c_info[i_old]; + if (c_info) { + dtor_component(world, c_info, column, + entities, 0, old_count); } - vector = resize(vector, offset, size * elem_size); - *array_inout = vector; - vector->size = size; - } - vector->count = count + 1; - return ECS_OFFSET(vector, offset + elem_size * count); + /* Old column does not occur in new table, remove */ + ecs_vector_free(column->data); + column->data = NULL; + + i_old ++; + } } - vector = _ecs_vector_new(elem_size, offset, 2); - *array_inout = vector; - vector->count = 1; - vector->size = 2; - return ECS_OFFSET(vector, offset); -} + move_switch_columns( + new_table, new_data, new_count, old_table, old_data, 0, old_count); -int32_t _ecs_vector_move_index( - ecs_vector_t **dst, - ecs_vector_t *src, - ecs_size_t elem_size, - int16_t offset, - int32_t index) -{ - if (dst && *dst) { - ecs_assert((*dst)->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + /* Initialize remaining columns */ + for (; i_new < new_column_count; i_new ++) { + ecs_column_t *column = &new_columns[i_new]; + int16_t size = column->size; + int16_t alignment = column->alignment; + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + + ecs_vector_set_count_t(&column->data, size, alignment, + old_count + new_count); + + /* Construct new values */ + ecs_type_info_t *c_info = new_table->c_info[i_new]; + if (c_info) { + ctor_component(world, c_info, column, + entities, 0, old_count + new_count); + } } - ecs_assert(src->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - void *dst_elem = _ecs_vector_add(dst, elem_size, offset); - void *src_elem = _ecs_vector_get(src, elem_size, offset, index); + /* Destroy remaining columns */ + for (; i_old < old_column_count; i_old ++) { + ecs_column_t *column = &old_columns[i_old]; - ecs_os_memcpy(dst_elem, src_elem, elem_size); - return _ecs_vector_remove(src, elem_size, offset, index); + /* Destruct old values */ + ecs_type_info_t *c_info = old_table->c_info[i_old]; + if (c_info) { + dtor_component(world, c_info, column, entities, + 0, old_count); + } + + /* Old column does not occur in new table, remove */ + ecs_vector_free(column->data); + column->data = NULL; + } + + /* Mark entity column as dirty */ + mark_table_dirty(new_table, 0); } -void ecs_vector_remove_last( - ecs_vector_t *vector) +int32_t ecs_table_count( + const ecs_table_t *table) { - if (vector && vector->count) vector->count --; + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + return flecs_table_data_count(&table->storage); } -bool _ecs_vector_pop( - ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset, - void *value) +void flecs_table_merge( + ecs_world_t *world, + ecs_table_t *new_table, + ecs_table_t *old_table, + ecs_data_t *new_data, + ecs_data_t *old_data) { - if (!vector) { - return false; + ecs_assert(old_table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!old_table->lock, ECS_LOCKED_STORAGE, NULL); + + bool move_data = false; + + /* If there is nothing to merge to, just clear the old table */ + if (!new_table) { + flecs_table_clear_data(world, old_table, old_data); + return; + } else { + ecs_assert(!new_table->lock, ECS_LOCKED_STORAGE, NULL); } - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - - int32_t count = vector->count; - if (!count) { - return false; + /* If there is no data to merge, drop out */ + if (!old_data) { + return; } - void *elem = ECS_OFFSET(vector, offset + (count - 1) * elem_size); - - if (value) { - ecs_os_memcpy(value, elem, elem_size); + if (!new_data) { + new_data = &new_table->storage; + if (new_table == old_table) { + move_data = true; + } } - ecs_vector_remove_last(vector); + ecs_entity_t *old_entities = ecs_vector_first(old_data->entities, ecs_entity_t); + int32_t old_count = ecs_vector_count(old_data->entities); + int32_t new_count = ecs_vector_count(new_data->entities); - return true; -} + ecs_record_t **old_records = ecs_vector_first( + old_data->record_ptrs, ecs_record_t*); -int32_t _ecs_vector_remove( - ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset, - int32_t index) -{ - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - - int32_t count = vector->count; - void *buffer = ECS_OFFSET(vector, offset); - void *elem = ECS_OFFSET(buffer, index * elem_size); + /* First, update entity index so old entities point to new type */ + int32_t i; + for(i = 0; i < old_count; i ++) { + ecs_record_t *record; + if (new_table != old_table) { + record = old_records[i]; + ecs_assert(record != NULL, ECS_INTERNAL_ERROR, NULL); + } else { + record = ecs_eis_ensure(world, old_entities[i]); + } - ecs_assert(index < count, ECS_INVALID_PARAMETER, NULL); + bool is_monitored = record->row < 0; + record->row = flecs_row_to_record(new_count + i, is_monitored); + record->table = new_table; + } - count --; - if (index != count) { - void *last_elem = ECS_OFFSET(buffer, elem_size * count); - ecs_os_memcpy(elem, last_elem, elem_size); + /* Merge table columns */ + if (move_data) { + *new_data = *old_data; + } else { + merge_table_data(world, new_table, old_table, old_count, new_count, + old_data, new_data); } - vector->count = count; + new_table->alloc_count ++; - return count; + if (!new_count && old_count) { + table_activate(world, new_table, true); + } } -void _ecs_vector_reclaim( - ecs_vector_t **array_inout, - ecs_size_t elem_size, - int16_t offset) +void flecs_table_replace_data( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data) { - ecs_vector_t *vector = *array_inout; + int32_t prev_count = 0; + ecs_data_t *table_data = &table->storage; + ecs_assert(!data || data != table_data, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - - int32_t size = vector->size; - int32_t count = vector->count; + prev_count = ecs_vector_count(table_data->entities); + run_on_remove(world, table, table_data); + flecs_table_clear_data(world, table, table_data); - if (count < size) { - size = count; - vector = resize(vector, offset, size * elem_size); - vector->size = size; - *array_inout = vector; + if (data) { + table->storage = *data; + } else { + flecs_table_init_data(world, table); } -} -int32_t ecs_vector_count( - const ecs_vector_t *vector) -{ - if (!vector) { - return 0; + int32_t count = ecs_table_count(table); + + if (!prev_count && count) { + table_activate(world, table, true); + } else if (prev_count && !count) { + table_activate(world, table, false); } - return vector->count; + + table->alloc_count ++; } -int32_t ecs_vector_size( - const ecs_vector_t *vector) +int32_t* flecs_table_get_dirty_state( + ecs_table_t *table) { - if (!vector) { - return 0; + ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL); + + if (!table->dirty_state) { + int32_t column_count = ecs_vector_count(table->storage_type); + table->dirty_state = ecs_os_calloc_n( int32_t, column_count + 1); + ecs_assert(table->dirty_state != NULL, ECS_INTERNAL_ERROR, NULL); } - return vector->size; + return table->dirty_state; } -int32_t _ecs_vector_set_size( - ecs_vector_t **array_inout, - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count) +int32_t* flecs_table_get_monitor( + ecs_table_t *table) { - ecs_vector_t *vector = *array_inout; - - if (!vector) { - *array_inout = _ecs_vector_new(elem_size, offset, elem_count); - return elem_count; - } else { - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - - int32_t result = vector->size; + int32_t *dirty_state = flecs_table_get_dirty_state(table); + ecs_assert(dirty_state != NULL, ECS_INTERNAL_ERROR, NULL); - if (elem_count < vector->count) { - elem_count = vector->count; - } + int32_t column_count = ecs_vector_count(table->storage_type); + return ecs_os_memdup(dirty_state, (column_count + 1) * ECS_SIZEOF(int32_t)); +} - if (result < elem_count) { - elem_count = flecs_next_pow_of_2(elem_count); - vector = resize(vector, offset, elem_count * elem_size); - vector->size = elem_count; - *array_inout = vector; - result = elem_count; - } +void flecs_table_notify( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_event_t *event) +{ + if (world->is_fini) { + return; + } - return result; + switch(event->kind) { + case EcsTableQueryMatch: + register_query(world, table, event->query); + break; + case EcsTableQueryUnmatch: + unregister_query(world, table, event->query); + break; + case EcsTableComponentInfo: + notify_component_info(world, table, event->component); + break; + case EcsTableTriggerMatch: + notify_trigger(world, table, event->event); + break; } } -int32_t _ecs_vector_grow( - ecs_vector_t **array_inout, - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count) +void ecs_table_lock( + ecs_world_t *world, + ecs_table_t *table) { - int32_t current = ecs_vector_count(*array_inout); - return _ecs_vector_set_size(array_inout, elem_size, offset, current + elem_count); + if (ecs_poly_is(world, ecs_world_t) && !world->is_readonly) { + table->lock ++; + } } -int32_t _ecs_vector_set_count( - ecs_vector_t **array_inout, - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count) +void ecs_table_unlock( + ecs_world_t *world, + ecs_table_t *table) { - if (!*array_inout) { - *array_inout = _ecs_vector_new(elem_size, offset, elem_count); + if (ecs_poly_is(world, ecs_world_t) && !world->is_readonly) { + table->lock --; + ecs_assert(table->lock >= 0, ECS_INVALID_OPERATION, NULL); } - - ecs_assert((*array_inout)->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - - (*array_inout)->count = elem_count; - ecs_size_t size = _ecs_vector_set_size(array_inout, elem_size, offset, elem_count); - return size; } -void* _ecs_vector_first( - const ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset) +bool ecs_table_has_module( + ecs_table_t *table) { - (void)elem_size; - - ecs_assert(!vector || vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - if (vector && vector->size) { - return ECS_OFFSET(vector, offset); - } else { - return NULL; - } + return table->flags & EcsTableHasModule; } -void* _ecs_vector_get( - const ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset, - int32_t index) +ecs_column_t *ecs_table_column_for_id( + const ecs_world_t *world, + const ecs_table_t *table, + ecs_id_t id) { - if (!vector) { + ecs_table_t *storage_table = table->storage_table; + if (!storage_table) { return NULL; } - - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); - - int32_t count = vector->count; - if (index >= count) { - return NULL; + ecs_table_record_t *tr = flecs_get_table_record(world, storage_table, id); + if (tr) { + return &table->storage.columns[tr->column]; } - return ECS_OFFSET(vector, offset + elem_size * index); + return NULL; } -void* _ecs_vector_last( - const ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset) +ecs_type_t ecs_table_get_type( + const ecs_table_t *table) { - if (vector) { - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - int32_t count = vector->count; - if (!count) { - return NULL; - } else { - return ECS_OFFSET(vector, offset + elem_size * (count - 1)); - } - } else { - return NULL; - } + return table->type; } -int32_t _ecs_vector_set_min_size( - ecs_vector_t **vector_inout, - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count) +ecs_type_t ecs_table_get_storage_type( + const ecs_table_t *table) { - if (!*vector_inout || (*vector_inout)->size < elem_count) { - return _ecs_vector_set_size(vector_inout, elem_size, offset, elem_count); - } else { - return (*vector_inout)->size; - } + return table->storage_type; } -int32_t _ecs_vector_set_min_count( - ecs_vector_t **vector_inout, - ecs_size_t elem_size, - int16_t offset, - int32_t elem_count) +int32_t ecs_table_storage_count( + const ecs_table_t *table) { - _ecs_vector_set_min_size(vector_inout, elem_size, offset, elem_count); + return ecs_vector_count(table->storage_type); +} - ecs_vector_t *v = *vector_inout; - if (v && v->count < elem_count) { - v->count = elem_count; +int32_t ecs_table_type_to_storage_index( + const ecs_table_t *table, + int32_t index) +{ + ecs_assert(index < ecs_vector_count(table->type), + ECS_INVALID_PARAMETER, NULL); + int32_t *storage_map = table->storage_map; + if (storage_map) { + return storage_map[index]; + } else { + return -1; } +} - return v->count; +int32_t ecs_table_storage_to_type_index( + const ecs_table_t *table, + int32_t index) +{ + ecs_assert(index < ecs_vector_count(table->storage_type), + ECS_INVALID_PARAMETER, NULL); + ecs_assert(table->storage_map != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t offset = ecs_vector_count(table->type); + return table->storage_map[offset + index]; } -void _ecs_vector_sort( - ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset, - ecs_comparator_t compare_action) +ecs_record_t* ecs_record_find( + ecs_world_t *world, + ecs_entity_t entity) { - if (!vector) { - return; + ecs_record_t *r = ecs_eis_get(world, entity); + if (r) { + return r; + } else { + return NULL; } +} - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - - int32_t count = vector->count; - void *buffer = ECS_OFFSET(vector, offset); +void* ecs_record_get_column( + ecs_record_t *r, + int32_t column, + size_t c_size) +{ + (void)c_size; + ecs_table_t *table = r->table; - if (count > 1) { - qsort(buffer, (size_t)count, (size_t)elem_size, compare_action); - } + ecs_assert(column < ecs_vector_count(table->storage_type), + ECS_INVALID_PARAMETER, NULL); + + ecs_column_t *c = &table->storage.columns[column]; + ecs_assert(c != NULL, ECS_INTERNAL_ERROR, NULL); + + int16_t size = c->size; + ecs_assert(!flecs_from_size_t(c_size) || + flecs_from_size_t(c_size) == c->size, + ECS_INVALID_PARAMETER, NULL); + + void *array = ecs_vector_first_t(c->data, c->size, c->alignment); + bool is_watched; + int32_t row = flecs_record_to_row(r->row, &is_watched); + + return ECS_OFFSET(array, size * row); } -void _ecs_vector_memory( - const ecs_vector_t *vector, - ecs_size_t elem_size, - int16_t offset, - int32_t *allocd, - int32_t *used) -{ - if (!vector) { - return; +#define INIT_CACHE(it, f, term_count)\ + if (!it->f && term_count) {\ + if (term_count < ECS_TERM_CACHE_SIZE) {\ + it->f = it->cache.f;\ + it->cache.f##_alloc = false;\ + } else {\ + it->f = ecs_os_calloc(ECS_SIZEOF(*(it->f)) * term_count);\ + it->cache.f##_alloc = true;\ + }\ } - ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); +#define FINI_CACHE(it, f)\ + if (it->f) {\ + if (it->cache.f##_alloc) {\ + ecs_os_free((void*)it->f);\ + }\ + } - if (allocd) { - *allocd += vector->size * elem_size + offset; - } - if (used) { - *used += vector->count * elem_size; - } +void flecs_iter_init( + ecs_iter_t *it) +{ + INIT_CACHE(it, ids, it->term_count); + INIT_CACHE(it, columns, it->term_count); + INIT_CACHE(it, subjects, it->term_count); + INIT_CACHE(it, sizes, it->term_count); + INIT_CACHE(it, ptrs, it->term_count); + INIT_CACHE(it, match_indices, it->term_count); + + it->is_valid = true; } -ecs_vector_t* _ecs_vector_copy( - const ecs_vector_t *src, - ecs_size_t elem_size, - int16_t offset) +void flecs_iter_fini( + ecs_iter_t *it) { - if (!src) { - return NULL; - } + ecs_assert(it->is_valid == true, ECS_INVALID_PARAMETER, NULL); + it->is_valid = false; - ecs_vector_t *dst = _ecs_vector_new(elem_size, offset, src->size); - ecs_os_memcpy(dst, src, offset + elem_size * src->count); - return dst; + FINI_CACHE(it, ids); + FINI_CACHE(it, columns); + FINI_CACHE(it, subjects); + FINI_CACHE(it, sizes); + FINI_CACHE(it, ptrs); + FINI_CACHE(it, match_indices); } -/** The number of elements in a single chunk */ -#define CHUNK_COUNT (4096) +static +void flecs_iter_populate_term_data( + ecs_world_t *world, + ecs_iter_t *it, + int32_t t, + int32_t column, + void **ptr_out, + ecs_size_t *size_out) +{ + if (!column) { + /* Term has no data. This includes terms that have Not operators. */ + goto no_data; + } -/** Compute the chunk index from an id by stripping the first 12 bits */ -#define CHUNK(index) ((int32_t)((uint32_t)index >> 12)) + ecs_table_t *table; + ecs_vector_t *vec; + ecs_size_t size; + ecs_size_t align; + int32_t row; -/** This computes the offset of an index inside a chunk */ -#define OFFSET(index) ((int32_t)index & 0xFFF) + if (column < 0) { + /* Data is not from This */ + if (it->references) { + /* Iterator provides cached references for non-This terms */ + ecs_ref_t *ref = &it->references[-column - 1]; + if (ptr_out) ptr_out[0] = (void*)ecs_get_ref_w_id( + world, ref, ref->entity, ref->component); -/* Utility to get a pointer to the payload */ -#define DATA(array, size, offset) (ECS_OFFSET(array, size * offset)) + /* If cached references were provided, the code that populated + * the iterator also had a chance to cache sizes, so size array + * should already have been assigned. This saves us from having + * to do additional lookups to find the component size. */ + ecs_assert(size_out == NULL, ECS_INTERNAL_ERROR, NULL); + return; + } else { + ecs_entity_t subj = it->subjects[t]; + ecs_assert(subj != 0, ECS_INTERNAL_ERROR, NULL); -typedef struct chunk_t { - int32_t *sparse; /* Sparse array with indices to dense array */ - void *data; /* Store data in sparse array to reduce - * indirection and provide stable pointers. */ -} chunk_t; + /* Don't use ecs_get_id directly. Instead, go directly to the + * storage so that we can get both the pointer and size */ + ecs_record_t *r = ecs_eis_get(world, subj); + ecs_assert(r != NULL && r->table != NULL, ECS_INTERNAL_ERROR, NULL); -struct ecs_sparse_t { - ecs_vector_t *dense; /* Dense array with indices to sparse array. The - * dense array stores both alive and not alive - * sparse indices. The 'count' member keeps - * track of which indices are alive. */ + bool is_monitored; + row = flecs_record_to_row(r->row, &is_monitored); + table = r->table; - ecs_vector_t *chunks; /* Chunks with sparse arrays & data */ - ecs_size_t size; /* Element size */ - int32_t count; /* Number of alive entries */ - uint64_t max_id_local; /* Local max index (if no global is set) */ - uint64_t *max_id; /* Maximum issued sparse index */ -}; + ecs_id_t id = it->ids[t]; + ecs_table_t *s_table = table->storage_table; + ecs_table_record_t *tr; -static -chunk_t* chunk_new( - ecs_sparse_t *sparse, - int32_t chunk_index) -{ - int32_t count = ecs_vector_count(sparse->chunks); - chunk_t *chunks; + if (!s_table || !(tr = flecs_get_table_record(world, s_table, id))){ + /* The entity has no components or the id is not a component */ + + ecs_id_t term_id = it->terms[t].id; + if (ECS_HAS_ROLE(term_id, SWITCH) || ECS_HAS_ROLE(term_id, CASE)) { + /* Edge case: if this is a switch. Find switch column in + * actual table, as its not in the storage table */ + tr = flecs_get_table_record(world, table, id); + ecs_assert(tr != NULL, ECS_INTERNAL_ERROR, NULL); + column = tr->column; + goto has_switch; + } else { + goto no_data; + } + } - if (count <= chunk_index) { - ecs_vector_set_count(&sparse->chunks, chunk_t, chunk_index + 1); - chunks = ecs_vector_first(sparse->chunks, chunk_t); - ecs_os_memset(&chunks[count], 0, (1 + chunk_index - count) * ECS_SIZEOF(chunk_t)); + /* We now have row and column, so we can get the storage for the id + * which gives us the pointer and size */ + column = tr->column; + ecs_column_t *s = &table->storage.columns[column]; + size = s->size; + align = s->alignment; + vec = s->data; + /* Fallthrough to has_data */ + } } else { - chunks = ecs_vector_first(sparse->chunks, chunk_t); - } - - ecs_assert(chunks != NULL, ECS_INTERNAL_ERROR, NULL); + /* Data is from This, use table from iterator */ + table = it->table; + if (!table) { + goto no_data; + } - chunk_t *result = &chunks[chunk_index]; - ecs_assert(result->sparse == NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(result->data == NULL, ECS_INTERNAL_ERROR, NULL); + row = it->offset; + + int32_t storage_column = ecs_table_type_to_storage_index( + table, column - 1); + if (storage_column == -1) { + ecs_id_t id = it->terms[t].id; + if (ECS_HAS_ROLE(id, SWITCH) || ECS_HAS_ROLE(id, CASE)) { + goto has_switch; + } + goto no_data; + } - /* Initialize sparse array with zero's, as zero is used to indicate that the - * sparse element has not been paired with a dense element. Use zero - * as this means we can take advantage of calloc having a possibly better - * performance than malloc + memset. */ - result->sparse = ecs_os_calloc(ECS_SIZEOF(int32_t) * CHUNK_COUNT); + ecs_column_t *s = &table->storage.columns[storage_column]; + size = s->size; + align = s->alignment; + vec = s->data; + /* Fallthrough to has_data */ + } - /* Initialize the data array with zero's to guarantee that data is - * always initialized. When an entry is removed, data is reset back to - * zero. Initialize now, as this can take advantage of calloc. */ - result->data = ecs_os_calloc(sparse->size * CHUNK_COUNT); +has_data: + if (ptr_out) ptr_out[0] = ecs_vector_get_t(vec, size, align, row); + if (size_out) size_out[0] = size; + return; - ecs_assert(result->sparse != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(result->data != NULL, ECS_INTERNAL_ERROR, NULL); +has_switch: { + /* Edge case: if column is a switch we should return the vector with case + * identifiers. Will be replaced in the future with pluggable storage */ + ecs_switch_t *sw = table->storage.sw_columns[ + (column - 1) - table->sw_column_offset].data; + vec = flecs_switch_values(sw); + size = ECS_SIZEOF(ecs_entity_t); + align = ECS_ALIGNOF(ecs_entity_t); + goto has_data; + } - return result; +no_data: + if (ptr_out) ptr_out[0] = NULL; + if (size_out) size_out[0] = 0; } -static -void chunk_free( - chunk_t *chunk) +void flecs_iter_populate_data( + ecs_world_t *world, + ecs_iter_t *it, + void **ptrs, + ecs_size_t *sizes) { - ecs_os_free(chunk->sparse); - ecs_os_free(chunk->data); + int t, term_count = it->term_count; + for (t = 0; t < term_count; t ++) { + int32_t column = it->columns[t]; + flecs_iter_populate_term_data(world, it, t, column, + &ptrs[t * (ptrs != NULL)], + &sizes[t * (sizes != NULL)]); + } } -static -chunk_t* get_chunk( - const ecs_sparse_t *sparse, - int32_t chunk_index) +/* --- Public API --- */ + +void* ecs_term_w_size( + const ecs_iter_t *it, + size_t size, + int32_t term) { - /* If chunk_index is below zero, application used an invalid entity id */ - ecs_assert(chunk_index >= 0, ECS_INVALID_PARAMETER, NULL); - chunk_t *result = ecs_vector_get(sparse->chunks, chunk_t, chunk_index); - if (result && !result->sparse) { + ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!size || ecs_term_size(it, term) == size, + ECS_INVALID_PARAMETER, NULL); + + (void)size; + + if (!term) { + return it->entities; + } + + if (!it->ptrs) { return NULL; } - return result; + return it->ptrs[term - 1]; } -static -chunk_t* get_or_create_chunk( - ecs_sparse_t *sparse, - int32_t chunk_index) +bool ecs_term_is_readonly( + const ecs_iter_t *it, + int32_t term_index) { - chunk_t *chunk = get_chunk(sparse, chunk_index); - if (chunk) { - return chunk; + ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); + ecs_assert(term_index > 0, ECS_INVALID_PARAMETER, NULL); + + ecs_term_t *term = &it->terms[term_index - 1]; + ecs_assert(term != NULL, ECS_INVALID_PARAMETER, NULL); + + if (term->inout == EcsIn) { + return true; + } else { + ecs_term_id_t *subj = &term->subj; + + if (term->inout == EcsInOutDefault) { + if (subj->entity != EcsThis) { + return true; + } + + if ((subj->set.mask != EcsSelf) && + (subj->set.mask != EcsDefaultSet)) + { + return true; + } + } } - return chunk_new(sparse, chunk_index); + return false; } -static -void grow_dense( - ecs_sparse_t *sparse) +int32_t ecs_iter_find_column( + const ecs_iter_t *it, + ecs_entity_t component) { - ecs_vector_add(&sparse->dense, uint64_t); + ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); + ecs_assert(it->table != NULL, ECS_INVALID_PARAMETER, NULL); + return ecs_type_index_of(it->table->type, 0, component); } -static -uint64_t strip_generation( - uint64_t *index_out) +bool ecs_term_is_set( + const ecs_iter_t *it, + int32_t index) { - uint64_t index = *index_out; - uint64_t gen = index & ECS_GENERATION_MASK; - /* Make sure there's no junk in the id */ - ecs_assert(gen == (index & (0xFFFFFFFFull << 32)), - ECS_INVALID_PARAMETER, NULL); - *index_out -= gen; - return gen; -} + ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); -static -void assign_index( - chunk_t * chunk, - uint64_t * dense_array, - uint64_t index, - int32_t dense) -{ - /* Initialize sparse-dense pair. This assigns the dense index to the sparse - * array, and the sparse index to the dense array .*/ - chunk->sparse[OFFSET(index)] = dense; - dense_array[dense] = index; -} + int32_t column = it->columns[index - 1]; + if (!column) { + return false; + } else if (column < 0) { + if (it->references) { + column = -column - 1; + ecs_ref_t *ref = &it->references[column]; + return ref->entity != 0; + } else { + return true; + } + } -static -uint64_t inc_gen( - uint64_t index) -{ - /* When an index is deleted, its generation is increased so that we can do - * liveliness checking while recycling ids */ - return ECS_GENERATION_INC(index); + return true; } -static -uint64_t inc_id( - ecs_sparse_t *sparse) +void* ecs_iter_column_w_size( + const ecs_iter_t *it, + size_t size, + int32_t index) { - /* Generate a new id. The last issued id could be stored in an external - * variable, such as is the case with the last issued entity id, which is - * stored on the world. */ - return ++ (sparse->max_id[0]); -} + ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); + ecs_assert(it->table != NULL, ECS_INVALID_PARAMETER, NULL); + (void)size; + + ecs_table_t *table = it->table; + int32_t storage_index = ecs_table_type_to_storage_index(table, index); + if (storage_index == -1) { + return NULL; + } -static -uint64_t get_id( - const ecs_sparse_t *sparse) -{ - return sparse->max_id[0]; -} + ecs_column_t *columns = table->storage.columns; + ecs_column_t *column = &columns[storage_index]; + ecs_assert(!size || (ecs_size_t)size == column->size, + ECS_INVALID_PARAMETER, NULL); -static -void set_id( - ecs_sparse_t *sparse, - uint64_t value) -{ - /* Sometimes the max id needs to be assigned directly, which typically - * happens when the API calls get_or_create for an id that hasn't been - * issued before. */ - sparse->max_id[0] = value; + void *ptr = ecs_vector_first_t( + column->data, column->size, column->alignment); + + return ECS_OFFSET(ptr, column->size * it->offset); } -/* Pair dense id with new sparse id */ -static -uint64_t create_id( - ecs_sparse_t *sparse, - int32_t dense) +size_t ecs_iter_column_size( + const ecs_iter_t *it, + int32_t index) { - uint64_t index = inc_id(sparse); - grow_dense(sparse); - - chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); - ecs_assert(chunk->sparse[OFFSET(index)] == 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); + ecs_assert(it->table != NULL, ECS_INVALID_PARAMETER, NULL); - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - assign_index(chunk, dense_array, index, dense); + ecs_table_t *table = it->table; + int32_t storage_index = ecs_table_type_to_storage_index(table, index); + if (storage_index == -1) { + return 0; + } + + ecs_column_t *columns = table->storage.columns; + ecs_column_t *column = &columns[storage_index]; - return index; + return flecs_to_size_t(column->size); } -/* Create new id */ -static -uint64_t new_index( - ecs_sparse_t *sparse) +char* ecs_iter_str( + const ecs_iter_t *it) { - ecs_vector_t *dense = sparse->dense; - int32_t dense_count = ecs_vector_count(dense); - int32_t count = sparse->count ++; + ecs_world_t *world = it->world; + ecs_strbuf_t buf = ECS_STRBUF_INIT; + int i; - ecs_assert(count <= dense_count, ECS_INTERNAL_ERROR, NULL); + if (it->term_count) { + ecs_strbuf_list_push(&buf, "term: ", ","); + for (i = 0; i < it->term_count; i ++) { + ecs_id_t id = ecs_term_id(it, i + 1); + char *str = ecs_id_str(world, id); + ecs_strbuf_list_appendstr(&buf, str); + ecs_os_free(str); + } + ecs_strbuf_list_pop(&buf, "\n"); - if (count < dense_count) { - /* If there are unused elements in the dense array, return first */ - uint64_t *dense_array = ecs_vector_first(dense, uint64_t); - return dense_array[count]; - } else { - return create_id(sparse, count); + ecs_strbuf_list_push(&buf, "subj: ", ","); + for (i = 0; i < it->term_count; i ++) { + ecs_entity_t subj = ecs_term_source(it, i + 1); + char *str = ecs_get_fullpath(world, subj); + ecs_strbuf_list_appendstr(&buf, str); + ecs_os_free(str); + } + ecs_strbuf_list_pop(&buf, "\n"); } -} -/* Try obtaining a value from the sparse set, don't care about whether the - * provided index matches the current generation count. */ -static -void* try_sparse_any( - const ecs_sparse_t *sparse, - uint64_t index) -{ - strip_generation(&index); + if (it->variable_count) { + int32_t actual_count = 0; + for (i = 0; i < it->variable_count; i ++) { + const char *var_name = it->variable_names[i]; + if (!var_name || var_name[0] == '_' || var_name[0] == '.') { + /* Skip anonymous variables */ + continue; + } - chunk_t *chunk = get_chunk(sparse, CHUNK(index)); - if (!chunk) { - return NULL; + ecs_entity_t var = it->variables[i]; + if (!var) { + /* Skip table variables */ + continue; + } + + if (!actual_count) { + ecs_strbuf_list_push(&buf, "vars: ", ","); + } + + char *str = ecs_get_fullpath(world, var); + ecs_strbuf_list_append(&buf, "%s=%s", var_name, str); + ecs_os_free(str); + + actual_count ++; + } + if (actual_count) { + ecs_strbuf_list_pop(&buf, "\n"); + } } - int32_t offset = OFFSET(index); - int32_t dense = chunk->sparse[offset]; - bool in_use = dense && (dense < sparse->count); - if (!in_use) { - return NULL; + if (it->count) { + ecs_strbuf_appendstr(&buf, "this:\n"); + for (i = 0; i < it->count; i ++) { + ecs_entity_t e = it->entities[i]; + char *str = ecs_get_fullpath(world, e); + ecs_strbuf_appendstr(&buf, " - "); + ecs_strbuf_appendstr(&buf, str); + ecs_strbuf_appendstr(&buf, "\n"); + ecs_os_free(str); + } } - ecs_assert(dense == chunk->sparse[offset], ECS_INTERNAL_ERROR, NULL); - return DATA(chunk->data, sparse->size, offset); + return ecs_strbuf_get(&buf); } -/* Try obtaining a value from the sparse set, make sure it's alive. */ static -void* try_sparse( - const ecs_sparse_t *sparse, - uint64_t index) -{ - chunk_t *chunk = get_chunk(sparse, CHUNK(index)); - if (!chunk) { - return NULL; - } +uint64_t ids_hash(const void *ptr) { + const ecs_ids_t *type = ptr; + ecs_id_t *ids = type->array; + int32_t count = type->count; + uint64_t hash = flecs_hash(ids, count * ECS_SIZEOF(ecs_id_t)); + return hash; +} - int32_t offset = OFFSET(index); - int32_t dense = chunk->sparse[offset]; - bool in_use = dense && (dense < sparse->count); - if (!in_use) { - return NULL; +static +int ids_compare(const void *ptr_1, const void *ptr_2) { + const ecs_ids_t *type_1 = ptr_1; + const ecs_ids_t *type_2 = ptr_2; + + int32_t count_1 = type_1->count; + int32_t count_2 = type_2->count; + + if (count_1 != count_2) { + return (count_1 > count_2) - (count_1 < count_2); } - uint64_t gen = strip_generation(&index); - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - uint64_t cur_gen = dense_array[dense] & ECS_GENERATION_MASK; + const ecs_id_t *ids_1 = type_1->array; + const ecs_id_t *ids_2 = type_2->array; + + int32_t i; + for (i = 0; i < count_1; i ++) { + ecs_id_t id_1 = ids_1[i]; + ecs_id_t id_2 = ids_2[i]; - if (cur_gen != gen) { - return NULL; + if (id_1 != id_2) { + return (id_1 > id_2) - (id_1 < id_2); + } } - ecs_assert(dense == chunk->sparse[offset], ECS_INTERNAL_ERROR, NULL); - return DATA(chunk->data, sparse->size, offset); + return 0; } -/* Get value from sparse set when it is guaranteed that the value exists. This - * function is used when values are obtained using a dense index */ -static -void* get_sparse( - const ecs_sparse_t *sparse, - int32_t dense, - uint64_t index) -{ - strip_generation(&index); - chunk_t *chunk = get_chunk(sparse, CHUNK(index)); - int32_t offset = OFFSET(index); - - ecs_assert(chunk != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(dense == chunk->sparse[offset], ECS_INTERNAL_ERROR, NULL); - (void)dense; - - return DATA(chunk->data, sparse->size, offset); +ecs_hashmap_t flecs_table_hashmap_new(void) { + return flecs_hashmap_new(ecs_ids_t, ecs_table_t*, ids_hash, ids_compare); } -/* Swap dense elements. A swap occurs when an element is removed, or when a - * removed element is recycled. */ -static -void swap_dense( - ecs_sparse_t * sparse, - chunk_t * chunk_a, - int32_t a, - int32_t b) +const EcsComponent* flecs_component_from_id( + const ecs_world_t *world, + ecs_entity_t e) { - ecs_assert(a != b, ECS_INTERNAL_ERROR, NULL); - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - uint64_t index_a = dense_array[a]; - uint64_t index_b = dense_array[b]; + ecs_entity_t pair = 0; - chunk_t *chunk_b = get_or_create_chunk(sparse, CHUNK(index_b)); - assign_index(chunk_a, dense_array, index_a, b); - assign_index(chunk_b, dense_array, index_b, a); -} + /* If this is a pair, get the pair component from the identifier */ + if (ECS_HAS_ROLE(e, PAIR)) { + pair = e; + e = ecs_get_alive(world, ECS_PAIR_RELATION(e)); -ecs_sparse_t* _flecs_sparse_new( - ecs_size_t size) -{ - ecs_sparse_t *result = ecs_os_calloc(ECS_SIZEOF(ecs_sparse_t)); - ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); - result->size = size; - result->max_id_local = UINT64_MAX; - result->max_id = &result->max_id_local; + if (ecs_has_id(world, e, EcsTag)) { + return NULL; + } + } - /* Consume first value in dense array as 0 is used in the sparse array to - * indicate that a sparse element hasn't been paired yet. */ - uint64_t *first = ecs_vector_add(&result->dense, uint64_t); - *first = 0; + if (e & ECS_ROLE_MASK) { + return NULL; + } - result->count = 1; + const EcsComponent *component = ecs_get(world, e, EcsComponent); + if ((!component || !component->size) && pair) { + /* If this is a pair column and the pair is not a component, use + * the component type of the component the pair is applied to. */ + e = ECS_PAIR_OBJECT(pair); - return result; -} + /* Because generations are not stored in the pair, get the currently + * alive id */ + e = ecs_get_alive(world, e); -void flecs_sparse_set_id_source( - ecs_sparse_t * sparse, - uint64_t * id_source) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - sparse->max_id = id_source; + /* If a pair is used with a not alive id, the pair is not valid */ + ecs_assert(e != 0, ECS_INTERNAL_ERROR, NULL); + + component = ecs_get(world, e, EcsComponent); + } + + return component; } -void flecs_sparse_clear( - ecs_sparse_t *sparse) +/* Ensure the ids used in the columns exist */ +static +int32_t ensure_columns( + ecs_world_t *world, + ecs_table_t *table) { - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t count = 0; + ecs_vector_each(table->type, ecs_entity_t, c_ptr, { + ecs_entity_t component = *c_ptr; - ecs_vector_each(sparse->chunks, chunk_t, chunk, { - chunk_free(chunk); + if (ECS_HAS_ROLE(component, PAIR)) { + ecs_entity_t rel = ECS_PAIR_RELATION(component); + ecs_entity_t obj = ECS_PAIR_OBJECT(component); + ecs_ensure(world, rel); + ecs_ensure(world, obj); + } else if (component & ECS_ROLE_MASK) { + ecs_entity_t e = ECS_PAIR_OBJECT(component); + ecs_ensure(world, e); + } else { + ecs_ensure(world, component); + } }); - ecs_vector_free(sparse->chunks); - ecs_vector_set_count(&sparse->dense, uint64_t, 1); - - sparse->chunks = NULL; - sparse->count = 1; - sparse->max_id_local = 0; + return count; } -void flecs_sparse_free( - ecs_sparse_t *sparse) +static +ecs_type_t ids_to_type( + ecs_ids_t *entities) { - if (sparse) { - flecs_sparse_clear(sparse); - ecs_vector_free(sparse->dense); - ecs_os_free(sparse); + if (entities->count) { + ecs_vector_t *result = NULL; + ecs_vector_set_count(&result, ecs_entity_t, entities->count); + ecs_entity_t *array = ecs_vector_first(result, ecs_entity_t); + ecs_os_memcpy_n(array, entities->array, ecs_entity_t, entities->count); + return result; + } else { + return NULL; } } -uint64_t flecs_sparse_new_id( - ecs_sparse_t *sparse) +static +ecs_edge_t* get_edge( + ecs_graph_edges_t *edges, + ecs_id_t id) { - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - return new_index(sparse); + if (id < ECS_HI_COMPONENT_ID) { + if (!edges->lo) { + return NULL; + } + return &edges->lo[id]; + } else { + if (!edges->hi) { + return NULL; + } + return ecs_map_get(edges->hi, ecs_edge_t, id); + } } -const uint64_t* flecs_sparse_new_ids( - ecs_sparse_t *sparse, - int32_t new_count) +static +ecs_edge_t* ensure_edge( + ecs_graph_edges_t *edges, + ecs_id_t id) { - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - int32_t dense_count = ecs_vector_count(sparse->dense); - int32_t count = sparse->count; - int32_t remaining = dense_count - count; - int32_t i, to_create = new_count - remaining; - - if (to_create > 0) { - flecs_sparse_set_size(sparse, dense_count + to_create); - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - - for (i = 0; i < to_create; i ++) { - uint64_t index = create_id(sparse, count + i); - dense_array[dense_count + i] = index; + if (id < ECS_HI_COMPONENT_ID) { + if (!edges->lo) { + edges->lo = ecs_os_calloc_n(ecs_edge_t, ECS_HI_COMPONENT_ID); + } + return &edges->lo[id]; + } else { + if (!edges->hi) { + edges->hi = ecs_map_new(ecs_edge_t, 1); } + return ecs_map_ensure(edges->hi, ecs_edge_t, id); } - - sparse->count += new_count; - - return ecs_vector_get(sparse->dense, uint64_t, count); } -void* _flecs_sparse_add( - ecs_sparse_t *sparse, - ecs_size_t size) +static +void init_edges( + ecs_graph_edges_t *edges) { - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); - uint64_t index = new_index(sparse); - chunk_t *chunk = get_chunk(sparse, CHUNK(index)); - ecs_assert(chunk != NULL, ECS_INTERNAL_ERROR, NULL); - return DATA(chunk->data, size, OFFSET(index)); + edges->lo = NULL; + edges->hi = NULL; } -uint64_t flecs_sparse_last_id( - const ecs_sparse_t *sparse) +static +void init_node( + ecs_graph_node_t *node) { - ecs_assert(sparse != NULL, ECS_INTERNAL_ERROR, NULL); - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - return dense_array[sparse->count - 1]; + init_edges(&node->add); + init_edges(&node->remove); } -void* _flecs_sparse_ensure( - ecs_sparse_t *sparse, - ecs_size_t size, - uint64_t index) +static +void init_flags( + ecs_world_t *world, + ecs_table_t *table) { - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ecs_vector_count(sparse->dense) > 0, ECS_INTERNAL_ERROR, NULL); - (void)size; + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); + int32_t count = ecs_vector_count(table->type); + + /* Iterate components to initialize table flags */ + int32_t i; + for (i = 0; i < count; i ++) { + ecs_id_t id = ids[i]; - uint64_t gen = strip_generation(&index); - chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); - int32_t offset = OFFSET(index); - int32_t dense = chunk->sparse[offset]; + /* 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, like prefabs or + * containers */ + if (id <= EcsLastInternalComponentId) { + table->flags |= EcsTableHasBuiltins; + } - if (dense) { - /* Check if element is alive. If element is not alive, update indices so - * that the first unused dense element points to the sparse element. */ - int32_t count = sparse->count; - if (dense == count) { - /* If dense is the next unused element in the array, simply increase - * the count to make it part of the alive set. */ - sparse->count ++; - } else if (dense > count) { - /* If dense is not alive, swap it with the first unused element. */ - swap_dense(sparse, chunk, dense, count); + if (id == EcsModule) { + table->flags |= EcsTableHasBuiltins; + table->flags |= EcsTableHasModule; + } - /* First unused element is now last used element */ - sparse->count ++; - } else { - /* Dense is already alive, nothing to be done */ + if (id == EcsPrefab) { + table->flags |= EcsTableIsPrefab; } - /* Ensure provided generation matches current. Only allow mismatching - * generations if the provided generation count is 0. This allows for - * using the ensure function in combination with ids that have their - * generation stripped. */ - ecs_vector_t *dense_vector = sparse->dense; - uint64_t *dense_array = ecs_vector_first(dense_vector, uint64_t); - ecs_assert(!gen || dense_array[dense] == (index | gen), ECS_INTERNAL_ERROR, NULL); - (void)dense_vector; - (void)dense_array; - } else { - /* Element is not paired yet. Must add a new element to dense array */ - grow_dense(sparse); + /* If table contains disabled entities, mark it as disabled */ + if (id == EcsDisabled) { + table->flags |= EcsTableIsDisabled; + } - ecs_vector_t *dense_vector = sparse->dense; - uint64_t *dense_array = ecs_vector_first(dense_vector, uint64_t); - int32_t dense_count = ecs_vector_count(dense_vector) - 1; - int32_t count = sparse->count ++; + /* Does table have exclusive or columns */ + if (ECS_HAS_ROLE(id, XOR)) { + table->flags |= EcsTableHasXor; + } - /* If index is larger than max id, update max id */ - if (index >= get_id(sparse)) { - set_id(sparse, index + 1); + /* Does table have IsA relations */ + if (ECS_HAS_RELATION(id, EcsIsA)) { + table->flags |= EcsTableHasIsA; } - if (count < dense_count) { - /* If there are unused elements in the list, move the first unused - * element to the end of the list */ - uint64_t unused = dense_array[count]; - chunk_t *unused_chunk = get_or_create_chunk(sparse, CHUNK(unused)); - assign_index(unused_chunk, dense_array, unused, dense_count); + /* Does table have switch columns */ + if (ECS_HAS_ROLE(id, SWITCH)) { + table->flags |= EcsTableHasSwitch; } - assign_index(chunk, dense_array, index, count); - dense_array[count] |= gen; - } + /* Does table support component disabling */ + if (ECS_HAS_ROLE(id, DISABLED)) { + table->flags |= EcsTableHasDisabled; + } - return DATA(chunk->data, sparse->size, offset); + /* Does table have ChildOf relations */ + if (ECS_HAS_RELATION(id, EcsChildOf)) { + ecs_entity_t obj = ecs_pair_object(world, id); + 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; + } + } + } } -void* _flecs_sparse_set( - ecs_sparse_t * sparse, - ecs_size_t elem_size, - uint64_t index, - void* value) +static +void init_table( + ecs_world_t *world, + ecs_table_t *table, + ecs_ids_t *entities) { - void *ptr = _flecs_sparse_ensure(sparse, elem_size, index); - ecs_os_memcpy(ptr, value, elem_size); - return ptr; -} + table->type = ids_to_type(entities); + table->c_info = NULL; + table->flags = 0; + table->dirty_state = NULL; + table->alloc_count = 0; + table->lock = 0; -void* _flecs_sparse_remove_get( - ecs_sparse_t *sparse, - ecs_size_t size, - uint64_t index) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); - (void)size; + /* Ensure the component ids for the table exist */ + ensure_columns(world, table); - chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); - uint64_t gen = strip_generation(&index); - int32_t offset = OFFSET(index); - int32_t dense = chunk->sparse[offset]; + table->queries = NULL; - if (dense) { - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - uint64_t cur_gen = dense_array[dense] & ECS_GENERATION_MASK; - if (gen != cur_gen) { - /* Generation doesn't match which means that the provided entity is - * already not alive. */ - return NULL; - } + init_node(&table->node); + init_flags(world, table); - /* Increase generation */ - dense_array[dense] = index | inc_gen(cur_gen); - - int32_t count = sparse->count; - if (dense == (count - 1)) { - /* If dense is the last used element, simply decrease count */ - sparse->count --; - } else if (dense < count) { - /* If element is alive, move it to unused elements */ - swap_dense(sparse, chunk, dense, count - 1); - sparse->count --; - } else { - /* Element is not alive, nothing to be done */ - return NULL; - } + flecs_register_table(world, table); + flecs_table_init_data(world, table); - /* Reset memory to zero on remove */ - return DATA(chunk->data, sparse->size, offset); - } else { - /* Element is not paired and thus not alive, nothing to be done */ - return NULL; - } + /* Register component info flags for all columns */ + flecs_table_notify(world, table, &(ecs_table_event_t){ + .kind = EcsTableComponentInfo + }); } -void flecs_sparse_remove( - ecs_sparse_t *sparse, - uint64_t index) +static +ecs_table_t *create_table( + ecs_world_t *world, + ecs_ids_t *entities, + flecs_hashmap_result_t table_elem) { - void *ptr = _flecs_sparse_remove_get(sparse, 0, index); - if (ptr) { - ecs_os_memset(ptr, 0, sparse->size); - } -} + ecs_table_t *result = flecs_sparse_add(world->store.tables, ecs_table_t); + result->id = flecs_sparse_last_id(world->store.tables); -void flecs_sparse_set_generation( - ecs_sparse_t *sparse, - uint64_t index) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); - - uint64_t index_w_gen = index; - strip_generation(&index); - int32_t offset = OFFSET(index); - int32_t dense = chunk->sparse[offset]; + ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL); - if (dense) { - /* Increase generation */ - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - dense_array[dense] = index_w_gen; - } else { - /* Element is not paired and thus not alive, nothing to be done */ - } -} + init_table(world, result, entities); -bool flecs_sparse_exists( - const ecs_sparse_t *sparse, - uint64_t index) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - chunk_t *chunk = get_chunk(sparse, CHUNK(index)); - if (!chunk) { - return false; - } - - strip_generation(&index); - int32_t offset = OFFSET(index); - int32_t dense = chunk->sparse[offset]; +#ifndef NDEBUG + char *expr = ecs_type_str(world, result->type); + ecs_dbg_1("table #[green][%s]#[normal] created", expr); + ecs_os_free(expr); +#endif + ecs_log_push(); - return dense != 0; -} + /* Store table in table hashmap */ + *(ecs_table_t**)table_elem.value = result; -void* _flecs_sparse_get_dense( - const ecs_sparse_t *sparse, - ecs_size_t size, - int32_t dense_index) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); - ecs_assert(dense_index < sparse->count, ECS_INVALID_PARAMETER, NULL); - (void)size; + /* Set keyvalue to one that has the same lifecycle as the table */ + ecs_ids_t key = { + .array = ecs_vector_first(result->type, ecs_id_t), + .count = ecs_vector_count(result->type) + }; + *(ecs_ids_t*)table_elem.key = key; - dense_index ++; + flecs_notify_queries(world, &(ecs_query_event_t) { + .kind = EcsQueryTableMatch, + .table = result + }); - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - return get_sparse(sparse, dense_index, dense_array[dense_index]); -} + ecs_log_pop(); -bool flecs_sparse_is_alive( - const ecs_sparse_t *sparse, - uint64_t index) -{ - return try_sparse(sparse, index) != NULL; + return result; } -uint64_t flecs_sparse_get_alive( - const ecs_sparse_t *sparse, - uint64_t index) +static +void add_id_to_ids( + ecs_type_t type, + ecs_entity_t add, + ecs_ids_t *out) { - chunk_t *chunk = get_chunk(sparse, CHUNK(index)); - if (!chunk) { - return 0; - } + int32_t count = ecs_vector_count(type); + ecs_id_t *array = ecs_vector_first(type, ecs_id_t); + bool added = false; - int32_t offset = OFFSET(index); - int32_t dense = chunk->sparse[offset]; - uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + int32_t i, el = 0; + for (i = 0; i < count; i ++) { + ecs_id_t e = array[i]; - /* If dense is 0 (tombstone) this will return 0 */ - return dense_array[dense]; -} + if (e >= add && !added) { + if (e != add) { + out->array[el ++] = add; + } + added = true; + } + + out->array[el ++] = e; + ecs_assert(el <= out->count, ECS_INTERNAL_ERROR, NULL); + } -void* _flecs_sparse_get( - const ecs_sparse_t *sparse, - ecs_size_t size, - uint64_t index) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); - (void)size; - return try_sparse(sparse, index); -} + if (!added) { + out->array[el ++] = add; + } -void* _flecs_sparse_get_any( - ecs_sparse_t *sparse, - ecs_size_t size, - uint64_t index) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); - (void)size; - return try_sparse_any(sparse, index); + out->count = el; } -int32_t flecs_sparse_count( - const ecs_sparse_t *sparse) +static +void remove_id_from_ids( + ecs_type_t type, + ecs_entity_t remove, + ecs_ids_t *out) { - if (!sparse) { - return 0; + int32_t count = ecs_vector_count(type); + ecs_id_t *array = ecs_vector_first(type, ecs_id_t); + + int32_t i, el = 0; + for (i = 0; i < count; i ++) { + ecs_id_t e = array[i]; + if (e != remove) { + out->array[el ++] = e; + ecs_assert(el <= count, ECS_INTERNAL_ERROR, NULL); + } } - return sparse->count - 1; + out->count = el; } -int32_t flecs_sparse_size( - const ecs_sparse_t *sparse) +int32_t flecs_table_switch_from_case( + const ecs_world_t *world, + const ecs_table_t *table, + ecs_entity_t add) { - if (!sparse) { - return 0; + ecs_type_t type = table->type; + ecs_entity_t *array = ecs_vector_first(type, ecs_entity_t); + + int32_t i, count = table->sw_column_count; + ecs_assert(count != 0, ECS_INTERNAL_ERROR, NULL); + + add = add & ECS_COMPONENT_MASK; + + ecs_sw_column_t *sw_columns = NULL; + + if ((sw_columns = table->storage.sw_columns)) { + /* Fast path, we can get the switch type from the column data */ + for (i = 0; i < count; i ++) { + ecs_type_t sw_type = sw_columns[i].type; + if (ecs_type_has_id(world, sw_type, add, true)) { + return i; + } + } + } else { + /* Slow path, table is empty, so we'll have to get the switch types by + * actually inspecting the switch type entities. */ + for (i = 0; i < count; i ++) { + ecs_entity_t e = array[i + table->sw_column_offset]; + ecs_assert(ECS_HAS_ROLE(e, SWITCH), ECS_INTERNAL_ERROR, NULL); + e = e & ECS_COMPONENT_MASK; + + const EcsType *type_ptr = ecs_get(world, e, EcsType); + ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + + if (ecs_type_has_id( + world, type_ptr->normalized, add, true)) + { + return i; + } + } } - - return ecs_vector_count(sparse->dense) - 1; -} -const uint64_t* flecs_sparse_ids( - const ecs_sparse_t *sparse) -{ - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - return &(ecs_vector_first(sparse->dense, uint64_t)[1]); + /* If a table was not found, this is an invalid switch case */ + ecs_abort(ECS_TYPE_INVALID_CASE, NULL); + + return -1; } -void flecs_sparse_set_size( - ecs_sparse_t *sparse, - int32_t elem_count) +static +void ids_append( + ecs_ids_t *ids, + ecs_id_t id) { - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_vector_set_size(&sparse->dense, uint64_t, elem_count); + ids->array = ecs_os_realloc_n(ids->array, ecs_id_t, ids->count + 1); + ids->array[ids->count ++] = id; } static -void sparse_copy( - ecs_sparse_t * dst, - const ecs_sparse_t * src) +void diff_insert_isa( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_diff_t *base_diff, + ecs_ids_t *append_to, + ecs_ids_t *append_from, + ecs_id_t add) { - flecs_sparse_set_size(dst, flecs_sparse_size(src)); - const uint64_t *indices = flecs_sparse_ids(src); - - ecs_size_t size = src->size; - int32_t i, count = src->count; - - for (i = 0; i < count - 1; i ++) { - uint64_t index = indices[i]; - void *src_ptr = _flecs_sparse_get(src, size, index); - void *dst_ptr = _flecs_sparse_ensure(dst, size, index); - flecs_sparse_set_generation(dst, index); - ecs_os_memcpy(dst_ptr, src_ptr, size); + ecs_entity_t base = ecs_pair_object(world, add); + ecs_table_t *base_table = ecs_get_table(world, base); + if (!base_table) { + return; } - set_id(dst, get_id(src)); - - ecs_assert(src->count == dst->count, ECS_INTERNAL_ERROR, NULL); -} + ecs_type_t base_type = base_table->type, type = table->type; + ecs_table_t *table_wo_base = base_table; -ecs_sparse_t* flecs_sparse_copy( - const ecs_sparse_t *src) -{ - if (!src) { - return NULL; - } + /* If the table does not have a component from the base, it should + * trigger an OnSet */ + ecs_id_t *ids = ecs_vector_first(base_type, ecs_id_t); + int32_t j, i, count = ecs_vector_count(base_type); + for (i = 0; i < count; i ++) { + ecs_id_t id = ids[i]; - ecs_sparse_t *dst = _flecs_sparse_new(src->size); - sparse_copy(dst, src); + if (ECS_HAS_RELATION(id, EcsIsA)) { + /* The base has an IsA relation. Find table without the base, which + * gives us the list of ids the current base inherits and doesn't + * override. This saves us from having to recursively check for each + * base in the hierarchy whether the component is overridden. */ + table_wo_base = flecs_table_traverse_remove( + world, table_wo_base, &id, base_diff); - return dst; -} + /* Because we removed, the ids are stored in un_set vs. on_set */ + for (j = 0; j < append_from->count; j ++) { + ecs_id_t base_id = append_from->array[j]; + /* We still have to make sure the id isn't overridden by the + * current table */ + if (ecs_type_match( + world, table, type, 0, base_id, 0, 0, 0, NULL, NULL) == -1) + { + ids_append(append_to, base_id); + } + } -void flecs_sparse_restore( - ecs_sparse_t * dst, - const ecs_sparse_t * src) -{ - ecs_assert(dst != NULL, ECS_INVALID_PARAMETER, NULL); - dst->count = 1; - if (src) { - sparse_copy(dst, src); - } -} + continue; + } -void flecs_sparse_memory( - ecs_sparse_t *sparse, - int32_t *allocd, - int32_t *used) -{ - (void)sparse; - (void)allocd; - (void)used; -} + /* Identifiers are not inherited */ + if (ECS_HAS_RELATION(id, ecs_id(EcsIdentifier))) { + continue; + } -ecs_sparse_t* _ecs_sparse_new( - ecs_size_t elem_size) -{ - return _flecs_sparse_new(elem_size); -} + if (!ecs_get_typeid(world, id)) { + continue; + } -void* _ecs_sparse_add( - ecs_sparse_t *sparse, - ecs_size_t elem_size) -{ - return _flecs_sparse_add(sparse, elem_size); + if (ecs_type_match(world, table, type, 0, id, 0, 0, 0, NULL, NULL) == -1) { + ids_append(append_to, id); + } + } } -uint64_t ecs_sparse_last_id( - const ecs_sparse_t *sparse) +static +void diff_insert_added_isa( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_diff_t *diff, + ecs_id_t id) { - return flecs_sparse_last_id(sparse); + ecs_table_diff_t base_diff; + diff_insert_isa(world, table, &base_diff, &diff->on_set, + &base_diff.un_set, id); } -int32_t ecs_sparse_count( - const ecs_sparse_t *sparse) +static +void diff_insert_removed_isa( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_diff_t *diff, + ecs_id_t id) { - return flecs_sparse_count(sparse); + ecs_table_diff_t base_diff; + diff_insert_isa(world, table, &base_diff, &diff->un_set, + &base_diff.un_set, id); } -void* _ecs_sparse_get_dense( - const ecs_sparse_t *sparse, - ecs_size_t elem_size, - int32_t index) +static +void diff_insert_added( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_diff_t *diff, + ecs_id_t id) { - return _flecs_sparse_get_dense(sparse, elem_size, index); -} + diff->added.array[diff->added.count ++] = id; -void* _ecs_sparse_get( - const ecs_sparse_t *sparse, - ecs_size_t elem_size, - uint64_t id) -{ - return _flecs_sparse_get(sparse, elem_size, id); + if (ECS_HAS_RELATION(id, EcsIsA)) { + diff_insert_added_isa(world, table, diff, id); + } } -ecs_sparse_iter_t _flecs_sparse_iter( - ecs_sparse_t *sparse, - ecs_size_t elem_size) +static +void diff_insert_removed( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_diff_t *diff, + ecs_id_t id) { - ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(elem_size == sparse->size, ECS_INVALID_PARAMETER, NULL); - ecs_sparse_iter_t result; - result.sparse = sparse; - result.ids = flecs_sparse_ids(sparse); - result.size = elem_size; - result.i = 0; - result.count = sparse->count - 1; - return result; -} + diff->removed.array[diff->removed.count ++] = id; -#ifdef FLECS_SANITIZE -static -void verify_nodes( - flecs_switch_header_t *hdr, - flecs_switch_node_t *nodes) -{ - if (!hdr) { + if (ECS_HAS_RELATION(id, EcsIsA)) { + /* Removing an IsA relation also "removes" all components from the + * instance. Any id from base that's not overridden should be UnSet. */ + diff_insert_removed_isa(world, table, diff, id); return; } - int32_t prev = -1, elem = hdr->element, count = 0; - while (elem != -1) { - ecs_assert(prev == nodes[elem].prev, ECS_INTERNAL_ERROR, NULL); - prev = elem; - elem = nodes[elem].next; - count ++; + if (table->flags & EcsTableHasIsA) { + if (!ecs_get_typeid(world, id)) { + /* Do nothing if id is not a component */ + return; + } + + /* If next table has a base and component is removed, check if + * the removed component was an override. Removed overrides reexpose the + * base component, thus "changing" the value which requires an OnSet. */ + if (ecs_type_match(world, table, table->type, 0, id, EcsIsA, + 1, 0, NULL, NULL) != -1) + { + ids_append(&diff->on_set, id); + return; + } } - ecs_assert(count == hdr->count, ECS_INTERNAL_ERROR, NULL); + if (ecs_get_typeid(world, id) != 0) { + ids_append(&diff->un_set, id); + } } -#else -#define verify_nodes(hdr, nodes) -#endif static -flecs_switch_header_t *get_header( - const ecs_switch_t *sw, - uint64_t value) +void compute_table_diff( + ecs_world_t *world, + ecs_table_t *node, + ecs_table_t *next, + ecs_edge_t *edge, + ecs_id_t id) { - if (value == 0) { - return NULL; - } - - value = (uint32_t)value; + ecs_type_t node_type = node->type; + ecs_type_t next_type = next->type; - ecs_assert(value >= sw->min, ECS_INTERNAL_ERROR, NULL); - ecs_assert(value <= sw->max, ECS_INTERNAL_ERROR, NULL); + ecs_id_t *ids_node = ecs_vector_first(node_type, ecs_id_t); + ecs_id_t *ids_next = ecs_vector_first(next_type, ecs_id_t); + int32_t i_node = 0, node_count = ecs_vector_count(node_type); + int32_t i_next = 0, next_count = ecs_vector_count(next_type); + int32_t added_count = 0; + int32_t removed_count = 0; + bool trivial_edge = !ECS_HAS_RELATION(id, EcsIsA) && + !(node->flags & EcsTableHasIsA) && !(next->flags & EcsTableHasIsA); - uint64_t index = value - sw->min; + /* First do a scan to see how big the diff is, so we don't have to realloc + * or alloc more memory than required. */ + for (; i_node < node_count && i_next < next_count; ) { + ecs_id_t id_node = ids_node[i_node]; + ecs_id_t id_next = ids_next[i_next]; - return &sw->headers[index]; -} + bool added = id_next < id_node; + bool removed = id_node < id_next; -static -void remove_node( - flecs_switch_header_t *hdr, - flecs_switch_node_t *nodes, - flecs_switch_node_t *node, - int32_t element) -{ - ecs_assert(&nodes[element] == node, ECS_INTERNAL_ERROR, NULL); + trivial_edge &= !added || id_next == id; + trivial_edge &= !removed || id_node == id; - /* Update previous node/header */ - if (hdr->element == element) { - ecs_assert(node->prev == -1, ECS_INVALID_PARAMETER, NULL); - /* If this is the first node, update the header */ - hdr->element = node->next; - } else { - /* If this is not the first node, update the previous node to the - * removed node's next ptr */ - ecs_assert(node->prev != -1, ECS_INVALID_PARAMETER, NULL); - flecs_switch_node_t *prev_node = &nodes[node->prev]; - prev_node->next = node->next; - } + added_count += added; + removed_count += removed; - /* Update next node */ - int32_t next = node->next; - if (next != -1) { - ecs_assert(next >= 0, ECS_INVALID_PARAMETER, NULL); - /* If this is not the last node, update the next node to point to the - * removed node's prev ptr */ - flecs_switch_node_t *next_node = &nodes[next]; - next_node->prev = node->prev; + i_node += id_node <= id_next; + i_next += id_next <= id_node; } - /* Decrease count of current header */ - hdr->count --; - ecs_assert(hdr->count >= 0, ECS_INTERNAL_ERROR, NULL); -} + added_count += next_count - i_next; + removed_count += node_count - i_node; -ecs_switch_t* flecs_switch_new( - uint64_t min, - uint64_t max, - int32_t elements) -{ - ecs_assert(min <= max, ECS_INVALID_PARAMETER, NULL); + trivial_edge &= (added_count + removed_count) <= 1; - /* Min must be larger than 0, as 0 is an invalid entity id, and should - * therefore never occur as case id */ - ecs_assert(min > 0, ECS_INVALID_PARAMETER, NULL); + if (trivial_edge) { + /* If edge is trivial there's no need to create a diff element for it. + * Encode in the id whether the id is a tag or not, so that wen can + * still tell whether an UnSet handler should be called or not. */ + edge->diff_index = -1 * (ecs_get_typeid(world, id) == 0); + return; + } - ecs_switch_t *result = ecs_os_malloc(ECS_SIZEOF(ecs_switch_t)); - result->min = (uint32_t)min; - result->max = (uint32_t)max; + ecs_table_diff_t *diff = ecs_vector_add(&node->node.diffs, ecs_table_diff_t); + ecs_os_memset_t(diff, 0, ecs_table_diff_t); + edge->diff_index = ecs_vector_count(node->node.diffs); + if (added_count) { + diff->added.array = ecs_os_malloc_n(ecs_id_t, added_count); + diff->added.count = 0; + diff->added.size = added_count; + } + if (removed_count) { + diff->removed.array = ecs_os_malloc_n(ecs_id_t, removed_count); + diff->removed.count = 0; + diff->removed.size = removed_count; + } - int32_t count = (int32_t)(max - min) + 1; - result->headers = ecs_os_calloc(ECS_SIZEOF(flecs_switch_header_t) * count); - result->nodes = ecs_vector_new(flecs_switch_node_t, elements); - result->values = ecs_vector_new(uint64_t, elements); + for (i_node = 0, i_next = 0; i_node < node_count && i_next < next_count; ) { + ecs_id_t id_node = ids_node[i_node]; + ecs_id_t id_next = ids_next[i_next]; - int64_t i; - for (i = 0; i < count; i ++) { - result->headers[i].element = -1; - result->headers[i].count = 0; - } + if (id_next < id_node) { + diff_insert_added(world, node, diff, id_next); + } else if (id_node < id_next) { + diff_insert_removed(world, next, diff, id_node); + } - flecs_switch_node_t *nodes = ecs_vector_first( - result->nodes, flecs_switch_node_t); - uint64_t *values = ecs_vector_first( - result->values, uint64_t); + i_node += id_node <= id_next; + i_next += id_next <= id_node; + } - for (i = 0; i < elements; i ++) { - nodes[i].prev = -1; - nodes[i].next = -1; - values[i] = 0; + for (; i_next < next_count; i_next ++) { + diff_insert_added(world, node, diff, ids_next[i_next]); + } + for (; i_node < node_count; i_node ++) { + diff_insert_removed(world, next, diff, ids_node[i_next]); } - return result; + ecs_assert(diff->added.count == added_count, ECS_INTERNAL_ERROR, NULL); + ecs_assert(diff->removed.count == removed_count, ECS_INTERNAL_ERROR, NULL); } -void flecs_switch_free( - ecs_switch_t *sw) +static +ecs_table_t* find_or_create_table_with_id( + ecs_world_t *world, + ecs_table_t *node, + ecs_entity_t id) { - ecs_os_free(sw->headers); - ecs_vector_free(sw->nodes); - ecs_vector_free(sw->values); - ecs_os_free(sw); -} + /* If table has one or more switches and this is a case, return self */ + if (ECS_HAS_ROLE(id, CASE)) { + ecs_assert((node->flags & EcsTableHasSwitch) != 0, + ECS_TYPE_INVALID_CASE, NULL); + return node; + } else { + ecs_type_t type = node->type; + int32_t count = ecs_vector_count(type); -void flecs_switch_add( - ecs_switch_t *sw) -{ - flecs_switch_node_t *node = ecs_vector_add(&sw->nodes, flecs_switch_node_t); - uint64_t *value = ecs_vector_add(&sw->values, uint64_t); - node->prev = -1; - node->next = -1; - *value = 0; + ecs_ids_t ids = { + .array = ecs_os_alloca_n(ecs_id_t, count + 1), + .count = count + 1 + }; + + add_id_to_ids(type, id, &ids); + + return flecs_table_find_or_create(world, &ids); + } } -void flecs_switch_set_count( - ecs_switch_t *sw, - int32_t count) +static +ecs_table_t* find_or_create_table_without_id( + ecs_world_t *world, + ecs_table_t *node, + ecs_entity_t id) { - int32_t old_count = ecs_vector_count(sw->nodes); - if (old_count == count) { - return; - } + /* If table has one or more switches and this is a case, return self */ + if (ECS_HAS_ROLE(id, CASE)) { + ecs_assert((node->flags & EcsTableHasSwitch) != 0, + ECS_TYPE_INVALID_CASE, NULL); + return node; + } else { + ecs_type_t type = node->type; + int32_t count = ecs_vector_count(type); - ecs_vector_set_count(&sw->nodes, flecs_switch_node_t, count); - ecs_vector_set_count(&sw->values, uint64_t, count); + ecs_ids_t ids = { + .array = ecs_os_alloca_n(ecs_id_t, count), + .count = count + }; - flecs_switch_node_t *nodes = ecs_vector_first(sw->nodes, flecs_switch_node_t); - uint64_t *values = ecs_vector_first(sw->values, uint64_t); + remove_id_from_ids(type, id, &ids); - int32_t i; - for (i = old_count; i < count; i ++) { - flecs_switch_node_t *node = &nodes[i]; - node->prev = -1; - node->next = -1; - values[i] = 0; + return flecs_table_find_or_create(world, &ids);; } } -void flecs_switch_ensure( - ecs_switch_t *sw, - int32_t count) +static +ecs_table_t* find_or_create_table_with_isa( + ecs_world_t *world, + ecs_table_t *node, + ecs_entity_t base) { - int32_t old_count = ecs_vector_count(sw->nodes); - if (old_count >= count) { - return; + ecs_type_t base_type = ecs_get_type(world, base); + ecs_id_t *ids = ecs_vector_first(base_type, ecs_id_t); + int32_t i, count = ecs_vector_count(base_type); + + /* Start from back, as roles have high ids */ + for (i = count - 1; i >= 0; i --) { + ecs_id_t id = ids[i]; + if (!(id & ECS_ROLE_MASK)) { /* early out if we found everything */ + break; + } + + if (ECS_HAS_RELATION(id, EcsIsA)) { + ecs_entity_t base_of_base = ecs_pair_object(world, id); + node = find_or_create_table_with_isa(world, node, base_of_base); + } + + if (ECS_HAS_ROLE(id, OVERRIDE)) { + /* Override found, add it to table */ + id &= ECS_COMPONENT_MASK; + node = flecs_table_traverse_add(world, node, &id, NULL); + } } - flecs_switch_set_count(sw, count); + return node; } -void flecs_switch_addn( - ecs_switch_t *sw, - int32_t count) +static +ecs_table_t* find_or_create_table_without( + ecs_world_t *world, + ecs_table_t *node, + ecs_edge_t *edge, + ecs_id_t id) { - int32_t old_count = ecs_vector_count(sw->nodes); - flecs_switch_set_count(sw, old_count + count); -} + ecs_table_t *next = find_or_create_table_without_id(world, node, id); -void flecs_switch_set( - ecs_switch_t *sw, - int32_t element, - uint64_t value) -{ - ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); - ecs_assert(element < ecs_vector_count(sw->values), ECS_INVALID_PARAMETER, NULL); - ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); + edge->next = next; - uint64_t *values = ecs_vector_first(sw->values, uint64_t); - uint64_t cur_value = values[element]; + compute_table_diff(world, node, next, edge, id); - /* If the node is already assigned to the value, nothing to be done */ - if (cur_value == value) { - return; + if (node != next) { + flecs_register_remove_ref(world, node, id); } - flecs_switch_node_t *nodes = ecs_vector_first(sw->nodes, flecs_switch_node_t); - flecs_switch_node_t *node = &nodes[element]; + return next; +} - flecs_switch_header_t *cur_hdr = get_header(sw, cur_value); - flecs_switch_header_t *dst_hdr = get_header(sw, value); +static +ecs_table_t* find_or_create_table_with( + ecs_world_t *world, + ecs_table_t *node, + ecs_edge_t *edge, + ecs_id_t id) +{ + ecs_table_t *next = find_or_create_table_with_id(world, node, id); - verify_nodes(cur_hdr, nodes); - verify_nodes(dst_hdr, nodes); + if (ECS_HAS_ROLE(id, PAIR) && ECS_PAIR_RELATION(id) == EcsIsA) { + ecs_entity_t base = ecs_pair_object(world, id); + next = find_or_create_table_with_isa(world, next, base); + } - /* If value is not 0, and dst_hdr is NULL, then this is not a valid value - * for this switch */ - ecs_assert(dst_hdr != NULL || !value, ECS_INVALID_PARAMETER, NULL); + edge->next = next; - if (cur_hdr) { - remove_node(cur_hdr, nodes, node, element); + compute_table_diff(world, node, next, edge, id); + + if (node != next) { + flecs_register_add_ref(world, node, id); } - /* Now update the node itself by adding it as the first node of dst */ - node->prev = -1; - values[element] = value; + return next; +} - if (dst_hdr) { - node->next = dst_hdr->element; +static +void populate_diff( + ecs_table_t *table, + ecs_edge_t *edge, + ecs_id_t *add_ptr, + ecs_id_t *remove_ptr, + ecs_table_diff_t *out) +{ + if (out) { + int32_t di = edge->diff_index; + if (di > 0) { + *out = ecs_vector_first(table->node.diffs, ecs_table_diff_t)[di - 1]; + } else { + out->on_set.count = 0; - /* Also update the dst header */ - int32_t first = dst_hdr->element; - if (first != -1) { - ecs_assert(first >= 0, ECS_INTERNAL_ERROR, NULL); - flecs_switch_node_t *first_node = &nodes[first]; - first_node->prev = element; - } + if (add_ptr) { + out->added.array = add_ptr; + out->added.count = 1; + } else { + out->added.count = 0; + } - dst_hdr->element = element; - dst_hdr->count ++; + if (remove_ptr) { + out->removed.array = remove_ptr; + out->removed.count = 1; + if (di == 0) { + out->un_set.array = remove_ptr; + out->un_set.count = 1; + } else { + out->un_set.count = 0; + } + } else { + out->removed.count = 0; + out->un_set.count = 0; + } + } } } -void flecs_switch_remove( - ecs_switch_t *sw, - int32_t element) +ecs_table_t* flecs_table_traverse_remove( + ecs_world_t *world, + ecs_table_t *node, + ecs_id_t *id_ptr, + ecs_table_diff_t *diff) { - ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); - ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); - - uint64_t *values = ecs_vector_first(sw->values, uint64_t); - uint64_t value = values[element]; - flecs_switch_node_t *nodes = ecs_vector_first(sw->nodes, flecs_switch_node_t); - flecs_switch_node_t *node = &nodes[element]; + ecs_poly_assert(world, ecs_world_t); - /* If node is currently assigned to a case, remove it from the list */ - if (value != 0) { - flecs_switch_header_t *hdr = get_header(sw, value); - ecs_assert(hdr != NULL, ECS_INTERNAL_ERROR, NULL); + node = node ? node : &world->store.root; - verify_nodes(hdr, nodes); - remove_node(hdr, nodes, node, element); - } + /* Removing 0 from an entity is not valid */ + ecs_assert(id_ptr != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(id_ptr[0] != 0, ECS_INVALID_PARAMETER, NULL); - int32_t last_elem = ecs_vector_count(sw->nodes) - 1; - if (last_elem != element) { - flecs_switch_node_t *last = ecs_vector_last(sw->nodes, flecs_switch_node_t); - int32_t next = last->next, prev = last->prev; - if (next != -1) { - flecs_switch_node_t *n = &nodes[next]; - n->prev = element; - } + ecs_id_t id = id_ptr[0]; + ecs_edge_t *edge = ensure_edge(&node->node.remove, id); + ecs_table_t *next = edge->next; - if (prev != -1) { - flecs_switch_node_t *n = &nodes[prev]; - n->next = element; - } else { - flecs_switch_header_t *hdr = get_header(sw, values[last_elem]); - if (hdr && hdr->element != -1) { - ecs_assert(hdr->element == last_elem, - ECS_INTERNAL_ERROR, NULL); - hdr->element = element; - } - } + if (!next) { + next = find_or_create_table_without(world, node, edge, id); + ecs_assert(next != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(edge->next != NULL, ECS_INTERNAL_ERROR, NULL); } - /* Remove element from arrays */ - ecs_vector_remove(sw->nodes, flecs_switch_node_t, element); - ecs_vector_remove(sw->values, uint64_t, element); + populate_diff(node, edge, NULL, id_ptr, diff); + + return next; } -uint64_t flecs_switch_get( - const ecs_switch_t *sw, - int32_t element) +ecs_table_t* flecs_table_traverse_add( + ecs_world_t *world, + ecs_table_t *node, + ecs_id_t *id_ptr, + ecs_table_diff_t *diff) { - ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); - ecs_assert(element < ecs_vector_count(sw->values), ECS_INVALID_PARAMETER, NULL); - ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); + ecs_poly_assert(world, ecs_world_t); - uint64_t *values = ecs_vector_first(sw->values, uint64_t); - return values[element]; -} + node = node ? node : &world->store.root; -ecs_vector_t* flecs_switch_values( - const ecs_switch_t *sw) -{ - return sw->values; -} + /* Adding 0 to an entity is not valid */ + ecs_assert(id_ptr != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(id_ptr[0] != 0, ECS_INVALID_PARAMETER, NULL); -int32_t flecs_switch_case_count( - const ecs_switch_t *sw, - uint64_t value) -{ - flecs_switch_header_t *hdr = get_header(sw, value); - if (!hdr) { - return 0; - } + ecs_id_t id = id_ptr[0]; + ecs_edge_t *edge = ensure_edge(&node->node.add, id); + ecs_table_t *next = edge->next; - return hdr->count; -} + if (!next) { + next = find_or_create_table_with(world, node, edge, id); + ecs_assert(next != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(edge->next != NULL, ECS_INTERNAL_ERROR, NULL); + } -void flecs_switch_swap( - ecs_switch_t *sw, - int32_t elem_1, - int32_t elem_2) -{ - uint64_t v1 = flecs_switch_get(sw, elem_1); - uint64_t v2 = flecs_switch_get(sw, elem_2); + populate_diff(node, edge, id_ptr, NULL, diff); - flecs_switch_set(sw, elem_2, v1); - flecs_switch_set(sw, elem_1, v2); + return next; } -int32_t flecs_switch_first( - const ecs_switch_t *sw, - uint64_t value) +static +bool ecs_entity_array_is_ordered( + const ecs_ids_t *entities) { - ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert((uint32_t)value <= sw->max, ECS_INVALID_PARAMETER, NULL); - ecs_assert((uint32_t)value >= sw->min, ECS_INVALID_PARAMETER, NULL); - - flecs_switch_header_t *hdr = get_header(sw, value); - ecs_assert(hdr != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_entity_t prev = 0; + ecs_entity_t *array = entities->array; + int32_t i, count = entities->count; - return hdr->element; + for (i = 0; i < count; i ++) { + if (!array[i] && !prev) { + continue; + } + if (array[i] <= prev) { + return false; + } + prev = array[i]; + } + + return true; } -int32_t flecs_switch_next( - const ecs_switch_t *sw, - int32_t element) +static +int32_t ecs_entity_array_dedup( + ecs_entity_t *array, + int32_t count) { - ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); - ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); + int32_t j, k; + ecs_entity_t prev = array[0]; - flecs_switch_node_t *nodes = ecs_vector_first( - sw->nodes, flecs_switch_node_t); + for (k = j = 1; k < count; j ++, k++) { + ecs_entity_t e = array[k]; + if (e == prev) { + k ++; + } - return nodes[element].next; + array[j] = e; + prev = e; + } + + return count - (k - j); } -#ifndef _MSC_VER -#pragma GCC diagnostic ignored "-Wimplicit-fallthrough" -#endif +static +ecs_table_t* find_or_create( + ecs_world_t *world, + const ecs_ids_t *ids) +{ + ecs_poly_assert(world, ecs_world_t); -/* See explanation below. The hashing function may read beyond the memory passed - * into the hashing function, but only at word boundaries. This should be safe, - * but trips up address sanitizers and valgrind. - * This ensures clean valgrind logs in debug mode & the best perf in release */ -#if !defined(NDEBUG) || defined(ADDRESS_SANITIZER) -#ifndef VALGRIND -#define VALGRIND -#endif -#endif + /* Make sure array is ordered and does not contain duplicates */ + int32_t type_count = ids->count; + ecs_id_t *ordered = NULL; -/* -------------------------------------------------------------------------------- -lookup3.c, by Bob Jenkins, May 2006, Public Domain. - http://burtleburtle.net/bob/c/lookup3.c -------------------------------------------------------------------------------- -*/ + if (!type_count) { + return &world->store.root; + } -#ifdef _MSC_VER -//FIXME -#else -#include /* attempt to define endianness */ -#endif -#ifdef linux -# include /* attempt to define endianness */ -#endif + if (!ecs_entity_array_is_ordered(ids)) { + ecs_size_t size = ECS_SIZEOF(ecs_entity_t) * type_count; + ordered = ecs_os_alloca(size); + ecs_os_memcpy(ordered, ids->array, size); + qsort(ordered, (size_t)type_count, sizeof(ecs_entity_t), + flecs_entity_compare_qsort); + type_count = ecs_entity_array_dedup(ordered, type_count); + } else { + ordered = ids->array; + } -/* - * My best guess at if you are big-endian or little-endian. This may - * need adjustment. - */ -#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ - __BYTE_ORDER == __LITTLE_ENDIAN) || \ - (defined(i386) || defined(__i386__) || defined(__i486__) || \ - defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL)) -# define HASH_LITTLE_ENDIAN 1 -#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ - __BYTE_ORDER == __BIG_ENDIAN) || \ - (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) -# define HASH_LITTLE_ENDIAN 0 -#else -# define HASH_LITTLE_ENDIAN 0 -#endif + ecs_ids_t ordered_ids = { + .array = ordered, + .count = type_count + }; -#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) + ecs_table_t *table; + flecs_hashmap_result_t elem = flecs_hashmap_ensure( + world->store.table_map, &ordered_ids, ecs_table_t*); + if ((table = *(ecs_table_t**)elem.value)) { + return table; + } -/* -------------------------------------------------------------------------------- -mix -- mix 3 32-bit values reversibly. -This is reversible, so any information in (a,b,c) before mix() is -still in (a,b,c) after mix(). -If four pairs of (a,b,c) inputs are run through mix(), or through -mix() in reverse, there are at least 32 bits of the output that -are sometimes the same for one pair and different for another pair. -This was tested for: -* pairs that differed by one bit, by two bits, in any combination - of top bits of (a,b,c), or in any combination of bottom bits of - (a,b,c). -* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - is commonly produced by subtraction) look like a single 1-bit - difference. -* the base values were pseudorandom, all zero but one bit set, or - all zero plus a counter that starts at zero. -Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that -satisfy this are - 4 6 8 16 19 4 - 9 15 3 18 27 15 - 14 9 3 7 17 3 -Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing -for "differ" defined as + with a one-bit base and a two-bit delta. I -used http://burtleburtle.net/bob/hash/avalanche.html to choose -the operations, constants, and arrangements of the variables. -This does not achieve avalanche. There are input bits of (a,b,c) -that fail to affect some output bits of (a,b,c), especially of a. The -most thoroughly mixed value is c, but it doesn't really even achieve -avalanche in c. -This allows some parallelism. Read-after-writes are good at doubling -the number of bits affected, so the goal of mixing pulls in the opposite -direction as the goal of parallelism. I did what I could. Rotates -seem to cost as much as shifts on every machine I could lay my hands -on, and rotates are much kinder to the top and bottom bits, so I used -rotates. -------------------------------------------------------------------------------- -*/ -#define mix(a,b,c) \ -{ \ - a -= c; a ^= rot(c, 4); c += b; \ - b -= a; b ^= rot(a, 6); a += c; \ - c -= b; c ^= rot(b, 8); b += a; \ - a -= c; a ^= rot(c,16); c += b; \ - b -= a; b ^= rot(a,19); a += c; \ - c -= b; c ^= rot(b, 4); b += a; \ + /* If we get here, table needs to be created which is only allowed when the + * application is not currently in progress */ + ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); + + /* If we get here, the table has not been found, so create it. */ + ecs_table_t *result = create_table(world, &ordered_ids, elem); + + ecs_assert(ordered_ids.count == ecs_vector_count(result->type), + ECS_INTERNAL_ERROR, NULL); + + return result; } -/* -------------------------------------------------------------------------------- -final -- final mixing of 3 32-bit values (a,b,c) into c -Pairs of (a,b,c) values differing in only a few bits will usually -produce values of c that look totally different. This was tested for -* pairs that differed by one bit, by two bits, in any combination - of top bits of (a,b,c), or in any combination of bottom bits of - (a,b,c). -* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - is commonly produced by subtraction) look like a single 1-bit - difference. -* the base values were pseudorandom, all zero but one bit set, or - all zero plus a counter that starts at zero. -These constants passed: - 14 11 25 16 4 14 24 - 12 14 25 16 4 14 24 -and these came close: - 4 8 15 26 3 22 24 - 10 8 15 26 3 22 24 - 11 8 15 26 3 22 24 -------------------------------------------------------------------------------- -*/ -#define final(a,b,c) \ -{ \ - c ^= b; c -= rot(b,14); \ - a ^= c; a -= rot(c,11); \ - b ^= a; b -= rot(a,25); \ - c ^= b; c -= rot(b,16); \ - a ^= c; a -= rot(c,4); \ - b ^= a; b -= rot(a,14); \ - c ^= b; c -= rot(b,24); \ +ecs_table_t* flecs_table_find_or_create( + ecs_world_t *world, + const ecs_ids_t *components) +{ + ecs_poly_assert(world, ecs_world_t); + return find_or_create(world, components); } +void flecs_init_root_table( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + + ecs_ids_t entities = { + .array = NULL, + .count = 0 + }; -/* - * hashlittle2: return 2 32-bit hash values - * - * This is identical to hashlittle(), except it returns two 32-bit hash - * values instead of just one. This is good enough for hash table - * lookup with 2^^64 buckets, or if you want a second hash if you're not - * happy with the first, or if you want a probably-unique 64-bit ID for - * the key. *pc is better mixed than *pb, so use *pc first. If you want - * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)". - */ -static -void hashlittle2( - const void *key, /* the key to hash */ - size_t length, /* length of the key */ - uint32_t *pc, /* IN: primary initval, OUT: primary hash */ - uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */ + init_table(world, &world->store.root, &entities); +} + +void flecs_table_clear_edges( + ecs_world_t *world, + ecs_table_t *table) { - uint32_t a,b,c; /* internal state */ - union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */ + (void)world; + ecs_poly_assert(world, ecs_world_t); - /* Set up the internal state */ - a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc; - c += *pb; + int32_t i; + ecs_graph_node_t *node = &table->node; - u.ptr = key; - if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { - const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ - const uint8_t *k8; - (void)k8; + ecs_os_free(node->add.lo); + ecs_os_free(node->remove.lo); + ecs_map_free(node->add.hi); + ecs_map_free(node->remove.hi); + node->add.lo = NULL; + node->remove.lo = NULL; + node->add.hi = NULL; + node->remove.hi = NULL; - /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ - while (length > 12) - { - a += k[0]; - b += k[1]; - c += k[2]; - mix(a,b,c); - length -= 12; - k += 3; + int32_t count = ecs_vector_count(node->diffs); + ecs_table_diff_t *diffs = ecs_vector_first(node->diffs, ecs_table_diff_t); + for (i = 0; i < count; i ++) { + ecs_table_diff_t *diff = &diffs[i]; + ecs_os_free(diff->added.array); + ecs_os_free(diff->removed.array); + ecs_os_free(diff->on_set.array); + ecs_os_free(diff->un_set.array); } - /*----------------------------- handle the last (probably partial) block */ - /* - * "k[2]&0xffffff" actually reads beyond the end of the string, but - * then masks off the part it's not allowed to read. Because the - * string is aligned, the masked-off tail is in the same word as the - * rest of the string. Every machine with memory protection I've seen - * does it on word boundaries, so is OK with this. But VALGRIND will - * still catch it and complain. The masking trick does make the hash - * noticably faster for short strings (like English words). - */ -#ifndef VALGRIND + ecs_vector_free(node->diffs); + node->diffs = NULL; +} - switch(length) - { - case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; - case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; - case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; - case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; - case 8 : b+=k[1]; a+=k[0]; break; - case 7 : b+=k[1]&0xffffff; a+=k[0]; break; - case 6 : b+=k[1]&0xffff; a+=k[0]; break; - case 5 : b+=k[1]&0xff; a+=k[0]; break; - case 4 : a+=k[0]; break; - case 3 : a+=k[0]&0xffffff; break; - case 2 : a+=k[0]&0xffff; break; - case 1 : a+=k[0]&0xff; break; - case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ +void flecs_table_clear_add_edge( + ecs_table_t *table, + ecs_id_t id) +{ + ecs_edge_t *edge = get_edge(&table->node.add, id); + if (edge) { + edge->next = NULL; + edge->diff_index = 0; } +} -#else /* make valgrind happy */ +void flecs_table_clear_remove_edge( + ecs_table_t *table, + ecs_id_t id) +{ + ecs_edge_t *edge = get_edge(&table->node.remove, id); + if (edge) { + edge->next = NULL; + edge->diff_index = 0; + } +} - k8 = (const uint8_t *)k; - switch(length) - { - case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; - case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ - case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ - case 9 : c+=k8[8]; /* fall through */ - case 8 : b+=k[1]; a+=k[0]; break; - case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ - case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ - case 5 : b+=k8[4]; /* fall through */ - case 4 : a+=k[0]; break; - case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ - case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ - case 1 : a+=k8[0]; break; - case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ +/* Public convenience functions for traversing table graph */ +ecs_table_t* ecs_table_add_id( + ecs_world_t *world, + ecs_table_t *table, + ecs_id_t id) +{ + return flecs_table_traverse_add(world, table, &id, NULL); +} + +ecs_table_t* ecs_table_remove_id( + ecs_world_t *world, + ecs_table_t *table, + ecs_id_t id) +{ + return flecs_table_traverse_remove(world, table, &id, NULL); +} +#include + +static const char* mixin_kind_str[] = { + [EcsMixinBase] = "base (should never be requested by application)", + [EcsMixinWorld] = "world", + [EcsMixinObservable] = "observable", + [EcsMixinMax] = "max (should never be requested by application)" +}; + +ecs_mixins_t ecs_world_t_mixins = { + .type_name = "ecs_world_t", + .elems = { + [EcsMixinWorld] = offsetof(ecs_world_t, self), + [EcsMixinObservable] = offsetof(ecs_world_t, observable) } +}; -#endif /* !valgrind */ +ecs_mixins_t ecs_stage_t_mixins = { + .type_name = "ecs_stage_t", + .elems = { + [EcsMixinBase] = offsetof(ecs_stage_t, world), + [EcsMixinWorld] = offsetof(ecs_stage_t, world) + } +}; - } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { - const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ - const uint8_t *k8; +ecs_mixins_t ecs_query_t_mixins = { + .type_name = "ecs_query_t", + .elems = { + [EcsMixinWorld] = offsetof(ecs_query_t, world) + } +}; - /*--------------- all but last block: aligned reads and different mixing */ - while (length > 12) - { - a += k[0] + (((uint32_t)k[1])<<16); - b += k[2] + (((uint32_t)k[3])<<16); - c += k[4] + (((uint32_t)k[5])<<16); - mix(a,b,c); - length -= 12; - k += 6; +static +void* get_mixin( + const ecs_poly_t *poly, + ecs_mixin_kind_t kind) +{ + ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(kind < EcsMixinMax, ECS_INVALID_PARAMETER, NULL); + + const ecs_header_t *hdr = poly; + ecs_assert(hdr != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(hdr->magic == ECS_OBJECT_MAGIC, ECS_INVALID_PARAMETER, NULL); + + const ecs_mixins_t *mixins = hdr->mixins; + if (!mixins) { + /* Object has no mixins */ + goto not_found; } - /*----------------------------- handle the last (probably partial) block */ - k8 = (const uint8_t *)k; - switch(length) - { - case 12: c+=k[4]+(((uint32_t)k[5])<<16); - b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ - case 10: c+=k[4]; - b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 9 : c+=k8[8]; /* fall through */ - case 8 : b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ - case 6 : b+=k[2]; - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 5 : b+=k8[4]; /* fall through */ - case 4 : a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ - case 2 : a+=k[0]; - break; - case 1 : a+=k8[0]; - break; - case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ + ecs_size_t offset = mixins->elems[kind]; + if (offset == 0) { + /* Object has mixins but not the requested one. Try to find the mixin + * in the poly's base */ + goto find_in_base; } - } else { /* need to read the key one byte at a time */ - const uint8_t *k = (const uint8_t *)key; + /* Object has mixin, return its address */ + return ECS_OFFSET(hdr, offset); - /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ - while (length > 12) - { - a += k[0]; - a += ((uint32_t)k[1])<<8; - a += ((uint32_t)k[2])<<16; - a += ((uint32_t)k[3])<<24; - b += k[4]; - b += ((uint32_t)k[5])<<8; - b += ((uint32_t)k[6])<<16; - b += ((uint32_t)k[7])<<24; - c += k[8]; - c += ((uint32_t)k[9])<<8; - c += ((uint32_t)k[10])<<16; - c += ((uint32_t)k[11])<<24; - mix(a,b,c); - length -= 12; - k += 12; +find_in_base: + if (offset) { + /* If the poly has a base, try to find the mixin in the base */ + ecs_poly_t *base = *(ecs_poly_t**)ECS_OFFSET(hdr, offset); + if (base) { + return get_mixin(base, kind); + } } + +not_found: + /* Mixin wasn't found for poly */ + return NULL; +} - /*-------------------------------- last block: affect all 32 bits of (c) */ - switch(length) /* all the case statements fall through */ - { - case 12: c+=((uint32_t)k[11])<<24; - case 11: c+=((uint32_t)k[10])<<16; - case 10: c+=((uint32_t)k[9])<<8; - case 9 : c+=k[8]; - case 8 : b+=((uint32_t)k[7])<<24; - case 7 : b+=((uint32_t)k[6])<<16; - case 6 : b+=((uint32_t)k[5])<<8; - case 5 : b+=k[4]; - case 4 : a+=((uint32_t)k[3])<<24; - case 3 : a+=((uint32_t)k[2])<<16; - case 2 : a+=((uint32_t)k[1])<<8; - case 1 : a+=k[0]; - break; - case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ +static +void* assert_mixin( + const ecs_poly_t *poly, + ecs_mixin_kind_t kind) +{ + void *ptr = get_mixin(poly, kind); + if (!ptr) { + const ecs_header_t *header = poly; + const ecs_mixins_t *mixins = header->mixins; + ecs_err("%s not available for type %s", + mixin_kind_str[kind], + mixins ? mixins->type_name : "unknown"); + ecs_os_abort(); } - } - final(a,b,c); - *pc=c; *pb=b; + return ptr; } -uint64_t flecs_hash( - const void *data, - ecs_size_t length) +void _ecs_poly_init( + ecs_poly_t *poly, + int32_t type, + ecs_size_t size, + ecs_mixins_t *mixins) { - uint32_t h_1 = 0; - uint32_t h_2 = 0; + ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); - hashlittle2( - data, - flecs_to_size_t(length), - &h_1, - &h_2); + ecs_header_t *hdr = poly; + ecs_os_memset(poly, 0, size); - return h_1 | ((uint64_t)h_2 << 32); + hdr->magic = ECS_OBJECT_MAGIC; + hdr->type = type; + hdr->mixins = mixins; } - -static -void ensure( - ecs_bitset_t *bs, - ecs_size_t size) +void _ecs_poly_fini( + ecs_poly_t *poly, + int32_t type) { - if (!bs->size) { - int32_t new_size = ((size - 1) / 64 + 1) * ECS_SIZEOF(uint64_t); - bs->size = ((size - 1) / 64 + 1) * 64; - bs->data = ecs_os_calloc(new_size); - } else if (size > bs->size) { - int32_t prev_size = ((bs->size - 1) / 64 + 1) * ECS_SIZEOF(uint64_t); - bs->size = ((size - 1) / 64 + 1) * 64; - int32_t new_size = ((size - 1) / 64 + 1) * ECS_SIZEOF(uint64_t); - bs->data = ecs_os_realloc(bs->data, new_size); - ecs_os_memset(ECS_OFFSET(bs->data, prev_size), 0, new_size - prev_size); - } + ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); + (void)type; + + ecs_header_t *hdr = poly; + + /* Don't deinit poly that wasn't initialized */ + ecs_assert(hdr->magic == ECS_OBJECT_MAGIC, ECS_INVALID_PARAMETER, NULL); + ecs_assert(hdr->type == type, ECS_INVALID_PARAMETER, NULL); + hdr->magic = 0; } -void flecs_bitset_init( - ecs_bitset_t* bs) +#define assert_object(cond, file, line)\ + _ecs_assert((cond), ECS_INVALID_PARAMETER, #cond, file, line, NULL);\ + assert(cond) + +#ifndef NDEBUG +void _ecs_poly_assert( + const ecs_poly_t *poly, + int32_t type, + const char *file, + int32_t line) { - bs->size = 0; - bs->count = 0; - bs->data = NULL; + assert_object(poly != NULL, file, line); + + const ecs_header_t *hdr = poly; + assert_object(hdr->magic == ECS_OBJECT_MAGIC, file, line); + assert_object(hdr->type == type, file, line); } +#endif -void flecs_bitset_ensure( - ecs_bitset_t *bs, - int32_t count) +bool _ecs_poly_is( + const ecs_poly_t *poly, + int32_t type) { - if (count > bs->count) { - bs->count = count; - ensure(bs, count); - } + ecs_assert(poly != NULL, ECS_INVALID_PARAMETER, NULL); + + const ecs_header_t *hdr = poly; + ecs_assert(hdr->magic == ECS_OBJECT_MAGIC, ECS_INVALID_PARAMETER, NULL); + return hdr->type == type; } -void flecs_bitset_deinit( - ecs_bitset_t *bs) +ecs_observable_t* ecs_get_observable( + const ecs_poly_t *poly) { - ecs_os_free(bs->data); + return (ecs_observable_t*)assert_mixin(poly, EcsMixinObservable); } -void flecs_bitset_addn( - ecs_bitset_t *bs, - int32_t count) +const ecs_world_t* ecs_get_world( + const ecs_poly_t *poly) { - int32_t elem = bs->count += count; - ensure(bs, elem); + return *(ecs_world_t**)assert_mixin(poly, EcsMixinWorld); } -void flecs_bitset_set( - ecs_bitset_t *bs, - int32_t elem, - bool value) +int8_t flecs_to_i8( + int64_t v) { - ecs_assert(elem < bs->count, ECS_INVALID_PARAMETER, NULL); - int32_t hi = elem >> 6; - int32_t lo = elem & 0x3F; - uint64_t v = bs->data[hi]; - bs->data[hi] = (v & ~((uint64_t)1 << lo)) | ((uint64_t)value << lo); + ecs_assert(v < INT8_MAX, ECS_INTERNAL_ERROR, NULL); + return (int8_t)v; } -bool flecs_bitset_get( - const ecs_bitset_t *bs, - int32_t elem) +int16_t flecs_to_i16( + int64_t v) { - ecs_assert(elem < bs->count, ECS_INVALID_PARAMETER, NULL); - return !!(bs->data[elem >> 6] & ((uint64_t)1 << ((uint64_t)elem & 0x3F))); + ecs_assert(v < INT16_MAX, ECS_INTERNAL_ERROR, NULL); + return (int16_t)v; } -int32_t flecs_bitset_count( - const ecs_bitset_t *bs) +int32_t flecs_to_i32( + int64_t v) { - return bs->count; + ecs_assert(v < INT32_MAX, ECS_INTERNAL_ERROR, NULL); + return (int32_t)v; } -void flecs_bitset_remove( - ecs_bitset_t *bs, - int32_t elem) +uint32_t flecs_to_u32( + uint64_t v) { - ecs_assert(elem < bs->count, ECS_INVALID_PARAMETER, NULL); - int32_t last = bs->count - 1; - bool last_value = flecs_bitset_get(bs, last); - flecs_bitset_set(bs, elem, last_value); - bs->count --; + ecs_assert(v < UINT32_MAX, ECS_INTERNAL_ERROR, NULL); + return (uint32_t)v; } -void flecs_bitset_swap( - ecs_bitset_t *bs, - int32_t elem_a, - int32_t elem_b) +size_t flecs_to_size_t( + int64_t size) { - ecs_assert(elem_a < bs->count, ECS_INVALID_PARAMETER, NULL); - ecs_assert(elem_b < bs->count, ECS_INVALID_PARAMETER, NULL); - - bool a = flecs_bitset_get(bs, elem_a); - bool b = flecs_bitset_get(bs, elem_b); - flecs_bitset_set(bs, elem_a, b); - flecs_bitset_set(bs, elem_b, a); + ecs_assert(size >= 0, ECS_INTERNAL_ERROR, NULL); + return (size_t)size; } -/* Add an extra element to the buffer */ -static -void ecs_strbuf_grow( - ecs_strbuf_t *b) +ecs_size_t flecs_from_size_t( + size_t size) { - /* Allocate new element */ - ecs_strbuf_element_embedded *e = ecs_os_malloc(sizeof(ecs_strbuf_element_embedded)); - b->size += b->current->pos; - b->current->next = (ecs_strbuf_element*)e; - b->current = (ecs_strbuf_element*)e; - b->elementCount ++; - e->super.buffer_embedded = true; - e->super.buf = e->buf; - e->super.pos = 0; - e->super.next = NULL; + ecs_assert(size < INT32_MAX, ECS_INTERNAL_ERROR, NULL); + return (ecs_size_t)size; } -/* Add an extra dynamic element */ -static -void ecs_strbuf_grow_str( - ecs_strbuf_t *b, - char *str, - char *alloc_str, - int32_t size) +int32_t flecs_next_pow_of_2( + int32_t n) { - /* Allocate new element */ - ecs_strbuf_element_str *e = ecs_os_malloc(sizeof(ecs_strbuf_element_str)); - b->size += b->current->pos; - b->current->next = (ecs_strbuf_element*)e; - b->current = (ecs_strbuf_element*)e; - b->elementCount ++; - e->super.buffer_embedded = false; - e->super.pos = size ? size : (int32_t)ecs_os_strlen(str); - e->super.next = NULL; - e->super.buf = str; - e->alloc_str = alloc_str; + n --; + n |= n >> 1; + n |= n >> 2; + n |= n >> 4; + n |= n >> 8; + n |= n >> 16; + n ++; + + return n; } -static -char* ecs_strbuf_ptr( - ecs_strbuf_t *b) +/** Convert time to double */ +double ecs_time_to_double( + ecs_time_t t) { - if (b->buf) { - return &b->buf[b->current->pos]; - } else { - return &b->current->buf[b->current->pos]; - } + double result; + result = t.sec; + return result + (double)t.nanosec / (double)1000000000; } -/* Compute the amount of space left in the current element */ -static -int32_t ecs_strbuf_memLeftInCurrentElement( - ecs_strbuf_t *b) +ecs_time_t ecs_time_sub( + ecs_time_t t1, + ecs_time_t t2) { - if (b->current->buffer_embedded) { - return ECS_STRBUF_ELEMENT_SIZE - b->current->pos; + ecs_time_t result; + + if (t1.nanosec >= t2.nanosec) { + result.nanosec = t1.nanosec - t2.nanosec; + result.sec = t1.sec - t2.sec; } else { - return 0; + result.nanosec = t1.nanosec - t2.nanosec + 1000000000; + result.sec = t1.sec - t2.sec - 1; } + + return result; } -/* Compute the amount of space left */ -static -int32_t ecs_strbuf_memLeft( - ecs_strbuf_t *b) +void ecs_sleepf( + double t) { - if (b->max) { - return b->max - b->size - b->current->pos; - } else { - return INT_MAX; + if (t > 0) { + int sec = (int)t; + int nsec = (int)((t - sec) * 1000000000); + ecs_os_sleep(sec, nsec); } } -static -void ecs_strbuf_init( - ecs_strbuf_t *b) +double ecs_time_measure( + ecs_time_t *start) { - /* Initialize buffer structure only once */ - if (!b->elementCount) { - b->size = 0; - b->firstElement.super.next = NULL; - b->firstElement.super.pos = 0; - b->firstElement.super.buffer_embedded = true; - b->firstElement.super.buf = b->firstElement.buf; - b->elementCount ++; - b->current = (ecs_strbuf_element*)&b->firstElement; - } + ecs_time_t stop, temp; + ecs_os_get_time(&stop); + temp = stop; + stop = ecs_time_sub(stop, *start); + *start = temp; + return ecs_time_to_double(stop); } -/* Quick custom function to copy a maxium number of characters and - * simultaneously determine length of source string. */ -static -int32_t fast_strncpy( - char * dst, - const char * src, - int n_cpy, - int n) +void* ecs_os_memdup( + const void *src, + ecs_size_t size) { - ecs_assert(n_cpy >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(n >= 0, ECS_INTERNAL_ERROR, NULL); - - const char *ptr, *orig = src; - char ch; - - for (ptr = src; (ptr - orig < n) && (ch = *ptr); ptr ++) { - if (ptr - orig < n_cpy) { - *dst = ch; - dst ++; - } + if (!src) { + return NULL; } + + void *dst = ecs_os_malloc(size); + ecs_assert(dst != NULL, ECS_OUT_OF_MEMORY, NULL); + ecs_os_memcpy(dst, src, size); + return dst; +} - ecs_assert(ptr - orig < INT32_MAX, ECS_INTERNAL_ERROR, NULL); +int flecs_entity_compare( + ecs_entity_t e1, + const void *ptr1, + ecs_entity_t e2, + const void *ptr2) +{ + (void)ptr1; + (void)ptr2; + return (e1 > e2) - (e1 < e2); +} - return (int32_t)(ptr - orig); +int flecs_entity_compare_qsort( + const void *e1, + const void *e2) +{ + ecs_entity_t v1 = *(ecs_entity_t*)e1; + ecs_entity_t v2 = *(ecs_entity_t*)e2; + return flecs_entity_compare(v1, NULL, v2, NULL); } -/* Append a format string to a buffer */ -static -bool ecs_strbuf_vappend_intern( - ecs_strbuf_t *b, - const char* str, - va_list args) +uint64_t flecs_string_hash( + const void *ptr) { - bool result = true; - va_list arg_cpy; + const ecs_hashed_string_t *str = ptr; + ecs_assert(str->hash != 0, ECS_INTERNAL_ERROR, NULL); + return str->hash; +} - if (!str) { - return result; - } +/* + This code was taken from sokol_time.h + + zlib/libpng license + Copyright (c) 2018 Andre Weissflog + This software is provided 'as-is', without any express or implied warranty. + In no event will the authors be held liable for any damages arising from the + use of this software. + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software in a + product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not + be misrepresented as being the original software. + 3. This notice may not be removed or altered from any source + distribution. +*/ - ecs_strbuf_init(b); - int32_t memLeftInElement = ecs_strbuf_memLeftInCurrentElement(b); - int32_t memLeft = ecs_strbuf_memLeft(b); +static int ecs_os_time_initialized; - if (!memLeft) { - return false; - } +#if defined(_WIN32) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +static double _ecs_os_time_win_freq; +static LARGE_INTEGER _ecs_os_time_win_start; +#elif defined(__APPLE__) && defined(__MACH__) +#include +static mach_timebase_info_data_t _ecs_os_time_osx_timebase; +static uint64_t _ecs_os_time_osx_start; +#else /* anything else, this will need more care for non-Linux platforms */ +#include +static uint64_t _ecs_os_time_posix_start; +#endif - /* Compute the memory required to add the string to the buffer. If user - * provided buffer, use space left in buffer, otherwise use space left in - * current element. */ - int32_t max_copy = b->buf ? memLeft : memLeftInElement; - int32_t memRequired; +/* prevent 64-bit overflow when computing relative timestamp + see https://gist.github.com/jspohr/3dc4f00033d79ec5bdaf67bc46c813e3 +*/ +#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__)) +int64_t int64_muldiv(int64_t value, int64_t numer, int64_t denom) { + int64_t q = value / denom; + int64_t r = value % denom; + return q * numer + r * numer / denom; +} +#endif - va_copy(arg_cpy, args); - memRequired = vsnprintf( - ecs_strbuf_ptr(b), (size_t)(max_copy + 1), str, args); +void flecs_os_time_setup(void) { + if ( ecs_os_time_initialized) { + return; + } + + ecs_os_time_initialized = 1; + #if defined(_WIN32) + LARGE_INTEGER freq; + QueryPerformanceFrequency(&freq); + QueryPerformanceCounter(&_ecs_os_time_win_start); + _ecs_os_time_win_freq = (double)freq.QuadPart / 1000000000.0; + #elif defined(__APPLE__) && defined(__MACH__) + mach_timebase_info(&_ecs_os_time_osx_timebase); + _ecs_os_time_osx_start = mach_absolute_time(); + #else + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + _ecs_os_time_posix_start = (uint64_t)ts.tv_sec*1000000000 + (uint64_t)ts.tv_nsec; + #endif +} - ecs_assert(memRequired != -1, ECS_INTERNAL_ERROR, NULL); +uint64_t flecs_os_time_now(void) { + ecs_assert(ecs_os_time_initialized != 0, ECS_INTERNAL_ERROR, NULL); - if (memRequired <= memLeftInElement) { - /* Element was large enough to fit string */ - b->current->pos += memRequired; - } else if ((memRequired - memLeftInElement) < memLeft) { - /* If string is a format string, a new buffer of size memRequired is - * needed to re-evaluate the format string and only use the part that - * wasn't already copied to the previous element */ - if (memRequired <= ECS_STRBUF_ELEMENT_SIZE) { - /* Resulting string fits in standard-size buffer. Note that the - * entire string needs to fit, not just the remainder, as the - * format string cannot be partially evaluated */ - ecs_strbuf_grow(b); + uint64_t now; - /* Copy entire string to new buffer */ - ecs_os_vsprintf(ecs_strbuf_ptr(b), str, arg_cpy); + #if defined(_WIN32) + LARGE_INTEGER qpc_t; + QueryPerformanceCounter(&qpc_t); + now = (uint64_t)(qpc_t.QuadPart / _ecs_os_time_win_freq); + #elif defined(__APPLE__) && defined(__MACH__) + now = (uint64_t) int64_muldiv((int64_t)mach_absolute_time(), (int64_t)_ecs_os_time_osx_timebase.numer, (int64_t)_ecs_os_time_osx_timebase.denom); + #else + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + now = ((uint64_t)ts.tv_sec * 1000000000 + (uint64_t)ts.tv_nsec); + #endif - /* Ignore the part of the string that was copied into the - * previous buffer. The string copied into the new buffer could - * be memmoved so that only the remainder is left, but that is - * most likely more expensive than just keeping the entire - * string. */ + return now; +} - /* Update position in buffer */ - b->current->pos += memRequired; - } else { - /* Resulting string does not fit in standard-size buffer. - * Allocate a new buffer that can hold the entire string. */ - char *dst = ecs_os_malloc(memRequired + 1); - ecs_os_vsprintf(dst, str, arg_cpy); - ecs_strbuf_grow_str(b, dst, dst, memRequired); - } - } else { - /* Buffer max has been reached */ - result = false; +void flecs_os_time_sleep( + int32_t sec, + int32_t nanosec) +{ +#ifndef _WIN32 + struct timespec sleepTime; + ecs_assert(sec >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(nanosec >= 0, ECS_INTERNAL_ERROR, NULL); + + sleepTime.tv_sec = sec; + sleepTime.tv_nsec = nanosec; + if (nanosleep(&sleepTime, NULL)) { + ecs_err("nanosleep failed"); } +#else + HANDLE timer; + LARGE_INTEGER ft; - va_end(arg_cpy); + ft.QuadPart = -((int64_t)sec * 10000000 + (int64_t)nanosec / 100); - return result; + timer = CreateWaitableTimer(NULL, TRUE, NULL); + SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); + WaitForSingleObject(timer, INFINITE); + CloseHandle(timer); +#endif } -static -bool ecs_strbuf_append_intern( - ecs_strbuf_t *b, - const char* str, - int n) -{ - bool result = true; - - if (!str) { - return result; - } - ecs_strbuf_init(b); +#if defined(_WIN32) - int32_t memLeftInElement = ecs_strbuf_memLeftInCurrentElement(b); - int32_t memLeft = ecs_strbuf_memLeft(b); +static ULONG win32_current_resolution; - if (memLeft <= 0) { - return false; +void flecs_increase_timer_resolution(bool enable) +{ + HMODULE hntdll = GetModuleHandle((LPCTSTR)"ntdll.dll"); + if (!hntdll) { + return; } - /* Compute the memory required to add the string to the buffer. If user - * provided buffer, use space left in buffer, otherwise use space left in - * current element. */ - int32_t max_copy = b->buf ? memLeft : memLeftInElement; - int32_t memRequired; + LONG (__stdcall *pNtSetTimerResolution)( + ULONG desired, BOOLEAN set, ULONG * current); - if (n < 0) n = INT_MAX; + pNtSetTimerResolution = (LONG(__stdcall*)(ULONG, BOOLEAN, ULONG*)) + GetProcAddress(hntdll, "NtSetTimerResolution"); - memRequired = fast_strncpy(ecs_strbuf_ptr(b), str, max_copy, n); + if(!pNtSetTimerResolution) { + return; + } - if (memRequired <= memLeftInElement) { - /* Element was large enough to fit string */ - b->current->pos += memRequired; - } else if ((memRequired - memLeftInElement) < memLeft) { - /* Element was not large enough, but buffer still has space */ - b->current->pos += memLeftInElement; - memRequired -= memLeftInElement; + ULONG current, resolution = 10000; /* 1 ms */ - /* Current element was too small, copy remainder into new element */ - if (memRequired < ECS_STRBUF_ELEMENT_SIZE) { - /* A standard-size buffer is large enough for the new string */ - ecs_strbuf_grow(b); + if (!enable && win32_current_resolution) { + pNtSetTimerResolution(win32_current_resolution, 0, ¤t); + win32_current_resolution = 0; + return; + } else if (!enable) { + return; + } - /* Copy the remainder to the new buffer */ - if (n) { - /* If a max number of characters to write is set, only a - * subset of the string should be copied to the buffer */ - ecs_os_strncpy( - ecs_strbuf_ptr(b), - str + memLeftInElement, - (size_t)memRequired); - } else { - ecs_os_strcpy(ecs_strbuf_ptr(b), str + memLeftInElement); - } + if (resolution == win32_current_resolution) { + return; + } - /* Update to number of characters copied to new buffer */ - b->current->pos += memRequired; - } else { - char *remainder = ecs_os_strdup(str + memLeftInElement); - ecs_strbuf_grow_str(b, remainder, remainder, memRequired); - } - } else { - /* Buffer max has been reached */ - result = false; + if (win32_current_resolution) { + pNtSetTimerResolution(win32_current_resolution, 0, ¤t); } - return result; + if (pNtSetTimerResolution(resolution, 1, ¤t)) { + /* Try setting a lower resolution */ + resolution *= 2; + if(pNtSetTimerResolution(resolution, 1, ¤t)) return; + } + + win32_current_resolution = resolution; } -bool ecs_strbuf_vappend( - ecs_strbuf_t *b, - const char* fmt, - va_list args) +#else +void flecs_increase_timer_resolution(bool enable) { - bool result = ecs_strbuf_vappend_intern( - b, fmt, args - ); - - return result; + (void)enable; + return; } +#endif -bool ecs_strbuf_append( - ecs_strbuf_t *b, - const char* fmt, - ...) +static +int32_t count_events( + const ecs_entity_t *events) { - va_list args; - va_start(args, fmt); - bool result = ecs_strbuf_vappend_intern( - b, fmt, args - ); - va_end(args); + int32_t i; - return result; + for (i = 0; i < ECS_TRIGGER_DESC_EVENT_COUNT_MAX; i ++) { + if (!events[i]) { + break; + } + } + + return i; } -bool ecs_strbuf_appendstrn( - ecs_strbuf_t *b, - const char* str, - int32_t len) -{ - return ecs_strbuf_append_intern( - b, str, len - ); -} - -bool ecs_strbuf_appendstr_zerocpy( - ecs_strbuf_t *b, - char* str) +static +ecs_entity_t get_actual_event( + ecs_trigger_t *trigger, + ecs_entity_t event) { - ecs_strbuf_init(b); - ecs_strbuf_grow_str(b, str, str, 0); - return true; -} + /* If operator is Not, reverse the event */ + if (trigger->term.oper == EcsNot) { + if (event == EcsOnAdd) { + event = EcsOnRemove; + } else if (event == EcsOnRemove) { + event = EcsOnAdd; + } + } -bool ecs_strbuf_appendstr_zerocpy_const( - ecs_strbuf_t *b, - const char* str) -{ - /* Removes const modifier, but logic prevents changing / delete string */ - ecs_strbuf_init(b); - ecs_strbuf_grow_str(b, (char*)str, NULL, 0); - return true; + return event; } -bool ecs_strbuf_appendstr( - ecs_strbuf_t *b, - const char* str) +static +void register_trigger_for_id( + ecs_world_t *world, + ecs_observable_t *observable, + ecs_trigger_t *trigger, + ecs_id_t id, + bool register_for_set) { - return ecs_strbuf_append_intern( - b, str, -1 - ); -} + ecs_sparse_t *triggers = observable->triggers; + ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); -bool ecs_strbuf_mergebuff( - ecs_strbuf_t *dst_buffer, - ecs_strbuf_t *src_buffer) -{ - if (src_buffer->elementCount) { - if (src_buffer->buf) { - return ecs_strbuf_appendstr(dst_buffer, src_buffer->buf); - } else { - ecs_strbuf_element *e = (ecs_strbuf_element*)&src_buffer->firstElement; + int i; + for (i = 0; i < trigger->event_count; i ++) { + ecs_entity_t event = get_actual_event(trigger, trigger->events[i]); - /* Copy first element as it is inlined in the src buffer */ - ecs_strbuf_appendstrn(dst_buffer, e->buf, e->pos); + /* Get triggers for event */ + ecs_event_triggers_t *evt = flecs_sparse_ensure( + triggers, ecs_event_triggers_t, event); + ecs_assert(evt != NULL, ECS_INTERNAL_ERROR, NULL); - while ((e = e->next)) { - dst_buffer->current->next = ecs_os_malloc(sizeof(ecs_strbuf_element)); - *dst_buffer->current->next = *e; - } + if (!evt->triggers) { + evt->triggers = ecs_map_new(ecs_id_triggers_t, 1); } - *src_buffer = ECS_STRBUF_INIT; - } - - return true; -} + /* Get triggers for (component) id */ + ecs_id_triggers_t *idt = ecs_map_ensure( + evt->triggers, ecs_id_triggers_t, id); + ecs_assert(idt != NULL, ECS_INTERNAL_ERROR, NULL); -char* ecs_strbuf_get(ecs_strbuf_t *b) { - char* result = NULL; + ecs_map_t *id_triggers = NULL; - if (b->elementCount) { - if (b->buf) { - b->buf[b->current->pos] = '\0'; - result = ecs_os_strdup(b->buf); + if (!register_for_set) { + if (!(id_triggers = idt->triggers)) { + id_triggers = idt->triggers = ecs_map_new(ecs_trigger_t*, 1); + } } else { - void *next = NULL; - int32_t len = b->size + b->current->pos + 1; - - ecs_strbuf_element *e = (ecs_strbuf_element*)&b->firstElement; - - result = ecs_os_malloc(len); - char* ptr = result; - - do { - ecs_os_memcpy(ptr, e->buf, e->pos); - ptr += e->pos; - next = e->next; - if (e != &b->firstElement.super) { - if (!e->buffer_embedded) { - ecs_os_free(((ecs_strbuf_element_str*)e)->alloc_str); - } - ecs_os_free(e); - } - } while ((e = next)); - - result[len - 1] = '\0'; + if (!(id_triggers = idt->set_triggers)) { + id_triggers = idt->set_triggers = + ecs_map_new(ecs_trigger_t*, 1); + } } - } else { - result = NULL; - } - - b->elementCount = 0; - return result; -} + ecs_trigger_t **elem = ecs_map_ensure( + id_triggers, ecs_trigger_t*, trigger->id); + *elem = trigger; -void ecs_strbuf_reset(ecs_strbuf_t *b) { - if (b->elementCount && !b->buf) { - void *next = NULL; - ecs_strbuf_element *e = (ecs_strbuf_element*)&b->firstElement; - do { - next = e->next; - if (e != (ecs_strbuf_element*)&b->firstElement) { - ecs_os_free(e); - } - } while ((e = next)); + // First trigger of its kind, send table notification + flecs_notify_tables(world, id, &(ecs_table_event_t){ + .kind = EcsTableTriggerMatch, + .event = trigger->events[i] + }); } - - *b = ECS_STRBUF_INIT; } -void ecs_strbuf_list_push( - ecs_strbuf_t *buffer, - const char *list_open, - const char *separator) +static +void register_trigger( + ecs_world_t *world, + ecs_observable_t *observable, + ecs_trigger_t *trigger) { - buffer->list_sp ++; - buffer->list_stack[buffer->list_sp].count = 0; - buffer->list_stack[buffer->list_sp].separator = separator; - - if (list_open) { - ecs_strbuf_appendstr(buffer, list_open); + ecs_term_t *term = &trigger->term; + if (term->subj.set.mask & EcsSelf) { + register_trigger_for_id(world, observable, trigger, term->id, false); } -} - -void ecs_strbuf_list_pop( - ecs_strbuf_t *buffer, - const char *list_close) -{ - buffer->list_sp --; - - if (list_close) { - ecs_strbuf_appendstr(buffer, list_close); + if (trigger->term.subj.set.mask & EcsSuperSet) { + ecs_id_t pair = ecs_pair(term->subj.set.relation, EcsWildcard); + register_trigger_for_id(world, observable, trigger, pair, true); } } -void ecs_strbuf_list_next( - ecs_strbuf_t *buffer) +static +void unregister_trigger_for_id( + ecs_observable_t *observable, + ecs_trigger_t *trigger, + ecs_id_t id, + bool unregister_for_set) { - int32_t list_sp = buffer->list_sp; - if (buffer->list_stack[list_sp].count != 0) { - ecs_strbuf_appendstr(buffer, buffer->list_stack[list_sp].separator); - } - buffer->list_stack[list_sp].count ++; -} + ecs_sparse_t *triggers = observable->triggers; + ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); -bool ecs_strbuf_list_append( - ecs_strbuf_t *buffer, - const char *fmt, - ...) -{ - ecs_strbuf_list_next(buffer); + int i; + for (i = 0; i < trigger->event_count; i ++) { + ecs_entity_t event = get_actual_event(trigger, trigger->events[i]); - va_list args; - va_start(args, fmt); - bool result = ecs_strbuf_vappend_intern( - buffer, fmt, args - ); - va_end(args); + /* Get triggers for event */ + ecs_event_triggers_t *evt = flecs_sparse_get( + triggers, ecs_event_triggers_t, event); + ecs_assert(evt != NULL, ECS_INTERNAL_ERROR, NULL); - return result; -} + /* Get triggers for (component) id */ + ecs_id_triggers_t *idt = ecs_map_get( + evt->triggers, ecs_id_triggers_t, id); + ecs_assert(idt != NULL, ECS_INTERNAL_ERROR, NULL); -bool ecs_strbuf_list_appendstr( - ecs_strbuf_t *buffer, - const char *str) -{ - ecs_strbuf_list_next(buffer); - return ecs_strbuf_appendstr(buffer, str); -} + ecs_map_t *id_triggers; -/* The ratio used to determine whether the map should rehash. If - * (element_count * LOAD_FACTOR) > bucket_count, bucket count is increased. */ -#define LOAD_FACTOR (1.5f) -#define KEY_SIZE (ECS_SIZEOF(ecs_map_key_t)) -#define GET_ELEM(array, elem_size, index) \ - ECS_OFFSET(array, (elem_size) * (index)) + if (unregister_for_set) { + id_triggers = idt->set_triggers; + } else { + id_triggers = idt->triggers; + } -typedef struct ecs_bucket_t { - ecs_map_key_t *keys; /* Array with keys */ - void *payload; /* Payload array */ - int32_t count; /* Number of elements in bucket */ -} ecs_bucket_t; + ecs_map_remove(id_triggers, trigger->id); -struct ecs_map_t { - ecs_bucket_t *buckets; - int32_t elem_size; - int32_t bucket_count; - int32_t count; -}; + if (!ecs_map_count(id_triggers)) { + ecs_map_free(id_triggers); -/* Get bucket count for number of elements */ -static -int32_t get_bucket_count( - int32_t element_count) -{ - return flecs_next_pow_of_2((int32_t)((float)element_count * LOAD_FACTOR)); -} + if (unregister_for_set) { + idt->set_triggers = NULL; + } else { + idt->triggers = NULL; + } -/* Get bucket index for provided map key */ -static -int32_t get_bucket_id( - int32_t bucket_count, - ecs_map_key_t key) -{ - ecs_assert(bucket_count > 0, ECS_INTERNAL_ERROR, NULL); - int32_t result = (int32_t)(key & ((uint64_t)bucket_count - 1)); - ecs_assert(result < INT32_MAX, ECS_INTERNAL_ERROR, NULL); - return result; + if (!idt->triggers && !idt->set_triggers) { + ecs_map_remove(evt->triggers, id); + if (!ecs_map_count(evt->triggers)) { + ecs_map_free(evt->triggers); + evt->triggers = NULL; + } + } + } + } } -/* Get bucket for key */ static -ecs_bucket_t* get_bucket( - const ecs_map_t *map, - ecs_map_key_t key) -{ - int32_t bucket_count = map->bucket_count; - if (!bucket_count) { - return NULL; +void unregister_trigger( + ecs_observable_t *observable, + ecs_trigger_t *trigger) +{ + ecs_term_t *term = &trigger->term; + if (term->subj.set.mask & EcsSelf) { + unregister_trigger_for_id(observable, trigger, term->id, false); + } else { + ecs_id_t pair = ecs_pair(term->subj.set.relation, EcsWildcard); + unregister_trigger_for_id(observable, trigger, pair, true); } - - int32_t bucket_id = get_bucket_id(bucket_count, key); - ecs_assert(bucket_id < bucket_count, ECS_INTERNAL_ERROR, NULL); - - return &map->buckets[bucket_id]; } -/* Ensure that map has at least new_count buckets */ static -void ensure_buckets( - ecs_map_t *map, - int32_t new_count) +ecs_map_t* get_triggers_for_event( + const ecs_poly_t *object, + ecs_entity_t event) { - int32_t bucket_count = map->bucket_count; - new_count = flecs_next_pow_of_2(new_count); - if (new_count && new_count > bucket_count) { - map->buckets = ecs_os_realloc(map->buckets, new_count * ECS_SIZEOF(ecs_bucket_t)); - map->bucket_count = new_count; + ecs_assert(object != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(event != 0, ECS_INTERNAL_ERROR, NULL); - ecs_os_memset( - ECS_OFFSET(map->buckets, bucket_count * ECS_SIZEOF(ecs_bucket_t)), - 0, (new_count - bucket_count) * ECS_SIZEOF(ecs_bucket_t)); + /* Get triggers for event */ + ecs_observable_t *observable = ecs_get_observable(object); + ecs_assert(observable != NULL, ECS_INVALID_PARAMETER, NULL); + + ecs_sparse_t *triggers = observable->triggers; + ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); + + const ecs_event_triggers_t *evt = flecs_sparse_get( + triggers, ecs_event_triggers_t, event); + + if (evt) { + return evt->triggers; } -} -/* Free contents of bucket */ -static -void clear_bucket( - ecs_bucket_t *bucket) -{ - ecs_os_free(bucket->keys); - ecs_os_free(bucket->payload); - bucket->keys = NULL; - bucket->payload = NULL; - bucket->count = 0; + return NULL; } -/* Clear all buckets */ static -void clear_buckets( - ecs_map_t *map) +ecs_id_triggers_t* get_triggers_for_id( + const ecs_map_t *evt, + ecs_id_t id) { - ecs_bucket_t *buckets = map->buckets; - int32_t i, count = map->bucket_count; - for (i = 0; i < count; i ++) { - clear_bucket(&buckets[i]); - } - ecs_os_free(buckets); - map->buckets = NULL; - map->bucket_count = 0; + return ecs_map_get(evt, ecs_id_triggers_t, id); } -/* Find or create bucket for specified key */ -static -ecs_bucket_t* ensure_bucket( - ecs_map_t *map, - ecs_map_key_t key) +ecs_id_triggers_t* flecs_triggers_for_id( + const ecs_poly_t *object, + ecs_id_t id, + ecs_entity_t event) { - if (!map->bucket_count) { - ensure_buckets(map, 2); + const ecs_map_t *evt = get_triggers_for_event(object, event); + if (!evt) { + return NULL; } - int32_t bucket_id = get_bucket_id(map->bucket_count, key); - ecs_assert(bucket_id >= 0, ECS_INTERNAL_ERROR, NULL); - return &map->buckets[bucket_id]; + return get_triggers_for_id(evt, id); } -/* Add element to bucket */ static -int32_t add_to_bucket( - ecs_bucket_t *bucket, - ecs_size_t elem_size, - ecs_map_key_t key, - const void *payload) +void init_iter( + ecs_iter_t *it, + ecs_entity_t *entity, + ecs_table_t *table, + int32_t row, + int32_t count, + bool *iter_set) { - int32_t index = bucket->count ++; - int32_t bucket_count = index + 1; + ecs_assert(it != NULL, ECS_INTERNAL_ERROR, NULL); + + if (*iter_set) { + return; + } - bucket->keys = ecs_os_realloc(bucket->keys, KEY_SIZE * bucket_count); - bucket->payload = ecs_os_realloc(bucket->payload, elem_size * bucket_count); - bucket->keys[index] = key; + flecs_iter_init(it); - if (payload) { - void *elem = GET_ELEM(bucket->payload, elem_size, index); - ecs_os_memcpy(elem, payload, elem_size); - } + *iter_set = true; - return index; -} + it->ids[0] = it->event_id; -/* Remove element from bucket */ -static -void remove_from_bucket( - ecs_bucket_t *bucket, - ecs_size_t elem_size, - ecs_map_key_t key, - int32_t index) -{ - (void)key; + if (count) { + if (table) { + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(!it->world->is_readonly, ECS_INTERNAL_ERROR, NULL); + ecs_data_t *data = &table->storage; + ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_entity_t *entities = ecs_vector_first( + data->entities, ecs_entity_t); + ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(count > 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(row < ecs_vector_count(data->entities), + ECS_INTERNAL_ERROR, NULL); + ecs_assert((row + count) <= ecs_vector_count(data->entities), + ECS_INTERNAL_ERROR, NULL); - ecs_assert(bucket->count != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(index < bucket->count, ECS_INTERNAL_ERROR, NULL); - - int32_t bucket_count = -- bucket->count; + it->entities = ECS_OFFSET(entities, ECS_SIZEOF(ecs_entity_t) * row); - if (index != bucket->count) { - ecs_assert(key == bucket->keys[index], ECS_INTERNAL_ERROR, NULL); - bucket->keys[index] = bucket->keys[bucket_count]; + ecs_entity_t subject = 0; + int32_t index = ecs_type_match(it->world, table, table->type, 0, + it->event_id, EcsIsA, 0, 0, &subject, NULL); - ecs_map_key_t *elem = GET_ELEM(bucket->payload, elem_size, index); - ecs_map_key_t *last_elem = GET_ELEM(bucket->payload, elem_size, bucket->count); + ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); + int32_t storage_index = ecs_table_type_to_storage_index( + table, index); + + index ++; + it->columns[0] = index; + it->sizes[0] = 0; - ecs_os_memcpy(elem, last_elem, elem_size); + if (storage_index == -1) { + it->columns[0] = 0; + } + + if (!subject && it->columns[0] && data && data->columns) { + ecs_column_t *col = &data->columns[storage_index]; + it->ptrs[0] = ecs_vector_get_t( + col->data, col->size, col->alignment, row); + it->sizes[0] = col->size; + } else if (subject) { + it->ptrs[0] = (void*)ecs_get_id( + it->world, subject, it->event_id); + ecs_entity_t e = ecs_get_typeid(it->world, it->event_id); + const EcsComponent *comp = ecs_get(it->world, e, EcsComponent); + if (comp) { + it->sizes[0] = comp->size; + } else { + it->sizes[0] = 0; + } + + it->subjects[0] = subject; + } + } else { + it->entities = entity; + } } } -/* Get payload pointer for key from bucket */ static -void* get_from_bucket( - ecs_bucket_t *bucket, - ecs_map_key_t key, - ecs_size_t elem_size) +bool ignore_table( + ecs_trigger_t *t, + ecs_table_t *table) { - ecs_map_key_t *keys = bucket->keys; - int32_t i, count = bucket->count; + if (!table) { + return false; + } - for (i = 0; i < count; i ++) { - if (keys[i] == key) { - return GET_ELEM(bucket->payload, elem_size, i); - } + if (!t->match_prefab && (table->flags & EcsTableIsPrefab)) { + return true; } - return NULL; + if (!t->match_disabled && (table->flags & EcsTableIsDisabled)) { + return true; + } + + return false; } -/* Grow number of buckets */ static -void rehash( - ecs_map_t *map, - int32_t bucket_count) +void notify_self_triggers( + ecs_iter_t *it, + const ecs_map_t *triggers) { - ecs_assert(bucket_count != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(bucket_count > map->bucket_count, ECS_INTERNAL_ERROR, NULL); + ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_size_t elem_size = map->elem_size; + ecs_map_iter_t mit = ecs_map_iter(triggers); + ecs_trigger_t *t; + while ((t = ecs_map_next_ptr(&mit, ecs_trigger_t*, NULL))) { + if (ignore_table(t, it->table)) { + continue; + } - ensure_buckets(map, bucket_count); + it->system = t->entity; + it->self = t->self; + it->ctx = t->ctx; + it->binding_ctx = t->binding_ctx; + it->term_index = t->term.index; + it->terms = &t->term; + t->action(it); + } +} - ecs_bucket_t *buckets = map->buckets; - ecs_assert(buckets != NULL, ECS_INTERNAL_ERROR, NULL); - - int32_t bucket_id; +static +void notify_set_triggers( + ecs_iter_t *it, + const ecs_map_t *triggers) +{ + ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); - /* Iterate backwards as elements could otherwise be moved to existing - * buckets which could temporarily cause the number of elements in a - * bucket to exceed BUCKET_COUNT. */ - for (bucket_id = bucket_count - 1; bucket_id >= 0; bucket_id --) { - ecs_bucket_t *bucket = &buckets[bucket_id]; + ecs_map_iter_t mit = ecs_map_iter(triggers); + ecs_trigger_t *t; + while ((t = ecs_map_next_ptr(&mit, ecs_trigger_t*, NULL))) { + if (ignore_table(t, it->table)) { + continue; + } - int i, count = bucket->count; - ecs_map_key_t *key_array = bucket->keys; - void *payload_array = bucket->payload; + if (flecs_term_match_table(it->world, &t->term, it->table, it->type, + it->offset, it->ids, it->columns, it->subjects, it->sizes, + it->ptrs, NULL, true)) + { + if (!it->subjects[0]) { + /* Do not match owned components */ + continue; + } - for (i = 0; i < count; i ++) { - ecs_map_key_t key = key_array[i]; - void *elem = GET_ELEM(payload_array, elem_size, i); - int32_t new_bucket_id = get_bucket_id(bucket_count, key); + ecs_entity_t event_id = it->event_id; + it->event_id = t->term.id; - if (new_bucket_id != bucket_id) { - ecs_bucket_t *new_bucket = &buckets[new_bucket_id]; + it->ids[0] = t->term.id; + it->system = t->entity; + it->self = t->self; + it->ctx = t->ctx; + it->binding_ctx = t->binding_ctx; + it->term_index = t->term.index; + it->terms = &t->term; + t->action(it); - add_to_bucket(new_bucket, elem_size, key, elem); - remove_from_bucket(bucket, elem_size, key, i); + it->event_id = event_id; + } + } +} - count --; - i --; - } +static +void notify_triggers_for_id( + const ecs_map_t *evt, + ecs_id_t event_id, + ecs_iter_t *it, + ecs_entity_t *entity, + ecs_table_t *table, + int32_t row, + int32_t count, + bool *iter_set) +{ + const ecs_id_triggers_t *idt = get_triggers_for_id(evt, event_id); + if (idt) { + if (idt->triggers) { + init_iter(it, entity, table, row, count, iter_set); + notify_self_triggers(it, idt->triggers); } - - if (!bucket->count) { - clear_bucket(bucket); + if (idt->set_triggers) { + init_iter(it, entity, table, row, count, iter_set); + notify_set_triggers(it, idt->set_triggers); } } } -ecs_map_t* _ecs_map_new( - ecs_size_t elem_size, - int32_t element_count) +void flecs_triggers_notify( + ecs_world_t *world, + ecs_poly_t *observable, + ecs_ids_t *ids, + ecs_entity_t event, + ecs_entity_t entity, + ecs_table_t *table, + ecs_table_t *other_table, + int32_t row, + int32_t count, + void *param) { - ecs_map_t *result = ecs_os_calloc(ECS_SIZEOF(ecs_map_t) * 1); - ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); + if (!ids || !ids->count) { + return; + } - int32_t bucket_count = get_bucket_count(element_count); + ecs_assert(ids->array != NULL, ECS_INTERNAL_ERROR, NULL); - result->count = 0; - result->elem_size = elem_size; + if (!observable) { + observable = world; + } - ensure_buckets(result, bucket_count); + ecs_entity_t trigger_event = event; - return result; -} + do { + const ecs_map_t *evt = get_triggers_for_event(observable, trigger_event); + if (!evt && trigger_event != EcsWildcard) { + trigger_event = EcsWildcard; + continue; + } -void ecs_map_free( - ecs_map_t *map) -{ - if (map) { - clear_buckets(map); - ecs_os_free(map); - } -} + if (!evt) { + return; + } -void* _ecs_map_get( - const ecs_map_t *map, - ecs_size_t elem_size, - ecs_map_key_t key) -{ - (void)elem_size; + ecs_iter_t it = { + .world = world, + .event = event, + .term_count = 1, + .table = table, + .type = table ? table->type : NULL, + .other_table = other_table, + .offset = row, + .count = count, + .param = param + }; - if (!map) { - return NULL; - } + int32_t i, ids_count = ids->count; + ecs_id_t *ids_array = ids->array; - ecs_assert(elem_size == map->elem_size, ECS_INVALID_PARAMETER, NULL); + for (i = 0; i < ids_count; i ++) { + ecs_id_t id = ids_array[i]; + bool iter_set = false; - ecs_bucket_t * bucket = get_bucket(map, key); - if (!bucket) { - return NULL; - } + it.event_id = id; - return get_from_bucket(bucket, key, elem_size); -} + notify_triggers_for_id( + evt, id, &it, &entity, table, row, count, &iter_set); -void* _ecs_map_get_ptr( - const ecs_map_t *map, - ecs_map_key_t key) -{ - void* ptr_ptr = _ecs_map_get(map, ECS_SIZEOF(void*), key); + if (ECS_HAS_ROLE(id, PAIR)) { + ecs_entity_t pred = ECS_PAIR_RELATION(id); + ecs_entity_t obj = ECS_PAIR_OBJECT(id); - if (ptr_ptr) { - return *(void**)ptr_ptr; - } else { - return NULL; - } -} + notify_triggers_for_id(evt, ecs_pair(pred, EcsWildcard), + &it, &entity, table, row, count, &iter_set); -bool ecs_map_has( - const ecs_map_t *map, - ecs_map_key_t key) -{ - if (!map) { - return false; - } + notify_triggers_for_id(evt, ecs_pair(EcsWildcard, obj), + &it, &entity, table, row, count, &iter_set); - ecs_bucket_t * bucket = get_bucket(map, key); - if (!bucket) { - return false; - } + notify_triggers_for_id(evt, ecs_pair(EcsWildcard, EcsWildcard), + &it, &entity, table, row, count, &iter_set); + } else { + notify_triggers_for_id(evt, EcsWildcard, + &it, &entity, table, row, count, &iter_set); + } + } - return get_from_bucket(bucket, key, 0) != NULL; + if (trigger_event == EcsWildcard) { + break; + } + + trigger_event = EcsWildcard; + } while (true); } -void* _ecs_map_ensure( - ecs_map_t *map, - ecs_size_t elem_size, - ecs_map_key_t key) +ecs_entity_t ecs_trigger_init( + ecs_world_t *world, + const ecs_trigger_desc_t *desc) { - void *result = _ecs_map_get(map, elem_size, key); - if (!result) { - result = _ecs_map_set(map, elem_size, key, NULL); - ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_os_memset(result, 0, elem_size); + ecs_poly_assert(world, ecs_world_t); + ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); + ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); + + char *name = NULL; + const char *expr = desc->expr; + + ecs_observable_t *observable = desc->observable; + if (!observable) { + observable = ecs_get_observable(world); } - return result; -} + /* If entity is provided, create it */ + ecs_entity_t existing = desc->entity.entity; + ecs_entity_t entity = ecs_entity_init(world, &desc->entity); -void* _ecs_map_set( - ecs_map_t *map, - ecs_size_t elem_size, - ecs_map_key_t key, - const void *payload) -{ - ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(elem_size == map->elem_size, ECS_INVALID_PARAMETER, NULL); + bool added = false; + EcsTrigger *comp = ecs_get_mut(world, entity, EcsTrigger, &added); + if (added) { + ecs_assert(desc->callback != NULL, ECS_INVALID_PARAMETER, NULL); + + /* Something went wrong with the construction of the entity */ + ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); + name = ecs_get_fullpath(world, entity); - ecs_bucket_t *bucket = ensure_bucket(map, key); - ecs_assert(bucket != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_term_t term; + if (expr) { + #ifdef FLECS_PARSER + const char *ptr = ecs_parse_term(world, name, expr, expr, &term); + if (!ptr) { + goto error; + } - void *elem = get_from_bucket(bucket, key, elem_size); - if (!elem) { - int32_t index = add_to_bucket(bucket, elem_size, key, payload); - - int32_t map_count = ++map->count; - int32_t target_bucket_count = get_bucket_count(map_count); - int32_t map_bucket_count = map->bucket_count; + if (!ecs_term_is_initialized(&term)) { + ecs_parser_error( + name, expr, 0, "invalid empty trigger expression"); + goto error; + } - if (target_bucket_count > map_bucket_count) { - rehash(map, target_bucket_count); - bucket = ensure_bucket(map, key); - return get_from_bucket(bucket, key, elem_size); + if (ptr[0]) { + ecs_parser_error(name, expr, 0, + "too many terms in trigger expression (expected 1)"); + goto error; + } + #else + ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); + #endif } else { - return GET_ELEM(bucket->payload, elem_size, index); - } - } else { - if (payload) { - ecs_os_memcpy(elem, payload, elem_size); + term = ecs_term_copy(&desc->term); } - return elem; - } -} - -void ecs_map_remove( - ecs_map_t *map, - ecs_map_key_t key) -{ - ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); - - ecs_bucket_t * bucket = get_bucket(map, key); - if (!bucket) { - return; - } - int32_t i, bucket_count = bucket->count; - for (i = 0; i < bucket_count; i ++) { - if (bucket->keys[i] == key) { - remove_from_bucket(bucket, map->elem_size, key, i); - map->count --; + if (ecs_term_finalize(world, name, &term)) { + goto error; } - } -} -int32_t ecs_map_count( - const ecs_map_t *map) -{ - return map ? map->count : 0; -} + /* Currently triggers are not supported for specific entities */ + ecs_assert(term.subj.entity == EcsThis, ECS_UNSUPPORTED, NULL); -int32_t ecs_map_bucket_count( - const ecs_map_t *map) -{ - return map ? map->bucket_count : 0; -} + ecs_trigger_t *trigger = flecs_sparse_add(world->triggers, ecs_trigger_t); + trigger->id = flecs_sparse_last_id(world->triggers); + trigger->term = ecs_term_move(&term); + trigger->action = desc->callback; + trigger->ctx = desc->ctx; + trigger->binding_ctx = desc->binding_ctx; + trigger->ctx_free = desc->ctx_free; + trigger->binding_ctx_free = desc->binding_ctx_free; + trigger->event_count = count_events(desc->events); + ecs_os_memcpy(trigger->events, desc->events, + trigger->event_count * ECS_SIZEOF(ecs_entity_t)); + trigger->entity = entity; + trigger->self = desc->self; + trigger->observable = observable; + trigger->match_prefab = desc->match_prefab; + trigger->match_disabled = desc->match_disabled; -void ecs_map_clear( - ecs_map_t *map) -{ - ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); - clear_buckets(map); - map->count = 0; -} + comp->trigger = trigger; -ecs_map_iter_t ecs_map_iter( - const ecs_map_t *map) -{ - return (ecs_map_iter_t){ - .map = map, - .bucket = NULL, - .bucket_index = 0, - .element_index = 0 - }; -} + /* Trigger must have at least one event */ + ecs_assert(trigger->event_count != 0, ECS_INVALID_PARAMETER, NULL); -void* _ecs_map_next( - ecs_map_iter_t *iter, - ecs_size_t elem_size, - ecs_map_key_t *key_out) -{ - const ecs_map_t *map = iter->map; - if (!map) { - return NULL; - } - - ecs_assert(!elem_size || elem_size == map->elem_size, ECS_INVALID_PARAMETER, NULL); - - ecs_bucket_t *bucket = iter->bucket; - int32_t element_index = iter->element_index; - elem_size = map->elem_size; + register_trigger(world, observable, trigger); - do { - if (!bucket) { - int32_t bucket_index = iter->bucket_index; - ecs_bucket_t *buckets = map->buckets; - if (bucket_index < map->bucket_count) { - bucket = &buckets[bucket_index]; - iter->bucket = bucket; + ecs_term_fini(&term); - element_index = 0; - iter->element_index = 0; - } else { - return NULL; - } + if (desc->entity.name) { + ecs_trace("#[green]observer#[reset] %s created", + ecs_get_name(world, entity)); } + } else { + ecs_assert(comp->trigger != NULL, ECS_INTERNAL_ERROR, NULL); - if (element_index < bucket->count) { - iter->element_index = element_index + 1; - break; - } else { - bucket = NULL; - iter->bucket_index ++; + /* If existing entity handle was provided, override existing params */ + if (existing) { + if (desc->callback) { + ((ecs_trigger_t*)comp->trigger)->action = desc->callback; + } + if (desc->ctx) { + ((ecs_trigger_t*)comp->trigger)->ctx = desc->ctx; + } + if (desc->binding_ctx) { + ((ecs_trigger_t*)comp->trigger)->binding_ctx = desc->binding_ctx; + } } - } while (true); - - if (key_out) { - *key_out = bucket->keys[element_index]; } - return GET_ELEM(bucket->payload, elem_size, element_index); + ecs_os_free(name); + return entity; +error: + ecs_os_free(name); + return 0; } -void* _ecs_map_next_ptr( - ecs_map_iter_t *iter, - ecs_map_key_t *key_out) +void* ecs_get_trigger_ctx( + const ecs_world_t *world, + ecs_entity_t trigger) { - void *result = _ecs_map_next(iter, ECS_SIZEOF(void*), key_out); - if (result) { - return *(void**)result; + const EcsTrigger *t = ecs_get(world, trigger, EcsTrigger); + if (t) { + return t->trigger->ctx; } else { return NULL; - } + } } -void ecs_map_grow( - ecs_map_t *map, - int32_t element_count) +void* ecs_get_trigger_binding_ctx( + const ecs_world_t *world, + ecs_entity_t trigger) { - ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); - int32_t target_count = map->count + element_count; - int32_t bucket_count = get_bucket_count(target_count); - - if (bucket_count > map->bucket_count) { - rehash(map, bucket_count); - } + const EcsTrigger *t = ecs_get(world, trigger, EcsTrigger); + if (t) { + return t->trigger->binding_ctx; + } else { + return NULL; + } } -void ecs_map_set_size( - ecs_map_t *map, - int32_t element_count) -{ - ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); - int32_t bucket_count = get_bucket_count(element_count); +void flecs_trigger_fini( + ecs_world_t *world, + ecs_trigger_t *trigger) +{ + ecs_assert(trigger != NULL, ECS_INVALID_PARAMETER, NULL); + + unregister_trigger(trigger->observable, trigger); + ecs_term_fini(&trigger->term); - if (bucket_count) { - rehash(map, bucket_count); + if (trigger->ctx_free) { + trigger->ctx_free(trigger->ctx); } -} -ecs_map_t* ecs_map_copy( - ecs_map_t *map) -{ - if (!map) { - return NULL; + if (trigger->binding_ctx_free) { + trigger->binding_ctx_free(trigger->binding_ctx); } - ecs_size_t elem_size = map->elem_size; - ecs_map_t *result = _ecs_map_new(map->elem_size, ecs_map_count(map)); + flecs_sparse_remove(world->triggers, trigger->id); +} - ecs_map_iter_t it = ecs_map_iter(map); - ecs_map_key_t key; - void *ptr; - while ((ptr = _ecs_map_next(&it, elem_size, &key))) { - _ecs_map_set(result, elem_size, key, ptr); - } +#ifdef FLECS_DEPRECATED - return result; -} -void ecs_map_memory( - ecs_map_t *map, - int32_t *allocd, - int32_t *used) -{ - ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); +#endif - if (used) { - *used = map->count * map->elem_size; - } +#ifdef FLECS_PARSER - if (allocd) { - *allocd += ECS_SIZEOF(ecs_map_t); - int i, bucket_count = map->bucket_count; - for (i = 0; i < bucket_count; i ++) { - ecs_bucket_t *bucket = &map->buckets[i]; - *allocd += KEY_SIZE * bucket->count; - *allocd += map->elem_size * bucket->count; - } +#define ECS_ANNOTATION_LENGTH_MAX (16) - *allocd += ECS_SIZEOF(ecs_bucket_t) * bucket_count; - } -} +#define TOK_NEWLINE '\n' +#define TOK_COLON ':' +#define TOK_AND ',' +#define TOK_OR "||" +#define TOK_NOT '!' +#define TOK_OPTIONAL '?' +#define TOK_BITWISE_OR '|' +#define TOK_NAME_SEP '.' +#define TOK_BRACKET_OPEN '[' +#define TOK_BRACKET_CLOSE ']' +#define TOK_WILDCARD '*' +#define TOK_SINGLETON '$' +#define TOK_PAREN_OPEN '(' +#define TOK_PAREN_CLOSE ')' +#define TOK_AS_ENTITY '\\' -typedef struct ecs_hm_bucket_t { - ecs_vector_t *keys; - ecs_vector_t *values; -} ecs_hm_bucket_t; +#define TOK_SELF "self" +#define TOK_SUPERSET "super" +#define TOK_SUBSET "sub" +#define TOK_CASCADE "cascade" +#define TOK_PARENT "parent" +#define TOK_ALL "all" -static -int32_t find_key( - ecs_hashmap_t map, - ecs_vector_t *keys, - ecs_size_t key_size, - const void *key) -{ - int32_t i, count = ecs_vector_count(keys); - void *key_array = ecs_vector_first_t(keys, key_size, 8); - for (i = 0; i < count; i ++) { - void *key_ptr = ECS_OFFSET(key_array, key_size * i); - if (map.compare(key_ptr, key) == 0) { - return i; - } - } - return -1; -} +#define TOK_OWNED "OVERRIDE" -ecs_hashmap_t _flecs_hashmap_new( - ecs_size_t key_size, - ecs_size_t value_size, - ecs_hash_value_action_t hash, - ecs_compare_action_t compare) -{ - return (ecs_hashmap_t){ - .key_size = key_size, - .value_size = value_size, - .compare = compare, - .hash = hash, - .impl = ecs_map_new(ecs_hm_bucket_t, 0) - }; -} +#define TOK_ROLE_PAIR "PAIR" +#define TOK_ROLE_AND "AND" +#define TOK_ROLE_OR "OR" +#define TOK_ROLE_XOR "XOR" +#define TOK_ROLE_NOT "NOT" +#define TOK_ROLE_SWITCH "SWITCH" +#define TOK_ROLE_CASE "CASE" +#define TOK_ROLE_DISABLED "DISABLED" -void flecs_hashmap_free( - ecs_hashmap_t map) +#define TOK_IN "in" +#define TOK_OUT "out" +#define TOK_INOUT "inout" + +#define ECS_MAX_TOKEN_SIZE (256) + +typedef char ecs_token_t[ECS_MAX_TOKEN_SIZE]; + +const char* ecs_parse_eol_and_whitespace( + const char *ptr) { - ecs_map_iter_t it = ecs_map_iter(map.impl); - ecs_hm_bucket_t *bucket; - while ((bucket = ecs_map_next(&it, ecs_hm_bucket_t, NULL))) { - ecs_vector_free(bucket->keys); - ecs_vector_free(bucket->values); + while (isspace(*ptr)) { + ptr ++; } - ecs_map_free(map.impl); + return ptr; } -ecs_hashmap_t flecs_hashmap_copy( - const ecs_hashmap_t src) +/** Skip spaces when parsing signature */ +const char* ecs_parse_whitespace( + const char *ptr) { - ecs_hashmap_t result = src; - result.impl = ecs_map_copy(result.impl); - - ecs_map_iter_t it = ecs_map_iter(result.impl); - ecs_hm_bucket_t *bucket; - while ((bucket = ecs_map_next(&it, ecs_hm_bucket_t, NULL))) { - bucket->keys = ecs_vector_copy_t(bucket->keys, result.key_size, 8); - bucket->values = ecs_vector_copy_t(bucket->values, result.value_size, 8); + while ((*ptr != '\n') && isspace(*ptr)) { + ptr ++; } - return result; + return ptr; } -void* _flecs_hashmap_get( - const ecs_hashmap_t map, - ecs_size_t key_size, - const void *key, - ecs_size_t value_size) +const char* ecs_parse_digit( + const char *ptr, + char *token) { - ecs_assert(map.key_size == key_size, ECS_INVALID_PARAMETER, NULL); - ecs_assert(map.value_size == value_size, ECS_INVALID_PARAMETER, NULL); + char *tptr = token; + char ch = ptr[0]; - uint64_t hash = map.hash(key); - ecs_hm_bucket_t *bucket = ecs_map_get(map.impl, ecs_hm_bucket_t, hash); - if (!bucket) { + if (!isdigit(ch) && ch != '-') { + ecs_parser_error(NULL, NULL, 0, "invalid start of number '%s'", ptr); return NULL; } - int32_t index = find_key(map, bucket->keys, key_size, key); - if (index == -1) { - return NULL; + tptr[0] = ch; + tptr ++; + ptr ++; + + for (; (ch = *ptr); ptr ++) { + if (!isdigit(ch)) { + break; + } + + tptr[0] = ch; + tptr ++; } - return ecs_vector_get_t(bucket->values, value_size, 8, index); + tptr[0] = '\0'; + + return ptr; } -flecs_hashmap_result_t _flecs_hashmap_ensure( - const ecs_hashmap_t map, - ecs_size_t key_size, - void *key, - ecs_size_t value_size) +static +bool is_newline_comment( + const char *ptr) { - ecs_assert(map.key_size == key_size, ECS_INVALID_PARAMETER, NULL); - ecs_assert(map.value_size == value_size, ECS_INVALID_PARAMETER, NULL); + if (ptr[0] == '/' && ptr[1] == '/') { + return true; + } + return false; +} - uint64_t hash = map.hash(key); - ecs_hm_bucket_t *bucket = ecs_map_ensure(map.impl, ecs_hm_bucket_t, hash); - ecs_assert(bucket != NULL, ECS_INTERNAL_ERROR, NULL); +const char* ecs_parse_fluff( + const char *ptr, + char **last_comment) +{ + const char *last_comment_start = NULL; - void *value_ptr, *key_ptr; + do { + /* Skip whitespaces before checking for a comment */ + ptr = ecs_parse_whitespace(ptr); - ecs_vector_t *keys = bucket->keys; - if (!keys) { - bucket->keys = ecs_vector_new_t(key_size, 8, 1); - bucket->values = ecs_vector_new_t(value_size, 8, 1); - key_ptr = ecs_vector_add_t(&bucket->keys, key_size, 8); - ecs_os_memcpy(key_ptr, key, key_size); - value_ptr = ecs_vector_add_t(&bucket->values, value_size, 8); - ecs_os_memset(value_ptr, 0, value_size); - } else { - int32_t index = find_key(map, keys, key_size, key); - if (index == -1) { - key_ptr = ecs_vector_add_t(&bucket->keys, key_size, 8); - ecs_os_memcpy(key_ptr, key, key_size); - value_ptr = ecs_vector_add_t(&bucket->values, value_size, 8); - ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_os_memset(value_ptr, 0, value_size); - } else { - key_ptr = ecs_vector_get_t(bucket->keys, key_size, 8, index); - value_ptr = ecs_vector_get_t(bucket->values, value_size, 8, index); + /* Newline comment, skip until newline character */ + if (is_newline_comment(ptr)) { + ptr += 2; + last_comment_start = ptr; + + while (ptr[0] && ptr[0] != TOK_NEWLINE) { + ptr ++; + } + } + + /* If a newline character is found, skip it */ + if (ptr[0] == TOK_NEWLINE) { + ptr ++; } + + } while (isspace(ptr[0]) || is_newline_comment(ptr)); + + if (last_comment) { + *last_comment = (char*)last_comment_start; } - return (flecs_hashmap_result_t){ - .key = key_ptr, - .value = value_ptr, - .hash = hash - }; + return ptr; } -void _flecs_hashmap_set( - const ecs_hashmap_t map, - ecs_size_t key_size, - void *key, - ecs_size_t value_size, - const void *value) -{ - void *value_ptr = _flecs_hashmap_ensure(map, key_size, key, value_size).value; - ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_os_memcpy(value_ptr, value, value_size); -} +/* -- Private functions -- */ -void _flecs_hashmap_remove_w_hash( - const ecs_hashmap_t map, - ecs_size_t key_size, - const void *key, - ecs_size_t value_size, - uint64_t hash) +static +bool valid_identifier_start_char( + char ch) { - ecs_hm_bucket_t *bucket = ecs_map_get(map.impl, ecs_hm_bucket_t, hash); - if (!bucket) { - return; + if (ch && (isalpha(ch) || (ch == '.') || (ch == '_') || (ch == '*') || + (ch == '0') || (ch == TOK_AS_ENTITY) || isdigit(ch))) + { + return true; } - int32_t index = find_key(map, bucket->keys, key_size, key); - if (index == -1) { - return; + return false; +} + +static +bool valid_token_start_char( + char ch) +{ + if ((ch == '"') || (ch == '{') || (ch == '}') || (ch == ',') || (ch == '-') + || (ch == '[') || (ch == ']') || valid_identifier_start_char(ch)) + { + return true; } - ecs_vector_remove_t(bucket->keys, key_size, 8, index); - ecs_vector_remove_t(bucket->values, value_size, 8, index); + return false; +} - if (!ecs_vector_count(bucket->keys)) { - ecs_vector_free(bucket->keys); - ecs_vector_free(bucket->values); - ecs_map_remove(map.impl, hash); +static +bool valid_token_char( + char ch) +{ + if (ch && + (isalpha(ch) || isdigit(ch) || ch == '_' || ch == '.' || ch == '"')) + { + return true; } + + return false; } -void _flecs_hashmap_remove( - const ecs_hashmap_t map, - ecs_size_t key_size, - const void *key, - ecs_size_t value_size) +static +bool valid_operator_char( + char ch) { - ecs_assert(map.key_size == key_size, ECS_INVALID_PARAMETER, NULL); - ecs_assert(map.value_size == value_size, ECS_INVALID_PARAMETER, NULL); + if (ch == TOK_OPTIONAL || ch == TOK_NOT) { + return true; + } - uint64_t hash = map.hash(key); - _flecs_hashmap_remove_w_hash(map, key_size, key, value_size, hash); + return false; } -flecs_hashmap_iter_t flecs_hashmap_iter( - ecs_hashmap_t map) +static +const char* parse_digit( + const char *ptr, + char *token_out) { - return (flecs_hashmap_iter_t){ - .it = ecs_map_iter(map.impl) - }; + ptr = ecs_parse_whitespace(ptr); + ptr = ecs_parse_digit(ptr, token_out); + return ecs_parse_whitespace(ptr); } -void* _flecs_hashmap_next( - flecs_hashmap_iter_t *it, - ecs_size_t key_size, - void *key_out, - ecs_size_t value_size) +const char* ecs_parse_token( + const char *name, + const char *expr, + const char *ptr, + char *token_out) { - int32_t index = ++ it->index; - ecs_hm_bucket_t *bucket = it->bucket; - while (!bucket || it->index >= ecs_vector_count(bucket->keys)) { - bucket = it->bucket = ecs_map_next(&it->it, ecs_hm_bucket_t, NULL); - if (!bucket) { - return NULL; + int64_t column = ptr - expr; + + ptr = ecs_parse_whitespace(ptr); + char *tptr = token_out, ch = ptr[0]; + + if (!valid_token_start_char(ch)) { + if (ch == '\0' || ch == '\n') { + ecs_parser_error(name, expr, column, + "unexpected end of expression"); + } else { + ecs_parser_error(name, expr, column, + "invalid start of token '%s'", ptr); } - index = it->index = 0; + return NULL; } - if (key_out) { - *(void**)key_out = ecs_vector_get_t(bucket->keys, key_size, 8, index); + tptr[0] = ch; + tptr ++; + ptr ++; + + if (ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == ',') { + tptr[0] = 0; + return ptr; } - - return ecs_vector_get_t(bucket->values, value_size, 8, index); -} -#ifdef FLECS_LOG + int tmpl_nesting = 0; + bool in_str = ch == '"'; -static -char *ecs_vasprintf( - const char *fmt, - va_list args) -{ - ecs_size_t size = 0; - char *result = NULL; - va_list tmpa; + for (; (ch = *ptr); ptr ++) { + if (ch == '<') { + tmpl_nesting ++; + } else if (ch == '>') { + if (!tmpl_nesting) { + break; + } + tmpl_nesting --; + } else if (ch == '"') { + in_str = !in_str; + } else + if (!valid_token_char(ch) && !in_str) { + break; + } - va_copy(tmpa, args); + tptr[0] = ch; + tptr ++; + } - size = vsnprintf(result, 0, fmt, tmpa); + tptr[0] = '\0'; - va_end(tmpa); + if (tmpl_nesting != 0) { + ecs_parser_error(name, expr, column, + "identifier '%s' has mismatching < > pairs", ptr); + return NULL; + } - if ((int32_t)size < 0) { - return NULL; + const char *next_ptr = ecs_parse_whitespace(ptr); + if (next_ptr[0] == ':' && next_ptr != ptr) { + /* Whitespace between token and : is significant */ + ptr = next_ptr - 1; + } else { + ptr = next_ptr; } - result = (char *) ecs_os_malloc(size + 1); + return ptr; +} - if (!result) { - return NULL; +static +const char* ecs_parse_identifier( + const char *name, + const char *expr, + const char *ptr, + char *token_out) +{ + if (!valid_identifier_start_char(ptr[0])) { + ecs_parser_error(name, expr, (ptr - expr), + "expected start of identifier"); + return NULL; } - ecs_os_vsprintf(result, fmt, args); + ptr = ecs_parse_token(name, expr, ptr, token_out); - return result; + return ptr; } static -void ecs_colorize_buf( - char *msg, - bool enable_colors, - ecs_strbuf_t *buf) +int parse_identifier( + const char *token, + ecs_term_id_t *out) { - char *ptr, ch, prev = '\0'; - bool isNum = false; - char isStr = '\0'; - bool isVar = false; - bool overrideColor = false; - bool autoColor = true; - bool dontAppend = false; + char ch = token[0]; - for (ptr = msg; (ch = *ptr); ptr++) { - dontAppend = false; + const char *tptr = token; + if (ch == TOK_AS_ENTITY) { + tptr ++; + } - if (!overrideColor) { - if (isNum && !isdigit(ch) && !isalpha(ch) && (ch != '.') && (ch != '%')) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - isNum = false; - } - if (isStr && (isStr == ch) && prev != '\\') { - isStr = '\0'; - } else if (((ch == '\'') || (ch == '"')) && !isStr && - !isalpha(prev) && (prev != '\\')) - { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_CYAN); - isStr = ch; - } + out->name = ecs_os_strdup(tptr); - if ((isdigit(ch) || (ch == '%' && isdigit(prev)) || - (ch == '-' && isdigit(ptr[1]))) && !isNum && !isStr && !isVar && - !isalpha(prev) && !isdigit(prev) && (prev != '_') && - (prev != '.')) - { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_GREEN); - isNum = true; - } + if (ch == TOK_AS_ENTITY) { + out->var = EcsVarIsEntity; + } - if (isVar && !isalpha(ch) && !isdigit(ch) && ch != '_') { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - isVar = false; - } + return 0; +} - if (!isStr && !isVar && ch == '$' && isalpha(ptr[1])) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_CYAN); - isVar = true; - } - } +static +ecs_entity_t parse_role( + const char *name, + const char *sig, + int64_t column, + const char *token) +{ + if (!ecs_os_strcmp(token, TOK_ROLE_PAIR)) + { + return ECS_PAIR; + } else if (!ecs_os_strcmp(token, TOK_ROLE_AND)) { + return ECS_AND; + } else if (!ecs_os_strcmp(token, TOK_ROLE_OR)) { + return ECS_OR; + } else if (!ecs_os_strcmp(token, TOK_ROLE_XOR)) { + return ECS_XOR; + } else if (!ecs_os_strcmp(token, TOK_ROLE_NOT)) { + return ECS_NOT; + } else if (!ecs_os_strcmp(token, TOK_ROLE_SWITCH)) { + return ECS_SWITCH; + } else if (!ecs_os_strcmp(token, TOK_ROLE_CASE)) { + return ECS_CASE; + } else if (!ecs_os_strcmp(token, TOK_OWNED)) { + return ECS_OVERRIDE; + } else if (!ecs_os_strcmp(token, TOK_ROLE_DISABLED)) { + return ECS_DISABLED; + } else { + ecs_parser_error(name, sig, column, "invalid role '%s'", token); + return 0; + } +} - if (!isVar && !isStr && !isNum && ch == '#' && ptr[1] == '[') { - bool isColor = true; - overrideColor = true; - - /* Custom colors */ - if (!ecs_os_strncmp(&ptr[2], "]", ecs_os_strlen("]"))) { - autoColor = false; - } else if (!ecs_os_strncmp(&ptr[2], "green]", ecs_os_strlen("green]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_GREEN); - } else if (!ecs_os_strncmp(&ptr[2], "red]", ecs_os_strlen("red]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_RED); - } else if (!ecs_os_strncmp(&ptr[2], "blue]", ecs_os_strlen("red]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_BLUE); - } else if (!ecs_os_strncmp(&ptr[2], "magenta]", ecs_os_strlen("magenta]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_MAGENTA); - } else if (!ecs_os_strncmp(&ptr[2], "cyan]", ecs_os_strlen("cyan]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_CYAN); - } else if (!ecs_os_strncmp(&ptr[2], "yellow]", ecs_os_strlen("yellow]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_YELLOW); - } else if (!ecs_os_strncmp(&ptr[2], "grey]", ecs_os_strlen("grey]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_GREY); - } else if (!ecs_os_strncmp(&ptr[2], "white]", ecs_os_strlen("white]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - } else if (!ecs_os_strncmp(&ptr[2], "bold]", ecs_os_strlen("bold]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_BOLD); - } else if (!ecs_os_strncmp(&ptr[2], "normal]", ecs_os_strlen("normal]"))) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - } else if (!ecs_os_strncmp(&ptr[2], "reset]", ecs_os_strlen("reset]"))) { - overrideColor = false; - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - } else { - isColor = false; - overrideColor = false; - } +static +ecs_oper_kind_t parse_operator( + char ch) +{ + if (ch == TOK_OPTIONAL) { + return EcsOptional; + } else if (ch == TOK_NOT) { + return EcsNot; + } else { + ecs_abort(ECS_INTERNAL_ERROR, NULL); + } +} - if (isColor) { - ptr += 2; - while ((ch = *ptr) != ']') ptr ++; - dontAppend = true; - } - if (!autoColor) { - overrideColor = true; - } - } +static +const char* parse_annotation( + const char *name, + const char *sig, + int64_t column, + const char *ptr, + ecs_inout_kind_t *inout_kind_out) +{ + char token[ECS_MAX_TOKEN_SIZE]; - if (ch == '\n') { - if (isNum || isStr || isVar || overrideColor) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - overrideColor = false; - isNum = false; - isStr = false; - isVar = false; - } - } + ptr = ecs_parse_identifier(name, sig, ptr, token); + if (!ptr) { + return NULL; + } - if (!dontAppend) { - ecs_strbuf_appendstrn(buf, ptr, 1); - } + if (!strcmp(token, "in")) { + *inout_kind_out = EcsIn; + } else + if (!strcmp(token, "out")) { + *inout_kind_out = EcsOut; + } else + if (!strcmp(token, "inout")) { + *inout_kind_out = EcsInOut; + } - if (!overrideColor) { - if (((ch == '\'') || (ch == '"')) && !isStr) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - } - } + ptr = ecs_parse_whitespace(ptr); - prev = ch; + if (ptr[0] != TOK_BRACKET_CLOSE) { + ecs_parser_error(name, sig, column, "expected ]"); + return NULL; } - if (isNum || isStr || isVar || overrideColor) { - if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); - } + return ptr + 1; } static -void log_print( - int level, - const char *file, - int32_t line, - const char *fmt, - va_list args) +uint8_t parse_set_token( + const char *token) { - (void)level; - (void)line; - - ecs_strbuf_t msg_buf = ECS_STRBUF_INIT; - - if (level > ecs_os_api.log_level_) { - return; + if (!ecs_os_strcmp(token, TOK_SELF)) { + return EcsSelf; + } else if (!ecs_os_strcmp(token, TOK_SUPERSET)) { + return EcsSuperSet; + } else if (!ecs_os_strcmp(token, TOK_SUBSET)) { + return EcsSubSet; + } else if (!ecs_os_strcmp(token, TOK_CASCADE)) { + return EcsCascade; + } else if (!ecs_os_strcmp(token, TOK_ALL)) { + return EcsAll; + } else if (!ecs_os_strcmp(token, TOK_PARENT)) { + return EcsParent; + } else { + return 0; } - - /* Apply color. Even if we don't want color, we still need to call the - * colorize function to get rid of the color tags (e.g. #[green]) */ - char *msg_nocolor = ecs_vasprintf(fmt, args); - ecs_colorize_buf(msg_nocolor, ecs_os_api.log_with_color_, &msg_buf); - ecs_os_free(msg_nocolor); - - char *msg = ecs_strbuf_get(&msg_buf); - ecs_os_api.log_(level, file, line, msg); - ecs_os_free(msg); } -void _ecs_log( - int level, - const char *file, - int32_t line, - const char *fmt, - ...) +static +const char* parse_set_expr( + const ecs_world_t *world, + const char *name, + const char *expr, + int64_t column, + const char *ptr, + char *token, + ecs_term_id_t *id, + char tok_end) { - va_list args; - va_start(args, fmt); - log_print(level, file, line, fmt, args); - va_end(args); -} + char token_buf[ECS_MAX_TOKEN_SIZE] = {0}; + if (!token) { + token = token_buf; + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + return NULL; + } + } -void ecs_log_push(void) { - ecs_os_api.log_indent_ ++; -} + do { + uint8_t tok = parse_set_token(token); + if (!tok) { + ecs_parser_error(name, expr, column, + "invalid set token '%s'", token); + return NULL; + } -void ecs_log_pop(void) { - ecs_os_api.log_indent_ --; -} + if (id->set.mask & tok) { + ecs_parser_error(name, expr, column, + "duplicate set token '%s'", token); + return NULL; + } -void ecs_log_set_level( - int level) -{ - ecs_os_api.log_level_ = level; -} + if ((tok == EcsSubSet && id->set.mask & EcsSuperSet) || + (tok == EcsSuperSet && id->set.mask & EcsSubSet)) + { + ecs_parser_error(name, expr, column, + "cannot mix super and sub", token); + return NULL; + } + + id->set.mask |= tok; -void ecs_log_enable_colors( - bool enabled) -{ - ecs_os_api.log_with_color_ = enabled; -} + if (ptr[0] == TOK_PAREN_OPEN) { + ptr ++; -void _ecs_parser_errorv( - const char *name, - const char *expr, - int64_t column_arg, - const char *fmt, - va_list args) -{ - int32_t column = flecs_to_i32(column_arg); + /* Relationship (overrides IsA default) */ + if (!isdigit(ptr[0]) && valid_token_start_char(ptr[0])) { + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + return NULL; + } - if (ecs_os_api.log_level_ >= -2) { - ecs_strbuf_t msg_buf = ECS_STRBUF_INIT; + id->set.relation = ecs_lookup_fullpath(world, token); + if (!id->set.relation) { + ecs_parser_error(name, expr, column, + "unresolved identifier '%s'", token); + return NULL; + } - ecs_strbuf_vappend(&msg_buf, fmt, args); + if (ptr[0] == TOK_AND) { + ptr = ecs_parse_whitespace(ptr + 1); + } else if (ptr[0] != TOK_PAREN_CLOSE) { + ecs_parser_error(name, expr, column, + "expected ',' or ')'"); + return NULL; + } + } - if (expr) { - ecs_strbuf_appendstr(&msg_buf, "\n"); + /* Max depth of search */ + if (isdigit(ptr[0])) { + ptr = parse_digit(ptr, token); + if (!ptr) { + return NULL; + } - /* Find start of line by taking column and looking for the - * last occurring newline */ - if (column != -1) { - const char *ptr = &expr[column]; - while (ptr[0] != '\n' && ptr > expr) { - ptr --; + id->set.max_depth = atoi(token); + if (id->set.max_depth < 0) { + ecs_parser_error(name, expr, column, + "invalid negative depth"); + return NULL; } - if (ptr == expr) { - /* ptr is already at start of line */ - } else { - column -= (int32_t)(ptr - expr + 1); - expr = ptr + 1; + if (ptr[0] == ',') { + ptr = ecs_parse_whitespace(ptr + 1); } } - /* Strip newlines from current statement, if any */ - char *newline_ptr = strchr(expr, '\n'); - if (newline_ptr) { - /* Strip newline from expr */ - ecs_strbuf_appendstrn(&msg_buf, expr, - (int32_t)(newline_ptr - expr)); - } else { - ecs_strbuf_appendstr(&msg_buf, expr); + /* If another digit is found, previous depth was min depth */ + if (isdigit(ptr[0])) { + ptr = parse_digit(ptr, token); + if (!ptr) { + return NULL; + } + + id->set.min_depth = id->set.max_depth; + id->set.max_depth = atoi(token); + if (id->set.max_depth < 0) { + ecs_parser_error(name, expr, column, + "invalid negative depth"); + return NULL; + } } - ecs_strbuf_appendstr(&msg_buf, "\n"); + if (ptr[0] != TOK_PAREN_CLOSE) { + ecs_parser_error(name, expr, column, "expected ')', got '%c'", + ptr[0]); + return NULL; + } else { + ptr = ecs_parse_whitespace(ptr + 1); + if (ptr[0] != tok_end && ptr[0] != TOK_AND && ptr[0] != 0) { + ecs_parser_error(name, expr, column, + "expected end of set expr"); + return NULL; + } + } + } - if (column != -1) { - ecs_strbuf_append(&msg_buf, "%*s^", column, ""); + /* Next token in set expression */ + if (ptr[0] == TOK_BITWISE_OR) { + ptr ++; + if (valid_token_start_char(ptr[0])) { + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + return NULL; + } } + + /* End of set expression */ + } else if (ptr[0] == tok_end || ptr[0] == TOK_AND || !ptr[0]) { + break; } + } while (true); - char *msg = ecs_strbuf_get(&msg_buf); - ecs_os_err(name, 0, msg); - ecs_os_free(msg); + if (id->set.mask & EcsCascade && !(id->set.mask & EcsSuperSet) && + !(id->set.mask & EcsSubSet)) + { + /* If cascade is used without specifying super or sub, assume + * super */ + id->set.mask |= EcsSuperSet; + } + + if (id->set.mask & EcsSelf && id->set.min_depth != 0) { + ecs_parser_error(name, expr, column, + "min_depth must be zero for set expression with 'self'"); + return NULL; } + + return ptr; } -void _ecs_parser_error( +static +const char* parse_arguments( + const ecs_world_t *world, const char *name, - const char *expr, + const char *expr, int64_t column, - const char *fmt, - ...) + const char *ptr, + char *token, + ecs_term_t *term) { - if (ecs_os_api.log_level_ >= -2) { - va_list args; - va_start(args, fmt); - _ecs_parser_errorv(name, expr, column, fmt, args); - va_end(args); - } -} + (void)column; -void _ecs_abort( - int32_t err, - const char *file, - int32_t line, - const char *fmt, - ...) -{ - if (fmt) { - va_list args; - va_start(args, fmt); - char *msg = ecs_vasprintf(fmt, args); - va_end(args); - _ecs_fatal(file, line, "%s (%s)", msg, ecs_strerror(err)); - ecs_os_free(msg); - } else { - _ecs_fatal(file, line, "%s", ecs_strerror(err)); - } + int32_t arg = 0; - ecs_os_abort(); -} + do { + if (valid_token_start_char(ptr[0])) { + if (arg == 2) { + ecs_parser_error(name, expr, (ptr - expr), + "too many arguments in term"); + return NULL; + } -void _ecs_assert( - bool condition, - int32_t err, - const char *cond_str, - const char *file, - int32_t line, - const char *fmt, - ...) -{ - if (!condition) { - if (fmt) { - va_list args; - va_start(args, fmt); - char *msg = ecs_vasprintf(fmt, args); - va_end(args); - _ecs_fatal(file, line, "assert(%s) %s (%s)", - cond_str, msg, ecs_strerror(err)); - ecs_os_free(msg); - } else { - _ecs_fatal(file, line, "assert(%s) %s", - cond_str, ecs_strerror(err)); - } + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + return NULL; + } - ecs_os_abort(); - } -} + ecs_term_id_t *term_id; -void _ecs_deprecated( - const char *file, - int32_t line, - const char *msg) -{ - _ecs_err(file, line, "%s", msg); -} + if (arg == 0) { + term_id = &term->subj; + } else if (arg == 1) { + term_id = &term->obj; + } -#define ECS_ERR_STR(code) case code: return &(#code[4]) + /* If token is a colon, the token is an identifier followed by a + * set expression. */ + if (ptr[0] == TOK_COLON) { + if (parse_identifier(token, term_id)) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid identifier '%s'", token); + return NULL; + } -const char* ecs_strerror( - int32_t error_code) -{ - switch (error_code) { - ECS_ERR_STR(ECS_INVALID_PARAMETER); - ECS_ERR_STR(ECS_NOT_A_COMPONENT); - ECS_ERR_STR(ECS_TYPE_NOT_AN_ENTITY); - ECS_ERR_STR(ECS_INTERNAL_ERROR); - ECS_ERR_STR(ECS_ALREADY_DEFINED); - ECS_ERR_STR(ECS_INVALID_COMPONENT_SIZE); - ECS_ERR_STR(ECS_INVALID_COMPONENT_ALIGNMENT); - ECS_ERR_STR(ECS_OUT_OF_MEMORY); - ECS_ERR_STR(ECS_MODULE_UNDEFINED); - ECS_ERR_STR(ECS_COLUMN_INDEX_OUT_OF_RANGE); - ECS_ERR_STR(ECS_COLUMN_IS_NOT_SHARED); - ECS_ERR_STR(ECS_COLUMN_IS_SHARED); - ECS_ERR_STR(ECS_COLUMN_HAS_NO_DATA); - ECS_ERR_STR(ECS_COLUMN_TYPE_MISMATCH); - ECS_ERR_STR(ECS_INVALID_WHILE_ITERATING); - ECS_ERR_STR(ECS_INVALID_FROM_WORKER); - ECS_ERR_STR(ECS_OUT_OF_RANGE); - ECS_ERR_STR(ECS_THREAD_ERROR); - ECS_ERR_STR(ECS_MISSING_OS_API); - ECS_ERR_STR(ECS_UNSUPPORTED); - ECS_ERR_STR(ECS_NO_OUT_COLUMNS); - ECS_ERR_STR(ECS_COLUMN_ACCESS_VIOLATION); - ECS_ERR_STR(ECS_DESERIALIZE_FORMAT_ERROR); - ECS_ERR_STR(ECS_TYPE_CONSTRAINT_VIOLATION); - ECS_ERR_STR(ECS_COMPONENT_NOT_REGISTERED); - ECS_ERR_STR(ECS_INCONSISTENT_COMPONENT_ID); - ECS_ERR_STR(ECS_TYPE_INVALID_CASE); - ECS_ERR_STR(ECS_INCONSISTENT_NAME); - ECS_ERR_STR(ECS_INCONSISTENT_COMPONENT_ACTION); - ECS_ERR_STR(ECS_INVALID_OPERATION); - ECS_ERR_STR(ECS_INVALID_DELETE); - ECS_ERR_STR(ECS_CYCLE_DETECTED); - ECS_ERR_STR(ECS_LOCKED_STORAGE); - } + ptr = ecs_parse_whitespace(ptr + 1); + ptr = parse_set_expr(world, name, expr, (ptr - expr), ptr, + NULL, term_id, TOK_PAREN_CLOSE); + if (!ptr) { + return NULL; + } - return "unknown error code"; -} + /* If token is a self, super or sub token, this is a set + * expression */ + } else if (!ecs_os_strcmp(token, TOK_ALL) || + !ecs_os_strcmp(token, TOK_CASCADE) || + !ecs_os_strcmp(token, TOK_SELF) || + !ecs_os_strcmp(token, TOK_SUPERSET) || + !ecs_os_strcmp(token, TOK_SUBSET) || + !(ecs_os_strcmp(token, TOK_PARENT))) + { + ptr = parse_set_expr(world, name, expr, (ptr - expr), ptr, + token, term_id, TOK_PAREN_CLOSE); + if (!ptr) { + return NULL; + } -#else + /* Regular identifier */ + } else if (parse_identifier(token, term_id)) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid identifier '%s'", token); + return NULL; + } -/* Empty bodies for when logging is disabled */ + if (ptr[0] == TOK_AND) { + ptr = ecs_parse_whitespace(ptr + 1); -FLECS_API -void _ecs_log( - int32_t level, - const char *file, - int32_t line, - const char *fmt, - ...) -{ - (void)level; - (void)file; - (void)line; - (void)fmt; -} + term->role = ECS_PAIR; -FLECS_API -void _ecs_parser_error( - const char *name, - const char *expr, - int64_t column, - const char *fmt, - ...) -{ - (void)name; - (void)expr; - (void)column; - (void)fmt; + } else if (ptr[0] == TOK_PAREN_CLOSE) { + ptr = ecs_parse_whitespace(ptr + 1); + break; + + } else { + ecs_parser_error(name, expr, (ptr - expr), + "expected ',' or ')'"); + return NULL; + } + + } else { + ecs_parser_error(name, expr, (ptr - expr), + "expected identifier or set expression"); + return NULL; + } + + arg ++; + + } while (true); + + return ptr; } -FLECS_API -void _ecs_parser_errorv( +static +const char* parse_term( + const ecs_world_t *world, const char *name, - const char *expr, - int64_t column, - const char *fmt, - va_list args) + const char *expr, + ecs_term_t *term_out) { - (void)name; - (void)expr; - (void)column; - (void)fmt; - (void)args; -} + const char *ptr = expr; + char token[ECS_MAX_TOKEN_SIZE] = {0}; + ecs_term_t term = { .move = true /* parser never owns resources */ }; -FLECS_API -void _ecs_abort( - int32_t error_code, - const char *file, - int32_t line, - const char *fmt, - ...) -{ - (void)error_code; - (void)file; - (void)line; - (void)fmt; -} + ptr = ecs_parse_whitespace(ptr); -FLECS_API -void _ecs_assert( - bool condition, - int32_t error_code, - const char *condition_str, - const char *file, - int32_t line, - const char *fmt, - ...) -{ - (void)condition; - (void)error_code; - (void)condition_str; - (void)file; - (void)line; - (void)fmt; -} + /* Inout specifiers always come first */ + if (ptr[0] == TOK_BRACKET_OPEN) { + ptr = parse_annotation(name, expr, (ptr - expr), ptr + 1, &term.inout); + if (!ptr) { + goto error; + } + ptr = ecs_parse_whitespace(ptr); + } -#endif + if (valid_operator_char(ptr[0])) { + term.oper = parse_operator(ptr[0]); + ptr = ecs_parse_whitespace(ptr + 1); + } -#ifdef FLECS_PIPELINE + /* If next token is the start of an identifier, it could be either a type + * role, source or component identifier */ + if (valid_token_start_char(ptr[0])) { + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + goto error; + } -#ifndef FLECS_PIPELINE_PRIVATE_H -#define FLECS_PIPELINE_PRIVATE_H + /* Is token a type role? */ + if (ptr[0] == TOK_BITWISE_OR && ptr[1] != TOK_BITWISE_OR) { + ptr ++; + goto parse_role; + } -#ifndef FLECS_SYSTEM_PRIVATE_H -#define FLECS_SYSTEM_PRIVATE_H + /* Is token a predicate? */ + if (ptr[0] == TOK_PAREN_OPEN) { + goto parse_predicate; + } + /* Next token must be a predicate */ + goto parse_predicate; -typedef struct EcsSystem { - ecs_iter_action_t action; /* Callback to be invoked for matching it */ + /* If next token is a singleton, assign identifier to pred and subject */ + } else if (ptr[0] == TOK_SINGLETON) { + ptr ++; + if (valid_token_start_char(ptr[0])) { + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + goto error; + } - ecs_entity_t entity; /* Entity id of system, used for ordering */ - ecs_query_t *query; /* System query */ - ecs_system_status_action_t status_action; /* Status action */ - ecs_entity_t tick_source; /* Tick source associated with system */ - - int32_t invoke_count; /* Number of times system is invoked */ - FLECS_FLOAT time_spent; /* Time spent on running system */ - FLECS_FLOAT time_passed; /* Time passed since last invocation */ + goto parse_singleton; - ecs_entity_t self; /* Entity associated with system */ + } else { + ecs_parser_error(name, expr, (ptr - expr), + "expected identifier after singleton operator"); + goto error; + } - void *ctx; /* Userdata for system */ - void *status_ctx; /* User data for status action */ - void *binding_ctx; /* Optional language binding context */ + /* Pair with implicit subject */ + } else if (ptr[0] == TOK_PAREN_OPEN) { + goto parse_pair; - ecs_ctx_free_t ctx_free; - ecs_ctx_free_t status_ctx_free; - ecs_ctx_free_t binding_ctx_free; -} EcsSystem; + /* Nothing else expected here */ + } else { + ecs_parser_error(name, expr, (ptr - expr), + "unexpected character '%c'", ptr[0]); + goto error; + } -/* Invoked when system becomes active / inactive */ -void ecs_system_activate( - ecs_world_t *world, - ecs_entity_t system, - bool activate, - const EcsSystem *system_data); +parse_role: + term.role = parse_role(name, expr, (ptr - expr), token); + if (!term.role) { + goto error; + } -/* Internal function to run a system */ -ecs_entity_t ecs_run_intern( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t system, - EcsSystem *system_data, - int32_t stage_current, - int32_t stage_count, - FLECS_FLOAT delta_time, - int32_t offset, - int32_t limit, - void *param); + ptr = ecs_parse_whitespace(ptr); -#endif + /* If next token is the source token, this is an empty source */ + if (valid_token_start_char(ptr[0])) { + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + goto error; + } -/** Instruction data for pipeline. - * This type is the element type in the "ops" vector of a pipeline and contains - * information about the set of systems that need to be ran before a merge. */ -typedef struct ecs_pipeline_op_t { - int32_t count; /* Number of systems to run before merge */ -} ecs_pipeline_op_t; + /* If not, it's a predicate */ + goto parse_predicate; -typedef struct EcsPipelineQuery { - ecs_query_t *query; - ecs_query_t *build_query; - int32_t match_count; - ecs_vector_t *ops; -} EcsPipelineQuery; + } else if (ptr[0] == TOK_PAREN_OPEN) { + goto parse_pair; + } else { + ecs_parser_error(name, expr, (ptr - expr), + "expected identifier after role"); + goto error; + } -//////////////////////////////////////////////////////////////////////////////// -//// Pipeline API -//////////////////////////////////////////////////////////////////////////////// +parse_predicate: + if (parse_identifier(token, &term.pred)) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid identifier '%s'", token); + goto error; + } -/** Update a pipeline (internal function). - * Before running a pipeline, it must be updated. During this update phase - * all systems in the pipeline are collected, ordered and sync points are - * inserted where necessary. This operation may only be called when staging is - * disabled. - * - * Because multiple threads may run a pipeline, preparing the pipeline must - * happen synchronously, which is why this function is separate from - * ecs_pipeline_run. Not running the prepare step may cause systems to not get - * ran, or ran in the wrong order. - * - * If 0 is provided for the pipeline id, the default pipeline will be ran (this - * is either the builtin pipeline or the pipeline set with set_pipeline()). - * - * @param world The world. - * @param pipeline The pipeline to run. - * @return The number of elements in the pipeline. - */ -int32_t ecs_pipeline_update( - ecs_world_t *world, - ecs_entity_t pipeline, - bool start_of_frame); + /* Set expression */ + if (ptr[0] == TOK_COLON) { + ptr = ecs_parse_whitespace(ptr + 1); + ptr = parse_set_expr(world, name, expr, (ptr - expr), ptr, NULL, + &term.pred, TOK_COLON); + if (!ptr) { + goto error; + } -//////////////////////////////////////////////////////////////////////////////// -//// Worker API -//////////////////////////////////////////////////////////////////////////////// + ptr = ecs_parse_whitespace(ptr); -void ecs_worker_begin( - ecs_world_t *world); + if (ptr[0] == TOK_AND || !ptr[0]) { + goto parse_done; + } -bool ecs_worker_sync( - ecs_world_t *world); + if (ptr[0] != TOK_COLON) { + ecs_parser_error(name, expr, (ptr - expr), + "unexpected token '%c' after predicate set expression", ptr[0]); + goto error; + } -void ecs_worker_end( - ecs_world_t *world); + ptr = ecs_parse_whitespace(ptr + 1); + } else { + ptr = ecs_parse_whitespace(ptr); + } + + if (ptr[0] == TOK_PAREN_OPEN) { + ptr ++; + if (ptr[0] == TOK_PAREN_CLOSE) { + term.subj.set.mask = EcsNothing; + ptr ++; + ptr = ecs_parse_whitespace(ptr); + } else { + ptr = parse_arguments( + world, name, expr, (ptr - expr), ptr, token, &term); + } -void ecs_workers_progress( - ecs_world_t *world, - ecs_entity_t pipeline, - FLECS_FLOAT delta_time); + goto parse_done; + } -#endif + goto parse_done; -/* Worker thread */ -static -void* worker(void *arg) { - ecs_stage_t *stage = arg; - ecs_world_t *world = stage->world; +parse_pair: + ptr = ecs_parse_identifier(name, expr, ptr + 1, token); + if (!ptr) { + goto error; + } - /* Start worker thread, increase counter so main thread knows how many - * workers are ready */ - ecs_os_mutex_lock(world->sync_mutex); - world->workers_running ++; + if (ptr[0] == TOK_AND) { + ptr ++; + term.subj.entity = EcsThis; + goto parse_pair_predicate; + } else if (ptr[0] == TOK_PAREN_CLOSE) { + term.subj.entity = EcsThis; + goto parse_pair_predicate; + } else { + ecs_parser_error(name, expr, (ptr - expr), + "unexpected character '%c'", ptr[0]); + goto error; + } - if (!world->quit_workers) { - ecs_os_cond_wait(world->worker_cond, world->sync_mutex); +parse_pair_predicate: + if (parse_identifier(token, &term.pred)) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid identifier '%s'", token); + goto error; } - ecs_os_mutex_unlock(world->sync_mutex); + ptr = ecs_parse_whitespace(ptr); + if (valid_token_start_char(ptr[0])) { + ptr = ecs_parse_identifier(name, expr, ptr, token); + if (!ptr) { + goto error; + } - while (!world->quit_workers) { - ecs_entity_t old_scope = ecs_set_scope((ecs_world_t*)stage, 0); + if (ptr[0] == TOK_PAREN_CLOSE) { + ptr ++; + goto parse_pair_object; + } else { + ecs_parser_error(name, expr, (ptr - expr), + "unexpected character '%c'", ptr[0]); + goto error; + } + } else if (ptr[0] == TOK_PAREN_CLOSE) { + /* No object */ + ptr ++; + goto parse_done; + } else { + ecs_parser_error(name, expr, (ptr - expr), + "expected pair object or ')'"); + goto error; + } - ecs_pipeline_run( - (ecs_world_t*)stage, - world->pipeline, - world->stats.delta_time); +parse_pair_object: + if (parse_identifier(token, &term.obj)) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid identifier '%s'", token); + goto error; + } - ecs_set_scope((ecs_world_t*)stage, old_scope); + term.role = ECS_PAIR; + + ptr = ecs_parse_whitespace(ptr); + goto parse_done; + +parse_singleton: + if (parse_identifier(token, &term.pred)) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid identifier '%s'", token); + goto error; } - ecs_os_mutex_lock(world->sync_mutex); - world->workers_running --; - ecs_os_mutex_unlock(world->sync_mutex); + parse_identifier(token, &term.subj); + goto parse_done; +parse_done: + *term_out = term; + + return ptr; +error: + ecs_term_fini(&term); return NULL; } -/* Start threads */ static -void start_workers( - ecs_world_t *world, - int32_t threads) +bool is_valid_end_of_term( + const char *ptr) { - ecs_set_stages(world, threads); - - ecs_assert(ecs_get_stage_count(world) == threads, ECS_INTERNAL_ERROR, NULL); - - int32_t i; - for (i = 0; i < threads; i ++) { - ecs_stage_t *stage = (ecs_stage_t*)ecs_get_stage(world, i); - ecs_assert(stage != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_poly_assert(stage, ecs_stage_t); - - ecs_vector_get(world->worker_stages, ecs_stage_t, i); - stage->thread = ecs_os_thread_new(worker, stage); - ecs_assert(stage->thread != 0, ECS_THREAD_ERROR, NULL); + if ((ptr[0] == TOK_AND) || /* another term with And operator */ + (ptr[0] == TOK_OR[0]) || /* another term with Or operator */ + (ptr[0] == '\n') || /* newlines are valid */ + (ptr[0] == '\0') || /* end of string */ + (ptr[0] == '/') || /* comment (in plecs) */ + (ptr[0] == '{') || /* scope (in plecs) */ + (ptr[0] == '}') || + (ptr[0] == ':') || /* inheritance (in plecs) */ + (ptr[0] == '=')) /* assignment (in plecs) */ + { + return true; } + return false; } -/* Wait until all workers are running */ -static -void wait_for_workers( - ecs_world_t *world) +char* ecs_parse_term( + const ecs_world_t *world, + const char *name, + const char *expr, + const char *ptr, + ecs_term_t *term) { - int32_t stage_count = ecs_get_stage_count(world); - bool wait = true; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(term != NULL, ECS_INVALID_PARAMETER, NULL); - do { - ecs_os_mutex_lock(world->sync_mutex); - if (world->workers_running == stage_count) { - wait = false; + ecs_term_id_t *subj = &term->subj; + + bool prev_or = false; + if (ptr != expr) { + if (ptr[0]) { + if (ptr[0] == ',') { + ptr ++; + } else if (ptr[0] == '|') { + ptr += 2; + prev_or = true; + } else { + ecs_parser_error(name, expr, (ptr - expr), + "invalid preceding token"); + } } - ecs_os_mutex_unlock(world->sync_mutex); - } while (wait); -} + } + + ptr = ecs_parse_eol_and_whitespace(ptr); + if (!ptr[0]) { + *term = (ecs_term_t){0}; + return (char*)ptr; + } -/* Synchronize worker threads */ -static -void sync_worker( - ecs_world_t *world) -{ - int32_t stage_count = ecs_get_stage_count(world); + if (ptr == expr && !strcmp(expr, "0")) { + return (char*)&ptr[1]; + } - /* Signal that thread is waiting */ - ecs_os_mutex_lock(world->sync_mutex); - if (++ world->workers_waiting == stage_count) { - /* Only signal main thread when all threads are waiting */ - ecs_os_cond_signal(world->sync_cond); + int32_t prev_set = subj->set.mask; + + /* Parse next element */ + ptr = parse_term(world, name, ptr, term); + if (!ptr) { + goto error; } - /* Wait until main thread signals that thread can continue */ - ecs_os_cond_wait(world->worker_cond, world->sync_mutex); - ecs_os_mutex_unlock(world->sync_mutex); -} + /* Post-parse consistency checks */ -/* Wait until all threads are waiting on sync point */ -static -void wait_for_sync( - ecs_world_t *world) -{ - int32_t stage_count = ecs_get_stage_count(world); + /* If next token is OR, term is part of an OR expression */ + if (!ecs_os_strncmp(ptr, TOK_OR, 2) || prev_or) { + /* An OR operator must always follow an AND or another OR */ + if (term->oper != EcsAnd) { + ecs_parser_error(name, expr, (ptr - expr), + "cannot combine || with other operators"); + goto error; + } - ecs_os_mutex_lock(world->sync_mutex); - if (world->workers_waiting != stage_count) { - ecs_os_cond_wait(world->sync_cond, world->sync_mutex); + term->oper = EcsOr; } - - /* We should have been signalled unless all workers are waiting on sync */ - ecs_assert(world->workers_waiting == stage_count, - ECS_INTERNAL_ERROR, NULL); - - ecs_os_mutex_unlock(world->sync_mutex); -} -/* Signal workers that they can start/resume work */ -static -void signal_workers( - ecs_world_t *world) -{ - ecs_os_mutex_lock(world->sync_mutex); - ecs_os_cond_broadcast(world->worker_cond); - ecs_os_mutex_unlock(world->sync_mutex); -} + /* Term must either end in end of expression, AND or OR token */ + if (!is_valid_end_of_term(ptr)) { + ecs_parser_error(name, expr, (ptr - expr), + "expected end of expression or next term"); + goto error; + } -/** Stop worker threads */ -static -bool ecs_stop_threads( - ecs_world_t *world) -{ - bool threads_active = false; + /* If the term just contained a 0, the expression has nothing. Ensure + * that after the 0 nothing else follows */ + if (!ecs_os_strcmp(term->pred.name, "0")) { + if (ptr[0]) { + ecs_parser_error(name, expr, (ptr - expr), + "unexpected term after 0"); + goto error; + } - /* Test if threads are created. Cannot use workers_running, since this is - * a potential race if threads haven't spun up yet. */ - ecs_vector_each(world->worker_stages, ecs_stage_t, stage, { - if (stage->thread) { - threads_active = true; - break; + if (subj->set.mask != EcsDefaultSet || + (subj->entity && subj->entity != EcsThis) || + (subj->name && ecs_os_strcmp(subj->name, "This"))) + { + ecs_parser_error(name, expr, (ptr - expr), + "invalid combination of 0 with non-default subject"); + goto error; } - stage->thread = 0; - }); - /* If no threads are active, just return */ - if (!threads_active) { - return false; + subj->set.mask = EcsNothing; + ecs_os_free(term->pred.name); + term->pred.name = NULL; } - /* Make sure all threads are running, to ensure they catch the signal */ - wait_for_workers(world); - - /* Signal threads should quit */ - world->quit_workers = true; - signal_workers(world); - - /* Join all threads with main */ - ecs_vector_each(world->worker_stages, ecs_stage_t, stage, { - ecs_os_thread_join(stage->thread); - stage->thread = 0; - }); + /* Cannot combine EcsNothing with operators other than AND */ + if (term->oper != EcsAnd && subj->set.mask == EcsNothing) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid operator for empty source"); + goto error; + } - world->quit_workers = false; - ecs_assert(world->workers_running == 0, ECS_INTERNAL_ERROR, NULL); + /* Verify consistency of OR expression */ + if (prev_or && term->oper == EcsOr) { + /* Set expressions must be the same for all OR terms */ + if (subj->set.mask != prev_set) { + ecs_parser_error(name, expr, (ptr - expr), + "cannot combine different sources in OR expression"); + goto error; + } - /* Deinitialize stages */ - ecs_set_stages(world, 0); + term->oper = EcsOr; + } - return true; -} + /* Automatically assign This if entity is not assigned and the set is + * nothing */ + if (subj->set.mask != EcsNothing) { + if (!subj->name) { + if (!subj->entity) { + subj->entity = EcsThis; + } + } + } -/* -- Private functions -- */ + if (subj->name && !ecs_os_strcmp(subj->name, "0")) { + subj->entity = 0; + subj->set.mask = EcsNothing; + } -void ecs_worker_begin( - ecs_world_t *world) -{ - flecs_stage_from_world(&world); - int32_t stage_count = ecs_get_stage_count(world); - ecs_assert(stage_count != 0, ECS_INTERNAL_ERROR, NULL); - - if (stage_count == 1) { - ecs_staging_begin(world); + /* Process role */ + if (term->role == ECS_AND) { + term->oper = EcsAndFrom; + term->role = 0; + } else if (term->role == ECS_OR) { + term->oper = EcsOrFrom; + term->role = 0; + } else if (term->role == ECS_NOT) { + term->oper = EcsNotFrom; + term->role = 0; } -} -bool ecs_worker_sync( - ecs_world_t *world) -{ - flecs_stage_from_world(&world); + ptr = ecs_parse_whitespace(ptr); - int32_t build_count = world->stats.pipeline_build_count_total; - int32_t stage_count = ecs_get_stage_count(world); - ecs_assert(stage_count != 0, ECS_INTERNAL_ERROR, NULL); + return (char*)ptr; +error: + ecs_term_fini(term); + return NULL; +} - /* If there are no threads, merge in place */ - if (stage_count == 1) { - ecs_staging_end(world); - ecs_pipeline_update(world, world->pipeline, false); - ecs_staging_begin(world); +#endif +#ifndef FLECS_META_PRIVATE_H +#define FLECS_META_PRIVATE_H - /* Synchronize all workers. The last worker to reach the sync point will - * signal the main thread, which will perform the merge. */ - } else { - sync_worker(world); - } - return world->stats.pipeline_build_count_total != build_count; -} +#ifdef FLECS_META -void ecs_worker_end( - ecs_world_t *world) -{ - flecs_stage_from_world(&world); +void ecs_meta_type_serialized_init( + ecs_iter_t *it); - int32_t stage_count = ecs_get_stage_count(world); - ecs_assert(stage_count != 0, ECS_INTERNAL_ERROR, NULL); +void ecs_meta_dtor_serialized( + EcsMetaTypeSerialized *ptr); - /* If there are no threads, merge in place */ - if (stage_count == 1) { - ecs_staging_end(world); +#endif + +#endif - /* Synchronize all workers. The last worker to reach the sync point will - * signal the main thread, which will perform the merge. */ - } else { - sync_worker(world); - } -} +#ifdef FLECS_META -void ecs_workers_progress( +ecs_entity_t ecs_enum_init( ecs_world_t *world, - ecs_entity_t pipeline, - FLECS_FLOAT delta_time) + const ecs_enum_desc_t *desc) { - ecs_poly_assert(world, ecs_world_t); - int32_t stage_count = ecs_get_stage_count(world); - - ecs_time_t start = {0}; - if (world->measure_frame_time) { - ecs_time_measure(&start); + ecs_entity_t t = ecs_entity_init(world, &desc->entity); + if (!t) { + return 0; } - if (stage_count == 1) { - ecs_pipeline_update(world, pipeline, true); - ecs_entity_t old_scope = ecs_set_scope(world, 0); - ecs_world_t *stage = ecs_get_stage(world, 0); - - ecs_pipeline_run(stage, pipeline, delta_time); - ecs_set_scope(world, old_scope); - } else { - int32_t i, sync_count = ecs_pipeline_update(world, pipeline, true); - - /* Make sure workers are running and ready */ - wait_for_workers(world); - - /* Synchronize n times for each op in the pipeline */ - for (i = 0; i < sync_count; i ++) { - ecs_staging_begin(world); + ecs_add(world, t, EcsEnum); - /* Signal workers that they should start running systems */ - world->workers_waiting = 0; - signal_workers(world); + ecs_entity_t old_scope = ecs_set_scope(world, t); - /* Wait until all workers are waiting on sync point */ - wait_for_sync(world); + int i; + for (i = 0; i < ECS_MEMBER_DESC_CACHE_SIZE; i ++) { + const ecs_enum_constant_t *m_desc = &desc->constants[i]; + if (!m_desc->name) { + break; + } - /* Merge */ - ecs_staging_end(world); + ecs_entity_t c = ecs_entity_init(world, &(ecs_entity_desc_t) { + .name = m_desc->name + }); - int32_t update_count; - if ((update_count = ecs_pipeline_update(world, pipeline, false))) { - /* The number of operations in the pipeline could have changed - * as result of the merge */ - sync_count = update_count; - } + if (!m_desc->value) { + ecs_add_id(world, c, EcsConstant); + } else { + ecs_set_pair_object(world, c, EcsConstant, ecs_i32_t, + {m_desc->value}); } } - if (world->measure_frame_time) { - world->stats.system_time_total += (FLECS_FLOAT)ecs_time_measure(&start); - } -} + ecs_set_scope(world, old_scope); + if (i == 0) { + ecs_err("enum '%s' has no constants", ecs_get_name(world, t)); + ecs_delete(world, t); + return 0; + } -/* -- Public functions -- */ + return t; +} -void ecs_set_threads( +ecs_entity_t ecs_bitmask_init( ecs_world_t *world, - int32_t threads) + const ecs_bitmask_desc_t *desc) { - ecs_assert(threads <= 1 || ecs_os_has_threading(), ECS_MISSING_OS_API, NULL); + ecs_entity_t t = ecs_entity_init(world, &desc->entity); + if (!t) { + return 0; + } - int32_t stage_count = ecs_get_stage_count(world); + ecs_add(world, t, EcsBitmask); - if (stage_count != threads) { - /* Stop existing threads */ - if (stage_count > 1) { - if (ecs_stop_threads(world)) { - ecs_os_cond_free(world->worker_cond); - ecs_os_cond_free(world->sync_cond); - ecs_os_mutex_free(world->sync_mutex); - } + ecs_entity_t old_scope = ecs_set_scope(world, t); + + int i; + for (i = 0; i < ECS_MEMBER_DESC_CACHE_SIZE; i ++) { + const ecs_bitmask_constant_t *m_desc = &desc->constants[i]; + if (!m_desc->name) { + break; } - /* Start threads if number of threads > 1 */ - if (threads > 1) { - world->worker_cond = ecs_os_cond_new(); - world->sync_cond = ecs_os_cond_new(); - world->sync_mutex = ecs_os_mutex_new(); - start_workers(world, threads); + ecs_entity_t c = ecs_entity_init(world, &(ecs_entity_desc_t) { + .name = m_desc->name + }); + + if (!m_desc->value) { + ecs_add_id(world, c, EcsConstant); + } else { + ecs_set_pair_object(world, c, EcsConstant, ecs_u32_t, + {m_desc->value}); } } -} -#endif + ecs_set_scope(world, old_scope); -#ifdef FLECS_PIPELINE + if (i == 0) { + ecs_err("bitmask '%s' has no constants", ecs_get_name(world, t)); + ecs_delete(world, t); + return 0; + } + return t; +} -static ECS_CTOR(EcsPipelineQuery, ptr, { - memset(ptr, 0, _size); -}) +ecs_entity_t ecs_array_init( + ecs_world_t *world, + const ecs_array_desc_t *desc) +{ + ecs_entity_t t = ecs_entity_init(world, &desc->entity); + if (!t) { + return 0; + } -static ECS_DTOR(EcsPipelineQuery, ptr, { - ecs_vector_free(ptr->ops); -}) + ecs_set(world, t, EcsArray, { + .type = desc->type, + .count = desc->count + }); -static -int compare_entity( - ecs_entity_t e1, - const void *ptr1, - ecs_entity_t e2, - const void *ptr2) -{ - (void)ptr1; - (void)ptr2; - return (e1 > e2) - (e1 < e2); + return t; } -static -uint64_t group_by_phase( +ecs_entity_t ecs_vector_init( ecs_world_t *world, - ecs_type_t type, - ecs_entity_t pipeline, - void *ctx) + const ecs_vector_desc_t *desc) { - (void)ctx; - - const EcsType *pipeline_type = ecs_get(world, pipeline, EcsType); - ecs_assert(pipeline_type != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_entity_t t = ecs_entity_init(world, &desc->entity); + if (!t) { + return 0; + } - /* Find tag in system that belongs to pipeline */ - ecs_entity_t *sys_comps = ecs_vector_first(type, ecs_entity_t); - int32_t c, t, count = ecs_vector_count(type); + ecs_set(world, t, EcsVector, { + .type = desc->type + }); - ecs_entity_t *tags = ecs_vector_first(pipeline_type->normalized, ecs_entity_t); - int32_t tag_count = ecs_vector_count(pipeline_type->normalized); + return t; +} - ecs_entity_t result = 0; +ecs_entity_t ecs_struct_init( + ecs_world_t *world, + const ecs_struct_desc_t *desc) +{ + ecs_entity_t t = ecs_entity_init(world, &desc->entity); + if (!t) { + return 0; + } - for (c = 0; c < count; c ++) { - ecs_entity_t comp = sys_comps[c]; - for (t = 0; t < tag_count; t ++) { - if (comp == tags[t]) { - result = comp; - break; - } - } - if (result) { + ecs_entity_t old_scope = ecs_set_scope(world, t); + + int i; + for (i = 0; i < ECS_MEMBER_DESC_CACHE_SIZE; i ++) { + const ecs_member_t *m_desc = &desc->members[i]; + if (!m_desc->type) { break; } + + if (!m_desc->name) { + ecs_err("member %d of struct '%s' does not have a name", i, + ecs_get_name(world, t)); + ecs_delete(world, t); + return 0; + } + + ecs_entity_t m = ecs_entity_init(world, &(ecs_entity_desc_t) { + .name = m_desc->name + }); + + ecs_set(world, m, EcsMember, { + .type = m_desc->type, + .count = m_desc->count + }); } - ecs_assert(result != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(result < INT_MAX, ECS_INTERNAL_ERROR, NULL); + ecs_set_scope(world, old_scope); - return result; + if (i == 0) { + ecs_err("struct '%s' has no members", ecs_get_name(world, t)); + ecs_delete(world, t); + return 0; + } + + return t; } -typedef enum ComponentWriteState { - NotWritten = 0, - WriteToMain, - WriteToStage -} ComponentWriteState; +#endif -typedef struct write_state_t { - ecs_map_t *components; - bool wildcard; -} write_state_t; +#ifdef FLECS_META static -int32_t get_write_state( - ecs_map_t *write_state, - ecs_entity_t component) -{ - int32_t *ptr = ecs_map_get(write_state, int32_t, component); - if (ptr) { - return *ptr; - } else { - return 0; - } +ecs_vector_t* serialize_type( + ecs_world_t *world, + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops); + +static +ecs_meta_type_op_kind_t primitive_to_op_kind(ecs_primitive_kind_t kind) { + return EcsOpPrimitive + kind; } static -void set_write_state( - write_state_t *write_state, - ecs_entity_t component, - int32_t value) -{ - if (component == EcsWildcard) { - ecs_assert(value == WriteToStage, ECS_INTERNAL_ERROR, NULL); - write_state->wildcard = true; - } else { - ecs_map_set(write_state->components, component, &value); - } +ecs_size_t type_size(ecs_world_t *world, ecs_entity_t type) { + const EcsComponent *comp = ecs_get(world, type, EcsComponent); + ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); + return comp->size; } static -void reset_write_state( - write_state_t *write_state) -{ - ecs_map_clear(write_state->components); - write_state->wildcard = false; +ecs_meta_type_op_t* ops_add(ecs_vector_t **ops, ecs_meta_type_op_kind_t kind) { + ecs_meta_type_op_t *op = ecs_vector_add(ops, ecs_meta_type_op_t); + op->kind = kind; + op->offset = 0; + op->count = 1; + op->op_count = 1; + op->size = 0; + op->name = NULL; + op->members = NULL; + op->type = 0; + return op; } static -int32_t get_any_write_state( - write_state_t *write_state) +ecs_meta_type_op_t* ops_get(ecs_vector_t *ops, int32_t index) { + ecs_meta_type_op_t* op = ecs_vector_get(ops, ecs_meta_type_op_t, index); + ecs_assert(op != NULL, ECS_INTERNAL_ERROR, NULL); + return op; +} + +static +ecs_vector_t* serialize_primitive( + ecs_world_t *world, + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops) { - if (write_state->wildcard) { - return WriteToStage; - } + const EcsPrimitive *ptr = ecs_get(world, type, EcsPrimitive); + ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_map_iter_t it = ecs_map_iter(write_state->components); - int32_t *elem; - while ((elem = ecs_map_next(&it, int32_t, NULL))) { - if (*elem == WriteToStage) { - return WriteToStage; - } - } + ecs_meta_type_op_t *op = ops_add(&ops, primitive_to_op_kind(ptr->kind)); + op->offset = offset, + op->type = type; + op->size = type_size(world, type); - return 0; + return ops; } static -bool check_term_component( - ecs_term_t *term, - bool is_active, - ecs_entity_t component, - write_state_t *write_state) +ecs_vector_t* serialize_enum( + ecs_world_t *world, + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops) { - int32_t state = get_write_state(write_state->components, component); + (void)world; + + ecs_meta_type_op_t *op = ops_add(&ops, EcsOpEnum); + op->offset = offset, + op->type = type; + op->size = ECS_SIZEOF(ecs_i32_t); - ecs_term_id_t *subj = &term->subj; + return ops; +} - if ((subj->set.mask & EcsSelf) && subj->entity == EcsThis && term->oper != EcsNot) { - switch(term->inout) { - case EcsInOutDefault: - case EcsInOut: - case EcsIn: - if (state == WriteToStage || write_state->wildcard) { - return true; - } - // fall through - case EcsOut: - if (is_active && term->inout != EcsIn) { - set_write_state(write_state, component, WriteToMain); - } - }; - } else if (!subj->entity || term->oper == EcsNot) { - bool needs_merge = false; +static +ecs_vector_t* serialize_bitmask( + ecs_world_t *world, + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops) +{ + (void)world; + + ecs_meta_type_op_t *op = ops_add(&ops, EcsOpBitmask); + op->offset = offset, + op->type = type; + op->size = ECS_SIZEOF(ecs_u32_t); - switch(term->inout) { - case EcsInOutDefault: - case EcsIn: - case EcsInOut: - if (state == WriteToStage) { - needs_merge = true; - } - if (component == EcsWildcard) { - if (get_any_write_state(write_state) == WriteToStage) { - needs_merge = true; - } - } - break; - default: - break; - }; + return ops; +} - switch(term->inout) { - case EcsInOutDefault: - if (!(subj->set.mask & EcsSelf) || !subj->entity || - subj->entity != EcsThis) - { - break; - } - // fall through - case EcsInOut: - case EcsOut: - if (is_active) { - set_write_state(write_state, component, WriteToStage); - } - break; - default: - break; - }; +static +ecs_vector_t* serialize_array( + ecs_world_t *world, + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops) +{ + (void)world; - if (needs_merge) { - return true; - } - } + ecs_meta_type_op_t *op = ops_add(&ops, EcsOpArray); + op->offset = offset; + op->type = type; + op->size = type_size(world, type); - return false; + return ops; } static -bool check_term( - ecs_term_t *term, - bool is_active, - write_state_t *write_state) +ecs_vector_t* serialize_array_component( + ecs_world_t *world, + ecs_entity_t type) { - if (term->oper != EcsOr) { - return check_term_component( - term, is_active, term->id, write_state); - } + const EcsArray *ptr = ecs_get(world, type, EcsArray); + if (!ptr) { + return NULL; /* Should never happen, will trigger internal error */ + } - return false; + ecs_vector_t *ops = serialize_type(world, ptr->type, 0, NULL); + ecs_assert(ops != NULL, ECS_INTERNAL_ERROR, NULL); + + ecs_meta_type_op_t *first = ecs_vector_first(ops, ecs_meta_type_op_t); + first->count = ptr->count; + + return ops; } static -bool build_pipeline( +ecs_vector_t* serialize_vector( ecs_world_t *world, - ecs_entity_t pipeline, - EcsPipelineQuery *pq) + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops) { - (void)pipeline; + (void)world; - ecs_query_iter(world, pq->query); + ecs_meta_type_op_t *op = ops_add(&ops, EcsOpVector); + op->offset = offset; + op->type = type; + op->size = type_size(world, type); - if (pq->match_count == pq->query->match_count) { - /* No need to rebuild the pipeline */ - return false; - } + return ops; +} - world->stats.pipeline_build_count_total ++; +static +ecs_vector_t* serialize_struct( + ecs_world_t *world, + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops) +{ + const EcsStruct *ptr = ecs_get(world, type, EcsStruct); + ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - write_state_t ws = { - .components = ecs_map_new(int32_t, ECS_HI_COMPONENT_ID), - .wildcard = false - }; + int32_t cur, first = ecs_vector_count(ops); + ecs_meta_type_op_t *op = ops_add(&ops, EcsOpPush); + op->offset = offset; + op->type = type; + op->size = type_size(world, type); - ecs_pipeline_op_t *op = NULL; - ecs_vector_t *ops = NULL; - ecs_query_t *query = pq->build_query; + ecs_member_t *members = ecs_vector_first(ptr->members, ecs_member_t); + int32_t i, count = ecs_vector_count(ptr->members); - if (pq->ops) { - ecs_vector_free(pq->ops); + ecs_hashmap_t *member_index = NULL; + if (count) { + member_index = ecs_os_malloc_t(ecs_hashmap_t); + *member_index = flecs_string_hashmap_new(int32_t); + op->members = member_index; } - /* Iterate systems in pipeline, add ops for running / merging */ - ecs_iter_t it = ecs_query_iter(world, query); - while (ecs_query_next(&it)) { - EcsSystem *sys = ecs_term(&it, EcsSystem, 1); - - int i; - for (i = 0; i < it.count; i ++) { - ecs_query_t *q = sys[i].query; - if (!q) { - continue; - } - - bool needs_merge = false; - bool is_active = !ecs_has_id( - world, it.entities[i], EcsInactive); - - ecs_term_t *terms = q->filter.terms; - int32_t t, term_count = q->filter.term_count; - for (t = 0; t < term_count; t ++) { - needs_merge |= check_term(&terms[t], is_active, &ws); - } + for (i = 0; i < count; i ++) { + ecs_member_t *member = &members[i]; - if (needs_merge) { - /* After merge all components will be merged, so reset state */ - reset_write_state(&ws); - op = NULL; + cur = ecs_vector_count(ops); + ops = serialize_type(world, member->type, offset + member->offset, ops); - /* Re-evaluate columns to set write flags if system is active. - * If system is inactive, it can't write anything and so it - * should not insert unnecessary merges. */ - needs_merge = false; - if (is_active) { - for (t = 0; t < term_count; t ++) { - needs_merge |= check_term(&terms[t], true, &ws); - } - } + op = ops_get(ops, cur); + if (!op->type) { + op->type = member->type; + } - /* The component states were just reset, so if we conclude that - * another merge is needed something is wrong. */ - ecs_assert(needs_merge == false, ECS_INTERNAL_ERROR, NULL); - } + if (op->count <= 1) { + op->count = member->count; + } + + const char *member_name = member->name; + op->name = member_name; + op->op_count = ecs_vector_count(ops) - cur; - if (!op) { - op = ecs_vector_add(&ops, ecs_pipeline_op_t); - op->count = 0; - } + ecs_size_t len = ecs_os_strlen(member_name); - /* Don't increase count for inactive systems, as they are ignored by - * the query used to run the pipeline. */ - if (is_active) { - op->count ++; - } - } + ecs_hashed_string_t key = ecs_get_hashed_string(member_name, len, 0); + flecs_hashmap_result_t hmr = flecs_hashmap_ensure( + *member_index, &key, int32_t); + *((int32_t*)hmr.value) = cur - first - 1; } - ecs_map_free(ws.components); - - /* Force sort of query as this could increase the match_count */ - pq->match_count = pq->query->match_count; - pq->ops = ops; + ops_add(&ops, EcsOpPop); + ops_get(ops, first)->op_count = ecs_vector_count(ops) - first; - return true; + return ops; } static -int32_t iter_reset( +ecs_vector_t* serialize_type( ecs_world_t *world, - const EcsPipelineQuery *pq, - ecs_iter_t *iter_out, - ecs_pipeline_op_t **op_out, - ecs_entity_t move_to) + ecs_entity_t type, + ecs_size_t offset, + ecs_vector_t *ops) { - ecs_pipeline_op_t *op = ecs_vector_first(pq->ops, ecs_pipeline_op_t); - int32_t ran_since_merge = 0; + const EcsMetaType *ptr = ecs_get(world, type, EcsMetaType); + if (!ptr) { + char *path = ecs_get_fullpath(world, type); + ecs_err("missing EcsMetaType for type %s'", path); + ecs_os_free(path); + return NULL; + } - *iter_out = ecs_query_iter(world, pq->query); - while (ecs_query_next(iter_out)) { - int32_t i; - for(i = 0; i < iter_out->count; i ++) { - ecs_entity_t e = iter_out->entities[i]; + switch(ptr->kind) { + case EcsPrimitiveType: + ops = serialize_primitive(world, type, offset, ops); + break; - ran_since_merge ++; - if (ran_since_merge == op->count) { - ran_since_merge = 0; - op ++; - } + case EcsEnumType: + ops = serialize_enum(world, type, offset, ops); + break; - if (e == move_to) { - *op_out = op; - return i; - } - } - } + case EcsBitmaskType: + ops = serialize_bitmask(world, type, offset, ops); + break; - ecs_abort(ECS_UNSUPPORTED, NULL); + case EcsStructType: + ops = serialize_struct(world, type, offset, ops); + break; - return -1; + case EcsArrayType: + ops = serialize_array(world, type, offset, ops); + break; + + case EcsVectorType: + ops = serialize_vector(world, type, offset, ops); + break; + } + + return ops; } -int32_t ecs_pipeline_update( +static +ecs_vector_t* serialize_component( ecs_world_t *world, - ecs_entity_t pipeline, - bool start_of_frame) + ecs_entity_t type) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); - ecs_assert(pipeline != 0, ECS_INTERNAL_ERROR, NULL); - - /* If any entity mutations happened that could have affected query matching - * notify appropriate queries so caches are up to date. This includes the - * pipeline query. */ - if (start_of_frame) { - flecs_eval_component_monitors(world); + const EcsMetaType *ptr = ecs_get(world, type, EcsMetaType); + if (!ptr) { + char *path = ecs_get_fullpath(world, type); + ecs_err("missing EcsMetaType for type %s'", path); + ecs_os_free(path); + return NULL; } - bool added = false; - EcsPipelineQuery *pq = ecs_get_mut(world, pipeline, EcsPipelineQuery, &added); - ecs_assert(added == false, ECS_INTERNAL_ERROR, NULL); - ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(pq->query != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_vector_t *ops = NULL; - build_pipeline(world, pipeline, pq); + switch(ptr->kind) { + case EcsArrayType: + ops = serialize_array_component(world, type); + break; + default: + ops = serialize_type(world, type, 0, NULL); + break; + } - return ecs_vector_count(pq->ops); + return ops; } -void ecs_pipeline_run( - ecs_world_t *world, - ecs_entity_t pipeline, - FLECS_FLOAT delta_time) +void ecs_meta_type_serialized_init( + ecs_iter_t *it) { - ecs_assert(world != NULL, ECS_INVALID_OPERATION, NULL); - - if (!pipeline) { - pipeline = world->pipeline; - } + ecs_world_t *world = it->world; - /* If the world is passed to ecs_pipeline_run, the function will take care - * of staging, so the world should not be in staged mode when called. */ - if (ecs_poly_is(world, ecs_world_t)) { - ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + ecs_vector_t *ops = serialize_component(world, e); + ecs_assert(ops != NULL, ECS_INTERNAL_ERROR, NULL); - /* Forward to worker_progress. This function handles staging, threading - * and synchronization across workers. */ - ecs_workers_progress(world, pipeline, delta_time); - return; + EcsMetaTypeSerialized *ptr = ecs_get_mut( + world, e, EcsMetaTypeSerialized, NULL); + if (ptr->ops) { + ecs_meta_dtor_serialized(ptr); + } - /* If a stage is passed, the function could be ran from a worker thread. In - * that case the main thread should manage staging, and staging should be - * enabled. */ - } else { - ecs_poly_assert(world, ecs_stage_t); + ptr->ops = ops; } +} - ecs_stage_t *stage = flecs_stage_from_world(&world); - - const EcsPipelineQuery *pq = ecs_get(world, pipeline, EcsPipelineQuery); - ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(pq->query != NULL, ECS_INTERNAL_ERROR, NULL); +#endif - ecs_vector_t *ops = pq->ops; - ecs_pipeline_op_t *op = ecs_vector_first(ops, ecs_pipeline_op_t); - ecs_pipeline_op_t *op_last = ecs_vector_last(ops, ecs_pipeline_op_t); - int32_t ran_since_merge = 0; +#ifdef FLECS_META - int32_t stage_index = ecs_get_stage_id(stage->thread_ctx); - int32_t stage_count = ecs_get_stage_count(world); +/* EcsMetaTypeSerialized lifecycle */ - ecs_worker_begin(stage->thread_ctx); +void ecs_meta_dtor_serialized( + EcsMetaTypeSerialized *ptr) +{ + int32_t i, count = ecs_vector_count(ptr->ops); + ecs_meta_type_op_t *ops = ecs_vector_first(ptr->ops, ecs_meta_type_op_t); - ecs_iter_t it = ecs_query_iter(world, pq->query); - while (ecs_query_next(&it)) { - EcsSystem *sys = ecs_term(&it, EcsSystem, 1); - - int32_t i; - for(i = 0; i < it.count; i ++) { - ecs_entity_t e = it.entities[i]; + for (i = 0; i < count; i ++) { + ecs_meta_type_op_t *op = &ops[i]; + if (op->members) { + flecs_hashmap_free(*op->members); + ecs_os_free(op->members); + } + } - ecs_run_intern(world, stage, e, &sys[i], stage_index, stage_count, - delta_time, 0, 0, NULL); + ecs_vector_free(ptr->ops); +} - ran_since_merge ++; - world->stats.systems_ran_frame ++; +static ECS_COPY(EcsMetaTypeSerialized, dst, src, { + ecs_meta_dtor_serialized(dst); - if (op != op_last && ran_since_merge == op->count) { - ran_since_merge = 0; - op++; + dst->ops = ecs_vector_copy(src->ops, ecs_meta_type_op_t); - /* If the set of matched systems changed as a result of the - * merge, we have to reset the iterator and move it to our - * current position (system). If there are a lot of systems - * in the pipeline this can be an expensive operation, but - * should happen infrequently. */ - if (ecs_worker_sync(stage->thread_ctx)) { - i = iter_reset(world, pq, &it, &op, e); - op_last = ecs_vector_last(pq->ops, ecs_pipeline_op_t); - sys = ecs_term(&it, EcsSystem, 1); - } - } + int32_t o, count = ecs_vector_count(src->ops); + ecs_meta_type_op_t *ops = ecs_vector_first(src->ops, ecs_meta_type_op_t); + + for (o = 0; o < count; o ++) { + ecs_meta_type_op_t *op = &ops[o]; + if (op->members) { + op->members = ecs_os_memdup_t(op->members, ecs_hashmap_t); + *op->members = flecs_hashmap_copy(*op->members); } } +}) - ecs_worker_end(stage->thread_ctx); -} +static ECS_MOVE(EcsMetaTypeSerialized, dst, src, { + ecs_meta_dtor_serialized(dst); + dst->ops = src->ops; + src->ops = NULL; +}) + +static ECS_DTOR(EcsMetaTypeSerialized, ptr, { + ecs_meta_dtor_serialized(ptr); +}) -static -void add_pipeline_tags_to_sig( - ecs_world_t *world, - ecs_term_t *terms, - ecs_type_t type) -{ - (void)world; - - int32_t i, count = ecs_vector_count(type); - ecs_entity_t *entities = ecs_vector_first(type, ecs_entity_t); +/* EcsStruct lifecycle */ + +static void dtor_struct( + EcsStruct *ptr) +{ + ecs_member_t *members = ecs_vector_first(ptr->members, ecs_member_t); + int32_t i, count = ecs_vector_count(ptr->members); for (i = 0; i < count; i ++) { - terms[i] = (ecs_term_t){ - .inout = EcsIn, - .oper = EcsOr, - .pred.entity = entities[i], - .subj = { - .entity = EcsThis, - .set.mask = EcsSelf | EcsSuperSet - } - }; + ecs_os_free((char*)members[i].name); } + ecs_vector_free(ptr->members); } -static -ecs_query_t* build_pipeline_query( - ecs_world_t *world, - ecs_entity_t pipeline, - const char *name, - bool with_inactive) -{ - const EcsType *type_ptr = ecs_get(world, pipeline, EcsType); - ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); +static ECS_COPY(EcsStruct, dst, src, { + dtor_struct(dst); - int32_t type_count = ecs_vector_count(type_ptr->normalized); - int32_t term_count = 1; + dst->members = ecs_vector_copy(src->members, ecs_member_t); - if (with_inactive) { - term_count ++; + ecs_member_t *members = ecs_vector_first(dst->members, ecs_member_t); + int32_t m, count = ecs_vector_count(dst->members); + + for (m = 0; m < count; m ++) { + members[m].name = ecs_os_strdup(members[m].name); } +}) - ecs_term_t *terms = ecs_os_malloc( - (type_count + term_count) * ECS_SIZEOF(ecs_term_t)); +static ECS_MOVE(EcsStruct, dst, src, { + dtor_struct(dst); + dst->members = src->members; + src->members = NULL; +}) - terms[0] = (ecs_term_t){ - .inout = EcsIn, - .oper = EcsAnd, - .pred.entity = ecs_id(EcsSystem), - .subj = { - .entity = EcsThis, - .set.mask = EcsSelf | EcsSuperSet - } - }; +static ECS_DTOR(EcsStruct, ptr, { dtor_struct(ptr); }) - if (with_inactive) { - terms[1] = (ecs_term_t){ - .inout = EcsIn, - .oper = EcsNot, - .pred.entity = EcsInactive, - .subj = { - .entity = EcsThis, - .set.mask = EcsSelf | EcsSuperSet - } - }; + +/* EcsEnum lifecycle */ + +static void dtor_enum( + EcsEnum *ptr) +{ + ecs_map_iter_t it = ecs_map_iter(ptr->constants); + ecs_enum_constant_t *c; + while ((c = ecs_map_next(&it, ecs_enum_constant_t, NULL))) { + ecs_os_free((char*)c->name); } + ecs_map_free(ptr->constants); +} - add_pipeline_tags_to_sig(world, &terms[term_count], type_ptr->normalized); +static ECS_COPY(EcsEnum, dst, src, { + dtor_enum(dst); - ecs_query_t *result = ecs_query_init(world, &(ecs_query_desc_t){ - .filter = { - .name = name, - .terms_buffer = terms, - .terms_buffer_count = term_count + type_count - }, - .order_by = compare_entity, - .group_by = group_by_phase, - .group_by_id = pipeline - }); + dst->constants = ecs_map_copy(src->constants); + ecs_assert(ecs_map_count(dst->constants) == ecs_map_count(src->constants), + ECS_INTERNAL_ERROR, NULL); - ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_map_iter_t it = ecs_map_iter(dst->constants); + ecs_enum_constant_t *c; + while ((c = ecs_map_next(&it, ecs_enum_constant_t, NULL))) { + c->name = ecs_os_strdup(c->name); + } +}) - ecs_os_free(terms); +static ECS_MOVE(EcsEnum, dst, src, { + dtor_enum(dst); + dst->constants = src->constants; + src->constants = NULL; +}) - return result; -} +static ECS_DTOR(EcsEnum, ptr, { dtor_enum(ptr); }) -static -void EcsOnUpdatePipeline( - ecs_iter_t *it) -{ - ecs_world_t *world = it->world; - ecs_entity_t *entities = it->entities; - int32_t i; - for (i = it->count - 1; i >= 0; i --) { - ecs_entity_t pipeline = entities[i]; - - ecs_trace("#[green]pipeline#[reset] %s created", - ecs_get_name(world, pipeline)); - ecs_log_push(); +/* EcsBitmask lifecycle */ - /* Build signature for pipeline quey that matches EcsSystems, has the - * pipeline phases as OR columns, and ignores systems with EcsInactive. - * Note that EcsDisabled is automatically ignored - * by the regular query matching */ - ecs_query_t *query = build_pipeline_query( - world, pipeline, "BuiltinPipelineQuery", true); - ecs_assert(query != NULL, ECS_INTERNAL_ERROR, NULL); +static void dtor_bitmask( + EcsBitmask *ptr) +{ + ecs_map_iter_t it = ecs_map_iter(ptr->constants); + ecs_bitmask_constant_t *c; + while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, NULL))) { + ecs_os_free((char*)c->name); + } + ecs_map_free(ptr->constants); +} - /* Build signature for pipeline build query. The build query includes - * systems that are inactive, as an inactive system may become active as - * a result of another system, and as a result the correct merge - * operations need to be put in place. */ - ecs_query_t *build_query = build_pipeline_query( - world, pipeline, "BuiltinPipelineBuildQuery", false); - ecs_assert(build_query != NULL, ECS_INTERNAL_ERROR, NULL); +static ECS_COPY(EcsBitmask, dst, src, { + dtor_bitmask(dst); - bool added = false; - EcsPipelineQuery *pq = ecs_get_mut( - world, pipeline, EcsPipelineQuery, &added); - ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); + dst->constants = ecs_map_copy(src->constants); + ecs_assert(ecs_map_count(dst->constants) == ecs_map_count(src->constants), + ECS_INTERNAL_ERROR, NULL); - if (added) { - /* Should not modify pipeline after it has been used */ - ecs_assert(pq->ops == NULL, ECS_INVALID_OPERATION, NULL); + ecs_map_iter_t it = ecs_map_iter(dst->constants); + ecs_bitmask_constant_t *c; + while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, NULL))) { + c->name = ecs_os_strdup(c->name); + } +}) - if (pq->query) { - ecs_query_fini(pq->query); - } - if (pq->build_query) { - ecs_query_fini(pq->build_query); - } - } +static ECS_MOVE(EcsBitmask, dst, src, { + dtor_bitmask(dst); + dst->constants = src->constants; + src->constants = NULL; +}) - pq->query = query; - pq->build_query = build_query; - pq->match_count = -1; - pq->ops = NULL; +static ECS_DTOR(EcsBitmask, ptr, { dtor_bitmask(ptr); }) - ecs_log_pop(); - } -} -/* -- Public API -- */ +/* Type initialization */ -bool ecs_progress( +static +int init_type( ecs_world_t *world, - FLECS_FLOAT user_delta_time) + ecs_entity_t type, + ecs_type_kind_t kind) { - float delta_time = ecs_frame_begin(world, user_delta_time); + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(type != 0, ECS_INTERNAL_ERROR, NULL); - ecs_pipeline_run(world, 0, delta_time); + EcsMetaType *meta_type = ecs_get_mut(world, type, EcsMetaType, NULL); + if (meta_type->kind && meta_type->kind != kind) { + ecs_err("type '%s' reregistered with different kind", + ecs_get_name(world, type)); + return -1; + } - ecs_frame_end(world); + meta_type->kind = kind; + ecs_modified(world, type, EcsMetaType); - return !world->should_quit; + return 0; } -void ecs_set_time_scale( +static +int init_component( ecs_world_t *world, - FLECS_FLOAT scale) -{ - world->stats.time_scale = scale; -} - -void ecs_reset_clock( - ecs_world_t *world) + ecs_entity_t type, + ecs_size_t size, + ecs_size_t alignment) { - world->stats.world_time_total = 0; - world->stats.world_time_total_raw = 0; -} + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(type != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(alignment != 0, ECS_INTERNAL_ERROR, NULL); -void ecs_deactivate_systems( - ecs_world_t *world) -{ - ecs_assert(!world->is_readonly, ECS_INVALID_WHILE_ITERATING, NULL); + EcsComponent *component = ecs_get_mut(world, type, EcsComponent, NULL); + if (component->size && component->size != size) { + ecs_err("type '%s' reregistered with different size", + ecs_get_name(world, type)); + return -1; + } - ecs_entity_t pipeline = world->pipeline; - const EcsPipelineQuery *pq = ecs_get( world, pipeline, EcsPipelineQuery); - ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); + if (component->alignment && component->alignment != alignment) { + ecs_err("type '%s' reregistered with different alignment", + ecs_get_name(world, type)); + return -1; + } - /* Iterate over all systems, add EcsInvalid tag if queries aren't matched - * with any tables */ - ecs_iter_t it = ecs_query_iter(world, pq->build_query); + component->size = size; + component->alignment = alignment; + ecs_modified(world, type, EcsComponent); - /* Make sure that we defer adding the inactive tags until after iterating - * the query */ - flecs_defer_none(world, &world->stage); + return 0; +} - while( ecs_query_next(&it)) { - EcsSystem *sys = ecs_term(&it, EcsSystem, 1); +static +void set_struct_member( + ecs_member_t *member, + ecs_entity_t entity, + const char *name, + ecs_entity_t type, + int32_t count) +{ + member->member = entity; + member->type = type; + member->count = count; - int32_t i; - for (i = 0; i < it.count; i ++) { - ecs_query_t *query = sys[i].query; - if (query) { - if (!ecs_query_table_count(query)) { - ecs_add_id(world, it.entities[i], EcsInactive); - } - } - } + if (!count) { + member->count = 1; } - flecs_defer_flush(world, &world->stage); + ecs_os_strset((char**)&member->name, name); } -void ecs_set_pipeline( +static +int add_member_to_struct( ecs_world_t *world, - ecs_entity_t pipeline) + ecs_entity_t type, + ecs_entity_t member, + EcsMember *m) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert( ecs_get(world, pipeline, EcsPipelineQuery) != NULL, - ECS_INVALID_PARAMETER, NULL); + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(type != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(member != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(m != NULL, ECS_INTERNAL_ERROR, NULL); - world->pipeline = pipeline; -} + const char *name = ecs_get_name(world, member); + if (!name) { + char *path = ecs_get_fullpath(world, type); + ecs_err("member for struct '%s' does not have a name", path); + ecs_os_free(path); + return -1; + } -ecs_entity_t ecs_get_pipeline( - const ecs_world_t *world) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - world = ecs_get_world(world); - return world->pipeline; -} + if (!m->type) { + char *path = ecs_get_fullpath(world, member); + ecs_err("member '%s' does not have a type", path); + ecs_os_free(path); + return -1; + } -/* -- Module implementation -- */ + if (ecs_get_typeid(world, m->type) == 0) { + char *path = ecs_get_fullpath(world, member); + char *ent_path = ecs_get_fullpath(world, m->type); + ecs_err("member '%s.type' is '%s' which is not a type", path, ent_path); + ecs_os_free(path); + ecs_os_free(ent_path); + return -1; + } -static -void FlecsPipelineFini( - ecs_world_t *world, - void *ctx) -{ - (void)ctx; - if (ecs_get_stage_count(world)) { - ecs_set_threads(world, 0); + EcsStruct *s = ecs_get_mut(world, type, EcsStruct, NULL); + ecs_assert(s != NULL, ECS_INTERNAL_ERROR, NULL); + + /* First check if member is already added to struct */ + ecs_member_t *members = ecs_vector_first(s->members, ecs_member_t); + int32_t i, count = ecs_vector_count(s->members); + for (i = 0; i < count; i ++) { + if (members[i].member == member) { + set_struct_member(&members[i], member, name, m->type, m->count); + break; + } } -} -void FlecsPipelineImport( - ecs_world_t *world) -{ - ECS_MODULE(world, FlecsPipeline); + /* If member wasn't added yet, add a new element to vector */ + if (i == count) { + ecs_member_t *elem = ecs_vector_add(&s->members, ecs_member_t); + elem->name = NULL; + set_struct_member(elem, member, name, m->type, m->count); - ECS_IMPORT(world, FlecsSystem); + /* Reobtain members array in case it was reallocated */ + members = ecs_vector_first(s->members, ecs_member_t); + count ++; + } - ecs_set_name_prefix(world, "Ecs"); + /* Compute member offsets and size & alignment of struct */ + ecs_size_t size = 0; + ecs_size_t alignment = 0; - flecs_bootstrap_tag(world, EcsPipeline); - flecs_bootstrap_component(world, EcsPipelineQuery); + for (i = 0; i < count; i ++) { + ecs_member_t *elem = &members[i]; - /* Phases of the builtin pipeline are regular entities. Names are set so - * they can be resolved by type expressions. */ - flecs_bootstrap_tag(world, EcsPreFrame); - flecs_bootstrap_tag(world, EcsOnLoad); - flecs_bootstrap_tag(world, EcsPostLoad); - flecs_bootstrap_tag(world, EcsPreUpdate); - flecs_bootstrap_tag(world, EcsOnUpdate); - flecs_bootstrap_tag(world, EcsOnValidate); - flecs_bootstrap_tag(world, EcsPostUpdate); - flecs_bootstrap_tag(world, EcsPreStore); - flecs_bootstrap_tag(world, EcsOnStore); - flecs_bootstrap_tag(world, EcsPostFrame); + ecs_assert(elem->name != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(elem->type != 0, ECS_INTERNAL_ERROR, NULL); - /* Set ctor and dtor for PipelineQuery */ - ecs_set(world, ecs_id(EcsPipelineQuery), EcsComponentLifecycle, { - .ctor = ecs_ctor(EcsPipelineQuery), - .dtor = ecs_dtor(EcsPipelineQuery) - }); + /* Get component of member type to get its size & alignment */ + const EcsComponent *mbr_comp = ecs_get(world, elem->type, EcsComponent); + if (!mbr_comp) { + char *path = ecs_get_fullpath(world, member); + ecs_err("member '%s' is not a type", path); + ecs_os_free(path); + return -1; + } - /* When the Pipeline tag is added a pipeline will be created */ - ECS_OBSERVER(world, EcsOnUpdatePipeline, EcsOnSet, Pipeline, Type); + ecs_size_t member_size = mbr_comp->size; + ecs_size_t member_alignment = mbr_comp->alignment; - /* Create the builtin pipeline */ - world->pipeline = ecs_type_init(world, &(ecs_type_desc_t){ - .entity = { - .name = "BuiltinPipeline", - .add = {EcsPipeline} - }, - .ids = { - EcsPreFrame, EcsOnLoad, EcsPostLoad, EcsPreUpdate, EcsOnUpdate, - EcsOnValidate, EcsPostUpdate, EcsPreStore, EcsOnStore, EcsPostFrame - } - }); + if (!member_size || !member_alignment) { + char *path = ecs_get_fullpath(world, member); + ecs_err("member '%s' has 0 size/alignment"); + ecs_os_free(path); + return -1; + } - /* Cleanup thread administration when world is destroyed */ - ecs_atfini(world, FlecsPipelineFini, NULL); -} + member_size *= elem->count; + size = ECS_ALIGN(size, member_alignment); + elem->size = member_size; + elem->offset = size; -#endif + size += member_size; -#ifdef FLECS_TIMER + if (member_alignment > alignment) { + alignment = member_alignment; + } + } + if (size == 0) { + ecs_err("struct '%s' has 0 size", ecs_get_name(world, type)); + return -1; + } -static -void AddTickSource(ecs_iter_t *it) { - int32_t i; - for (i = 0; i < it->count; i ++) { - ecs_set(it->world, it->entities[i], EcsTickSource, {0}); + if (alignment == 0) { + ecs_err("struct '%s' has 0 alignment", ecs_get_name(world, type)); + return -1; } -} -static -void ProgressTimers(ecs_iter_t *it) { - EcsTimer *timer = ecs_term(it, EcsTimer, 1); - EcsTickSource *tick_source = ecs_term(it, EcsTickSource, 2); + /* Align struct size to struct alignment */ + size = ECS_ALIGN(size, alignment); - ecs_assert(timer != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_modified(world, type, EcsStruct); - int i; - for (i = 0; i < it->count; i ++) { - tick_source[i].tick = false; + /* Overwrite component size & alignment */ + if (type != ecs_id(EcsComponent)) { + EcsComponent *comp = ecs_get_mut(world, type, EcsComponent, NULL); + comp->size = size; + comp->alignment = alignment; + ecs_modified(world, type, EcsComponent); + } - if (!timer[i].active) { - continue; - } + /* Do this last as it triggers the update of EcsMetaTypeSerialized */ + if (init_type(world, type, EcsStructType)) { + return -1; + } - const ecs_world_info_t *info = ecs_get_world_info(it->world); - FLECS_FLOAT time_elapsed = timer[i].time + info->delta_time_raw; - FLECS_FLOAT timeout = timer[i].timeout; - - if (time_elapsed >= timeout) { - FLECS_FLOAT t = time_elapsed - timeout; - if (t > timeout) { - t = 0; - } + /* If current struct is also a member, assign to itself */ + if (ecs_has(world, type, EcsMember)) { + EcsMember *type_mbr = ecs_get_mut(world, type, EcsMember, NULL); + ecs_assert(type_mbr != NULL, ECS_INTERNAL_ERROR, NULL); - timer[i].time = t; /* Initialize with remainder */ - tick_source[i].tick = true; - tick_source[i].time_elapsed = time_elapsed; + type_mbr->type = type; + type_mbr->count = 1; - if (timer[i].single_shot) { - timer[i].active = false; - } - } else { - timer[i].time = time_elapsed; - } + ecs_modified(world, type, EcsMember); } + + return 0; } static -void ProgressRateFilters(ecs_iter_t *it) { - EcsRateFilter *filter = ecs_term(it, EcsRateFilter, 1); - EcsTickSource *tick_dst = ecs_term(it, EcsTickSource, 2); +int add_constant_to_enum( + ecs_world_t *world, + ecs_entity_t type, + ecs_entity_t e, + ecs_id_t constant_id) +{ + EcsEnum *ptr = ecs_get_mut(world, type, EcsEnum, NULL); + + /* Remove constant from map if it was already added */ + ecs_map_iter_t it = ecs_map_iter(ptr->constants); + ecs_enum_constant_t *c; + ecs_map_key_t key; + while ((c = ecs_map_next(&it, ecs_enum_constant_t, &key))) { + if (c->constant == e) { + ecs_os_free((char*)c->name); + ecs_map_remove(ptr->constants, key); + } + } - int i; - for (i = 0; i < it->count; i ++) { - ecs_entity_t src = filter[i].src; - bool inc = false; + /* Check if constant sets explicit value */ + int32_t value = 0; + bool value_set = false; + if (ecs_id_is_pair(constant_id)) { + if (ecs_pair_object(world, constant_id) != ecs_id(ecs_i32_t)) { + char *path = ecs_get_fullpath(world, e); + ecs_err("expected i32 type for enum constant '%s'", path); + ecs_os_free(path); + return -1; + } - filter[i].time_elapsed += it->delta_time; + const int32_t *value_ptr = ecs_get_pair_object( + world, e, EcsConstant, ecs_i32_t); + ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + value = *value_ptr; + value_set = true; + } - if (src) { - const EcsTickSource *tick_src = ecs_get(it->world, src, EcsTickSource); - if (tick_src) { - inc = tick_src->tick; - } else { - inc = true; + /* Make sure constant value doesn't conflict if set / find the next value */ + it = ecs_map_iter(ptr->constants); + while ((c = ecs_map_next(&it, ecs_enum_constant_t, &key))) { + if (value_set) { + if (c->value == value) { + char *path = ecs_get_fullpath(world, e); + ecs_err("conflicting constant value for '%s' (other is '%s')", + path, c->name); + ecs_os_free(path); + return -1; } } else { - inc = true; + if (c->value >= value) { + value = c->value + 1; + } } + } - if (inc) { - filter[i].tick_count ++; - bool triggered = !(filter[i].tick_count % filter[i].rate); - tick_dst[i].tick = triggered; - tick_dst[i].time_elapsed = filter[i].time_elapsed; + if (!ptr->constants) { + ptr->constants = ecs_map_new(ecs_enum_constant_t, 1); + } - if (triggered) { - filter[i].time_elapsed = 0; - } - } else { - tick_dst[i].tick = false; - } - } -} + c = ecs_map_ensure(ptr->constants, ecs_enum_constant_t, value); + c->name = ecs_os_strdup(ecs_get_name(world, e)); + c->value = value; + c->constant = e; -static -void ProgressTickSource(ecs_iter_t *it) { - EcsTickSource *tick_src = ecs_term(it, EcsTickSource, 1); + ecs_i32_t *cptr = ecs_get_mut_pair_object( + world, e, EcsConstant, ecs_i32_t, NULL); + ecs_assert(cptr != NULL, ECS_INTERNAL_ERROR, NULL); + cptr[0] = value; - /* If tick source has no filters, tick unconditionally */ - int i; - for (i = 0; i < it->count; i ++) { - tick_src[i].tick = true; - tick_src[i].time_elapsed = it->delta_time; - } + return 0; } -ecs_entity_t ecs_set_timeout( - ecs_world_t *world, - ecs_entity_t timer, - FLECS_FLOAT timeout) +static +int add_constant_to_bitmask( + ecs_world_t *world, + ecs_entity_t type, + ecs_entity_t e, + ecs_id_t constant_id) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - - timer = ecs_set(world, timer, EcsTimer, { - .timeout = timeout, - .single_shot = true, - .active = true - }); - - EcsSystem *system_data = ecs_get_mut(world, timer, EcsSystem, NULL); - if (system_data) { - system_data->tick_source = timer; + EcsBitmask *ptr = ecs_get_mut(world, type, EcsBitmask, NULL); + + /* Remove constant from map if it was already added */ + ecs_map_iter_t it = ecs_map_iter(ptr->constants); + ecs_bitmask_constant_t *c; + ecs_map_key_t key; + while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { + if (c->constant == e) { + ecs_os_free((char*)c->name); + ecs_map_remove(ptr->constants, key); + } } - return timer; -} - -FLECS_FLOAT ecs_get_timeout( - const ecs_world_t *world, - ecs_entity_t timer) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(timer != 0, ECS_INVALID_PARAMETER, NULL); + /* Check if constant sets explicit value */ + uint32_t value = 1; + if (ecs_id_is_pair(constant_id)) { + if (ecs_pair_object(world, constant_id) != ecs_id(ecs_u32_t)) { + char *path = ecs_get_fullpath(world, e); + ecs_err("expected u32 type for bitmask constant '%s'", path); + ecs_os_free(path); + return -1; + } - const EcsTimer *value = ecs_get(world, timer, EcsTimer); - if (value) { - return value->timeout; + const uint32_t *value_ptr = ecs_get_pair_object( + world, e, EcsConstant, ecs_u32_t); + ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + value = *value_ptr; } else { - return 0; + value = 1u << (ecs_u32_t)ecs_map_count(ptr->constants); } -} - -ecs_entity_t ecs_set_interval( - ecs_world_t *world, - ecs_entity_t timer, - FLECS_FLOAT interval) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - - timer = ecs_set(world, timer, EcsTimer, { - .timeout = interval, - .active = true - }); - EcsSystem *system_data = ecs_get_mut(world, timer, EcsSystem, NULL); - if (system_data) { - system_data->tick_source = timer; - } - - return timer; -} - -FLECS_FLOAT ecs_get_interval( - const ecs_world_t *world, - ecs_entity_t timer) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - - if (!timer) { - return 0; + /* Make sure constant value doesn't conflict */ + it = ecs_map_iter(ptr->constants); + while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { + if (c->value == value) { + char *path = ecs_get_fullpath(world, e); + ecs_err("conflicting constant value for '%s' (other is '%s')", + path, c->name); + ecs_os_free(path); + return -1; + } } - const EcsTimer *value = ecs_get(world, timer, EcsTimer); - if (value) { - return value->timeout; - } else { - return 0; + if (!ptr->constants) { + ptr->constants = ecs_map_new(ecs_bitmask_constant_t, 1); } -} - -void ecs_start_timer( - ecs_world_t *world, - ecs_entity_t timer) -{ - EcsTimer *ptr = ecs_get_mut(world, timer, EcsTimer, NULL); - ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); - ptr->active = true; - ptr->time = 0; -} + c = ecs_map_ensure(ptr->constants, ecs_bitmask_constant_t, value); + c->name = ecs_os_strdup(ecs_get_name(world, e)); + c->value = value; + c->constant = e; -void ecs_stop_timer( - ecs_world_t *world, - ecs_entity_t timer) -{ - EcsTimer *ptr = ecs_get_mut(world, timer, EcsTimer, NULL); - ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_u32_t *cptr = ecs_get_mut_pair_object( + world, e, EcsConstant, ecs_u32_t, NULL); + ecs_assert(cptr != NULL, ECS_INTERNAL_ERROR, NULL); + cptr[0] = value; - ptr->active = false; + return 0; } -ecs_entity_t ecs_set_rate( - ecs_world_t *world, - ecs_entity_t filter, - int32_t rate, - ecs_entity_t source) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - - filter = ecs_set(world, filter, EcsRateFilter, { - .rate = rate, - .src = source - }); - - EcsSystem *system_data = ecs_get_mut(world, filter, EcsSystem, NULL); - if (system_data) { - system_data->tick_source = filter; - } +static +void set_primitive(ecs_iter_t *it) { + ecs_world_t *world = it->world; + EcsPrimitive *type = ecs_term(it, EcsPrimitive, 1); - return filter; + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + switch(type->kind) { + case EcsBool: + init_component(world, e, + ECS_SIZEOF(bool), ECS_ALIGNOF(bool)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsChar: + init_component(world, e, + ECS_SIZEOF(char), ECS_ALIGNOF(char)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsByte: + init_component(world, e, + ECS_SIZEOF(bool), ECS_ALIGNOF(bool)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsU8: + init_component(world, e, + ECS_SIZEOF(uint8_t), ECS_ALIGNOF(uint8_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsU16: + init_component(world, e, + ECS_SIZEOF(uint16_t), ECS_ALIGNOF(uint16_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsU32: + init_component(world, e, + ECS_SIZEOF(uint32_t), ECS_ALIGNOF(uint32_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsU64: + init_component(world, e, + ECS_SIZEOF(uint64_t), ECS_ALIGNOF(uint64_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsI8: + init_component(world, e, + ECS_SIZEOF(int8_t), ECS_ALIGNOF(int8_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsI16: + init_component(world, e, + ECS_SIZEOF(int16_t), ECS_ALIGNOF(int16_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsI32: + init_component(world, e, + ECS_SIZEOF(int32_t), ECS_ALIGNOF(int32_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsI64: + init_component(world, e, + ECS_SIZEOF(int64_t), ECS_ALIGNOF(int64_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsF32: + init_component(world, e, + ECS_SIZEOF(float), ECS_ALIGNOF(float)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsF64: + init_component(world, e, + ECS_SIZEOF(double), ECS_ALIGNOF(double)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsUPtr: + init_component(world, e, + ECS_SIZEOF(uintptr_t), ECS_ALIGNOF(uintptr_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsIPtr: + init_component(world, e, + ECS_SIZEOF(intptr_t), ECS_ALIGNOF(intptr_t)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsString: + init_component(world, e, + ECS_SIZEOF(char*), ECS_ALIGNOF(char*)); + init_type(world, e, EcsPrimitiveType); + break; + case EcsEntity: + init_component(world, e, + ECS_SIZEOF(ecs_entity_t), ECS_ALIGNOF(ecs_entity_t)); + init_type(world, e, EcsPrimitiveType); + break; + } + } } -void ecs_set_tick_source( - ecs_world_t *world, - ecs_entity_t system, - ecs_entity_t tick_source) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(system != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(tick_source != 0, ECS_INVALID_PARAMETER, NULL); +static +void set_member(ecs_iter_t *it) { + ecs_world_t *world = it->world; + EcsMember *member = ecs_term(it, EcsMember, 1); - EcsSystem *system_data = ecs_get_mut(world, system, EcsSystem, NULL); - ecs_assert(system_data != NULL, ECS_INVALID_PARAMETER, NULL); + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + ecs_entity_t parent = ecs_get_object(world, e, EcsChildOf, 0); + if (!parent) { + ecs_err("missing parent for member '%s'", ecs_get_name(world, e)); + continue; + } - system_data->tick_source = tick_source; + add_member_to_struct(world, parent, e, member); + } } -void FlecsTimerImport( - ecs_world_t *world) -{ - ECS_MODULE(world, FlecsTimer); - - ECS_IMPORT(world, FlecsPipeline); - - ecs_set_name_prefix(world, "Ecs"); +static +void add_enum(ecs_iter_t *it) { + ecs_world_t *world = it->world; - flecs_bootstrap_component(world, EcsTimer); - flecs_bootstrap_component(world, EcsRateFilter); + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + + if (init_component( + world, e, ECS_SIZEOF(ecs_i32_t), ECS_ALIGNOF(ecs_i32_t))) + { + continue; + } - /* Add EcsTickSource to timers and rate filters */ - ECS_SYSTEM(world, AddTickSource, EcsPreFrame, [in] Timer || RateFilter, [out] !flecs.system.TickSource); + if (init_type(world, e, EcsEnumType)) { + continue; + } + } +} - /* Timer handling */ - ECS_SYSTEM(world, ProgressTimers, EcsPreFrame, Timer, flecs.system.TickSource); +static +void add_bitmask(ecs_iter_t *it) { + ecs_world_t *world = it->world; - /* Rate filter handling */ - ECS_SYSTEM(world, ProgressRateFilters, EcsPreFrame, [in] RateFilter, [out] flecs.system.TickSource); + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + + if (init_component( + world, e, ECS_SIZEOF(ecs_u32_t), ECS_ALIGNOF(ecs_u32_t))) + { + continue; + } - /* TickSource without a timer or rate filter just increases each frame */ - ECS_SYSTEM(world, ProgressTickSource, EcsPreFrame, [out] flecs.system.TickSource, !RateFilter, !Timer); + if (init_type(world, e, EcsBitmaskType)) { + continue; + } + } } -#endif - -#ifdef FLECS_DEPRECATED +static +void add_constant(ecs_iter_t *it) { + ecs_world_t *world = it->world; + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + ecs_entity_t parent = ecs_get_object(world, e, EcsChildOf, 0); + if (!parent) { + ecs_err("missing parent for constant '%s'", ecs_get_name(world, e)); + continue; + } -#endif + if (ecs_has(world, parent, EcsEnum)) { + add_constant_to_enum(world, parent, e, it->event_id); + } else if (ecs_has(world, parent, EcsBitmask)) { + add_constant_to_bitmask(world, parent, e, it->event_id); + } + } +} -#ifdef FLECS_PLECS +static +void set_array(ecs_iter_t *it) { + ecs_world_t *world = it->world; + EcsArray *array = ecs_term(it, EcsArray, 1); + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + ecs_entity_t elem_type = array[i].type; + int32_t elem_count = array[i].count; -#define TOK_NEWLINE '\n' -#define TOK_WITH "with" -#define TOK_USING "using" + if (!elem_type) { + ecs_err("array '%s' has no element type", ecs_get_name(world, e)); + continue; + } -#define STACK_MAX_SIZE (64) + if (!elem_count) { + ecs_err("array '%s' has size 0", ecs_get_name(world, e)); + continue; + } -typedef struct { - const char *name; - const char *code; + const EcsComponent *elem_ptr = ecs_get(world, elem_type, EcsComponent); + if (init_component( + world, e, elem_ptr->size * elem_count, elem_ptr->alignment)) + { + continue; + } - ecs_entity_t last_predicate; - ecs_entity_t last_subject; - ecs_entity_t last_object; + if (init_type(world, e, EcsArrayType)) { + continue; + } + } +} - ecs_id_t last_assign_id; - ecs_entity_t assign_to; +static +void set_vector(ecs_iter_t *it) { + ecs_world_t *world = it->world; + EcsVector *array = ecs_term(it, EcsVector, 1); - ecs_entity_t scope[STACK_MAX_SIZE]; - ecs_entity_t default_scope_type[STACK_MAX_SIZE]; - ecs_entity_t with[STACK_MAX_SIZE]; - ecs_entity_t using[STACK_MAX_SIZE]; - int32_t with_frames[STACK_MAX_SIZE]; - int32_t using_frames[STACK_MAX_SIZE]; - int32_t sp; - int32_t with_frame; - int32_t using_frame; + int i, count = it->count; + for (i = 0; i < count; i ++) { + ecs_entity_t e = it->entities[i]; + ecs_entity_t elem_type = array[i].type; - char *comment; + if (!elem_type) { + ecs_err("vector '%s' has no element type", ecs_get_name(world, e)); + continue; + } - bool with_stmt; - bool using_stmt; - bool assign_stmt; - bool isa_stmt; + if (init_component(world, e, + ECS_SIZEOF(ecs_vector_t*), ECS_ALIGNOF(ecs_vector_t*))) + { + continue; + } - int32_t errors; -} plecs_state_t; + if (init_type(world, e, EcsVectorType)) { + continue; + } + } +} static -ecs_entity_t plecs_lookup( - const ecs_world_t *world, - const char *path, - plecs_state_t *state, - bool is_subject) -{ - ecs_entity_t e = 0; +void ecs_meta_type_init_default_ctor(ecs_iter_t *it) { + ecs_world_t *world = it->world; - if (!is_subject) { - int using_scope = state->using_frame - 1; - for (; using_scope >= 0; using_scope--) { - e = ecs_lookup_path_w_sep( - world, state->using[using_scope], path, NULL, NULL, false); - if (e) { - break; - } - } - } + int i; + for (i = 0; i < it->count; i ++) { + ecs_entity_t type = it->entities[i]; - if (!e) { - e = ecs_lookup_path_w_sep(world, 0, path, NULL, NULL, !is_subject); + /* If component has no component actions (which is typical if a type is + * created with reflection data) make sure its values are always + * initialized with zero. This prevents the injection of invalid data + * through generic APIs after adding a component without setting it. */ + if (!ecs_component_has_actions(world, type)) { + ecs_set_component_actions_w_id(world, type, + &(EcsComponentLifecycle){ + .ctor = ecs_default_ctor + }); + } } - - return e; } -/* Lookup action used for deserializing entity refs in component values */ -#ifdef FLECS_EXPR static -ecs_entity_t plecs_lookup_action( - const ecs_world_t *world, - const char *path, +void member_on_set( + ecs_world_t *world, + ecs_entity_t component, + const ecs_entity_t *entity_ptr, + void *ptr, + size_t size, + int32_t count, void *ctx) { - return plecs_lookup(world, path, ctx, false); + (void)world; + (void)component; + (void)entity_ptr; + (void)size; + (void)count; + (void)ctx; + + EcsMember *mbr = ptr; + if (!mbr->count) { + mbr->count = 1; + } } -#endif -static -void clear_comment( - const char *expr, - const char *ptr, - plecs_state_t *state) +void FlecsMetaImport( + ecs_world_t *world) { - if (state->comment) { - ecs_parser_error(state->name, expr, ptr - expr, "unused doc comment"); - ecs_os_free(state->comment); - state->comment = NULL; + ECS_MODULE(world, FlecsMeta); - state->errors ++; /* Non-fatal error */ - } -} + ecs_set_name_prefix(world, "Ecs"); -static -const char* parse_fluff( - const char *expr, - const char *ptr, - plecs_state_t *state) -{ - char *comment; - const char *next = ecs_parse_fluff(ptr, &comment); + flecs_bootstrap_component(world, EcsMetaType); + flecs_bootstrap_component(world, EcsMetaTypeSerialized); + flecs_bootstrap_component(world, EcsPrimitive); + flecs_bootstrap_component(world, EcsEnum); + flecs_bootstrap_component(world, EcsBitmask); + flecs_bootstrap_component(world, EcsMember); + flecs_bootstrap_component(world, EcsStruct); + flecs_bootstrap_component(world, EcsArray); + flecs_bootstrap_component(world, EcsVector); - if (comment && comment[0] == '/') { - comment = (char*)ecs_parse_fluff(comment + 1, NULL); - int32_t len = (ecs_size_t)(next - comment); - int32_t newline_count = 0; + flecs_bootstrap_tag(world, EcsConstant); - /* Trim trailing whitespaces */ - while (len >= 0 && (isspace(comment[len - 1]))) { - if (comment[len - 1] == '\n') { - newline_count ++; - if (newline_count > 1) { - /* If newline separates comment from statement, discard */ - len = -1; - break; - } - } - len --; - } + ecs_set_component_actions(world, EcsMetaType, { .ctor = ecs_default_ctor }); - if (len > 0) { - clear_comment(expr, ptr, state); - state->comment = ecs_os_calloc_n(char, len + 1); - ecs_os_strncpy(state->comment, comment, len); - } else { - ecs_parser_error(state->name, expr, ptr - expr, - "unused doc comment"); - state->errors ++; - } - } else { - if (ptr != next && state->comment) { - clear_comment(expr, ptr, state); - } - } + ecs_set_component_actions(world, EcsMetaTypeSerialized, { + .ctor = ecs_default_ctor, + .move = ecs_move(EcsMetaTypeSerialized), + .copy = ecs_copy(EcsMetaTypeSerialized), + .dtor = ecs_dtor(EcsMetaTypeSerialized) + }); - return next; -} + ecs_set_component_actions(world, EcsStruct, { + .ctor = ecs_default_ctor, + .move = ecs_move(EcsStruct), + .copy = ecs_copy(EcsStruct), + .dtor = ecs_dtor(EcsStruct) + }); -static -ecs_entity_t ensure_entity( - ecs_world_t *world, - plecs_state_t *state, - const char *path, - bool is_subject) -{ - if (!path) { - return 0; - } + ecs_set_component_actions(world, EcsMember, { + .ctor = ecs_default_ctor, + .on_set = member_on_set + }); - ecs_entity_t e = plecs_lookup(world, path, state, is_subject); - if (!e) { - if (!is_subject) { - /* If this is not a subject create an existing empty id, which - * ensures that scope & with are not applied */ - e = ecs_new_id(world); - } + ecs_set_component_actions(world, EcsEnum, { + .ctor = ecs_default_ctor, + .move = ecs_move(EcsEnum), + .copy = ecs_copy(EcsEnum), + .dtor = ecs_dtor(EcsEnum) + }); - e = ecs_add_path(world, e, 0, path); - ecs_assert(e != 0, ECS_INTERNAL_ERROR, NULL); - } else { - /* If entity exists, make sure it gets the right scope and with */ - if (is_subject) { - ecs_entity_t scope = ecs_get_scope(world); - if (scope) { - ecs_add_pair(world, e, EcsChildOf, scope); - } + ecs_set_component_actions(world, EcsBitmask, { + .ctor = ecs_default_ctor, + .move = ecs_move(EcsBitmask), + .copy = ecs_copy(EcsBitmask), + .dtor = ecs_dtor(EcsBitmask) + }); - ecs_entity_t with = ecs_get_with(world); - if (with) { - ecs_add_id(world, e, with); - } - } - } + /* Register triggers to finalize type information from component data */ + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsPrimitive), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnSet}, + .callback = set_primitive + }); - return e; -} + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsMember), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnSet}, + .callback = set_member + }); -static -bool pred_is_subj( - ecs_term_t *term, - plecs_state_t *state) -{ - if (term->subj.name != NULL) { - return false; - } - if (term->obj.name != NULL) { - return false; - } - if (term->subj.set.mask == EcsNothing) { - return false; - } - if (state->with_stmt) { - return false; - } - if (state->assign_stmt) { - return false; - } - if (state->isa_stmt) { - return false; - } - if (state->using_stmt) { - return false; - } - return true; -} + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsEnum), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnAdd}, + .callback = add_enum + }); -static -int create_term( - ecs_world_t *world, - ecs_term_t *term, - const char *name, - const char *expr, - int64_t column, - plecs_state_t *state) -{ - state->last_subject = 0; - state->last_predicate = 0; - state->last_object = 0; - state->last_assign_id = 0; + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsBitmask), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnAdd}, + .callback = add_bitmask + }); - if (!ecs_term_id_is_set(&term->pred)) { - ecs_parser_error(name, expr, column, "missing predicate in expression"); - return -1; - } + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = EcsConstant, + .term.subj.set.mask = EcsSelf, + .events = {EcsOnAdd}, + .callback = add_constant + }); - if (state->assign_stmt && term->subj.entity != EcsThis) { - ecs_parser_error(name, expr, column, - "invalid statement in assign statement"); - return -1; - } + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_pair(EcsConstant, EcsWildcard), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnSet}, + .callback = add_constant + }); - bool pred_as_subj = pred_is_subj(term, state); + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsArray), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnSet}, + .callback = set_array + }); - ecs_entity_t pred = ensure_entity(world, state, term->pred.name, pred_as_subj); - ecs_entity_t subj = ensure_entity(world, state, term->subj.name, true); - ecs_entity_t obj = 0; + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsVector), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnSet}, + .callback = set_vector + }); - if (ecs_term_id_is_set(&term->obj)) { - obj = ensure_entity(world, state, term->obj.name, - state->assign_stmt == false); - } + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsMetaType), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnSet}, + .callback = ecs_meta_type_serialized_init + }); - if (state->assign_stmt || state->isa_stmt) { - subj = state->assign_to; - } + ecs_trigger_init(world, &(ecs_trigger_desc_t) { + .term.id = ecs_id(EcsMetaType), + .term.subj.set.mask = EcsSelf, + .events = {EcsOnSet}, + .callback = ecs_meta_type_init_default_ctor + }); - if (state->isa_stmt && obj) { - ecs_parser_error(name, expr, column, - "invalid object in inheritance statement"); - return -1; - } + /* Initialize primitive types */ + #define ECS_PRIMITIVE(world, type, primitive_kind)\ + ecs_entity_init(world, &(ecs_entity_desc_t) {\ + .entity = ecs_id(ecs_##type##_t),\ + .name = #type });\ + ecs_set(world, ecs_id(ecs_##type##_t), EcsPrimitive, {\ + .kind = primitive_kind\ + }); - if (state->using_stmt && (obj || subj)) { - ecs_parser_error(name, expr, column, - "invalid predicate/object in using statement"); - return -1; - } + ECS_PRIMITIVE(world, bool, EcsBool); + ECS_PRIMITIVE(world, char, EcsChar); + ECS_PRIMITIVE(world, byte, EcsByte); + ECS_PRIMITIVE(world, u8, EcsU8); + ECS_PRIMITIVE(world, u16, EcsU16); + ECS_PRIMITIVE(world, u32, EcsU32); + ECS_PRIMITIVE(world, u64, EcsU64); + ECS_PRIMITIVE(world, uptr, EcsUPtr); + ECS_PRIMITIVE(world, i8, EcsI8); + ECS_PRIMITIVE(world, i16, EcsI16); + ECS_PRIMITIVE(world, i32, EcsI32); + ECS_PRIMITIVE(world, i64, EcsI64); + ECS_PRIMITIVE(world, iptr, EcsIPtr); + ECS_PRIMITIVE(world, f32, EcsF32); + ECS_PRIMITIVE(world, f64, EcsF64); + ECS_PRIMITIVE(world, string, EcsString); + ECS_PRIMITIVE(world, entity, EcsEntity); - if (state->isa_stmt) { - pred = ecs_pair(EcsIsA, pred); - } + #undef ECS_PRIMITIVE - if (subj) { - if (!obj) { - ecs_add_id(world, subj, pred); - state->last_assign_id = pred; - } else { - ecs_add_pair(world, subj, pred, obj); - state->last_object = obj; - state->last_assign_id = ecs_pair(pred, obj); - } - state->last_predicate = pred; - state->last_subject = subj; + /* Set default child components */ + ecs_add_pair(world, ecs_id(EcsStruct), + EcsDefaultChildComponent, ecs_id(EcsMember)); - pred_as_subj = false; - } else { - if (!obj) { - /* If no subject or object were provided, use predicate as subj - * unless the expression explictly excluded the subject */ - if (pred_as_subj) { - state->last_subject = pred; - subj = pred; - } else { - state->last_predicate = pred; - pred_as_subj = false; - } - } else { - state->last_predicate = pred; - state->last_object = obj; - pred_as_subj = false; - } - } + ecs_add_pair(world, ecs_id(EcsMember), + EcsDefaultChildComponent, ecs_id(EcsMember)); - /* If this is a with clause (the list of entities between 'with' and scope - * open), add subject to the array of with frames */ - if (state->with_stmt) { - ecs_assert(pred != 0, ECS_INTERNAL_ERROR, NULL); - ecs_id_t id; + ecs_add_pair(world, ecs_id(EcsEnum), + EcsDefaultChildComponent, EcsConstant); - if (obj) { - id = ecs_pair(pred, obj); - } else { - id = pred; + ecs_add_pair(world, ecs_id(EcsBitmask), + EcsDefaultChildComponent, EcsConstant); + + /* Initialize reflection data for meta components */ + ecs_entity_t type_kind = ecs_enum_init(world, &(ecs_enum_desc_t) { + .entity.name = "TypeKind", + .constants = { + {.name = "PrimitiveType"}, + {.name = "BitmaskType"}, + {.name = "EnumType"}, + {.name = "StructType"}, + {.name = "ArrayType"}, + {.name = "VectorType"} } + }); - state->with[state->with_frame ++] = id; - - } else if (state->using_stmt) { - ecs_assert(pred != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(obj == 0, ECS_INTERNAL_ERROR, NULL); + ecs_struct_init(world, &(ecs_struct_desc_t) { + .entity.entity = ecs_id(EcsMetaType), + .members = { + {.name = (char*)"kind", .type = type_kind} + } + }); - state->using[state->using_frame ++] = pred; - state->using_frames[state->sp] = state->using_frame; + ecs_entity_t primitive_kind = ecs_enum_init(world, &(ecs_enum_desc_t) { + .entity.name = "PrimitiveKind", + .constants = { + {.name = "Bool", 1}, + {.name = "Char"}, + {.name = "Byte"}, + {.name = "U8"}, + {.name = "U16"}, + {.name = "U32"}, + {.name = "U64"}, + {.name = "I8"}, + {.name = "I16"}, + {.name = "I32"}, + {.name = "I64"}, + {.name = "F32"}, + {.name = "F64"}, + {.name = "UPtr"}, + {.name = "IPtr"}, + {.name = "String"}, + {.name = "Entity"} + } + }); - /* If this is not a with/using clause, add with frames to subject */ - } else { - if (subj) { - int32_t i, frame_count = state->with_frames[state->sp]; - for (i = 0; i < frame_count; i ++) { - ecs_add_id(world, subj, state->with[i]); - } + ecs_struct_init(world, &(ecs_struct_desc_t) { + .entity.entity = ecs_id(EcsPrimitive), + .members = { + {.name = (char*)"kind", .type = primitive_kind} } - } + }); - /* If an id was provided by itself, add default scope type to it */ - ecs_entity_t default_scope_type = state->default_scope_type[state->sp]; - if (pred_as_subj && default_scope_type) { - ecs_add_id(world, subj, default_scope_type); - } + ecs_struct_init(world, &(ecs_struct_desc_t) { + .entity.entity = ecs_id(EcsMember), + .members = { + {.name = (char*)"type", .type = ecs_id(ecs_entity_t)}, + {.name = (char*)"count", .type = ecs_id(ecs_i32_t)} + } + }); +} - /* If a comment preceded the statement, add it as a brief description */ -#ifdef FLECS_DOC - if (subj && state->comment) { - ecs_doc_set_brief(world, subj, state->comment); - ecs_os_free(state->comment); - state->comment = NULL; - } #endif - if (state->isa_stmt) { - state->last_assign_id = 0; - state->assign_to = 0; - state->isa_stmt = false; - } - - return 0; -} +#ifdef FLECS_META static -const char* parse_inherit_stmt( - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) +const char* op_kind_str( + ecs_meta_type_op_kind_t kind) { - if (state->isa_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "cannot nest inheritance"); - return NULL; - } + switch(kind) { - if (!state->last_subject) { - ecs_parser_error(name, expr, ptr - expr, - "missing entity to assign inheritance to"); - return NULL; + case EcsOpEnum: return "Enum"; + case EcsOpBitmask: return "Bitmask"; + case EcsOpArray: return "Array"; + case EcsOpVector: return "Vector"; + case EcsOpPush: return "Push"; + case EcsOpPop: return "Pop"; + case EcsOpPrimitive: return "Primitive"; + case EcsOpBool: return "Bool"; + case EcsOpChar: return "Char"; + case EcsOpByte: return "Byte"; + case EcsOpU8: return "U8"; + case EcsOpU16: return "U16"; + case EcsOpU32: return "U32"; + case EcsOpU64: return "U64"; + case EcsOpI8: return "I8"; + case EcsOpI16: return "I16"; + case EcsOpI32: return "I32"; + case EcsOpI64: return "I64"; + case EcsOpF32: return "F32"; + case EcsOpF64: return "F64"; + case EcsOpUPtr: return "UPtr"; + case EcsOpIPtr: return "IPtr"; + case EcsOpString: return "String"; + case EcsOpEntity: return "Entity"; + default: return "<< invalid kind >>"; } - - state->isa_stmt = true; - state->assign_to = state->last_subject; +} - return ptr; +/* Get current scope */ +static +ecs_meta_scope_t* get_scope( + ecs_meta_cursor_t *cursor) +{ + ecs_assert(cursor != NULL, ECS_INVALID_PARAMETER, NULL); + return &cursor->scope[cursor->depth]; } +/* Get previous scope */ static -const char* parse_assign_expr( - ecs_world_t *world, - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) +ecs_meta_scope_t* get_prev_scope( + ecs_meta_cursor_t *cursor) { - (void)world; - - if (!state->assign_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "unexpected value outside of assignment statement"); - return NULL; - } + ecs_assert(cursor != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(cursor->depth > 0, ECS_INVALID_PARAMETER, NULL); + return &cursor->scope[cursor->depth - 1]; +} - ecs_id_t assign_id = state->last_assign_id; - if (!assign_id) { - ecs_parser_error(name, expr, ptr - expr, - "missing type for assignment statement"); - return NULL; - } +/* Get current operation for scope */ +static +ecs_meta_type_op_t* get_op( + ecs_meta_scope_t *scope) +{ + return &scope->ops[scope->op_cur]; +} -#ifndef FLECS_EXPR - ecs_parser_error(name, expr, ptr - expr, - "cannot parse value, missing FLECS_EXPR addon"); - return NULL; -#else - ecs_entity_t assign_to = state->assign_to; - if (!assign_to) { - assign_to = state->last_subject; - } - - if (!assign_to) { - ecs_parser_error(name, expr, ptr - expr, - "missing entity to assign to"); - return NULL; +/* Get component for type in current scope */ +static +const EcsComponent* get_component_ptr( + const ecs_world_t *world, + ecs_meta_scope_t *scope) +{ + const EcsComponent *comp = scope->comp; + if (!comp) { + comp = scope->comp = ecs_get(world, scope->type, EcsComponent); + ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); } + return comp; +} - ecs_entity_t type = ecs_get_typeid(world, assign_id); - if (!type) { - char *id_str = ecs_id_str(world, assign_id); - ecs_parser_error(name, expr, ptr - expr, - "invalid assignment, '%s' is not a type", id_str); - ecs_os_free(id_str); - return NULL; - } +/* Get size for type in current scope */ +static +ecs_size_t get_size( + const ecs_world_t *world, + ecs_meta_scope_t *scope) +{ + return get_component_ptr(world, scope)->size; +} - void *value_ptr = ecs_get_mut_id( - world, assign_to, assign_id, NULL); +/* Get alignment for type in current scope */ +static +ecs_size_t get_alignment( + const ecs_world_t *world, + ecs_meta_scope_t *scope) +{ + return get_component_ptr(world, scope)->alignment; +} - ptr = ecs_parse_expr(world, ptr, type, value_ptr, - &(ecs_parse_expr_desc_t) { - .name = name, - .expr = expr, - .lookup_action = plecs_lookup_action, - .lookup_ctx = state - }); - if (!ptr) { - return NULL; +static +int32_t get_elem_count( + ecs_meta_scope_t *scope) +{ + if (scope->vector) { + return ecs_vector_count(*(scope->vector)); } - ecs_modified_id(world, assign_to, assign_id); -#endif - - return ptr; + ecs_meta_type_op_t *op = get_op(scope); + return op->count; } +/* Get pointer to current field/element */ static -const char* parse_assign_stmt( - ecs_world_t *world, - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) +ecs_meta_type_op_t* get_ptr( + const ecs_world_t *world, + ecs_meta_scope_t *scope) { - (void)world; + ecs_meta_type_op_t *op = get_op(scope); + ecs_size_t size = get_size(world, scope); - if (state->isa_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "missing base for inheritance statement"); - return NULL; + if (scope->vector) { + ecs_size_t align = get_alignment(world, scope); + ecs_vector_set_min_count_t( + scope->vector, size, align, scope->elem_cur + 1); + scope->ptr = ecs_vector_first_t(*(scope->vector), size, align); } - /* Component scope (add components to entity) */ - if (!state->last_subject) { - ecs_parser_error(name, expr, ptr - expr, - "missing entity to assign to"); - return NULL; - } + return ECS_OFFSET(scope->ptr, size * scope->elem_cur + op->offset); +} - if (state->assign_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "invalid assign statement in assign statement"); - return NULL; +static +int push_type( + const ecs_world_t *world, + ecs_meta_scope_t *scope, + ecs_entity_t type, + void *ptr) +{ + const EcsMetaTypeSerialized *ser = ecs_get( + world, type, EcsMetaTypeSerialized); + if (ser == NULL) { + char *str = ecs_id_str(world, type); + ecs_err("cannot open scope for entity '%s' which is not a type", str); + ecs_os_free(str); + return -1; } - state->assign_stmt = true; - state->assign_to = state->last_subject; - - return ptr; + scope[0] = (ecs_meta_scope_t) { + .type = type, + .ops = ecs_vector_first(ser->ops, ecs_meta_type_op_t), + .op_count = ecs_vector_count(ser->ops), + .ptr = ptr + }; + + return 0; } -static -const char* parse_using_stmt( - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) +ecs_meta_cursor_t ecs_meta_cursor( + const ecs_world_t *world, + ecs_entity_t type, + void *ptr) { - if (state->isa_stmt || state->assign_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "invalid usage of using keyword"); - return NULL; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(type != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); + + ecs_meta_cursor_t result = { + .world = world, + .valid = true + }; + + if (push_type(world, result.scope, type, ptr) != 0) { + result.valid = false; } - /* Add following expressions to using list */ - state->using_stmt = true; + return result; +} - return ptr + 5; +void* ecs_meta_get_ptr( + ecs_meta_cursor_t *cursor) +{ + return get_ptr(cursor->world, get_scope(cursor)); } -static -const char* parse_with_stmt( - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) +int ecs_meta_next( + ecs_meta_cursor_t *cursor) { - if (state->isa_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "invalid with after inheritance"); - return NULL; + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + + if (scope->is_collection) { + scope->elem_cur ++; + scope->op_cur = 0; + if (scope->elem_cur >= get_elem_count(scope)) { + ecs_err("out of collection bounds (%d)", scope->elem_cur); + return -1; + } + + return 0; } - if (state->assign_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "invalid with in assign_stmt"); - return NULL; + scope->op_cur += op->op_count; + if (scope->op_cur >= scope->op_count) { + ecs_err("out of bounds"); + return -1; } - /* Add following expressions to with list */ - state->with_stmt = true; - return ptr + 5; + return 0; } -static -const char* parse_scope_open( - ecs_world_t *world, - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) +int ecs_meta_member( + ecs_meta_cursor_t *cursor, + const char *name) { - if (state->isa_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "missing base for inheritance"); - return NULL; + if (cursor->depth == 0) { + ecs_err("cannot move to member in root scope"); + return -1; } - if (state->assign_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "invalid scope in assign_stmt"); - return NULL; + ecs_meta_scope_t *prev_scope = get_prev_scope(cursor); + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *push_op = get_op(prev_scope); + const ecs_world_t *world = cursor->world; + + ecs_assert(push_op->kind == EcsOpPush, ECS_INTERNAL_ERROR, NULL); + + if (!push_op->members) { + ecs_err("cannot move to member '%s' for non-struct type", name); + return -1; } - state->sp ++; + ecs_hashed_string_t key = ecs_get_hashed_string( + name, ecs_os_strlen(name), 0); + int32_t *cur = flecs_hashmap_get(*push_op->members, &key, int32_t); + if (!cur) { + char *path = ecs_get_fullpath(world, scope->type); + ecs_err("unknown member '%s' for type '%s'", name, path); + ecs_os_free(path); + return -1; + } - ecs_entity_t scope = 0; - ecs_entity_t default_scope_type = 0; + scope->op_cur = *cur; - if (!state->with_stmt) { - if (state->last_subject) { - scope = state->last_subject; - ecs_set_scope(world, state->last_subject); + return 0; +} - /* Check if scope has a default child component */ - ecs_entity_t def_type_src = ecs_get_object_for_id(world, scope, - 0, ecs_pair(EcsDefaultChildComponent, EcsWildcard)); +int ecs_meta_push( + ecs_meta_cursor_t *cursor) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + const ecs_world_t *world = cursor->world; - if (def_type_src) { - default_scope_type = ecs_get_object( - world, def_type_src, EcsDefaultChildComponent, 0); - } - } else { - if (state->last_object) { - scope = ecs_pair( - state->last_predicate, state->last_object); - ecs_set_with(world, scope); - } else { - if (state->last_predicate) { - scope = ecs_pair(EcsChildOf, state->last_predicate); - } - ecs_set_scope(world, state->last_predicate); + if (cursor->depth == 0) { + if (!cursor->is_primitive_scope) { + if (op->kind > EcsOpScope) { + cursor->is_primitive_scope = true; + return 0; } } - - state->scope[state->sp] = scope; - state->default_scope_type[state->sp] = default_scope_type; - } else { - state->scope[state->sp] = state->scope[state->sp - 1]; - state->default_scope_type[state->sp] = - state->default_scope_type[state->sp - 1]; } - state->using_frames[state->sp] = state->using_frame; - state->with_frames[state->sp] = state->with_frame; - state->with_stmt = false; - - return ptr; -} + void *ptr = get_ptr(world, scope); + cursor->depth ++; + ecs_assert(cursor->depth < ECS_META_MAX_SCOPE_DEPTH, + ECS_INTERNAL_ERROR, NULL); -static -const char* parse_scope_close( - ecs_world_t *world, - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) -{ - if (state->isa_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "invalid '}' after inheritance statement"); - return NULL; - } + ecs_meta_scope_t *next_scope = get_scope(cursor); - if (state->assign_stmt) { - ecs_parser_error(name, expr, ptr - expr, - "unfinished assignment before }"); - return NULL; - } + /* If we're not already in an inline array and this operation is an inline + * array, push a frame for the array. + * Doing this first ensures that inline arrays take precedence over other + * kinds of push operations, such as for a struct element type. */ + if (!scope->is_inline_array && op->count > 1 && !scope->is_collection) { + /* Push a frame just for the element type, with inline_array = true */ + next_scope[0] = (ecs_meta_scope_t){ + .ops = op, + .op_count = op->op_count, + .ptr = scope->ptr, + .type = op->type, + .is_collection = true, + .is_inline_array = true + }; - state->scope[state->sp] = 0; - state->default_scope_type[state->sp] = 0; - state->sp --; + /* With 'is_inline_array' set to true we ensure that we can never push + * the same inline array twice */ - if (state->sp < 0) { - ecs_parser_error(name, expr, ptr - expr, "invalid } without a {"); - return NULL; + return 0; } - ecs_id_t id = state->scope[state->sp]; + switch(op->kind) { + case EcsOpPush: + next_scope[0] = (ecs_meta_scope_t) { + .ops = &op[1], /* op after push */ + .op_count = op->op_count - 1, /* don't include pop */ + .ptr = scope->ptr, + .type = op->type + }; + break; - if (!id || ECS_HAS_ROLE(id, PAIR)) { - ecs_set_with(world, id); - } + case EcsOpArray: { + if (push_type(world, next_scope, op->type, ptr) != 0) { + return -1; + } - if (!id || !ECS_HAS_ROLE(id, PAIR)) { - ecs_set_scope(world, id); + const EcsArray *type_ptr = ecs_get(world, op->type, EcsArray); + next_scope->type = type_ptr->type; + next_scope->is_collection = true; + break; } - state->with_frame = state->with_frames[state->sp]; - state->using_frame = state->using_frames[state->sp]; - state->last_subject = 0; - state->assign_stmt = false; - - return ptr; -} + case EcsOpVector: + next_scope->vector = ptr; + if (push_type(world, next_scope, op->type, NULL) != 0) { + return -1; + } -static -const char *parse_plecs_term( - ecs_world_t *world, - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) -{ - ecs_term_t term = {0}; + const EcsVector *type_ptr = ecs_get(world, op->type, EcsVector); + next_scope->type = type_ptr->type; + next_scope->is_collection = true; + break; - ptr = ecs_parse_term(world, name, expr, ptr, &term); - if (!ptr) { - return NULL; + default: { + char *path = ecs_get_fullpath(world, scope->type); + ecs_err("invalid push for type '%s'", path); + ecs_os_free(path); + return -1; } - - if (!ecs_term_is_initialized(&term)) { - ecs_parser_error(name, expr, ptr - expr, "expected identifier"); - return NULL; /* No term found */ } - if (create_term(world, &term, name, expr, (ptr - expr), state)) { - ecs_term_fini(&term); - return NULL; /* Failed to create term */ + if (scope->is_collection) { + next_scope[0].ptr = ECS_OFFSET(next_scope[0].ptr, + scope->elem_cur * get_size(world, scope)); } - ecs_term_fini(&term); - - return ptr; + return 0; } -static -const char* parse_stmt( - ecs_world_t *world, - const char *name, - const char *expr, - const char *ptr, - plecs_state_t *state) +int ecs_meta_pop( + ecs_meta_cursor_t *cursor) { - state->assign_stmt = false; - state->isa_stmt = false; - state->with_stmt = false; - state->using_stmt = false; - state->last_subject = 0; - state->last_predicate = 0; - state->last_object = 0; + if (cursor->is_primitive_scope) { + cursor->is_primitive_scope = false; + return 0; + } - ptr = parse_fluff(expr, ptr, state); + ecs_meta_scope_t *scope = get_scope(cursor); + cursor->depth --; + if (cursor->depth < 0) { + ecs_err("unexpected end of scope"); + return -1; + } - char ch = ptr[0]; + ecs_meta_scope_t *next_scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(next_scope); - if (!ch) { - goto done; - } else if (ch == '{') { - ptr = parse_fluff(expr, ptr + 1, state); - goto scope_open; - } else if (ch == '}') { - ptr = parse_fluff(expr, ptr + 1, state); - goto scope_close; - } else if (ch == '(') { - if (ecs_get_scope(world) != 0) { - goto assign_to_scope_stmt; + if (!scope->is_inline_array) { + if (op->kind == EcsOpPush) { + next_scope->op_cur += op->op_count - 1; + + /* push + op_count should point to the operation after pop */ + op = get_op(next_scope); + ecs_assert(op->kind == EcsOpPop, ECS_INTERNAL_ERROR, NULL); + } else if (op->kind == EcsOpArray || op->kind == EcsOpVector) { + /* Collection type, nothing else to do */ } else { - goto term_expr; + /* should not have been able to push if the previous scope was not + * a complex or collection type */ + ecs_assert(false, ECS_INTERNAL_ERROR, NULL); } - } else if (!ecs_os_strncmp(ptr, TOK_USING " ", 5)) { - ptr = parse_using_stmt(name, expr, ptr, state); - if (!ptr) goto error; - goto term_expr; - } else if (!ecs_os_strncmp(ptr, TOK_WITH " ", 5)) { - ptr = parse_with_stmt(name, expr, ptr, state); - if (!ptr) goto error; - goto term_expr; } else { - goto term_expr; + /* Make sure that this was an inline array */ + ecs_assert(next_scope->op_count > 1, ECS_INTERNAL_ERROR, NULL); } - ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - - goto done; + return 0; +} -term_expr: - if (!ptr[0]) { - goto done; - } +int ecs_meta_is_collection( + ecs_meta_cursor_t *cursor) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + return scope->is_collection; +} - if (!(ptr = parse_plecs_term(world, name, ptr, ptr, state))) { - goto error; - } +ecs_entity_t ecs_meta_get_type( + ecs_meta_cursor_t *cursor) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + return op->type; +} - ptr = parse_fluff(expr, ptr, state); +/* Utility macro's to let the compiler do the conversion work for us */ +#define set_T(T, ptr, value)\ + ((T*)ptr)[0] = ((T)value) - if (ptr[0] == '{' && !isspace(ptr[-1])) { - /* A '{' directly after an identifier (no whitespace) is a literal */ - goto assign_expr; - } +#define case_T(kind, T, dst, src)\ +case kind:\ + set_T(T, dst, src);\ + break - if (!state->using_stmt) { - if (ptr[0] == ':') { - ptr = parse_fluff(expr, ptr + 1, state); - goto inherit_stmt; - } else if (ptr[0] == '=') { - ptr = parse_fluff(expr, ptr + 1, state); - goto assign_stmt; - } else if (ptr[0] == ',') { - ptr = parse_fluff(expr, ptr + 1, state); - goto term_expr; - } else if (ptr[0] == '{') { - state->assign_stmt = false; - ptr = parse_fluff(expr, ptr + 1, state); - goto scope_open; - } - } +#define cases_T_float(dst, src)\ + case_T(EcsOpF32, ecs_f32_t, dst, src);\ + case_T(EcsOpF64, ecs_f64_t, dst, src) - state->assign_stmt = false; - goto done; +#define cases_T_signed(dst, src)\ + case_T(EcsOpChar, ecs_char_t, dst, src);\ + case_T(EcsOpI8, ecs_i8_t, dst, src);\ + case_T(EcsOpI16, ecs_i16_t, dst, src);\ + case_T(EcsOpI32, ecs_i32_t, dst, src);\ + case_T(EcsOpI64, ecs_i64_t, dst, src);\ + case_T(EcsOpIPtr, ecs_iptr_t, dst, src) -inherit_stmt: - ptr = parse_inherit_stmt(name, expr, ptr, state); - if (!ptr) goto error; +#define cases_T_unsigned(dst, src)\ + case_T(EcsOpByte, ecs_byte_t, dst, src);\ + case_T(EcsOpU8, ecs_u8_t, dst, src);\ + case_T(EcsOpU16, ecs_u16_t, dst, src);\ + case_T(EcsOpU32, ecs_u32_t, dst, src);\ + case_T(EcsOpU64, ecs_u64_t, dst, src);\ + case_T(EcsOpUPtr, ecs_uptr_t, dst, src);\ - /* Expect base identifier */ - goto term_expr; +#define cases_T_bool(dst, src)\ +case EcsOpBool:\ + set_T(ecs_bool_t, dst, value != 0);\ + break -assign_to_scope_stmt: - state->last_subject = ecs_get_scope(world); - goto assign_stmt; +static +void conversion_error( + ecs_meta_cursor_t *cursor, + ecs_meta_type_op_t *op, + const char *from) +{ + char *path = ecs_get_fullpath(cursor->world, op->type); + ecs_err("unsupported conversion from %s to '%s'", from, path); + ecs_os_free(path); +} -assign_stmt: - ptr = parse_assign_stmt(world, name, expr, ptr, state); - if (!ptr) goto error; +int ecs_meta_set_bool( + ecs_meta_cursor_t *cursor, + bool value) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); - ptr = parse_fluff(expr, ptr, state); + switch(op->kind) { + cases_T_bool(ptr, value); + cases_T_unsigned(ptr, value); + default: + conversion_error(cursor, op, "bool"); + return -1; + } - if (ptr[0] == '{') { - /* Assignment without a preceding component */ - ecs_entity_t type = state->default_scope_type[state->sp]; - if (!type) { - ecs_parser_error(name, expr, ptr - expr, - "missing type for assignment"); - return NULL; - } + return 0; +} - state->last_assign_id = type; - goto assign_expr; +int ecs_meta_set_char( + ecs_meta_cursor_t *cursor, + char value) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); + + switch(op->kind) { + cases_T_bool(ptr, value); + cases_T_signed(ptr, value); + default: + conversion_error(cursor, op, "char"); + return -1; } - /* Expect component identifiers */ - goto term_expr; + return 0; +} -assign_expr: - ptr = parse_assign_expr(world, name, expr, ptr, state); - if (!ptr) goto error; +int ecs_meta_set_int( + ecs_meta_cursor_t *cursor, + int64_t value) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); - ptr = parse_fluff(expr, ptr, state); - if (ptr[0] == ',') { - ptr ++; - goto term_expr; - } else if (ptr[0] == '{') { - state->assign_stmt = false; - ptr ++; - goto scope_open; - } else { - state->assign_stmt = false; - goto done; + switch(op->kind) { + cases_T_bool(ptr, value); + cases_T_signed(ptr, value); + cases_T_float(ptr, value); + default: { + conversion_error(cursor, op, "int"); + return -1; + } } -scope_open: - ptr = parse_scope_open(world, name, expr, ptr, state); - if (!ptr) goto error; - goto done; + return 0; +} -scope_close: - ptr = parse_scope_close(world, name, expr, ptr, state); - if (!ptr) goto error; - goto done; +int ecs_meta_set_uint( + ecs_meta_cursor_t *cursor, + uint64_t value) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); -done: - return ptr; -error: - return NULL; + switch(op->kind) { + cases_T_bool(ptr, value); + cases_T_unsigned(ptr, value); + cases_T_float(ptr, value); + case EcsOpEntity: + set_T(ecs_entity_t, ptr, value); + break; + default: + conversion_error(cursor, op, "uint"); + return -1; + } + + return 0; } -int ecs_plecs_from_str( - ecs_world_t *world, - const char *name, - const char *expr) +int ecs_meta_set_float( + ecs_meta_cursor_t *cursor, + double value) { - const char *ptr = expr; - ecs_term_t term = {0}; - plecs_state_t state = {0}; + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); - if (!expr) { + switch(op->kind) { + cases_T_bool(ptr, value); + cases_T_signed(ptr, value); + cases_T_unsigned(ptr, value); + cases_T_float(ptr, value); + default: + conversion_error(cursor, op, "float"); + return -1; + } + + return 0; +} + +static +int add_bitmask_constant( + ecs_meta_cursor_t *cursor, + ecs_meta_type_op_t *op, + void *out, + const char *value) +{ + ecs_assert(op->type != 0, ECS_INTERNAL_ERROR, NULL); + + if (!ecs_os_strcmp(value, "0")) { return 0; } - state.scope[0] = ecs_get_scope(world); - ecs_entity_t prev_with = ecs_set_with(world, 0); + ecs_entity_t c = ecs_lookup_child(cursor->world, op->type, value); + if (!c) { + char *path = ecs_get_fullpath(cursor->world, op->type); + ecs_err("unresolved bitmask constant '%s' for type '%s'", value, path); + ecs_os_free(path); + return -1; + } - do { - expr = ptr = parse_stmt(world, name, expr, ptr, &state); - if (!ptr) { - goto error; - } + const ecs_u32_t *v = ecs_get_pair_object( + cursor->world, c, EcsConstant, ecs_u32_t); + if (v == NULL) { + char *path = ecs_get_fullpath(cursor->world, op->type); + ecs_err("'%s' is not an bitmask constant for type '%s'", value, path); + ecs_os_free(path); + return -1; + } - if (!ptr[0]) { - break; /* End of expression */ + *(ecs_u32_t*)out |= v[0]; + + return 0; +} + +static +int parse_bitmask( + ecs_meta_cursor_t *cursor, + ecs_meta_type_op_t *op, + void *out, + const char *value) +{ + char token[ECS_MAX_TOKEN_SIZE]; + + const char *prev = value, *ptr = value; + + *(ecs_u32_t*)out = 0; + + while ((ptr = strchr(ptr, '|'))) { + ecs_os_memcpy(token, prev, ptr - prev); + token[ptr - prev] = '\0'; + if (add_bitmask_constant(cursor, op, out, token) != 0) { + return -1; } - } while (true); - ecs_set_scope(world, state.scope[0]); - ecs_set_with(world, prev_with); - - clear_comment(expr, ptr, &state); + ptr ++; + prev = ptr; + } - if (state.sp != 0) { - ecs_parser_error(name, expr, 0, "missing end of scope"); - goto error; + if (add_bitmask_constant(cursor, op, out, prev) != 0) { + return -1; } - if (state.assign_stmt) { - ecs_parser_error(name, expr, 0, "unfinished assignment"); - goto error; + return 0; +} + +int ecs_meta_set_string( + ecs_meta_cursor_t *cursor, + const char *value) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); + + switch(op->kind) { + case EcsOpBool: + if (!ecs_os_strcmp(value, "true")) { + set_T(ecs_bool_t, ptr, true); + } else if (!ecs_os_strcmp(value, "false")) { + set_T(ecs_bool_t, ptr, false); + } else { + ecs_err("invalid value for boolean '%s'", value); + return -1; + } + break; + case EcsOpI8: + case EcsOpU8: + case EcsOpChar: + case EcsOpByte: + set_T(ecs_i8_t, ptr, atol(value)); + break; + case EcsOpI16: + case EcsOpU16: + set_T(ecs_i16_t, ptr, atol(value)); + break; + case EcsOpI32: + case EcsOpU32: + set_T(ecs_i32_t, ptr, atol(value)); + break; + case EcsOpI64: + case EcsOpU64: + set_T(ecs_i64_t, ptr, atol(value)); + break; + case EcsOpIPtr: + case EcsOpUPtr: + set_T(ecs_iptr_t, ptr, atol(value)); + break; + case EcsOpF32: + set_T(ecs_f32_t, ptr, atof(value)); + break; + case EcsOpF64: + set_T(ecs_f64_t, ptr, atof(value)); + break; + case EcsOpString: { + ecs_os_free(*(char**)ptr); + char *result = ecs_os_strdup(value); + set_T(ecs_string_t, ptr, result); + break; } + case EcsOpEnum: { + ecs_assert(op->type != 0, ECS_INTERNAL_ERROR, NULL); + ecs_entity_t c = ecs_lookup_child(cursor->world, op->type, value); + if (!c) { + char *path = ecs_get_fullpath(cursor->world, op->type); + ecs_err("unresolved enum constant '%s' for type '%s'", value, path); + ecs_os_free(path); + return -1; + } - if (state.errors) { - goto error; + const ecs_i32_t *v = ecs_get_pair_object( + cursor->world, c, EcsConstant, ecs_i32_t); + if (v == NULL) { + char *path = ecs_get_fullpath(cursor->world, op->type); + ecs_err("'%s' is not an enum constant for type '%s'", value, path); + ecs_os_free(path); + return -1; + } + + set_T(ecs_i32_t, ptr, v[0]); + break; + } + case EcsOpBitmask: + if (parse_bitmask(cursor, op, ptr, value) != 0) { + return -1; + } + break; + case EcsOpEntity: { + ecs_entity_t e = 0; + + if (ecs_os_strcmp(value, "0")) { + if (cursor->lookup_action) { + e = cursor->lookup_action( + cursor->world, value, + cursor->lookup_ctx); + } else { + e = ecs_lookup_path(cursor->world, 0, value); + } + + if (!e) { + ecs_err("unresolved entity identifier '%s'", value); + return -1; + } + } + + set_T(ecs_entity_t, ptr, e); + break; + } + case EcsOpPop: + ecs_err("excess element '%s' in scope", value); + return -1; + default: + ecs_err("unsupported conversion from string '%s' to '%s'", + value, op_kind_str(op->kind)); + return -1; } return 0; -error: - ecs_set_scope(world, state.scope[0]); - ecs_set_with(world, prev_with); - ecs_term_fini(&term); - return -1; } -int ecs_plecs_from_file( - ecs_world_t *world, - const char *filename) +int ecs_meta_set_string_literal( + ecs_meta_cursor_t *cursor, + const char *value) { - FILE* file; - char* content = NULL; - int32_t bytes; - size_t size; + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); - /* Open file for reading */ - ecs_os_fopen(&file, filename, "r"); - if (!file) { - ecs_err("%s (%s)", ecs_os_strerror(errno), filename); - goto error; + ecs_size_t len = ecs_os_strlen(value); + if (value[0] != '\"' || value[len - 1] != '\"') { + ecs_err("invalid string literal '%s'", value); + return -1; } - /* Determine file size */ - fseek(file, 0 , SEEK_END); - bytes = (int32_t)ftell(file); - if (bytes == -1) { - goto error; + switch(op->kind) { + case EcsOpChar: + set_T(ecs_char_t, ptr, value[1]); + break; + + default: + case EcsOpEntity: + case EcsOpString: + len -= 2; + + char *result = ecs_os_malloc(len + 1); + ecs_os_memcpy(result, value + 1, len); + result[len] = '\0'; + + if (ecs_meta_set_string(cursor, result)) { + ecs_os_free(result); + return -1; + } + + ecs_os_free(result); + + break; } - rewind(file); - /* Load contents in memory */ - content = ecs_os_malloc(bytes + 1); - size = (size_t)bytes; - if (!(size = fread(content, 1, size, file)) && bytes) { - ecs_err("%s: read zero bytes instead of %d", filename, size); - ecs_os_free(content); - content = NULL; - goto error; - } else { - content[size] = '\0'; + return 0; +} + +int ecs_meta_set_entity( + ecs_meta_cursor_t *cursor, + ecs_entity_t value) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); + + switch(op->kind) { + case EcsOpEntity: + set_T(ecs_entity_t, ptr, value); + break; + default: + conversion_error(cursor, op, "entity"); + return -1; } - fclose(file); + return 0; +} - int result = ecs_plecs_from_str(world, filename, content); - ecs_os_free(content); - return result; -error: - ecs_os_free(content); - return -1; +int ecs_meta_set_null( + ecs_meta_cursor_t *cursor) +{ + ecs_meta_scope_t *scope = get_scope(cursor); + ecs_meta_type_op_t *op = get_op(scope); + void *ptr = get_ptr(cursor->world, scope); + switch (op->kind) { + case EcsOpString: + ecs_os_free(*(char**)ptr); + set_T(ecs_string_t, ptr, NULL); + break; + default: + conversion_error(cursor, op, "null"); + return -1; + } + + return 0; } #endif @@ -18999,4903 +19337,4561 @@ bool ecs_rule_next( #endif -#ifdef FLECS_MODULE +#ifdef FLECS_META_C +#define ECS_META_IDENTIFIER_LENGTH (256) -char* ecs_module_path_from_c( - const char *c_name) -{ - ecs_strbuf_t str = ECS_STRBUF_INIT; - const char *ptr; - char ch; +#define ecs_meta_error(ctx, ptr, ...)\ + ecs_parser_error((ctx)->name, (ctx)->desc, ptr - (ctx)->desc, __VA_ARGS__); - for (ptr = c_name; (ch = *ptr); ptr++) { - if (isupper(ch)) { - ch = flecs_to_i8(tolower(ch)); - if (ptr != c_name) { - ecs_strbuf_appendstrn(&str, ".", 1); - } - } +typedef char ecs_meta_token_t[ECS_META_IDENTIFIER_LENGTH]; - ecs_strbuf_appendstrn(&str, &ch, 1); - } +typedef struct meta_parse_ctx_t { + const char *name; + const char *desc; +} meta_parse_ctx_t; - return ecs_strbuf_get(&str); -} +typedef struct meta_type_t { + ecs_meta_token_t type; + ecs_meta_token_t params; + bool is_const; + bool is_ptr; +} meta_type_t; -ecs_entity_t ecs_import( - ecs_world_t *world, - ecs_module_action_t init_action, - const char *module_name) -{ - ecs_assert(!world->is_readonly, ECS_INVALID_WHILE_ITERATING, NULL); +typedef struct meta_member_t { + meta_type_t type; + ecs_meta_token_t name; + int64_t count; + bool is_partial; +} meta_member_t; - ecs_entity_t old_scope = ecs_set_scope(world, 0); - const char *old_name_prefix = world->name_prefix; +typedef struct meta_constant_t { + ecs_meta_token_t name; + int64_t value; + bool is_value_set; +} meta_constant_t; - char *path = ecs_module_path_from_c(module_name); - ecs_entity_t e = ecs_lookup_fullpath(world, path); - ecs_os_free(path); - - if (!e) { - ecs_trace("#[magenta]import#[reset] %s", module_name); - ecs_log_push(); +typedef struct meta_params_t { + meta_type_t key_type; + meta_type_t type; + int64_t count; + bool is_key_value; + bool is_fixed_size; +} meta_params_t; - /* Load module */ - init_action(world); +static +const char* skip_scope(const char *ptr, meta_parse_ctx_t *ctx) { + /* Keep track of which characters were used to open the scope */ + char stack[256]; + int32_t sp = 0; + char ch; - /* Lookup module entity (must be registered by module) */ - e = ecs_lookup_fullpath(world, module_name); - ecs_assert(e != 0, ECS_MODULE_UNDEFINED, module_name); + while ((ch = *ptr)) { + if (ch == '(' || ch == '<') { + stack[sp] = ch; - ecs_log_pop(); - } + sp ++; + if (sp >= 256) { + ecs_meta_error(ctx, ptr, "maximum level of nesting reached"); + goto error; + } + } else if (ch == ')' || ch == '>') { + sp --; + if ((sp < 0) || (ch == '>' && stack[sp] != '<') || + (ch == ')' && stack[sp] != '(')) + { + ecs_meta_error(ctx, ptr, "mismatching %c in identifier", ch); + goto error; + } + } - /* Restore to previous state */ - ecs_set_scope(world, old_scope); - world->name_prefix = old_name_prefix; + ptr ++; - return e; + if (!sp) { + break; + } + } + + return ptr; +error: + return NULL; } -ecs_entity_t ecs_import_from_library( - ecs_world_t *world, - const char *library_name, - const char *module_name) +static +const char* parse_c_digit( + const char *ptr, + int64_t *value_out) { - ecs_assert(library_name != NULL, ECS_INVALID_PARAMETER, NULL); - - char *import_func = (char*)module_name; /* safe */ - char *module = (char*)module_name; - - if (!ecs_os_has_modules() || !ecs_os_has_dl()) { - ecs_err( - "library loading not supported, set module_to_dl, dlopen, dlclose " - "and dlproc os API callbacks first"); - return 0; + char token[24]; + ptr = ecs_parse_eol_and_whitespace(ptr); + ptr = ecs_parse_digit(ptr, token); + if (!ptr) { + goto error; } - /* If no module name is specified, try default naming convention for loading - * the main module from the library */ - if (!import_func) { - import_func = ecs_os_malloc(ecs_os_strlen(library_name) + ECS_SIZEOF("Import")); - ecs_assert(import_func != NULL, ECS_OUT_OF_MEMORY, NULL); - - const char *ptr; - char ch, *bptr = import_func; - bool capitalize = true; - for (ptr = library_name; (ch = *ptr); ptr ++) { - if (ch == '.') { - capitalize = true; - } else { - if (capitalize) { - *bptr = flecs_to_i8(toupper(ch)); - bptr ++; - capitalize = false; - } else { - *bptr = flecs_to_i8(tolower(ch)); - bptr ++; - } - } - } + *value_out = strtol(token, NULL, 0); - *bptr = '\0'; + return ecs_parse_eol_and_whitespace(ptr); +error: + return NULL; +} - module = ecs_os_strdup(import_func); - ecs_assert(module != NULL, ECS_OUT_OF_MEMORY, NULL); +static +const char* parse_c_identifier( + const char *ptr, + char *buff, + char *params, + meta_parse_ctx_t *ctx) +{ + ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(buff != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ctx != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_os_strcat(bptr, "Import"); - } + char *bptr = buff, ch; - char *library_filename = ecs_os_module_to_dl(library_name); - if (!library_filename) { - ecs_err("failed to find library file for '%s'", library_name); - if (module != module_name) { - ecs_os_free(module); - } - return 0; - } else { - ecs_trace("found file '%s' for library '%s'", - library_filename, library_name); + if (params) { + params[0] = '\0'; } - ecs_os_dl_t dl = ecs_os_dlopen(library_filename); - if (!dl) { - ecs_err("failed to load library '%s' ('%s')", - library_name, library_filename); - - ecs_os_free(library_filename); - - if (module != module_name) { - ecs_os_free(module); - } + /* Ignore whitespaces */ + ptr = ecs_parse_eol_and_whitespace(ptr); - return 0; - } else { - ecs_trace("library '%s' ('%s') loaded", - library_name, library_filename); + if (!isalpha(*ptr)) { + ecs_meta_error(ctx, ptr, + "invalid identifier (starts with '%c')", *ptr); + goto error; } - ecs_module_action_t action = (ecs_module_action_t) - ecs_os_dlproc(dl, import_func); - if (!action) { - ecs_err("failed to load import function %s from library %s", - import_func, library_name); - ecs_os_free(library_filename); - ecs_os_dlclose(dl); - return 0; - } else { - ecs_trace("found import function '%s' in library '%s' for module '%s'", - import_func, library_name, module); - } + while ((ch = *ptr) && !isspace(ch) && ch != ';' && ch != ',' && ch != ')' && ch != '>') { + /* Type definitions can contain macro's or templates */ + if (ch == '(' || ch == '<') { + if (!params) { + ecs_meta_error(ctx, ptr, "unexpected %c", *ptr); + goto error; + } - /* Do not free id, as it will be stored as the component identifier */ - ecs_entity_t result = ecs_import(world, action, module); + const char *end = skip_scope(ptr, ctx); + ecs_os_strncpy(params, ptr, (ecs_size_t)(end - ptr)); + params[end - ptr] = '\0'; - if (import_func != module_name) { - ecs_os_free(import_func); + ptr = end; + } else { + *bptr = ch; + bptr ++; + ptr ++; + } } - if (module != module_name) { - ecs_os_free(module); - } + *bptr = '\0'; - ecs_os_free(library_filename); + if (!ch) { + ecs_meta_error(ctx, ptr, "unexpected end of token"); + goto error; + } - return result; + return ptr; +error: + return NULL; } -ecs_entity_t ecs_module_init( - ecs_world_t *world, - const ecs_component_desc_t *desc) +static +const char * meta_open_scope( + const char *ptr, + meta_parse_ctx_t *ctx) { - ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_poly_assert(world, ecs_world_t); + /* Skip initial whitespaces */ + ptr = ecs_parse_eol_and_whitespace(ptr); - const char *name = desc->entity.name; + /* Is this the start of the type definition? */ + if (ctx->desc == ptr) { + if (*ptr != '{') { + ecs_meta_error(ctx, ptr, "missing '{' in struct definition"); + goto error; + } - char *module_path = ecs_module_path_from_c(name); - ecs_entity_t e = ecs_new_from_fullpath(world, module_path); - ecs_set_symbol(world, e, module_path); - ecs_os_free(module_path); + ptr ++; + ptr = ecs_parse_eol_and_whitespace(ptr); + } - ecs_component_desc_t private_desc = *desc; - private_desc.entity.entity = e; - private_desc.entity.name = NULL; + /* Is this the end of the type definition? */ + if (!*ptr) { + ecs_meta_error(ctx, ptr, "missing '}' at end of struct definition"); + goto error; + } - ecs_entity_t result = ecs_component_init(world, &private_desc); - ecs_assert(result != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(result == e, ECS_INTERNAL_ERROR, NULL); + /* Is this the end of the type definition? */ + if (*ptr == '}') { + ptr = ecs_parse_eol_and_whitespace(ptr + 1); + if (*ptr) { + ecs_meta_error(ctx, ptr, + "stray characters after struct definition"); + goto error; + } + return NULL; + } - return result; + return ptr; +error: + return NULL; } -#endif -#ifndef FLECS_META_PRIVATE_H -#define FLECS_META_PRIVATE_H - +static +const char* meta_parse_constant( + const char *ptr, + meta_constant_t *token, + meta_parse_ctx_t *ctx) +{ + ptr = meta_open_scope(ptr, ctx); + if (!ptr) { + return NULL; + } -#ifdef FLECS_META + token->is_value_set = false; -void ecs_meta_type_serialized_init( - ecs_iter_t *it); + /* Parse token, constant identifier */ + ptr = parse_c_identifier(ptr, token->name, NULL, ctx); + ptr = ecs_parse_eol_and_whitespace(ptr); -void ecs_meta_dtor_serialized( - EcsMetaTypeSerialized *ptr); + /* Explicit value assignment */ + if (*ptr == '=') { + int64_t value = 0; + ptr = parse_c_digit(ptr + 1, &value); + token->value = value; + token->is_value_set = true; + } -#endif - -#endif + /* Expect a ',' or '}' */ + if (*ptr != ',' && *ptr != '}') { + ecs_meta_error(ctx, ptr, "missing , after enum constant"); + goto error; + } -#ifdef FLECS_META + if (*ptr == ',') { + return ptr + 1; + } else { + return ptr; + } +error: + return NULL; +} -ecs_entity_t ecs_enum_init( - ecs_world_t *world, - const ecs_enum_desc_t *desc) +static +const char* meta_parse_type( + const char *ptr, + meta_type_t *token, + meta_parse_ctx_t *ctx) { - ecs_entity_t t = ecs_entity_init(world, &desc->entity); - if (!t) { - return 0; - } + token->is_ptr = false; + token->is_const = false; - ecs_add(world, t, EcsEnum); + ptr = ecs_parse_eol_and_whitespace(ptr); - ecs_entity_t old_scope = ecs_set_scope(world, t); + /* Parse token, expect type identifier or ECS_PROPERTY */ + ptr = parse_c_identifier(ptr, token->type, token->params, ctx); + if (!ptr) { + goto error; + } - int i; - for (i = 0; i < ECS_MEMBER_DESC_CACHE_SIZE; i ++) { - const ecs_enum_constant_t *m_desc = &desc->constants[i]; - if (!m_desc->name) { - break; - } + if (!strcmp(token->type, "ECS_PRIVATE")) { + /* Members from this point are not stored in metadata */ + ptr += ecs_os_strlen(ptr); + goto done; + } - ecs_entity_t c = ecs_entity_init(world, &(ecs_entity_desc_t) { - .name = m_desc->name - }); + /* If token is const, set const flag and continue parsing type */ + if (!strcmp(token->type, "const")) { + token->is_const = true; - if (!m_desc->value) { - ecs_add_id(world, c, EcsConstant); - } else { - ecs_set_pair_object(world, c, EcsConstant, ecs_i32_t, - {m_desc->value}); - } + /* Parse type after const */ + ptr = parse_c_identifier(ptr + 1, token->type, token->params, ctx); } - ecs_set_scope(world, old_scope); - - if (i == 0) { - ecs_err("enum '%s' has no constants", ecs_get_name(world, t)); - ecs_delete(world, t); - return 0; + /* Check if type is a pointer */ + ptr = ecs_parse_eol_and_whitespace(ptr); + if (*ptr == '*') { + token->is_ptr = true; + ptr ++; } - return t; +done: + return ptr; +error: + return NULL; } -ecs_entity_t ecs_bitmask_init( - ecs_world_t *world, - const ecs_bitmask_desc_t *desc) +static +const char* meta_parse_member( + const char *ptr, + meta_member_t *token, + meta_parse_ctx_t *ctx) { - ecs_entity_t t = ecs_entity_init(world, &desc->entity); - if (!t) { - return 0; + ptr = meta_open_scope(ptr, ctx); + if (!ptr) { + return NULL; } - ecs_add(world, t, EcsBitmask); + token->count = 1; + token->is_partial = false; - ecs_entity_t old_scope = ecs_set_scope(world, t); + /* Parse member type */ + ptr = meta_parse_type(ptr, &token->type, ctx); + if (!ptr) { + token->is_partial = true; + goto error; + } - int i; - for (i = 0; i < ECS_MEMBER_DESC_CACHE_SIZE; i ++) { - const ecs_bitmask_constant_t *m_desc = &desc->constants[i]; - if (!m_desc->name) { - break; - } + /* Next token is the identifier */ + ptr = parse_c_identifier(ptr, token->name, NULL, ctx); + if (!ptr) { + goto error; + } - ecs_entity_t c = ecs_entity_init(world, &(ecs_entity_desc_t) { - .name = m_desc->name - }); + /* Skip whitespace between member and [ or ; */ + ptr = ecs_parse_eol_and_whitespace(ptr); - if (!m_desc->value) { - ecs_add_id(world, c, EcsConstant); - } else { - ecs_set_pair_object(world, c, EcsConstant, ecs_u32_t, - {m_desc->value}); + /* Check if this is an array */ + char *array_start = strchr(token->name, '['); + if (!array_start) { + /* If the [ was separated by a space, it will not be parsed as part of + * the name */ + if (*ptr == '[') { + array_start = (char*)ptr; /* safe, will not be modified */ } } - ecs_set_scope(world, old_scope); + if (array_start) { + /* Check if the [ matches with a ] */ + char *array_end = strchr(array_start, ']'); + if (!array_end) { + ecs_meta_error(ctx, ptr, "missing ']'"); + goto error; - if (i == 0) { - ecs_err("bitmask '%s' has no constants", ecs_get_name(world, t)); - ecs_delete(world, t); - return 0; - } + } else if (array_end - array_start == 0) { + ecs_meta_error(ctx, ptr, "dynamic size arrays are not supported"); + goto error; + } - return t; -} + token->count = atoi(array_start + 1); -ecs_entity_t ecs_array_init( - ecs_world_t *world, - const ecs_array_desc_t *desc) -{ - ecs_entity_t t = ecs_entity_init(world, &desc->entity); - if (!t) { - return 0; + if (array_start == ptr) { + /* If [ was found after name, continue parsing after ] */ + ptr = array_end + 1; + } else { + /* If [ was fonud in name, replace it with 0 terminator */ + array_start[0] = '\0'; + } } - ecs_set(world, t, EcsArray, { - .type = desc->type, - .count = desc->count - }); + /* Expect a ; */ + if (*ptr != ';') { + ecs_meta_error(ctx, ptr, "missing ; after member declaration"); + goto error; + } - return t; + return ptr + 1; +error: + return NULL; } -ecs_entity_t ecs_vector_init( - ecs_world_t *world, - const ecs_vector_desc_t *desc) +static +int meta_parse_desc( + const char *ptr, + meta_params_t *token, + meta_parse_ctx_t *ctx) { - ecs_entity_t t = ecs_entity_init(world, &desc->entity); - if (!t) { - return 0; - } + token->is_key_value = false; + token->is_fixed_size = false; - ecs_set(world, t, EcsVector, { - .type = desc->type - }); + ptr = ecs_parse_eol_and_whitespace(ptr); + if (*ptr != '(' && *ptr != '<') { + ecs_meta_error(ctx, ptr, + "expected '(' at start of collection definition"); + goto error; + } - return t; -} + ptr ++; -ecs_entity_t ecs_struct_init( - ecs_world_t *world, - const ecs_struct_desc_t *desc) -{ - ecs_entity_t t = ecs_entity_init(world, &desc->entity); - if (!t) { - return 0; + /* Parse type identifier */ + ptr = meta_parse_type(ptr, &token->type, ctx); + if (!ptr) { + goto error; } - ecs_entity_t old_scope = ecs_set_scope(world, t); + ptr = ecs_parse_eol_and_whitespace(ptr); - int i; - for (i = 0; i < ECS_MEMBER_DESC_CACHE_SIZE; i ++) { - const ecs_member_t *m_desc = &desc->members[i]; - if (!m_desc->type) { - break; - } + /* If next token is a ',' the first type was a key type */ + if (*ptr == ',') { + ptr = ecs_parse_eol_and_whitespace(ptr + 1); + + if (isdigit(*ptr)) { + int64_t value; + ptr = parse_c_digit(ptr, &value); + if (!ptr) { + goto error; + } - if (!m_desc->name) { - ecs_err("member %d of struct '%s' does not have a name", i, - ecs_get_name(world, t)); - ecs_delete(world, t); - return 0; - } + token->count = value; + token->is_fixed_size = true; + } else { + token->key_type = token->type; - ecs_entity_t m = ecs_entity_init(world, &(ecs_entity_desc_t) { - .name = m_desc->name - }); + /* Parse element type */ + ptr = meta_parse_type(ptr, &token->type, ctx); + ptr = ecs_parse_eol_and_whitespace(ptr); - ecs_set(world, m, EcsMember, { - .type = m_desc->type, - .count = m_desc->count - }); + token->is_key_value = true; + } } - ecs_set_scope(world, old_scope); - - if (i == 0) { - ecs_err("struct '%s' has no members", ecs_get_name(world, t)); - ecs_delete(world, t); - return 0; + if (*ptr != ')' && *ptr != '>') { + ecs_meta_error(ctx, ptr, + "expected ')' at end of collection definition"); + goto error; } - return t; + return 0; +error: + return -1; } -#endif - -#ifdef FLECS_META - static -ecs_vector_t* serialize_type( +ecs_entity_t meta_lookup( ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops); + meta_type_t *token, + const char *ptr, + int64_t count, + meta_parse_ctx_t *ctx); static -ecs_meta_type_op_kind_t primitive_to_op_kind(ecs_primitive_kind_t kind) { - return EcsOpPrimitive + kind; -} +ecs_entity_t meta_lookup_array( + ecs_world_t *world, + ecs_entity_t e, + const char *params_decl, + meta_parse_ctx_t *ctx) +{ + meta_parse_ctx_t param_ctx = { + .name = ctx->name, + .desc = params_decl + }; -static -ecs_size_t type_size(ecs_world_t *world, ecs_entity_t type) { - const EcsComponent *comp = ecs_get(world, type, EcsComponent); - ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); - return comp->size; -} + meta_params_t params; + if (meta_parse_desc(params_decl, ¶ms, ¶m_ctx)) { + goto error; + } + if (!params.is_fixed_size) { + ecs_meta_error(ctx, params_decl, "missing size for array"); + goto error; + } -static -ecs_meta_type_op_t* ops_add(ecs_vector_t **ops, ecs_meta_type_op_kind_t kind) { - ecs_meta_type_op_t *op = ecs_vector_add(ops, ecs_meta_type_op_t); - op->kind = kind; - op->offset = 0; - op->count = 1; - op->op_count = 1; - op->size = 0; - op->name = NULL; - op->members = NULL; - op->type = 0; - return op; -} + if (!params.count) { + ecs_meta_error(ctx, params_decl, "invalid array size"); + goto error; + } -static -ecs_meta_type_op_t* ops_get(ecs_vector_t *ops, int32_t index) { - ecs_meta_type_op_t* op = ecs_vector_get(ops, ecs_meta_type_op_t, index); - ecs_assert(op != NULL, ECS_INTERNAL_ERROR, NULL); - return op; -} + ecs_entity_t element_type = ecs_lookup_symbol(world, params.type.type, true); + if (!element_type) { + ecs_meta_error(ctx, params_decl, "unknown element type '%s'", + params.type.type); + } -static -ecs_vector_t* serialize_primitive( - ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops) -{ - const EcsPrimitive *ptr = ecs_get(world, type, EcsPrimitive); - ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); + if (!e) { + e = ecs_set(world, 0, EcsMetaType, { EcsArrayType }); + } - ecs_meta_type_op_t *op = ops_add(&ops, primitive_to_op_kind(ptr->kind)); - op->offset = offset, - op->type = type; - op->size = type_size(world, type); + ecs_assert(params.count <= INT32_MAX, ECS_INVALID_PARAMETER, NULL); - return ops; + return ecs_set(world, e, EcsArray, { element_type, (int32_t)params.count }); +error: + return 0; } static -ecs_vector_t* serialize_enum( +ecs_entity_t meta_lookup_vector( ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops) + ecs_entity_t e, + const char *params_decl, + meta_parse_ctx_t *ctx) { - (void)world; - - ecs_meta_type_op_t *op = ops_add(&ops, EcsOpEnum); - op->offset = offset, - op->type = type; - op->size = ECS_SIZEOF(ecs_i32_t); + meta_parse_ctx_t param_ctx = { + .name = ctx->name, + .desc = params_decl + }; - return ops; -} + meta_params_t params; + if (meta_parse_desc(params_decl, ¶ms, ¶m_ctx)) { + goto error; + } -static -ecs_vector_t* serialize_bitmask( - ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops) -{ - (void)world; - - ecs_meta_type_op_t *op = ops_add(&ops, EcsOpBitmask); - op->offset = offset, - op->type = type; - op->size = ECS_SIZEOF(ecs_u32_t); + if (params.is_key_value) { + ecs_meta_error(ctx, params_decl, + "unexpected key value parameters for vector"); + goto error; + } - return ops; + ecs_entity_t element_type = meta_lookup( + world, ¶ms.type, params_decl, 1, ¶m_ctx); + + if (!e) { + e = ecs_set(world, 0, EcsMetaType, {EcsVectorType}); + } + + return ecs_set(world, e, EcsVector, { element_type }); +error: + return 0; } static -ecs_vector_t* serialize_array( +ecs_entity_t meta_lookup_bitmask( ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops) + ecs_entity_t e, + const char *params_decl, + meta_parse_ctx_t *ctx) { - (void)world; + (void)e; - ecs_meta_type_op_t *op = ops_add(&ops, EcsOpArray); - op->offset = offset; - op->type = type; - op->size = type_size(world, type); + meta_parse_ctx_t param_ctx = { + .name = ctx->name, + .desc = params_decl + }; - return ops; -} + meta_params_t params; + if (meta_parse_desc(params_decl, ¶ms, ¶m_ctx)) { + goto error; + } -static -ecs_vector_t* serialize_array_component( - ecs_world_t *world, - ecs_entity_t type) -{ - const EcsArray *ptr = ecs_get(world, type, EcsArray); - if (!ptr) { - return NULL; /* Should never happen, will trigger internal error */ + if (params.is_key_value) { + ecs_meta_error(ctx, params_decl, + "unexpected key value parameters for bitmask"); + goto error; } - ecs_vector_t *ops = serialize_type(world, ptr->type, 0, NULL); - ecs_assert(ops != NULL, ECS_INTERNAL_ERROR, NULL); + if (params.is_fixed_size) { + ecs_meta_error(ctx, params_decl, + "unexpected size for bitmask"); + goto error; + } - ecs_meta_type_op_t *first = ecs_vector_first(ops, ecs_meta_type_op_t); - first->count = ptr->count; + ecs_entity_t bitmask_type = meta_lookup( + world, ¶ms.type, params_decl, 1, ¶m_ctx); + ecs_assert(bitmask_type != 0, ECS_INVALID_PARAMETER, NULL); - return ops; +#ifndef NDEBUG + /* Make sure this is a bitmask type */ + const EcsMetaType *type_ptr = ecs_get(world, bitmask_type, EcsMetaType); + ecs_assert(type_ptr != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(type_ptr->kind == EcsBitmaskType, ECS_INVALID_PARAMETER, NULL); +#endif + + return bitmask_type; +error: + return 0; } static -ecs_vector_t* serialize_vector( +ecs_entity_t meta_lookup( ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops) + meta_type_t *token, + const char *ptr, + int64_t count, + meta_parse_ctx_t *ctx) { - (void)world; + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(token != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ctx != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_meta_type_op_t *op = ops_add(&ops, EcsOpVector); - op->offset = offset; - op->type = type; - op->size = type_size(world, type); + const char *typename = token->type; + ecs_entity_t type = 0; - return ops; -} + /* Parse vector type */ + if (!token->is_ptr) { + if (!ecs_os_strcmp(typename, "ecs_array")) { + type = meta_lookup_array(world, 0, token->params, ctx); -static -ecs_vector_t* serialize_struct( - ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops) -{ - const EcsStruct *ptr = ecs_get(world, type, EcsStruct); - ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); + } else if (!ecs_os_strcmp(typename, "ecs_vector") || + !ecs_os_strcmp(typename, "flecs::vector")) + { + type = meta_lookup_vector(world, 0, token->params, ctx); - int32_t cur, first = ecs_vector_count(ops); - ecs_meta_type_op_t *op = ops_add(&ops, EcsOpPush); - op->offset = offset; - op->type = type; - op->size = type_size(world, type); + } else if (!ecs_os_strcmp(typename, "flecs::bitmask")) { + type = meta_lookup_bitmask(world, 0, token->params, ctx); - ecs_member_t *members = ecs_vector_first(ptr->members, ecs_member_t); - int32_t i, count = ecs_vector_count(ptr->members); + } else if (!ecs_os_strcmp(typename, "flecs::byte")) { + type = ecs_id(ecs_byte_t); - ecs_hashmap_t *member_index = NULL; - if (count) { - member_index = ecs_os_malloc_t(ecs_hashmap_t); - *member_index = flecs_string_hashmap_new(int32_t); - op->members = member_index; - } + } else if (!ecs_os_strcmp(typename, "char")) { + type = ecs_id(ecs_char_t); - for (i = 0; i < count; i ++) { - ecs_member_t *member = &members[i]; + } else if (!ecs_os_strcmp(typename, "bool") || + !ecs_os_strcmp(typename, "_Bool")) + { + type = ecs_id(ecs_bool_t); - cur = ecs_vector_count(ops); - ops = serialize_type(world, member->type, offset + member->offset, ops); + } else if (!ecs_os_strcmp(typename, "int8_t")) { + type = ecs_id(ecs_i8_t); + } else if (!ecs_os_strcmp(typename, "int16_t")) { + type = ecs_id(ecs_i16_t); + } else if (!ecs_os_strcmp(typename, "int32_t")) { + type = ecs_id(ecs_i32_t); + } else if (!ecs_os_strcmp(typename, "int64_t")) { + type = ecs_id(ecs_i64_t); - op = ops_get(ops, cur); - if (!op->type) { - op->type = member->type; - } + } else if (!ecs_os_strcmp(typename, "uint8_t")) { + type = ecs_id(ecs_u8_t); + } else if (!ecs_os_strcmp(typename, "uint16_t")) { + type = ecs_id(ecs_u16_t); + } else if (!ecs_os_strcmp(typename, "uint32_t")) { + type = ecs_id(ecs_u32_t); + } else if (!ecs_os_strcmp(typename, "uint64_t")) { + type = ecs_id(ecs_u64_t); - if (op->count <= 1) { - op->count = member->count; + } else if (!ecs_os_strcmp(typename, "float")) { + type = ecs_id(ecs_f32_t); + } else if (!ecs_os_strcmp(typename, "double")) { + type = ecs_id(ecs_f64_t); + + } else if (!ecs_os_strcmp(typename, "ecs_entity_t")) { + type = ecs_id(ecs_entity_t); + + } else if (!ecs_os_strcmp(typename, "char*")) { + type = ecs_id(ecs_string_t); + } else { + type = ecs_lookup_symbol(world, typename, true); + } + } else { + if (!ecs_os_strcmp(typename, "char")) { + typename = "flecs.meta.string"; + } else + if (token->is_ptr) { + typename = "flecs.meta.uptr"; + } else + if (!ecs_os_strcmp(typename, "char*") || + !ecs_os_strcmp(typename, "flecs::string")) + { + typename = "flecs.meta.string"; } - - const char *member_name = member->name; - op->name = member_name; - op->op_count = ecs_vector_count(ops) - cur; - ecs_size_t len = ecs_os_strlen(member_name); + type = ecs_lookup_symbol(world, typename, true); + } - ecs_hashed_string_t key = ecs_get_hashed_string(member_name, len, 0); - flecs_hashmap_result_t hmr = flecs_hashmap_ensure( - *member_index, &key, int32_t); - *((int32_t*)hmr.value) = cur - first - 1; + if (count != 1) { + ecs_assert(count <= INT32_MAX, ECS_INVALID_PARAMETER, NULL); + + type = ecs_set(world, ecs_set(world, 0, + EcsMetaType, {EcsArrayType}), + EcsArray, {type, (int32_t)count}); } - ops_add(&ops, EcsOpPop); - ops_get(ops, first)->op_count = ecs_vector_count(ops) - first; + if (!type) { + ecs_meta_error(ctx, ptr, "unknown type '%s'", typename); + goto error; + } - return ops; + return type; +error: + return 0; } static -ecs_vector_t* serialize_type( +int meta_parse_struct( ecs_world_t *world, - ecs_entity_t type, - ecs_size_t offset, - ecs_vector_t *ops) + ecs_entity_t t, + const char *desc) { - const EcsMetaType *ptr = ecs_get(world, type, EcsMetaType); - if (!ptr) { - char *path = ecs_get_fullpath(world, type); - ecs_err("missing EcsMetaType for type %s'", path); - ecs_os_free(path); - return NULL; - } - - switch(ptr->kind) { - case EcsPrimitiveType: - ops = serialize_primitive(world, type, offset, ops); - break; + const char *ptr = desc; + const char *name = ecs_get_name(world, t); - case EcsEnumType: - ops = serialize_enum(world, type, offset, ops); - break; + meta_member_t token; + meta_parse_ctx_t ctx = { + .name = name, + .desc = ptr + }; - case EcsBitmaskType: - ops = serialize_bitmask(world, type, offset, ops); - break; + ecs_entity_t old_scope = ecs_set_scope(world, t); - case EcsStructType: - ops = serialize_struct(world, type, offset, ops); - break; + while ((ptr = meta_parse_member(ptr, &token, &ctx)) && ptr[0]) { + ecs_entity_t m = ecs_entity_init(world, &(ecs_entity_desc_t) { + .name = token.name + }); - case EcsArrayType: - ops = serialize_array(world, type, offset, ops); - break; + ecs_entity_t type = meta_lookup( + world, &token.type, ptr, 1, &ctx); + if (!type) { + goto error; + } - case EcsVectorType: - ops = serialize_vector(world, type, offset, ops); - break; + ecs_set(world, m, EcsMember, { + .type = type, + .count = (ecs_size_t)token.count + }); } - return ops; + ecs_set_scope(world, old_scope); + + return 0; +error: + return -1; } static -ecs_vector_t* serialize_component( +int meta_parse_constants( ecs_world_t *world, - ecs_entity_t type) + ecs_entity_t t, + const char *desc, + bool is_bitmask) { - const EcsMetaType *ptr = ecs_get(world, type, EcsMetaType); - if (!ptr) { - char *path = ecs_get_fullpath(world, type); - ecs_err("missing EcsMetaType for type %s'", path); - ecs_os_free(path); - return NULL; - } + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(t != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(desc != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_vector_t *ops = NULL; + const char *ptr = desc; + const char *name = ecs_get_name(world, t); - switch(ptr->kind) { - case EcsArrayType: - ops = serialize_array_component(world, type); - break; - default: - ops = serialize_type(world, type, 0, NULL); - break; + meta_parse_ctx_t ctx = { + .name = name, + .desc = ptr + }; + + meta_constant_t token; + int64_t last_value = 0; + + ecs_entity_t old_scope = ecs_set_scope(world, t); + + while ((ptr = meta_parse_constant(ptr, &token, &ctx))) { + if (token.is_value_set) { + last_value = token.value; + } else if (is_bitmask) { + ecs_meta_error(&ctx, ptr, + "bitmask requires explicit value assignment"); + goto error; + } + + ecs_entity_t c = ecs_entity_init(world, &(ecs_entity_desc_t) { + .name = token.name + }); + + if (!is_bitmask) { + ecs_set_pair_object(world, c, EcsConstant, ecs_i32_t, + {(ecs_i32_t)last_value}); + } else { + ecs_set_pair_object(world, c, EcsConstant, ecs_u32_t, + {(ecs_u32_t)last_value}); + } + + last_value ++; } - return ops; + ecs_set_scope(world, old_scope); + + return 0; +error: + return -1; } -void ecs_meta_type_serialized_init( - ecs_iter_t *it) +static +int meta_parse_enum( + ecs_world_t *world, + ecs_entity_t t, + const char *desc) { - ecs_world_t *world = it->world; + ecs_add(world, t, EcsEnum); + return meta_parse_constants(world, t, desc, false); +} - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - ecs_vector_t *ops = serialize_component(world, e); - ecs_assert(ops != NULL, ECS_INTERNAL_ERROR, NULL); +static +int meta_parse_bitmask( + ecs_world_t *world, + ecs_entity_t t, + const char *desc) +{ + ecs_add(world, t, EcsBitmask); + return meta_parse_constants(world, t, desc, true); +} - EcsMetaTypeSerialized *ptr = ecs_get_mut( - world, e, EcsMetaTypeSerialized, NULL); - if (ptr->ops) { - ecs_meta_dtor_serialized(ptr); +int ecs_meta_from_desc( + ecs_world_t *world, + ecs_entity_t component, + ecs_type_kind_t kind, + const char *desc) +{ + switch(kind) { + case EcsStructType: + if (meta_parse_struct(world, component, desc)) { + goto error; } - - ptr->ops = ops; + break; + case EcsEnumType: + if (meta_parse_enum(world, component, desc)) { + goto error; + } + break; + case EcsBitmaskType: + if (meta_parse_bitmask(world, component, desc)) { + goto error; + } + break; + default: + break; } + + return 0; +error: + return -1; } #endif -#ifdef FLECS_META +#ifdef FLECS_MODULE -/* EcsMetaTypeSerialized lifecycle */ -void ecs_meta_dtor_serialized( - EcsMetaTypeSerialized *ptr) +char* ecs_module_path_from_c( + const char *c_name) { - int32_t i, count = ecs_vector_count(ptr->ops); - ecs_meta_type_op_t *ops = ecs_vector_first(ptr->ops, ecs_meta_type_op_t); - - for (i = 0; i < count; i ++) { - ecs_meta_type_op_t *op = &ops[i]; - if (op->members) { - flecs_hashmap_free(*op->members); - ecs_os_free(op->members); + ecs_strbuf_t str = ECS_STRBUF_INIT; + const char *ptr; + char ch; + + for (ptr = c_name; (ch = *ptr); ptr++) { + if (isupper(ch)) { + ch = flecs_to_i8(tolower(ch)); + if (ptr != c_name) { + ecs_strbuf_appendstrn(&str, ".", 1); + } } + + ecs_strbuf_appendstrn(&str, &ch, 1); } - ecs_vector_free(ptr->ops); + return ecs_strbuf_get(&str); } -static ECS_COPY(EcsMetaTypeSerialized, dst, src, { - ecs_meta_dtor_serialized(dst); +ecs_entity_t ecs_import( + ecs_world_t *world, + ecs_module_action_t init_action, + const char *module_name) +{ + ecs_assert(!world->is_readonly, ECS_INVALID_WHILE_ITERATING, NULL); - dst->ops = ecs_vector_copy(src->ops, ecs_meta_type_op_t); + ecs_entity_t old_scope = ecs_set_scope(world, 0); + const char *old_name_prefix = world->name_prefix; - int32_t o, count = ecs_vector_count(src->ops); - ecs_meta_type_op_t *ops = ecs_vector_first(src->ops, ecs_meta_type_op_t); + char *path = ecs_module_path_from_c(module_name); + ecs_entity_t e = ecs_lookup_fullpath(world, path); + ecs_os_free(path); - for (o = 0; o < count; o ++) { - ecs_meta_type_op_t *op = &ops[o]; - if (op->members) { - op->members = ecs_os_memdup_t(op->members, ecs_hashmap_t); - *op->members = flecs_hashmap_copy(*op->members); - } - } -}) + if (!e) { + ecs_trace("#[magenta]import#[reset] %s", module_name); + ecs_log_push(); -static ECS_MOVE(EcsMetaTypeSerialized, dst, src, { - ecs_meta_dtor_serialized(dst); - dst->ops = src->ops; - src->ops = NULL; -}) + /* Load module */ + init_action(world); -static ECS_DTOR(EcsMetaTypeSerialized, ptr, { - ecs_meta_dtor_serialized(ptr); -}) + /* Lookup module entity (must be registered by module) */ + e = ecs_lookup_fullpath(world, module_name); + ecs_assert(e != 0, ECS_MODULE_UNDEFINED, module_name); + ecs_log_pop(); + } -/* EcsStruct lifecycle */ + /* Restore to previous state */ + ecs_set_scope(world, old_scope); + world->name_prefix = old_name_prefix; -static void dtor_struct( - EcsStruct *ptr) + return e; +} + +ecs_entity_t ecs_import_from_library( + ecs_world_t *world, + const char *library_name, + const char *module_name) { - ecs_member_t *members = ecs_vector_first(ptr->members, ecs_member_t); - int32_t i, count = ecs_vector_count(ptr->members); - for (i = 0; i < count; i ++) { - ecs_os_free((char*)members[i].name); + ecs_assert(library_name != NULL, ECS_INVALID_PARAMETER, NULL); + + char *import_func = (char*)module_name; /* safe */ + char *module = (char*)module_name; + + if (!ecs_os_has_modules() || !ecs_os_has_dl()) { + ecs_err( + "library loading not supported, set module_to_dl, dlopen, dlclose " + "and dlproc os API callbacks first"); + return 0; } - ecs_vector_free(ptr->members); -} -static ECS_COPY(EcsStruct, dst, src, { - dtor_struct(dst); + /* If no module name is specified, try default naming convention for loading + * the main module from the library */ + if (!import_func) { + import_func = ecs_os_malloc(ecs_os_strlen(library_name) + ECS_SIZEOF("Import")); + ecs_assert(import_func != NULL, ECS_OUT_OF_MEMORY, NULL); + + const char *ptr; + char ch, *bptr = import_func; + bool capitalize = true; + for (ptr = library_name; (ch = *ptr); ptr ++) { + if (ch == '.') { + capitalize = true; + } else { + if (capitalize) { + *bptr = flecs_to_i8(toupper(ch)); + bptr ++; + capitalize = false; + } else { + *bptr = flecs_to_i8(tolower(ch)); + bptr ++; + } + } + } - dst->members = ecs_vector_copy(src->members, ecs_member_t); + *bptr = '\0'; - ecs_member_t *members = ecs_vector_first(dst->members, ecs_member_t); - int32_t m, count = ecs_vector_count(dst->members); + module = ecs_os_strdup(import_func); + ecs_assert(module != NULL, ECS_OUT_OF_MEMORY, NULL); - for (m = 0; m < count; m ++) { - members[m].name = ecs_os_strdup(members[m].name); + ecs_os_strcat(bptr, "Import"); } -}) -static ECS_MOVE(EcsStruct, dst, src, { - dtor_struct(dst); - dst->members = src->members; - src->members = NULL; -}) + char *library_filename = ecs_os_module_to_dl(library_name); + if (!library_filename) { + ecs_err("failed to find library file for '%s'", library_name); + if (module != module_name) { + ecs_os_free(module); + } + return 0; + } else { + ecs_trace("found file '%s' for library '%s'", + library_filename, library_name); + } -static ECS_DTOR(EcsStruct, ptr, { dtor_struct(ptr); }) + ecs_os_dl_t dl = ecs_os_dlopen(library_filename); + if (!dl) { + ecs_err("failed to load library '%s' ('%s')", + library_name, library_filename); + + ecs_os_free(library_filename); + if (module != module_name) { + ecs_os_free(module); + } -/* EcsEnum lifecycle */ + return 0; + } else { + ecs_trace("library '%s' ('%s') loaded", + library_name, library_filename); + } -static void dtor_enum( - EcsEnum *ptr) -{ - ecs_map_iter_t it = ecs_map_iter(ptr->constants); - ecs_enum_constant_t *c; - while ((c = ecs_map_next(&it, ecs_enum_constant_t, NULL))) { - ecs_os_free((char*)c->name); + ecs_module_action_t action = (ecs_module_action_t) + ecs_os_dlproc(dl, import_func); + if (!action) { + ecs_err("failed to load import function %s from library %s", + import_func, library_name); + ecs_os_free(library_filename); + ecs_os_dlclose(dl); + return 0; + } else { + ecs_trace("found import function '%s' in library '%s' for module '%s'", + import_func, library_name, module); } - ecs_map_free(ptr->constants); -} -static ECS_COPY(EcsEnum, dst, src, { - dtor_enum(dst); + /* Do not free id, as it will be stored as the component identifier */ + ecs_entity_t result = ecs_import(world, action, module); - dst->constants = ecs_map_copy(src->constants); - ecs_assert(ecs_map_count(dst->constants) == ecs_map_count(src->constants), - ECS_INTERNAL_ERROR, NULL); + if (import_func != module_name) { + ecs_os_free(import_func); + } - ecs_map_iter_t it = ecs_map_iter(dst->constants); - ecs_enum_constant_t *c; - while ((c = ecs_map_next(&it, ecs_enum_constant_t, NULL))) { - c->name = ecs_os_strdup(c->name); + if (module != module_name) { + ecs_os_free(module); } -}) -static ECS_MOVE(EcsEnum, dst, src, { - dtor_enum(dst); - dst->constants = src->constants; - src->constants = NULL; -}) + ecs_os_free(library_filename); -static ECS_DTOR(EcsEnum, ptr, { dtor_enum(ptr); }) + return result; +} +ecs_entity_t ecs_module_init( + ecs_world_t *world, + const ecs_component_desc_t *desc) +{ + ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_poly_assert(world, ecs_world_t); -/* EcsBitmask lifecycle */ + const char *name = desc->entity.name; -static void dtor_bitmask( - EcsBitmask *ptr) -{ - ecs_map_iter_t it = ecs_map_iter(ptr->constants); - ecs_bitmask_constant_t *c; - while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, NULL))) { - ecs_os_free((char*)c->name); - } - ecs_map_free(ptr->constants); -} + char *module_path = ecs_module_path_from_c(name); + ecs_entity_t e = ecs_new_from_fullpath(world, module_path); + ecs_set_symbol(world, e, module_path); + ecs_os_free(module_path); -static ECS_COPY(EcsBitmask, dst, src, { - dtor_bitmask(dst); + ecs_component_desc_t private_desc = *desc; + private_desc.entity.entity = e; + private_desc.entity.name = NULL; - dst->constants = ecs_map_copy(src->constants); - ecs_assert(ecs_map_count(dst->constants) == ecs_map_count(src->constants), - ECS_INTERNAL_ERROR, NULL); + ecs_entity_t result = ecs_component_init(world, &private_desc); + ecs_assert(result != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(result == e, ECS_INTERNAL_ERROR, NULL); - ecs_map_iter_t it = ecs_map_iter(dst->constants); - ecs_bitmask_constant_t *c; - while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, NULL))) { - c->name = ecs_os_strdup(c->name); - } -}) + return result; +} -static ECS_MOVE(EcsBitmask, dst, src, { - dtor_bitmask(dst); - dst->constants = src->constants; - src->constants = NULL; -}) +#endif -static ECS_DTOR(EcsBitmask, ptr, { dtor_bitmask(ptr); }) +#ifdef FLECS_STATS -/* Type initialization */ +#ifdef FLECS_SYSTEM +#endif -static -int init_type( - ecs_world_t *world, - ecs_entity_t type, - ecs_type_kind_t kind) -{ - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(type != 0, ECS_INTERNAL_ERROR, NULL); +#ifdef FLECS_PIPELINE +#ifndef FLECS_PIPELINE_PRIVATE_H +#define FLECS_PIPELINE_PRIVATE_H - EcsMetaType *meta_type = ecs_get_mut(world, type, EcsMetaType, NULL); - if (meta_type->kind && meta_type->kind != kind) { - ecs_err("type '%s' reregistered with different kind", - ecs_get_name(world, type)); - return -1; - } - meta_type->kind = kind; - ecs_modified(world, type, EcsMetaType); +/** Instruction data for pipeline. + * This type is the element type in the "ops" vector of a pipeline and contains + * information about the set of systems that need to be ran before a merge. */ +typedef struct ecs_pipeline_op_t { + int32_t count; /* Number of systems to run before merge */ +} ecs_pipeline_op_t; - return 0; -} +typedef struct EcsPipelineQuery { + ecs_query_t *query; + ecs_query_t *build_query; + int32_t match_count; + ecs_vector_t *ops; +} EcsPipelineQuery; -static -int init_component( +//////////////////////////////////////////////////////////////////////////////// +//// Pipeline API +//////////////////////////////////////////////////////////////////////////////// + +/** Update a pipeline (internal function). + * Before running a pipeline, it must be updated. During this update phase + * all systems in the pipeline are collected, ordered and sync points are + * inserted where necessary. This operation may only be called when staging is + * disabled. + * + * Because multiple threads may run a pipeline, preparing the pipeline must + * happen synchronously, which is why this function is separate from + * ecs_pipeline_run. Not running the prepare step may cause systems to not get + * ran, or ran in the wrong order. + * + * If 0 is provided for the pipeline id, the default pipeline will be ran (this + * is either the builtin pipeline or the pipeline set with set_pipeline()). + * + * @param world The world. + * @param pipeline The pipeline to run. + * @return The number of elements in the pipeline. + */ +int32_t ecs_pipeline_update( ecs_world_t *world, - ecs_entity_t type, - ecs_size_t size, - ecs_size_t alignment) -{ - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(type != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(alignment != 0, ECS_INTERNAL_ERROR, NULL); + ecs_entity_t pipeline, + bool start_of_frame); - EcsComponent *component = ecs_get_mut(world, type, EcsComponent, NULL); - if (component->size && component->size != size) { - ecs_err("type '%s' reregistered with different size", - ecs_get_name(world, type)); - return -1; - } +//////////////////////////////////////////////////////////////////////////////// +//// Worker API +//////////////////////////////////////////////////////////////////////////////// - if (component->alignment && component->alignment != alignment) { - ecs_err("type '%s' reregistered with different alignment", - ecs_get_name(world, type)); - return -1; - } +void ecs_worker_begin( + ecs_world_t *world); - component->size = size; - component->alignment = alignment; - ecs_modified(world, type, EcsComponent); +bool ecs_worker_sync( + ecs_world_t *world); - return 0; -} +void ecs_worker_end( + ecs_world_t *world); + +void ecs_workers_progress( + ecs_world_t *world, + ecs_entity_t pipeline, + FLECS_FLOAT delta_time); + +#endif +#endif static -void set_struct_member( - ecs_member_t *member, - ecs_entity_t entity, - const char *name, - ecs_entity_t type, - int32_t count) +int32_t t_next( + int32_t t) { - member->member = entity; - member->type = type; - member->count = count; - - if (!count) { - member->count = 1; - } + return (t + 1) % ECS_STAT_WINDOW; +} - ecs_os_strset((char**)&member->name, name); +static +int32_t t_prev( + int32_t t) +{ + return (t - 1 + ECS_STAT_WINDOW) % ECS_STAT_WINDOW; } static -int add_member_to_struct( - ecs_world_t *world, - ecs_entity_t type, - ecs_entity_t member, - EcsMember *m) +void _record_gauge( + ecs_gauge_t *m, + int32_t t, + float value) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(type != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(member != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(m != NULL, ECS_INTERNAL_ERROR, NULL); + m->avg[t] = value; + m->min[t] = value; + m->max[t] = value; +} - const char *name = ecs_get_name(world, member); - if (!name) { - char *path = ecs_get_fullpath(world, type); - ecs_err("member for struct '%s' does not have a name", path); - ecs_os_free(path); - return -1; - } +static +float _record_counter( + ecs_counter_t *m, + int32_t t, + float value) +{ + int32_t tp = t_prev(t); + float prev = m->value[tp]; + m->value[t] = value; + _record_gauge((ecs_gauge_t*)m, t, value - prev); + return value - prev; +} - if (!m->type) { - char *path = ecs_get_fullpath(world, member); - ecs_err("member '%s' does not have a type", path); - ecs_os_free(path); - return -1; - } +/* Macro's to silence conversion warnings without adding casts everywhere */ +#define record_gauge(m, t, value)\ + _record_gauge(m, t, (float)value) - if (ecs_get_typeid(world, m->type) == 0) { - char *path = ecs_get_fullpath(world, member); - char *ent_path = ecs_get_fullpath(world, m->type); - ecs_err("member '%s.type' is '%s' which is not a type", path, ent_path); - ecs_os_free(path); - ecs_os_free(ent_path); - return -1; - } +#define record_counter(m, t, value)\ + _record_counter(m, t, (float)value) - EcsStruct *s = ecs_get_mut(world, type, EcsStruct, NULL); - ecs_assert(s != NULL, ECS_INTERNAL_ERROR, NULL); +static +void print_value( + const char *name, + float value) +{ + ecs_size_t len = ecs_os_strlen(name); + printf("%s: %*s %.2f\n", name, 32 - len, "", (double)value); +} - /* First check if member is already added to struct */ - ecs_member_t *members = ecs_vector_first(s->members, ecs_member_t); - int32_t i, count = ecs_vector_count(s->members); - for (i = 0; i < count; i ++) { - if (members[i].member == member) { - set_struct_member(&members[i], member, name, m->type, m->count); - break; +static +void print_gauge( + const char *name, + int32_t t, + const ecs_gauge_t *m) +{ + print_value(name, m->avg[t]); +} + +static +void print_counter( + const char *name, + int32_t t, + const ecs_counter_t *m) +{ + print_value(name, m->rate.avg[t]); +} + +void ecs_gauge_reduce( + ecs_gauge_t *dst, + int32_t t_dst, + ecs_gauge_t *src, + int32_t t_src) +{ + ecs_assert(dst != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(src != NULL, ECS_INVALID_PARAMETER, NULL); + + bool min_set = false; + dst->min[t_dst] = 0; + dst->avg[t_dst] = 0; + dst->max[t_dst] = 0; + + int32_t i; + for (i = 0; i < ECS_STAT_WINDOW; i ++) { + int32_t t = (t_src + i) % ECS_STAT_WINDOW; + dst->avg[t_dst] += src->avg[t] / (float)ECS_STAT_WINDOW; + if (!min_set || (src->min[t] < dst->min[t_dst])) { + dst->min[t_dst] = src->min[t]; + min_set = true; + } + if ((src->max[t] > dst->max[t_dst])) { + dst->max[t_dst] = src->max[t]; } } +} - /* If member wasn't added yet, add a new element to vector */ - if (i == count) { - ecs_member_t *elem = ecs_vector_add(&s->members, ecs_member_t); - elem->name = NULL; - set_struct_member(elem, member, name, m->type, m->count); +void ecs_get_world_stats( + const ecs_world_t *world, + ecs_world_stats_t *s) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); - /* Reobtain members array in case it was reallocated */ - members = ecs_vector_first(s->members, ecs_member_t); - count ++; + world = ecs_get_world(world); + + int32_t t = s->t = t_next(s->t); + + float delta_world_time = record_counter(&s->world_time_total_raw, t, world->stats.world_time_total_raw); + record_counter(&s->world_time_total, t, world->stats.world_time_total); + record_counter(&s->frame_time_total, t, world->stats.frame_time_total); + record_counter(&s->system_time_total, t, world->stats.system_time_total); + record_counter(&s->merge_time_total, t, world->stats.merge_time_total); + + float delta_frame_count = record_counter(&s->frame_count_total, t, world->stats.frame_count_total); + record_counter(&s->merge_count_total, t, world->stats.merge_count_total); + record_counter(&s->pipeline_build_count_total, t, world->stats.pipeline_build_count_total); + record_counter(&s->systems_ran_frame, t, world->stats.systems_ran_frame); + + if (delta_world_time != 0.0f && delta_frame_count != 0.0f) { + record_gauge( + &s->fps, t, 1.0f / (delta_world_time / (float)delta_frame_count)); + } else { + record_gauge(&s->fps, t, 0); } - /* Compute member offsets and size & alignment of struct */ - ecs_size_t size = 0; - ecs_size_t alignment = 0; + record_gauge(&s->entity_count, t, flecs_sparse_count(world->store.entity_index)); + record_gauge(&s->component_count, t, ecs_count_id(world, ecs_id(EcsComponent))); + record_gauge(&s->query_count, t, flecs_sparse_count(world->queries)); + record_gauge(&s->system_count, t, ecs_count_id(world, ecs_id(EcsSystem))); - for (i = 0; i < count; i ++) { - ecs_member_t *elem = &members[i]; + record_counter(&s->new_count, t, world->new_count); + record_counter(&s->bulk_new_count, t, world->bulk_new_count); + record_counter(&s->delete_count, t, world->delete_count); + record_counter(&s->clear_count, t, world->clear_count); + record_counter(&s->add_count, t, world->add_count); + record_counter(&s->remove_count, t, world->remove_count); + record_counter(&s->set_count, t, world->set_count); + record_counter(&s->discard_count, t, world->discard_count); - ecs_assert(elem->name != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(elem->type != 0, ECS_INTERNAL_ERROR, NULL); + /* Compute table statistics */ + int32_t empty_table_count = 0; + int32_t singleton_table_count = 0; + int32_t matched_table_count = 0, matched_entity_count = 0; - /* Get component of member type to get its size & alignment */ - const EcsComponent *mbr_comp = ecs_get(world, elem->type, EcsComponent); - if (!mbr_comp) { - char *path = ecs_get_fullpath(world, member); - ecs_err("member '%s' is not a type", path); - ecs_os_free(path); - return -1; + int32_t i, count = flecs_sparse_count(world->store.tables); + for (i = 0; i < count; i ++) { + ecs_table_t *table = flecs_sparse_get_dense(world->store.tables, ecs_table_t, i); + int32_t entity_count = ecs_table_count(table); + + if (!entity_count) { + empty_table_count ++; } - ecs_size_t member_size = mbr_comp->size; - ecs_size_t member_alignment = mbr_comp->alignment; + /* Singleton tables are tables that have just one entity that also has + * itself in the table type. */ + if (entity_count == 1) { + ecs_entity_t *entities = ecs_vector_first( + table->storage.entities, ecs_entity_t); + if (ecs_type_has_id(world, table->type, entities[0], false)) { + singleton_table_count ++; + } + } - if (!member_size || !member_alignment) { - char *path = ecs_get_fullpath(world, member); - ecs_err("member '%s' has 0 size/alignment"); - ecs_os_free(path); - return -1; + /* If this table matches with queries and is not empty, increase the + * matched table & matched entity count. These statistics can be used to + * compute actual fragmentation ratio for queries. */ + int32_t queries_matched = ecs_vector_count(table->queries); + if (queries_matched && entity_count) { + matched_table_count ++; + matched_entity_count += entity_count; } + } - member_size *= elem->count; - size = ECS_ALIGN(size, member_alignment); - elem->size = member_size; - elem->offset = size; + record_gauge(&s->matched_table_count, t, matched_table_count); + record_gauge(&s->matched_entity_count, t, matched_entity_count); + + record_gauge(&s->table_count, t, count); + record_gauge(&s->empty_table_count, t, empty_table_count); + record_gauge(&s->singleton_table_count, t, singleton_table_count); +} - size += member_size; +void ecs_get_query_stats( + const ecs_world_t *world, + const ecs_query_t *query, + ecs_query_stats_t *s) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(query != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); + (void)world; - if (member_alignment > alignment) { - alignment = member_alignment; + int32_t t = s->t = t_next(s->t); + + int32_t i, entity_count = 0, count = ecs_query_table_count(query); + ecs_query_table_match_t *matched_tables = ecs_vector_first( + query->cache.tables, ecs_query_table_match_t); + for (i = 0; i < count; i ++) { + ecs_query_table_match_t *matched = &matched_tables[i]; + if (matched->table) { + entity_count += ecs_table_count(matched->table); } } - if (size == 0) { - ecs_err("struct '%s' has 0 size", ecs_get_name(world, type)); - return -1; - } + record_gauge(&s->matched_table_count, t, count); + record_gauge(&s->matched_empty_table_count, t, + ecs_query_empty_table_count(query)); + record_gauge(&s->matched_entity_count, t, entity_count); +} - if (alignment == 0) { - ecs_err("struct '%s' has 0 alignment", ecs_get_name(world, type)); - return -1; +#ifdef FLECS_SYSTEM +bool ecs_get_system_stats( + const ecs_world_t *world, + ecs_entity_t system, + ecs_system_stats_t *s) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(system != 0, ECS_INVALID_PARAMETER, NULL); + + world = ecs_get_world(world); + + const EcsSystem *ptr = ecs_get(world, system, EcsSystem); + if (!ptr) { + return false; } - /* Align struct size to struct alignment */ - size = ECS_ALIGN(size, alignment); + ecs_get_query_stats(world, ptr->query, &s->query_stats); + int32_t t = s->query_stats.t; - ecs_modified(world, type, EcsStruct); + record_counter(&s->time_spent, t, ptr->time_spent); + record_counter(&s->invoke_count, t, ptr->invoke_count); + record_gauge(&s->active, t, !ecs_has_id(world, system, EcsInactive)); + record_gauge(&s->enabled, t, !ecs_has_id(world, system, EcsDisabled)); - /* Overwrite component size & alignment */ - if (type != ecs_id(EcsComponent)) { - EcsComponent *comp = ecs_get_mut(world, type, EcsComponent, NULL); - comp->size = size; - comp->alignment = alignment; - ecs_modified(world, type, EcsComponent); - } + return true; +} +#endif - /* Do this last as it triggers the update of EcsMetaTypeSerialized */ - if (init_type(world, type, EcsStructType)) { - return -1; - } - /* If current struct is also a member, assign to itself */ - if (ecs_has(world, type, EcsMember)) { - EcsMember *type_mbr = ecs_get_mut(world, type, EcsMember, NULL); - ecs_assert(type_mbr != NULL, ECS_INTERNAL_ERROR, NULL); +#ifdef FLECS_PIPELINE - type_mbr->type = type; - type_mbr->count = 1; +static ecs_system_stats_t* get_system_stats( + ecs_map_t *systems, + ecs_entity_t system) +{ + ecs_assert(systems != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(system != 0, ECS_INVALID_PARAMETER, NULL); - ecs_modified(world, type, EcsMember); + ecs_system_stats_t *s = ecs_map_get(systems, ecs_system_stats_t, system); + if (!s) { + ecs_system_stats_t stats; + memset(&stats, 0, sizeof(ecs_system_stats_t)); + ecs_map_set(systems, system, &stats); + s = ecs_map_get(systems, ecs_system_stats_t, system); + ecs_assert(s != NULL, ECS_INTERNAL_ERROR, NULL); } - return 0; + return s; } -static -int add_constant_to_enum( - ecs_world_t *world, - ecs_entity_t type, - ecs_entity_t e, - ecs_id_t constant_id) +bool ecs_get_pipeline_stats( + ecs_world_t *stage, + ecs_entity_t pipeline, + ecs_pipeline_stats_t *s) { - EcsEnum *ptr = ecs_get_mut(world, type, EcsEnum, NULL); - - /* Remove constant from map if it was already added */ - ecs_map_iter_t it = ecs_map_iter(ptr->constants); - ecs_enum_constant_t *c; - ecs_map_key_t key; - while ((c = ecs_map_next(&it, ecs_enum_constant_t, &key))) { - if (c->constant == e) { - ecs_os_free((char*)c->name); - ecs_map_remove(ptr->constants, key); - } - } - - /* Check if constant sets explicit value */ - int32_t value = 0; - bool value_set = false; - if (ecs_id_is_pair(constant_id)) { - if (ecs_pair_object(world, constant_id) != ecs_id(ecs_i32_t)) { - char *path = ecs_get_fullpath(world, e); - ecs_err("expected i32 type for enum constant '%s'", path); - ecs_os_free(path); - return -1; - } + ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(pipeline != 0, ECS_INVALID_PARAMETER, NULL); - const int32_t *value_ptr = ecs_get_pair_object( - world, e, EcsConstant, ecs_i32_t); - ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - value = *value_ptr; - value_set = true; - } + const ecs_world_t *world = ecs_get_world(stage); - /* Make sure constant value doesn't conflict if set / find the next value */ - it = ecs_map_iter(ptr->constants); - while ((c = ecs_map_next(&it, ecs_enum_constant_t, &key))) { - if (value_set) { - if (c->value == value) { - char *path = ecs_get_fullpath(world, e); - ecs_err("conflicting constant value for '%s' (other is '%s')", - path, c->name); - ecs_os_free(path); - return -1; - } - } else { - if (c->value >= value) { - value = c->value + 1; - } - } + const EcsPipelineQuery *pq = ecs_get(world, pipeline, EcsPipelineQuery); + if (!pq) { + return false; } - if (!ptr->constants) { - ptr->constants = ecs_map_new(ecs_enum_constant_t, 1); + /* First find out how many systems are matched by the pipeline */ + ecs_iter_t it = ecs_query_iter(stage, pq->query); + int32_t count = 0; + while (ecs_query_next(&it)) { + count += it.count; } - c = ecs_map_ensure(ptr->constants, ecs_enum_constant_t, value); - c->name = ecs_os_strdup(ecs_get_name(world, e)); - c->value = value; - c->constant = e; + if (!s->system_stats) { + s->system_stats = ecs_map_new(ecs_system_stats_t, count); + } - ecs_i32_t *cptr = ecs_get_mut_pair_object( - world, e, EcsConstant, ecs_i32_t, NULL); - ecs_assert(cptr != NULL, ECS_INTERNAL_ERROR, NULL); - cptr[0] = value; + /* Also count synchronization points */ + ecs_vector_t *ops = pq->ops; + ecs_pipeline_op_t *op = ecs_vector_first(ops, ecs_pipeline_op_t); + ecs_pipeline_op_t *op_last = ecs_vector_last(ops, ecs_pipeline_op_t); + count += ecs_vector_count(ops); - return 0; -} + /* Make sure vector is large enough to store all systems & sync points */ + ecs_vector_set_count(&s->systems, ecs_entity_t, count - 1); + ecs_entity_t *systems = ecs_vector_first(s->systems, ecs_entity_t); -static -int add_constant_to_bitmask( - ecs_world_t *world, - ecs_entity_t type, - ecs_entity_t e, - ecs_id_t constant_id) -{ - EcsBitmask *ptr = ecs_get_mut(world, type, EcsBitmask, NULL); + /* Populate systems vector, keep track of sync points */ + it = ecs_query_iter(stage, pq->query); - /* Remove constant from map if it was already added */ - ecs_map_iter_t it = ecs_map_iter(ptr->constants); - ecs_bitmask_constant_t *c; - ecs_map_key_t key; - while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { - if (c->constant == e) { - ecs_os_free((char*)c->name); - ecs_map_remove(ptr->constants, key); - } - } + int32_t i_system = 0, ran_since_merge = 0; + while (ecs_query_next(&it)) { + int32_t i; + for (i = 0; i < it.count; i ++) { + systems[i_system ++] = it.entities[i]; + ran_since_merge ++; + if (op != op_last && ran_since_merge == op->count) { + ran_since_merge = 0; + op++; + systems[i_system ++] = 0; /* 0 indicates a merge point */ + } - /* Check if constant sets explicit value */ - uint32_t value = 1; - if (ecs_id_is_pair(constant_id)) { - if (ecs_pair_object(world, constant_id) != ecs_id(ecs_u32_t)) { - char *path = ecs_get_fullpath(world, e); - ecs_err("expected u32 type for bitmask constant '%s'", path); - ecs_os_free(path); - return -1; + ecs_system_stats_t *sys_stats = get_system_stats( + s->system_stats, it.entities[i]); + ecs_get_system_stats(world, it.entities[i], sys_stats); } - - const uint32_t *value_ptr = ecs_get_pair_object( - world, e, EcsConstant, ecs_u32_t); - ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - value = *value_ptr; - } else { - value = 1u << (ecs_u32_t)ecs_map_count(ptr->constants); } - /* Make sure constant value doesn't conflict */ - it = ecs_map_iter(ptr->constants); - while ((c = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { - if (c->value == value) { - char *path = ecs_get_fullpath(world, e); - ecs_err("conflicting constant value for '%s' (other is '%s')", - path, c->name); - ecs_os_free(path); - return -1; - } - } + ecs_assert(i_system == (count - 1), ECS_INTERNAL_ERROR, NULL); - if (!ptr->constants) { - ptr->constants = ecs_map_new(ecs_bitmask_constant_t, 1); - } + return true; +} +#endif - c = ecs_map_ensure(ptr->constants, ecs_bitmask_constant_t, value); - c->name = ecs_os_strdup(ecs_get_name(world, e)); - c->value = value; - c->constant = e; +void ecs_dump_world_stats( + const ecs_world_t *world, + const ecs_world_stats_t *s) +{ + int32_t t = s->t; - ecs_u32_t *cptr = ecs_get_mut_pair_object( - world, e, EcsConstant, ecs_u32_t, NULL); - ecs_assert(cptr != NULL, ECS_INTERNAL_ERROR, NULL); - cptr[0] = value; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); - return 0; + world = ecs_get_world(world); + + print_counter("Frame", t, &s->frame_count_total); + printf("-------------------------------------\n"); + print_counter("pipeline rebuilds", t, &s->pipeline_build_count_total); + print_counter("systems ran last frame", t, &s->systems_ran_frame); + printf("\n"); + print_value("target FPS", world->stats.target_fps); + print_value("time scale", world->stats.time_scale); + printf("\n"); + print_gauge("actual FPS", t, &s->fps); + print_counter("frame time", t, &s->frame_time_total); + print_counter("system time", t, &s->system_time_total); + print_counter("merge time", t, &s->merge_time_total); + print_counter("simulation time elapsed", t, &s->world_time_total); + printf("\n"); + print_gauge("entity count", t, &s->entity_count); + print_gauge("component count", t, &s->component_count); + print_gauge("query count", t, &s->query_count); + print_gauge("system count", t, &s->system_count); + print_gauge("table count", t, &s->table_count); + print_gauge("singleton table count", t, &s->singleton_table_count); + print_gauge("empty table count", t, &s->empty_table_count); + printf("\n"); + print_counter("deferred new operations", t, &s->new_count); + print_counter("deferred bulk_new operations", t, &s->bulk_new_count); + print_counter("deferred delete operations", t, &s->delete_count); + print_counter("deferred clear operations", t, &s->clear_count); + print_counter("deferred add operations", t, &s->add_count); + print_counter("deferred remove operations", t, &s->remove_count); + print_counter("deferred set operations", t, &s->set_count); + print_counter("discarded operations", t, &s->discard_count); + printf("\n"); } -static -void set_primitive(ecs_iter_t *it) { - ecs_world_t *world = it->world; - EcsPrimitive *type = ecs_term(it, EcsPrimitive, 1); +#endif - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - switch(type->kind) { - case EcsBool: - init_component(world, e, - ECS_SIZEOF(bool), ECS_ALIGNOF(bool)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsChar: - init_component(world, e, - ECS_SIZEOF(char), ECS_ALIGNOF(char)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsByte: - init_component(world, e, - ECS_SIZEOF(bool), ECS_ALIGNOF(bool)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsU8: - init_component(world, e, - ECS_SIZEOF(uint8_t), ECS_ALIGNOF(uint8_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsU16: - init_component(world, e, - ECS_SIZEOF(uint16_t), ECS_ALIGNOF(uint16_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsU32: - init_component(world, e, - ECS_SIZEOF(uint32_t), ECS_ALIGNOF(uint32_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsU64: - init_component(world, e, - ECS_SIZEOF(uint64_t), ECS_ALIGNOF(uint64_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsI8: - init_component(world, e, - ECS_SIZEOF(int8_t), ECS_ALIGNOF(int8_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsI16: - init_component(world, e, - ECS_SIZEOF(int16_t), ECS_ALIGNOF(int16_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsI32: - init_component(world, e, - ECS_SIZEOF(int32_t), ECS_ALIGNOF(int32_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsI64: - init_component(world, e, - ECS_SIZEOF(int64_t), ECS_ALIGNOF(int64_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsF32: - init_component(world, e, - ECS_SIZEOF(float), ECS_ALIGNOF(float)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsF64: - init_component(world, e, - ECS_SIZEOF(double), ECS_ALIGNOF(double)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsUPtr: - init_component(world, e, - ECS_SIZEOF(uintptr_t), ECS_ALIGNOF(uintptr_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsIPtr: - init_component(world, e, - ECS_SIZEOF(intptr_t), ECS_ALIGNOF(intptr_t)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsString: - init_component(world, e, - ECS_SIZEOF(char*), ECS_ALIGNOF(char*)); - init_type(world, e, EcsPrimitiveType); - break; - case EcsEntity: - init_component(world, e, - ECS_SIZEOF(ecs_entity_t), ECS_ALIGNOF(ecs_entity_t)); - init_type(world, e, EcsPrimitiveType); - break; - } - } -} +#ifdef FLECS_SNAPSHOT + + +/* World snapshot */ +struct ecs_snapshot_t { + ecs_world_t *world; + ecs_sparse_t *entity_index; + ecs_vector_t *tables; + ecs_entity_t last_id; + ecs_filter_t filter; +}; + +/** Small footprint data structure for storing data associated with a table. */ +typedef struct ecs_table_leaf_t { + ecs_table_t *table; + ecs_vector_t *type; + ecs_data_t *data; +} ecs_table_leaf_t; static -void set_member(ecs_iter_t *it) { - ecs_world_t *world = it->world; - EcsMember *member = ecs_term(it, EcsMember, 1); +ecs_data_t* duplicate_data( + const ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *main_data) +{ + if (!ecs_table_count(table)) { + return NULL; + } - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - ecs_entity_t parent = ecs_get_object(world, e, EcsChildOf, 0); - if (!parent) { - ecs_err("missing parent for member '%s'", ecs_get_name(world, e)); - continue; + ecs_data_t *result = ecs_os_calloc(ECS_SIZEOF(ecs_data_t)); + + ecs_type_t storage_type = table->storage_type; + int32_t i, column_count = ecs_vector_count(storage_type); + ecs_entity_t *components = ecs_vector_first(storage_type, ecs_entity_t); + + result->columns = ecs_os_memdup( + main_data->columns, ECS_SIZEOF(ecs_column_t) * column_count); + + /* Copy entities */ + result->entities = ecs_vector_copy(main_data->entities, ecs_entity_t); + ecs_entity_t *entities = ecs_vector_first(result->entities, ecs_entity_t); + + /* Copy record ptrs */ + result->record_ptrs = ecs_vector_copy(main_data->record_ptrs, ecs_record_t*); + + /* Copy each column */ + for (i = 0; i < column_count; i ++) { + ecs_entity_t component = components[i]; + ecs_column_t *column = &result->columns[i]; + + component = ecs_get_typeid(world, component); + + const ecs_type_info_t *cdata = flecs_get_c_info(world, component); + int16_t size = column->size; + int16_t alignment = column->alignment; + ecs_copy_t copy; + + if (cdata && (copy = cdata->lifecycle.copy)) { + int32_t count = ecs_vector_count(column->data); + ecs_vector_t *dst_vec = ecs_vector_new_t(size, alignment, count); + ecs_vector_set_count_t(&dst_vec, size, alignment, count); + void *dst_ptr = ecs_vector_first_t(dst_vec, size, alignment); + void *ctx = cdata->lifecycle.ctx; + + ecs_xtor_t ctor = cdata->lifecycle.ctor; + if (ctor) { + ctor((ecs_world_t*)world, component, entities, dst_ptr, + flecs_to_size_t(size), count, ctx); + } + + void *src_ptr = ecs_vector_first_t(column->data, size, alignment); + copy((ecs_world_t*)world, component, entities, entities, dst_ptr, + src_ptr, flecs_to_size_t(size), count, ctx); + + column->data = dst_vec; + } else { + column->data = ecs_vector_copy_t(column->data, size, alignment); } + } - add_member_to_struct(world, parent, e, member); + return result; +} + +static +void snapshot_table( + const ecs_world_t *world, + ecs_snapshot_t *snapshot, + ecs_table_t *table) +{ + if (table->flags & EcsTableHasBuiltins) { + return; } + + ecs_table_leaf_t *l = ecs_vector_get( + snapshot->tables, ecs_table_leaf_t, (int32_t)table->id); + ecs_assert(l != NULL, ECS_INTERNAL_ERROR, NULL); + + l->table = table; + l->type = ecs_vector_copy(table->type, ecs_id_t); + l->data = duplicate_data(world, table, &table->storage); } static -void add_enum(ecs_iter_t *it) { - ecs_world_t *world = it->world; +ecs_snapshot_t* snapshot_create( + const ecs_world_t *world, + const ecs_sparse_t *entity_index, + ecs_iter_t *iter, + ecs_iter_next_action_t next) +{ + ecs_snapshot_t *result = ecs_os_calloc_t(ecs_snapshot_t); + ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - - if (init_component( - world, e, ECS_SIZEOF(ecs_i32_t), ECS_ALIGNOF(ecs_i32_t))) - { - continue; - } + result->world = (ecs_world_t*)world; - if (init_type(world, e, EcsEnumType)) { - continue; - } + /* If no iterator is provided, the snapshot will be taken of the entire + * world, and we can simply copy the entity index as it will be restored + * entirely upon snapshote restore. */ + if (!iter && entity_index) { + result->entity_index = flecs_sparse_copy(entity_index); } -} -static -void add_bitmask(ecs_iter_t *it) { - ecs_world_t *world = it->world; + /* Create vector with as many elements as tables, so we can store the + * snapshot tables at their element ids. When restoring a snapshot, the code + * will run a diff between the tables in the world and the snapshot, to see + * which of the world tables still exist, no longer exist, or need to be + * deleted. */ + uint64_t t, table_count = flecs_sparse_last_id(world->store.tables) + 1; + result->tables = ecs_vector_new(ecs_table_leaf_t, (int32_t)table_count); + ecs_vector_set_count(&result->tables, ecs_table_leaf_t, (int32_t)table_count); + ecs_table_leaf_t *arr = ecs_vector_first(result->tables, ecs_table_leaf_t); - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - - if (init_component( - world, e, ECS_SIZEOF(ecs_u32_t), ECS_ALIGNOF(ecs_u32_t))) - { - continue; - } + /* Array may have holes, so initialize with 0 */ + ecs_os_memset_n(arr, 0, ecs_table_leaf_t, table_count); - if (init_type(world, e, EcsBitmaskType)) { - continue; + /* Iterate tables in iterator */ + if (iter) { + while (next(iter)) { + ecs_table_t *table = iter->table; + snapshot_table(world, result, table); + } + } else { + for (t = 0; t < table_count; t ++) { + ecs_table_t *table = flecs_sparse_get( + world->store.tables, ecs_table_t, t); + snapshot_table(world, result, table); } } + + return result; } -static -void add_constant(ecs_iter_t *it) { - ecs_world_t *world = it->world; +/** Create a snapshot */ +ecs_snapshot_t* ecs_snapshot_take( + ecs_world_t *stage) +{ + const ecs_world_t *world = ecs_get_world(stage); - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - ecs_entity_t parent = ecs_get_object(world, e, EcsChildOf, 0); - if (!parent) { - ecs_err("missing parent for constant '%s'", ecs_get_name(world, e)); - continue; - } + ecs_snapshot_t *result = snapshot_create( + world, + world->store.entity_index, + NULL, + NULL); - if (ecs_has(world, parent, EcsEnum)) { - add_constant_to_enum(world, parent, e, it->event_id); - } else if (ecs_has(world, parent, EcsBitmask)) { - add_constant_to_bitmask(world, parent, e, it->event_id); - } - } + result->last_id = world->stats.last_id; + + return result; +} + +/** Create a filtered snapshot */ +ecs_snapshot_t* ecs_snapshot_take_w_iter( + ecs_iter_t *iter, + ecs_iter_next_action_t next) +{ + ecs_world_t *world = iter->world; + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + + ecs_snapshot_t *result = snapshot_create( + world, + world->store.entity_index, + iter, + next); + + result->last_id = world->stats.last_id; + + return result; } +/* Restoring an unfiltered snapshot restores the world to the exact state it was + * when the snapshot was taken. */ static -void set_array(ecs_iter_t *it) { - ecs_world_t *world = it->world; - EcsArray *array = ecs_term(it, EcsArray, 1); +void restore_unfiltered( + ecs_world_t *world, + ecs_snapshot_t *snapshot) +{ + flecs_sparse_restore(world->store.entity_index, snapshot->entity_index); + flecs_sparse_free(snapshot->entity_index); + + world->stats.last_id = snapshot->last_id; - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - ecs_entity_t elem_type = array[i].type; - int32_t elem_count = array[i].count; + ecs_table_leaf_t *leafs = ecs_vector_first( + snapshot->tables, ecs_table_leaf_t); + int32_t i, count = (int32_t)flecs_sparse_last_id(world->store.tables); + int32_t snapshot_count = ecs_vector_count(snapshot->tables); - if (!elem_type) { - ecs_err("array '%s' has no element type", ecs_get_name(world, e)); + for (i = 0; i <= count; i ++) { + ecs_table_t *world_table = flecs_sparse_get( + world->store.tables, ecs_table_t, (uint32_t)i); + + if (world_table && (world_table->flags & EcsTableHasBuiltins)) { continue; } - if (!elem_count) { - ecs_err("array '%s' has size 0", ecs_get_name(world, e)); - continue; + ecs_table_leaf_t *snapshot_table = NULL; + if (i < snapshot_count) { + snapshot_table = &leafs[i]; + if (!snapshot_table->table) { + snapshot_table = NULL; + } } - const EcsComponent *elem_ptr = ecs_get(world, elem_type, EcsComponent); - if (init_component( - world, e, elem_ptr->size * elem_count, elem_ptr->alignment)) - { - continue; + /* If the world table no longer exists but the snapshot table does, + * reinsert it */ + if (!world_table && snapshot_table) { + ecs_ids_t type = { + .array = ecs_vector_first(snapshot_table->type, ecs_id_t), + .count = ecs_vector_count(snapshot_table->type) + }; + + ecs_table_t *table = flecs_table_find_or_create(world, &type); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + + if (snapshot_table->data) { + flecs_table_replace_data(world, table, snapshot_table->data); + } + + /* If the world table still exists, replace its data */ + } else if (world_table && snapshot_table) { + if (snapshot_table->data) { + flecs_table_replace_data( + world, world_table, snapshot_table->data); + } else { + flecs_table_clear_data( + world, world_table, &world_table->storage); + flecs_table_init_data(world, world_table); + } + + /* If the snapshot table doesn't exist, this table was created after the + * snapshot was taken and needs to be deleted */ + } else if (world_table && !snapshot_table) { + /* Deleting a table invokes OnRemove triggers & updates the entity + * index. That is not what we want, since entities may no longer be + * valid (if they don't exist in the snapshot) or may have been + * restored in a different table. Therefore first clear the data + * from the table (which doesn't invoke triggers), and then delete + * the table. */ + flecs_table_clear_data(world, world_table, &world_table->storage); + flecs_delete_table(world, world_table); + + /* If there is no world & snapshot table, nothing needs to be done */ + } else { } + + if (snapshot_table) { + ecs_os_free(snapshot_table->data); + ecs_os_free(snapshot_table->type); } + } - if (init_type(world, e, EcsArrayType)) { + /* Now that all tables have been restored and world is in a consistent + * state, run OnSet systems */ + int32_t world_count = flecs_sparse_count(world->store.tables); + for (i = 0; i < world_count; i ++) { + ecs_table_t *table = flecs_sparse_get_dense( + world->store.tables, ecs_table_t, i); + if (table->flags & EcsTableHasBuiltins) { continue; } + + int32_t tcount = ecs_table_count(table); + if (tcount) { + flecs_notify_on_set(world, table, 0, tcount, NULL, true); + } } } +/* Restoring a filtered snapshots only restores the entities in the snapshot + * to their previous state. */ static -void set_vector(ecs_iter_t *it) { - ecs_world_t *world = it->world; - EcsVector *array = ecs_term(it, EcsVector, 1); +void restore_filtered( + ecs_world_t *world, + ecs_snapshot_t *snapshot) +{ + ecs_table_leaf_t *leafs = ecs_vector_first( + snapshot->tables, ecs_table_leaf_t); + int32_t l = 0, snapshot_count = ecs_vector_count(snapshot->tables); - int i, count = it->count; - for (i = 0; i < count; i ++) { - ecs_entity_t e = it->entities[i]; - ecs_entity_t elem_type = array[i].type; + for (l = 0; l < snapshot_count; l ++) { + ecs_table_leaf_t *snapshot_table = &leafs[l]; + ecs_table_t *table = snapshot_table->table; - if (!elem_type) { - ecs_err("vector '%s' has no element type", ecs_get_name(world, e)); + if (!table) { continue; } - if (init_component(world, e, - ECS_SIZEOF(ecs_vector_t*), ECS_ALIGNOF(ecs_vector_t*))) - { + ecs_data_t *data = snapshot_table->data; + if (!data) { + ecs_vector_free(snapshot_table->type); continue; } - if (init_type(world, e, EcsVectorType)) { - continue; + /* Delete entity from storage first, so that when we restore it to the + * current table we can be sure that there won't be any duplicates */ + int32_t i, entity_count = ecs_vector_count(data->entities); + ecs_entity_t *entities = ecs_vector_first( + snapshot_table->data->entities, ecs_entity_t); + for (i = 0; i < entity_count; i ++) { + ecs_entity_t e = entities[i]; + ecs_record_t *r = ecs_eis_get(world, e); + if (r && r->table) { + bool is_monitored; + int32_t row = flecs_record_to_row(r->row, &is_monitored); + flecs_table_delete( + world, r->table, &r->table->storage, row, true); + } else { + /* Make sure that the entity has the same generation count */ + ecs_eis_set_generation(world, e); + } } - } -} -static -void ecs_meta_type_init_default_ctor(ecs_iter_t *it) { - ecs_world_t *world = it->world; + /* Merge data from snapshot table with world table */ + int32_t old_count = ecs_table_count(snapshot_table->table); + int32_t new_count = flecs_table_data_count(snapshot_table->data); - int i; - for (i = 0; i < it->count; i ++) { - ecs_entity_t type = it->entities[i]; + flecs_table_merge(world, table, table, &table->storage, snapshot_table->data); - /* If component has no component actions (which is typical if a type is - * created with reflection data) make sure its values are always - * initialized with zero. This prevents the injection of invalid data - * through generic APIs after adding a component without setting it. */ - if (!ecs_component_has_actions(world, type)) { - ecs_set_component_actions_w_id(world, type, - &(EcsComponentLifecycle){ - .ctor = ecs_default_ctor - }); + /* Run OnSet systems for merged entities */ + if (new_count) { + flecs_notify_on_set( + world, table, old_count, new_count, NULL, true); } + + ecs_os_free(snapshot_table->data->columns); + ecs_os_free(snapshot_table->data); + ecs_vector_free(snapshot_table->type); } } -static -void member_on_set( +/** Restore a snapshot */ +void ecs_snapshot_restore( ecs_world_t *world, - ecs_entity_t component, - const ecs_entity_t *entity_ptr, - void *ptr, - size_t size, - int32_t count, - void *ctx) + ecs_snapshot_t *snapshot) { - (void)world; - (void)component; - (void)entity_ptr; - (void)size; - (void)count; - (void)ctx; - - EcsMember *mbr = ptr; - if (!mbr->count) { - mbr->count = 1; + if (snapshot->entity_index) { + /* Unfiltered snapshots have a copy of the entity index which is + * copied back entirely when the snapshot is restored */ + restore_unfiltered(world, snapshot); + } else { + restore_filtered(world, snapshot); } -} -void FlecsMetaImport( - ecs_world_t *world) -{ - ECS_MODULE(world, FlecsMeta); - - ecs_set_name_prefix(world, "Ecs"); - - flecs_bootstrap_component(world, EcsMetaType); - flecs_bootstrap_component(world, EcsMetaTypeSerialized); - flecs_bootstrap_component(world, EcsPrimitive); - flecs_bootstrap_component(world, EcsEnum); - flecs_bootstrap_component(world, EcsBitmask); - flecs_bootstrap_component(world, EcsMember); - flecs_bootstrap_component(world, EcsStruct); - flecs_bootstrap_component(world, EcsArray); - flecs_bootstrap_component(world, EcsVector); - - flecs_bootstrap_tag(world, EcsConstant); - - ecs_set_component_actions(world, EcsMetaType, { .ctor = ecs_default_ctor }); - - ecs_set_component_actions(world, EcsMetaTypeSerialized, { - .ctor = ecs_default_ctor, - .move = ecs_move(EcsMetaTypeSerialized), - .copy = ecs_copy(EcsMetaTypeSerialized), - .dtor = ecs_dtor(EcsMetaTypeSerialized) - }); - - ecs_set_component_actions(world, EcsStruct, { - .ctor = ecs_default_ctor, - .move = ecs_move(EcsStruct), - .copy = ecs_copy(EcsStruct), - .dtor = ecs_dtor(EcsStruct) - }); - - ecs_set_component_actions(world, EcsMember, { - .ctor = ecs_default_ctor, - .on_set = member_on_set - }); - - ecs_set_component_actions(world, EcsEnum, { - .ctor = ecs_default_ctor, - .move = ecs_move(EcsEnum), - .copy = ecs_copy(EcsEnum), - .dtor = ecs_dtor(EcsEnum) - }); - - ecs_set_component_actions(world, EcsBitmask, { - .ctor = ecs_default_ctor, - .move = ecs_move(EcsBitmask), - .copy = ecs_copy(EcsBitmask), - .dtor = ecs_dtor(EcsBitmask) - }); - - /* Register triggers to finalize type information from component data */ - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsPrimitive), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnSet}, - .callback = set_primitive - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsMember), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnSet}, - .callback = set_member - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsEnum), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnAdd}, - .callback = add_enum - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsBitmask), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnAdd}, - .callback = add_bitmask - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = EcsConstant, - .term.subj.set.mask = EcsSelf, - .events = {EcsOnAdd}, - .callback = add_constant - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_pair(EcsConstant, EcsWildcard), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnSet}, - .callback = add_constant - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsArray), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnSet}, - .callback = set_array - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsVector), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnSet}, - .callback = set_vector - }); - - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsMetaType), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnSet}, - .callback = ecs_meta_type_serialized_init - }); + ecs_vector_free(snapshot->tables); - ecs_trigger_init(world, &(ecs_trigger_desc_t) { - .term.id = ecs_id(EcsMetaType), - .term.subj.set.mask = EcsSelf, - .events = {EcsOnSet}, - .callback = ecs_meta_type_init_default_ctor - }); + ecs_os_free(snapshot); +} - /* Initialize primitive types */ - #define ECS_PRIMITIVE(world, type, primitive_kind)\ - ecs_entity_init(world, &(ecs_entity_desc_t) {\ - .entity = ecs_id(ecs_##type##_t),\ - .name = #type });\ - ecs_set(world, ecs_id(ecs_##type##_t), EcsPrimitive, {\ - .kind = primitive_kind\ - }); +ecs_iter_t ecs_snapshot_iter( + ecs_snapshot_t *snapshot) +{ + ecs_snapshot_iter_t iter = { + .tables = snapshot->tables, + .index = 0 + }; - ECS_PRIMITIVE(world, bool, EcsBool); - ECS_PRIMITIVE(world, char, EcsChar); - ECS_PRIMITIVE(world, byte, EcsByte); - ECS_PRIMITIVE(world, u8, EcsU8); - ECS_PRIMITIVE(world, u16, EcsU16); - ECS_PRIMITIVE(world, u32, EcsU32); - ECS_PRIMITIVE(world, u64, EcsU64); - ECS_PRIMITIVE(world, uptr, EcsUPtr); - ECS_PRIMITIVE(world, i8, EcsI8); - ECS_PRIMITIVE(world, i16, EcsI16); - ECS_PRIMITIVE(world, i32, EcsI32); - ECS_PRIMITIVE(world, i64, EcsI64); - ECS_PRIMITIVE(world, iptr, EcsIPtr); - ECS_PRIMITIVE(world, f32, EcsF32); - ECS_PRIMITIVE(world, f64, EcsF64); - ECS_PRIMITIVE(world, string, EcsString); - ECS_PRIMITIVE(world, entity, EcsEntity); + return (ecs_iter_t){ + .world = snapshot->world, + .table_count = ecs_vector_count(snapshot->tables), + .iter.snapshot = iter + }; +} - #undef ECS_PRIMITIVE +bool ecs_snapshot_next( + ecs_iter_t *it) +{ + ecs_snapshot_iter_t *iter = &it->iter.snapshot; + ecs_table_leaf_t *tables = ecs_vector_first(iter->tables, ecs_table_leaf_t); + int32_t count = ecs_vector_count(iter->tables); + int32_t i; - /* Set default child components */ - ecs_add_pair(world, ecs_id(EcsStruct), - EcsDefaultChildComponent, ecs_id(EcsMember)); + for (i = iter->index; i < count; i ++) { + ecs_table_t *table = tables[i].table; + if (!table) { + continue; + } - ecs_add_pair(world, ecs_id(EcsMember), - EcsDefaultChildComponent, ecs_id(EcsMember)); + ecs_data_t *data = tables[i].data; - ecs_add_pair(world, ecs_id(EcsEnum), - EcsDefaultChildComponent, EcsConstant); + it->table = table; + it->count = ecs_table_count(table); + if (data) { + it->entities = ecs_vector_first(data->entities, ecs_entity_t); + } else { + it->entities = NULL; + } - ecs_add_pair(world, ecs_id(EcsBitmask), - EcsDefaultChildComponent, EcsConstant); + it->is_valid = true; + iter->index = i + 1; + + goto yield; + } - /* Initialize reflection data for meta components */ - ecs_entity_t type_kind = ecs_enum_init(world, &(ecs_enum_desc_t) { - .entity.name = "TypeKind", - .constants = { - {.name = "PrimitiveType"}, - {.name = "BitmaskType"}, - {.name = "EnumType"}, - {.name = "StructType"}, - {.name = "ArrayType"}, - {.name = "VectorType"} - } - }); + it->is_valid = false; + return false; - ecs_struct_init(world, &(ecs_struct_desc_t) { - .entity.entity = ecs_id(EcsMetaType), - .members = { - {.name = (char*)"kind", .type = type_kind} - } - }); +yield: + it->is_valid = true; + return true; +} - ecs_entity_t primitive_kind = ecs_enum_init(world, &(ecs_enum_desc_t) { - .entity.name = "PrimitiveKind", - .constants = { - {.name = "Bool", 1}, - {.name = "Char"}, - {.name = "Byte"}, - {.name = "U8"}, - {.name = "U16"}, - {.name = "U32"}, - {.name = "U64"}, - {.name = "I8"}, - {.name = "I16"}, - {.name = "I32"}, - {.name = "I64"}, - {.name = "F32"}, - {.name = "F64"}, - {.name = "UPtr"}, - {.name = "IPtr"}, - {.name = "String"}, - {.name = "Entity"} - } - }); +/** Cleanup snapshot */ +void ecs_snapshot_free( + ecs_snapshot_t *snapshot) +{ + flecs_sparse_free(snapshot->entity_index); - ecs_struct_init(world, &(ecs_struct_desc_t) { - .entity.entity = ecs_id(EcsPrimitive), - .members = { - {.name = (char*)"kind", .type = primitive_kind} + ecs_table_leaf_t *tables = ecs_vector_first(snapshot->tables, ecs_table_leaf_t); + int32_t i, count = ecs_vector_count(snapshot->tables); + for (i = 0; i < count; i ++) { + ecs_table_leaf_t *snapshot_table = &tables[i]; + ecs_table_t *table = snapshot_table->table; + if (table) { + ecs_data_t *data = snapshot_table->data; + if (data) { + flecs_table_clear_data(snapshot->world, table, data); + ecs_os_free(data); + } + ecs_vector_free(snapshot_table->type); } - }); + } - ecs_struct_init(world, &(ecs_struct_desc_t) { - .entity.entity = ecs_id(EcsMember), - .members = { - {.name = (char*)"type", .type = ecs_id(ecs_entity_t)}, - {.name = (char*)"count", .type = ecs_id(ecs_i32_t)} - } - }); + ecs_vector_free(snapshot->tables); + ecs_os_free(snapshot); } #endif -#ifdef FLECS_META - -static -const char* op_kind_str( - ecs_meta_type_op_kind_t kind) -{ - switch(kind) { - - case EcsOpEnum: return "Enum"; - case EcsOpBitmask: return "Bitmask"; - case EcsOpArray: return "Array"; - case EcsOpVector: return "Vector"; - case EcsOpPush: return "Push"; - case EcsOpPop: return "Pop"; - case EcsOpPrimitive: return "Primitive"; - case EcsOpBool: return "Bool"; - case EcsOpChar: return "Char"; - case EcsOpByte: return "Byte"; - case EcsOpU8: return "U8"; - case EcsOpU16: return "U16"; - case EcsOpU32: return "U32"; - case EcsOpU64: return "U64"; - case EcsOpI8: return "I8"; - case EcsOpI16: return "I16"; - case EcsOpI32: return "I32"; - case EcsOpI64: return "I64"; - case EcsOpF32: return "F32"; - case EcsOpF64: return "F64"; - case EcsOpUPtr: return "UPtr"; - case EcsOpIPtr: return "IPtr"; - case EcsOpString: return "String"; - case EcsOpEntity: return "Entity"; - default: return "<< invalid kind >>"; - } -} -/* Get current scope */ -static -ecs_meta_scope_t* get_scope( - ecs_meta_cursor_t *cursor) -{ - ecs_assert(cursor != NULL, ECS_INVALID_PARAMETER, NULL); - return &cursor->scope[cursor->depth]; -} +#ifdef FLECS_EXPR -/* Get previous scope */ static -ecs_meta_scope_t* get_prev_scope( - ecs_meta_cursor_t *cursor) -{ - ecs_assert(cursor != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(cursor->depth > 0, ECS_INVALID_PARAMETER, NULL); - return &cursor->scope[cursor->depth - 1]; -} +int expr_ser_type( + const ecs_world_t *world, + ecs_vector_t *ser, + const void *base, + ecs_strbuf_t *str); -/* Get current operation for scope */ static -ecs_meta_type_op_t* get_op( - ecs_meta_scope_t *scope) -{ - return &scope->ops[scope->op_cur]; -} +int expr_ser_type_ops( + const ecs_world_t *world, + ecs_meta_type_op_t *ops, + int32_t op_count, + const void *base, + ecs_strbuf_t *str); -/* Get component for type in current scope */ static -const EcsComponent* get_component_ptr( +int expr_ser_type_op( const ecs_world_t *world, - ecs_meta_scope_t *scope) -{ - const EcsComponent *comp = scope->comp; - if (!comp) { - comp = scope->comp = ecs_get(world, scope->type, EcsComponent); - ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); - } - return comp; -} + ecs_meta_type_op_t *op, + const void *base, + ecs_strbuf_t *str); -/* Get size for type in current scope */ static -ecs_size_t get_size( - const ecs_world_t *world, - ecs_meta_scope_t *scope) -{ - return get_component_ptr(world, scope)->size; +ecs_primitive_kind_t expr_op_to_primitive_kind(ecs_meta_type_op_kind_t kind) { + return kind - EcsOpPrimitive; } -/* Get alignment for type in current scope */ +/* Serialize a primitive value */ static -ecs_size_t get_alignment( +int expr_ser_primitive( const ecs_world_t *world, - ecs_meta_scope_t *scope) + ecs_primitive_kind_t kind, + const void *base, + ecs_strbuf_t *str) { - return get_component_ptr(world, scope)->alignment; -} + const char *bool_str[] = { "false", "true" }; -static -int32_t get_elem_count( - ecs_meta_scope_t *scope) -{ - if (scope->vector) { - return ecs_vector_count(*(scope->vector)); + switch(kind) { + case EcsBool: + ecs_strbuf_appendstr(str, bool_str[(int)*(bool*)base]); + break; + case EcsChar: { + char chbuf[3]; + char ch = *(char*)base; + if (ch) { + ecs_chresc(chbuf, *(char*)base, '"'); + ecs_strbuf_appendstrn(str, "\"", 1); + ecs_strbuf_appendstr(str, chbuf); + ecs_strbuf_appendstrn(str, "\"", 1); + } else { + ecs_strbuf_appendstr(str, "0"); + } + break; + } + case EcsByte: + ecs_strbuf_append(str, "%u", *(uint8_t*)base); + break; + case EcsU8: + ecs_strbuf_append(str, "%u", *(uint8_t*)base); + break; + case EcsU16: + ecs_strbuf_append(str, "%u", *(uint16_t*)base); + break; + case EcsU32: + ecs_strbuf_append(str, "%u", *(uint32_t*)base); + break; + case EcsU64: + ecs_strbuf_append(str, "%llu", *(uint64_t*)base); + break; + case EcsI8: + ecs_strbuf_append(str, "%d", *(int8_t*)base); + break; + case EcsI16: + ecs_strbuf_append(str, "%d", *(int16_t*)base); + break; + case EcsI32: + ecs_strbuf_append(str, "%d", *(int32_t*)base); + break; + case EcsI64: + ecs_strbuf_append(str, "%lld", *(int64_t*)base); + break; + case EcsF32: + ecs_strbuf_append(str, "%f", (double)*(float*)base); + break; + case EcsF64: + ecs_strbuf_append(str, "%f", *(double*)base); + break; + case EcsIPtr: + ecs_strbuf_append(str, "%i", *(intptr_t*)base); + break; + case EcsUPtr: + ecs_strbuf_append(str, "%u", *(uintptr_t*)base); + break; + case EcsString: { + char *value = *(char**)base; + if (value) { + ecs_size_t length = ecs_stresc(NULL, 0, '"', value); + if (length == ecs_os_strlen(value)) { + ecs_strbuf_appendstrn(str, "\"", 1); + ecs_strbuf_appendstr(str, value); + ecs_strbuf_appendstrn(str, "\"", 1); + } else { + char *out = ecs_os_malloc(length + 3); + ecs_stresc(out + 1, length, '"', value); + out[0] = '"'; + out[length + 1] = '"'; + out[length + 2] = '\0'; + ecs_strbuf_appendstr_zerocpy(str, out); + } + } else { + ecs_strbuf_appendstr(str, "null"); + } + break; + } + case EcsEntity: { + ecs_entity_t e = *(ecs_entity_t*)base; + if (!e) { + ecs_strbuf_appendstr(str, "0"); + } else { + char *path = ecs_get_fullpath(world, e); + ecs_assert(path != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_strbuf_appendstr(str, path); + ecs_os_free(path); + } + break; + } + default: + ecs_err("invalid primitive kind"); + return -1; } - ecs_meta_type_op_t *op = get_op(scope); - return op->count; + return 0; } -/* Get pointer to current field/element */ +/* Serialize enumeration */ static -ecs_meta_type_op_t* get_ptr( +int expr_ser_enum( const ecs_world_t *world, - ecs_meta_scope_t *scope) + ecs_meta_type_op_t *op, + const void *base, + ecs_strbuf_t *str) { - ecs_meta_type_op_t *op = get_op(scope); - ecs_size_t size = get_size(world, scope); + const EcsEnum *enum_type = ecs_get(world, op->type, EcsEnum); + ecs_assert(enum_type != NULL, ECS_INVALID_PARAMETER, NULL); - if (scope->vector) { - ecs_size_t align = get_alignment(world, scope); - ecs_vector_set_min_count_t( - scope->vector, size, align, scope->elem_cur + 1); - scope->ptr = ecs_vector_first_t(*(scope->vector), size, align); + int32_t value = *(int32_t*)base; + + /* Enumeration constants are stored in a map that is keyed on the + * enumeration value. */ + ecs_enum_constant_t *constant = ecs_map_get( + enum_type->constants, ecs_enum_constant_t, value); + if (!constant) { + char *path = ecs_get_fullpath(world, op->type); + ecs_err("value %d is not valid for enum type '%s'", value, path); + ecs_os_free(path); + return -1; } - return ECS_OFFSET(scope->ptr, size * scope->elem_cur + op->offset); + ecs_strbuf_appendstr(str, ecs_get_name(world, constant->constant)); + + return 0; } +/* Serialize bitmask */ static -int push_type( +int expr_ser_bitmask( const ecs_world_t *world, - ecs_meta_scope_t *scope, - ecs_entity_t type, - void *ptr) + ecs_meta_type_op_t *op, + const void *ptr, + ecs_strbuf_t *str) { - const EcsMetaTypeSerialized *ser = ecs_get( - world, type, EcsMetaTypeSerialized); - if (ser == NULL) { - char *str = ecs_id_str(world, type); - ecs_err("cannot open scope for entity '%s' which is not a type", str); - ecs_os_free(str); - return -1; - } + const EcsBitmask *bitmask_type = ecs_get(world, op->type, EcsBitmask); + ecs_assert(bitmask_type != NULL, ECS_INVALID_PARAMETER, NULL); - scope[0] = (ecs_meta_scope_t) { - .type = type, - .ops = ecs_vector_first(ser->ops, ecs_meta_type_op_t), - .op_count = ecs_vector_count(ser->ops), - .ptr = ptr - }; + uint32_t value = *(uint32_t*)ptr; + ecs_map_key_t key; + ecs_bitmask_constant_t *constant; + int count = 0; - return 0; -} + ecs_strbuf_list_push(str, "", "|"); -ecs_meta_cursor_t ecs_meta_cursor( - const ecs_world_t *world, - ecs_entity_t type, - void *ptr) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(type != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); + /* Multiple flags can be set at a given time. Iterate through all the flags + * and append the ones that are set. */ + ecs_map_iter_t it = ecs_map_iter(bitmask_type->constants); + while ((constant = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { + if ((value & key) == key) { + ecs_strbuf_list_appendstr(str, + ecs_get_name(world, constant->constant)); + count ++; + value -= (uint32_t)key; + } + } - ecs_meta_cursor_t result = { - .world = world, - .valid = true - }; + if (value != 0) { + /* All bits must have been matched by a constant */ + char *path = ecs_get_fullpath(world, op->type); + ecs_err( + "value for bitmask %s contains bits (%u) that cannot be mapped to constant", + path, value); + ecs_os_free(path); + goto error; + } - if (push_type(world, result.scope, type, ptr) != 0) { - result.valid = false; + if (!count) { + ecs_strbuf_list_appendstr(str, "0"); } - return result; -} + ecs_strbuf_list_pop(str, ""); -void* ecs_meta_get_ptr( - ecs_meta_cursor_t *cursor) -{ - return get_ptr(cursor->world, get_scope(cursor)); + return 0; +error: + return -1; } -int ecs_meta_next( - ecs_meta_cursor_t *cursor) +/* Serialize elements of a contiguous array */ +static +int expr_ser_elements( + const ecs_world_t *world, + ecs_meta_type_op_t *ops, + int32_t op_count, + const void *base, + int32_t elem_count, + int32_t elem_size, + ecs_strbuf_t *str) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); + ecs_strbuf_list_push(str, "[", ", "); - if (scope->is_collection) { - scope->elem_cur ++; - scope->op_cur = 0; - if (scope->elem_cur >= get_elem_count(scope)) { - ecs_err("out of collection bounds (%d)", scope->elem_cur); + const void *ptr = base; + + int i; + for (i = 0; i < elem_count; i ++) { + ecs_strbuf_list_next(str); + if (expr_ser_type_ops(world, ops, op_count, ptr, str)) { return -1; } - - return 0; + ptr = ECS_OFFSET(ptr, elem_size); } - scope->op_cur += op->op_count; - if (scope->op_cur >= scope->op_count) { - ecs_err("out of bounds"); - return -1; - } + ecs_strbuf_list_pop(str, "]"); return 0; } -int ecs_meta_member( - ecs_meta_cursor_t *cursor, - const char *name) +static +int expr_ser_type_elements( + const ecs_world_t *world, + ecs_entity_t type, + const void *base, + int32_t elem_count, + ecs_strbuf_t *str) { - if (cursor->depth == 0) { - ecs_err("cannot move to member in root scope"); - return -1; - } + const EcsMetaTypeSerialized *ser = ecs_get( + world, type, EcsMetaTypeSerialized); + ecs_assert(ser != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_meta_scope_t *prev_scope = get_prev_scope(cursor); - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *push_op = get_op(prev_scope); - const ecs_world_t *world = cursor->world; + const EcsComponent *comp = ecs_get(world, type, EcsComponent); + ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(push_op->kind == EcsOpPush, ECS_INTERNAL_ERROR, NULL); - - if (!push_op->members) { - ecs_err("cannot move to member '%s' for non-struct type", name); - return -1; - } + ecs_meta_type_op_t *ops = ecs_vector_first(ser->ops, ecs_meta_type_op_t); + int32_t op_count = ecs_vector_count(ser->ops); - ecs_hashed_string_t key = ecs_get_hashed_string( - name, ecs_os_strlen(name), 0); - int32_t *cur = flecs_hashmap_get(*push_op->members, &key, int32_t); - if (!cur) { - char *path = ecs_get_fullpath(world, scope->type); - ecs_err("unknown member '%s' for type '%s'", name, path); - ecs_os_free(path); - return -1; - } + return expr_ser_elements( + world, ops, op_count, base, elem_count, comp->size, str); +} - scope->op_cur = *cur; +/* Serialize array */ +static +int expr_ser_array( + const ecs_world_t *world, + ecs_meta_type_op_t *op, + const void *ptr, + ecs_strbuf_t *str) +{ + const EcsArray *a = ecs_get(world, op->type, EcsArray); + ecs_assert(a != NULL, ECS_INTERNAL_ERROR, NULL); - return 0; + return expr_ser_type_elements( + world, a->type, ptr, a->count, str); } -int ecs_meta_push( - ecs_meta_cursor_t *cursor) +/* Serialize vector */ +static +int expr_ser_vector( + const ecs_world_t *world, + ecs_meta_type_op_t *op, + const void *base, + ecs_strbuf_t *str) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - const ecs_world_t *world = cursor->world; - - if (cursor->depth == 0) { - if (!cursor->is_primitive_scope) { - if (op->kind > EcsOpScope) { - cursor->is_primitive_scope = true; - return 0; - } - } + ecs_vector_t *value = *(ecs_vector_t**)base; + if (!value) { + ecs_strbuf_appendstr(str, "null"); + return 0; } - void *ptr = get_ptr(world, scope); - cursor->depth ++; - ecs_assert(cursor->depth < ECS_META_MAX_SCOPE_DEPTH, - ECS_INTERNAL_ERROR, NULL); - - ecs_meta_scope_t *next_scope = get_scope(cursor); + const EcsVector *v = ecs_get(world, op->type, EcsVector); + ecs_assert(v != NULL, ECS_INTERNAL_ERROR, NULL); - /* If we're not already in an inline array and this operation is an inline - * array, push a frame for the array. - * Doing this first ensures that inline arrays take precedence over other - * kinds of push operations, such as for a struct element type. */ - if (!scope->is_inline_array && op->count > 1 && !scope->is_collection) { - /* Push a frame just for the element type, with inline_array = true */ - next_scope[0] = (ecs_meta_scope_t){ - .ops = op, - .op_count = op->op_count, - .ptr = scope->ptr, - .type = op->type, - .is_collection = true, - .is_inline_array = true - }; + const EcsComponent *comp = ecs_get(world, v->type, EcsComponent); + ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); - /* With 'is_inline_array' set to true we ensure that we can never push - * the same inline array twice */ + int32_t count = ecs_vector_count(value); + void *array = ecs_vector_first_t(value, comp->size, comp->alignment); - return 0; - } + /* Serialize contiguous buffer of vector */ + return expr_ser_type_elements(world, v->type, array, count, str); +} +/* Forward serialization to the different type kinds */ +static +int expr_ser_type_op( + const ecs_world_t *world, + ecs_meta_type_op_t *op, + const void *ptr, + ecs_strbuf_t *str) +{ switch(op->kind) { case EcsOpPush: - next_scope[0] = (ecs_meta_scope_t) { - .ops = &op[1], /* op after push */ - .op_count = op->op_count - 1, /* don't include pop */ - .ptr = scope->ptr, - .type = op->type - }; + case EcsOpPop: + /* Should not be parsed as single op */ + ecs_abort(ECS_INVALID_PARAMETER, NULL); break; - - case EcsOpArray: { - if (push_type(world, next_scope, op->type, ptr) != 0) { - return -1; + case EcsOpEnum: + if (expr_ser_enum(world, op, ECS_OFFSET(ptr, op->offset), str)) { + goto error; + } + break; + case EcsOpBitmask: + if (expr_ser_bitmask(world, op, ECS_OFFSET(ptr, op->offset), str)) { + goto error; + } + break; + case EcsOpArray: + if (expr_ser_array(world, op, ECS_OFFSET(ptr, op->offset), str)) { + goto error; } - - const EcsArray *type_ptr = ecs_get(world, op->type, EcsArray); - next_scope->type = type_ptr->type; - next_scope->is_collection = true; break; - } - case EcsOpVector: - next_scope->vector = ptr; - if (push_type(world, next_scope, op->type, NULL) != 0) { - return -1; + if (expr_ser_vector(world, op, ECS_OFFSET(ptr, op->offset), str)) { + goto error; + } + break; + default: + if (expr_ser_primitive(world, expr_op_to_primitive_kind(op->kind), + ECS_OFFSET(ptr, op->offset), str)) + { + /* Unknown operation */ + ecs_abort(ECS_INTERNAL_ERROR, NULL); + goto error; } - - const EcsVector *type_ptr = ecs_get(world, op->type, EcsVector); - next_scope->type = type_ptr->type; - next_scope->is_collection = true; break; - - default: { - char *path = ecs_get_fullpath(world, scope->type); - ecs_err("invalid push for type '%s'", path); - ecs_os_free(path); - return -1; - } - } - - if (scope->is_collection) { - next_scope[0].ptr = ECS_OFFSET(next_scope[0].ptr, - scope->elem_cur * get_size(world, scope)); } return 0; +error: + return -1; } -int ecs_meta_pop( - ecs_meta_cursor_t *cursor) +/* Iterate over a slice of the type ops array */ +static +int expr_ser_type_ops( + const ecs_world_t *world, + ecs_meta_type_op_t *ops, + int32_t op_count, + const void *base, + ecs_strbuf_t *str) { - if (cursor->is_primitive_scope) { - cursor->is_primitive_scope = false; - return 0; - } - - ecs_meta_scope_t *scope = get_scope(cursor); - cursor->depth --; - if (cursor->depth < 0) { - ecs_err("unexpected end of scope"); - return -1; - } + for (int i = 0; i < op_count; i ++) { + ecs_meta_type_op_t *op = &ops[i]; - ecs_meta_scope_t *next_scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(next_scope); + if (op != ops) { + if (op->name) { + ecs_strbuf_list_next(str); + ecs_strbuf_append(str, "%s: ", op->name); + } - if (!scope->is_inline_array) { - if (op->kind == EcsOpPush) { - next_scope->op_cur += op->op_count - 1; + int32_t elem_count = op->count; + if (elem_count > 1 && op != ops) { + /* Serialize inline array */ + if (expr_ser_elements(world, op, op->op_count, base, + elem_count, op->size, str)) + { + return -1; + } - /* push + op_count should point to the operation after pop */ - op = get_op(next_scope); - ecs_assert(op->kind == EcsOpPop, ECS_INTERNAL_ERROR, NULL); - } else if (op->kind == EcsOpArray || op->kind == EcsOpVector) { - /* Collection type, nothing else to do */ - } else { - /* should not have been able to push if the previous scope was not - * a complex or collection type */ - ecs_assert(false, ECS_INTERNAL_ERROR, NULL); + i += op->op_count - 1; + continue; + } + } + + switch(op->kind) { + case EcsOpPush: + ecs_strbuf_list_push(str, "{", ", "); + break; + case EcsOpPop: + ecs_strbuf_list_pop(str, "}"); + break; + default: + if (expr_ser_type_op(world, op, base, str)) { + goto error; + } + break; } - } else { - /* Make sure that this was an inline array */ - ecs_assert(next_scope->op_count > 1, ECS_INTERNAL_ERROR, NULL); } return 0; +error: + return -1; } -int ecs_meta_is_collection( - ecs_meta_cursor_t *cursor) -{ - ecs_meta_scope_t *scope = get_scope(cursor); - return scope->is_collection; -} - -ecs_entity_t ecs_meta_get_type( - ecs_meta_cursor_t *cursor) -{ - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - return op->type; -} - -/* Utility macro's to let the compiler do the conversion work for us */ -#define set_T(T, ptr, value)\ - ((T*)ptr)[0] = ((T)value) - -#define case_T(kind, T, dst, src)\ -case kind:\ - set_T(T, dst, src);\ - break - -#define cases_T_float(dst, src)\ - case_T(EcsOpF32, ecs_f32_t, dst, src);\ - case_T(EcsOpF64, ecs_f64_t, dst, src) - -#define cases_T_signed(dst, src)\ - case_T(EcsOpChar, ecs_char_t, dst, src);\ - case_T(EcsOpI8, ecs_i8_t, dst, src);\ - case_T(EcsOpI16, ecs_i16_t, dst, src);\ - case_T(EcsOpI32, ecs_i32_t, dst, src);\ - case_T(EcsOpI64, ecs_i64_t, dst, src);\ - case_T(EcsOpIPtr, ecs_iptr_t, dst, src) - -#define cases_T_unsigned(dst, src)\ - case_T(EcsOpByte, ecs_byte_t, dst, src);\ - case_T(EcsOpU8, ecs_u8_t, dst, src);\ - case_T(EcsOpU16, ecs_u16_t, dst, src);\ - case_T(EcsOpU32, ecs_u32_t, dst, src);\ - case_T(EcsOpU64, ecs_u64_t, dst, src);\ - case_T(EcsOpUPtr, ecs_uptr_t, dst, src);\ - -#define cases_T_bool(dst, src)\ -case EcsOpBool:\ - set_T(ecs_bool_t, dst, value != 0);\ - break - +/* Iterate over the type ops of a type */ static -void conversion_error( - ecs_meta_cursor_t *cursor, - ecs_meta_type_op_t *op, - const char *from) +int expr_ser_type( + const ecs_world_t *world, + ecs_vector_t *v_ops, + const void *base, + ecs_strbuf_t *str) { - char *path = ecs_get_fullpath(cursor->world, op->type); - ecs_err("unsupported conversion from %s to '%s'", from, path); - ecs_os_free(path); + ecs_meta_type_op_t *ops = ecs_vector_first(v_ops, ecs_meta_type_op_t); + int32_t count = ecs_vector_count(v_ops); + return expr_ser_type_ops(world, ops, count, base, str); } -int ecs_meta_set_bool( - ecs_meta_cursor_t *cursor, - bool value) +int ecs_ptr_to_expr_buf( + const ecs_world_t *world, + ecs_entity_t type, + const void *ptr, + ecs_strbuf_t *buf_out) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); + const EcsMetaTypeSerialized *ser = ecs_get( + world, type, EcsMetaTypeSerialized); + if (ser == NULL) { + char *path = ecs_get_fullpath(world, type); + ecs_err("cannot serialize value for type '%s'", path); + ecs_os_free(path); + goto error; + } - switch(op->kind) { - cases_T_bool(ptr, value); - cases_T_unsigned(ptr, value); - default: - conversion_error(cursor, op, "bool"); - return -1; + if (expr_ser_type(world, ser->ops, ptr, buf_out)) { + goto error; } return 0; +error: + return -1; } -int ecs_meta_set_char( - ecs_meta_cursor_t *cursor, - char value) +char* ecs_ptr_to_expr( + const ecs_world_t *world, + ecs_entity_t type, + const void* ptr) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); + ecs_strbuf_t str = ECS_STRBUF_INIT; - switch(op->kind) { - cases_T_bool(ptr, value); - cases_T_signed(ptr, value); - default: - conversion_error(cursor, op, "char"); - return -1; + if (ecs_ptr_to_expr_buf(world, type, ptr, &str) != 0) { + ecs_strbuf_reset(&str); + return NULL; } - return 0; + return ecs_strbuf_get(&str); } -int ecs_meta_set_int( - ecs_meta_cursor_t *cursor, - int64_t value) +int ecs_primitive_to_expr_buf( + const ecs_world_t *world, + ecs_primitive_kind_t kind, + const void *base, + ecs_strbuf_t *str) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); + return expr_ser_primitive(world, kind, base, str); +} - switch(op->kind) { - cases_T_bool(ptr, value); - cases_T_signed(ptr, value); - cases_T_float(ptr, value); - default: { - conversion_error(cursor, op, "int"); - return -1; - } - } +#endif - return 0; -} -int ecs_meta_set_uint( - ecs_meta_cursor_t *cursor, - uint64_t value) -{ - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); - - switch(op->kind) { - cases_T_bool(ptr, value); - cases_T_unsigned(ptr, value); - cases_T_float(ptr, value); - case EcsOpEntity: - set_T(ecs_entity_t, ptr, value); - break; - default: - conversion_error(cursor, op, "uint"); - return -1; - } - - return 0; -} +#ifdef FLECS_EXPR -int ecs_meta_set_float( - ecs_meta_cursor_t *cursor, - double value) +const char *ecs_parse_expr_token( + const char *name, + const char *expr, + const char *ptr, + char *token) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); + const char *start = ptr; + char *token_ptr = token; - switch(op->kind) { - cases_T_bool(ptr, value); - cases_T_signed(ptr, value); - cases_T_unsigned(ptr, value); - cases_T_float(ptr, value); - default: - conversion_error(cursor, op, "float"); - return -1; + while ((ptr = ecs_parse_token(name, expr, ptr, token_ptr))) { + if (ptr[0] == '|') { + token_ptr = &token_ptr[ptr - start]; + token_ptr[0] = '|'; + token_ptr[1] = '\0'; + token_ptr ++; + ptr ++; + start = ptr; + } else { + break; + } } - return 0; + return ptr; } -static -int add_bitmask_constant( - ecs_meta_cursor_t *cursor, - ecs_meta_type_op_t *op, - void *out, - const char *value) +const char* ecs_parse_expr( + const ecs_world_t *world, + const char *ptr, + ecs_entity_t type, + void *data_out, + const ecs_parse_expr_desc_t *desc) { - ecs_assert(op->type != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); + char token[ECS_MAX_TOKEN_SIZE]; + int depth = 0; - if (!ecs_os_strcmp(value, "0")) { - return 0; - } + const char *name = NULL; + const char *expr = NULL; - ecs_entity_t c = ecs_lookup_child(cursor->world, op->type, value); - if (!c) { - char *path = ecs_get_fullpath(cursor->world, op->type); - ecs_err("unresolved bitmask constant '%s' for type '%s'", value, path); - ecs_os_free(path); - return -1; + ptr = ecs_parse_fluff(ptr, NULL); + + ecs_meta_cursor_t cur = ecs_meta_cursor(world, type, data_out); + if (cur.valid == false) { + return NULL; } - const ecs_u32_t *v = ecs_get_pair_object( - cursor->world, c, EcsConstant, ecs_u32_t); - if (v == NULL) { - char *path = ecs_get_fullpath(cursor->world, op->type); - ecs_err("'%s' is not an bitmask constant for type '%s'", value, path); - ecs_os_free(path); - return -1; + if (desc) { + name = desc->name; + expr = desc->expr; + cur.lookup_action = desc->lookup_action; + cur.lookup_ctx = desc->lookup_ctx; } - *(ecs_u32_t*)out |= v[0]; + while ((ptr = ecs_parse_expr_token(name, expr, ptr, token))) { - return 0; -} + if (!ecs_os_strcmp(token, "{")) { + ecs_entity_t scope_type = ecs_meta_get_type(&cur); + depth ++; + if (ecs_meta_push(&cur) != 0) { + goto error; + } -static -int parse_bitmask( - ecs_meta_cursor_t *cursor, - ecs_meta_type_op_t *op, - void *out, - const char *value) -{ - char token[ECS_MAX_TOKEN_SIZE]; + if (ecs_meta_is_collection(&cur)) { + char *path = ecs_get_fullpath(world, scope_type); + ecs_parser_error(name, expr, ptr - expr, + "expected '[' for collection type '%s'", path); + ecs_os_free(path); + return NULL; + } + } - const char *prev = value, *ptr = value; + else if (!ecs_os_strcmp(token, "}")) { + depth --; - *(ecs_u32_t*)out = 0; + if (ecs_meta_is_collection(&cur)) { + ecs_parser_error(name, expr, ptr - expr, "expected ']'"); + return NULL; + } - while ((ptr = strchr(ptr, '|'))) { - ecs_os_memcpy(token, prev, ptr - prev); - token[ptr - prev] = '\0'; - if (add_bitmask_constant(cursor, op, out, token) != 0) { - return -1; + if (ecs_meta_pop(&cur) != 0) { + goto error; + } } - ptr ++; - prev = ptr; - } + else if (!ecs_os_strcmp(token, "[")) { + depth ++; + if (ecs_meta_push(&cur) != 0) { + goto error; + } - if (add_bitmask_constant(cursor, op, out, prev) != 0) { - return -1; - } + if (!ecs_meta_is_collection(&cur)) { + ecs_parser_error(name, expr, ptr - expr, "expected '{'"); + return NULL; + } + } - return 0; -} + else if (!ecs_os_strcmp(token, "]")) { + depth --; -int ecs_meta_set_string( - ecs_meta_cursor_t *cursor, - const char *value) -{ - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); + if (!ecs_meta_is_collection(&cur)) { + ecs_parser_error(name, expr, ptr - expr, "expected '}'"); + return NULL; + } - switch(op->kind) { - case EcsOpBool: - if (!ecs_os_strcmp(value, "true")) { - set_T(ecs_bool_t, ptr, true); - } else if (!ecs_os_strcmp(value, "false")) { - set_T(ecs_bool_t, ptr, false); - } else { - ecs_err("invalid value for boolean '%s'", value); - return -1; + if (ecs_meta_pop(&cur) != 0) { + goto error; + } } - break; - case EcsOpI8: - case EcsOpU8: - case EcsOpChar: - case EcsOpByte: - set_T(ecs_i8_t, ptr, atol(value)); - break; - case EcsOpI16: - case EcsOpU16: - set_T(ecs_i16_t, ptr, atol(value)); - break; - case EcsOpI32: - case EcsOpU32: - set_T(ecs_i32_t, ptr, atol(value)); - break; - case EcsOpI64: - case EcsOpU64: - set_T(ecs_i64_t, ptr, atol(value)); - break; - case EcsOpIPtr: - case EcsOpUPtr: - set_T(ecs_iptr_t, ptr, atol(value)); - break; - case EcsOpF32: - set_T(ecs_f32_t, ptr, atof(value)); - break; - case EcsOpF64: - set_T(ecs_f64_t, ptr, atof(value)); - break; - case EcsOpString: { - ecs_os_free(*(char**)ptr); - char *result = ecs_os_strdup(value); - set_T(ecs_string_t, ptr, result); - break; - } - case EcsOpEnum: { - ecs_assert(op->type != 0, ECS_INTERNAL_ERROR, NULL); - ecs_entity_t c = ecs_lookup_child(cursor->world, op->type, value); - if (!c) { - char *path = ecs_get_fullpath(cursor->world, op->type); - ecs_err("unresolved enum constant '%s' for type '%s'", value, path); - ecs_os_free(path); - return -1; + + else if (!ecs_os_strcmp(token, ",")) { + if (ecs_meta_next(&cur) != 0) { + goto error; + } } - const ecs_i32_t *v = ecs_get_pair_object( - cursor->world, c, EcsConstant, ecs_i32_t); - if (v == NULL) { - char *path = ecs_get_fullpath(cursor->world, op->type); - ecs_err("'%s' is not an enum constant for type '%s'", value, path); - ecs_os_free(path); - return -1; + else if (!ecs_os_strcmp(token, "null")) { + if (ecs_meta_set_null(&cur) != 0) { + goto error; + } } - set_T(ecs_i32_t, ptr, v[0]); - break; - } - case EcsOpBitmask: - if (parse_bitmask(cursor, op, ptr, value) != 0) { - return -1; + else if (token[0] == '\"') { + if (ecs_meta_set_string_literal(&cur, token) != 0) { + goto error; + } } - break; - case EcsOpEntity: { - ecs_entity_t e = 0; - if (ecs_os_strcmp(value, "0")) { - if (cursor->lookup_action) { - e = cursor->lookup_action( - cursor->world, value, - cursor->lookup_ctx); + else { + ptr = ecs_parse_fluff(ptr, NULL); + + if (ptr[0] == ':') { + /* Member assignment */ + ptr ++; + if (ecs_meta_member(&cur, token) != 0) { + goto error; + } } else { - e = ecs_lookup_path(cursor->world, 0, value); + if (ecs_meta_set_string(&cur, token) != 0) { + goto error; + } } + } - if (!e) { - ecs_err("unresolved entity identifier '%s'", value); - return -1; - } + if (!depth) { + break; } - set_T(ecs_entity_t, ptr, e); - break; - } - case EcsOpPop: - ecs_err("excess element '%s' in scope", value); - return -1; - default: - ecs_err("unsupported conversion from string '%s' to '%s'", - value, op_kind_str(op->kind)); - return -1; + ptr = ecs_parse_fluff(ptr, NULL); } - return 0; + return ptr; +error: + return NULL; } -int ecs_meta_set_string_literal( - ecs_meta_cursor_t *cursor, - const char *value) -{ - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); +#endif - ecs_size_t len = ecs_os_strlen(value); - if (value[0] != '\"' || value[len - 1] != '\"') { - ecs_err("invalid string literal '%s'", value); - return -1; - } - switch(op->kind) { - case EcsOpChar: - set_T(ecs_char_t, ptr, value[1]); +#ifdef FLECS_EXPR + +char* ecs_chresc( + char *out, + char in, + char delimiter) +{ + char *bptr = out; + switch(in) { + case '\a': + *bptr++ = '\\'; + *bptr = 'a'; + break; + case '\b': + *bptr++ = '\\'; + *bptr = 'b'; + break; + case '\f': + *bptr++ = '\\'; + *bptr = 'f'; + break; + case '\n': + *bptr++ = '\\'; + *bptr = 'n'; + break; + case '\r': + *bptr++ = '\\'; + *bptr = 'r'; + break; + case '\t': + *bptr++ = '\\'; + *bptr = 't'; + break; + case '\v': + *bptr++ = '\\'; + *bptr = 'v'; + break; + case '\\': + *bptr++ = '\\'; + *bptr = '\\'; break; - default: - case EcsOpEntity: - case EcsOpString: - len -= 2; + if (in == delimiter) { + *bptr++ = '\\'; + *bptr = delimiter; + } else { + *bptr = in; + } + break; + } - char *result = ecs_os_malloc(len + 1); - ecs_os_memcpy(result, value + 1, len); - result[len] = '\0'; + *(++bptr) = '\0'; - if (ecs_meta_set_string(cursor, result)) { - ecs_os_free(result); - return -1; - } + return bptr; +} - ecs_os_free(result); +const char* ecs_chrparse( + const char *in, + char *out) +{ + const char *result = in + 1; + char ch; - break; + if (in[0] == '\\') { + result ++; + + switch(in[1]) { + case 'a': + ch = '\a'; + break; + case 'b': + ch = '\b'; + break; + case 'f': + ch = '\f'; + break; + case 'n': + ch = '\n'; + break; + case 'r': + ch = '\r'; + break; + case 't': + ch = '\t'; + break; + case 'v': + ch = '\v'; + break; + case '\\': + ch = '\\'; + break; + case '"': + ch = '"'; + break; + case '0': + ch = '\0'; + break; + case ' ': + ch = ' '; + break; + case '$': + ch = '$'; + break; + default: + goto error; + } + } else { + ch = in[0]; } - return 0; + if (out) { + *out = ch; + } + + return result; +error: + return NULL; } -int ecs_meta_set_entity( - ecs_meta_cursor_t *cursor, - ecs_entity_t value) +ecs_size_t ecs_stresc( + char *out, + ecs_size_t n, + char delimiter, + const char *in) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); - - switch(op->kind) { - case EcsOpEntity: - set_T(ecs_entity_t, ptr, value); - break; - default: - conversion_error(cursor, op, "entity"); - return -1; + const char *ptr = in; + char ch, *bptr = out, buff[3]; + ecs_size_t written = 0; + while ((ch = *ptr++)) { + if ((written += (ecs_size_t)(ecs_chresc(buff, ch, delimiter) - buff)) <= n) { + /* If size != 0, an out buffer must be provided. */ + ecs_assert(out != NULL, ECS_INVALID_PARAMETER, NULL); + *bptr++ = buff[0]; + if ((ch = buff[1])) { + *bptr = ch; + bptr++; + } + } } - return 0; + if (bptr) { + while (written < n) { + *bptr = '\0'; + bptr++; + written++; + } + } + return written; } -int ecs_meta_set_null( - ecs_meta_cursor_t *cursor) +char* ecs_astresc( + char delimiter, + const char *in) { - ecs_meta_scope_t *scope = get_scope(cursor); - ecs_meta_type_op_t *op = get_op(scope); - void *ptr = get_ptr(cursor->world, scope); - switch (op->kind) { - case EcsOpString: - ecs_os_free(*(char**)ptr); - set_T(ecs_string_t, ptr, NULL); - break; - default: - conversion_error(cursor, op, "null"); - return -1; + if (!in) { + return NULL; } - return 0; + ecs_size_t len = ecs_stresc(NULL, 0, delimiter, in); + char *out = ecs_os_malloc_n(char, len + 1); + ecs_stresc(out, len, delimiter, in); + out[len] = '\0'; + return out; } #endif +#ifdef FLECS_APP -#ifdef FLECS_EXPR - -static -int expr_ser_type( - const ecs_world_t *world, - ecs_vector_t *ser, - const void *base, - ecs_strbuf_t *str); +static ecs_frame_action_t frame_action; -static -int expr_ser_type_ops( - const ecs_world_t *world, - ecs_meta_type_op_t *ops, - int32_t op_count, - const void *base, - ecs_strbuf_t *str); +int ecs_app_run( + ecs_world_t *world, + const ecs_app_desc_t *desc) +{ + ecs_set_target_fps(world, desc->target_fps); + ecs_set_threads(world, desc->threads); -static -int expr_ser_type_op( - const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *base, - ecs_strbuf_t *str); + ecs_frame_action_t callback = frame_action; + if (!callback) { + callback = ecs_app_run_frame; + } -static -ecs_primitive_kind_t expr_op_to_primitive_kind(ecs_meta_type_op_kind_t kind) { - return kind - EcsOpPrimitive; + int result; + while ((result = callback(world, desc)) == 0) { } + + return result; } -/* Serialize a primitive value */ -static -int expr_ser_primitive( - const ecs_world_t *world, - ecs_primitive_kind_t kind, - const void *base, - ecs_strbuf_t *str) +int ecs_app_run_frame( + ecs_world_t *world, + const ecs_app_desc_t *desc) { - const char *bool_str[] = { "false", "true" }; + return ecs_progress(world, desc->delta_time); +} - switch(kind) { - case EcsBool: - ecs_strbuf_appendstr(str, bool_str[(int)*(bool*)base]); - break; - case EcsChar: { - char chbuf[3]; - char ch = *(char*)base; - if (ch) { - ecs_chresc(chbuf, *(char*)base, '"'); - ecs_strbuf_appendstrn(str, "\"", 1); - ecs_strbuf_appendstr(str, chbuf); - ecs_strbuf_appendstrn(str, "\"", 1); - } else { - ecs_strbuf_appendstr(str, "0"); - } - break; - } - case EcsByte: - ecs_strbuf_append(str, "%u", *(uint8_t*)base); - break; - case EcsU8: - ecs_strbuf_append(str, "%u", *(uint8_t*)base); - break; - case EcsU16: - ecs_strbuf_append(str, "%u", *(uint16_t*)base); - break; - case EcsU32: - ecs_strbuf_append(str, "%u", *(uint32_t*)base); - break; - case EcsU64: - ecs_strbuf_append(str, "%llu", *(uint64_t*)base); - break; - case EcsI8: - ecs_strbuf_append(str, "%d", *(int8_t*)base); - break; - case EcsI16: - ecs_strbuf_append(str, "%d", *(int16_t*)base); - break; - case EcsI32: - ecs_strbuf_append(str, "%d", *(int32_t*)base); - break; - case EcsI64: - ecs_strbuf_append(str, "%lld", *(int64_t*)base); - break; - case EcsF32: - ecs_strbuf_append(str, "%f", (double)*(float*)base); - break; - case EcsF64: - ecs_strbuf_append(str, "%f", *(double*)base); - break; - case EcsIPtr: - ecs_strbuf_append(str, "%i", *(intptr_t*)base); - break; - case EcsUPtr: - ecs_strbuf_append(str, "%u", *(uintptr_t*)base); - break; - case EcsString: { - char *value = *(char**)base; - if (value) { - ecs_size_t length = ecs_stresc(NULL, 0, '"', value); - if (length == ecs_os_strlen(value)) { - ecs_strbuf_appendstrn(str, "\"", 1); - ecs_strbuf_appendstr(str, value); - ecs_strbuf_appendstrn(str, "\"", 1); - } else { - char *out = ecs_os_malloc(length + 3); - ecs_stresc(out + 1, length, '"', value); - out[0] = '"'; - out[length + 1] = '"'; - out[length + 2] = '\0'; - ecs_strbuf_appendstr_zerocpy(str, out); - } - } else { - ecs_strbuf_appendstr(str, "null"); - } - break; - } - case EcsEntity: { - ecs_entity_t e = *(ecs_entity_t*)base; - if (!e) { - ecs_strbuf_appendstr(str, "0"); - } else { - char *path = ecs_get_fullpath(world, e); - ecs_assert(path != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_strbuf_appendstr(str, path); - ecs_os_free(path); - } - break; - } - default: - ecs_err("invalid primitive kind"); +int ecs_app_set_frame_action( + ecs_frame_action_t callback) +{ + if (frame_action) { + ecs_err("frame action already set"); return -1; } + frame_action = callback; + return 0; } -/* Serialize enumeration */ +#endif + +#ifdef FLECS_SYSTEM + + static -int expr_ser_enum( - const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *base, - ecs_strbuf_t *str) +void invoke_status_action( + ecs_world_t *world, + ecs_entity_t system, + const EcsSystem *system_data, + ecs_system_status_t status) { - const EcsEnum *enum_type = ecs_get(world, op->type, EcsEnum); - ecs_assert(enum_type != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_system_status_action_t action = system_data->status_action; + if (action) { + action(world, system, status, system_data->status_ctx); + } +} - int32_t value = *(int32_t*)base; - - /* Enumeration constants are stored in a map that is keyed on the - * enumeration value. */ - ecs_enum_constant_t *constant = ecs_map_get( - enum_type->constants, ecs_enum_constant_t, value); - if (!constant) { - char *path = ecs_get_fullpath(world, op->type); - ecs_err("value %d is not valid for enum type '%s'", value, path); - ecs_os_free(path); - return -1; +/* Invoked when system becomes active or inactive */ +void ecs_system_activate( + ecs_world_t *world, + ecs_entity_t system, + bool activate, + const EcsSystem *system_data) +{ + ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); + + if (activate) { + /* If activating system, ensure that it doesn't have the Inactive tag. + * Systems are implicitly activated so they are kept out of the main + * loop as long as they aren't used. They are not implicitly deactivated + * to prevent overhead in case of oscillating app behavior. + * After activation, systems that aren't matched with anything can be + * deactivated again by explicitly calling ecs_deactivate_systems. + */ + ecs_remove_id(world, system, EcsInactive); } - ecs_strbuf_appendstr(str, ecs_get_name(world, constant->constant)); + if (!system_data) { + system_data = ecs_get(world, system, EcsSystem); + } + if (!system_data || !system_data->query) { + return; + } - return 0; + if (!activate) { + if (ecs_has_id(world, system, EcsDisabled)) { + if (!ecs_query_table_count(system_data->query)) { + /* If deactivating a disabled system that isn't matched with + * any active tables, there is nothing to deactivate. */ + return; + } + } + } + + /* Invoke system status action */ + invoke_status_action(world, system, system_data, + activate ? EcsSystemActivated : EcsSystemDeactivated); + + ecs_dbg_1("system #[green]%s#[reset] %s", + ecs_get_name(world, system), + activate ? "activated" : "deactivated"); } -/* Serialize bitmask */ +/* Actually enable or disable system */ static -int expr_ser_bitmask( - const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *ptr, - ecs_strbuf_t *str) +void ecs_enable_system( + ecs_world_t *world, + ecs_entity_t system, + EcsSystem *system_data, + bool enabled) { - const EcsBitmask *bitmask_type = ecs_get(world, op->type, EcsBitmask); - ecs_assert(bitmask_type != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_poly_assert(world, ecs_world_t); + ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); - uint32_t value = *(uint32_t*)ptr; - ecs_map_key_t key; - ecs_bitmask_constant_t *constant; - int count = 0; + ecs_query_t *query = system_data->query; + if (!query) { + return; + } - ecs_strbuf_list_push(str, "", "|"); + if (ecs_query_table_count(query)) { + /* Only (de)activate system if it has non-empty tables. */ + ecs_system_activate(world, system, enabled, system_data); + system_data = ecs_get_mut(world, system, EcsSystem, NULL); + } + + /* Invoke action for enable/disable status */ + invoke_status_action( + world, system, system_data, + enabled ? EcsSystemEnabled : EcsSystemDisabled); +} - /* Multiple flags can be set at a given time. Iterate through all the flags - * and append the ones that are set. */ - ecs_map_iter_t it = ecs_map_iter(bitmask_type->constants); - while ((constant = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { - if ((value & key) == key) { - ecs_strbuf_list_appendstr(str, - ecs_get_name(world, constant->constant)); - count ++; - value -= (uint32_t)key; +/* -- Public API -- */ + +void ecs_enable( + ecs_world_t *world, + ecs_entity_t entity, + bool enabled) +{ + ecs_poly_assert(world, ecs_world_t); + + const EcsType *type_ptr = ecs_get( world, entity, EcsType); + if (type_ptr) { + /* If entity is a type, disable all entities in the type */ + ecs_vector_each(type_ptr->normalized, ecs_entity_t, e, { + ecs_enable(world, *e, enabled); + }); + } else { + if (enabled) { + ecs_remove_id(world, entity, EcsDisabled); + } else { + ecs_add_id(world, entity, EcsDisabled); } } +} - if (value != 0) { - /* All bits must have been matched by a constant */ - char *path = ecs_get_fullpath(world, op->type); - ecs_err( - "value for bitmask %s contains bits (%u) that cannot be mapped to constant", - path, value); - ecs_os_free(path); - goto error; +ecs_entity_t ecs_run_intern( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t system, + EcsSystem *system_data, + int32_t stage_current, + int32_t stage_count, + FLECS_FLOAT delta_time, + int32_t offset, + int32_t limit, + void *param) +{ + FLECS_FLOAT time_elapsed = delta_time; + ecs_entity_t tick_source = system_data->tick_source; + + /* Support legacy behavior */ + if (!param) { + param = system_data->ctx; } - if (!count) { - ecs_strbuf_list_appendstr(str, "0"); + if (tick_source) { + const EcsTickSource *tick = ecs_get( + world, tick_source, EcsTickSource); + + if (tick) { + time_elapsed = tick->time_elapsed; + + /* If timer hasn't fired we shouldn't run the system */ + if (!tick->tick) { + return 0; + } + } else { + /* If a timer has been set but the timer entity does not have the + * EcsTimer component, don't run the system. This can be the result + * of a single-shot timer that has fired already. Not resetting the + * timer field of the system will ensure that the system won't be + * ran after the timer has fired. */ + return 0; + } } - ecs_strbuf_list_pop(str, ""); + ecs_time_t time_start; + bool measure_time = world->measure_system_time; + if (measure_time) { + ecs_os_get_time(&time_start); + } - return 0; -error: - return -1; -} + ecs_defer_begin(stage->thread_ctx); -/* Serialize elements of a contiguous array */ -static -int expr_ser_elements( - const ecs_world_t *world, - ecs_meta_type_op_t *ops, - int32_t op_count, - const void *base, - int32_t elem_count, - int32_t elem_size, - ecs_strbuf_t *str) -{ - ecs_strbuf_list_push(str, "[", ", "); + /* Prepare the query iterator */ + ecs_iter_t it = ecs_query_iter_page( + stage->thread_ctx, system_data->query, offset, limit); - const void *ptr = base; + it.system = system; + it.self = system_data->self; + it.delta_time = delta_time; + it.delta_system_time = time_elapsed; + it.frame_offset = offset; + it.param = param; + it.ctx = system_data->ctx; + it.binding_ctx = system_data->binding_ctx; - int i; - for (i = 0; i < elem_count; i ++) { - ecs_strbuf_list_next(str); - if (expr_ser_type_ops(world, ops, op_count, ptr, str)) { - return -1; + ecs_iter_action_t action = system_data->action; + + /* If no filter is provided, just iterate tables & invoke action */ + if (stage_count <= 1) { + while (ecs_query_next(&it)) { + action(&it); + } + } else { + while (ecs_query_next_worker(&it, stage_current, stage_count)) { + action(&it); } - ptr = ECS_OFFSET(ptr, elem_size); } - ecs_strbuf_list_pop(str, "]"); + ecs_defer_end(stage->thread_ctx); - return 0; + if (measure_time) { + system_data->time_spent += (FLECS_FLOAT)ecs_time_measure(&time_start); + } + + system_data->invoke_count ++; + + return it.interrupted_by; } -static -int expr_ser_type_elements( - const ecs_world_t *world, - ecs_entity_t type, - const void *base, - int32_t elem_count, - ecs_strbuf_t *str) -{ - const EcsMetaTypeSerialized *ser = ecs_get( - world, type, EcsMetaTypeSerialized); - ecs_assert(ser != NULL, ECS_INTERNAL_ERROR, NULL); +/* -- Public API -- */ - const EcsComponent *comp = ecs_get(world, type, EcsComponent); - ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); +ecs_entity_t ecs_run_w_filter( + ecs_world_t *world, + ecs_entity_t system, + FLECS_FLOAT delta_time, + int32_t offset, + int32_t limit, + void *param) +{ + ecs_stage_t *stage = flecs_stage_from_world(&world); - ecs_meta_type_op_t *ops = ecs_vector_first(ser->ops, ecs_meta_type_op_t); - int32_t op_count = ecs_vector_count(ser->ops); + EcsSystem *system_data = (EcsSystem*)ecs_get( + world, system, EcsSystem); + assert(system_data != NULL); - return expr_ser_elements( - world, ops, op_count, base, elem_count, comp->size, str); + return ecs_run_intern(world, stage, system, system_data, 0, 0, delta_time, + offset, limit, param); } -/* Serialize array */ -static -int expr_ser_array( - const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *ptr, - ecs_strbuf_t *str) +ecs_entity_t ecs_run_worker( + ecs_world_t *world, + ecs_entity_t system, + int32_t stage_current, + int32_t stage_count, + FLECS_FLOAT delta_time, + void *param) { - const EcsArray *a = ecs_get(world, op->type, EcsArray); - ecs_assert(a != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_stage_t *stage = flecs_stage_from_world(&world); - return expr_ser_type_elements( - world, a->type, ptr, a->count, str); + EcsSystem *system_data = (EcsSystem*)ecs_get( + world, system, EcsSystem); + assert(system_data != NULL); + + return ecs_run_intern( + world, stage, system, system_data, stage_current, stage_count, + delta_time, 0, 0, param); } -/* Serialize vector */ -static -int expr_ser_vector( +ecs_entity_t ecs_run( + ecs_world_t *world, + ecs_entity_t system, + FLECS_FLOAT delta_time, + void *param) +{ + return ecs_run_w_filter(world, system, delta_time, 0, 0, param); +} + +ecs_query_t* ecs_system_get_query( const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *base, - ecs_strbuf_t *str) + ecs_entity_t system) { - ecs_vector_t *value = *(ecs_vector_t**)base; - if (!value) { - ecs_strbuf_appendstr(str, "null"); - return 0; + const EcsQuery *q = ecs_get(world, system, EcsQuery); + if (q) { + return q->query; + } else { + const EcsSystem *s = ecs_get(world, system, EcsSystem); + if (s) { + return s->query; + } else { + return NULL; + } } +} - const EcsVector *v = ecs_get(world, op->type, EcsVector); - ecs_assert(v != NULL, ECS_INTERNAL_ERROR, NULL); - - const EcsComponent *comp = ecs_get(world, v->type, EcsComponent); - ecs_assert(comp != NULL, ECS_INTERNAL_ERROR, NULL); - - int32_t count = ecs_vector_count(value); - void *array = ecs_vector_first_t(value, comp->size, comp->alignment); - - /* Serialize contiguous buffer of vector */ - return expr_ser_type_elements(world, v->type, array, count, str); +void* ecs_get_system_ctx( + const ecs_world_t *world, + ecs_entity_t system) +{ + const EcsSystem *s = ecs_get(world, system, EcsSystem); + if (s) { + return s->ctx; + } else { + return NULL; + } } -/* Forward serialization to the different type kinds */ -static -int expr_ser_type_op( +void* ecs_get_system_binding_ctx( const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *ptr, - ecs_strbuf_t *str) + ecs_entity_t system) { - switch(op->kind) { - case EcsOpPush: - case EcsOpPop: - /* Should not be parsed as single op */ - ecs_abort(ECS_INVALID_PARAMETER, NULL); - break; - case EcsOpEnum: - if (expr_ser_enum(world, op, ECS_OFFSET(ptr, op->offset), str)) { - goto error; - } - break; - case EcsOpBitmask: - if (expr_ser_bitmask(world, op, ECS_OFFSET(ptr, op->offset), str)) { - goto error; - } - break; - case EcsOpArray: - if (expr_ser_array(world, op, ECS_OFFSET(ptr, op->offset), str)) { - goto error; - } - break; - case EcsOpVector: - if (expr_ser_vector(world, op, ECS_OFFSET(ptr, op->offset), str)) { - goto error; - } - break; - default: - if (expr_ser_primitive(world, expr_op_to_primitive_kind(op->kind), - ECS_OFFSET(ptr, op->offset), str)) - { - /* Unknown operation */ - ecs_abort(ECS_INTERNAL_ERROR, NULL); - goto error; - } - break; - } + const EcsSystem *s = ecs_get(world, system, EcsSystem); + if (s) { + return s->binding_ctx; + } else { + return NULL; + } +} - return 0; -error: - return -1; +/* Generic constructor to initialize a component to 0 */ +static +void sys_ctor_init_zero( + ecs_world_t *world, + ecs_entity_t component, + const ecs_entity_t *entities, + void *ptr, + size_t size, + int32_t count, + void *ctx) +{ + (void)world; + (void)component; + (void)entities; + (void)ctx; + memset(ptr, 0, size * (size_t)count); } -/* Iterate over a slice of the type ops array */ +/* System destructor */ static -int expr_ser_type_ops( - const ecs_world_t *world, - ecs_meta_type_op_t *ops, - int32_t op_count, - const void *base, - ecs_strbuf_t *str) +void ecs_colsystem_dtor( + ecs_world_t *world, + ecs_entity_t component, + const ecs_entity_t *entities, + void *ptr, + size_t size, + int32_t count, + void *ctx) { - for (int i = 0; i < op_count; i ++) { - ecs_meta_type_op_t *op = &ops[i]; + (void)component; + (void)ctx; + (void)size; - if (op != ops) { - if (op->name) { - ecs_strbuf_list_next(str); - ecs_strbuf_append(str, "%s: ", op->name); - } + EcsSystem *system_data = ptr; - int32_t elem_count = op->count; - if (elem_count > 1 && op != ops) { - /* Serialize inline array */ - if (expr_ser_elements(world, op, op->op_count, base, - elem_count, op->size, str)) - { - return -1; - } + int i; + for (i = 0; i < count; i ++) { + EcsSystem *system = &system_data[i]; + ecs_entity_t e = entities[i]; - i += op->op_count - 1; - continue; - } + if (!ecs_is_alive(world, e)) { + /* This can happen when a set is deferred while a system is being + * cleaned up. The operation will be discarded, but the destructor + * still needs to be invoked for the value */ + continue; } - - switch(op->kind) { - case EcsOpPush: - ecs_strbuf_list_push(str, "{", ", "); - break; - case EcsOpPop: - ecs_strbuf_list_pop(str, "}"); - break; - default: - if (expr_ser_type_op(world, op, base, str)) { - goto error; - } - break; + + /* Invoke Deactivated action for active systems */ + if (system->query && ecs_query_table_count(system->query)) { + invoke_status_action(world, e, ptr, EcsSystemDeactivated); } - } - return 0; -error: - return -1; + /* Invoke Disabled action for enabled systems */ + if (!ecs_has_id(world, e, EcsDisabled)) { + invoke_status_action(world, e, ptr, EcsSystemDisabled); + } + + if (system->ctx_free) { + system->ctx_free(system->ctx); + } + + if (system->status_ctx_free) { + system->status_ctx_free(system->status_ctx); + } + + if (system->binding_ctx_free) { + system->binding_ctx_free(system->binding_ctx); + } + + if (system->query) { + ecs_query_fini(system->query); + } + } } -/* Iterate over the type ops of a type */ static -int expr_ser_type( - const ecs_world_t *world, - ecs_vector_t *v_ops, - const void *base, - ecs_strbuf_t *str) +void EnableMonitor( + ecs_iter_t *it) { - ecs_meta_type_op_t *ops = ecs_vector_first(v_ops, ecs_meta_type_op_t); - int32_t count = ecs_vector_count(v_ops); - return expr_ser_type_ops(world, ops, count, base, str); + EcsSystem *sys = ecs_term(it, EcsSystem, 1); + + int32_t i; + for (i = 0; i < it->count; i ++) { + if (it->event == EcsOnAdd) { + ecs_enable_system(it->world, it->entities[i], &sys[i], true); + } else if (it->event == EcsOnRemove) { + ecs_enable_system(it->world, it->entities[i], &sys[i], false); + } + } } -int ecs_ptr_to_expr_buf( - const ecs_world_t *world, - ecs_entity_t type, - const void *ptr, - ecs_strbuf_t *buf_out) +ecs_entity_t ecs_system_init( + ecs_world_t *world, + const ecs_system_desc_t *desc) { - const EcsMetaTypeSerialized *ser = ecs_get( - world, type, EcsMetaTypeSerialized); - if (ser == NULL) { - char *path = ecs_get_fullpath(world, type); - ecs_err("cannot serialize value for type '%s'", path); - ecs_os_free(path); - goto error; - } + ecs_poly_assert(world, ecs_world_t); + ecs_assert(!world->is_readonly, ECS_INVALID_WHILE_ITERATING, NULL); - if (expr_ser_type(world, ser->ops, ptr, buf_out)) { - goto error; + ecs_entity_t existing = desc->entity.entity; + ecs_entity_t result = ecs_entity_init(world, &desc->entity); + if (!result) { + return 0; } - return 0; -error: - return -1; -} + bool added = false; + EcsSystem *system = ecs_get_mut(world, result, EcsSystem, &added); + if (added) { + ecs_assert(desc->callback != NULL, ECS_INVALID_PARAMETER, NULL); -char* ecs_ptr_to_expr( - const ecs_world_t *world, - ecs_entity_t type, - const void* ptr) -{ - ecs_strbuf_t str = ECS_STRBUF_INIT; + memset(system, 0, sizeof(EcsSystem)); - if (ecs_ptr_to_expr_buf(world, type, ptr, &str) != 0) { - ecs_strbuf_reset(&str); - return NULL; - } + ecs_query_desc_t query_desc = desc->query; + query_desc.filter.name = desc->entity.name; + query_desc.system = result; - return ecs_strbuf_get(&str); -} + ecs_query_t *query = ecs_query_init(world, &query_desc); + if (!query) { + ecs_delete(world, result); + return 0; + } -int ecs_primitive_to_expr_buf( - const ecs_world_t *world, - ecs_primitive_kind_t kind, - const void *base, - ecs_strbuf_t *str) -{ - return expr_ser_primitive(world, kind, base, str); -} + /* Re-obtain pointer, as query may have added components */ + system = ecs_get_mut(world, result, EcsSystem, &added); + ecs_assert(added == false, ECS_INTERNAL_ERROR, NULL); -#endif + /* Prevent the system from moving while we're initializing */ + ecs_defer_begin(world); + system->entity = result; + system->query = query; -#ifdef FLECS_EXPR + system->action = desc->callback; + system->status_action = desc->status_callback; -char* ecs_chresc( - char *out, - char in, - char delimiter) -{ - char *bptr = out; - switch(in) { - case '\a': - *bptr++ = '\\'; - *bptr = 'a'; - break; - case '\b': - *bptr++ = '\\'; - *bptr = 'b'; - break; - case '\f': - *bptr++ = '\\'; - *bptr = 'f'; - break; - case '\n': - *bptr++ = '\\'; - *bptr = 'n'; - break; - case '\r': - *bptr++ = '\\'; - *bptr = 'r'; - break; - case '\t': - *bptr++ = '\\'; - *bptr = 't'; - break; - case '\v': - *bptr++ = '\\'; - *bptr = 'v'; - break; - case '\\': - *bptr++ = '\\'; - *bptr = '\\'; - break; - default: - if (in == delimiter) { - *bptr++ = '\\'; - *bptr = delimiter; + system->self = desc->self; + system->ctx = desc->ctx; + system->status_ctx = desc->status_ctx; + system->binding_ctx = desc->binding_ctx; + + system->ctx_free = desc->ctx_free; + system->status_ctx_free = desc->status_ctx_free; + system->binding_ctx_free = desc->binding_ctx_free; + + system->tick_source = desc->tick_source; + + /* If tables have been matched with this system it is active, and we + * should activate the in terms, if any. This will ensure that any + * OnDemand systems get enabled. */ + if (ecs_query_table_count(query)) { + ecs_system_activate(world, result, true, system); } else { - *bptr = in; + /* If system isn't matched with any tables, mark it as inactive. This + * causes it to be ignored by the main loop. When the system matches + * with a table it will be activated. */ + ecs_add_id(world, result, EcsInactive); } - break; - } - *(++bptr) = '\0'; + if (!ecs_has_id(world, result, EcsDisabled)) { + /* If system is already enabled, generate enable status. The API + * should guarantee that it exactly matches enable-disable + * notifications and activate-deactivate notifications. */ + invoke_status_action(world, result, system, EcsSystemEnabled); - return bptr; -} + /* If column system has active (non-empty) tables, also generate the + * activate status. */ + if (ecs_query_table_count(system->query)) { + invoke_status_action(world, result, system, EcsSystemActivated); + } + } -const char* ecs_chrparse( - const char *in, - char *out) -{ - const char *result = in + 1; - char ch; + if (desc->interval != 0 || desc->rate != 0 || desc->tick_source != 0) { +#ifdef FLECS_TIMER + if (desc->interval != 0) { + ecs_set_interval(world, result, desc->interval); + } - if (in[0] == '\\') { - result ++; + if (desc->rate) { + ecs_set_rate(world, result, desc->rate, desc->tick_source); + } else if (desc->tick_source) { + ecs_set_tick_source(world, result, desc->tick_source); + } +#else + ecs_abort(ECS_UNSUPPORTED, "timer module not available"); +#endif + } - switch(in[1]) { - case 'a': - ch = '\a'; - break; - case 'b': - ch = '\b'; - break; - case 'f': - ch = '\f'; - break; - case 'n': - ch = '\n'; - break; - case 'r': - ch = '\r'; - break; - case 't': - ch = '\t'; - break; - case 'v': - ch = '\v'; - break; - case '\\': - ch = '\\'; - break; - case '"': - ch = '"'; - break; - case '0': - ch = '\0'; - break; - case ' ': - ch = ' '; - break; - case '$': - ch = '$'; - break; - default: - goto error; + ecs_modified(world, result, EcsSystem); + + if (desc->entity.name) { + ecs_trace("#[green]system#[reset] %s created", + ecs_get_name(world, result)); } + + ecs_defer_end(world); } else { - ch = in[0]; - } + const char *expr_desc = desc->query.filter.expr; + const char *expr_sys = system->query->filter.expr; - if (out) { - *out = ch; - } + /* Only check expression if it's set */ + if (expr_desc) { + if (expr_sys && !strcmp(expr_sys, "0")) expr_sys = NULL; + if (expr_desc && !strcmp(expr_desc, "0")) expr_desc = NULL; - return result; -error: - return NULL; -} + if (expr_sys && expr_desc) { + if (strcmp(expr_sys, expr_desc)) { + ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); + } + } else { + if (expr_sys != expr_desc) { + ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); + } + } -ecs_size_t ecs_stresc( - char *out, - ecs_size_t n, - char delimiter, - const char *in) -{ - const char *ptr = in; - char ch, *bptr = out, buff[3]; - ecs_size_t written = 0; - while ((ch = *ptr++)) { - if ((written += (ecs_size_t)(ecs_chresc(buff, ch, delimiter) - buff)) <= n) { - /* If size != 0, an out buffer must be provided. */ - ecs_assert(out != NULL, ECS_INVALID_PARAMETER, NULL); - *bptr++ = buff[0]; - if ((ch = buff[1])) { - *bptr = ch; - bptr++; + /* If expr_desc is not set, and this is an existing system, don't throw + * an error because we could be updating existing parameters of the + * system such as the context or system callback. However, if no + * entity handle was provided, we have to assume that the application is + * trying to redeclare the system. */ + } else if (!existing) { + if (expr_sys) { + ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); } } - } - if (bptr) { - while (written < n) { - *bptr = '\0'; - bptr++; - written++; + /* Override the existing callback or context */ + if (desc->callback) { + system->action = desc->callback; + } + if (desc->ctx) { + system->ctx = desc->ctx; + } + if (desc->binding_ctx) { + system->binding_ctx = desc->binding_ctx; } } - return written; + + return result; } -char* ecs_astresc( - char delimiter, - const char *in) +void FlecsSystemImport( + ecs_world_t *world) { - if (!in) { - return NULL; - } + ECS_MODULE(world, FlecsSystem); - ecs_size_t len = ecs_stresc(NULL, 0, delimiter, in); - char *out = ecs_os_malloc_n(char, len + 1); - ecs_stresc(out, len, delimiter, in); - out[len] = '\0'; - return out; + ecs_set_name_prefix(world, "Ecs"); + + flecs_bootstrap_component(world, EcsSystem); + flecs_bootstrap_component(world, EcsTickSource); + + flecs_bootstrap_tag(world, EcsOnAdd); + flecs_bootstrap_tag(world, EcsOnRemove); + flecs_bootstrap_tag(world, EcsOnSet); + flecs_bootstrap_tag(world, EcsUnSet); + + /* Put following tags in flecs.core so they can be looked up + * without using the flecs.systems prefix. */ + ecs_entity_t old_scope = ecs_set_scope(world, EcsFlecsCore); + flecs_bootstrap_tag(world, EcsInactive); + flecs_bootstrap_tag(world, EcsMonitor); + ecs_set_scope(world, old_scope); + + /* Bootstrap ctor and dtor for EcsSystem */ + ecs_set_component_actions_w_id(world, ecs_id(EcsSystem), + &(EcsComponentLifecycle) { + .ctor = sys_ctor_init_zero, + .dtor = ecs_colsystem_dtor + }); + + ECS_OBSERVER(world, EnableMonitor, EcsMonitor, System, !Disabled); } #endif +#ifdef FLECS_PIPELINE -#ifdef FLECS_EXPR -const char *ecs_parse_expr_token( - const char *name, - const char *expr, - const char *ptr, - char *token) -{ - const char *start = ptr; - char *token_ptr = token; +/* Worker thread */ +static +void* worker(void *arg) { + ecs_stage_t *stage = arg; + ecs_world_t *world = stage->world; - while ((ptr = ecs_parse_token(name, expr, ptr, token_ptr))) { - if (ptr[0] == '|') { - token_ptr = &token_ptr[ptr - start]; - token_ptr[0] = '|'; - token_ptr[1] = '\0'; - token_ptr ++; - ptr ++; - start = ptr; - } else { - break; - } - } - - return ptr; -} + /* Start worker thread, increase counter so main thread knows how many + * workers are ready */ + ecs_os_mutex_lock(world->sync_mutex); + world->workers_running ++; -const char* ecs_parse_expr( - const ecs_world_t *world, - const char *ptr, - ecs_entity_t type, - void *data_out, - const ecs_parse_expr_desc_t *desc) -{ - ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - char token[ECS_MAX_TOKEN_SIZE]; - int depth = 0; + if (!world->quit_workers) { + ecs_os_cond_wait(world->worker_cond, world->sync_mutex); + } - const char *name = NULL; - const char *expr = NULL; + ecs_os_mutex_unlock(world->sync_mutex); - ptr = ecs_parse_fluff(ptr, NULL); + while (!world->quit_workers) { + ecs_entity_t old_scope = ecs_set_scope((ecs_world_t*)stage, 0); - ecs_meta_cursor_t cur = ecs_meta_cursor(world, type, data_out); - if (cur.valid == false) { - return NULL; - } + ecs_pipeline_run( + (ecs_world_t*)stage, + world->pipeline, + world->stats.delta_time); - if (desc) { - name = desc->name; - expr = desc->expr; - cur.lookup_action = desc->lookup_action; - cur.lookup_ctx = desc->lookup_ctx; + ecs_set_scope((ecs_world_t*)stage, old_scope); } - while ((ptr = ecs_parse_expr_token(name, expr, ptr, token))) { + ecs_os_mutex_lock(world->sync_mutex); + world->workers_running --; + ecs_os_mutex_unlock(world->sync_mutex); - if (!ecs_os_strcmp(token, "{")) { - ecs_entity_t scope_type = ecs_meta_get_type(&cur); - depth ++; - if (ecs_meta_push(&cur) != 0) { - goto error; - } + return NULL; +} - if (ecs_meta_is_collection(&cur)) { - char *path = ecs_get_fullpath(world, scope_type); - ecs_parser_error(name, expr, ptr - expr, - "expected '[' for collection type '%s'", path); - ecs_os_free(path); - return NULL; - } - } +/* Start threads */ +static +void start_workers( + ecs_world_t *world, + int32_t threads) +{ + ecs_set_stages(world, threads); - else if (!ecs_os_strcmp(token, "}")) { - depth --; + ecs_assert(ecs_get_stage_count(world) == threads, ECS_INTERNAL_ERROR, NULL); - if (ecs_meta_is_collection(&cur)) { - ecs_parser_error(name, expr, ptr - expr, "expected ']'"); - return NULL; - } + int32_t i; + for (i = 0; i < threads; i ++) { + ecs_stage_t *stage = (ecs_stage_t*)ecs_get_stage(world, i); + ecs_assert(stage != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_poly_assert(stage, ecs_stage_t); - if (ecs_meta_pop(&cur) != 0) { - goto error; - } - } + ecs_vector_get(world->worker_stages, ecs_stage_t, i); + stage->thread = ecs_os_thread_new(worker, stage); + ecs_assert(stage->thread != 0, ECS_THREAD_ERROR, NULL); + } +} - else if (!ecs_os_strcmp(token, "[")) { - depth ++; - if (ecs_meta_push(&cur) != 0) { - goto error; - } +/* Wait until all workers are running */ +static +void wait_for_workers( + ecs_world_t *world) +{ + int32_t stage_count = ecs_get_stage_count(world); + bool wait = true; - if (!ecs_meta_is_collection(&cur)) { - ecs_parser_error(name, expr, ptr - expr, "expected '{'"); - return NULL; - } + do { + ecs_os_mutex_lock(world->sync_mutex); + if (world->workers_running == stage_count) { + wait = false; } + ecs_os_mutex_unlock(world->sync_mutex); + } while (wait); +} - else if (!ecs_os_strcmp(token, "]")) { - depth --; +/* Synchronize worker threads */ +static +void sync_worker( + ecs_world_t *world) +{ + int32_t stage_count = ecs_get_stage_count(world); - if (!ecs_meta_is_collection(&cur)) { - ecs_parser_error(name, expr, ptr - expr, "expected '}'"); - return NULL; - } + /* Signal that thread is waiting */ + ecs_os_mutex_lock(world->sync_mutex); + if (++ world->workers_waiting == stage_count) { + /* Only signal main thread when all threads are waiting */ + ecs_os_cond_signal(world->sync_cond); + } - if (ecs_meta_pop(&cur) != 0) { - goto error; - } - } + /* Wait until main thread signals that thread can continue */ + ecs_os_cond_wait(world->worker_cond, world->sync_mutex); + ecs_os_mutex_unlock(world->sync_mutex); +} - else if (!ecs_os_strcmp(token, ",")) { - if (ecs_meta_next(&cur) != 0) { - goto error; - } - } +/* Wait until all threads are waiting on sync point */ +static +void wait_for_sync( + ecs_world_t *world) +{ + int32_t stage_count = ecs_get_stage_count(world); - else if (!ecs_os_strcmp(token, "null")) { - if (ecs_meta_set_null(&cur) != 0) { - goto error; - } - } + ecs_os_mutex_lock(world->sync_mutex); + if (world->workers_waiting != stage_count) { + ecs_os_cond_wait(world->sync_cond, world->sync_mutex); + } + + /* We should have been signalled unless all workers are waiting on sync */ + ecs_assert(world->workers_waiting == stage_count, + ECS_INTERNAL_ERROR, NULL); - else if (token[0] == '\"') { - if (ecs_meta_set_string_literal(&cur, token) != 0) { - goto error; - } - } + ecs_os_mutex_unlock(world->sync_mutex); +} - else { - ptr = ecs_parse_fluff(ptr, NULL); +/* Signal workers that they can start/resume work */ +static +void signal_workers( + ecs_world_t *world) +{ + ecs_os_mutex_lock(world->sync_mutex); + ecs_os_cond_broadcast(world->worker_cond); + ecs_os_mutex_unlock(world->sync_mutex); +} - if (ptr[0] == ':') { - /* Member assignment */ - ptr ++; - if (ecs_meta_member(&cur, token) != 0) { - goto error; - } - } else { - if (ecs_meta_set_string(&cur, token) != 0) { - goto error; - } - } - } +/** Stop worker threads */ +static +bool ecs_stop_threads( + ecs_world_t *world) +{ + bool threads_active = false; - if (!depth) { + /* Test if threads are created. Cannot use workers_running, since this is + * a potential race if threads haven't spun up yet. */ + ecs_vector_each(world->worker_stages, ecs_stage_t, stage, { + if (stage->thread) { + threads_active = true; break; } + stage->thread = 0; + }); - ptr = ecs_parse_fluff(ptr, NULL); + /* If no threads are active, just return */ + if (!threads_active) { + return false; } - return ptr; -error: - return NULL; -} - -#endif + /* Make sure all threads are running, to ensure they catch the signal */ + wait_for_workers(world); + /* Signal threads should quit */ + world->quit_workers = true; + signal_workers(world); -#ifdef FLECS_STATS + /* Join all threads with main */ + ecs_vector_each(world->worker_stages, ecs_stage_t, stage, { + ecs_os_thread_join(stage->thread); + stage->thread = 0; + }); -#ifdef FLECS_SYSTEM -#endif + world->quit_workers = false; + ecs_assert(world->workers_running == 0, ECS_INTERNAL_ERROR, NULL); -#ifdef FLECS_PIPELINE -#endif + /* Deinitialize stages */ + ecs_set_stages(world, 0); -static -int32_t t_next( - int32_t t) -{ - return (t + 1) % ECS_STAT_WINDOW; + return true; } -static -int32_t t_prev( - int32_t t) -{ - return (t - 1 + ECS_STAT_WINDOW) % ECS_STAT_WINDOW; -} +/* -- Private functions -- */ -static -void _record_gauge( - ecs_gauge_t *m, - int32_t t, - float value) +void ecs_worker_begin( + ecs_world_t *world) { - m->avg[t] = value; - m->min[t] = value; - m->max[t] = value; + flecs_stage_from_world(&world); + int32_t stage_count = ecs_get_stage_count(world); + ecs_assert(stage_count != 0, ECS_INTERNAL_ERROR, NULL); + + if (stage_count == 1) { + ecs_staging_begin(world); + } } -static -float _record_counter( - ecs_counter_t *m, - int32_t t, - float value) +bool ecs_worker_sync( + ecs_world_t *world) { - int32_t tp = t_prev(t); - float prev = m->value[tp]; - m->value[t] = value; - _record_gauge((ecs_gauge_t*)m, t, value - prev); - return value - prev; -} + flecs_stage_from_world(&world); -/* Macro's to silence conversion warnings without adding casts everywhere */ -#define record_gauge(m, t, value)\ - _record_gauge(m, t, (float)value) + int32_t build_count = world->stats.pipeline_build_count_total; + int32_t stage_count = ecs_get_stage_count(world); + ecs_assert(stage_count != 0, ECS_INTERNAL_ERROR, NULL); -#define record_counter(m, t, value)\ - _record_counter(m, t, (float)value) + /* If there are no threads, merge in place */ + if (stage_count == 1) { + ecs_staging_end(world); + ecs_pipeline_update(world, world->pipeline, false); + ecs_staging_begin(world); -static -void print_value( - const char *name, - float value) -{ - ecs_size_t len = ecs_os_strlen(name); - printf("%s: %*s %.2f\n", name, 32 - len, "", (double)value); -} + /* Synchronize all workers. The last worker to reach the sync point will + * signal the main thread, which will perform the merge. */ + } else { + sync_worker(world); + } -static -void print_gauge( - const char *name, - int32_t t, - const ecs_gauge_t *m) -{ - print_value(name, m->avg[t]); + return world->stats.pipeline_build_count_total != build_count; } -static -void print_counter( - const char *name, - int32_t t, - const ecs_counter_t *m) +void ecs_worker_end( + ecs_world_t *world) { - print_value(name, m->rate.avg[t]); -} + flecs_stage_from_world(&world); -void ecs_gauge_reduce( - ecs_gauge_t *dst, - int32_t t_dst, - ecs_gauge_t *src, - int32_t t_src) -{ - ecs_assert(dst != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(src != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t stage_count = ecs_get_stage_count(world); + ecs_assert(stage_count != 0, ECS_INTERNAL_ERROR, NULL); - bool min_set = false; - dst->min[t_dst] = 0; - dst->avg[t_dst] = 0; - dst->max[t_dst] = 0; + /* If there are no threads, merge in place */ + if (stage_count == 1) { + ecs_staging_end(world); - int32_t i; - for (i = 0; i < ECS_STAT_WINDOW; i ++) { - int32_t t = (t_src + i) % ECS_STAT_WINDOW; - dst->avg[t_dst] += src->avg[t] / (float)ECS_STAT_WINDOW; - if (!min_set || (src->min[t] < dst->min[t_dst])) { - dst->min[t_dst] = src->min[t]; - min_set = true; - } - if ((src->max[t] > dst->max[t_dst])) { - dst->max[t_dst] = src->max[t]; - } + /* Synchronize all workers. The last worker to reach the sync point will + * signal the main thread, which will perform the merge. */ + } else { + sync_worker(world); } } -void ecs_get_world_stats( - const ecs_world_t *world, - ecs_world_stats_t *s) +void ecs_workers_progress( + ecs_world_t *world, + ecs_entity_t pipeline, + FLECS_FLOAT delta_time) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); - - world = ecs_get_world(world); - - int32_t t = s->t = t_next(s->t); + ecs_poly_assert(world, ecs_world_t); + int32_t stage_count = ecs_get_stage_count(world); - float delta_world_time = record_counter(&s->world_time_total_raw, t, world->stats.world_time_total_raw); - record_counter(&s->world_time_total, t, world->stats.world_time_total); - record_counter(&s->frame_time_total, t, world->stats.frame_time_total); - record_counter(&s->system_time_total, t, world->stats.system_time_total); - record_counter(&s->merge_time_total, t, world->stats.merge_time_total); + ecs_time_t start = {0}; + if (world->measure_frame_time) { + ecs_time_measure(&start); + } - float delta_frame_count = record_counter(&s->frame_count_total, t, world->stats.frame_count_total); - record_counter(&s->merge_count_total, t, world->stats.merge_count_total); - record_counter(&s->pipeline_build_count_total, t, world->stats.pipeline_build_count_total); - record_counter(&s->systems_ran_frame, t, world->stats.systems_ran_frame); + if (stage_count == 1) { + ecs_pipeline_update(world, pipeline, true); + ecs_entity_t old_scope = ecs_set_scope(world, 0); + ecs_world_t *stage = ecs_get_stage(world, 0); - if (delta_world_time != 0.0f && delta_frame_count != 0.0f) { - record_gauge( - &s->fps, t, 1.0f / (delta_world_time / (float)delta_frame_count)); + ecs_pipeline_run(stage, pipeline, delta_time); + ecs_set_scope(world, old_scope); } else { - record_gauge(&s->fps, t, 0); - } + int32_t i, sync_count = ecs_pipeline_update(world, pipeline, true); - record_gauge(&s->entity_count, t, flecs_sparse_count(world->store.entity_index)); - record_gauge(&s->component_count, t, ecs_count_id(world, ecs_id(EcsComponent))); - record_gauge(&s->query_count, t, flecs_sparse_count(world->queries)); - record_gauge(&s->system_count, t, ecs_count_id(world, ecs_id(EcsSystem))); + /* Make sure workers are running and ready */ + wait_for_workers(world); - record_counter(&s->new_count, t, world->new_count); - record_counter(&s->bulk_new_count, t, world->bulk_new_count); - record_counter(&s->delete_count, t, world->delete_count); - record_counter(&s->clear_count, t, world->clear_count); - record_counter(&s->add_count, t, world->add_count); - record_counter(&s->remove_count, t, world->remove_count); - record_counter(&s->set_count, t, world->set_count); - record_counter(&s->discard_count, t, world->discard_count); + /* Synchronize n times for each op in the pipeline */ + for (i = 0; i < sync_count; i ++) { + ecs_staging_begin(world); - /* Compute table statistics */ - int32_t empty_table_count = 0; - int32_t singleton_table_count = 0; - int32_t matched_table_count = 0, matched_entity_count = 0; + /* Signal workers that they should start running systems */ + world->workers_waiting = 0; + signal_workers(world); - int32_t i, count = flecs_sparse_count(world->store.tables); - for (i = 0; i < count; i ++) { - ecs_table_t *table = flecs_sparse_get_dense(world->store.tables, ecs_table_t, i); - int32_t entity_count = ecs_table_count(table); + /* Wait until all workers are waiting on sync point */ + wait_for_sync(world); - if (!entity_count) { - empty_table_count ++; - } + /* Merge */ + ecs_staging_end(world); - /* Singleton tables are tables that have just one entity that also has - * itself in the table type. */ - if (entity_count == 1) { - ecs_entity_t *entities = ecs_vector_first( - table->storage.entities, ecs_entity_t); - if (ecs_type_has_id(world, table->type, entities[0], false)) { - singleton_table_count ++; + int32_t update_count; + if ((update_count = ecs_pipeline_update(world, pipeline, false))) { + /* The number of operations in the pipeline could have changed + * as result of the merge */ + sync_count = update_count; } } - - /* If this table matches with queries and is not empty, increase the - * matched table & matched entity count. These statistics can be used to - * compute actual fragmentation ratio for queries. */ - int32_t queries_matched = ecs_vector_count(table->queries); - if (queries_matched && entity_count) { - matched_table_count ++; - matched_entity_count += entity_count; - } } - record_gauge(&s->matched_table_count, t, matched_table_count); - record_gauge(&s->matched_entity_count, t, matched_entity_count); - - record_gauge(&s->table_count, t, count); - record_gauge(&s->empty_table_count, t, empty_table_count); - record_gauge(&s->singleton_table_count, t, singleton_table_count); + if (world->measure_frame_time) { + world->stats.system_time_total += (FLECS_FLOAT)ecs_time_measure(&start); + } } -void ecs_get_query_stats( - const ecs_world_t *world, - const ecs_query_t *query, - ecs_query_stats_t *s) + +/* -- Public functions -- */ + +void ecs_set_threads( + ecs_world_t *world, + int32_t threads) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(query != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); - (void)world; + ecs_assert(threads <= 1 || ecs_os_has_threading(), ECS_MISSING_OS_API, NULL); - int32_t t = s->t = t_next(s->t); + int32_t stage_count = ecs_get_stage_count(world); - int32_t i, entity_count = 0, count = ecs_query_table_count(query); - ecs_query_table_match_t *matched_tables = ecs_vector_first( - query->cache.tables, ecs_query_table_match_t); - for (i = 0; i < count; i ++) { - ecs_query_table_match_t *matched = &matched_tables[i]; - if (matched->table) { - entity_count += ecs_table_count(matched->table); + if (stage_count != threads) { + /* Stop existing threads */ + if (stage_count > 1) { + if (ecs_stop_threads(world)) { + ecs_os_cond_free(world->worker_cond); + ecs_os_cond_free(world->sync_cond); + ecs_os_mutex_free(world->sync_mutex); + } + } + + /* Start threads if number of threads > 1 */ + if (threads > 1) { + world->worker_cond = ecs_os_cond_new(); + world->sync_cond = ecs_os_cond_new(); + world->sync_mutex = ecs_os_mutex_new(); + start_workers(world, threads); } } +} - record_gauge(&s->matched_table_count, t, count); - record_gauge(&s->matched_empty_table_count, t, - ecs_query_empty_table_count(query)); - record_gauge(&s->matched_entity_count, t, entity_count); +#endif + +#ifdef FLECS_PIPELINE + + +static ECS_CTOR(EcsPipelineQuery, ptr, { + memset(ptr, 0, _size); +}) + +static ECS_DTOR(EcsPipelineQuery, ptr, { + ecs_vector_free(ptr->ops); +}) + +static +int compare_entity( + ecs_entity_t e1, + const void *ptr1, + ecs_entity_t e2, + const void *ptr2) +{ + (void)ptr1; + (void)ptr2; + return (e1 > e2) - (e1 < e2); } -#ifdef FLECS_SYSTEM -bool ecs_get_system_stats( - const ecs_world_t *world, - ecs_entity_t system, - ecs_system_stats_t *s) +static +uint64_t group_by_phase( + ecs_world_t *world, + ecs_type_t type, + ecs_entity_t pipeline, + void *ctx) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(system != 0, ECS_INVALID_PARAMETER, NULL); + (void)ctx; + + const EcsType *pipeline_type = ecs_get(world, pipeline, EcsType); + ecs_assert(pipeline_type != NULL, ECS_INTERNAL_ERROR, NULL); - world = ecs_get_world(world); + /* Find tag in system that belongs to pipeline */ + ecs_entity_t *sys_comps = ecs_vector_first(type, ecs_entity_t); + int32_t c, t, count = ecs_vector_count(type); - const EcsSystem *ptr = ecs_get(world, system, EcsSystem); - if (!ptr) { - return false; - } + ecs_entity_t *tags = ecs_vector_first(pipeline_type->normalized, ecs_entity_t); + int32_t tag_count = ecs_vector_count(pipeline_type->normalized); - ecs_get_query_stats(world, ptr->query, &s->query_stats); - int32_t t = s->query_stats.t; + ecs_entity_t result = 0; - record_counter(&s->time_spent, t, ptr->time_spent); - record_counter(&s->invoke_count, t, ptr->invoke_count); - record_gauge(&s->active, t, !ecs_has_id(world, system, EcsInactive)); - record_gauge(&s->enabled, t, !ecs_has_id(world, system, EcsDisabled)); + for (c = 0; c < count; c ++) { + ecs_entity_t comp = sys_comps[c]; + for (t = 0; t < tag_count; t ++) { + if (comp == tags[t]) { + result = comp; + break; + } + } + if (result) { + break; + } + } - return true; + ecs_assert(result != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(result < INT_MAX, ECS_INTERNAL_ERROR, NULL); + + return result; } -#endif +typedef enum ComponentWriteState { + NotWritten = 0, + WriteToMain, + WriteToStage +} ComponentWriteState; -#ifdef FLECS_PIPELINE +typedef struct write_state_t { + ecs_map_t *components; + bool wildcard; +} write_state_t; -static ecs_system_stats_t* get_system_stats( - ecs_map_t *systems, - ecs_entity_t system) +static +int32_t get_write_state( + ecs_map_t *write_state, + ecs_entity_t component) { - ecs_assert(systems != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(system != 0, ECS_INVALID_PARAMETER, NULL); - - ecs_system_stats_t *s = ecs_map_get(systems, ecs_system_stats_t, system); - if (!s) { - ecs_system_stats_t stats; - memset(&stats, 0, sizeof(ecs_system_stats_t)); - ecs_map_set(systems, system, &stats); - s = ecs_map_get(systems, ecs_system_stats_t, system); - ecs_assert(s != NULL, ECS_INTERNAL_ERROR, NULL); + int32_t *ptr = ecs_map_get(write_state, int32_t, component); + if (ptr) { + return *ptr; + } else { + return 0; } - - return s; } -bool ecs_get_pipeline_stats( - ecs_world_t *stage, - ecs_entity_t pipeline, - ecs_pipeline_stats_t *s) +static +void set_write_state( + write_state_t *write_state, + ecs_entity_t component, + int32_t value) { - ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(pipeline != 0, ECS_INVALID_PARAMETER, NULL); + if (component == EcsWildcard) { + ecs_assert(value == WriteToStage, ECS_INTERNAL_ERROR, NULL); + write_state->wildcard = true; + } else { + ecs_map_set(write_state->components, component, &value); + } +} - const ecs_world_t *world = ecs_get_world(stage); +static +void reset_write_state( + write_state_t *write_state) +{ + ecs_map_clear(write_state->components); + write_state->wildcard = false; +} - const EcsPipelineQuery *pq = ecs_get(world, pipeline, EcsPipelineQuery); - if (!pq) { - return false; +static +int32_t get_any_write_state( + write_state_t *write_state) +{ + if (write_state->wildcard) { + return WriteToStage; } - /* First find out how many systems are matched by the pipeline */ - ecs_iter_t it = ecs_query_iter(stage, pq->query); - int32_t count = 0; - while (ecs_query_next(&it)) { - count += it.count; + ecs_map_iter_t it = ecs_map_iter(write_state->components); + int32_t *elem; + while ((elem = ecs_map_next(&it, int32_t, NULL))) { + if (*elem == WriteToStage) { + return WriteToStage; + } } - if (!s->system_stats) { - s->system_stats = ecs_map_new(ecs_system_stats_t, count); - } + return 0; +} - /* Also count synchronization points */ - ecs_vector_t *ops = pq->ops; - ecs_pipeline_op_t *op = ecs_vector_first(ops, ecs_pipeline_op_t); - ecs_pipeline_op_t *op_last = ecs_vector_last(ops, ecs_pipeline_op_t); - count += ecs_vector_count(ops); +static +bool check_term_component( + ecs_term_t *term, + bool is_active, + ecs_entity_t component, + write_state_t *write_state) +{ + int32_t state = get_write_state(write_state->components, component); - /* Make sure vector is large enough to store all systems & sync points */ - ecs_vector_set_count(&s->systems, ecs_entity_t, count - 1); - ecs_entity_t *systems = ecs_vector_first(s->systems, ecs_entity_t); + ecs_term_id_t *subj = &term->subj; - /* Populate systems vector, keep track of sync points */ - it = ecs_query_iter(stage, pq->query); - - int32_t i_system = 0, ran_since_merge = 0; - while (ecs_query_next(&it)) { - int32_t i; - for (i = 0; i < it.count; i ++) { - systems[i_system ++] = it.entities[i]; - ran_since_merge ++; - if (op != op_last && ran_since_merge == op->count) { - ran_since_merge = 0; - op++; - systems[i_system ++] = 0; /* 0 indicates a merge point */ + if ((subj->set.mask & EcsSelf) && subj->entity == EcsThis && term->oper != EcsNot) { + switch(term->inout) { + case EcsInOutDefault: + case EcsInOut: + case EcsIn: + if (state == WriteToStage || write_state->wildcard) { + return true; + } + // fall through + case EcsOut: + if (is_active && term->inout != EcsIn) { + set_write_state(write_state, component, WriteToMain); } + }; + } else if (!subj->entity || term->oper == EcsNot) { + bool needs_merge = false; - ecs_system_stats_t *sys_stats = get_system_stats( - s->system_stats, it.entities[i]); - ecs_get_system_stats(world, it.entities[i], sys_stats); + switch(term->inout) { + case EcsInOutDefault: + case EcsIn: + case EcsInOut: + if (state == WriteToStage) { + needs_merge = true; + } + if (component == EcsWildcard) { + if (get_any_write_state(write_state) == WriteToStage) { + needs_merge = true; + } + } + break; + default: + break; + }; + + switch(term->inout) { + case EcsInOutDefault: + if (!(subj->set.mask & EcsSelf) || !subj->entity || + subj->entity != EcsThis) + { + break; + } + // fall through + case EcsInOut: + case EcsOut: + if (is_active) { + set_write_state(write_state, component, WriteToStage); + } + break; + default: + break; + }; + + if (needs_merge) { + return true; } } - ecs_assert(i_system == (count - 1), ECS_INTERNAL_ERROR, NULL); - - return true; + return false; } -#endif -void ecs_dump_world_stats( - const ecs_world_t *world, - const ecs_world_stats_t *s) +static +bool check_term( + ecs_term_t *term, + bool is_active, + write_state_t *write_state) { - int32_t t = s->t; - - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(s != NULL, ECS_INVALID_PARAMETER, NULL); + if (term->oper != EcsOr) { + return check_term_component( + term, is_active, term->id, write_state); + } - world = ecs_get_world(world); - - print_counter("Frame", t, &s->frame_count_total); - printf("-------------------------------------\n"); - print_counter("pipeline rebuilds", t, &s->pipeline_build_count_total); - print_counter("systems ran last frame", t, &s->systems_ran_frame); - printf("\n"); - print_value("target FPS", world->stats.target_fps); - print_value("time scale", world->stats.time_scale); - printf("\n"); - print_gauge("actual FPS", t, &s->fps); - print_counter("frame time", t, &s->frame_time_total); - print_counter("system time", t, &s->system_time_total); - print_counter("merge time", t, &s->merge_time_total); - print_counter("simulation time elapsed", t, &s->world_time_total); - printf("\n"); - print_gauge("entity count", t, &s->entity_count); - print_gauge("component count", t, &s->component_count); - print_gauge("query count", t, &s->query_count); - print_gauge("system count", t, &s->system_count); - print_gauge("table count", t, &s->table_count); - print_gauge("singleton table count", t, &s->singleton_table_count); - print_gauge("empty table count", t, &s->empty_table_count); - printf("\n"); - print_counter("deferred new operations", t, &s->new_count); - print_counter("deferred bulk_new operations", t, &s->bulk_new_count); - print_counter("deferred delete operations", t, &s->delete_count); - print_counter("deferred clear operations", t, &s->clear_count); - print_counter("deferred add operations", t, &s->add_count); - print_counter("deferred remove operations", t, &s->remove_count); - print_counter("deferred set operations", t, &s->set_count); - print_counter("discarded operations", t, &s->discard_count); - printf("\n"); + return false; } -#endif - -#ifdef FLECS_SNAPSHOT - - -/* World snapshot */ -struct ecs_snapshot_t { - ecs_world_t *world; - ecs_sparse_t *entity_index; - ecs_vector_t *tables; - ecs_entity_t last_id; - ecs_filter_t filter; -}; - -/** Small footprint data structure for storing data associated with a table. */ -typedef struct ecs_table_leaf_t { - ecs_table_t *table; - ecs_vector_t *type; - ecs_data_t *data; -} ecs_table_leaf_t; - static -ecs_data_t* duplicate_data( - const ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *main_data) +bool build_pipeline( + ecs_world_t *world, + ecs_entity_t pipeline, + EcsPipelineQuery *pq) { - if (!ecs_table_count(table)) { - return NULL; - } + (void)pipeline; - ecs_data_t *result = ecs_os_calloc(ECS_SIZEOF(ecs_data_t)); + ecs_query_iter(world, pq->query); - ecs_type_t storage_type = table->storage_type; - int32_t i, column_count = ecs_vector_count(storage_type); - ecs_entity_t *components = ecs_vector_first(storage_type, ecs_entity_t); + if (pq->match_count == pq->query->match_count) { + /* No need to rebuild the pipeline */ + return false; + } - result->columns = ecs_os_memdup( - main_data->columns, ECS_SIZEOF(ecs_column_t) * column_count); + world->stats.pipeline_build_count_total ++; - /* Copy entities */ - result->entities = ecs_vector_copy(main_data->entities, ecs_entity_t); - ecs_entity_t *entities = ecs_vector_first(result->entities, ecs_entity_t); + write_state_t ws = { + .components = ecs_map_new(int32_t, ECS_HI_COMPONENT_ID), + .wildcard = false + }; - /* Copy record ptrs */ - result->record_ptrs = ecs_vector_copy(main_data->record_ptrs, ecs_record_t*); + ecs_pipeline_op_t *op = NULL; + ecs_vector_t *ops = NULL; + ecs_query_t *query = pq->build_query; - /* Copy each column */ - for (i = 0; i < column_count; i ++) { - ecs_entity_t component = components[i]; - ecs_column_t *column = &result->columns[i]; + if (pq->ops) { + ecs_vector_free(pq->ops); + } - component = ecs_get_typeid(world, component); + /* Iterate systems in pipeline, add ops for running / merging */ + ecs_iter_t it = ecs_query_iter(world, query); + while (ecs_query_next(&it)) { + EcsSystem *sys = ecs_term(&it, EcsSystem, 1); - const ecs_type_info_t *cdata = flecs_get_c_info(world, component); - int16_t size = column->size; - int16_t alignment = column->alignment; - ecs_copy_t copy; + int i; + for (i = 0; i < it.count; i ++) { + ecs_query_t *q = sys[i].query; + if (!q) { + continue; + } - if (cdata && (copy = cdata->lifecycle.copy)) { - int32_t count = ecs_vector_count(column->data); - ecs_vector_t *dst_vec = ecs_vector_new_t(size, alignment, count); - ecs_vector_set_count_t(&dst_vec, size, alignment, count); - void *dst_ptr = ecs_vector_first_t(dst_vec, size, alignment); - void *ctx = cdata->lifecycle.ctx; + bool needs_merge = false; + bool is_active = !ecs_has_id( + world, it.entities[i], EcsInactive); - ecs_xtor_t ctor = cdata->lifecycle.ctor; - if (ctor) { - ctor((ecs_world_t*)world, component, entities, dst_ptr, - flecs_to_size_t(size), count, ctx); + ecs_term_t *terms = q->filter.terms; + int32_t t, term_count = q->filter.term_count; + for (t = 0; t < term_count; t ++) { + needs_merge |= check_term(&terms[t], is_active, &ws); } - void *src_ptr = ecs_vector_first_t(column->data, size, alignment); - copy((ecs_world_t*)world, component, entities, entities, dst_ptr, - src_ptr, flecs_to_size_t(size), count, ctx); + if (needs_merge) { + /* After merge all components will be merged, so reset state */ + reset_write_state(&ws); + op = NULL; - column->data = dst_vec; - } else { - column->data = ecs_vector_copy_t(column->data, size, alignment); + /* Re-evaluate columns to set write flags if system is active. + * If system is inactive, it can't write anything and so it + * should not insert unnecessary merges. */ + needs_merge = false; + if (is_active) { + for (t = 0; t < term_count; t ++) { + needs_merge |= check_term(&terms[t], true, &ws); + } + } + + /* The component states were just reset, so if we conclude that + * another merge is needed something is wrong. */ + ecs_assert(needs_merge == false, ECS_INTERNAL_ERROR, NULL); + } + + if (!op) { + op = ecs_vector_add(&ops, ecs_pipeline_op_t); + op->count = 0; + } + + /* Don't increase count for inactive systems, as they are ignored by + * the query used to run the pipeline. */ + if (is_active) { + op->count ++; + } } } - return result; -} + ecs_map_free(ws.components); -static -void snapshot_table( - const ecs_world_t *world, - ecs_snapshot_t *snapshot, - ecs_table_t *table) -{ - if (table->flags & EcsTableHasBuiltins) { - return; - } + /* Force sort of query as this could increase the match_count */ + pq->match_count = pq->query->match_count; + pq->ops = ops; - ecs_table_leaf_t *l = ecs_vector_get( - snapshot->tables, ecs_table_leaf_t, (int32_t)table->id); - ecs_assert(l != NULL, ECS_INTERNAL_ERROR, NULL); - - l->table = table; - l->type = ecs_vector_copy(table->type, ecs_id_t); - l->data = duplicate_data(world, table, &table->storage); + return true; } static -ecs_snapshot_t* snapshot_create( - const ecs_world_t *world, - const ecs_sparse_t *entity_index, - ecs_iter_t *iter, - ecs_iter_next_action_t next) +int32_t iter_reset( + ecs_world_t *world, + const EcsPipelineQuery *pq, + ecs_iter_t *iter_out, + ecs_pipeline_op_t **op_out, + ecs_entity_t move_to) { - ecs_snapshot_t *result = ecs_os_calloc_t(ecs_snapshot_t); - ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); + ecs_pipeline_op_t *op = ecs_vector_first(pq->ops, ecs_pipeline_op_t); + int32_t ran_since_merge = 0; - result->world = (ecs_world_t*)world; + *iter_out = ecs_query_iter(world, pq->query); + while (ecs_query_next(iter_out)) { + int32_t i; + for(i = 0; i < iter_out->count; i ++) { + ecs_entity_t e = iter_out->entities[i]; - /* If no iterator is provided, the snapshot will be taken of the entire - * world, and we can simply copy the entity index as it will be restored - * entirely upon snapshote restore. */ - if (!iter && entity_index) { - result->entity_index = flecs_sparse_copy(entity_index); + ran_since_merge ++; + if (ran_since_merge == op->count) { + ran_since_merge = 0; + op ++; + } + + if (e == move_to) { + *op_out = op; + return i; + } + } } - /* Create vector with as many elements as tables, so we can store the - * snapshot tables at their element ids. When restoring a snapshot, the code - * will run a diff between the tables in the world and the snapshot, to see - * which of the world tables still exist, no longer exist, or need to be - * deleted. */ - uint64_t t, table_count = flecs_sparse_last_id(world->store.tables) + 1; - result->tables = ecs_vector_new(ecs_table_leaf_t, (int32_t)table_count); - ecs_vector_set_count(&result->tables, ecs_table_leaf_t, (int32_t)table_count); - ecs_table_leaf_t *arr = ecs_vector_first(result->tables, ecs_table_leaf_t); + ecs_abort(ECS_UNSUPPORTED, NULL); - /* Array may have holes, so initialize with 0 */ - ecs_os_memset_n(arr, 0, ecs_table_leaf_t, table_count); + return -1; +} - /* Iterate tables in iterator */ - if (iter) { - while (next(iter)) { - ecs_table_t *table = iter->table; - snapshot_table(world, result, table); - } - } else { - for (t = 0; t < table_count; t ++) { - ecs_table_t *table = flecs_sparse_get( - world->store.tables, ecs_table_t, t); - snapshot_table(world, result, table); - } - } - - return result; -} - -/** Create a snapshot */ -ecs_snapshot_t* ecs_snapshot_take( - ecs_world_t *stage) +int32_t ecs_pipeline_update( + ecs_world_t *world, + ecs_entity_t pipeline, + bool start_of_frame) { - const ecs_world_t *world = ecs_get_world(stage); + ecs_poly_assert(world, ecs_world_t); + ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); + ecs_assert(pipeline != 0, ECS_INTERNAL_ERROR, NULL); - ecs_snapshot_t *result = snapshot_create( - world, - world->store.entity_index, - NULL, - NULL); + /* If any entity mutations happened that could have affected query matching + * notify appropriate queries so caches are up to date. This includes the + * pipeline query. */ + if (start_of_frame) { + flecs_eval_component_monitors(world); + } - result->last_id = world->stats.last_id; + bool added = false; + EcsPipelineQuery *pq = ecs_get_mut(world, pipeline, EcsPipelineQuery, &added); + ecs_assert(added == false, ECS_INTERNAL_ERROR, NULL); + ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(pq->query != NULL, ECS_INTERNAL_ERROR, NULL); - return result; + build_pipeline(world, pipeline, pq); + + return ecs_vector_count(pq->ops); } -/** Create a filtered snapshot */ -ecs_snapshot_t* ecs_snapshot_take_w_iter( - ecs_iter_t *iter, - ecs_iter_next_action_t next) +void ecs_pipeline_run( + ecs_world_t *world, + ecs_entity_t pipeline, + FLECS_FLOAT delta_time) { - ecs_world_t *world = iter->world; - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(world != NULL, ECS_INVALID_OPERATION, NULL); - ecs_snapshot_t *result = snapshot_create( - world, - world->store.entity_index, - iter, - next); + if (!pipeline) { + pipeline = world->pipeline; + } - result->last_id = world->stats.last_id; + /* If the world is passed to ecs_pipeline_run, the function will take care + * of staging, so the world should not be in staged mode when called. */ + if (ecs_poly_is(world, ecs_world_t)) { + ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); - return result; -} + /* Forward to worker_progress. This function handles staging, threading + * and synchronization across workers. */ + ecs_workers_progress(world, pipeline, delta_time); + return; -/* Restoring an unfiltered snapshot restores the world to the exact state it was - * when the snapshot was taken. */ -static -void restore_unfiltered( - ecs_world_t *world, - ecs_snapshot_t *snapshot) -{ - flecs_sparse_restore(world->store.entity_index, snapshot->entity_index); - flecs_sparse_free(snapshot->entity_index); + /* If a stage is passed, the function could be ran from a worker thread. In + * that case the main thread should manage staging, and staging should be + * enabled. */ + } else { + ecs_poly_assert(world, ecs_stage_t); + } + + ecs_stage_t *stage = flecs_stage_from_world(&world); - world->stats.last_id = snapshot->last_id; + const EcsPipelineQuery *pq = ecs_get(world, pipeline, EcsPipelineQuery); + ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(pq->query != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_table_leaf_t *leafs = ecs_vector_first( - snapshot->tables, ecs_table_leaf_t); - int32_t i, count = (int32_t)flecs_sparse_last_id(world->store.tables); - int32_t snapshot_count = ecs_vector_count(snapshot->tables); + ecs_vector_t *ops = pq->ops; + ecs_pipeline_op_t *op = ecs_vector_first(ops, ecs_pipeline_op_t); + ecs_pipeline_op_t *op_last = ecs_vector_last(ops, ecs_pipeline_op_t); + int32_t ran_since_merge = 0; - for (i = 0; i <= count; i ++) { - ecs_table_t *world_table = flecs_sparse_get( - world->store.tables, ecs_table_t, (uint32_t)i); + int32_t stage_index = ecs_get_stage_id(stage->thread_ctx); + int32_t stage_count = ecs_get_stage_count(world); - if (world_table && (world_table->flags & EcsTableHasBuiltins)) { - continue; - } + ecs_worker_begin(stage->thread_ctx); + + ecs_iter_t it = ecs_query_iter(world, pq->query); + while (ecs_query_next(&it)) { + EcsSystem *sys = ecs_term(&it, EcsSystem, 1); - ecs_table_leaf_t *snapshot_table = NULL; - if (i < snapshot_count) { - snapshot_table = &leafs[i]; - if (!snapshot_table->table) { - snapshot_table = NULL; - } - } + int32_t i; + for(i = 0; i < it.count; i ++) { + ecs_entity_t e = it.entities[i]; - /* If the world table no longer exists but the snapshot table does, - * reinsert it */ - if (!world_table && snapshot_table) { - ecs_ids_t type = { - .array = ecs_vector_first(snapshot_table->type, ecs_id_t), - .count = ecs_vector_count(snapshot_table->type) - }; + ecs_run_intern(world, stage, e, &sys[i], stage_index, stage_count, + delta_time, 0, 0, NULL); - ecs_table_t *table = flecs_table_find_or_create(world, &type); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ran_since_merge ++; + world->stats.systems_ran_frame ++; - if (snapshot_table->data) { - flecs_table_replace_data(world, table, snapshot_table->data); - } - - /* If the world table still exists, replace its data */ - } else if (world_table && snapshot_table) { - if (snapshot_table->data) { - flecs_table_replace_data( - world, world_table, snapshot_table->data); - } else { - flecs_table_clear_data( - world, world_table, &world_table->storage); - flecs_table_init_data(world, world_table); - } - - /* If the snapshot table doesn't exist, this table was created after the - * snapshot was taken and needs to be deleted */ - } else if (world_table && !snapshot_table) { - /* Deleting a table invokes OnRemove triggers & updates the entity - * index. That is not what we want, since entities may no longer be - * valid (if they don't exist in the snapshot) or may have been - * restored in a different table. Therefore first clear the data - * from the table (which doesn't invoke triggers), and then delete - * the table. */ - flecs_table_clear_data(world, world_table, &world_table->storage); - flecs_delete_table(world, world_table); - - /* If there is no world & snapshot table, nothing needs to be done */ - } else { } + if (op != op_last && ran_since_merge == op->count) { + ran_since_merge = 0; + op++; - if (snapshot_table) { - ecs_os_free(snapshot_table->data); - ecs_os_free(snapshot_table->type); + /* If the set of matched systems changed as a result of the + * merge, we have to reset the iterator and move it to our + * current position (system). If there are a lot of systems + * in the pipeline this can be an expensive operation, but + * should happen infrequently. */ + if (ecs_worker_sync(stage->thread_ctx)) { + i = iter_reset(world, pq, &it, &op, e); + op_last = ecs_vector_last(pq->ops, ecs_pipeline_op_t); + sys = ecs_term(&it, EcsSystem, 1); + } + } } } - /* Now that all tables have been restored and world is in a consistent - * state, run OnSet systems */ - int32_t world_count = flecs_sparse_count(world->store.tables); - for (i = 0; i < world_count; i ++) { - ecs_table_t *table = flecs_sparse_get_dense( - world->store.tables, ecs_table_t, i); - if (table->flags & EcsTableHasBuiltins) { - continue; - } + ecs_worker_end(stage->thread_ctx); +} - int32_t tcount = ecs_table_count(table); - if (tcount) { - flecs_notify_on_set(world, table, 0, tcount, NULL, true); - } +static +void add_pipeline_tags_to_sig( + ecs_world_t *world, + ecs_term_t *terms, + ecs_type_t type) +{ + (void)world; + + int32_t i, count = ecs_vector_count(type); + ecs_entity_t *entities = ecs_vector_first(type, ecs_entity_t); + + for (i = 0; i < count; i ++) { + terms[i] = (ecs_term_t){ + .inout = EcsIn, + .oper = EcsOr, + .pred.entity = entities[i], + .subj = { + .entity = EcsThis, + .set.mask = EcsSelf | EcsSuperSet + } + }; } } -/* Restoring a filtered snapshots only restores the entities in the snapshot - * to their previous state. */ static -void restore_filtered( +ecs_query_t* build_pipeline_query( ecs_world_t *world, - ecs_snapshot_t *snapshot) + ecs_entity_t pipeline, + const char *name, + bool with_inactive) { - ecs_table_leaf_t *leafs = ecs_vector_first( - snapshot->tables, ecs_table_leaf_t); - int32_t l = 0, snapshot_count = ecs_vector_count(snapshot->tables); + const EcsType *type_ptr = ecs_get(world, pipeline, EcsType); + ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - for (l = 0; l < snapshot_count; l ++) { - ecs_table_leaf_t *snapshot_table = &leafs[l]; - ecs_table_t *table = snapshot_table->table; + int32_t type_count = ecs_vector_count(type_ptr->normalized); + int32_t term_count = 1; - if (!table) { - continue; - } + if (with_inactive) { + term_count ++; + } - ecs_data_t *data = snapshot_table->data; - if (!data) { - ecs_vector_free(snapshot_table->type); - continue; + ecs_term_t *terms = ecs_os_malloc( + (type_count + term_count) * ECS_SIZEOF(ecs_term_t)); + + terms[0] = (ecs_term_t){ + .inout = EcsIn, + .oper = EcsAnd, + .pred.entity = ecs_id(EcsSystem), + .subj = { + .entity = EcsThis, + .set.mask = EcsSelf | EcsSuperSet } + }; - /* Delete entity from storage first, so that when we restore it to the - * current table we can be sure that there won't be any duplicates */ - int32_t i, entity_count = ecs_vector_count(data->entities); - ecs_entity_t *entities = ecs_vector_first( - snapshot_table->data->entities, ecs_entity_t); - for (i = 0; i < entity_count; i ++) { - ecs_entity_t e = entities[i]; - ecs_record_t *r = ecs_eis_get(world, e); - if (r && r->table) { - bool is_monitored; - int32_t row = flecs_record_to_row(r->row, &is_monitored); - flecs_table_delete( - world, r->table, &r->table->storage, row, true); - } else { - /* Make sure that the entity has the same generation count */ - ecs_eis_set_generation(world, e); + if (with_inactive) { + terms[1] = (ecs_term_t){ + .inout = EcsIn, + .oper = EcsNot, + .pred.entity = EcsInactive, + .subj = { + .entity = EcsThis, + .set.mask = EcsSelf | EcsSuperSet } - } + }; + } - /* Merge data from snapshot table with world table */ - int32_t old_count = ecs_table_count(snapshot_table->table); - int32_t new_count = flecs_table_data_count(snapshot_table->data); + add_pipeline_tags_to_sig(world, &terms[term_count], type_ptr->normalized); - flecs_table_merge(world, table, table, &table->storage, snapshot_table->data); + ecs_query_t *result = ecs_query_init(world, &(ecs_query_desc_t){ + .filter = { + .name = name, + .terms_buffer = terms, + .terms_buffer_count = term_count + type_count + }, + .order_by = compare_entity, + .group_by = group_by_phase, + .group_by_id = pipeline + }); - /* Run OnSet systems for merged entities */ - if (new_count) { - flecs_notify_on_set( - world, table, old_count, new_count, NULL, true); + ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL); + + ecs_os_free(terms); + + return result; +} + +static +void EcsOnUpdatePipeline( + ecs_iter_t *it) +{ + ecs_world_t *world = it->world; + ecs_entity_t *entities = it->entities; + + int32_t i; + for (i = it->count - 1; i >= 0; i --) { + ecs_entity_t pipeline = entities[i]; + + ecs_trace("#[green]pipeline#[reset] %s created", + ecs_get_name(world, pipeline)); + ecs_log_push(); + + /* Build signature for pipeline quey that matches EcsSystems, has the + * pipeline phases as OR columns, and ignores systems with EcsInactive. + * Note that EcsDisabled is automatically ignored + * by the regular query matching */ + ecs_query_t *query = build_pipeline_query( + world, pipeline, "BuiltinPipelineQuery", true); + ecs_assert(query != NULL, ECS_INTERNAL_ERROR, NULL); + + /* Build signature for pipeline build query. The build query includes + * systems that are inactive, as an inactive system may become active as + * a result of another system, and as a result the correct merge + * operations need to be put in place. */ + ecs_query_t *build_query = build_pipeline_query( + world, pipeline, "BuiltinPipelineBuildQuery", false); + ecs_assert(build_query != NULL, ECS_INTERNAL_ERROR, NULL); + + bool added = false; + EcsPipelineQuery *pq = ecs_get_mut( + world, pipeline, EcsPipelineQuery, &added); + ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); + + if (added) { + /* Should not modify pipeline after it has been used */ + ecs_assert(pq->ops == NULL, ECS_INVALID_OPERATION, NULL); + + if (pq->query) { + ecs_query_fini(pq->query); + } + if (pq->build_query) { + ecs_query_fini(pq->build_query); + } } - ecs_os_free(snapshot_table->data->columns); - ecs_os_free(snapshot_table->data); - ecs_vector_free(snapshot_table->type); + pq->query = query; + pq->build_query = build_query; + pq->match_count = -1; + pq->ops = NULL; + + ecs_log_pop(); } } -/** Restore a snapshot */ -void ecs_snapshot_restore( +/* -- Public API -- */ + +bool ecs_progress( ecs_world_t *world, - ecs_snapshot_t *snapshot) + FLECS_FLOAT user_delta_time) { - if (snapshot->entity_index) { - /* Unfiltered snapshots have a copy of the entity index which is - * copied back entirely when the snapshot is restored */ - restore_unfiltered(world, snapshot); - } else { - restore_filtered(world, snapshot); - } + float delta_time = ecs_frame_begin(world, user_delta_time); - ecs_vector_free(snapshot->tables); + ecs_pipeline_run(world, 0, delta_time); - ecs_os_free(snapshot); + ecs_frame_end(world); + + return !world->should_quit; } -ecs_iter_t ecs_snapshot_iter( - ecs_snapshot_t *snapshot) +void ecs_set_time_scale( + ecs_world_t *world, + FLECS_FLOAT scale) { - ecs_snapshot_iter_t iter = { - .tables = snapshot->tables, - .index = 0 - }; + world->stats.time_scale = scale; +} - return (ecs_iter_t){ - .world = snapshot->world, - .table_count = ecs_vector_count(snapshot->tables), - .iter.snapshot = iter - }; +void ecs_reset_clock( + ecs_world_t *world) +{ + world->stats.world_time_total = 0; + world->stats.world_time_total_raw = 0; } -bool ecs_snapshot_next( - ecs_iter_t *it) +void ecs_deactivate_systems( + ecs_world_t *world) { - ecs_snapshot_iter_t *iter = &it->iter.snapshot; - ecs_table_leaf_t *tables = ecs_vector_first(iter->tables, ecs_table_leaf_t); - int32_t count = ecs_vector_count(iter->tables); - int32_t i; + ecs_assert(!world->is_readonly, ECS_INVALID_WHILE_ITERATING, NULL); - for (i = iter->index; i < count; i ++) { - ecs_table_t *table = tables[i].table; - if (!table) { - continue; - } + ecs_entity_t pipeline = world->pipeline; + const EcsPipelineQuery *pq = ecs_get( world, pipeline, EcsPipelineQuery); + ecs_assert(pq != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_data_t *data = tables[i].data; + /* Iterate over all systems, add EcsInvalid tag if queries aren't matched + * with any tables */ + ecs_iter_t it = ecs_query_iter(world, pq->build_query); - it->table = table; - it->count = ecs_table_count(table); - if (data) { - it->entities = ecs_vector_first(data->entities, ecs_entity_t); - } else { - it->entities = NULL; - } + /* Make sure that we defer adding the inactive tags until after iterating + * the query */ + flecs_defer_none(world, &world->stage); - it->is_valid = true; - iter->index = i + 1; - - goto yield; - } + while( ecs_query_next(&it)) { + EcsSystem *sys = ecs_term(&it, EcsSystem, 1); - it->is_valid = false; - return false; + int32_t i; + for (i = 0; i < it.count; i ++) { + ecs_query_t *query = sys[i].query; + if (query) { + if (!ecs_query_table_count(query)) { + ecs_add_id(world, it.entities[i], EcsInactive); + } + } + } + } -yield: - it->is_valid = true; - return true; + flecs_defer_flush(world, &world->stage); } -/** Cleanup snapshot */ -void ecs_snapshot_free( - ecs_snapshot_t *snapshot) +void ecs_set_pipeline( + ecs_world_t *world, + ecs_entity_t pipeline) { - flecs_sparse_free(snapshot->entity_index); - - ecs_table_leaf_t *tables = ecs_vector_first(snapshot->tables, ecs_table_leaf_t); - int32_t i, count = ecs_vector_count(snapshot->tables); - for (i = 0; i < count; i ++) { - ecs_table_leaf_t *snapshot_table = &tables[i]; - ecs_table_t *table = snapshot_table->table; - if (table) { - ecs_data_t *data = snapshot_table->data; - if (data) { - flecs_table_clear_data(snapshot->world, table, data); - ecs_os_free(data); - } - ecs_vector_free(snapshot_table->type); - } - } + ecs_poly_assert(world, ecs_world_t); + ecs_assert( ecs_get(world, pipeline, EcsPipelineQuery) != NULL, + ECS_INVALID_PARAMETER, NULL); - ecs_vector_free(snapshot->tables); - ecs_os_free(snapshot); + world->pipeline = pipeline; } -#endif - -#ifdef FLECS_SYSTEM +ecs_entity_t ecs_get_pipeline( + const ecs_world_t *world) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + world = ecs_get_world(world); + return world->pipeline; +} +/* -- Module implementation -- */ static -void invoke_status_action( +void FlecsPipelineFini( ecs_world_t *world, - ecs_entity_t system, - const EcsSystem *system_data, - ecs_system_status_t status) + void *ctx) { - ecs_system_status_action_t action = system_data->status_action; - if (action) { - action(world, system, status, system_data->status_ctx); + (void)ctx; + if (ecs_get_stage_count(world)) { + ecs_set_threads(world, 0); } } -/* Invoked when system becomes active or inactive */ -void ecs_system_activate( - ecs_world_t *world, - ecs_entity_t system, - bool activate, - const EcsSystem *system_data) +void FlecsPipelineImport( + ecs_world_t *world) { - ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); - - if (activate) { - /* If activating system, ensure that it doesn't have the Inactive tag. - * Systems are implicitly activated so they are kept out of the main - * loop as long as they aren't used. They are not implicitly deactivated - * to prevent overhead in case of oscillating app behavior. - * After activation, systems that aren't matched with anything can be - * deactivated again by explicitly calling ecs_deactivate_systems. - */ - ecs_remove_id(world, system, EcsInactive); - } - - if (!system_data) { - system_data = ecs_get(world, system, EcsSystem); - } - if (!system_data || !system_data->query) { - return; - } - - if (!activate) { - if (ecs_has_id(world, system, EcsDisabled)) { - if (!ecs_query_table_count(system_data->query)) { - /* If deactivating a disabled system that isn't matched with - * any active tables, there is nothing to deactivate. */ - return; - } - } - } + ECS_MODULE(world, FlecsPipeline); - /* Invoke system status action */ - invoke_status_action(world, system, system_data, - activate ? EcsSystemActivated : EcsSystemDeactivated); + ECS_IMPORT(world, FlecsSystem); - ecs_dbg_1("system #[green]%s#[reset] %s", - ecs_get_name(world, system), - activate ? "activated" : "deactivated"); -} + ecs_set_name_prefix(world, "Ecs"); -/* Actually enable or disable system */ -static -void ecs_enable_system( - ecs_world_t *world, - ecs_entity_t system, - EcsSystem *system_data, - bool enabled) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); + flecs_bootstrap_tag(world, EcsPipeline); + flecs_bootstrap_component(world, EcsPipelineQuery); - ecs_query_t *query = system_data->query; - if (!query) { - return; - } + /* Phases of the builtin pipeline are regular entities. Names are set so + * they can be resolved by type expressions. */ + flecs_bootstrap_tag(world, EcsPreFrame); + flecs_bootstrap_tag(world, EcsOnLoad); + flecs_bootstrap_tag(world, EcsPostLoad); + flecs_bootstrap_tag(world, EcsPreUpdate); + flecs_bootstrap_tag(world, EcsOnUpdate); + flecs_bootstrap_tag(world, EcsOnValidate); + flecs_bootstrap_tag(world, EcsPostUpdate); + flecs_bootstrap_tag(world, EcsPreStore); + flecs_bootstrap_tag(world, EcsOnStore); + flecs_bootstrap_tag(world, EcsPostFrame); - if (ecs_query_table_count(query)) { - /* Only (de)activate system if it has non-empty tables. */ - ecs_system_activate(world, system, enabled, system_data); - system_data = ecs_get_mut(world, system, EcsSystem, NULL); - } - - /* Invoke action for enable/disable status */ - invoke_status_action( - world, system, system_data, - enabled ? EcsSystemEnabled : EcsSystemDisabled); -} + /* Set ctor and dtor for PipelineQuery */ + ecs_set(world, ecs_id(EcsPipelineQuery), EcsComponentLifecycle, { + .ctor = ecs_ctor(EcsPipelineQuery), + .dtor = ecs_dtor(EcsPipelineQuery) + }); -/* -- Public API -- */ + /* When the Pipeline tag is added a pipeline will be created */ + ECS_OBSERVER(world, EcsOnUpdatePipeline, EcsOnSet, Pipeline, Type); -void ecs_enable( - ecs_world_t *world, - ecs_entity_t entity, - bool enabled) -{ - ecs_poly_assert(world, ecs_world_t); + /* Create the builtin pipeline */ + world->pipeline = ecs_type_init(world, &(ecs_type_desc_t){ + .entity = { + .name = "BuiltinPipeline", + .add = {EcsPipeline} + }, + .ids = { + EcsPreFrame, EcsOnLoad, EcsPostLoad, EcsPreUpdate, EcsOnUpdate, + EcsOnValidate, EcsPostUpdate, EcsPreStore, EcsOnStore, EcsPostFrame + } + }); - const EcsType *type_ptr = ecs_get( world, entity, EcsType); - if (type_ptr) { - /* If entity is a type, disable all entities in the type */ - ecs_vector_each(type_ptr->normalized, ecs_entity_t, e, { - ecs_enable(world, *e, enabled); - }); - } else { - if (enabled) { - ecs_remove_id(world, entity, EcsDisabled); - } else { - ecs_add_id(world, entity, EcsDisabled); - } - } + /* Cleanup thread administration when world is destroyed */ + ecs_atfini(world, FlecsPipelineFini, NULL); } -ecs_entity_t ecs_run_intern( - ecs_world_t *world, - ecs_stage_t *stage, - ecs_entity_t system, - EcsSystem *system_data, - int32_t stage_current, - int32_t stage_count, - FLECS_FLOAT delta_time, - int32_t offset, - int32_t limit, - void *param) -{ - FLECS_FLOAT time_elapsed = delta_time; - ecs_entity_t tick_source = system_data->tick_source; - - /* Support legacy behavior */ - if (!param) { - param = system_data->ctx; - } - - if (tick_source) { - const EcsTickSource *tick = ecs_get( - world, tick_source, EcsTickSource); - - if (tick) { - time_elapsed = tick->time_elapsed; - - /* If timer hasn't fired we shouldn't run the system */ - if (!tick->tick) { - return 0; - } - } else { - /* If a timer has been set but the timer entity does not have the - * EcsTimer component, don't run the system. This can be the result - * of a single-shot timer that has fired already. Not resetting the - * timer field of the system will ensure that the system won't be - * ran after the timer has fired. */ - return 0; - } - } - - ecs_time_t time_start; - bool measure_time = world->measure_system_time; - if (measure_time) { - ecs_os_get_time(&time_start); - } - - ecs_defer_begin(stage->thread_ctx); - - /* Prepare the query iterator */ - ecs_iter_t it = ecs_query_iter_page( - stage->thread_ctx, system_data->query, offset, limit); +#endif - it.system = system; - it.self = system_data->self; - it.delta_time = delta_time; - it.delta_system_time = time_elapsed; - it.frame_offset = offset; - it.param = param; - it.ctx = system_data->ctx; - it.binding_ctx = system_data->binding_ctx; - ecs_iter_action_t action = system_data->action; +#ifdef FLECS_JSON - /* If no filter is provided, just iterate tables & invoke action */ - if (stage_count <= 1) { - while (ecs_query_next(&it)) { - action(&it); - } - } else { - while (ecs_query_next_worker(&it, stage_current, stage_count)) { - action(&it); - } - } - ecs_defer_end(stage->thread_ctx); +static +int json_ser_type( + const ecs_world_t *world, + ecs_vector_t *ser, + const void *base, + ecs_strbuf_t *str); - if (measure_time) { - system_data->time_spent += (FLECS_FLOAT)ecs_time_measure(&time_start); - } +static +int json_ser_type_ops( + const ecs_world_t *world, + ecs_meta_type_op_t *ops, + int32_t op_count, + const void *base, + ecs_strbuf_t *str); - system_data->invoke_count ++; +static +int json_ser_type_op( + const ecs_world_t *world, + ecs_meta_type_op_t *op, + const void *base, + ecs_strbuf_t *str); - return it.interrupted_by; +static +void json_next( + ecs_strbuf_t *buf) +{ + ecs_strbuf_list_next(buf); } -/* -- Public API -- */ - -ecs_entity_t ecs_run_w_filter( - ecs_world_t *world, - ecs_entity_t system, - FLECS_FLOAT delta_time, - int32_t offset, - int32_t limit, - void *param) +static +void json_literal( + ecs_strbuf_t *buf, + const char *value) { - ecs_stage_t *stage = flecs_stage_from_world(&world); - - EcsSystem *system_data = (EcsSystem*)ecs_get( - world, system, EcsSystem); - assert(system_data != NULL); - - return ecs_run_intern(world, stage, system, system_data, 0, 0, delta_time, - offset, limit, param); + ecs_strbuf_appendstr(buf, value); } -ecs_entity_t ecs_run_worker( - ecs_world_t *world, - ecs_entity_t system, - int32_t stage_current, - int32_t stage_count, - FLECS_FLOAT delta_time, - void *param) +static +void json_true( + ecs_strbuf_t *buf) { - ecs_stage_t *stage = flecs_stage_from_world(&world); - - EcsSystem *system_data = (EcsSystem*)ecs_get( - world, system, EcsSystem); - assert(system_data != NULL); + json_literal(buf, "true"); +} - return ecs_run_intern( - world, stage, system, system_data, stage_current, stage_count, - delta_time, 0, 0, param); +static +void json_false( + ecs_strbuf_t *buf) +{ + json_literal(buf, "false"); } -ecs_entity_t ecs_run( - ecs_world_t *world, - ecs_entity_t system, - FLECS_FLOAT delta_time, - void *param) +static +void json_array_push( + ecs_strbuf_t *buf) { - return ecs_run_w_filter(world, system, delta_time, 0, 0, param); + ecs_strbuf_list_push(buf, "[", ", "); } -ecs_query_t* ecs_system_get_query( - const ecs_world_t *world, - ecs_entity_t system) +static +void json_array_pop( + ecs_strbuf_t *buf) { - const EcsQuery *q = ecs_get(world, system, EcsQuery); - if (q) { - return q->query; - } else { - const EcsSystem *s = ecs_get(world, system, EcsSystem); - if (s) { - return s->query; - } else { - return NULL; - } - } + ecs_strbuf_list_pop(buf, "]"); } -void* ecs_get_system_ctx( - const ecs_world_t *world, - ecs_entity_t system) +static +void json_object_push( + ecs_strbuf_t *buf) { - const EcsSystem *s = ecs_get(world, system, EcsSystem); - if (s) { - return s->ctx; - } else { - return NULL; - } + ecs_strbuf_list_push(buf, "{", ", "); } -void* ecs_get_system_binding_ctx( - const ecs_world_t *world, - ecs_entity_t system) +static +void json_object_pop( + ecs_strbuf_t *buf) { - const EcsSystem *s = ecs_get(world, system, EcsSystem); - if (s) { - return s->binding_ctx; - } else { - return NULL; - } + ecs_strbuf_list_pop(buf, "}"); } -/* Generic constructor to initialize a component to 0 */ static -void sys_ctor_init_zero( - ecs_world_t *world, - ecs_entity_t component, - const ecs_entity_t *entities, - void *ptr, - size_t size, - int32_t count, - void *ctx) +void json_string( + ecs_strbuf_t *buf, + const char *value) { - (void)world; - (void)component; - (void)entities; - (void)ctx; - memset(ptr, 0, size * (size_t)count); + ecs_strbuf_appendstr(buf, "\""); + ecs_strbuf_appendstr(buf, value); + ecs_strbuf_appendstr(buf, "\""); } -/* System destructor */ static -void ecs_colsystem_dtor( - ecs_world_t *world, - ecs_entity_t component, - const ecs_entity_t *entities, - void *ptr, - size_t size, - int32_t count, - void *ctx) +void json_member( + ecs_strbuf_t *buf, + const char *name) { - (void)component; - (void)ctx; - (void)size; + ecs_strbuf_list_appendstr(buf, "\""); + ecs_strbuf_appendstr(buf, name); + ecs_strbuf_appendstr(buf, "\":"); +} - EcsSystem *system_data = ptr; +static +ecs_primitive_kind_t json_op_to_primitive_kind(ecs_meta_type_op_kind_t kind) { + return kind - EcsOpPrimitive; +} - int i; - for (i = 0; i < count; i ++) { - EcsSystem *system = &system_data[i]; - ecs_entity_t e = entities[i]; +/* Serialize enumeration */ +static +int json_ser_enum( + const ecs_world_t *world, + ecs_meta_type_op_t *op, + const void *base, + ecs_strbuf_t *str) +{ + const EcsEnum *enum_type = ecs_get(world, op->type, EcsEnum); + ecs_assert(enum_type != NULL, ECS_INVALID_PARAMETER, NULL); - if (!ecs_is_alive(world, e)) { - /* This can happen when a set is deferred while a system is being - * cleaned up. The operation will be discarded, but the destructor - * still needs to be invoked for the value */ - continue; - } + int32_t value = *(int32_t*)base; + + /* Enumeration constants are stored in a map that is keyed on the + * enumeration value. */ + ecs_enum_constant_t *constant = ecs_map_get( + enum_type->constants, ecs_enum_constant_t, value); + if (!constant) { + return -1; + } - /* Invoke Deactivated action for active systems */ - if (system->query && ecs_query_table_count(system->query)) { - invoke_status_action(world, e, ptr, EcsSystemDeactivated); - } + ecs_strbuf_appendstr(str, "\""); + ecs_strbuf_appendstr(str, ecs_get_name(world, constant->constant)); + ecs_strbuf_appendstr(str, "\""); - /* Invoke Disabled action for enabled systems */ - if (!ecs_has_id(world, e, EcsDisabled)) { - invoke_status_action(world, e, ptr, EcsSystemDisabled); - } + return 0; +} - if (system->ctx_free) { - system->ctx_free(system->ctx); - } +/* Serialize bitmask */ +static +int json_ser_bitmask( + const ecs_world_t *world, + ecs_meta_type_op_t *op, + const void *ptr, + ecs_strbuf_t *str) +{ + const EcsBitmask *bitmask_type = ecs_get(world, op->type, EcsBitmask); + ecs_assert(bitmask_type != NULL, ECS_INVALID_PARAMETER, NULL); - if (system->status_ctx_free) { - system->status_ctx_free(system->status_ctx); - } + uint32_t value = *(uint32_t*)ptr; + ecs_map_key_t key; + ecs_bitmask_constant_t *constant; - if (system->binding_ctx_free) { - system->binding_ctx_free(system->binding_ctx); - } + if (!value) { + ecs_strbuf_appendstr(str, "0"); + return 0; + } - if (system->query) { - ecs_query_fini(system->query); + ecs_strbuf_list_push(str, "\"", "|"); + + /* Multiple flags can be set at a given time. Iterate through all the flags + * and append the ones that are set. */ + ecs_map_iter_t it = ecs_map_iter(bitmask_type->constants); + while ((constant = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { + if ((value & key) == key) { + ecs_strbuf_list_appendstr(str, + ecs_get_name(world, constant->constant)); + value -= (uint32_t)key; } } + + if (value != 0) { + /* All bits must have been matched by a constant */ + return -1; + } + + ecs_strbuf_list_pop(str, "\""); + + return 0; } +/* Serialize elements of a contiguous array */ static -void EnableMonitor( - ecs_iter_t *it) +int json_ser_elements( + const ecs_world_t *world, + ecs_meta_type_op_t *ops, + int32_t op_count, + const void *base, + int32_t elem_count, + int32_t elem_size, + ecs_strbuf_t *str) { - EcsSystem *sys = ecs_term(it, EcsSystem, 1); + json_array_push(str); - int32_t i; - for (i = 0; i < it->count; i ++) { - if (it->event == EcsOnAdd) { - ecs_enable_system(it->world, it->entities[i], &sys[i], true); - } else if (it->event == EcsOnRemove) { - ecs_enable_system(it->world, it->entities[i], &sys[i], false); + const void *ptr = base; + + int i; + for (i = 0; i < elem_count; i ++) { + ecs_strbuf_list_next(str); + if (json_ser_type_ops(world, ops, op_count, ptr, str)) { + return -1; } + ptr = ECS_OFFSET(ptr, elem_size); } -} -ecs_entity_t ecs_system_init( - ecs_world_t *world, - const ecs_system_desc_t *desc) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(!world->is_readonly, ECS_INVALID_WHILE_ITERATING, NULL); + json_array_pop(str); - ecs_entity_t existing = desc->entity.entity; - ecs_entity_t result = ecs_entity_init(world, &desc->entity); - if (!result) { - return 0; - } - - bool added = false; - EcsSystem *system = ecs_get_mut(world, result, EcsSystem, &added); - if (added) { - ecs_assert(desc->callback != NULL, ECS_INVALID_PARAMETER, NULL); - - memset(system, 0, sizeof(EcsSystem)); - - ecs_query_desc_t query_desc = desc->query; - query_desc.filter.name = desc->entity.name; - query_desc.system = result; - - ecs_query_t *query = ecs_query_init(world, &query_desc); - if (!query) { - ecs_delete(world, result); - return 0; - } - - /* Re-obtain pointer, as query may have added components */ - system = ecs_get_mut(world, result, EcsSystem, &added); - ecs_assert(added == false, ECS_INTERNAL_ERROR, NULL); - - /* Prevent the system from moving while we're initializing */ - ecs_defer_begin(world); - - system->entity = result; - system->query = query; - - system->action = desc->callback; - system->status_action = desc->status_callback; - - system->self = desc->self; - system->ctx = desc->ctx; - system->status_ctx = desc->status_ctx; - system->binding_ctx = desc->binding_ctx; - - system->ctx_free = desc->ctx_free; - system->status_ctx_free = desc->status_ctx_free; - system->binding_ctx_free = desc->binding_ctx_free; - - system->tick_source = desc->tick_source; - - /* If tables have been matched with this system it is active, and we - * should activate the in terms, if any. This will ensure that any - * OnDemand systems get enabled. */ - if (ecs_query_table_count(query)) { - ecs_system_activate(world, result, true, system); - } else { - /* If system isn't matched with any tables, mark it as inactive. This - * causes it to be ignored by the main loop. When the system matches - * with a table it will be activated. */ - ecs_add_id(world, result, EcsInactive); - } - - if (!ecs_has_id(world, result, EcsDisabled)) { - /* If system is already enabled, generate enable status. The API - * should guarantee that it exactly matches enable-disable - * notifications and activate-deactivate notifications. */ - invoke_status_action(world, result, system, EcsSystemEnabled); - - /* If column system has active (non-empty) tables, also generate the - * activate status. */ - if (ecs_query_table_count(system->query)) { - invoke_status_action(world, result, system, EcsSystemActivated); - } - } - - if (desc->interval != 0 || desc->rate != 0 || desc->tick_source != 0) { -#ifdef FLECS_TIMER - if (desc->interval != 0) { - ecs_set_interval(world, result, desc->interval); - } - - if (desc->rate) { - ecs_set_rate(world, result, desc->rate, desc->tick_source); - } else if (desc->tick_source) { - ecs_set_tick_source(world, result, desc->tick_source); - } -#else - ecs_abort(ECS_UNSUPPORTED, "timer module not available"); -#endif - } - - ecs_modified(world, result, EcsSystem); - - if (desc->entity.name) { - ecs_trace("#[green]system#[reset] %s created", - ecs_get_name(world, result)); - } - - ecs_defer_end(world); - } else { - const char *expr_desc = desc->query.filter.expr; - const char *expr_sys = system->query->filter.expr; - - /* Only check expression if it's set */ - if (expr_desc) { - if (expr_sys && !strcmp(expr_sys, "0")) expr_sys = NULL; - if (expr_desc && !strcmp(expr_desc, "0")) expr_desc = NULL; - - if (expr_sys && expr_desc) { - if (strcmp(expr_sys, expr_desc)) { - ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); - } - } else { - if (expr_sys != expr_desc) { - ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); - } - } - - /* If expr_desc is not set, and this is an existing system, don't throw - * an error because we could be updating existing parameters of the - * system such as the context or system callback. However, if no - * entity handle was provided, we have to assume that the application is - * trying to redeclare the system. */ - } else if (!existing) { - if (expr_sys) { - ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); - } - } - - /* Override the existing callback or context */ - if (desc->callback) { - system->action = desc->callback; - } - if (desc->ctx) { - system->ctx = desc->ctx; - } - if (desc->binding_ctx) { - system->binding_ctx = desc->binding_ctx; - } - } - - return result; -} - -void FlecsSystemImport( - ecs_world_t *world) -{ - ECS_MODULE(world, FlecsSystem); - - ecs_set_name_prefix(world, "Ecs"); - - flecs_bootstrap_component(world, EcsSystem); - flecs_bootstrap_component(world, EcsTickSource); - - flecs_bootstrap_tag(world, EcsOnAdd); - flecs_bootstrap_tag(world, EcsOnRemove); - flecs_bootstrap_tag(world, EcsOnSet); - flecs_bootstrap_tag(world, EcsUnSet); - - /* Put following tags in flecs.core so they can be looked up - * without using the flecs.systems prefix. */ - ecs_entity_t old_scope = ecs_set_scope(world, EcsFlecsCore); - flecs_bootstrap_tag(world, EcsInactive); - flecs_bootstrap_tag(world, EcsMonitor); - ecs_set_scope(world, old_scope); - - /* Bootstrap ctor and dtor for EcsSystem */ - ecs_set_component_actions_w_id(world, ecs_id(EcsSystem), - &(EcsComponentLifecycle) { - .ctor = sys_ctor_init_zero, - .dtor = ecs_colsystem_dtor - }); - - ECS_OBSERVER(world, EnableMonitor, EcsMonitor, System, !Disabled); -} - -#endif - - -#ifdef FLECS_JSON - - -static -int json_ser_type( - const ecs_world_t *world, - ecs_vector_t *ser, - const void *base, - ecs_strbuf_t *str); - -static -int json_ser_type_ops( - const ecs_world_t *world, - ecs_meta_type_op_t *ops, - int32_t op_count, - const void *base, - ecs_strbuf_t *str); - -static -int json_ser_type_op( - const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *base, - ecs_strbuf_t *str); - -static -void json_next( - ecs_strbuf_t *buf) -{ - ecs_strbuf_list_next(buf); -} - -static -void json_literal( - ecs_strbuf_t *buf, - const char *value) -{ - ecs_strbuf_appendstr(buf, value); -} - -static -void json_true( - ecs_strbuf_t *buf) -{ - json_literal(buf, "true"); -} - -static -void json_false( - ecs_strbuf_t *buf) -{ - json_literal(buf, "false"); -} - -static -void json_array_push( - ecs_strbuf_t *buf) -{ - ecs_strbuf_list_push(buf, "[", ", "); -} - -static -void json_array_pop( - ecs_strbuf_t *buf) -{ - ecs_strbuf_list_pop(buf, "]"); -} - -static -void json_object_push( - ecs_strbuf_t *buf) -{ - ecs_strbuf_list_push(buf, "{", ", "); -} - -static -void json_object_pop( - ecs_strbuf_t *buf) -{ - ecs_strbuf_list_pop(buf, "}"); -} - -static -void json_string( - ecs_strbuf_t *buf, - const char *value) -{ - ecs_strbuf_appendstr(buf, "\""); - ecs_strbuf_appendstr(buf, value); - ecs_strbuf_appendstr(buf, "\""); -} - -static -void json_member( - ecs_strbuf_t *buf, - const char *name) -{ - ecs_strbuf_list_appendstr(buf, "\""); - ecs_strbuf_appendstr(buf, name); - ecs_strbuf_appendstr(buf, "\":"); -} - -static -ecs_primitive_kind_t json_op_to_primitive_kind(ecs_meta_type_op_kind_t kind) { - return kind - EcsOpPrimitive; -} - -/* Serialize enumeration */ -static -int json_ser_enum( - const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *base, - ecs_strbuf_t *str) -{ - const EcsEnum *enum_type = ecs_get(world, op->type, EcsEnum); - ecs_assert(enum_type != NULL, ECS_INVALID_PARAMETER, NULL); - - int32_t value = *(int32_t*)base; - - /* Enumeration constants are stored in a map that is keyed on the - * enumeration value. */ - ecs_enum_constant_t *constant = ecs_map_get( - enum_type->constants, ecs_enum_constant_t, value); - if (!constant) { - return -1; - } - - ecs_strbuf_appendstr(str, "\""); - ecs_strbuf_appendstr(str, ecs_get_name(world, constant->constant)); - ecs_strbuf_appendstr(str, "\""); - - return 0; -} - -/* Serialize bitmask */ -static -int json_ser_bitmask( - const ecs_world_t *world, - ecs_meta_type_op_t *op, - const void *ptr, - ecs_strbuf_t *str) -{ - const EcsBitmask *bitmask_type = ecs_get(world, op->type, EcsBitmask); - ecs_assert(bitmask_type != NULL, ECS_INVALID_PARAMETER, NULL); - - uint32_t value = *(uint32_t*)ptr; - ecs_map_key_t key; - ecs_bitmask_constant_t *constant; - - if (!value) { - ecs_strbuf_appendstr(str, "0"); - return 0; - } - - ecs_strbuf_list_push(str, "\"", "|"); - - /* Multiple flags can be set at a given time. Iterate through all the flags - * and append the ones that are set. */ - ecs_map_iter_t it = ecs_map_iter(bitmask_type->constants); - while ((constant = ecs_map_next(&it, ecs_bitmask_constant_t, &key))) { - if ((value & key) == key) { - ecs_strbuf_list_appendstr(str, - ecs_get_name(world, constant->constant)); - value -= (uint32_t)key; - } - } - - if (value != 0) { - /* All bits must have been matched by a constant */ - return -1; - } - - ecs_strbuf_list_pop(str, "\""); - - return 0; -} - -/* Serialize elements of a contiguous array */ -static -int json_ser_elements( - const ecs_world_t *world, - ecs_meta_type_op_t *ops, - int32_t op_count, - const void *base, - int32_t elem_count, - int32_t elem_size, - ecs_strbuf_t *str) -{ - json_array_push(str); - - const void *ptr = base; - - int i; - for (i = 0; i < elem_count; i ++) { - ecs_strbuf_list_next(str); - if (json_ser_type_ops(world, ops, op_count, ptr, str)) { - return -1; - } - ptr = ECS_OFFSET(ptr, elem_size); - } - - json_array_pop(str); - - return 0; -} + return 0; +} static int json_ser_type_elements( @@ -24969,6410 +24965,5768 @@ void FlecsCoreDocImport( #endif +#ifdef FLECS_PLECS -#ifdef FLECS_DOC -static ECS_COPY(EcsDocDescription, dst, src, { - ecs_os_strset((char**)&dst->value, src->value); +#define TOK_NEWLINE '\n' +#define TOK_WITH "with" +#define TOK_USING "using" -}) +#define STACK_MAX_SIZE (64) -static ECS_MOVE(EcsDocDescription, dst, src, { - ecs_os_free((char*)dst->value); - dst->value = src->value; - src->value = NULL; -}) +typedef struct { + const char *name; + const char *code; -static ECS_DTOR(EcsDocDescription, ptr, { - ecs_os_free((char*)ptr->value); -}) + ecs_entity_t last_predicate; + ecs_entity_t last_subject; + ecs_entity_t last_object; -void ecs_doc_set_brief( - ecs_world_t *world, - ecs_entity_t entity, - const char *description) -{ - ecs_set_pair(world, entity, EcsDocDescription, EcsDocBrief, { - .value = description - }); -} + ecs_id_t last_assign_id; + ecs_entity_t assign_to; -void ecs_doc_set_detail( - ecs_world_t *world, - ecs_entity_t entity, - const char *description) -{ - ecs_set_pair(world, entity, EcsDocDescription, EcsDocDetail, { - .value = description - }); -} + ecs_entity_t scope[STACK_MAX_SIZE]; + ecs_entity_t default_scope_type[STACK_MAX_SIZE]; + ecs_entity_t with[STACK_MAX_SIZE]; + ecs_entity_t using[STACK_MAX_SIZE]; + int32_t with_frames[STACK_MAX_SIZE]; + int32_t using_frames[STACK_MAX_SIZE]; + int32_t sp; + int32_t with_frame; + int32_t using_frame; -void ecs_doc_set_link( - ecs_world_t *world, - ecs_entity_t entity, - const char *link) -{ - ecs_set_pair(world, entity, EcsDocDescription, EcsDocLink, { - .value = link - }); -} + char *comment; -const char* ecs_doc_get_brief( - const ecs_world_t *world, - ecs_entity_t entity) -{ - EcsDocDescription *ptr = ecs_get_pair( - world, entity, EcsDocDescription, EcsDocBrief); - if (ptr) { - return ptr->value; - } else { - return NULL; - } -} + bool with_stmt; + bool using_stmt; + bool assign_stmt; + bool isa_stmt; -const char* ecs_doc_get_detail( - const ecs_world_t *world, - ecs_entity_t entity) -{ - EcsDocDescription *ptr = ecs_get_pair( - world, entity, EcsDocDescription, EcsDocDetail); - if (ptr) { - return ptr->value; - } else { - return NULL; - } -} + int32_t errors; +} plecs_state_t; -const char* ecs_doc_get_link( +static +ecs_entity_t plecs_lookup( const ecs_world_t *world, - ecs_entity_t entity) + const char *path, + plecs_state_t *state, + bool is_subject) { - EcsDocDescription *ptr = ecs_get_pair( - world, entity, EcsDocDescription, EcsDocLink); - if (ptr) { - return ptr->value; - } else { - return NULL; - } -} - -void FlecsDocImport( - ecs_world_t *world) -{ - ECS_MODULE(world, FlecsDoc); - - ecs_set_name_prefix(world, "EcsDoc"); - - flecs_bootstrap_component(world, EcsDocDescription); - flecs_bootstrap_tag(world, EcsDocBrief); - flecs_bootstrap_tag(world, EcsDocDetail); - flecs_bootstrap_tag(world, EcsDocLink); - - ecs_set_component_actions(world, EcsDocDescription, { - .ctor = ecs_default_ctor, - .move = ecs_move(EcsDocDescription), - .copy = ecs_copy(EcsDocDescription), - .dtor = ecs_dtor(EcsDocDescription) - }); -} - -#endif - -#ifdef FLECS_PARSER - - -#define ECS_ANNOTATION_LENGTH_MAX (16) - -#define TOK_NEWLINE '\n' -#define TOK_COLON ':' -#define TOK_AND ',' -#define TOK_OR "||" -#define TOK_NOT '!' -#define TOK_OPTIONAL '?' -#define TOK_BITWISE_OR '|' -#define TOK_NAME_SEP '.' -#define TOK_BRACKET_OPEN '[' -#define TOK_BRACKET_CLOSE ']' -#define TOK_WILDCARD '*' -#define TOK_SINGLETON '$' -#define TOK_PAREN_OPEN '(' -#define TOK_PAREN_CLOSE ')' -#define TOK_AS_ENTITY '\\' - -#define TOK_SELF "self" -#define TOK_SUPERSET "super" -#define TOK_SUBSET "sub" -#define TOK_CASCADE "cascade" -#define TOK_PARENT "parent" -#define TOK_ALL "all" - -#define TOK_OWNED "OVERRIDE" - -#define TOK_ROLE_PAIR "PAIR" -#define TOK_ROLE_AND "AND" -#define TOK_ROLE_OR "OR" -#define TOK_ROLE_XOR "XOR" -#define TOK_ROLE_NOT "NOT" -#define TOK_ROLE_SWITCH "SWITCH" -#define TOK_ROLE_CASE "CASE" -#define TOK_ROLE_DISABLED "DISABLED" - -#define TOK_IN "in" -#define TOK_OUT "out" -#define TOK_INOUT "inout" - -#define ECS_MAX_TOKEN_SIZE (256) + ecs_entity_t e = 0; -typedef char ecs_token_t[ECS_MAX_TOKEN_SIZE]; + if (!is_subject) { + int using_scope = state->using_frame - 1; + for (; using_scope >= 0; using_scope--) { + e = ecs_lookup_path_w_sep( + world, state->using[using_scope], path, NULL, NULL, false); + if (e) { + break; + } + } + } -const char* ecs_parse_eol_and_whitespace( - const char *ptr) -{ - while (isspace(*ptr)) { - ptr ++; + if (!e) { + e = ecs_lookup_path_w_sep(world, 0, path, NULL, NULL, !is_subject); } - return ptr; + return e; } -/** Skip spaces when parsing signature */ -const char* ecs_parse_whitespace( - const char *ptr) +/* Lookup action used for deserializing entity refs in component values */ +#ifdef FLECS_EXPR +static +ecs_entity_t plecs_lookup_action( + const ecs_world_t *world, + const char *path, + void *ctx) { - while ((*ptr != '\n') && isspace(*ptr)) { - ptr ++; - } - - return ptr; + return plecs_lookup(world, path, ctx, false); } +#endif -const char* ecs_parse_digit( +static +void clear_comment( + const char *expr, const char *ptr, - char *token) + plecs_state_t *state) { - char *tptr = token; - char ch = ptr[0]; - - if (!isdigit(ch) && ch != '-') { - ecs_parser_error(NULL, NULL, 0, "invalid start of number '%s'", ptr); - return NULL; - } - - tptr[0] = ch; - tptr ++; - ptr ++; - - for (; (ch = *ptr); ptr ++) { - if (!isdigit(ch)) { - break; - } + if (state->comment) { + ecs_parser_error(state->name, expr, ptr - expr, "unused doc comment"); + ecs_os_free(state->comment); + state->comment = NULL; - tptr[0] = ch; - tptr ++; + state->errors ++; /* Non-fatal error */ } - - tptr[0] = '\0'; - - return ptr; } static -bool is_newline_comment( - const char *ptr) -{ - if (ptr[0] == '/' && ptr[1] == '/') { - return true; - } - return false; -} - -const char* ecs_parse_fluff( +const char* parse_fluff( + const char *expr, const char *ptr, - char **last_comment) + plecs_state_t *state) { - const char *last_comment_start = NULL; - - do { - /* Skip whitespaces before checking for a comment */ - ptr = ecs_parse_whitespace(ptr); + char *comment; + const char *next = ecs_parse_fluff(ptr, &comment); - /* Newline comment, skip until newline character */ - if (is_newline_comment(ptr)) { - ptr += 2; - last_comment_start = ptr; + if (comment && comment[0] == '/') { + comment = (char*)ecs_parse_fluff(comment + 1, NULL); + int32_t len = (ecs_size_t)(next - comment); + int32_t newline_count = 0; - while (ptr[0] && ptr[0] != TOK_NEWLINE) { - ptr ++; + /* Trim trailing whitespaces */ + while (len >= 0 && (isspace(comment[len - 1]))) { + if (comment[len - 1] == '\n') { + newline_count ++; + if (newline_count > 1) { + /* If newline separates comment from statement, discard */ + len = -1; + break; + } } + len --; } - /* If a newline character is found, skip it */ - if (ptr[0] == TOK_NEWLINE) { - ptr ++; + if (len > 0) { + clear_comment(expr, ptr, state); + state->comment = ecs_os_calloc_n(char, len + 1); + ecs_os_strncpy(state->comment, comment, len); + } else { + ecs_parser_error(state->name, expr, ptr - expr, + "unused doc comment"); + state->errors ++; + } + } else { + if (ptr != next && state->comment) { + clear_comment(expr, ptr, state); } - - } while (isspace(ptr[0]) || is_newline_comment(ptr)); - - if (last_comment) { - *last_comment = (char*)last_comment_start; } - return ptr; + return next; } -/* -- Private functions -- */ - static -bool valid_identifier_start_char( - char ch) +ecs_entity_t ensure_entity( + ecs_world_t *world, + plecs_state_t *state, + const char *path, + bool is_subject) { - if (ch && (isalpha(ch) || (ch == '.') || (ch == '_') || (ch == '*') || - (ch == '0') || (ch == TOK_AS_ENTITY) || isdigit(ch))) - { - return true; + if (!path) { + return 0; } - return false; -} - -static -bool valid_token_start_char( - char ch) -{ - if ((ch == '"') || (ch == '{') || (ch == '}') || (ch == ',') || (ch == '-') - || (ch == '[') || (ch == ']') || valid_identifier_start_char(ch)) - { - return true; - } + ecs_entity_t e = plecs_lookup(world, path, state, is_subject); + if (!e) { + if (!is_subject) { + /* If this is not a subject create an existing empty id, which + * ensures that scope & with are not applied */ + e = ecs_new_id(world); + } - return false; -} + e = ecs_add_path(world, e, 0, path); + ecs_assert(e != 0, ECS_INTERNAL_ERROR, NULL); + } else { + /* If entity exists, make sure it gets the right scope and with */ + if (is_subject) { + ecs_entity_t scope = ecs_get_scope(world); + if (scope) { + ecs_add_pair(world, e, EcsChildOf, scope); + } -static -bool valid_token_char( - char ch) -{ - if (ch && - (isalpha(ch) || isdigit(ch) || ch == '_' || ch == '.' || ch == '"')) - { - return true; + ecs_entity_t with = ecs_get_with(world); + if (with) { + ecs_add_id(world, e, with); + } + } } - return false; + return e; } static -bool valid_operator_char( - char ch) +bool pred_is_subj( + ecs_term_t *term, + plecs_state_t *state) { - if (ch == TOK_OPTIONAL || ch == TOK_NOT) { - return true; + if (term->subj.name != NULL) { + return false; } - - return false; + if (term->obj.name != NULL) { + return false; + } + if (term->subj.set.mask == EcsNothing) { + return false; + } + if (state->with_stmt) { + return false; + } + if (state->assign_stmt) { + return false; + } + if (state->isa_stmt) { + return false; + } + if (state->using_stmt) { + return false; + } + return true; } static -const char* parse_digit( - const char *ptr, - char *token_out) -{ - ptr = ecs_parse_whitespace(ptr); - ptr = ecs_parse_digit(ptr, token_out); - return ecs_parse_whitespace(ptr); -} - -const char* ecs_parse_token( +int create_term( + ecs_world_t *world, + ecs_term_t *term, const char *name, const char *expr, - const char *ptr, - char *token_out) + int64_t column, + plecs_state_t *state) { - int64_t column = ptr - expr; - - ptr = ecs_parse_whitespace(ptr); - char *tptr = token_out, ch = ptr[0]; + state->last_subject = 0; + state->last_predicate = 0; + state->last_object = 0; + state->last_assign_id = 0; - if (!valid_token_start_char(ch)) { - if (ch == '\0' || ch == '\n') { - ecs_parser_error(name, expr, column, - "unexpected end of expression"); - } else { - ecs_parser_error(name, expr, column, - "invalid start of token '%s'", ptr); - } - return NULL; + if (!ecs_term_id_is_set(&term->pred)) { + ecs_parser_error(name, expr, column, "missing predicate in expression"); + return -1; } - tptr[0] = ch; - tptr ++; - ptr ++; - - if (ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == ',') { - tptr[0] = 0; - return ptr; + if (state->assign_stmt && term->subj.entity != EcsThis) { + ecs_parser_error(name, expr, column, + "invalid statement in assign statement"); + return -1; } - int tmpl_nesting = 0; - bool in_str = ch == '"'; + bool pred_as_subj = pred_is_subj(term, state); - for (; (ch = *ptr); ptr ++) { - if (ch == '<') { - tmpl_nesting ++; - } else if (ch == '>') { - if (!tmpl_nesting) { - break; - } - tmpl_nesting --; - } else if (ch == '"') { - in_str = !in_str; - } else - if (!valid_token_char(ch) && !in_str) { - break; - } + ecs_entity_t pred = ensure_entity(world, state, term->pred.name, pred_as_subj); + ecs_entity_t subj = ensure_entity(world, state, term->subj.name, true); + ecs_entity_t obj = 0; - tptr[0] = ch; - tptr ++; + if (ecs_term_id_is_set(&term->obj)) { + obj = ensure_entity(world, state, term->obj.name, + state->assign_stmt == false); } - tptr[0] = '\0'; + if (state->assign_stmt || state->isa_stmt) { + subj = state->assign_to; + } - if (tmpl_nesting != 0) { + if (state->isa_stmt && obj) { ecs_parser_error(name, expr, column, - "identifier '%s' has mismatching < > pairs", ptr); - return NULL; + "invalid object in inheritance statement"); + return -1; } - const char *next_ptr = ecs_parse_whitespace(ptr); - if (next_ptr[0] == ':' && next_ptr != ptr) { - /* Whitespace between token and : is significant */ - ptr = next_ptr - 1; - } else { - ptr = next_ptr; + if (state->using_stmt && (obj || subj)) { + ecs_parser_error(name, expr, column, + "invalid predicate/object in using statement"); + return -1; } - return ptr; -} - -static -const char* ecs_parse_identifier( - const char *name, - const char *expr, - const char *ptr, - char *token_out) -{ - if (!valid_identifier_start_char(ptr[0])) { - ecs_parser_error(name, expr, (ptr - expr), - "expected start of identifier"); - return NULL; + if (state->isa_stmt) { + pred = ecs_pair(EcsIsA, pred); } - ptr = ecs_parse_token(name, expr, ptr, token_out); - - return ptr; -} - -static -int parse_identifier( - const char *token, - ecs_term_id_t *out) -{ - char ch = token[0]; + if (subj) { + if (!obj) { + ecs_add_id(world, subj, pred); + state->last_assign_id = pred; + } else { + ecs_add_pair(world, subj, pred, obj); + state->last_object = obj; + state->last_assign_id = ecs_pair(pred, obj); + } + state->last_predicate = pred; + state->last_subject = subj; - const char *tptr = token; - if (ch == TOK_AS_ENTITY) { - tptr ++; + pred_as_subj = false; + } else { + if (!obj) { + /* If no subject or object were provided, use predicate as subj + * unless the expression explictly excluded the subject */ + if (pred_as_subj) { + state->last_subject = pred; + subj = pred; + } else { + state->last_predicate = pred; + pred_as_subj = false; + } + } else { + state->last_predicate = pred; + state->last_object = obj; + pred_as_subj = false; + } } - out->name = ecs_os_strdup(tptr); + /* If this is a with clause (the list of entities between 'with' and scope + * open), add subject to the array of with frames */ + if (state->with_stmt) { + ecs_assert(pred != 0, ECS_INTERNAL_ERROR, NULL); + ecs_id_t id; - if (ch == TOK_AS_ENTITY) { - out->var = EcsVarIsEntity; + if (obj) { + id = ecs_pair(pred, obj); + } else { + id = pred; + } + + state->with[state->with_frame ++] = id; + + } else if (state->using_stmt) { + ecs_assert(pred != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(obj == 0, ECS_INTERNAL_ERROR, NULL); + + state->using[state->using_frame ++] = pred; + state->using_frames[state->sp] = state->using_frame; + + /* If this is not a with/using clause, add with frames to subject */ + } else { + if (subj) { + int32_t i, frame_count = state->with_frames[state->sp]; + for (i = 0; i < frame_count; i ++) { + ecs_add_id(world, subj, state->with[i]); + } + } + } + + /* If an id was provided by itself, add default scope type to it */ + ecs_entity_t default_scope_type = state->default_scope_type[state->sp]; + if (pred_as_subj && default_scope_type) { + ecs_add_id(world, subj, default_scope_type); + } + + /* If a comment preceded the statement, add it as a brief description */ +#ifdef FLECS_DOC + if (subj && state->comment) { + ecs_doc_set_brief(world, subj, state->comment); + ecs_os_free(state->comment); + state->comment = NULL; + } +#endif + + if (state->isa_stmt) { + state->last_assign_id = 0; + state->assign_to = 0; + state->isa_stmt = false; } return 0; } static -ecs_entity_t parse_role( +const char* parse_inherit_stmt( const char *name, - const char *sig, - int64_t column, - const char *token) + const char *expr, + const char *ptr, + plecs_state_t *state) { - if (!ecs_os_strcmp(token, TOK_ROLE_PAIR)) - { - return ECS_PAIR; - } else if (!ecs_os_strcmp(token, TOK_ROLE_AND)) { - return ECS_AND; - } else if (!ecs_os_strcmp(token, TOK_ROLE_OR)) { - return ECS_OR; - } else if (!ecs_os_strcmp(token, TOK_ROLE_XOR)) { - return ECS_XOR; - } else if (!ecs_os_strcmp(token, TOK_ROLE_NOT)) { - return ECS_NOT; - } else if (!ecs_os_strcmp(token, TOK_ROLE_SWITCH)) { - return ECS_SWITCH; - } else if (!ecs_os_strcmp(token, TOK_ROLE_CASE)) { - return ECS_CASE; - } else if (!ecs_os_strcmp(token, TOK_OWNED)) { - return ECS_OVERRIDE; - } else if (!ecs_os_strcmp(token, TOK_ROLE_DISABLED)) { - return ECS_DISABLED; - } else { - ecs_parser_error(name, sig, column, "invalid role '%s'", token); - return 0; + if (state->isa_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "cannot nest inheritance"); + return NULL; } -} -static -ecs_oper_kind_t parse_operator( - char ch) -{ - if (ch == TOK_OPTIONAL) { - return EcsOptional; - } else if (ch == TOK_NOT) { - return EcsNot; - } else { - ecs_abort(ECS_INTERNAL_ERROR, NULL); + if (!state->last_subject) { + ecs_parser_error(name, expr, ptr - expr, + "missing entity to assign inheritance to"); + return NULL; } + + state->isa_stmt = true; + state->assign_to = state->last_subject; + + return ptr; } static -const char* parse_annotation( +const char* parse_assign_expr( + ecs_world_t *world, const char *name, - const char *sig, - int64_t column, - const char *ptr, - ecs_inout_kind_t *inout_kind_out) + const char *expr, + const char *ptr, + plecs_state_t *state) { - char token[ECS_MAX_TOKEN_SIZE]; + (void)world; + + if (!state->assign_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "unexpected value outside of assignment statement"); + return NULL; + } - ptr = ecs_parse_identifier(name, sig, ptr, token); - if (!ptr) { + ecs_id_t assign_id = state->last_assign_id; + if (!assign_id) { + ecs_parser_error(name, expr, ptr - expr, + "missing type for assignment statement"); return NULL; } - if (!strcmp(token, "in")) { - *inout_kind_out = EcsIn; - } else - if (!strcmp(token, "out")) { - *inout_kind_out = EcsOut; - } else - if (!strcmp(token, "inout")) { - *inout_kind_out = EcsInOut; +#ifndef FLECS_EXPR + ecs_parser_error(name, expr, ptr - expr, + "cannot parse value, missing FLECS_EXPR addon"); + return NULL; +#else + ecs_entity_t assign_to = state->assign_to; + if (!assign_to) { + assign_to = state->last_subject; } - ptr = ecs_parse_whitespace(ptr); + if (!assign_to) { + ecs_parser_error(name, expr, ptr - expr, + "missing entity to assign to"); + return NULL; + } - if (ptr[0] != TOK_BRACKET_CLOSE) { - ecs_parser_error(name, sig, column, "expected ]"); + ecs_entity_t type = ecs_get_typeid(world, assign_id); + if (!type) { + char *id_str = ecs_id_str(world, assign_id); + ecs_parser_error(name, expr, ptr - expr, + "invalid assignment, '%s' is not a type", id_str); + ecs_os_free(id_str); return NULL; } - return ptr + 1; + void *value_ptr = ecs_get_mut_id( + world, assign_to, assign_id, NULL); + + ptr = ecs_parse_expr(world, ptr, type, value_ptr, + &(ecs_parse_expr_desc_t) { + .name = name, + .expr = expr, + .lookup_action = plecs_lookup_action, + .lookup_ctx = state + }); + if (!ptr) { + return NULL; + } + + ecs_modified_id(world, assign_to, assign_id); +#endif + + return ptr; } static -uint8_t parse_set_token( - const char *token) +const char* parse_assign_stmt( + ecs_world_t *world, + const char *name, + const char *expr, + const char *ptr, + plecs_state_t *state) { - if (!ecs_os_strcmp(token, TOK_SELF)) { - return EcsSelf; - } else if (!ecs_os_strcmp(token, TOK_SUPERSET)) { - return EcsSuperSet; - } else if (!ecs_os_strcmp(token, TOK_SUBSET)) { - return EcsSubSet; - } else if (!ecs_os_strcmp(token, TOK_CASCADE)) { - return EcsCascade; - } else if (!ecs_os_strcmp(token, TOK_ALL)) { - return EcsAll; - } else if (!ecs_os_strcmp(token, TOK_PARENT)) { - return EcsParent; - } else { - return 0; + (void)world; + + if (state->isa_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "missing base for inheritance statement"); + return NULL; + } + + /* Component scope (add components to entity) */ + if (!state->last_subject) { + ecs_parser_error(name, expr, ptr - expr, + "missing entity to assign to"); + return NULL; + } + + if (state->assign_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "invalid assign statement in assign statement"); + return NULL; } + + state->assign_stmt = true; + state->assign_to = state->last_subject; + + return ptr; } static -const char* parse_set_expr( - const ecs_world_t *world, +const char* parse_using_stmt( const char *name, const char *expr, - int64_t column, const char *ptr, - char *token, - ecs_term_id_t *id, - char tok_end) + plecs_state_t *state) { - char token_buf[ECS_MAX_TOKEN_SIZE] = {0}; - if (!token) { - token = token_buf; - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - return NULL; - } + if (state->isa_stmt || state->assign_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "invalid usage of using keyword"); + return NULL; } - do { - uint8_t tok = parse_set_token(token); - if (!tok) { - ecs_parser_error(name, expr, column, - "invalid set token '%s'", token); - return NULL; - } + /* Add following expressions to using list */ + state->using_stmt = true; - if (id->set.mask & tok) { - ecs_parser_error(name, expr, column, - "duplicate set token '%s'", token); - return NULL; - } + return ptr + 5; +} - if ((tok == EcsSubSet && id->set.mask & EcsSuperSet) || - (tok == EcsSuperSet && id->set.mask & EcsSubSet)) - { - ecs_parser_error(name, expr, column, - "cannot mix super and sub", token); - return NULL; - } - - id->set.mask |= tok; +static +const char* parse_with_stmt( + const char *name, + const char *expr, + const char *ptr, + plecs_state_t *state) +{ + if (state->isa_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "invalid with after inheritance"); + return NULL; + } - if (ptr[0] == TOK_PAREN_OPEN) { - ptr ++; + if (state->assign_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "invalid with in assign_stmt"); + return NULL; + } - /* Relationship (overrides IsA default) */ - if (!isdigit(ptr[0]) && valid_token_start_char(ptr[0])) { - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - return NULL; - } + /* Add following expressions to with list */ + state->with_stmt = true; + return ptr + 5; +} - id->set.relation = ecs_lookup_fullpath(world, token); - if (!id->set.relation) { - ecs_parser_error(name, expr, column, - "unresolved identifier '%s'", token); - return NULL; - } +static +const char* parse_scope_open( + ecs_world_t *world, + const char *name, + const char *expr, + const char *ptr, + plecs_state_t *state) +{ + if (state->isa_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "missing base for inheritance"); + return NULL; + } - if (ptr[0] == TOK_AND) { - ptr = ecs_parse_whitespace(ptr + 1); - } else if (ptr[0] != TOK_PAREN_CLOSE) { - ecs_parser_error(name, expr, column, - "expected ',' or ')'"); - return NULL; - } - } + if (state->assign_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "invalid scope in assign_stmt"); + return NULL; + } - /* Max depth of search */ - if (isdigit(ptr[0])) { - ptr = parse_digit(ptr, token); - if (!ptr) { - return NULL; - } + state->sp ++; - id->set.max_depth = atoi(token); - if (id->set.max_depth < 0) { - ecs_parser_error(name, expr, column, - "invalid negative depth"); - return NULL; - } + ecs_entity_t scope = 0; + ecs_entity_t default_scope_type = 0; - if (ptr[0] == ',') { - ptr = ecs_parse_whitespace(ptr + 1); - } - } + if (!state->with_stmt) { + if (state->last_subject) { + scope = state->last_subject; + ecs_set_scope(world, state->last_subject); - /* If another digit is found, previous depth was min depth */ - if (isdigit(ptr[0])) { - ptr = parse_digit(ptr, token); - if (!ptr) { - return NULL; - } + /* Check if scope has a default child component */ + ecs_entity_t def_type_src = ecs_get_object_for_id(world, scope, + 0, ecs_pair(EcsDefaultChildComponent, EcsWildcard)); - id->set.min_depth = id->set.max_depth; - id->set.max_depth = atoi(token); - if (id->set.max_depth < 0) { - ecs_parser_error(name, expr, column, - "invalid negative depth"); - return NULL; - } + if (def_type_src) { + default_scope_type = ecs_get_object( + world, def_type_src, EcsDefaultChildComponent, 0); } - - if (ptr[0] != TOK_PAREN_CLOSE) { - ecs_parser_error(name, expr, column, "expected ')', got '%c'", - ptr[0]); - return NULL; + } else { + if (state->last_object) { + scope = ecs_pair( + state->last_predicate, state->last_object); + ecs_set_with(world, scope); } else { - ptr = ecs_parse_whitespace(ptr + 1); - if (ptr[0] != tok_end && ptr[0] != TOK_AND && ptr[0] != 0) { - ecs_parser_error(name, expr, column, - "expected end of set expr"); - return NULL; - } - } - } - - /* Next token in set expression */ - if (ptr[0] == TOK_BITWISE_OR) { - ptr ++; - if (valid_token_start_char(ptr[0])) { - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - return NULL; + if (state->last_predicate) { + scope = ecs_pair(EcsChildOf, state->last_predicate); } + ecs_set_scope(world, state->last_predicate); } - - /* End of set expression */ - } else if (ptr[0] == tok_end || ptr[0] == TOK_AND || !ptr[0]) { - break; } - } while (true); - if (id->set.mask & EcsCascade && !(id->set.mask & EcsSuperSet) && - !(id->set.mask & EcsSubSet)) - { - /* If cascade is used without specifying super or sub, assume - * super */ - id->set.mask |= EcsSuperSet; + state->scope[state->sp] = scope; + state->default_scope_type[state->sp] = default_scope_type; + } else { + state->scope[state->sp] = state->scope[state->sp - 1]; + state->default_scope_type[state->sp] = + state->default_scope_type[state->sp - 1]; } - if (id->set.mask & EcsSelf && id->set.min_depth != 0) { - ecs_parser_error(name, expr, column, - "min_depth must be zero for set expression with 'self'"); - return NULL; - } + state->using_frames[state->sp] = state->using_frame; + state->with_frames[state->sp] = state->with_frame; + state->with_stmt = false; return ptr; } static -const char* parse_arguments( - const ecs_world_t *world, +const char* parse_scope_close( + ecs_world_t *world, const char *name, const char *expr, - int64_t column, const char *ptr, - char *token, - ecs_term_t *term) + plecs_state_t *state) { - (void)column; + if (state->isa_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "invalid '}' after inheritance statement"); + return NULL; + } - int32_t arg = 0; + if (state->assign_stmt) { + ecs_parser_error(name, expr, ptr - expr, + "unfinished assignment before }"); + return NULL; + } - do { - if (valid_token_start_char(ptr[0])) { - if (arg == 2) { - ecs_parser_error(name, expr, (ptr - expr), - "too many arguments in term"); - return NULL; - } + state->scope[state->sp] = 0; + state->default_scope_type[state->sp] = 0; + state->sp --; - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - return NULL; - } + if (state->sp < 0) { + ecs_parser_error(name, expr, ptr - expr, "invalid } without a {"); + return NULL; + } - ecs_term_id_t *term_id; + ecs_id_t id = state->scope[state->sp]; - if (arg == 0) { - term_id = &term->subj; - } else if (arg == 1) { - term_id = &term->obj; - } + if (!id || ECS_HAS_ROLE(id, PAIR)) { + ecs_set_with(world, id); + } - /* If token is a colon, the token is an identifier followed by a - * set expression. */ - if (ptr[0] == TOK_COLON) { - if (parse_identifier(token, term_id)) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid identifier '%s'", token); - return NULL; - } - - ptr = ecs_parse_whitespace(ptr + 1); - ptr = parse_set_expr(world, name, expr, (ptr - expr), ptr, - NULL, term_id, TOK_PAREN_CLOSE); - if (!ptr) { - return NULL; - } - - /* If token is a self, super or sub token, this is a set - * expression */ - } else if (!ecs_os_strcmp(token, TOK_ALL) || - !ecs_os_strcmp(token, TOK_CASCADE) || - !ecs_os_strcmp(token, TOK_SELF) || - !ecs_os_strcmp(token, TOK_SUPERSET) || - !ecs_os_strcmp(token, TOK_SUBSET) || - !(ecs_os_strcmp(token, TOK_PARENT))) - { - ptr = parse_set_expr(world, name, expr, (ptr - expr), ptr, - token, term_id, TOK_PAREN_CLOSE); - if (!ptr) { - return NULL; - } - - /* Regular identifier */ - } else if (parse_identifier(token, term_id)) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid identifier '%s'", token); - return NULL; - } + if (!id || !ECS_HAS_ROLE(id, PAIR)) { + ecs_set_scope(world, id); + } - if (ptr[0] == TOK_AND) { - ptr = ecs_parse_whitespace(ptr + 1); + state->with_frame = state->with_frames[state->sp]; + state->using_frame = state->using_frames[state->sp]; + state->last_subject = 0; + state->assign_stmt = false; - term->role = ECS_PAIR; + return ptr; +} - } else if (ptr[0] == TOK_PAREN_CLOSE) { - ptr = ecs_parse_whitespace(ptr + 1); - break; +static +const char *parse_plecs_term( + ecs_world_t *world, + const char *name, + const char *expr, + const char *ptr, + plecs_state_t *state) +{ + ecs_term_t term = {0}; - } else { - ecs_parser_error(name, expr, (ptr - expr), - "expected ',' or ')'"); - return NULL; - } + ptr = ecs_parse_term(world, name, expr, ptr, &term); + if (!ptr) { + return NULL; + } - } else { - ecs_parser_error(name, expr, (ptr - expr), - "expected identifier or set expression"); - return NULL; - } + if (!ecs_term_is_initialized(&term)) { + ecs_parser_error(name, expr, ptr - expr, "expected identifier"); + return NULL; /* No term found */ + } - arg ++; + if (create_term(world, &term, name, expr, (ptr - expr), state)) { + ecs_term_fini(&term); + return NULL; /* Failed to create term */ + } - } while (true); + ecs_term_fini(&term); return ptr; } static -const char* parse_term( - const ecs_world_t *world, +const char* parse_stmt( + ecs_world_t *world, const char *name, const char *expr, - ecs_term_t *term_out) + const char *ptr, + plecs_state_t *state) { - const char *ptr = expr; - char token[ECS_MAX_TOKEN_SIZE] = {0}; - ecs_term_t term = { .move = true /* parser never owns resources */ }; + state->assign_stmt = false; + state->isa_stmt = false; + state->with_stmt = false; + state->using_stmt = false; + state->last_subject = 0; + state->last_predicate = 0; + state->last_object = 0; - ptr = ecs_parse_whitespace(ptr); + ptr = parse_fluff(expr, ptr, state); - /* Inout specifiers always come first */ - if (ptr[0] == TOK_BRACKET_OPEN) { - ptr = parse_annotation(name, expr, (ptr - expr), ptr + 1, &term.inout); - if (!ptr) { - goto error; + char ch = ptr[0]; + + if (!ch) { + goto done; + } else if (ch == '{') { + ptr = parse_fluff(expr, ptr + 1, state); + goto scope_open; + } else if (ch == '}') { + ptr = parse_fluff(expr, ptr + 1, state); + goto scope_close; + } else if (ch == '(') { + if (ecs_get_scope(world) != 0) { + goto assign_to_scope_stmt; + } else { + goto term_expr; } - ptr = ecs_parse_whitespace(ptr); + } else if (!ecs_os_strncmp(ptr, TOK_USING " ", 5)) { + ptr = parse_using_stmt(name, expr, ptr, state); + if (!ptr) goto error; + goto term_expr; + } else if (!ecs_os_strncmp(ptr, TOK_WITH " ", 5)) { + ptr = parse_with_stmt(name, expr, ptr, state); + if (!ptr) goto error; + goto term_expr; + } else { + goto term_expr; } - if (valid_operator_char(ptr[0])) { - term.oper = parse_operator(ptr[0]); - ptr = ecs_parse_whitespace(ptr + 1); + ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); + + goto done; + +term_expr: + if (!ptr[0]) { + goto done; } - /* If next token is the start of an identifier, it could be either a type - * role, source or component identifier */ - if (valid_token_start_char(ptr[0])) { - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - goto error; - } + if (!(ptr = parse_plecs_term(world, name, ptr, ptr, state))) { + goto error; + } - /* Is token a type role? */ - if (ptr[0] == TOK_BITWISE_OR && ptr[1] != TOK_BITWISE_OR) { - ptr ++; - goto parse_role; - } + ptr = parse_fluff(expr, ptr, state); - /* Is token a predicate? */ - if (ptr[0] == TOK_PAREN_OPEN) { - goto parse_predicate; + if (ptr[0] == '{' && !isspace(ptr[-1])) { + /* A '{' directly after an identifier (no whitespace) is a literal */ + goto assign_expr; + } + + if (!state->using_stmt) { + if (ptr[0] == ':') { + ptr = parse_fluff(expr, ptr + 1, state); + goto inherit_stmt; + } else if (ptr[0] == '=') { + ptr = parse_fluff(expr, ptr + 1, state); + goto assign_stmt; + } else if (ptr[0] == ',') { + ptr = parse_fluff(expr, ptr + 1, state); + goto term_expr; + } else if (ptr[0] == '{') { + state->assign_stmt = false; + ptr = parse_fluff(expr, ptr + 1, state); + goto scope_open; } + } - /* Next token must be a predicate */ - goto parse_predicate; + state->assign_stmt = false; + goto done; - /* If next token is a singleton, assign identifier to pred and subject */ - } else if (ptr[0] == TOK_SINGLETON) { - ptr ++; - if (valid_token_start_char(ptr[0])) { - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - goto error; - } +inherit_stmt: + ptr = parse_inherit_stmt(name, expr, ptr, state); + if (!ptr) goto error; - goto parse_singleton; + /* Expect base identifier */ + goto term_expr; - } else { - ecs_parser_error(name, expr, (ptr - expr), - "expected identifier after singleton operator"); - goto error; +assign_to_scope_stmt: + state->last_subject = ecs_get_scope(world); + goto assign_stmt; + +assign_stmt: + ptr = parse_assign_stmt(world, name, expr, ptr, state); + if (!ptr) goto error; + + ptr = parse_fluff(expr, ptr, state); + + if (ptr[0] == '{') { + /* Assignment without a preceding component */ + ecs_entity_t type = state->default_scope_type[state->sp]; + if (!type) { + ecs_parser_error(name, expr, ptr - expr, + "missing type for assignment"); + return NULL; } - /* Pair with implicit subject */ - } else if (ptr[0] == TOK_PAREN_OPEN) { - goto parse_pair; + state->last_assign_id = type; + goto assign_expr; + } - /* Nothing else expected here */ + /* Expect component identifiers */ + goto term_expr; + +assign_expr: + ptr = parse_assign_expr(world, name, expr, ptr, state); + if (!ptr) goto error; + + ptr = parse_fluff(expr, ptr, state); + if (ptr[0] == ',') { + ptr ++; + goto term_expr; + } else if (ptr[0] == '{') { + state->assign_stmt = false; + ptr ++; + goto scope_open; } else { - ecs_parser_error(name, expr, (ptr - expr), - "unexpected character '%c'", ptr[0]); - goto error; + state->assign_stmt = false; + goto done; } -parse_role: - term.role = parse_role(name, expr, (ptr - expr), token); - if (!term.role) { - goto error; - } +scope_open: + ptr = parse_scope_open(world, name, expr, ptr, state); + if (!ptr) goto error; + goto done; - ptr = ecs_parse_whitespace(ptr); +scope_close: + ptr = parse_scope_close(world, name, expr, ptr, state); + if (!ptr) goto error; + goto done; - /* If next token is the source token, this is an empty source */ - if (valid_token_start_char(ptr[0])) { - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - goto error; - } +done: + return ptr; +error: + return NULL; +} - /* If not, it's a predicate */ - goto parse_predicate; +int ecs_plecs_from_str( + ecs_world_t *world, + const char *name, + const char *expr) +{ + const char *ptr = expr; + ecs_term_t term = {0}; + plecs_state_t state = {0}; - } else if (ptr[0] == TOK_PAREN_OPEN) { - goto parse_pair; - } else { - ecs_parser_error(name, expr, (ptr - expr), - "expected identifier after role"); - goto error; + if (!expr) { + return 0; } -parse_predicate: - if (parse_identifier(token, &term.pred)) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid identifier '%s'", token); - goto error; - } + state.scope[0] = ecs_get_scope(world); + ecs_entity_t prev_with = ecs_set_with(world, 0); - /* Set expression */ - if (ptr[0] == TOK_COLON) { - ptr = ecs_parse_whitespace(ptr + 1); - ptr = parse_set_expr(world, name, expr, (ptr - expr), ptr, NULL, - &term.pred, TOK_COLON); + do { + expr = ptr = parse_stmt(world, name, expr, ptr, &state); if (!ptr) { goto error; } - ptr = ecs_parse_whitespace(ptr); - - if (ptr[0] == TOK_AND || !ptr[0]) { - goto parse_done; - } - - if (ptr[0] != TOK_COLON) { - ecs_parser_error(name, expr, (ptr - expr), - "unexpected token '%c' after predicate set expression", ptr[0]); - goto error; + if (!ptr[0]) { + break; /* End of expression */ } + } while (true); - ptr = ecs_parse_whitespace(ptr + 1); - } else { - ptr = ecs_parse_whitespace(ptr); - } + ecs_set_scope(world, state.scope[0]); + ecs_set_with(world, prev_with); - if (ptr[0] == TOK_PAREN_OPEN) { - ptr ++; - if (ptr[0] == TOK_PAREN_CLOSE) { - term.subj.set.mask = EcsNothing; - ptr ++; - ptr = ecs_parse_whitespace(ptr); - } else { - ptr = parse_arguments( - world, name, expr, (ptr - expr), ptr, token, &term); - } - - goto parse_done; - } - - goto parse_done; + clear_comment(expr, ptr, &state); -parse_pair: - ptr = ecs_parse_identifier(name, expr, ptr + 1, token); - if (!ptr) { + if (state.sp != 0) { + ecs_parser_error(name, expr, 0, "missing end of scope"); goto error; } - if (ptr[0] == TOK_AND) { - ptr ++; - term.subj.entity = EcsThis; - goto parse_pair_predicate; - } else if (ptr[0] == TOK_PAREN_CLOSE) { - term.subj.entity = EcsThis; - goto parse_pair_predicate; - } else { - ecs_parser_error(name, expr, (ptr - expr), - "unexpected character '%c'", ptr[0]); + if (state.assign_stmt) { + ecs_parser_error(name, expr, 0, "unfinished assignment"); goto error; } -parse_pair_predicate: - if (parse_identifier(token, &term.pred)) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid identifier '%s'", token); + if (state.errors) { goto error; } - ptr = ecs_parse_whitespace(ptr); - if (valid_token_start_char(ptr[0])) { - ptr = ecs_parse_identifier(name, expr, ptr, token); - if (!ptr) { - goto error; - } + return 0; +error: + ecs_set_scope(world, state.scope[0]); + ecs_set_with(world, prev_with); + ecs_term_fini(&term); + return -1; +} - if (ptr[0] == TOK_PAREN_CLOSE) { - ptr ++; - goto parse_pair_object; - } else { - ecs_parser_error(name, expr, (ptr - expr), - "unexpected character '%c'", ptr[0]); - goto error; - } - } else if (ptr[0] == TOK_PAREN_CLOSE) { - /* No object */ - ptr ++; - goto parse_done; - } else { - ecs_parser_error(name, expr, (ptr - expr), - "expected pair object or ')'"); +int ecs_plecs_from_file( + ecs_world_t *world, + const char *filename) +{ + FILE* file; + char* content = NULL; + int32_t bytes; + size_t size; + + /* Open file for reading */ + ecs_os_fopen(&file, filename, "r"); + if (!file) { + ecs_err("%s (%s)", ecs_os_strerror(errno), filename); goto error; } -parse_pair_object: - if (parse_identifier(token, &term.obj)) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid identifier '%s'", token); + /* Determine file size */ + fseek(file, 0 , SEEK_END); + bytes = (int32_t)ftell(file); + if (bytes == -1) { goto error; } + rewind(file); - term.role = ECS_PAIR; - - ptr = ecs_parse_whitespace(ptr); - goto parse_done; - -parse_singleton: - if (parse_identifier(token, &term.pred)) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid identifier '%s'", token); - goto error; + /* Load contents in memory */ + content = ecs_os_malloc(bytes + 1); + size = (size_t)bytes; + if (!(size = fread(content, 1, size, file)) && bytes) { + ecs_err("%s: read zero bytes instead of %d", filename, size); + ecs_os_free(content); + content = NULL; + goto error; + } else { + content[size] = '\0'; } - parse_identifier(token, &term.subj); - goto parse_done; - -parse_done: - *term_out = term; + fclose(file); - return ptr; + int result = ecs_plecs_from_str(world, filename, content); + ecs_os_free(content); + return result; error: - ecs_term_fini(&term); - return NULL; + ecs_os_free(content); + return -1; } +#endif + +#ifdef FLECS_LOG + static -bool is_valid_end_of_term( - const char *ptr) +char *ecs_vasprintf( + const char *fmt, + va_list args) { - if ((ptr[0] == TOK_AND) || /* another term with And operator */ - (ptr[0] == TOK_OR[0]) || /* another term with Or operator */ - (ptr[0] == '\n') || /* newlines are valid */ - (ptr[0] == '\0') || /* end of string */ - (ptr[0] == '/') || /* comment (in plecs) */ - (ptr[0] == '{') || /* scope (in plecs) */ - (ptr[0] == '}') || - (ptr[0] == ':') || /* inheritance (in plecs) */ - (ptr[0] == '=')) /* assignment (in plecs) */ - { - return true; - } - return false; -} + ecs_size_t size = 0; + char *result = NULL; + va_list tmpa; -char* ecs_parse_term( - const ecs_world_t *world, - const char *name, - const char *expr, - const char *ptr, - ecs_term_t *term) -{ - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(term != NULL, ECS_INVALID_PARAMETER, NULL); + va_copy(tmpa, args); - ecs_term_id_t *subj = &term->subj; + size = vsnprintf(result, 0, fmt, tmpa); - bool prev_or = false; - if (ptr != expr) { - if (ptr[0]) { - if (ptr[0] == ',') { - ptr ++; - } else if (ptr[0] == '|') { - ptr += 2; - prev_or = true; - } else { - ecs_parser_error(name, expr, (ptr - expr), - "invalid preceding token"); - } - } - } - - ptr = ecs_parse_eol_and_whitespace(ptr); - if (!ptr[0]) { - *term = (ecs_term_t){0}; - return (char*)ptr; + va_end(tmpa); + + if ((int32_t)size < 0) { + return NULL; } - if (ptr == expr && !strcmp(expr, "0")) { - return (char*)&ptr[1]; + result = (char *) ecs_os_malloc(size + 1); + + if (!result) { + return NULL; } - int32_t prev_set = subj->set.mask; + ecs_os_vsprintf(result, fmt, args); - /* Parse next element */ - ptr = parse_term(world, name, ptr, term); - if (!ptr) { - goto error; - } + return result; +} - /* Post-parse consistency checks */ +static +void ecs_colorize_buf( + char *msg, + bool enable_colors, + ecs_strbuf_t *buf) +{ + char *ptr, ch, prev = '\0'; + bool isNum = false; + char isStr = '\0'; + bool isVar = false; + bool overrideColor = false; + bool autoColor = true; + bool dontAppend = false; - /* If next token is OR, term is part of an OR expression */ - if (!ecs_os_strncmp(ptr, TOK_OR, 2) || prev_or) { - /* An OR operator must always follow an AND or another OR */ - if (term->oper != EcsAnd) { - ecs_parser_error(name, expr, (ptr - expr), - "cannot combine || with other operators"); - goto error; - } + for (ptr = msg; (ch = *ptr); ptr++) { + dontAppend = false; - term->oper = EcsOr; - } + if (!overrideColor) { + if (isNum && !isdigit(ch) && !isalpha(ch) && (ch != '.') && (ch != '%')) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); + isNum = false; + } + if (isStr && (isStr == ch) && prev != '\\') { + isStr = '\0'; + } else if (((ch == '\'') || (ch == '"')) && !isStr && + !isalpha(prev) && (prev != '\\')) + { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_CYAN); + isStr = ch; + } - /* Term must either end in end of expression, AND or OR token */ - if (!is_valid_end_of_term(ptr)) { - ecs_parser_error(name, expr, (ptr - expr), - "expected end of expression or next term"); - goto error; - } + if ((isdigit(ch) || (ch == '%' && isdigit(prev)) || + (ch == '-' && isdigit(ptr[1]))) && !isNum && !isStr && !isVar && + !isalpha(prev) && !isdigit(prev) && (prev != '_') && + (prev != '.')) + { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_GREEN); + isNum = true; + } - /* If the term just contained a 0, the expression has nothing. Ensure - * that after the 0 nothing else follows */ - if (!ecs_os_strcmp(term->pred.name, "0")) { - if (ptr[0]) { - ecs_parser_error(name, expr, (ptr - expr), - "unexpected term after 0"); - goto error; - } + if (isVar && !isalpha(ch) && !isdigit(ch) && ch != '_') { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); + isVar = false; + } - if (subj->set.mask != EcsDefaultSet || - (subj->entity && subj->entity != EcsThis) || - (subj->name && ecs_os_strcmp(subj->name, "This"))) - { - ecs_parser_error(name, expr, (ptr - expr), - "invalid combination of 0 with non-default subject"); - goto error; + if (!isStr && !isVar && ch == '$' && isalpha(ptr[1])) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_CYAN); + isVar = true; + } } - subj->set.mask = EcsNothing; - ecs_os_free(term->pred.name); - term->pred.name = NULL; - } + if (!isVar && !isStr && !isNum && ch == '#' && ptr[1] == '[') { + bool isColor = true; + overrideColor = true; - /* Cannot combine EcsNothing with operators other than AND */ - if (term->oper != EcsAnd && subj->set.mask == EcsNothing) { - ecs_parser_error(name, expr, (ptr - expr), - "invalid operator for empty source"); - goto error; - } + /* Custom colors */ + if (!ecs_os_strncmp(&ptr[2], "]", ecs_os_strlen("]"))) { + autoColor = false; + } else if (!ecs_os_strncmp(&ptr[2], "green]", ecs_os_strlen("green]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_GREEN); + } else if (!ecs_os_strncmp(&ptr[2], "red]", ecs_os_strlen("red]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_RED); + } else if (!ecs_os_strncmp(&ptr[2], "blue]", ecs_os_strlen("red]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_BLUE); + } else if (!ecs_os_strncmp(&ptr[2], "magenta]", ecs_os_strlen("magenta]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_MAGENTA); + } else if (!ecs_os_strncmp(&ptr[2], "cyan]", ecs_os_strlen("cyan]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_CYAN); + } else if (!ecs_os_strncmp(&ptr[2], "yellow]", ecs_os_strlen("yellow]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_YELLOW); + } else if (!ecs_os_strncmp(&ptr[2], "grey]", ecs_os_strlen("grey]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_GREY); + } else if (!ecs_os_strncmp(&ptr[2], "white]", ecs_os_strlen("white]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); + } else if (!ecs_os_strncmp(&ptr[2], "bold]", ecs_os_strlen("bold]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_BOLD); + } else if (!ecs_os_strncmp(&ptr[2], "normal]", ecs_os_strlen("normal]"))) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); + } else if (!ecs_os_strncmp(&ptr[2], "reset]", ecs_os_strlen("reset]"))) { + overrideColor = false; + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); + } else { + isColor = false; + overrideColor = false; + } - /* Verify consistency of OR expression */ - if (prev_or && term->oper == EcsOr) { - /* Set expressions must be the same for all OR terms */ - if (subj->set.mask != prev_set) { - ecs_parser_error(name, expr, (ptr - expr), - "cannot combine different sources in OR expression"); - goto error; + if (isColor) { + ptr += 2; + while ((ch = *ptr) != ']') ptr ++; + dontAppend = true; + } + if (!autoColor) { + overrideColor = true; + } } - term->oper = EcsOr; - } + if (ch == '\n') { + if (isNum || isStr || isVar || overrideColor) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); + overrideColor = false; + isNum = false; + isStr = false; + isVar = false; + } + } - /* Automatically assign This if entity is not assigned and the set is - * nothing */ - if (subj->set.mask != EcsNothing) { - if (!subj->name) { - if (!subj->entity) { - subj->entity = EcsThis; + if (!dontAppend) { + ecs_strbuf_appendstrn(buf, ptr, 1); + } + + if (!overrideColor) { + if (((ch == '\'') || (ch == '"')) && !isStr) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); } } + + prev = ch; } - if (subj->name && !ecs_os_strcmp(subj->name, "0")) { - subj->entity = 0; - subj->set.mask = EcsNothing; + if (isNum || isStr || isVar || overrideColor) { + if (enable_colors) ecs_strbuf_appendstr(buf, ECS_NORMAL); } +} - /* Process role */ - if (term->role == ECS_AND) { - term->oper = EcsAndFrom; - term->role = 0; - } else if (term->role == ECS_OR) { - term->oper = EcsOrFrom; - term->role = 0; - } else if (term->role == ECS_NOT) { - term->oper = EcsNotFrom; - term->role = 0; +static +void log_print( + int level, + const char *file, + int32_t line, + const char *fmt, + va_list args) +{ + (void)level; + (void)line; + + ecs_strbuf_t msg_buf = ECS_STRBUF_INIT; + + if (level > ecs_os_api.log_level_) { + return; } - ptr = ecs_parse_whitespace(ptr); + /* Apply color. Even if we don't want color, we still need to call the + * colorize function to get rid of the color tags (e.g. #[green]) */ + char *msg_nocolor = ecs_vasprintf(fmt, args); + ecs_colorize_buf(msg_nocolor, ecs_os_api.log_with_color_, &msg_buf); + ecs_os_free(msg_nocolor); + + char *msg = ecs_strbuf_get(&msg_buf); + ecs_os_api.log_(level, file, line, msg); + ecs_os_free(msg); +} - return (char*)ptr; -error: - ecs_term_fini(term); - return NULL; +void _ecs_log( + int level, + const char *file, + int32_t line, + const char *fmt, + ...) +{ + va_list args; + va_start(args, fmt); + log_print(level, file, line, fmt, args); + va_end(args); } -#endif +void ecs_log_push(void) { + ecs_os_api.log_indent_ ++; +} -#ifdef FLECS_META_C +void ecs_log_pop(void) { + ecs_os_api.log_indent_ --; +} -#define ECS_META_IDENTIFIER_LENGTH (256) +void ecs_log_set_level( + int level) +{ + ecs_os_api.log_level_ = level; +} -#define ecs_meta_error(ctx, ptr, ...)\ - ecs_parser_error((ctx)->name, (ctx)->desc, ptr - (ctx)->desc, __VA_ARGS__); +void ecs_log_enable_colors( + bool enabled) +{ + ecs_os_api.log_with_color_ = enabled; +} -typedef char ecs_meta_token_t[ECS_META_IDENTIFIER_LENGTH]; +void _ecs_parser_errorv( + const char *name, + const char *expr, + int64_t column_arg, + const char *fmt, + va_list args) +{ + int32_t column = flecs_to_i32(column_arg); -typedef struct meta_parse_ctx_t { - const char *name; - const char *desc; -} meta_parse_ctx_t; + if (ecs_os_api.log_level_ >= -2) { + ecs_strbuf_t msg_buf = ECS_STRBUF_INIT; -typedef struct meta_type_t { - ecs_meta_token_t type; - ecs_meta_token_t params; - bool is_const; - bool is_ptr; -} meta_type_t; + ecs_strbuf_vappend(&msg_buf, fmt, args); -typedef struct meta_member_t { - meta_type_t type; - ecs_meta_token_t name; - int64_t count; - bool is_partial; -} meta_member_t; + if (expr) { + ecs_strbuf_appendstr(&msg_buf, "\n"); -typedef struct meta_constant_t { - ecs_meta_token_t name; - int64_t value; - bool is_value_set; -} meta_constant_t; + /* Find start of line by taking column and looking for the + * last occurring newline */ + if (column != -1) { + const char *ptr = &expr[column]; + while (ptr[0] != '\n' && ptr > expr) { + ptr --; + } -typedef struct meta_params_t { - meta_type_t key_type; - meta_type_t type; - int64_t count; - bool is_key_value; - bool is_fixed_size; -} meta_params_t; + if (ptr == expr) { + /* ptr is already at start of line */ + } else { + column -= (int32_t)(ptr - expr + 1); + expr = ptr + 1; + } + } -static -const char* skip_scope(const char *ptr, meta_parse_ctx_t *ctx) { - /* Keep track of which characters were used to open the scope */ - char stack[256]; - int32_t sp = 0; - char ch; + /* Strip newlines from current statement, if any */ + char *newline_ptr = strchr(expr, '\n'); + if (newline_ptr) { + /* Strip newline from expr */ + ecs_strbuf_appendstrn(&msg_buf, expr, + (int32_t)(newline_ptr - expr)); + } else { + ecs_strbuf_appendstr(&msg_buf, expr); + } - while ((ch = *ptr)) { - if (ch == '(' || ch == '<') { - stack[sp] = ch; + ecs_strbuf_appendstr(&msg_buf, "\n"); - sp ++; - if (sp >= 256) { - ecs_meta_error(ctx, ptr, "maximum level of nesting reached"); - goto error; - } - } else if (ch == ')' || ch == '>') { - sp --; - if ((sp < 0) || (ch == '>' && stack[sp] != '<') || - (ch == ')' && stack[sp] != '(')) - { - ecs_meta_error(ctx, ptr, "mismatching %c in identifier", ch); - goto error; + if (column != -1) { + ecs_strbuf_append(&msg_buf, "%*s^", column, ""); } } - ptr ++; - - if (!sp) { - break; - } + char *msg = ecs_strbuf_get(&msg_buf); + ecs_os_err(name, 0, msg); + ecs_os_free(msg); } - - return ptr; -error: - return NULL; } -static -const char* parse_c_digit( - const char *ptr, - int64_t *value_out) +void _ecs_parser_error( + const char *name, + const char *expr, + int64_t column, + const char *fmt, + ...) { - char token[24]; - ptr = ecs_parse_eol_and_whitespace(ptr); - ptr = ecs_parse_digit(ptr, token); - if (!ptr) { - goto error; + if (ecs_os_api.log_level_ >= -2) { + va_list args; + va_start(args, fmt); + _ecs_parser_errorv(name, expr, column, fmt, args); + va_end(args); } - - *value_out = strtol(token, NULL, 0); - - return ecs_parse_eol_and_whitespace(ptr); -error: - return NULL; } -static -const char* parse_c_identifier( - const char *ptr, - char *buff, - char *params, - meta_parse_ctx_t *ctx) +void _ecs_abort( + int32_t err, + const char *file, + int32_t line, + const char *fmt, + ...) { - ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(buff != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ctx != NULL, ECS_INTERNAL_ERROR, NULL); - - char *bptr = buff, ch; - - if (params) { - params[0] = '\0'; - } - - /* Ignore whitespaces */ - ptr = ecs_parse_eol_and_whitespace(ptr); - - if (!isalpha(*ptr)) { - ecs_meta_error(ctx, ptr, - "invalid identifier (starts with '%c')", *ptr); - goto error; + if (fmt) { + va_list args; + va_start(args, fmt); + char *msg = ecs_vasprintf(fmt, args); + va_end(args); + _ecs_fatal(file, line, "%s (%s)", msg, ecs_strerror(err)); + ecs_os_free(msg); + } else { + _ecs_fatal(file, line, "%s", ecs_strerror(err)); } - while ((ch = *ptr) && !isspace(ch) && ch != ';' && ch != ',' && ch != ')' && ch != '>') { - /* Type definitions can contain macro's or templates */ - if (ch == '(' || ch == '<') { - if (!params) { - ecs_meta_error(ctx, ptr, "unexpected %c", *ptr); - goto error; - } - - const char *end = skip_scope(ptr, ctx); - ecs_os_strncpy(params, ptr, (ecs_size_t)(end - ptr)); - params[end - ptr] = '\0'; + ecs_os_abort(); +} - ptr = end; +void _ecs_assert( + bool condition, + int32_t err, + const char *cond_str, + const char *file, + int32_t line, + const char *fmt, + ...) +{ + if (!condition) { + if (fmt) { + va_list args; + va_start(args, fmt); + char *msg = ecs_vasprintf(fmt, args); + va_end(args); + _ecs_fatal(file, line, "assert(%s) %s (%s)", + cond_str, msg, ecs_strerror(err)); + ecs_os_free(msg); } else { - *bptr = ch; - bptr ++; - ptr ++; + _ecs_fatal(file, line, "assert(%s) %s", + cond_str, ecs_strerror(err)); } - } - - *bptr = '\0'; - if (!ch) { - ecs_meta_error(ctx, ptr, "unexpected end of token"); - goto error; + ecs_os_abort(); } - - return ptr; -error: - return NULL; } -static -const char * meta_open_scope( - const char *ptr, - meta_parse_ctx_t *ctx) +void _ecs_deprecated( + const char *file, + int32_t line, + const char *msg) { - /* Skip initial whitespaces */ - ptr = ecs_parse_eol_and_whitespace(ptr); - - /* Is this the start of the type definition? */ - if (ctx->desc == ptr) { - if (*ptr != '{') { - ecs_meta_error(ctx, ptr, "missing '{' in struct definition"); - goto error; - } - - ptr ++; - ptr = ecs_parse_eol_and_whitespace(ptr); - } - - /* Is this the end of the type definition? */ - if (!*ptr) { - ecs_meta_error(ctx, ptr, "missing '}' at end of struct definition"); - goto error; - } - - /* Is this the end of the type definition? */ - if (*ptr == '}') { - ptr = ecs_parse_eol_and_whitespace(ptr + 1); - if (*ptr) { - ecs_meta_error(ctx, ptr, - "stray characters after struct definition"); - goto error; - } - return NULL; - } - - return ptr; -error: - return NULL; + _ecs_err(file, line, "%s", msg); } -static -const char* meta_parse_constant( - const char *ptr, - meta_constant_t *token, - meta_parse_ctx_t *ctx) -{ - ptr = meta_open_scope(ptr, ctx); - if (!ptr) { - return NULL; +#define ECS_ERR_STR(code) case code: return &(#code[4]) + +const char* ecs_strerror( + int32_t error_code) +{ + switch (error_code) { + ECS_ERR_STR(ECS_INVALID_PARAMETER); + ECS_ERR_STR(ECS_NOT_A_COMPONENT); + ECS_ERR_STR(ECS_TYPE_NOT_AN_ENTITY); + ECS_ERR_STR(ECS_INTERNAL_ERROR); + ECS_ERR_STR(ECS_ALREADY_DEFINED); + ECS_ERR_STR(ECS_INVALID_COMPONENT_SIZE); + ECS_ERR_STR(ECS_INVALID_COMPONENT_ALIGNMENT); + ECS_ERR_STR(ECS_OUT_OF_MEMORY); + ECS_ERR_STR(ECS_MODULE_UNDEFINED); + ECS_ERR_STR(ECS_COLUMN_INDEX_OUT_OF_RANGE); + ECS_ERR_STR(ECS_COLUMN_IS_NOT_SHARED); + ECS_ERR_STR(ECS_COLUMN_IS_SHARED); + ECS_ERR_STR(ECS_COLUMN_HAS_NO_DATA); + ECS_ERR_STR(ECS_COLUMN_TYPE_MISMATCH); + ECS_ERR_STR(ECS_INVALID_WHILE_ITERATING); + ECS_ERR_STR(ECS_INVALID_FROM_WORKER); + ECS_ERR_STR(ECS_OUT_OF_RANGE); + ECS_ERR_STR(ECS_THREAD_ERROR); + ECS_ERR_STR(ECS_MISSING_OS_API); + ECS_ERR_STR(ECS_UNSUPPORTED); + ECS_ERR_STR(ECS_NO_OUT_COLUMNS); + ECS_ERR_STR(ECS_COLUMN_ACCESS_VIOLATION); + ECS_ERR_STR(ECS_DESERIALIZE_FORMAT_ERROR); + ECS_ERR_STR(ECS_TYPE_CONSTRAINT_VIOLATION); + ECS_ERR_STR(ECS_COMPONENT_NOT_REGISTERED); + ECS_ERR_STR(ECS_INCONSISTENT_COMPONENT_ID); + ECS_ERR_STR(ECS_TYPE_INVALID_CASE); + ECS_ERR_STR(ECS_INCONSISTENT_NAME); + ECS_ERR_STR(ECS_INCONSISTENT_COMPONENT_ACTION); + ECS_ERR_STR(ECS_INVALID_OPERATION); + ECS_ERR_STR(ECS_INVALID_DELETE); + ECS_ERR_STR(ECS_CYCLE_DETECTED); + ECS_ERR_STR(ECS_LOCKED_STORAGE); } - token->is_value_set = false; + return "unknown error code"; +} - /* Parse token, constant identifier */ - ptr = parse_c_identifier(ptr, token->name, NULL, ctx); - ptr = ecs_parse_eol_and_whitespace(ptr); +#else - /* Explicit value assignment */ - if (*ptr == '=') { - int64_t value = 0; - ptr = parse_c_digit(ptr + 1, &value); - token->value = value; - token->is_value_set = true; - } +/* Empty bodies for when logging is disabled */ - /* Expect a ',' or '}' */ - if (*ptr != ',' && *ptr != '}') { - ecs_meta_error(ctx, ptr, "missing , after enum constant"); - goto error; - } +FLECS_API +void _ecs_log( + int32_t level, + const char *file, + int32_t line, + const char *fmt, + ...) +{ + (void)level; + (void)file; + (void)line; + (void)fmt; +} - if (*ptr == ',') { - return ptr + 1; - } else { - return ptr; - } -error: - return NULL; +FLECS_API +void _ecs_parser_error( + const char *name, + const char *expr, + int64_t column, + const char *fmt, + ...) +{ + (void)name; + (void)expr; + (void)column; + (void)fmt; } -static -const char* meta_parse_type( - const char *ptr, - meta_type_t *token, - meta_parse_ctx_t *ctx) +FLECS_API +void _ecs_parser_errorv( + const char *name, + const char *expr, + int64_t column, + const char *fmt, + va_list args) { - token->is_ptr = false; - token->is_const = false; + (void)name; + (void)expr; + (void)column; + (void)fmt; + (void)args; +} - ptr = ecs_parse_eol_and_whitespace(ptr); +FLECS_API +void _ecs_abort( + int32_t error_code, + const char *file, + int32_t line, + const char *fmt, + ...) +{ + (void)error_code; + (void)file; + (void)line; + (void)fmt; +} - /* Parse token, expect type identifier or ECS_PROPERTY */ - ptr = parse_c_identifier(ptr, token->type, token->params, ctx); - if (!ptr) { - goto error; - } +FLECS_API +void _ecs_assert( + bool condition, + int32_t error_code, + const char *condition_str, + const char *file, + int32_t line, + const char *fmt, + ...) +{ + (void)condition; + (void)error_code; + (void)condition_str; + (void)file; + (void)line; + (void)fmt; +} - if (!strcmp(token->type, "ECS_PRIVATE")) { - /* Members from this point are not stored in metadata */ - ptr += ecs_os_strlen(ptr); - goto done; - } +#endif - /* If token is const, set const flag and continue parsing type */ - if (!strcmp(token->type, "const")) { - token->is_const = true; +#ifdef FLECS_TIMER - /* Parse type after const */ - ptr = parse_c_identifier(ptr + 1, token->type, token->params, ctx); - } - /* Check if type is a pointer */ - ptr = ecs_parse_eol_and_whitespace(ptr); - if (*ptr == '*') { - token->is_ptr = true; - ptr ++; +static +void AddTickSource(ecs_iter_t *it) { + int32_t i; + for (i = 0; i < it->count; i ++) { + ecs_set(it->world, it->entities[i], EcsTickSource, {0}); } - -done: - return ptr; -error: - return NULL; } static -const char* meta_parse_member( - const char *ptr, - meta_member_t *token, - meta_parse_ctx_t *ctx) -{ - ptr = meta_open_scope(ptr, ctx); - if (!ptr) { - return NULL; - } - - token->count = 1; - token->is_partial = false; - - /* Parse member type */ - ptr = meta_parse_type(ptr, &token->type, ctx); - if (!ptr) { - token->is_partial = true; - goto error; - } +void ProgressTimers(ecs_iter_t *it) { + EcsTimer *timer = ecs_term(it, EcsTimer, 1); + EcsTickSource *tick_source = ecs_term(it, EcsTickSource, 2); - /* Next token is the identifier */ - ptr = parse_c_identifier(ptr, token->name, NULL, ctx); - if (!ptr) { - goto error; - } + ecs_assert(timer != NULL, ECS_INTERNAL_ERROR, NULL); - /* Skip whitespace between member and [ or ; */ - ptr = ecs_parse_eol_and_whitespace(ptr); + int i; + for (i = 0; i < it->count; i ++) { + tick_source[i].tick = false; - /* Check if this is an array */ - char *array_start = strchr(token->name, '['); - if (!array_start) { - /* If the [ was separated by a space, it will not be parsed as part of - * the name */ - if (*ptr == '[') { - array_start = (char*)ptr; /* safe, will not be modified */ + if (!timer[i].active) { + continue; } - } - - if (array_start) { - /* Check if the [ matches with a ] */ - char *array_end = strchr(array_start, ']'); - if (!array_end) { - ecs_meta_error(ctx, ptr, "missing ']'"); - goto error; - } else if (array_end - array_start == 0) { - ecs_meta_error(ctx, ptr, "dynamic size arrays are not supported"); - goto error; - } + const ecs_world_info_t *info = ecs_get_world_info(it->world); + FLECS_FLOAT time_elapsed = timer[i].time + info->delta_time_raw; + FLECS_FLOAT timeout = timer[i].timeout; + + if (time_elapsed >= timeout) { + FLECS_FLOAT t = time_elapsed - timeout; + if (t > timeout) { + t = 0; + } - token->count = atoi(array_start + 1); + timer[i].time = t; /* Initialize with remainder */ + tick_source[i].tick = true; + tick_source[i].time_elapsed = time_elapsed; - if (array_start == ptr) { - /* If [ was found after name, continue parsing after ] */ - ptr = array_end + 1; + if (timer[i].single_shot) { + timer[i].active = false; + } } else { - /* If [ was fonud in name, replace it with 0 terminator */ - array_start[0] = '\0'; - } - } - - /* Expect a ; */ - if (*ptr != ';') { - ecs_meta_error(ctx, ptr, "missing ; after member declaration"); - goto error; + timer[i].time = time_elapsed; + } } - - return ptr + 1; -error: - return NULL; } static -int meta_parse_desc( - const char *ptr, - meta_params_t *token, - meta_parse_ctx_t *ctx) -{ - token->is_key_value = false; - token->is_fixed_size = false; - - ptr = ecs_parse_eol_and_whitespace(ptr); - if (*ptr != '(' && *ptr != '<') { - ecs_meta_error(ctx, ptr, - "expected '(' at start of collection definition"); - goto error; - } - - ptr ++; +void ProgressRateFilters(ecs_iter_t *it) { + EcsRateFilter *filter = ecs_term(it, EcsRateFilter, 1); + EcsTickSource *tick_dst = ecs_term(it, EcsTickSource, 2); - /* Parse type identifier */ - ptr = meta_parse_type(ptr, &token->type, ctx); - if (!ptr) { - goto error; - } + int i; + for (i = 0; i < it->count; i ++) { + ecs_entity_t src = filter[i].src; + bool inc = false; - ptr = ecs_parse_eol_and_whitespace(ptr); + filter[i].time_elapsed += it->delta_time; - /* If next token is a ',' the first type was a key type */ - if (*ptr == ',') { - ptr = ecs_parse_eol_and_whitespace(ptr + 1); - - if (isdigit(*ptr)) { - int64_t value; - ptr = parse_c_digit(ptr, &value); - if (!ptr) { - goto error; + if (src) { + const EcsTickSource *tick_src = ecs_get(it->world, src, EcsTickSource); + if (tick_src) { + inc = tick_src->tick; + } else { + inc = true; } - - token->count = value; - token->is_fixed_size = true; } else { - token->key_type = token->type; + inc = true; + } - /* Parse element type */ - ptr = meta_parse_type(ptr, &token->type, ctx); - ptr = ecs_parse_eol_and_whitespace(ptr); + if (inc) { + filter[i].tick_count ++; + bool triggered = !(filter[i].tick_count % filter[i].rate); + tick_dst[i].tick = triggered; + tick_dst[i].time_elapsed = filter[i].time_elapsed; - token->is_key_value = true; + if (triggered) { + filter[i].time_elapsed = 0; + } + } else { + tick_dst[i].tick = false; } } - - if (*ptr != ')' && *ptr != '>') { - ecs_meta_error(ctx, ptr, - "expected ')' at end of collection definition"); - goto error; - } - - return 0; -error: - return -1; } static -ecs_entity_t meta_lookup( - ecs_world_t *world, - meta_type_t *token, - const char *ptr, - int64_t count, - meta_parse_ctx_t *ctx); +void ProgressTickSource(ecs_iter_t *it) { + EcsTickSource *tick_src = ecs_term(it, EcsTickSource, 1); -static -ecs_entity_t meta_lookup_array( + /* If tick source has no filters, tick unconditionally */ + int i; + for (i = 0; i < it->count; i ++) { + tick_src[i].tick = true; + tick_src[i].time_elapsed = it->delta_time; + } +} + +ecs_entity_t ecs_set_timeout( ecs_world_t *world, - ecs_entity_t e, - const char *params_decl, - meta_parse_ctx_t *ctx) + ecs_entity_t timer, + FLECS_FLOAT timeout) { - meta_parse_ctx_t param_ctx = { - .name = ctx->name, - .desc = params_decl - }; - - meta_params_t params; - if (meta_parse_desc(params_decl, ¶ms, ¶m_ctx)) { - goto error; - } - if (!params.is_fixed_size) { - ecs_meta_error(ctx, params_decl, "missing size for array"); - goto error; - } + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - if (!params.count) { - ecs_meta_error(ctx, params_decl, "invalid array size"); - goto error; - } + timer = ecs_set(world, timer, EcsTimer, { + .timeout = timeout, + .single_shot = true, + .active = true + }); - ecs_entity_t element_type = ecs_lookup_symbol(world, params.type.type, true); - if (!element_type) { - ecs_meta_error(ctx, params_decl, "unknown element type '%s'", - params.type.type); + EcsSystem *system_data = ecs_get_mut(world, timer, EcsSystem, NULL); + if (system_data) { + system_data->tick_source = timer; } - if (!e) { - e = ecs_set(world, 0, EcsMetaType, { EcsArrayType }); - } + return timer; +} - ecs_assert(params.count <= INT32_MAX, ECS_INVALID_PARAMETER, NULL); +FLECS_FLOAT ecs_get_timeout( + const ecs_world_t *world, + ecs_entity_t timer) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(timer != 0, ECS_INVALID_PARAMETER, NULL); - return ecs_set(world, e, EcsArray, { element_type, (int32_t)params.count }); -error: - return 0; + const EcsTimer *value = ecs_get(world, timer, EcsTimer); + if (value) { + return value->timeout; + } else { + return 0; + } } -static -ecs_entity_t meta_lookup_vector( +ecs_entity_t ecs_set_interval( ecs_world_t *world, - ecs_entity_t e, - const char *params_decl, - meta_parse_ctx_t *ctx) + ecs_entity_t timer, + FLECS_FLOAT interval) { - meta_parse_ctx_t param_ctx = { - .name = ctx->name, - .desc = params_decl - }; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - meta_params_t params; - if (meta_parse_desc(params_decl, ¶ms, ¶m_ctx)) { - goto error; - } + timer = ecs_set(world, timer, EcsTimer, { + .timeout = interval, + .active = true + }); - if (params.is_key_value) { - ecs_meta_error(ctx, params_decl, - "unexpected key value parameters for vector"); - goto error; - } + EcsSystem *system_data = ecs_get_mut(world, timer, EcsSystem, NULL); + if (system_data) { + system_data->tick_source = timer; + } - ecs_entity_t element_type = meta_lookup( - world, ¶ms.type, params_decl, 1, ¶m_ctx); + return timer; +} - if (!e) { - e = ecs_set(world, 0, EcsMetaType, {EcsVectorType}); +FLECS_FLOAT ecs_get_interval( + const ecs_world_t *world, + ecs_entity_t timer) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + + if (!timer) { + return 0; } - return ecs_set(world, e, EcsVector, { element_type }); -error: - return 0; + const EcsTimer *value = ecs_get(world, timer, EcsTimer); + if (value) { + return value->timeout; + } else { + return 0; + } } -static -ecs_entity_t meta_lookup_bitmask( +void ecs_start_timer( ecs_world_t *world, - ecs_entity_t e, - const char *params_decl, - meta_parse_ctx_t *ctx) + ecs_entity_t timer) { - (void)e; - - meta_parse_ctx_t param_ctx = { - .name = ctx->name, - .desc = params_decl - }; + EcsTimer *ptr = ecs_get_mut(world, timer, EcsTimer, NULL); + ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); - meta_params_t params; - if (meta_parse_desc(params_decl, ¶ms, ¶m_ctx)) { - goto error; - } + ptr->active = true; + ptr->time = 0; +} - if (params.is_key_value) { - ecs_meta_error(ctx, params_decl, - "unexpected key value parameters for bitmask"); - goto error; - } +void ecs_stop_timer( + ecs_world_t *world, + ecs_entity_t timer) +{ + EcsTimer *ptr = ecs_get_mut(world, timer, EcsTimer, NULL); + ecs_assert(ptr != NULL, ECS_INVALID_PARAMETER, NULL); - if (params.is_fixed_size) { - ecs_meta_error(ctx, params_decl, - "unexpected size for bitmask"); - goto error; - } + ptr->active = false; +} - ecs_entity_t bitmask_type = meta_lookup( - world, ¶ms.type, params_decl, 1, ¶m_ctx); - ecs_assert(bitmask_type != 0, ECS_INVALID_PARAMETER, NULL); +ecs_entity_t ecs_set_rate( + ecs_world_t *world, + ecs_entity_t filter, + int32_t rate, + ecs_entity_t source) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); -#ifndef NDEBUG - /* Make sure this is a bitmask type */ - const EcsMetaType *type_ptr = ecs_get(world, bitmask_type, EcsMetaType); - ecs_assert(type_ptr != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(type_ptr->kind == EcsBitmaskType, ECS_INVALID_PARAMETER, NULL); -#endif + filter = ecs_set(world, filter, EcsRateFilter, { + .rate = rate, + .src = source + }); - return bitmask_type; -error: - return 0; + EcsSystem *system_data = ecs_get_mut(world, filter, EcsSystem, NULL); + if (system_data) { + system_data->tick_source = filter; + } + + return filter; } -static -ecs_entity_t meta_lookup( +void ecs_set_tick_source( ecs_world_t *world, - meta_type_t *token, - const char *ptr, - int64_t count, - meta_parse_ctx_t *ctx) + ecs_entity_t system, + ecs_entity_t tick_source) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(token != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ctx != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(system != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(tick_source != 0, ECS_INVALID_PARAMETER, NULL); - const char *typename = token->type; - ecs_entity_t type = 0; + EcsSystem *system_data = ecs_get_mut(world, system, EcsSystem, NULL); + ecs_assert(system_data != NULL, ECS_INVALID_PARAMETER, NULL); - /* Parse vector type */ - if (!token->is_ptr) { - if (!ecs_os_strcmp(typename, "ecs_array")) { - type = meta_lookup_array(world, 0, token->params, ctx); + system_data->tick_source = tick_source; +} - } else if (!ecs_os_strcmp(typename, "ecs_vector") || - !ecs_os_strcmp(typename, "flecs::vector")) - { - type = meta_lookup_vector(world, 0, token->params, ctx); +void FlecsTimerImport( + ecs_world_t *world) +{ + ECS_MODULE(world, FlecsTimer); - } else if (!ecs_os_strcmp(typename, "flecs::bitmask")) { - type = meta_lookup_bitmask(world, 0, token->params, ctx); + ECS_IMPORT(world, FlecsPipeline); - } else if (!ecs_os_strcmp(typename, "flecs::byte")) { - type = ecs_id(ecs_byte_t); + ecs_set_name_prefix(world, "Ecs"); - } else if (!ecs_os_strcmp(typename, "char")) { - type = ecs_id(ecs_char_t); + flecs_bootstrap_component(world, EcsTimer); + flecs_bootstrap_component(world, EcsRateFilter); - } else if (!ecs_os_strcmp(typename, "bool") || - !ecs_os_strcmp(typename, "_Bool")) - { - type = ecs_id(ecs_bool_t); + /* Add EcsTickSource to timers and rate filters */ + ECS_SYSTEM(world, AddTickSource, EcsPreFrame, [in] Timer || RateFilter, [out] !flecs.system.TickSource); - } else if (!ecs_os_strcmp(typename, "int8_t")) { - type = ecs_id(ecs_i8_t); - } else if (!ecs_os_strcmp(typename, "int16_t")) { - type = ecs_id(ecs_i16_t); - } else if (!ecs_os_strcmp(typename, "int32_t")) { - type = ecs_id(ecs_i32_t); - } else if (!ecs_os_strcmp(typename, "int64_t")) { - type = ecs_id(ecs_i64_t); + /* Timer handling */ + ECS_SYSTEM(world, ProgressTimers, EcsPreFrame, Timer, flecs.system.TickSource); - } else if (!ecs_os_strcmp(typename, "uint8_t")) { - type = ecs_id(ecs_u8_t); - } else if (!ecs_os_strcmp(typename, "uint16_t")) { - type = ecs_id(ecs_u16_t); - } else if (!ecs_os_strcmp(typename, "uint32_t")) { - type = ecs_id(ecs_u32_t); - } else if (!ecs_os_strcmp(typename, "uint64_t")) { - type = ecs_id(ecs_u64_t); + /* Rate filter handling */ + ECS_SYSTEM(world, ProgressRateFilters, EcsPreFrame, [in] RateFilter, [out] flecs.system.TickSource); - } else if (!ecs_os_strcmp(typename, "float")) { - type = ecs_id(ecs_f32_t); - } else if (!ecs_os_strcmp(typename, "double")) { - type = ecs_id(ecs_f64_t); + /* TickSource without a timer or rate filter just increases each frame */ + ECS_SYSTEM(world, ProgressTickSource, EcsPreFrame, [out] flecs.system.TickSource, !RateFilter, !Timer); +} - } else if (!ecs_os_strcmp(typename, "ecs_entity_t")) { - type = ecs_id(ecs_entity_t); +#endif - } else if (!ecs_os_strcmp(typename, "char*")) { - type = ecs_id(ecs_string_t); - } else { - type = ecs_lookup_symbol(world, typename, true); - } - } else { - if (!ecs_os_strcmp(typename, "char")) { - typename = "flecs.meta.string"; - } else - if (token->is_ptr) { - typename = "flecs.meta.uptr"; - } else - if (!ecs_os_strcmp(typename, "char*") || - !ecs_os_strcmp(typename, "flecs::string")) - { - typename = "flecs.meta.string"; - } - type = ecs_lookup_symbol(world, typename, true); - } +#ifdef FLECS_DOC - if (count != 1) { - ecs_assert(count <= INT32_MAX, ECS_INVALID_PARAMETER, NULL); +static ECS_COPY(EcsDocDescription, dst, src, { + ecs_os_strset((char**)&dst->value, src->value); - type = ecs_set(world, ecs_set(world, 0, - EcsMetaType, {EcsArrayType}), - EcsArray, {type, (int32_t)count}); - } +}) - if (!type) { - ecs_meta_error(ctx, ptr, "unknown type '%s'", typename); - goto error; - } +static ECS_MOVE(EcsDocDescription, dst, src, { + ecs_os_free((char*)dst->value); + dst->value = src->value; + src->value = NULL; +}) - return type; -error: - return 0; -} +static ECS_DTOR(EcsDocDescription, ptr, { + ecs_os_free((char*)ptr->value); +}) -static -int meta_parse_struct( +void ecs_doc_set_brief( ecs_world_t *world, - ecs_entity_t t, - const char *desc) + ecs_entity_t entity, + const char *description) { - const char *ptr = desc; - const char *name = ecs_get_name(world, t); + ecs_set_pair(world, entity, EcsDocDescription, EcsDocBrief, { + .value = description + }); +} - meta_member_t token; - meta_parse_ctx_t ctx = { - .name = name, - .desc = ptr - }; +void ecs_doc_set_detail( + ecs_world_t *world, + ecs_entity_t entity, + const char *description) +{ + ecs_set_pair(world, entity, EcsDocDescription, EcsDocDetail, { + .value = description + }); +} - ecs_entity_t old_scope = ecs_set_scope(world, t); +void ecs_doc_set_link( + ecs_world_t *world, + ecs_entity_t entity, + const char *link) +{ + ecs_set_pair(world, entity, EcsDocDescription, EcsDocLink, { + .value = link + }); +} - while ((ptr = meta_parse_member(ptr, &token, &ctx)) && ptr[0]) { - ecs_entity_t m = ecs_entity_init(world, &(ecs_entity_desc_t) { - .name = token.name - }); +const char* ecs_doc_get_brief( + const ecs_world_t *world, + ecs_entity_t entity) +{ + EcsDocDescription *ptr = ecs_get_pair( + world, entity, EcsDocDescription, EcsDocBrief); + if (ptr) { + return ptr->value; + } else { + return NULL; + } +} - ecs_entity_t type = meta_lookup( - world, &token.type, ptr, 1, &ctx); - if (!type) { - goto error; - } +const char* ecs_doc_get_detail( + const ecs_world_t *world, + ecs_entity_t entity) +{ + EcsDocDescription *ptr = ecs_get_pair( + world, entity, EcsDocDescription, EcsDocDetail); + if (ptr) { + return ptr->value; + } else { + return NULL; + } +} - ecs_set(world, m, EcsMember, { - .type = type, - .count = (ecs_size_t)token.count - }); +const char* ecs_doc_get_link( + const ecs_world_t *world, + ecs_entity_t entity) +{ + EcsDocDescription *ptr = ecs_get_pair( + world, entity, EcsDocDescription, EcsDocLink); + if (ptr) { + return ptr->value; + } else { + return NULL; } +} - ecs_set_scope(world, old_scope); +void FlecsDocImport( + ecs_world_t *world) +{ + ECS_MODULE(world, FlecsDoc); - return 0; -error: - return -1; + ecs_set_name_prefix(world, "EcsDoc"); + + flecs_bootstrap_component(world, EcsDocDescription); + flecs_bootstrap_tag(world, EcsDocBrief); + flecs_bootstrap_tag(world, EcsDocDetail); + flecs_bootstrap_tag(world, EcsDocLink); + + ecs_set_component_actions(world, EcsDocDescription, { + .ctor = ecs_default_ctor, + .move = ecs_move(EcsDocDescription), + .copy = ecs_copy(EcsDocDescription), + .dtor = ecs_dtor(EcsDocDescription) + }); } +#endif + +/* Move table from empty to non-empty list, or vice versa */ static -int meta_parse_constants( - ecs_world_t *world, - ecs_entity_t t, - const char *desc, - bool is_bitmask) +int32_t move_table( + ecs_table_cache_t *cache, + const ecs_table_t *table, + int32_t index, + ecs_vector_t **dst_array, + ecs_vector_t *src_array, + bool empty) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(t != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(desc != NULL, ECS_INTERNAL_ERROR, NULL); - - const char *ptr = desc; - const char *name = ecs_get_name(world, t); + (void)table; - meta_parse_ctx_t ctx = { - .name = name, - .desc = ptr - }; + int32_t new_index = 0, old_index = 0; + ecs_size_t size = cache->size; + int32_t last_src_index = ecs_vector_count(src_array) - 1; + ecs_assert(last_src_index >= 0, ECS_INTERNAL_ERROR, NULL); - meta_constant_t token; - int64_t last_value = 0; + ecs_table_cache_hdr_t *elem = ecs_vector_last_t(src_array, size, 8); + + /* The last table of the source array will be moved to the location of the + * table to move, do some bookkeeping to keep things consistent. */ + if (last_src_index) { + int32_t *old_index_ptr = ecs_map_get(cache->index, + int32_t, elem->table->id); + ecs_assert(old_index_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_entity_t old_scope = ecs_set_scope(world, t); + old_index = old_index_ptr[0]; + if (!empty) { + if (old_index >= 0) { + /* old_index should be negative if not empty, since + * we're moving from the empty list to the non-empty list. + * However, if the last table in the source array is also + * the table being moved, this can happen. */ + ecs_assert(table == elem->table, ECS_INTERNAL_ERROR, NULL); + } else { + /* If not empty, src = the empty list, and index should + * be negative. */ + old_index = old_index * -1 - 1; /* Normalize */ + } + } - while ((ptr = meta_parse_constant(ptr, &token, &ctx))) { - if (token.is_value_set) { - last_value = token.value; - } else if (is_bitmask) { - ecs_meta_error(&ctx, ptr, - "bitmask requires explicit value assignment"); - goto error; + if (old_index == last_src_index) { + old_index_ptr[0] = index; } + } else { + /* If last_src_index is 0, the table to move was the only table in the + * src array, so no other administration needs to be updated. */ + } - ecs_entity_t c = ecs_entity_init(world, &(ecs_entity_desc_t) { - .name = token.name - }); + if (!empty) { + old_index = index * -1 - 1; + } else { + old_index = index; + } - if (!is_bitmask) { - ecs_set_pair_object(world, c, EcsConstant, ecs_i32_t, - {(ecs_i32_t)last_value}); - } else { - ecs_set_pair_object(world, c, EcsConstant, ecs_u32_t, - {(ecs_u32_t)last_value}); - } + /* Actually move the table. Only move from src to dst if we have a + * dst_array, otherwise just remove it from src. */ + if (dst_array) { + new_index = ecs_vector_count(*dst_array); + ecs_vector_move_index_t(dst_array, src_array, size, 8, old_index); - last_value ++; + /* Make sure table is where we expect it */ + elem = ecs_vector_last_t(*dst_array, size, 8); + ecs_assert(elem->table == table, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ecs_vector_count(*dst_array) == (new_index + 1), + ECS_INTERNAL_ERROR, NULL); + elem->empty = empty; + } else { + ecs_vector_remove_t(src_array, size, 8, old_index); } - ecs_set_scope(world, old_scope); + /* Ensure that src array has now one element less */ + ecs_assert(ecs_vector_count(src_array) == last_src_index, + ECS_INTERNAL_ERROR, NULL); - return 0; -error: - return -1; + if (empty) { + /* Table is now empty, index is negative */ + new_index = new_index * -1 - 1; + } + + return new_index; } static -int meta_parse_enum( - ecs_world_t *world, - ecs_entity_t t, - const char *desc) +void ensure_index( + ecs_table_cache_t *cache) { - ecs_add(world, t, EcsEnum); - return meta_parse_constants(world, t, desc, false); + if (!cache->index) { + cache->index = ecs_map_new(int32_t, 0); + } } -static -int meta_parse_bitmask( - ecs_world_t *world, - ecs_entity_t t, - const char *desc) +void _ecs_table_cache_init( + ecs_table_cache_t *cache, + ecs_size_t size, + ecs_poly_t *parent, + void(*free_payload)(ecs_poly_t*, void*)) { - ecs_add(world, t, EcsBitmask); - return meta_parse_constants(world, t, desc, true); + ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(size >= ECS_SIZEOF(ecs_table_cache_hdr_t), + ECS_INTERNAL_ERROR, NULL); + cache->index = NULL; + cache->empty_tables = NULL; + cache->tables = NULL; + cache->size = size; + cache->parent = parent; + cache->free_payload = free_payload; } -int ecs_meta_from_desc( - ecs_world_t *world, - ecs_entity_t component, - ecs_type_kind_t kind, - const char *desc) +static +void free_payload( + ecs_table_cache_t *cache, + ecs_vector_t *tables) { - switch(kind) { - case EcsStructType: - if (meta_parse_struct(world, component, desc)) { - goto error; - } - break; - case EcsEnumType: - if (meta_parse_enum(world, component, desc)) { - goto error; - } - break; - case EcsBitmaskType: - if (meta_parse_bitmask(world, component, desc)) { - goto error; + void(*free_payload_func)(ecs_poly_t*, void*) = cache->free_payload; + if (free_payload_func) { + ecs_poly_t *parent = cache->parent; + ecs_size_t size = cache->size; + int32_t i, count = ecs_vector_count(tables); + + for (i = 0; i < count; i ++) { + void *ptr = ecs_vector_get_t(tables, size, 8, i); + free_payload_func(parent, ptr); } - break; - default: - break; } - return 0; -error: - return -1; + ecs_vector_free(tables); } -#endif - -#ifdef FLECS_APP - -static ecs_frame_action_t frame_action; - -int ecs_app_run( - ecs_world_t *world, - const ecs_app_desc_t *desc) +void ecs_table_cache_fini( + ecs_table_cache_t *cache) { - ecs_set_target_fps(world, desc->target_fps); - ecs_set_threads(world, desc->threads); - - ecs_frame_action_t callback = frame_action; - if (!callback) { - callback = ecs_app_run_frame; - } - - int result; - while ((result = callback(world, desc)) == 0) { } - - return result; + ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_map_free(cache->index); + free_payload(cache, cache->tables); + free_payload(cache, cache->empty_tables); } -int ecs_app_run_frame( - ecs_world_t *world, - const ecs_app_desc_t *desc) +bool ecs_table_cache_is_initialized( + ecs_table_cache_t *cache) { - return ecs_progress(world, desc->delta_time); + return cache->size != 0; } -int ecs_app_set_frame_action( - ecs_frame_action_t callback) +void* _ecs_table_cache_insert( + ecs_table_cache_t *cache, + ecs_size_t size, + const ecs_table_t *table) { - if (frame_action) { - ecs_err("frame action already set"); - return -1; - } - - frame_action = callback; + ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(size == cache->size, ECS_INTERNAL_ERROR, NULL); - return 0; -} + ecs_assert(!table || (_ecs_table_cache_get(cache, size, table) == NULL), + ECS_INTERNAL_ERROR, NULL); -#endif + int32_t index; + ecs_table_cache_hdr_t *result; + bool empty; -/* Roles */ -const ecs_id_t ECS_CASE = (ECS_ROLE | (0x7Cull << 56)); -const ecs_id_t ECS_SWITCH = (ECS_ROLE | (0x7Bull << 56)); -const ecs_id_t ECS_PAIR = (ECS_ROLE | (0x7Aull << 56)); -const ecs_id_t ECS_OVERRIDE = (ECS_ROLE | (0x75ull << 56)); -const ecs_id_t ECS_DISABLED = (ECS_ROLE | (0x74ull << 56)); + if (!table) { + empty = false; + } else { + empty = ecs_table_count(table) == 0; + } -/** Builtin component ids */ -const ecs_entity_t ecs_id(EcsComponent) = 1; -const ecs_entity_t ecs_id(EcsComponentLifecycle) = 2; -const ecs_entity_t ecs_id(EcsType) = 3; -const ecs_entity_t ecs_id(EcsIdentifier) = 4; -const ecs_entity_t ecs_id(EcsTrigger) = 5; -const ecs_entity_t ecs_id(EcsQuery) = 6; -const ecs_entity_t ecs_id(EcsObserver) = 7; - -/* System module component ids */ -const ecs_entity_t ecs_id(EcsSystem) = 10; -const ecs_entity_t ecs_id(EcsTickSource) = 11; + if (empty) { + result = ecs_vector_add_t(&cache->empty_tables, size, 8); + index = -ecs_vector_count(cache->empty_tables); + } else { + index = ecs_vector_count(cache->tables); + result = ecs_vector_add_t(&cache->tables, size, 8); + } -/** Pipeline module component ids */ -const ecs_entity_t ecs_id(EcsPipelineQuery) = 12; + if (table) { + ensure_index(cache); + ecs_map_set(cache->index, table->id, &index); + } + + ecs_os_memset(result, 0, size); + result->table = (ecs_table_t*)table; + result->empty = empty; -/** Timer module component ids */ -const ecs_entity_t ecs_id(EcsTimer) = 13; -const ecs_entity_t ecs_id(EcsRateFilter) = 14; + return result; +} -/** Meta module component ids */ -const ecs_entity_t ecs_id(EcsMetaType) = 15; -const ecs_entity_t ecs_id(EcsMetaTypeSerialized) = 16; -const ecs_entity_t ecs_id(EcsPrimitive) = 17; -const ecs_entity_t ecs_id(EcsEnum) = 18; -const ecs_entity_t ecs_id(EcsBitmask) = 19; -const ecs_entity_t ecs_id(EcsMember) = 20; -const ecs_entity_t ecs_id(EcsStruct) = 21; -const ecs_entity_t ecs_id(EcsArray) = 22; -const ecs_entity_t ecs_id(EcsVector) = 23; +void _ecs_table_cache_remove( + ecs_table_cache_t *cache, + ecs_size_t size, + const ecs_table_t *table) +{ + ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(size == cache->size, ECS_INTERNAL_ERROR, NULL); + (void)size; -/* Core scopes & entities */ -const ecs_entity_t EcsWorld = ECS_HI_COMPONENT_ID + 0; -const ecs_entity_t EcsFlecs = ECS_HI_COMPONENT_ID + 1; -const ecs_entity_t EcsFlecsCore = ECS_HI_COMPONENT_ID + 2; -const ecs_entity_t EcsModule = ECS_HI_COMPONENT_ID + 3; -const ecs_entity_t EcsPrefab = ECS_HI_COMPONENT_ID + 4; -const ecs_entity_t EcsDisabled = ECS_HI_COMPONENT_ID + 5; + int32_t *index = ecs_map_get(cache->index, int32_t, table->id); + if (!index) { + return; + } -/* Relation properties */ -const ecs_entity_t EcsWildcard = ECS_HI_COMPONENT_ID + 10; -const ecs_entity_t EcsThis = ECS_HI_COMPONENT_ID + 11; -const ecs_entity_t EcsTransitive = ECS_HI_COMPONENT_ID + 12; -const ecs_entity_t EcsInclusive = ECS_HI_COMPONENT_ID + 13; -const ecs_entity_t EcsFinal = ECS_HI_COMPONENT_ID + 14; -const ecs_entity_t EcsTag = ECS_HI_COMPONENT_ID + 15; + if (cache->free_payload) { + ecs_table_cache_hdr_t *elem = _ecs_table_cache_get( + cache, cache->size, table); + ecs_assert(elem != NULL, ECS_INTERNAL_ERROR, NULL); + cache->free_payload(cache->parent, elem); + } -/* Identifier tags */ -const ecs_entity_t EcsName = ECS_HI_COMPONENT_ID + 16; -const ecs_entity_t EcsSymbol = ECS_HI_COMPONENT_ID + 17; + if (index[0] < 0) { + move_table(cache, table, index[0], NULL, cache->empty_tables, false); + } else { + move_table(cache, table, index[0], NULL, cache->tables, true); + } -/* Relations */ -const ecs_entity_t EcsChildOf = ECS_HI_COMPONENT_ID + 20; -const ecs_entity_t EcsIsA = ECS_HI_COMPONENT_ID + 21; + ecs_map_remove(cache->index, table->id); -/* Events */ -const ecs_entity_t EcsOnAdd = ECS_HI_COMPONENT_ID + 30; -const ecs_entity_t EcsOnRemove = ECS_HI_COMPONENT_ID + 31; -const ecs_entity_t EcsOnSet = ECS_HI_COMPONENT_ID + 32; -const ecs_entity_t EcsUnSet = ECS_HI_COMPONENT_ID + 33; -const ecs_entity_t EcsOnDelete = ECS_HI_COMPONENT_ID + 34; -const ecs_entity_t EcsOnCreateTable = ECS_HI_COMPONENT_ID + 35; -const ecs_entity_t EcsOnDeleteTable = ECS_HI_COMPONENT_ID + 36; -const ecs_entity_t EcsOnTableEmpty = ECS_HI_COMPONENT_ID + 37; -const ecs_entity_t EcsOnTableNonEmpty = ECS_HI_COMPONENT_ID + 38; -const ecs_entity_t EcsOnCreateTrigger = ECS_HI_COMPONENT_ID + 39; -const ecs_entity_t EcsOnDeleteTrigger = ECS_HI_COMPONENT_ID + 40; -const ecs_entity_t EcsOnDeleteObservable = ECS_HI_COMPONENT_ID + 41; -const ecs_entity_t EcsOnComponentLifecycle = ECS_HI_COMPONENT_ID + 42; -const ecs_entity_t EcsOnDeleteObject = ECS_HI_COMPONENT_ID + 43; + if (!ecs_map_count(cache->index)) { + ecs_assert(ecs_vector_count(cache->tables) == 0, + ECS_INTERNAL_ERROR, NULL); + ecs_assert(ecs_vector_count(cache->empty_tables) == 0, + ECS_INTERNAL_ERROR, NULL); + ecs_table_cache_fini(cache); -/* Actions */ -const ecs_entity_t EcsRemove = ECS_HI_COMPONENT_ID + 50; -const ecs_entity_t EcsDelete = ECS_HI_COMPONENT_ID + 51; -const ecs_entity_t EcsThrow = ECS_HI_COMPONENT_ID + 52; + cache->index = NULL; + cache->tables = NULL; + cache->empty_tables = NULL; + } +} -/* Misc */ -const ecs_entity_t EcsDefaultChildComponent = ECS_HI_COMPONENT_ID + 55; +void* _ecs_table_cache_get( + const ecs_table_cache_t *cache, + ecs_size_t size, + const ecs_table_t *table) +{ + ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(size == cache->size, ECS_INTERNAL_ERROR, NULL); -/* Systems */ -const ecs_entity_t EcsMonitor = ECS_HI_COMPONENT_ID + 61; -const ecs_entity_t EcsInactive = ECS_HI_COMPONENT_ID + 63; -const ecs_entity_t EcsPipeline = ECS_HI_COMPONENT_ID + 64; -const ecs_entity_t EcsPreFrame = ECS_HI_COMPONENT_ID + 65; -const ecs_entity_t EcsOnLoad = ECS_HI_COMPONENT_ID + 66; -const ecs_entity_t EcsPostLoad = ECS_HI_COMPONENT_ID + 67; -const ecs_entity_t EcsPreUpdate = ECS_HI_COMPONENT_ID + 68; -const ecs_entity_t EcsOnUpdate = ECS_HI_COMPONENT_ID + 69; -const ecs_entity_t EcsOnValidate = ECS_HI_COMPONENT_ID + 70; -const ecs_entity_t EcsPostUpdate = ECS_HI_COMPONENT_ID + 71; -const ecs_entity_t EcsPreStore = ECS_HI_COMPONENT_ID + 72; -const ecs_entity_t EcsOnStore = ECS_HI_COMPONENT_ID + 73; -const ecs_entity_t EcsPostFrame = ECS_HI_COMPONENT_ID + 74; + int32_t *index = ecs_map_get(cache->index, int32_t, table->id); + if (!index) { + return NULL; + } -/* Meta primitive components (don't use low ids to save id space) */ -const ecs_entity_t EcsConstant = ECS_HI_COMPONENT_ID + 80; -const ecs_entity_t ecs_id(ecs_bool_t) = ECS_HI_COMPONENT_ID + 81; -const ecs_entity_t ecs_id(ecs_char_t) = ECS_HI_COMPONENT_ID + 82; -const ecs_entity_t ecs_id(ecs_byte_t) = ECS_HI_COMPONENT_ID + 83; -const ecs_entity_t ecs_id(ecs_u8_t) = ECS_HI_COMPONENT_ID + 84; -const ecs_entity_t ecs_id(ecs_u16_t) = ECS_HI_COMPONENT_ID + 85; -const ecs_entity_t ecs_id(ecs_u32_t) = ECS_HI_COMPONENT_ID + 86; -const ecs_entity_t ecs_id(ecs_u64_t) = ECS_HI_COMPONENT_ID + 87; -const ecs_entity_t ecs_id(ecs_uptr_t) = ECS_HI_COMPONENT_ID + 88; -const ecs_entity_t ecs_id(ecs_i8_t) = ECS_HI_COMPONENT_ID + 89; -const ecs_entity_t ecs_id(ecs_i16_t) = ECS_HI_COMPONENT_ID + 90; -const ecs_entity_t ecs_id(ecs_i32_t) = ECS_HI_COMPONENT_ID + 91; -const ecs_entity_t ecs_id(ecs_i64_t) = ECS_HI_COMPONENT_ID + 92; -const ecs_entity_t ecs_id(ecs_iptr_t) = ECS_HI_COMPONENT_ID + 93; -const ecs_entity_t ecs_id(ecs_f32_t) = ECS_HI_COMPONENT_ID + 94; -const ecs_entity_t ecs_id(ecs_f64_t) = ECS_HI_COMPONENT_ID + 95; -const ecs_entity_t ecs_id(ecs_string_t) = ECS_HI_COMPONENT_ID + 96; -const ecs_entity_t ecs_id(ecs_entity_t) = ECS_HI_COMPONENT_ID + 97; + ecs_table_cache_hdr_t *result; + if (index[0] >= 0) { + result = ecs_vector_get_t(cache->tables, size, 8, index[0]); + } else { + result = ecs_vector_get_t( + cache->empty_tables, size, 8, index[0] * -1 - 1); + } -/* Doc module components */ -const ecs_entity_t ecs_id(EcsDocDescription) =ECS_HI_COMPONENT_ID + 100; -const ecs_entity_t EcsDocBrief = ECS_HI_COMPONENT_ID + 101; -const ecs_entity_t EcsDocDetail = ECS_HI_COMPONENT_ID + 102; -const ecs_entity_t EcsDocLink = ECS_HI_COMPONENT_ID + 103; + ecs_assert(!result || result->table == table, ECS_INTERNAL_ERROR, NULL); -/* -- Private functions -- */ + return result; +} -const ecs_stage_t* flecs_stage_from_readonly_world( - const ecs_world_t *world) +void ecs_table_cache_set_empty( + ecs_table_cache_t *cache, + const ecs_table_t *table, + bool empty) { - ecs_assert(ecs_poly_is(world, ecs_world_t) || - ecs_poly_is(world, ecs_stage_t), - ECS_INTERNAL_ERROR, - NULL); + ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - if (ecs_poly_is(world, ecs_world_t)) { - return &world->stage; + int32_t *index = ecs_map_get(cache->index, int32_t, table->id); + if (!index) { + return; + } - } else if (ecs_poly_is(world, ecs_stage_t)) { - return (ecs_stage_t*)world; + /* If table is already in the correct array nothing needs to be done */ + if (empty && index[0] < 0) { + return; + } else if (!empty && index[0] >= 0) { + return; + } + + if (index[0] < 0) { + index[0] = move_table( + cache, table, index[0], &cache->tables, cache->empty_tables, empty); + } else { + index[0] = move_table( + cache, table, index[0], &cache->empty_tables, cache->tables, empty); } - - return NULL; } -ecs_stage_t *flecs_stage_from_world( - ecs_world_t **world_ptr) +void* _ecs_table_cache_tables( + const ecs_table_cache_t *cache, + ecs_size_t size) { - ecs_world_t *world = *world_ptr; - - ecs_assert(ecs_poly_is(world, ecs_world_t) || - ecs_poly_is(world, ecs_stage_t), - ECS_INTERNAL_ERROR, - NULL); - - if (ecs_poly_is(world, ecs_world_t)) { - ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); - return &world->stage; - - } else if (ecs_poly_is(world, ecs_stage_t)) { - ecs_stage_t *stage = (ecs_stage_t*)world; - *world_ptr = stage->world; - return stage; + if (!cache) { + return NULL; } - - return NULL; + return ecs_vector_first_t(cache->tables, size, 8); } -/* Evaluate component monitor. If a monitored entity changed it will have set a - * flag in one of the world's component monitors. Queries can register - * themselves with component monitors to determine whether they need to rematch - * with tables. */ -static -void eval_component_monitor( - ecs_world_t *world) +void* _ecs_table_cache_empty_tables( + const ecs_table_cache_t *cache, + ecs_size_t size) { - ecs_relation_monitor_t *rm = &world->monitors; - - if (!rm->is_dirty) { - return; + if (!cache) { + return NULL; } + return ecs_vector_first_t(cache->empty_tables, size, 8); +} - ecs_map_iter_t it = ecs_map_iter(rm->monitor_sets); - ecs_monitor_set_t *ms; - - while ((ms = ecs_map_next(&it, ecs_monitor_set_t, NULL))) { - if (!ms->is_dirty) { - continue; - } - - if (ms->monitors) { - ecs_map_iter_t mit = ecs_map_iter(ms->monitors); - ecs_monitor_t *m; - while ((m = ecs_map_next(&mit, ecs_monitor_t, NULL))) { - if (!m->is_dirty) { - continue; - } - - ecs_vector_each(m->queries, ecs_query_t*, q_ptr, { - flecs_query_notify(world, *q_ptr, &(ecs_query_event_t) { - .kind = EcsQueryTableRematch - }); - }); - - m->is_dirty = false; - } - } - - ms->is_dirty = false; +int32_t ecs_table_cache_count( + const ecs_table_cache_t *cache) +{ + if (!cache) { + return 0; } - - rm->is_dirty = false; + return ecs_vector_count(cache->tables); } -void flecs_monitor_mark_dirty( - ecs_world_t *world, - ecs_entity_t relation, - ecs_entity_t id) +int32_t ecs_table_cache_empty_count( + const ecs_table_cache_t *cache) { - ecs_assert(world->monitors.monitor_sets != NULL, ECS_INTERNAL_ERROR, NULL); + if (!cache) { + return 0; + } + return ecs_vector_count(cache->empty_tables); +} - /* Only flag if there are actually monitors registered, so that we - * don't waste cycles evaluating monitors if there's no interest */ - ecs_monitor_set_t *ms = ecs_map_get(world->monitors.monitor_sets, - ecs_monitor_set_t, relation); - if (ms && ms->monitors) { - ecs_monitor_t *m = ecs_map_get(ms->monitors, - ecs_monitor_t, id); - if (m) { - m->is_dirty = true; - ms->is_dirty = true; - world->monitors.is_dirty = true; - } +bool ecs_table_cache_is_empty( + const ecs_table_cache_t *cache) +{ + if (!cache) { + return true; } + return ecs_map_count(cache->index) == 0; } -void flecs_monitor_register( +void _ecs_table_cache_fini_delete_all( ecs_world_t *world, - ecs_entity_t relation, - ecs_entity_t id, - ecs_query_t *query) + ecs_table_cache_t *cache, + ecs_size_t size) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(query != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(world->monitors.monitor_sets != NULL, ECS_INTERNAL_ERROR, NULL); + if (!cache || !cache->index) { + return; + } - ecs_monitor_set_t *ms = ecs_map_ensure( - world->monitors.monitor_sets, ecs_monitor_set_t, relation); - ecs_assert(ms != NULL, ECS_INTERNAL_ERROR, NULL); + /* Temporarily set index to NULL, so that when the table tries to remove + * itself from the cache it won't be able to. This keeps the arrays we're + * iterating over consistent */ + ecs_map_t *index = cache->index; + cache->index = NULL; - if (!ms->monitors) { - ms->monitors = ecs_map_new(ecs_monitor_t, 1); + int32_t i, count = ecs_vector_count(cache->tables); + for (i = 0; i < count; i ++) { + ecs_table_cache_hdr_t *ptr = ecs_vector_get_t( + cache->tables, size, 8, i); + flecs_delete_table(world, ptr->table); } - ecs_monitor_t *m = ecs_map_ensure(ms->monitors, ecs_monitor_t, id); - ecs_assert(m != NULL, ECS_INTERNAL_ERROR, NULL); + count = ecs_vector_count(cache->empty_tables); + for (i = 0; i < count; i ++) { + ecs_table_cache_hdr_t *ptr = ecs_vector_get_t( + cache->empty_tables, size, 8, i); + flecs_delete_table(world, ptr->table); + } - ecs_query_t **q = ecs_vector_add(&m->queries, ecs_query_t*); - *q = query; -} + cache->index = index; -static -void monitors_init( - ecs_relation_monitor_t *rm) -{ - rm->monitor_sets = ecs_map_new(ecs_monitor_t, 0); - rm->is_dirty = false; + ecs_table_cache_fini(cache); } +/** Resize the vector buffer */ static -void monitors_fini( - ecs_relation_monitor_t *rm) +ecs_vector_t* resize( + ecs_vector_t *vector, + int16_t offset, + int32_t size) { - ecs_map_iter_t it = ecs_map_iter(rm->monitor_sets); - ecs_monitor_set_t *ms; + ecs_vector_t *result = ecs_os_realloc(vector, offset + size); + ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, 0); + return result; +} - while ((ms = ecs_map_next(&it, ecs_monitor_set_t, NULL))) { - if (ms->monitors) { - ecs_map_iter_t mit = ecs_map_iter(ms->monitors); - ecs_monitor_t *m; - while ((m = ecs_map_next(&mit, ecs_monitor_t, NULL))) { - ecs_vector_free(m->queries); - } +/* -- Public functions -- */ - ecs_map_free(ms->monitors); - } - } +ecs_vector_t* _ecs_vector_new( + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count) +{ + ecs_assert(elem_size != 0, ECS_INTERNAL_ERROR, NULL); + + ecs_vector_t *result = + ecs_os_malloc(offset + elem_size * elem_count); + ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); - ecs_map_free(rm->monitor_sets); + result->count = 0; + result->size = elem_count; +#ifndef NDEBUG + result->elem_size = elem_size; +#endif + return result; } -static -void init_store( - ecs_world_t *world) +ecs_vector_t* _ecs_vector_from_array( + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count, + void *array) { - ecs_os_memset(&world->store, 0, ECS_SIZEOF(ecs_store_t)); + ecs_assert(elem_size != 0, ECS_INTERNAL_ERROR, NULL); - /* Initialize entity index */ - world->store.entity_index = flecs_sparse_new(ecs_record_t); - flecs_sparse_set_id_source(world->store.entity_index, &world->stats.last_id); - - /* Initialize root table */ - world->store.tables = flecs_sparse_new(ecs_table_t); + ecs_vector_t *result = + ecs_os_malloc(offset + elem_size * elem_count); + ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); - /* Initialize table map */ - world->store.table_map = flecs_table_hashmap_new(); + ecs_os_memcpy(ECS_OFFSET(result, offset), array, elem_size * elem_count); - /* Initialize one root table per stage */ - flecs_init_root_table(world); + result->count = elem_count; + result->size = elem_count; +#ifndef NDEBUG + result->elem_size = elem_size; +#endif + return result; } -static -void clean_tables( - ecs_world_t *world) +void ecs_vector_free( + ecs_vector_t *vector) { - int32_t i, count = flecs_sparse_count(world->store.tables); + ecs_os_free(vector); +} - for (i = 0; i < count; i ++) { - ecs_table_t *t = flecs_sparse_get_dense(world->store.tables, ecs_table_t, i); - flecs_table_free(world, t); +void ecs_vector_clear( + ecs_vector_t *vector) +{ + if (vector) { + vector->count = 0; } +} - /* Free table types separately so that if application destructors rely on - * a type it's still valid. */ - for (i = 0; i < count; i ++) { - ecs_table_t *t = flecs_sparse_get_dense(world->store.tables, ecs_table_t, i); - flecs_table_free_type(t); - } +void _ecs_vector_zero( + ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset) +{ + void *array = ECS_OFFSET(vector, offset); + ecs_os_memset(array, 0, elem_size * vector->count); +} - /* Clear the root table */ - if (count) { - flecs_table_reset(world, &world->store.root); +void ecs_vector_assert_size( + ecs_vector_t *vector, + ecs_size_t elem_size) +{ + (void)elem_size; + + if (vector) { + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); } } -static -void fini_store(ecs_world_t *world) { - clean_tables(world); - flecs_sparse_free(world->store.tables); - flecs_table_free(world, &world->store.root); - flecs_sparse_clear(world->store.entity_index); - flecs_hashmap_free(world->store.table_map); -} +void* _ecs_vector_addn( + ecs_vector_t **array_inout, + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count) +{ + ecs_assert(array_inout != NULL, ECS_INTERNAL_ERROR, NULL); + + if (elem_count == 1) { + return _ecs_vector_add(array_inout, elem_size, offset); + } + + ecs_vector_t *vector = *array_inout; + if (!vector) { + vector = _ecs_vector_new(elem_size, offset, 1); + *array_inout = vector; + } -static -void log_addons(void) { - ecs_trace("addons included in build:"); - ecs_log_push(); - #ifdef FLECS_CPP - ecs_trace("FLECS_CPP"); - #endif - #ifdef FLECS_MODULE - ecs_trace("FLECS_MODULE"); - #endif - #ifdef FLECS_PARSER - ecs_trace("FLECS_PARSER"); - #endif - #ifdef FLECS_PLECS - ecs_trace("FLECS_PLECS"); - #endif - #ifdef FLECS_RULES - ecs_trace("FLECS_RULES"); - #endif - #ifdef FLECS_SNAPSHOT - ecs_trace("FLECS_SNAPSHOT"); - #endif - #ifdef FLECS_STATS - ecs_trace("FLECS_STATS"); - #endif - #ifdef FLECS_SYSTEM - ecs_trace("FLECS_SYSTEM"); - #endif - #ifdef FLECS_PIPELINE - ecs_trace("FLECS_PIPELINE"); - #endif - #ifdef FLECS_TIMER - ecs_trace("FLECS_TIMER"); - #endif - #ifdef FLECS_META - ecs_trace("FLECS_META"); - #endif - #ifdef FLECS_META_C - ecs_trace("FLECS_META_C"); - #endif - #ifdef FLECS_EXPR - ecs_trace("FLECS_EXPR"); - #endif - #ifdef FLECS_JSON - ecs_trace("FLECS_JSON"); - #endif - #ifdef FLECS_DOC - ecs_trace("FLECS_DOC"); - #endif - #ifdef FLECS_COREDOC - ecs_trace("FLECS_COREDOC"); - #endif - #ifdef FLECS_LOG - ecs_trace("FLECS_LOG"); - #endif - ecs_log_pop(); -} + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); -/* -- Public functions -- */ + int32_t max_count = vector->size; + int32_t old_count = vector->count; + int32_t new_count = old_count + elem_count; -ecs_world_t *ecs_mini(void) { - ecs_os_init(); + if ((new_count - 1) >= max_count) { + if (!max_count) { + max_count = elem_count; + } else { + while (max_count < new_count) { + max_count *= 2; + } + } - /* Log information about current build & OS API config */ + vector = resize(vector, offset, max_count * elem_size); + vector->size = max_count; + *array_inout = vector; + } - ecs_trace("#[bold]bootstrapping world"); - ecs_log_push(); + vector->count = new_count; - ecs_trace("tracing enabled, call ecs_log_set_level(-1) to disable"); + return ECS_OFFSET(vector, offset + elem_size * old_count); +} - if (!ecs_os_has_heap()) { - ecs_abort(ECS_MISSING_OS_API, NULL); - } +void* _ecs_vector_add( + ecs_vector_t **array_inout, + ecs_size_t elem_size, + int16_t offset) +{ + ecs_assert(array_inout != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_vector_t *vector = *array_inout; + int32_t count, size; - if (!ecs_os_has_threading()) { - ecs_trace("threading unavailable, to use threads set OS API first (see examples)"); - } + if (vector) { + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + count = vector->count; + size = vector->size; - if (!ecs_os_has_time()) { - ecs_trace("time management not available"); - } + if (count >= size) { + size *= 2; + if (!size) { + size = 2; + } + vector = resize(vector, offset, size * elem_size); + *array_inout = vector; + vector->size = size; + } - log_addons(); + vector->count = count + 1; + return ECS_OFFSET(vector, offset + elem_size * count); + } -#ifndef NDEBUG - ecs_trace("debug build, rebuild with NDEBUG for improved performance"); -#else - ecs_trace("#[green]release#[reset] build"); -#endif + vector = _ecs_vector_new(elem_size, offset, 2); + *array_inout = vector; + vector->count = 1; + vector->size = 2; + return ECS_OFFSET(vector, offset); +} - ecs_world_t *world = ecs_os_calloc(sizeof(ecs_world_t)); - ecs_assert(world != NULL, ECS_OUT_OF_MEMORY, NULL); - ecs_poly_init(world, ecs_world_t); +int32_t _ecs_vector_move_index( + ecs_vector_t **dst, + ecs_vector_t *src, + ecs_size_t elem_size, + int16_t offset, + int32_t index) +{ + if (dst && *dst) { + ecs_assert((*dst)->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + } + ecs_assert(src->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - world->self = world; + void *dst_elem = _ecs_vector_add(dst, elem_size, offset); + void *src_elem = _ecs_vector_get(src, elem_size, offset, index); - world->type_info = flecs_sparse_new(ecs_type_info_t); - world->id_index = ecs_map_new(ecs_id_record_t, 8); - flecs_observable_init(&world->observable); + ecs_os_memcpy(dst_elem, src_elem, elem_size); + return _ecs_vector_remove(src, elem_size, offset, index); +} - world->queries = flecs_sparse_new(ecs_query_t); - world->triggers = flecs_sparse_new(ecs_trigger_t); - world->observers = flecs_sparse_new(ecs_observer_t); - world->fini_tasks = ecs_vector_new(ecs_entity_t, 0); +void ecs_vector_remove_last( + ecs_vector_t *vector) +{ + if (vector && vector->count) vector->count --; +} - world->aliases = flecs_string_hashmap_new(ecs_entity_t); - world->symbols = flecs_string_hashmap_new(ecs_entity_t); - world->type_handles = ecs_map_new(ecs_entity_t, 0); +bool _ecs_vector_pop( + ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset, + void *value) +{ + if (!vector) { + return false; + } - world->stats.time_scale = 1.0; - - monitors_init(&world->monitors); + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - if (ecs_os_has_time()) { - ecs_os_get_time(&world->world_start_time); + int32_t count = vector->count; + if (!count) { + return false; } - flecs_stage_init(world, &world->stage); - ecs_set_stages(world, 1); - - init_store(world); - ecs_trace("table store initialized"); + void *elem = ECS_OFFSET(vector, offset + (count - 1) * elem_size); - flecs_bootstrap(world); + if (value) { + ecs_os_memcpy(value, elem, elem_size); + } - ecs_trace("world ready!"); - ecs_log_pop(); + ecs_vector_remove_last(vector); - return world; + return true; } -ecs_world_t *ecs_init(void) { - ecs_world_t *world = ecs_mini(); - -#ifdef FLECS_MODULE_H - ecs_trace("#[bold]import addons"); - ecs_log_push(); - ecs_trace("use ecs_mini to create world without importing addons"); -#ifdef FLECS_SYSTEM_H - ECS_IMPORT(world, FlecsSystem); -#endif -#ifdef FLECS_PIPELINE_H - ECS_IMPORT(world, FlecsPipeline); -#endif -#ifdef FLECS_TIMER_H - ECS_IMPORT(world, FlecsTimer); -#endif -#ifdef FLECS_META_H - ECS_IMPORT(world, FlecsMeta); -#endif -#ifdef FLECS_DOC_H - ECS_IMPORT(world, FlecsDoc); -#endif -#ifdef FLECS_COREDOC_H - ECS_IMPORT(world, FlecsCoreDoc); -#endif - ecs_trace("addons imported!"); - ecs_log_pop(); -#endif +int32_t _ecs_vector_remove( + ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset, + int32_t index) +{ + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + + int32_t count = vector->count; + void *buffer = ECS_OFFSET(vector, offset); + void *elem = ECS_OFFSET(buffer, index * elem_size); - return world; -} + ecs_assert(index < count, ECS_INVALID_PARAMETER, NULL); -#define ARG(short, long, action)\ - if (i < argc) {\ - if (argv[i][0] == '-') {\ - if (argv[i][1] == '-') {\ - if (long && !strcmp(&argv[i][2], long ? long : "")) {\ - action;\ - parsed = true;\ - }\ - } else {\ - if (short && argv[i][1] == short) {\ - action;\ - parsed = true;\ - }\ - }\ - }\ + count --; + if (index != count) { + void *last_elem = ECS_OFFSET(buffer, elem_size * count); + ecs_os_memcpy(elem, last_elem, elem_size); } -ecs_world_t* ecs_init_w_args( - int argc, - char *argv[]) -{ - (void)argc; - (void)argv; - return ecs_init(); + vector->count = count; + + return count; } -void ecs_quit( - ecs_world_t *world) +void _ecs_vector_reclaim( + ecs_vector_t **array_inout, + ecs_size_t elem_size, + int16_t offset) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - flecs_stage_from_world(&world); - world->should_quit = true; + ecs_vector_t *vector = *array_inout; + + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + + int32_t size = vector->size; + int32_t count = vector->count; + + if (count < size) { + size = count; + vector = resize(vector, offset, size * elem_size); + vector->size = size; + *array_inout = vector; + } } -bool ecs_should_quit( - const ecs_world_t *world) +int32_t ecs_vector_count( + const ecs_vector_t *vector) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - world = ecs_get_world(world); - return world->should_quit; + if (!vector) { + return 0; + } + return vector->count; } -void flecs_notify_tables( - ecs_world_t *world, - ecs_id_t id, - ecs_table_event_t *event) +int32_t ecs_vector_size( + const ecs_vector_t *vector) { - ecs_poly_assert(world, ecs_world_t); + if (!vector) { + return 0; + } + return vector->size; +} - /* If no id is specified, broadcast to all tables */ - if (!id) { - ecs_sparse_t *tables = world->store.tables; - int32_t i, count = flecs_sparse_count(tables); - for (i = 0; i < count; i ++) { - ecs_table_t *table = flecs_sparse_get_dense(tables, ecs_table_t, i); - flecs_table_notify(world, table, event); - } +int32_t _ecs_vector_set_size( + ecs_vector_t **array_inout, + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count) +{ + ecs_vector_t *vector = *array_inout; - /* If id is specified, only broadcast to tables with id */ + if (!vector) { + *array_inout = _ecs_vector_new(elem_size, offset, elem_count); + return elem_count; } else { - ecs_id_record_t *idr = flecs_get_id_record(world, id); - if (!idr) { - return; - } + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - const ecs_table_record_t *tables = flecs_id_record_tables(idr); - int32_t i, count = flecs_id_record_count(idr); - for (i = 0; i < count; i ++) { - flecs_table_notify(world, tables[i].table, event); + int32_t result = vector->size; + + if (elem_count < vector->count) { + elem_count = vector->count; } - tables = flecs_id_record_empty_tables(idr); - count = flecs_id_record_empty_count(idr); - for (i = 0; i < count; i ++) { - flecs_table_notify(world, tables[i].table, event); + if (result < elem_count) { + elem_count = flecs_next_pow_of_2(elem_count); + vector = resize(vector, offset, elem_count * elem_size); + vector->size = elem_count; + *array_inout = vector; + result = elem_count; } + + return result; } } -void ecs_default_ctor( - ecs_world_t *world, ecs_entity_t component, const ecs_entity_t *entity_ptr, - void *ptr, size_t size, int32_t count, void *ctx) +int32_t _ecs_vector_grow( + ecs_vector_t **array_inout, + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count) { - (void)world; (void)component; (void)entity_ptr; (void)ctx; - ecs_os_memset(ptr, 0, flecs_from_size_t(size) * count); + int32_t current = ecs_vector_count(*array_inout); + return _ecs_vector_set_size(array_inout, elem_size, offset, current + elem_count); } -static -void default_copy_ctor( - ecs_world_t *world, ecs_entity_t component, - const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, - const ecs_entity_t *src_entity, void *dst_ptr, const void *src_ptr, - size_t size, int32_t count, void *ctx) +int32_t _ecs_vector_set_count( + ecs_vector_t **array_inout, + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count) { - callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); - callbacks->copy(world, component, dst_entity, src_entity, dst_ptr, src_ptr, - size, count, ctx); + if (!*array_inout) { + *array_inout = _ecs_vector_new(elem_size, offset, elem_count); + } + + ecs_assert((*array_inout)->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + + (*array_inout)->count = elem_count; + ecs_size_t size = _ecs_vector_set_size(array_inout, elem_size, offset, elem_count); + return size; } -static -void default_move_ctor( - ecs_world_t *world, ecs_entity_t component, - const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, - const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, - int32_t count, void *ctx) +void* _ecs_vector_first( + const ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset) { - callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); - callbacks->move(world, component, dst_entity, src_entity, dst_ptr, src_ptr, - size, count, ctx); + (void)elem_size; + + ecs_assert(!vector || vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + if (vector && vector->size) { + return ECS_OFFSET(vector, offset); + } else { + return NULL; + } } -static -void default_ctor_w_move_w_dtor( - ecs_world_t *world, ecs_entity_t component, - const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, - const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, - int32_t count, void *ctx) +void* _ecs_vector_get( + const ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset, + int32_t index) { - callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); - callbacks->move(world, component, dst_entity, src_entity, dst_ptr, src_ptr, - size, count, ctx); - callbacks->dtor(world, component, src_entity, src_ptr, size, count, ctx); + if (!vector) { + return NULL; + } + + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); + + int32_t count = vector->count; + + if (index >= count) { + return NULL; + } + + return ECS_OFFSET(vector, offset + elem_size * index); } -static -void default_move_ctor_w_dtor( - ecs_world_t *world, ecs_entity_t component, - const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, - const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, - int32_t count, void *ctx) +void* _ecs_vector_last( + const ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset) { - callbacks->move_ctor(world, component, callbacks, dst_entity, src_entity, - dst_ptr, src_ptr, size, count, ctx); - callbacks->dtor(world, component, src_entity, src_ptr, size, count, ctx); + if (vector) { + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); + int32_t count = vector->count; + if (!count) { + return NULL; + } else { + return ECS_OFFSET(vector, offset + elem_size * (count - 1)); + } + } else { + return NULL; + } } -static -void default_move( - ecs_world_t *world, ecs_entity_t component, - const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, - const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, - int32_t count, void *ctx) +int32_t _ecs_vector_set_min_size( + ecs_vector_t **vector_inout, + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count) { - callbacks->move(world, component, dst_entity, src_entity, - dst_ptr, src_ptr, size, count, ctx); + if (!*vector_inout || (*vector_inout)->size < elem_count) { + return _ecs_vector_set_size(vector_inout, elem_size, offset, elem_count); + } else { + return (*vector_inout)->size; + } } -static -void default_dtor( - ecs_world_t *world, ecs_entity_t component, - const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, - const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, - int32_t count, void *ctx) +int32_t _ecs_vector_set_min_count( + ecs_vector_t **vector_inout, + ecs_size_t elem_size, + int16_t offset, + int32_t elem_count) { - (void)callbacks; - (void)src_entity; + _ecs_vector_set_min_size(vector_inout, elem_size, offset, elem_count); - /* When there is no move, destruct the destination component & memcpy the - * component to dst. The src component does not have to be destructed when - * a component has a trivial move. */ - callbacks->dtor(world, component, dst_entity, dst_ptr, size, count, ctx); - ecs_os_memcpy(dst_ptr, src_ptr, flecs_from_size_t(size) * count); + ecs_vector_t *v = *vector_inout; + if (v && v->count < elem_count) { + v->count = elem_count; + } + + return v->count; } -static -void default_move_w_dtor( - ecs_world_t *world, ecs_entity_t component, - const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, - const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, - int32_t count, void *ctx) -{ - /* If a component has a move, the move will take care of memcpying the data - * and destroying any data in dst. Because this is not a trivial move, the - * src component must also be destructed. */ - callbacks->move(world, component, dst_entity, src_entity, - dst_ptr, src_ptr, size, count, ctx); - callbacks->dtor(world, component, src_entity, src_ptr, size, count, ctx); -} - -void ecs_set_component_actions_w_id( - ecs_world_t *world, - ecs_entity_t component, - EcsComponentLifecycle *lifecycle) +void _ecs_vector_sort( + ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset, + ecs_comparator_t compare_action) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - flecs_stage_from_world(&world); - - const EcsComponent *component_ptr = ecs_get(world, component, EcsComponent); - - /* Cannot register lifecycle actions for things that aren't a component */ - ecs_assert(component_ptr != NULL, ECS_INVALID_PARAMETER, NULL); - - /* Cannot register lifecycle actions for components with size 0 */ - ecs_assert(component_ptr->size != 0, ECS_INVALID_PARAMETER, NULL); - - ecs_type_info_t *c_info = flecs_get_or_create_c_info(world, component); - ecs_assert(c_info != NULL, ECS_INTERNAL_ERROR, NULL); - - if (c_info->lifecycle_set) { - ecs_assert(c_info->component == component, ECS_INTERNAL_ERROR, NULL); - ecs_assert(c_info->lifecycle.ctor == lifecycle->ctor, - ECS_INCONSISTENT_COMPONENT_ACTION, NULL); - ecs_assert(c_info->lifecycle.dtor == lifecycle->dtor, - ECS_INCONSISTENT_COMPONENT_ACTION, NULL); - ecs_assert(c_info->lifecycle.copy == lifecycle->copy, - ECS_INCONSISTENT_COMPONENT_ACTION, NULL); - ecs_assert(c_info->lifecycle.move == lifecycle->move, - ECS_INCONSISTENT_COMPONENT_ACTION, NULL); - } else { - c_info->component = component; - c_info->lifecycle = *lifecycle; - c_info->lifecycle_set = true; - c_info->size = component_ptr->size; - c_info->alignment = component_ptr->alignment; + if (!vector) { + return; + } - /* If no constructor is set, invoking any of the other lifecycle actions - * is not safe as they will potentially access uninitialized memory. For - * ease of use, if no constructor is specified, set a default one that - * initializes the component to 0. */ - if (!lifecycle->ctor && - (lifecycle->dtor || lifecycle->copy || lifecycle->move)) - { - c_info->lifecycle.ctor = ecs_default_ctor; - } + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - /* Set default copy ctor, move ctor and merge */ - if (lifecycle->copy && !lifecycle->copy_ctor) { - c_info->lifecycle.copy_ctor = default_copy_ctor; - } + int32_t count = vector->count; + void *buffer = ECS_OFFSET(vector, offset); - if (lifecycle->move && !lifecycle->move_ctor) { - c_info->lifecycle.move_ctor = default_move_ctor; - } + if (count > 1) { + qsort(buffer, (size_t)count, (size_t)elem_size, compare_action); + } +} - if (!lifecycle->ctor_move_dtor) { - if (lifecycle->move) { - if (lifecycle->dtor) { - if (lifecycle->move_ctor) { - /* If an explicit move ctor has been set, use callback - * that uses the move ctor vs. using a ctor+move */ - c_info->lifecycle.ctor_move_dtor = - default_move_ctor_w_dtor; - } else { - /* If no explicit move_ctor has been set, use - * combination of ctor + move + dtor */ - c_info->lifecycle.ctor_move_dtor = - default_ctor_w_move_w_dtor; - } - } else { - /* If no dtor has been set, this is just a move ctor */ - c_info->lifecycle.ctor_move_dtor = - c_info->lifecycle.move_ctor; - } - } - } +void _ecs_vector_memory( + const ecs_vector_t *vector, + ecs_size_t elem_size, + int16_t offset, + int32_t *allocd, + int32_t *used) +{ + if (!vector) { + return; + } - if (!lifecycle->move_dtor) { - if (lifecycle->move) { - if (lifecycle->dtor) { - c_info->lifecycle.move_dtor = default_move_w_dtor; - } else { - c_info->lifecycle.move_dtor = default_move; - } - } else { - if (lifecycle->dtor) { - c_info->lifecycle.move_dtor = default_dtor; - } - } - } + ecs_assert(vector->elem_size == elem_size, ECS_INTERNAL_ERROR, NULL); - /* Broadcast to all tables since we need to register a ctor for every - * table that uses the component as itself, as predicate or as object. - * The latter is what makes selecting the right set of tables complex, - * as it depends on the predicate of a pair whether the object is used - * as the component type or not. - * A more selective approach requires a more expressive notification - * framework. */ - flecs_notify_tables(world, 0, &(ecs_table_event_t) { - .kind = EcsTableComponentInfo, - .component = component - }); + if (allocd) { + *allocd += vector->size * elem_size + offset; + } + if (used) { + *used += vector->count * elem_size; } } -bool ecs_component_has_actions( - const ecs_world_t *world, - ecs_entity_t component) +ecs_vector_t* _ecs_vector_copy( + const ecs_vector_t *src, + ecs_size_t elem_size, + int16_t offset) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - world = ecs_get_world(world); + if (!src) { + return NULL; + } - const ecs_type_info_t *c_info = flecs_get_c_info(world, component); - return (c_info != NULL) && c_info->lifecycle_set; + ecs_vector_t *dst = _ecs_vector_new(elem_size, offset, src->size); + ecs_os_memcpy(dst, src, offset + elem_size * src->count); + return dst; } -void ecs_atfini( - ecs_world_t *world, - ecs_fini_action_t action, - void *ctx) -{ - ecs_poly_assert(world, ecs_world_t); +/* The ratio used to determine whether the map should rehash. If + * (element_count * LOAD_FACTOR) > bucket_count, bucket count is increased. */ +#define LOAD_FACTOR (1.5f) +#define KEY_SIZE (ECS_SIZEOF(ecs_map_key_t)) +#define GET_ELEM(array, elem_size, index) \ + ECS_OFFSET(array, (elem_size) * (index)) - ecs_assert(action != NULL, ECS_INTERNAL_ERROR, NULL); +typedef struct ecs_bucket_t { + ecs_map_key_t *keys; /* Array with keys */ + void *payload; /* Payload array */ + int32_t count; /* Number of elements in bucket */ +} ecs_bucket_t; - ecs_action_elem_t *elem = ecs_vector_add(&world->fini_actions, - ecs_action_elem_t); - ecs_assert(elem != NULL, ECS_INTERNAL_ERROR, NULL); +struct ecs_map_t { + ecs_bucket_t *buckets; + int32_t elem_size; + int32_t bucket_count; + int32_t count; +}; - elem->action = action; - elem->ctx = ctx; +/* Get bucket count for number of elements */ +static +int32_t get_bucket_count( + int32_t element_count) +{ + return flecs_next_pow_of_2((int32_t)((float)element_count * LOAD_FACTOR)); } -void ecs_run_post_frame( - ecs_world_t *world, - ecs_fini_action_t action, - void *ctx) +/* Get bucket index for provided map key */ +static +int32_t get_bucket_id( + int32_t bucket_count, + ecs_map_key_t key) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(action != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_stage_t *stage = flecs_stage_from_world(&world); - - ecs_action_elem_t *elem = ecs_vector_add(&stage->post_frame_actions, - ecs_action_elem_t); - ecs_assert(elem != NULL, ECS_INTERNAL_ERROR, NULL); - - elem->action = action; - elem->ctx = ctx; + ecs_assert(bucket_count > 0, ECS_INTERNAL_ERROR, NULL); + int32_t result = (int32_t)(key & ((uint64_t)bucket_count - 1)); + ecs_assert(result < INT32_MAX, ECS_INTERNAL_ERROR, NULL); + return result; } -/* Unset data in tables */ +/* Get bucket for key */ static -void fini_unset_tables( - ecs_world_t *world) +ecs_bucket_t* get_bucket( + const ecs_map_t *map, + ecs_map_key_t key) { - ecs_sparse_t *tables = world->store.tables; - int32_t i, count = flecs_sparse_count(tables); - - for (i = 0; i < count; i ++) { - ecs_table_t *table = flecs_sparse_get_dense(tables, ecs_table_t, i); - flecs_table_remove_actions(world, table); + int32_t bucket_count = map->bucket_count; + if (!bucket_count) { + return NULL; } + + int32_t bucket_id = get_bucket_id(bucket_count, key); + ecs_assert(bucket_id < bucket_count, ECS_INTERNAL_ERROR, NULL); + + return &map->buckets[bucket_id]; } -/* Invoke fini actions */ +/* Ensure that map has at least new_count buckets */ static -void fini_actions( - ecs_world_t *world) +void ensure_buckets( + ecs_map_t *map, + int32_t new_count) { - ecs_vector_each(world->fini_actions, ecs_action_elem_t, elem, { - elem->action(world, elem->ctx); - }); + int32_t bucket_count = map->bucket_count; + new_count = flecs_next_pow_of_2(new_count); + if (new_count && new_count > bucket_count) { + map->buckets = ecs_os_realloc(map->buckets, new_count * ECS_SIZEOF(ecs_bucket_t)); + map->bucket_count = new_count; - ecs_vector_free(world->fini_actions); + ecs_os_memset( + ECS_OFFSET(map->buckets, bucket_count * ECS_SIZEOF(ecs_bucket_t)), + 0, (new_count - bucket_count) * ECS_SIZEOF(ecs_bucket_t)); + } } -/* Cleanup component lifecycle callbacks & systems */ +/* Free contents of bucket */ static -void fini_component_lifecycle( - ecs_world_t *world) +void clear_bucket( + ecs_bucket_t *bucket) { - flecs_sparse_free(world->type_info); + ecs_os_free(bucket->keys); + ecs_os_free(bucket->payload); + bucket->keys = NULL; + bucket->payload = NULL; + bucket->count = 0; } -/* Cleanup queries */ +/* Clear all buckets */ static -void fini_queries( - ecs_world_t *world) +void clear_buckets( + ecs_map_t *map) { - int32_t i, count = flecs_sparse_count(world->queries); + ecs_bucket_t *buckets = map->buckets; + int32_t i, count = map->bucket_count; for (i = 0; i < count; i ++) { - ecs_query_t *query = flecs_sparse_get_dense(world->queries, ecs_query_t, 0); - ecs_query_fini(query); + clear_bucket(&buckets[i]); } - flecs_sparse_free(world->queries); + ecs_os_free(buckets); + map->buckets = NULL; + map->bucket_count = 0; } +/* Find or create bucket for specified key */ static -void fini_observers( - ecs_world_t *world) +ecs_bucket_t* ensure_bucket( + ecs_map_t *map, + ecs_map_key_t key) { - flecs_sparse_free(world->observers); -} + if (!map->bucket_count) { + ensure_buckets(map, 2); + } -/* Cleanup stages */ -static -void fini_stages( - ecs_world_t *world) -{ - flecs_stage_deinit(world, &world->stage); - ecs_set_stages(world, 0); + int32_t bucket_id = get_bucket_id(map->bucket_count, key); + ecs_assert(bucket_id >= 0, ECS_INTERNAL_ERROR, NULL); + return &map->buckets[bucket_id]; } -/* Cleanup id index */ +/* Add element to bucket */ static -void fini_id_index( - ecs_world_t *world) +int32_t add_to_bucket( + ecs_bucket_t *bucket, + ecs_size_t elem_size, + ecs_map_key_t key, + const void *payload) { - ecs_map_iter_t it = ecs_map_iter(world->id_index); - ecs_id_record_t *idr; - while ((idr = ecs_map_next(&it, ecs_id_record_t, NULL))) { - ecs_table_cache_fini(&idr->cache); - ecs_map_free(idr->add_refs); - ecs_map_free(idr->remove_refs); + int32_t index = bucket->count ++; + int32_t bucket_count = index + 1; + + bucket->keys = ecs_os_realloc(bucket->keys, KEY_SIZE * bucket_count); + bucket->payload = ecs_os_realloc(bucket->payload, elem_size * bucket_count); + bucket->keys[index] = key; + + if (payload) { + void *elem = GET_ELEM(bucket->payload, elem_size, index); + ecs_os_memcpy(elem, payload, elem_size); } - ecs_map_free(world->id_index); + return index; } -/* Cleanup aliases & symbols */ +/* Remove element from bucket */ static -void fini_aliases( - ecs_hashmap_t *map) +void remove_from_bucket( + ecs_bucket_t *bucket, + ecs_size_t elem_size, + ecs_map_key_t key, + int32_t index) { - flecs_hashmap_iter_t it = flecs_hashmap_iter(*map); - ecs_hashed_string_t *key; - while (flecs_hashmap_next_w_key(&it, ecs_hashed_string_t, &key, ecs_entity_t)) { - ecs_os_free(key->value); - } + (void)key; + + ecs_assert(bucket->count != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(index < bucket->count, ECS_INTERNAL_ERROR, NULL); - flecs_hashmap_free(*map); + int32_t bucket_count = -- bucket->count; + + if (index != bucket->count) { + ecs_assert(key == bucket->keys[index], ECS_INTERNAL_ERROR, NULL); + bucket->keys[index] = bucket->keys[bucket_count]; + + ecs_map_key_t *elem = GET_ELEM(bucket->payload, elem_size, index); + ecs_map_key_t *last_elem = GET_ELEM(bucket->payload, elem_size, bucket->count); + + ecs_os_memcpy(elem, last_elem, elem_size); + } } -/* Cleanup misc structures */ +/* Get payload pointer for key from bucket */ static -void fini_misc( - ecs_world_t *world) +void* get_from_bucket( + ecs_bucket_t *bucket, + ecs_map_key_t key, + ecs_size_t elem_size) { - ecs_map_free(world->type_handles); - ecs_vector_free(world->fini_tasks); - monitors_fini(&world->monitors); + ecs_map_key_t *keys = bucket->keys; + int32_t i, count = bucket->count; + + for (i = 0; i < count; i ++) { + if (keys[i] == key) { + return GET_ELEM(bucket->payload, elem_size, i); + } + } + return NULL; } -/* The destroyer of worlds */ -int ecs_fini( - ecs_world_t *world) +/* Grow number of buckets */ +static +void rehash( + ecs_map_t *map, + int32_t bucket_count) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); - ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); - - ecs_trace("#[bold]shutting down world"); - ecs_log_push(); - - world->is_fini = true; - - /* Operations invoked during UnSet/OnRemove/destructors are deferred and - * will be discarded after world cleanup */ - ecs_defer_begin(world); - - /* Run UnSet/OnRemove actions for components while the store is still - * unmodified by cleanup. */ - fini_unset_tables(world); - - /* Run fini actions (simple callbacks ran when world is deleted) before - * destroying the storage */ - fini_actions(world); + ecs_assert(bucket_count != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(bucket_count > map->bucket_count, ECS_INTERNAL_ERROR, NULL); - /* This will destroy all entities and components. After this point no more - * user code is executed. */ - fini_store(world); + ecs_size_t elem_size = map->elem_size; - /* Purge deferred operations from the queue. This discards operations but - * makes sure that any resources in the queue are freed */ - flecs_defer_purge(world, &world->stage); + ensure_buckets(map, bucket_count); - /* Entity index is kept alive until this point so that user code can do - * validity checks on entity ids, even though after store cleanup the index - * will be empty, so all entity ids are invalid. */ - flecs_sparse_free(world->store.entity_index); + ecs_bucket_t *buckets = map->buckets; + ecs_assert(buckets != NULL, ECS_INTERNAL_ERROR, NULL); - if (world->locking_enabled) { - ecs_os_mutex_free(world->mutex); - } - - ecs_trace("table store deinitialized"); - - fini_stages(world); - - fini_component_lifecycle(world); + int32_t bucket_id; - fini_queries(world); + /* Iterate backwards as elements could otherwise be moved to existing + * buckets which could temporarily cause the number of elements in a + * bucket to exceed BUCKET_COUNT. */ + for (bucket_id = bucket_count - 1; bucket_id >= 0; bucket_id --) { + ecs_bucket_t *bucket = &buckets[bucket_id]; - fini_observers(world); + int i, count = bucket->count; + ecs_map_key_t *key_array = bucket->keys; + void *payload_array = bucket->payload; - fini_id_index(world); + for (i = 0; i < count; i ++) { + ecs_map_key_t key = key_array[i]; + void *elem = GET_ELEM(payload_array, elem_size, i); + int32_t new_bucket_id = get_bucket_id(bucket_count, key); - flecs_observable_fini(&world->observable); + if (new_bucket_id != bucket_id) { + ecs_bucket_t *new_bucket = &buckets[new_bucket_id]; - flecs_sparse_free(world->triggers); + add_to_bucket(new_bucket, elem_size, key, elem); + remove_from_bucket(bucket, elem_size, key, i); - fini_aliases(&world->aliases); - - fini_aliases(&world->symbols); - - fini_misc(world); + count --; + i --; + } + } - flecs_increase_timer_resolution(0); + if (!bucket->count) { + clear_bucket(bucket); + } + } +} - /* End of the world */ - ecs_poly_free(world, ecs_world_t); +ecs_map_t* _ecs_map_new( + ecs_size_t elem_size, + int32_t element_count) +{ + ecs_map_t *result = ecs_os_calloc(ECS_SIZEOF(ecs_map_t) * 1); + ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); - ecs_os_fini(); + int32_t bucket_count = get_bucket_count(element_count); - ecs_trace("world destroyed, bye!"); - ecs_log_pop(); + result->count = 0; + result->elem_size = elem_size; - return 0; -} + ensure_buckets(result, bucket_count); -void ecs_dim( - ecs_world_t *world, - int32_t entity_count) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_eis_set_size(world, entity_count + ECS_HI_COMPONENT_ID); + return result; } -void flecs_eval_component_monitors( - ecs_world_t *world) +void ecs_map_free( + ecs_map_t *map) { - ecs_poly_assert(world, ecs_world_t); - eval_component_monitor(world); + if (map) { + clear_buckets(map); + ecs_os_free(map); + } } -void ecs_measure_frame_time( - ecs_world_t *world, - bool enable) +void* _ecs_map_get( + const ecs_map_t *map, + ecs_size_t elem_size, + ecs_map_key_t key) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(ecs_os_has_time(), ECS_MISSING_OS_API, NULL); + (void)elem_size; - if (world->stats.target_fps == 0.0f || enable) { - world->measure_frame_time = enable; + if (!map) { + return NULL; } -} -void ecs_measure_system_time( - ecs_world_t *world, - bool enable) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(ecs_os_has_time(), ECS_MISSING_OS_API, NULL); - world->measure_system_time = enable; -} - -/* Increase timer resolution based on target fps */ -static -void set_timer_resolution( - FLECS_FLOAT fps) -{ - if(fps >= 60.0f) flecs_increase_timer_resolution(1); - else flecs_increase_timer_resolution(0); -} + ecs_assert(elem_size == map->elem_size, ECS_INVALID_PARAMETER, NULL); -void ecs_set_target_fps( - ecs_world_t *world, - FLECS_FLOAT fps) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(ecs_os_has_time(), ECS_MISSING_OS_API, NULL); + ecs_bucket_t * bucket = get_bucket(map, key); + if (!bucket) { + return NULL; + } - ecs_measure_frame_time(world, true); - world->stats.target_fps = fps; - set_timer_resolution(fps); + return get_from_bucket(bucket, key, elem_size); } -void* ecs_get_context( - const ecs_world_t *world) +void* _ecs_map_get_ptr( + const ecs_map_t *map, + ecs_map_key_t key) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - world = ecs_get_world(world); - return world->context; -} + void* ptr_ptr = _ecs_map_get(map, ECS_SIZEOF(void*), key); -void ecs_set_context( - ecs_world_t *world, - void *context) -{ - ecs_poly_assert(world, ecs_world_t); - world->context = context; + if (ptr_ptr) { + return *(void**)ptr_ptr; + } else { + return NULL; + } } -void ecs_set_entity_range( - ecs_world_t *world, - ecs_entity_t id_start, - ecs_entity_t id_end) +bool ecs_map_has( + const ecs_map_t *map, + ecs_map_key_t key) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(!id_end || id_end > id_start, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!id_end || id_end > world->stats.last_id, ECS_INVALID_PARAMETER, NULL); + if (!map) { + return false; + } - if (world->stats.last_id < id_start) { - world->stats.last_id = id_start - 1; + ecs_bucket_t * bucket = get_bucket(map, key); + if (!bucket) { + return false; } - world->stats.min_id = id_start; - world->stats.max_id = id_end; + return get_from_bucket(bucket, key, 0) != NULL; } -bool ecs_enable_range_check( - ecs_world_t *world, - bool enable) +void* _ecs_map_ensure( + ecs_map_t *map, + ecs_size_t elem_size, + ecs_map_key_t key) { - ecs_poly_assert(world, ecs_world_t); - bool old_value = world->range_check_enabled; - world->range_check_enabled = enable; - return old_value; -} + void *result = _ecs_map_get(map, elem_size, key); + if (!result) { + result = _ecs_map_set(map, elem_size, key, NULL); + ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_os_memset(result, 0, elem_size); + } -int32_t ecs_get_threads( - ecs_world_t *world) -{ - return ecs_vector_count(world->worker_stages); + return result; } -bool ecs_enable_locking( - ecs_world_t *world, - bool enable) +void* _ecs_map_set( + ecs_map_t *map, + ecs_size_t elem_size, + ecs_map_key_t key, + const void *payload) { - ecs_poly_assert(world, ecs_world_t); + ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(elem_size == map->elem_size, ECS_INVALID_PARAMETER, NULL); - if (enable) { - if (!world->locking_enabled) { - world->mutex = ecs_os_mutex_new(); - world->thr_sync = ecs_os_mutex_new(); - world->thr_cond = ecs_os_cond_new(); - } + ecs_bucket_t *bucket = ensure_bucket(map, key); + ecs_assert(bucket != NULL, ECS_INTERNAL_ERROR, NULL); + + void *elem = get_from_bucket(bucket, key, elem_size); + if (!elem) { + int32_t index = add_to_bucket(bucket, elem_size, key, payload); + + int32_t map_count = ++map->count; + int32_t target_bucket_count = get_bucket_count(map_count); + int32_t map_bucket_count = map->bucket_count; + + if (target_bucket_count > map_bucket_count) { + rehash(map, target_bucket_count); + bucket = ensure_bucket(map, key); + return get_from_bucket(bucket, key, elem_size); + } else { + return GET_ELEM(bucket->payload, elem_size, index); + } } else { - if (world->locking_enabled) { - ecs_os_mutex_free(world->mutex); - ecs_os_mutex_free(world->thr_sync); - ecs_os_cond_free(world->thr_cond); + if (payload) { + ecs_os_memcpy(elem, payload, elem_size); } + return elem; } +} - bool old = world->locking_enabled; - world->locking_enabled = enable; - return old; +void ecs_map_remove( + ecs_map_t *map, + ecs_map_key_t key) +{ + ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); + + ecs_bucket_t * bucket = get_bucket(map, key); + if (!bucket) { + return; + } + + int32_t i, bucket_count = bucket->count; + for (i = 0; i < bucket_count; i ++) { + if (bucket->keys[i] == key) { + remove_from_bucket(bucket, map->elem_size, key, i); + map->count --; + } + } } -void ecs_lock( - ecs_world_t *world) +int32_t ecs_map_count( + const ecs_map_t *map) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); - ecs_os_mutex_lock(world->mutex); + return map ? map->count : 0; } -void ecs_unlock( - ecs_world_t *world) +int32_t ecs_map_bucket_count( + const ecs_map_t *map) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); - ecs_os_mutex_unlock(world->mutex); + return map ? map->bucket_count : 0; } -void ecs_begin_wait( - ecs_world_t *world) +void ecs_map_clear( + ecs_map_t *map) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); - ecs_os_mutex_lock(world->thr_sync); - ecs_os_cond_wait(world->thr_cond, world->thr_sync); + ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); + clear_buckets(map); + map->count = 0; } -void ecs_end_wait( - ecs_world_t *world) +ecs_map_iter_t ecs_map_iter( + const ecs_map_t *map) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(world->locking_enabled, ECS_INVALID_PARAMETER, NULL); - ecs_os_mutex_unlock(world->thr_sync); + return (ecs_map_iter_t){ + .map = map, + .bucket = NULL, + .bucket_index = 0, + .element_index = 0 + }; } -const ecs_type_info_t* flecs_get_c_info( - const ecs_world_t *world, - ecs_entity_t component) +void* _ecs_map_next( + ecs_map_iter_t *iter, + ecs_size_t elem_size, + ecs_map_key_t *key_out) { - ecs_poly_assert(world, ecs_world_t); + const ecs_map_t *map = iter->map; + if (!map) { + return NULL; + } + + ecs_assert(!elem_size || elem_size == map->elem_size, ECS_INVALID_PARAMETER, NULL); + + ecs_bucket_t *bucket = iter->bucket; + int32_t element_index = iter->element_index; + elem_size = map->elem_size; - ecs_assert(component != 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!(component & ECS_ROLE_MASK), ECS_INTERNAL_ERROR, NULL); + do { + if (!bucket) { + int32_t bucket_index = iter->bucket_index; + ecs_bucket_t *buckets = map->buckets; + if (bucket_index < map->bucket_count) { + bucket = &buckets[bucket_index]; + iter->bucket = bucket; - return flecs_sparse_get(world->type_info, ecs_type_info_t, component); + element_index = 0; + iter->element_index = 0; + } else { + return NULL; + } + } + + if (element_index < bucket->count) { + iter->element_index = element_index + 1; + break; + } else { + bucket = NULL; + iter->bucket_index ++; + } + } while (true); + + if (key_out) { + *key_out = bucket->keys[element_index]; + } + + return GET_ELEM(bucket->payload, elem_size, element_index); } -ecs_type_info_t* flecs_get_or_create_c_info( - ecs_world_t *world, - ecs_entity_t component) +void* _ecs_map_next_ptr( + ecs_map_iter_t *iter, + ecs_map_key_t *key_out) { - ecs_poly_assert(world, ecs_world_t); - - const ecs_type_info_t *c_info = flecs_get_c_info(world, component); - ecs_type_info_t *c_info_mut = NULL; - if (!c_info) { - c_info_mut = flecs_sparse_ensure( - world->type_info, ecs_type_info_t, component); - ecs_assert(c_info_mut != NULL, ECS_INTERNAL_ERROR, NULL); + void *result = _ecs_map_next(iter, ECS_SIZEOF(void*), key_out); + if (result) { + return *(void**)result; } else { - c_info_mut = (ecs_type_info_t*)c_info; + return NULL; } - - return c_info_mut; } -static -FLECS_FLOAT insert_sleep( - ecs_world_t *world, - ecs_time_t *stop) +void ecs_map_grow( + ecs_map_t *map, + int32_t element_count) { - ecs_poly_assert(world, ecs_world_t); - - ecs_time_t start = *stop; - FLECS_FLOAT delta_time = (FLECS_FLOAT)ecs_time_measure(stop); + ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t target_count = map->count + element_count; + int32_t bucket_count = get_bucket_count(target_count); - if (world->stats.target_fps == (FLECS_FLOAT)0.0) { - return delta_time; + if (bucket_count > map->bucket_count) { + rehash(map, bucket_count); } +} - FLECS_FLOAT target_delta_time = - ((FLECS_FLOAT)1.0 / (FLECS_FLOAT)world->stats.target_fps); +void ecs_map_set_size( + ecs_map_t *map, + int32_t element_count) +{ + ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t bucket_count = get_bucket_count(element_count); - /* Calculate the time we need to sleep by taking the measured delta from the - * previous frame, and subtracting it from target_delta_time. */ - FLECS_FLOAT sleep = target_delta_time - delta_time; + if (bucket_count) { + rehash(map, bucket_count); + } +} - /* Pick a sleep interval that is 20 times lower than the time one frame - * should take. This means that this function at most iterates 20 times in - * a busy loop */ - FLECS_FLOAT sleep_time = sleep / (FLECS_FLOAT)4.0; +ecs_map_t* ecs_map_copy( + ecs_map_t *map) +{ + if (!map) { + return NULL; + } - do { - /* Only call sleep when sleep_time is not 0. On some platforms, even - * a sleep with a timeout of 0 can cause stutter. */ - if (sleep_time != 0) { - ecs_sleepf((double)sleep_time); - } + ecs_size_t elem_size = map->elem_size; + ecs_map_t *result = _ecs_map_new(map->elem_size, ecs_map_count(map)); - ecs_time_t now = start; - delta_time = (FLECS_FLOAT)ecs_time_measure(&now); - } while ((target_delta_time - delta_time) > - (sleep_time / (FLECS_FLOAT)2.0)); + ecs_map_iter_t it = ecs_map_iter(map); + ecs_map_key_t key; + void *ptr; + while ((ptr = _ecs_map_next(&it, elem_size, &key))) { + _ecs_map_set(result, elem_size, key, ptr); + } - return delta_time; + return result; } -static -FLECS_FLOAT start_measure_frame( - ecs_world_t *world, - FLECS_FLOAT user_delta_time) +void ecs_map_memory( + ecs_map_t *map, + int32_t *allocd, + int32_t *used) { - ecs_poly_assert(world, ecs_world_t); - - FLECS_FLOAT delta_time = 0; + ecs_assert(map != NULL, ECS_INVALID_PARAMETER, NULL); - if (world->measure_frame_time || (user_delta_time == 0)) { - ecs_time_t t = world->frame_start_time; - do { - if (world->frame_start_time.sec) { - delta_time = insert_sleep(world, &t); + if (used) { + *used = map->count * map->elem_size; + } - ecs_time_measure(&t); - } else { - ecs_time_measure(&t); - if (world->stats.target_fps != 0) { - delta_time = (FLECS_FLOAT)1.0 / world->stats.target_fps; - } else { - /* Best guess */ - delta_time = (FLECS_FLOAT)1.0 / (FLECS_FLOAT)60.0; - } - } - - /* Keep trying while delta_time is zero */ - } while (delta_time == 0); + if (allocd) { + *allocd += ECS_SIZEOF(ecs_map_t); - world->frame_start_time = t; + int i, bucket_count = map->bucket_count; + for (i = 0; i < bucket_count; i ++) { + ecs_bucket_t *bucket = &map->buckets[i]; + *allocd += KEY_SIZE * bucket->count; + *allocd += map->elem_size * bucket->count; + } - /* Keep track of total time passed in world */ - world->stats.world_time_total_raw += (FLECS_FLOAT)delta_time; + *allocd += ECS_SIZEOF(ecs_bucket_t) * bucket_count; } - - return (FLECS_FLOAT)delta_time; } +typedef struct ecs_hm_bucket_t { + ecs_vector_t *keys; + ecs_vector_t *values; +} ecs_hm_bucket_t; + static -void stop_measure_frame( - ecs_world_t* world) +int32_t find_key( + ecs_hashmap_t map, + ecs_vector_t *keys, + ecs_size_t key_size, + const void *key) { - ecs_poly_assert(world, ecs_world_t); - - if (world->measure_frame_time) { - ecs_time_t t = world->frame_start_time; - world->stats.frame_time_total += (FLECS_FLOAT)ecs_time_measure(&t); + int32_t i, count = ecs_vector_count(keys); + void *key_array = ecs_vector_first_t(keys, key_size, 8); + for (i = 0; i < count; i ++) { + void *key_ptr = ECS_OFFSET(key_array, key_size * i); + if (map.compare(key_ptr, key) == 0) { + return i; + } } + return -1; } -FLECS_FLOAT ecs_frame_begin( - ecs_world_t *world, - FLECS_FLOAT user_delta_time) +ecs_hashmap_t _flecs_hashmap_new( + ecs_size_t key_size, + ecs_size_t value_size, + ecs_hash_value_action_t hash, + ecs_compare_action_t compare) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(world->is_readonly == false, ECS_INVALID_OPERATION, NULL); - - ecs_assert(user_delta_time != 0 || ecs_os_has_time(), ECS_MISSING_OS_API, "get_time"); + return (ecs_hashmap_t){ + .key_size = key_size, + .value_size = value_size, + .compare = compare, + .hash = hash, + .impl = ecs_map_new(ecs_hm_bucket_t, 0) + }; +} - if (world->locking_enabled) { - ecs_lock(world); +void flecs_hashmap_free( + ecs_hashmap_t map) +{ + ecs_map_iter_t it = ecs_map_iter(map.impl); + ecs_hm_bucket_t *bucket; + while ((bucket = ecs_map_next(&it, ecs_hm_bucket_t, NULL))) { + ecs_vector_free(bucket->keys); + ecs_vector_free(bucket->values); } - /* Start measuring total frame time */ - FLECS_FLOAT delta_time = start_measure_frame(world, user_delta_time); - if (user_delta_time == 0) { - user_delta_time = delta_time; - } - - world->stats.delta_time_raw = user_delta_time; - world->stats.delta_time = user_delta_time * world->stats.time_scale; - - /* Keep track of total scaled time passed in world */ - world->stats.world_time_total += world->stats.delta_time; - - flecs_eval_component_monitors(world); - - return world->stats.delta_time; + ecs_map_free(map.impl); } -void ecs_frame_end( - ecs_world_t *world) +ecs_hashmap_t flecs_hashmap_copy( + const ecs_hashmap_t src) { - ecs_poly_assert(world, ecs_world_t); - ecs_assert(world->is_readonly == false, ECS_INVALID_OPERATION, NULL); + ecs_hashmap_t result = src; + result.impl = ecs_map_copy(result.impl); - world->stats.frame_count_total ++; + ecs_map_iter_t it = ecs_map_iter(result.impl); + ecs_hm_bucket_t *bucket; + while ((bucket = ecs_map_next(&it, ecs_hm_bucket_t, NULL))) { + bucket->keys = ecs_vector_copy_t(bucket->keys, result.key_size, 8); + bucket->values = ecs_vector_copy_t(bucket->values, result.value_size, 8); + } - ecs_vector_each(world->worker_stages, ecs_stage_t, stage, { - flecs_stage_merge_post_frame(world, stage); - }); + return result; +} - if (world->locking_enabled) { - ecs_unlock(world); +void* _flecs_hashmap_get( + const ecs_hashmap_t map, + ecs_size_t key_size, + const void *key, + ecs_size_t value_size) +{ + ecs_assert(map.key_size == key_size, ECS_INVALID_PARAMETER, NULL); + ecs_assert(map.value_size == value_size, ECS_INVALID_PARAMETER, NULL); - ecs_os_mutex_lock(world->thr_sync); - ecs_os_cond_broadcast(world->thr_cond); - ecs_os_mutex_unlock(world->thr_sync); + uint64_t hash = map.hash(key); + ecs_hm_bucket_t *bucket = ecs_map_get(map.impl, ecs_hm_bucket_t, hash); + if (!bucket) { + return NULL; } - stop_measure_frame(world); -} + int32_t index = find_key(map, bucket->keys, key_size, key); + if (index == -1) { + return NULL; + } -const ecs_world_info_t* ecs_get_world_info( - const ecs_world_t *world) -{ - world = ecs_get_world(world); - return &world->stats; + return ecs_vector_get_t(bucket->values, value_size, 8, index); } -void flecs_notify_queries( - ecs_world_t *world, - ecs_query_event_t *event) +flecs_hashmap_result_t _flecs_hashmap_ensure( + const ecs_hashmap_t map, + ecs_size_t key_size, + void *key, + ecs_size_t value_size) { - ecs_poly_assert(world, ecs_world_t); + ecs_assert(map.key_size == key_size, ECS_INVALID_PARAMETER, NULL); + ecs_assert(map.value_size == value_size, ECS_INVALID_PARAMETER, NULL); - int32_t i, count = flecs_sparse_count(world->queries); - for (i = 0; i < count; i ++) { - ecs_query_t *query = flecs_sparse_get_dense( - world->queries, ecs_query_t, i); - if (query->flags & EcsQueryIsSubquery) { - continue; - } - - flecs_query_notify(world, query, event); - } -} - -void flecs_delete_table( - ecs_world_t *world, - ecs_table_t *table) -{ - ecs_poly_assert(world, ecs_world_t); - - /* Notify queries that table is to be removed */ - flecs_notify_queries( - world, &(ecs_query_event_t){ - .kind = EcsQueryTableUnmatch, - .table = table - }); + uint64_t hash = map.hash(key); + ecs_hm_bucket_t *bucket = ecs_map_ensure(map.impl, ecs_hm_bucket_t, hash); + ecs_assert(bucket != NULL, ECS_INTERNAL_ERROR, NULL); - uint64_t id = table->id; + void *value_ptr, *key_ptr; - /* Free resources associated with table */ - flecs_table_free(world, table); - flecs_table_free_type(table); + ecs_vector_t *keys = bucket->keys; + if (!keys) { + bucket->keys = ecs_vector_new_t(key_size, 8, 1); + bucket->values = ecs_vector_new_t(value_size, 8, 1); + key_ptr = ecs_vector_add_t(&bucket->keys, key_size, 8); + ecs_os_memcpy(key_ptr, key, key_size); + value_ptr = ecs_vector_add_t(&bucket->values, value_size, 8); + ecs_os_memset(value_ptr, 0, value_size); + } else { + int32_t index = find_key(map, keys, key_size, key); + if (index == -1) { + key_ptr = ecs_vector_add_t(&bucket->keys, key_size, 8); + ecs_os_memcpy(key_ptr, key, key_size); + value_ptr = ecs_vector_add_t(&bucket->values, value_size, 8); + ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_os_memset(value_ptr, 0, value_size); + } else { + key_ptr = ecs_vector_get_t(bucket->keys, key_size, 8, index); + value_ptr = ecs_vector_get_t(bucket->values, value_size, 8, index); + } + } - /* Remove table from sparse set */ - ecs_assert(id != 0, ECS_INTERNAL_ERROR, NULL); - flecs_sparse_remove(world->store.tables, id); + return (flecs_hashmap_result_t){ + .key = key_ptr, + .value = value_ptr, + .hash = hash + }; } -static -void register_table( - ecs_world_t *world, - ecs_table_t *table, - ecs_id_t id, - int32_t column) +void _flecs_hashmap_set( + const ecs_hashmap_t map, + ecs_size_t key_size, + void *key, + ecs_size_t value_size, + const void *value) { - ecs_id_record_t *idr = flecs_ensure_id_record(world, id); - ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL); - - ecs_table_record_t *tr = ecs_table_cache_get( - &idr->cache, ecs_table_record_t, table); - if (tr) { - /* Tables can be registered multiple times for wildcard caches */ - tr->count ++; - } else { - tr = ecs_table_cache_insert(&idr->cache, ecs_table_record_t, table); - tr->table = table; - tr->column = column; - tr->count = 1; - } - - /* Set flags if triggers are registered for table */ - if (flecs_triggers_for_id(world, id, EcsOnAdd)) { - table->flags |= EcsTableHasOnAdd; - } - if (flecs_triggers_for_id(world, id, EcsOnRemove)) { - table->flags |= EcsTableHasOnRemove; - } - if (flecs_triggers_for_id(world, id, EcsOnSet)) { - table->flags |= EcsTableHasOnSet; - } - if (flecs_triggers_for_id(world, id, EcsUnSet)) { - table->flags |= EcsTableHasUnSet; - } + void *value_ptr = _flecs_hashmap_ensure(map, key_size, key, value_size).value; + ecs_assert(value_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_os_memcpy(value_ptr, value, value_size); } -static -void unregister_table( - ecs_world_t *world, - ecs_table_t *table, - ecs_id_t id, - int32_t column) +void _flecs_hashmap_remove_w_hash( + const ecs_hashmap_t map, + ecs_size_t key_size, + const void *key, + ecs_size_t value_size, + uint64_t hash) { - (void)column; + ecs_hm_bucket_t *bucket = ecs_map_get(map.impl, ecs_hm_bucket_t, hash); + if (!bucket) { + return; + } - ecs_id_record_t *idr = flecs_get_id_record(world, id); - if (!idr) { + int32_t index = find_key(map, bucket->keys, key_size, key); + if (index == -1) { return; } - ecs_table_cache_remove(&idr->cache, ecs_table_record_t, table); + ecs_vector_remove_t(bucket->keys, key_size, 8, index); + ecs_vector_remove_t(bucket->values, value_size, 8, index); - if (ecs_table_cache_is_empty(&idr->cache)) { - flecs_clear_id_record(world, id); + if (!ecs_vector_count(bucket->keys)) { + ecs_vector_free(bucket->keys); + ecs_vector_free(bucket->values); + ecs_map_remove(map.impl, hash); } } -static -void set_empty( - ecs_world_t *world, - ecs_table_t *table, - ecs_id_t id, - int32_t column) +void _flecs_hashmap_remove( + const ecs_hashmap_t map, + ecs_size_t key_size, + const void *key, + ecs_size_t value_size) { - (void)column; + ecs_assert(map.key_size == key_size, ECS_INVALID_PARAMETER, NULL); + ecs_assert(map.value_size == value_size, ECS_INVALID_PARAMETER, NULL); - ecs_id_record_t *idr = flecs_get_id_record(world, id); - if (idr) { - ecs_table_cache_set_empty(&idr->cache, table, - ecs_table_count(table) == 0); - } + uint64_t hash = map.hash(key); + _flecs_hashmap_remove_w_hash(map, key_size, key, value_size, hash); } -static -void for_each_id( - ecs_world_t *world, - ecs_table_t *table, - void(*action)(ecs_world_t*, ecs_table_t*, ecs_id_t, int32_t), - bool set_watch) +flecs_hashmap_iter_t flecs_hashmap_iter( + ecs_hashmap_t map) { - int32_t i, count = ecs_vector_count(table->type); - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - bool has_childof = false; - - for (i = 0; i < count; i ++) { - ecs_entity_t id = ids[i]; - - /* This check ensures that legacy CHILDOF works */ - if (ECS_HAS_RELATION(id, EcsChildOf)) { - has_childof = true; - } - - action(world, table, ecs_strip_generation(id), i); - action(world, table, EcsWildcard, i); - - if (ECS_HAS_ROLE(id, PAIR)) { - ecs_entity_t pred_w_wildcard = ecs_pair( - ECS_PAIR_RELATION(id), EcsWildcard); - action(world, table, pred_w_wildcard, i); - - ecs_entity_t obj_w_wildcard = ecs_pair( - EcsWildcard, ECS_PAIR_OBJECT(id)); - action(world, table, obj_w_wildcard, i); - - ecs_entity_t all_wildcard = ecs_pair(EcsWildcard, EcsWildcard); - action(world, table, all_wildcard, i); - - if (set_watch) { - flecs_set_watch(world, ecs_pair_relation(world, id)); - flecs_set_watch(world, ecs_pair_object(world, id)); - } - } else { - if (id & ECS_ROLE_MASK) { - id &= ECS_COMPONENT_MASK; - action(world, table, ecs_pair(id, EcsWildcard), i); - } + return (flecs_hashmap_iter_t){ + .it = ecs_map_iter(map.impl) + }; +} - if (set_watch) { - flecs_set_watch(world, id); - } +void* _flecs_hashmap_next( + flecs_hashmap_iter_t *it, + ecs_size_t key_size, + void *key_out, + ecs_size_t value_size) +{ + int32_t index = ++ it->index; + ecs_hm_bucket_t *bucket = it->bucket; + while (!bucket || it->index >= ecs_vector_count(bucket->keys)) { + bucket = it->bucket = ecs_map_next(&it->it, ecs_hm_bucket_t, NULL); + if (!bucket) { + return NULL; } + index = it->index = 0; } - if (!has_childof) { - action(world, table, ecs_pair(EcsChildOf, 0), 0); + if (key_out) { + *(void**)key_out = ecs_vector_get_t(bucket->keys, key_size, 8, index); } + + return ecs_vector_get_t(bucket->values, value_size, 8, index); } -void flecs_register_table( - ecs_world_t *world, - ecs_table_t *table) +/* Add an extra element to the buffer */ +static +void ecs_strbuf_grow( + ecs_strbuf_t *b) { - for_each_id(world, table, register_table, true); + /* Allocate new element */ + ecs_strbuf_element_embedded *e = ecs_os_malloc(sizeof(ecs_strbuf_element_embedded)); + b->size += b->current->pos; + b->current->next = (ecs_strbuf_element*)e; + b->current = (ecs_strbuf_element*)e; + b->elementCount ++; + e->super.buffer_embedded = true; + e->super.buf = e->buf; + e->super.pos = 0; + e->super.next = NULL; } -void flecs_unregister_table( - ecs_world_t *world, - ecs_table_t *table) +/* Add an extra dynamic element */ +static +void ecs_strbuf_grow_str( + ecs_strbuf_t *b, + char *str, + char *alloc_str, + int32_t size) { - for_each_id(world, table, unregister_table, false); - - ecs_ids_t key = { - .array = ecs_vector_first(table->type, ecs_id_t), - .count = ecs_vector_count(table->type) - }; - - flecs_hashmap_remove(world->store.table_map, &key, ecs_table_t*); + /* Allocate new element */ + ecs_strbuf_element_str *e = ecs_os_malloc(sizeof(ecs_strbuf_element_str)); + b->size += b->current->pos; + b->current->next = (ecs_strbuf_element*)e; + b->current = (ecs_strbuf_element*)e; + b->elementCount ++; + e->super.buffer_embedded = false; + e->super.pos = size ? size : (int32_t)ecs_os_strlen(str); + e->super.next = NULL; + e->super.buf = str; + e->alloc_str = alloc_str; } -void flecs_table_set_empty( - ecs_world_t *world, - ecs_table_t *table) +static +char* ecs_strbuf_ptr( + ecs_strbuf_t *b) { - for_each_id(world, table, set_empty, false); + if (b->buf) { + return &b->buf[b->current->pos]; + } else { + return &b->current->buf[b->current->pos]; + } } -ecs_id_record_t* flecs_ensure_id_record( - ecs_world_t *world, - ecs_id_t id) +/* Compute the amount of space left in the current element */ +static +int32_t ecs_strbuf_memLeftInCurrentElement( + ecs_strbuf_t *b) { - ecs_id_record_t *idr = ecs_map_ensure(world->id_index, ecs_id_record_t, - ecs_strip_generation(id)); - - if (!ecs_table_cache_is_initialized(&idr->cache)) { - ecs_table_cache_init(&idr->cache, ecs_table_record_t, world, NULL); + if (b->current->buffer_embedded) { + return ECS_STRBUF_ELEMENT_SIZE - b->current->pos; + } else { + return 0; } - - return idr; } -ecs_id_record_t* flecs_get_id_record( - const ecs_world_t *world, - ecs_id_t id) +/* Compute the amount of space left */ +static +int32_t ecs_strbuf_memLeft( + ecs_strbuf_t *b) { - return ecs_map_get(world->id_index, ecs_id_record_t, - ecs_strip_generation(id)); + if (b->max) { + return b->max - b->size - b->current->pos; + } else { + return INT_MAX; + } } -ecs_table_record_t* flecs_get_table_record( - const ecs_world_t *world, - const ecs_table_t *table, - ecs_id_t id) +static +void ecs_strbuf_init( + ecs_strbuf_t *b) { - ecs_id_record_t* idr = flecs_get_id_record(world, id); - if (!idr) { - return NULL; + /* Initialize buffer structure only once */ + if (!b->elementCount) { + b->size = 0; + b->firstElement.super.next = NULL; + b->firstElement.super.pos = 0; + b->firstElement.super.buffer_embedded = true; + b->firstElement.super.buf = b->firstElement.buf; + b->elementCount ++; + b->current = (ecs_strbuf_element*)&b->firstElement; } - - return ecs_table_cache_get(&idr->cache, ecs_table_record_t, table); } -void flecs_register_add_ref( - ecs_world_t *world, - const ecs_table_t *table, - ecs_id_t id) +/* Quick custom function to copy a maxium number of characters and + * simultaneously determine length of source string. */ +static +int32_t fast_strncpy( + char * dst, + const char * src, + int n_cpy, + int n) { - ecs_id_record_t *idr = flecs_ensure_id_record(world, id); - if (!idr->add_refs) { - idr->add_refs = ecs_map_new(ecs_table_t*, 1); - } + ecs_assert(n_cpy >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(n >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_table_t **ptr = ecs_map_ensure( - idr->add_refs, ecs_table_t*, table->id); - ptr[0] = (ecs_table_t*)table; -} + const char *ptr, *orig = src; + char ch; -void flecs_register_remove_ref( - ecs_world_t *world, - const ecs_table_t *table, - ecs_id_t id) -{ - ecs_id_record_t *idr = flecs_ensure_id_record(world, id); - if (!idr->remove_refs) { - idr->remove_refs = ecs_map_new(ecs_table_t*, 1); + for (ptr = src; (ptr - orig < n) && (ch = *ptr); ptr ++) { + if (ptr - orig < n_cpy) { + *dst = ch; + dst ++; + } } - ecs_table_t **ptr = ecs_map_ensure( - idr->remove_refs, ecs_table_t*, table->id); - ptr[0] = (ecs_table_t*)table; + ecs_assert(ptr - orig < INT32_MAX, ECS_INTERNAL_ERROR, NULL); + + return (int32_t)(ptr - orig); } -void flecs_clear_id_record( - ecs_world_t *world, - ecs_id_t id) +/* Append a format string to a buffer */ +static +bool ecs_strbuf_vappend_intern( + ecs_strbuf_t *b, + const char* str, + va_list args) { - if (world->is_fini) { - return; + bool result = true; + va_list arg_cpy; + + if (!str) { + return result; } - - ecs_id_record_t *idr_ptr = flecs_get_id_record(world, id); - if (!idr_ptr) { - return; + + ecs_strbuf_init(b); + + int32_t memLeftInElement = ecs_strbuf_memLeftInCurrentElement(b); + int32_t memLeft = ecs_strbuf_memLeft(b); + + if (!memLeft) { + return false; } - ecs_id_record_t idr = *idr_ptr; + /* Compute the memory required to add the string to the buffer. If user + * provided buffer, use space left in buffer, otherwise use space left in + * current element. */ + int32_t max_copy = b->buf ? memLeft : memLeftInElement; + int32_t memRequired; - ecs_map_remove(world->id_index, ecs_strip_generation(id)); + va_copy(arg_cpy, args); + memRequired = vsnprintf( + ecs_strbuf_ptr(b), (size_t)(max_copy + 1), str, args); - ecs_table_cache_fini_delete_all(world, &idr.cache, ecs_table_record_t); + ecs_assert(memRequired != -1, ECS_INTERNAL_ERROR, NULL); - /* Remove add & remove references to id from tables */ - ecs_table_t *table; - ecs_map_iter_t it = ecs_map_iter(idr.add_refs); - while ((table = ecs_map_next_ptr(&it, ecs_table_t*, NULL))) { - flecs_table_clear_add_edge(table, id); - } + if (memRequired <= memLeftInElement) { + /* Element was large enough to fit string */ + b->current->pos += memRequired; + } else if ((memRequired - memLeftInElement) < memLeft) { + /* If string is a format string, a new buffer of size memRequired is + * needed to re-evaluate the format string and only use the part that + * wasn't already copied to the previous element */ + if (memRequired <= ECS_STRBUF_ELEMENT_SIZE) { + /* Resulting string fits in standard-size buffer. Note that the + * entire string needs to fit, not just the remainder, as the + * format string cannot be partially evaluated */ + ecs_strbuf_grow(b); - it = ecs_map_iter(idr.remove_refs); - while ((table = ecs_map_next_ptr(&it, ecs_table_t*, NULL))) { - flecs_table_clear_remove_edge(table, id); - } + /* Copy entire string to new buffer */ + ecs_os_vsprintf(ecs_strbuf_ptr(b), str, arg_cpy); - ecs_map_free(idr.add_refs); - ecs_map_free(idr.remove_refs); -} + /* Ignore the part of the string that was copied into the + * previous buffer. The string copied into the new buffer could + * be memmoved so that only the remainder is left, but that is + * most likely more expensive than just keeping the entire + * string. */ -const ecs_table_record_t* flecs_id_record_table( - ecs_id_record_t *idr, - ecs_table_t *table) -{ - if (!idr) { - return NULL; + /* Update position in buffer */ + b->current->pos += memRequired; + } else { + /* Resulting string does not fit in standard-size buffer. + * Allocate a new buffer that can hold the entire string. */ + char *dst = ecs_os_malloc(memRequired + 1); + ecs_os_vsprintf(dst, str, arg_cpy); + ecs_strbuf_grow_str(b, dst, dst, memRequired); + } + } else { + /* Buffer max has been reached */ + result = false; } - return ecs_table_cache_get(&idr->cache, ecs_table_record_t, table); -} -const ecs_table_record_t* flecs_id_record_tables( - const ecs_id_record_t *idr) -{ - if (!idr) { - return NULL; - } - return ecs_table_cache_tables(&idr->cache, ecs_table_record_t); -} + va_end(arg_cpy); -const ecs_table_record_t* flecs_id_record_empty_tables( - const ecs_id_record_t *idr) -{ - if (!idr) { - return NULL; - } - return ecs_table_cache_empty_tables(&idr->cache, ecs_table_record_t); + return result; } -int32_t flecs_id_record_count( - const ecs_id_record_t *idr) +static +bool ecs_strbuf_append_intern( + ecs_strbuf_t *b, + const char* str, + int n) { - if (!idr) { - return 0; - } - return ecs_table_cache_count(&idr->cache); -} + bool result = true; -int32_t flecs_id_record_empty_count( - const ecs_id_record_t *idr) -{ - if (!idr) { - return 0; + if (!str) { + return result; } - return ecs_table_cache_empty_count(&idr->cache); -} -void flecs_observable_init( - ecs_observable_t *observable) -{ - observable->triggers = ecs_sparse_new(ecs_event_triggers_t); -} - -void flecs_observable_fini( - ecs_observable_t *observable) -{ - ecs_sparse_t *triggers = observable->triggers; - int32_t i, count = flecs_sparse_count(triggers); + ecs_strbuf_init(b); - for (i = 0; i < count; i ++) { - ecs_event_triggers_t *et = - ecs_sparse_get_dense(triggers, ecs_event_triggers_t, i); - ecs_assert(et != NULL, ECS_INTERNAL_ERROR, NULL); + int32_t memLeftInElement = ecs_strbuf_memLeftInCurrentElement(b); + int32_t memLeft = ecs_strbuf_memLeft(b); - ecs_map_iter_t it = ecs_map_iter(et->triggers); - ecs_id_triggers_t *idt; - while ((idt = ecs_map_next(&it, ecs_id_triggers_t, NULL))) { - ecs_map_free(idt->triggers); - ecs_map_free(idt->set_triggers); - } - ecs_map_free(et->triggers); + if (memLeft <= 0) { + return false; } - flecs_sparse_free(observable->triggers); -} + /* Compute the memory required to add the string to the buffer. If user + * provided buffer, use space left in buffer, otherwise use space left in + * current element. */ + int32_t max_copy = b->buf ? memLeft : memLeftInElement; + int32_t memRequired; -void ecs_emit( - ecs_world_t *world, - ecs_event_desc_t *desc) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(desc->event != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(desc->event != EcsWildcard, ECS_INVALID_PARAMETER, NULL); + if (n < 0) n = INT_MAX; - ecs_ids_t ids_storage, *ids = desc->ids; - ecs_table_t *table = NULL, *other_table = NULL; - int32_t row = 0; - int32_t count = 0; - ecs_entity_t event = desc->event; - ecs_entity_t entity = 0; + memRequired = fast_strncpy(ecs_strbuf_ptr(b), str, max_copy, n); - world->event_id ++; + if (memRequired <= memLeftInElement) { + /* Element was large enough to fit string */ + b->current->pos += memRequired; + } else if ((memRequired - memLeftInElement) < memLeft) { + /* Element was not large enough, but buffer still has space */ + b->current->pos += memLeftInElement; + memRequired -= memLeftInElement; - if (desc->payload_kind == EcsPayloadEntity) { - entity = desc->payload.entity; - ecs_assert(entity != 0, ECS_INTERNAL_ERROR, NULL); - ecs_record_t *r = ecs_eis_get(world, entity); - if (r) { - bool is_watched; - table = r->table; - row = flecs_record_to_row(r->row, &is_watched); - } - count = 1; + /* Current element was too small, copy remainder into new element */ + if (memRequired < ECS_STRBUF_ELEMENT_SIZE) { + /* A standard-size buffer is large enough for the new string */ + ecs_strbuf_grow(b); - } else if (desc->payload_kind == EcsPayloadTable) { - ecs_assert(desc->payload.table.table != NULL, ECS_INTERNAL_ERROR, NULL); - table = desc->payload.table.table; - other_table = desc->payload.table.other_table; - row = desc->payload.table.offset; - int32_t payload_count = desc->payload.table.count; - if (!payload_count) { - count = ecs_table_count(table) - row; - } else { - count = payload_count; - } - } + /* Copy the remainder to the new buffer */ + if (n) { + /* If a max number of characters to write is set, only a + * subset of the string should be copied to the buffer */ + ecs_os_strncpy( + ecs_strbuf_ptr(b), + str + memLeftInElement, + (size_t)memRequired); + } else { + ecs_os_strcpy(ecs_strbuf_ptr(b), str + memLeftInElement); + } - if (table) { - if (!ids) { - ids = &ids_storage; - ids_storage.array = ecs_vector_first(table->type, ecs_id_t); - ids_storage.count = ecs_vector_count(table->type); + /* Update to number of characters copied to new buffer */ + b->current->pos += memRequired; + } else { + char *remainder = ecs_os_strdup(str + memLeftInElement); + ecs_strbuf_grow_str(b, remainder, remainder, memRequired); } + } else { + /* Buffer max has been reached */ + result = false; } - flecs_triggers_notify(world, desc->observable, ids, event, - entity, table, other_table, row, count, desc->param); + return result; } +bool ecs_strbuf_vappend( + ecs_strbuf_t *b, + const char* fmt, + va_list args) +{ + bool result = ecs_strbuf_vappend_intern( + b, fmt, args + ); + + return result; +} -static -void term_error( - const ecs_world_t *world, - const ecs_term_t *term, - const char *name, - const char *fmt, +bool ecs_strbuf_append( + ecs_strbuf_t *b, + const char* fmt, ...) { va_list args; va_start(args, fmt); - - char *expr = ecs_term_str(world, term); - ecs_parser_errorv(name, expr, 0, fmt, args); - ecs_os_free(expr); - + bool result = ecs_strbuf_vappend_intern( + b, fmt, args + ); va_end(args); + + return result; } -static -ecs_entity_t term_id_entity( - const ecs_world_t *world, - ecs_term_id_t *term_id) +bool ecs_strbuf_appendstrn( + ecs_strbuf_t *b, + const char* str, + int32_t len) { - if (term_id->entity && term_id->entity != EcsThis && - term_id->entity != EcsWildcard) - { - if (!(term_id->entity & ECS_ROLE_MASK)) { - return term_id->entity; - } else { - return 0; - } - } else if (term_id->name) { - if (term_id->var == EcsVarIsEntity || - (term_id->var == EcsVarDefault && - !ecs_identifier_is_var(term_id->name))) - { - return ecs_lookup_fullpath(world, term_id->name); - } else { - return 0; - } - } else { - return 0; - } + return ecs_strbuf_append_intern( + b, str, len + ); } -static -void finalize_default_substitution( - const ecs_world_t *world, - ecs_term_t *terms, - int32_t term_count) +bool ecs_strbuf_appendstr_zerocpy( + ecs_strbuf_t *b, + char* str) { - int i; - for (i = 0; i < term_count; i ++) { - ecs_term_id_t *pred = &terms[i].pred; - ecs_term_id_t *subj = &terms[i].subj; - ecs_term_id_t *obj = &terms[i].obj; - - bool pred_transitive = false; - if (pred->set.mask == EcsDefaultSet) { - ecs_entity_t e = term_id_entity(world, pred); - if (e) { - if (ecs_has_id(world, e, EcsFinal)) { - /* Final predicates are never substituted */ - pred->set.mask = EcsSelf; - } else { - pred->set.mask = EcsSelf|EcsSuperSet; - pred->set.relation = EcsIsA; - } - pred_transitive = ecs_has_id(world, e, EcsTransitive); - } - } - - if (subj->set.mask == EcsDefaultSet) { - /* By default subjects are substituted with their supersets */ - subj->set.mask = EcsSelf|EcsSuperSet; - subj->set.relation = EcsIsA; - } + ecs_strbuf_init(b); + ecs_strbuf_grow_str(b, str, str, 0); + return true; +} - if (obj->set.mask == EcsDefaultSet) { - /* By default subjects are substituted with their supersets */ - if (!pred_transitive) { - obj->set.mask = EcsSelf|EcsSuperSet; - obj->set.relation = EcsIsA; - } else { - /* If query is transitive substitute object. If object is fixed - * insert SubSet ("is "). If object is a - * variable, insert SuperSet ("find for ") */ - ecs_entity_t e = term_id_entity(world, obj); - obj->set.relation = EcsIsA; - if (!e) { - obj->set.mask = EcsSelf|EcsSuperSet; - } else { - obj->set.mask = EcsSelf|EcsSubSet; - } - } - } - } +bool ecs_strbuf_appendstr_zerocpy_const( + ecs_strbuf_t *b, + const char* str) +{ + /* Removes const modifier, but logic prevents changing / delete string */ + ecs_strbuf_init(b); + ecs_strbuf_grow_str(b, (char*)str, NULL, 0); + return true; } -static -int finalize_term_identifier( - const ecs_world_t *world, - ecs_term_t *term, - ecs_term_id_t *identifier, - const char *name) +bool ecs_strbuf_appendstr( + ecs_strbuf_t *b, + const char* str) { - /* Default set is Self */ - if (identifier->set.mask == EcsDefaultSet) { - identifier->set.mask |= EcsSelf; - } + return ecs_strbuf_append_intern( + b, str, -1 + ); +} - if (identifier->set.mask & EcsParent) { - identifier->set.mask |= EcsSuperSet; - identifier->set.relation = EcsChildOf; - } +bool ecs_strbuf_mergebuff( + ecs_strbuf_t *dst_buffer, + ecs_strbuf_t *src_buffer) +{ + if (src_buffer->elementCount) { + if (src_buffer->buf) { + return ecs_strbuf_appendstr(dst_buffer, src_buffer->buf); + } else { + ecs_strbuf_element *e = (ecs_strbuf_element*)&src_buffer->firstElement; - /* Default relation for superset/subset is EcsIsA */ - if (identifier->set.mask & (EcsSuperSet|EcsSubSet)) { - if (!identifier->set.relation) { - identifier->set.relation = EcsIsA; - } + /* Copy first element as it is inlined in the src buffer */ + ecs_strbuf_appendstrn(dst_buffer, e->buf, e->pos); - if (!(identifier->set.mask & EcsSelf)) { - if (!identifier->set.min_depth) { - identifier->set.min_depth = 1; + while ((e = e->next)) { + dst_buffer->current->next = ecs_os_malloc(sizeof(ecs_strbuf_element)); + *dst_buffer->current->next = *e; } } - } else { - if (identifier->set.min_depth > 0) { - term_error(world, term, name, - "min depth cannnot be non-zero for Self term"); - return -1; - } - if (identifier->set.max_depth > 1) { - term_error(world, term, name, - "max depth cannnot be larger than 1 for Self term"); - return -1; - } - identifier->set.max_depth = 1; + *src_buffer = ECS_STRBUF_INIT; } - if ((identifier->set.mask != EcsNothing) && - (identifier->set.mask & EcsNothing)) - { - term_error(world, term, name, "invalid Nothing in set mask"); - return -1; - } + return true; +} - if (identifier->var == EcsVarDefault) { - const char *var = ecs_identifier_is_var(identifier->name); - if (ecs_identifier_is_var(identifier->name)) { - char *var_id = ecs_os_strdup(var); - ecs_os_free(identifier->name); - identifier->name = var_id; - identifier->var = EcsVarIsVariable; - } - } +char* ecs_strbuf_get(ecs_strbuf_t *b) { + char* result = NULL; - if (identifier->var == EcsVarDefault && identifier->set.mask != EcsNothing){ - identifier->var = EcsVarIsEntity; - } + if (b->elementCount) { + if (b->buf) { + b->buf[b->current->pos] = '\0'; + result = ecs_os_strdup(b->buf); + } else { + void *next = NULL; + int32_t len = b->size + b->current->pos + 1; - if (!identifier->name) { - return 0; - } + ecs_strbuf_element *e = (ecs_strbuf_element*)&b->firstElement; - if (identifier->var != EcsVarIsVariable) { - if (ecs_identifier_is_0(identifier->name)) { - identifier->entity = 0; - } else { - ecs_entity_t e = ecs_lookup_symbol(world, identifier->name, true); - if (!e) { - term_error(world, term, name, - "unresolved identifier '%s'", identifier->name); - return -1; - } + result = ecs_os_malloc(len); + char* ptr = result; - identifier->entity = e; + do { + ecs_os_memcpy(ptr, e->buf, e->pos); + ptr += e->pos; + next = e->next; + if (e != &b->firstElement.super) { + if (!e->buffer_embedded) { + ecs_os_free(((ecs_strbuf_element_str*)e)->alloc_str); + } + ecs_os_free(e); + } + } while ((e = next)); + + result[len - 1] = '\0'; } + } else { + result = NULL; } - if ((identifier->set.mask == EcsNothing) && - (identifier->var != EcsVarDefault)) - { - term_error(world, term, name, "Invalid Nothing with entity"); - return -1; - } + b->elementCount = 0; - return 0; + return result; } -static -int finalize_term_identifiers( - const ecs_world_t *world, - ecs_term_t *term, - const char *name) -{ - if (finalize_term_identifier(world, term, &term->pred, name)) { - return -1; - } - if (finalize_term_identifier(world, term, &term->subj, name)) { - return -1; - } - if (finalize_term_identifier(world, term, &term->obj, name)) { - return -1; +void ecs_strbuf_reset(ecs_strbuf_t *b) { + if (b->elementCount && !b->buf) { + void *next = NULL; + ecs_strbuf_element *e = (ecs_strbuf_element*)&b->firstElement; + do { + next = e->next; + if (e != (ecs_strbuf_element*)&b->firstElement) { + ecs_os_free(e); + } + } while ((e = next)); } - if (term->pred.set.mask & EcsNothing) { - term_error(world, term, name, - "invalid Nothing value for predicate set mask"); - return -1; - } + *b = ECS_STRBUF_INIT; +} - if (term->obj.set.mask & EcsNothing) { - term_error(world, term, name, - "invalid Nothing value for object set mask"); - return -1; - } +void ecs_strbuf_list_push( + ecs_strbuf_t *buffer, + const char *list_open, + const char *separator) +{ + buffer->list_sp ++; + buffer->list_stack[buffer->list_sp].count = 0; + buffer->list_stack[buffer->list_sp].separator = separator; - if (!(term->subj.set.mask & EcsNothing) && - !term->subj.entity && - term->subj.var == EcsVarIsEntity) - { - term->subj.entity = EcsThis; + if (list_open) { + ecs_strbuf_appendstr(buffer, list_open); } +} + +void ecs_strbuf_list_pop( + ecs_strbuf_t *buffer, + const char *list_close) +{ + buffer->list_sp --; - if (term->pred.entity == EcsThis) { - term->pred.var = EcsVarIsVariable; - } - if (term->subj.entity == EcsThis) { - term->subj.var = EcsVarIsVariable; - } - if (term->obj.entity == EcsThis) { - term->obj.var = EcsVarIsVariable; + if (list_close) { + ecs_strbuf_appendstr(buffer, list_close); } - - return 0; } -static -ecs_entity_t entity_from_identifier( - const ecs_term_id_t *identifier) +void ecs_strbuf_list_next( + ecs_strbuf_t *buffer) { - if (identifier->var == EcsVarDefault) { - return 0; - } else if (identifier->var == EcsVarIsEntity) { - return identifier->entity; - } else if (identifier->var == EcsVarIsVariable) { - return EcsWildcard; - } else { - /* This should've been caught earlier */ - ecs_abort(ECS_INTERNAL_ERROR, NULL); + int32_t list_sp = buffer->list_sp; + if (buffer->list_stack[list_sp].count != 0) { + ecs_strbuf_appendstr(buffer, buffer->list_stack[list_sp].separator); } + buffer->list_stack[list_sp].count ++; } -static -int finalize_term_id( - const ecs_world_t *world, - ecs_term_t *term, - const char *name) +bool ecs_strbuf_list_append( + ecs_strbuf_t *buffer, + const char *fmt, + ...) { - ecs_entity_t pred = entity_from_identifier(&term->pred); - ecs_entity_t obj = entity_from_identifier(&term->obj); - ecs_id_t role = term->role; + ecs_strbuf_list_next(buffer); - if (ECS_HAS_ROLE(pred, PAIR)) { - if (obj) { - term_error(world, term, name, - "cannot set term.pred to a pair and term.obj at the same time"); - return -1; - } + va_list args; + va_start(args, fmt); + bool result = ecs_strbuf_vappend_intern( + buffer, fmt, args + ); + va_end(args); - obj = ECS_PAIR_OBJECT(pred); - pred = ECS_PAIR_RELATION(pred); + return result; +} - term->pred.entity = pred; - term->obj.entity = obj; +bool ecs_strbuf_list_appendstr( + ecs_strbuf_t *buffer, + const char *str) +{ + ecs_strbuf_list_next(buffer); + return ecs_strbuf_appendstr(buffer, str); +} - finalize_term_identifier(world, term, &term->obj, name); - } +/** The number of elements in a single chunk */ +#define CHUNK_COUNT (4096) - if (!obj && role != ECS_PAIR) { - term->id = pred | role; - } else { - if (role && role != ECS_PAIR) { - term_error(world, term, name, "invalid role for pair"); - return -1; - } +/** Compute the chunk index from an id by stripping the first 12 bits */ +#define CHUNK(index) ((int32_t)((uint32_t)index >> 12)) - term->id = ecs_pair(pred, obj); - term->role = ECS_PAIR; - } +/** This computes the offset of an index inside a chunk */ +#define OFFSET(index) ((int32_t)index & 0xFFF) - return 0; -} +/* Utility to get a pointer to the payload */ +#define DATA(array, size, offset) (ECS_OFFSET(array, size * offset)) -static -int populate_from_term_id( - const ecs_world_t *world, - ecs_term_t *term, - const char *name) -{ - ecs_entity_t pred = 0; - ecs_entity_t obj = 0; - ecs_id_t role = term->id & ECS_ROLE_MASK; - - if (!role && term->role) { - role = term->role; - term->id |= role; - } - - if (term->role && term->role != role) { - term_error(world, term, name, "mismatch between term.id & term.role"); - return -1; - } - - term->role = role; - - if (ECS_HAS_ROLE(term->id, PAIR)) { - pred = ECS_PAIR_RELATION(term->id); - obj = ECS_PAIR_OBJECT(term->id); - - if (!pred) { - term_error(world, term, name, "missing predicate in term.id pair"); - return -1; - } - if (!obj) { - if (pred != EcsChildOf) { - term_error(world, term, name, "missing object in term.id pair"); - return -1; - } - } - } else { - pred = term->id & ECS_COMPONENT_MASK; - if (!pred) { - term_error(world, term, name, "missing predicate in term.id"); - return -1; - } - } - - ecs_entity_t term_pred = entity_from_identifier(&term->pred); - if (term_pred) { - if (term_pred != pred) { - term_error(world, term, name, - "mismatch between term.id and term.pred"); - return -1; - } - } else { - term->pred.entity = pred; - if (finalize_term_identifier(world, term, &term->pred, name)) { - return -1; - } - } +typedef struct chunk_t { + int32_t *sparse; /* Sparse array with indices to dense array */ + void *data; /* Store data in sparse array to reduce + * indirection and provide stable pointers. */ +} chunk_t; - ecs_entity_t term_obj = entity_from_identifier(&term->obj); - if (term_obj) { - if (term_obj != obj) { - term_error(world, term, name, - "mismatch between term.id and term.obj"); - return -1; - } - } else { - term->obj.entity = obj; - if (finalize_term_identifier(world, term, &term->obj, name)) { - return -1; - } - } +struct ecs_sparse_t { + ecs_vector_t *dense; /* Dense array with indices to sparse array. The + * dense array stores both alive and not alive + * sparse indices. The 'count' member keeps + * track of which indices are alive. */ - return 0; -} + ecs_vector_t *chunks; /* Chunks with sparse arrays & data */ + ecs_size_t size; /* Element size */ + int32_t count; /* Number of alive entries */ + uint64_t max_id_local; /* Local max index (if no global is set) */ + uint64_t *max_id; /* Maximum issued sparse index */ +}; static -int verify_term_consistency( - const ecs_world_t *world, - const ecs_term_t *term, - const char *name) +chunk_t* chunk_new( + ecs_sparse_t *sparse, + int32_t chunk_index) { - ecs_entity_t pred = entity_from_identifier(&term->pred); - ecs_entity_t obj = entity_from_identifier(&term->obj); - ecs_id_t role = term->role; - ecs_id_t id = term->id; + int32_t count = ecs_vector_count(sparse->chunks); + chunk_t *chunks; - if (obj && (!role || (role != ECS_PAIR))) { - term_error(world, term, name, - "invalid role for term with pair (expected ECS_PAIR)"); - return -1; + if (count <= chunk_index) { + ecs_vector_set_count(&sparse->chunks, chunk_t, chunk_index + 1); + chunks = ecs_vector_first(sparse->chunks, chunk_t); + ecs_os_memset(&chunks[count], 0, (1 + chunk_index - count) * ECS_SIZEOF(chunk_t)); + } else { + chunks = ecs_vector_first(sparse->chunks, chunk_t); } - if (!pred) { - term_error(world, term, name, "missing predicate for term"); - return -1; - } + ecs_assert(chunks != NULL, ECS_INTERNAL_ERROR, NULL); - if (role != (id & ECS_ROLE_MASK)) { - term_error(world, term, name, "mismatch between term.role & term.id"); - return -1; - } + chunk_t *result = &chunks[chunk_index]; + ecs_assert(result->sparse == NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(result->data == NULL, ECS_INTERNAL_ERROR, NULL); - if (obj && !(ECS_HAS_ROLE(id, PAIR))) { - term_error(world, term, name, "term has object but id is not a pair"); - return -1; - } + /* Initialize sparse array with zero's, as zero is used to indicate that the + * sparse element has not been paired with a dense element. Use zero + * as this means we can take advantage of calloc having a possibly better + * performance than malloc + memset. */ + result->sparse = ecs_os_calloc(ECS_SIZEOF(int32_t) * CHUNK_COUNT); - if (ECS_HAS_ROLE(id, PAIR)) { - if (id != ecs_pair(pred, obj)) { - char *id_str = ecs_id_str(world, ecs_pair(pred, obj)); - term_error(world, term, name, - "term id does not match pred/obj (%s)", id_str); - ecs_os_free(id_str); - return -1; - } - } else if (pred != (id & ECS_COMPONENT_MASK)) { - char *pred_str = ecs_get_fullpath(world, pred); - term_error(world, term, name, "term id does not match pred '%s'", - pred_str); - ecs_os_free(pred_str); - return -1; - } + /* Initialize the data array with zero's to guarantee that data is + * always initialized. When an entry is removed, data is reset back to + * zero. Initialize now, as this can take advantage of calloc. */ + result->data = ecs_os_calloc(sparse->size * CHUNK_COUNT); - return 0; + ecs_assert(result->sparse != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(result->data != NULL, ECS_INTERNAL_ERROR, NULL); + + return result; } -bool ecs_identifier_is_0( - const char *id) +static +void chunk_free( + chunk_t *chunk) { - return id[0] == '0' && !id[1]; + ecs_os_free(chunk->sparse); + ecs_os_free(chunk->data); } -const char* ecs_identifier_is_var( - const char *id) +static +chunk_t* get_chunk( + const ecs_sparse_t *sparse, + int32_t chunk_index) { - if (!id) { - return NULL; - } - - /* Variable identifiers cannot start with a number */ - if (isdigit(id[0])) { + /* If chunk_index is below zero, application used an invalid entity id */ + ecs_assert(chunk_index >= 0, ECS_INVALID_PARAMETER, NULL); + chunk_t *result = ecs_vector_get(sparse->chunks, chunk_t, chunk_index); + if (result && !result->sparse) { return NULL; } - /* Identifiers that start with _ are variables */ - if (id[0] == '_') { - return &id[1]; - } - - /* Identifiers that have a single uppercase character are variables */ - if (ecs_os_strlen(id) == 1 && isupper(id[0])) { - return id; - } - - return NULL; + return result; } -bool ecs_id_match( - ecs_id_t id, - ecs_id_t pattern) +static +chunk_t* get_or_create_chunk( + ecs_sparse_t *sparse, + int32_t chunk_index) { - if (id == pattern) { - return true; + chunk_t *chunk = get_chunk(sparse, chunk_index); + if (chunk) { + return chunk; } - if (ECS_HAS_ROLE(pattern, PAIR)) { - if (!ECS_HAS_ROLE(id, PAIR)) { - return false; - } - - ecs_entity_t id_rel = ECS_PAIR_RELATION(id); - ecs_entity_t id_obj = ECS_PAIR_OBJECT(id); - ecs_entity_t pattern_rel = ECS_PAIR_RELATION(pattern); - ecs_entity_t pattern_obj = ECS_PAIR_OBJECT(pattern); - - ecs_assert(id_rel != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(id_obj != 0, ECS_INVALID_PARAMETER, NULL); + return chunk_new(sparse, chunk_index); +} - ecs_assert(pattern_rel != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(pattern_obj != 0, ECS_INVALID_PARAMETER, NULL); - - if (pattern_rel == EcsWildcard) { - if (pattern_obj == EcsWildcard || pattern_obj == id_obj) { - return true; - } - } else if (pattern_obj == EcsWildcard) { - if (pattern_rel == id_rel) { - return true; - } - } - } else { - if ((id & ECS_ROLE_MASK) != (pattern & ECS_ROLE_MASK)) { - return false; - } +static +void grow_dense( + ecs_sparse_t *sparse) +{ + ecs_vector_add(&sparse->dense, uint64_t); +} - if ((ECS_COMPONENT_MASK & pattern) == EcsWildcard) { - return true; - } - } +static +uint64_t strip_generation( + uint64_t *index_out) +{ + uint64_t index = *index_out; + uint64_t gen = index & ECS_GENERATION_MASK; + /* Make sure there's no junk in the id */ + ecs_assert(gen == (index & (0xFFFFFFFFull << 32)), + ECS_INVALID_PARAMETER, NULL); + *index_out -= gen; + return gen; +} - return false; +static +void assign_index( + chunk_t * chunk, + uint64_t * dense_array, + uint64_t index, + int32_t dense) +{ + /* Initialize sparse-dense pair. This assigns the dense index to the sparse + * array, and the sparse index to the dense array .*/ + chunk->sparse[OFFSET(index)] = dense; + dense_array[dense] = index; } -bool ecs_id_is_pair( - ecs_id_t id) +static +uint64_t inc_gen( + uint64_t index) { - return ECS_HAS_ROLE(id, PAIR); + /* When an index is deleted, its generation is increased so that we can do + * liveliness checking while recycling ids */ + return ECS_GENERATION_INC(index); } -bool ecs_id_is_wildcard( - ecs_id_t id) +static +uint64_t inc_id( + ecs_sparse_t *sparse) { - if (id == EcsWildcard) { - return true; - } else if (ECS_HAS_ROLE(id, PAIR)) { - return ECS_PAIR_RELATION(id) == EcsWildcard || - ECS_PAIR_OBJECT(id) == EcsWildcard; - } + /* Generate a new id. The last issued id could be stored in an external + * variable, such as is the case with the last issued entity id, which is + * stored on the world. */ + return ++ (sparse->max_id[0]); +} - return false; +static +uint64_t get_id( + const ecs_sparse_t *sparse) +{ + return sparse->max_id[0]; } -bool ecs_term_id_is_set( - const ecs_term_id_t *id) +static +void set_id( + ecs_sparse_t *sparse, + uint64_t value) { - return id->entity != 0 || id->name != NULL; + /* Sometimes the max id needs to be assigned directly, which typically + * happens when the API calls get_or_create for an id that hasn't been + * issued before. */ + sparse->max_id[0] = value; } -bool ecs_term_is_initialized( - const ecs_term_t *term) +/* Pair dense id with new sparse id */ +static +uint64_t create_id( + ecs_sparse_t *sparse, + int32_t dense) { - return term->id != 0 || ecs_term_id_is_set(&term->pred); + uint64_t index = inc_id(sparse); + grow_dense(sparse); + + chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); + ecs_assert(chunk->sparse[OFFSET(index)] == 0, ECS_INTERNAL_ERROR, NULL); + + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + assign_index(chunk, dense_array, index, dense); + + return index; } -bool ecs_term_is_trivial( - const ecs_term_t *term) +/* Create new id */ +static +uint64_t new_index( + ecs_sparse_t *sparse) { - if (term->inout != EcsInOutDefault) { - return false; - } + ecs_vector_t *dense = sparse->dense; + int32_t dense_count = ecs_vector_count(dense); + int32_t count = sparse->count ++; - if (term->subj.entity != EcsThis) { - return false; - } + ecs_assert(count <= dense_count, ECS_INTERNAL_ERROR, NULL); - if (term->subj.set.mask && (term->subj.set.mask != EcsSelf)) { - return false; + if (count < dense_count) { + /* If there are unused elements in the dense array, return first */ + uint64_t *dense_array = ecs_vector_first(dense, uint64_t); + return dense_array[count]; + } else { + return create_id(sparse, count); } +} - if (term->oper != EcsAnd && term->oper != EcsAndFrom) { - return false; +/* Try obtaining a value from the sparse set, don't care about whether the + * provided index matches the current generation count. */ +static +void* try_sparse_any( + const ecs_sparse_t *sparse, + uint64_t index) +{ + strip_generation(&index); + + chunk_t *chunk = get_chunk(sparse, CHUNK(index)); + if (!chunk) { + return NULL; } - if (term->name != NULL) { - return false; + int32_t offset = OFFSET(index); + int32_t dense = chunk->sparse[offset]; + bool in_use = dense && (dense < sparse->count); + if (!in_use) { + return NULL; } - return true; + ecs_assert(dense == chunk->sparse[offset], ECS_INTERNAL_ERROR, NULL); + return DATA(chunk->data, sparse->size, offset); } -int ecs_term_finalize( - const ecs_world_t *world, - const char *name, - ecs_term_t *term) +/* Try obtaining a value from the sparse set, make sure it's alive. */ +static +void* try_sparse( + const ecs_sparse_t *sparse, + uint64_t index) { - if (finalize_term_identifiers(world, term, name)) { - return -1; + chunk_t *chunk = get_chunk(sparse, CHUNK(index)); + if (!chunk) { + return NULL; } - if (!term->id) { - if (finalize_term_id(world, term, name)) { - return -1; - } - } else { - if (populate_from_term_id(world, term, name)) { - return -1; - } + int32_t offset = OFFSET(index); + int32_t dense = chunk->sparse[offset]; + bool in_use = dense && (dense < sparse->count); + if (!in_use) { + return NULL; } - if (verify_term_consistency(world, term, name)) { - return -1; + uint64_t gen = strip_generation(&index); + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + uint64_t cur_gen = dense_array[dense] & ECS_GENERATION_MASK; + + if (cur_gen != gen) { + return NULL; } - return 0; + ecs_assert(dense == chunk->sparse[offset], ECS_INTERNAL_ERROR, NULL); + return DATA(chunk->data, sparse->size, offset); } -ecs_term_t ecs_term_copy( - const ecs_term_t *src) +/* Get value from sparse set when it is guaranteed that the value exists. This + * function is used when values are obtained using a dense index */ +static +void* get_sparse( + const ecs_sparse_t *sparse, + int32_t dense, + uint64_t index) { - ecs_term_t dst = *src; - dst.name = ecs_os_strdup(src->name); - dst.pred.name = ecs_os_strdup(src->pred.name); - dst.subj.name = ecs_os_strdup(src->subj.name); - dst.obj.name = ecs_os_strdup(src->obj.name); - return dst; -} + strip_generation(&index); + chunk_t *chunk = get_chunk(sparse, CHUNK(index)); + int32_t offset = OFFSET(index); + + ecs_assert(chunk != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(dense == chunk->sparse[offset], ECS_INTERNAL_ERROR, NULL); + (void)dense; -ecs_term_t ecs_term_move( - ecs_term_t *src) -{ - if (src->move) { - ecs_term_t dst = *src; - src->name = NULL; - src->pred.name = NULL; - src->subj.name = NULL; - src->obj.name = NULL; - return dst; - } else { - return ecs_term_copy(src); - } + return DATA(chunk->data, sparse->size, offset); } -void ecs_term_fini( - ecs_term_t *term) +/* Swap dense elements. A swap occurs when an element is removed, or when a + * removed element is recycled. */ +static +void swap_dense( + ecs_sparse_t * sparse, + chunk_t * chunk_a, + int32_t a, + int32_t b) { - ecs_os_free(term->pred.name); - ecs_os_free(term->subj.name); - ecs_os_free(term->obj.name); - ecs_os_free(term->name); + ecs_assert(a != b, ECS_INTERNAL_ERROR, NULL); + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + uint64_t index_a = dense_array[a]; + uint64_t index_b = dense_array[b]; - term->pred.name = NULL; - term->subj.name = NULL; - term->obj.name = NULL; - term->name = NULL; + chunk_t *chunk_b = get_or_create_chunk(sparse, CHUNK(index_b)); + assign_index(chunk_a, dense_array, index_a, b); + assign_index(chunk_b, dense_array, index_b, a); } -int ecs_filter_finalize( - const ecs_world_t *world, - ecs_filter_t *f) +ecs_sparse_t* _flecs_sparse_new( + ecs_size_t size) { - int32_t i, term_count = f->term_count, actual_count = 0; - ecs_term_t *terms = f->terms; - bool is_or = false, prev_or = false; + ecs_sparse_t *result = ecs_os_calloc(ECS_SIZEOF(ecs_sparse_t)); + ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); + result->size = size; + result->max_id_local = UINT64_MAX; + result->max_id = &result->max_id_local; - for (i = 0; i < term_count; i ++) { - ecs_term_t *term = &terms[i]; + /* Consume first value in dense array as 0 is used in the sparse array to + * indicate that a sparse element hasn't been paired yet. */ + uint64_t *first = ecs_vector_add(&result->dense, uint64_t); + *first = 0; - if (ecs_term_finalize(world, f->name, term)) { - return -1; - } + result->count = 1; - is_or = term->oper == EcsOr; - actual_count += !(is_or && prev_or); - term->index = actual_count - 1; - prev_or = is_or; + return result; +} - if (term->subj.entity == EcsThis) { - f->match_this = true; - if (term->subj.set.mask != EcsSelf) { - f->match_only_this = false; - } - } else { - f->match_only_this = false; - } +void flecs_sparse_set_id_source( + ecs_sparse_t * sparse, + uint64_t * id_source) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + sparse->max_id = id_source; +} - if (term->id == EcsPrefab) { - f->match_prefab = true; - } - if (term->id == EcsDisabled) { - f->match_disabled = true; - } - } +void flecs_sparse_clear( + ecs_sparse_t *sparse) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - f->term_count_actual = actual_count; + ecs_vector_each(sparse->chunks, chunk_t, chunk, { + chunk_free(chunk); + }); - return 0; + ecs_vector_free(sparse->chunks); + ecs_vector_set_count(&sparse->dense, uint64_t, 1); + + sparse->chunks = NULL; + sparse->count = 1; + sparse->max_id_local = 0; } -int ecs_filter_init( - const ecs_world_t *stage, - ecs_filter_t *filter_out, - const ecs_filter_desc_t *desc) +void flecs_sparse_free( + ecs_sparse_t *sparse) { - ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(filter_out != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); - - const ecs_world_t *world = ecs_get_world(stage); + if (sparse) { + flecs_sparse_clear(sparse); + ecs_vector_free(sparse->dense); + ecs_os_free(sparse); + } +} - int i, term_count = 0; - ecs_term_t *terms = desc->terms_buffer; - const char *name = desc->name; - const char *expr = desc->expr; +uint64_t flecs_sparse_new_id( + ecs_sparse_t *sparse) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + return new_index(sparse); +} - ecs_filter_t f = { - /* Temporarily set the fields to the values provided in desc, until the - * filter has been validated. */ - .name = (char*)name, - .expr = (char*)expr - }; +const uint64_t* flecs_sparse_new_ids( + ecs_sparse_t *sparse, + int32_t new_count) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + int32_t dense_count = ecs_vector_count(sparse->dense); + int32_t count = sparse->count; + int32_t remaining = dense_count - count; + int32_t i, to_create = new_count - remaining; - if (terms) { - terms = desc->terms_buffer; - term_count = desc->terms_buffer_count; - } else { - terms = (ecs_term_t*)desc->terms; - for (i = 0; i < ECS_TERM_DESC_CACHE_SIZE; i ++) { - if (!ecs_term_is_initialized(&terms[i])) { - break; - } + if (to_create > 0) { + flecs_sparse_set_size(sparse, dense_count + to_create); + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - term_count ++; + for (i = 0; i < to_create; i ++) { + uint64_t index = create_id(sparse, count + i); + dense_array[dense_count + i] = index; } } - /* Temporarily set array from desc to filter, until the filter has been - * validated. */ - f.terms = terms; - f.term_count = term_count; + sparse->count += new_count; - if (expr) { -#ifdef FLECS_PARSER - int32_t buffer_count = 0; + return ecs_vector_get(sparse->dense, uint64_t, count); +} - /* If terms have already been set, copy buffer to allocated one */ - if (terms && term_count) { - terms = ecs_os_memdup(terms, term_count * ECS_SIZEOF(ecs_term_t)); - buffer_count = term_count; - } else { - terms = NULL; - } +void* _flecs_sparse_add( + ecs_sparse_t *sparse, + ecs_size_t size) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); + uint64_t index = new_index(sparse); + chunk_t *chunk = get_chunk(sparse, CHUNK(index)); + ecs_assert(chunk != NULL, ECS_INTERNAL_ERROR, NULL); + return DATA(chunk->data, size, OFFSET(index)); +} - /* Parse expression into array of terms */ - const char *ptr = desc->expr; - ecs_term_t term = {0}; - while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))){ - if (!ecs_term_is_initialized(&term)) { - break; - } - - if (term_count == buffer_count) { - buffer_count = buffer_count ? buffer_count * 2 : 8; - terms = ecs_os_realloc(terms, - buffer_count * ECS_SIZEOF(ecs_term_t)); - } +uint64_t flecs_sparse_last_id( + const ecs_sparse_t *sparse) +{ + ecs_assert(sparse != NULL, ECS_INTERNAL_ERROR, NULL); + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + return dense_array[sparse->count - 1]; +} - terms[term_count] = term; - term_count ++; +void* _flecs_sparse_ensure( + ecs_sparse_t *sparse, + ecs_size_t size, + uint64_t index) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_vector_count(sparse->dense) > 0, ECS_INTERNAL_ERROR, NULL); + (void)size; - if (ptr[0] == '\n') { - break; - } - } + uint64_t gen = strip_generation(&index); + chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); + int32_t offset = OFFSET(index); + int32_t dense = chunk->sparse[offset]; - f.terms = terms; - f.term_count = term_count; + if (dense) { + /* Check if element is alive. If element is not alive, update indices so + * that the first unused dense element points to the sparse element. */ + int32_t count = sparse->count; + if (dense == count) { + /* If dense is the next unused element in the array, simply increase + * the count to make it part of the alive set. */ + sparse->count ++; + } else if (dense > count) { + /* If dense is not alive, swap it with the first unused element. */ + swap_dense(sparse, chunk, dense, count); - if (!ptr) { - goto error; + /* First unused element is now last used element */ + sparse->count ++; + } else { + /* Dense is already alive, nothing to be done */ } -#else - ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); -#endif - } - /* If default substitution is enabled, replace DefaultSet with SuperSet */ - if (desc->substitute_default) { - finalize_default_substitution(world, terms, term_count); + /* Ensure provided generation matches current. Only allow mismatching + * generations if the provided generation count is 0. This allows for + * using the ensure function in combination with ids that have their + * generation stripped. */ + ecs_vector_t *dense_vector = sparse->dense; + uint64_t *dense_array = ecs_vector_first(dense_vector, uint64_t); + ecs_assert(!gen || dense_array[dense] == (index | gen), ECS_INTERNAL_ERROR, NULL); + (void)dense_vector; + (void)dense_array; } else { - for (i = 0; i < term_count; i ++) { - if (terms[i].subj.set.mask == EcsDefaultSet) { - terms[i].subj.set.mask = EcsSelf; - } - } - } - - /* Ensure all fields are consistent and properly filled out */ - if (ecs_filter_finalize(world, &f)) { - goto error; - } + /* Element is not paired yet. Must add a new element to dense array */ + grow_dense(sparse); - *filter_out = f; + ecs_vector_t *dense_vector = sparse->dense; + uint64_t *dense_array = ecs_vector_first(dense_vector, uint64_t); + int32_t dense_count = ecs_vector_count(dense_vector) - 1; + int32_t count = sparse->count ++; - /* Copy term resources. */ - if (term_count) { - if (!filter_out->expr) { - if (term_count < ECS_TERM_CACHE_SIZE) { - filter_out->terms = filter_out->term_cache; - filter_out->term_cache_used = true; - } else { - filter_out->terms = ecs_os_malloc_n(ecs_term_t, term_count); - } + /* If index is larger than max id, update max id */ + if (index >= get_id(sparse)) { + set_id(sparse, index + 1); } - for (i = 0; i < term_count; i ++) { - filter_out->terms[i] = ecs_term_move(&terms[i]); - } - } else { - filter_out->terms = NULL; - } - - filter_out->name = ecs_os_strdup(desc->name); - filter_out->expr = ecs_os_strdup(desc->expr); - - ecs_assert(!filter_out->term_cache_used || - filter_out->terms == filter_out->term_cache, - ECS_INTERNAL_ERROR, NULL); + if (count < dense_count) { + /* If there are unused elements in the list, move the first unused + * element to the end of the list */ + uint64_t unused = dense_array[count]; + chunk_t *unused_chunk = get_or_create_chunk(sparse, CHUNK(unused)); + assign_index(unused_chunk, dense_array, unused, dense_count); + } - return 0; -error: - /* NULL members that point to non-owned resources */ - if (!f.expr) { - f.terms = NULL; + assign_index(chunk, dense_array, index, count); + dense_array[count] |= gen; } - f.name = NULL; - f.expr = NULL; - - ecs_filter_fini(&f); + return DATA(chunk->data, sparse->size, offset); +} - return -1; +void* _flecs_sparse_set( + ecs_sparse_t * sparse, + ecs_size_t elem_size, + uint64_t index, + void* value) +{ + void *ptr = _flecs_sparse_ensure(sparse, elem_size, index); + ecs_os_memcpy(ptr, value, elem_size); + return ptr; } -void ecs_filter_copy( - ecs_filter_t *dst, - const ecs_filter_t *src) +void* _flecs_sparse_remove_get( + ecs_sparse_t *sparse, + ecs_size_t size, + uint64_t index) { - if (src) { - *dst = *src; + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); + (void)size; - int32_t term_count = src->term_count; + chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); + uint64_t gen = strip_generation(&index); + int32_t offset = OFFSET(index); + int32_t dense = chunk->sparse[offset]; - if (src->term_cache_used) { - dst->terms = dst->term_cache; - } else { - dst->terms = ecs_os_memdup_n(src->terms, ecs_term_t, term_count); + if (dense) { + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + uint64_t cur_gen = dense_array[dense] & ECS_GENERATION_MASK; + if (gen != cur_gen) { + /* Generation doesn't match which means that the provided entity is + * already not alive. */ + return NULL; } - int i; - for (i = 0; i < term_count; i ++) { - dst->terms[i] = ecs_term_copy(&src->terms[i]); + /* Increase generation */ + dense_array[dense] = index | inc_gen(cur_gen); + + int32_t count = sparse->count; + if (dense == (count - 1)) { + /* If dense is the last used element, simply decrease count */ + sparse->count --; + } else if (dense < count) { + /* If element is alive, move it to unused elements */ + swap_dense(sparse, chunk, dense, count - 1); + sparse->count --; + } else { + /* Element is not alive, nothing to be done */ + return NULL; } + + /* Reset memory to zero on remove */ + return DATA(chunk->data, sparse->size, offset); } else { - ecs_os_memset_t(dst, 0, ecs_filter_t); + /* Element is not paired and thus not alive, nothing to be done */ + return NULL; } } -void ecs_filter_move( - ecs_filter_t *dst, - ecs_filter_t *src) +void flecs_sparse_remove( + ecs_sparse_t *sparse, + uint64_t index) { - if (src) { - *dst = *src; + void *ptr = _flecs_sparse_remove_get(sparse, 0, index); + if (ptr) { + ecs_os_memset(ptr, 0, sparse->size); + } +} - if (src->term_cache_used) { - dst->terms = dst->term_cache; - } +void flecs_sparse_set_generation( + ecs_sparse_t *sparse, + uint64_t index) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + chunk_t *chunk = get_or_create_chunk(sparse, CHUNK(index)); + + uint64_t index_w_gen = index; + strip_generation(&index); + int32_t offset = OFFSET(index); + int32_t dense = chunk->sparse[offset]; - src->terms = NULL; - src->term_count = 0; + if (dense) { + /* Increase generation */ + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + dense_array[dense] = index_w_gen; } else { - ecs_os_memset_t(dst, 0, ecs_filter_t); + /* Element is not paired and thus not alive, nothing to be done */ } } -void ecs_filter_fini( - ecs_filter_t *filter) +bool flecs_sparse_exists( + const ecs_sparse_t *sparse, + uint64_t index) { - if (filter->terms) { - int i, count = filter->term_count; - for (i = 0; i < count; i ++) { - ecs_term_fini(&filter->terms[i]); - } - - if (!filter->term_cache_used) { - ecs_os_free(filter->terms); - } + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + chunk_t *chunk = get_chunk(sparse, CHUNK(index)); + if (!chunk) { + return false; } + + strip_generation(&index); + int32_t offset = OFFSET(index); + int32_t dense = chunk->sparse[offset]; - ecs_os_free(filter->name); - ecs_os_free(filter->expr); + return dense != 0; } -static -void filter_str_add_id( - const ecs_world_t *world, - ecs_strbuf_t *buf, - const ecs_term_id_t *id, - bool is_subject) +void* _flecs_sparse_get_dense( + const ecs_sparse_t *sparse, + ecs_size_t size, + int32_t dense_index) { - if (id->name) { - ecs_strbuf_appendstr(buf, id->name); - } else if (id->entity) { - bool id_added = false; - if (!is_subject || id->entity != EcsThis) { - char *path = ecs_get_fullpath(world, id->entity); - ecs_strbuf_appendstr(buf, path); - ecs_os_free(path); - id_added = true; - } - - if (id->set.mask != EcsSelf) { - if (id_added) { - ecs_strbuf_list_push(buf, ":", "|"); - } else { - ecs_strbuf_list_push(buf, "", "|"); - } - if (id->set.mask & EcsSelf) { - ecs_strbuf_list_appendstr(buf, "self"); - } - if (id->set.mask & EcsSuperSet) { - ecs_strbuf_list_appendstr(buf, "superset"); - } - if (id->set.mask & EcsSubSet) { - ecs_strbuf_list_appendstr(buf, "subset"); - } - - if (id->set.relation != EcsIsA) { - ecs_strbuf_list_push(buf, "(", ""); - - char *rel_path = ecs_get_fullpath(world, id->set.relation); - ecs_strbuf_appendstr(buf, rel_path); - ecs_os_free(rel_path); + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); + ecs_assert(dense_index < sparse->count, ECS_INVALID_PARAMETER, NULL); + (void)size; - ecs_strbuf_list_pop(buf, ")"); - } + dense_index ++; - ecs_strbuf_list_pop(buf, ""); - } - } else { - ecs_strbuf_appendstr(buf, "0"); - } + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); + return get_sparse(sparse, dense_index, dense_array[dense_index]); } -static -void term_str_w_strbuf( - const ecs_world_t *world, - const ecs_term_t *term, - ecs_strbuf_t *buf) +bool flecs_sparse_is_alive( + const ecs_sparse_t *sparse, + uint64_t index) { - const ecs_term_id_t *subj = &term->subj; - const ecs_term_id_t *obj = &term->obj; - - bool pred_set = ecs_term_id_is_set(&term->pred); - bool subj_set = ecs_term_id_is_set(subj); - bool obj_set = ecs_term_id_is_set(obj); + return try_sparse(sparse, index) != NULL; +} - if (term->role && term->role != ECS_PAIR) { - ecs_strbuf_appendstr(buf, ecs_role_str(term->role)); - ecs_strbuf_appendstr(buf, " "); +uint64_t flecs_sparse_get_alive( + const ecs_sparse_t *sparse, + uint64_t index) +{ + chunk_t *chunk = get_chunk(sparse, CHUNK(index)); + if (!chunk) { + return 0; } - if (term->oper == EcsNot) { - ecs_strbuf_appendstr(buf, "!"); - } else if (term->oper == EcsOptional) { - ecs_strbuf_appendstr(buf, "?"); - } + int32_t offset = OFFSET(index); + int32_t dense = chunk->sparse[offset]; + uint64_t *dense_array = ecs_vector_first(sparse->dense, uint64_t); - if (!subj_set) { - filter_str_add_id(world, buf, &term->pred, false); - ecs_strbuf_appendstr(buf, "()"); - } else if (subj_set && subj->entity == EcsThis && subj->set.mask == EcsSelf) - { - if (term->id) { - char *str = ecs_id_str(world, term->id); - ecs_strbuf_appendstr(buf, str); - ecs_os_free(str); - } else if (pred_set) { - filter_str_add_id(world, buf, &term->pred, false); - } - } else { - filter_str_add_id(world, buf, &term->pred, false); - ecs_strbuf_appendstr(buf, "("); - filter_str_add_id(world, buf, &term->subj, true); - if (obj_set) { - ecs_strbuf_appendstr(buf, ","); - filter_str_add_id(world, buf, &term->obj, false); - } - ecs_strbuf_appendstr(buf, ")"); - } + /* If dense is 0 (tombstone) this will return 0 */ + return dense_array[dense]; } -char* ecs_term_str( - const ecs_world_t *world, - const ecs_term_t *term) +void* _flecs_sparse_get( + const ecs_sparse_t *sparse, + ecs_size_t size, + uint64_t index) { - ecs_strbuf_t buf = ECS_STRBUF_INIT; - term_str_w_strbuf(world, term, &buf); - return ecs_strbuf_get(&buf); + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); + (void)size; + return try_sparse(sparse, index); } -char* ecs_filter_str( - const ecs_world_t *world, - const ecs_filter_t *filter) +void* _flecs_sparse_get_any( + ecs_sparse_t *sparse, + ecs_size_t size, + uint64_t index) { - ecs_strbuf_t buf = ECS_STRBUF_INIT; - - ecs_assert(!filter->term_cache_used || filter->terms == filter->term_cache, - ECS_INTERNAL_ERROR, NULL); + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!size || size == sparse->size, ECS_INVALID_PARAMETER, NULL); + (void)size; + return try_sparse_any(sparse, index); +} - ecs_term_t *terms = filter->terms; - int32_t i, count = filter->term_count; - int32_t or_count = 0; +int32_t flecs_sparse_count( + const ecs_sparse_t *sparse) +{ + if (!sparse) { + return 0; + } - for (i = 0; i < count; i ++) { - ecs_term_t *term = &terms[i]; + return sparse->count - 1; +} - if (i) { - if (terms[i - 1].oper == EcsOr && term->oper == EcsOr) { - ecs_strbuf_appendstr(&buf, " || "); - } else { - ecs_strbuf_appendstr(&buf, ", "); - } - } +int32_t flecs_sparse_size( + const ecs_sparse_t *sparse) +{ + if (!sparse) { + return 0; + } + + return ecs_vector_count(sparse->dense) - 1; +} - if (or_count < 1) { - if (term->inout == EcsIn) { - ecs_strbuf_appendstr(&buf, "[in] "); - } else if (term->inout == EcsInOut) { - ecs_strbuf_appendstr(&buf, "[inout] "); - } else if (term->inout == EcsOut) { - ecs_strbuf_appendstr(&buf, "[out] "); - } - } +const uint64_t* flecs_sparse_ids( + const ecs_sparse_t *sparse) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + return &(ecs_vector_first(sparse->dense, uint64_t)[1]); +} - if (term->oper == EcsOr) { - or_count ++; - } else { - or_count = 0; - } +void flecs_sparse_set_size( + ecs_sparse_t *sparse, + int32_t elem_count) +{ + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_vector_set_size(&sparse->dense, uint64_t, elem_count); +} - term_str_w_strbuf(world, term, &buf); +static +void sparse_copy( + ecs_sparse_t * dst, + const ecs_sparse_t * src) +{ + flecs_sparse_set_size(dst, flecs_sparse_size(src)); + const uint64_t *indices = flecs_sparse_ids(src); + + ecs_size_t size = src->size; + int32_t i, count = src->count; + + for (i = 0; i < count - 1; i ++) { + uint64_t index = indices[i]; + void *src_ptr = _flecs_sparse_get(src, size, index); + void *dst_ptr = _flecs_sparse_ensure(dst, size, index); + flecs_sparse_set_generation(dst, index); + ecs_os_memcpy(dst_ptr, src_ptr, size); } - return ecs_strbuf_get(&buf); + set_id(dst, get_id(src)); + + ecs_assert(src->count == dst->count, ECS_INTERNAL_ERROR, NULL); } -static -bool populate_from_column( - ecs_world_t *world, - const ecs_table_t *table, - int32_t offset, - ecs_id_t id, - int32_t column, - ecs_entity_t source, - ecs_id_t *id_out, - ecs_entity_t *subject_out, - ecs_size_t *size_out, - void **ptr_out) +ecs_sparse_t* flecs_sparse_copy( + const ecs_sparse_t *src) { - ecs_column_t *storage = NULL; - ecs_size_t size = 0; + if (!src) { + return NULL; + } - if (column != -1) { - /* If source is not This, find table of source */ - if (source) { - table = ecs_get_table(world, source); - ecs_table_record_t *tr = flecs_get_table_record(world, table, id); - column = tr->column; - } + ecs_sparse_t *dst = _flecs_sparse_new(src->size); + sparse_copy(dst, src); - int32_t storage_column = ecs_table_type_to_storage_index(table, column); - if (storage_column != -1) { - storage = &table->storage.columns[storage_column]; - size = storage->size; - } + return dst; +} - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - id = ids[column]; +void flecs_sparse_restore( + ecs_sparse_t * dst, + const ecs_sparse_t * src) +{ + ecs_assert(dst != NULL, ECS_INVALID_PARAMETER, NULL); + dst->count = 1; + if (src) { + sparse_copy(dst, src); + } +} - if (subject_out) { - *subject_out = source; - } +void flecs_sparse_memory( + ecs_sparse_t *sparse, + int32_t *allocd, + int32_t *used) +{ + (void)sparse; + (void)allocd; + (void)used; +} - if (ptr_out) { - if (storage) { - if (source) { - *ptr_out = (void*)ecs_get_id(world, source, id); - } else { - *ptr_out = ecs_vector_first_t( - storage->data, size, storage->alignment); - - if (*ptr_out && offset) { - *ptr_out = ECS_OFFSET(*ptr_out, size * offset); - } - } - } else { - *ptr_out = NULL; - } - } - } +ecs_sparse_t* _ecs_sparse_new( + ecs_size_t elem_size) +{ + return _flecs_sparse_new(elem_size); +} - if (id_out) { - *id_out = id; - } +void* _ecs_sparse_add( + ecs_sparse_t *sparse, + ecs_size_t elem_size) +{ + return _flecs_sparse_add(sparse, elem_size); +} - if (size_out) { - *size_out = size; - } +uint64_t ecs_sparse_last_id( + const ecs_sparse_t *sparse) +{ + return flecs_sparse_last_id(sparse); +} - return storage != NULL; +int32_t ecs_sparse_count( + const ecs_sparse_t *sparse) +{ + return flecs_sparse_count(sparse); } -static -void populate_from_table( - ecs_iter_t *it, - ecs_table_t *table) +void* _ecs_sparse_get_dense( + const ecs_sparse_t *sparse, + ecs_size_t elem_size, + int32_t index) { - it->table = table; - it->type = table->type; - it->count = ecs_table_count(table); + return _flecs_sparse_get_dense(sparse, elem_size, index); +} - const ecs_data_t *data = &table->storage; - it->entities = ecs_vector_first(data->entities, ecs_entity_t); +void* _ecs_sparse_get( + const ecs_sparse_t *sparse, + ecs_size_t elem_size, + uint64_t id) +{ + return _flecs_sparse_get(sparse, elem_size, id); } -bool flecs_term_match_table( - ecs_world_t *world, - const ecs_term_t *term, - const ecs_table_t *table, - ecs_type_t type, - int32_t offset, - ecs_id_t *id_out, - int32_t *column_out, - ecs_entity_t *subject_out, - ecs_size_t *size_out, - void **ptr_out, - int32_t *match_index_out, - bool first) +ecs_sparse_iter_t _flecs_sparse_iter( + ecs_sparse_t *sparse, + ecs_size_t elem_size) { - const ecs_term_id_t *subj = &term->subj; - ecs_oper_kind_t oper = term->oper; - const ecs_table_t *match_table = table; - ecs_type_t match_type = type; + ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(elem_size == sparse->size, ECS_INVALID_PARAMETER, NULL); + ecs_sparse_iter_t result; + result.sparse = sparse; + result.ids = flecs_sparse_ids(sparse); + result.size = elem_size; + result.i = 0; + result.count = sparse->count - 1; + return result; +} - ecs_entity_t subj_entity = subj->entity; - if (!subj_entity) { - id_out[0] = term->id; - return true; +#ifdef FLECS_SANITIZE +static +void verify_nodes( + flecs_switch_header_t *hdr, + flecs_switch_node_t *nodes) +{ + if (!hdr) { + return; } - if (subj_entity != EcsThis) { - match_table = ecs_get_table(world, subj_entity); - if (match_table) { - match_type = match_table->type; - } else { - match_type = NULL; - } + int32_t prev = -1, elem = hdr->element, count = 0; + while (elem != -1) { + ecs_assert(prev == nodes[elem].prev, ECS_INTERNAL_ERROR, NULL); + prev = elem; + elem = nodes[elem].next; + count ++; } - ecs_entity_t source; + ecs_assert(count == hdr->count, ECS_INTERNAL_ERROR, NULL); +} +#else +#define verify_nodes(hdr, nodes) +#endif - int32_t column = 0; - if (!first && column_out && column_out[0] != 0) { - column = column_out[0] - 1; +static +flecs_switch_header_t *get_header( + const ecs_switch_t *sw, + uint64_t value) +{ + if (value == 0) { + return NULL; } - column = ecs_type_match(world, match_table, match_type, - column, term->id, subj->set.relation, subj->set.min_depth, - subj->set.max_depth, &source, match_index_out); - - bool result = column != -1; + value = (uint32_t)value; - if (oper == EcsNot) { - result = !result; - } + ecs_assert(value >= sw->min, ECS_INTERNAL_ERROR, NULL); + ecs_assert(value <= sw->max, ECS_INTERNAL_ERROR, NULL); - if (oper == EcsOptional) { - result = true; - } + uint64_t index = value - sw->min; - if (subj_entity != EcsThis) { - if (!source) { - source = subj_entity; - } - } + return &sw->headers[index]; +} - if (column_out && result) { - ecs_assert(id_out != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(subject_out != NULL, ECS_INTERNAL_ERROR, NULL); +static +void remove_node( + flecs_switch_header_t *hdr, + flecs_switch_node_t *nodes, + flecs_switch_node_t *node, + int32_t element) +{ + ecs_assert(&nodes[element] == node, ECS_INTERNAL_ERROR, NULL); - populate_from_column(world, table, offset, term->id, column, - source, id_out, subject_out, size_out, ptr_out); + /* Update previous node/header */ + if (hdr->element == element) { + ecs_assert(node->prev == -1, ECS_INVALID_PARAMETER, NULL); + /* If this is the first node, update the header */ + hdr->element = node->next; + } else { + /* If this is not the first node, update the previous node to the + * removed node's next ptr */ + ecs_assert(node->prev != -1, ECS_INVALID_PARAMETER, NULL); + flecs_switch_node_t *prev_node = &nodes[node->prev]; + prev_node->next = node->next; + } - if (column != -1) { - column_out[0] = column + 1; - } else { - column_out[0] = 0; - } + /* Update next node */ + int32_t next = node->next; + if (next != -1) { + ecs_assert(next >= 0, ECS_INVALID_PARAMETER, NULL); + /* If this is not the last node, update the next node to point to the + * removed node's prev ptr */ + flecs_switch_node_t *next_node = &nodes[next]; + next_node->prev = node->prev; } - return result; + /* Decrease count of current header */ + hdr->count --; + ecs_assert(hdr->count >= 0, ECS_INTERNAL_ERROR, NULL); } -bool flecs_filter_match_table( - ecs_world_t *world, - const ecs_filter_t *filter, - const ecs_table_t *table, - ecs_type_t type, - int32_t offset, - ecs_id_t *ids, - int32_t *columns, - ecs_entity_t *subjects, - ecs_size_t *sizes, - void **ptrs, - int32_t *match_indices, - int32_t *matches_left, - bool first, - int32_t skip_term) +ecs_switch_t* flecs_switch_new( + uint64_t min, + uint64_t max, + int32_t elements) { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(filter != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(min <= max, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!filter->term_cache_used || filter->terms == filter->term_cache, - ECS_INTERNAL_ERROR, NULL); + /* Min must be larger than 0, as 0 is an invalid entity id, and should + * therefore never occur as case id */ + ecs_assert(min > 0, ECS_INVALID_PARAMETER, NULL); - ecs_term_t *terms = filter->terms; - int32_t i, count = filter->term_count; + ecs_switch_t *result = ecs_os_malloc(ECS_SIZEOF(ecs_switch_t)); + result->min = (uint32_t)min; + result->max = (uint32_t)max; - bool is_or = false; - bool or_result = false; - int32_t match_count = 0; + int32_t count = (int32_t)(max - min) + 1; + result->headers = ecs_os_calloc(ECS_SIZEOF(flecs_switch_header_t) * count); + result->nodes = ecs_vector_new(flecs_switch_node_t, elements); + result->values = ecs_vector_new(uint64_t, elements); + int64_t i; for (i = 0; i < count; i ++) { - if (i == skip_term) { - continue; - } + result->headers[i].element = -1; + result->headers[i].count = 0; + } - ecs_term_t *term = &terms[i]; - ecs_term_id_t *subj = &term->subj; - ecs_oper_kind_t oper = term->oper; - const ecs_table_t *match_table = table; - ecs_type_t match_type = type; - int32_t t_i = term->index; + flecs_switch_node_t *nodes = ecs_vector_first( + result->nodes, flecs_switch_node_t); + uint64_t *values = ecs_vector_first( + result->values, uint64_t); - if (!is_or && oper == EcsOr) { - is_or = true; - or_result = false; - } else if (is_or && oper != EcsOr) { - if (!or_result) { - return false; - } + for (i = 0; i < elements; i ++) { + nodes[i].prev = -1; + nodes[i].next = -1; + values[i] = 0; + } - is_or = false; - } + return result; +} - ecs_entity_t subj_entity = subj->entity; - if (!subj_entity) { - if (ids) { - ids[t_i] = term->id; - } - continue; - } +void flecs_switch_free( + ecs_switch_t *sw) +{ + ecs_os_free(sw->headers); + ecs_vector_free(sw->nodes); + ecs_vector_free(sw->values); + ecs_os_free(sw); +} - if (subj_entity != EcsThis) { - match_table = ecs_get_table(world, subj_entity); - if (match_table) { - match_type = match_table->type; - } else { - match_type = NULL; - } - } +void flecs_switch_add( + ecs_switch_t *sw) +{ + flecs_switch_node_t *node = ecs_vector_add(&sw->nodes, flecs_switch_node_t); + uint64_t *value = ecs_vector_add(&sw->values, uint64_t); + node->prev = -1; + node->next = -1; + *value = 0; +} - bool result = flecs_term_match_table(world, term, match_table, - match_type, offset, - ids ? &ids[t_i] : NULL, - columns ? &columns[t_i] : NULL, - subjects ? &subjects[t_i] : NULL, - sizes ? &sizes[t_i] : NULL, - ptrs ? &ptrs[t_i] : NULL, - match_indices ? &match_indices[t_i] : NULL, - first); +void flecs_switch_set_count( + ecs_switch_t *sw, + int32_t count) +{ + int32_t old_count = ecs_vector_count(sw->nodes); + if (old_count == count) { + return; + } - if (is_or) { - or_result |= result; - } else if (!result) { - return false; - } + ecs_vector_set_count(&sw->nodes, flecs_switch_node_t, count); + ecs_vector_set_count(&sw->values, uint64_t, count); - /* Match indices is populated with the number of matches for this term. - * This is used to determine whether to keep iterating this table. */ - if (first && match_indices && match_indices[t_i]) { - match_indices[t_i] --; - match_count += match_indices[t_i]; - } + flecs_switch_node_t *nodes = ecs_vector_first(sw->nodes, flecs_switch_node_t); + uint64_t *values = ecs_vector_first(sw->values, uint64_t); + + int32_t i; + for (i = old_count; i < count; i ++) { + flecs_switch_node_t *node = &nodes[i]; + node->prev = -1; + node->next = -1; + values[i] = 0; } +} - if (matches_left) { - *matches_left = match_count; +void flecs_switch_ensure( + ecs_switch_t *sw, + int32_t count) +{ + int32_t old_count = ecs_vector_count(sw->nodes); + if (old_count >= count) { + return; } - return !is_or || or_result; + flecs_switch_set_count(sw, count); } -static -void term_iter_init_no_data( - ecs_term_iter_t *iter) +void flecs_switch_addn( + ecs_switch_t *sw, + int32_t count) { - iter->term = (ecs_term_t){ .index = -1 }; - iter->self_index = NULL; - iter->index = 0; + int32_t old_count = ecs_vector_count(sw->nodes); + flecs_switch_set_count(sw, old_count + count); } -static -void term_iter_init_wildcard( - const ecs_world_t *world, - ecs_term_iter_t *iter) +void flecs_switch_set( + ecs_switch_t *sw, + int32_t element, + uint64_t value) { - iter->term = (ecs_term_t){ .index = -1 }; - iter->self_index = flecs_get_id_record(world, EcsWildcard); - iter->cur = iter->self_index; - iter->index = 0; -} - -static -void term_iter_init( - const ecs_world_t *world, - ecs_term_t *term, - ecs_term_iter_t *iter) -{ - const ecs_term_id_t *subj = &term->subj; + ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); + ecs_assert(element < ecs_vector_count(sw->values), ECS_INVALID_PARAMETER, NULL); + ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); - iter->term = *term; + uint64_t *values = ecs_vector_first(sw->values, uint64_t); + uint64_t cur_value = values[element]; - if (subj->set.mask == EcsDefaultSet || subj->set.mask & EcsSelf) { - iter->self_index = flecs_get_id_record(world, term->id); + /* If the node is already assigned to the value, nothing to be done */ + if (cur_value == value) { + return; } - if (subj->set.mask & EcsSuperSet) { - iter->set_index = flecs_get_id_record(world, - ecs_pair(subj->set.relation, EcsWildcard)); - } + flecs_switch_node_t *nodes = ecs_vector_first(sw->nodes, flecs_switch_node_t); + flecs_switch_node_t *node = &nodes[element]; - iter->index = 0; - if (iter->self_index) { - iter->cur = iter->self_index; - } else { - iter->cur = iter->set_index; - } -} + flecs_switch_header_t *cur_hdr = get_header(sw, cur_value); + flecs_switch_header_t *dst_hdr = get_header(sw, value); -ecs_iter_t ecs_term_iter( - const ecs_world_t *stage, - ecs_term_t *term) -{ - ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(term != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(term->id != 0, ECS_INVALID_PARAMETER, NULL); + verify_nodes(cur_hdr, nodes); + verify_nodes(dst_hdr, nodes); - const ecs_world_t *world = ecs_get_world(stage); + /* If value is not 0, and dst_hdr is NULL, then this is not a valid value + * for this switch */ + ecs_assert(dst_hdr != NULL || !value, ECS_INVALID_PARAMETER, NULL); - if (ecs_term_finalize(world, NULL, term)) { - /* Invalid term */ - ecs_abort(ECS_INVALID_PARAMETER, NULL); + if (cur_hdr) { + remove_node(cur_hdr, nodes, node, element); } - ecs_iter_t it = { - .real_world = (ecs_world_t*)world, - .world = (ecs_world_t*)stage, - .terms = term, - .term_count = 1, - .next = ecs_term_next - }; + /* Now update the node itself by adding it as the first node of dst */ + node->prev = -1; + values[element] = value; - term_iter_init(world, term, &it.iter.term); + if (dst_hdr) { + node->next = dst_hdr->element; - return it; -} + /* Also update the dst header */ + int32_t first = dst_hdr->element; + if (first != -1) { + ecs_assert(first >= 0, ECS_INTERNAL_ERROR, NULL); + flecs_switch_node_t *first_node = &nodes[first]; + first_node->prev = element; + } -static -const ecs_table_record_t *next_table( - ecs_term_iter_t *iter) -{ - const ecs_table_record_t *tables = flecs_id_record_tables(iter->cur); - int32_t count = flecs_id_record_count(iter->cur); - if (iter->index >= count) { - return NULL; + dst_hdr->element = element; + dst_hdr->count ++; } - - return &tables[iter->index ++]; } -static -ecs_table_record_t term_iter_next( - ecs_world_t *world, - ecs_term_iter_t *iter, - ecs_entity_t *source_out, - bool match_prefab, - bool match_disabled) +void flecs_switch_remove( + ecs_switch_t *sw, + int32_t element) { - ecs_table_t *table = iter->table; - ecs_entity_t source = 0; - const ecs_table_record_t *tr; - ecs_table_record_t result = { .table = NULL }; - ecs_term_t *term = &iter->term; - - do { - if (table) { - iter->cur_match ++; - if (iter->cur_match >= iter->match_count) { - table = NULL; - } else { - result.table = table; - result.count = iter->match_count; - result.column = iter->last_column = ecs_type_index_of( - table->type, iter->last_column + 1, term->id); - } - } - - if (!table) { - if (!(tr = next_table(iter))) { - if (iter->cur != iter->set_index && iter->set_index != NULL) { - iter->cur = iter->set_index; - iter->index = 0; - tr = next_table(iter); - } - - if (!tr) { - result.table = NULL; - return result; - } - } - - table = tr->table; - - if (!match_prefab && (table->flags & EcsTableIsPrefab)) { - continue; - } + ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); + ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); - if (!match_disabled && (table->flags & EcsTableIsDisabled)) { - continue; - } + uint64_t *values = ecs_vector_first(sw->values, uint64_t); + uint64_t value = values[element]; + flecs_switch_node_t *nodes = ecs_vector_first(sw->nodes, flecs_switch_node_t); + flecs_switch_node_t *node = &nodes[element]; - if (!ecs_table_count(table)) { - continue; - } + /* If node is currently assigned to a case, remove it from the list */ + if (value != 0) { + flecs_switch_header_t *hdr = get_header(sw, value); + ecs_assert(hdr != NULL, ECS_INTERNAL_ERROR, NULL); - iter->table = table; - iter->match_count = tr->count; - iter->cur_match = 0; - iter->last_column = tr->column; + verify_nodes(hdr, nodes); + remove_node(hdr, nodes, node, element); + } - result = *tr; + int32_t last_elem = ecs_vector_count(sw->nodes) - 1; + if (last_elem != element) { + flecs_switch_node_t *last = ecs_vector_last(sw->nodes, flecs_switch_node_t); + int32_t next = last->next, prev = last->prev; + if (next != -1) { + flecs_switch_node_t *n = &nodes[next]; + n->prev = element; } - if (iter->cur == iter->set_index) { - const ecs_term_id_t *subj = &term->subj; - - if (iter->self_index) { - if (flecs_id_record_table(iter->self_index, table) != NULL) { - /* If the table has the id itself and this term matched Self - * we already matched it */ - continue; - } - } - - /* Test if following the relation finds the id */ - int32_t index = ecs_type_match(world, table, table->type, 0, - term->id, subj->set.relation, subj->set.min_depth, - subj->set.max_depth, &source, NULL); - if (index == -1) { - continue; + if (prev != -1) { + flecs_switch_node_t *n = &nodes[prev]; + n->next = element; + } else { + flecs_switch_header_t *hdr = get_header(sw, values[last_elem]); + if (hdr && hdr->element != -1) { + ecs_assert(hdr->element == last_elem, + ECS_INTERNAL_ERROR, NULL); + hdr->element = element; } - - ecs_assert(source != 0, ECS_INTERNAL_ERROR, NULL); } - - break; - } while (true); - - if (source_out) { - *source_out = source; } - return result; + /* Remove element from arrays */ + ecs_vector_remove(sw->nodes, flecs_switch_node_t, element); + ecs_vector_remove(sw->values, uint64_t, element); } -bool ecs_term_next( - ecs_iter_t *it) +uint64_t flecs_switch_get( + const ecs_switch_t *sw, + int32_t element) { - ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(it->next == ecs_term_next, ECS_INVALID_PARAMETER, NULL); + ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); + ecs_assert(element < ecs_vector_count(sw->values), ECS_INVALID_PARAMETER, NULL); + ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); - ecs_term_iter_t *iter = &it->iter.term; - ecs_term_t *term = &iter->term; - ecs_world_t *world = it->real_world; + uint64_t *values = ecs_vector_first(sw->values, uint64_t); + return values[element]; +} - ecs_entity_t source = 0; - ecs_table_record_t tr = term_iter_next(world, iter, &source, false, false); - ecs_table_t *table = tr.table; - if (!table) { - it->is_valid = false; - return false; - } +ecs_vector_t* flecs_switch_values( + const ecs_switch_t *sw) +{ + return sw->values; +} - /* Source must either be 0 (EcsThis) or nonzero in case of substitution */ - ecs_assert(source || iter->cur != iter->set_index, ECS_INTERNAL_ERROR, NULL); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); +int32_t flecs_switch_case_count( + const ecs_switch_t *sw, + uint64_t value) +{ + flecs_switch_header_t *hdr = get_header(sw, value); + if (!hdr) { + return 0; + } - it->table = table; - it->type = table->type; - it->ids = &iter->id; - it->columns = &iter->column; - it->subjects = &iter->subject; - it->sizes = &iter->size; - it->ptrs = &iter->ptr; + return hdr->count; +} - it->count = ecs_table_count(table); - it->entities = ecs_vector_first(table->storage.entities, ecs_entity_t); - it->is_valid = true; +void flecs_switch_swap( + ecs_switch_t *sw, + int32_t elem_1, + int32_t elem_2) +{ + uint64_t v1 = flecs_switch_get(sw, elem_1); + uint64_t v2 = flecs_switch_get(sw, elem_2); - bool has_data = populate_from_column(world, table, 0, term->id, tr.column, - source, &iter->id, &iter->subject, &iter->size, - &iter->ptr); + flecs_switch_set(sw, elem_2, v1); + flecs_switch_set(sw, elem_1, v2); +} - if (!source) { - if (has_data) { - iter->column = tr.column + 1; - } else { - iter->column = 0; - } - } else { - iter->column = -1; /* Point to ref */ - } +int32_t flecs_switch_first( + const ecs_switch_t *sw, + uint64_t value) +{ + ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert((uint32_t)value <= sw->max, ECS_INVALID_PARAMETER, NULL); + ecs_assert((uint32_t)value >= sw->min, ECS_INVALID_PARAMETER, NULL); + + flecs_switch_header_t *hdr = get_header(sw, value); + ecs_assert(hdr != NULL, ECS_INVALID_PARAMETER, NULL); - return true; + return hdr->element; } -static -const ecs_filter_t* init_filter_iter( - const ecs_world_t *world, - ecs_iter_t *it, - const ecs_filter_t *filter) +int32_t flecs_switch_next( + const ecs_switch_t *sw, + int32_t element) { - ecs_filter_iter_t *iter = &it->iter.filter; - - if (filter) { - iter->filter = *filter; + ecs_assert(sw != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(element < ecs_vector_count(sw->nodes), ECS_INVALID_PARAMETER, NULL); + ecs_assert(element >= 0, ECS_INVALID_PARAMETER, NULL); - if (filter->term_cache_used) { - iter->filter.terms = iter->filter.term_cache; - } + flecs_switch_node_t *nodes = ecs_vector_first( + sw->nodes, flecs_switch_node_t); - ecs_filter_finalize(world, &iter->filter); + return nodes[element].next; +} - ecs_assert(!filter->term_cache_used || - filter->terms == filter->term_cache, ECS_INTERNAL_ERROR, NULL); - } else { - ecs_filter_init(world, &iter->filter, &(ecs_filter_desc_t) { - .terms = {{ .id = EcsWildcard }} - }); - filter = &iter->filter; +static +void ensure( + ecs_bitset_t *bs, + ecs_size_t size) +{ + if (!bs->size) { + int32_t new_size = ((size - 1) / 64 + 1) * ECS_SIZEOF(uint64_t); + bs->size = ((size - 1) / 64 + 1) * 64; + bs->data = ecs_os_calloc(new_size); + } else if (size > bs->size) { + int32_t prev_size = ((bs->size - 1) / 64 + 1) * ECS_SIZEOF(uint64_t); + bs->size = ((size - 1) / 64 + 1) * 64; + int32_t new_size = ((size - 1) / 64 + 1) * ECS_SIZEOF(uint64_t); + bs->data = ecs_os_realloc(bs->data, new_size); + ecs_os_memset(ECS_OFFSET(bs->data, prev_size), 0, new_size - prev_size); } +} - it->term_count = filter->term_count_actual; +void flecs_bitset_init( + ecs_bitset_t* bs) +{ + bs->size = 0; + bs->count = 0; + bs->data = NULL; +} - return filter; +void flecs_bitset_ensure( + ecs_bitset_t *bs, + int32_t count) +{ + if (count > bs->count) { + bs->count = count; + ensure(bs, count); + } } -ecs_iter_t ecs_filter_iter( - const ecs_world_t *stage, - const ecs_filter_t *filter) +void flecs_bitset_deinit( + ecs_bitset_t *bs) { - ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_os_free(bs->data); +} - const ecs_world_t *world = ecs_get_world(stage); +void flecs_bitset_addn( + ecs_bitset_t *bs, + int32_t count) +{ + int32_t elem = bs->count += count; + ensure(bs, elem); +} - ecs_iter_t it = { - .real_world = (ecs_world_t*)world, - .world = (ecs_world_t*)stage, - .terms = filter ? filter->terms : NULL, - .next = ecs_filter_next - }; +void flecs_bitset_set( + ecs_bitset_t *bs, + int32_t elem, + bool value) +{ + ecs_assert(elem < bs->count, ECS_INVALID_PARAMETER, NULL); + int32_t hi = elem >> 6; + int32_t lo = elem & 0x3F; + uint64_t v = bs->data[hi]; + bs->data[hi] = (v & ~((uint64_t)1 << lo)) | ((uint64_t)value << lo); +} - ecs_filter_iter_t *iter = &it.iter.filter; +bool flecs_bitset_get( + const ecs_bitset_t *bs, + int32_t elem) +{ + ecs_assert(elem < bs->count, ECS_INVALID_PARAMETER, NULL); + return !!(bs->data[elem >> 6] & ((uint64_t)1 << ((uint64_t)elem & 0x3F))); +} - filter = init_filter_iter(world, &it, filter); +int32_t flecs_bitset_count( + const ecs_bitset_t *bs) +{ + return bs->count; +} - int32_t i, term_count = filter->term_count; - ecs_term_t *terms = filter->terms; - int32_t min_count = -1; - int32_t min_term_index = -1; +void flecs_bitset_remove( + ecs_bitset_t *bs, + int32_t elem) +{ + ecs_assert(elem < bs->count, ECS_INVALID_PARAMETER, NULL); + int32_t last = bs->count - 1; + bool last_value = flecs_bitset_get(bs, last); + flecs_bitset_set(bs, elem, last_value); + bs->count --; +} - /* Find term that represents smallest superset */ - if (filter->match_this) { - iter->kind = EcsFilterIterEvalIndex; +void flecs_bitset_swap( + ecs_bitset_t *bs, + int32_t elem_a, + int32_t elem_b) +{ + ecs_assert(elem_a < bs->count, ECS_INVALID_PARAMETER, NULL); + ecs_assert(elem_b < bs->count, ECS_INVALID_PARAMETER, NULL); - for (i = 0; i < term_count; i ++) { - ecs_term_t *term = &terms[i]; + bool a = flecs_bitset_get(bs, elem_a); + bool b = flecs_bitset_get(bs, elem_b); + flecs_bitset_set(bs, elem_a, b); + flecs_bitset_set(bs, elem_b, a); +} - ecs_assert(term != NULL, ECS_INTERNAL_ERROR, NULL); +#ifndef _MSC_VER +#pragma GCC diagnostic ignored "-Wimplicit-fallthrough" +#endif - if (term->oper != EcsAnd) { - continue; - } +/* See explanation below. The hashing function may read beyond the memory passed + * into the hashing function, but only at word boundaries. This should be safe, + * but trips up address sanitizers and valgrind. + * This ensures clean valgrind logs in debug mode & the best perf in release */ +#if !defined(NDEBUG) || defined(ADDRESS_SANITIZER) +#ifndef VALGRIND +#define VALGRIND +#endif +#endif - if (term->subj.entity != EcsThis) { - continue; - } +/* +------------------------------------------------------------------------------- +lookup3.c, by Bob Jenkins, May 2006, Public Domain. + http://burtleburtle.net/bob/c/lookup3.c +------------------------------------------------------------------------------- +*/ - ecs_id_record_t *idr = flecs_get_id_record(world, term->id); - if (!idr) { - /* If one of the terms does not match with any data, iterator - * should not return anything */ - term_iter_init_no_data(&iter->term_iter); - return it; - } +#ifdef _MSC_VER +//FIXME +#else +#include /* attempt to define endianness */ +#endif +#ifdef linux +# include /* attempt to define endianness */ +#endif - int32_t table_count = flecs_id_record_count(idr); - if (min_count == -1 || table_count < min_count) { - min_count = table_count; - min_term_index = i; - } - } +/* + * My best guess at if you are big-endian or little-endian. This may + * need adjustment. + */ +#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ + __BYTE_ORDER == __LITTLE_ENDIAN) || \ + (defined(i386) || defined(__i386__) || defined(__i486__) || \ + defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL)) +# define HASH_LITTLE_ENDIAN 1 +#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ + __BYTE_ORDER == __BIG_ENDIAN) || \ + (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) +# define HASH_LITTLE_ENDIAN 0 +#else +# define HASH_LITTLE_ENDIAN 0 +#endif - iter->min_term_index = min_term_index; +#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) - if (min_term_index == -1) { - term_iter_init_wildcard(world, &iter->term_iter); - } else { - term_iter_init(world, &terms[min_term_index], &iter->term_iter); - } - } else { - /* If filter has no this terms, no tables need to be evaluated */ - iter->kind = EcsFilterIterEvalNone; - } - - if (filter->terms == filter->term_cache) { - /* Because we're returning the iterator by value, the address of the - * term cache changes. The ecs_filter_next function will set the correct - * address when it detects that terms is set to NULL */ - iter->filter.terms = NULL; - } - - return it; +/* +------------------------------------------------------------------------------- +mix -- mix 3 32-bit values reversibly. +This is reversible, so any information in (a,b,c) before mix() is +still in (a,b,c) after mix(). +If four pairs of (a,b,c) inputs are run through mix(), or through +mix() in reverse, there are at least 32 bits of the output that +are sometimes the same for one pair and different for another pair. +This was tested for: +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. +Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that +satisfy this are + 4 6 8 16 19 4 + 9 15 3 18 27 15 + 14 9 3 7 17 3 +Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing +for "differ" defined as + with a one-bit base and a two-bit delta. I +used http://burtleburtle.net/bob/hash/avalanche.html to choose +the operations, constants, and arrangements of the variables. +This does not achieve avalanche. There are input bits of (a,b,c) +that fail to affect some output bits of (a,b,c), especially of a. The +most thoroughly mixed value is c, but it doesn't really even achieve +avalanche in c. +This allows some parallelism. Read-after-writes are good at doubling +the number of bits affected, so the goal of mixing pulls in the opposite +direction as the goal of parallelism. I did what I could. Rotates +seem to cost as much as shifts on every machine I could lay my hands +on, and rotates are much kinder to the top and bottom bits, so I used +rotates. +------------------------------------------------------------------------------- +*/ +#define mix(a,b,c) \ +{ \ + a -= c; a ^= rot(c, 4); c += b; \ + b -= a; b ^= rot(a, 6); a += c; \ + c -= b; c ^= rot(b, 8); b += a; \ + a -= c; a ^= rot(c,16); c += b; \ + b -= a; b ^= rot(a,19); a += c; \ + c -= b; c ^= rot(b, 4); b += a; \ } -ecs_iter_t ecs_filter_chain_iter( - ecs_iter_t *chain_it, - const ecs_filter_t *filter) -{ - ecs_iter_t it = { - .chain_it = chain_it, - .next = ecs_filter_next, - .world = chain_it->world, - .real_world = chain_it->real_world - }; - - ecs_filter_iter_t *iter = &it.iter.filter; - init_filter_iter(it.world, &it, filter); - - iter->kind = EcsFilterIterEvalChain; - - if (filter->terms == filter->term_cache) { - /* See ecs_filter_iter*/ - iter->filter.terms = NULL; - } - - return it; +/* +------------------------------------------------------------------------------- +final -- final mixing of 3 32-bit values (a,b,c) into c +Pairs of (a,b,c) values differing in only a few bits will usually +produce values of c that look totally different. This was tested for +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. +These constants passed: + 14 11 25 16 4 14 24 + 12 14 25 16 4 14 24 +and these came close: + 4 8 15 26 3 22 24 + 10 8 15 26 3 22 24 + 11 8 15 26 3 22 24 +------------------------------------------------------------------------------- +*/ +#define final(a,b,c) \ +{ \ + c ^= b; c -= rot(b,14); \ + a ^= c; a -= rot(c,11); \ + b ^= a; b -= rot(a,25); \ + c ^= b; c -= rot(b,16); \ + a ^= c; a -= rot(c,4); \ + b ^= a; b -= rot(a,14); \ + c ^= b; c -= rot(b,24); \ } -bool ecs_filter_next( - ecs_iter_t *it) -{ - ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(it->next == ecs_filter_next, ECS_INVALID_PARAMETER, NULL); - ecs_assert(it->chain_it != it, ECS_INVALID_PARAMETER, NULL); - - ecs_filter_iter_t *iter = &it->iter.filter; - ecs_filter_t *filter = &iter->filter; - ecs_world_t *world = it->real_world; - ecs_table_t *table; - bool match; - int i; - - if (!filter->terms) { - filter->terms = filter->term_cache; - } - - flecs_iter_init(it); - - if (iter->kind == EcsFilterIterEvalIndex) { - ecs_term_iter_t *term_iter = &iter->term_iter; - ecs_term_t *term = &term_iter->term; - int32_t term_index = term->index; - - do { - ecs_assert(iter->matches_left >= 0, ECS_INTERNAL_ERROR, NULL); - bool first_match = iter->matches_left == 0; - - if (first_match) { - /* Find new match, starting with the leading term */ - ecs_entity_t source; - ecs_table_record_t tr = term_iter_next(world, term_iter, - &source, filter->match_prefab, filter->match_disabled); - table = tr.table; - if (!table) { - goto done; - } - - /* Populate term data as flecs_filter_match_table skips it */ - populate_from_column(world, table, 0, term->id, tr.column, source, - &it->ids[term_index], - &it->subjects[term_index], - &it->sizes[term_index], - &it->ptrs[term_index]); - - it->columns[term_index] = tr.column + 1; - } else { - /* Progress iterator to next match for table, if any */ - table = it->table; - first_match = false; - - for (i = filter->term_count_actual - 1; i >= 0; i --) { - if (it->match_indices[i] > 0) { - it->match_indices[i] --; - it->columns[i] ++; - break; - } - } - } - - /* Match the remainder of the terms */ - match = flecs_filter_match_table(world, filter, table, table->type, - 0, it->ids, it->columns, it->subjects, it->sizes, - it->ptrs, it->match_indices, &iter->matches_left, - first_match, term->index); - - /* Check if there are any terms which have more matching columns */ - if (!first_match) { - iter->matches_left = 0; - for (i = 0; i < filter->term_count_actual; i ++) { - if (it->match_indices[i] > 0) { - iter->matches_left += it->match_indices[i]; - } - } - } - } while (!match); - - populate_from_table(it, table); - goto yield; - - } else if (iter->kind == EcsFilterIterEvalChain) { - ecs_iter_t *chain_it = it->chain_it; - ecs_iter_next_action_t next = chain_it->next; - - /* If this is a chain iterator, the input iterator must be set */ - ecs_assert(chain_it != NULL, ECS_INVALID_PARAMETER, NULL); - - do { - if (!next(chain_it)) { - goto done; - } - - table = chain_it->table; - - match = flecs_filter_match_table(world, filter, table, table->type, - 0, it->ids, it->columns, it->subjects, it->sizes, - it->ptrs, it->match_indices, NULL, true, -1); - } while (!match); - - populate_from_table(it, table); - goto yield; - } - -done: - flecs_iter_fini(it); - return false; - -yield: - it->is_valid = true; - return true; -} +/* + * hashlittle2: return 2 32-bit hash values + * + * This is identical to hashlittle(), except it returns two 32-bit hash + * values instead of just one. This is good enough for hash table + * lookup with 2^^64 buckets, or if you want a second hash if you're not + * happy with the first, or if you want a probably-unique 64-bit ID for + * the key. *pc is better mixed than *pb, so use *pc first. If you want + * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)". + */ static -void observer_callback(ecs_iter_t *it) { - ecs_observer_t *o = it->ctx; - ecs_world_t *world = it->world; - - if (o->last_event_id == world->event_id) { - /* Already handled this event */ - return; - } - - ecs_iter_t user_it = *it; - user_it.term_count = o->filter.term_count_actual, - user_it.ids = NULL; - user_it.columns = NULL; - user_it.subjects = NULL; - user_it.sizes = NULL; - user_it.ptrs = NULL; - - flecs_iter_init(&user_it); - - ecs_table_t *table = it->table; - ecs_table_t *prev_table = it->other_table; - ecs_term_t *term = &o->filter.terms[it->term_index]; - - if (term->oper == EcsNot) { - table = it->other_table; - prev_table = it->table; - } - - if (!table) { - table = &world->store.root; - } - if (!prev_table) { - prev_table = &world->store.root; - } - - ecs_type_t type = table->type; - ecs_type_t prev_type = prev_table->type; - - /* Populate the column for the term that triggered. This will allow the - * matching algorithm to pick the right column in case the term is a - * wildcard matching multiple columns. */ - user_it.columns[0] = 0; - user_it.columns[it->term_index] = it->columns[0]; - - if (flecs_filter_match_table(world, &o->filter, table, type, user_it.offset, - user_it.ids, user_it.columns, user_it.subjects, user_it.sizes, - user_it.ptrs, NULL, NULL, false, -1)) - { - /* Monitor observers only trigger when the filter matches for the first - * time with an entity */ - if (o->is_monitor) { - if (world->is_fini) { - goto done; - } - - if (flecs_filter_match_table(world, &o->filter, prev_table, - prev_type, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, true, - -1)) - { - goto done; - } - - if (term->oper == EcsNot) { - /* Flip event if this is a Not, so OnAdd and OnRemove can be - * reliably used to check if we're entering or leaving the - * monitor */ - if (it->event == EcsOnAdd) { - user_it.event = EcsOnRemove; - } else if (it->event == EcsOnRemove) { - user_it.event = EcsOnAdd; - } - } - } - - user_it.ids[it->term_index] = it->event_id; - user_it.system = o->entity; - user_it.term_index = it->term_index; - user_it.self = o->self; - user_it.ctx = o->ctx; - user_it.term_count = o->filter.term_count_actual; - - o->action(&user_it); - o->last_event_id = world->event_id; - } - -done: - flecs_iter_fini(&user_it); -} - -ecs_entity_t ecs_observer_init( - ecs_world_t *world, - const ecs_observer_desc_t *desc) +void hashlittle2( + const void *key, /* the key to hash */ + size_t length, /* length of the key */ + uint32_t *pc, /* IN: primary initval, OUT: primary hash */ + uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */ { - ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); - - /* If entity is provided, create it */ - ecs_entity_t existing = desc->entity.entity; - ecs_entity_t entity = ecs_entity_init(world, &desc->entity); - - bool added = false; - EcsObserver *comp = ecs_get_mut(world, entity, EcsObserver, &added); - if (added) { - ecs_observer_t *observer = flecs_sparse_add( - world->observers, ecs_observer_t); - ecs_assert(observer != NULL, ECS_INTERNAL_ERROR, NULL); - observer->id = flecs_sparse_last_id(world->observers); - - /* Make writeable copy of filter desc so that we can set name. This will - * make debugging easier, as any error messages related to creating the - * filter will have the name of the observer. */ - ecs_filter_desc_t filter_desc = desc->filter; - filter_desc.name = desc->entity.name; - - /* Parse filter */ - if (ecs_filter_init(world, &observer->filter, &filter_desc)) { - flecs_observer_fini(world, observer); - return 0; - } - - ecs_filter_t *filter = &observer->filter; - - int i; - for (i = 0; i < ECS_TRIGGER_DESC_EVENT_COUNT_MAX; i ++) { - ecs_entity_t event = desc->events[i]; - if (!event) { - break; - } - - if (event == EcsMonitor) { - /* Monitor event must be first and last event */ - ecs_assert(i == 0, ECS_INVALID_PARAMETER, NULL); - - observer->events[0] = EcsOnAdd; - observer->events[1] = EcsOnRemove; - observer->event_count ++; - observer->is_monitor = true; - } else { - observer->events[i] = event; - } - - observer->event_count ++; - } - - /* Observer must have at least one event */ - ecs_assert(observer->event_count != 0, ECS_INVALID_PARAMETER, NULL); - - /* Create a trigger for each term in the filter */ - observer->triggers = ecs_os_malloc_n(ecs_entity_t, - observer->filter.term_count); - - for (i = 0; i < filter->term_count; i ++) { - const ecs_term_t *terms = filter->terms; - const ecs_term_t *t = &terms[i]; - - if (terms[i].subj.entity != EcsThis) { - observer->triggers[i] = 0; - continue; - } - - ecs_trigger_desc_t trigger_desc = { - .term = *t, - .callback = observer_callback, - .ctx = observer, - .binding_ctx = desc->binding_ctx, - .match_prefab = observer->filter.match_prefab, - .match_disabled = observer->filter.match_disabled - }; - - ecs_os_memcpy(trigger_desc.events, observer->events, - ECS_SIZEOF(ecs_entity_t) * observer->event_count); - - observer->triggers[i] = ecs_trigger_init(world, &trigger_desc); - } - - observer->action = desc->callback; - observer->self = desc->self; - observer->ctx = desc->ctx; - observer->binding_ctx = desc->binding_ctx; - observer->ctx_free = desc->ctx_free; - observer->binding_ctx_free = desc->binding_ctx_free; - observer->entity = entity; + uint32_t a,b,c; /* internal state */ + union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */ - comp->observer = observer; + /* Set up the internal state */ + a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc; + c += *pb; - if (desc->entity.name) { - ecs_trace("#[green]observer#[reset] %s created", - ecs_get_name(world, entity)); - } - } else { - ecs_assert(comp->observer != NULL, ECS_INTERNAL_ERROR, NULL); + u.ptr = key; + if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { + const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ + const uint8_t *k8; + (void)k8; - /* If existing entity handle was provided, override existing params */ - if (existing) { - if (desc->callback) { - ((ecs_observer_t*)comp->observer)->action = desc->callback; - } - if (desc->ctx) { - ((ecs_observer_t*)comp->observer)->ctx = desc->ctx; - } - if (desc->binding_ctx) { - ((ecs_observer_t*)comp->observer)->binding_ctx = - desc->binding_ctx; - } - } + /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; } - return entity; -} + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]&0xffffff" actually reads beyond the end of the string, but + * then masks off the part it's not allowed to read. Because the + * string is aligned, the masked-off tail is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticably faster for short strings (like English words). + */ +#ifndef VALGRIND -void flecs_observer_fini( - ecs_world_t *world, - ecs_observer_t *observer) -{ - int i, count = observer->filter.term_count; - for (i = 0; i < count; i ++) { - ecs_entity_t trigger = observer->triggers[i]; - if (trigger) { - ecs_delete(world, trigger); - } + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff; a+=k[0]; break; + case 6 : b+=k[1]&0xffff; a+=k[0]; break; + case 5 : b+=k[1]&0xff; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff; break; + case 2 : a+=k[0]&0xffff; break; + case 1 : a+=k[0]&0xff; break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ } - ecs_os_free(observer->triggers); - - ecs_filter_fini(&observer->filter); - if (observer->ctx_free) { - observer->ctx_free(observer->ctx); - } +#else /* make valgrind happy */ - if (observer->binding_ctx_free) { - observer->binding_ctx_free(observer->binding_ctx); + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ + case 1 : a+=k8[0]; break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ } - flecs_sparse_remove(world->observers, observer->id); -} - -void* ecs_get_observer_ctx( - const ecs_world_t *world, - ecs_entity_t observer) -{ - const EcsObserver *o = ecs_get(world, observer, EcsObserver); - if (o) { - return o->observer->ctx; - } else { - return NULL; - } -} - -void* ecs_get_observer_binding_ctx( - const ecs_world_t *world, - ecs_entity_t observer) -{ - const EcsObserver *o = ecs_get(world, observer, EcsObserver); - if (o) { - return o->observer->binding_ctx; - } else { - return NULL; - } -} - -/* Move table from empty to non-empty list, or vice versa */ -static -int32_t move_table( - ecs_table_cache_t *cache, - const ecs_table_t *table, - int32_t index, - ecs_vector_t **dst_array, - ecs_vector_t *src_array, - bool empty) -{ - (void)table; - - int32_t new_index = 0, old_index = 0; - ecs_size_t size = cache->size; - int32_t last_src_index = ecs_vector_count(src_array) - 1; - ecs_assert(last_src_index >= 0, ECS_INTERNAL_ERROR, NULL); - - ecs_table_cache_hdr_t *elem = ecs_vector_last_t(src_array, size, 8); - - /* The last table of the source array will be moved to the location of the - * table to move, do some bookkeeping to keep things consistent. */ - if (last_src_index) { - int32_t *old_index_ptr = ecs_map_get(cache->index, - int32_t, elem->table->id); - ecs_assert(old_index_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - - old_index = old_index_ptr[0]; - if (!empty) { - if (old_index >= 0) { - /* old_index should be negative if not empty, since - * we're moving from the empty list to the non-empty list. - * However, if the last table in the source array is also - * the table being moved, this can happen. */ - ecs_assert(table == elem->table, ECS_INTERNAL_ERROR, NULL); - } else { - /* If not empty, src = the empty list, and index should - * be negative. */ - old_index = old_index * -1 - 1; /* Normalize */ - } - } +#endif /* !valgrind */ - if (old_index == last_src_index) { - old_index_ptr[0] = index; - } - } else { - /* If last_src_index is 0, the table to move was the only table in the - * src array, so no other administration needs to be updated. */ - } + } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { + const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ + const uint8_t *k8; - if (!empty) { - old_index = index * -1 - 1; - } else { - old_index = index; + /*--------------- all but last block: aligned reads and different mixing */ + while (length > 12) + { + a += k[0] + (((uint32_t)k[1])<<16); + b += k[2] + (((uint32_t)k[3])<<16); + c += k[4] + (((uint32_t)k[5])<<16); + mix(a,b,c); + length -= 12; + k += 6; } - /* Actually move the table. Only move from src to dst if we have a - * dst_array, otherwise just remove it from src. */ - if (dst_array) { - new_index = ecs_vector_count(*dst_array); - ecs_vector_move_index_t(dst_array, src_array, size, 8, old_index); - - /* Make sure table is where we expect it */ - elem = ecs_vector_last_t(*dst_array, size, 8); - ecs_assert(elem->table == table, ECS_INTERNAL_ERROR, NULL); - ecs_assert(ecs_vector_count(*dst_array) == (new_index + 1), - ECS_INTERNAL_ERROR, NULL); - elem->empty = empty; - } else { - ecs_vector_remove_t(src_array, size, 8, old_index); + /*----------------------------- handle the last (probably partial) block */ + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[4]+(((uint32_t)k[5])<<16); + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=k[4]; + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=k[2]; + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=k[0]; + break; + case 1 : a+=k8[0]; + break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ } - /* Ensure that src array has now one element less */ - ecs_assert(ecs_vector_count(src_array) == last_src_index, - ECS_INTERNAL_ERROR, NULL); + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; - if (empty) { - /* Table is now empty, index is negative */ - new_index = new_index * -1 - 1; + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + a += ((uint32_t)k[1])<<8; + a += ((uint32_t)k[2])<<16; + a += ((uint32_t)k[3])<<24; + b += k[4]; + b += ((uint32_t)k[5])<<8; + b += ((uint32_t)k[6])<<16; + b += ((uint32_t)k[7])<<24; + c += k[8]; + c += ((uint32_t)k[9])<<8; + c += ((uint32_t)k[10])<<16; + c += ((uint32_t)k[11])<<24; + mix(a,b,c); + length -= 12; + k += 12; } - return new_index; -} - -static -void ensure_index( - ecs_table_cache_t *cache) -{ - if (!cache->index) { - cache->index = ecs_map_new(int32_t, 0); + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=((uint32_t)k[11])<<24; + case 11: c+=((uint32_t)k[10])<<16; + case 10: c+=((uint32_t)k[9])<<8; + case 9 : c+=k[8]; + case 8 : b+=((uint32_t)k[7])<<24; + case 7 : b+=((uint32_t)k[6])<<16; + case 6 : b+=((uint32_t)k[5])<<8; + case 5 : b+=k[4]; + case 4 : a+=((uint32_t)k[3])<<24; + case 3 : a+=((uint32_t)k[2])<<16; + case 2 : a+=((uint32_t)k[1])<<8; + case 1 : a+=k[0]; + break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ } -} + } -void _ecs_table_cache_init( - ecs_table_cache_t *cache, - ecs_size_t size, - ecs_poly_t *parent, - void(*free_payload)(ecs_poly_t*, void*)) -{ - ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(size >= ECS_SIZEOF(ecs_table_cache_hdr_t), - ECS_INTERNAL_ERROR, NULL); - cache->index = NULL; - cache->empty_tables = NULL; - cache->tables = NULL; - cache->size = size; - cache->parent = parent; - cache->free_payload = free_payload; + final(a,b,c); + *pc=c; *pb=b; } -static -void free_payload( - ecs_table_cache_t *cache, - ecs_vector_t *tables) +uint64_t flecs_hash( + const void *data, + ecs_size_t length) { - void(*free_payload_func)(ecs_poly_t*, void*) = cache->free_payload; - if (free_payload_func) { - ecs_poly_t *parent = cache->parent; - ecs_size_t size = cache->size; - int32_t i, count = ecs_vector_count(tables); + uint32_t h_1 = 0; + uint32_t h_2 = 0; - for (i = 0; i < count; i ++) { - void *ptr = ecs_vector_get_t(tables, size, 8, i); - free_payload_func(parent, ptr); - } - } + hashlittle2( + data, + flecs_to_size_t(length), + &h_1, + &h_2); - ecs_vector_free(tables); + return h_1 | ((uint64_t)h_2 << 32); } -void ecs_table_cache_fini( - ecs_table_cache_t *cache) +void flecs_observable_init( + ecs_observable_t *observable) { - ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_map_free(cache->index); - free_payload(cache, cache->tables); - free_payload(cache, cache->empty_tables); + observable->triggers = ecs_sparse_new(ecs_event_triggers_t); } -bool ecs_table_cache_is_initialized( - ecs_table_cache_t *cache) +void flecs_observable_fini( + ecs_observable_t *observable) { - return cache->size != 0; + ecs_sparse_t *triggers = observable->triggers; + int32_t i, count = flecs_sparse_count(triggers); + + for (i = 0; i < count; i ++) { + ecs_event_triggers_t *et = + ecs_sparse_get_dense(triggers, ecs_event_triggers_t, i); + ecs_assert(et != NULL, ECS_INTERNAL_ERROR, NULL); + + ecs_map_iter_t it = ecs_map_iter(et->triggers); + ecs_id_triggers_t *idt; + while ((idt = ecs_map_next(&it, ecs_id_triggers_t, NULL))) { + ecs_map_free(idt->triggers); + ecs_map_free(idt->set_triggers); + } + ecs_map_free(et->triggers); + } + + flecs_sparse_free(observable->triggers); } -void* _ecs_table_cache_insert( - ecs_table_cache_t *cache, - ecs_size_t size, - const ecs_table_t *table) +void ecs_emit( + ecs_world_t *world, + ecs_event_desc_t *desc) { - ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(size == cache->size, ECS_INTERNAL_ERROR, NULL); + ecs_poly_assert(world, ecs_world_t); + ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(desc->event != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(desc->event != EcsWildcard, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!table || (_ecs_table_cache_get(cache, size, table) == NULL), - ECS_INTERNAL_ERROR, NULL); + ecs_ids_t ids_storage, *ids = desc->ids; + ecs_table_t *table = NULL, *other_table = NULL; + int32_t row = 0; + int32_t count = 0; + ecs_entity_t event = desc->event; + ecs_entity_t entity = 0; - int32_t index; - ecs_table_cache_hdr_t *result; - bool empty; + world->event_id ++; - if (!table) { - empty = false; - } else { - empty = ecs_table_count(table) == 0; - } + if (desc->payload_kind == EcsPayloadEntity) { + entity = desc->payload.entity; + ecs_assert(entity != 0, ECS_INTERNAL_ERROR, NULL); + ecs_record_t *r = ecs_eis_get(world, entity); + if (r) { + bool is_watched; + table = r->table; + row = flecs_record_to_row(r->row, &is_watched); + } + count = 1; - if (empty) { - result = ecs_vector_add_t(&cache->empty_tables, size, 8); - index = -ecs_vector_count(cache->empty_tables); - } else { - index = ecs_vector_count(cache->tables); - result = ecs_vector_add_t(&cache->tables, size, 8); + } else if (desc->payload_kind == EcsPayloadTable) { + ecs_assert(desc->payload.table.table != NULL, ECS_INTERNAL_ERROR, NULL); + table = desc->payload.table.table; + other_table = desc->payload.table.other_table; + row = desc->payload.table.offset; + int32_t payload_count = desc->payload.table.count; + if (!payload_count) { + count = ecs_table_count(table) - row; + } else { + count = payload_count; + } } if (table) { - ensure_index(cache); - ecs_map_set(cache->index, table->id, &index); + if (!ids) { + ids = &ids_storage; + ids_storage.array = ecs_vector_first(table->type, ecs_id_t); + ids_storage.count = ecs_vector_count(table->type); + } } - - ecs_os_memset(result, 0, size); - result->table = (ecs_table_t*)table; - result->empty = empty; - return result; + flecs_triggers_notify(world, desc->observable, ids, event, + entity, table, other_table, row, count, desc->param); } -void _ecs_table_cache_remove( - ecs_table_cache_t *cache, - ecs_size_t size, - const ecs_table_t *table) -{ - ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(size == cache->size, ECS_INTERNAL_ERROR, NULL); - (void)size; +static +void observer_callback(ecs_iter_t *it) { + ecs_observer_t *o = it->ctx; + ecs_world_t *world = it->world; - int32_t *index = ecs_map_get(cache->index, int32_t, table->id); - if (!index) { + if (o->last_event_id == world->event_id) { + /* Already handled this event */ return; } - if (cache->free_payload) { - ecs_table_cache_hdr_t *elem = _ecs_table_cache_get( - cache, cache->size, table); - ecs_assert(elem != NULL, ECS_INTERNAL_ERROR, NULL); - cache->free_payload(cache->parent, elem); - } - - if (index[0] < 0) { - move_table(cache, table, index[0], NULL, cache->empty_tables, false); - } else { - move_table(cache, table, index[0], NULL, cache->tables, true); - } + ecs_iter_t user_it = *it; + user_it.term_count = o->filter.term_count_actual, + user_it.ids = NULL; + user_it.columns = NULL; + user_it.subjects = NULL; + user_it.sizes = NULL; + user_it.ptrs = NULL; - ecs_map_remove(cache->index, table->id); + flecs_iter_init(&user_it); - if (!ecs_map_count(cache->index)) { - ecs_assert(ecs_vector_count(cache->tables) == 0, - ECS_INTERNAL_ERROR, NULL); - ecs_assert(ecs_vector_count(cache->empty_tables) == 0, - ECS_INTERNAL_ERROR, NULL); - ecs_table_cache_fini(cache); + ecs_table_t *table = it->table; + ecs_table_t *prev_table = it->other_table; + ecs_term_t *term = &o->filter.terms[it->term_index]; - cache->index = NULL; - cache->tables = NULL; - cache->empty_tables = NULL; + if (term->oper == EcsNot) { + table = it->other_table; + prev_table = it->table; } -} - -void* _ecs_table_cache_get( - const ecs_table_cache_t *cache, - ecs_size_t size, - const ecs_table_t *table) -{ - ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(size == cache->size, ECS_INTERNAL_ERROR, NULL); - int32_t *index = ecs_map_get(cache->index, int32_t, table->id); - if (!index) { - return NULL; + if (!table) { + table = &world->store.root; } - - ecs_table_cache_hdr_t *result; - if (index[0] >= 0) { - result = ecs_vector_get_t(cache->tables, size, 8, index[0]); - } else { - result = ecs_vector_get_t( - cache->empty_tables, size, 8, index[0] * -1 - 1); + if (!prev_table) { + prev_table = &world->store.root; } - ecs_assert(!result || result->table == table, ECS_INTERNAL_ERROR, NULL); + ecs_type_t type = table->type; + ecs_type_t prev_type = prev_table->type; - return result; -} + /* Populate the column for the term that triggered. This will allow the + * matching algorithm to pick the right column in case the term is a + * wildcard matching multiple columns. */ + user_it.columns[0] = 0; + user_it.columns[it->term_index] = it->columns[0]; -void ecs_table_cache_set_empty( - ecs_table_cache_t *cache, - const ecs_table_t *table, - bool empty) -{ - ecs_assert(cache != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + if (flecs_filter_match_table(world, &o->filter, table, type, user_it.offset, + user_it.ids, user_it.columns, user_it.subjects, user_it.sizes, + user_it.ptrs, NULL, NULL, false, -1)) + { + /* Monitor observers only trigger when the filter matches for the first + * time with an entity */ + if (o->is_monitor) { + if (world->is_fini) { + goto done; + } - int32_t *index = ecs_map_get(cache->index, int32_t, table->id); - if (!index) { - return; - } + if (flecs_filter_match_table(world, &o->filter, prev_table, + prev_type, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, true, + -1)) + { + goto done; + } - /* If table is already in the correct array nothing needs to be done */ - if (empty && index[0] < 0) { - return; - } else if (!empty && index[0] >= 0) { - return; - } + if (term->oper == EcsNot) { + /* Flip event if this is a Not, so OnAdd and OnRemove can be + * reliably used to check if we're entering or leaving the + * monitor */ + if (it->event == EcsOnAdd) { + user_it.event = EcsOnRemove; + } else if (it->event == EcsOnRemove) { + user_it.event = EcsOnAdd; + } + } + } - if (index[0] < 0) { - index[0] = move_table( - cache, table, index[0], &cache->tables, cache->empty_tables, empty); - } else { - index[0] = move_table( - cache, table, index[0], &cache->empty_tables, cache->tables, empty); - } -} + user_it.ids[it->term_index] = it->event_id; + user_it.system = o->entity; + user_it.term_index = it->term_index; + user_it.self = o->self; + user_it.ctx = o->ctx; + user_it.term_count = o->filter.term_count_actual; -void* _ecs_table_cache_tables( - const ecs_table_cache_t *cache, - ecs_size_t size) -{ - if (!cache) { - return NULL; + o->action(&user_it); + o->last_event_id = world->event_id; } - return ecs_vector_first_t(cache->tables, size, 8); -} -void* _ecs_table_cache_empty_tables( - const ecs_table_cache_t *cache, - ecs_size_t size) -{ - if (!cache) { - return NULL; - } - return ecs_vector_first_t(cache->empty_tables, size, 8); +done: + flecs_iter_fini(&user_it); } -int32_t ecs_table_cache_count( - const ecs_table_cache_t *cache) +ecs_entity_t ecs_observer_init( + ecs_world_t *world, + const ecs_observer_desc_t *desc) { - if (!cache) { - return 0; - } - return ecs_vector_count(cache->tables); -} + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); -int32_t ecs_table_cache_empty_count( - const ecs_table_cache_t *cache) -{ - if (!cache) { - return 0; - } - return ecs_vector_count(cache->empty_tables); -} + /* If entity is provided, create it */ + ecs_entity_t existing = desc->entity.entity; + ecs_entity_t entity = ecs_entity_init(world, &desc->entity); -bool ecs_table_cache_is_empty( - const ecs_table_cache_t *cache) -{ - if (!cache) { - return true; - } - return ecs_map_count(cache->index) == 0; -} + bool added = false; + EcsObserver *comp = ecs_get_mut(world, entity, EcsObserver, &added); + if (added) { + ecs_observer_t *observer = flecs_sparse_add( + world->observers, ecs_observer_t); + ecs_assert(observer != NULL, ECS_INTERNAL_ERROR, NULL); + observer->id = flecs_sparse_last_id(world->observers); -void _ecs_table_cache_fini_delete_all( - ecs_world_t *world, - ecs_table_cache_t *cache, - ecs_size_t size) -{ - if (!cache || !cache->index) { - return; - } + /* Make writeable copy of filter desc so that we can set name. This will + * make debugging easier, as any error messages related to creating the + * filter will have the name of the observer. */ + ecs_filter_desc_t filter_desc = desc->filter; + filter_desc.name = desc->entity.name; - /* Temporarily set index to NULL, so that when the table tries to remove - * itself from the cache it won't be able to. This keeps the arrays we're - * iterating over consistent */ - ecs_map_t *index = cache->index; - cache->index = NULL; + /* Parse filter */ + if (ecs_filter_init(world, &observer->filter, &filter_desc)) { + flecs_observer_fini(world, observer); + return 0; + } - int32_t i, count = ecs_vector_count(cache->tables); - for (i = 0; i < count; i ++) { - ecs_table_cache_hdr_t *ptr = ecs_vector_get_t( - cache->tables, size, 8, i); - flecs_delete_table(world, ptr->table); - } + ecs_filter_t *filter = &observer->filter; - count = ecs_vector_count(cache->empty_tables); - for (i = 0; i < count; i ++) { - ecs_table_cache_hdr_t *ptr = ecs_vector_get_t( - cache->empty_tables, size, 8, i); - flecs_delete_table(world, ptr->table); - } + int i; + for (i = 0; i < ECS_TRIGGER_DESC_EVENT_COUNT_MAX; i ++) { + ecs_entity_t event = desc->events[i]; + if (!event) { + break; + } - cache->index = index; + if (event == EcsMonitor) { + /* Monitor event must be first and last event */ + ecs_assert(i == 0, ECS_INVALID_PARAMETER, NULL); - ecs_table_cache_fini(cache); -} + observer->events[0] = EcsOnAdd; + observer->events[1] = EcsOnRemove; + observer->event_count ++; + observer->is_monitor = true; + } else { + observer->events[i] = event; + } -/* -- Public API -- */ + observer->event_count ++; + } -static -bool has_case( - const ecs_world_t *world, - ecs_entity_t sw_case, - ecs_entity_t e) -{ - const EcsType *type_ptr = ecs_get(world, e & ECS_COMPONENT_MASK, EcsType); - ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); - return ecs_type_has_id(world, type_ptr->normalized, sw_case, false); -} + /* Observer must have at least one event */ + ecs_assert(observer->event_count != 0, ECS_INVALID_PARAMETER, NULL); -static -bool match_id( - const ecs_world_t *world, - ecs_entity_t id, - ecs_entity_t match_with) -{ - if (ECS_HAS_ROLE(match_with, CASE)) { - ecs_entity_t sw_case = match_with & ECS_COMPONENT_MASK; - if (ECS_HAS_ROLE(id, SWITCH) && has_case(world, sw_case, id)) { - return 1; - } else { - return 0; - } - } else { - return ecs_id_match(id, match_with); - } -} + /* Create a trigger for each term in the filter */ + observer->triggers = ecs_os_malloc_n(ecs_entity_t, + observer->filter.term_count); -static -int32_t search_type( - const ecs_world_t *world, - const ecs_table_t *table, - ecs_type_t type, - int32_t offset, - ecs_id_t id, - ecs_entity_t rel, - int32_t min_depth, - int32_t max_depth, - int32_t depth, - ecs_entity_t *subject_out, - int32_t *count_out) -{ - if (!id) { - return -1; - } + for (i = 0; i < filter->term_count; i ++) { + const ecs_term_t *terms = filter->terms; + const ecs_term_t *t = &terms[i]; - if (!type) { - return -1; - } + if (terms[i].subj.entity != EcsThis) { + observer->triggers[i] = 0; + continue; + } - if (max_depth && depth > max_depth) { - return -1; - } + ecs_trigger_desc_t trigger_desc = { + .term = *t, + .callback = observer_callback, + .ctx = observer, + .binding_ctx = desc->binding_ctx, + .match_prefab = observer->filter.match_prefab, + .match_disabled = observer->filter.match_disabled + }; - int32_t i, count = ecs_vector_count(type); - ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); + ecs_os_memcpy(trigger_desc.events, observer->events, + ECS_SIZEOF(ecs_entity_t) * observer->event_count); - if (depth >= min_depth) { - if (table && !offset && !(ECS_HAS_ROLE(id, CASE))) { - ecs_table_record_t *tr = flecs_get_table_record(world, table, id); - if (tr) { - if (count_out) { - *count_out = tr->count; - } - return tr->column; - } - } else { - for (i = offset; i < count; i ++) { - if (match_id(world, ids[i], id)) { - return i; - } - } + observer->triggers[i] = ecs_trigger_init(world, &trigger_desc); } - } - - if (rel && id != EcsPrefab && id != EcsDisabled && - ECS_PAIR_RELATION(id) != ecs_id(EcsIdentifier) && - ECS_PAIR_RELATION(id) != EcsChildOf) - { - for (i = 0; i < count; i ++) { - ecs_entity_t e = ids[i]; - if (!ECS_HAS_RELATION(e, rel)) { - continue; - } - ecs_entity_t obj = ecs_pair_object(world, e); - ecs_assert(obj != 0, ECS_INTERNAL_ERROR, NULL); + observer->action = desc->callback; + observer->self = desc->self; + observer->ctx = desc->ctx; + observer->binding_ctx = desc->binding_ctx; + observer->ctx_free = desc->ctx_free; + observer->binding_ctx_free = desc->binding_ctx_free; + observer->entity = entity; - ecs_table_t *obj_table = ecs_get_table(world, obj); - if (!obj_table) { - continue; - } + comp->observer = observer; - if ((search_type(world, obj_table, obj_table->type, 0, id, - rel, min_depth, max_depth, depth + 1, subject_out, NULL) != -1)) - { - if (subject_out && !*subject_out) { - *subject_out = obj; - } - return i; + if (desc->entity.name) { + ecs_trace("#[green]observer#[reset] %s created", + ecs_get_name(world, entity)); + } + } else { + ecs_assert(comp->observer != NULL, ECS_INTERNAL_ERROR, NULL); - /* If the id could not be found on the object and the relationship - * is not IsA, try substituting the object type with IsA */ - } else if (rel != EcsIsA) { - if (search_type(world, obj_table, obj_table->type, 0, - id, EcsIsA, 1, 0, 0, subject_out, NULL) != -1) - { - if (subject_out && !*subject_out) { - *subject_out = obj; - } - return i; - } + /* If existing entity handle was provided, override existing params */ + if (existing) { + if (desc->callback) { + ((ecs_observer_t*)comp->observer)->action = desc->callback; } - } + if (desc->ctx) { + ((ecs_observer_t*)comp->observer)->ctx = desc->ctx; + } + if (desc->binding_ctx) { + ((ecs_observer_t*)comp->observer)->binding_ctx = + desc->binding_ctx; + } + } } - return -1; + return entity; } -bool ecs_type_has_id( - const ecs_world_t *world, - ecs_type_t type, - ecs_id_t id, - bool owned) +void flecs_observer_fini( + ecs_world_t *world, + ecs_observer_t *observer) { - return search_type(world, NULL, type, 0, id, owned ? 0 : EcsIsA, 0, 0, 0, NULL, NULL) != -1; -} + int i, count = observer->filter.term_count; + for (i = 0; i < count; i ++) { + ecs_entity_t trigger = observer->triggers[i]; + if (trigger) { + ecs_delete(world, trigger); + } + } + ecs_os_free(observer->triggers); -int32_t ecs_type_index_of( - ecs_type_t type, - int32_t offset, - ecs_id_t id) -{ - return search_type(NULL, NULL, type, offset, id, 0, 0, 0, 0, NULL, NULL); + ecs_filter_fini(&observer->filter); + + if (observer->ctx_free) { + observer->ctx_free(observer->ctx); + } + + if (observer->binding_ctx_free) { + observer->binding_ctx_free(observer->binding_ctx); + } + + flecs_sparse_remove(world->observers, observer->id); } -int32_t ecs_type_match( +void* ecs_get_observer_ctx( const ecs_world_t *world, - const ecs_table_t *table, - ecs_type_t type, - int32_t offset, - ecs_id_t id, - ecs_entity_t rel, - int32_t min_depth, - int32_t max_depth, - ecs_entity_t *subject_out, - int32_t *count_out) + ecs_entity_t observer) { - if (subject_out) { - *subject_out = 0; - } - return search_type(world, table, type, offset, id, rel, min_depth, max_depth, 0, subject_out, count_out); + const EcsObserver *o = ecs_get(world, observer, EcsObserver); + if (o) { + return o->observer->ctx; + } else { + return NULL; + } } -char* ecs_type_str( +void* ecs_get_observer_binding_ctx( const ecs_world_t *world, - ecs_type_t type) + ecs_entity_t observer) { - if (!type) { - return ecs_os_strdup(""); - } - - ecs_vector_t *chbuf = ecs_vector_new(char, 32); - char *dst; - - ecs_entity_t *entities = ecs_vector_first(type, ecs_entity_t); - int32_t i, count = ecs_vector_count(type); - - for (i = 0; i < count; i ++) { - ecs_entity_t e = entities[i]; - char buffer[256]; - ecs_size_t len; - - if (i) { - *(char*)ecs_vector_add(&chbuf, char) = ','; - } - - if (e == 1) { - ecs_os_strcpy(buffer, "EcsComponent"); - len = ecs_os_strlen("EcsComponent"); - } else { - len = flecs_from_size_t(ecs_id_str_w_buf(world, e, buffer, 256)); - } - - dst = ecs_vector_addn(&chbuf, char, len); - ecs_os_memcpy(dst, buffer, len); - } - - *(char*)ecs_vector_add(&chbuf, char) = '\0'; - - char* result = ecs_os_strdup(ecs_vector_first(chbuf, char)); - ecs_vector_free(chbuf); - return result; + const EcsObserver *o = ecs_get(world, observer, EcsObserver); + if (o) { + return o->observer->binding_ctx; + } else { + return NULL; + } } void ecs_os_api_impl(ecs_os_api_t *api); @@ -31541,5773 +30895,6489 @@ void ecs_os_fatal( int32_t line, const char *msg) { - if (ecs_os_api.log_) { - ecs_os_api.log_(-4, file, line, msg); + if (ecs_os_api.log_) { + ecs_os_api.log_(-4, file, line, msg); + } +} + +static +void ecs_os_gettime(ecs_time_t *time) +{ + uint64_t now = flecs_os_time_now(); + uint64_t sec = now / 1000000000; + + assert(sec < UINT32_MAX); + assert((now - sec * 1000000000) < UINT32_MAX); + + time->sec = (uint32_t)sec; + time->nanosec = (uint32_t)(now - sec * 1000000000); +} + +static +void* ecs_os_api_malloc(ecs_size_t size) { + ecs_os_api_malloc_count ++; + ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL); + return malloc((size_t)size); +} + +static +void* ecs_os_api_calloc(ecs_size_t size) { + ecs_os_api_calloc_count ++; + ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL); + return calloc(1, (size_t)size); +} + +static +void* ecs_os_api_realloc(void *ptr, ecs_size_t size) { + ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL); + + if (ptr) { + ecs_os_api_realloc_count ++; + } else { + /* If not actually reallocing, treat as malloc */ + ecs_os_api_malloc_count ++; + } + + return realloc(ptr, (size_t)size); +} + +static +void ecs_os_api_free(void *ptr) { + if (ptr) { + ecs_os_api_free_count ++; + } + free(ptr); +} + +static +char* ecs_os_api_strdup(const char *str) { + if (str) { + int len = ecs_os_strlen(str); + char *result = ecs_os_malloc(len + 1); + ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); + ecs_os_strcpy(result, str); + return result; + } else { + return NULL; + } +} + +/* Replace dots with underscores */ +static +char *module_file_base(const char *module, char sep) { + char *base = ecs_os_strdup(module); + ecs_size_t i, len = ecs_os_strlen(base); + for (i = 0; i < len; i ++) { + if (base[i] == '.') { + base[i] = sep; + } + } + + return base; +} + +static +char* ecs_os_api_module_to_dl(const char *module) { + ecs_strbuf_t lib = ECS_STRBUF_INIT; + + /* Best guess, use module name with underscores + OS library extension */ + char *file_base = module_file_base(module, '_'); + +#if defined(ECS_OS_LINUX) + ecs_strbuf_appendstr(&lib, "lib"); + ecs_strbuf_appendstr(&lib, file_base); + ecs_strbuf_appendstr(&lib, ".so"); +#elif defined(ECS_OS_DARWIN) + ecs_strbuf_appendstr(&lib, "lib"); + ecs_strbuf_appendstr(&lib, file_base); + ecs_strbuf_appendstr(&lib, ".dylib"); +#elif defined(ECS_OS_WINDOWS) + ecs_strbuf_appendstr(&lib, file_base); + ecs_strbuf_appendstr(&lib, ".dll"); +#endif + + ecs_os_free(file_base); + + return ecs_strbuf_get(&lib); +} + +static +char* ecs_os_api_module_to_etc(const char *module) { + ecs_strbuf_t lib = ECS_STRBUF_INIT; + + /* Best guess, use module name with dashes + /etc */ + char *file_base = module_file_base(module, '-'); + + ecs_strbuf_appendstr(&lib, file_base); + ecs_strbuf_appendstr(&lib, "/etc"); + + ecs_os_free(file_base); + + return ecs_strbuf_get(&lib); +} + +void ecs_os_set_api_defaults(void) +{ + /* Don't overwrite if already initialized */ + if (ecs_os_api_initialized != 0) { + return; + } + + flecs_os_time_setup(); + + /* Memory management */ + ecs_os_api.malloc_ = ecs_os_api_malloc; + ecs_os_api.free_ = ecs_os_api_free; + ecs_os_api.realloc_ = ecs_os_api_realloc; + ecs_os_api.calloc_ = ecs_os_api_calloc; + + /* Strings */ + ecs_os_api.strdup_ = ecs_os_api_strdup; + + /* Time */ + ecs_os_api.sleep_ = flecs_os_time_sleep; + ecs_os_api.get_time_ = ecs_os_gettime; + + /* Logging */ + ecs_os_api.log_ = log_msg; + + /* Modules */ + if (!ecs_os_api.module_to_dl_) { + ecs_os_api.module_to_dl_ = ecs_os_api_module_to_dl; + } + + if (!ecs_os_api.module_to_etc_) { + ecs_os_api.module_to_etc_ = ecs_os_api_module_to_etc; + } + + ecs_os_api.abort_ = abort; +} + +bool ecs_os_has_heap(void) { + return + (ecs_os_api.malloc_ != NULL) && + (ecs_os_api.calloc_ != NULL) && + (ecs_os_api.realloc_ != NULL) && + (ecs_os_api.free_ != NULL); +} + +bool ecs_os_has_threading(void) { + return + (ecs_os_api.mutex_new_ != NULL) && + (ecs_os_api.mutex_free_ != NULL) && + (ecs_os_api.mutex_lock_ != NULL) && + (ecs_os_api.mutex_unlock_ != NULL) && + (ecs_os_api.cond_new_ != NULL) && + (ecs_os_api.cond_free_ != NULL) && + (ecs_os_api.cond_wait_ != NULL) && + (ecs_os_api.cond_signal_ != NULL) && + (ecs_os_api.cond_broadcast_ != NULL) && + (ecs_os_api.thread_new_ != NULL) && + (ecs_os_api.thread_join_ != NULL); +} + +bool ecs_os_has_time(void) { + return + (ecs_os_api.get_time_ != NULL) && + (ecs_os_api.sleep_ != NULL); +} + +bool ecs_os_has_logging(void) { + return (ecs_os_api.log_ != NULL); +} + +bool ecs_os_has_dl(void) { + return + (ecs_os_api.dlopen_ != NULL) && + (ecs_os_api.dlproc_ != NULL) && + (ecs_os_api.dlclose_ != NULL); +} + +bool ecs_os_has_modules(void) { + return + (ecs_os_api.module_to_dl_ != NULL) && + (ecs_os_api.module_to_etc_ != NULL); +} + +#if defined(_MSC_VER) +static char error_str[255]; +#endif + +const char* ecs_os_strerror(int err) { +#if defined(_MSC_VER) + strerror_s(error_str, 255, err); + return error_str; +#else + return strerror(err); +#endif +} + + +static +const ecs_entity_t* new_w_data( + ecs_world_t *world, + ecs_table_t *table, + const ecs_entity_t *entities, + ecs_ids_t *component_ids, + int32_t count, + void **c_info, + bool move, + int32_t *row_out, + ecs_table_diff_t *diff); + +static +void* get_component_w_index( + ecs_table_t *table, + int32_t column_index, + int32_t row) +{ + ecs_assert(column_index < ecs_table_storage_count(table), + ECS_NOT_A_COMPONENT, NULL); + + ecs_column_t *column = &table->storage.columns[column_index]; + + /* If size is 0, component does not have a value. This is likely caused by + * an application trying to call ecs_get with a tag. */ + int32_t size = column->size; + ecs_assert(size != 0, ECS_INVALID_PARAMETER, NULL); + + void *ptr = ecs_vector_first_t(column->data, size, column->alignment); + return ECS_OFFSET(ptr, size * row); +} + +static +void* get_component( + const ecs_world_t *world, + ecs_table_t *table, + int32_t row, + ecs_id_t id) +{ + if (!table->storage_table) { + ecs_assert(ecs_type_index_of(table->type, 0, id) == -1, + ECS_NOT_A_COMPONENT, NULL); + return NULL; + } + + ecs_table_record_t *tr = flecs_get_table_record( + world, table->storage_table, id); + if (!tr) { + ecs_assert(ecs_type_index_of(table->type, 0, id) == -1, + ECS_NOT_A_COMPONENT, NULL); + return NULL; + } + + return get_component_w_index(table, tr->column, row); +} + +static +void* get_base_component( + const ecs_world_t *world, + ecs_table_t *table, + ecs_id_t id, + ecs_id_record_t *table_index, + ecs_id_record_t *table_index_isa, + int32_t recur_depth) +{ + /* Cycle detected in IsA relation */ + ecs_assert(recur_depth < ECS_MAX_RECURSION, ECS_INVALID_PARAMETER, NULL); + + /* Table (and thus entity) does not have component, look for base */ + if (!(table->flags & EcsTableHasIsA)) { + return NULL; + } + + /* Exclude Name */ + if (id == ecs_pair(ecs_id(EcsIdentifier), EcsName)) { + return NULL; + } + + /* Should always be an id record for IsA, otherwise a table with a + * HasBase flag set should not exist. */ + if (!table_index_isa) { + ecs_id_record_t *idr = flecs_get_id_record(world, ecs_pair(EcsIsA, EcsWildcard)); + ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL); + table_index_isa = idr; + } + + /* Table should always be in the table index for (IsA, *), otherwise the + * HasBase flag should not have been set */ + const ecs_table_record_t *tr_isa = flecs_id_record_table( + table_index_isa, table); + ecs_assert(tr_isa != NULL, ECS_INTERNAL_ERROR, NULL); + + ecs_type_t type = table->type; + ecs_id_t *ids = ecs_vector_first(type, ecs_id_t); + int32_t i = tr_isa->column, end = tr_isa->count + tr_isa->column; + void *ptr = NULL; + + do { + ecs_id_t pair = ids[i ++]; + ecs_entity_t base = ecs_pair_object(world, pair); + + ecs_record_t *r = ecs_eis_get(world, base); + if (!r) { + continue; + } + + table = r->table; + if (!table) { + continue; + } + + const ecs_table_record_t *tr = flecs_id_record_table( + table_index, table); + if (!tr) { + ptr = get_base_component(world, table, id, table_index, + table_index_isa, recur_depth + 1); + } else { + bool is_monitored; + int32_t row = flecs_record_to_row(r->row, &is_monitored); + ptr = get_component_w_index(table, tr->column, row); + } + } while (!ptr && (i < end)); + + return ptr; +} + +/* Utility to compute actual row from row in record */ +static +int32_t set_row_info( + ecs_entity_info_t *info, + int32_t row) +{ + return info->row = flecs_record_to_row(row, &info->is_watched); +} + +/* Utility to set info from main stage record */ +static +void set_info_from_record( + ecs_entity_info_t *info, + ecs_record_t *record) +{ + ecs_assert(record != NULL, ECS_INTERNAL_ERROR, NULL); + + info->record = record; + + ecs_table_t *table = record->table; + + set_row_info(info, record->row); + + info->table = table; + if (!info->table) { + return; + } + + info->data = &table->storage; + + ecs_assert(ecs_vector_count(table->storage.entities) > info->row, + ECS_INTERNAL_ERROR, NULL); +} + +static +const ecs_type_info_t *get_c_info( + ecs_world_t *world, + ecs_entity_t component) +{ + ecs_entity_t real_id = ecs_get_typeid(world, component); + if (real_id) { + return flecs_get_c_info(world, real_id); + } else { + return NULL; } } static -void ecs_os_gettime(ecs_time_t *time) +void ids_merge( + ecs_ids_t *ids, + ecs_ids_t *add) { - uint64_t now = flecs_os_time_now(); - uint64_t sec = now / 1000000000; + if (!add || !add->count) { + return; + } + + int32_t new_count = ids->count + add->count; + if (new_count >= ids->size) { + ids->size = flecs_next_pow_of_2(new_count); + ecs_id_t *arr = ecs_os_malloc(ids->size * ECS_SIZEOF(ecs_id_t)); + ecs_os_memcpy_n(arr, ids->array, ecs_id_t, ids->count); - assert(sec < UINT32_MAX); - assert((now - sec * 1000000000) < UINT32_MAX); + if (ids->count >= ECS_MAX_ADD_REMOVE) { + ecs_os_free(ids->array); + } + + ids->array = arr; + } - time->sec = (uint32_t)sec; - time->nanosec = (uint32_t)(now - sec * 1000000000); + ecs_os_memcpy_n(&ids->array[ids->count], add->array, ecs_id_t, add->count); + ids->count += add->count; } -static -void* ecs_os_api_malloc(ecs_size_t size) { - ecs_os_api_malloc_count ++; - ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL); - return malloc((size_t)size); +#define ECS_TABLE_DIFF_INIT {\ + .added = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ + .removed = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ + .on_set = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ + .un_set = {.array = (ecs_id_t[ECS_MAX_ADD_REMOVE]){0}, .size = ECS_MAX_ADD_REMOVE},\ } static -void* ecs_os_api_calloc(ecs_size_t size) { - ecs_os_api_calloc_count ++; - ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL); - return calloc(1, (size_t)size); +void diff_append( + ecs_table_diff_t *dst, + ecs_table_diff_t *src) +{ + ids_merge(&dst->added, &src->added); + ids_merge(&dst->removed, &src->removed); + ids_merge(&dst->on_set, &src->on_set); + ids_merge(&dst->un_set, &src->un_set); } static -void* ecs_os_api_realloc(void *ptr, ecs_size_t size) { - ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL); - - if (ptr) { - ecs_os_api_realloc_count ++; - } else { - /* If not actually reallocing, treat as malloc */ - ecs_os_api_malloc_count ++; +void diff_free( + ecs_table_diff_t *diff) +{ + if (diff->added.count > ECS_MAX_ADD_REMOVE) { + ecs_os_free(diff->added.array); + } + if (diff->removed.count > ECS_MAX_ADD_REMOVE) { + ecs_os_free(diff->removed.array); + } + if (diff->on_set.count > ECS_MAX_ADD_REMOVE) { + ecs_os_free(diff->on_set.array); + } + if (diff->un_set.count > ECS_MAX_ADD_REMOVE) { + ecs_os_free(diff->un_set.array); } - - return realloc(ptr, (size_t)size); } static -void ecs_os_api_free(void *ptr) { - if (ptr) { - ecs_os_api_free_count ++; - } - free(ptr); +ecs_table_t* table_append( + ecs_world_t *world, + ecs_table_t *table, + ecs_id_t id, + ecs_table_diff_t *diff) +{ + ecs_table_diff_t temp_diff; + table = flecs_table_traverse_add(world, table, &id, &temp_diff); + ecs_assert(table != NULL, ECS_INVALID_PARAMETER, NULL); + diff_append(diff, &temp_diff); + return table; } static -char* ecs_os_api_strdup(const char *str) { - if (str) { - int len = ecs_os_strlen(str); - char *result = ecs_os_malloc(len + 1); - ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL); - ecs_os_strcpy(result, str); - return result; - } else { - return NULL; - } +void notify( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_t *other_table, + int32_t row, + int32_t count, + ecs_entity_t event, + ecs_ids_t *ids) +{ + ecs_emit(world, &(ecs_event_desc_t) { + .event = event, + .ids = ids, + .payload_kind = EcsPayloadTable, + .payload.table = { + .table = table, + .other_table = other_table, + .offset = row, + .count = count + } + }); } -/* Replace dots with underscores */ static -char *module_file_base(const char *module, char sep) { - char *base = ecs_os_strdup(module); - ecs_size_t i, len = ecs_os_strlen(base); - for (i = 0; i < len; i ++) { - if (base[i] == '.') { - base[i] = sep; - } +void instantiate( + ecs_world_t *world, + ecs_entity_t base, + ecs_table_t *table, + ecs_data_t *data, + int32_t row, + int32_t count); + +static +void instantiate_children( + ecs_world_t *world, + ecs_entity_t base, + ecs_table_t *table, + ecs_data_t *data, + int32_t row, + int32_t count, + ecs_table_t *child_table) +{ + if (!ecs_table_count(child_table)) { + return; } - return base; -} + ecs_type_t type = child_table->type; + ecs_data_t *child_data = &child_table->storage; -static -char* ecs_os_api_module_to_dl(const char *module) { - ecs_strbuf_t lib = ECS_STRBUF_INIT; + ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); + int32_t type_count = ecs_vector_count(type); - /* Best guess, use module name with underscores + OS library extension */ - char *file_base = module_file_base(module, '_'); + /* Instantiate child table for each instance */ -#if defined(ECS_OS_LINUX) - ecs_strbuf_appendstr(&lib, "lib"); - ecs_strbuf_appendstr(&lib, file_base); - ecs_strbuf_appendstr(&lib, ".so"); -#elif defined(ECS_OS_DARWIN) - ecs_strbuf_appendstr(&lib, "lib"); - ecs_strbuf_appendstr(&lib, file_base); - ecs_strbuf_appendstr(&lib, ".dylib"); -#elif defined(ECS_OS_WINDOWS) - ecs_strbuf_appendstr(&lib, file_base); - ecs_strbuf_appendstr(&lib, ".dll"); -#endif + /* Create component array for creating the table */ + ecs_ids_t components = { + .array = ecs_os_alloca_n(ecs_entity_t, type_count + 1) + }; - ecs_os_free(file_base); + void **component_data = ecs_os_alloca_n(void*, type_count + 1); - return ecs_strbuf_get(&lib); -} + /* Copy in component identifiers. Find the base index in the component + * array, since we'll need this to replace the base with the instance id */ + int j, i, childof_base_index = -1, pos = 0; + for (i = 0; i < type_count; i ++) { + ecs_id_t id = ids[i]; -static -char* ecs_os_api_module_to_etc(const char *module) { - ecs_strbuf_t lib = ECS_STRBUF_INIT; + /* Make sure instances don't have EcsPrefab */ + if (id == EcsPrefab) { + continue; + } - /* Best guess, use module name with dashes + /etc */ - char *file_base = module_file_base(module, '-'); + /* Keep track of the element that creates the ChildOf relationship with + * the prefab parent. We need to replace this element to make sure the + * created children point to the instance and not the prefab */ + if (ECS_HAS_RELATION(id, EcsChildOf) && (ECS_PAIR_OBJECT(id) == base)) { + childof_base_index = pos; + } - ecs_strbuf_appendstr(&lib, file_base); - ecs_strbuf_appendstr(&lib, "/etc"); + int32_t storage_index = ecs_table_type_to_storage_index(child_table, i); + if (storage_index != -1) { + ecs_column_t *column = &child_data->columns[storage_index]; + component_data[pos] = ecs_vector_first_t( + column->data, column->size, column->alignment); + } else { + component_data[pos] = NULL; + } - ecs_os_free(file_base); + components.array[pos] = id; + pos ++; + } - return ecs_strbuf_get(&lib); -} + /* Table must contain children of base */ + ecs_assert(childof_base_index != -1, ECS_INTERNAL_ERROR, NULL); -void ecs_os_set_api_defaults(void) -{ - /* Don't overwrite if already initialized */ - if (ecs_os_api_initialized != 0) { - return; + /* If children are added to a prefab, make sure they are prefabs too */ + if (table->flags & EcsTableIsPrefab) { + components.array[pos] = EcsPrefab; + component_data[pos] = NULL; + pos ++; } - flecs_os_time_setup(); - - /* Memory management */ - ecs_os_api.malloc_ = ecs_os_api_malloc; - ecs_os_api.free_ = ecs_os_api_free; - ecs_os_api.realloc_ = ecs_os_api_realloc; - ecs_os_api.calloc_ = ecs_os_api_calloc; + components.count = pos; - /* Strings */ - ecs_os_api.strdup_ = ecs_os_api_strdup; + /* Instantiate the prefab child table for each new instance */ + ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); + int32_t child_count = ecs_vector_count(child_data->entities); - /* Time */ - ecs_os_api.sleep_ = flecs_os_time_sleep; - ecs_os_api.get_time_ = ecs_os_gettime; + for (i = row; i < count + row; i ++) { + ecs_entity_t instance = entities[i]; + ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; + ecs_table_t *i_table = NULL; + + /* Replace ChildOf element in the component array with instance id */ + components.array[childof_base_index] = ecs_pair(EcsChildOf, instance); - /* Logging */ - ecs_os_api.log_ = log_msg; + /* Find or create table */ + for (j = 0; j < components.count; j ++) { + i_table = table_append(world, i_table, components.array[j], &diff); + } - /* Modules */ - if (!ecs_os_api.module_to_dl_) { - ecs_os_api.module_to_dl_ = ecs_os_api_module_to_dl; - } + ecs_assert(i_table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ecs_vector_count(i_table->type) == components.count, + ECS_INTERNAL_ERROR, NULL); - if (!ecs_os_api.module_to_etc_) { - ecs_os_api.module_to_etc_ = ecs_os_api_module_to_etc; - } + /* The instance is trying to instantiate from a base that is also + * its parent. This would cause the hierarchy to instantiate itself + * which would cause infinite recursion. */ + ecs_entity_t *children = ecs_vector_first( + child_data->entities, ecs_entity_t); +#ifndef NDEBUG + for (j = 0; j < child_count; j ++) { + ecs_entity_t child = children[j]; + ecs_assert(child != instance, ECS_INVALID_PARAMETER, NULL); + } +#endif - ecs_os_api.abort_ = abort; -} + /* Create children */ + int32_t child_row; + new_w_data(world, i_table, NULL, &components, child_count, + component_data, false, &child_row, &diff); + diff_free(&diff); -bool ecs_os_has_heap(void) { - return - (ecs_os_api.malloc_ != NULL) && - (ecs_os_api.calloc_ != NULL) && - (ecs_os_api.realloc_ != NULL) && - (ecs_os_api.free_ != NULL); + /* If prefab child table has children itself, recursively instantiate */ + ecs_data_t *i_data = &i_table->storage; + for (j = 0; j < child_count; j ++) { + ecs_entity_t child = children[j]; + instantiate(world, child, i_table, i_data, child_row + j, 1); + } + } } -bool ecs_os_has_threading(void) { - return - (ecs_os_api.mutex_new_ != NULL) && - (ecs_os_api.mutex_free_ != NULL) && - (ecs_os_api.mutex_lock_ != NULL) && - (ecs_os_api.mutex_unlock_ != NULL) && - (ecs_os_api.cond_new_ != NULL) && - (ecs_os_api.cond_free_ != NULL) && - (ecs_os_api.cond_wait_ != NULL) && - (ecs_os_api.cond_signal_ != NULL) && - (ecs_os_api.cond_broadcast_ != NULL) && - (ecs_os_api.thread_new_ != NULL) && - (ecs_os_api.thread_join_ != NULL); -} +static +void instantiate( + ecs_world_t *world, + ecs_entity_t base, + ecs_table_t *table, + ecs_data_t *data, + int32_t row, + int32_t count) +{ + ecs_table_t *base_table = ecs_get_table(world, ecs_get_alive(world, base)); + if (!base_table || !(base_table->flags & EcsTableIsPrefab)) { + /* Don't instantiate children from base entities that aren't prefabs */ + return; + } -bool ecs_os_has_time(void) { - return - (ecs_os_api.get_time_ != NULL) && - (ecs_os_api.sleep_ != NULL); -} + /* If base is a parent, instantiate children of base for instances */ + const ecs_id_record_t *idr = flecs_get_id_record( + world, ecs_pair(EcsChildOf, base)); -bool ecs_os_has_logging(void) { - return (ecs_os_api.log_ != NULL); + const ecs_table_record_t *tables = flecs_id_record_tables(idr); + int32_t i, table_count = flecs_id_record_count(idr); + for (i = 0; i < table_count; i ++) { + instantiate_children( + world, base, table, data, row, count, tables[i].table); + } } -bool ecs_os_has_dl(void) { - return - (ecs_os_api.dlopen_ != NULL) && - (ecs_os_api.dlproc_ != NULL) && - (ecs_os_api.dlclose_ != NULL); -} +static +bool override_component( + ecs_world_t *world, + ecs_entity_t component, + ecs_type_t type, + ecs_data_t *data, + ecs_column_t *column, + int32_t row, + int32_t count); -bool ecs_os_has_modules(void) { - return - (ecs_os_api.module_to_dl_ != NULL) && - (ecs_os_api.module_to_etc_ != NULL); -} +static +bool override_from_base( + ecs_world_t *world, + ecs_entity_t base, + ecs_entity_t component, + ecs_data_t *data, + ecs_column_t *column, + int32_t row, + int32_t count) +{ + ecs_assert(component != 0, ECS_INTERNAL_ERROR, NULL); -#if defined(_MSC_VER) -static char error_str[255]; -#endif + ecs_entity_info_t base_info; + ecs_assert(component != 0, ECS_INTERNAL_ERROR, NULL); -const char* ecs_os_strerror(int err) { -#if defined(_MSC_VER) - strerror_s(error_str, 255, err); - return error_str; -#else - return strerror(err); -#endif -} + if (!flecs_get_info(world, base, &base_info) || !base_info.table) { + return false; + } -#ifdef FLECS_SYSTEMS_H -#endif + void *base_ptr = get_component( + world, base_info.table, base_info.row, component); + if (base_ptr) { + int16_t data_size = column->size; + void *data_array = ecs_vector_first_t( + column->data, column->size, column->alignment); + void *data_ptr = ECS_OFFSET(data_array, data_size * row); -static -void compute_group_id( - ecs_query_t *query, - ecs_query_table_match_t *match) -{ - ecs_assert(match != NULL, ECS_INTERNAL_ERROR, NULL); + component = ecs_get_typeid(world, component); + const ecs_type_info_t *cdata = flecs_get_c_info(world, component); + int32_t index; - if (query->group_by) { - ecs_table_t *table = match->table; - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_copy_t copy = cdata ? cdata->lifecycle.copy : NULL; + if (copy) { + ecs_entity_t *entities = ecs_vector_first( + data->entities, ecs_entity_t); - match->group_id = query->group_by(query->world, table->type, - query->group_by_id, query->group_by_ctx); + void *ctx = cdata->lifecycle.ctx; + for (index = 0; index < count; index ++) { + copy(world, component, &entities[row], &base, + data_ptr, base_ptr, flecs_to_size_t(data_size), 1, ctx); + data_ptr = ECS_OFFSET(data_ptr, data_size); + } + } else { + for (index = 0; index < count; index ++) { + ecs_os_memcpy(data_ptr, base_ptr, data_size); + data_ptr = ECS_OFFSET(data_ptr, data_size); + } + } + + return true; } else { - match->group_id = 0; + /* If component not found on base, check if base itself inherits */ + ecs_type_t base_type = base_info.table->type; + return override_component(world, component, base_type, data, column, + row, count); } } static -ecs_query_table_list_t* get_group( - ecs_query_t *query, - uint64_t group_id) +bool override_component( + ecs_world_t *world, + ecs_entity_t component, + ecs_type_t type, + ecs_data_t *data, + ecs_column_t *column, + int32_t row, + int32_t count) { - return ecs_map_get(query->groups, ecs_query_table_list_t, group_id); -} + ecs_entity_t *type_array = ecs_vector_first(type, ecs_entity_t); + int32_t i, type_count = ecs_vector_count(type); -static -ecs_query_table_list_t* ensure_group( - ecs_query_t *query, - uint64_t group_id) -{ - return ecs_map_ensure(query->groups, ecs_query_table_list_t, group_id); + /* Walk prefabs */ + i = type_count - 1; + do { + ecs_entity_t e = type_array[i]; + + if (!(e & ECS_ROLE_MASK)) { + break; + } + + if (ECS_HAS_RELATION(e, EcsIsA)) { + if (override_from_base(world, ecs_pair_object(world, e), component, + data, column, row, count)) + { + return true; + } + } + } while (--i >= 0); + + return false; } -/* Find the last node of the group after which this group should be inserted */ static -ecs_query_table_node_t* find_group_insertion_node( - ecs_query_t *query, - uint64_t group_id) +void components_override( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + int32_t row, + int32_t count, + ecs_ids_t *added) { - /* Grouping must be enabled */ - ecs_assert(query->group_by != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); + + ecs_column_t *columns = data->columns; + ecs_type_t type = table->type; + ecs_table_t *storage_table = table->storage_table; + + int i; + for (i = 0; i < added->count; i ++) { + ecs_entity_t id = added->array[i]; + + if (ECS_HAS_RELATION(id, EcsIsA)) { + ecs_entity_t base = ECS_PAIR_OBJECT(id); + + /* Cannot inherit from base if base is final */ + ecs_assert(!ecs_has_id(world, ecs_get_alive(world, base), EcsFinal), + ECS_INVALID_PARAMETER, NULL); - ecs_map_iter_t it = ecs_map_iter(query->groups); - ecs_query_table_list_t *list, *closest_list = NULL; - uint64_t id, closest_id = 0; + ecs_assert(base != 0, ECS_INVALID_PARAMETER, NULL); - /* Find closest smaller group id */ - while ((list = ecs_map_next(&it, ecs_query_table_list_t, &id))) { - if (id >= group_id) { + if (!world->stage.base) { + /* Setting base prevents instantiating the hierarchy multiple + * times. The instantiate function recursively iterates the + * hierarchy to instantiate children. While this is happening, + * new tables are created which end up calling this function, + * which would call instantiate multiple times for the same + * level in the hierarchy. */ + world->stage.base = base; + instantiate(world, base, table, data, row, count); + world->stage.base = 0; + } + } + + if (!storage_table) { continue; } - if (!list->last) { - ecs_assert(list->first == NULL, ECS_INTERNAL_ERROR, NULL); + ecs_table_record_t *tr = flecs_get_table_record( + world, storage_table, id); + if (!tr) { continue; } - if (!closest_list || ((group_id - id) < (group_id - closest_id))) { - closest_id = id; - closest_list = list; + ecs_column_t *column = &columns[tr->column]; + if (!column->size) { + continue; } - } - if (closest_list) { - return closest_list->last; - } else { - return NULL; /* Group should be first in query */ + override_component(world, id, type, data, column, row, count); } } -/* Initialize group with first node */ static -void create_group( - ecs_query_t *query, - ecs_query_table_node_t *node) +void set_switch( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + int32_t row, + int32_t count, + ecs_ids_t *entities, + bool reset) { - ecs_query_table_match_t *match = node->match; - uint64_t group_id = match->group_id; + ecs_entity_t *array = entities->array; + int32_t i, comp_count = entities->count; - /* If query has grouping enabled & this is a new/empty group, find - * the insertion point for the group */ - ecs_query_table_node_t *insert_after = find_group_insertion_node( - query, group_id); + for (i = 0; i < comp_count; i ++) { + ecs_entity_t e = array[i]; - if (!insert_after) { - /* This group should appear first in the query list */ - ecs_query_table_node_t *query_first = query->list.first; - if (query_first) { - /* If this is not the first match for the query, insert before it */ - node->next = query_first; - query_first->prev = node; - query->list.first = node; - } else { - /* If this is the first match of the query, initialize its list */ - ecs_assert(query->list.last == NULL, ECS_INTERNAL_ERROR, NULL); - query->list.first = node; - query->list.last = node; - } - } else { - ecs_assert(query->list.first != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(query->list.last != NULL, ECS_INTERNAL_ERROR, NULL); + if (ECS_HAS_ROLE(e, CASE)) { + e = e & ECS_COMPONENT_MASK; - /* This group should appear after another group */ - ecs_query_table_node_t *insert_before = insert_after->next; - node->prev = insert_after; - insert_after->next = node; - node->next = insert_before; - if (insert_before) { - insert_before->prev = node; - } else { - ecs_assert(query->list.last == insert_after, - ECS_INTERNAL_ERROR, NULL); - - /* This group should appear last in the query list */ - query->list.last = node; + ecs_entity_t sw_case = 0; + if (!reset) { + sw_case = e; + ecs_assert(sw_case != 0, ECS_INTERNAL_ERROR, NULL); + } + + int32_t sw_index = flecs_table_switch_from_case(world, table, e); + ecs_assert(sw_index != -1, ECS_INTERNAL_ERROR, NULL); + ecs_switch_t *sw = data->sw_columns[sw_index].data; + ecs_assert(sw != NULL, ECS_INTERNAL_ERROR, NULL); + + int32_t r; + for (r = 0; r < count; r ++) { + flecs_switch_set(sw, row + r, sw_case); + } } } } static -void remove_group( - ecs_query_t *query, - uint64_t group_id) -{ - ecs_map_remove(query->groups, group_id); -} - -/* Find the list the node should be part of */ -static -ecs_query_table_list_t* get_node_list( - ecs_query_t *query, - ecs_query_table_node_t *node) +void ecs_components_switch( + ecs_world_t *world, + ecs_table_t *table, + ecs_data_t *data, + int32_t row, + int32_t count, + ecs_ids_t *added, + ecs_ids_t *removed) { - ecs_query_table_match_t *match = node->match; - if (query->group_by) { - return get_group(query, match->group_id); - } else { - return &query->list; + if (added) { + set_switch(world, table, data, row, count, added, false); } + if (removed) { + set_switch(world, table, data, row, count, removed, true); + } } -/* Find or create the list the node should be part of */ static -ecs_query_table_list_t* ensure_node_list( - ecs_query_t *query, - ecs_query_table_node_t *node) +int32_t new_entity( + ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_info_t *info, + ecs_table_t *new_table, + ecs_table_diff_t *diff, + bool construct) { - ecs_query_table_match_t *match = node->match; - if (query->group_by) { - return ensure_group(query, match->group_id); - } else { - return &query->list; + ecs_record_t *record = info->record; + ecs_data_t *new_data = &new_table->storage; + int32_t new_row; + + if (!record) { + record = ecs_eis_ensure(world, entity); } -} -/* Remove node from list */ -static -void remove_table_node( - ecs_query_t *query, - ecs_query_table_node_t *node) -{ - ecs_query_table_node_t *prev = node->prev; - ecs_query_table_node_t *next = node->next; + new_row = flecs_table_append( + world, new_table, new_data, entity, record, construct); - ecs_assert(prev != node, ECS_INTERNAL_ERROR, NULL); - ecs_assert(next != node, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!prev || prev != next, ECS_INTERNAL_ERROR, NULL); + record->table = new_table; + record->row = flecs_row_to_record(new_row, info->is_watched); - ecs_query_table_list_t *list = get_node_list(query, node); + ecs_assert( + ecs_vector_count(new_data[0].entities) > new_row, + ECS_INTERNAL_ERROR, NULL); - if (!list || !list->first) { - /* If list contains no nodes, the node must be empty */ - ecs_assert(!list || list->last == NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(prev == NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(next == NULL, ECS_INTERNAL_ERROR, NULL); - return; + if (new_table->flags & EcsTableHasAddActions) { + flecs_notify_on_add( + world, new_table, NULL, new_data, new_row, 1, diff, true); } - ecs_assert(prev != NULL || query->list.first == node, + info->data = new_data; + + return new_row; +} + +static +int32_t move_entity( + ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_info_t *info, + ecs_table_t *src_table, + ecs_data_t *src_data, + int32_t src_row, + ecs_table_t *dst_table, + ecs_table_diff_t *diff, + bool construct) +{ + ecs_data_t *dst_data = &dst_table->storage; + ecs_assert(src_data != dst_data, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ecs_is_alive(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(src_table != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(src_data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(src_row >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ecs_vector_count(src_data->entities) > src_row, ECS_INTERNAL_ERROR, NULL); - ecs_assert(next != NULL || query->list.last == node, + + ecs_record_t *record = info->record; + ecs_assert(!record || record == ecs_eis_get(world, entity), ECS_INTERNAL_ERROR, NULL); - if (prev) { - prev->next = next; - } - if (next) { - next->prev = prev; + int32_t dst_row = flecs_table_append(world, dst_table, dst_data, entity, + record, false); + + ecs_assert(ecs_vector_count(src_data->entities) > src_row, + ECS_INTERNAL_ERROR, NULL); + + /* Copy entity & components from src_table to dst_table */ + if (src_table->type) { + flecs_notify_on_remove( + world, src_table, dst_table, src_row, 1, diff); + + + flecs_table_move(world, entity, entity, dst_table, dst_data, dst_row, + src_table, src_data, src_row, construct); } - ecs_assert(list->count > 0, ECS_INTERNAL_ERROR, NULL); - list->count --; + /* Update entity index & delete old data after running remove actions */ + record->table = dst_table; + record->row = flecs_row_to_record(dst_row, info->is_watched); - if (query->group_by) { - ecs_query_table_match_t *match = node->match; - uint64_t group_id = match->group_id; + flecs_table_delete(world, src_table, src_data, src_row, false); - /* Make sure query.list is updated if this is the first or last group */ - if (query->list.first == node) { - ecs_assert(prev == NULL, ECS_INTERNAL_ERROR, NULL); - query->list.first = next; - prev = next; - } - if (query->list.last == node) { - ecs_assert(next == NULL, ECS_INTERNAL_ERROR, NULL); - query->list.last = prev; - next = prev; + /* If components were added, invoke add actions */ + if (src_table != dst_table || diff->added.count) { + if (diff->added.count && (dst_table->flags & EcsTableHasAddActions)) { + flecs_notify_on_add(world, dst_table, src_table, dst_data, + dst_row, 1, diff, true); } + } - ecs_assert(query->list.count > 0, ECS_INTERNAL_ERROR, NULL); - query->list.count --; + info->data = dst_data; + + return dst_row; +} + +static +void delete_entity( + ecs_world_t *world, + ecs_table_t *src_table, + ecs_data_t *src_data, + int32_t src_row, + ecs_table_diff_t *diff) +{ + if (src_table) { + /* Invoke remove actions before deleting */ + if (src_table->flags & EcsTableHasRemoveActions) { + flecs_notify_on_remove(world, src_table, NULL, src_row, 1, diff); + } + } + + flecs_table_delete(world, src_table, src_data, src_row, true); +} + +/* Updating component monitors is a relatively expensive operation that only + * happens for entities that are monitored. The approach balances the amount of + * processing between the operation on the entity vs the amount of work that + * needs to be done to rematch queries, as a simple brute force approach does + * not scale when there are many tables / queries. Therefore we need to do a bit + * of bookkeeping that is more intelligent than simply flipping a flag */ +static +void update_component_monitor_w_array( + ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_t relation, + ecs_ids_t *entities) +{ + if (!entities) { + return; + } + + int i; + for (i = 0; i < entities->count; i ++) { + ecs_entity_t id = entities->array[i]; + if (ECS_HAS_ROLE(id, PAIR)) { + ecs_entity_t rel = ECS_PAIR_RELATION(id); + + /* If a relationship has changed, check if it could have impacted + * the shape of the graph for that relationship. If so, mark the + * relationship as dirty */ + if (rel != relation && flecs_get_id_record(world, ecs_pair(rel, entity))) { + update_component_monitor_w_array(world, entity, rel, entities); + } - /* Make sure group list only contains nodes that belong to the group */ - if (prev && prev->match->group_id != group_id) { - /* The previous node belonged to another group */ - prev = next; - } - if (next && next->match->group_id != group_id) { - /* The next node belonged to another group */ - next = prev; } + + if (ECS_HAS_RELATION(id, EcsIsA)) { + /* If an IsA relationship is added to a monitored entity (can + * be either a parent or a base) component monitors need to be + * evaluated for the components of the prefab. */ + ecs_entity_t base = ecs_pair_object(world, id); + ecs_type_t type = ecs_get_type(world, base); + ecs_ids_t base_entities = flecs_type_to_ids(type); - /* Do check again, in case both prev & next belonged to another group */ - if (prev && prev->match->group_id != group_id) { - /* There are no more matches left in this group */ - remove_group(query, group_id); - list = NULL; + /* This evaluates the component monitor for all components of the + * base entity. If the base entity contains IsA relationships + * these will be evaluated recursively as well. */ + update_component_monitor_w_array( + world, entity, relation, &base_entities); + } else { + flecs_monitor_mark_dirty(world, relation, id); } } +} - if (list) { - if (list->first == node) { - list->first = next; - } - if (list->last == node) { - list->last = prev; +static +void update_component_monitors( + ecs_world_t *world, + ecs_entity_t entity, + ecs_ids_t *added, + ecs_ids_t *removed) +{ + update_component_monitor_w_array(world, entity, 0, added); + update_component_monitor_w_array(world, entity, 0, removed); +} + +static +void commit( + ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_info_t *info, + ecs_table_t *dst_table, + ecs_table_diff_t *diff, + bool construct) +{ + ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); + + ecs_table_t *src_table = info->table; + if (src_table == dst_table) { + /* If source and destination table are the same no action is needed * + * However, if a component was added in the process of traversing a + * table, this suggests that a case switch could have occured. */ + if (((diff->added.count) || (diff->removed.count)) && + src_table && src_table->flags & EcsTableHasSwitch) + { + ecs_components_switch( + world, src_table, info->data, info->row, 1, + &diff->added, &diff->removed); } + + return; } - node->prev = NULL; - node->next = NULL; + if (src_table) { + ecs_data_t *src_data = info->data; + ecs_assert(dst_table != NULL, ECS_INTERNAL_ERROR, NULL); -#ifdef FLECS_SYSTEMS_H - if (query->list.first == NULL && query->system && !query->world->is_fini) { - ecs_system_activate(query->world, query->system, false, NULL); + if (dst_table->type) { + info->row = move_entity(world, entity, info, src_table, + src_data, info->row, dst_table, diff, construct); + info->table = dst_table; + } else { + delete_entity(world, src_table, src_data, info->row, diff); + + ecs_eis_set(world, entity, &(ecs_record_t){ + NULL, (info->is_watched == true) * -1 + }); + } + } else { + if (dst_table->type) { + info->row = new_entity( + world, entity, info, dst_table, diff, construct); + info->table = dst_table; + } } -#endif - query->match_count ++; + /* If the entity is being watched, it is being monitored for changes and + * requires rematching systems when components are added or removed. This + * ensures that systems that rely on components from containers or prefabs + * update the matched tables when the application adds or removes a + * component from, for example, a container. */ + if (info->is_watched) { + update_component_monitors(world, entity, &diff->added, &diff->removed); + } + + if ((!src_table || !src_table->type) && world->range_check_enabled) { + ecs_assert(!world->stats.max_id || entity <= world->stats.max_id, ECS_OUT_OF_RANGE, 0); + ecs_assert(entity >= world->stats.min_id, ECS_OUT_OF_RANGE, 0); + } } -/* Add node to list */ static -void insert_table_node( - ecs_query_t *query, - ecs_query_table_node_t *node) +void new( + ecs_world_t *world, + ecs_entity_t entity, + ecs_ids_t *to_add) { - /* Node should not be part of an existing list */ - ecs_assert(node->prev == NULL && node->next == NULL, - ECS_INTERNAL_ERROR, NULL); + ecs_entity_info_t info = {0}; + int32_t i, count = to_add->count; + ecs_table_t *table = &world->store.root; + + ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; + for (i = 0; i < count; i ++) { + table = table_append(world, table, to_add->array[i], &diff); + } - /* If this is the first match, activate system */ -#ifdef FLECS_SYSTEMS_H - if (!query->list.first && query->system) { - ecs_system_activate(query->world, query->system, true, NULL); + new_entity(world, entity, &info, table, &diff, true); + + diff_free(&diff); +} + +static +const ecs_entity_t* new_w_data( + ecs_world_t *world, + ecs_table_t *table, + const ecs_entity_t *entities, + ecs_ids_t *component_ids, + int32_t count, + void **component_data, + bool is_move, + int32_t *row_out, + ecs_table_diff_t *diff) +{ + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(count != 0, ECS_INTERNAL_ERROR, NULL); + + int32_t sparse_count = 0; + if (!entities) { + sparse_count = ecs_eis_count(world); + entities = flecs_sparse_new_ids(world->store.entity_index, count); } -#endif - compute_group_id(query, node->match); + ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_query_table_list_t *list = ensure_node_list(query, node); - if (list->last) { - ecs_assert(query->list.first != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(query->list.last != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(list->first != NULL, ECS_INTERNAL_ERROR, NULL); + if (!table) { + return entities; + } - ecs_query_table_node_t *last = list->last; - ecs_query_table_node_t *last_next = last->next; + ecs_type_t type = table->type; + if (!type) { + return entities; + } - node->prev = last; - node->next = last_next; - last->next = node; + ecs_ids_t component_array = { 0 }; + if (!component_ids) { + component_ids = &component_array; + component_array.array = ecs_vector_first(type, ecs_entity_t); + component_array.count = ecs_vector_count(type); + } + + ecs_data_t *data = &table->storage; + int32_t row = flecs_table_appendn(world, table, data, count, entities); + + /* Update entity index. */ + int i; + ecs_record_t **record_ptrs = ecs_vector_first(data->record_ptrs, ecs_record_t*); + for (i = 0; i < count; i ++) { + record_ptrs[row + i] = ecs_eis_set(world, entities[i], + &(ecs_record_t){ + .table = table, + .row = row + i + 1 + }); + } - if (last_next) { - last_next->prev = node; - } + flecs_defer_none(world, &world->stage); - list->last = node; + flecs_notify_on_add(world, table, NULL, data, row, count, diff, + component_data == NULL); - if (query->group_by) { - /* Make sure to update query list if this is the last group */ - if (query->list.last == last) { - query->list.last = node; + if (component_data) { + /* Set components that we're setting in the component mask so the init + * actions won't call OnSet triggers for them. This ensures we won't + * call OnSet triggers multiple times for the same component */ + int32_t c_i; + for (c_i = 0; c_i < component_ids->count; c_i ++) { + void *src_ptr = component_data[c_i]; + if (!src_ptr) { + continue; } - } - } else { - ecs_assert(list->first == NULL, ECS_INTERNAL_ERROR, NULL); - list->first = node; - list->last = node; + /* Find component in storage type */ + ecs_entity_t id = component_ids->array[c_i]; + ecs_column_t *column = ecs_table_column_for_id(world, table, id); + ecs_assert(column != NULL, ECS_INTERNAL_ERROR, NULL); - if (query->group_by) { - /* Initialize group with its first node */ - create_group(query, node); - } - } + int16_t size = column->size; + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); - if (query->group_by) { - query->list.count ++; - } + int16_t alignment = column->alignment; + void *ptr = ecs_vector_first_t(column->data, size, alignment); + ptr = ECS_OFFSET(ptr, size * row); - list->count ++; - query->match_count ++; + const ecs_type_info_t *cdata = get_c_info(world, id); + ecs_copy_t copy; + ecs_move_t move; + if (cdata && is_move && (move = cdata->lifecycle.move)) { + ecs_entity_t *eids = ecs_vector_first(data->entities, ecs_entity_t); + move(world, id, eids, eids, ptr, src_ptr, + flecs_to_size_t(size), count, cdata->lifecycle.ctx); + } else if (cdata && !is_move && (copy = cdata->lifecycle.copy)) { + ecs_entity_t *eids = ecs_vector_first(data->entities, ecs_entity_t); + copy(world, id, eids, eids, ptr, src_ptr, + flecs_to_size_t(size), count, cdata->lifecycle.ctx); + } else { + ecs_os_memcpy(ptr, src_ptr, size * count); + } + }; - ecs_assert(node->prev != node, ECS_INTERNAL_ERROR, NULL); - ecs_assert(node->next != node, ECS_INTERNAL_ERROR, NULL); + flecs_notify_on_set(world, table, row, count, NULL, true); + flecs_notify_on_set(world, table, row, count, &diff->on_set, false); + } - ecs_assert(list->first != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(list->last != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(list->last == node, ECS_INTERNAL_ERROR, NULL); - ecs_assert(query->list.first != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(query->list.last != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(query->list.first->prev == NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(query->list.last->next == NULL, ECS_INTERNAL_ERROR, NULL); -} + flecs_defer_flush(world, &world->stage); -static -ecs_query_table_match_t* cache_add( - ecs_query_table_t *elem) -{ - ecs_query_table_match_t *result = ecs_os_calloc_t(ecs_query_table_match_t); - ecs_query_table_node_t *node = &result->node; + if (row_out) { + *row_out = row; + } - node->match = result; - if (!elem->first) { - elem->first = result; - elem->last = result; + if (sparse_count) { + entities = flecs_sparse_ids(world->store.entity_index); + return &entities[sparse_count]; } else { - ecs_assert(elem->last != NULL, ECS_INTERNAL_ERROR, NULL); - elem->last->next_match = result; - elem->last = result; + return entities; } - - return result; } -/* Builtin group_by callback for Cascade terms. - * This function traces the hierarchy depth of an entity type by following a - * relation upwards (to its 'parents') for as long as those parents have the - * specified component id. - * The result of the function is the number of parents with the provided - * component for a given relation. */ static -uint64_t group_by_cascade( +void add_id_w_info( ecs_world_t *world, - ecs_type_t type, - ecs_entity_t component, - void *ctx) + ecs_entity_t entity, + ecs_entity_info_t *info, + ecs_id_t id, + bool construct) { - uint64_t result = 0; - int32_t i, count = ecs_vector_count(type); - ecs_entity_t *array = ecs_vector_first(type, ecs_entity_t); - ecs_term_t *term = ctx; - ecs_entity_t relation = term->subj.set.relation; + ecs_table_diff_t diff; - /* Cascade needs a relation to calculate depth from */ - ecs_assert(relation != 0, ECS_INVALID_PARAMETER, NULL); + ecs_table_t *src_table = info->table; + ecs_table_t *dst_table = flecs_table_traverse_add( + world, src_table, &id, &diff); - /* Should only be used with cascade terms */ - ecs_assert(term->subj.set.mask & EcsCascade, - ECS_INVALID_PARAMETER, NULL); + commit(world, entity, info, dst_table, &diff, construct); +} - /* Iterate back to front as relations are more likely to occur near the - * end of a type. */ - for (i = count - 1; i >= 0; i --) { - /* Find relation & relation object in entity type */ - if (ECS_HAS_RELATION(array[i], relation)) { - ecs_type_t obj_type = ecs_get_type(world, - ecs_pair_object(world, array[i])); - int32_t j, c_count = ecs_vector_count(obj_type); - ecs_entity_t *c_array = ecs_vector_first(obj_type, ecs_entity_t); +static +void add_id( + ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_stage_t *stage = flecs_stage_from_world(&world); - /* Iterate object type, check if it has the specified component */ - for (j = 0; j < c_count; j ++) { - /* If it has the component, it is part of the tree matched by - * the query, increase depth */ - if (c_array[j] == component) { - result ++; + if (flecs_defer_add(world, stage, entity, id)) { + return; + } - /* Recurse to test if the object has matching parents */ - result += group_by_cascade(world, obj_type, component, ctx); - break; - } - } + ecs_entity_info_t info; + flecs_get_info(world, entity, &info); - if (j != c_count) { - break; - } + ecs_table_diff_t diff; + ecs_table_t *src_table = info.table; + ecs_table_t *dst_table = flecs_table_traverse_add( + world, src_table, &id, &diff); - /* If the id doesn't have a role set, we'll find no more relations */ - } else if (!(array[i] & ECS_ROLE_MASK)) { - break; - } - } + commit(world, entity, &info, dst_table, &diff, true); - return result; + flecs_defer_flush(world, stage); } -#ifndef NDEBUG - static -const char* query_name( +void remove_id( ecs_world_t *world, - ecs_query_t *q) + ecs_entity_t entity, + ecs_id_t id) { - if (q->system) { - return ecs_get_name(world, q->system); - } else if (q->filter.name) { - return q->filter.name; - } else { - return q->filter.expr; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_stage_t *stage = flecs_stage_from_world(&world); + + if (flecs_defer_remove(world, stage, entity, id)) { + return; } -} -#endif + ecs_entity_info_t info; + flecs_get_info(world, entity, &info); + + ecs_table_diff_t diff; + ecs_table_t *src_table = info.table; + ecs_table_t *dst_table = flecs_table_traverse_remove( + world, src_table, &id, &diff); + + commit(world, entity, &info, dst_table, &diff, true); + + flecs_defer_flush(world, stage); +} static -int get_comp_and_src( +void *get_mutable( ecs_world_t *world, - ecs_query_t *query, - int32_t t, - ecs_table_t *table_arg, - ecs_entity_t *component_out, - ecs_entity_t *entity_out, - bool *match_out) + ecs_entity_t entity, + ecs_entity_t component, + ecs_entity_info_t *info, + bool *is_added) { - ecs_entity_t component = 0, entity = 0; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(component != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert((component & ECS_COMPONENT_MASK) == component || + ECS_HAS_ROLE(component, PAIR), ECS_INVALID_PARAMETER, NULL); - ecs_term_t *terms = query->filter.terms; - int32_t term_count = query->filter.term_count; - ecs_term_t *term = &terms[t]; - ecs_term_id_t *subj = &term->subj; - ecs_oper_kind_t op = term->oper; + void *dst = NULL; + if (flecs_get_info(world, entity, info) && info->table) { + dst = get_component(world, info->table, info->row, component); + } - *match_out = true; + if (!dst) { + ecs_table_t *table = info->table; + add_id_w_info(world, entity, info, component, true); + flecs_get_info(world, entity, info); + ecs_assert(info->table != NULL, ECS_INTERNAL_ERROR, NULL); - if (op == EcsNot) { - entity = subj->entity; - } + ecs_assert(info->table->storage_table != NULL, ECS_INTERNAL_ERROR, NULL); + dst = get_component(world, info->table, info->row, component); - if (!subj->entity) { - component = term->id; - } else { - ecs_table_t *table = table_arg; - if (subj->entity != EcsThis) { - table = ecs_get_table(world, subj->entity); + if (is_added) { + *is_added = table != info->table; } - ecs_type_t type = NULL; - if (table) { - type = table->type; + return dst; + } else { + if (is_added) { + *is_added = false; } - if (op == EcsOr) { - for (; t < term_count; t ++) { - term = &terms[t]; - - /* Keep iterating until the next non-OR expression */ - if (term->oper != EcsOr) { - t --; - break; - } - - if (!component) { - ecs_entity_t source = 0; - int32_t result = ecs_type_match(world, table, type, - 0, term->id, subj->set.relation, subj->set.min_depth, - subj->set.max_depth, &source, NULL); - - if (result != -1) { - component = term->id; - } - - if (source) { - entity = source; - } - } - } - } else { - component = term->id; - - ecs_entity_t source = 0; - bool result = ecs_type_match(world, table, type, 0, component, - subj->set.relation, subj->set.min_depth, subj->set.max_depth, - &source, NULL) != -1; - - *match_out = result; - - if (op == EcsNot) { - result = !result; - } + return dst; + } +} - /* Optional terms may not have the component. *From terms contain - * the id of a type of which the contents must match, but the type - * itself does not need to match. */ - if (op == EcsOptional || op == EcsAndFrom || op == EcsOrFrom || - op == EcsNotFrom) - { - result = true; - } - /* Table has already been matched, so unless column is optional - * any components matched from the table must be available. */ - if (table == table_arg) { - ecs_assert(result == true, ECS_INTERNAL_ERROR, NULL); - } +/* -- Private functions -- */ - if (source) { - entity = source; - } - } +void flecs_notify_on_add( + ecs_world_t *world, + ecs_table_t *table, + ecs_table_t *other_table, + ecs_data_t *data, + int32_t row, + int32_t count, + ecs_table_diff_t *diff, + bool run_on_set) +{ + ecs_assert(diff != NULL, ECS_INTERNAL_ERROR, NULL); - if (subj->entity != EcsThis) { - entity = subj->entity; - } + if (table->flags & EcsTableHasIsA) { + components_override(world, table, data, row, count, &diff->added); } - if (entity == EcsThis) { - entity = 0; + if (table->flags & EcsTableHasSwitch) { + ecs_components_switch( + world, table, data, row, count, &diff->added, NULL); } - *component_out = component; - *entity_out = entity; - - return t; -} - -typedef struct pair_offset_t { - int32_t index; - int32_t count; -} pair_offset_t; - -/* Get index for specified pair. Take into account that a pair can be matched - * multiple times per table, by keeping an offset of the last found index */ -static -int32_t get_pair_index( - ecs_type_t table_type, - ecs_id_t pair, - int32_t column_index, - pair_offset_t *pair_offsets, - int32_t count) -{ - int32_t result; + if (table->flags & EcsTableHasOnAdd) { + notify(world, table, other_table, row, count, EcsOnAdd, &diff->added); + } - /* The count variable keeps track of the number of times a pair has been - * matched with the current table. Compare the count to check if the index - * was already resolved for this iteration */ - if (pair_offsets[column_index].count == count) { - /* If it was resolved, return the last stored index. Subtract one as the - * index is offset by one, to ensure we're not getting stuck on the same - * index. */ - result = pair_offsets[column_index].index - 1; - } else { - /* First time for this iteration that the pair index is resolved, look - * it up in the type. */ - result = ecs_type_index_of(table_type, - pair_offsets[column_index].index, pair); - pair_offsets[column_index].index = result + 1; - pair_offsets[column_index].count = count; + if (run_on_set) { + notify(world, table, other_table, row, count, EcsOnSet, &diff->on_set); } - - return result; } -static -int32_t get_component_index( +void flecs_notify_on_remove( ecs_world_t *world, ecs_table_t *table, - ecs_type_t table_type, - ecs_entity_t *component_out, - int32_t column_index, - ecs_oper_kind_t op, - pair_offset_t *pair_offsets, - int32_t count) + ecs_table_t *other_table, + int32_t row, + int32_t count, + ecs_table_diff_t *diff) { - int32_t result = 0; - ecs_entity_t component = *component_out; + ecs_assert(diff != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + if (count) { + notify(world, table, other_table, row, count, EcsUnSet, &diff->un_set); - if (component) { - /* If requested component is a case, find the corresponding switch to - * lookup in the table */ - if (ECS_HAS_ROLE(component, CASE)) { - result = flecs_table_switch_from_case( - world, table, component); - ecs_assert(result != -1, ECS_INTERNAL_ERROR, NULL); + if (table->flags & EcsTableHasOnRemove) { + notify(world, table, other_table, row, count, EcsOnRemove, + &diff->removed); + } - result += table->sw_column_offset; - } else - if (ECS_HAS_ROLE(component, PAIR)) { - ecs_entity_t rel = ECS_PAIR_RELATION(component); - ecs_entity_t obj = ECS_PAIR_OBJECT(component); + if (table->flags & EcsTableHasIsA) { + notify(world, table, other_table, row, count, EcsOnSet, &diff->on_set); + } + } +} - /* Both the relationship and the object of the pair must be set */ - ecs_assert(rel != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(obj != 0, ECS_INVALID_PARAMETER, NULL); +void flecs_notify_on_set( + ecs_world_t *world, + ecs_table_t *table, + int32_t row, + int32_t count, + ecs_ids_t *ids, + bool owned) +{ + ecs_data_t *data = &table->storage; - if (rel == EcsWildcard || obj == EcsWildcard) { - ecs_assert(pair_offsets != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); + ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(row < ecs_vector_count(data->entities), + ECS_INTERNAL_ERROR, NULL); + ecs_assert((row + count) <= ecs_vector_count(data->entities), + ECS_INTERNAL_ERROR, NULL); + entities = ECS_OFFSET(entities, ECS_SIZEOF(ecs_entity_t) * row); - /* Get index of pair. Start looking from the last pair index - * as this may not be the first instance of the pair. */ - result = get_pair_index( - table_type, component, column_index, pair_offsets, count); - - if (result != -1) { - /* If component of current column is a pair, get the actual - * pair type for the table, so the system can see which - * component the pair was applied to */ - ecs_entity_t *pair = ecs_vector_get( - table_type, ecs_entity_t, result); - *component_out = *pair; + ecs_ids_t local_ids; + if (!ids) { + local_ids.array = ecs_vector_first(table->storage_type, ecs_id_t); + local_ids.count = ecs_vector_count(table->storage_type); + ids = &local_ids; + } - /* Check if the pair is a tag or whether it has data */ - if (ecs_get(world, rel, EcsComponent) == NULL) { - /* If pair has no data associated with it, use the - * component to which the pair has been added */ - component = ECS_PAIR_OBJECT(*pair); - } else { - component = rel; - } - } - } else { + if (owned) { + int i; + for (i = 0; i < ids->count; i ++) { + ecs_id_t id = ids->array[i]; + const ecs_type_info_t *info = get_c_info(world, id); + ecs_on_set_t on_set; + if (info && (on_set = info->lifecycle.on_set)) { + ecs_column_t *c = ecs_table_column_for_id(world, table, id); + ecs_size_t size = c->size; + ecs_assert(size != 0, ECS_INTERNAL_ERROR, NULL); - /* If the low part is a regular entity (component), then - * this query exactly matches a single pair instance. In - * this case we can simply do a lookup of the pair - * identifier in the table type. */ - result = ecs_type_index_of(table_type, 0, component); + void *ptr = ecs_vector_get_t(c->data, size, c->alignment, row); + on_set(world, id, entities, ptr, flecs_to_size_t(size), + count, info->lifecycle.ctx); } - } else { - /* Get column index for component */ - result = ecs_type_index_of(table_type, 0, component); - } - - /* If column is found, add one to the index, as column zero in - * a table is reserved for entity id's */ - if (result != -1) { - result ++; - } - - /* ecs_table_column_offset may return -1 if the component comes - * from a prefab. If so, the component will be resolved as a - * reference (see below) */ - } - - if (op == EcsAndFrom || op == EcsOrFrom || op == EcsNotFrom) { - result = 0; - } else if (op == EcsOptional) { - /* If table doesn't have the field, mark it as no data */ - if (!ecs_type_has_id(world, table_type, component, false)) { - result = 0; } - } + } - return result; + /* Run OnSet notifications */ + if (table->flags & EcsTableHasOnSet) { + notify(world, table, NULL, row, count, EcsOnSet, ids); + } } -static -ecs_vector_t* add_ref( - ecs_world_t *world, - ecs_query_t *query, - ecs_vector_t *references, - ecs_term_t *term, - ecs_entity_t component, - ecs_entity_t entity) -{ - ecs_ref_t *ref = ecs_vector_add(&references, ecs_ref_t); - ecs_term_id_t *subj = &term->subj; +bool flecs_get_info( + const ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_info_t *info) +{ + info->table = NULL; + info->record = NULL; + info->data = NULL; + info->is_watched = false; - if (!(subj->set.mask & EcsCascade)) { - ecs_assert(entity != 0, ECS_INTERNAL_ERROR, NULL); + if (entity & ECS_ROLE) { + return false; } - *ref = (ecs_ref_t){0}; - ref->entity = entity; - ref->component = component; - - const EcsComponent *c_info = flecs_component_from_id(world, component); - if (c_info) { - if (c_info->size && subj->entity != 0) { - if (entity) { - ecs_get_ref_w_id(world, ref, entity, component); - } + ecs_record_t *record = ecs_eis_get(world, entity); - query->flags |= EcsQueryHasRefs; - } + if (!record) { + return false; } - return references; + set_info_from_record(info, record); + + return true; } -static -int32_t get_pair_count( - ecs_type_t type, - ecs_entity_t pair) +int32_t flecs_record_to_row( + int32_t row, + bool *is_watched_out) { - int32_t i = -1, result = 0; - while (-1 != (i = ecs_type_index_of(type, i + 1, pair))) { - result ++; - } + bool is_watched = row < 0; + row = row * -(is_watched * 2 - 1) - 1 * (row != 0); + *is_watched_out = is_watched; + return row; +} - return result; +int32_t flecs_row_to_record( + int32_t row, + bool is_watched) +{ + return (row + 1) * -(is_watched * 2 - 1); } -/* For each pair that the query subscribes for, count the occurrences in the - * table. Cardinality of subscribed for pairs must be the same as in the table - * or else the table won't match. */ -static -int32_t count_pairs( - const ecs_query_t *query, +ecs_ids_t flecs_type_to_ids( ecs_type_t type) { - ecs_term_t *terms = query->filter.terms; - int32_t i, count = query->filter.term_count; - int32_t first_count = 0, pair_count = 0; - - for (i = 0; i < count; i ++) { - ecs_term_t *term = &terms[i]; + return (ecs_ids_t){ + .array = ecs_vector_first(type, ecs_entity_t), + .count = ecs_vector_count(type) + }; +} - if (!ECS_HAS_ROLE(term->id, PAIR)) { - continue; - } +void flecs_set_watch( + ecs_world_t *world, + ecs_entity_t entity) +{ + (void)world; - if (term->subj.entity != EcsThis) { - continue; - } + ecs_record_t *record = ecs_eis_get(world, entity); + if (!record) { + ecs_record_t new_record = {.row = -1, .table = NULL}; + ecs_eis_set(world, entity, &new_record); + } else { + if (record->row > 0) { + record->row *= -1; - if (ecs_id_is_wildcard(term->id)) { - pair_count = get_pair_count(type, term->id); - if (!first_count) { - first_count = pair_count; - } else { - if (first_count != pair_count) { - /* The pairs that this query subscribed for occur in the - * table but don't have the same cardinality. Ignore the - * table. This could typically happen for empty tables along - * a path in the table graph. */ - return -1; - } - } + } else if (record->row == 0) { + /* If entity is empty, there is no index to change the sign of. In + * this case, set the index to -1, and assign an empty type. */ + record->row = -1; + record->table = NULL; } } - - return first_count; } -static -ecs_type_t get_term_type( - ecs_world_t *world, - ecs_term_t *term, - ecs_entity_t component) -{ - ecs_oper_kind_t oper = term->oper; - ecs_assert(oper == EcsAndFrom || oper == EcsOrFrom || oper == EcsNotFrom, - ECS_INTERNAL_ERROR, NULL); - (void)oper; - const EcsType *type = ecs_get(world, component, EcsType); - if (type) { - return type->normalized; - } else { - return ecs_get_type(world, component); - } -} +/* -- Public functions -- */ -/** Add table to system, compute offsets for system components in table it */ -static -void add_table( +bool ecs_commit( ecs_world_t *world, - ecs_query_t *query, - ecs_table_t *table) + ecs_entity_t entity, + ecs_record_t *record, + ecs_table_t *table, + ecs_ids_t *added, + ecs_ids_t *removed) { - ecs_type_t table_type = NULL; - ecs_term_t *terms = query->filter.terms; - int32_t t, c, term_count = query->filter.term_count; + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - if (table) { - table_type = table->type; + ecs_table_t *src_table = NULL; + if (!record) { + record = ecs_eis_get(world, entity); + src_table = record->table; } - int32_t pair_cur = 0, pair_count = count_pairs(query, table_type); - - /* If the query has pairs, we need to account for the fact that a table may - * have multiple components to which the pair is applied, which means the - * table has to be registered with the query multiple times, with different - * table columns. If so, allocate a small array for each pair in which the - * last added table index of the pair is stored, so that in the next - * iteration we can start the search from the correct offset type. */ - pair_offset_t *pair_offsets = NULL; - if (pair_count) { - pair_offsets = ecs_os_calloc( - ECS_SIZEOF(pair_offset_t) * term_count); + ecs_entity_info_t info = {0}; + if (record) { + set_info_from_record(&info, record); } - /* From here we recurse */ - ecs_query_table_match_t *table_data; - ecs_vector_t *references = NULL; - - ecs_query_table_t *qt = ecs_table_cache_insert( - &query->cache, ecs_query_table_t, table); - -add_pair: - table_data = cache_add(qt); - table_data->table = table; - if (table) { - table_type = table->type; + ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; + if (added) { + diff.added = *added; } - - if (term_count) { - /* Array that contains the system column to table column mapping */ - table_data->columns = ecs_os_calloc_n(int32_t, query->filter.term_count_actual); - ecs_assert(table_data->columns != NULL, ECS_OUT_OF_MEMORY, NULL); - - /* Store the components of the matched table. In the case of OR expressions, - * components may differ per matched table. */ - table_data->ids = ecs_os_calloc_n(ecs_entity_t, query->filter.term_count_actual); - ecs_assert(table_data->ids != NULL, ECS_OUT_OF_MEMORY, NULL); - - /* Cache subject (source) entity ids for components */ - table_data->subjects = ecs_os_calloc_n(ecs_entity_t, query->filter.term_count_actual); - ecs_assert(table_data->subjects != NULL, ECS_OUT_OF_MEMORY, NULL); - - /* Cache subject (source) entity ids for components */ - table_data->sizes = ecs_os_calloc_n(ecs_size_t, query->filter.term_count_actual); - ecs_assert(table_data->sizes != NULL, ECS_OUT_OF_MEMORY, NULL); + if (removed) { + diff.added = *removed; } + + commit(world, entity, &info, table, &diff, true); - /* Walk columns parsed from the system signature */ - c = 0; - for (t = 0; t < term_count; t ++) { - ecs_term_t *term = &terms[t]; - ecs_term_id_t subj = term->subj; - ecs_entity_t entity = 0, component = 0; - ecs_oper_kind_t op = term->oper; - - if (op == EcsNot) { - subj.entity = 0; - } - - /* Get actual component and component source for current column */ - bool match; - t = get_comp_and_src(world, query, t, table, &component, &entity, &match); - - /* This column does not retrieve data from a static entity */ - if (!entity && subj.entity) { - int32_t index = get_component_index(world, table, table_type, - &component, c, op, pair_offsets, pair_cur + 1); - - if (index == -1) { - if (op == EcsOptional && subj.set.mask == EcsSelf) { - index = 0; - } - } else { - if (op == EcsOptional && !(subj.set.mask & EcsSelf)) { - index = 0; - } - } - - table_data->columns[c] = index; + return src_table != table; +} - /* If the column is a case, we should only iterate the entities in - * the column for this specific case. Add a sparse column with the - * case id so we can find the correct entities when iterating */ - if (ECS_HAS_ROLE(component, CASE)) { - flecs_sparse_column_t *sc = ecs_vector_add( - &table_data->sparse_columns, flecs_sparse_column_t); - sc->signature_column_index = t; - sc->sw_case = component & ECS_COMPONENT_MASK; - sc->sw_column = NULL; - } +ecs_entity_t ecs_new_id( + ecs_world_t *world) +{ + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - /* If table has a disabled bitmask for components, check if there is - * a disabled column for the queried for component. If so, cache it - * in a vector as the iterator will need to skip the entity when the - * component is disabled. */ - if (index && (table && table->flags & EcsTableHasDisabled)) { - ecs_entity_t bs_id = - (component & ECS_COMPONENT_MASK) | ECS_DISABLED; - int32_t bs_index = ecs_type_index_of(table->type, 0, bs_id); - if (bs_index != -1) { - flecs_bitset_column_t *elem = ecs_vector_add( - &table_data->bitset_columns, flecs_bitset_column_t); - elem->column_index = bs_index; - elem->bs_column = NULL; - } - } - } + const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); - ecs_entity_t type_id = ecs_get_typeid(world, component); - if (!type_id && !(ECS_ROLE_MASK & component)) { - type_id = component; - } + /* It is possible that the world passed to this function is a stage, so + * make sure we have the actual world. Cast away const since this is one of + * the few functions that may modify the world while it is in readonly mode, + * since it is thread safe (uses atomic inc when in threading mode) */ + ecs_world_t *unsafe_world = (ecs_world_t*)ecs_get_world(world); - if (entity || table_data->columns[c] == -1 || subj.set.mask & EcsCascade) { - if (type_id) { - references = add_ref(world, query, references, term, - component, entity); - table_data->columns[c] = -ecs_vector_count(references); - } + ecs_entity_t entity; - table_data->subjects[c] = entity; - flecs_set_watch(world, entity); + int32_t stage_count = ecs_get_stage_count(unsafe_world); + if (stage->asynchronous || (ecs_os_has_threading() && stage_count > 1)) { + /* Can't atomically increase number above max int */ + ecs_assert( + unsafe_world->stats.last_id < UINT_MAX, ECS_INTERNAL_ERROR, NULL); - if (!match) { - ecs_ref_t *ref = ecs_vector_last(references, ecs_ref_t); - ref->entity = 0; - } - } + entity = (ecs_entity_t)ecs_os_ainc( + (int32_t*)&unsafe_world->stats.last_id); + } else { + entity = ecs_eis_recycle(unsafe_world); + } - if (type_id) { - const EcsComponent *cptr = ecs_get(world, type_id, EcsComponent); - if (!cptr || !cptr->size) { - int32_t column = table_data->columns[c]; - if (column < 0) { - ecs_ref_t *r = ecs_vector_get( - references, ecs_ref_t, -column - 1); - r->component = 0; - } - } + ecs_assert(!unsafe_world->stats.max_id || + ecs_entity_t_lo(entity) <= unsafe_world->stats.max_id, + ECS_OUT_OF_RANGE, NULL); - if (cptr) { - table_data->sizes[c] = cptr->size; - } else { - table_data->sizes[c] = 0; - } - } else { - table_data->sizes[c] = 0; - } + return entity; +} +ecs_entity_t ecs_set_with( + ecs_world_t *world, + ecs_id_t id) +{ + ecs_stage_t *stage = flecs_stage_from_world(&world); + ecs_id_t prev = stage->with; + stage->with = id; + return prev; +} - if (ECS_HAS_ROLE(component, SWITCH)) { - table_data->sizes[c] = ECS_SIZEOF(ecs_entity_t); - } else if (ECS_HAS_ROLE(component, CASE)) { - table_data->sizes[c] = ECS_SIZEOF(ecs_entity_t); - } +ecs_entity_t ecs_get_with( + const ecs_world_t *world) +{ + const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); + return stage->with; +} - table_data->ids[c] = component; +ecs_entity_t ecs_new_low_id( + ecs_world_t *world) +{ + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - c ++; - } + /* It is possible that the world passed to this function is a stage, so + * make sure we have the actual world. Cast away const since this is one of + * the few functions that may modify the world while it is in readonly mode, + * but only if single threaded. */ + ecs_world_t *unsafe_world = (ecs_world_t*)ecs_get_world(world); - if (references) { - ecs_size_t ref_size = ECS_SIZEOF(ecs_ref_t) * ecs_vector_count(references); - table_data->references = ecs_os_malloc(ref_size); - ecs_os_memcpy(table_data->references, - ecs_vector_first(references, ecs_ref_t), ref_size); - ecs_vector_free(references); - references = NULL; + if (unsafe_world->is_readonly) { + /* Can't issue new comp id while iterating when in multithreaded mode */ + ecs_assert(ecs_get_stage_count(world) <= 1, + ECS_INVALID_WHILE_ITERATING, NULL); } - /* Insert match to iteration list if table is not empty */ - if (!table || ecs_table_count(table) != 0) { - ecs_assert(table == qt->hdr.table, ECS_INTERNAL_ERROR, NULL); - insert_table_node(query, &table_data->node); - } + ecs_entity_t id; - /* Use tail recursion when adding table for multiple pairs */ - pair_cur ++; - if (pair_cur < pair_count) { - goto add_pair; + if (unsafe_world->stats.last_component_id < ECS_HI_COMPONENT_ID) { + do { + id = unsafe_world->stats.last_component_id ++; + } while (ecs_exists(unsafe_world, id) && id < ECS_HI_COMPONENT_ID); } - - if (table && !(query->flags & EcsQueryIsSubquery)) { - flecs_table_notify(world, table, &(ecs_table_event_t){ - .kind = EcsTableQueryMatch, - .query = query - }); + + if (unsafe_world->stats.last_component_id >= ECS_HI_COMPONENT_ID) { + /* If the low component ids are depleted, return a regular entity id */ + id = ecs_new_id(unsafe_world); } - if (pair_offsets) { - ecs_os_free(pair_offsets); - } + return id; } -static -bool match_term( - const ecs_world_t *world, - const ecs_table_t *table, - ecs_term_t *term) +ecs_entity_t ecs_new_w_id( + ecs_world_t *world, + ecs_id_t id) { - ecs_term_id_t *subj = &term->subj; - - /* If term has no subject, there's nothing to match */ - if (!subj->entity) { - return true; - } + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - if (term->subj.entity != EcsThis) { - table = ecs_get_table(world, subj->entity); - } + ecs_stage_t *stage = flecs_stage_from_world(&world); + ecs_entity_t entity = ecs_new_id(world); - return ecs_type_match( - world, table, table->type, 0, term->id, subj->set.relation, - subj->set.min_depth, subj->set.max_depth, NULL, NULL) != -1; -} + ecs_id_t ids[3]; + ecs_ids_t to_add = { .array = ids, .count = 0 }; -/* Match table with query */ -bool flecs_query_match( - const ecs_world_t *world, - const ecs_table_t *table, - const ecs_query_t *query) -{ - if (!(query->flags & EcsQueryNeedsTables)) { - return false; + if (id) { + ids[to_add.count ++] = id; } - ecs_type_t table_type = table->type; - - /* Don't match disabled entities */ - if (!(query->flags & EcsQueryMatchDisabled) && ecs_type_has_id( - world, table_type, EcsDisabled, true)) - { - return false; + ecs_id_t with = stage->with; + if (with) { + ids[to_add.count ++] = with; } - /* Don't match prefab entities */ - if (!(query->flags & EcsQueryMatchPrefab) && ecs_type_has_id( - world, table_type, EcsPrefab, true)) - { - return false; + ecs_entity_t scope = stage->scope; + if (scope) { + if (!id || !ECS_HAS_RELATION(id, EcsChildOf)) { + ids[to_add.count ++] = ecs_pair(EcsChildOf, scope); + } } - /* Check if pair cardinality matches pairs in query, if any */ - if (count_pairs(query, table->type) == -1) { - return false; + if (to_add.count) { + if (flecs_defer_new(world, stage, entity, to_add.array[0])) { + int i; + for (i = 1; i < to_add.count; i ++) { + flecs_defer_add(world, stage, entity, to_add.array[i]); + } + return entity; + } + + new(world, entity, &to_add); + } else { + if (flecs_defer_new(world, stage, entity, 0)) { + return entity; + } + + ecs_eis_set(world, entity, &(ecs_record_t){ 0 }); } - ecs_term_t *terms = query->filter.terms; - int32_t i, term_count = query->filter.term_count; + flecs_defer_flush(world, stage); - for (i = 0; i < term_count; i ++) { - ecs_term_t *term = &terms[i]; - ecs_oper_kind_t oper = term->oper; + return entity; +} - if (oper == EcsAnd) { - if (!match_term(world, table, term)) { - return false; - } +#ifdef FLECS_PARSER - } else if (oper == EcsNot) { - if (match_term(world, table, term)) { - return false; +/* Traverse table graph by either adding or removing identifiers parsed from the + * passed in expression. */ +static +ecs_table_t *traverse_from_expr( + ecs_world_t *world, + ecs_table_t *table, + const char *name, + const char *expr, + ecs_table_diff_t *diff, + bool replace_and, + bool *error) +{ + const char *ptr = expr; + if (ptr) { + ecs_term_t term = {0}; + while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))){ + if (!ecs_term_is_initialized(&term)) { + break; } - } else if (oper == EcsOr) { - bool match = false; - - for (; i < term_count; i ++) { - term = &terms[i]; - if (term->oper != EcsOr) { - i --; - break; - } - - if (!match && match_term( world, table, term)) { - match = true; + if (ecs_term_finalize(world, name, &term)) { + if (error) { + *error = true; } + return NULL; } - if (!match) { - return false; + if (!ecs_term_is_trivial(&term)) { + if (error) { + *error = true; + } + ecs_parser_error(name, expr, (ptr - expr), + "invalid non-trivial term in add expression"); + return NULL; } - - } else if (oper == EcsAndFrom || oper == EcsOrFrom || oper == EcsNotFrom) { - ecs_type_t type = get_term_type((ecs_world_t*)world, term, term->id); - int32_t match_count = 0, j, count = ecs_vector_count(type); - ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t); - - for (j = 0; j < count; j ++) { - ecs_term_t tmp_term = *term; - tmp_term.oper = EcsAnd; - tmp_term.id = ids[j]; - tmp_term.pred.entity = ids[j]; - if (match_term(world, table, &tmp_term)) { - match_count ++; + if (term.oper == EcsAnd || !replace_and) { + /* Regular AND expression */ + table = table_append(world, table, term.id, diff); + } else if (term.oper == EcsAndFrom) { + /* Add all components from the specified type */ + const EcsType *t = ecs_get(world, term.id, EcsType); + if (!t) { + if (error) { + *error = true; + } + ecs_parser_error(name, expr, (ptr - expr), + "expected type for AND role"); + return NULL; + } + + ecs_id_t *ids = ecs_vector_first(t->normalized, ecs_id_t); + int32_t i, count = ecs_vector_count(t->normalized); + for (i = 0; i < count; i ++) { + table = table_append(world, table, ids[i], diff); } } - if (oper == EcsAndFrom && match_count != count) { - return false; - } - if (oper == EcsOrFrom && match_count == 0) { - return false; - } - if (oper == EcsNotFrom && match_count != 0) { - return false; + ecs_term_fini(&term); + } + + if (!ptr) { + if (error) { + *error = true; } + return NULL; } } - return true; + return table; } -/** Match existing tables against system (table is created before system) */ +/* Add/remove components based on the parsed expression. This operation is + * slower than traverse_from_expr, but safe to use from a deferred context. */ static -void match_tables( +void defer_from_expr( ecs_world_t *world, - ecs_query_t *query) + ecs_entity_t entity, + const char *name, + const char *expr, + bool is_add, + bool replace_and) { - int32_t i, count = flecs_sparse_count(world->store.tables); + const char *ptr = expr; + if (ptr) { + ecs_term_t term = {0}; + while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))) { + if (!ecs_term_is_initialized(&term)) { + break; + } - for (i = 0; i < count; i ++) { - ecs_table_t *table = flecs_sparse_get_dense( - world->store.tables, ecs_table_t, i); + if (ecs_term_finalize(world, name, &term)) { + return; + } - if (flecs_query_match(world, table, query)) { - add_table(world, query, table); + if (!ecs_term_is_trivial(&term)) { + ecs_parser_error(name, expr, (ptr - expr), + "invalid non-trivial term in add expression"); + return; + } + + if (term.oper == EcsAnd || !replace_and) { + /* Regular AND expression */ + if (is_add) { + ecs_add_id(world, entity, term.id); + } else { + ecs_remove_id(world, entity, term.id); + } + } else if (term.oper == EcsAndFrom) { + /* Add all components from the specified type */ + const EcsType *t = ecs_get(world, term.id, EcsType); + if (!t) { + ecs_parser_error(name, expr, (ptr - expr), + "expected type for AND role"); + return; + } + + ecs_id_t *ids = ecs_vector_first(t->normalized, ecs_id_t); + int32_t i, count = ecs_vector_count(t->normalized); + for (i = 0; i < count; i ++) { + if (is_add) { + ecs_add_id(world, entity, ids[i]); + } else { + ecs_remove_id(world, entity, ids[i]); + } + } + } + + ecs_term_fini(&term); } } } +#endif -#define ELEM(ptr, size, index) ECS_OFFSET(ptr, size * index) - -static -int32_t qsort_partition( +/* If operation is not deferred, add components by finding the target + * table and moving the entity towards it. */ +static +int traverse_add( ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - ecs_entity_t *entities, - void *ptr, - int32_t elem_size, - int32_t lo, - int32_t hi, - ecs_order_by_action_t compare) + ecs_entity_t result, + const char *name, + const ecs_entity_desc_t *desc, + ecs_entity_t scope, + ecs_id_t with, + bool new_entity, + bool name_assigned) { - int32_t p = (hi + lo) / 2; - void *pivot = ELEM(ptr, elem_size, p); - ecs_entity_t pivot_e = entities[p]; - int32_t i = lo - 1, j = hi + 1; - void *el; + const char *sep = desc->sep; -repeat: - { - do { - i ++; - el = ELEM(ptr, elem_size, i); - } while ( compare(entities[i], el, pivot_e, pivot) < 0); + /* Find existing table */ + ecs_entity_info_t info = {0}; + ecs_table_t *src_table = NULL, *table = NULL; + if (!new_entity) { + if (flecs_get_info(world, result, &info)) { + table = info.table; + } + } - do { - j --; - el = ELEM(ptr, elem_size, j); - } while ( compare(entities[j], el, pivot_e, pivot) > 0); - if (i >= j) { - return j; + /* Find destination table */ + ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; + + /* If this is a new entity without a name, add the scope. If a name is + * provided, the scope will be added by the add_path_w_sep function */ + if (new_entity) { + if (new_entity && scope && !name && !name_assigned) { + table = table_append( + world, table, ecs_pair(EcsChildOf, scope), &diff); + } + if (with) { + table = table_append(world, table, with, &diff); } + } - flecs_table_swap(world, table, data, i, j); + /* If a name is provided but not yet assigned, add the Name component */ + if (name && !name_assigned) { + table = table_append(world, table, + ecs_pair(ecs_id(EcsIdentifier), EcsName), &diff); + } - if (p == i) { - pivot = ELEM(ptr, elem_size, j); - pivot_e = entities[j]; - } else if (p == j) { - pivot = ELEM(ptr, elem_size, i); - pivot_e = entities[i]; + /* Add components from the 'add' id array */ + int32_t i = 0; + ecs_id_t id; + const ecs_id_t *ids = desc->add; + while ((i < ECS_MAX_ADD_REMOVE) && (id = ids[i ++])) { + table = table_append(world, table, id, &diff); + } + + /* Add components from the 'add_expr' expression */ + if (desc->add_expr) { +#ifdef FLECS_PARSER + bool error = false; + table = traverse_from_expr( + world, table, name, desc->add_expr, &diff, true, &error); + if (error) { + return -1; } +#else + ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); +#endif + } - goto repeat; + /* Commit entity to destination table */ + if (src_table != table) { + commit(world, result, &info, table, &diff, true); } -} -static -void qsort_array( - ecs_world_t *world, - ecs_table_t *table, - ecs_data_t *data, - ecs_entity_t *entities, - void *ptr, - int32_t size, - int32_t lo, - int32_t hi, - ecs_order_by_action_t compare) -{ - if ((hi - lo) < 1) { - return; + /* Set name */ + if (name && !name_assigned) { + ecs_add_path_w_sep(world, result, scope, name, sep, NULL); + ecs_assert(ecs_get_name(world, result) != NULL, + ECS_INTERNAL_ERROR, NULL); } - int32_t p = qsort_partition( - world, table, data, entities, ptr, size, lo, hi, compare); + if (desc->symbol) { + const char *sym = ecs_get_symbol(world, result); + if (sym) { + ecs_assert(!ecs_os_strcmp(desc->symbol, sym), + ECS_INCONSISTENT_NAME, desc->symbol); + } else { + ecs_set_symbol(world, result, desc->symbol); + } + } - qsort_array(world, table, data, entities, ptr, size, lo, p, compare); + diff_free(&diff); - qsort_array(world, table, data, entities, ptr, size, p + 1, hi, compare); + return 0; } -static -void sort_table( +/* When in deferred mode, we need to add/remove components one by one using + * the regular operations. */ +static +void deferred_add_remove( ecs_world_t *world, - ecs_table_t *table, - int32_t column_index, - ecs_order_by_action_t compare) + ecs_entity_t entity, + const char *name, + const ecs_entity_desc_t *desc, + ecs_entity_t scope, + ecs_id_t with, + bool new_entity, + bool name_assigned) { - ecs_data_t *data = &table->storage; - if (!data->entities) { - /* Nothing to sort */ - return; - } - - int32_t count = flecs_table_data_count(data); - if (count < 2) { - return; - } + const char *sep = desc->sep; - ecs_entity_t *entities = ecs_vector_first(data->entities, ecs_entity_t); + /* If this is a new entity without a name, add the scope. If a name is + * provided, the scope will be added by the add_path_w_sep function */ + if (new_entity) { + if (new_entity && scope && !name && !name_assigned) { + ecs_add_id(world, entity, ecs_pair(EcsChildOf, scope)); + } - void *ptr = NULL; - int32_t size = 0; - if (column_index != -1) { - ecs_column_t *column = &data->columns[column_index]; - size = column->size; - ptr = ecs_vector_first_t(column->data, size, column->alignment); + if (with) { + ecs_add_id(world, entity, with); + } } - qsort_array(world, table, data, entities, ptr, size, 0, count - 1, compare); -} + /* Add components from the 'add' id array */ + int32_t i = 0; + ecs_id_t id; + const ecs_id_t *ids = desc->add; + while ((i < ECS_MAX_ADD_REMOVE) && (id = ids[i ++])) { + ecs_add_id(world, entity, id); + } -/* Helper struct for building sorted table ranges */ -typedef struct sort_helper_t { - ecs_query_table_match_t *match; - ecs_entity_t *entities; - const void *ptr; - int32_t row; - int32_t elem_size; - int32_t count; - bool shared; -} sort_helper_t; + /* Add components from the 'add_expr' expression */ + if (desc->add_expr) { +#ifdef FLECS_PARSER + defer_from_expr(world, entity, name, desc->add_expr, true, true); +#else + ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); +#endif + } -static -const void* ptr_from_helper( - sort_helper_t *helper) -{ - ecs_assert(helper->row < helper->count, ECS_INTERNAL_ERROR, NULL); - ecs_assert(helper->elem_size >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(helper->row >= 0, ECS_INTERNAL_ERROR, NULL); - if (helper->shared) { - return helper->ptr; - } else { - return ELEM(helper->ptr, helper->elem_size, helper->row); + /* Set name */ + if (name && !name_assigned) { + ecs_add_path_w_sep(world, entity, scope, name, sep, NULL); } -} -static -ecs_entity_t e_from_helper( - sort_helper_t *helper) -{ - if (helper->row < helper->count) { - return helper->entities[helper->row]; - } else { - return 0; + /* Currently it's not supported to set the symbol from a deferred context */ + if (desc->symbol) { + const char *sym = ecs_get_symbol(world, entity); + ecs_assert(!ecs_os_strcmp(sym, desc->symbol), ECS_UNSUPPORTED, NULL); + (void)sym; } } -static -void build_sorted_table_range( - ecs_query_t *query, - ecs_query_table_list_t *list) +ecs_entity_t ecs_entity_init( + ecs_world_t *world, + const ecs_entity_desc_t *desc) { - ecs_world_t *world = query->world; - ecs_entity_t component = query->order_by_component; - ecs_order_by_action_t compare = query->order_by; - - if (!list->count) { - return; - } + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(desc != NULL, ECS_INTERNAL_ERROR, NULL); - int to_sort = 0; - sort_helper_t *helper = ecs_os_malloc_n(sort_helper_t, list->count); - ecs_query_table_node_t *cur, *end = list->last->next; - for (cur = list->first; cur != end; cur = cur->next) { - ecs_query_table_match_t *match = cur->match; - ecs_table_t *table = match->table; - ecs_data_t *data = &table->storage; - ecs_vector_t *entities; - if (!(entities = data->entities) || !ecs_table_count(table)) { - continue; - } + ecs_stage_t *stage = flecs_stage_from_world(&world); + ecs_entity_t scope = ecs_get_scope(world); + ecs_id_t with = ecs_get_with(world); - int32_t index = ecs_type_index_of(table->type, 0, component); - if (index != -1) { - ecs_column_t *column = &data->columns[index]; - int16_t size = column->size; - int16_t align = column->alignment; - helper[to_sort].ptr = ecs_vector_first_t(column->data, size, align); - helper[to_sort].elem_size = size; - helper[to_sort].shared = false; - } else if (component) { - /* Find component in prefab */ - ecs_entity_t base; - ecs_type_match(world, table, table->type, 0, component, - EcsIsA, 1, 0, &base, NULL); + const char *name = desc->name; + const char *sep = desc->sep; + if (!sep) { + sep = "."; + } - /* If a base was not found, the query should not have allowed using - * the component for sorting */ - ecs_assert(base != 0, ECS_INTERNAL_ERROR, NULL); + const char *root_sep = desc->root_sep; - const EcsComponent *cptr = ecs_get(world, component, EcsComponent); - ecs_assert(cptr != NULL, ECS_INTERNAL_ERROR, NULL); + bool new_entity = false; + bool name_assigned = false; - helper[to_sort].ptr = ecs_get_id(world, base, component); - helper[to_sort].elem_size = cptr->size; - helper[to_sort].shared = true; - } else { - helper[to_sort].ptr = NULL; - helper[to_sort].elem_size = 0; - helper[to_sort].shared = false; + /* Remove optional prefix from name. Entity names can be derived from + * language identifiers, such as components (typenames) and systems + * function names). Because C does not have namespaces, such identifiers + * often encode the namespace as a prefix. + * To ensure interoperability between C and C++ (and potentially other + * languages with namespacing) the entity must be stored without this prefix + * and with the proper namespace, which is what the name_prefix is for */ + const char *prefix = world->name_prefix; + if (name && prefix) { + ecs_size_t len = ecs_os_strlen(prefix); + if (!ecs_os_strncmp(name, prefix, len) && + (isupper(name[len]) || name[len] == '_')) + { + if (name[len] == '_') { + name = name + len + 1; + } else { + name = name + len; + } } - - helper[to_sort].match = match; - helper[to_sort].entities = ecs_vector_first(entities, ecs_entity_t); - helper[to_sort].row = 0; - helper[to_sort].count = ecs_table_count(table); - to_sort ++; } - bool proceed; - do { - int32_t j, min = 0; - proceed = true; + /* Find or create entity */ + ecs_entity_t result = desc->entity; + if (!result) { + if (name) { + result = ecs_lookup_path_w_sep( + world, scope, name, sep, root_sep, false); + if (result) { + name_assigned = true; + } + } - ecs_entity_t e1; - while (!(e1 = e_from_helper(&helper[min]))) { - min ++; - if (min == to_sort) { - proceed = false; - break; + if (!result) { + if (desc->use_low_id) { + result = ecs_new_low_id(world); + } else { + result = ecs_new_id(world); + } + new_entity = true; + ecs_assert(ecs_get_type(world, result) == NULL, + ECS_INTERNAL_ERROR, NULL); + } + } else { + ecs_assert(ecs_is_valid(world, result), ECS_INVALID_PARAMETER, NULL); + + name_assigned = ecs_has_pair( + world, result, ecs_id(EcsIdentifier), EcsName); + if (name && name_assigned) { + /* If entity has name, verify that name matches */ + char *path = ecs_get_path_w_sep(world, scope, result, sep, NULL); + if (path) { + if (ecs_os_strcmp(path, name)) { + /* Mismatching name */ + ecs_os_free(path); + return 0; + } + ecs_os_free(path); } } + } + + ecs_assert(name_assigned == ecs_has_pair( + world, result, ecs_id(EcsIdentifier), EcsName), + ECS_INTERNAL_ERROR, NULL); - if (!proceed) { - break; + if (stage->defer) { + deferred_add_remove((ecs_world_t*)stage, result, name, desc, + scope, with, new_entity, name_assigned); + } else { + if (traverse_add(world, result, name, desc, + scope, with, new_entity, name_assigned)) + { + return 0; } + } - for (j = min + 1; j < to_sort; j++) { - ecs_entity_t e2 = e_from_helper(&helper[j]); - if (!e2) { - continue; - } + return result; +} - const void *ptr1 = ptr_from_helper(&helper[min]); - const void *ptr2 = ptr_from_helper(&helper[j]); +const ecs_entity_t* ecs_bulk_init( + ecs_world_t *world, + const ecs_bulk_desc_t *desc) +{ + const ecs_entity_t *entities = desc->entities; + int32_t count = desc->count; - if (compare(e1, ptr1, e2, ptr2) > 0) { - min = j; - e1 = e_from_helper(&helper[min]); - } + int32_t sparse_count = 0; + if (!entities) { + sparse_count = ecs_eis_count(world); + entities = flecs_sparse_new_ids(world->store.entity_index, count); + ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); + } else { + int i; + for (i = 0; i < count; i ++) { + ecs_ensure(world, entities[i]); } + } - sort_helper_t *cur_helper = &helper[min]; - if (!cur || cur->match != cur_helper->match) { - cur = ecs_vector_add(&query->table_slices, ecs_query_table_node_t); - ecs_assert(cur != NULL, ECS_INTERNAL_ERROR, NULL); - cur->match = cur_helper->match; - cur->offset = cur_helper->row; - cur->count = 1; - } else { - cur->count ++; + ecs_ids_t ids; + + ecs_table_t *table = desc->table; + ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; + if (!table) { + int32_t i = 0; + ecs_id_t id; + while ((id = desc->ids[i])) { + table = table_append(world, table, id, &diff); + i ++; } - cur_helper->row ++; - } while (proceed); + ids.array = (ecs_id_t*)desc->ids; + ids.count = i; + } else { + diff.added.array = ecs_vector_first(table->type, ecs_id_t); + diff.added.count = ecs_vector_count(table->type); - /* Iterate through the vector of slices to set the prev/next ptrs. This - * can't be done while building the vector, as reallocs may occur */ - int32_t i, count = ecs_vector_count(query->table_slices); - ecs_query_table_node_t *nodes = ecs_vector_first( - query->table_slices, ecs_query_table_node_t); - for (i = 0; i < count; i ++) { - nodes[i].prev = &nodes[i - 1]; - nodes[i].next = &nodes[i + 1]; + ids = diff.added; } - nodes[0].prev = NULL; - nodes[i - 1].next = NULL; + new_w_data( + world, table, entities, &ids, count, desc->data, true, NULL, &diff); - ecs_os_free(helper); + if (!sparse_count) { + return entities; + } else { + /* Refetch entity ids, in case the underlying array was reallocated */ + entities = flecs_sparse_ids(world->store.entity_index); + return &entities[sparse_count]; + } } -static -void build_sorted_tables( - ecs_query_t *query) +ecs_entity_t ecs_component_init( + ecs_world_t *world, + const ecs_component_desc_t *desc) { - ecs_vector_clear(query->table_slices); + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + world = (ecs_world_t*)ecs_get_world(world); - if (query->group_by) { - /* Populate sorted node list in grouping order */ - ecs_query_table_node_t *cur = query->list.first; - if (cur) { - do { - /* Find list for current group */ - ecs_query_table_match_t *match = cur->match; - ecs_assert(match != NULL, ECS_INTERNAL_ERROR, NULL); - uint64_t group_id = match->group_id; - ecs_query_table_list_t *list = ecs_map_get(query->groups, - ecs_query_table_list_t, group_id); - ecs_assert(list != NULL, ECS_INTERNAL_ERROR, NULL); + bool is_readonly = world->is_readonly; + bool is_deferred = ecs_is_deferred(world); + int32_t defer_count = 0; + ecs_vector_t *defer_queue = NULL; + ecs_stage_t *stage = NULL; - /* Sort tables in current group */ - build_sorted_table_range(query, list); - - /* Find next group to sort */ - cur = list->last->next; - } while (cur); - } - } else { - build_sorted_table_range(query, &query->list); + /* If world is readonly or deferring is enabled, component registration can + * still happen directly on the main storage, but only if the application + * is singlethreaded. */ + if (is_readonly || is_deferred) { + ecs_assert(ecs_get_stage_count(world) <= 1, + ECS_INVALID_WHILE_ITERATING, NULL); + + /* Silence readonly warnings */ + world->is_readonly = false; + + /* Hack around safety checks (this ought to look ugly) */ + ecs_world_t *temp_world = world; + stage = flecs_stage_from_world(&temp_world); + defer_count = stage->defer; + defer_queue = stage->defer_queue; + stage->defer = 0; + stage->defer_queue = NULL; } -} -static -bool tables_dirty( - ecs_query_t *query) -{ - bool is_dirty = false; + ecs_entity_desc_t entity_desc = desc->entity; + entity_desc.use_low_id = true; + if (!entity_desc.symbol) { + entity_desc.symbol = entity_desc.name; + } - ecs_vector_t *vec = query->cache.tables; - ecs_query_table_t *tables = ecs_vector_first(vec, ecs_query_table_t); - int32_t i, count = ecs_vector_count(vec); + ecs_entity_t e = desc->entity.entity; + ecs_entity_t result = ecs_entity_init(world, &entity_desc); + if (!result) { + return 0; + } - for (i = 0; i < count; i ++) { - ecs_query_table_t *qt = &tables[i]; - ecs_table_t *table = qt->hdr.table; + bool added = false; + EcsComponent *ptr = ecs_get_mut(world, result, EcsComponent, &added); - if (!qt->monitor) { - qt->monitor = flecs_table_get_monitor(table); - is_dirty = true; + if (added) { + ptr->size = flecs_from_size_t(desc->size); + ptr->alignment = flecs_from_size_t(desc->alignment); + if (!ptr->size) { + ecs_trace("#[green]tag#[reset] %s registered", + ecs_get_name(world, result)); + } else { + ecs_trace("#[green]component#[reset] %s registered", + ecs_get_name(world, result)); } - - int32_t *dirty_state = flecs_table_get_dirty_state(table); - int32_t t, storage_count = ecs_table_storage_count(table); - for (t = 0; t < storage_count + 1; t ++) { - is_dirty = is_dirty || (dirty_state[t] != qt->monitor[t]); + } else { + if (ptr->size != flecs_from_size_t(desc->size)) { + ecs_abort(ECS_INVALID_COMPONENT_SIZE, desc->entity.name); + } + if (ptr->alignment != flecs_from_size_t(desc->alignment)) { + ecs_abort(ECS_INVALID_COMPONENT_ALIGNMENT, desc->entity.name); } } - is_dirty = is_dirty || (query->match_count != query->prev_match_count); - - return is_dirty; -} + ecs_modified(world, result, EcsComponent); -static -void tables_reset_dirty( - ecs_query_t *query) -{ - query->prev_match_count = query->match_count; + if (e > world->stats.last_component_id && e < ECS_HI_COMPONENT_ID) { + world->stats.last_component_id = e + 1; + } - ecs_vector_t *vec = query->cache.tables; - ecs_query_table_t *tables = ecs_vector_first(vec, ecs_query_table_t); - int32_t i, count = ecs_vector_count(vec); + /* Ensure components cannot be deleted */ + ecs_add_pair(world, result, EcsOnDelete, EcsThrow); - for (i = 0; i < count; i ++) { - ecs_query_table_t *qt = &tables[i]; - ecs_table_t *table = qt->hdr.table; + if (is_readonly || is_deferred) { + /* Restore readonly state / defer count */ + world->is_readonly = is_readonly; + stage->defer = defer_count; + stage->defer_queue = defer_queue; + } - if (!qt->monitor) { - /* If one table doesn't have a monitor, none of the tables will have - * a monitor, so early out. */ - return; - } + ecs_assert(result != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(ecs_has(world, result, EcsComponent), ECS_INTERNAL_ERROR, NULL); - int32_t *dirty_state = flecs_table_get_dirty_state(table); - int32_t t, storage_count = ecs_table_storage_count(table); - for (t = 0; t < storage_count + 1; t ++) { - qt->monitor[t] = dirty_state[t]; - } - } + return result; } -static -void sort_tables( +ecs_entity_t ecs_type_init( ecs_world_t *world, - ecs_query_t *query) + const ecs_type_desc_t *desc) { - ecs_order_by_action_t compare = query->order_by; - if (!compare) { - return; + ecs_entity_t result = ecs_entity_init(world, &desc->entity); + if (!result) { + return 0; } - - ecs_entity_t order_by_component = query->order_by_component; - /* Iterate over non-empty tables. Don't bother with empty tables as they - * have nothing to sort */ + ecs_table_t *table = NULL, *normalized = NULL; + ecs_table_diff_t temp_diff, diff = ECS_TABLE_DIFF_INIT; - bool tables_sorted = false; - ecs_vector_t *vec = query->cache.tables; - ecs_query_table_t *tables = ecs_vector_first(vec, ecs_query_table_t); - int32_t i, count = ecs_vector_count(vec); + /* Find destination table (and type) */ - for (i = 0; i < count; i ++) { - ecs_query_table_t *qt = &tables[i]; - ecs_table_t *table = qt->hdr.table; + /* Add components from the 'add' id array */ + int32_t i = 0; + ecs_id_t id; + const ecs_id_t *ids = desc->ids; + while ((i < ECS_MAX_ADD_REMOVE) && (id = ids[i ++])) { + normalized = flecs_table_traverse_add( + world, normalized, &id, &temp_diff); + table = flecs_table_traverse_add(world, table, &id, NULL); + ecs_assert(table != NULL, ECS_INVALID_PARAMETER, NULL); + diff_append(&diff, &temp_diff); + } - /* If no monitor had been created for the table yet, create it now */ - bool is_dirty = false; - if (!qt->monitor) { - qt->monitor = flecs_table_get_monitor(table); + /* If expression is set, add it to the table */ + if (desc->ids_expr) { +#ifdef FLECS_PARSER + bool error = false; - /* A new table is always dirty */ - is_dirty = true; + normalized = traverse_from_expr(world, normalized, desc->entity.name, + desc->ids_expr, &diff, true, &error); + if (error) { + return 0; } - int32_t *dirty_state = flecs_table_get_dirty_state(table); - is_dirty = is_dirty || (dirty_state[0] != qt->monitor[0]); - - int32_t index = -1; - if (order_by_component) { - /* Get index of sorted component. We only care if the component we're - * sorting on has changed or if entities have been added / re(moved) */ - index = ecs_type_index_of(table->type, 0, order_by_component); - if (index != -1) { - ecs_assert(index < ecs_vector_count(table->type), - ECS_INTERNAL_ERROR, NULL); - is_dirty = is_dirty || (dirty_state[index + 1] != qt->monitor[index + 1]); - } else { - /* Table does not contain component which means the sorted - * component is shared. Table does not need to be sorted */ - continue; - } - } - - /* Check both if entities have moved (element 0) or if the component - * we're sorting on has changed (index + 1) */ - if (is_dirty) { - /* Sort the table */ - sort_table(world, table, index, compare); - tables_sorted = true; + table = traverse_from_expr(world, table, desc->entity.name, + desc->ids_expr, &diff, false, &error); + if (error) { + return 0; } +#else + ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); +#endif } - if (tables_sorted || query->match_count != query->prev_match_count) { - build_sorted_tables(query); - query->match_count ++; /* Increase version if tables changed */ + diff_free(&diff); + + ecs_type_t type = NULL; + ecs_type_t normalized_type = NULL; + + if (table) { + type = table->type; + } + if (normalized) { + normalized_type = normalized->type; } -} -static -bool has_refs( - ecs_query_t *query) -{ - ecs_term_t *terms = query->filter.terms; - int32_t i, count = query->filter.term_count; + bool add = false; + EcsType *type_ptr = ecs_get_mut(world, result, EcsType, &add); + if (add) { + type_ptr->type = type; + type_ptr->normalized = normalized_type; - for (i = 0; i < count; i ++) { - ecs_term_t *term = &terms[i]; - ecs_term_id_t *subj = &term->subj; + /* This will allow the type to show up in debug tools */ + if (type) { + ecs_map_set(world->type_handles, (uintptr_t)type, &result); + } - if (term->oper == EcsNot && !subj->entity) { - /* Special case: if oper kind is Not and the query contained a - * shared expression, the expression is translated to FromEmpty to - * prevent resolving the ref */ - return true; - } else if (subj->entity && (subj->entity != EcsThis || subj->set.mask != EcsSelf)) { - /* If entity is not this, or if it can be substituted by other - * entities, the query can have references. */ - return true; - } + ecs_modified(world, result, EcsType); + } else { + if (type_ptr->type != type) { + ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); + } + if (type_ptr->normalized != normalized_type) { + ecs_abort(ECS_ALREADY_DEFINED, desc->entity.name); + } } - return false; + return result; } -static -bool has_pairs( - ecs_query_t *query) +const ecs_entity_t* ecs_bulk_new_w_id( + ecs_world_t *world, + ecs_id_t id, + int32_t count) { - ecs_term_t *terms = query->filter.terms; - int32_t i, count = query->filter.term_count; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_stage_t *stage = flecs_stage_from_world(&world); - for (i = 0; i < count; i ++) { - if (ecs_id_is_wildcard(terms[i].id)) { - return true; - } + const ecs_entity_t *ids; + if (flecs_defer_bulk_new(world, stage, count, id, &ids)) { + return ids; } - return false; + ecs_table_t *table = &world->store.root; + ecs_table_diff_t diff = ECS_TABLE_DIFF_INIT; + + if (id) { + table = table_append(world, table, id, &diff); + } + + ids = new_w_data(world, table, NULL, NULL, count, NULL, false, NULL, &diff); + flecs_defer_flush(world, stage); + + return ids; } -static -void register_monitors( +void ecs_clear( ecs_world_t *world, - ecs_query_t *query) + ecs_entity_t entity) { - ecs_term_t *terms = query->filter.terms; - int32_t i, count = query->filter.term_count; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - for (i = 0; i < count; i++) { - ecs_term_t *term = &terms[i]; - ecs_term_id_t *subj = &term->subj; + ecs_stage_t *stage = flecs_stage_from_world(&world); + if (flecs_defer_clear(world, stage, entity)) { + return; + } - /* If component is requested with EcsCascade register component as a - * parent monitor. Parent monitors keep track of whether an entity moved - * in the hierarchy, which potentially requires the query to reorder its - * tables. - * Also register a regular component monitor for EcsCascade columns. - * This ensures that when the component used in the EcsCascade column - * is added or removed tables are updated accordingly*/ - if (subj->set.mask & EcsSuperSet && subj->set.mask & EcsCascade && - subj->set.relation != EcsIsA) - { - if (term->oper != EcsOr) { - if (term->subj.set.relation != EcsIsA) { - flecs_monitor_register( - world, term->subj.set.relation, term->id, query); - } - flecs_monitor_register(world, 0, term->id, query); - } + ecs_entity_info_t info; + info.table = NULL; - /* FromAny also requires registering a monitor, as FromAny columns can - * be matched with prefabs. The only term kinds that do not require - * registering a monitor are FromOwned and FromEmpty. */ - } else if ((subj->set.mask & EcsSuperSet) || (subj->entity != EcsThis)){ - if (term->oper != EcsOr) { - flecs_monitor_register(world, 0, term->id, query); - } - } - }; + flecs_get_info(world, entity, &info); + + ecs_table_t *table = info.table; + if (table) { + ecs_table_diff_t diff = { + .removed = flecs_type_to_ids(table->type), + .un_set = flecs_type_to_ids(table->storage_type) + }; + + delete_entity(world, table, &table->storage, info.row, &diff); + info.record->table = NULL; + info.record->row = 0; + } + + flecs_defer_flush(world, stage); } static -void process_signature( +void on_delete_any_w_entity( ecs_world_t *world, - ecs_query_t *query) -{ - ecs_term_t *terms = query->filter.terms; - int32_t i, count = query->filter.term_count; - - for (i = 0; i < count; i ++) { - ecs_term_t *term = &terms[i]; - ecs_term_id_t *pred = &term->pred; - ecs_term_id_t *subj = &term->subj; - ecs_term_id_t *obj = &term->obj; - ecs_oper_kind_t op = term->oper; - ecs_inout_kind_t inout = term->inout; + ecs_entity_t entity, + ecs_entity_t action); - (void)pred; - (void)obj; +static +void throw_invalid_delete( + ecs_world_t *world, + ecs_id_t id) +{ + ecs_abort(ECS_INVALID_DELETE, ecs_id_str(world, id)); +} - /* Queries do not support variables */ - ecs_assert(pred->var != EcsVarIsVariable, - ECS_UNSUPPORTED, NULL); - ecs_assert(subj->entity == EcsThis || subj->var != EcsVarIsVariable, - ECS_UNSUPPORTED, NULL); - ecs_assert(obj->var != EcsVarIsVariable, - ECS_UNSUPPORTED, NULL); +static +void remove_from_table( + ecs_world_t *world, + ecs_table_t *src_table, + ecs_id_t id, + int32_t column, + int32_t column_count) +{ + ecs_table_diff_t temp_diff, diff = ECS_TABLE_DIFF_INIT; + ecs_table_t *dst_table = src_table; + ecs_id_t *ids = ecs_vector_first(src_table->type, ecs_id_t); - /* If self is not included in set, always start from depth 1 */ - if (!subj->set.min_depth && !(subj->set.mask & EcsSelf)) { - subj->set.min_depth = 1; - } + /* If id is pair but the column pointed to is not a pair, the record is + * pointing to an instance of the id that has a (non-PAIR) role. */ + bool is_pair = ECS_HAS_ROLE(id, PAIR); + bool is_role = is_pair && !ECS_HAS_ROLE(ids[column], PAIR); + ecs_assert(!is_role || ((ids[column] & ECS_ROLE_MASK) != 0), + ECS_INTERNAL_ERROR, NULL); + bool is_wildcard = ecs_id_is_wildcard(id); - if (inout != EcsIn) { - query->flags |= EcsQueryHasOutColumns; - } + int32_t i, count = ecs_vector_count(src_table->type), removed_count = 0; + ecs_entity_t entity = ECS_PAIR_RELATION(id); - if (op == EcsOptional) { - query->flags |= EcsQueryHasOptional; - } + for (i = column; i < count; i ++) { + ecs_id_t e = ids[i]; - if (!(query->flags & EcsQueryMatchDisabled)) { - if (op == EcsAnd || op == EcsOr || op == EcsOptional) { - if (term->id == EcsDisabled) { - query->flags |= EcsQueryMatchDisabled; - } + if (is_role) { + if ((e & ECS_COMPONENT_MASK) != entity) { + continue; } + } else if (is_wildcard && !ecs_id_match(e, id)) { + continue; } - if (!(query->flags & EcsQueryMatchPrefab)) { - if (op == EcsAnd || op == EcsOr || op == EcsOptional) { - if (term->id == EcsPrefab) { - query->flags |= EcsQueryMatchPrefab; - } - } + dst_table = flecs_table_traverse_remove(world, dst_table, &e, &temp_diff); + diff_append(&diff, &temp_diff); + + removed_count ++; + if (removed_count == column_count) { + break; } + } - if (subj->entity == EcsThis) { - query->flags |= EcsQueryNeedsTables; - } + ecs_assert(dst_table != NULL, ECS_INTERNAL_ERROR, NULL); - if (subj->set.mask & EcsCascade && term->oper == EcsOptional) { - /* Query can only have one cascade column */ - ecs_assert(query->cascade_by == 0, ECS_INVALID_PARAMETER, NULL); - query->cascade_by = i + 1; - } + if (!dst_table->type) { + /* If this removes all components, clear table */ + flecs_table_clear_entities(world, src_table); + } else { + /* Otherwise, merge table into dst_table */ + if (dst_table != src_table) { + ecs_data_t *src_data = &src_table->storage; + int32_t src_count = ecs_table_count(src_table); + if (diff.removed.count) { + flecs_notify_on_remove(world, src_table, NULL, + 0, src_count, &diff); + } - if (subj->entity && subj->entity != EcsThis && - subj->set.mask == EcsSelf) - { - flecs_set_watch(world, term->subj.entity); + flecs_table_merge(world, dst_table, src_table, + &dst_table->storage, src_data); } } - query->flags |= (ecs_flags32_t)(has_refs(query) * EcsQueryHasRefs); - query->flags |= (ecs_flags32_t)(has_pairs(query) * EcsQueryHasTraits); - - if (!(query->flags & EcsQueryIsSubquery)) { - register_monitors(world, query); - } + diff_free(&diff); } static -bool match_table( +void delete_objects( ecs_world_t *world, - ecs_query_t *query, ecs_table_t *table) { - if (flecs_query_match(world, table, query)) { - add_table(world, query, table); - return true; - } - return false; + ecs_data_t *data = &table->storage; + if (data) { + ecs_entity_t *entities = ecs_vector_first( + data->entities, ecs_entity_t); + + int32_t i, count = ecs_vector_count(data->entities); + for (i = 0; i < count; i ++) { + ecs_entity_t e = entities[i]; + ecs_record_t *r = flecs_sparse_get( + world->store.entity_index, ecs_record_t, e); + + /* If row is negative, it means the entity is being monitored. Only + * monitored entities can have delete actions */ + if (r && r->row < 0) { + /* Make row positive which prevents infinite recursion in case + * of cyclic delete actions */ + r->row = (-r->row); + + /* Run delete actions for objects */ + on_delete_any_w_entity(world, entities[i], 0); + } + } + + /* Clear components from table (invokes destructors, OnRemove) */ + flecs_table_delete_entities(world, table); + } } -/** When a table becomes empty remove it from the query list, or vice versa. */ static -void update_table( - ecs_query_t *query, - ecs_table_t *table, - bool empty) +void on_delete_object_action( + ecs_world_t *world, + ecs_id_t id, + ecs_entity_t action) { - int32_t prev_count = ecs_query_table_count(query); - ecs_table_cache_set_empty(&query->cache, table, empty); - int32_t cur_count = ecs_query_table_count(query); + ecs_id_record_t *idr = flecs_get_id_record(world, id); + if (idr) { + const ecs_table_record_t *tables = flecs_id_record_tables(idr); + int32_t i, count = flecs_id_record_count(idr); - if (prev_count != cur_count) { - ecs_query_table_t *qt = ecs_table_cache_get( - &query->cache, ecs_query_table_t, table); - ecs_assert(qt != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_query_table_match_t *cur, *next; + for (i = 0; i < count; i ++) { + const ecs_table_record_t *tr = &tables[i]; + ecs_table_t *table = tr->table; - for (cur = qt->first; cur != NULL; cur = next) { - next = cur->next_match; + if (!ecs_table_count(table)) { + continue; + } - if (empty) { - ecs_assert(ecs_table_count(table) == 0, - ECS_INTERNAL_ERROR, NULL); + ecs_id_t *rel_id = ecs_vector_get(table->type, ecs_id_t, tr->column); + ecs_assert(rel_id != NULL, ECS_INTERNAL_ERROR, NULL); - remove_table_node(query, &cur->node); - } else { - ecs_assert(ecs_table_count(table) != 0, - ECS_INTERNAL_ERROR, NULL); - insert_table_node(query, &cur->node); + ecs_entity_t rel = ECS_PAIR_RELATION(*rel_id); + /* delete_object_action should be invoked for relations */ + ecs_assert(rel != 0, ECS_INTERNAL_ERROR, NULL); + + /* Get the record for the relation, to find the delete action */ + ecs_id_record_t *idrr; + if (!action) { + idrr = flecs_get_id_record(world, rel); + if (idrr) { + action =idrr->on_delete_object; + } + } + + if (!action || action == EcsRemove) { + remove_from_table(world, table, id, tr->column, tr->count); + i = 0; count = flecs_id_record_count(idr); + } else if (action == EcsDelete) { + delete_objects(world, table); + } else if (action == EcsThrow) { + throw_invalid_delete(world, id); } } - } - ecs_assert(cur_count || query->list.first == NULL, - ECS_INTERNAL_ERROR, NULL); + flecs_clear_id_record(world, id); + } } static -void add_subquery( - ecs_world_t *world, - ecs_query_t *parent, - ecs_query_t *subquery) +void on_delete_id_action( + ecs_world_t *world, + ecs_id_t id, + ecs_entity_t action) { - ecs_query_t **elem = ecs_vector_add(&parent->subqueries, ecs_query_t*); - *elem = subquery; + ecs_id_record_t *idr = flecs_get_id_record(world, id); + if (idr) { + if (!action) { + action = idr->on_delete; + } - ecs_table_cache_t *cache = &parent->cache; - ecs_vector_t *tables = cache->tables, *empty = cache->empty_tables; + if (action == EcsThrow) { + throw_invalid_delete(world, id); + } - ecs_query_table_t *elems = ecs_vector_first(tables, ecs_query_table_t); - int32_t i, count = ecs_vector_count(tables); - for (i = 0; i < count; i ++) { - match_table(world, subquery, elems[i].hdr.table); - } + const ecs_table_record_t *tables = flecs_id_record_tables(idr); + int32_t i, count = flecs_id_record_count(idr); - elems = ecs_vector_first(empty, ecs_query_table_t); - count = ecs_vector_count(empty); - for (i = 0; i < count; i ++) { - match_table(world, subquery, elems[i].hdr.table); + for (i = 0; i < count ; i ++) { + const ecs_table_record_t *tr = &tables[i]; + ecs_table_t *table = tr->table; + + if (!action || action == EcsRemove) { + remove_from_table(world, table, id, tr->column, tr->count); + } else if (action == EcsDelete) { + delete_objects(world, table); + } + } + + flecs_clear_id_record(world, id); } } static -void notify_subqueries( +void on_delete_action( ecs_world_t *world, - ecs_query_t *query, - ecs_query_event_t *event) + ecs_id_t id, + ecs_entity_t action) { - if (query->subqueries) { - ecs_query_t **queries = ecs_vector_first(query->subqueries, ecs_query_t*); - int32_t i, count = ecs_vector_count(query->subqueries); - - ecs_query_event_t sub_event = *event; - sub_event.parent_query = query; - - for (i = 0; i < count; i ++) { - ecs_query_t *sub = queries[i]; - flecs_query_notify(world, sub, &sub_event); + if (ecs_id_is_wildcard(id)) { + /* If id is wildcard, check if the relation or object is a wildcard. + * Relation wildcard ids are implemented differently as relations + * with the same object aren't guaranteed to occupy neighboring + * elements in the type, other wildcards with the same relation. */ + if (ECS_PAIR_RELATION(id) == EcsWildcard) { + on_delete_object_action(world, id, action); + } else { + on_delete_id_action(world, id, action); } + } else { + /* If the id is not a wildcard it's simple, as a table can only have + * at most one instance of the id */ + on_delete_id_action(world, id, action); } } static -void resolve_cascade_subject_for_table( +void on_delete_any_w_entity( ecs_world_t *world, - ecs_query_t *query, - const ecs_table_t *table, - ecs_type_t table_type, - ecs_query_table_match_t *table_data) + ecs_id_t id, + ecs_entity_t action) { - int32_t term_index = query->cascade_by - 1; - ecs_term_t *term = &query->filter.terms[term_index]; - - ecs_assert(table_data->references != 0, - ECS_INTERNAL_ERROR, NULL); - - /* Obtain reference index */ - int32_t *column_indices = table_data->columns; - int32_t ref_index = -column_indices[term_index] - 1; + /* Make sure any references to the entity are cleaned up */ + on_delete_action(world, id, action); + on_delete_action(world, ecs_pair(id, EcsWildcard), action); + on_delete_action(world, ecs_pair(EcsWildcard, id), action); +} - /* Obtain pointer to the reference data */ - ecs_ref_t *references = table_data->references; +void ecs_delete_with( + ecs_world_t *world, + ecs_id_t id) +{ + ecs_stage_t *stage = flecs_stage_from_world(&world); + if (flecs_defer_on_delete_action(world, stage, id, EcsDelete)) { + return; + } - /* Find source for component */ - ecs_entity_t subject; - ecs_type_match(world, table, table_type, 0, term->id, - term->subj.set.relation, 1, 0, &subject, NULL); + on_delete_action(world, id, EcsDelete); - /* If container was found, update the reference */ - if (subject) { - ecs_ref_t *ref = &references[ref_index]; - ecs_assert(ref->component == term->id, ECS_INTERNAL_ERROR, NULL); + flecs_defer_flush(world, stage); +} - references[ref_index].entity = ecs_get_alive(world, subject); - table_data->subjects[term_index] = subject; - ecs_get_ref_w_id(world, ref, subject, term->id); - } else { - references[ref_index].entity = 0; - table_data->subjects[term_index] = 0; +void ecs_remove_all( + ecs_world_t *world, + ecs_id_t id) +{ + ecs_stage_t *stage = flecs_stage_from_world(&world); + if (flecs_defer_on_delete_action(world, stage, id, EcsRemove)) { + return; } - if (ecs_table_count(table)) { - /* The subject (or depth of the subject) may have changed, so reinsert - * the node to make sure it's in the right group */ - remove_table_node(query, &table_data->node); - insert_table_node(query, &table_data->node); - } + on_delete_action(world, id, EcsRemove); + + flecs_defer_flush(world, stage); } -static -void resolve_cascade_subject( +void ecs_delete( ecs_world_t *world, - ecs_query_t *query, - ecs_query_table_t *elem, - const ecs_table_t *table, - ecs_type_t table_type) + ecs_entity_t entity) { - ecs_query_table_match_t *cur; - for (cur = elem->first; cur != NULL; cur = cur->next_match) { - resolve_cascade_subject_for_table( - world, query, table, table_type, cur); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); + + ecs_stage_t *stage = flecs_stage_from_world(&world); + if (flecs_defer_delete(world, stage, entity)) { + return; } -} -/* Remove table */ -static -void remove_table( - ecs_poly_t *poly, - void *ptr) -{ - ecs_poly_assert(poly, ecs_query_t); + ecs_record_t *r = flecs_sparse_get( + world->store.entity_index, ecs_record_t, entity); + if (r) { + ecs_entity_info_t info = {0}; + set_info_from_record(&info, r); - ecs_query_t *query = poly; - ecs_query_table_t *elem = ptr; - ecs_query_table_match_t *cur, *next; + ecs_table_t *table = info.table; + uint64_t table_id = 0; + if (table) { + table_id = table->id; + } - for (cur = elem->first; cur != NULL; cur = next) { - ecs_os_free(cur->columns); - ecs_os_free(cur->ids); - ecs_os_free(cur->subjects); - ecs_os_free(cur->sizes); - ecs_os_free(cur->references); - ecs_os_free(cur->sparse_columns); - ecs_os_free(cur->bitset_columns); + if (info.is_watched) { + /* Make row positive which prevents infinite recursion in case + * of cyclic delete actions */ + r->row = (-r->row); - if (!elem->hdr.empty) { - remove_table_node(query, &cur->node); + /* Ensure that the store contains no dangling references to the + * deleted entity (as a component, or as part of a relation) */ + on_delete_any_w_entity(world, entity, 0); + + /* Refetch data. In case of circular relations, the entity may have + * moved to a different table. */ + set_info_from_record(&info, r); + + table = info.table; + if (table) { + table_id = table->id; + } else { + table_id = 0; + } + + if (r->table) { + ecs_ids_t to_remove = flecs_type_to_ids(r->table->type); + update_component_monitors(world, entity, NULL, &to_remove); + } } - next = cur->next_match; + ecs_assert(!table_id || table, ECS_INTERNAL_ERROR, NULL); - ecs_os_free(cur); + /* If entity has components, remove them. Check if table is still alive, + * as delete actions could have deleted the table already. */ + if (table_id && flecs_sparse_is_alive(world->store.tables, table_id)) { + ecs_table_diff_t diff = { + .removed = flecs_type_to_ids(table->type), + .un_set = flecs_type_to_ids(table->storage_type) + }; + + delete_entity(world, table, info.data, info.row, &diff); + r->table = NULL; + } + + r->row = 0; + + /* Remove (and invalidate) entity after executing handlers */ + flecs_sparse_remove(world->store.entity_index, entity); } - ecs_os_free(elem->monitor); + flecs_defer_flush(world, stage); +} - elem->first = NULL; +void ecs_add_id( + ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + add_id(world, entity, id); } -static -void unmatch_table( - ecs_query_t *query, - ecs_table_t *table) +void ecs_remove_id( + ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id) { - ecs_table_cache_remove(&query->cache, ecs_query_table_t, table); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + remove_id(world, entity, id); } -static -void rematch_table( +ecs_entity_t ecs_clone( ecs_world_t *world, - ecs_query_t *query, - ecs_table_t *table) + ecs_entity_t dst, + ecs_entity_t src, + bool copy_value) { - ecs_query_table_t *match = ecs_table_cache_get( - &query->cache, ecs_query_table_t, table); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(src != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, src), ECS_INVALID_PARAMETER, NULL); - if (flecs_query_match(world, table, query)) { - /* If the table matches, and it is not currently matched, add */ - if (match == NULL) { - add_table(world, query, table); + ecs_stage_t *stage = flecs_stage_from_world(&world); + + if (!dst) { + dst = ecs_new_id(world); + } - /* If table still matches and has cascade column, reevaluate the - * sources of references. This may have changed in case - * components were added/removed to container entities */ - } else if (query->cascade_by) { - resolve_cascade_subject(world, query, match, table, table->type); + if (flecs_defer_clone(world, stage, dst, src, copy_value)) { + return dst; + } - /* If query has optional columns, it is possible that a column that - * previously had data no longer has data, or vice versa. Do a full - * rematch to make sure data is consistent. */ - } else if (query->flags & EcsQueryHasOptional) { - unmatch_table(query, table); - if (!(query->flags & EcsQueryIsSubquery)) { - flecs_table_notify(world, table, &(ecs_table_event_t){ - .kind = EcsTableQueryUnmatch, - .query = query - }); - } - add_table(world, query, table); - } - } else { - /* Table no longer matches, remove */ - if (match != NULL) { - unmatch_table(query, table); - if (!(query->flags & EcsQueryIsSubquery)) { - flecs_table_notify(world, table, &(ecs_table_event_t){ - .kind = EcsTableQueryUnmatch, - .query = query - }); - } - notify_subqueries(world, query, &(ecs_query_event_t){ - .kind = EcsQueryTableUnmatch, - .table = table - }); - } + ecs_entity_info_t src_info; + bool found = flecs_get_info(world, src, &src_info); + ecs_table_t *src_table = src_info.table; + + if (!found || !src_table) { + goto done; + } + + ecs_type_t src_type = src_table->type; + ecs_table_diff_t diff = {.added = flecs_type_to_ids(src_type)}; + + ecs_entity_info_t dst_info = {0}; + dst_info.row = new_entity(world, dst, &dst_info, src_table, &diff, true); + + if (copy_value) { + flecs_table_move(world, dst, src, src_table, dst_info.data, + dst_info.row, src_table, src_info.data, src_info.row, true); + + flecs_notify_on_set(world, src_table, dst_info.row, 1, NULL, true); } + + +done: + flecs_defer_flush(world, stage); + return dst; } -static -bool satisfy_constraints( - ecs_world_t *world, - const ecs_filter_t *filter) +const void* ecs_get_id( + const ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id) { - ecs_term_t *terms = filter->terms; - int32_t i, count = filter->term_count; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(flecs_stage_from_readonly_world(world)->asynchronous == false, + ECS_INVALID_PARAMETER, NULL); + + world = ecs_get_world(world); + + ecs_record_t *r = ecs_eis_get(world, entity); + if (!r) { + return NULL; + } - for (i = 0; i < count; i ++) { - ecs_term_t *term = &terms[i]; - ecs_term_id_t *subj = &term->subj; - ecs_oper_kind_t oper = term->oper; + ecs_table_t *table = r->table; + if (!table) { + return NULL; + } - if (oper == EcsOptional) { - continue; - } + ecs_id_record_t *idr = flecs_get_id_record(world, id); + if (!idr) { + return NULL; + } - if (subj->entity != EcsThis && subj->set.mask & EcsSelf) { - ecs_type_t type = ecs_get_type(world, subj->entity); + const ecs_table_record_t *tr = NULL; + ecs_table_t *storage_table = table->storage_table; + if (storage_table) { + tr = flecs_id_record_table(idr, storage_table); + } else { + /* If the entity does not have a storage table (has no data) but it does + * have the id, the id must be a tag, and getting a tag is illegal. */ + ecs_assert(!ecs_owns_id(world, entity, id), ECS_NOT_A_COMPONENT, NULL); + } - if (ecs_type_has_id(world, type, term->id, false)) { - if (oper == EcsNot) { - return false; - } - } else { - if (oper != EcsNot) { - return false; - } - } - } + if (!tr) { + return get_base_component(world, table, id, idr, NULL, 0); } - return true; + bool is_monitored; + int32_t row = flecs_record_to_row(r->row, &is_monitored); + + return get_component_w_index(table, tr->column, row); } -/* Rematch system with tables after a change happened to a watched entity */ -static -void rematch_tables( - ecs_world_t *world, - ecs_query_t *query, - ecs_query_t *parent_query) +const void* ecs_get_ref_w_id( + const ecs_world_t *world, + ecs_ref_t *ref, + ecs_entity_t entity, + ecs_id_t id) { - if (parent_query) { - ecs_query_table_t *tables = ecs_vector_first( - parent_query->cache.tables, ecs_query_table_t); - int32_t i, count = ecs_vector_count(parent_query->cache.tables); - for (i = 0; i < count; i ++) { - rematch_table(world, query, tables[i].hdr.table); - } + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ref != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!entity || !ref->entity || entity == ref->entity, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!id || !ref->component || id == ref->component, ECS_INVALID_PARAMETER, NULL); + ecs_record_t *record = ref->record; - tables = ecs_vector_first( - parent_query->cache.empty_tables, ecs_query_table_t); - count = ecs_vector_count(parent_query->cache.empty_tables); - for (i = 0; i < count; i ++) { - rematch_table(world, query, tables[i].hdr.table); - } - } else { - ecs_sparse_t *tables = world->store.tables; - int32_t i, count = flecs_sparse_count(tables); + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - for (i = 0; i < count; i ++) { - /* Is the system currently matched with the table? */ - ecs_table_t *table = flecs_sparse_get_dense(tables, ecs_table_t, i); - rematch_table(world, query, table); - } + entity |= ref->entity; + + if (!record) { + record = ecs_eis_get(world, entity); } - /* Enable/disable system if constraints are (not) met. If the system is - * already dis/enabled this operation has no side effects. */ - query->constraints_satisfied = satisfy_constraints(world, &query->filter); -} + if (!record || !record->table) { + return NULL; + } -static -void remove_subquery( - ecs_query_t *parent, - ecs_query_t *sub) -{ - ecs_assert(parent != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(sub != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(parent->subqueries != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_table_t *table = record->table; - int32_t i, count = ecs_vector_count(parent->subqueries); - ecs_query_t **sq = ecs_vector_first(parent->subqueries, ecs_query_t*); + if (ref->record == record && + ref->table == table && + ref->row == record->row && + ref->alloc_count == table->alloc_count) + { + return ref->ptr; + } - for (i = 0; i < count; i ++) { - if (sq[i] == sub) { - break; - } + id |= ref->component; + + int32_t row = record->row; + + ref->entity = entity; + ref->component = id; + ref->table = table; + ref->row = record->row; + ref->alloc_count = table->alloc_count; + + if (table) { + bool is_monitored; + row = flecs_record_to_row(row, &is_monitored); + ref->ptr = get_component(world, table, row, id); } - ecs_vector_remove(parent->subqueries, ecs_query_t*, i); -} + ref->record = record; -/* -- Private API -- */ + return ref->ptr; +} -void flecs_query_notify( +void* ecs_get_mut_id( ecs_world_t *world, - ecs_query_t *query, - ecs_query_event_t *event) + ecs_entity_t entity, + ecs_id_t id, + bool *is_added) { - bool notify = true; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + + ecs_stage_t *stage = flecs_stage_from_world(&world); + void *result; - switch(event->kind) { - case EcsQueryTableMatch: - /* Creation of new table */ - if (match_table(world, query, event->table)) { - if (query->subqueries) { - notify_subqueries(world, query, event); - } - } - notify = false; - break; - case EcsQueryTableUnmatch: - /* Deletion of table */ - unmatch_table(query, event->table); - break; - case EcsQueryTableRematch: - /* Rematch tables of query */ - rematch_tables(world, query, event->parent_query); - break; - case EcsQueryTableEmpty: - /* Table is empty, deactivate */ - update_table(query, event->table, true); - break; - case EcsQueryTableNonEmpty: - /* Table is non-empty, activate */ - update_table(query, event->table, false); - break; - case EcsQueryOrphan: - ecs_assert(query->flags & EcsQueryIsSubquery, ECS_INTERNAL_ERROR, NULL); - query->flags |= EcsQueryIsOrphaned; - query->parent = NULL; - break; + if (flecs_defer_set( + world, stage, EcsOpMut, entity, id, 0, NULL, &result, is_added)) + { + return result; } - if (notify) { - notify_subqueries(world, query, event); + ecs_entity_info_t info; + result = get_mutable(world, entity, id, &info, is_added); + + /* Store table so we can quickly check if returned pointer is still valid */ + ecs_table_t *table = info.record->table; + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + + /* Keep track of alloc count of table, since even if the entity has not + * moved, other entities could have been added to the table which could + * reallocate arrays. Also store the row, as the entity could have + * reallocated. */ + int32_t alloc_count = table->alloc_count; + int32_t row = info.record->row; + + flecs_defer_flush(world, stage); + + /* Ensure that after flushing, the pointer is still valid. Flushing may + * trigger callbacks, which could do anything with the entity */ + if (table != info.record->table || + alloc_count != info.record->table->alloc_count || + row != info.record->row) + { + if (flecs_get_info(world, entity, &info) && info.table) { + result = get_component(world, info.table, info.row, id); + } else { + /* A trigger has removed the component we just added. This is not + * allowed, an application should always be able to assume that + * get_mut returns a valid pointer. */ + ecs_assert(false, ECS_INVALID_OPERATION, NULL); + } } + + return result; } -static -void query_order_by( +void* ecs_emplace_id( ecs_world_t *world, - ecs_query_t *query, - ecs_entity_t order_by_component, - ecs_order_by_action_t order_by) + ecs_entity_t entity, + ecs_id_t id) { - ecs_assert(query != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!(query->flags & EcsQueryIsOrphaned), ECS_INVALID_PARAMETER, NULL); - ecs_assert(query->flags & EcsQueryNeedsTables, ECS_INVALID_PARAMETER, NULL); - - query->order_by_component = order_by_component; - query->order_by = order_by; - - ecs_vector_free(query->table_slices); - query->table_slices = NULL; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + ecs_assert(!ecs_has_id(world, entity, id), ECS_INVALID_PARAMETER, NULL); - sort_tables(world, query); + ecs_stage_t *stage = flecs_stage_from_world(&world); + void *result; - if (!query->table_slices) { - build_sorted_tables(query); + if (flecs_defer_set( + world, stage, EcsOpMut, entity, id, 0, NULL, &result, NULL)) + { + return result; } -} -static -void query_group_by( - ecs_query_t *query, - ecs_entity_t sort_component, - ecs_group_by_action_t group_by) -{ - /* Cannot change grouping once a query has been created */ - ecs_assert(query->group_by_id == 0, ECS_INVALID_OPERATION, NULL); - ecs_assert(query->group_by == 0, ECS_INVALID_OPERATION, NULL); + ecs_entity_info_t info = {0}; + flecs_get_info(world, entity, &info); - query->group_by_id = sort_component; - query->group_by = group_by; - query->groups = ecs_map_new(ecs_query_table_list_t, 16); -} + add_id_w_info(world, entity, &info, id, false /* Add without ctor */); + void *ptr = get_component(world, info.table, info.row, id); -/* -- Public API -- */ + flecs_defer_flush(world, stage); -ecs_query_t* ecs_query_init( + return ptr; +} + +void ecs_modified_id( ecs_world_t *world, - const ecs_query_desc_t *desc) + ecs_entity_t entity, + ecs_id_t id) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); - - ecs_query_t *result = flecs_sparse_add(world->queries, ecs_query_t); - ecs_poly_init(result, ecs_query_t); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - result->id = flecs_sparse_last_id(world->queries); + ecs_stage_t *stage = flecs_stage_from_world(&world); - if (ecs_filter_init(world, &result->filter, &desc->filter)) { - flecs_sparse_remove(world->queries, result->id); - return NULL; + if (flecs_defer_modified(world, stage, entity, id)) { + return; } - result->world = world; - result->system = desc->system; - result->prev_match_count = -1; + /* If the entity does not have the component, calling ecs_modified is + * invalid. The assert needs to happen after the defer statement, as the + * entity may not have the component when this function is called while + * operations are being deferred. */ + ecs_assert(ecs_has_id(world, entity, id), + ECS_INVALID_PARAMETER, NULL); - ecs_table_cache_init( - &result->cache, ecs_query_table_t, result, remove_table); + ecs_entity_info_t info = {0}; + if (flecs_get_info(world, entity, &info)) { + ecs_ids_t ids = { .array = &id, .count = 1 }; + flecs_notify_on_set(world, info.table, info.row, 1, &ids, true); + } - process_signature(world, result); + flecs_table_mark_dirty(world, info.table, id); + + flecs_defer_flush(world, stage); +} - /* Group before matching so we won't have to move tables around later */ - int32_t cascade_by = result->cascade_by; - if (cascade_by) { - query_group_by(result, result->filter.terms[cascade_by - 1].id, - group_by_cascade); - result->group_by_ctx = &result->filter.terms[cascade_by - 1]; - } +static +ecs_entity_t assign_ptr_w_id( + ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id, + size_t size, + void *ptr, + bool is_move, + bool notify) +{ + ecs_stage_t *stage = flecs_stage_from_world(&world); - if (desc->group_by) { - /* Can't have a cascade term and group by at the same time, as cascade - * uses the group_by mechanism */ - ecs_assert(!result->cascade_by, ECS_INVALID_PARAMETER, NULL); - query_group_by(result, desc->group_by_id, desc->group_by); - result->group_by_ctx = desc->group_by_ctx; - result->group_by_ctx_free = desc->group_by_ctx_free; + if (!entity) { + entity = ecs_new_id(world); + ecs_entity_t scope = stage->scope; + if (scope) { + ecs_add_pair(world, entity, EcsChildOf, scope); + } } - if (desc->parent != NULL) { - result->flags |= EcsQueryIsSubquery; + if (flecs_defer_set(world, stage, EcsOpSet, entity, id, + flecs_from_size_t(size), ptr, NULL, NULL)) + { + return entity; } - /* If a system is specified, ensure that if there are any subjects in the - * filter that refer to the system, the component is added */ - if (desc->system) { - int32_t t, term_count = result->filter.term_count; - ecs_term_t *terms = result->filter.terms; - - for (t = 0; t < term_count; t ++) { - ecs_term_t *term = &terms[t]; - if (term->subj.entity == desc->system) { - ecs_add_id(world, desc->system, term->id); - } - } - } + ecs_entity_info_t info; - ecs_dbg_1("query #[green]%s#[reset] created with expression #[red]%s", - query_name(world, result), result->filter.expr); + void *dst = get_mutable(world, entity, id, &info, NULL); - ecs_log_push(); + /* This can no longer happen since we defer operations */ + ecs_assert(dst != NULL, ECS_INTERNAL_ERROR, NULL); - if (!desc->parent) { - if (result->flags & EcsQueryNeedsTables) { - match_tables(world, result); + if (ptr) { + ecs_entity_t real_id = ecs_get_typeid(world, id); + const ecs_type_info_t *cdata = get_c_info(world, real_id); + if (cdata) { + if (is_move) { + ecs_move_t move = cdata->lifecycle.move; + if (move) { + move(world, real_id, &entity, &entity, dst, ptr, size, 1, + cdata->lifecycle.ctx); + } else { + ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); + } + } else { + ecs_copy_t copy = cdata->lifecycle.copy; + if (copy) { + copy(world, real_id, &entity, &entity, dst, ptr, size, 1, + cdata->lifecycle.ctx); + } else { + ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); + } + } } else { - /* Add stub table that resolves references (if any) so everything is - * preprocessed when the query is evaluated. */ - add_table(world, result, NULL); + ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); } } else { - add_subquery(world, desc->parent, result); - result->parent = desc->parent; + memset(dst, 0, size); } - if (desc->order_by) { - query_order_by( - world, result, desc->order_by_component, desc->order_by); + flecs_table_mark_dirty(world, info.table, id); + + if (notify) { + ecs_ids_t ids = { .array = &id, .count = 1 }; + flecs_notify_on_set(world, info.table, info.row, 1, &ids, true); } - result->constraints_satisfied = satisfy_constraints(world, &result->filter); + flecs_defer_flush(world, stage); - ecs_log_pop(); + return entity; +} - return result; +ecs_entity_t ecs_set_id( + ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id, + size_t size, + const void *ptr) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(!entity || ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + + /* Safe to cast away const: function won't modify if move arg is false */ + return assign_ptr_w_id( + world, entity, id, size, (void*)ptr, false, true); } -void ecs_query_fini( - ecs_query_t *query) +ecs_entity_t ecs_get_case( + const ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_t sw_id) { - ecs_poly_assert(query, ecs_query_t); - ecs_world_t *world = query->world; ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, sw_id), ECS_INVALID_PARAMETER, NULL); - if (query->group_by_ctx_free) { - if (query->group_by_ctx) { - query->group_by_ctx_free(query->group_by_ctx); - } + world = ecs_get_world(world); + + ecs_entity_info_t info; + ecs_table_t *table; + if (!flecs_get_info(world, entity, &info) || !(table = info.table)) { + return 0; } - if ((query->flags & EcsQueryIsSubquery) && - !(query->flags & EcsQueryIsOrphaned)) - { - remove_subquery(query->parent, query); + sw_id = sw_id | ECS_SWITCH; + + ecs_type_t type = table->type; + int32_t index = ecs_type_index_of(type, 0, sw_id); + if (index == -1) { + return 0; } - notify_subqueries(world, query, &(ecs_query_event_t){ - .kind = EcsQueryOrphan - }); + index -= table->sw_column_offset; + ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_vector_each(query->cache.empty_tables, ecs_query_table_t, table, { - if (!(query->flags & EcsQueryIsSubquery)) { - flecs_table_notify(world, table->hdr.table, &(ecs_table_event_t){ - .kind = EcsTableQueryUnmatch, - .query = query - }); - } - }); + /* Data cannot be NULl, since entity is stored in the table */ + ecs_assert(info.data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_switch_t *sw = info.data->sw_columns[index].data; + return flecs_switch_get(sw, info.row); +} - ecs_vector_each(query->cache.tables, ecs_query_table_t, table, { - if (!(query->flags & EcsQueryIsSubquery)) { - flecs_table_notify(world, table->hdr.table, &(ecs_table_event_t){ - .kind = EcsTableQueryUnmatch, - .query = query - }); - } - }); +void ecs_enable_component_w_id( + ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id, + bool enable) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); + + ecs_stage_t *stage = flecs_stage_from_world(&world); + + if (flecs_defer_enable( + world, stage, entity, id, enable)) + { + return; + } else { + /* Operations invoked by enable/disable should not be deferred */ + stage->defer --; + } + + ecs_entity_info_t info; + flecs_get_info(world, entity, &info); + + ecs_entity_t bs_id = (id & ECS_COMPONENT_MASK) | ECS_DISABLED; + + ecs_table_t *table = info.table; + int32_t index = -1; + if (table) { + index = ecs_type_index_of(table->type, 0, bs_id); + } - ecs_table_cache_fini(&query->cache); - ecs_map_free(query->groups); + if (index == -1) { + ecs_add_id(world, entity, bs_id); + ecs_enable_component_w_id(world, entity, id, enable); + return; + } - ecs_vector_free(query->subqueries); - ecs_vector_free(query->table_slices); - ecs_filter_fini(&query->filter); + index -= table->bs_column_offset; + ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_poly_fini(query, ecs_query_t); - - /* Remove query from storage */ - flecs_sparse_remove(world->queries, query->id); -} + /* Data cannot be NULl, since entity is stored in the table */ + ecs_assert(info.data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_bitset_t *bs = &info.data->bs_columns[index].data; + ecs_assert(bs != NULL, ECS_INTERNAL_ERROR, NULL); -const ecs_filter_t* ecs_query_get_filter( - ecs_query_t *query) -{ - ecs_poly_assert(query, ecs_query_t); - return &query->filter; + flecs_bitset_set(bs, info.row, enable); } -/* Create query iterator */ -ecs_iter_t ecs_query_iter_page( - const ecs_world_t *stage, - ecs_query_t *query, - int32_t offset, - int32_t limit) +bool ecs_is_component_enabled_w_id( + const ecs_world_t *world, + ecs_entity_t entity, + ecs_id_t id) { - ecs_poly_assert(query, ecs_query_t); - ecs_assert(!(query->flags & EcsQueryIsOrphaned), ECS_INVALID_PARAMETER, NULL); - - ecs_world_t *world = (ecs_world_t*)ecs_get_world(stage); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - sort_tables(world, query); + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - if (!world->is_readonly && query->flags & EcsQueryHasRefs) { - flecs_eval_component_monitors(world); + ecs_entity_info_t info; + ecs_table_t *table; + if (!flecs_get_info(world, entity, &info) || !(table = info.table)) { + return false; } - tables_reset_dirty(query); + ecs_entity_t bs_id = (id & ECS_COMPONENT_MASK) | ECS_DISABLED; - int32_t table_count; - if (query->table_slices) { - table_count = ecs_vector_count(query->table_slices); - } else { - table_count = ecs_vector_count(query->cache.tables); + ecs_type_t type = table->type; + int32_t index = ecs_type_index_of(type, 0, bs_id); + if (index == -1) { + /* If table does not have DISABLED column for component, component is + * always enabled, if the entity has it */ + return ecs_has_id(world, entity, id); } - ecs_query_iter_t it = { - .query = query, - .node = query->list.first, - .page_iter = { - .offset = offset, - .limit = limit, - .remaining = limit - } - }; + index -= table->bs_column_offset; + ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); - if (query->order_by && query->list.count) { - it.node = ecs_vector_first(query->table_slices, ecs_query_table_node_t); - } + /* Data cannot be NULl, since entity is stored in the table */ + ecs_assert(info.data != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_bitset_t *bs = &info.data->bs_columns[index].data; - return (ecs_iter_t){ - .real_world = world, - .world = (ecs_world_t*)stage, - .terms = query->filter.terms, - .term_count = query->filter.term_count_actual, - .table_count = table_count, - .iter.query = it, - .next = ecs_query_next - }; + return flecs_bitset_get(bs, info.row); } -ecs_iter_t ecs_query_iter( +bool ecs_has_id( const ecs_world_t *world, - ecs_query_t *query) + ecs_entity_t entity, + ecs_id_t id) { - ecs_poly_assert(query, ecs_query_t); - return ecs_query_iter_page(world, query, 0, 0); -} + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + ecs_assert(id != 0, ECS_INVALID_PARAMETER, NULL); -static -int ecs_page_iter_next( - ecs_page_iter_t *it, - ecs_page_cursor_t *cur) -{ - int32_t offset = it->offset; - int32_t limit = it->limit; - if (!(offset || limit)) { - return cur->count == 0; + if (!id) { + return true; } - int32_t count = cur->count; - int32_t remaining = it->remaining; + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - if (offset) { - if (offset > count) { - /* No entities to iterate in current table */ - it->offset -= count; - return 1; - } else { - cur->first += offset; - count = cur->count -= offset; - it->offset = 0; + if (ECS_HAS_ROLE(id, CASE)) { + ecs_entity_info_t info; + ecs_table_t *table; + if (!flecs_get_info(world, entity, &info) || !(table = info.table)) { + return false; } - } - if (remaining) { - if (remaining > count) { - it->remaining -= count; - } else { - count = cur->count = remaining; - it->remaining = 0; + int32_t index = flecs_table_switch_from_case(world, table, id); + ecs_assert(index < table->sw_column_count, ECS_INTERNAL_ERROR, NULL); + + ecs_data_t *data = info.data; + ecs_switch_t *sw = data->sw_columns[index].data; + ecs_entity_t value = flecs_switch_get(sw, info.row); + + return value == (id & ECS_COMPONENT_MASK); + } else { + ecs_table_t *table = ecs_get_table(world, entity); + if (!table) { + return false; } - } else if (limit) { - /* Limit hit: no more entities left to iterate */ - return -1; - } - return count == 0; + return ecs_type_match( + world, table, table->type, 0, id, EcsIsA, 0, 0, NULL, NULL) != -1; + } } -static -int find_smallest_column( - ecs_table_t *table, - ecs_query_table_match_t *table_data, - ecs_vector_t *sparse_columns) +ecs_entity_t ecs_get_object( + const ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_t rel, + int32_t index) { - flecs_sparse_column_t *sparse_column_array = - ecs_vector_first(sparse_columns, flecs_sparse_column_t); - int32_t i, count = ecs_vector_count(sparse_columns); - int32_t min = INT_MAX, index = 0; - - for (i = 0; i < count; i ++) { - /* The array with sparse queries for the matched table */ - flecs_sparse_column_t *sparse_column = &sparse_column_array[i]; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(rel != 0, ECS_INVALID_PARAMETER, NULL); - /* Pointer to the switch column struct of the table */ - ecs_sw_column_t *sc = sparse_column->sw_column; + world = ecs_get_world(world); - /* If the sparse column pointer hadn't been retrieved yet, do it now */ - if (!sc) { - /* Get the table column index from the signature column index */ - int32_t table_column_index = table_data->columns[ - sparse_column->signature_column_index]; + if (!entity) { + return 0; + } - /* Translate the table column index to switch column index */ - table_column_index -= table->sw_column_offset; - ecs_assert(table_column_index >= 1, ECS_INTERNAL_ERROR, NULL); + ecs_table_t *table = ecs_get_table(world, entity); + if (!table) { + return 0; + } - /* Get the sparse column */ - ecs_data_t *data = &table->storage; - sc = sparse_column->sw_column = - &data->sw_columns[table_column_index - 1]; - } + ecs_id_t wc = ecs_pair(rel, EcsWildcard); + ecs_table_record_t *tr = flecs_get_table_record(world, table, wc); + if (!tr) { + return 0; + } - /* Find the smallest column */ - ecs_switch_t *sw = sc->data; - int32_t case_count = flecs_switch_case_count(sw, sparse_column->sw_case); - if (case_count < min) { - min = case_count; - index = i + 1; - } + if (index >= tr->count) { + return 0; } - return index; + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); + return ecs_pair_object(world, ids[tr->column + index]); } -static -int sparse_column_next( - ecs_table_t *table, - ecs_query_table_match_t *matched_table, - ecs_vector_t *sparse_columns, - ecs_query_iter_t *iter, - ecs_page_cursor_t *cur) +ecs_entity_t ecs_get_object_for_id( + const ecs_world_t *world, + ecs_entity_t entity, + ecs_entity_t rel, + ecs_id_t id) { - bool first_iteration = false; - int32_t sparse_smallest; - - if (!(sparse_smallest = iter->sparse_smallest)) { - sparse_smallest = iter->sparse_smallest = find_smallest_column( - table, matched_table, sparse_columns); - first_iteration = true; - } - - sparse_smallest -= 1; - - flecs_sparse_column_t *columns = ecs_vector_first( - sparse_columns, flecs_sparse_column_t); - flecs_sparse_column_t *column = &columns[sparse_smallest]; - ecs_switch_t *sw, *sw_smallest = column->sw_column->data; - ecs_entity_t case_smallest = column->sw_case; + ecs_table_t *table = ecs_get_table(world, entity); + ecs_entity_t subject = 0; - /* Find next entity to iterate in sparse column */ - int32_t first; - if (first_iteration) { - first = flecs_switch_first(sw_smallest, case_smallest); + if (rel) { + int32_t column = ecs_type_match( + world, table, table->type, 0, id, rel, 0, 0, &subject, NULL); + if (column == -1) { + return 0; + } } else { - first = flecs_switch_next(sw_smallest, iter->sparse_first); - } - - if (first == -1) { - goto done; - } + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); + int32_t i, count = ecs_vector_count(table->type); - /* Check if entity matches with other sparse columns, if any */ - int32_t i, count = ecs_vector_count(sparse_columns); - do { for (i = 0; i < count; i ++) { - if (i == sparse_smallest) { - /* Already validated this one */ - continue; + ecs_id_t ent = ids[i]; + if (ent & ECS_ROLE_MASK) { + /* Skip ids with pairs, roles since 0 was provided for rel */ + break; } - column = &columns[i]; - sw = column->sw_column->data; - - if (flecs_switch_get(sw, first) != column->sw_case) { - first = flecs_switch_next(sw_smallest, first); - if (first == -1) { - goto done; - } + if (ecs_has_id(world, ent, id)) { + subject = ent; + break; } } - } while (i != count); - - cur->first = iter->sparse_first = first; - cur->count = 1; - - return 0; -done: - /* Iterated all elements in the sparse list, we should move to the - * next matched table. */ - iter->sparse_smallest = 0; - iter->sparse_first = 0; + } - return -1; + if (subject == 0) { + return entity; + } else { + return subject; + } } -#define BS_MAX ((uint64_t)0xFFFFFFFFFFFFFFFF) - -static -int bitset_column_next( - ecs_table_t *table, - ecs_vector_t *bitset_columns, - ecs_query_iter_t *iter, - ecs_page_cursor_t *cur) +const char* ecs_get_name( + const ecs_world_t *world, + ecs_entity_t entity) { - /* Precomputed single-bit test */ - static const uint64_t bitmask[64] = { - (uint64_t)1 << 0, (uint64_t)1 << 1, (uint64_t)1 << 2, (uint64_t)1 << 3, - (uint64_t)1 << 4, (uint64_t)1 << 5, (uint64_t)1 << 6, (uint64_t)1 << 7, - (uint64_t)1 << 8, (uint64_t)1 << 9, (uint64_t)1 << 10, (uint64_t)1 << 11, - (uint64_t)1 << 12, (uint64_t)1 << 13, (uint64_t)1 << 14, (uint64_t)1 << 15, - (uint64_t)1 << 16, (uint64_t)1 << 17, (uint64_t)1 << 18, (uint64_t)1 << 19, - (uint64_t)1 << 20, (uint64_t)1 << 21, (uint64_t)1 << 22, (uint64_t)1 << 23, - (uint64_t)1 << 24, (uint64_t)1 << 25, (uint64_t)1 << 26, (uint64_t)1 << 27, - (uint64_t)1 << 28, (uint64_t)1 << 29, (uint64_t)1 << 30, (uint64_t)1 << 31, - (uint64_t)1 << 32, (uint64_t)1 << 33, (uint64_t)1 << 34, (uint64_t)1 << 35, - (uint64_t)1 << 36, (uint64_t)1 << 37, (uint64_t)1 << 38, (uint64_t)1 << 39, - (uint64_t)1 << 40, (uint64_t)1 << 41, (uint64_t)1 << 42, (uint64_t)1 << 43, - (uint64_t)1 << 44, (uint64_t)1 << 45, (uint64_t)1 << 46, (uint64_t)1 << 47, - (uint64_t)1 << 48, (uint64_t)1 << 49, (uint64_t)1 << 50, (uint64_t)1 << 51, - (uint64_t)1 << 52, (uint64_t)1 << 53, (uint64_t)1 << 54, (uint64_t)1 << 55, - (uint64_t)1 << 56, (uint64_t)1 << 57, (uint64_t)1 << 58, (uint64_t)1 << 59, - (uint64_t)1 << 60, (uint64_t)1 << 61, (uint64_t)1 << 62, (uint64_t)1 << 63 - }; - - /* Precomputed test to verify if remainder of block is set (or not) */ - static const uint64_t bitmask_remain[64] = { - BS_MAX, BS_MAX - (BS_MAX >> 63), BS_MAX - (BS_MAX >> 62), - BS_MAX - (BS_MAX >> 61), BS_MAX - (BS_MAX >> 60), BS_MAX - (BS_MAX >> 59), - BS_MAX - (BS_MAX >> 58), BS_MAX - (BS_MAX >> 57), BS_MAX - (BS_MAX >> 56), - BS_MAX - (BS_MAX >> 55), BS_MAX - (BS_MAX >> 54), BS_MAX - (BS_MAX >> 53), - BS_MAX - (BS_MAX >> 52), BS_MAX - (BS_MAX >> 51), BS_MAX - (BS_MAX >> 50), - BS_MAX - (BS_MAX >> 49), BS_MAX - (BS_MAX >> 48), BS_MAX - (BS_MAX >> 47), - BS_MAX - (BS_MAX >> 46), BS_MAX - (BS_MAX >> 45), BS_MAX - (BS_MAX >> 44), - BS_MAX - (BS_MAX >> 43), BS_MAX - (BS_MAX >> 42), BS_MAX - (BS_MAX >> 41), - BS_MAX - (BS_MAX >> 40), BS_MAX - (BS_MAX >> 39), BS_MAX - (BS_MAX >> 38), - BS_MAX - (BS_MAX >> 37), BS_MAX - (BS_MAX >> 36), BS_MAX - (BS_MAX >> 35), - BS_MAX - (BS_MAX >> 34), BS_MAX - (BS_MAX >> 33), BS_MAX - (BS_MAX >> 32), - BS_MAX - (BS_MAX >> 31), BS_MAX - (BS_MAX >> 30), BS_MAX - (BS_MAX >> 29), - BS_MAX - (BS_MAX >> 28), BS_MAX - (BS_MAX >> 27), BS_MAX - (BS_MAX >> 26), - BS_MAX - (BS_MAX >> 25), BS_MAX - (BS_MAX >> 24), BS_MAX - (BS_MAX >> 23), - BS_MAX - (BS_MAX >> 22), BS_MAX - (BS_MAX >> 21), BS_MAX - (BS_MAX >> 20), - BS_MAX - (BS_MAX >> 19), BS_MAX - (BS_MAX >> 18), BS_MAX - (BS_MAX >> 17), - BS_MAX - (BS_MAX >> 16), BS_MAX - (BS_MAX >> 15), BS_MAX - (BS_MAX >> 14), - BS_MAX - (BS_MAX >> 13), BS_MAX - (BS_MAX >> 12), BS_MAX - (BS_MAX >> 11), - BS_MAX - (BS_MAX >> 10), BS_MAX - (BS_MAX >> 9), BS_MAX - (BS_MAX >> 8), - BS_MAX - (BS_MAX >> 7), BS_MAX - (BS_MAX >> 6), BS_MAX - (BS_MAX >> 5), - BS_MAX - (BS_MAX >> 4), BS_MAX - (BS_MAX >> 3), BS_MAX - (BS_MAX >> 2), - BS_MAX - (BS_MAX >> 1) - }; - - int32_t i, count = ecs_vector_count(bitset_columns); - flecs_bitset_column_t *columns = ecs_vector_first( - bitset_columns, flecs_bitset_column_t); - int32_t bs_offset = table->bs_column_offset; - - int32_t first = iter->bitset_first; - int32_t last = 0; - - for (i = 0; i < count; i ++) { - flecs_bitset_column_t *column = &columns[i]; - ecs_bs_column_t *bs_column = columns[i].bs_column; - - if (!bs_column) { - int32_t index = column->column_index; - ecs_assert((index - bs_offset >= 0), ECS_INTERNAL_ERROR, NULL); - bs_column = &table->storage.bs_columns[index - bs_offset]; - columns[i].bs_column = bs_column; - } - - ecs_bitset_t *bs = &bs_column->data; - int32_t bs_elem_count = bs->count; - int32_t bs_block = first >> 6; - int32_t bs_block_count = ((bs_elem_count - 1) >> 6) + 1; - - if (bs_block >= bs_block_count) { - goto done; - } - - uint64_t *data = bs->data; - int32_t bs_start = first & 0x3F; - - /* Step 1: find the first non-empty block */ - uint64_t v = data[bs_block]; - uint64_t remain = bitmask_remain[bs_start]; - while (!(v & remain)) { - /* If no elements are remaining, move to next block */ - if ((++bs_block) >= bs_block_count) { - /* No non-empty blocks left */ - goto done; - } - - bs_start = 0; - remain = BS_MAX; /* Test the full block */ - v = data[bs_block]; - } + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - /* Step 2: find the first non-empty element in the block */ - while (!(v & bitmask[bs_start])) { - bs_start ++; + const EcsIdentifier *ptr = ecs_get_pair( + world, entity, EcsIdentifier, EcsName); - /* Block was not empty, so bs_start must be smaller than 64 */ - ecs_assert(bs_start < 64, ECS_INTERNAL_ERROR, NULL); - } - - /* Step 3: Find number of contiguous enabled elements after start */ - int32_t bs_end = bs_start, bs_block_end = bs_block; - - remain = bitmask_remain[bs_end]; - while ((v & remain) == remain) { - bs_end = 0; - bs_block_end ++; + if (ptr) { + return ptr->value; + } else { + return NULL; + } +} - if (bs_block_end == bs_block_count) { - break; - } +const char* ecs_get_symbol( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); - v = data[bs_block_end]; - remain = BS_MAX; /* Test the full block */ - } + const EcsIdentifier *ptr = ecs_get_pair( + world, entity, EcsIdentifier, EcsSymbol); - /* Step 4: find remainder of enabled elements in current block */ - if (bs_block_end != bs_block_count) { - while ((v & bitmask[bs_end])) { - bs_end ++; - } - } + if (ptr) { + return ptr->value; + } else { + return NULL; + } +} - /* Block was not 100% occupied, so bs_start must be smaller than 64 */ - ecs_assert(bs_end < 64, ECS_INTERNAL_ERROR, NULL); +ecs_entity_t ecs_set_name( + ecs_world_t *world, + ecs_entity_t entity, + const char *name) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - /* Step 5: translate to element start/end and make sure that each column - * range is a subset of the previous one. */ - first = bs_block * 64 + bs_start; - int32_t cur_last = bs_block_end * 64 + bs_end; - - /* No enabled elements found in table */ - if (first == cur_last) { - goto done; - } + if (!entity) { + entity = ecs_new_id(world); + } + + ecs_set_pair(world, entity, EcsIdentifier, EcsName, {.value = (char*)name}); - /* If multiple bitsets are evaluated, make sure each subsequent range - * is equal or a subset of the previous range */ - if (i) { - /* If the first element of a subsequent bitset is larger than the - * previous last value, start over. */ - if (first >= last) { - i = -1; - continue; - } + return entity; +} - /* Make sure the last element of the range doesn't exceed the last - * element of the previous range. */ - if (cur_last > last) { - cur_last = last; - } - } - - last = cur_last; - int32_t elem_count = last - first; +ecs_entity_t ecs_set_symbol( + ecs_world_t *world, + ecs_entity_t entity, + const char *name) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - /* Make sure last element doesn't exceed total number of elements in - * the table */ - if (elem_count > bs_elem_count) { - elem_count = bs_elem_count; - } - - cur->first = first; - cur->count = elem_count; - iter->bitset_first = first; + if (!entity) { + entity = ecs_new_id(world); } - /* Keep track of last processed element for iteration */ - iter->bitset_first = last; + ecs_set_pair(world, entity, EcsIdentifier, EcsSymbol, { + .value = (char*)name + }); - return 0; -done: - return -1; + return entity; } -static -void mark_columns_dirty( - ecs_query_t *query, - ecs_query_table_match_t *table_data) +ecs_id_t ecs_make_pair( + ecs_entity_t relation, + ecs_entity_t object) { - ecs_table_t *table = table_data->table; + return ecs_pair(relation, object); +} - if (table && table->dirty_state) { - ecs_term_t *terms = query->filter.terms; - int32_t c = 0, i, count = query->filter.term_count; - for (i = 0; i < count; i ++) { - ecs_term_t *term = &terms[i]; - ecs_term_id_t *subj = &term->subj; +bool ecs_is_valid( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - if (term->inout != EcsIn && (term->inout != EcsInOutDefault || - (subj->entity == EcsThis && subj->set.mask == EcsSelf))) - { - int32_t index = table_data->columns[c]; - if (index > 0) { - int32_t storage_index = ecs_table_type_to_storage_index( - table, index - 1); - if (storage_index >= 0) { - table->dirty_state[storage_index + 1] ++; - } - } - } + /* 0 is not a valid entity id */ + if (!entity) { + return false; + } - if (terms[i].oper == EcsOr) { - do { - i ++; - } while ((i < count) && terms[i].oper == EcsOr); - } + /* Entities should not contain data in dead zone bits */ + if (entity & ~0xFF00FFFFFFFFFFFF) { + return false; + } + + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - c ++; - } + /* When checking roles and/or pairs, the generation count may have been + * stripped away. Just test if the entity is 0 or not. */ + if (ECS_HAS_ROLE(entity, PAIR)) { + ecs_entity_t lo = ECS_PAIR_OBJECT(entity); + ecs_entity_t hi = ECS_PAIR_RELATION(entity); + return lo != 0 && hi != 0; + } else + if (entity & ECS_ROLE) { + return ecs_entity_t_lo(entity) != 0; } + + /* An id may not yet exist in the world which does not mean it cannot be + * used as an entity identifier. An example is when a hard-coded entity id + * is used. However, if the entity id does exist in the world, it must be + * alive. */ + return !ecs_exists(world, entity) || ecs_is_alive(world, entity); } -/* Return next table */ -bool ecs_query_next( - ecs_iter_t *it) +ecs_id_t ecs_strip_generation( + ecs_entity_t e) { - ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(it->next == ecs_query_next, ECS_INVALID_PARAMETER, NULL); + /* If this is not a pair, erase the generation bits */ + if (!(e & ECS_ROLE_MASK)) { + e &= ~ECS_GENERATION_MASK; + } - ecs_query_iter_t *iter = &it->iter.query; - ecs_page_iter_t *piter = &iter->page_iter; - ecs_query_t *query = iter->query; - ecs_world_t *world = query->world; - (void)world; + return e; +} - it->is_valid = true; +bool ecs_is_alive( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); - ecs_poly_assert(world, ecs_world_t); + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - if (!query->constraints_satisfied) { - goto done; - } + return ecs_eis_is_alive(world, entity); +} + +ecs_entity_t ecs_get_alive( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_page_cursor_t cur; - int32_t prev_count = it->total_count; + if (!entity) { + return 0; + } - ecs_query_table_node_t *node, *next; - for (node = iter->node; node != NULL; node = next) { - ecs_query_table_match_t *match = node->match; - ecs_table_t *table = match->table; + if (ecs_is_alive(world, entity)) { + return entity; + } - next = node->next; + /* Make sure id does not have generation. This guards against accidentally + * "upcasting" a not alive identifier to a alive one. */ + ecs_assert((uint32_t)entity == entity, ECS_INVALID_PARAMETER, NULL); - if (table) { - cur.first = node->offset; - cur.count = node->count; - if (!cur.count) { - cur.count = ecs_table_count(table); + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - /* List should never contain empty tables */ - ecs_assert(cur.count != 0, ECS_INTERNAL_ERROR, NULL); - } + ecs_entity_t current = ecs_eis_get_current(world, entity); + if (!current || !ecs_is_alive(world, current)) { + return 0; + } - ecs_vector_t *bitset_columns = match->bitset_columns; - ecs_vector_t *sparse_columns = match->sparse_columns; + return current; +} - if (bitset_columns) { - if (bitset_column_next(table, bitset_columns, iter, - &cur) == -1) - { - /* No more enabled components for table */ - continue; - } else { - next = node; - } - } +void ecs_ensure( + ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); - if (sparse_columns) { - if (sparse_column_next(table, match, - sparse_columns, iter, &cur) == -1) - { - /* No more elements in sparse column */ - continue; - } else { - next = node; - } - } + if (ecs_eis_is_alive(world, entity)) { + /* Nothing to be done, already alive */ + return; + } - int ret = ecs_page_iter_next(piter, &cur); - if (ret < 0) { - goto done; - } else if (ret > 0) { - continue; - } + /* Ensure id exists. The underlying datastructure will verify that the + * generation count matches the provided one. */ + ecs_eis_ensure(world, entity); +} - ecs_entity_t *entity_buffer = ecs_vector_first( - table->storage.entities, ecs_entity_t); - it->entities = &entity_buffer[cur.first]; - it->offset = cur.first; - it->count = cur.count; - it->total_count = cur.count; - } +bool ecs_exists( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); - it->table = match->table; - if (it->table) { - it->type = it->table->type; - } + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - it->ids = match->ids; - it->columns = match->columns; - it->subjects = match->subjects; - it->sizes = match->sizes; - it->references = match->references; - it->frame_offset += prev_count; + return ecs_eis_exists(world, entity); +} - flecs_iter_init(it); +ecs_table_t* ecs_get_table( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, entity), ECS_INVALID_PARAMETER, NULL); + + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - flecs_iter_populate_data(world, it, it->ptrs, NULL); + ecs_record_t *record = ecs_eis_get(world, entity); + ecs_table_t *table; + if (record && (table = record->table)) { + return table; + } + + return NULL; +} - if (query->flags & EcsQueryHasOutColumns) { - if (table) { - mark_columns_dirty(query, match); - } - } +ecs_table_t* ecs_get_storage_table( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_table_t *table = ecs_get_table(world, entity); + if (table) { + return table->storage_table; + } - iter->node = next; + return NULL; +} - goto yield; +ecs_type_t ecs_get_type( + const ecs_world_t *world, + ecs_entity_t entity) +{ + ecs_table_t *table = ecs_get_table(world, entity); + if (table) { + return table->type; } -done: - flecs_iter_fini(it); - return false; - -yield: - return true; + return NULL; } -bool ecs_query_next_worker( - ecs_iter_t *it, - int32_t current, - int32_t total) +ecs_id_t ecs_get_typeid( + const ecs_world_t *world, + ecs_id_t id) { - int32_t per_worker, first, prev_offset = it->offset; - ecs_world_t *world = it->world; - - do { - if (!ecs_query_next(it)) { - return false; - } + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_is_valid(world, id), ECS_INVALID_PARAMETER, NULL); - int32_t count = it->count; - per_worker = count / total; - first = per_worker * current; + if (ECS_HAS_ROLE(id, PAIR)) { + /* Make sure we're not working with a stage */ + world = ecs_get_world(world); - count -= per_worker * total; + ecs_entity_t rel = ecs_get_alive(world, ECS_PAIR_RELATION(id)); - if (count) { - if (current < count) { - per_worker ++; - first += current; - } else { - first += count; - } + /* If relation is marked as a tag, it never has data. Return relation */ + if (ecs_has_id(world, rel, EcsTag)) { + return 0; } - if (!per_worker && !(it->iter.query.query->flags & EcsQueryNeedsTables)) { - if (current == 0) { - flecs_iter_populate_data(world, it, it->ptrs, NULL); - return true; - } else { - return false; + const EcsComponent *ptr = ecs_get(world, rel, EcsComponent); + if (ptr && ptr->size != 0) { + return rel; + } else { + ecs_entity_t obj = ecs_get_alive(world, ECS_PAIR_OBJECT(id)); + ptr = ecs_get(world, obj, EcsComponent); + + if (ptr && ptr->size != 0) { + return obj; } - } - } while (!per_worker); - it->frame_offset -= prev_offset; - it->count = per_worker; - it->offset += first; - it->entities = &it->entities[first]; - it->frame_offset += first; + /* Neither relation nor object have data */ + return 0; + } - flecs_iter_populate_data(world, it, it->ptrs, NULL); + } else if (id & ECS_ROLE_MASK) { + return 0; + } else { + const EcsComponent *ptr = ecs_get(world, id, EcsComponent); + if (!ptr || !ptr->size) { + return 0; + } + } - return true; + return id; } -bool ecs_query_changed( - ecs_query_t *query) +int32_t ecs_count_id( + const ecs_world_t *world, + ecs_entity_t id) { - ecs_poly_assert(query, ecs_query_t); - ecs_assert(!(query->flags & EcsQueryIsOrphaned), ECS_INVALID_PARAMETER, NULL); - return tables_dirty(query); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + + if (!id) { + return 0; + } + + int32_t count = 0; + + ecs_iter_t it = ecs_term_iter(world, &(ecs_term_t) { .id = id }); + while (ecs_term_next(&it)) { + count += it.count; + } + + return count; } -bool ecs_query_orphaned( - ecs_query_t *query) +bool ecs_defer_begin( + ecs_world_t *world) { - ecs_poly_assert(query, ecs_query_t); - return query->flags & EcsQueryIsOrphaned; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_stage_t *stage = flecs_stage_from_world(&world); + return flecs_defer_none(world, stage); } -static -uint64_t ids_hash(const void *ptr) { - const ecs_ids_t *type = ptr; - ecs_id_t *ids = type->array; - int32_t count = type->count; - uint64_t hash = flecs_hash(ids, count * ECS_SIZEOF(ecs_id_t)); - return hash; +bool ecs_defer_end( + ecs_world_t *world) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_stage_t *stage = flecs_stage_from_world(&world); + return flecs_defer_flush(world, stage); } static -int ids_compare(const void *ptr_1, const void *ptr_2) { - const ecs_ids_t *type_1 = ptr_1; - const ecs_ids_t *type_2 = ptr_2; - - int32_t count_1 = type_1->count; - int32_t count_2 = type_2->count; - - if (count_1 != count_2) { - return (count_1 > count_2) - (count_1 < count_2); +size_t append_to_str( + char **buffer, + const char *str, + size_t bytes_left, + size_t *required) +{ + char *ptr = NULL; + if (buffer) { + ptr = *buffer; } - const ecs_id_t *ids_1 = type_1->array; - const ecs_id_t *ids_2 = type_2->array; + size_t len = strlen(str); + size_t to_write; + if (bytes_left < len) { + to_write = bytes_left; + bytes_left = 0; + } else { + to_write = len; + bytes_left -= len; + } - int32_t i; - for (i = 0; i < count_1; i ++) { - ecs_id_t id_1 = ids_1[i]; - ecs_id_t id_2 = ids_2[i]; + if (to_write && ptr) { + ecs_os_memcpy(ptr, str, to_write); + } - if (id_1 != id_2) { - return (id_1 > id_2) - (id_1 < id_2); - } + (*required) += len; + + if (buffer) { + (*buffer) += to_write; } - return 0; + return bytes_left; } -ecs_hashmap_t flecs_table_hashmap_new(void) { - return flecs_hashmap_new(ecs_ids_t, ecs_table_t*, ids_hash, ids_compare); +const char* ecs_role_str( + ecs_entity_t entity) +{ + if (ECS_HAS_ROLE(entity, PAIR)) { + return "PAIR"; + } else + if (ECS_HAS_ROLE(entity, DISABLED)) { + return "DISABLED"; + } else + if (ECS_HAS_ROLE(entity, XOR)) { + return "XOR"; + } else + if (ECS_HAS_ROLE(entity, OR)) { + return "OR"; + } else + if (ECS_HAS_ROLE(entity, AND)) { + return "AND"; + } else + if (ECS_HAS_ROLE(entity, NOT)) { + return "NOT"; + } else + if (ECS_HAS_ROLE(entity, SWITCH)) { + return "SWITCH"; + } else + if (ECS_HAS_ROLE(entity, CASE)) { + return "CASE"; + } else + if (ECS_HAS_ROLE(entity, OVERRIDE)) { + return "OVERRIDE"; + } else { + return "UNKNOWN"; + } } -const EcsComponent* flecs_component_from_id( +size_t ecs_id_str_w_buf( const ecs_world_t *world, - ecs_entity_t e) + ecs_id_t id, + char *buffer, + size_t buffer_len) { - ecs_entity_t pair = 0; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - /* If this is a pair, get the pair component from the identifier */ - if (ECS_HAS_ROLE(e, PAIR)) { - pair = e; - e = ecs_get_alive(world, ECS_PAIR_RELATION(e)); + world = ecs_get_world(world); - if (ecs_has_id(world, e, EcsTag)) { - return NULL; - } + char *ptr = buffer; + char **pptr = NULL; + if (ptr) { + pptr = &ptr; } - if (e & ECS_ROLE_MASK) { - return NULL; + size_t bytes_left = buffer_len - 1, required = 0; + if (id & ECS_ROLE_MASK && !ECS_HAS_ROLE(id, PAIR)) { + const char *role = ecs_role_str(id); + bytes_left = append_to_str(pptr, role, bytes_left, &required); + bytes_left = append_to_str(pptr, "|", bytes_left, &required); } - const EcsComponent *component = ecs_get(world, e, EcsComponent); - if ((!component || !component->size) && pair) { - /* If this is a pair column and the pair is not a component, use - * the component type of the component the pair is applied to. */ - e = ECS_PAIR_OBJECT(pair); + if (ECS_HAS_ROLE(id, PAIR)) { + ecs_entity_t rel = ECS_PAIR_RELATION(id); + ecs_entity_t obj = ECS_PAIR_OBJECT(id); + + ecs_entity_t e; + if ((e = ecs_get_alive(world, rel))) { + rel = e; + } + if ((e = ecs_get_alive(world, obj))) { + obj = e; + } - /* Because generations are not stored in the pair, get the currently - * alive id */ - e = ecs_get_alive(world, e); + char *rel_str = ecs_get_fullpath(world, rel); + bytes_left = append_to_str(pptr, "(", bytes_left, &required); + bytes_left = append_to_str(pptr, rel_str, bytes_left, &required); + ecs_os_free(rel_str); + bytes_left = append_to_str(pptr, ",", bytes_left, &required); - /* If a pair is used with a not alive id, the pair is not valid */ - ecs_assert(e != 0, ECS_INTERNAL_ERROR, NULL); + char *obj_str = ecs_get_fullpath(world, obj); + bytes_left = append_to_str(pptr, obj_str, bytes_left, &required); + ecs_os_free(obj_str); - component = ecs_get(world, e, EcsComponent); + append_to_str(pptr, ")", bytes_left, &required); + } else { + ecs_entity_t e = id & ECS_COMPONENT_MASK; + char *path = ecs_get_fullpath(world, e); + append_to_str(pptr, path, bytes_left, &required); + ecs_os_free(path); } - return component; + if (ptr) { + ptr[0] = '\0'; + } + + return required; +} + +char* ecs_id_str( + const ecs_world_t *world, + ecs_id_t id) +{ + size_t size = ecs_id_str_w_buf(world, id, NULL, 0); + char *result = ecs_os_malloc(flecs_from_size_t(size) + 1); + ecs_id_str_w_buf(world, id, result, size + 1); + return result; } -/* Ensure the ids used in the columns exist */ static -int32_t ensure_columns( +void flush_bulk_new( ecs_world_t *world, - ecs_table_t *table) + ecs_defer_op_t *op) { - int32_t count = 0; - ecs_vector_each(table->type, ecs_entity_t, c_ptr, { - ecs_entity_t component = *c_ptr; + ecs_entity_t *entities = op->is._n.entities; - if (ECS_HAS_ROLE(component, PAIR)) { - ecs_entity_t rel = ECS_PAIR_RELATION(component); - ecs_entity_t obj = ECS_PAIR_OBJECT(component); - ecs_ensure(world, rel); - ecs_ensure(world, obj); - } else if (component & ECS_ROLE_MASK) { - ecs_entity_t e = ECS_PAIR_OBJECT(component); - ecs_ensure(world, e); - } else { - ecs_ensure(world, component); + if (op->id) { + int i, count = op->is._n.count; + for (i = 0; i < count; i ++) { + add_id(world, entities[i], op->id); } - }); + } - return count; + ecs_os_free(entities); } static -ecs_type_t ids_to_type( - ecs_ids_t *entities) +void free_value( + ecs_world_t *world, + ecs_entity_t *entities, + ecs_id_t id, + void *value, + int32_t count) { - if (entities->count) { - ecs_vector_t *result = NULL; - ecs_vector_set_count(&result, ecs_entity_t, entities->count); - ecs_entity_t *array = ecs_vector_first(result, ecs_entity_t); - ecs_os_memcpy_n(array, entities->array, ecs_entity_t, entities->count); - return result; - } else { - return NULL; + ecs_entity_t real_id = ecs_get_typeid(world, id); + const ecs_type_info_t *info = flecs_get_c_info(world, real_id); + ecs_xtor_t dtor; + + if (info && (dtor = info->lifecycle.dtor)) { + ecs_size_t size = info->size; + void *ptr; + int i; + for (i = 0, ptr = value; i < count; i ++, ptr = ECS_OFFSET(ptr, size)) { + dtor(world, id, &entities[i], ptr, flecs_to_size_t(size), 1, + info->lifecycle.ctx); + } } } static -ecs_edge_t* get_edge( - ecs_graph_edges_t *edges, - ecs_id_t id) +void discard_op( + ecs_world_t *world, + ecs_defer_op_t *op) { - if (id < ECS_HI_COMPONENT_ID) { - if (!edges->lo) { - return NULL; + if (op->kind != EcsOpBulkNew) { + void *value = op->is._1.value; + if (value) { + free_value(world, &op->is._1.entity, op->id, op->is._1.value, 1); + ecs_os_free(value); } - return &edges->lo[id]; } else { - if (!edges->hi) { - return NULL; - } - return ecs_map_get(edges->hi, ecs_edge_t, id); + ecs_os_free(op->is._n.entities); } } -static -ecs_edge_t* ensure_edge( - ecs_graph_edges_t *edges, - ecs_id_t id) +static +bool is_entity_valid( + ecs_world_t *world, + ecs_entity_t e) { - if (id < ECS_HI_COMPONENT_ID) { - if (!edges->lo) { - edges->lo = ecs_os_calloc_n(ecs_edge_t, ECS_HI_COMPONENT_ID); - } - return &edges->lo[id]; - } else { - if (!edges->hi) { - edges->hi = ecs_map_new(ecs_edge_t, 1); - } - return ecs_map_ensure(edges->hi, ecs_edge_t, id); + if (ecs_exists(world, e) && !ecs_is_alive(world, e)) { + return false; } + return true; } static -void init_edges( - ecs_graph_edges_t *edges) +bool remove_invalid( + ecs_world_t *world, + ecs_id_t *id_out) { - edges->lo = NULL; - edges->hi = NULL; -} + ecs_id_t id = *id_out; -static -void init_node( - ecs_graph_node_t *node) -{ - init_edges(&node->add); - init_edges(&node->remove); + if (ECS_HAS_ROLE(id, PAIR)) { + ecs_entity_t rel = ecs_pair_relation(world, id); + if (!rel || !is_entity_valid(world, rel)) { + /* After relation is deleted we can no longer see what its + * delete action was, so pretend this never happened */ + *id_out = 0; + return true; + } else { + ecs_entity_t obj = ecs_pair_object(world, id); + if (!obj || !is_entity_valid(world, obj)) { + /* Check the relation's policy for deleted objects */ + ecs_id_record_t *idr = flecs_get_id_record(world, rel); + if (!idr || (idr->on_delete_object == EcsRemove)) { + *id_out = 0; + return true; + } else { + if (idr->on_delete_object == EcsDelete) { + /* Entity should be deleted, don't bother checking + * other ids */ + return false; + } else if (idr->on_delete_object == EcsThrow) { + /* If policy is throw this object should not have + * been deleted */ + throw_invalid_delete(world, id); + } + } + } + } + + } else { + id &= ECS_COMPONENT_MASK; + if (!is_entity_valid(world, id)) { + /* After relation is deleted we can no longer see what its + * delete action was, so pretend this never happened */ + *id_out = 0; + return true; + } + } + + return true; } -static -void init_flags( +/* Leave safe section. Run all deferred commands. */ +bool flecs_defer_flush( ecs_world_t *world, - ecs_table_t *table) + ecs_stage_t *stage) { - ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); - int32_t count = ecs_vector_count(table->type); - - /* Iterate components to initialize table flags */ - int32_t i; - for (i = 0; i < count; i ++) { - ecs_id_t id = ids[i]; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); - /* 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, like prefabs or - * containers */ - if (id <= EcsLastInternalComponentId) { - table->flags |= EcsTableHasBuiltins; - } + if (!--stage->defer) { + /* Set to NULL. Processing deferred commands can cause additional + * commands to get enqueued (as result of reactive systems). Make sure + * that the original array is not reallocated, as this would complicate + * processing the queue. */ + ecs_vector_t *defer_queue = stage->defer_queue; + stage->defer_queue = NULL; - if (id == EcsModule) { - table->flags |= EcsTableHasBuiltins; - table->flags |= EcsTableHasModule; - } + if (defer_queue) { + ecs_defer_op_t *ops = ecs_vector_first(defer_queue, ecs_defer_op_t); + int32_t i, count = ecs_vector_count(defer_queue); + + for (i = 0; i < count; i ++) { + ecs_defer_op_t *op = &ops[i]; + ecs_entity_t e = op->is._1.entity; + if (op->kind == EcsOpBulkNew) { + e = 0; + } - if (id == EcsPrefab) { - table->flags |= EcsTableIsPrefab; - } + /* If entity is no longer alive, this could be because the queue + * contained both a delete and a subsequent add/remove/set which + * should be ignored. */ + if (e && !ecs_is_alive(world, e) && ecs_eis_exists(world, e)) { + ecs_assert(op->kind != EcsOpNew && op->kind != EcsOpClone, + ECS_INTERNAL_ERROR, NULL); + world->discard_count ++; + discard_op(world, op); + continue; + } - /* If table contains disabled entities, mark it as disabled */ - if (id == EcsDisabled) { - table->flags |= EcsTableIsDisabled; - } + switch(op->kind) { + case EcsOpNew: + case EcsOpAdd: + ecs_assert(op->id != 0, ECS_INTERNAL_ERROR, NULL); + if (remove_invalid(world, &op->id)) { + if (op->id) { + world->add_count ++; + add_id(world, e, op->id); + } + } else { + ecs_delete(world, e); + } + break; + case EcsOpRemove: + remove_id(world, e, op->id); + break; + case EcsOpClone: + ecs_clone(world, e, op->id, op->is._1.clone_value); + break; + case EcsOpSet: + assign_ptr_w_id(world, e, + op->id, flecs_to_size_t(op->is._1.size), + op->is._1.value, true, true); + break; + case EcsOpMut: + assign_ptr_w_id(world, e, + op->id, flecs_to_size_t(op->is._1.size), + op->is._1.value, true, false); + break; + case EcsOpModified: + ecs_modified_id(world, e, op->id); + break; + case EcsOpDelete: { + ecs_delete(world, e); + break; + } + case EcsOpClear: + ecs_clear(world, e); + break; + case EcsOpOnDeleteAction: + on_delete_action(world, op->id, e); + break; + case EcsOpEnable: + ecs_enable_component_w_id(world, e, op->id, true); + break; + case EcsOpDisable: + ecs_enable_component_w_id(world, e, op->id, false); + break; + case EcsOpBulkNew: + flush_bulk_new(world, op); + continue; + } - /* Does table have exclusive or columns */ - if (ECS_HAS_ROLE(id, XOR)) { - table->flags |= EcsTableHasXor; - } + if (op->is._1.value) { + ecs_os_free(op->is._1.value); + } + } - /* Does table have IsA relations */ - if (ECS_HAS_RELATION(id, EcsIsA)) { - table->flags |= EcsTableHasIsA; - } + if (stage->defer_queue) { + ecs_vector_free(stage->defer_queue); + } - /* Does table have switch columns */ - if (ECS_HAS_ROLE(id, SWITCH)) { - table->flags |= EcsTableHasSwitch; + /* Restore defer queue */ + ecs_vector_clear(defer_queue); + stage->defer_queue = defer_queue; } - /* Does table support component disabling */ - if (ECS_HAS_ROLE(id, DISABLED)) { - table->flags |= EcsTableHasDisabled; - } - - /* Does table have ChildOf relations */ - if (ECS_HAS_RELATION(id, EcsChildOf)) { - ecs_entity_t obj = ecs_pair_object(world, id); - 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; - } - } + return true; } + + return false; } -static -void init_table( +/* Delete operations from queue without executing them. */ +bool flecs_defer_purge( ecs_world_t *world, - ecs_table_t *table, - ecs_ids_t *entities) + ecs_stage_t *stage) { - table->type = ids_to_type(entities); - table->c_info = NULL; - table->flags = 0; - table->dirty_state = NULL; - table->alloc_count = 0; - table->lock = 0; + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); - /* Ensure the component ids for the table exist */ - ensure_columns(world, table); + if (!--stage->defer) { + ecs_vector_t *defer_queue = stage->defer_queue; + stage->defer_queue = NULL; - table->queries = NULL; + if (defer_queue) { + ecs_defer_op_t *ops = ecs_vector_first(defer_queue, ecs_defer_op_t); + int32_t i, count = ecs_vector_count(defer_queue); + for (i = 0; i < count; i ++) { + discard_op(world, &ops[i]); + } - init_node(&table->node); - init_flags(world, table); + if (stage->defer_queue) { + ecs_vector_free(stage->defer_queue); + } - flecs_register_table(world, table); - flecs_table_init_data(world, table); + /* Restore defer queue */ + ecs_vector_clear(defer_queue); + stage->defer_queue = defer_queue; + } - /* Register component info flags for all columns */ - flecs_table_notify(world, table, &(ecs_table_event_t){ - .kind = EcsTableComponentInfo - }); + return true; + } + + return false; } + static -ecs_table_t *create_table( - ecs_world_t *world, - ecs_ids_t *entities, - flecs_hashmap_result_t table_elem) +void term_error( + const ecs_world_t *world, + const ecs_term_t *term, + const char *name, + const char *fmt, + ...) { - ecs_table_t *result = flecs_sparse_add(world->store.tables, ecs_table_t); - result->id = flecs_sparse_last_id(world->store.tables); - - ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL); - - init_table(world, result, entities); + va_list args; + va_start(args, fmt); -#ifndef NDEBUG - char *expr = ecs_type_str(world, result->type); - ecs_dbg_1("table #[green][%s]#[normal] created", expr); + char *expr = ecs_term_str(world, term); + ecs_parser_errorv(name, expr, 0, fmt, args); ecs_os_free(expr); -#endif - ecs_log_push(); - - /* Store table in table hashmap */ - *(ecs_table_t**)table_elem.value = result; - - /* Set keyvalue to one that has the same lifecycle as the table */ - ecs_ids_t key = { - .array = ecs_vector_first(result->type, ecs_id_t), - .count = ecs_vector_count(result->type) - }; - *(ecs_ids_t*)table_elem.key = key; - - flecs_notify_queries(world, &(ecs_query_event_t) { - .kind = EcsQueryTableMatch, - .table = result - }); - ecs_log_pop(); + va_end(args); +} - return result; +static +ecs_entity_t term_id_entity( + const ecs_world_t *world, + ecs_term_id_t *term_id) +{ + if (term_id->entity && term_id->entity != EcsThis && + term_id->entity != EcsWildcard) + { + if (!(term_id->entity & ECS_ROLE_MASK)) { + return term_id->entity; + } else { + return 0; + } + } else if (term_id->name) { + if (term_id->var == EcsVarIsEntity || + (term_id->var == EcsVarDefault && + !ecs_identifier_is_var(term_id->name))) + { + return ecs_lookup_fullpath(world, term_id->name); + } else { + return 0; + } + } else { + return 0; + } } static -void add_id_to_ids( - ecs_type_t type, - ecs_entity_t add, - ecs_ids_t *out) +void finalize_default_substitution( + const ecs_world_t *world, + ecs_term_t *terms, + int32_t term_count) { - int32_t count = ecs_vector_count(type); - ecs_id_t *array = ecs_vector_first(type, ecs_id_t); - bool added = false; + int i; + for (i = 0; i < term_count; i ++) { + ecs_term_id_t *pred = &terms[i].pred; + ecs_term_id_t *subj = &terms[i].subj; + ecs_term_id_t *obj = &terms[i].obj; - int32_t i, el = 0; - for (i = 0; i < count; i ++) { - ecs_id_t e = array[i]; + bool pred_transitive = false; + if (pred->set.mask == EcsDefaultSet) { + ecs_entity_t e = term_id_entity(world, pred); + if (e) { + if (ecs_has_id(world, e, EcsFinal)) { + /* Final predicates are never substituted */ + pred->set.mask = EcsSelf; + } else { + pred->set.mask = EcsSelf|EcsSuperSet; + pred->set.relation = EcsIsA; + } + pred_transitive = ecs_has_id(world, e, EcsTransitive); + } + } - if (e >= add && !added) { - if (e != add) { - out->array[el ++] = add; + if (subj->set.mask == EcsDefaultSet) { + /* By default subjects are substituted with their supersets */ + subj->set.mask = EcsSelf|EcsSuperSet; + subj->set.relation = EcsIsA; + } + + if (obj->set.mask == EcsDefaultSet) { + /* By default subjects are substituted with their supersets */ + if (!pred_transitive) { + obj->set.mask = EcsSelf|EcsSuperSet; + obj->set.relation = EcsIsA; + } else { + /* If query is transitive substitute object. If object is fixed + * insert SubSet ("is "). If object is a + * variable, insert SuperSet ("find for ") */ + ecs_entity_t e = term_id_entity(world, obj); + obj->set.relation = EcsIsA; + if (!e) { + obj->set.mask = EcsSelf|EcsSuperSet; + } else { + obj->set.mask = EcsSelf|EcsSubSet; + } } - added = true; } - - out->array[el ++] = e; - ecs_assert(el <= out->count, ECS_INTERNAL_ERROR, NULL); - } - - if (!added) { - out->array[el ++] = add; } - - out->count = el; } static -void remove_id_from_ids( - ecs_type_t type, - ecs_entity_t remove, - ecs_ids_t *out) +int finalize_term_identifier( + const ecs_world_t *world, + ecs_term_t *term, + ecs_term_id_t *identifier, + const char *name) { - int32_t count = ecs_vector_count(type); - ecs_id_t *array = ecs_vector_first(type, ecs_id_t); - - int32_t i, el = 0; - for (i = 0; i < count; i ++) { - ecs_id_t e = array[i]; - if (e != remove) { - out->array[el ++] = e; - ecs_assert(el <= count, ECS_INTERNAL_ERROR, NULL); - } + /* Default set is Self */ + if (identifier->set.mask == EcsDefaultSet) { + identifier->set.mask |= EcsSelf; } - out->count = el; -} + if (identifier->set.mask & EcsParent) { + identifier->set.mask |= EcsSuperSet; + identifier->set.relation = EcsChildOf; + } -int32_t flecs_table_switch_from_case( - const ecs_world_t *world, - const ecs_table_t *table, - ecs_entity_t add) -{ - ecs_type_t type = table->type; - ecs_entity_t *array = ecs_vector_first(type, ecs_entity_t); + /* Default relation for superset/subset is EcsIsA */ + if (identifier->set.mask & (EcsSuperSet|EcsSubSet)) { + if (!identifier->set.relation) { + identifier->set.relation = EcsIsA; + } - int32_t i, count = table->sw_column_count; - ecs_assert(count != 0, ECS_INTERNAL_ERROR, NULL); + if (!(identifier->set.mask & EcsSelf)) { + if (!identifier->set.min_depth) { + identifier->set.min_depth = 1; + } + } + } else { + if (identifier->set.min_depth > 0) { + term_error(world, term, name, + "min depth cannnot be non-zero for Self term"); + return -1; + } + if (identifier->set.max_depth > 1) { + term_error(world, term, name, + "max depth cannnot be larger than 1 for Self term"); + return -1; + } - add = add & ECS_COMPONENT_MASK; + identifier->set.max_depth = 1; + } - ecs_sw_column_t *sw_columns = NULL; + if ((identifier->set.mask != EcsNothing) && + (identifier->set.mask & EcsNothing)) + { + term_error(world, term, name, "invalid Nothing in set mask"); + return -1; + } - if ((sw_columns = table->storage.sw_columns)) { - /* Fast path, we can get the switch type from the column data */ - for (i = 0; i < count; i ++) { - ecs_type_t sw_type = sw_columns[i].type; - if (ecs_type_has_id(world, sw_type, add, true)) { - return i; - } + if (identifier->var == EcsVarDefault) { + const char *var = ecs_identifier_is_var(identifier->name); + if (ecs_identifier_is_var(identifier->name)) { + char *var_id = ecs_os_strdup(var); + ecs_os_free(identifier->name); + identifier->name = var_id; + identifier->var = EcsVarIsVariable; } - } else { - /* Slow path, table is empty, so we'll have to get the switch types by - * actually inspecting the switch type entities. */ - for (i = 0; i < count; i ++) { - ecs_entity_t e = array[i + table->sw_column_offset]; - ecs_assert(ECS_HAS_ROLE(e, SWITCH), ECS_INTERNAL_ERROR, NULL); - e = e & ECS_COMPONENT_MASK; + } - const EcsType *type_ptr = ecs_get(world, e, EcsType); - ecs_assert(type_ptr != NULL, ECS_INTERNAL_ERROR, NULL); + if (identifier->var == EcsVarDefault && identifier->set.mask != EcsNothing){ + identifier->var = EcsVarIsEntity; + } - if (ecs_type_has_id( - world, type_ptr->normalized, add, true)) - { - return i; + if (!identifier->name) { + return 0; + } + + if (identifier->var != EcsVarIsVariable) { + if (ecs_identifier_is_0(identifier->name)) { + identifier->entity = 0; + } else { + ecs_entity_t e = ecs_lookup_symbol(world, identifier->name, true); + if (!e) { + term_error(world, term, name, + "unresolved identifier '%s'", identifier->name); + return -1; } + + identifier->entity = e; } } - /* If a table was not found, this is an invalid switch case */ - ecs_abort(ECS_TYPE_INVALID_CASE, NULL); - - return -1; -} + if ((identifier->set.mask == EcsNothing) && + (identifier->var != EcsVarDefault)) + { + term_error(world, term, name, "Invalid Nothing with entity"); + return -1; + } -static -void ids_append( - ecs_ids_t *ids, - ecs_id_t id) -{ - ids->array = ecs_os_realloc_n(ids->array, ecs_id_t, ids->count + 1); - ids->array[ids->count ++] = id; + return 0; } static -void diff_insert_isa( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_diff_t *base_diff, - ecs_ids_t *append_to, - ecs_ids_t *append_from, - ecs_id_t add) +int finalize_term_identifiers( + const ecs_world_t *world, + ecs_term_t *term, + const char *name) { - ecs_entity_t base = ecs_pair_object(world, add); - ecs_table_t *base_table = ecs_get_table(world, base); - if (!base_table) { - return; + if (finalize_term_identifier(world, term, &term->pred, name)) { + return -1; + } + if (finalize_term_identifier(world, term, &term->subj, name)) { + return -1; + } + if (finalize_term_identifier(world, term, &term->obj, name)) { + return -1; } - ecs_type_t base_type = base_table->type, type = table->type; - ecs_table_t *table_wo_base = base_table; - - /* If the table does not have a component from the base, it should - * trigger an OnSet */ - ecs_id_t *ids = ecs_vector_first(base_type, ecs_id_t); - int32_t j, i, count = ecs_vector_count(base_type); - for (i = 0; i < count; i ++) { - ecs_id_t id = ids[i]; - - if (ECS_HAS_RELATION(id, EcsIsA)) { - /* The base has an IsA relation. Find table without the base, which - * gives us the list of ids the current base inherits and doesn't - * override. This saves us from having to recursively check for each - * base in the hierarchy whether the component is overridden. */ - table_wo_base = flecs_table_traverse_remove( - world, table_wo_base, &id, base_diff); - - /* Because we removed, the ids are stored in un_set vs. on_set */ - for (j = 0; j < append_from->count; j ++) { - ecs_id_t base_id = append_from->array[j]; - /* We still have to make sure the id isn't overridden by the - * current table */ - if (ecs_type_match( - world, table, type, 0, base_id, 0, 0, 0, NULL, NULL) == -1) - { - ids_append(append_to, base_id); - } - } - - continue; - } - - /* Identifiers are not inherited */ - if (ECS_HAS_RELATION(id, ecs_id(EcsIdentifier))) { - continue; - } + if (term->pred.set.mask & EcsNothing) { + term_error(world, term, name, + "invalid Nothing value for predicate set mask"); + return -1; + } - if (!ecs_get_typeid(world, id)) { - continue; - } + if (term->obj.set.mask & EcsNothing) { + term_error(world, term, name, + "invalid Nothing value for object set mask"); + return -1; + } - if (ecs_type_match(world, table, type, 0, id, 0, 0, 0, NULL, NULL) == -1) { - ids_append(append_to, id); - } + if (!(term->subj.set.mask & EcsNothing) && + !term->subj.entity && + term->subj.var == EcsVarIsEntity) + { + term->subj.entity = EcsThis; + } + + if (term->pred.entity == EcsThis) { + term->pred.var = EcsVarIsVariable; + } + if (term->subj.entity == EcsThis) { + term->subj.var = EcsVarIsVariable; + } + if (term->obj.entity == EcsThis) { + term->obj.var = EcsVarIsVariable; } -} -static -void diff_insert_added_isa( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_diff_t *diff, - ecs_id_t id) -{ - ecs_table_diff_t base_diff; - diff_insert_isa(world, table, &base_diff, &diff->on_set, - &base_diff.un_set, id); + return 0; } static -void diff_insert_removed_isa( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_diff_t *diff, - ecs_id_t id) +ecs_entity_t entity_from_identifier( + const ecs_term_id_t *identifier) { - ecs_table_diff_t base_diff; - diff_insert_isa(world, table, &base_diff, &diff->un_set, - &base_diff.un_set, id); + if (identifier->var == EcsVarDefault) { + return 0; + } else if (identifier->var == EcsVarIsEntity) { + return identifier->entity; + } else if (identifier->var == EcsVarIsVariable) { + return EcsWildcard; + } else { + /* This should've been caught earlier */ + ecs_abort(ECS_INTERNAL_ERROR, NULL); + } } static -void diff_insert_added( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_diff_t *diff, - ecs_id_t id) +int finalize_term_id( + const ecs_world_t *world, + ecs_term_t *term, + const char *name) { - diff->added.array[diff->added.count ++] = id; + ecs_entity_t pred = entity_from_identifier(&term->pred); + ecs_entity_t obj = entity_from_identifier(&term->obj); + ecs_id_t role = term->role; - if (ECS_HAS_RELATION(id, EcsIsA)) { - diff_insert_added_isa(world, table, diff, id); - } -} + if (ECS_HAS_ROLE(pred, PAIR)) { + if (obj) { + term_error(world, term, name, + "cannot set term.pred to a pair and term.obj at the same time"); + return -1; + } -static -void diff_insert_removed( - ecs_world_t *world, - ecs_table_t *table, - ecs_table_diff_t *diff, - ecs_id_t id) -{ - diff->removed.array[diff->removed.count ++] = id; + obj = ECS_PAIR_OBJECT(pred); + pred = ECS_PAIR_RELATION(pred); - if (ECS_HAS_RELATION(id, EcsIsA)) { - /* Removing an IsA relation also "removes" all components from the - * instance. Any id from base that's not overridden should be UnSet. */ - diff_insert_removed_isa(world, table, diff, id); - return; + term->pred.entity = pred; + term->obj.entity = obj; + + finalize_term_identifier(world, term, &term->obj, name); } - if (table->flags & EcsTableHasIsA) { - if (!ecs_get_typeid(world, id)) { - /* Do nothing if id is not a component */ - return; + if (!obj && role != ECS_PAIR) { + term->id = pred | role; + } else { + if (role && role != ECS_PAIR) { + term_error(world, term, name, "invalid role for pair"); + return -1; } - /* If next table has a base and component is removed, check if - * the removed component was an override. Removed overrides reexpose the - * base component, thus "changing" the value which requires an OnSet. */ - if (ecs_type_match(world, table, table->type, 0, id, EcsIsA, - 1, 0, NULL, NULL) != -1) - { - ids_append(&diff->on_set, id); - return; - } + term->id = ecs_pair(pred, obj); + term->role = ECS_PAIR; } - if (ecs_get_typeid(world, id) != 0) { - ids_append(&diff->un_set, id); - } + return 0; } static -void compute_table_diff( - ecs_world_t *world, - ecs_table_t *node, - ecs_table_t *next, - ecs_edge_t *edge, - ecs_id_t id) +int populate_from_term_id( + const ecs_world_t *world, + ecs_term_t *term, + const char *name) { - ecs_type_t node_type = node->type; - ecs_type_t next_type = next->type; - - ecs_id_t *ids_node = ecs_vector_first(node_type, ecs_id_t); - ecs_id_t *ids_next = ecs_vector_first(next_type, ecs_id_t); - int32_t i_node = 0, node_count = ecs_vector_count(node_type); - int32_t i_next = 0, next_count = ecs_vector_count(next_type); - int32_t added_count = 0; - int32_t removed_count = 0; - bool trivial_edge = !ECS_HAS_RELATION(id, EcsIsA) && - !(node->flags & EcsTableHasIsA) && !(next->flags & EcsTableHasIsA); - - /* First do a scan to see how big the diff is, so we don't have to realloc - * or alloc more memory than required. */ - for (; i_node < node_count && i_next < next_count; ) { - ecs_id_t id_node = ids_node[i_node]; - ecs_id_t id_next = ids_next[i_next]; - - bool added = id_next < id_node; - bool removed = id_node < id_next; - - trivial_edge &= !added || id_next == id; - trivial_edge &= !removed || id_node == id; + ecs_entity_t pred = 0; + ecs_entity_t obj = 0; + ecs_id_t role = term->id & ECS_ROLE_MASK; - added_count += added; - removed_count += removed; + if (!role && term->role) { + role = term->role; + term->id |= role; + } - i_node += id_node <= id_next; - i_next += id_next <= id_node; + if (term->role && term->role != role) { + term_error(world, term, name, "mismatch between term.id & term.role"); + return -1; } - added_count += next_count - i_next; - removed_count += node_count - i_node; + term->role = role; - trivial_edge &= (added_count + removed_count) <= 1; + if (ECS_HAS_ROLE(term->id, PAIR)) { + pred = ECS_PAIR_RELATION(term->id); + obj = ECS_PAIR_OBJECT(term->id); - if (trivial_edge) { - /* If edge is trivial there's no need to create a diff element for it. - * Encode in the id whether the id is a tag or not, so that wen can - * still tell whether an UnSet handler should be called or not. */ - edge->diff_index = -1 * (ecs_get_typeid(world, id) == 0); - return; + if (!pred) { + term_error(world, term, name, "missing predicate in term.id pair"); + return -1; + } + if (!obj) { + if (pred != EcsChildOf) { + term_error(world, term, name, "missing object in term.id pair"); + return -1; + } + } + } else { + pred = term->id & ECS_COMPONENT_MASK; + if (!pred) { + term_error(world, term, name, "missing predicate in term.id"); + return -1; + } } - ecs_table_diff_t *diff = ecs_vector_add(&node->node.diffs, ecs_table_diff_t); - ecs_os_memset_t(diff, 0, ecs_table_diff_t); - edge->diff_index = ecs_vector_count(node->node.diffs); - if (added_count) { - diff->added.array = ecs_os_malloc_n(ecs_id_t, added_count); - diff->added.count = 0; - diff->added.size = added_count; + ecs_entity_t term_pred = entity_from_identifier(&term->pred); + if (term_pred) { + if (term_pred != pred) { + term_error(world, term, name, + "mismatch between term.id and term.pred"); + return -1; + } + } else { + term->pred.entity = pred; + if (finalize_term_identifier(world, term, &term->pred, name)) { + return -1; + } } - if (removed_count) { - diff->removed.array = ecs_os_malloc_n(ecs_id_t, removed_count); - diff->removed.count = 0; - diff->removed.size = removed_count; + + ecs_entity_t term_obj = entity_from_identifier(&term->obj); + if (term_obj) { + if (term_obj != obj) { + term_error(world, term, name, + "mismatch between term.id and term.obj"); + return -1; + } + } else { + term->obj.entity = obj; + if (finalize_term_identifier(world, term, &term->obj, name)) { + return -1; + } } - for (i_node = 0, i_next = 0; i_node < node_count && i_next < next_count; ) { - ecs_id_t id_node = ids_node[i_node]; - ecs_id_t id_next = ids_next[i_next]; + return 0; +} - if (id_next < id_node) { - diff_insert_added(world, node, diff, id_next); - } else if (id_node < id_next) { - diff_insert_removed(world, next, diff, id_node); - } +static +int verify_term_consistency( + const ecs_world_t *world, + const ecs_term_t *term, + const char *name) +{ + ecs_entity_t pred = entity_from_identifier(&term->pred); + ecs_entity_t obj = entity_from_identifier(&term->obj); + ecs_id_t role = term->role; + ecs_id_t id = term->id; - i_node += id_node <= id_next; - i_next += id_next <= id_node; + if (obj && (!role || (role != ECS_PAIR))) { + term_error(world, term, name, + "invalid role for term with pair (expected ECS_PAIR)"); + return -1; } - for (; i_next < next_count; i_next ++) { - diff_insert_added(world, node, diff, ids_next[i_next]); - } - for (; i_node < node_count; i_node ++) { - diff_insert_removed(world, next, diff, ids_node[i_next]); + if (!pred) { + term_error(world, term, name, "missing predicate for term"); + return -1; } - ecs_assert(diff->added.count == added_count, ECS_INTERNAL_ERROR, NULL); - ecs_assert(diff->removed.count == removed_count, ECS_INTERNAL_ERROR, NULL); -} + if (role != (id & ECS_ROLE_MASK)) { + term_error(world, term, name, "mismatch between term.role & term.id"); + return -1; + } -static -ecs_table_t* find_or_create_table_with_id( - ecs_world_t *world, - ecs_table_t *node, - ecs_entity_t id) -{ - /* If table has one or more switches and this is a case, return self */ - if (ECS_HAS_ROLE(id, CASE)) { - ecs_assert((node->flags & EcsTableHasSwitch) != 0, - ECS_TYPE_INVALID_CASE, NULL); - return node; - } else { - ecs_type_t type = node->type; - int32_t count = ecs_vector_count(type); + if (obj && !(ECS_HAS_ROLE(id, PAIR))) { + term_error(world, term, name, "term has object but id is not a pair"); + return -1; + } - ecs_ids_t ids = { - .array = ecs_os_alloca_n(ecs_id_t, count + 1), - .count = count + 1 - }; + if (ECS_HAS_ROLE(id, PAIR)) { + if (id != ecs_pair(pred, obj)) { + char *id_str = ecs_id_str(world, ecs_pair(pred, obj)); + term_error(world, term, name, + "term id does not match pred/obj (%s)", id_str); + ecs_os_free(id_str); + return -1; + } + } else if (pred != (id & ECS_COMPONENT_MASK)) { + char *pred_str = ecs_get_fullpath(world, pred); + term_error(world, term, name, "term id does not match pred '%s'", + pred_str); + ecs_os_free(pred_str); + return -1; + } - add_id_to_ids(type, id, &ids); + return 0; +} - return flecs_table_find_or_create(world, &ids); - } +bool ecs_identifier_is_0( + const char *id) +{ + return id[0] == '0' && !id[1]; } -static -ecs_table_t* find_or_create_table_without_id( - ecs_world_t *world, - ecs_table_t *node, - ecs_entity_t id) +const char* ecs_identifier_is_var( + const char *id) { - /* If table has one or more switches and this is a case, return self */ - if (ECS_HAS_ROLE(id, CASE)) { - ecs_assert((node->flags & EcsTableHasSwitch) != 0, - ECS_TYPE_INVALID_CASE, NULL); - return node; - } else { - ecs_type_t type = node->type; - int32_t count = ecs_vector_count(type); + if (!id) { + return NULL; + } - ecs_ids_t ids = { - .array = ecs_os_alloca_n(ecs_id_t, count), - .count = count - }; + /* Variable identifiers cannot start with a number */ + if (isdigit(id[0])) { + return NULL; + } - remove_id_from_ids(type, id, &ids); + /* Identifiers that start with _ are variables */ + if (id[0] == '_') { + return &id[1]; + } - return flecs_table_find_or_create(world, &ids);; + /* Identifiers that have a single uppercase character are variables */ + if (ecs_os_strlen(id) == 1 && isupper(id[0])) { + return id; } + + return NULL; } -static -ecs_table_t* find_or_create_table_with_isa( - ecs_world_t *world, - ecs_table_t *node, - ecs_entity_t base) +bool ecs_id_match( + ecs_id_t id, + ecs_id_t pattern) { - ecs_type_t base_type = ecs_get_type(world, base); - ecs_id_t *ids = ecs_vector_first(base_type, ecs_id_t); - int32_t i, count = ecs_vector_count(base_type); + if (id == pattern) { + return true; + } - /* Start from back, as roles have high ids */ - for (i = count - 1; i >= 0; i --) { - ecs_id_t id = ids[i]; - if (!(id & ECS_ROLE_MASK)) { /* early out if we found everything */ - break; + if (ECS_HAS_ROLE(pattern, PAIR)) { + if (!ECS_HAS_ROLE(id, PAIR)) { + return false; } - if (ECS_HAS_RELATION(id, EcsIsA)) { - ecs_entity_t base_of_base = ecs_pair_object(world, id); - node = find_or_create_table_with_isa(world, node, base_of_base); + ecs_entity_t id_rel = ECS_PAIR_RELATION(id); + ecs_entity_t id_obj = ECS_PAIR_OBJECT(id); + ecs_entity_t pattern_rel = ECS_PAIR_RELATION(pattern); + ecs_entity_t pattern_obj = ECS_PAIR_OBJECT(pattern); + + ecs_assert(id_rel != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(id_obj != 0, ECS_INVALID_PARAMETER, NULL); + + ecs_assert(pattern_rel != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(pattern_obj != 0, ECS_INVALID_PARAMETER, NULL); + + if (pattern_rel == EcsWildcard) { + if (pattern_obj == EcsWildcard || pattern_obj == id_obj) { + return true; + } + } else if (pattern_obj == EcsWildcard) { + if (pattern_rel == id_rel) { + return true; + } + } + } else { + if ((id & ECS_ROLE_MASK) != (pattern & ECS_ROLE_MASK)) { + return false; } - if (ECS_HAS_ROLE(id, OVERRIDE)) { - /* Override found, add it to table */ - id &= ECS_COMPONENT_MASK; - node = flecs_table_traverse_add(world, node, &id, NULL); + if ((ECS_COMPONENT_MASK & pattern) == EcsWildcard) { + return true; } } - return node; + return false; } -static -ecs_table_t* find_or_create_table_without( - ecs_world_t *world, - ecs_table_t *node, - ecs_edge_t *edge, +bool ecs_id_is_pair( ecs_id_t id) { - ecs_table_t *next = find_or_create_table_without_id(world, node, id); + return ECS_HAS_ROLE(id, PAIR); +} - edge->next = next; +bool ecs_id_is_wildcard( + ecs_id_t id) +{ + if (id == EcsWildcard) { + return true; + } else if (ECS_HAS_ROLE(id, PAIR)) { + return ECS_PAIR_RELATION(id) == EcsWildcard || + ECS_PAIR_OBJECT(id) == EcsWildcard; + } - compute_table_diff(world, node, next, edge, id); + return false; +} - if (node != next) { - flecs_register_remove_ref(world, node, id); - } +bool ecs_term_id_is_set( + const ecs_term_id_t *id) +{ + return id->entity != 0 || id->name != NULL; +} - return next; +bool ecs_term_is_initialized( + const ecs_term_t *term) +{ + return term->id != 0 || ecs_term_id_is_set(&term->pred); } -static -ecs_table_t* find_or_create_table_with( - ecs_world_t *world, - ecs_table_t *node, - ecs_edge_t *edge, - ecs_id_t id) +bool ecs_term_is_trivial( + const ecs_term_t *term) { - ecs_table_t *next = find_or_create_table_with_id(world, node, id); + if (term->inout != EcsInOutDefault) { + return false; + } - if (ECS_HAS_ROLE(id, PAIR) && ECS_PAIR_RELATION(id) == EcsIsA) { - ecs_entity_t base = ecs_pair_object(world, id); - next = find_or_create_table_with_isa(world, next, base); + if (term->subj.entity != EcsThis) { + return false; } - edge->next = next; + if (term->subj.set.mask && (term->subj.set.mask != EcsSelf)) { + return false; + } - compute_table_diff(world, node, next, edge, id); + if (term->oper != EcsAnd && term->oper != EcsAndFrom) { + return false; + } - if (node != next) { - flecs_register_add_ref(world, node, id); + if (term->name != NULL) { + return false; } - return next; + return true; } -static -void populate_diff( - ecs_table_t *table, - ecs_edge_t *edge, - ecs_id_t *add_ptr, - ecs_id_t *remove_ptr, - ecs_table_diff_t *out) +int ecs_term_finalize( + const ecs_world_t *world, + const char *name, + ecs_term_t *term) { - if (out) { - int32_t di = edge->diff_index; - if (di > 0) { - *out = ecs_vector_first(table->node.diffs, ecs_table_diff_t)[di - 1]; - } else { - out->on_set.count = 0; - - if (add_ptr) { - out->added.array = add_ptr; - out->added.count = 1; - } else { - out->added.count = 0; - } + if (finalize_term_identifiers(world, term, name)) { + return -1; + } - if (remove_ptr) { - out->removed.array = remove_ptr; - out->removed.count = 1; - if (di == 0) { - out->un_set.array = remove_ptr; - out->un_set.count = 1; - } else { - out->un_set.count = 0; - } - } else { - out->removed.count = 0; - out->un_set.count = 0; - } + if (!term->id) { + if (finalize_term_id(world, term, name)) { + return -1; + } + } else { + if (populate_from_term_id(world, term, name)) { + return -1; } } -} - -ecs_table_t* flecs_table_traverse_remove( - ecs_world_t *world, - ecs_table_t *node, - ecs_id_t *id_ptr, - ecs_table_diff_t *diff) -{ - ecs_poly_assert(world, ecs_world_t); - node = node ? node : &world->store.root; + if (verify_term_consistency(world, term, name)) { + return -1; + } - /* Removing 0 from an entity is not valid */ - ecs_assert(id_ptr != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(id_ptr[0] != 0, ECS_INVALID_PARAMETER, NULL); + return 0; +} - ecs_id_t id = id_ptr[0]; - ecs_edge_t *edge = ensure_edge(&node->node.remove, id); - ecs_table_t *next = edge->next; +ecs_term_t ecs_term_copy( + const ecs_term_t *src) +{ + ecs_term_t dst = *src; + dst.name = ecs_os_strdup(src->name); + dst.pred.name = ecs_os_strdup(src->pred.name); + dst.subj.name = ecs_os_strdup(src->subj.name); + dst.obj.name = ecs_os_strdup(src->obj.name); + return dst; +} - if (!next) { - next = find_or_create_table_without(world, node, edge, id); - ecs_assert(next != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(edge->next != NULL, ECS_INTERNAL_ERROR, NULL); +ecs_term_t ecs_term_move( + ecs_term_t *src) +{ + if (src->move) { + ecs_term_t dst = *src; + src->name = NULL; + src->pred.name = NULL; + src->subj.name = NULL; + src->obj.name = NULL; + return dst; + } else { + return ecs_term_copy(src); } +} - populate_diff(node, edge, NULL, id_ptr, diff); +void ecs_term_fini( + ecs_term_t *term) +{ + ecs_os_free(term->pred.name); + ecs_os_free(term->subj.name); + ecs_os_free(term->obj.name); + ecs_os_free(term->name); - return next; + term->pred.name = NULL; + term->subj.name = NULL; + term->obj.name = NULL; + term->name = NULL; } -ecs_table_t* flecs_table_traverse_add( - ecs_world_t *world, - ecs_table_t *node, - ecs_id_t *id_ptr, - ecs_table_diff_t *diff) +int ecs_filter_finalize( + const ecs_world_t *world, + ecs_filter_t *f) { - ecs_poly_assert(world, ecs_world_t); + int32_t i, term_count = f->term_count, actual_count = 0; + ecs_term_t *terms = f->terms; + bool is_or = false, prev_or = false; - node = node ? node : &world->store.root; + for (i = 0; i < term_count; i ++) { + ecs_term_t *term = &terms[i]; - /* Adding 0 to an entity is not valid */ - ecs_assert(id_ptr != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(id_ptr[0] != 0, ECS_INVALID_PARAMETER, NULL); + if (ecs_term_finalize(world, f->name, term)) { + return -1; + } - ecs_id_t id = id_ptr[0]; - ecs_edge_t *edge = ensure_edge(&node->node.add, id); - ecs_table_t *next = edge->next; + is_or = term->oper == EcsOr; + actual_count += !(is_or && prev_or); + term->index = actual_count - 1; + prev_or = is_or; - if (!next) { - next = find_or_create_table_with(world, node, edge, id); - ecs_assert(next != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(edge->next != NULL, ECS_INTERNAL_ERROR, NULL); + if (term->subj.entity == EcsThis) { + f->match_this = true; + if (term->subj.set.mask != EcsSelf) { + f->match_only_this = false; + } + } else { + f->match_only_this = false; + } + + if (term->id == EcsPrefab) { + f->match_prefab = true; + } + if (term->id == EcsDisabled) { + f->match_disabled = true; + } } - populate_diff(node, edge, id_ptr, NULL, diff); + f->term_count_actual = actual_count; - return next; + return 0; } -static -bool ecs_entity_array_is_ordered( - const ecs_ids_t *entities) +int ecs_filter_init( + const ecs_world_t *stage, + ecs_filter_t *filter_out, + const ecs_filter_desc_t *desc) { - ecs_entity_t prev = 0; - ecs_entity_t *array = entities->array; - int32_t i, count = entities->count; + ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(filter_out != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); - for (i = 0; i < count; i ++) { - if (!array[i] && !prev) { - continue; + const ecs_world_t *world = ecs_get_world(stage); + + int i, term_count = 0; + ecs_term_t *terms = desc->terms_buffer; + const char *name = desc->name; + const char *expr = desc->expr; + + ecs_filter_t f = { + /* Temporarily set the fields to the values provided in desc, until the + * filter has been validated. */ + .name = (char*)name, + .expr = (char*)expr + }; + + if (terms) { + terms = desc->terms_buffer; + term_count = desc->terms_buffer_count; + } else { + terms = (ecs_term_t*)desc->terms; + for (i = 0; i < ECS_TERM_DESC_CACHE_SIZE; i ++) { + if (!ecs_term_is_initialized(&terms[i])) { + break; + } + + term_count ++; } - if (array[i] <= prev) { - return false; + } + + /* Temporarily set array from desc to filter, until the filter has been + * validated. */ + f.terms = terms; + f.term_count = term_count; + + if (expr) { +#ifdef FLECS_PARSER + int32_t buffer_count = 0; + + /* If terms have already been set, copy buffer to allocated one */ + if (terms && term_count) { + terms = ecs_os_memdup(terms, term_count * ECS_SIZEOF(ecs_term_t)); + buffer_count = term_count; + } else { + terms = NULL; } - prev = array[i]; - } - return true; -} + /* Parse expression into array of terms */ + const char *ptr = desc->expr; + ecs_term_t term = {0}; + while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))){ + if (!ecs_term_is_initialized(&term)) { + break; + } + + if (term_count == buffer_count) { + buffer_count = buffer_count ? buffer_count * 2 : 8; + terms = ecs_os_realloc(terms, + buffer_count * ECS_SIZEOF(ecs_term_t)); + } -static -int32_t ecs_entity_array_dedup( - ecs_entity_t *array, - int32_t count) -{ - int32_t j, k; - ecs_entity_t prev = array[0]; + terms[term_count] = term; + term_count ++; - for (k = j = 1; k < count; j ++, k++) { - ecs_entity_t e = array[k]; - if (e == prev) { - k ++; + if (ptr[0] == '\n') { + break; + } } - array[j] = e; - prev = e; + f.terms = terms; + f.term_count = term_count; + + if (!ptr) { + goto error; + } +#else + ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); +#endif } - return count - (k - j); -} + /* If default substitution is enabled, replace DefaultSet with SuperSet */ + if (desc->substitute_default) { + finalize_default_substitution(world, terms, term_count); + } else { + for (i = 0; i < term_count; i ++) { + if (terms[i].subj.set.mask == EcsDefaultSet) { + terms[i].subj.set.mask = EcsSelf; + } + } + } -static -ecs_table_t* find_or_create( - ecs_world_t *world, - const ecs_ids_t *ids) -{ - ecs_poly_assert(world, ecs_world_t); + /* Ensure all fields are consistent and properly filled out */ + if (ecs_filter_finalize(world, &f)) { + goto error; + } - /* Make sure array is ordered and does not contain duplicates */ - int32_t type_count = ids->count; - ecs_id_t *ordered = NULL; + *filter_out = f; - if (!type_count) { - return &world->store.root; - } + /* Copy term resources. */ + if (term_count) { + if (!filter_out->expr) { + if (term_count < ECS_TERM_CACHE_SIZE) { + filter_out->terms = filter_out->term_cache; + filter_out->term_cache_used = true; + } else { + filter_out->terms = ecs_os_malloc_n(ecs_term_t, term_count); + } + } - if (!ecs_entity_array_is_ordered(ids)) { - ecs_size_t size = ECS_SIZEOF(ecs_entity_t) * type_count; - ordered = ecs_os_alloca(size); - ecs_os_memcpy(ordered, ids->array, size); - qsort(ordered, (size_t)type_count, sizeof(ecs_entity_t), - flecs_entity_compare_qsort); - type_count = ecs_entity_array_dedup(ordered, type_count); + for (i = 0; i < term_count; i ++) { + filter_out->terms[i] = ecs_term_move(&terms[i]); + } } else { - ordered = ids->array; + filter_out->terms = NULL; } - ecs_ids_t ordered_ids = { - .array = ordered, - .count = type_count - }; + filter_out->name = ecs_os_strdup(desc->name); + filter_out->expr = ecs_os_strdup(desc->expr); - ecs_table_t *table; - flecs_hashmap_result_t elem = flecs_hashmap_ensure( - world->store.table_map, &ordered_ids, ecs_table_t*); - if ((table = *(ecs_table_t**)elem.value)) { - return table; + ecs_assert(!filter_out->term_cache_used || + filter_out->terms == filter_out->term_cache, + ECS_INTERNAL_ERROR, NULL); + + return 0; +error: + /* NULL members that point to non-owned resources */ + if (!f.expr) { + f.terms = NULL; } - /* If we get here, table needs to be created which is only allowed when the - * application is not currently in progress */ - ecs_assert(!world->is_readonly, ECS_INTERNAL_ERROR, NULL); + f.name = NULL; + f.expr = NULL; - /* If we get here, the table has not been found, so create it. */ - ecs_table_t *result = create_table(world, &ordered_ids, elem); - - ecs_assert(ordered_ids.count == ecs_vector_count(result->type), - ECS_INTERNAL_ERROR, NULL); + ecs_filter_fini(&f); - return result; + return -1; } -ecs_table_t* flecs_table_find_or_create( - ecs_world_t *world, - const ecs_ids_t *components) +void ecs_filter_copy( + ecs_filter_t *dst, + const ecs_filter_t *src) { - ecs_poly_assert(world, ecs_world_t); - return find_or_create(world, components); -} + if (src) { + *dst = *src; -void flecs_init_root_table( - ecs_world_t *world) -{ - ecs_poly_assert(world, ecs_world_t); + int32_t term_count = src->term_count; - ecs_ids_t entities = { - .array = NULL, - .count = 0 - }; + if (src->term_cache_used) { + dst->terms = dst->term_cache; + } else { + dst->terms = ecs_os_memdup_n(src->terms, ecs_term_t, term_count); + } - init_table(world, &world->store.root, &entities); + int i; + for (i = 0; i < term_count; i ++) { + dst->terms[i] = ecs_term_copy(&src->terms[i]); + } + } else { + ecs_os_memset_t(dst, 0, ecs_filter_t); + } } -void flecs_table_clear_edges( - ecs_world_t *world, - ecs_table_t *table) +void ecs_filter_move( + ecs_filter_t *dst, + ecs_filter_t *src) { - (void)world; - ecs_poly_assert(world, ecs_world_t); - - int32_t i; - ecs_graph_node_t *node = &table->node; + if (src) { + *dst = *src; - ecs_os_free(node->add.lo); - ecs_os_free(node->remove.lo); - ecs_map_free(node->add.hi); - ecs_map_free(node->remove.hi); - node->add.lo = NULL; - node->remove.lo = NULL; - node->add.hi = NULL; - node->remove.hi = NULL; + if (src->term_cache_used) { + dst->terms = dst->term_cache; + } - int32_t count = ecs_vector_count(node->diffs); - ecs_table_diff_t *diffs = ecs_vector_first(node->diffs, ecs_table_diff_t); - for (i = 0; i < count; i ++) { - ecs_table_diff_t *diff = &diffs[i]; - ecs_os_free(diff->added.array); - ecs_os_free(diff->removed.array); - ecs_os_free(diff->on_set.array); - ecs_os_free(diff->un_set.array); + src->terms = NULL; + src->term_count = 0; + } else { + ecs_os_memset_t(dst, 0, ecs_filter_t); } - - ecs_vector_free(node->diffs); - node->diffs = NULL; } -void flecs_table_clear_add_edge( - ecs_table_t *table, - ecs_id_t id) +void ecs_filter_fini( + ecs_filter_t *filter) { - ecs_edge_t *edge = get_edge(&table->node.add, id); - if (edge) { - edge->next = NULL; - edge->diff_index = 0; - } -} + if (filter->terms) { + int i, count = filter->term_count; + for (i = 0; i < count; i ++) { + ecs_term_fini(&filter->terms[i]); + } -void flecs_table_clear_remove_edge( - ecs_table_t *table, - ecs_id_t id) -{ - ecs_edge_t *edge = get_edge(&table->node.remove, id); - if (edge) { - edge->next = NULL; - edge->diff_index = 0; + if (!filter->term_cache_used) { + ecs_os_free(filter->terms); + } } -} -/* Public convenience functions for traversing table graph */ -ecs_table_t* ecs_table_add_id( - ecs_world_t *world, - ecs_table_t *table, - ecs_id_t id) -{ - return flecs_table_traverse_add(world, table, &id, NULL); + ecs_os_free(filter->name); + ecs_os_free(filter->expr); } -ecs_table_t* ecs_table_remove_id( - ecs_world_t *world, - ecs_table_t *table, - ecs_id_t id) +static +void filter_str_add_id( + const ecs_world_t *world, + ecs_strbuf_t *buf, + const ecs_term_id_t *id, + bool is_subject) { - return flecs_table_traverse_remove(world, table, &id, NULL); -} - -#define INIT_CACHE(it, f, term_count)\ - if (!it->f && term_count) {\ - if (term_count < ECS_TERM_CACHE_SIZE) {\ - it->f = it->cache.f;\ - it->cache.f##_alloc = false;\ - } else {\ - it->f = ecs_os_calloc(ECS_SIZEOF(*(it->f)) * term_count);\ - it->cache.f##_alloc = true;\ - }\ - } + if (id->name) { + ecs_strbuf_appendstr(buf, id->name); + } else if (id->entity) { + bool id_added = false; + if (!is_subject || id->entity != EcsThis) { + char *path = ecs_get_fullpath(world, id->entity); + ecs_strbuf_appendstr(buf, path); + ecs_os_free(path); + id_added = true; + } -#define FINI_CACHE(it, f)\ - if (it->f) {\ - if (it->cache.f##_alloc) {\ - ecs_os_free((void*)it->f);\ - }\ - } + if (id->set.mask != EcsSelf) { + if (id_added) { + ecs_strbuf_list_push(buf, ":", "|"); + } else { + ecs_strbuf_list_push(buf, "", "|"); + } + if (id->set.mask & EcsSelf) { + ecs_strbuf_list_appendstr(buf, "self"); + } + if (id->set.mask & EcsSuperSet) { + ecs_strbuf_list_appendstr(buf, "superset"); + } + if (id->set.mask & EcsSubSet) { + ecs_strbuf_list_appendstr(buf, "subset"); + } -void flecs_iter_init( - ecs_iter_t *it) -{ - INIT_CACHE(it, ids, it->term_count); - INIT_CACHE(it, columns, it->term_count); - INIT_CACHE(it, subjects, it->term_count); - INIT_CACHE(it, sizes, it->term_count); - INIT_CACHE(it, ptrs, it->term_count); - INIT_CACHE(it, match_indices, it->term_count); + if (id->set.relation != EcsIsA) { + ecs_strbuf_list_push(buf, "(", ""); - it->is_valid = true; -} + char *rel_path = ecs_get_fullpath(world, id->set.relation); + ecs_strbuf_appendstr(buf, rel_path); + ecs_os_free(rel_path); -void flecs_iter_fini( - ecs_iter_t *it) -{ - ecs_assert(it->is_valid == true, ECS_INVALID_PARAMETER, NULL); - it->is_valid = false; + ecs_strbuf_list_pop(buf, ")"); + } - FINI_CACHE(it, ids); - FINI_CACHE(it, columns); - FINI_CACHE(it, subjects); - FINI_CACHE(it, sizes); - FINI_CACHE(it, ptrs); - FINI_CACHE(it, match_indices); + ecs_strbuf_list_pop(buf, ""); + } + } else { + ecs_strbuf_appendstr(buf, "0"); + } } static -void flecs_iter_populate_term_data( - ecs_world_t *world, - ecs_iter_t *it, - int32_t t, - int32_t column, - void **ptr_out, - ecs_size_t *size_out) +void term_str_w_strbuf( + const ecs_world_t *world, + const ecs_term_t *term, + ecs_strbuf_t *buf) { - if (!column) { - /* Term has no data. This includes terms that have Not operators. */ - goto no_data; - } - - ecs_table_t *table; - ecs_vector_t *vec; - ecs_size_t size; - ecs_size_t align; - int32_t row; - - if (column < 0) { - /* Data is not from This */ - if (it->references) { - /* Iterator provides cached references for non-This terms */ - ecs_ref_t *ref = &it->references[-column - 1]; - if (ptr_out) ptr_out[0] = (void*)ecs_get_ref_w_id( - world, ref, ref->entity, ref->component); - - /* If cached references were provided, the code that populated - * the iterator also had a chance to cache sizes, so size array - * should already have been assigned. This saves us from having - * to do additional lookups to find the component size. */ - ecs_assert(size_out == NULL, ECS_INTERNAL_ERROR, NULL); - return; - } else { - ecs_entity_t subj = it->subjects[t]; - ecs_assert(subj != 0, ECS_INTERNAL_ERROR, NULL); - - /* Don't use ecs_get_id directly. Instead, go directly to the - * storage so that we can get both the pointer and size */ - ecs_record_t *r = ecs_eis_get(world, subj); - ecs_assert(r != NULL && r->table != NULL, ECS_INTERNAL_ERROR, NULL); + const ecs_term_id_t *subj = &term->subj; + const ecs_term_id_t *obj = &term->obj; - bool is_monitored; - row = flecs_record_to_row(r->row, &is_monitored); - table = r->table; + bool pred_set = ecs_term_id_is_set(&term->pred); + bool subj_set = ecs_term_id_is_set(subj); + bool obj_set = ecs_term_id_is_set(obj); - ecs_id_t id = it->ids[t]; - ecs_table_t *s_table = table->storage_table; - ecs_table_record_t *tr; + if (term->role && term->role != ECS_PAIR) { + ecs_strbuf_appendstr(buf, ecs_role_str(term->role)); + ecs_strbuf_appendstr(buf, " "); + } - if (!s_table || !(tr = flecs_get_table_record(world, s_table, id))){ - /* The entity has no components or the id is not a component */ - - ecs_id_t term_id = it->terms[t].id; - if (ECS_HAS_ROLE(term_id, SWITCH) || ECS_HAS_ROLE(term_id, CASE)) { - /* Edge case: if this is a switch. Find switch column in - * actual table, as its not in the storage table */ - tr = flecs_get_table_record(world, table, id); - ecs_assert(tr != NULL, ECS_INTERNAL_ERROR, NULL); - column = tr->column; - goto has_switch; - } else { - goto no_data; - } - } + if (term->oper == EcsNot) { + ecs_strbuf_appendstr(buf, "!"); + } else if (term->oper == EcsOptional) { + ecs_strbuf_appendstr(buf, "?"); + } - /* We now have row and column, so we can get the storage for the id - * which gives us the pointer and size */ - column = tr->column; - ecs_column_t *s = &table->storage.columns[column]; - size = s->size; - align = s->alignment; - vec = s->data; - /* Fallthrough to has_data */ + if (!subj_set) { + filter_str_add_id(world, buf, &term->pred, false); + ecs_strbuf_appendstr(buf, "()"); + } else if (subj_set && subj->entity == EcsThis && subj->set.mask == EcsSelf) + { + if (term->id) { + char *str = ecs_id_str(world, term->id); + ecs_strbuf_appendstr(buf, str); + ecs_os_free(str); + } else if (pred_set) { + filter_str_add_id(world, buf, &term->pred, false); } } else { - /* Data is from This, use table from iterator */ - table = it->table; - if (!table) { - goto no_data; - } - - row = it->offset; - - int32_t storage_column = ecs_table_type_to_storage_index( - table, column - 1); - if (storage_column == -1) { - ecs_id_t id = it->terms[t].id; - if (ECS_HAS_ROLE(id, SWITCH) || ECS_HAS_ROLE(id, CASE)) { - goto has_switch; - } - goto no_data; + filter_str_add_id(world, buf, &term->pred, false); + ecs_strbuf_appendstr(buf, "("); + filter_str_add_id(world, buf, &term->subj, true); + if (obj_set) { + ecs_strbuf_appendstr(buf, ","); + filter_str_add_id(world, buf, &term->obj, false); } - - ecs_column_t *s = &table->storage.columns[storage_column]; - size = s->size; - align = s->alignment; - vec = s->data; - /* Fallthrough to has_data */ - } - -has_data: - if (ptr_out) ptr_out[0] = ecs_vector_get_t(vec, size, align, row); - if (size_out) size_out[0] = size; - return; - -has_switch: { - /* Edge case: if column is a switch we should return the vector with case - * identifiers. Will be replaced in the future with pluggable storage */ - ecs_switch_t *sw = table->storage.sw_columns[ - (column - 1) - table->sw_column_offset].data; - vec = flecs_switch_values(sw); - size = ECS_SIZEOF(ecs_entity_t); - align = ECS_ALIGNOF(ecs_entity_t); - goto has_data; + ecs_strbuf_appendstr(buf, ")"); } - -no_data: - if (ptr_out) ptr_out[0] = NULL; - if (size_out) size_out[0] = 0; } -void flecs_iter_populate_data( - ecs_world_t *world, - ecs_iter_t *it, - void **ptrs, - ecs_size_t *sizes) +char* ecs_term_str( + const ecs_world_t *world, + const ecs_term_t *term) { - int t, term_count = it->term_count; - for (t = 0; t < term_count; t ++) { - int32_t column = it->columns[t]; - flecs_iter_populate_term_data(world, it, t, column, - &ptrs[t * (ptrs != NULL)], - &sizes[t * (sizes != NULL)]); - } + ecs_strbuf_t buf = ECS_STRBUF_INIT; + term_str_w_strbuf(world, term, &buf); + return ecs_strbuf_get(&buf); } -/* --- Public API --- */ - -void* ecs_term_w_size( - const ecs_iter_t *it, - size_t size, - int32_t term) +char* ecs_filter_str( + const ecs_world_t *world, + const ecs_filter_t *filter) { - ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!size || ecs_term_size(it, term) == size, - ECS_INVALID_PARAMETER, NULL); - - (void)size; - - if (!term) { - return it->entities; - } - - if (!it->ptrs) { - return NULL; - } + ecs_strbuf_t buf = ECS_STRBUF_INIT; - return it->ptrs[term - 1]; -} + ecs_assert(!filter->term_cache_used || filter->terms == filter->term_cache, + ECS_INTERNAL_ERROR, NULL); -bool ecs_term_is_readonly( - const ecs_iter_t *it, - int32_t term_index) -{ - ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); - ecs_assert(term_index > 0, ECS_INVALID_PARAMETER, NULL); + ecs_term_t *terms = filter->terms; + int32_t i, count = filter->term_count; + int32_t or_count = 0; - ecs_term_t *term = &it->terms[term_index - 1]; - ecs_assert(term != NULL, ECS_INVALID_PARAMETER, NULL); - - if (term->inout == EcsIn) { - return true; - } else { - ecs_term_id_t *subj = &term->subj; + for (i = 0; i < count; i ++) { + ecs_term_t *term = &terms[i]; - if (term->inout == EcsInOutDefault) { - if (subj->entity != EcsThis) { - return true; + if (i) { + if (terms[i - 1].oper == EcsOr && term->oper == EcsOr) { + ecs_strbuf_appendstr(&buf, " || "); + } else { + ecs_strbuf_appendstr(&buf, ", "); } + } - if ((subj->set.mask != EcsSelf) && - (subj->set.mask != EcsDefaultSet)) - { - return true; + if (or_count < 1) { + if (term->inout == EcsIn) { + ecs_strbuf_appendstr(&buf, "[in] "); + } else if (term->inout == EcsInOut) { + ecs_strbuf_appendstr(&buf, "[inout] "); + } else if (term->inout == EcsOut) { + ecs_strbuf_appendstr(&buf, "[out] "); } } + + if (term->oper == EcsOr) { + or_count ++; + } else { + or_count = 0; + } + + term_str_w_strbuf(world, term, &buf); } - return false; + return ecs_strbuf_get(&buf); } -int32_t ecs_iter_find_column( - const ecs_iter_t *it, - ecs_entity_t component) +static +bool populate_from_column( + ecs_world_t *world, + const ecs_table_t *table, + int32_t offset, + ecs_id_t id, + int32_t column, + ecs_entity_t source, + ecs_id_t *id_out, + ecs_entity_t *subject_out, + ecs_size_t *size_out, + void **ptr_out) { - ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); - ecs_assert(it->table != NULL, ECS_INVALID_PARAMETER, NULL); - return ecs_type_index_of(it->table->type, 0, component); -} + ecs_column_t *storage = NULL; + ecs_size_t size = 0; -bool ecs_term_is_set( - const ecs_iter_t *it, - int32_t index) -{ - ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); + if (column != -1) { + /* If source is not This, find table of source */ + if (source) { + table = ecs_get_table(world, source); + ecs_table_record_t *tr = flecs_get_table_record(world, table, id); + column = tr->column; + } - int32_t column = it->columns[index - 1]; - if (!column) { - return false; - } else if (column < 0) { - if (it->references) { - column = -column - 1; - ecs_ref_t *ref = &it->references[column]; - return ref->entity != 0; - } else { - return true; + int32_t storage_column = ecs_table_type_to_storage_index(table, column); + if (storage_column != -1) { + storage = &table->storage.columns[storage_column]; + size = storage->size; + } + + ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t); + id = ids[column]; + + if (subject_out) { + *subject_out = source; + } + + if (ptr_out) { + if (storage) { + if (source) { + *ptr_out = (void*)ecs_get_id(world, source, id); + } else { + *ptr_out = ecs_vector_first_t( + storage->data, size, storage->alignment); + + if (*ptr_out && offset) { + *ptr_out = ECS_OFFSET(*ptr_out, size * offset); + } + } + } else { + *ptr_out = NULL; + } } } - return true; -} - -void* ecs_iter_column_w_size( - const ecs_iter_t *it, - size_t size, - int32_t index) -{ - ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); - ecs_assert(it->table != NULL, ECS_INVALID_PARAMETER, NULL); - (void)size; - - ecs_table_t *table = it->table; - int32_t storage_index = ecs_table_type_to_storage_index(table, index); - if (storage_index == -1) { - return NULL; + if (id_out) { + *id_out = id; } - ecs_column_t *columns = table->storage.columns; - ecs_column_t *column = &columns[storage_index]; - ecs_assert(!size || (ecs_size_t)size == column->size, - ECS_INVALID_PARAMETER, NULL); - - void *ptr = ecs_vector_first_t( - column->data, column->size, column->alignment); + if (size_out) { + *size_out = size; + } - return ECS_OFFSET(ptr, column->size * it->offset); + return storage != NULL; } -size_t ecs_iter_column_size( - const ecs_iter_t *it, - int32_t index) +static +void populate_from_table( + ecs_iter_t *it, + ecs_table_t *table) { - ecs_assert(it->is_valid, ECS_INVALID_PARAMETER, NULL); - ecs_assert(it->table != NULL, ECS_INVALID_PARAMETER, NULL); - - ecs_table_t *table = it->table; - int32_t storage_index = ecs_table_type_to_storage_index(table, index); - if (storage_index == -1) { - return 0; - } + it->table = table; + it->type = table->type; + it->count = ecs_table_count(table); - ecs_column_t *columns = table->storage.columns; - ecs_column_t *column = &columns[storage_index]; - - return flecs_to_size_t(column->size); + const ecs_data_t *data = &table->storage; + it->entities = ecs_vector_first(data->entities, ecs_entity_t); } -char* ecs_iter_str( - const ecs_iter_t *it) +bool flecs_term_match_table( + ecs_world_t *world, + const ecs_term_t *term, + const ecs_table_t *table, + ecs_type_t type, + int32_t offset, + ecs_id_t *id_out, + int32_t *column_out, + ecs_entity_t *subject_out, + ecs_size_t *size_out, + void **ptr_out, + int32_t *match_index_out, + bool first) { - ecs_world_t *world = it->world; - ecs_strbuf_t buf = ECS_STRBUF_INIT; - int i; + const ecs_term_id_t *subj = &term->subj; + ecs_oper_kind_t oper = term->oper; + const ecs_table_t *match_table = table; + ecs_type_t match_type = type; - if (it->term_count) { - ecs_strbuf_list_push(&buf, "term: ", ","); - for (i = 0; i < it->term_count; i ++) { - ecs_id_t id = ecs_term_id(it, i + 1); - char *str = ecs_id_str(world, id); - ecs_strbuf_list_appendstr(&buf, str); - ecs_os_free(str); - } - ecs_strbuf_list_pop(&buf, "\n"); + ecs_entity_t subj_entity = subj->entity; + if (!subj_entity) { + id_out[0] = term->id; + return true; + } - ecs_strbuf_list_push(&buf, "subj: ", ","); - for (i = 0; i < it->term_count; i ++) { - ecs_entity_t subj = ecs_term_source(it, i + 1); - char *str = ecs_get_fullpath(world, subj); - ecs_strbuf_list_appendstr(&buf, str); - ecs_os_free(str); + if (subj_entity != EcsThis) { + match_table = ecs_get_table(world, subj_entity); + if (match_table) { + match_type = match_table->type; + } else { + match_type = NULL; } - ecs_strbuf_list_pop(&buf, "\n"); } - if (it->variable_count) { - int32_t actual_count = 0; - for (i = 0; i < it->variable_count; i ++) { - const char *var_name = it->variable_names[i]; - if (!var_name || var_name[0] == '_' || var_name[0] == '.') { - /* Skip anonymous variables */ - continue; - } + ecs_entity_t source; - ecs_entity_t var = it->variables[i]; - if (!var) { - /* Skip table variables */ - continue; - } + int32_t column = 0; + if (!first && column_out && column_out[0] != 0) { + column = column_out[0] - 1; + } - if (!actual_count) { - ecs_strbuf_list_push(&buf, "vars: ", ","); - } + column = ecs_type_match(world, match_table, match_type, + column, term->id, subj->set.relation, subj->set.min_depth, + subj->set.max_depth, &source, match_index_out); - char *str = ecs_get_fullpath(world, var); - ecs_strbuf_list_append(&buf, "%s=%s", var_name, str); - ecs_os_free(str); + bool result = column != -1; - actual_count ++; - } - if (actual_count) { - ecs_strbuf_list_pop(&buf, "\n"); - } + if (oper == EcsNot) { + result = !result; } - if (it->count) { - ecs_strbuf_appendstr(&buf, "this:\n"); - for (i = 0; i < it->count; i ++) { - ecs_entity_t e = it->entities[i]; - char *str = ecs_get_fullpath(world, e); - ecs_strbuf_appendstr(&buf, " - "); - ecs_strbuf_appendstr(&buf, str); - ecs_strbuf_appendstr(&buf, "\n"); - ecs_os_free(str); - } + if (oper == EcsOptional) { + result = true; } - return ecs_strbuf_get(&buf); -} - -static -int32_t count_events( - const ecs_entity_t *events) -{ - int32_t i; - - for (i = 0; i < ECS_TRIGGER_DESC_EVENT_COUNT_MAX; i ++) { - if (!events[i]) { - break; + if (subj_entity != EcsThis) { + if (!source) { + source = subj_entity; } } - return i; -} + if (column_out && result) { + ecs_assert(id_out != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(subject_out != NULL, ECS_INTERNAL_ERROR, NULL); -static -ecs_entity_t get_actual_event( - ecs_trigger_t *trigger, - ecs_entity_t event) -{ - /* If operator is Not, reverse the event */ - if (trigger->term.oper == EcsNot) { - if (event == EcsOnAdd) { - event = EcsOnRemove; - } else if (event == EcsOnRemove) { - event = EcsOnAdd; + populate_from_column(world, table, offset, term->id, column, + source, id_out, subject_out, size_out, ptr_out); + + if (column != -1) { + column_out[0] = column + 1; + } else { + column_out[0] = 0; } } - return event; + return result; } -static -void register_trigger_for_id( +bool flecs_filter_match_table( ecs_world_t *world, - ecs_observable_t *observable, - ecs_trigger_t *trigger, - ecs_id_t id, - bool register_for_set) + const ecs_filter_t *filter, + const ecs_table_t *table, + ecs_type_t type, + int32_t offset, + ecs_id_t *ids, + int32_t *columns, + ecs_entity_t *subjects, + ecs_size_t *sizes, + void **ptrs, + int32_t *match_indices, + int32_t *matches_left, + bool first, + int32_t skip_term) { - ecs_sparse_t *triggers = observable->triggers; - ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(filter != NULL, ECS_INVALID_PARAMETER, NULL); - int i; - for (i = 0; i < trigger->event_count; i ++) { - ecs_entity_t event = get_actual_event(trigger, trigger->events[i]); + ecs_assert(!filter->term_cache_used || filter->terms == filter->term_cache, + ECS_INTERNAL_ERROR, NULL); - /* Get triggers for event */ - ecs_event_triggers_t *evt = flecs_sparse_ensure( - triggers, ecs_event_triggers_t, event); - ecs_assert(evt != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_term_t *terms = filter->terms; + int32_t i, count = filter->term_count; - if (!evt->triggers) { - evt->triggers = ecs_map_new(ecs_id_triggers_t, 1); + bool is_or = false; + bool or_result = false; + int32_t match_count = 0; + + for (i = 0; i < count; i ++) { + if (i == skip_term) { + continue; } - /* Get triggers for (component) id */ - ecs_id_triggers_t *idt = ecs_map_ensure( - evt->triggers, ecs_id_triggers_t, id); - ecs_assert(idt != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_term_t *term = &terms[i]; + ecs_term_id_t *subj = &term->subj; + ecs_oper_kind_t oper = term->oper; + const ecs_table_t *match_table = table; + ecs_type_t match_type = type; + int32_t t_i = term->index; - ecs_map_t *id_triggers = NULL; + if (!is_or && oper == EcsOr) { + is_or = true; + or_result = false; + } else if (is_or && oper != EcsOr) { + if (!or_result) { + return false; + } - if (!register_for_set) { - if (!(id_triggers = idt->triggers)) { - id_triggers = idt->triggers = ecs_map_new(ecs_trigger_t*, 1); + is_or = false; + } + + ecs_entity_t subj_entity = subj->entity; + if (!subj_entity) { + if (ids) { + ids[t_i] = term->id; } - } else { - if (!(id_triggers = idt->set_triggers)) { - id_triggers = idt->set_triggers = - ecs_map_new(ecs_trigger_t*, 1); + continue; + } + + if (subj_entity != EcsThis) { + match_table = ecs_get_table(world, subj_entity); + if (match_table) { + match_type = match_table->type; + } else { + match_type = NULL; } } - ecs_trigger_t **elem = ecs_map_ensure( - id_triggers, ecs_trigger_t*, trigger->id); - *elem = trigger; + bool result = flecs_term_match_table(world, term, match_table, + match_type, offset, + ids ? &ids[t_i] : NULL, + columns ? &columns[t_i] : NULL, + subjects ? &subjects[t_i] : NULL, + sizes ? &sizes[t_i] : NULL, + ptrs ? &ptrs[t_i] : NULL, + match_indices ? &match_indices[t_i] : NULL, + first); - // First trigger of its kind, send table notification - flecs_notify_tables(world, id, &(ecs_table_event_t){ - .kind = EcsTableTriggerMatch, - .event = trigger->events[i] - }); + if (is_or) { + or_result |= result; + } else if (!result) { + return false; + } + + /* Match indices is populated with the number of matches for this term. + * This is used to determine whether to keep iterating this table. */ + if (first && match_indices && match_indices[t_i]) { + match_indices[t_i] --; + match_count += match_indices[t_i]; + } + } + + if (matches_left) { + *matches_left = match_count; } + + return !is_or || or_result; } static -void register_trigger( - ecs_world_t *world, - ecs_observable_t *observable, - ecs_trigger_t *trigger) +void term_iter_init_no_data( + ecs_term_iter_t *iter) { - ecs_term_t *term = &trigger->term; - if (term->subj.set.mask & EcsSelf) { - register_trigger_for_id(world, observable, trigger, term->id, false); - } - if (trigger->term.subj.set.mask & EcsSuperSet) { - ecs_id_t pair = ecs_pair(term->subj.set.relation, EcsWildcard); - register_trigger_for_id(world, observable, trigger, pair, true); - } + iter->term = (ecs_term_t){ .index = -1 }; + iter->self_index = NULL; + iter->index = 0; } static -void unregister_trigger_for_id( - ecs_observable_t *observable, - ecs_trigger_t *trigger, - ecs_id_t id, - bool unregister_for_set) +void term_iter_init_wildcard( + const ecs_world_t *world, + ecs_term_iter_t *iter) { - ecs_sparse_t *triggers = observable->triggers; - ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); - - int i; - for (i = 0; i < trigger->event_count; i ++) { - ecs_entity_t event = get_actual_event(trigger, trigger->events[i]); - - /* Get triggers for event */ - ecs_event_triggers_t *evt = flecs_sparse_get( - triggers, ecs_event_triggers_t, event); - ecs_assert(evt != NULL, ECS_INTERNAL_ERROR, NULL); - - /* Get triggers for (component) id */ - ecs_id_triggers_t *idt = ecs_map_get( - evt->triggers, ecs_id_triggers_t, id); - ecs_assert(idt != NULL, ECS_INTERNAL_ERROR, NULL); - - ecs_map_t *id_triggers; - - if (unregister_for_set) { - id_triggers = idt->set_triggers; - } else { - id_triggers = idt->triggers; - } + iter->term = (ecs_term_t){ .index = -1 }; + iter->self_index = flecs_get_id_record(world, EcsWildcard); + iter->cur = iter->self_index; + iter->index = 0; +} - ecs_map_remove(id_triggers, trigger->id); +static +void term_iter_init( + const ecs_world_t *world, + ecs_term_t *term, + ecs_term_iter_t *iter) +{ + const ecs_term_id_t *subj = &term->subj; - if (!ecs_map_count(id_triggers)) { - ecs_map_free(id_triggers); + iter->term = *term; - if (unregister_for_set) { - idt->set_triggers = NULL; - } else { - idt->triggers = NULL; - } + if (subj->set.mask == EcsDefaultSet || subj->set.mask & EcsSelf) { + iter->self_index = flecs_get_id_record(world, term->id); + } - if (!idt->triggers && !idt->set_triggers) { - ecs_map_remove(evt->triggers, id); - if (!ecs_map_count(evt->triggers)) { - ecs_map_free(evt->triggers); - evt->triggers = NULL; - } - } - } + if (subj->set.mask & EcsSuperSet) { + iter->set_index = flecs_get_id_record(world, + ecs_pair(subj->set.relation, EcsWildcard)); } -} -static -void unregister_trigger( - ecs_observable_t *observable, - ecs_trigger_t *trigger) -{ - ecs_term_t *term = &trigger->term; - if (term->subj.set.mask & EcsSelf) { - unregister_trigger_for_id(observable, trigger, term->id, false); + iter->index = 0; + if (iter->self_index) { + iter->cur = iter->self_index; } else { - ecs_id_t pair = ecs_pair(term->subj.set.relation, EcsWildcard); - unregister_trigger_for_id(observable, trigger, pair, true); + iter->cur = iter->set_index; } } -static -ecs_map_t* get_triggers_for_event( - const ecs_poly_t *object, - ecs_entity_t event) +ecs_iter_t ecs_term_iter( + const ecs_world_t *stage, + ecs_term_t *term) { - ecs_assert(object != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(event != 0, ECS_INTERNAL_ERROR, NULL); - - /* Get triggers for event */ - ecs_observable_t *observable = ecs_get_observable(object); - ecs_assert(observable != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(term != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(term->id != 0, ECS_INVALID_PARAMETER, NULL); - ecs_sparse_t *triggers = observable->triggers; - ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); + const ecs_world_t *world = ecs_get_world(stage); - const ecs_event_triggers_t *evt = flecs_sparse_get( - triggers, ecs_event_triggers_t, event); - - if (evt) { - return evt->triggers; + if (ecs_term_finalize(world, NULL, term)) { + /* Invalid term */ + ecs_abort(ECS_INVALID_PARAMETER, NULL); } - return NULL; -} + ecs_iter_t it = { + .real_world = (ecs_world_t*)world, + .world = (ecs_world_t*)stage, + .terms = term, + .term_count = 1, + .next = ecs_term_next + }; -static -ecs_id_triggers_t* get_triggers_for_id( - const ecs_map_t *evt, - ecs_id_t id) -{ - return ecs_map_get(evt, ecs_id_triggers_t, id); + term_iter_init(world, term, &it.iter.term); + + return it; } -ecs_id_triggers_t* flecs_triggers_for_id( - const ecs_poly_t *object, - ecs_id_t id, - ecs_entity_t event) +static +const ecs_table_record_t *next_table( + ecs_term_iter_t *iter) { - const ecs_map_t *evt = get_triggers_for_event(object, event); - if (!evt) { + const ecs_table_record_t *tables = flecs_id_record_tables(iter->cur); + int32_t count = flecs_id_record_count(iter->cur); + if (iter->index >= count) { return NULL; } - return get_triggers_for_id(evt, id); + return &tables[iter->index ++]; } static -void init_iter( - ecs_iter_t *it, - ecs_entity_t *entity, - ecs_table_t *table, - int32_t row, - int32_t count, - bool *iter_set) +ecs_table_record_t term_iter_next( + ecs_world_t *world, + ecs_term_iter_t *iter, + ecs_entity_t *source_out, + bool match_prefab, + bool match_disabled) { - ecs_assert(it != NULL, ECS_INTERNAL_ERROR, NULL); - - if (*iter_set) { - return; - } + ecs_table_t *table = iter->table; + ecs_entity_t source = 0; + const ecs_table_record_t *tr; + ecs_table_record_t result = { .table = NULL }; + ecs_term_t *term = &iter->term; + + do { + if (table) { + iter->cur_match ++; + if (iter->cur_match >= iter->match_count) { + table = NULL; + } else { + result.table = table; + result.count = iter->match_count; + result.column = iter->last_column = ecs_type_index_of( + table->type, iter->last_column + 1, term->id); + } + } - flecs_iter_init(it); + if (!table) { + if (!(tr = next_table(iter))) { + if (iter->cur != iter->set_index && iter->set_index != NULL) { + iter->cur = iter->set_index; + iter->index = 0; + tr = next_table(iter); + } - *iter_set = true; + if (!tr) { + result.table = NULL; + return result; + } + } - it->ids[0] = it->event_id; + table = tr->table; - if (count) { - if (table) { - ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(!it->world->is_readonly, ECS_INTERNAL_ERROR, NULL); - ecs_data_t *data = &table->storage; - ecs_assert(data != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_entity_t *entities = ecs_vector_first( - data->entities, ecs_entity_t); - ecs_assert(entities != NULL, ECS_INTERNAL_ERROR, NULL); - ecs_assert(count > 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(row < ecs_vector_count(data->entities), - ECS_INTERNAL_ERROR, NULL); - ecs_assert((row + count) <= ecs_vector_count(data->entities), - ECS_INTERNAL_ERROR, NULL); + if (!match_prefab && (table->flags & EcsTableIsPrefab)) { + continue; + } - it->entities = ECS_OFFSET(entities, ECS_SIZEOF(ecs_entity_t) * row); + if (!match_disabled && (table->flags & EcsTableIsDisabled)) { + continue; + } - ecs_entity_t subject = 0; - int32_t index = ecs_type_match(it->world, table, table->type, 0, - it->event_id, EcsIsA, 0, 0, &subject, NULL); + if (!ecs_table_count(table)) { + continue; + } - ecs_assert(index >= 0, ECS_INTERNAL_ERROR, NULL); - int32_t storage_index = ecs_table_type_to_storage_index( - table, index); - - index ++; - it->columns[0] = index; - it->sizes[0] = 0; + iter->table = table; + iter->match_count = tr->count; + iter->cur_match = 0; + iter->last_column = tr->column; - if (storage_index == -1) { - it->columns[0] = 0; - } + result = *tr; + } - if (!subject && it->columns[0] && data && data->columns) { - ecs_column_t *col = &data->columns[storage_index]; - it->ptrs[0] = ecs_vector_get_t( - col->data, col->size, col->alignment, row); - it->sizes[0] = col->size; - } else if (subject) { - it->ptrs[0] = (void*)ecs_get_id( - it->world, subject, it->event_id); - ecs_entity_t e = ecs_get_typeid(it->world, it->event_id); - const EcsComponent *comp = ecs_get(it->world, e, EcsComponent); - if (comp) { - it->sizes[0] = comp->size; - } else { - it->sizes[0] = 0; + if (iter->cur == iter->set_index) { + const ecs_term_id_t *subj = &term->subj; + + if (iter->self_index) { + if (flecs_id_record_table(iter->self_index, table) != NULL) { + /* If the table has the id itself and this term matched Self + * we already matched it */ + continue; } - - it->subjects[0] = subject; } - } else { - it->entities = entity; + + /* Test if following the relation finds the id */ + int32_t index = ecs_type_match(world, table, table->type, 0, + term->id, subj->set.relation, subj->set.min_depth, + subj->set.max_depth, &source, NULL); + if (index == -1) { + continue; + } + + ecs_assert(source != 0, ECS_INTERNAL_ERROR, NULL); } + + break; + } while (true); + + if (source_out) { + *source_out = source; } + + return result; } -static -bool ignore_table( - ecs_trigger_t *t, - ecs_table_t *table) +bool ecs_term_next( + ecs_iter_t *it) { + ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(it->next == ecs_term_next, ECS_INVALID_PARAMETER, NULL); + + ecs_term_iter_t *iter = &it->iter.term; + ecs_term_t *term = &iter->term; + ecs_world_t *world = it->real_world; + + ecs_entity_t source = 0; + ecs_table_record_t tr = term_iter_next(world, iter, &source, false, false); + ecs_table_t *table = tr.table; if (!table) { + it->is_valid = false; return false; } - if (!t->match_prefab && (table->flags & EcsTableIsPrefab)) { - return true; - } - if (!t->match_disabled && (table->flags & EcsTableIsDisabled)) { - return true; + /* Source must either be 0 (EcsThis) or nonzero in case of substitution */ + ecs_assert(source || iter->cur != iter->set_index, ECS_INTERNAL_ERROR, NULL); + ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL); + + it->table = table; + it->type = table->type; + it->ids = &iter->id; + it->columns = &iter->column; + it->subjects = &iter->subject; + it->sizes = &iter->size; + it->ptrs = &iter->ptr; + + it->count = ecs_table_count(table); + it->entities = ecs_vector_first(table->storage.entities, ecs_entity_t); + it->is_valid = true; + + bool has_data = populate_from_column(world, table, 0, term->id, tr.column, + source, &iter->id, &iter->subject, &iter->size, + &iter->ptr); + + if (!source) { + if (has_data) { + iter->column = tr.column + 1; + } else { + iter->column = 0; + } + } else { + iter->column = -1; /* Point to ref */ } - - return false; + + return true; } static -void notify_self_triggers( +const ecs_filter_t* init_filter_iter( + const ecs_world_t *world, ecs_iter_t *it, - const ecs_map_t *triggers) + const ecs_filter_t *filter) { - ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_filter_iter_t *iter = &it->iter.filter; - ecs_map_iter_t mit = ecs_map_iter(triggers); - ecs_trigger_t *t; - while ((t = ecs_map_next_ptr(&mit, ecs_trigger_t*, NULL))) { - if (ignore_table(t, it->table)) { - continue; + if (filter) { + iter->filter = *filter; + + if (filter->term_cache_used) { + iter->filter.terms = iter->filter.term_cache; } - it->system = t->entity; - it->self = t->self; - it->ctx = t->ctx; - it->binding_ctx = t->binding_ctx; - it->term_index = t->term.index; - it->terms = &t->term; - t->action(it); + ecs_filter_finalize(world, &iter->filter); + + ecs_assert(!filter->term_cache_used || + filter->terms == filter->term_cache, ECS_INTERNAL_ERROR, NULL); + } else { + ecs_filter_init(world, &iter->filter, &(ecs_filter_desc_t) { + .terms = {{ .id = EcsWildcard }} + }); + + filter = &iter->filter; } + + it->term_count = filter->term_count_actual; + + return filter; } -static -void notify_set_triggers( - ecs_iter_t *it, - const ecs_map_t *triggers) +ecs_iter_t ecs_filter_iter( + const ecs_world_t *stage, + const ecs_filter_t *filter) { - ecs_assert(triggers != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_assert(stage != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_map_iter_t mit = ecs_map_iter(triggers); - ecs_trigger_t *t; - while ((t = ecs_map_next_ptr(&mit, ecs_trigger_t*, NULL))) { - if (ignore_table(t, it->table)) { - continue; - } + const ecs_world_t *world = ecs_get_world(stage); - if (flecs_term_match_table(it->world, &t->term, it->table, it->type, - it->offset, it->ids, it->columns, it->subjects, it->sizes, - it->ptrs, NULL, true)) - { - if (!it->subjects[0]) { - /* Do not match owned components */ + ecs_iter_t it = { + .real_world = (ecs_world_t*)world, + .world = (ecs_world_t*)stage, + .terms = filter ? filter->terms : NULL, + .next = ecs_filter_next + }; + + ecs_filter_iter_t *iter = &it.iter.filter; + + filter = init_filter_iter(world, &it, filter); + + int32_t i, term_count = filter->term_count; + ecs_term_t *terms = filter->terms; + int32_t min_count = -1; + int32_t min_term_index = -1; + + /* Find term that represents smallest superset */ + if (filter->match_this) { + iter->kind = EcsFilterIterEvalIndex; + + for (i = 0; i < term_count; i ++) { + ecs_term_t *term = &terms[i]; + + ecs_assert(term != NULL, ECS_INTERNAL_ERROR, NULL); + + if (term->oper != EcsAnd) { continue; } - ecs_entity_t event_id = it->event_id; - it->event_id = t->term.id; - - it->ids[0] = t->term.id; - it->system = t->entity; - it->self = t->self; - it->ctx = t->ctx; - it->binding_ctx = t->binding_ctx; - it->term_index = t->term.index; - it->terms = &t->term; - t->action(it); + if (term->subj.entity != EcsThis) { + continue; + } - it->event_id = event_id; - } - } -} + ecs_id_record_t *idr = flecs_get_id_record(world, term->id); + if (!idr) { + /* If one of the terms does not match with any data, iterator + * should not return anything */ + term_iter_init_no_data(&iter->term_iter); + return it; + } -static -void notify_triggers_for_id( - const ecs_map_t *evt, - ecs_id_t event_id, - ecs_iter_t *it, - ecs_entity_t *entity, - ecs_table_t *table, - int32_t row, - int32_t count, - bool *iter_set) -{ - const ecs_id_triggers_t *idt = get_triggers_for_id(evt, event_id); - if (idt) { - if (idt->triggers) { - init_iter(it, entity, table, row, count, iter_set); - notify_self_triggers(it, idt->triggers); + int32_t table_count = flecs_id_record_count(idr); + if (min_count == -1 || table_count < min_count) { + min_count = table_count; + min_term_index = i; + } } - if (idt->set_triggers) { - init_iter(it, entity, table, row, count, iter_set); - notify_set_triggers(it, idt->set_triggers); + + iter->min_term_index = min_term_index; + + if (min_term_index == -1) { + term_iter_init_wildcard(world, &iter->term_iter); + } else { + term_iter_init(world, &terms[min_term_index], &iter->term_iter); } + } else { + /* If filter has no this terms, no tables need to be evaluated */ + iter->kind = EcsFilterIterEvalNone; } -} -void flecs_triggers_notify( - ecs_world_t *world, - ecs_poly_t *observable, - ecs_ids_t *ids, - ecs_entity_t event, - ecs_entity_t entity, - ecs_table_t *table, - ecs_table_t *other_table, - int32_t row, - int32_t count, - void *param) -{ - if (!ids || !ids->count) { - return; + if (filter->terms == filter->term_cache) { + /* Because we're returning the iterator by value, the address of the + * term cache changes. The ecs_filter_next function will set the correct + * address when it detects that terms is set to NULL */ + iter->filter.terms = NULL; } - ecs_assert(ids->array != NULL, ECS_INTERNAL_ERROR, NULL); + return it; +} - if (!observable) { - observable = world; - } +ecs_iter_t ecs_filter_chain_iter( + ecs_iter_t *chain_it, + const ecs_filter_t *filter) +{ + ecs_iter_t it = { + .chain_it = chain_it, + .next = ecs_filter_next, + .world = chain_it->world, + .real_world = chain_it->real_world + }; - ecs_entity_t trigger_event = event; + ecs_filter_iter_t *iter = &it.iter.filter; + init_filter_iter(it.world, &it, filter); - do { - const ecs_map_t *evt = get_triggers_for_event(observable, trigger_event); - if (!evt && trigger_event != EcsWildcard) { - trigger_event = EcsWildcard; - continue; - } + iter->kind = EcsFilterIterEvalChain; - if (!evt) { - return; - } + if (filter->terms == filter->term_cache) { + /* See ecs_filter_iter*/ + iter->filter.terms = NULL; + } - ecs_iter_t it = { - .world = world, - .event = event, - .term_count = 1, - .table = table, - .type = table ? table->type : NULL, - .other_table = other_table, - .offset = row, - .count = count, - .param = param - }; + return it; +} - int32_t i, ids_count = ids->count; - ecs_id_t *ids_array = ids->array; +bool ecs_filter_next( + ecs_iter_t *it) +{ + ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(it->next == ecs_filter_next, ECS_INVALID_PARAMETER, NULL); + ecs_assert(it->chain_it != it, ECS_INVALID_PARAMETER, NULL); - for (i = 0; i < ids_count; i ++) { - ecs_id_t id = ids_array[i]; - bool iter_set = false; + ecs_filter_iter_t *iter = &it->iter.filter; + ecs_filter_t *filter = &iter->filter; + ecs_world_t *world = it->real_world; + ecs_table_t *table; + bool match; + int i; - it.event_id = id; + if (!filter->terms) { + filter->terms = filter->term_cache; + } - notify_triggers_for_id( - evt, id, &it, &entity, table, row, count, &iter_set); + flecs_iter_init(it); - if (ECS_HAS_ROLE(id, PAIR)) { - ecs_entity_t pred = ECS_PAIR_RELATION(id); - ecs_entity_t obj = ECS_PAIR_OBJECT(id); + if (iter->kind == EcsFilterIterEvalIndex) { + ecs_term_iter_t *term_iter = &iter->term_iter; + ecs_term_t *term = &term_iter->term; + int32_t term_index = term->index; - notify_triggers_for_id(evt, ecs_pair(pred, EcsWildcard), - &it, &entity, table, row, count, &iter_set); + do { + ecs_assert(iter->matches_left >= 0, ECS_INTERNAL_ERROR, NULL); + bool first_match = iter->matches_left == 0; - notify_triggers_for_id(evt, ecs_pair(EcsWildcard, obj), - &it, &entity, table, row, count, &iter_set); + if (first_match) { + /* Find new match, starting with the leading term */ + ecs_entity_t source; + ecs_table_record_t tr = term_iter_next(world, term_iter, + &source, filter->match_prefab, filter->match_disabled); + table = tr.table; + if (!table) { + goto done; + } - notify_triggers_for_id(evt, ecs_pair(EcsWildcard, EcsWildcard), - &it, &entity, table, row, count, &iter_set); + /* Populate term data as flecs_filter_match_table skips it */ + populate_from_column(world, table, 0, term->id, tr.column, source, + &it->ids[term_index], + &it->subjects[term_index], + &it->sizes[term_index], + &it->ptrs[term_index]); + + it->columns[term_index] = tr.column + 1; } else { - notify_triggers_for_id(evt, EcsWildcard, - &it, &entity, table, row, count, &iter_set); - } - } + /* Progress iterator to next match for table, if any */ + table = it->table; + first_match = false; - if (trigger_event == EcsWildcard) { - break; - } - - trigger_event = EcsWildcard; - } while (true); -} + for (i = filter->term_count_actual - 1; i >= 0; i --) { + if (it->match_indices[i] > 0) { + it->match_indices[i] --; + it->columns[i] ++; + break; + } + } + } -ecs_entity_t ecs_trigger_init( - ecs_world_t *world, - const ecs_trigger_desc_t *desc) -{ - ecs_poly_assert(world, ecs_world_t); - ecs_assert(!world->is_readonly, ECS_INVALID_OPERATION, NULL); - ecs_assert(desc != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_assert(!world->is_fini, ECS_INVALID_OPERATION, NULL); + /* Match the remainder of the terms */ + match = flecs_filter_match_table(world, filter, table, table->type, + 0, it->ids, it->columns, it->subjects, it->sizes, + it->ptrs, it->match_indices, &iter->matches_left, + first_match, term->index); - char *name = NULL; - const char *expr = desc->expr; - - ecs_observable_t *observable = desc->observable; - if (!observable) { - observable = ecs_get_observable(world); - } + /* Check if there are any terms which have more matching columns */ + if (!first_match) { + iter->matches_left = 0; + for (i = 0; i < filter->term_count_actual; i ++) { + if (it->match_indices[i] > 0) { + iter->matches_left += it->match_indices[i]; + } + } + } + } while (!match); - /* If entity is provided, create it */ - ecs_entity_t existing = desc->entity.entity; - ecs_entity_t entity = ecs_entity_init(world, &desc->entity); + populate_from_table(it, table); + goto yield; - bool added = false; - EcsTrigger *comp = ecs_get_mut(world, entity, EcsTrigger, &added); - if (added) { - ecs_assert(desc->callback != NULL, ECS_INVALID_PARAMETER, NULL); - - /* Something went wrong with the construction of the entity */ - ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); - name = ecs_get_fullpath(world, entity); + } else if (iter->kind == EcsFilterIterEvalChain) { + ecs_iter_t *chain_it = it->chain_it; + ecs_iter_next_action_t next = chain_it->next; - ecs_term_t term; - if (expr) { - #ifdef FLECS_PARSER - const char *ptr = ecs_parse_term(world, name, expr, expr, &term); - if (!ptr) { - goto error; - } + /* If this is a chain iterator, the input iterator must be set */ + ecs_assert(chain_it != NULL, ECS_INVALID_PARAMETER, NULL); - if (!ecs_term_is_initialized(&term)) { - ecs_parser_error( - name, expr, 0, "invalid empty trigger expression"); - goto error; + do { + if (!next(chain_it)) { + goto done; } - if (ptr[0]) { - ecs_parser_error(name, expr, 0, - "too many terms in trigger expression (expected 1)"); - goto error; - } - #else - ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); - #endif - } else { - term = ecs_term_copy(&desc->term); - } + table = chain_it->table; - if (ecs_term_finalize(world, name, &term)) { - goto error; - } + match = flecs_filter_match_table(world, filter, table, table->type, + 0, it->ids, it->columns, it->subjects, it->sizes, + it->ptrs, it->match_indices, NULL, true, -1); + } while (!match); - /* Currently triggers are not supported for specific entities */ - ecs_assert(term.subj.entity == EcsThis, ECS_UNSUPPORTED, NULL); + populate_from_table(it, table); + goto yield; + } - ecs_trigger_t *trigger = flecs_sparse_add(world->triggers, ecs_trigger_t); - trigger->id = flecs_sparse_last_id(world->triggers); - trigger->term = ecs_term_move(&term); - trigger->action = desc->callback; - trigger->ctx = desc->ctx; - trigger->binding_ctx = desc->binding_ctx; - trigger->ctx_free = desc->ctx_free; - trigger->binding_ctx_free = desc->binding_ctx_free; - trigger->event_count = count_events(desc->events); - ecs_os_memcpy(trigger->events, desc->events, - trigger->event_count * ECS_SIZEOF(ecs_entity_t)); - trigger->entity = entity; - trigger->self = desc->self; - trigger->observable = observable; - trigger->match_prefab = desc->match_prefab; - trigger->match_disabled = desc->match_disabled; +done: + flecs_iter_fini(it); + return false; - comp->trigger = trigger; +yield: + it->is_valid = true; + return true; +} - /* Trigger must have at least one event */ - ecs_assert(trigger->event_count != 0, ECS_INVALID_PARAMETER, NULL); - register_trigger(world, observable, trigger); +#define ECS_NAME_BUFFER_LENGTH (64) - ecs_term_fini(&term); +static +bool path_append( + const ecs_world_t *world, + ecs_entity_t parent, + ecs_entity_t child, + const char *sep, + const char *prefix, + ecs_strbuf_t *buf) +{ + ecs_poly_assert(world, ecs_world_t); - if (desc->entity.name) { - ecs_trace("#[green]observer#[reset] %s created", - ecs_get_name(world, entity)); - } - } else { - ecs_assert(comp->trigger != NULL, ECS_INTERNAL_ERROR, NULL); + ecs_entity_t cur = 0; + char buff[22]; + const char *name; - /* If existing entity handle was provided, override existing params */ - if (existing) { - if (desc->callback) { - ((ecs_trigger_t*)comp->trigger)->action = desc->callback; - } - if (desc->ctx) { - ((ecs_trigger_t*)comp->trigger)->ctx = desc->ctx; - } - if (desc->binding_ctx) { - ((ecs_trigger_t*)comp->trigger)->binding_ctx = desc->binding_ctx; + if (ecs_is_valid(world, child)) { + cur = ecs_get_object(world, child, EcsChildOf, 0); + if (cur) { + if (cur != parent && cur != EcsFlecsCore) { + path_append(world, parent, cur, sep, prefix, buf); + ecs_strbuf_appendstr(buf, sep); } + } else if (prefix) { + ecs_strbuf_appendstr(buf, prefix); } - } - - ecs_os_free(name); - return entity; -error: - ecs_os_free(name); - return 0; -} -void* ecs_get_trigger_ctx( - const ecs_world_t *world, - ecs_entity_t trigger) -{ - const EcsTrigger *t = ecs_get(world, trigger, EcsTrigger); - if (t) { - return t->trigger->ctx; + name = ecs_get_name(world, child); + if (!name || !ecs_os_strlen(name)) { + ecs_os_sprintf(buff, "%u", (uint32_t)child); + name = buff; + } } else { - return NULL; - } -} + ecs_os_sprintf(buff, "%u", (uint32_t)child); + name = buff; + } -void* ecs_get_trigger_binding_ctx( - const ecs_world_t *world, - ecs_entity_t trigger) -{ - const EcsTrigger *t = ecs_get(world, trigger, EcsTrigger); - if (t) { - return t->trigger->binding_ctx; - } else { - return NULL; - } + ecs_strbuf_appendstr(buf, name); + + return cur != 0; } -void flecs_trigger_fini( - ecs_world_t *world, - ecs_trigger_t *trigger) +ecs_hashed_string_t ecs_get_hashed_string( + const char *name, + ecs_size_t length, + uint64_t hash) { - ecs_assert(trigger != NULL, ECS_INVALID_PARAMETER, NULL); - - unregister_trigger(trigger->observable, trigger); - ecs_term_fini(&trigger->term); + ecs_assert(!length || length == ecs_os_strlen(name), + ECS_INTERNAL_ERROR, NULL); - if (trigger->ctx_free) { - trigger->ctx_free(trigger->ctx); + if (!length) { + length = ecs_os_strlen(name); } - if (trigger->binding_ctx_free) { - trigger->binding_ctx_free(trigger->binding_ctx); + ecs_assert(!hash || hash == flecs_hash(name, length), + ECS_INTERNAL_ERROR, NULL); + + if (!hash) { + hash = flecs_hash(name, length); } - flecs_sparse_remove(world->triggers, trigger->id); + return (ecs_hashed_string_t) { + .value = (char*)name, + .length = length, + .hash = hash + }; } -int8_t flecs_to_i8( - int64_t v) +static +ecs_entity_t find_by_name( + const ecs_hashmap_t *map, + const char *name, + ecs_size_t length, + uint64_t hash) { - ecs_assert(v < INT8_MAX, ECS_INTERNAL_ERROR, NULL); - return (int8_t)v; -} + ecs_hashed_string_t key = ecs_get_hashed_string(name, length, hash); -int16_t flecs_to_i16( - int64_t v) -{ - ecs_assert(v < INT16_MAX, ECS_INTERNAL_ERROR, NULL); - return (int16_t)v; -} + ecs_entity_t *e = flecs_hashmap_get(*map, &key, ecs_entity_t); -int32_t flecs_to_i32( - int64_t v) -{ - ecs_assert(v < INT32_MAX, ECS_INTERNAL_ERROR, NULL); - return (int32_t)v; -} + if (!e) { + return 0; + } -uint32_t flecs_to_u32( - uint64_t v) -{ - ecs_assert(v < UINT32_MAX, ECS_INTERNAL_ERROR, NULL); - return (uint32_t)v; + return *e; } -size_t flecs_to_size_t( - int64_t size) +static +void register_by_name( + ecs_hashmap_t *map, + ecs_entity_t entity, + const char *name, + ecs_size_t length, + uint64_t hash) { - ecs_assert(size >= 0, ECS_INTERNAL_ERROR, NULL); - return (size_t)size; -} + ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(name != NULL, ECS_INVALID_PARAMETER, NULL); -ecs_size_t flecs_from_size_t( - size_t size) -{ - ecs_assert(size < INT32_MAX, ECS_INTERNAL_ERROR, NULL); - return (ecs_size_t)size; -} + ecs_hashed_string_t key = ecs_get_hashed_string(name, length, hash); + + ecs_entity_t existing = find_by_name(map, name, key.length, key.hash); + if (existing) { + if (existing != entity) { + ecs_abort(ECS_ALREADY_DEFINED, + "conflicting entity registered with name '%s'", name); + } + } else { + key.value = ecs_os_strdup(key.value); + } -int32_t flecs_next_pow_of_2( - int32_t n) -{ - n --; - n |= n >> 1; - n |= n >> 2; - n |= n >> 4; - n |= n >> 8; - n |= n >> 16; - n ++; + flecs_hashmap_result_t hmr = flecs_hashmap_ensure( + *map, &key, ecs_entity_t); - return n; + *((ecs_entity_t*)hmr.value) = entity; } -/** Convert time to double */ -double ecs_time_to_double( - ecs_time_t t) +static +bool is_number( + const char *name) { - double result; - result = t.sec; - return result + (double)t.nanosec / (double)1000000000; + ecs_assert(name != NULL, ECS_INTERNAL_ERROR, NULL); + + if (!isdigit(name[0])) { + return false; + } + + ecs_size_t i, length = ecs_os_strlen(name); + for (i = 1; i < length; i ++) { + char ch = name[i]; + + if (!isdigit(ch)) { + break; + } + } + + return i >= length; } -ecs_time_t ecs_time_sub( - ecs_time_t t1, - ecs_time_t t2) +static +ecs_entity_t name_to_id( + const ecs_world_t *world, + const char *name) { - ecs_time_t result; - - if (t1.nanosec >= t2.nanosec) { - result.nanosec = t1.nanosec - t2.nanosec; - result.sec = t1.sec - t2.sec; + long int result = atol(name); + ecs_assert(result >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_entity_t alive = ecs_get_alive(world, (ecs_entity_t)result); + if (alive) { + return alive; } else { - result.nanosec = t1.nanosec - t2.nanosec + 1000000000; - result.sec = t1.sec - t2.sec - 1; + return (ecs_entity_t)result; } - - return result; } -void ecs_sleepf( - double t) +static +ecs_entity_t get_builtin( + const char *name) { - if (t > 0) { - int sec = (int)t; - int nsec = (int)((t - sec) * 1000000000); - ecs_os_sleep(sec, nsec); + if (name[0] == '.' && name[1] == '\0') { + return EcsThis; + } else if (name[0] == '*' && name[1] == '\0') { + return EcsWildcard; } + + return 0; } -double ecs_time_measure( - ecs_time_t *start) +static +bool is_sep( + const char **ptr, + const char *sep) { - ecs_time_t stop, temp; - ecs_os_get_time(&stop); - temp = stop; - stop = ecs_time_sub(stop, *start); - *start = temp; - return ecs_time_to_double(stop); + ecs_size_t len = ecs_os_strlen(sep); + + if (!ecs_os_strncmp(*ptr, sep, len)) { + *ptr += len; + return true; + } else { + return false; + } } -void* ecs_os_memdup( - const void *src, - ecs_size_t size) +static +const char* path_elem( + const char *path, + const char *sep, + int32_t *len) { - if (!src) { + const char *ptr; + char ch; + int32_t template_nesting = 0; + int32_t count = 0; + + for (ptr = path; (ch = *ptr); ptr ++) { + if (ch == '<') { + template_nesting ++; + } else if (ch == '>') { + template_nesting --; + } + + ecs_assert(template_nesting >= 0, ECS_INVALID_PARAMETER, path); + + if (!template_nesting && is_sep(&ptr, sep)) { + break; + } + + count ++; + } + + if (len) { + *len = count; + } + + if (count) { + return ptr; + } else { return NULL; } - - void *dst = ecs_os_malloc(size); - ecs_assert(dst != NULL, ECS_OUT_OF_MEMORY, NULL); - ecs_os_memcpy(dst, src, size); - return dst; } -int flecs_entity_compare( - ecs_entity_t e1, - const void *ptr1, - ecs_entity_t e2, - const void *ptr2) +static +ecs_entity_t get_parent_from_path( + const ecs_world_t *world, + ecs_entity_t parent, + const char **path_ptr, + const char *prefix, + bool new_entity) { - (void)ptr1; - (void)ptr2; - return (e1 > e2) - (e1 < e2); + bool start_from_root = false; + const char *path = *path_ptr; + + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + + if (prefix) { + ecs_size_t len = ecs_os_strlen(prefix); + if (!ecs_os_strncmp(path, prefix, len)) { + path += len; + parent = 0; + start_from_root = true; + } + } + + if (!start_from_root && !parent && new_entity) { + parent = ecs_get_scope(world); + } + + *path_ptr = path; + + return parent; } -int flecs_entity_compare_qsort( - const void *e1, - const void *e2) -{ - ecs_entity_t v1 = *(ecs_entity_t*)e1; - ecs_entity_t v2 = *(ecs_entity_t*)e2; - return flecs_entity_compare(v1, NULL, v2, NULL); +static +void on_set_symbol(ecs_iter_t *it) { + EcsIdentifier *n = ecs_term(it, EcsIdentifier, 1); + ecs_world_t *world = it->world; + + int i; + for (i = 0; i < it->count; i ++) { + ecs_entity_t e = it->entities[i]; + register_by_name( + &world->symbols, e, n[i].value, n[i].length, n[i].hash); + } } -uint64_t flecs_string_hash( +static +uint64_t string_hash( const void *ptr) { const ecs_hashed_string_t *str = ptr; - ecs_assert(str->hash != 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(str->hash != 0, ECS_INVALID_PARAMETER, NULL); return str->hash; } -/* - This code was taken from sokol_time.h - - zlib/libpng license - Copyright (c) 2018 Andre Weissflog - This software is provided 'as-is', without any express or implied warranty. - In no event will the authors be held liable for any damages arising from the - use of this software. - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software in a - product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not - be misrepresented as being the original software. - 3. This notice may not be removed or altered from any source - distribution. -*/ - - -static int ecs_os_time_initialized; +static +int string_compare( + const void *ptr1, + const void *ptr2) +{ + const ecs_hashed_string_t *str1 = ptr1; + const ecs_hashed_string_t *str2 = ptr2; + ecs_size_t len1 = str1->length; + ecs_size_t len2 = str2->length; + if (len1 != len2) { + return (len1 > len2) - (len1 < len2); + } -#if defined(_WIN32) -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include -static double _ecs_os_time_win_freq; -static LARGE_INTEGER _ecs_os_time_win_start; -#elif defined(__APPLE__) && defined(__MACH__) -#include -static mach_timebase_info_data_t _ecs_os_time_osx_timebase; -static uint64_t _ecs_os_time_osx_start; -#else /* anything else, this will need more care for non-Linux platforms */ -#include -static uint64_t _ecs_os_time_posix_start; -#endif + return ecs_os_memcmp(str1->value, str2->value, len1); +} -/* prevent 64-bit overflow when computing relative timestamp - see https://gist.github.com/jspohr/3dc4f00033d79ec5bdaf67bc46c813e3 -*/ -#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__)) -int64_t int64_muldiv(int64_t value, int64_t numer, int64_t denom) { - int64_t q = value / denom; - int64_t r = value % denom; - return q * numer + r * numer / denom; +ecs_hashmap_t _flecs_string_hashmap_new(ecs_size_t size) { + return _flecs_hashmap_new(ECS_SIZEOF(ecs_hashed_string_t), size, + string_hash, + string_compare); } -#endif -void flecs_os_time_setup(void) { - if ( ecs_os_time_initialized) { - return; - } - - ecs_os_time_initialized = 1; - #if defined(_WIN32) - LARGE_INTEGER freq; - QueryPerformanceFrequency(&freq); - QueryPerformanceCounter(&_ecs_os_time_win_start); - _ecs_os_time_win_freq = (double)freq.QuadPart / 1000000000.0; - #elif defined(__APPLE__) && defined(__MACH__) - mach_timebase_info(&_ecs_os_time_osx_timebase); - _ecs_os_time_osx_start = mach_absolute_time(); - #else - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - _ecs_os_time_posix_start = (uint64_t)ts.tv_sec*1000000000 + (uint64_t)ts.tv_nsec; - #endif +void flecs_bootstrap_hierarchy(ecs_world_t *world) { + ecs_trigger_init(world, &(ecs_trigger_desc_t){ + .term = {.id = ecs_pair(ecs_id(EcsIdentifier), EcsSymbol)}, + .callback = on_set_symbol, + .events = {EcsOnSet} + }); } -uint64_t flecs_os_time_now(void) { - ecs_assert(ecs_os_time_initialized != 0, ECS_INTERNAL_ERROR, NULL); - - uint64_t now; - - #if defined(_WIN32) - LARGE_INTEGER qpc_t; - QueryPerformanceCounter(&qpc_t); - now = (uint64_t)(qpc_t.QuadPart / _ecs_os_time_win_freq); - #elif defined(__APPLE__) && defined(__MACH__) - now = (uint64_t) int64_muldiv((int64_t)mach_absolute_time(), (int64_t)_ecs_os_time_osx_timebase.numer, (int64_t)_ecs_os_time_osx_timebase.denom); - #else - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - now = ((uint64_t)ts.tv_sec * 1000000000 + (uint64_t)ts.tv_nsec); - #endif - return now; -} +/* Public functions */ -void flecs_os_time_sleep( - int32_t sec, - int32_t nanosec) +char* ecs_get_path_w_sep( + const ecs_world_t *world, + ecs_entity_t parent, + ecs_entity_t child, + const char *sep, + const char *prefix) { -#ifndef _WIN32 - struct timespec sleepTime; - ecs_assert(sec >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_assert(nanosec >= 0, ECS_INTERNAL_ERROR, NULL); + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + world = ecs_get_world(world); - sleepTime.tv_sec = sec; - sleepTime.tv_nsec = nanosec; - if (nanosleep(&sleepTime, NULL)) { - ecs_err("nanosleep failed"); + if (!sep) { + sep = "."; } -#else - HANDLE timer; - LARGE_INTEGER ft; - ft.QuadPart = -((int64_t)sec * 10000000 + (int64_t)nanosec / 100); + ecs_strbuf_t buf = ECS_STRBUF_INIT; - timer = CreateWaitableTimer(NULL, TRUE, NULL); - SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); - WaitForSingleObject(timer, INFINITE); - CloseHandle(timer); -#endif + if (!child || parent != child) { + path_append(world, parent, child, sep, prefix, &buf); + } else { + ecs_strbuf_appendstr(&buf, ""); + } + + return ecs_strbuf_get(&buf); } +ecs_entity_t ecs_lookup_child( + const ecs_world_t *world, + ecs_entity_t parent, + const char *name) +{ + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); -#if defined(_WIN32) + if (is_number(name)) { + return name_to_id(world, name); + } -static ULONG win32_current_resolution; + ecs_filter_t f; + int ret = ecs_filter_init(world, &f, &(ecs_filter_desc_t) { + .terms = { + { .id = ecs_pair( ecs_id(EcsIdentifier), EcsName) }, + { .id = ecs_pair(EcsChildOf, parent) }, + { .id = EcsDisabled, .oper = EcsOptional }, + { .id = EcsPrefab, .oper = EcsOptional } + } + }); + + ecs_assert(ret == 0, ECS_INTERNAL_ERROR, NULL); + (void)ret; -void flecs_increase_timer_resolution(bool enable) -{ - HMODULE hntdll = GetModuleHandle((LPCTSTR)"ntdll.dll"); - if (!hntdll) { - return; + ecs_iter_t it = ecs_filter_iter(world, &f); + while (ecs_filter_next(&it)) { + EcsIdentifier *ids = ecs_term(&it, EcsIdentifier, 1); + int i; + for (i = 0; i < it.count; i ++) { + char *cur_name = ids[i].value; + if (cur_name && !ecs_os_strcmp(cur_name, name)) { + ecs_filter_fini(&f); + return it.entities[i]; + } + } } - LONG (__stdcall *pNtSetTimerResolution)( - ULONG desired, BOOLEAN set, ULONG * current); - - pNtSetTimerResolution = (LONG(__stdcall*)(ULONG, BOOLEAN, ULONG*)) - GetProcAddress(hntdll, "NtSetTimerResolution"); + ecs_filter_fini(&f); + return 0; +} - if(!pNtSetTimerResolution) { - return; +ecs_entity_t ecs_lookup( + const ecs_world_t *world, + const char *name) +{ + if (!name) { + return 0; } - ULONG current, resolution = 10000; /* 1 ms */ + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + world = ecs_get_world(world); - if (!enable && win32_current_resolution) { - pNtSetTimerResolution(win32_current_resolution, 0, ¤t); - win32_current_resolution = 0; - return; - } else if (!enable) { - return; + ecs_entity_t e = get_builtin(name); + if (e) { + return e; } - if (resolution == win32_current_resolution) { - return; + if (is_number(name)) { + return name_to_id(world, name); } - if (win32_current_resolution) { - pNtSetTimerResolution(win32_current_resolution, 0, ¤t); + e = find_by_name(&world->aliases, name, 0, 0); + if (e) { + return e; + } + + return ecs_lookup_child(world, 0, name); +} + +ecs_entity_t ecs_lookup_symbol( + const ecs_world_t *world, + const char *name, + bool lookup_as_path) +{ + if (!name) { + return 0; } - if (pNtSetTimerResolution(resolution, 1, ¤t)) { - /* Try setting a lower resolution */ - resolution *= 2; - if(pNtSetTimerResolution(resolution, 1, ¤t)) return; + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + world = ecs_get_world(world); + + ecs_entity_t e = find_by_name(&world->symbols, name, 0, 0); + if (e) { + return e; } - win32_current_resolution = resolution; -} + if (lookup_as_path) { + return ecs_lookup_fullpath(world, name); + } -#else -void flecs_increase_timer_resolution(bool enable) -{ - (void)enable; - return; + return 0; } -#endif -static -ecs_vector_t* sort_and_dedup( - ecs_vector_t *result) +ecs_entity_t ecs_lookup_path_w_sep( + const ecs_world_t *world, + ecs_entity_t parent, + const char *path, + const char *sep, + const char *prefix, + bool recursive) { - /* Sort vector */ - ecs_vector_sort(result, ecs_id_t, flecs_entity_compare_qsort); - - /* Ensure vector doesn't contain duplicates */ - ecs_id_t *ids = ecs_vector_first(result, ecs_id_t); - int32_t i, offset = 0, count = ecs_vector_count(result); + if (!path) { + return 0; + } - for (i = 0; i < count; i ++) { - if (i && ids[i] == ids[i - 1]) { - offset ++; - } + if (!sep) { + sep = "."; + } - if (i + offset >= count) { - break; - } + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + world = ecs_get_world(world); - ids[i] = ids[i + offset]; + ecs_entity_t e = get_builtin(path); + if (e) { + return e; } - ecs_vector_set_count(&result, ecs_id_t, i - offset); - - return result; -} + e = find_by_name(&world->aliases, path, 0, 0); + if (e) { + return e; + } -/** Parse callback that adds type to type identifier */ -static -ecs_vector_t* expr_to_ids( - ecs_world_t *world, - const char *name, - const char *expr) -{ -#ifdef FLECS_PARSER - ecs_vector_t *result = NULL; - const char *ptr = expr; - ecs_term_t term = {0}; + char buff[ECS_NAME_BUFFER_LENGTH]; + const char *ptr, *ptr_start; + char *elem = buff; + int32_t len, size = ECS_NAME_BUFFER_LENGTH; + ecs_entity_t cur; + bool core_searched = false; - if (!ptr) { - return NULL; + if (!sep) { + sep = "."; } - while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term))) { - if (term.name) { - ecs_parser_error(name, expr, (ptr - expr), - "column names not supported in type expression"); - goto error; - } + parent = get_parent_from_path(world, parent, &path, prefix, true); - if (term.oper != EcsAnd && term.oper != EcsAndFrom) { - ecs_parser_error(name, expr, (ptr - expr), - "operator other than AND not supported in type expression"); - goto error; - } +retry: + cur = parent; + ptr_start = ptr = path; - if (ecs_term_finalize(world, name, &term)) { - goto error; - } + while ((ptr = path_elem(ptr, sep, &len))) { + if (len < size) { + ecs_os_memcpy(elem, ptr_start, len); + } else { + if (size == ECS_NAME_BUFFER_LENGTH) { + elem = NULL; + } - if (term.subj.entity == 0) { - /* Empty term */ - goto done; + elem = ecs_os_realloc(elem, len + 1); + ecs_os_memcpy(elem, ptr_start, len); + size = len + 1; } - if (term.subj.set.mask != EcsSelf) { - ecs_parser_error(name, expr, (ptr - expr), - "source modifiers not supported for type expressions"); - goto error; - } + elem[len] = '\0'; + ptr_start = ptr; - if (term.subj.entity != EcsThis) { - ecs_parser_error(name, expr, (ptr - expr), - "subject other than this not supported in type expression"); - goto error; + cur = ecs_lookup_child(world, cur, elem); + if (!cur) { + goto tail; } + } - if (term.oper == EcsAndFrom) { - term.role = ECS_AND; +tail: + if (!cur && recursive) { + if (!core_searched) { + if (parent) { + parent = ecs_get_object(world, parent, EcsChildOf, 0); + } else { + parent = EcsFlecsCore; + core_searched = true; + } + goto retry; } - - ecs_id_t* elem = ecs_vector_add(&result, ecs_id_t); - *elem = term.id | term.role; - - ecs_term_fini(&term); } - result = sort_and_dedup(result); + if (elem != buff) { + ecs_os_free(elem); + } -done: - return result; -error: - ecs_term_fini(&term); - ecs_vector_free(result); - return NULL; -#else - (void)world; - (void)name; - (void)expr; - ecs_abort(ECS_UNSUPPORTED, "parser addon is not available"); - return NULL; -#endif + return cur; } -/* Create normalized type. A normalized type resolves all elements with an - * AND flag and appends them to the resulting type, where the default type - * maintains the original type hierarchy. */ -static -ecs_vector_t* ids_to_normalized_ids( +ecs_entity_t ecs_set_scope( ecs_world_t *world, - ecs_vector_t *ids) + ecs_entity_t scope) { - ecs_vector_t *result = NULL; - - ecs_entity_t *array = ecs_vector_first(ids, ecs_id_t); - int32_t i, count = ecs_vector_count(ids); - - for (i = 0; i < count; i ++) { - ecs_entity_t e = array[i]; - if (ECS_HAS_ROLE(e, AND)) { - ecs_entity_t entity = ECS_PAIR_OBJECT(e); + ecs_stage_t *stage = flecs_stage_from_world(&world); - const EcsType *type_ptr = ecs_get(world, entity, EcsType); - ecs_assert(type_ptr != NULL, ECS_INVALID_PARAMETER, - "flag must be applied to type"); + ecs_entity_t cur = stage->scope; + stage->scope = scope; - ecs_vector_each(type_ptr->normalized, ecs_id_t, c_ptr, { - ecs_entity_t *el = ecs_vector_add(&result, ecs_id_t); - *el = *c_ptr; - }) - } else { - ecs_entity_t *el = ecs_vector_add(&result, ecs_id_t); - *el = e; - } + if (scope) { + ecs_id_t id = ecs_pair(EcsChildOf, scope); + stage->scope_table = flecs_table_traverse_add( + world, &world->store.root, &id, NULL); + } else { + stage->scope_table = &world->store.root; } - return sort_and_dedup(result); + return cur; } -static -ecs_table_t* table_from_ids( +ecs_entity_t ecs_get_scope( + const ecs_world_t *world) +{ + const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); + return stage->scope; +} + +const char* ecs_set_name_prefix( ecs_world_t *world, - ecs_vector_t *ids) + const char *prefix) { - ecs_ids_t ids_array = flecs_type_to_ids(ids); - ecs_table_t *result = flecs_table_find_or_create(world, &ids_array); - return result; + ecs_poly_assert(world, ecs_world_t); + + const char *old_prefix = world->name_prefix; + world->name_prefix = prefix; + return old_prefix; } -/* If a name prefix is set with ecs_set_name_prefix, check if the entity name - * has the prefix, and if so remove it. This enables using prefixed names in C - * for components / systems while storing a canonical / language independent - * identifier. */ -const char* flecs_name_from_symbol( +ecs_entity_t ecs_add_path_w_sep( ecs_world_t *world, - const char *type_name) + ecs_entity_t entity, + ecs_entity_t parent, + const char *path, + const char *sep, + const char *prefix) { - const char *prefix = world->name_prefix; - if (type_name && prefix) { - ecs_size_t len = ecs_os_strlen(prefix); - if (!ecs_os_strncmp(type_name, prefix, len) && - (isupper(type_name[len]) || type_name[len] == '_')) - { - if (type_name[len] == '_') { - return type_name + len + 1; - } else { - return type_name + len; + ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); + + if (!sep) { + sep = "."; + } + + if (!path) { + if (!entity) { + entity = ecs_new_id(world); + } + + if (parent) { + ecs_add_pair(world, entity, EcsChildOf, entity); + } + + return entity; + } + + char buff[ECS_NAME_BUFFER_LENGTH]; + const char *ptr = path; + const char *ptr_start = path; + char *elem = buff; + int32_t len, size = ECS_NAME_BUFFER_LENGTH; + + parent = get_parent_from_path(world, parent, &path, prefix, entity == 0); + + ecs_entity_t cur = parent; + + char *name = NULL; + + while ((ptr = path_elem(ptr, sep, &len))) { + if (len < size) { + ecs_os_memcpy(elem, ptr_start, len); + } else { + if (size == ECS_NAME_BUFFER_LENGTH) { + elem = NULL; + } + + elem = ecs_os_realloc(elem, len + 1); + ecs_os_memcpy(elem, ptr_start, len); + size = len + 1; + } + + elem[len] = '\0'; + ptr_start = ptr; + + ecs_entity_t e = ecs_lookup_child(world, cur, elem); + if (!e) { + if (name) { + ecs_os_free(name); + } + + name = ecs_os_strdup(elem); + + /* If this is the last entity in the path, use the provided id */ + bool last_elem = false; + if (!path_elem(ptr, sep, NULL)) { + e = entity; + last_elem = true; + } + + if (!e) { + if (last_elem) { + ecs_entity_t prev = ecs_set_scope(world, 0); + e = ecs_new(world, 0); + ecs_set_scope(world, prev); + } else { + e = ecs_new_id(world); + } + } + + ecs_set_name(world, e, name); + + if (cur) { + ecs_add_pair(world, e, EcsChildOf, cur); } } + + cur = e; } - return type_name; -} + if (entity && (cur != entity)) { + if (name) { + ecs_os_free(name); + } -/* -- Public functions -- */ + name = ecs_os_strdup(elem); -ecs_type_t ecs_type_from_str( - ecs_world_t *world, - const char *expr) -{ - ecs_vector_t *ids = expr_to_ids(world, NULL, expr); - if (!ids) { - return NULL; + ecs_set_name(world, entity, name); } - ecs_vector_t *normalized_ids = ids_to_normalized_ids(world, ids); - ecs_vector_free(ids); + if (name) { + ecs_os_free(name); + } - ecs_table_t *table = table_from_ids(world, normalized_ids); - ecs_vector_free(normalized_ids); + if (elem != buff) { + ecs_os_free(elem); + } - return table->type; + return cur; +} + +ecs_entity_t ecs_new_from_path_w_sep( + ecs_world_t *world, + ecs_entity_t parent, + const char *path, + const char *sep, + const char *prefix) +{ + if (!sep) { + sep = "."; + } + + return ecs_add_path_w_sep(world, 0, parent, path, sep, prefix); } -ecs_table_t* ecs_table_from_str( +void ecs_use( ecs_world_t *world, - const char *expr) + ecs_entity_t entity, + const char *name) { - ecs_poly_assert(world, ecs_world_t); - - ecs_vector_t *ids = expr_to_ids(world, NULL, expr); - if (!ids) { - return NULL; - } - - ecs_table_t *result = table_from_ids(world, ids); - ecs_vector_free(ids); - - return result; + register_by_name(&world->aliases, entity, name, 0, 0); } /* Component lifecycle actions for EcsIdentifier */ @@ -37743,684 +37813,631 @@ void flecs_bootstrap( ecs_log_pop(); } - -#define ECS_NAME_BUFFER_LENGTH (64) +static +ecs_defer_op_t* new_defer_op(ecs_stage_t *stage) { + ecs_defer_op_t *result = ecs_vector_add(&stage->defer_queue, ecs_defer_op_t); + ecs_os_memset(result, 0, ECS_SIZEOF(ecs_defer_op_t)); + return result; +} static -bool path_append( - const ecs_world_t *world, - ecs_entity_t parent, - ecs_entity_t child, - const char *sep, - const char *prefix, - ecs_strbuf_t *buf) +bool defer_add_remove( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_defer_op_kind_t op_kind, + ecs_entity_t entity, + ecs_id_t id) { - ecs_poly_assert(world, ecs_world_t); + if (stage->defer) { + if (!id) { + return true; + } - ecs_entity_t cur = 0; - char buff[22]; - const char *name; + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = op_kind; + op->id = id; + op->is._1.entity = entity; - if (ecs_is_valid(world, child)) { - cur = ecs_get_object(world, child, EcsChildOf, 0); - if (cur) { - if (cur != parent && cur != EcsFlecsCore) { - path_append(world, parent, cur, sep, prefix, buf); - ecs_strbuf_appendstr(buf, sep); - } - } else if (prefix) { - ecs_strbuf_appendstr(buf, prefix); + if (op_kind == EcsOpNew) { + world->new_count ++; + } else if (op_kind == EcsOpAdd) { + world->add_count ++; + } else if (op_kind == EcsOpRemove) { + world->remove_count ++; } - name = ecs_get_name(world, child); - if (!name || !ecs_os_strlen(name)) { - ecs_os_sprintf(buff, "%u", (uint32_t)child); - name = buff; - } + return true; } else { - ecs_os_sprintf(buff, "%u", (uint32_t)child); - name = buff; - } - - ecs_strbuf_appendstr(buf, name); - - return cur != 0; -} - -ecs_hashed_string_t ecs_get_hashed_string( - const char *name, - ecs_size_t length, - uint64_t hash) -{ - ecs_assert(!length || length == ecs_os_strlen(name), - ECS_INTERNAL_ERROR, NULL); - - if (!length) { - length = ecs_os_strlen(name); - } - - ecs_assert(!hash || hash == flecs_hash(name, length), - ECS_INTERNAL_ERROR, NULL); - - if (!hash) { - hash = flecs_hash(name, length); + stage->defer ++; } - - return (ecs_hashed_string_t) { - .value = (char*)name, - .length = length, - .hash = hash - }; + + return false; } static -ecs_entity_t find_by_name( - const ecs_hashmap_t *map, - const char *name, - ecs_size_t length, - uint64_t hash) +void merge_stages( + ecs_world_t *world, + bool force_merge) { - ecs_hashed_string_t key = ecs_get_hashed_string(name, length, hash); + bool is_stage = ecs_poly_is(world, ecs_stage_t); + ecs_stage_t *stage = flecs_stage_from_world(&world); - ecs_entity_t *e = flecs_hashmap_get(*map, &key, ecs_entity_t); + bool measure_frame_time = world->measure_frame_time; - if (!e) { - return 0; + ecs_time_t t_start; + if (measure_frame_time) { + ecs_os_get_time(&t_start); } - return *e; -} - -static -void register_by_name( - ecs_hashmap_t *map, - ecs_entity_t entity, - const char *name, - ecs_size_t length, - uint64_t hash) -{ - ecs_assert(entity != 0, ECS_INVALID_PARAMETER, NULL); - ecs_assert(name != NULL, ECS_INVALID_PARAMETER, NULL); - - ecs_hashed_string_t key = ecs_get_hashed_string(name, length, hash); - - ecs_entity_t existing = find_by_name(map, name, key.length, key.hash); - if (existing) { - if (existing != entity) { - ecs_abort(ECS_ALREADY_DEFINED, - "conflicting entity registered with name '%s'", name); + if (is_stage) { + /* Check for consistency if force_merge is enabled. In practice this + * function will never get called with force_merge disabled for just + * a single stage. */ + if (force_merge || stage->auto_merge) { + ecs_defer_end((ecs_world_t*)stage); } } else { - key.value = ecs_os_strdup(key.value); + /* Merge stages. Only merge if the stage has auto_merging turned on, or + * if this is a forced merge (like when ecs_merge is called) */ + int32_t i, count = ecs_get_stage_count(world); + for (i = 0; i < count; i ++) { + ecs_stage_t *s = (ecs_stage_t*)ecs_get_stage(world, i); + ecs_poly_assert(s, ecs_stage_t); + if (force_merge || s->auto_merge) { + ecs_defer_end((ecs_world_t*)s); + } + } } - flecs_hashmap_result_t hmr = flecs_hashmap_ensure( - *map, &key, ecs_entity_t); - - *((ecs_entity_t*)hmr.value) = entity; -} + flecs_eval_component_monitors(world); -static -bool is_number( - const char *name) -{ - ecs_assert(name != NULL, ECS_INTERNAL_ERROR, NULL); - - if (!isdigit(name[0])) { - return false; + if (measure_frame_time) { + world->stats.merge_time_total += + (FLECS_FLOAT)ecs_time_measure(&t_start); } - ecs_size_t i, length = ecs_os_strlen(name); - for (i = 1; i < length; i ++) { - char ch = name[i]; + world->stats.merge_count_total ++; - if (!isdigit(ch)) { - break; - } + /* If stage is asynchronous, deferring is always enabled */ + if (stage->asynchronous) { + ecs_defer_begin((ecs_world_t*)stage); } - - return i >= length; } -static -ecs_entity_t name_to_id( - const ecs_world_t *world, - const char *name) +static +void do_auto_merge( + ecs_world_t *world) { - long int result = atol(name); - ecs_assert(result >= 0, ECS_INTERNAL_ERROR, NULL); - ecs_entity_t alive = ecs_get_alive(world, (ecs_entity_t)result); - if (alive) { - return alive; - } else { - return (ecs_entity_t)result; - } + merge_stages(world, false); } static -ecs_entity_t get_builtin( - const char *name) +void do_manual_merge( + ecs_world_t *world) { - if (name[0] == '.' && name[1] == '\0') { - return EcsThis; - } else if (name[0] == '*' && name[1] == '\0') { - return EcsWildcard; - } - - return 0; + merge_stages(world, true); } -static -bool is_sep( - const char **ptr, - const char *sep) +bool flecs_defer_none( + ecs_world_t *world, + ecs_stage_t *stage) { - ecs_size_t len = ecs_os_strlen(sep); + (void)world; + return (++ stage->defer) == 1; +} - if (!ecs_os_strncmp(*ptr, sep, len)) { - *ptr += len; +bool flecs_defer_modified( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity, + ecs_id_t id) +{ + (void)world; + if (stage->defer) { + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = EcsOpModified; + op->id = id; + op->is._1.entity = entity; return true; } else { - return false; + stage->defer ++; } + + return false; } -static -const char* path_elem( - const char *path, - const char *sep, - int32_t *len) -{ - const char *ptr; - char ch; - int32_t template_nesting = 0; - int32_t count = 0; - - for (ptr = path; (ch = *ptr); ptr ++) { - if (ch == '<') { - template_nesting ++; - } else if (ch == '>') { - template_nesting --; - } - - ecs_assert(template_nesting >= 0, ECS_INVALID_PARAMETER, path); - - if (!template_nesting && is_sep(&ptr, sep)) { - break; - } - - count ++; - } - - if (len) { - *len = count; - } - - if (count) { - return ptr; +bool flecs_defer_clone( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity, + ecs_entity_t src, + bool clone_value) +{ + (void)world; + if (stage->defer) { + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = EcsOpClone; + op->id = src; + op->is._1.entity = entity; + op->is._1.clone_value = clone_value; + return true; } else { - return NULL; + stage->defer ++; } + + return false; } -static -ecs_entity_t get_parent_from_path( - const ecs_world_t *world, - ecs_entity_t parent, - const char **path_ptr, - const char *prefix, - bool new_entity) +bool flecs_defer_delete( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity) { - bool start_from_root = false; - const char *path = *path_ptr; - - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - - if (prefix) { - ecs_size_t len = ecs_os_strlen(prefix); - if (!ecs_os_strncmp(path, prefix, len)) { - path += len; - parent = 0; - start_from_root = true; - } - } - - if (!start_from_root && !parent && new_entity) { - parent = ecs_get_scope(world); - } - - *path_ptr = path; - - return parent; -} - -static -void on_set_symbol(ecs_iter_t *it) { - EcsIdentifier *n = ecs_term(it, EcsIdentifier, 1); - ecs_world_t *world = it->world; - - int i; - for (i = 0; i < it->count; i ++) { - ecs_entity_t e = it->entities[i]; - register_by_name( - &world->symbols, e, n[i].value, n[i].length, n[i].hash); + (void)world; + if (stage->defer) { + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = EcsOpDelete; + op->is._1.entity = entity; + world->delete_count ++; + return true; + } else { + stage->defer ++; } + return false; } -static -uint64_t string_hash( - const void *ptr) +bool flecs_defer_clear( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity) { - const ecs_hashed_string_t *str = ptr; - ecs_assert(str->hash != 0, ECS_INVALID_PARAMETER, NULL); - return str->hash; + (void)world; + if (stage->defer) { + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = EcsOpClear; + op->is._1.entity = entity; + world->clear_count ++; + return true; + } else { + stage->defer ++; + } + return false; } -static -int string_compare( - const void *ptr1, - const void *ptr2) +bool flecs_defer_on_delete_action( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_id_t id, + ecs_entity_t action) { - const ecs_hashed_string_t *str1 = ptr1; - const ecs_hashed_string_t *str2 = ptr2; - ecs_size_t len1 = str1->length; - ecs_size_t len2 = str2->length; - if (len1 != len2) { - return (len1 > len2) - (len1 < len2); + (void)world; + if (stage->defer) { + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = EcsOpOnDeleteAction; + op->id = id; + op->is._1.entity = action; + world->clear_count ++; + return true; + } else { + stage->defer ++; } - - return ecs_os_memcmp(str1->value, str2->value, len1); -} - -ecs_hashmap_t _flecs_string_hashmap_new(ecs_size_t size) { - return _flecs_hashmap_new(ECS_SIZEOF(ecs_hashed_string_t), size, - string_hash, - string_compare); -} - -void flecs_bootstrap_hierarchy(ecs_world_t *world) { - ecs_trigger_init(world, &(ecs_trigger_desc_t){ - .term = {.id = ecs_pair(ecs_id(EcsIdentifier), EcsSymbol)}, - .callback = on_set_symbol, - .events = {EcsOnSet} - }); + return false; } - -/* Public functions */ - -char* ecs_get_path_w_sep( - const ecs_world_t *world, - ecs_entity_t parent, - ecs_entity_t child, - const char *sep, - const char *prefix) +bool flecs_defer_enable( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity, + ecs_id_t id, + bool enable) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - world = ecs_get_world(world); - - if (!sep) { - sep = "."; - } - - ecs_strbuf_t buf = ECS_STRBUF_INIT; - - if (!child || parent != child) { - path_append(world, parent, child, sep, prefix, &buf); + (void)world; + if (stage->defer) { + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = enable ? EcsOpEnable : EcsOpDisable; + op->is._1.entity = entity; + op->id = id; + return true; } else { - ecs_strbuf_appendstr(&buf, ""); + stage->defer ++; } - - return ecs_strbuf_get(&buf); + return false; } -ecs_entity_t ecs_lookup_child( - const ecs_world_t *world, - ecs_entity_t parent, - const char *name) +bool flecs_defer_bulk_new( + ecs_world_t *world, + ecs_stage_t *stage, + int32_t count, + ecs_id_t id, + const ecs_entity_t **ids_out) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - - if (is_number(name)) { - return name_to_id(world, name); - } - - ecs_filter_t f; - int ret = ecs_filter_init(world, &f, &(ecs_filter_desc_t) { - .terms = { - { .id = ecs_pair( ecs_id(EcsIdentifier), EcsName) }, - { .id = ecs_pair(EcsChildOf, parent) }, - { .id = EcsDisabled, .oper = EcsOptional }, - { .id = EcsPrefab, .oper = EcsOptional } - } - }); - - ecs_assert(ret == 0, ECS_INTERNAL_ERROR, NULL); - (void)ret; + if (stage->defer) { + ecs_entity_t *ids = ecs_os_malloc(count * ECS_SIZEOF(ecs_entity_t)); + world->bulk_new_count ++; - ecs_iter_t it = ecs_filter_iter(world, &f); - while (ecs_filter_next(&it)) { - EcsIdentifier *ids = ecs_term(&it, EcsIdentifier, 1); + /* Use ecs_new_id as this is thread safe */ int i; - for (i = 0; i < it.count; i ++) { - char *cur_name = ids[i].value; - if (cur_name && !ecs_os_strcmp(cur_name, name)) { - ecs_filter_fini(&f); - return it.entities[i]; - } + for (i = 0; i < count; i ++) { + ids[i] = ecs_new_id(world); } + + *ids_out = ids; + + /* Store data in op */ + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = EcsOpBulkNew; + op->id = id; + op->is._n.entities = ids; + op->is._n.count = count; + + return true; + } else { + stage->defer ++; } - ecs_filter_fini(&f); - return 0; + return false; +} + +bool flecs_defer_new( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity, + ecs_id_t id) +{ + return defer_add_remove(world, stage, EcsOpNew, entity, id); } -ecs_entity_t ecs_lookup( - const ecs_world_t *world, - const char *name) +bool flecs_defer_add( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity, + ecs_id_t id) { - if (!name) { - return 0; - } + return defer_add_remove(world, stage, EcsOpAdd, entity, id); +} - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - world = ecs_get_world(world); +bool flecs_defer_remove( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_entity_t entity, + ecs_id_t id) +{ + return defer_add_remove(world, stage, EcsOpRemove, entity, id); +} - ecs_entity_t e = get_builtin(name); - if (e) { - return e; - } +bool flecs_defer_set( + ecs_world_t *world, + ecs_stage_t *stage, + ecs_defer_op_kind_t op_kind, + ecs_entity_t entity, + ecs_id_t id, + ecs_size_t size, + const void *value, + void **value_out, + bool *is_added) +{ + if (stage->defer) { + world->set_count ++; + if (!size) { + const EcsComponent *cptr = flecs_component_from_id(world, id); + ecs_assert(cptr != NULL, ECS_INVALID_PARAMETER, NULL); + size = cptr->size; + } - if (is_number(name)) { - return name_to_id(world, name); - } + ecs_defer_op_t *op = new_defer_op(stage); + op->kind = op_kind; + op->id = id; + op->is._1.entity = entity; + op->is._1.size = size; + op->is._1.value = ecs_os_malloc(size); - e = find_by_name(&world->aliases, name, 0, 0); - if (e) { - return e; - } - - return ecs_lookup_child(world, 0, name); -} + if (!value) { + value = ecs_get_id(world, entity, id); + if (is_added) { + *is_added = value == NULL; + } + } -ecs_entity_t ecs_lookup_symbol( - const ecs_world_t *world, - const char *name, - bool lookup_as_path) -{ - if (!name) { - return 0; - } + const ecs_type_info_t *c_info = NULL; + ecs_entity_t real_id = ecs_get_typeid(world, id); + if (real_id) { + c_info = flecs_get_c_info(world, real_id); + } - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - world = ecs_get_world(world); + if (value) { + ecs_copy_ctor_t copy; + if (c_info && (copy = c_info->lifecycle.copy_ctor)) { + copy(world, id, &c_info->lifecycle, &entity, &entity, + op->is._1.value, value, flecs_to_size_t(size), 1, + c_info->lifecycle.ctx); + } else { + ecs_os_memcpy(op->is._1.value, value, size); + } + } else { + ecs_xtor_t ctor; + if (c_info && (ctor = c_info->lifecycle.ctor)) { + ctor(world, id, &entity, op->is._1.value, + flecs_to_size_t(size), 1, c_info->lifecycle.ctx); + } + } - ecs_entity_t e = find_by_name(&world->symbols, name, 0, 0); - if (e) { - return e; - } + if (value_out) { + *value_out = op->is._1.value; + } - if (lookup_as_path) { - return ecs_lookup_fullpath(world, name); + return true; + } else { + stage->defer ++; } - return 0; + return false; } -ecs_entity_t ecs_lookup_path_w_sep( - const ecs_world_t *world, - ecs_entity_t parent, - const char *path, - const char *sep, - const char *prefix, - bool recursive) +void flecs_stage_merge_post_frame( + ecs_world_t *world, + ecs_stage_t *stage) { - if (!path) { - return 0; - } + /* Execute post frame actions */ + ecs_vector_each(stage->post_frame_actions, ecs_action_elem_t, action, { + action->action(world, action->ctx); + }); - if (!sep) { - sep = "."; - } + ecs_vector_free(stage->post_frame_actions); + stage->post_frame_actions = NULL; +} - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - world = ecs_get_world(world); +void flecs_stage_init( + ecs_world_t *world, + ecs_stage_t *stage) +{ + ecs_poly_assert(world, ecs_world_t); - ecs_entity_t e = get_builtin(path); - if (e) { - return e; - } + ecs_poly_init(stage, ecs_stage_t); - e = find_by_name(&world->aliases, path, 0, 0); - if (e) { - return e; - } + stage->world = world; + stage->thread_ctx = world; + stage->auto_merge = true; + stage->asynchronous = false; +} - char buff[ECS_NAME_BUFFER_LENGTH]; - const char *ptr, *ptr_start; - char *elem = buff; - int32_t len, size = ECS_NAME_BUFFER_LENGTH; - ecs_entity_t cur; - bool core_searched = false; +void flecs_stage_deinit( + ecs_world_t *world, + ecs_stage_t *stage) +{ + (void)world; + ecs_poly_assert(world, ecs_world_t); + ecs_poly_assert(stage, ecs_stage_t); - if (!sep) { - sep = "."; - } + /* Make sure stage has no unmerged data */ + ecs_assert(ecs_vector_count(stage->defer_queue) == 0, + ECS_INVALID_PARAMETER, NULL); - parent = get_parent_from_path(world, parent, &path, prefix, true); + ecs_poly_fini(stage, ecs_stage_t); -retry: - cur = parent; - ptr_start = ptr = path; + ecs_vector_free(stage->defer_queue); +} - while ((ptr = path_elem(ptr, sep, &len))) { - if (len < size) { - ecs_os_memcpy(elem, ptr_start, len); - } else { - if (size == ECS_NAME_BUFFER_LENGTH) { - elem = NULL; - } +void ecs_set_stages( + ecs_world_t *world, + int32_t stage_count) +{ + ecs_poly_assert(world, ecs_world_t); - elem = ecs_os_realloc(elem, len + 1); - ecs_os_memcpy(elem, ptr_start, len); - size = len + 1; - } + ecs_stage_t *stages; + int32_t i, count = ecs_vector_count(world->worker_stages); - elem[len] = '\0'; - ptr_start = ptr; + if (count && count != stage_count) { + stages = ecs_vector_first(world->worker_stages, ecs_stage_t); - cur = ecs_lookup_child(world, cur, elem); - if (!cur) { - goto tail; + for (i = 0; i < count; i ++) { + /* If stage contains a thread handle, ecs_set_threads was used to + * create the stages. ecs_set_threads and ecs_set_stages should not + * be mixed. */ + ecs_poly_assert(&stages[i], ecs_stage_t); + ecs_assert(stages[i].thread == 0, ECS_INVALID_OPERATION, NULL); + flecs_stage_deinit(world, &stages[i]); } + + ecs_vector_free(world->worker_stages); } + + if (stage_count) { + world->worker_stages = ecs_vector_new(ecs_stage_t, stage_count); -tail: - if (!cur && recursive) { - if (!core_searched) { - if (parent) { - parent = ecs_get_object(world, parent, EcsChildOf, 0); - } else { - parent = EcsFlecsCore; - core_searched = true; - } - goto retry; + for (i = 0; i < stage_count; i ++) { + ecs_stage_t *stage = ecs_vector_add( + &world->worker_stages, ecs_stage_t); + flecs_stage_init(world, stage); + stage->id = 1 + i; /* 0 is reserved for main/temp stage */ + + /* Set thread_ctx to stage, as this stage might be used in a + * multithreaded context */ + stage->thread_ctx = (ecs_world_t*)stage; } + } else { + /* Set to NULL to prevent double frees */ + world->worker_stages = NULL; } - if (elem != buff) { - ecs_os_free(elem); + /* Regardless of whether the stage was just initialized or not, when the + * ecs_set_stages function is called, all stages inherit the auto_merge + * property from the world */ + for (i = 0; i < stage_count; i ++) { + ecs_stage_t *stage = (ecs_stage_t*)ecs_get_stage(world, i); + stage->auto_merge = world->stage.auto_merge; } +} - return cur; +int32_t ecs_get_stage_count( + const ecs_world_t *world) +{ + world = ecs_get_world(world); + return ecs_vector_count(world->worker_stages); } -ecs_entity_t ecs_set_scope( - ecs_world_t *world, - ecs_entity_t scope) +int32_t ecs_get_stage_id( + const ecs_world_t *world) { - ecs_stage_t *stage = flecs_stage_from_world(&world); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_entity_t cur = stage->scope; - stage->scope = scope; + if (ecs_poly_is(world, ecs_stage_t)) { + ecs_stage_t *stage = (ecs_stage_t*)world; - if (scope) { - ecs_id_t id = ecs_pair(EcsChildOf, scope); - stage->scope_table = flecs_table_traverse_add( - world, &world->store.root, &id, NULL); + /* Index 0 is reserved for main stage */ + return stage->id - 1; + } else if (ecs_poly_is(world, ecs_world_t)) { + return 0; } else { - stage->scope_table = &world->store.root; + ecs_abort(ECS_INTERNAL_ERROR, NULL); } - - return cur; -} - -ecs_entity_t ecs_get_scope( - const ecs_world_t *world) -{ - const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); - return stage->scope; } -const char* ecs_set_name_prefix( - ecs_world_t *world, - const char *prefix) +ecs_world_t* ecs_get_stage( + const ecs_world_t *world, + int32_t stage_id) { ecs_poly_assert(world, ecs_world_t); + ecs_assert(ecs_vector_count(world->worker_stages) > stage_id, + ECS_INVALID_PARAMETER, NULL); - const char *old_prefix = world->name_prefix; - world->name_prefix = prefix; - return old_prefix; + return (ecs_world_t*)ecs_vector_get( + world->worker_stages, ecs_stage_t, stage_id); } -ecs_entity_t ecs_add_path_w_sep( - ecs_world_t *world, - ecs_entity_t entity, - ecs_entity_t parent, - const char *path, - const char *sep, - const char *prefix) +bool ecs_staging_begin( + ecs_world_t *world) { - ecs_assert(world != NULL, ECS_INTERNAL_ERROR, NULL); - - if (!sep) { - sep = "."; - } - - if (!path) { - if (!entity) { - entity = ecs_new_id(world); - } - - if (parent) { - ecs_add_pair(world, entity, EcsChildOf, entity); - } + ecs_poly_assert(world, ecs_world_t); - return entity; + int32_t i, count = ecs_get_stage_count(world); + for (i = 0; i < count; i ++) { + ecs_defer_begin(ecs_get_stage(world, i)); } - char buff[ECS_NAME_BUFFER_LENGTH]; - const char *ptr = path; - const char *ptr_start = path; - char *elem = buff; - int32_t len, size = ECS_NAME_BUFFER_LENGTH; - - parent = get_parent_from_path(world, parent, &path, prefix, entity == 0); + bool is_readonly = world->is_readonly; - ecs_entity_t cur = parent; + /* From this point on, the world is "locked" for mutations, and it is only + * allowed to enqueue commands from stages */ + world->is_readonly = true; - char *name = NULL; + return is_readonly; +} - while ((ptr = path_elem(ptr, sep, &len))) { - if (len < size) { - ecs_os_memcpy(elem, ptr_start, len); - } else { - if (size == ECS_NAME_BUFFER_LENGTH) { - elem = NULL; - } +void ecs_staging_end( + ecs_world_t *world) +{ + ecs_poly_assert(world, ecs_world_t); + ecs_assert(world->is_readonly == true, ECS_INVALID_OPERATION, NULL); - elem = ecs_os_realloc(elem, len + 1); - ecs_os_memcpy(elem, ptr_start, len); - size = len + 1; - } + /* After this it is safe again to mutate the world directly */ + world->is_readonly = false; - elem[len] = '\0'; - ptr_start = ptr; + do_auto_merge(world); +} - ecs_entity_t e = ecs_lookup_child(world, cur, elem); - if (!e) { - if (name) { - ecs_os_free(name); - } +void ecs_merge( + ecs_world_t *world) +{ + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + ecs_assert(ecs_poly_is(world, ecs_world_t) || + ecs_poly_is(world, ecs_stage_t), ECS_INVALID_PARAMETER, NULL); + do_manual_merge(world); +} - name = ecs_os_strdup(elem); +void ecs_set_automerge( + ecs_world_t *world, + bool auto_merge) +{ + /* If a world is provided, set auto_merge globally for the world. This + * doesn't actually do anything (the main stage never merges) but it serves + * as the default for when stages are created. */ + if (ecs_poly_is(world, ecs_world_t)) { + world->stage.auto_merge = auto_merge; - /* If this is the last entity in the path, use the provided id */ - bool last_elem = false; - if (!path_elem(ptr, sep, NULL)) { - e = entity; - last_elem = true; - } + /* Propagate change to all stages */ + int i, stage_count = ecs_get_stage_count(world); + for (i = 0; i < stage_count; i ++) { + ecs_stage_t *stage = (ecs_stage_t*)ecs_get_stage(world, i); + stage->auto_merge = auto_merge; + } - if (!e) { - if (last_elem) { - ecs_entity_t prev = ecs_set_scope(world, 0); - e = ecs_new(world, 0); - ecs_set_scope(world, prev); - } else { - e = ecs_new_id(world); - } - } + /* If a stage is provided, override the auto_merge value for the individual + * stage. This allows an application to control per-stage which stage should + * be automatically merged and which one shouldn't */ + } else { + ecs_poly_assert(world, ecs_stage_t); + ecs_stage_t *stage = (ecs_stage_t*)world; + stage->auto_merge = auto_merge; + } +} - ecs_set_name(world, e, name); +bool ecs_stage_is_readonly( + const ecs_world_t *stage) +{ + const ecs_world_t *world = ecs_get_world(stage); - if (cur) { - ecs_add_pair(world, e, EcsChildOf, cur); - } + if (ecs_poly_is(stage, ecs_stage_t)) { + if (((ecs_stage_t*)stage)->asynchronous) { + return false; } - - cur = e; } - if (entity && (cur != entity)) { - if (name) { - ecs_os_free(name); + if (world->is_readonly) { + if (ecs_poly_is(stage, ecs_world_t)) { + return true; + } + } else { + if (ecs_poly_is(stage, ecs_stage_t)) { + return true; } + } - name = ecs_os_strdup(elem); + return false; +} - ecs_set_name(world, entity, name); - } +ecs_world_t* ecs_async_stage_new( + ecs_world_t *world) +{ + ecs_stage_t *stage = ecs_os_calloc(sizeof(ecs_stage_t)); + flecs_stage_init(world, stage); - if (name) { - ecs_os_free(name); - } + stage->id = -1; + stage->auto_merge = false; + stage->asynchronous = true; - if (elem != buff) { - ecs_os_free(elem); - } + ecs_defer_begin((ecs_world_t*)stage); - return cur; + return (ecs_world_t*)stage; } -ecs_entity_t ecs_new_from_path_w_sep( - ecs_world_t *world, - ecs_entity_t parent, - const char *path, - const char *sep, - const char *prefix) +void ecs_async_stage_free( + ecs_world_t *world) { - if (!sep) { - sep = "."; + ecs_poly_assert(world, ecs_stage_t); + ecs_stage_t *stage = (ecs_stage_t*)world; + ecs_assert(stage->asynchronous == true, ECS_INVALID_PARAMETER, NULL); + flecs_stage_deinit(stage->world, stage); + ecs_os_free(stage); +} + +bool ecs_stage_is_async( + ecs_world_t *stage) +{ + if (!stage) { + return false; + } + + if (!ecs_poly_is(stage, ecs_stage_t)) { + return false; } - return ecs_add_path_w_sep(world, 0, parent, path, sep, prefix); + return ((ecs_stage_t*)stage)->asynchronous; } -void ecs_use( - ecs_world_t *world, - ecs_entity_t entity, - const char *name) +bool ecs_is_deferred( + const ecs_world_t *world) { - register_by_name(&world->aliases, entity, name, 0, 0); + ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL); + const ecs_stage_t *stage = flecs_stage_from_readonly_world(world); + return stage->defer != 0; } diff --git a/src/world.c b/src/world.c index 092f976d86..dd73f68fcb 100644 --- a/src/world.c +++ b/src/world.c @@ -646,6 +646,18 @@ void default_move_ctor_w_dtor( callbacks->dtor(world, component, src_entity, src_ptr, size, count, ctx); } +static +void default_ctor_w_move_dtor( + ecs_world_t *world, ecs_entity_t component, + const EcsComponentLifecycle *callbacks, const ecs_entity_t *dst_entity, + const ecs_entity_t *src_entity, void *dst_ptr, void *src_ptr, size_t size, + int32_t count, void *ctx) +{ + callbacks->ctor(world, component, dst_entity, dst_ptr, size, count, ctx); + callbacks->move_dtor(world, component, callbacks, dst_entity, src_entity, + dst_ptr, src_ptr, size, count, ctx); +} + static void default_move( ecs_world_t *world, ecs_entity_t component, @@ -752,8 +764,13 @@ void ecs_set_component_actions_w_id( * that uses the move ctor vs. using a ctor+move */ c_info->lifecycle.ctor_move_dtor = default_move_ctor_w_dtor; + } else if (lifecycle->move_dtor) { + /* If no explicit move_dtor has been set, use callback + * that uses the move dtor vs. using a move+dtor */ + c_info->lifecycle.ctor_move_dtor = + default_ctor_w_move_dtor; } else { - /* If no explicit move_ctor has been set, use + /* If neither move_ctor nor move_dtor has been set, use * combination of ctor + move + dtor */ c_info->lifecycle.ctor_move_dtor = default_ctor_w_move_w_dtor; From 4a887edac1c286a73f2212ae133d42561c67d879 Mon Sep 17 00:00:00 2001 From: Joe Eli McIlvain Date: Fri, 29 Oct 2021 00:18:08 -0700 Subject: [PATCH 2/2] Switch from using move to move_dtor in assign_ptr_w_id function. See this discord conversation for more background: https://discord.com/channels/633826290415435777/633826290415435781/902342988158623744 --- flecs.c | 121 +++++++++++++++++++++++++++++++-- src/addons/pipeline/pipeline.c | 15 ++++ src/addons/system/system.c | 98 ++++++++++++++++++++++++++ src/entity.c | 8 +-- 4 files changed, 234 insertions(+), 8 deletions(-) diff --git a/flecs.c b/flecs.c index 0a8d240c3c..335cb0bef9 100644 --- a/flecs.c +++ b/flecs.c @@ -22379,6 +22379,103 @@ void sys_ctor_init_zero( memset(ptr, 0, size * (size_t)count); } +/* System move */ +static +void ecs_colsystem_move( + ecs_world_t *world, + ecs_entity_t component, + const ecs_entity_t *dst_entities, + const ecs_entity_t *src_entities, + void *dst_ptr, + void *src_ptr, + size_t size, + int32_t count, + void *ctx) +{ + (void)world; + (void)dst_entities; + (void)src_entities; + (void)component; + (void)ctx; + (void)size; + + EcsSystem *dst_data = dst_ptr; + EcsSystem *src_data = src_ptr; + + int i; + for (i = 0; i < count; i ++) { + EcsSystem *dst = &dst_data[i]; + EcsSystem *src = &src_data[i]; + // ecs_entity_t dst_e = dst_entities[i]; + // ecs_entity_t src_e = src_entities[i]; + + // if (!ecs_is_alive(world, dst_e)) { + // /* This can happen when a set is deferred while a system is being + // * cleaned up. The operation will be discarded, but the destructor + // * still needs to be invoked for the value */ + // continue; + // } + + // /* Invoke Deactivated action for active systems */ + // if (dst->query && dst->query != src->query && ecs_query_table_count(dst->query)) { + // invoke_status_action(world, dst_e, dst, EcsSystemDeactivated); + // } + + // /* Invoke Disabled action for enabled systems */ + // if (!ecs_has_id(world, dst_e, EcsDisabled)) { + // invoke_status_action(world, dst_e, dst, EcsSystemDisabled); + // } + + if (dst->ctx != src->ctx && dst->ctx_free) { + dst->ctx_free(dst->ctx); + } + + if (dst->status_ctx != src->status_ctx && dst->status_ctx_free) { + dst->status_ctx_free(dst->status_ctx); + } + + if (dst->binding_ctx != src->binding_ctx && dst->binding_ctx_free) { + dst->binding_ctx_free(dst->binding_ctx); + } + + if (dst->query && dst->query != src->query) { + ecs_query_fini(dst->query); + } + + dst->action = src->action; + dst->entity = src->entity; + dst->query = src->query; + dst->status_action = src->status_action; + dst->tick_source = src->tick_source; + dst->invoke_count = src->invoke_count; + dst->time_spent = src->time_spent; + dst->time_passed = src->time_passed; + dst->self = src->self; + dst->ctx = src->ctx; + dst->status_ctx = src->status_ctx; + dst->binding_ctx = src->binding_ctx; + dst->ctx_free = src->ctx_free; + dst->status_ctx_free = src->status_ctx_free; + dst->binding_ctx_free = src->binding_ctx_free; + + src->action = NULL; + src->entity = 0; + src->query = NULL; + src->status_action = NULL; + src->tick_source = 0; + src->invoke_count = 0; + src->time_spent = 0; + src->time_passed = 0; + src->self = 0; + src->ctx = NULL; + src->status_ctx = NULL; + src->binding_ctx = NULL; + src->ctx_free = NULL; + src->status_ctx_free = NULL; + src->binding_ctx_free = NULL; + } +} + /* System destructor */ static void ecs_colsystem_dtor( @@ -22626,6 +22723,7 @@ void FlecsSystemImport( ecs_set_component_actions_w_id(world, ecs_id(EcsSystem), &(EcsComponentLifecycle) { .ctor = sys_ctor_init_zero, + .move = ecs_colsystem_move, .dtor = ecs_colsystem_dtor }); @@ -22954,6 +23052,20 @@ static ECS_CTOR(EcsPipelineQuery, ptr, { memset(ptr, 0, _size); }) +static ECS_MOVE(EcsPipelineQuery, dst, src, { + ecs_vector_free(dst->ops); + + dst->query = src->query; + dst->build_query = src->build_query; + dst->match_count = src->match_count; + dst->ops = src->ops; + + src->query = NULL; + src->build_query = NULL; + src->match_count = 0; + src->ops = NULL; +}) + static ECS_DTOR(EcsPipelineQuery, ptr, { ecs_vector_free(ptr->ops); }) @@ -23661,6 +23773,7 @@ void FlecsPipelineImport( /* Set ctor and dtor for PipelineQuery */ ecs_set(world, ecs_id(EcsPipelineQuery), EcsComponentLifecycle, { .ctor = ecs_ctor(EcsPipelineQuery), + .move = ecs_move(EcsPipelineQuery), .dtor = ecs_dtor(EcsPipelineQuery) }); @@ -33928,10 +34041,10 @@ ecs_entity_t assign_ptr_w_id( const ecs_type_info_t *cdata = get_c_info(world, real_id); if (cdata) { if (is_move) { - ecs_move_t move = cdata->lifecycle.move; - if (move) { - move(world, real_id, &entity, &entity, dst, ptr, size, 1, - cdata->lifecycle.ctx); + ecs_move_ctor_t move_dtor = cdata->lifecycle.move_dtor; + if (move_dtor) { + move_dtor(world, real_id, &cdata->lifecycle, &entity, + &entity, dst, ptr, size, 1, cdata->lifecycle.ctx); } else { ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); } diff --git a/src/addons/pipeline/pipeline.c b/src/addons/pipeline/pipeline.c index f9e3d3249f..0a363cb128 100644 --- a/src/addons/pipeline/pipeline.c +++ b/src/addons/pipeline/pipeline.c @@ -8,6 +8,20 @@ static ECS_CTOR(EcsPipelineQuery, ptr, { memset(ptr, 0, _size); }) +static ECS_MOVE(EcsPipelineQuery, dst, src, { + ecs_vector_free(dst->ops); + + dst->query = src->query; + dst->build_query = src->build_query; + dst->match_count = src->match_count; + dst->ops = src->ops; + + src->query = NULL; + src->build_query = NULL; + src->match_count = 0; + src->ops = NULL; +}) + static ECS_DTOR(EcsPipelineQuery, ptr, { ecs_vector_free(ptr->ops); }) @@ -715,6 +729,7 @@ void FlecsPipelineImport( /* Set ctor and dtor for PipelineQuery */ ecs_set(world, ecs_id(EcsPipelineQuery), EcsComponentLifecycle, { .ctor = ecs_ctor(EcsPipelineQuery), + .move = ecs_move(EcsPipelineQuery), .dtor = ecs_dtor(EcsPipelineQuery) }); diff --git a/src/addons/system/system.c b/src/addons/system/system.c index 1a1866fceb..121814fe39 100644 --- a/src/addons/system/system.c +++ b/src/addons/system/system.c @@ -309,6 +309,103 @@ void sys_ctor_init_zero( memset(ptr, 0, size * (size_t)count); } +/* System move */ +static +void ecs_colsystem_move( + ecs_world_t *world, + ecs_entity_t component, + const ecs_entity_t *dst_entities, + const ecs_entity_t *src_entities, + void *dst_ptr, + void *src_ptr, + size_t size, + int32_t count, + void *ctx) +{ + (void)world; + (void)dst_entities; + (void)src_entities; + (void)component; + (void)ctx; + (void)size; + + EcsSystem *dst_data = dst_ptr; + EcsSystem *src_data = src_ptr; + + int i; + for (i = 0; i < count; i ++) { + EcsSystem *dst = &dst_data[i]; + EcsSystem *src = &src_data[i]; + // ecs_entity_t dst_e = dst_entities[i]; + // ecs_entity_t src_e = src_entities[i]; + + // if (!ecs_is_alive(world, dst_e)) { + // /* This can happen when a set is deferred while a system is being + // * cleaned up. The operation will be discarded, but the destructor + // * still needs to be invoked for the value */ + // continue; + // } + + // /* Invoke Deactivated action for active systems */ + // if (dst->query && dst->query != src->query && ecs_query_table_count(dst->query)) { + // invoke_status_action(world, dst_e, dst, EcsSystemDeactivated); + // } + + // /* Invoke Disabled action for enabled systems */ + // if (!ecs_has_id(world, dst_e, EcsDisabled)) { + // invoke_status_action(world, dst_e, dst, EcsSystemDisabled); + // } + + if (dst->ctx != src->ctx && dst->ctx_free) { + dst->ctx_free(dst->ctx); + } + + if (dst->status_ctx != src->status_ctx && dst->status_ctx_free) { + dst->status_ctx_free(dst->status_ctx); + } + + if (dst->binding_ctx != src->binding_ctx && dst->binding_ctx_free) { + dst->binding_ctx_free(dst->binding_ctx); + } + + if (dst->query && dst->query != src->query) { + ecs_query_fini(dst->query); + } + + dst->action = src->action; + dst->entity = src->entity; + dst->query = src->query; + dst->status_action = src->status_action; + dst->tick_source = src->tick_source; + dst->invoke_count = src->invoke_count; + dst->time_spent = src->time_spent; + dst->time_passed = src->time_passed; + dst->self = src->self; + dst->ctx = src->ctx; + dst->status_ctx = src->status_ctx; + dst->binding_ctx = src->binding_ctx; + dst->ctx_free = src->ctx_free; + dst->status_ctx_free = src->status_ctx_free; + dst->binding_ctx_free = src->binding_ctx_free; + + src->action = NULL; + src->entity = 0; + src->query = NULL; + src->status_action = NULL; + src->tick_source = 0; + src->invoke_count = 0; + src->time_spent = 0; + src->time_passed = 0; + src->self = 0; + src->ctx = NULL; + src->status_ctx = NULL; + src->binding_ctx = NULL; + src->ctx_free = NULL; + src->status_ctx_free = NULL; + src->binding_ctx_free = NULL; + } +} + /* System destructor */ static void ecs_colsystem_dtor( @@ -556,6 +653,7 @@ void FlecsSystemImport( ecs_set_component_actions_w_id(world, ecs_id(EcsSystem), &(EcsComponentLifecycle) { .ctor = sys_ctor_init_zero, + .move = ecs_colsystem_move, .dtor = ecs_colsystem_dtor }); diff --git a/src/entity.c b/src/entity.c index b52d51f460..cba512d228 100644 --- a/src/entity.c +++ b/src/entity.c @@ -2816,10 +2816,10 @@ ecs_entity_t assign_ptr_w_id( const ecs_type_info_t *cdata = get_c_info(world, real_id); if (cdata) { if (is_move) { - ecs_move_t move = cdata->lifecycle.move; - if (move) { - move(world, real_id, &entity, &entity, dst, ptr, size, 1, - cdata->lifecycle.ctx); + ecs_move_ctor_t move_dtor = cdata->lifecycle.move_dtor; + if (move_dtor) { + move_dtor(world, real_id, &cdata->lifecycle, &entity, + &entity, dst, ptr, size, 1, cdata->lifecycle.ctx); } else { ecs_os_memcpy(dst, ptr, flecs_from_size_t(size)); }