summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2021-03-02 09:59:58 -0800
committerGitHub <noreply@github.com>2021-03-02 09:59:58 -0800
commite4f0e47d9f0bb620f56b93c649197b16105ab5b2 (patch)
tree2a3b9b292039ba592866690f8f89d6f61e288123 /py
parentce70b95990b45b4799055b6b7e7d347ac35257a0 (diff)
parentcb2cf81136c48695b1097fb2f601bce90e1086c9 (diff)
Merge pull request #4233 from pewpew-game/displayio-group-list
displayio: make Group use a python list internally
Diffstat (limited to 'py')
-rw-r--r--py/objlist.c30
-rw-r--r--py/objlist.h2
2 files changed, 20 insertions, 12 deletions
diff --git a/py/objlist.c b/py/objlist.c
index 51ec920be..7aa4ee89c 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);
+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);
}
- 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) {
@@ -371,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);
@@ -385,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 f02030557..eb005e81c 100644
--- a/py/objlist.h
+++ b/py/objlist.h
@@ -36,5 +36,7 @@ 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);
+void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj);
#endif // MICROPY_INCLUDED_PY_OBJLIST_H