blob: fc124fcd8c9b9daf120916500b195b0e617d8ce3 [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 Georgedd4135a2016-09-28 10:55:23 +100060// This macro is shorthand for mp_check_self to verify the argument is a
61// set or frozenset for methods that operate on both of these types.
62#define check_set_or_frozenset(o) mp_check_self(is_set_or_frozenset(o))
Paul Sokolovskyb181b582014-05-10 16:02:17 +030063
Damien Georgedd4135a2016-09-28 10:55:23 +100064// This function is used to verify the argument for methods that modify
65// the set object, and raises an exception if the arg is a frozenset.
Paul Sokolovskyb181b582014-05-10 16:02:17 +030066STATIC void check_set(mp_obj_t o) {
Damien Georgedd4135a2016-09-28 10:55:23 +100067 #if MICROPY_PY_BUILTINS_FROZENSET
68 if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) {
69 // Mutable method called on frozenset; emulate CPython behavior, eg:
Paul Sokolovskyb181b582014-05-10 16:02:17 +030070 // AttributeError: 'frozenset' object has no attribute 'add'
Damien George7d0d7212016-10-17 12:17:37 +110071 mp_raise_msg(&mp_type_AttributeError, "'frozenset' has no such attribute");
Paul Sokolovskyb181b582014-05-10 16:02:17 +030072 }
Damien Georgedd4135a2016-09-28 10:55:23 +100073 #endif
74 mp_check_self(MP_OBJ_IS_TYPE(o, &mp_type_set));
Paul Sokolovskyb181b582014-05-10 16:02:17 +030075}
76
Damien George7f9d1d62015-04-09 23:56:15 +010077STATIC 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 +000078 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000079 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgefb510b32014-06-01 13:32:54 +010080 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030081 bool is_frozen = MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset);
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030082 #endif
John R. Lenton7244a142014-01-12 23:37:45 +000083 if (self->set.used == 0) {
Damien Georgefb510b32014-06-01 13:32:54 +010084 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030085 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +010086 mp_print_str(print, "frozen");
Paul Sokolovskyb181b582014-05-10 16:02:17 +030087 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030088 #endif
Damien George7f9d1d62015-04-09 23:56:15 +010089 mp_print_str(print, "set()");
John R. Lenton7244a142014-01-12 23:37:45 +000090 return;
91 }
Damiend99b0522013-12-21 18:17:45 +000092 bool first = true;
Damien Georgefb510b32014-06-01 13:32:54 +010093 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030094 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +010095 mp_print_str(print, "frozenset(");
Paul Sokolovskyb181b582014-05-10 16:02:17 +030096 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030097 #endif
Damien George7f9d1d62015-04-09 23:56:15 +010098 mp_print_str(print, "{");
Damien George93965e72014-08-30 13:23:35 +010099 for (mp_uint_t i = 0; i < self->set.alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100100 if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
Damiend99b0522013-12-21 18:17:45 +0000101 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +0100102 mp_print_str(print, ", ");
Damiend99b0522013-12-21 18:17:45 +0000103 }
104 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +0100105 mp_obj_print_helper(print, self->set.table[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +0000106 }
107 }
Damien George7f9d1d62015-04-09 23:56:15 +0100108 mp_print_str(print, "}");
Damien Georgefb510b32014-06-01 13:32:54 +0100109 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300110 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +0100111 mp_print_str(print, ")");
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300112 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +0300113 #endif
Damiend99b0522013-12-21 18:17:45 +0000114}
115
Damien George5b3f0b72016-01-03 15:55:55 +0000116STATIC 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 +0100117 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Damien George20006db2014-01-18 14:10:48 +0000118
Damien George71c51812014-01-04 20:21:15 +0000119 switch (n_args) {
Damien George1d34e322014-05-11 18:28:48 +0100120 case 0: {
121 // create a new, empty set
Damien George999cedb2015-11-27 17:01:44 +0000122 mp_obj_set_t *set = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
Damien George1d34e322014-05-11 18:28:48 +0100123 // set actual set/frozenset type
Damien George5b3f0b72016-01-03 15:55:55 +0000124 set->base.type = type;
Damien George999cedb2015-11-27 17:01:44 +0000125 return MP_OBJ_FROM_PTR(set);
Damien George1d34e322014-05-11 18:28:48 +0100126 }
Damien George71c51812014-01-04 20:21:15 +0000127
128 case 1:
Damien George1d34e322014-05-11 18:28:48 +0100129 default: { // can only be 0 or 1 arg
Damien George71c51812014-01-04 20:21:15 +0000130 // 1 argument, an iterable from which we make a new set
Damien George999cedb2015-11-27 17:01:44 +0000131 mp_obj_t set = mp_obj_new_set(0, NULL);
Damien Georged17926d2014-03-30 13:35:08 +0100132 mp_obj_t iterable = mp_getiter(args[0]);
Damien George71c51812014-01-04 20:21:15 +0000133 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100134 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George71c51812014-01-04 20:21:15 +0000135 mp_obj_set_store(set, item);
136 }
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300137 // Set actual set/frozenset type
Damien George5b3f0b72016-01-03 15:55:55 +0000138 ((mp_obj_set_t*)MP_OBJ_TO_PTR(set))->base.type = type;
Damien George71c51812014-01-04 20:21:15 +0000139 return set;
140 }
Damien George71c51812014-01-04 20:21:15 +0000141 }
142}
143
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200144STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000145 mp_obj_set_it_t *self = MP_OBJ_TO_PTR(self_in);
Damien George40f3c022014-07-03 13:25:24 +0100146 mp_uint_t max = self->set->set.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100147 mp_set_t *set = &self->set->set;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000148
Damien George40f3c022014-07-03 13:25:24 +0100149 for (mp_uint_t i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100150 if (MP_SET_SLOT_IS_FILLED(set, i)) {
John R. Lenton0ce03b42014-01-12 15:17:42 +0000151 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100152 return set->table[i];
John R. Lenton0ce03b42014-01-12 15:17:42 +0000153 }
154 }
155
Damien Georgeea8d06c2014-04-17 23:19:36 +0100156 return MP_OBJ_STOP_ITERATION;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000157}
158
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200159STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
John R. Lenton0ce03b42014-01-12 15:17:42 +0000160 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
Damien George8212d972016-01-03 16:27:55 +0000161 o->base.type = &mp_type_polymorph_iter;
162 o->iternext = set_it_iternext;
Damien George999cedb2015-11-27 17:01:44 +0000163 o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000164 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000165 return MP_OBJ_FROM_PTR(o);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000166}
167
John R. Lenton19b14d32014-01-12 15:29:11 +0000168
169/******************************************************************************/
170/* set methods */
171
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200172STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300173 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000174 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000175 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000176 return mp_const_none;
177}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200178STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton19b14d32014-01-12 15:29:11 +0000179
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200180STATIC mp_obj_t set_clear(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300181 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000182 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000183
184 mp_set_clear(&self->set);
185
186 return mp_const_none;
187}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200188STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000189
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300190STATIC mp_obj_t set_copy_as_mutable(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000191 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000192
193 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000194 other->base.type = &mp_type_set;
Paul Sokolovsky46bd12d2014-04-07 03:07:21 +0300195 mp_set_init(&other->set, self->set.alloc);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000196 other->set.used = self->set.used;
197 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
198
Damien George999cedb2015-11-27 17:01:44 +0000199 return MP_OBJ_FROM_PTR(other);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000200}
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300201
202STATIC mp_obj_t set_copy(mp_obj_t self_in) {
203 check_set_or_frozenset(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300204
Damien George999cedb2015-11-27 17:01:44 +0000205 mp_obj_t other = set_copy_as_mutable(self_in);
206 ((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 +0300207
208 return other;
209}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200210STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000211
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200212STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300213 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000214 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000215 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
216 return mp_const_none;
217}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200218STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000219
Damien George4b72b3a2016-01-03 14:21:40 +0000220STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) {
Damien George999cedb2015-11-27 17:01:44 +0000221 mp_obj_t self;
John R. Lenton032129f2014-01-12 17:07:17 +0000222 if (update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300223 check_set(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000224 self = args[0];
225 } else {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300226 check_set_or_frozenset(args[0]);
227 self = set_copy_as_mutable(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000228 }
229
230
Damien George93965e72014-08-30 13:23:35 +0100231 for (mp_uint_t i = 1; i < n_args; i++) {
John R. Lenton032129f2014-01-12 17:07:17 +0000232 mp_obj_t other = args[i];
233 if (self == other) {
234 set_clear(self);
235 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100236 mp_obj_t iter = mp_getiter(other);
John R. Lenton032129f2014-01-12 17:07:17 +0000237 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100238 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton032129f2014-01-12 17:07:17 +0000239 set_discard(self, next);
240 }
241 }
242 }
243
Damien George999cedb2015-11-27 17:01:44 +0000244 ((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 +0000245 return self;
246}
247
Damien George4b72b3a2016-01-03 14:21:40 +0000248STATIC mp_obj_t set_diff(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000249 return set_diff_int(n_args, args, false);
250}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200251STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
John R. Lenton032129f2014-01-12 17:07:17 +0000252
Damien George4b72b3a2016-01-03 14:21:40 +0000253STATIC mp_obj_t set_diff_update(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000254 set_diff_int(n_args, args, true);
255 return mp_const_none;
256}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200257STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
John R. Lenton032129f2014-01-12 17:07:17 +0000258
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200259STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300260 if (update) {
261 check_set(self_in);
262 } else {
263 check_set_or_frozenset(self_in);
264 }
265
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000266 if (self_in == other) {
267 return update ? mp_const_none : set_copy(self_in);
268 }
269
Damien George999cedb2015-11-27 17:01:44 +0000270 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
271 mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000272
Damien Georged17926d2014-03-30 13:35:08 +0100273 mp_obj_t iter = mp_getiter(other);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000274 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100275 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000276 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
Damien George999cedb2015-11-27 17:01:44 +0000277 set_add(MP_OBJ_FROM_PTR(out), next);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000278 }
279 }
280
281 if (update) {
282 m_del(mp_obj_t, self->set.table, self->set.alloc);
283 self->set.alloc = out->set.alloc;
284 self->set.used = out->set.used;
285 self->set.table = out->set.table;
286 }
287
Damien George999cedb2015-11-27 17:01:44 +0000288 return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000289}
290
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200291STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000292 return set_intersect_int(self_in, other, false);
293}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200294STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000295
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200296STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000297 return set_intersect_int(self_in, other, true);
298}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200299STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000300
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200301STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300302 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000303 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton4a080672014-01-12 18:03:21 +0000304
Damien Georged17926d2014-03-30 13:35:08 +0100305 mp_obj_t iter = mp_getiter(other);
John R. Lenton4a080672014-01-12 18:03:21 +0000306 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100307 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton4a080672014-01-12 18:03:21 +0000308 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
309 return mp_const_false;
310 }
311 }
312 return mp_const_true;
313}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200314STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton4a080672014-01-12 18:03:21 +0000315
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200316STATIC 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 +0000317 mp_obj_set_t *self;
318 bool cleanup_self = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300319 if (is_set_or_frozenset(self_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000320 self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000321 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000322 self = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &self_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000323 cleanup_self = true;
324 }
325
326 mp_obj_set_t *other;
327 bool cleanup_other = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300328 if (is_set_or_frozenset(other_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000329 other = MP_OBJ_TO_PTR(other_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000330 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000331 other = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &other_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000332 cleanup_other = true;
333 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000334 bool out = true;
335 if (proper && self->set.used == other->set.used) {
336 out = false;
337 } else {
Damien George999cedb2015-11-27 17:01:44 +0000338 mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self));
John R. Lentonbe790f92014-01-12 23:09:10 +0000339 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100340 while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000341 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
342 out = false;
343 break;
344 }
John R. Lentonae00d332014-01-12 18:23:36 +0000345 }
346 }
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300347 // TODO: Should free objects altogether
John R. Lentonae00d332014-01-12 18:23:36 +0000348 if (cleanup_self) {
Damien George999cedb2015-11-27 17:01:44 +0000349 set_clear(MP_OBJ_FROM_PTR(self));
John R. Lentonae00d332014-01-12 18:23:36 +0000350 }
351 if (cleanup_other) {
Damien George999cedb2015-11-27 17:01:44 +0000352 set_clear(MP_OBJ_FROM_PTR(other));
John R. Lentonae00d332014-01-12 18:23:36 +0000353 }
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300354 return mp_obj_new_bool(out);
John R. Lentonbe790f92014-01-12 23:09:10 +0000355}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200356STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000357 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000358}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200359STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lentonae00d332014-01-12 18:23:36 +0000360
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200361STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000362 return set_issubset_internal(self_in, other_in, true);
363}
364
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200365STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000366 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000367}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200368STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lentonae00d332014-01-12 18:23:36 +0000369
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200370STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000371 return set_issubset_internal(other_in, self_in, true);
372}
373
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200374STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300375 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000376 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300377 if (!is_set_or_frozenset(other_in)) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000378 return mp_const_false;
379 }
Damien George999cedb2015-11-27 17:01:44 +0000380 mp_obj_set_t *other = MP_OBJ_TO_PTR(other_in);
John R. Lentonbe790f92014-01-12 23:09:10 +0000381 if (self->set.used != other->set.used) {
382 return mp_const_false;
383 }
384 return set_issubset(self_in, other_in);
385}
386
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200387STATIC mp_obj_t set_pop(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300388 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000389 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100390 mp_obj_t obj = mp_set_remove_first(&self->set);
391 if (obj == MP_OBJ_NULL) {
Damien George7d0d7212016-10-17 12:17:37 +1100392 mp_raise_msg(&mp_type_KeyError, "pop from an empty set");
John R. Lentonae00d332014-01-12 18:23:36 +0000393 }
John R. Lentonae00d332014-01-12 18:23:36 +0000394 return obj;
395}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200396STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lentonae00d332014-01-12 18:23:36 +0000397
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200398STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300399 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000400 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000401 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100402 nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lentonae00d332014-01-12 18:23:36 +0000403 }
404 return mp_const_none;
405}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200406STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000407
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200408STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300409 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000410 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georged17926d2014-03-30 13:35:08 +0100411 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000412 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100413 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georged1cee022015-03-20 17:41:37 +0000414 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 +0000415 }
416 return mp_const_none;
417}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200418STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000419
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200420STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300421 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000422 mp_obj_t self_out = set_copy_as_mutable(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300423 set_symmetric_difference_update(self_out, other_in);
Damien George999cedb2015-11-27 17:01:44 +0000424 ((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 +0300425 return self_out;
John R. Lenton0de386b2014-01-12 19:39:48 +0000426}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200427STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton0de386b2014-01-12 19:39:48 +0000428
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200429STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100430 mp_obj_t iter = mp_getiter(other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000431 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100432 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000433 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
434 }
435}
436
Damien George4b72b3a2016-01-03 14:21:40 +0000437STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
Damien George2c7716f2016-09-28 11:06:18 +1000438 check_set(args[0]);
Damien George93965e72014-08-30 13:23:35 +0100439 for (mp_uint_t i = 1; i < n_args; i++) {
Damien George999cedb2015-11-27 17:01:44 +0000440 set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
John R. Lenton0de386b2014-01-12 19:39:48 +0000441 }
442
443 return mp_const_none;
444}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200445STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000446
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200447STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300448 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000449 mp_obj_t self = set_copy(self_in);
450 set_update_int(MP_OBJ_TO_PTR(self), other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000451 return self;
452}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200453STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton0de386b2014-01-12 19:39:48 +0000454
Damien Georgeecc88e92014-08-30 00:35:11 +0100455STATIC mp_obj_t set_unary_op(mp_uint_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000456 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100457 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300458 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->set.used != 0);
Damien Georgebb4c6f32014-07-31 10:49:14 +0100459 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->set.used);
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300460#if MICROPY_PY_BUILTINS_FROZENSET
461 case MP_UNARY_OP_HASH:
462 if (MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset)) {
463 // start hash with unique value
Damien George999cedb2015-11-27 17:01:44 +0000464 mp_int_t hash = (mp_int_t)(uintptr_t)&mp_type_frozenset;
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300465 mp_uint_t max = self->set.alloc;
466 mp_set_t *set = &self->set;
467
468 for (mp_uint_t i = 0; i < max; i++) {
469 if (MP_SET_SLOT_IS_FILLED(set, i)) {
470 hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
471 }
472 }
473 return MP_OBJ_NEW_SMALL_INT(hash);
474 }
475#endif
Damien George6ac5dce2014-05-21 19:42:43 +0100476 default: return MP_OBJ_NULL; // op not supported
Damien George95004e52014-04-05 17:17:19 +0100477 }
478}
John R. Lenton0de386b2014-01-12 19:39:48 +0000479
Damien Georgeecc88e92014-08-30 00:35:11 +0100480STATIC 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 +0000481 mp_obj_t args[] = {lhs, rhs};
482 switch (op) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100483 case MP_BINARY_OP_OR:
484 return set_union(lhs, rhs);
485 case MP_BINARY_OP_XOR:
486 return set_symmetric_difference(lhs, rhs);
487 case MP_BINARY_OP_AND:
488 return set_intersect(lhs, rhs);
489 case MP_BINARY_OP_SUBTRACT:
490 return set_diff(2, args);
491 case MP_BINARY_OP_INPLACE_OR:
492 return set_union(lhs, rhs);
493 case MP_BINARY_OP_INPLACE_XOR:
494 return set_symmetric_difference(lhs, rhs);
495 case MP_BINARY_OP_INPLACE_AND:
496 return set_intersect(lhs, rhs);
497 case MP_BINARY_OP_INPLACE_SUBTRACT:
498 return set_diff(2, args);
499 case MP_BINARY_OP_LESS:
500 return set_issubset_proper(lhs, rhs);
501 case MP_BINARY_OP_MORE:
502 return set_issuperset_proper(lhs, rhs);
503 case MP_BINARY_OP_EQUAL:
504 return set_equal(lhs, rhs);
505 case MP_BINARY_OP_LESS_EQUAL:
506 return set_issubset(lhs, rhs);
507 case MP_BINARY_OP_MORE_EQUAL:
508 return set_issuperset(lhs, rhs);
509 case MP_BINARY_OP_IN: {
Damien George999cedb2015-11-27 17:01:44 +0000510 mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
Damien Georged0a5bf32014-05-10 13:55:11 +0100511 mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
Damien George999cedb2015-11-27 17:01:44 +0000512 return mp_obj_new_bool(elem != MP_OBJ_NULL);
Damien Georged0a5bf32014-05-10 13:55:11 +0100513 }
514 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100515 return MP_OBJ_NULL; // op not supported
John R. Lentonbe790f92014-01-12 23:09:10 +0000516 }
517}
John R. Lenton0de386b2014-01-12 19:39:48 +0000518
John R. Lenton19b14d32014-01-12 15:29:11 +0000519/******************************************************************************/
520/* set constructors & public C API */
521
522
Damien Georgecbf76742015-11-27 13:38:15 +0000523STATIC const mp_rom_map_elem_t set_locals_dict_table[] = {
524 { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&set_add_obj) },
525 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&set_clear_obj) },
526 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
527 { MP_ROM_QSTR(MP_QSTR_discard), MP_ROM_PTR(&set_discard_obj) },
528 { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
529 { MP_ROM_QSTR(MP_QSTR_difference_update), MP_ROM_PTR(&set_diff_update_obj) },
530 { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
531 { MP_ROM_QSTR(MP_QSTR_intersection_update), MP_ROM_PTR(&set_intersect_update_obj) },
532 { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
533 { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
534 { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
535 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&set_pop_obj) },
536 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&set_remove_obj) },
537 { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
538 { MP_ROM_QSTR(MP_QSTR_symmetric_difference_update), MP_ROM_PTR(&set_symmetric_difference_update_obj) },
539 { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
540 { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&set_update_obj) },
541 { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
John R. Lenton19b14d32014-01-12 15:29:11 +0000542};
543
Damien George9b196cd2014-03-26 21:47:19 +0000544STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
545
Damien George3e1a5c12014-03-29 13:43:38 +0000546const mp_obj_type_t mp_type_set = {
Damien Georgec5966122014-02-15 16:10:44 +0000547 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000548 .name = MP_QSTR_set,
Damien George97209d32014-01-07 15:58:30 +0000549 .print = set_print,
550 .make_new = set_make_new,
Damien George95004e52014-04-05 17:17:19 +0100551 .unary_op = set_unary_op,
John R. Lentonbe790f92014-01-12 23:09:10 +0000552 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000553 .getiter = set_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000554 .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000555};
556
Damien Georgefb510b32014-06-01 13:32:54 +0100557#if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300558const mp_obj_type_t mp_type_frozenset = {
559 { &mp_type_type },
560 .name = MP_QSTR_frozenset,
561 .print = set_print,
562 .make_new = set_make_new,
563 .unary_op = set_unary_op,
564 .binary_op = set_binary_op,
565 .getiter = set_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000566 .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300567};
568#endif
569
Damien George93965e72014-08-30 13:23:35 +0100570mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000571 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000572 o->base.type = &mp_type_set;
Damiend99b0522013-12-21 18:17:45 +0000573 mp_set_init(&o->set, n_args);
Damien George93965e72014-08-30 13:23:35 +0100574 for (mp_uint_t i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000575 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000576 }
Damien George999cedb2015-11-27 17:01:44 +0000577 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000578}
579
580void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300581 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
Damien George999cedb2015-11-27 17:01:44 +0000582 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000583 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000584}
Damien George3ebd4d02014-06-01 13:46:47 +0100585
586#endif // MICROPY_PY_BUILTINS_SET