Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
45a87446
Commit
45a87446
authored
Jan 04, 2014
by
John R. Lenton
Browse files
Implements list.insert. Fixes issue #61.
parent
5d4a8213
Changes
4
Hide whitespace changes
Inline
Side-by-side
py/obj.h
View file @
45a87446
...
...
@@ -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
*
);
...
...
py/objfun.c
View file @
45a87446
...
...
@@ -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
;
...
...
py/objlist.c
View file @
45a87446
...
...
@@ -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
...
...
tests/basics/tests/list_insert.py
0 → 100644
View file @
45a87446
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
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment