Fix ir_array_insert, ir_array_remove, ir_list_insert, ir_list_remove

This commit is contained in:
Dmitry Stogov 2022-04-13 18:03:25 +03:00
parent 1f7a5bcdc7
commit 58a993ab32
2 changed files with 24 additions and 16 deletions

22
ir.c
View File

@ -813,17 +813,35 @@ void ir_array_insert(ir_array *a, uint32_t i, ir_ref val)
if (a->refs[a->size - 1]) {
ir_array_grow(a, a->size + 1);
}
memmove(a->refs + i, a->refs + i + 1, (a->size - i - 1) * sizeof(ir_ref));
memmove(a->refs + i + 1, a->refs + i, (a->size - i - 1) * sizeof(ir_ref));
a->refs[i] = val;
}
void ir_array_remove(ir_array *a, uint32_t i)
{
IR_ASSERT(i < a->size);
memmove(a->refs + i + 1, a->refs + i, (a->size - i - 1) * sizeof(ir_ref));
memmove(a->refs + i, a->refs + i + 1, (a->size - i - 1) * sizeof(ir_ref));
a->refs[a->size - 1] = IR_UNUSED;
}
void ir_list_insert(ir_list *l, uint32_t i, ir_ref val)
{
IR_ASSERT(i < l->len);
if (l->len >= l->a.size) {
ir_array_grow(&l->a, l->a.size + 1);
}
memmove(l->a.refs + i + 1, l->a.refs + i, (l->len - i) * sizeof(ir_ref));
l->a.refs[i] = val;
l->len++;
}
void ir_list_remove(ir_list *l, uint32_t i)
{
IR_ASSERT(i < l->len);
memmove(l->a.refs + i, l->a.refs + i + 1, (l->len - i) * sizeof(ir_ref));
l->len--;
}
bool ir_list_contains(ir_list *l, ir_ref val)
{
uint32_t i;

View File

@ -344,7 +344,7 @@ IR_ALWAYS_INLINE ir_ref ir_array_at(ir_array *a, uint32_t i)
return a->refs[i];
}
IR_ALWAYS_INLINE void ir_array_map(ir_array *a, uint32_t i, ir_ref val)
IR_ALWAYS_INLINE void ir_array_set(ir_array *a, uint32_t i, ir_ref val)
{
if (i >= a->size) {
ir_array_grow(a, i);
@ -359,6 +359,8 @@ typedef struct _ir_list {
} ir_list;
bool ir_list_contains(ir_list *l, ir_ref val);
void ir_list_insert(ir_list *l, uint32_t i, ir_ref val);
void ir_list_remove(ir_list *l, uint32_t i);
IR_ALWAYS_INLINE void ir_list_init(ir_list *l, uint32_t size)
{
@ -389,7 +391,7 @@ IR_ALWAYS_INLINE uint32_t ir_list_capasity(ir_list *l)
IR_ALWAYS_INLINE void ir_list_push(ir_list *l, ir_ref val)
{
ir_array_map(&l->a, l->len++, val);
ir_array_set(&l->a, l->len++, val);
}
IR_ALWAYS_INLINE ir_ref ir_list_pop(ir_list *l)
@ -410,18 +412,6 @@ IR_ALWAYS_INLINE ir_ref ir_list_at(ir_list *l, uint32_t i)
return ir_array_at(&l->a, i);
}
IR_ALWAYS_INLINE void ir_list_insert(ir_list *l, uint32_t i, ir_ref val)
{
ir_array_insert(&l->a, i, val);
l->len++;
}
IR_ALWAYS_INLINE void ir_list_remove(ir_list *l, uint32_t i)
{
ir_array_remove(&l->a, i);
l->len--;
}
/* Worklist (unique list) */
typedef struct _ir_worklist {
ir_list l;