Separate out mp_obj_list_insert for use in display.Group

Note that for some reason this makes the binary 500 bytes larger!
This commit is contained in:
Radomir Dopieralski 2021-02-27 21:13:55 +01:00
parent 38fb7b511b
commit 24473b7983
4 changed files with 14 additions and 15 deletions

View File

@ -270,7 +270,7 @@ STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
return mp_const_none; // return None, as per CPython return mp_const_none; // return None, as per CPython
} }
mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) { inline mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) {
if (self->len == 0) { if (self->len == 0) {
mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list); mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list);
} }
@ -375,6 +375,15 @@ STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) {
return mp_seq_index_obj(self->items, self->len, n_args, args); return mp_seq_index_obj(self->items, self->len, n_args, args);
} }
inline void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj) {
mp_obj_list_append(MP_OBJ_FROM_PTR(self), mp_const_none);
for (size_t i = self->len - 1; i > index; --i) {
self->items[i] = self->items[i - 1];
}
self->items[index] = obj;
}
STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) { STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list)); mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list); mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
@ -389,14 +398,7 @@ STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
if ((size_t)index > self->len) { if ((size_t)index > self->len) {
index = self->len; index = self->len;
} }
mp_obj_list_insert(self, index, obj);
mp_obj_list_append(self_in, mp_const_none);
for (mp_int_t i = self->len-1; i > index; i--) {
self->items[i] = self->items[i-1];
}
self->items[index] = obj;
return mp_const_none; return mp_const_none;
} }

View File

@ -37,5 +37,6 @@ typedef struct _mp_obj_list_t {
void mp_obj_list_init(mp_obj_list_t *o, size_t n); void mp_obj_list_init(mp_obj_list_t *o, size_t n);
mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index); mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index);
void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj);
#endif // MICROPY_INCLUDED_PY_OBJLIST_H #endif // MICROPY_INCLUDED_PY_OBJLIST_H

View File

@ -333,7 +333,7 @@ STATIC mp_obj_t displayio_group_obj_sort(size_t n_args, const mp_obj_t *pos_args
for (size_t i = 1; i < n_args; ++i) { for (size_t i = 1; i < n_args; ++i) {
args[i] = pos_args[i]; args[i] = pos_args[i];
} }
args[0] = self->members; args[0] = MP_OBJ_FROM_PTR(self->members);
return mp_obj_list_sort(n_args, pos_args, kw_args); return mp_obj_list_sort(n_args, pos_args, kw_args);
} }
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_sort_obj, 1, displayio_group_obj_sort); MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_sort_obj, 1, displayio_group_obj_sort);

View File

@ -314,11 +314,7 @@ static void _remove_layer(displayio_group_t* self, size_t index) {
void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer) { void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer) {
_add_layer(self, layer); _add_layer(self, layer);
mp_obj_list_append(self->members, mp_const_none); mp_obj_list_insert(self->members, index, layer);
for (size_t i = self->members->len - 1; i > index; i--) {
self->members->items[i] = self->members->items[i - 1];
}
self->members->items[index] = layer;
} }
mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) { mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {