diff options
author | John R. Lenton <jlenton@gmail.com> | 2014-01-12 18:03:21 +0000 |
---|---|---|
committer | John R. Lenton <jlenton@gmail.com> | 2014-01-12 18:03:21 +0000 |
commit | 4a08067c0c9c8417525e89eef4c3693cdc05b954 (patch) | |
tree | d55dd960e076d53042c4de77d1e6fe217017e22a /py/objset.c | |
parent | f1ae6b48fbc88d4acdbe1136f56c136f8e1b2991 (diff) | |
download | micropython-4a08067c0c9c8417525e89eef4c3693cdc05b954.tar.gz micropython-4a08067c0c9c8417525e89eef4c3693cdc05b954.zip |
Implemented set.isdisjoint
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 144bb6daa8..afc426b3b0 100644 --- a/py/objset.c +++ b/py/objset.c @@ -212,6 +212,21 @@ static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) { } static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update); +static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + mp_obj_t iter = rt_getiter(other); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { + return mp_const_false; + } + } + return mp_const_true; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint); + /******************************************************************************/ /* set constructors & public C API */ @@ -226,6 +241,7 @@ static const mp_method_t set_type_methods[] = { { "difference_update", &set_diff_update_obj }, { "intersection", &set_intersect_obj }, { "intersection_update", &set_intersect_update_obj }, + { "isdisjoint", &set_isdisjoint_obj }, { NULL, NULL }, // end-of-list sentinel }; |