blob: bdc8d7f382e41a11871b188bde6a28345ca8f31e [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
John R. Lenton3b0bd872014-01-12 15:56:25 +00002#include <string.h>
Damiend99b0522013-12-21 18:17:45 +00003#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "obj.h"
Damien George71c51812014-01-04 20:21:15 +000010#include "runtime.h"
John R. Lentonc1bef212014-01-11 12:39:33 +000011#include "runtime0.h"
Paul Sokolovsky68e7c512014-04-13 11:53:15 +030012#include "builtin.h"
Damiend99b0522013-12-21 18:17:45 +000013
14typedef struct _mp_obj_set_t {
15 mp_obj_base_t base;
16 mp_set_t set;
17} mp_obj_set_t;
18
John R. Lenton0ce03b42014-01-12 15:17:42 +000019typedef struct _mp_obj_set_it_t {
20 mp_obj_base_t base;
21 mp_obj_set_t *set;
22 machine_uint_t cur;
23} mp_obj_set_it_t;
24
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020025STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
John R. Lenton0ce03b42014-01-12 15:17:42 +000026
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020027STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000028 mp_obj_set_t *self = self_in;
John R. Lenton7244a142014-01-12 23:37:45 +000029 if (self->set.used == 0) {
30 print(env, "set()");
31 return;
32 }
Damiend99b0522013-12-21 18:17:45 +000033 bool first = true;
34 print(env, "{");
35 for (int i = 0; i < self->set.alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +010036 if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
Damiend99b0522013-12-21 18:17:45 +000037 if (!first) {
38 print(env, ", ");
39 }
40 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020041 mp_obj_print_helper(print, env, self->set.table[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000042 }
43 }
44 print(env, "}");
45}
46
John R. Lentonc1bef212014-01-11 12:39:33 +000047
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020048STATIC mp_obj_t set_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000049 // TODO check n_kw == 0
50
Damien George71c51812014-01-04 20:21:15 +000051 switch (n_args) {
52 case 0:
53 // return a new, empty set
54 return mp_obj_new_set(0, NULL);
55
56 case 1:
57 {
58 // 1 argument, an iterable from which we make a new set
59 mp_obj_t set = mp_obj_new_set(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +010060 mp_obj_t iterable = mp_getiter(args[0]);
Damien George71c51812014-01-04 20:21:15 +000061 mp_obj_t item;
Damien Georged17926d2014-03-30 13:35:08 +010062 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
Damien George71c51812014-01-04 20:21:15 +000063 mp_obj_set_store(set, item);
64 }
65 return set;
66 }
67
68 default:
Damien Georgeea13f402014-04-05 18:32:08 +010069 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "set takes at most 1 argument, %d given", n_args));
Damien George71c51812014-01-04 20:21:15 +000070 }
71}
72
Damien George3e1a5c12014-03-29 13:43:38 +000073const mp_obj_type_t mp_type_set_it = {
Damien Georgec5966122014-02-15 16:10:44 +000074 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000075 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +030076 .getiter = mp_identity,
John R. Lenton0ce03b42014-01-12 15:17:42 +000077 .iternext = set_it_iternext,
78};
79
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020080STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +000081 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set_it));
John R. Lenton0ce03b42014-01-12 15:17:42 +000082 mp_obj_set_it_t *self = self_in;
83 machine_uint_t max = self->set->set.alloc;
Damien George8b0535e2014-04-05 21:53:54 +010084 mp_set_t *set = &self->set->set;
John R. Lenton0ce03b42014-01-12 15:17:42 +000085
86 for (machine_uint_t i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +010087 if (MP_SET_SLOT_IS_FILLED(set, i)) {
John R. Lenton0ce03b42014-01-12 15:17:42 +000088 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +010089 return set->table[i];
John R. Lenton0ce03b42014-01-12 15:17:42 +000090 }
91 }
92
Damien George66eaf842014-03-26 19:27:58 +000093 return MP_OBJ_NULL;
John R. Lenton0ce03b42014-01-12 15:17:42 +000094}
95
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020096STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
John R. Lenton0ce03b42014-01-12 15:17:42 +000097 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +000098 o->base.type = &mp_type_set_it;
John R. Lenton0ce03b42014-01-12 15:17:42 +000099 o->set = (mp_obj_set_t *)set_in;
100 o->cur = 0;
101 return o;
102}
103
John R. Lenton19b14d32014-01-12 15:29:11 +0000104
105/******************************************************************************/
106/* set methods */
107
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200108STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000109 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton19b14d32014-01-12 15:29:11 +0000110 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000111 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000112 return mp_const_none;
113}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200114STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton19b14d32014-01-12 15:29:11 +0000115
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200116STATIC mp_obj_t set_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000117 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000118 mp_obj_set_t *self = self_in;
119
120 mp_set_clear(&self->set);
121
122 return mp_const_none;
123}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200124STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000125
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200126STATIC mp_obj_t set_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000127 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton3b0bd872014-01-12 15:56:25 +0000128 mp_obj_set_t *self = self_in;
129
130 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000131 other->base.type = &mp_type_set;
Paul Sokolovsky46bd12d2014-04-07 03:07:21 +0300132 mp_set_init(&other->set, self->set.alloc);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000133 other->set.used = self->set.used;
134 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
135
136 return other;
137}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200138STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000139
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200140STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000141 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton2a241722014-01-12 16:39:39 +0000142 mp_obj_set_t *self = self_in;
143 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
144 return mp_const_none;
145}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200146STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000147
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200148STATIC mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
John R. Lenton032129f2014-01-12 17:07:17 +0000149 assert(n_args > 0);
Damien George3e1a5c12014-03-29 13:43:38 +0000150 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_set));
John R. Lenton032129f2014-01-12 17:07:17 +0000151 mp_obj_set_t *self;
152 if (update) {
153 self = args[0];
154 } else {
155 self = set_copy(args[0]);
156 }
157
158
159 for (int i = 1; i < n_args; i++) {
160 mp_obj_t other = args[i];
161 if (self == other) {
162 set_clear(self);
163 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100164 mp_obj_t iter = mp_getiter(other);
John R. Lenton032129f2014-01-12 17:07:17 +0000165 mp_obj_t next;
Damien Georged17926d2014-03-30 13:35:08 +0100166 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton032129f2014-01-12 17:07:17 +0000167 set_discard(self, next);
168 }
169 }
170 }
171
172 return self;
173}
174
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200175STATIC mp_obj_t set_diff(uint n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000176 return set_diff_int(n_args, args, false);
177}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200178STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
John R. Lenton032129f2014-01-12 17:07:17 +0000179
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200180STATIC mp_obj_t set_diff_update(uint n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000181 set_diff_int(n_args, args, true);
182 return mp_const_none;
183}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200184STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
John R. Lenton032129f2014-01-12 17:07:17 +0000185
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200186STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
Damien George3e1a5c12014-03-29 13:43:38 +0000187 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000188 if (self_in == other) {
189 return update ? mp_const_none : set_copy(self_in);
190 }
191
192 mp_obj_set_t *self = self_in;
193 mp_obj_set_t *out = mp_obj_new_set(0, NULL);
194
Damien Georged17926d2014-03-30 13:35:08 +0100195 mp_obj_t iter = mp_getiter(other);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000196 mp_obj_t next;
Damien Georged17926d2014-03-30 13:35:08 +0100197 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000198 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
199 set_add(out, next);
200 }
201 }
202
203 if (update) {
204 m_del(mp_obj_t, self->set.table, self->set.alloc);
205 self->set.alloc = out->set.alloc;
206 self->set.used = out->set.used;
207 self->set.table = out->set.table;
208 }
209
210 return update ? mp_const_none : out;
211}
212
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200213STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000214 return set_intersect_int(self_in, other, false);
215}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200216STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000217
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200218STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000219 return set_intersect_int(self_in, other, true);
220}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200221STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000222
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200223STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
Damien George3e1a5c12014-03-29 13:43:38 +0000224 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton4a080672014-01-12 18:03:21 +0000225 mp_obj_set_t *self = self_in;
226
Damien Georged17926d2014-03-30 13:35:08 +0100227 mp_obj_t iter = mp_getiter(other);
John R. Lenton4a080672014-01-12 18:03:21 +0000228 mp_obj_t next;
Damien Georged17926d2014-03-30 13:35:08 +0100229 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton4a080672014-01-12 18:03:21 +0000230 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
231 return mp_const_false;
232 }
233 }
234 return mp_const_true;
235}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200236STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton4a080672014-01-12 18:03:21 +0000237
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200238STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) {
John R. Lentonae00d332014-01-12 18:23:36 +0000239 mp_obj_set_t *self;
240 bool cleanup_self = false;
Damien George3e1a5c12014-03-29 13:43:38 +0000241 if (MP_OBJ_IS_TYPE(self_in, &mp_type_set)) {
John R. Lentonae00d332014-01-12 18:23:36 +0000242 self = self_in;
243 } else {
Damien George3e1a5c12014-03-29 13:43:38 +0000244 self = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000245 cleanup_self = true;
246 }
247
248 mp_obj_set_t *other;
249 bool cleanup_other = false;
Damien George3e1a5c12014-03-29 13:43:38 +0000250 if (MP_OBJ_IS_TYPE(other_in, &mp_type_set)) {
John R. Lentonae00d332014-01-12 18:23:36 +0000251 other = other_in;
252 } else {
Damien George3e1a5c12014-03-29 13:43:38 +0000253 other = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &other_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000254 cleanup_other = true;
255 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000256 bool out = true;
257 if (proper && self->set.used == other->set.used) {
258 out = false;
259 } else {
260 mp_obj_t iter = set_getiter(self);
261 mp_obj_t next;
Damien George66eaf842014-03-26 19:27:58 +0000262 while ((next = set_it_iternext(iter)) != MP_OBJ_NULL) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000263 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
264 out = false;
265 break;
266 }
John R. Lentonae00d332014-01-12 18:23:36 +0000267 }
268 }
269 if (cleanup_self) {
270 set_clear(self);
271 }
272 if (cleanup_other) {
273 set_clear(other);
274 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000275 return MP_BOOL(out);
276}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200277STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000278 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000279}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200280STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lentonae00d332014-01-12 18:23:36 +0000281
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200282STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000283 return set_issubset_internal(self_in, other_in, true);
284}
285
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200286STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000287 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000288}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200289STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lentonae00d332014-01-12 18:23:36 +0000290
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200291STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000292 return set_issubset_internal(other_in, self_in, true);
293}
294
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200295STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000296 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonbe790f92014-01-12 23:09:10 +0000297 mp_obj_set_t *self = self_in;
Damien George3e1a5c12014-03-29 13:43:38 +0000298 if (!MP_OBJ_IS_TYPE(other_in, &mp_type_set)) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000299 return mp_const_false;
300 }
301 mp_obj_set_t *other = other_in;
302 if (self->set.used != other->set.used) {
303 return mp_const_false;
304 }
305 return set_issubset(self_in, other_in);
306}
307
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200308STATIC mp_obj_t set_pop(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000309 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonae00d332014-01-12 18:23:36 +0000310 mp_obj_set_t *self = self_in;
Damien George95004e52014-04-05 17:17:19 +0100311 mp_obj_t obj = mp_set_remove_first(&self->set);
312 if (obj == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100313 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "pop from an empty set"));
John R. Lentonae00d332014-01-12 18:23:36 +0000314 }
John R. Lentonae00d332014-01-12 18:23:36 +0000315 return obj;
316}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200317STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lentonae00d332014-01-12 18:23:36 +0000318
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200319STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000320 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonae00d332014-01-12 18:23:36 +0000321 mp_obj_set_t *self = self_in;
322 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100323 nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lentonae00d332014-01-12 18:23:36 +0000324 }
325 return mp_const_none;
326}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200327STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000328
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200329STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000330 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000331 mp_obj_set_t *self = self_in;
Damien Georged17926d2014-03-30 13:35:08 +0100332 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000333 mp_obj_t next;
Damien Georged17926d2014-03-30 13:35:08 +0100334 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000335 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
336 }
337 return mp_const_none;
338}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200339STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000340
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200341STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000342 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000343 self_in = set_copy(self_in);
344 set_symmetric_difference_update(self_in, other_in);
345 return self_in;
346}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200347STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton0de386b2014-01-12 19:39:48 +0000348
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200349STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100350 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000351 mp_obj_t next;
Damien Georged17926d2014-03-30 13:35:08 +0100352 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000353 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
354 }
355}
356
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200357STATIC mp_obj_t set_update(uint n_args, const mp_obj_t *args) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000358 assert(n_args > 0);
Damien George3e1a5c12014-03-29 13:43:38 +0000359 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000360
361 for (int i = 1; i < n_args; i++) {
362 set_update_int(args[0], args[i]);
363 }
364
365 return mp_const_none;
366}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200367STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000368
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200369STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000370 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000371 mp_obj_set_t *self = set_copy(self_in);
372 set_update_int(self, other_in);
373 return self;
374}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200375STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton0de386b2014-01-12 19:39:48 +0000376
Damien George95004e52014-04-05 17:17:19 +0100377STATIC mp_obj_t set_unary_op(int op, mp_obj_t self_in) {
378 mp_obj_set_t *self = self_in;
379 switch (op) {
380 case MP_UNARY_OP_BOOL: return MP_BOOL(self->set.used != 0);
381 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->set.used);
382 default: return MP_OBJ_NULL; // op not supported for None
383 }
384}
John R. Lenton0de386b2014-01-12 19:39:48 +0000385
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200386STATIC mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000387 mp_obj_t args[] = {lhs, rhs};
388 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100389 case MP_BINARY_OP_OR:
John R. Lentonbe790f92014-01-12 23:09:10 +0000390 return set_union(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100391 case MP_BINARY_OP_XOR:
John R. Lentonbe790f92014-01-12 23:09:10 +0000392 return set_symmetric_difference(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100393 case MP_BINARY_OP_AND:
John R. Lentonbe790f92014-01-12 23:09:10 +0000394 return set_intersect(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100395 case MP_BINARY_OP_SUBTRACT:
John R. Lentonbe790f92014-01-12 23:09:10 +0000396 return set_diff(2, args);
Damien Georged17926d2014-03-30 13:35:08 +0100397 case MP_BINARY_OP_INPLACE_OR:
John R. Lentonbe790f92014-01-12 23:09:10 +0000398 return set_union(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100399 case MP_BINARY_OP_INPLACE_XOR:
John R. Lentonbe790f92014-01-12 23:09:10 +0000400 return set_symmetric_difference(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100401 case MP_BINARY_OP_INPLACE_AND:
John R. Lentonbe790f92014-01-12 23:09:10 +0000402 return set_intersect(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100403 case MP_BINARY_OP_INPLACE_SUBTRACT:
John R. Lentonbe790f92014-01-12 23:09:10 +0000404 return set_diff(2, args);
Damien Georged17926d2014-03-30 13:35:08 +0100405 case MP_BINARY_OP_LESS:
John R. Lentonbe790f92014-01-12 23:09:10 +0000406 return set_issubset_proper(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100407 case MP_BINARY_OP_MORE:
John R. Lentonbe790f92014-01-12 23:09:10 +0000408 return set_issuperset_proper(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100409 case MP_BINARY_OP_EQUAL:
John R. Lentonbe790f92014-01-12 23:09:10 +0000410 return set_equal(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100411 case MP_BINARY_OP_LESS_EQUAL:
John R. Lentonbe790f92014-01-12 23:09:10 +0000412 return set_issubset(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100413 case MP_BINARY_OP_MORE_EQUAL:
John R. Lentonbe790f92014-01-12 23:09:10 +0000414 return set_issuperset(lhs, rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100415 case MP_BINARY_OP_IN:
John R. Lenton189c8e12014-01-13 00:52:06 +0000416 {
417 mp_obj_set_t *o = lhs;
418 mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +0000419 return MP_BOOL(elem != NULL);
John R. Lenton189c8e12014-01-13 00:52:06 +0000420 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000421 default:
422 // op not supported
423 return NULL;
424 }
425}
John R. Lenton0de386b2014-01-12 19:39:48 +0000426
John R. Lenton19b14d32014-01-12 15:29:11 +0000427/******************************************************************************/
428/* set constructors & public C API */
429
430
Damien George9b196cd2014-03-26 21:47:19 +0000431STATIC const mp_map_elem_t set_locals_dict_table[] = {
432 { MP_OBJ_NEW_QSTR(MP_QSTR_add), (mp_obj_t)&set_add_obj },
433 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&set_clear_obj },
434 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&set_copy_obj },
435 { MP_OBJ_NEW_QSTR(MP_QSTR_discard), (mp_obj_t)&set_discard_obj },
436 { MP_OBJ_NEW_QSTR(MP_QSTR_difference), (mp_obj_t)&set_diff_obj },
437 { MP_OBJ_NEW_QSTR(MP_QSTR_difference_update), (mp_obj_t)&set_diff_update_obj },
438 { MP_OBJ_NEW_QSTR(MP_QSTR_intersection), (mp_obj_t)&set_intersect_obj },
439 { MP_OBJ_NEW_QSTR(MP_QSTR_intersection_update), (mp_obj_t)&set_intersect_update_obj },
440 { MP_OBJ_NEW_QSTR(MP_QSTR_isdisjoint), (mp_obj_t)&set_isdisjoint_obj },
441 { MP_OBJ_NEW_QSTR(MP_QSTR_issubset), (mp_obj_t)&set_issubset_obj },
442 { MP_OBJ_NEW_QSTR(MP_QSTR_issuperset), (mp_obj_t)&set_issuperset_obj },
443 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&set_pop_obj },
444 { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&set_remove_obj },
445 { MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference), (mp_obj_t)&set_symmetric_difference_obj },
446 { MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference_update), (mp_obj_t)&set_symmetric_difference_update_obj },
447 { MP_OBJ_NEW_QSTR(MP_QSTR_union), (mp_obj_t)&set_union_obj },
448 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&set_update_obj },
Paul Sokolovsky68e7c512014-04-13 11:53:15 +0300449 { MP_OBJ_NEW_QSTR(MP_QSTR___contains__), (mp_obj_t)&mp_op_contains_obj },
John R. Lenton19b14d32014-01-12 15:29:11 +0000450};
451
Damien George9b196cd2014-03-26 21:47:19 +0000452STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
453
Damien George3e1a5c12014-03-29 13:43:38 +0000454const mp_obj_type_t mp_type_set = {
Damien Georgec5966122014-02-15 16:10:44 +0000455 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000456 .name = MP_QSTR_set,
Damien George97209d32014-01-07 15:58:30 +0000457 .print = set_print,
458 .make_new = set_make_new,
Damien George95004e52014-04-05 17:17:19 +0100459 .unary_op = set_unary_op,
John R. Lentonbe790f92014-01-12 23:09:10 +0000460 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000461 .getiter = set_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000462 .locals_dict = (mp_obj_t)&set_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000463};
464
465mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
466 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000467 o->base.type = &mp_type_set;
Damiend99b0522013-12-21 18:17:45 +0000468 mp_set_init(&o->set, n_args);
469 for (int i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000470 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000471 }
472 return o;
473}
474
475void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000476 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
Damiend99b0522013-12-21 18:17:45 +0000477 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000478 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000479}