summaryrefslogtreecommitdiffstatshomepage
path: root/py/objset.c
diff options
context:
space:
mode:
authorJohn R. Lenton <jlenton@gmail.com>2014-01-12 17:54:03 +0000
committerJohn R. Lenton <jlenton@gmail.com>2014-01-12 17:54:03 +0000
commitf1ae6b48fbc88d4acdbe1136f56c136f8e1b2991 (patch)
tree831d0da417f1267e79e594acec5fd76752ff2256 /py/objset.c
parent032129f3b595f132046b9f4c6f108f1677aef944 (diff)
downloadmicropython-f1ae6b48fbc88d4acdbe1136f56c136f8e1b2991.tar.gz
micropython-f1ae6b48fbc88d4acdbe1136f56c136f8e1b2991.zip
Implemented set.intersection and set.intersection_update
Diffstat (limited to 'py/objset.c')
-rw-r--r--py/objset.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/py/objset.c b/py/objset.c
index c6e0dcf137..144bb6daa8 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -175,6 +175,43 @@ static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) {
}
static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
+static mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ if (self_in == other) {
+ return update ? mp_const_none : set_copy(self_in);
+ }
+
+ mp_obj_set_t *self = self_in;
+ mp_obj_set_t *out = mp_obj_new_set(0, NULL);
+
+ 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)) {
+ set_add(out, next);
+ }
+ }
+
+ if (update) {
+ m_del(mp_obj_t, self->set.table, self->set.alloc);
+ self->set.alloc = out->set.alloc;
+ self->set.used = out->set.used;
+ self->set.table = out->set.table;
+ }
+
+ return update ? mp_const_none : out;
+}
+
+static mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
+ return set_intersect_int(self_in, other, false);
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
+
+static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
+ return set_intersect_int(self_in, other, true);
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
+
/******************************************************************************/
/* set constructors & public C API */
@@ -187,6 +224,8 @@ static const mp_method_t set_type_methods[] = {
{ "discard", &set_discard_obj },
{ "difference", &set_diff_obj },
{ "difference_update", &set_diff_update_obj },
+ { "intersection", &set_intersect_obj },
+ { "intersection_update", &set_intersect_update_obj },
{ NULL, NULL }, // end-of-list sentinel
};