blob: 3e56fd79efff23d4c153049ef2939aac89aeec97 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
John R. Lenton3b0bd872014-01-12 15:56:25 +000028#include <string.h>
Damiend99b0522013-12-21 18:17:45 +000029#include <assert.h>
30
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "obj.h"
Damien George71c51812014-01-04 20:21:15 +000036#include "runtime.h"
John R. Lentonc1bef212014-01-11 12:39:33 +000037#include "runtime0.h"
Paul Sokolovsky68e7c512014-04-13 11:53:15 +030038#include "builtin.h"
Damiend99b0522013-12-21 18:17:45 +000039
40typedef struct _mp_obj_set_t {
41 mp_obj_base_t base;
42 mp_set_t set;
43} mp_obj_set_t;
44
John R. Lenton0ce03b42014-01-12 15:17:42 +000045typedef struct _mp_obj_set_it_t {
46 mp_obj_base_t base;
47 mp_obj_set_t *set;
48 machine_uint_t cur;
49} mp_obj_set_it_t;
50
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020051STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
John R. Lenton0ce03b42014-01-12 15:17:42 +000052
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020053STATIC 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 +000054 mp_obj_set_t *self = self_in;
John R. Lenton7244a142014-01-12 23:37:45 +000055 if (self->set.used == 0) {
56 print(env, "set()");
57 return;
58 }
Damiend99b0522013-12-21 18:17:45 +000059 bool first = true;
60 print(env, "{");
61 for (int i = 0; i < self->set.alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +010062 if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
Damiend99b0522013-12-21 18:17:45 +000063 if (!first) {
64 print(env, ", ");
65 }
66 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020067 mp_obj_print_helper(print, env, self->set.table[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000068 }
69 }
70 print(env, "}");
71}
72
John R. Lentonc1bef212014-01-11 12:39:33 +000073
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020074STATIC 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 +000075 // TODO check n_kw == 0
76
Damien George71c51812014-01-04 20:21:15 +000077 switch (n_args) {
78 case 0:
79 // return a new, empty set
80 return mp_obj_new_set(0, NULL);
81
82 case 1:
83 {
84 // 1 argument, an iterable from which we make a new set
85 mp_obj_t set = mp_obj_new_set(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +010086 mp_obj_t iterable = mp_getiter(args[0]);
Damien George71c51812014-01-04 20:21:15 +000087 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +010088 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George71c51812014-01-04 20:21:15 +000089 mp_obj_set_store(set, item);
90 }
91 return set;
92 }
93
94 default:
Damien Georgeea13f402014-04-05 18:32:08 +010095 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 +000096 }
97}
98
Damien George3e1a5c12014-03-29 13:43:38 +000099const mp_obj_type_t mp_type_set_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000100 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000101 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300102 .getiter = mp_identity,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000103 .iternext = set_it_iternext,
104};
105
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200106STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000107 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set_it));
John R. Lenton0ce03b42014-01-12 15:17:42 +0000108 mp_obj_set_it_t *self = self_in;
109 machine_uint_t max = self->set->set.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100110 mp_set_t *set = &self->set->set;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000111
112 for (machine_uint_t i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100113 if (MP_SET_SLOT_IS_FILLED(set, i)) {
John R. Lenton0ce03b42014-01-12 15:17:42 +0000114 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100115 return set->table[i];
John R. Lenton0ce03b42014-01-12 15:17:42 +0000116 }
117 }
118
Damien Georgeea8d06c2014-04-17 23:19:36 +0100119 return MP_OBJ_STOP_ITERATION;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000120}
121
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200122STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
John R. Lenton0ce03b42014-01-12 15:17:42 +0000123 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000124 o->base.type = &mp_type_set_it;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000125 o->set = (mp_obj_set_t *)set_in;
126 o->cur = 0;
127 return o;
128}
129
John R. Lenton19b14d32014-01-12 15:29:11 +0000130
131/******************************************************************************/
132/* set methods */
133
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200134STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000135 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton19b14d32014-01-12 15:29:11 +0000136 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000137 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000138 return mp_const_none;
139}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200140STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton19b14d32014-01-12 15:29:11 +0000141
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200142STATIC mp_obj_t set_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000143 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000144 mp_obj_set_t *self = self_in;
145
146 mp_set_clear(&self->set);
147
148 return mp_const_none;
149}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200150STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000151
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200152STATIC mp_obj_t set_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000153 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton3b0bd872014-01-12 15:56:25 +0000154 mp_obj_set_t *self = self_in;
155
156 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000157 other->base.type = &mp_type_set;
Paul Sokolovsky46bd12d2014-04-07 03:07:21 +0300158 mp_set_init(&other->set, self->set.alloc);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000159 other->set.used = self->set.used;
160 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
161
162 return other;
163}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200164STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000165
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200166STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000167 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton2a241722014-01-12 16:39:39 +0000168 mp_obj_set_t *self = self_in;
169 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
170 return mp_const_none;
171}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200172STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000173
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200174STATIC mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
John R. Lenton032129f2014-01-12 17:07:17 +0000175 assert(n_args > 0);
Damien George3e1a5c12014-03-29 13:43:38 +0000176 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_set));
John R. Lenton032129f2014-01-12 17:07:17 +0000177 mp_obj_set_t *self;
178 if (update) {
179 self = args[0];
180 } else {
181 self = set_copy(args[0]);
182 }
183
184
185 for (int i = 1; i < n_args; i++) {
186 mp_obj_t other = args[i];
187 if (self == other) {
188 set_clear(self);
189 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100190 mp_obj_t iter = mp_getiter(other);
John R. Lenton032129f2014-01-12 17:07:17 +0000191 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100192 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton032129f2014-01-12 17:07:17 +0000193 set_discard(self, next);
194 }
195 }
196 }
197
198 return self;
199}
200
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200201STATIC mp_obj_t set_diff(uint n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000202 return set_diff_int(n_args, args, false);
203}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200204STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
John R. Lenton032129f2014-01-12 17:07:17 +0000205
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200206STATIC mp_obj_t set_diff_update(uint n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000207 set_diff_int(n_args, args, true);
208 return mp_const_none;
209}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200210STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
John R. Lenton032129f2014-01-12 17:07:17 +0000211
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200212STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
Damien George3e1a5c12014-03-29 13:43:38 +0000213 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000214 if (self_in == other) {
215 return update ? mp_const_none : set_copy(self_in);
216 }
217
218 mp_obj_set_t *self = self_in;
219 mp_obj_set_t *out = mp_obj_new_set(0, NULL);
220
Damien Georged17926d2014-03-30 13:35:08 +0100221 mp_obj_t iter = mp_getiter(other);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000222 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100223 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000224 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
225 set_add(out, next);
226 }
227 }
228
229 if (update) {
230 m_del(mp_obj_t, self->set.table, self->set.alloc);
231 self->set.alloc = out->set.alloc;
232 self->set.used = out->set.used;
233 self->set.table = out->set.table;
234 }
235
236 return update ? mp_const_none : out;
237}
238
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200239STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000240 return set_intersect_int(self_in, other, false);
241}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200242STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000243
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200244STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000245 return set_intersect_int(self_in, other, true);
246}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200247STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000248
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200249STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
Damien George3e1a5c12014-03-29 13:43:38 +0000250 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton4a080672014-01-12 18:03:21 +0000251 mp_obj_set_t *self = self_in;
252
Damien Georged17926d2014-03-30 13:35:08 +0100253 mp_obj_t iter = mp_getiter(other);
John R. Lenton4a080672014-01-12 18:03:21 +0000254 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100255 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton4a080672014-01-12 18:03:21 +0000256 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
257 return mp_const_false;
258 }
259 }
260 return mp_const_true;
261}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200262STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton4a080672014-01-12 18:03:21 +0000263
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200264STATIC 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 +0000265 mp_obj_set_t *self;
266 bool cleanup_self = false;
Damien George3e1a5c12014-03-29 13:43:38 +0000267 if (MP_OBJ_IS_TYPE(self_in, &mp_type_set)) {
John R. Lentonae00d332014-01-12 18:23:36 +0000268 self = self_in;
269 } else {
Damien George3e1a5c12014-03-29 13:43:38 +0000270 self = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000271 cleanup_self = true;
272 }
273
274 mp_obj_set_t *other;
275 bool cleanup_other = false;
Damien George3e1a5c12014-03-29 13:43:38 +0000276 if (MP_OBJ_IS_TYPE(other_in, &mp_type_set)) {
John R. Lentonae00d332014-01-12 18:23:36 +0000277 other = other_in;
278 } else {
Damien George3e1a5c12014-03-29 13:43:38 +0000279 other = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &other_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000280 cleanup_other = true;
281 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000282 bool out = true;
283 if (proper && self->set.used == other->set.used) {
284 out = false;
285 } else {
286 mp_obj_t iter = set_getiter(self);
287 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100288 while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000289 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
290 out = false;
291 break;
292 }
John R. Lentonae00d332014-01-12 18:23:36 +0000293 }
294 }
295 if (cleanup_self) {
296 set_clear(self);
297 }
298 if (cleanup_other) {
299 set_clear(other);
300 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000301 return MP_BOOL(out);
302}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200303STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000304 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000305}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200306STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lentonae00d332014-01-12 18:23:36 +0000307
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200308STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000309 return set_issubset_internal(self_in, other_in, true);
310}
311
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200312STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000313 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000314}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200315STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lentonae00d332014-01-12 18:23:36 +0000316
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200317STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000318 return set_issubset_internal(other_in, self_in, true);
319}
320
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200321STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000322 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonbe790f92014-01-12 23:09:10 +0000323 mp_obj_set_t *self = self_in;
Damien George3e1a5c12014-03-29 13:43:38 +0000324 if (!MP_OBJ_IS_TYPE(other_in, &mp_type_set)) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000325 return mp_const_false;
326 }
327 mp_obj_set_t *other = other_in;
328 if (self->set.used != other->set.used) {
329 return mp_const_false;
330 }
331 return set_issubset(self_in, other_in);
332}
333
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200334STATIC mp_obj_t set_pop(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000335 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonae00d332014-01-12 18:23:36 +0000336 mp_obj_set_t *self = self_in;
Damien George95004e52014-04-05 17:17:19 +0100337 mp_obj_t obj = mp_set_remove_first(&self->set);
338 if (obj == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100339 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "pop from an empty set"));
John R. Lentonae00d332014-01-12 18:23:36 +0000340 }
John R. Lentonae00d332014-01-12 18:23:36 +0000341 return obj;
342}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200343STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lentonae00d332014-01-12 18:23:36 +0000344
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200345STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000346 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lentonae00d332014-01-12 18:23:36 +0000347 mp_obj_set_t *self = self_in;
348 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100349 nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lentonae00d332014-01-12 18:23:36 +0000350 }
351 return mp_const_none;
352}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200353STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000354
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200355STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000356 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000357 mp_obj_set_t *self = self_in;
Damien Georged17926d2014-03-30 13:35:08 +0100358 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000359 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100360 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000361 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
362 }
363 return mp_const_none;
364}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200365STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000366
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200367STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000368 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000369 self_in = set_copy(self_in);
370 set_symmetric_difference_update(self_in, other_in);
371 return self_in;
372}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200373STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton0de386b2014-01-12 19:39:48 +0000374
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200375STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100376 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000377 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100378 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000379 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
380 }
381}
382
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200383STATIC mp_obj_t set_update(uint n_args, const mp_obj_t *args) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000384 assert(n_args > 0);
Damien George3e1a5c12014-03-29 13:43:38 +0000385 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000386
387 for (int i = 1; i < n_args; i++) {
388 set_update_int(args[0], args[i]);
389 }
390
391 return mp_const_none;
392}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200393STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000394
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200395STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000396 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton0de386b2014-01-12 19:39:48 +0000397 mp_obj_set_t *self = set_copy(self_in);
398 set_update_int(self, other_in);
399 return self;
400}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200401STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton0de386b2014-01-12 19:39:48 +0000402
Damien George95004e52014-04-05 17:17:19 +0100403STATIC mp_obj_t set_unary_op(int op, mp_obj_t self_in) {
404 mp_obj_set_t *self = self_in;
405 switch (op) {
406 case MP_UNARY_OP_BOOL: return MP_BOOL(self->set.used != 0);
407 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->set.used);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100408 default: return MP_OBJ_NOT_SUPPORTED;
Damien George95004e52014-04-05 17:17:19 +0100409 }
410}
John R. Lenton0de386b2014-01-12 19:39:48 +0000411
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200412STATIC mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000413 mp_obj_t args[] = {lhs, rhs};
414 switch (op) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100415 case MP_BINARY_OP_OR:
416 return set_union(lhs, rhs);
417 case MP_BINARY_OP_XOR:
418 return set_symmetric_difference(lhs, rhs);
419 case MP_BINARY_OP_AND:
420 return set_intersect(lhs, rhs);
421 case MP_BINARY_OP_SUBTRACT:
422 return set_diff(2, args);
423 case MP_BINARY_OP_INPLACE_OR:
424 return set_union(lhs, rhs);
425 case MP_BINARY_OP_INPLACE_XOR:
426 return set_symmetric_difference(lhs, rhs);
427 case MP_BINARY_OP_INPLACE_AND:
428 return set_intersect(lhs, rhs);
429 case MP_BINARY_OP_INPLACE_SUBTRACT:
430 return set_diff(2, args);
431 case MP_BINARY_OP_LESS:
432 return set_issubset_proper(lhs, rhs);
433 case MP_BINARY_OP_MORE:
434 return set_issuperset_proper(lhs, rhs);
435 case MP_BINARY_OP_EQUAL:
436 return set_equal(lhs, rhs);
437 case MP_BINARY_OP_LESS_EQUAL:
438 return set_issubset(lhs, rhs);
439 case MP_BINARY_OP_MORE_EQUAL:
440 return set_issuperset(lhs, rhs);
441 case MP_BINARY_OP_IN: {
442 mp_obj_set_t *o = lhs;
443 mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
444 return MP_BOOL(elem != NULL);
445 }
446 default:
447 return MP_OBJ_NOT_SUPPORTED;
John R. Lentonbe790f92014-01-12 23:09:10 +0000448 }
449}
John R. Lenton0de386b2014-01-12 19:39:48 +0000450
John R. Lenton19b14d32014-01-12 15:29:11 +0000451/******************************************************************************/
452/* set constructors & public C API */
453
454
Damien George9b196cd2014-03-26 21:47:19 +0000455STATIC const mp_map_elem_t set_locals_dict_table[] = {
456 { MP_OBJ_NEW_QSTR(MP_QSTR_add), (mp_obj_t)&set_add_obj },
457 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&set_clear_obj },
458 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&set_copy_obj },
459 { MP_OBJ_NEW_QSTR(MP_QSTR_discard), (mp_obj_t)&set_discard_obj },
460 { MP_OBJ_NEW_QSTR(MP_QSTR_difference), (mp_obj_t)&set_diff_obj },
461 { MP_OBJ_NEW_QSTR(MP_QSTR_difference_update), (mp_obj_t)&set_diff_update_obj },
462 { MP_OBJ_NEW_QSTR(MP_QSTR_intersection), (mp_obj_t)&set_intersect_obj },
463 { MP_OBJ_NEW_QSTR(MP_QSTR_intersection_update), (mp_obj_t)&set_intersect_update_obj },
464 { MP_OBJ_NEW_QSTR(MP_QSTR_isdisjoint), (mp_obj_t)&set_isdisjoint_obj },
465 { MP_OBJ_NEW_QSTR(MP_QSTR_issubset), (mp_obj_t)&set_issubset_obj },
466 { MP_OBJ_NEW_QSTR(MP_QSTR_issuperset), (mp_obj_t)&set_issuperset_obj },
467 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&set_pop_obj },
468 { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&set_remove_obj },
469 { MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference), (mp_obj_t)&set_symmetric_difference_obj },
470 { MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference_update), (mp_obj_t)&set_symmetric_difference_update_obj },
471 { MP_OBJ_NEW_QSTR(MP_QSTR_union), (mp_obj_t)&set_union_obj },
472 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&set_update_obj },
Paul Sokolovsky68e7c512014-04-13 11:53:15 +0300473 { MP_OBJ_NEW_QSTR(MP_QSTR___contains__), (mp_obj_t)&mp_op_contains_obj },
John R. Lenton19b14d32014-01-12 15:29:11 +0000474};
475
Damien George9b196cd2014-03-26 21:47:19 +0000476STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
477
Damien George3e1a5c12014-03-29 13:43:38 +0000478const mp_obj_type_t mp_type_set = {
Damien Georgec5966122014-02-15 16:10:44 +0000479 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000480 .name = MP_QSTR_set,
Damien George97209d32014-01-07 15:58:30 +0000481 .print = set_print,
482 .make_new = set_make_new,
Damien George95004e52014-04-05 17:17:19 +0100483 .unary_op = set_unary_op,
John R. Lentonbe790f92014-01-12 23:09:10 +0000484 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000485 .getiter = set_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000486 .locals_dict = (mp_obj_t)&set_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000487};
488
489mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
490 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000491 o->base.type = &mp_type_set;
Damiend99b0522013-12-21 18:17:45 +0000492 mp_set_init(&o->set, n_args);
493 for (int i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000494 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000495 }
496 return o;
497}
498
499void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
Damien George3e1a5c12014-03-29 13:43:38 +0000500 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
Damiend99b0522013-12-21 18:17:45 +0000501 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000502 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000503}