objlist: Implement non-growing slice assignment.

Slice value to assign can be only a list so far too.
This commit is contained in:
Paul Sokolovsky
2014-05-10 22:23:00 +03:00
parent d915a52eb6
commit 94d8246272
3 changed files with 52 additions and 0 deletions

View File

@@ -173,6 +173,26 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
uint index_val = mp_get_index(self->base.type, self->len, index, false);
return self->items[index_val];
} else {
#if MICROPY_ENABLE_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_obj_list_t *self = self_in;
assert(MP_OBJ_IS_TYPE(value, &mp_type_list));
mp_obj_list_t *slice = value;
machine_uint_t start, stop;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
assert(0);
}
int len_adj = slice->len - (stop - start);
//printf("Len adj: %d\n", len_adj);
assert(len_adj <= 0);
mp_seq_replace_slice_no_grow(self->items, self->len, start, stop, slice->items, slice->len, mp_obj_t);
// Clear "freed" elements at the end of list
mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
self->len += len_adj;
// TODO: apply allocation policy re: alloc_size
return mp_const_none;
}
#endif
mp_obj_list_store(self_in, index, value);
return mp_const_none;
}