diff options
author | Damien George <damien.p.george@gmail.com> | 2014-01-05 15:59:00 -0800 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-01-05 15:59:00 -0800 |
commit | aa35fc60d77085781ab862fa9a048d3c35823d83 (patch) | |
tree | a3553cff33606547e5f2492cb51a36b2806f3ee8 /py/objlist.c | |
parent | 17f4497d6d16eafb00aa4ca62e9ab24ec6cf6775 (diff) | |
parent | 8428b8f3c94fb4adab6b094c1e0718168ebd8cbd (diff) | |
download | micropython-aa35fc60d77085781ab862fa9a048d3c35823d83.tar.gz micropython-aa35fc60d77085781ab862fa9a048d3c35823d83.zip |
Merge pull request #92 from chipaca/list_insert
List insert. Fixes issue #61.
Diffstat (limited to 'py/objlist.c')
-rw-r--r-- | py/objlist.c | 27 |
1 files changed, 27 insertions, 0 deletions
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 |