From e505c59ed838095ccb2bd996fc77bd4aeada2d80 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sun, 21 Feb 2021 16:25:17 +0100 Subject: Separate mp_obj_list_pop so it can be used outside of objlist.c --- py/objlist.c | 12 ++++++++---- py/objlist.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'py') diff --git a/py/objlist.c b/py/objlist.c index 51ec920be..309e0e542 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -270,13 +270,10 @@ 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 } -STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { - mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list)); - mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list); +mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) { if (self->len == 0) { mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list); } - size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); mp_obj_t ret = self->items[index]; self->len -= 1; memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t)); @@ -289,6 +286,13 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { return ret; } +STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { + mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list)); + mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list); + size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); + return mp_obj_list_pop(self, index); +} + STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj_t binop_less_result) { MP_STACK_CHECK(); while (head < tail) { diff --git a/py/objlist.h b/py/objlist.h index f02030557..cec23e06c 100644 --- a/py/objlist.h +++ b/py/objlist.h @@ -36,5 +36,6 @@ typedef struct _mp_obj_list_t { } mp_obj_list_t; 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); #endif // MICROPY_INCLUDED_PY_OBJLIST_H -- cgit v1.2.3 From 24473b79838412e7be0c65e6dfae45146cf15a6b Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 27 Feb 2021 21:13:55 +0100 Subject: Separate out mp_obj_list_insert for use in display.Group Note that for some reason this makes the binary 500 bytes larger! --- py/objlist.c | 20 +++++++++++--------- py/objlist.h | 1 + shared-bindings/displayio/Group.c | 2 +- shared-module/displayio/Group.c | 6 +----- 4 files changed, 14 insertions(+), 15 deletions(-) (limited to 'py') diff --git a/py/objlist.c b/py/objlist.c index 309e0e542..7aa4ee89c 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -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 } -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) { 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); } +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) { 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); @@ -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) { index = self->len; } - - 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; - + mp_obj_list_insert(self, index, obj); return mp_const_none; } diff --git a/py/objlist.h b/py/objlist.h index cec23e06c..eb005e81c 100644 --- a/py/objlist.h +++ b/py/objlist.h @@ -37,5 +37,6 @@ typedef struct _mp_obj_list_t { 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); +void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj); #endif // MICROPY_INCLUDED_PY_OBJLIST_H diff --git a/shared-bindings/displayio/Group.c b/shared-bindings/displayio/Group.c index d61307938..c1c05504d 100644 --- a/shared-bindings/displayio/Group.c +++ b/shared-bindings/displayio/Group.c @@ -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) { 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); } MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_sort_obj, 1, displayio_group_obj_sort); diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c index 9f08eac65..42efeb18c 100644 --- a/shared-module/displayio/Group.c +++ b/shared-module/displayio/Group.c @@ -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) { _add_layer(self, layer); - mp_obj_list_append(self->members, mp_const_none); - 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_list_insert(self->members, index, layer); } mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) { -- cgit v1.2.3