blob: fb89c07f3d6dac3dea9b4cd9320ef3d2c4c9f913 [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
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/runtime.h"
33#include "py/runtime0.h"
34#include "py/builtin.h"
Damiend99b0522013-12-21 18:17:45 +000035
Damien George3ebd4d02014-06-01 13:46:47 +010036#if MICROPY_PY_BUILTINS_SET
37
Damiend99b0522013-12-21 18:17:45 +000038typedef struct _mp_obj_set_t {
39 mp_obj_base_t base;
40 mp_set_t set;
41} mp_obj_set_t;
42
John R. Lenton0ce03b42014-01-12 15:17:42 +000043typedef struct _mp_obj_set_it_t {
44 mp_obj_base_t base;
Damien George8212d972016-01-03 16:27:55 +000045 mp_fun_1_t iternext;
John R. Lenton0ce03b42014-01-12 15:17:42 +000046 mp_obj_set_t *set;
Damien George40f3c022014-07-03 13:25:24 +010047 mp_uint_t cur;
John R. Lenton0ce03b42014-01-12 15:17:42 +000048} mp_obj_set_it_t;
49
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020050STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
John R. Lenton0ce03b42014-01-12 15:17:42 +000051
Paul Sokolovskyb181b582014-05-10 16:02:17 +030052STATIC bool is_set_or_frozenset(mp_obj_t o) {
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030053 return MP_OBJ_IS_TYPE(o, &mp_type_set)
Damien Georgefb510b32014-06-01 13:32:54 +010054#if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030055 || MP_OBJ_IS_TYPE(o, &mp_type_frozenset)
56#endif
57 ;
Paul Sokolovskyb181b582014-05-10 16:02:17 +030058}
59
Damien Georgefb510b32014-06-01 13:32:54 +010060#if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030061STATIC void check_set_or_frozenset(mp_obj_t o) {
62 if (!is_set_or_frozenset(o)) {
63 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
64 }
65}
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030066#else
67#define check_set_or_frozenset(o) check_set(o)
68#endif
Paul Sokolovskyb181b582014-05-10 16:02:17 +030069
70STATIC void check_set(mp_obj_t o) {
71 if (!MP_OBJ_IS_TYPE(o, &mp_type_set)) {
72 // Emulate CPython behavior
73 // AttributeError: 'frozenset' object has no attribute 'add'
Damien Georgefb510b32014-06-01 13:32:54 +010074 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030075 if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) {
76 nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "'frozenset' has no such attribute"));
77 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030078 #endif
Paul Sokolovskyb181b582014-05-10 16:02:17 +030079 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
80 }
81}
82
Damien George7f9d1d62015-04-09 23:56:15 +010083STATIC void set_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000084 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000085 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgefb510b32014-06-01 13:32:54 +010086 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030087 bool is_frozen = MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset);
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030088 #endif
John R. Lenton7244a142014-01-12 23:37:45 +000089 if (self->set.used == 0) {
Damien Georgefb510b32014-06-01 13:32:54 +010090 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030091 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +010092 mp_print_str(print, "frozen");
Paul Sokolovskyb181b582014-05-10 16:02:17 +030093 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030094 #endif
Damien George7f9d1d62015-04-09 23:56:15 +010095 mp_print_str(print, "set()");
John R. Lenton7244a142014-01-12 23:37:45 +000096 return;
97 }
Damiend99b0522013-12-21 18:17:45 +000098 bool first = true;
Damien Georgefb510b32014-06-01 13:32:54 +010099 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300100 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +0100101 mp_print_str(print, "frozenset(");
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300102 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +0300103 #endif
Damien George7f9d1d62015-04-09 23:56:15 +0100104 mp_print_str(print, "{");
Damien George93965e72014-08-30 13:23:35 +0100105 for (mp_uint_t i = 0; i < self->set.alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100106 if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
Damiend99b0522013-12-21 18:17:45 +0000107 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +0100108 mp_print_str(print, ", ");
Damiend99b0522013-12-21 18:17:45 +0000109 }
110 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +0100111 mp_obj_print_helper(print, self->set.table[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +0000112 }
113 }
Damien George7f9d1d62015-04-09 23:56:15 +0100114 mp_print_str(print, "}");
Damien Georgefb510b32014-06-01 13:32:54 +0100115 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300116 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +0100117 mp_print_str(print, ")");
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300118 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +0300119 #endif
Damiend99b0522013-12-21 18:17:45 +0000120}
121
Damien George5b3f0b72016-01-03 15:55:55 +0000122STATIC mp_obj_t set_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien George1d34e322014-05-11 18:28:48 +0100123 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Damien George20006db2014-01-18 14:10:48 +0000124
Damien George71c51812014-01-04 20:21:15 +0000125 switch (n_args) {
Damien George1d34e322014-05-11 18:28:48 +0100126 case 0: {
127 // create a new, empty set
Damien George999cedb2015-11-27 17:01:44 +0000128 mp_obj_set_t *set = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
Damien George1d34e322014-05-11 18:28:48 +0100129 // set actual set/frozenset type
Damien George5b3f0b72016-01-03 15:55:55 +0000130 set->base.type = type;
Damien George999cedb2015-11-27 17:01:44 +0000131 return MP_OBJ_FROM_PTR(set);
Damien George1d34e322014-05-11 18:28:48 +0100132 }
Damien George71c51812014-01-04 20:21:15 +0000133
134 case 1:
Damien George1d34e322014-05-11 18:28:48 +0100135 default: { // can only be 0 or 1 arg
Damien George71c51812014-01-04 20:21:15 +0000136 // 1 argument, an iterable from which we make a new set
Damien George999cedb2015-11-27 17:01:44 +0000137 mp_obj_t set = mp_obj_new_set(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +0100138 mp_obj_t iterable = mp_getiter(args[0]);
Damien George71c51812014-01-04 20:21:15 +0000139 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100140 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George71c51812014-01-04 20:21:15 +0000141 mp_obj_set_store(set, item);
142 }
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300143 // Set actual set/frozenset type
Damien George5b3f0b72016-01-03 15:55:55 +0000144 ((mp_obj_set_t*)MP_OBJ_TO_PTR(set))->base.type = type;
Damien George71c51812014-01-04 20:21:15 +0000145 return set;
146 }
Damien George71c51812014-01-04 20:21:15 +0000147 }
148}
149
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200150STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000151 mp_obj_set_it_t *self = MP_OBJ_TO_PTR(self_in);
Damien George40f3c022014-07-03 13:25:24 +0100152 mp_uint_t max = self->set->set.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100153 mp_set_t *set = &self->set->set;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000154
Damien George40f3c022014-07-03 13:25:24 +0100155 for (mp_uint_t i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100156 if (MP_SET_SLOT_IS_FILLED(set, i)) {
John R. Lenton0ce03b42014-01-12 15:17:42 +0000157 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100158 return set->table[i];
John R. Lenton0ce03b42014-01-12 15:17:42 +0000159 }
160 }
161
Damien Georgeea8d06c2014-04-17 23:19:36 +0100162 return MP_OBJ_STOP_ITERATION;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000163}
164
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200165STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
John R. Lenton0ce03b42014-01-12 15:17:42 +0000166 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
Damien George8212d972016-01-03 16:27:55 +0000167 o->base.type = &mp_type_polymorph_iter;
168 o->iternext = set_it_iternext;
Damien George999cedb2015-11-27 17:01:44 +0000169 o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000170 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000171 return MP_OBJ_FROM_PTR(o);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000172}
173
John R. Lenton19b14d32014-01-12 15:29:11 +0000174
175/******************************************************************************/
176/* set methods */
177
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200178STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300179 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000180 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000181 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000182 return mp_const_none;
183}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200184STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton19b14d32014-01-12 15:29:11 +0000185
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200186STATIC mp_obj_t set_clear(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300187 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000188 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000189
190 mp_set_clear(&self->set);
191
192 return mp_const_none;
193}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200194STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000195
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300196STATIC mp_obj_t set_copy_as_mutable(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000197 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000198
199 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000200 other->base.type = &mp_type_set;
Paul Sokolovsky46bd12d2014-04-07 03:07:21 +0300201 mp_set_init(&other->set, self->set.alloc);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000202 other->set.used = self->set.used;
203 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
204
Damien George999cedb2015-11-27 17:01:44 +0000205 return MP_OBJ_FROM_PTR(other);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000206}
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300207
208STATIC mp_obj_t set_copy(mp_obj_t self_in) {
209 check_set_or_frozenset(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300210
Damien George999cedb2015-11-27 17:01:44 +0000211 mp_obj_t other = set_copy_as_mutable(self_in);
212 ((mp_obj_base_t*)MP_OBJ_TO_PTR(other))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_in))->type;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300213
214 return other;
215}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200216STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000217
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200218STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300219 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000220 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000221 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
222 return mp_const_none;
223}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200224STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000225
Damien George4b72b3a2016-01-03 14:21:40 +0000226STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) {
Damien George999cedb2015-11-27 17:01:44 +0000227 mp_obj_t self;
John R. Lenton032129f2014-01-12 17:07:17 +0000228 if (update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300229 check_set(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000230 self = args[0];
231 } else {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300232 check_set_or_frozenset(args[0]);
233 self = set_copy_as_mutable(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000234 }
235
236
Damien George93965e72014-08-30 13:23:35 +0100237 for (mp_uint_t i = 1; i < n_args; i++) {
John R. Lenton032129f2014-01-12 17:07:17 +0000238 mp_obj_t other = args[i];
239 if (self == other) {
240 set_clear(self);
241 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100242 mp_obj_t iter = mp_getiter(other);
John R. Lenton032129f2014-01-12 17:07:17 +0000243 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100244 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton032129f2014-01-12 17:07:17 +0000245 set_discard(self, next);
246 }
247 }
248 }
249
Damien George999cedb2015-11-27 17:01:44 +0000250 ((mp_obj_base_t*)MP_OBJ_TO_PTR(self))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]))->type;
John R. Lenton032129f2014-01-12 17:07:17 +0000251 return self;
252}
253
Damien George4b72b3a2016-01-03 14:21:40 +0000254STATIC mp_obj_t set_diff(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000255 return set_diff_int(n_args, args, false);
256}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200257STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
John R. Lenton032129f2014-01-12 17:07:17 +0000258
Damien George4b72b3a2016-01-03 14:21:40 +0000259STATIC mp_obj_t set_diff_update(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000260 set_diff_int(n_args, args, true);
261 return mp_const_none;
262}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200263STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
John R. Lenton032129f2014-01-12 17:07:17 +0000264
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200265STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300266 if (update) {
267 check_set(self_in);
268 } else {
269 check_set_or_frozenset(self_in);
270 }
271
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000272 if (self_in == other) {
273 return update ? mp_const_none : set_copy(self_in);
274 }
275
Damien George999cedb2015-11-27 17:01:44 +0000276 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
277 mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000278
Damien Georged17926d2014-03-30 13:35:08 +0100279 mp_obj_t iter = mp_getiter(other);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000280 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100281 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000282 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
Damien George999cedb2015-11-27 17:01:44 +0000283 set_add(MP_OBJ_FROM_PTR(out), next);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000284 }
285 }
286
287 if (update) {
288 m_del(mp_obj_t, self->set.table, self->set.alloc);
289 self->set.alloc = out->set.alloc;
290 self->set.used = out->set.used;
291 self->set.table = out->set.table;
292 }
293
Damien George999cedb2015-11-27 17:01:44 +0000294 return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000295}
296
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200297STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000298 return set_intersect_int(self_in, other, false);
299}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200300STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000301
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200302STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000303 return set_intersect_int(self_in, other, true);
304}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200305STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000306
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200307STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300308 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000309 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton4a080672014-01-12 18:03:21 +0000310
Damien Georged17926d2014-03-30 13:35:08 +0100311 mp_obj_t iter = mp_getiter(other);
John R. Lenton4a080672014-01-12 18:03:21 +0000312 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100313 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton4a080672014-01-12 18:03:21 +0000314 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
315 return mp_const_false;
316 }
317 }
318 return mp_const_true;
319}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200320STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton4a080672014-01-12 18:03:21 +0000321
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200322STATIC 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 +0000323 mp_obj_set_t *self;
324 bool cleanup_self = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300325 if (is_set_or_frozenset(self_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000326 self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000327 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000328 self = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &self_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000329 cleanup_self = true;
330 }
331
332 mp_obj_set_t *other;
333 bool cleanup_other = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300334 if (is_set_or_frozenset(other_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000335 other = MP_OBJ_TO_PTR(other_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000336 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000337 other = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &other_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000338 cleanup_other = true;
339 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000340 bool out = true;
341 if (proper && self->set.used == other->set.used) {
342 out = false;
343 } else {
Damien George999cedb2015-11-27 17:01:44 +0000344 mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self));
John R. Lentonbe790f92014-01-12 23:09:10 +0000345 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100346 while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000347 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
348 out = false;
349 break;
350 }
John R. Lentonae00d332014-01-12 18:23:36 +0000351 }
352 }
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300353 // TODO: Should free objects altogether
John R. Lentonae00d332014-01-12 18:23:36 +0000354 if (cleanup_self) {
Damien George999cedb2015-11-27 17:01:44 +0000355 set_clear(MP_OBJ_FROM_PTR(self));
John R. Lentonae00d332014-01-12 18:23:36 +0000356 }
357 if (cleanup_other) {
Damien George999cedb2015-11-27 17:01:44 +0000358 set_clear(MP_OBJ_FROM_PTR(other));
John R. Lentonae00d332014-01-12 18:23:36 +0000359 }
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300360 return mp_obj_new_bool(out);
John R. Lentonbe790f92014-01-12 23:09:10 +0000361}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200362STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000363 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000364}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200365STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lentonae00d332014-01-12 18:23:36 +0000366
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200367STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000368 return set_issubset_internal(self_in, other_in, true);
369}
370
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200371STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000372 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000373}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200374STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lentonae00d332014-01-12 18:23:36 +0000375
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200376STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000377 return set_issubset_internal(other_in, self_in, true);
378}
379
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200380STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300381 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000382 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300383 if (!is_set_or_frozenset(other_in)) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000384 return mp_const_false;
385 }
Damien George999cedb2015-11-27 17:01:44 +0000386 mp_obj_set_t *other = MP_OBJ_TO_PTR(other_in);
John R. Lentonbe790f92014-01-12 23:09:10 +0000387 if (self->set.used != other->set.used) {
388 return mp_const_false;
389 }
390 return set_issubset(self_in, other_in);
391}
392
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200393STATIC mp_obj_t set_pop(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300394 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000395 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100396 mp_obj_t obj = mp_set_remove_first(&self->set);
397 if (obj == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100398 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "pop from an empty set"));
John R. Lentonae00d332014-01-12 18:23:36 +0000399 }
John R. Lentonae00d332014-01-12 18:23:36 +0000400 return obj;
401}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200402STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lentonae00d332014-01-12 18:23:36 +0000403
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200404STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300405 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000406 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000407 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100408 nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lentonae00d332014-01-12 18:23:36 +0000409 }
410 return mp_const_none;
411}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200412STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000413
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200414STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300415 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000416 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georged17926d2014-03-30 13:35:08 +0100417 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000418 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100419 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georged1cee022015-03-20 17:41:37 +0000420 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND);
John R. Lenton0de386b2014-01-12 19:39:48 +0000421 }
422 return mp_const_none;
423}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200424STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000425
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200426STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300427 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000428 mp_obj_t self_out = set_copy_as_mutable(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300429 set_symmetric_difference_update(self_out, other_in);
Damien George999cedb2015-11-27 17:01:44 +0000430 ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_out))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_in))->type;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300431 return self_out;
John R. Lenton0de386b2014-01-12 19:39:48 +0000432}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200433STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton0de386b2014-01-12 19:39:48 +0000434
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200435STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100436 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000437 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100438 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000439 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
440 }
441}
442
Damien George4b72b3a2016-01-03 14:21:40 +0000443STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
Damien George93965e72014-08-30 13:23:35 +0100444 for (mp_uint_t i = 1; i < n_args; i++) {
Damien George999cedb2015-11-27 17:01:44 +0000445 set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
John R. Lenton0de386b2014-01-12 19:39:48 +0000446 }
447
448 return mp_const_none;
449}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200450STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000451
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200452STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300453 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000454 mp_obj_t self = set_copy(self_in);
455 set_update_int(MP_OBJ_TO_PTR(self), other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000456 return self;
457}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200458STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton0de386b2014-01-12 19:39:48 +0000459
Damien Georgeecc88e92014-08-30 00:35:11 +0100460STATIC mp_obj_t set_unary_op(mp_uint_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000461 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100462 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300463 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->set.used != 0);
Damien Georgebb4c6f32014-07-31 10:49:14 +0100464 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->set.used);
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300465#if MICROPY_PY_BUILTINS_FROZENSET
466 case MP_UNARY_OP_HASH:
467 if (MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset)) {
468 // start hash with unique value
Damien George999cedb2015-11-27 17:01:44 +0000469 mp_int_t hash = (mp_int_t)(uintptr_t)&mp_type_frozenset;
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300470 mp_uint_t max = self->set.alloc;
471 mp_set_t *set = &self->set;
472
473 for (mp_uint_t i = 0; i < max; i++) {
474 if (MP_SET_SLOT_IS_FILLED(set, i)) {
475 hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
476 }
477 }
478 return MP_OBJ_NEW_SMALL_INT(hash);
479 }
480#endif
Damien George6ac5dce2014-05-21 19:42:43 +0100481 default: return MP_OBJ_NULL; // op not supported
Damien George95004e52014-04-05 17:17:19 +0100482 }
483}
John R. Lenton0de386b2014-01-12 19:39:48 +0000484
Damien Georgeecc88e92014-08-30 00:35:11 +0100485STATIC mp_obj_t set_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000486 mp_obj_t args[] = {lhs, rhs};
487 switch (op) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100488 case MP_BINARY_OP_OR:
489 return set_union(lhs, rhs);
490 case MP_BINARY_OP_XOR:
491 return set_symmetric_difference(lhs, rhs);
492 case MP_BINARY_OP_AND:
493 return set_intersect(lhs, rhs);
494 case MP_BINARY_OP_SUBTRACT:
495 return set_diff(2, args);
496 case MP_BINARY_OP_INPLACE_OR:
497 return set_union(lhs, rhs);
498 case MP_BINARY_OP_INPLACE_XOR:
499 return set_symmetric_difference(lhs, rhs);
500 case MP_BINARY_OP_INPLACE_AND:
501 return set_intersect(lhs, rhs);
502 case MP_BINARY_OP_INPLACE_SUBTRACT:
503 return set_diff(2, args);
504 case MP_BINARY_OP_LESS:
505 return set_issubset_proper(lhs, rhs);
506 case MP_BINARY_OP_MORE:
507 return set_issuperset_proper(lhs, rhs);
508 case MP_BINARY_OP_EQUAL:
509 return set_equal(lhs, rhs);
510 case MP_BINARY_OP_LESS_EQUAL:
511 return set_issubset(lhs, rhs);
512 case MP_BINARY_OP_MORE_EQUAL:
513 return set_issuperset(lhs, rhs);
514 case MP_BINARY_OP_IN: {
Damien George999cedb2015-11-27 17:01:44 +0000515 mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
Damien Georged0a5bf32014-05-10 13:55:11 +0100516 mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
Damien George999cedb2015-11-27 17:01:44 +0000517 return mp_obj_new_bool(elem != MP_OBJ_NULL);
Damien Georged0a5bf32014-05-10 13:55:11 +0100518 }
519 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100520 return MP_OBJ_NULL; // op not supported
John R. Lentonbe790f92014-01-12 23:09:10 +0000521 }
522}
John R. Lenton0de386b2014-01-12 19:39:48 +0000523
John R. Lenton19b14d32014-01-12 15:29:11 +0000524/******************************************************************************/
525/* set constructors & public C API */
526
527
Damien Georgecbf76742015-11-27 13:38:15 +0000528STATIC const mp_rom_map_elem_t set_locals_dict_table[] = {
529 { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&set_add_obj) },
530 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&set_clear_obj) },
531 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
532 { MP_ROM_QSTR(MP_QSTR_discard), MP_ROM_PTR(&set_discard_obj) },
533 { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
534 { MP_ROM_QSTR(MP_QSTR_difference_update), MP_ROM_PTR(&set_diff_update_obj) },
535 { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
536 { MP_ROM_QSTR(MP_QSTR_intersection_update), MP_ROM_PTR(&set_intersect_update_obj) },
537 { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
538 { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
539 { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
540 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&set_pop_obj) },
541 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&set_remove_obj) },
542 { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
543 { MP_ROM_QSTR(MP_QSTR_symmetric_difference_update), MP_ROM_PTR(&set_symmetric_difference_update_obj) },
544 { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
545 { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&set_update_obj) },
546 { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
John R. Lenton19b14d32014-01-12 15:29:11 +0000547};
548
Damien George9b196cd2014-03-26 21:47:19 +0000549STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
550
Damien George3e1a5c12014-03-29 13:43:38 +0000551const mp_obj_type_t mp_type_set = {
Damien Georgec5966122014-02-15 16:10:44 +0000552 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000553 .name = MP_QSTR_set,
Damien George97209d32014-01-07 15:58:30 +0000554 .print = set_print,
555 .make_new = set_make_new,
Damien George95004e52014-04-05 17:17:19 +0100556 .unary_op = set_unary_op,
John R. Lentonbe790f92014-01-12 23:09:10 +0000557 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000558 .getiter = set_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000559 .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000560};
561
Damien Georgefb510b32014-06-01 13:32:54 +0100562#if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300563const mp_obj_type_t mp_type_frozenset = {
564 { &mp_type_type },
565 .name = MP_QSTR_frozenset,
566 .print = set_print,
567 .make_new = set_make_new,
568 .unary_op = set_unary_op,
569 .binary_op = set_binary_op,
570 .getiter = set_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000571 .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300572};
573#endif
574
Damien George93965e72014-08-30 13:23:35 +0100575mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000576 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000577 o->base.type = &mp_type_set;
Damiend99b0522013-12-21 18:17:45 +0000578 mp_set_init(&o->set, n_args);
Damien George93965e72014-08-30 13:23:35 +0100579 for (mp_uint_t i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000580 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000581 }
Damien George999cedb2015-11-27 17:01:44 +0000582 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000583}
584
585void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300586 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
Damien George999cedb2015-11-27 17:01:44 +0000587 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000588 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000589}
Damien George3ebd4d02014-06-01 13:46:47 +0100590
591#endif // MICROPY_PY_BUILTINS_SET