diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-05-25 02:36:12 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-05-25 02:36:12 +0300 |
commit | 2705f4c782292eeb0b5ce77aadd1a1ea8a106164 (patch) | |
tree | 9d8e8b3fc5ab68e8ce1632dbdaad2cfe2a08ae79 /py/objlist.c | |
parent | 69d081a7cf7e0ae1e815365af8b3c19be293da84 (diff) | |
download | micropython-2705f4c782292eeb0b5ce77aadd1a1ea8a106164.tar.gz micropython-2705f4c782292eeb0b5ce77aadd1a1ea8a106164.zip |
objlist: Implement growing slice assignment.
This means that complete slice operations are supported for lists (but not
for bytearray's and array.array's).
Diffstat (limited to 'py/objlist.c')
-rw-r--r-- | py/objlist.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/py/objlist.c b/py/objlist.c index 7a15006adf..7d9f56d2e4 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -199,12 +199,21 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } 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)); + if (len_adj > 0) { + if (self->len + len_adj > self->alloc) { + // TODO: Might optimize memory copies here by checking if block can + // be grown inplace or not + self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + len_adj); + self->alloc = self->len + len_adj; + } + mp_seq_replace_slice_grow_inplace(self->items, self->len, start, stop, slice->items, slice->len, len_adj, mp_obj_t); + } else { + 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)); + // TODO: apply allocation policy re: alloc_size + } self->len += len_adj; - // TODO: apply allocation policy re: alloc_size return mp_const_none; } #endif |