Skip to content

Commit 24473b7

Browse files
committed
Separate out mp_obj_list_insert for use in display.Group
Note that for some reason this makes the binary 500 bytes larger!
1 parent 38fb7b5 commit 24473b7

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

py/objlist.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
270270
return mp_const_none; // return None, as per CPython
271271
}
272272

273-
mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) {
273+
inline mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) {
274274
if (self->len == 0) {
275275
mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list);
276276
}
@@ -375,6 +375,15 @@ STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) {
375375
return mp_seq_index_obj(self->items, self->len, n_args, args);
376376
}
377377

378+
inline void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj) {
379+
mp_obj_list_append(MP_OBJ_FROM_PTR(self), mp_const_none);
380+
381+
for (size_t i = self->len - 1; i > index; --i) {
382+
self->items[i] = self->items[i - 1];
383+
}
384+
self->items[index] = obj;
385+
}
386+
378387
STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
379388
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
380389
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) {
389398
if ((size_t)index > self->len) {
390399
index = self->len;
391400
}
392-
393-
mp_obj_list_append(self_in, mp_const_none);
394-
395-
for (mp_int_t i = self->len-1; i > index; i--) {
396-
self->items[i] = self->items[i-1];
397-
}
398-
self->items[index] = obj;
399-
401+
mp_obj_list_insert(self, index, obj);
400402
return mp_const_none;
401403
}
402404

py/objlist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ typedef struct _mp_obj_list_t {
3737

3838
void mp_obj_list_init(mp_obj_list_t *o, size_t n);
3939
mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index);
40+
void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj);
4041

4142
#endif // MICROPY_INCLUDED_PY_OBJLIST_H

shared-bindings/displayio/Group.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ STATIC mp_obj_t displayio_group_obj_sort(size_t n_args, const mp_obj_t *pos_args
333333
for (size_t i = 1; i < n_args; ++i) {
334334
args[i] = pos_args[i];
335335
}
336-
args[0] = self->members;
336+
args[0] = MP_OBJ_FROM_PTR(self->members);
337337
return mp_obj_list_sort(n_args, pos_args, kw_args);
338338
}
339339
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_sort_obj, 1, displayio_group_obj_sort);

shared-module/displayio/Group.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,7 @@ static void _remove_layer(displayio_group_t* self, size_t index) {
314314

315315
void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer) {
316316
_add_layer(self, layer);
317-
mp_obj_list_append(self->members, mp_const_none);
318-
for (size_t i = self->members->len - 1; i > index; i--) {
319-
self->members->items[i] = self->members->items[i - 1];
320-
}
321-
self->members->items[index] = layer;
317+
mp_obj_list_insert(self->members, index, layer);
322318
}
323319

324320
mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {

0 commit comments

Comments
 (0)