Skip to content

Commit e4f0e47

Browse files
authored
Merge pull request #4233 from pewpew-game/displayio-group-list
displayio: make Group use a python list internally
2 parents ce70b95 + cb2cf81 commit e4f0e47

File tree

8 files changed

+192
-138
lines changed

8 files changed

+192
-138
lines changed

ports/atmel-samd/boards/ugame10/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CIRCUITPY_ANALOGIO = 1
1818
CIRCUITPY_GAMEPAD = 1
1919
CIRCUITPY_DISPLAYIO = 1
2020

21+
CIRCUITPY_PULSEIO = 0
2122
CIRCUITPY_AUDIOBUSIO = 0
2223
CIRCUITPY_BITBANGIO = 0
2324
CIRCUITPY_BITMAPTOOLS = 0

py/objlist.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,10 @@ 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-
STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
274-
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
275-
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
273+
inline mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) {
276274
if (self->len == 0) {
277275
mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list);
278276
}
279-
size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
280277
mp_obj_t ret = self->items[index];
281278
self->len -= 1;
282279
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) {
289286
return ret;
290287
}
291288

289+
STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
290+
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
291+
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
292+
size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
293+
return mp_obj_list_pop(self, index);
294+
}
295+
292296
STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj_t binop_less_result) {
293297
MP_STACK_CHECK();
294298
while (head < tail) {
@@ -371,6 +375,15 @@ STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) {
371375
return mp_seq_index_obj(self->items, self->len, n_args, args);
372376
}
373377

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+
374387
STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
375388
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
376389
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
@@ -385,14 +398,7 @@ STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
385398
if ((size_t)index > self->len) {
386399
index = self->len;
387400
}
388-
389-
mp_obj_list_append(self_in, mp_const_none);
390-
391-
for (mp_int_t i = self->len-1; i > index; i--) {
392-
self->items[i] = self->items[i-1];
393-
}
394-
self->items[index] = obj;
395-
401+
mp_obj_list_insert(self, index, obj);
396402
return mp_const_none;
397403
}
398404

py/objlist.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ typedef struct _mp_obj_list_t {
3636
} mp_obj_list_t;
3737

3838
void mp_obj_list_init(mp_obj_list_t *o, size_t n);
39+
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);
3941

4042
#endif // MICROPY_INCLUDED_PY_OBJLIST_H

shared-bindings/displayio/Group.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//| """Create a Group of a given size and scale. Scale is in one dimension. For example, scale=2
4343
//| leads to a layer's pixel being 2x2 pixels when in the group.
4444
//|
45-
//| :param int max_size: The maximum group size.
45+
//| :param int max_size: Ignored. Will be removed in 7.x.
4646
//| :param int scale: Scale of layer pixels in one dimension.
4747
//| :param int x: Initial x position within the parent.
4848
//| :param int y: Initial y position within the parent."""
@@ -59,19 +59,14 @@ STATIC mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_arg
5959
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
6060
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
6161

62-
mp_int_t max_size = args[ARG_max_size].u_int;
63-
if (max_size < 1) {
64-
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_max_size);
65-
}
66-
6762
mp_int_t scale = args[ARG_scale].u_int;
6863
if (scale < 1) {
6964
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_scale);
7065
}
7166

7267
displayio_group_t *self = m_new_obj(displayio_group_t);
7368
self->base.type = &displayio_group_type;
74-
common_hal_displayio_group_construct(self, max_size, scale, args[ARG_x].u_int, args[ARG_y].u_int);
69+
common_hal_displayio_group_construct(self, scale, args[ARG_x].u_int, args[ARG_y].u_int);
7570

7671
return MP_OBJ_FROM_PTR(self);
7772
}
@@ -328,6 +323,21 @@ STATIC mp_obj_t group_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t valu
328323
return mp_const_none;
329324
}
330325

326+
//| def sort(self, key: function, reverse: bool) -> None:
327+
//| """Sort the members of the group."""
328+
//| ...
329+
//|
330+
STATIC mp_obj_t displayio_group_obj_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
331+
displayio_group_t *self = native_group(pos_args[0]);
332+
mp_obj_t *args = m_new(mp_obj_t, n_args);
333+
for (size_t i = 1; i < n_args; ++i) {
334+
args[i] = pos_args[i];
335+
}
336+
args[0] = MP_OBJ_FROM_PTR(self->members);
337+
return mp_obj_list_sort(n_args, pos_args, kw_args);
338+
}
339+
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_sort_obj, 1, displayio_group_obj_sort);
340+
331341
STATIC const mp_rom_map_elem_t displayio_group_locals_dict_table[] = {
332342
{ MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&displayio_group_hidden_obj) },
333343
{ MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&displayio_group_scale_obj) },
@@ -338,6 +348,7 @@ STATIC const mp_rom_map_elem_t displayio_group_locals_dict_table[] = {
338348
{ MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&displayio_group_index_obj) },
339349
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&displayio_group_pop_obj) },
340350
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&displayio_group_remove_obj) },
351+
{ MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&displayio_group_sort_obj) },
341352
};
342353
STATIC MP_DEFINE_CONST_DICT(displayio_group_locals_dict, displayio_group_locals_dict_table);
343354

shared-bindings/displayio/Group.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern const mp_obj_type_t displayio_group_type;
3333

3434
displayio_group_t* native_group(mp_obj_t group_obj);
3535

36-
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y);
36+
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t scale, mp_int_t x, mp_int_t y);
3737
uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self);
3838
void common_hal_displayio_group_set_scale(displayio_group_t* self, uint32_t scale);
3939
bool common_hal_displayio_group_get_hidden(displayio_group_t* self);

0 commit comments

Comments
 (0)