blob: 75bc7efb99f83ee0561620c9892fc4077099906a [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"
Damiend99b0522013-12-21 18:17:45 +000012#include "map.h"
13
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
25static mp_obj_t set_it_iternext(mp_obj_t self_in);
26
Damiend99b0522013-12-21 18:17:45 +000027void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
28 mp_obj_set_t *self = self_in;
29 bool first = true;
30 print(env, "{");
31 for (int i = 0; i < self->set.alloc; i++) {
32 if (self->set.table[i] != MP_OBJ_NULL) {
33 if (!first) {
34 print(env, ", ");
35 }
36 first = false;
37 mp_obj_print_helper(print, env, self->set.table[i]);
38 }
39 }
40 print(env, "}");
41}
42
Damien George71c51812014-01-04 20:21:15 +000043static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
44 switch (n_args) {
45 case 0:
46 // return a new, empty set
47 return mp_obj_new_set(0, NULL);
48
49 case 1:
50 {
51 // 1 argument, an iterable from which we make a new set
52 mp_obj_t set = mp_obj_new_set(0, NULL);
53 mp_obj_t iterable = rt_getiter(args[0]);
54 mp_obj_t item;
55 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
56 mp_obj_set_store(set, item);
57 }
58 return set;
59 }
60
61 default:
62 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));
63 }
64}
65
John R. Lenton0ce03b42014-01-12 15:17:42 +000066const mp_obj_type_t set_it_type = {
67 { &mp_const_type },
68 "set_iterator",
69 .iternext = set_it_iternext,
70};
71
72static mp_obj_t set_it_iternext(mp_obj_t self_in) {
73 assert(MP_OBJ_IS_TYPE(self_in, &set_it_type));
74 mp_obj_set_it_t *self = self_in;
75 machine_uint_t max = self->set->set.alloc;
76 mp_obj_t *table = self->set->set.table;
77
78 for (machine_uint_t i = self->cur; i < max; i++) {
79 if (table[i] != NULL) {
80 self->cur = i + 1;
81 return table[i];
82 }
83 }
84
85 return mp_const_stop_iteration;
86}
87
88static mp_obj_t set_getiter(mp_obj_t set_in) {
89 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
90 o->base.type = &set_it_type;
91 o->set = (mp_obj_set_t *)set_in;
92 o->cur = 0;
93 return o;
94}
95
John R. Lenton19b14d32014-01-12 15:29:11 +000096
97/******************************************************************************/
98/* set methods */
99
100static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
101 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
102 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000103 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000104 return mp_const_none;
105}
106static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
107
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000108static mp_obj_t set_clear(mp_obj_t self_in) {
109 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
110 mp_obj_set_t *self = self_in;
111
112 mp_set_clear(&self->set);
113
114 return mp_const_none;
115}
116static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
117
John R. Lenton3b0bd872014-01-12 15:56:25 +0000118static mp_obj_t set_copy(mp_obj_t self_in) {
119 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
120 mp_obj_set_t *self = self_in;
121
122 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
123 other->base.type = &set_type;
124 mp_set_init(&other->set, self->set.alloc);
125 other->set.used = self->set.used;
126 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
127
128 return other;
129}
130static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
131
John R. Lenton2a241722014-01-12 16:39:39 +0000132static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
133 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
134 mp_obj_set_t *self = self_in;
135 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
136 return mp_const_none;
137}
138static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000139
John R. Lenton032129f2014-01-12 17:07:17 +0000140static mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
141 assert(n_args > 0);
142 assert(MP_OBJ_IS_TYPE(args[0], &set_type));
143 mp_obj_set_t *self;
144 if (update) {
145 self = args[0];
146 } else {
147 self = set_copy(args[0]);
148 }
149
150
151 for (int i = 1; i < n_args; i++) {
152 mp_obj_t other = args[i];
153 if (self == other) {
154 set_clear(self);
155 } else {
156 mp_obj_t iter = rt_getiter(other);
157 mp_obj_t next;
158 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
159 set_discard(self, next);
160 }
161 }
162 }
163
164 return self;
165}
166
167static mp_obj_t set_diff(int n_args, const mp_obj_t *args) {
168 return set_diff_int(n_args, args, false);
169}
170static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
171
172static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) {
173 set_diff_int(n_args, args, true);
174 return mp_const_none;
175}
176static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
177
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000178static mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
179 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
180 if (self_in == other) {
181 return update ? mp_const_none : set_copy(self_in);
182 }
183
184 mp_obj_set_t *self = self_in;
185 mp_obj_set_t *out = mp_obj_new_set(0, NULL);
186
187 mp_obj_t iter = rt_getiter(other);
188 mp_obj_t next;
189 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
190 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
191 set_add(out, next);
192 }
193 }
194
195 if (update) {
196 m_del(mp_obj_t, self->set.table, self->set.alloc);
197 self->set.alloc = out->set.alloc;
198 self->set.used = out->set.used;
199 self->set.table = out->set.table;
200 }
201
202 return update ? mp_const_none : out;
203}
204
205static mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
206 return set_intersect_int(self_in, other, false);
207}
208static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
209
210static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
211 return set_intersect_int(self_in, other, true);
212}
213static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
214
John R. Lenton4a080672014-01-12 18:03:21 +0000215static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
216 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
217 mp_obj_set_t *self = self_in;
218
219 mp_obj_t iter = rt_getiter(other);
220 mp_obj_t next;
221 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
222 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
223 return mp_const_false;
224 }
225 }
226 return mp_const_true;
227}
228static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
229
John R. Lentonae00d332014-01-12 18:23:36 +0000230static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
231 mp_obj_set_t *self;
232 bool cleanup_self = false;
233 if (MP_OBJ_IS_TYPE(self_in, &set_type)) {
234 self = self_in;
235 } else {
236 self = set_make_new(NULL, 1, &self_in);
237 cleanup_self = true;
238 }
239
240 mp_obj_set_t *other;
241 bool cleanup_other = false;
242 if (MP_OBJ_IS_TYPE(other_in, &set_type)) {
243 other = other_in;
244 } else {
245 other = set_make_new(NULL, 1, &other_in);
246 cleanup_other = true;
247 }
248 mp_obj_t iter = set_getiter(self);
249 mp_obj_t next;
250 mp_obj_t out = mp_const_true;
251 while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) {
252 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
253 out = mp_const_false;
254 break;
255 }
256 }
257 if (cleanup_self) {
258 set_clear(self);
259 }
260 if (cleanup_other) {
261 set_clear(other);
262 }
263 return out;
264}
265static MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
266
267static mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
268 return set_issubset(other_in, self_in);
269}
270static MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
271
272static mp_obj_t set_pop(mp_obj_t self_in) {
273 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
274 mp_obj_set_t *self = self_in;
275
276 if (self->set.used == 0) {
277 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "pop from an empty set"));
278 }
279 mp_obj_t obj = mp_set_lookup(&self->set, NULL,
280 MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST);
281 return obj;
282}
283static MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
284
285static mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
286 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
287 mp_obj_set_t *self = self_in;
288 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
289 nlr_jump(mp_obj_new_exception(MP_QSTR_KeyError));
290 }
291 return mp_const_none;
292}
293static MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000294
John R. Lenton19b14d32014-01-12 15:29:11 +0000295/******************************************************************************/
296/* set constructors & public C API */
297
298
299static const mp_method_t set_type_methods[] = {
300 { "add", &set_add_obj },
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000301 { "clear", &set_clear_obj },
John R. Lenton3b0bd872014-01-12 15:56:25 +0000302 { "copy", &set_copy_obj },
John R. Lenton2a241722014-01-12 16:39:39 +0000303 { "discard", &set_discard_obj },
John R. Lenton032129f2014-01-12 17:07:17 +0000304 { "difference", &set_diff_obj },
305 { "difference_update", &set_diff_update_obj },
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000306 { "intersection", &set_intersect_obj },
307 { "intersection_update", &set_intersect_update_obj },
John R. Lenton4a080672014-01-12 18:03:21 +0000308 { "isdisjoint", &set_isdisjoint_obj },
John R. Lentonae00d332014-01-12 18:23:36 +0000309 { "issubset", &set_issubset_obj },
310 { "issuperset", &set_issuperset_obj },
311 { "pop", &set_pop_obj },
312 { "remove", &set_remove_obj },
John R. Lenton19b14d32014-01-12 15:29:11 +0000313 { NULL, NULL }, // end-of-list sentinel
314};
315
Damien George71c51812014-01-04 20:21:15 +0000316const mp_obj_type_t set_type = {
Damiend99b0522013-12-21 18:17:45 +0000317 { &mp_const_type },
318 "set",
Damien George97209d32014-01-07 15:58:30 +0000319 .print = set_print,
320 .make_new = set_make_new,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000321 .getiter = set_getiter,
John R. Lenton19b14d32014-01-12 15:29:11 +0000322 .methods = set_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000323};
324
325mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
326 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
327 o->base.type = &set_type;
328 mp_set_init(&o->set, n_args);
329 for (int i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000330 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000331 }
332 return o;
333}
334
335void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
336 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
337 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000338 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000339}