blob: e41f2c47f4182b2762db6d33e5e4db502c4f1765 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
John R. Lenton3b0bd872014-01-12 15:56:25 +00003#include <string.h>
Damiend99b0522013-12-21 18:17:45 +00004#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien George71c51812014-01-04 20:21:15 +00009#include "mpqstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
Damien George71c51812014-01-04 20:21:15 +000011#include "runtime.h"
John R. Lentonbe790f92014-01-12 23:09:10 +000012#include "runtime0.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "map.h"
14
15typedef struct _mp_obj_set_t {
16 mp_obj_base_t base;
17 mp_set_t set;
18} mp_obj_set_t;
19
John R. Lenton0ce03b42014-01-12 15:17:42 +000020typedef struct _mp_obj_set_it_t {
21 mp_obj_base_t base;
22 mp_obj_set_t *set;
23 machine_uint_t cur;
24} mp_obj_set_it_t;
25
26static mp_obj_t set_it_iternext(mp_obj_t self_in);
27
Damiend99b0522013-12-21 18:17:45 +000028void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
29 mp_obj_set_t *self = self_in;
John R. Lenton7244a142014-01-12 23:37:45 +000030 if (self->set.used == 0) {
31 print(env, "set()");
32 return;
33 }
Damiend99b0522013-12-21 18:17:45 +000034 bool first = true;
35 print(env, "{");
36 for (int i = 0; i < self->set.alloc; i++) {
37 if (self->set.table[i] != MP_OBJ_NULL) {
38 if (!first) {
39 print(env, ", ");
40 }
41 first = false;
42 mp_obj_print_helper(print, env, self->set.table[i]);
43 }
44 }
45 print(env, "}");
46}
47
Damien George71c51812014-01-04 20:21:15 +000048static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
49 switch (n_args) {
50 case 0:
51 // return a new, empty set
52 return mp_obj_new_set(0, NULL);
53
54 case 1:
55 {
56 // 1 argument, an iterable from which we make a new set
57 mp_obj_t set = mp_obj_new_set(0, NULL);
58 mp_obj_t iterable = rt_getiter(args[0]);
59 mp_obj_t item;
60 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
61 mp_obj_set_store(set, item);
62 }
63 return set;
64 }
65
66 default:
67 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "set takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
68 }
69}
70
John R. Lenton0ce03b42014-01-12 15:17:42 +000071const mp_obj_type_t set_it_type = {
72 { &mp_const_type },
73 "set_iterator",
74 .iternext = set_it_iternext,
75};
76
77static mp_obj_t set_it_iternext(mp_obj_t self_in) {
78 assert(MP_OBJ_IS_TYPE(self_in, &set_it_type));
79 mp_obj_set_it_t *self = self_in;
80 machine_uint_t max = self->set->set.alloc;
81 mp_obj_t *table = self->set->set.table;
82
83 for (machine_uint_t i = self->cur; i < max; i++) {
84 if (table[i] != NULL) {
85 self->cur = i + 1;
86 return table[i];
87 }
88 }
89
90 return mp_const_stop_iteration;
91}
92
93static mp_obj_t set_getiter(mp_obj_t set_in) {
94 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
95 o->base.type = &set_it_type;
96 o->set = (mp_obj_set_t *)set_in;
97 o->cur = 0;
98 return o;
99}
100
John R. Lenton19b14d32014-01-12 15:29:11 +0000101
102/******************************************************************************/
103/* set methods */
104
105static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
106 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
107 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000108 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000109 return mp_const_none;
110}
111static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
112
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000113static mp_obj_t set_clear(mp_obj_t self_in) {
114 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
115 mp_obj_set_t *self = self_in;
116
117 mp_set_clear(&self->set);
118
119 return mp_const_none;
120}
121static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
122
John R. Lenton3b0bd872014-01-12 15:56:25 +0000123static mp_obj_t set_copy(mp_obj_t self_in) {
124 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
125 mp_obj_set_t *self = self_in;
126
127 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
128 other->base.type = &set_type;
John R. Lenton7244a142014-01-12 23:37:45 +0000129 mp_set_init(&other->set, self->set.alloc - 1);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000130 other->set.used = self->set.used;
131 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
132
133 return other;
134}
135static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
136
John R. Lenton2a241722014-01-12 16:39:39 +0000137static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
138 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
139 mp_obj_set_t *self = self_in;
140 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
141 return mp_const_none;
142}
143static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000144
John R. Lenton032129f2014-01-12 17:07:17 +0000145static mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
146 assert(n_args > 0);
147 assert(MP_OBJ_IS_TYPE(args[0], &set_type));
148 mp_obj_set_t *self;
149 if (update) {
150 self = args[0];
151 } else {
152 self = set_copy(args[0]);
153 }
154
155
156 for (int i = 1; i < n_args; i++) {
157 mp_obj_t other = args[i];
158 if (self == other) {
159 set_clear(self);
160 } else {
161 mp_obj_t iter = rt_getiter(other);
162 mp_obj_t next;
163 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
164 set_discard(self, next);
165 }
166 }
167 }
168
169 return self;
170}
171
172static mp_obj_t set_diff(int n_args, const mp_obj_t *args) {
173 return set_diff_int(n_args, args, false);
174}
175static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
176
177static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) {
178 set_diff_int(n_args, args, true);
179 return mp_const_none;
180}
181static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
182
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000183static mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
184 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
185 if (self_in == other) {
186 return update ? mp_const_none : set_copy(self_in);
187 }
188
189 mp_obj_set_t *self = self_in;
190 mp_obj_set_t *out = mp_obj_new_set(0, NULL);
191
192 mp_obj_t iter = rt_getiter(other);
193 mp_obj_t next;
194 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
195 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
196 set_add(out, next);
197 }
198 }
199
200 if (update) {
201 m_del(mp_obj_t, self->set.table, self->set.alloc);
202 self->set.alloc = out->set.alloc;
203 self->set.used = out->set.used;
204 self->set.table = out->set.table;
205 }
206
207 return update ? mp_const_none : out;
208}
209
210static mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
211 return set_intersect_int(self_in, other, false);
212}
213static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
214
215static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
216 return set_intersect_int(self_in, other, true);
217}
218static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
219
John R. Lenton4a080672014-01-12 18:03:21 +0000220static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
221 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
222 mp_obj_set_t *self = self_in;
223
224 mp_obj_t iter = rt_getiter(other);
225 mp_obj_t next;
226 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
227 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
228 return mp_const_false;
229 }
230 }
231 return mp_const_true;
232}
233static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
234
John R. Lentonbe790f92014-01-12 23:09:10 +0000235static 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 +0000236 mp_obj_set_t *self;
237 bool cleanup_self = false;
238 if (MP_OBJ_IS_TYPE(self_in, &set_type)) {
239 self = self_in;
240 } else {
241 self = set_make_new(NULL, 1, &self_in);
242 cleanup_self = true;
243 }
244
245 mp_obj_set_t *other;
246 bool cleanup_other = false;
247 if (MP_OBJ_IS_TYPE(other_in, &set_type)) {
248 other = other_in;
249 } else {
250 other = set_make_new(NULL, 1, &other_in);
251 cleanup_other = true;
252 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000253 bool out = true;
254 if (proper && self->set.used == other->set.used) {
255 out = false;
256 } else {
257 mp_obj_t iter = set_getiter(self);
258 mp_obj_t next;
259 while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) {
260 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
261 out = false;
262 break;
263 }
John R. Lentonae00d332014-01-12 18:23:36 +0000264 }
265 }
266 if (cleanup_self) {
267 set_clear(self);
268 }
269 if (cleanup_other) {
270 set_clear(other);
271 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000272 return MP_BOOL(out);
273}
274static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
275 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000276}
277static MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
278
John R. Lentonbe790f92014-01-12 23:09:10 +0000279static mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
280 return set_issubset_internal(self_in, other_in, true);
281}
282
John R. Lentonae00d332014-01-12 18:23:36 +0000283static mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000284 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000285}
286static MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
287
John R. Lentonbe790f92014-01-12 23:09:10 +0000288static mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
289 return set_issubset_internal(other_in, self_in, true);
290}
291
292static mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
293 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
294 mp_obj_set_t *self = self_in;
295 if (!MP_OBJ_IS_TYPE(other_in, &set_type)) {
296 return mp_const_false;
297 }
298 mp_obj_set_t *other = other_in;
299 if (self->set.used != other->set.used) {
300 return mp_const_false;
301 }
302 return set_issubset(self_in, other_in);
303}
304
John R. Lentonae00d332014-01-12 18:23:36 +0000305static mp_obj_t set_pop(mp_obj_t self_in) {
306 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
307 mp_obj_set_t *self = self_in;
308
309 if (self->set.used == 0) {
310 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "pop from an empty set"));
311 }
312 mp_obj_t obj = mp_set_lookup(&self->set, NULL,
313 MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST);
314 return obj;
315}
316static MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
317
318static mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
319 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
320 mp_obj_set_t *self = self_in;
321 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
322 nlr_jump(mp_obj_new_exception(MP_QSTR_KeyError));
323 }
324 return mp_const_none;
325}
326static MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000327
John R. Lenton0de386b2014-01-12 19:39:48 +0000328static mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
329 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
330 mp_obj_set_t *self = self_in;
331 mp_obj_t iter = rt_getiter(other_in);
332 mp_obj_t next;
333 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
334 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
335 }
336 return mp_const_none;
337}
338static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
339
340static mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
341 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
342 self_in = set_copy(self_in);
343 set_symmetric_difference_update(self_in, other_in);
344 return self_in;
345}
346static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
347
348static void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
349 mp_obj_t iter = rt_getiter(other_in);
350 mp_obj_t next;
351 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
352 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
353 }
354}
355
356static mp_obj_t set_update(int n_args, const mp_obj_t *args) {
357 assert(n_args > 0);
358 assert(MP_OBJ_IS_TYPE(args[0], &set_type));
359
360 for (int i = 1; i < n_args; i++) {
361 set_update_int(args[0], args[i]);
362 }
363
364 return mp_const_none;
365}
366static MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
367
368static mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
369 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
370 mp_obj_set_t *self = set_copy(self_in);
371 set_update_int(self, other_in);
372 return self;
373}
374static MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
375
376
John R. Lentonbe790f92014-01-12 23:09:10 +0000377static mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
378 mp_obj_t args[] = {lhs, rhs};
379 switch (op) {
380 case RT_BINARY_OP_OR:
381 return set_union(lhs, rhs);
382 case RT_BINARY_OP_XOR:
383 return set_symmetric_difference(lhs, rhs);
384 case RT_BINARY_OP_AND:
385 return set_intersect(lhs, rhs);
386 case RT_BINARY_OP_SUBTRACT:
387 return set_diff(2, args);
388 case RT_BINARY_OP_INPLACE_OR:
389 return set_union(lhs, rhs);
390 case RT_BINARY_OP_INPLACE_XOR:
391 return set_symmetric_difference(lhs, rhs);
392 case RT_BINARY_OP_INPLACE_AND:
393 return set_intersect(lhs, rhs);
394 case RT_BINARY_OP_INPLACE_SUBTRACT:
395 return set_diff(2, args);
396 case RT_COMPARE_OP_LESS:
397 return set_issubset_proper(lhs, rhs);
398 case RT_COMPARE_OP_MORE:
399 return set_issuperset_proper(lhs, rhs);
400 case RT_COMPARE_OP_EQUAL:
401 return set_equal(lhs, rhs);
402 case RT_COMPARE_OP_LESS_EQUAL:
403 return set_issubset(lhs, rhs);
404 case RT_COMPARE_OP_MORE_EQUAL:
405 return set_issuperset(lhs, rhs);
406 case RT_COMPARE_OP_NOT_EQUAL:
407 return MP_BOOL(set_equal(lhs, rhs) == mp_const_false);
408 default:
409 // op not supported
410 return NULL;
411 }
412}
John R. Lenton0de386b2014-01-12 19:39:48 +0000413
John R. Lenton19b14d32014-01-12 15:29:11 +0000414/******************************************************************************/
415/* set constructors & public C API */
416
417
418static const mp_method_t set_type_methods[] = {
419 { "add", &set_add_obj },
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000420 { "clear", &set_clear_obj },
John R. Lenton3b0bd872014-01-12 15:56:25 +0000421 { "copy", &set_copy_obj },
John R. Lenton2a241722014-01-12 16:39:39 +0000422 { "discard", &set_discard_obj },
John R. Lenton032129f2014-01-12 17:07:17 +0000423 { "difference", &set_diff_obj },
424 { "difference_update", &set_diff_update_obj },
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000425 { "intersection", &set_intersect_obj },
426 { "intersection_update", &set_intersect_update_obj },
John R. Lenton4a080672014-01-12 18:03:21 +0000427 { "isdisjoint", &set_isdisjoint_obj },
John R. Lentonae00d332014-01-12 18:23:36 +0000428 { "issubset", &set_issubset_obj },
429 { "issuperset", &set_issuperset_obj },
430 { "pop", &set_pop_obj },
431 { "remove", &set_remove_obj },
John R. Lenton0de386b2014-01-12 19:39:48 +0000432 { "symmetric_difference", &set_symmetric_difference_obj },
433 { "symmetric_difference_update", &set_symmetric_difference_update_obj },
434 { "union", &set_union_obj },
435 { "update", &set_update_obj },
John R. Lenton19b14d32014-01-12 15:29:11 +0000436 { NULL, NULL }, // end-of-list sentinel
437};
438
Damien George71c51812014-01-04 20:21:15 +0000439const mp_obj_type_t set_type = {
Damiend99b0522013-12-21 18:17:45 +0000440 { &mp_const_type },
441 "set",
Damien George97209d32014-01-07 15:58:30 +0000442 .print = set_print,
443 .make_new = set_make_new,
John R. Lentonbe790f92014-01-12 23:09:10 +0000444 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000445 .getiter = set_getiter,
John R. Lenton19b14d32014-01-12 15:29:11 +0000446 .methods = set_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000447};
448
449mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
450 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
451 o->base.type = &set_type;
452 mp_set_init(&o->set, n_args);
453 for (int i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000454 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000455 }
456 return o;
457}
458
459void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
460 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
461 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000462 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000463}