Add ENT_ARRAY_COMPACT(array_ptr) macro
Removes all dead references from the array. This operation may move elements around.
This commit is contained in:
parent
31ce18129a
commit
6671aa6ecd
2 changed files with 20 additions and 0 deletions
11
src/entity.c
11
src/entity.c
|
@ -228,3 +228,14 @@ void ent_hook_post_draw(EntityDrawHookCallback callback, void *arg) {
|
|||
void ent_unhook_post_draw(EntityDrawHookCallback callback) {
|
||||
remove_hook(&entities.hooks.post_draw, callback);
|
||||
}
|
||||
|
||||
void _ent_array_compact_Entity(BoxedEntityArray *a) {
|
||||
for(int i = 0; i < a->size; ++i) {
|
||||
while(ENT_UNBOX(a->array[i]) == NULL) {
|
||||
if(i >= --a->size) {
|
||||
return;
|
||||
}
|
||||
memmove(a->array + i, a->array + i + 1, (a->size - i) * sizeof(a->array[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -251,8 +251,13 @@ typedef struct BoxedEntityArray {
|
|||
INLINE void _ent_array_add_##typename(Boxed##typename box, Boxed##typename##Array *a) { \
|
||||
assert(a->size < a->capacity); \
|
||||
a->array[a->size++] = box; \
|
||||
} \
|
||||
INLINE void _ent_array_compact_##typename(Boxed##typename##Array *a) { \
|
||||
_ent_array_compact_Entity(&a->as_generic_UNSAFE); \
|
||||
}
|
||||
|
||||
void _ent_array_compact_Entity(BoxedEntityArray *a);
|
||||
|
||||
ENTITIES(ENT_EMIT_ARRAY_DEFS,)
|
||||
#undef ENT_EMIT_ARRAY_DEFS
|
||||
|
||||
|
@ -268,6 +273,10 @@ INLINE void _ent_array_add_Entity(struct EntityInterface *ent, BoxedEntityArray
|
|||
#define ENT_ARRAY_ADD(_array, _ent) ENT_BOXED_DISPATCH_FUNCTION(_ent_array_add_, ENT_BOX_OR_PASSTHROUGH(_ent), _array)
|
||||
#define ENT_ARRAY_GET_BOXED(_array, _index) ((_array)->array[_index])
|
||||
#define ENT_ARRAY_GET(_array, _index) ENT_UNBOX(ENT_ARRAY_GET_BOXED(_array, _index))
|
||||
#define ENT_ARRAY_COMPACT(_array) \
|
||||
_Generic((_array)->array[0], \
|
||||
ENT_BOXED_DISPATCH_TABLE(_ent_array_compact_) \
|
||||
)(_array)
|
||||
|
||||
#define DECLARE_ENT_ARRAY(_ent_type, _name, _capacity) \
|
||||
Boxed##_ent_type##Array _name; \
|
||||
|
|
Loading…
Reference in a new issue