diff options
author | John R. Lenton <jlenton@gmail.com> | 2014-01-12 15:56:25 +0000 |
---|---|---|
committer | John R. Lenton <jlenton@gmail.com> | 2014-01-12 15:56:25 +0000 |
commit | 3b0bd87906eb301ebfe6588773ebfe07b0509ad8 (patch) | |
tree | bfaf5a2159665157ac8fd0547ccbd78ba3c02efe /py/objset.c | |
parent | 1d7fb2f21be8dd6f95e5889442a3735464c94dfb (diff) | |
download | micropython-3b0bd87906eb301ebfe6588773ebfe07b0509ad8.tar.gz micropython-3b0bd87906eb301ebfe6588773ebfe07b0509ad8.zip |
Implemented set.copy
Diffstat (limited to 'py/objset.c')
-rw-r--r-- | py/objset.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/py/objset.c b/py/objset.c index 8bd006a761..40f19c85d3 100644 --- a/py/objset.c +++ b/py/objset.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <stdint.h> +#include <string.h> #include <assert.h> #include "nlr.h" @@ -114,6 +115,20 @@ static mp_obj_t set_clear(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear); +static mp_obj_t set_copy(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + mp_obj_set_t *other = m_new_obj(mp_obj_set_t); + other->base.type = &set_type; + mp_set_init(&other->set, self->set.alloc); + other->set.used = self->set.used; + memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t)); + + return other; +} +static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy); + /******************************************************************************/ /* set constructors & public C API */ @@ -122,6 +137,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear); static const mp_method_t set_type_methods[] = { { "add", &set_add_obj }, { "clear", &set_clear_obj }, + { "copy", &set_copy_obj }, { NULL, NULL }, // end-of-list sentinel }; |