Skip to content

Commit aa35fc6

Browse files
committed
Merge pull request adafruit#92 from chipaca/list_insert
List insert. Fixes issue adafruit#61.
2 parents 17f4497 + 8428b8f commit aa35fc6

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

py/obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct _mp_obj_base_t {
4646
#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}
4747
#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}
4848
#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}
49+
#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}
4950
#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}
5051
#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}
5152

@@ -54,6 +55,7 @@ struct _mp_obj_base_t {
5455
typedef mp_obj_t (*mp_fun_0_t)(void);
5556
typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
5657
typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
58+
typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
5759
typedef mp_obj_t (*mp_fun_t)(void);
5860
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
5961

py/objfun.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
3939
case 2:
4040
return ((mp_fun_2_t)self->fun)(args[1], args[0]);
4141

42+
case 3:
43+
return ((mp_fun_3_t)self->fun)(args[2], args[1], args[0]);
44+
4245
default:
4346
assert(0);
4447
return mp_const_none;
@@ -108,6 +111,15 @@ mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
108111
return o;
109112
}
110113

114+
mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
115+
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
116+
o->base.type = &fun_native_type;
117+
o->n_args_min = 3;
118+
o->n_args_max = 3;
119+
o->fun = fun;
120+
return o;
121+
}
122+
111123
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
112124
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
113125
o->base.type = &fun_native_type;

py/objlist.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,37 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
201201
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
202202
}
203203

204+
static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
205+
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
206+
mp_obj_list_t *self = self_in;
207+
// insert has its own strange index logic
208+
int index = MP_OBJ_SMALL_INT_VALUE(idx);
209+
if (index < 0) {
210+
index += self->len;
211+
}
212+
if (index < 0) {
213+
index = 0;
214+
}
215+
if (index > self->len) {
216+
index = self->len;
217+
}
218+
219+
mp_obj_list_append(self_in, mp_const_none);
220+
221+
for (int i = self->len-1; i > index; i--) {
222+
self->items[i] = self->items[i-1];
223+
}
224+
self->items[index] = obj;
225+
226+
return mp_const_none;
227+
}
228+
204229
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
205230
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
206231
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
207232
static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
208233
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
234+
static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
209235
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
210236
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
211237

@@ -223,6 +249,7 @@ const mp_obj_type_t list_type = {
223249
{ "copy", &list_copy_obj },
224250
{ "count", &list_count_obj },
225251
{ "index", &list_index_obj },
252+
{ "insert", &list_insert_obj },
226253
{ "pop", &list_pop_obj },
227254
{ "sort", &list_sort_obj },
228255
{ NULL, NULL }, // end-of-list sentinel

tests/basics/tests/list_insert.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
a = [1, 2, 3]
2+
a.insert(1, 42)
3+
print(a)
4+
a.insert(-1, -1)
5+
print(a)
6+
a.insert(99, 99)
7+
print(a)
8+
a.insert(-99, -99)
9+
print(a)

0 commit comments

Comments
 (0)