Implemented set.add
This commit is contained in:
parent
0ce03b48a0
commit
19b14d3d8a
23
py/objset.c
23
py/objset.c
@ -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) {
|
||||
|
5
tests/basics/tests/set_add.py
Normal file
5
tests/basics/tests/set_add.py
Normal file
@ -0,0 +1,5 @@
|
||||
s = {1, 2, 3, 4}
|
||||
print(s.add(5))
|
||||
l = list(s)
|
||||
l.sort()
|
||||
print(l)
|
Loading…
Reference in New Issue
Block a user