Skip to content

Commit 4a08067

Browse files
committed
Implemented set.isdisjoint
1 parent f1ae6b4 commit 4a08067

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

py/objset.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
212212
}
213213
static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
214214

215+
static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
216+
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
217+
mp_obj_set_t *self = self_in;
218+
219+
mp_obj_t iter = rt_getiter(other);
220+
mp_obj_t next;
221+
while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
222+
if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
223+
return mp_const_false;
224+
}
225+
}
226+
return mp_const_true;
227+
}
228+
static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
229+
215230

216231
/******************************************************************************/
217232
/* set constructors & public C API */
@@ -226,6 +241,7 @@ static const mp_method_t set_type_methods[] = {
226241
{ "difference_update", &set_diff_update_obj },
227242
{ "intersection", &set_intersect_obj },
228243
{ "intersection_update", &set_intersect_update_obj },
244+
{ "isdisjoint", &set_isdisjoint_obj },
229245
{ NULL, NULL }, // end-of-list sentinel
230246
};
231247

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
s = {1, 2, 3, 4}
2+
print(s.isdisjoint({1}))
3+
print(s.isdisjoint([2]))
4+
print(s.isdisjoint([]))
5+
print(s.isdisjoint({7,8,9,10}))
6+
print(s.isdisjoint([7,8,9,1]))

0 commit comments

Comments
 (0)