From 94d8246272b2df7dbfb70ec85ed06e3b13793bac Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 10 May 2014 22:23:00 +0300 Subject: objlist: Implement non-growing slice assignment. Slice value to assign can be only a list so far too. --- py/objlist.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'py/objlist.c') diff --git a/py/objlist.c b/py/objlist.c index 08c171617..4432b2447 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -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; } -- cgit v1.2.3