list: Implement list multiplication.
This commit is contained in:
parent
bab5cfb34f
commit
074d3b5f86
15
py/objlist.c
15
py/objlist.c
@ -81,6 +81,21 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
|||||||
memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
|
memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
case RT_BINARY_OP_MULTIPLY:
|
||||||
|
{
|
||||||
|
if (!MP_OBJ_IS_SMALL_INT(rhs)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
int n = MP_OBJ_SMALL_INT_VALUE(rhs);
|
||||||
|
int len = o->len;
|
||||||
|
mp_obj_list_t *s = list_new(len * n);
|
||||||
|
mp_obj_t *dest = s->items;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
memcpy(dest, o->items, sizeof(mp_obj_t) * len);
|
||||||
|
dest += len;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
// op not supported
|
// op not supported
|
||||||
return NULL;
|
return NULL;
|
||||||
|
4
tests/basics/tests/list_mult.py
Normal file
4
tests/basics/tests/list_mult.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
print([0] * 5)
|
||||||
|
a = [1, 2, 3]
|
||||||
|
c = a * 3
|
||||||
|
print(c)
|
Loading…
Reference in New Issue
Block a user