Implemented set.add

This commit is contained in:
John R. Lenton 2014-01-12 15:29:11 +00:00
parent 0ce03b48a0
commit 19b14d3d8a
2 changed files with 28 additions and 0 deletions

View File

@ -92,12 +92,35 @@ static mp_obj_t set_getiter(mp_obj_t set_in) {
return o;
}
/******************************************************************************/
/* set methods */
static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
mp_obj_set_t *self = self_in;
mp_set_lookup(&self->set, item, true);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
/******************************************************************************/
/* set constructors & public C API */
static const mp_method_t set_type_methods[] = {
{ "add", &set_add_obj },
{ NULL, NULL }, // end-of-list sentinel
};
const mp_obj_type_t set_type = {
{ &mp_const_type },
"set",
.print = set_print,
.make_new = set_make_new,
.getiter = set_getiter,
.methods = set_type_methods,
};
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {

View File

@ -0,0 +1,5 @@
s = {1, 2, 3, 4}
print(s.add(5))
l = list(s)
l.sort()
print(l)