From 0ce03b48a04a7766f8694b1de8a88073542dcc20 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 15:17:42 +0000 Subject: make sets iterable --- py/objset.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 67dab11df..5606c4751 100644 --- a/py/objset.c +++ b/py/objset.c @@ -15,6 +15,14 @@ typedef struct _mp_obj_set_t { mp_set_t set; } mp_obj_set_t; +typedef struct _mp_obj_set_it_t { + mp_obj_base_t base; + mp_obj_set_t *set; + machine_uint_t cur; +} mp_obj_set_it_t; + +static mp_obj_t set_it_iternext(mp_obj_t self_in); + void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { mp_obj_set_t *self = self_in; bool first = true; @@ -54,11 +62,42 @@ static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) } } +const mp_obj_type_t set_it_type = { + { &mp_const_type }, + "set_iterator", + .iternext = set_it_iternext, +}; + +static mp_obj_t set_it_iternext(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_it_type)); + mp_obj_set_it_t *self = self_in; + machine_uint_t max = self->set->set.alloc; + mp_obj_t *table = self->set->set.table; + + for (machine_uint_t i = self->cur; i < max; i++) { + if (table[i] != NULL) { + self->cur = i + 1; + return table[i]; + } + } + + return mp_const_stop_iteration; +} + +static mp_obj_t set_getiter(mp_obj_t set_in) { + mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t); + o->base.type = &set_it_type; + o->set = (mp_obj_set_t *)set_in; + o->cur = 0; + return o; +} + const mp_obj_type_t set_type = { { &mp_const_type }, "set", .print = set_print, .make_new = set_make_new, + .getiter = set_getiter, }; mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { -- cgit v1.2.3 From 19b14d3d8ae229f17f8b63825f96220db37e3770 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 15:29:11 +0000 Subject: Implemented set.add --- py/objset.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 5606c4751..a74d1eb6a 100644 --- a/py/objset.c +++ b/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) { -- cgit v1.2.3 From 1d7fb2f21be8dd6f95e5889442a3735464c94dfb Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 15:44:26 +0000 Subject: Implemented set.clear --- py/objset.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index a74d1eb6a..8bd006a76 100644 --- a/py/objset.c +++ b/py/objset.c @@ -104,6 +104,16 @@ static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) { } static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add); +static mp_obj_t set_clear(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + mp_set_clear(&self->set); + + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear); + /******************************************************************************/ /* set constructors & public C API */ @@ -111,6 +121,7 @@ static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add); static const mp_method_t set_type_methods[] = { { "add", &set_add_obj }, + { "clear", &set_clear_obj }, { NULL, NULL }, // end-of-list sentinel }; -- cgit v1.2.3 From 3b0bd87906eb301ebfe6588773ebfe07b0509ad8 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 15:56:25 +0000 Subject: Implemented set.copy --- py/objset.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 8bd006a76..40f19c85d 100644 --- a/py/objset.c +++ b/py/objset.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "nlr.h" @@ -114,6 +115,20 @@ static mp_obj_t set_clear(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear); +static mp_obj_t set_copy(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + mp_obj_set_t *other = m_new_obj(mp_obj_set_t); + other->base.type = &set_type; + mp_set_init(&other->set, self->set.alloc); + other->set.used = self->set.used; + memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t)); + + return other; +} +static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy); + /******************************************************************************/ /* set constructors & public C API */ @@ -122,6 +137,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear); static const mp_method_t set_type_methods[] = { { "add", &set_add_obj }, { "clear", &set_clear_obj }, + { "copy", &set_copy_obj }, { NULL, NULL }, // end-of-list sentinel }; -- cgit v1.2.3 From 2a24172cdcdb5286c48afd6cf726548848e4840f Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 16:39:39 +0000 Subject: Implemented set.discard --- py/objset.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 40f19c85d..363f7d365 100644 --- a/py/objset.c +++ b/py/objset.c @@ -100,7 +100,7 @@ static mp_obj_t set_getiter(mp_obj_t set_in) { 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); + mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); return mp_const_none; } static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add); @@ -129,6 +129,13 @@ static mp_obj_t set_copy(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy); +static mp_obj_t set_discard(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, MP_MAP_LOOKUP_REMOVE_IF_FOUND); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard); /******************************************************************************/ /* set constructors & public C API */ @@ -138,6 +145,7 @@ static const mp_method_t set_type_methods[] = { { "add", &set_add_obj }, { "clear", &set_clear_obj }, { "copy", &set_copy_obj }, + { "discard", &set_discard_obj }, { NULL, NULL }, // end-of-list sentinel }; @@ -155,7 +163,7 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { o->base.type = &set_type; mp_set_init(&o->set, n_args); for (int i = 0; i < n_args; i++) { - mp_set_lookup(&o->set, items[i], true); + mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); } return o; } @@ -163,5 +171,5 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { void mp_obj_set_store(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); + mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); } -- cgit v1.2.3 From 032129f3b595f132046b9f4c6f108f1677aef944 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 17:07:17 +0000 Subject: Implemented set.difference and set.difference_update --- py/objset.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 363f7d365..c6e0dcf13 100644 --- a/py/objset.c +++ b/py/objset.c @@ -137,6 +137,45 @@ static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) { } static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard); +static mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) { + assert(n_args > 0); + assert(MP_OBJ_IS_TYPE(args[0], &set_type)); + mp_obj_set_t *self; + if (update) { + self = args[0]; + } else { + self = set_copy(args[0]); + } + + + for (int i = 1; i < n_args; i++) { + mp_obj_t other = args[i]; + if (self == other) { + set_clear(self); + } else { + mp_obj_t iter = rt_getiter(other); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + set_discard(self, next); + } + } + } + + return self; +} + +static mp_obj_t set_diff(int n_args, const mp_obj_t *args) { + return set_diff_int(n_args, args, false); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff); + +static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) { + set_diff_int(n_args, args, true); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update); + + /******************************************************************************/ /* set constructors & public C API */ @@ -146,6 +185,8 @@ static const mp_method_t set_type_methods[] = { { "clear", &set_clear_obj }, { "copy", &set_copy_obj }, { "discard", &set_discard_obj }, + { "difference", &set_diff_obj }, + { "difference_update", &set_diff_update_obj }, { NULL, NULL }, // end-of-list sentinel }; -- cgit v1.2.3 From f1ae6b48fbc88d4acdbe1136f56c136f8e1b2991 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 17:54:03 +0000 Subject: Implemented set.intersection and set.intersection_update --- py/objset.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index c6e0dcf13..144bb6daa 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 }; -- cgit v1.2.3 From 4a08067c0c9c8417525e89eef4c3693cdc05b954 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 18:03:21 +0000 Subject: Implemented set.isdisjoint --- py/objset.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 144bb6daa..afc426b3b 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 }; -- cgit v1.2.3 From ae00d334c6222fa9716135967a7c512b03f08191 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 18:23:36 +0000 Subject: Implemented set.remove --- py/objset.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index afc426b3b..75bc7efb9 100644 --- a/py/objset.c +++ b/py/objset.c @@ -227,6 +227,70 @@ static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) { } static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint); +static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) { + mp_obj_set_t *self; + bool cleanup_self = false; + if (MP_OBJ_IS_TYPE(self_in, &set_type)) { + self = self_in; + } else { + self = set_make_new(NULL, 1, &self_in); + cleanup_self = true; + } + + mp_obj_set_t *other; + bool cleanup_other = false; + if (MP_OBJ_IS_TYPE(other_in, &set_type)) { + other = other_in; + } else { + other = set_make_new(NULL, 1, &other_in); + cleanup_other = true; + } + mp_obj_t iter = set_getiter(self); + mp_obj_t next; + mp_obj_t out = mp_const_true; + while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) { + if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) { + out = mp_const_false; + break; + } + } + if (cleanup_self) { + set_clear(self); + } + if (cleanup_other) { + set_clear(other); + } + return out; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset); + +static mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset(other_in, self_in); +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset); + +static mp_obj_t set_pop(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + if (self->set.used == 0) { + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "pop from an empty set")); + } + mp_obj_t obj = mp_set_lookup(&self->set, NULL, + MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST); + return obj; +} +static MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop); + +static mp_obj_t set_remove(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; + if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) { + nlr_jump(mp_obj_new_exception(MP_QSTR_KeyError)); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove); /******************************************************************************/ /* set constructors & public C API */ @@ -242,6 +306,10 @@ static const mp_method_t set_type_methods[] = { { "intersection", &set_intersect_obj }, { "intersection_update", &set_intersect_update_obj }, { "isdisjoint", &set_isdisjoint_obj }, + { "issubset", &set_issubset_obj }, + { "issuperset", &set_issuperset_obj }, + { "pop", &set_pop_obj }, + { "remove", &set_remove_obj }, { NULL, NULL }, // end-of-list sentinel }; -- cgit v1.2.3 From 0de386bffec8acddcbe8c15913396035ea0b6405 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 19:39:48 +0000 Subject: Implemented set.update --- py/objset.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 75bc7efb9..27cc91e4f 100644 --- a/py/objset.c +++ b/py/objset.c @@ -292,6 +292,56 @@ static mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) { } static MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove); +static mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + mp_obj_t iter = rt_getiter(other_in); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update); + +static mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + self_in = set_copy(self_in); + set_symmetric_difference_update(self_in, other_in); + return self_in; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference); + +static void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) { + mp_obj_t iter = rt_getiter(other_in); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); + } +} + +static mp_obj_t set_update(int n_args, const mp_obj_t *args) { + assert(n_args > 0); + assert(MP_OBJ_IS_TYPE(args[0], &set_type)); + + for (int i = 1; i < n_args; i++) { + set_update_int(args[0], args[i]); + } + + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update); + +static mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = set_copy(self_in); + set_update_int(self, other_in); + return self; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union); + + + /******************************************************************************/ /* set constructors & public C API */ @@ -310,6 +360,10 @@ static const mp_method_t set_type_methods[] = { { "issuperset", &set_issuperset_obj }, { "pop", &set_pop_obj }, { "remove", &set_remove_obj }, + { "symmetric_difference", &set_symmetric_difference_obj }, + { "symmetric_difference_update", &set_symmetric_difference_update_obj }, + { "union", &set_union_obj }, + { "update", &set_update_obj }, { NULL, NULL }, // end-of-list sentinel }; -- cgit v1.2.3 From be790f94d5c66b96aa99b09a83639507f037f0a3 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 23:09:10 +0000 Subject: Implemented set binary ops. --- py/objset.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 10 deletions(-) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 27cc91e4f..2ed2abb61 100644 --- a/py/objset.c +++ b/py/objset.c @@ -9,6 +9,7 @@ #include "mpqstr.h" #include "obj.h" #include "runtime.h" +#include "runtime0.h" #include "map.h" typedef struct _mp_obj_set_t { @@ -227,7 +228,7 @@ static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) { } static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint); -static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) { +static mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) { mp_obj_set_t *self; bool cleanup_self = false; if (MP_OBJ_IS_TYPE(self_in, &set_type)) { @@ -245,13 +246,17 @@ static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) { other = set_make_new(NULL, 1, &other_in); cleanup_other = true; } - mp_obj_t iter = set_getiter(self); - mp_obj_t next; - mp_obj_t out = mp_const_true; - while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) { - if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) { - out = mp_const_false; - break; + bool out = true; + if (proper && self->set.used == other->set.used) { + out = false; + } else { + mp_obj_t iter = set_getiter(self); + mp_obj_t next; + while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) { + if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) { + out = false; + break; + } } } if (cleanup_self) { @@ -260,15 +265,39 @@ static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) { if (cleanup_other) { set_clear(other); } - return out; + return MP_BOOL(out); +} +static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset_internal(self_in, other_in, false); } static MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset); +static mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset_internal(self_in, other_in, true); +} + static mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) { - return set_issubset(other_in, self_in); + return set_issubset_internal(other_in, self_in, false); } static MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset); +static mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset_internal(other_in, self_in, true); +} + +static mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + if (!MP_OBJ_IS_TYPE(other_in, &set_type)) { + return mp_const_false; + } + mp_obj_set_t *other = other_in; + if (self->set.used != other->set.used) { + return mp_const_false; + } + return set_issubset(self_in, other_in); +} + static mp_obj_t set_pop(mp_obj_t self_in) { assert(MP_OBJ_IS_TYPE(self_in, &set_type)); mp_obj_set_t *self = self_in; @@ -341,6 +370,42 @@ static mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) { static MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union); +static mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { + mp_obj_t args[] = {lhs, rhs}; + switch (op) { + case RT_BINARY_OP_OR: + return set_union(lhs, rhs); + case RT_BINARY_OP_XOR: + return set_symmetric_difference(lhs, rhs); + case RT_BINARY_OP_AND: + return set_intersect(lhs, rhs); + case RT_BINARY_OP_SUBTRACT: + return set_diff(2, args); + case RT_BINARY_OP_INPLACE_OR: + return set_union(lhs, rhs); + case RT_BINARY_OP_INPLACE_XOR: + return set_symmetric_difference(lhs, rhs); + case RT_BINARY_OP_INPLACE_AND: + return set_intersect(lhs, rhs); + case RT_BINARY_OP_INPLACE_SUBTRACT: + return set_diff(2, args); + case RT_COMPARE_OP_LESS: + return set_issubset_proper(lhs, rhs); + case RT_COMPARE_OP_MORE: + return set_issuperset_proper(lhs, rhs); + case RT_COMPARE_OP_EQUAL: + return set_equal(lhs, rhs); + case RT_COMPARE_OP_LESS_EQUAL: + return set_issubset(lhs, rhs); + case RT_COMPARE_OP_MORE_EQUAL: + return set_issuperset(lhs, rhs); + case RT_COMPARE_OP_NOT_EQUAL: + return MP_BOOL(set_equal(lhs, rhs) == mp_const_false); + default: + // op not supported + return NULL; + } +} /******************************************************************************/ /* set constructors & public C API */ @@ -372,6 +437,7 @@ const mp_obj_type_t set_type = { "set", .print = set_print, .make_new = set_make_new, + .binary_op = set_binary_op, .getiter = set_getiter, .methods = set_type_methods, }; -- cgit v1.2.3 From 7244a1443930824b31498cd30f1840af648131c3 Mon Sep 17 00:00:00 2001 From: John R. Lenton Date: Sun, 12 Jan 2014 23:37:45 +0000 Subject: oops, nasty off-by-one in set_copy --- py/objset.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'py/objset.c') diff --git a/py/objset.c b/py/objset.c index 2ed2abb61..e41f2c47f 100644 --- a/py/objset.c +++ b/py/objset.c @@ -27,6 +27,10 @@ static mp_obj_t set_it_iternext(mp_obj_t self_in); void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { mp_obj_set_t *self = self_in; + if (self->set.used == 0) { + print(env, "set()"); + return; + } bool first = true; print(env, "{"); for (int i = 0; i < self->set.alloc; i++) { @@ -122,7 +126,7 @@ static mp_obj_t set_copy(mp_obj_t self_in) { mp_obj_set_t *other = m_new_obj(mp_obj_set_t); other->base.type = &set_type; - mp_set_init(&other->set, self->set.alloc); + mp_set_init(&other->set, self->set.alloc - 1); other->set.used = self->set.used; memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t)); -- cgit v1.2.3