From 45a87446179c0c629a2ae89e24fa61919a57c0e5 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Sat, 4 Jan 2014 01:15:01 +0000 Subject: [PATCH] Implements list.insert. Fixes issue #61. --- py/obj.h | 2 ++ py/objfun.c | 12 ++++++++++++ py/objlist.c | 27 +++++++++++++++++++++++++++ tests/basics/tests/list_insert.py | 9 +++++++++ 4 files changed, 50 insertions(+) create mode 100644 tests/basics/tests/list_insert.py diff --git a/py/obj.h b/py/obj.h index 7b4b0656f..86234bea2 100644 --- a/py/obj.h +++ b/py/obj.h @@ -46,6 +46,7 @@ struct _mp_obj_base_t { #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 0, 0, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 1, 1, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 2, 2, fun_name} +#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 3, 3, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, (~((machine_uint_t)0)), fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, fun_name} @@ -54,6 +55,7 @@ struct _mp_obj_base_t { typedef mp_obj_t (*mp_fun_0_t)(void); typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t); typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t); +typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t); typedef mp_obj_t (*mp_fun_t)(void); typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *); diff --git a/py/objfun.c b/py/objfun.c index e998bd28d..9601622d8 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -38,6 +38,9 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { case 2: return ((mp_fun_2_t)self->fun)(args[1], args[0]); + case 3: + return ((mp_fun_3_t)self->fun)(args[2], args[1], args[0]); + default: assert(0); return mp_const_none; @@ -106,6 +109,15 @@ mp_obj_t rt_make_function_2(mp_fun_2_t fun) { return o; } +mp_obj_t rt_make_function_3(mp_fun_3_t fun) { + mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); + o->base.type = &fun_native_type; + o->n_args_min = 3; + o->n_args_max = 3; + o->fun = fun; + return o; +} + mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; diff --git a/py/objlist.c b/py/objlist.c index 20409a26c..f5299ddc8 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -173,11 +173,37 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) { nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "Object not in list.")); } +static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) { + assert(MP_OBJ_IS_TYPE(self_in, &list_type)); + mp_obj_list_t *self = self_in; + // insert has its own strange index logic + int index = MP_OBJ_SMALL_INT_VALUE(idx); + if (index < 0) { + index += self->len; + } + if (index < 0) { + index = 0; + } + if (index > self->len) { + index = self->len; + } + + mp_obj_list_append(self_in, mp_const_none); + + for (int i = self->len-1; i > index; i--) { + self->items[i] = self->items[i-1]; + } + self->items[index] = obj; + + return mp_const_none; +} + static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append); static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear); static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy); static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count); static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index); +static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert); static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop); static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort); @@ -196,6 +222,7 @@ const mp_obj_type_t list_type = { { "copy", &list_copy_obj }, { "count", &list_count_obj }, { "index", &list_index_obj }, + { "insert", &list_insert_obj }, { "pop", &list_pop_obj }, { "sort", &list_sort_obj }, { NULL, NULL }, // end-of-list sentinel diff --git a/tests/basics/tests/list_insert.py b/tests/basics/tests/list_insert.py new file mode 100644 index 000000000..6db97cf5c --- /dev/null +++ b/tests/basics/tests/list_insert.py @@ -0,0 +1,9 @@ +a = [1, 2, 3] +a.insert(1, 42) +print(a) +a.insert(-1, -1) +print(a) +a.insert(99, 99) +print(a) +a.insert(-99, -99) +print(a)