summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-05 15:59:00 -0800
committerDamien George <damien.p.george@gmail.com>2014-01-05 15:59:00 -0800
commitaa35fc60d77085781ab862fa9a048d3c35823d83 (patch)
treea3553cff33606547e5f2492cb51a36b2806f3ee8
parent17f4497d6d16eafb00aa4ca62e9ab24ec6cf6775 (diff)
parent8428b8f3c94fb4adab6b094c1e0718168ebd8cbd (diff)
downloadmicropython-aa35fc60d77085781ab862fa9a048d3c35823d83.tar.gz
micropython-aa35fc60d77085781ab862fa9a048d3c35823d83.zip
Merge pull request #92 from chipaca/list_insert
List insert. Fixes issue #61.
-rw-r--r--py/obj.h2
-rw-r--r--py/objfun.c12
-rw-r--r--py/objlist.c27
-rw-r--r--tests/basics/tests/list_insert.py9
4 files changed, 50 insertions, 0 deletions
diff --git a/py/obj.h b/py/obj.h
index 03e67dd479..88a611ba7b 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 2d81f8f5c1..0db6459a39 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -39,6 +39,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;
@@ -108,6 +111,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 e416f7fed2..df03d9ae17 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -201,11 +201,37 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_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);
@@ -223,6 +249,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 0000000000..6db97cf5c1
--- /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)