blob: 2ed2abb611cc1482f4cdfb9fe2b338db33df3842 [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;
30 bool first = true;
31 print(env, "{");
32 for (int i = 0; i < self->set.alloc; i++) {
33 if (self->set.table[i] != MP_OBJ_NULL) {
34 if (!first) {
35 print(env, ", ");
36 }
37 first = false;
38 mp_obj_print_helper(print, env, self->set.table[i]);
39 }
40 }
41 print(env, "}");
42}
43
Damien George71c51812014-01-04 20:21:15 +000044static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
45 switch (n_args) {
46 case 0:
47 // return a new, empty set
48 return mp_obj_new_set(0, NULL);
49
50 case 1:
51 {
52 // 1 argument, an iterable from which we make a new set
53 mp_obj_t set = mp_obj_new_set(0, NULL);
54 mp_obj_t iterable = rt_getiter(args[0]);
55 mp_obj_t item;
56 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
57 mp_obj_set_store(set, item);
58 }
59 return set;
60 }
61
62 default:
63 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));
64 }
65}
66
John R. Lenton0ce03b42014-01-12 15:17:42 +000067const mp_obj_type_t set_it_type = {
68 { &mp_const_type },
69 "set_iterator",
70 .iternext = set_it_iternext,
71};
72
73static mp_obj_t set_it_iternext(mp_obj_t self_in) {
74 assert(MP_OBJ_IS_TYPE(self_in, &set_it_type));
75 mp_obj_set_it_t *self = self_in;
76 machine_uint_t max = self->set->set.alloc;
77 mp_obj_t *table = self->set->set.table;
78
79 for (machine_uint_t i = self->cur; i < max; i++) {
80 if (table[i] != NULL) {
81 self->cur = i + 1;
82 return table[i];
83 }
84 }
85
86 return mp_const_stop_iteration;
87}
88
89static mp_obj_t set_getiter(mp_obj_t set_in) {
90 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
91 o->base.type = &set_it_type;
92 o->set = (mp_obj_set_t *)set_in;
93 o->cur = 0;
94 return o;
95}
96
John R. Lenton19b14d32014-01-12 15:29:11 +000097
98/******************************************************************************/
99/* set methods */
100
101static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
102 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
103 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000104 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000105 return mp_const_none;
106}
107static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
108
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000109static mp_obj_t set_clear(mp_obj_t self_in) {
110 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
111 mp_obj_set_t *self = self_in;
112
113 mp_set_clear(&self->set);
114
115 return mp_const_none;
116}
117static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
118
John R. Lenton3b0bd872014-01-12 15:56:25 +0000119static mp_obj_t set_copy(mp_obj_t self_in) {
120 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
121 mp_obj_set_t *self = self_in;
122
123 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
124 other->base.type = &set_type;
125 mp_set_init(&other->set, self->set.alloc);
126 other->set.used = self->set.used;
127 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
128
129 return other;
130}
131static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
132
John R. Lenton2a241722014-01-12 16:39:39 +0000133static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
134 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
135 mp_obj_set_t *self = self_in;
136 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
137 return mp_const_none;
138}
139static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000140
John R. Lenton032129f2014-01-12 17:07:17 +0000141static mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
142 assert(n_args > 0);
143 assert(MP_OBJ_IS_TYPE(args[0], &set_type));
144 mp_obj_set_t *self;
145 if (update) {
146 self = args[0];
147 } else {
148 self = set_copy(args[0]);
149 }
150
151
152 for (int i = 1; i < n_args; i++) {
153 mp_obj_t other = args[i];
154 if (self == other) {
155 set_clear(self);
156 } else {
157 mp_obj_t iter = rt_getiter(other);
158 mp_obj_t next;
159 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
160 set_discard(self, next);
161 }
162 }
163 }
164
165 return self;
166}
167
168static mp_obj_t set_diff(int n_args, const mp_obj_t *args) {
169 return set_diff_int(n_args, args, false);
170}
171static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
172
173static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) {
174 set_diff_int(n_args, args, true);
175 return mp_const_none;
176}
177static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
178
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000179static mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
180 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
181 if (self_in == other) {
182 return update ? mp_const_none : set_copy(self_in);
183 }
184
185 mp_obj_set_t *self = self_in;
186 mp_obj_set_t *out = mp_obj_new_set(0, NULL);
187
188 mp_obj_t iter = rt_getiter(other);
189 mp_obj_t next;
190 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
191 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
192 set_add(out, next);
193 }
194 }
195
196 if (update) {
197 m_del(mp_obj_t, self->set.table, self->set.alloc);
198 self->set.alloc = out->set.alloc;
199 self->set.used = out->set.used;
200 self->set.table = out->set.table;
201 }
202
203 return update ? mp_const_none : out;
204}
205
206static mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
207 return set_intersect_int(self_in, other, false);
208}
209static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
210
211static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
212 return set_intersect_int(self_in, other, true);
213}
214static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
215
John R. Lenton4a080672014-01-12 18:03:21 +0000216static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
217 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
218 mp_obj_set_t *self = self_in;
219
220 mp_obj_t iter = rt_getiter(other);
221 mp_obj_t next;
222 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
223 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
224 return mp_const_false;
225 }
226 }
227 return mp_const_true;
228}
229static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
230
John R. Lentonbe790f92014-01-12 23:09:10 +0000231static 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 +0000232 mp_obj_set_t *self;
233 bool cleanup_self = false;
234 if (MP_OBJ_IS_TYPE(self_in, &set_type)) {
235 self = self_in;
236 } else {
237 self = set_make_new(NULL, 1, &self_in);
238 cleanup_self = true;
239 }
240
241 mp_obj_set_t *other;
242 bool cleanup_other = false;
243 if (MP_OBJ_IS_TYPE(other_in, &set_type)) {
244 other = other_in;
245 } else {
246 other = set_make_new(NULL, 1, &other_in);
247 cleanup_other = true;
248 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000249 bool out = true;
250 if (proper && self->set.used == other->set.used) {
251 out = false;
252 } else {
253 mp_obj_t iter = set_getiter(self);
254 mp_obj_t next;
255 while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) {
256 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
257 out = false;
258 break;
259 }
John R. Lentonae00d332014-01-12 18:23:36 +0000260 }
261 }
262 if (cleanup_self) {
263 set_clear(self);
264 }
265 if (cleanup_other) {
266 set_clear(other);
267 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000268 return MP_BOOL(out);
269}
270static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
271 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000272}
273static MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
274
John R. Lentonbe790f92014-01-12 23:09:10 +0000275static mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
276 return set_issubset_internal(self_in, other_in, true);
277}
278
John R. Lentonae00d332014-01-12 18:23:36 +0000279static mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000280 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000281}
282static MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
283
John R. Lentonbe790f92014-01-12 23:09:10 +0000284static mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
285 return set_issubset_internal(other_in, self_in, true);
286}
287
288static mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
289 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
290 mp_obj_set_t *self = self_in;
291 if (!MP_OBJ_IS_TYPE(other_in, &set_type)) {
292 return mp_const_false;
293 }
294 mp_obj_set_t *other = other_in;
295 if (self->set.used != other->set.used) {
296 return mp_const_false;
297 }
298 return set_issubset(self_in, other_in);
299}
300
John R. Lentonae00d332014-01-12 18:23:36 +0000301static mp_obj_t set_pop(mp_obj_t self_in) {
302 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
303 mp_obj_set_t *self = self_in;
304
305 if (self->set.used == 0) {
306 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "pop from an empty set"));
307 }
308 mp_obj_t obj = mp_set_lookup(&self->set, NULL,
309 MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST);
310 return obj;
311}
312static MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
313
314static mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
315 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
316 mp_obj_set_t *self = self_in;
317 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
318 nlr_jump(mp_obj_new_exception(MP_QSTR_KeyError));
319 }
320 return mp_const_none;
321}
322static MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000323
John R. Lenton0de386b2014-01-12 19:39:48 +0000324static mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
325 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
326 mp_obj_set_t *self = self_in;
327 mp_obj_t iter = rt_getiter(other_in);
328 mp_obj_t next;
329 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
330 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
331 }
332 return mp_const_none;
333}
334static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
335
336static mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
337 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
338 self_in = set_copy(self_in);
339 set_symmetric_difference_update(self_in, other_in);
340 return self_in;
341}
342static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
343
344static void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
345 mp_obj_t iter = rt_getiter(other_in);
346 mp_obj_t next;
347 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
348 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
349 }
350}
351
352static mp_obj_t set_update(int n_args, const mp_obj_t *args) {
353 assert(n_args > 0);
354 assert(MP_OBJ_IS_TYPE(args[0], &set_type));
355
356 for (int i = 1; i < n_args; i++) {
357 set_update_int(args[0], args[i]);
358 }
359
360 return mp_const_none;
361}
362static MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
363
364static mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
365 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
366 mp_obj_set_t *self = set_copy(self_in);
367 set_update_int(self, other_in);
368 return self;
369}
370static MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
371
372
John R. Lentonbe790f92014-01-12 23:09:10 +0000373static mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
374 mp_obj_t args[] = {lhs, rhs};
375 switch (op) {
376 case RT_BINARY_OP_OR:
377 return set_union(lhs, rhs);
378 case RT_BINARY_OP_XOR:
379 return set_symmetric_difference(lhs, rhs);
380 case RT_BINARY_OP_AND:
381 return set_intersect(lhs, rhs);
382 case RT_BINARY_OP_SUBTRACT:
383 return set_diff(2, args);
384 case RT_BINARY_OP_INPLACE_OR:
385 return set_union(lhs, rhs);
386 case RT_BINARY_OP_INPLACE_XOR:
387 return set_symmetric_difference(lhs, rhs);
388 case RT_BINARY_OP_INPLACE_AND:
389 return set_intersect(lhs, rhs);
390 case RT_BINARY_OP_INPLACE_SUBTRACT:
391 return set_diff(2, args);
392 case RT_COMPARE_OP_LESS:
393 return set_issubset_proper(lhs, rhs);
394 case RT_COMPARE_OP_MORE:
395 return set_issuperset_proper(lhs, rhs);
396 case RT_COMPARE_OP_EQUAL:
397 return set_equal(lhs, rhs);
398 case RT_COMPARE_OP_LESS_EQUAL:
399 return set_issubset(lhs, rhs);
400 case RT_COMPARE_OP_MORE_EQUAL:
401 return set_issuperset(lhs, rhs);
402 case RT_COMPARE_OP_NOT_EQUAL:
403 return MP_BOOL(set_equal(lhs, rhs) == mp_const_false);
404 default:
405 // op not supported
406 return NULL;
407 }
408}
John R. Lenton0de386b2014-01-12 19:39:48 +0000409
John R. Lenton19b14d32014-01-12 15:29:11 +0000410/******************************************************************************/
411/* set constructors & public C API */
412
413
414static const mp_method_t set_type_methods[] = {
415 { "add", &set_add_obj },
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000416 { "clear", &set_clear_obj },
John R. Lenton3b0bd872014-01-12 15:56:25 +0000417 { "copy", &set_copy_obj },
John R. Lenton2a241722014-01-12 16:39:39 +0000418 { "discard", &set_discard_obj },
John R. Lenton032129f2014-01-12 17:07:17 +0000419 { "difference", &set_diff_obj },
420 { "difference_update", &set_diff_update_obj },
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000421 { "intersection", &set_intersect_obj },
422 { "intersection_update", &set_intersect_update_obj },
John R. Lenton4a080672014-01-12 18:03:21 +0000423 { "isdisjoint", &set_isdisjoint_obj },
John R. Lentonae00d332014-01-12 18:23:36 +0000424 { "issubset", &set_issubset_obj },
425 { "issuperset", &set_issuperset_obj },
426 { "pop", &set_pop_obj },
427 { "remove", &set_remove_obj },
John R. Lenton0de386b2014-01-12 19:39:48 +0000428 { "symmetric_difference", &set_symmetric_difference_obj },
429 { "symmetric_difference_update", &set_symmetric_difference_update_obj },
430 { "union", &set_union_obj },
431 { "update", &set_update_obj },
John R. Lenton19b14d32014-01-12 15:29:11 +0000432 { NULL, NULL }, // end-of-list sentinel
433};
434
Damien George71c51812014-01-04 20:21:15 +0000435const mp_obj_type_t set_type = {
Damiend99b0522013-12-21 18:17:45 +0000436 { &mp_const_type },
437 "set",
Damien George97209d32014-01-07 15:58:30 +0000438 .print = set_print,
439 .make_new = set_make_new,
John R. Lentonbe790f92014-01-12 23:09:10 +0000440 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000441 .getiter = set_getiter,
John R. Lenton19b14d32014-01-12 15:29:11 +0000442 .methods = set_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000443};
444
445mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
446 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
447 o->base.type = &set_type;
448 mp_set_init(&o->set, n_args);
449 for (int i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000450 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000451 }
452 return o;
453}
454
455void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
456 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
457 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000458 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000459}