blob: 80ed26334035f7f1588b5a05fdc68735c9ecb96e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
Damien George01978642017-10-03 17:26:45 +11006 * Copyright (c) 2013-2017 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
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 George68cd3a92017-02-16 16:16:33 +110047 size_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 George01978642017-10-03 17:26:45 +110060// This macro is shorthand for mp_check_self to verify the argument is a set.
61#define check_set(o) mp_check_self(MP_OBJ_IS_TYPE(o, &mp_type_set))
62
Damien Georgedd4135a2016-09-28 10:55:23 +100063// This macro is shorthand for mp_check_self to verify the argument is a
64// set or frozenset for methods that operate on both of these types.
65#define check_set_or_frozenset(o) mp_check_self(is_set_or_frozenset(o))
Paul Sokolovskyb181b582014-05-10 16:02:17 +030066
Damien George7f9d1d62015-04-09 23:56:15 +010067STATIC 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 +000068 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000069 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgefb510b32014-06-01 13:32:54 +010070 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030071 bool is_frozen = MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset);
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030072 #endif
John R. Lenton7244a142014-01-12 23:37:45 +000073 if (self->set.used == 0) {
Damien Georgefb510b32014-06-01 13:32:54 +010074 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030075 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +010076 mp_print_str(print, "frozen");
Paul Sokolovskyb181b582014-05-10 16:02:17 +030077 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030078 #endif
Damien George7f9d1d62015-04-09 23:56:15 +010079 mp_print_str(print, "set()");
John R. Lenton7244a142014-01-12 23:37:45 +000080 return;
81 }
Damiend99b0522013-12-21 18:17:45 +000082 bool first = true;
Damien Georgefb510b32014-06-01 13:32:54 +010083 #if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +030084 if (is_frozen) {
Damien George7f9d1d62015-04-09 23:56:15 +010085 mp_print_str(print, "frozenset(");
Paul Sokolovskyb181b582014-05-10 16:02:17 +030086 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +030087 #endif
Damien George7f9d1d62015-04-09 23:56:15 +010088 mp_print_str(print, "{");
Damien George68cd3a92017-02-16 16:16:33 +110089 for (size_t i = 0; i < self->set.alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +010090 if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
Damiend99b0522013-12-21 18:17:45 +000091 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +010092 mp_print_str(print, ", ");
Damiend99b0522013-12-21 18:17:45 +000093 }
94 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +010095 mp_obj_print_helper(print, self->set.table[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000096 }
97 }
Damien George7f9d1d62015-04-09 23:56:15 +010098 mp_print_str(print, "}");
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, ")");
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300102 }
Paul Sokolovskyd80e2472014-05-10 16:11:04 +0300103 #endif
Damiend99b0522013-12-21 18:17:45 +0000104}
105
Damien George5b3f0b72016-01-03 15:55:55 +0000106STATIC 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 +0100107 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Damien George20006db2014-01-18 14:10:48 +0000108
Damien George71c51812014-01-04 20:21:15 +0000109 switch (n_args) {
Damien George1d34e322014-05-11 18:28:48 +0100110 case 0: {
111 // create a new, empty set
Damien George999cedb2015-11-27 17:01:44 +0000112 mp_obj_set_t *set = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
Damien George1d34e322014-05-11 18:28:48 +0100113 // set actual set/frozenset type
Damien George5b3f0b72016-01-03 15:55:55 +0000114 set->base.type = type;
Damien George999cedb2015-11-27 17:01:44 +0000115 return MP_OBJ_FROM_PTR(set);
Damien George1d34e322014-05-11 18:28:48 +0100116 }
Damien George71c51812014-01-04 20:21:15 +0000117
118 case 1:
Damien George1d34e322014-05-11 18:28:48 +0100119 default: { // can only be 0 or 1 arg
Damien George71c51812014-01-04 20:21:15 +0000120 // 1 argument, an iterable from which we make a new set
Damien George999cedb2015-11-27 17:01:44 +0000121 mp_obj_t set = mp_obj_new_set(0, NULL);
Damien Georgee6003f42017-02-13 15:44:31 +1100122 mp_obj_t iterable = mp_getiter(args[0], NULL);
Damien George71c51812014-01-04 20:21:15 +0000123 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100124 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George71c51812014-01-04 20:21:15 +0000125 mp_obj_set_store(set, item);
126 }
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300127 // Set actual set/frozenset type
Damien George5b3f0b72016-01-03 15:55:55 +0000128 ((mp_obj_set_t*)MP_OBJ_TO_PTR(set))->base.type = type;
Damien George71c51812014-01-04 20:21:15 +0000129 return set;
130 }
Damien George71c51812014-01-04 20:21:15 +0000131 }
132}
133
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200134STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000135 mp_obj_set_it_t *self = MP_OBJ_TO_PTR(self_in);
Damien George68cd3a92017-02-16 16:16:33 +1100136 size_t max = self->set->set.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100137 mp_set_t *set = &self->set->set;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000138
Damien George68cd3a92017-02-16 16:16:33 +1100139 for (size_t i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100140 if (MP_SET_SLOT_IS_FILLED(set, i)) {
John R. Lenton0ce03b42014-01-12 15:17:42 +0000141 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100142 return set->table[i];
John R. Lenton0ce03b42014-01-12 15:17:42 +0000143 }
144 }
145
Damien Georgeea8d06c2014-04-17 23:19:36 +0100146 return MP_OBJ_STOP_ITERATION;
John R. Lenton0ce03b42014-01-12 15:17:42 +0000147}
148
Damien Georgeae8d8672016-01-09 23:14:54 +0000149STATIC mp_obj_t set_getiter(mp_obj_t set_in, mp_obj_iter_buf_t *iter_buf) {
150 assert(sizeof(mp_obj_set_it_t) <= sizeof(mp_obj_iter_buf_t));
151 mp_obj_set_it_t *o = (mp_obj_set_it_t*)iter_buf;
Damien George8212d972016-01-03 16:27:55 +0000152 o->base.type = &mp_type_polymorph_iter;
153 o->iternext = set_it_iternext;
Damien George999cedb2015-11-27 17:01:44 +0000154 o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000155 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000156 return MP_OBJ_FROM_PTR(o);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000157}
158
John R. Lenton19b14d32014-01-12 15:29:11 +0000159
160/******************************************************************************/
161/* set methods */
162
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200163STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300164 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000165 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000166 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000167 return mp_const_none;
168}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200169STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton19b14d32014-01-12 15:29:11 +0000170
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200171STATIC mp_obj_t set_clear(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300172 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000173 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000174
175 mp_set_clear(&self->set);
176
177 return mp_const_none;
178}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200179STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000180
Damien George01978642017-10-03 17:26:45 +1100181STATIC mp_obj_t set_copy(mp_obj_t self_in) {
182 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000183 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000184 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
Damien George01978642017-10-03 17:26:45 +1100185 other->base.type = self->base.type;
Paul Sokolovsky46bd12d2014-04-07 03:07:21 +0300186 mp_set_init(&other->set, self->set.alloc);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000187 other->set.used = self->set.used;
188 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
Damien George999cedb2015-11-27 17:01:44 +0000189 return MP_OBJ_FROM_PTR(other);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000190}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200191STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000192
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200193STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300194 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000195 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000196 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
197 return mp_const_none;
198}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200199STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000200
Damien George4b72b3a2016-01-03 14:21:40 +0000201STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) {
Damien George999cedb2015-11-27 17:01:44 +0000202 mp_obj_t self;
John R. Lenton032129f2014-01-12 17:07:17 +0000203 if (update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300204 check_set(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000205 self = args[0];
206 } else {
Damien George01978642017-10-03 17:26:45 +1100207 self = set_copy(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000208 }
209
Damien George68cd3a92017-02-16 16:16:33 +1100210 for (size_t i = 1; i < n_args; i++) {
John R. Lenton032129f2014-01-12 17:07:17 +0000211 mp_obj_t other = args[i];
212 if (self == other) {
213 set_clear(self);
214 } else {
Damien George01978642017-10-03 17:26:45 +1100215 mp_set_t *self_set = &((mp_obj_set_t*)MP_OBJ_TO_PTR(self))->set;
Damien Georgee6003f42017-02-13 15:44:31 +1100216 mp_obj_t iter = mp_getiter(other, NULL);
John R. Lenton032129f2014-01-12 17:07:17 +0000217 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100218 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien George01978642017-10-03 17:26:45 +1100219 mp_set_lookup(self_set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton032129f2014-01-12 17:07:17 +0000220 }
221 }
222 }
223
224 return self;
225}
226
Damien George4b72b3a2016-01-03 14:21:40 +0000227STATIC mp_obj_t set_diff(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000228 return set_diff_int(n_args, args, false);
229}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200230STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
John R. Lenton032129f2014-01-12 17:07:17 +0000231
Damien George4b72b3a2016-01-03 14:21:40 +0000232STATIC mp_obj_t set_diff_update(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000233 set_diff_int(n_args, args, true);
234 return mp_const_none;
235}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200236STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
John R. Lenton032129f2014-01-12 17:07:17 +0000237
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200238STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300239 if (update) {
240 check_set(self_in);
241 } else {
242 check_set_or_frozenset(self_in);
243 }
244
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000245 if (self_in == other) {
246 return update ? mp_const_none : set_copy(self_in);
247 }
248
Damien George999cedb2015-11-27 17:01:44 +0000249 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
250 mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000251
Damien Georgee6003f42017-02-13 15:44:31 +1100252 mp_obj_t iter = mp_getiter(other, NULL);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000253 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100254 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000255 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
Damien George999cedb2015-11-27 17:01:44 +0000256 set_add(MP_OBJ_FROM_PTR(out), next);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000257 }
258 }
259
260 if (update) {
261 m_del(mp_obj_t, self->set.table, self->set.alloc);
262 self->set.alloc = out->set.alloc;
263 self->set.used = out->set.used;
264 self->set.table = out->set.table;
265 }
266
Damien George999cedb2015-11-27 17:01:44 +0000267 return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000268}
269
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200270STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000271 return set_intersect_int(self_in, other, false);
272}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200273STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000274
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200275STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000276 return set_intersect_int(self_in, other, true);
277}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200278STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000279
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200280STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300281 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000282 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton4a080672014-01-12 18:03:21 +0000283
Damien Georgeae8d8672016-01-09 23:14:54 +0000284 mp_obj_iter_buf_t iter_buf;
285 mp_obj_t iter = mp_getiter(other, &iter_buf);
John R. Lenton4a080672014-01-12 18:03:21 +0000286 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100287 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton4a080672014-01-12 18:03:21 +0000288 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
289 return mp_const_false;
290 }
291 }
292 return mp_const_true;
293}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200294STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton4a080672014-01-12 18:03:21 +0000295
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200296STATIC 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 +0000297 mp_obj_set_t *self;
298 bool cleanup_self = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300299 if (is_set_or_frozenset(self_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000300 self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000301 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000302 self = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &self_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000303 cleanup_self = true;
304 }
305
306 mp_obj_set_t *other;
307 bool cleanup_other = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300308 if (is_set_or_frozenset(other_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000309 other = MP_OBJ_TO_PTR(other_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000310 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000311 other = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &other_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000312 cleanup_other = true;
313 }
Damien George01978642017-10-03 17:26:45 +1100314 mp_obj_t out = mp_const_true;
John R. Lentonbe790f92014-01-12 23:09:10 +0000315 if (proper && self->set.used == other->set.used) {
Damien George01978642017-10-03 17:26:45 +1100316 out = mp_const_false;
John R. Lentonbe790f92014-01-12 23:09:10 +0000317 } else {
Damien Georgeae8d8672016-01-09 23:14:54 +0000318 mp_obj_iter_buf_t iter_buf;
319 mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self), &iter_buf);
John R. Lentonbe790f92014-01-12 23:09:10 +0000320 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100321 while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000322 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
Damien George01978642017-10-03 17:26:45 +1100323 out = mp_const_false;
John R. Lentonbe790f92014-01-12 23:09:10 +0000324 break;
325 }
John R. Lentonae00d332014-01-12 18:23:36 +0000326 }
327 }
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300328 // TODO: Should free objects altogether
John R. Lentonae00d332014-01-12 18:23:36 +0000329 if (cleanup_self) {
Damien George999cedb2015-11-27 17:01:44 +0000330 set_clear(MP_OBJ_FROM_PTR(self));
John R. Lentonae00d332014-01-12 18:23:36 +0000331 }
332 if (cleanup_other) {
Damien George999cedb2015-11-27 17:01:44 +0000333 set_clear(MP_OBJ_FROM_PTR(other));
John R. Lentonae00d332014-01-12 18:23:36 +0000334 }
Damien George01978642017-10-03 17:26:45 +1100335 return out;
John R. Lentonbe790f92014-01-12 23:09:10 +0000336}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200337STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000338 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000339}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200340STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lentonae00d332014-01-12 18:23:36 +0000341
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200342STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000343 return set_issubset_internal(self_in, other_in, true);
344}
345
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200346STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000347 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000348}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200349STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lentonae00d332014-01-12 18:23:36 +0000350
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200351STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000352 return set_issubset_internal(other_in, self_in, true);
353}
354
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200355STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300356 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000357 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300358 if (!is_set_or_frozenset(other_in)) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000359 return mp_const_false;
360 }
Damien George999cedb2015-11-27 17:01:44 +0000361 mp_obj_set_t *other = MP_OBJ_TO_PTR(other_in);
John R. Lentonbe790f92014-01-12 23:09:10 +0000362 if (self->set.used != other->set.used) {
363 return mp_const_false;
364 }
365 return set_issubset(self_in, other_in);
366}
367
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200368STATIC mp_obj_t set_pop(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300369 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000370 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100371 mp_obj_t obj = mp_set_remove_first(&self->set);
372 if (obj == MP_OBJ_NULL) {
Damien George7d0d7212016-10-17 12:17:37 +1100373 mp_raise_msg(&mp_type_KeyError, "pop from an empty set");
John R. Lentonae00d332014-01-12 18:23:36 +0000374 }
John R. Lentonae00d332014-01-12 18:23:36 +0000375 return obj;
376}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200377STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lentonae00d332014-01-12 18:23:36 +0000378
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200379STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300380 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000381 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000382 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100383 nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lentonae00d332014-01-12 18:23:36 +0000384 }
385 return mp_const_none;
386}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200387STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000388
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200389STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
Damien George01978642017-10-03 17:26:45 +1100390 check_set_or_frozenset(self_in); // can be frozenset due to call from set_symmetric_difference
Damien George999cedb2015-11-27 17:01:44 +0000391 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgee6003f42017-02-13 15:44:31 +1100392 mp_obj_t iter = mp_getiter(other_in, NULL);
John R. Lenton0de386b2014-01-12 19:39:48 +0000393 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100394 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georged1cee022015-03-20 17:41:37 +0000395 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 +0000396 }
397 return mp_const_none;
398}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200399STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000400
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200401STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
Damien George01978642017-10-03 17:26:45 +1100402 mp_obj_t self_out = set_copy(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300403 set_symmetric_difference_update(self_out, other_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300404 return self_out;
John R. Lenton0de386b2014-01-12 19:39:48 +0000405}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200406STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton0de386b2014-01-12 19:39:48 +0000407
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200408STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien Georgee6003f42017-02-13 15:44:31 +1100409 mp_obj_t iter = mp_getiter(other_in, NULL);
John R. Lenton0de386b2014-01-12 19:39:48 +0000410 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100411 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000412 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
413 }
414}
415
Damien George4b72b3a2016-01-03 14:21:40 +0000416STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
Damien George2c7716f2016-09-28 11:06:18 +1000417 check_set(args[0]);
Damien George68cd3a92017-02-16 16:16:33 +1100418 for (size_t i = 1; i < n_args; i++) {
Damien George999cedb2015-11-27 17:01:44 +0000419 set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
John R. Lenton0de386b2014-01-12 19:39:48 +0000420 }
421
422 return mp_const_none;
423}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200424STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000425
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200426STATIC mp_obj_t set_union(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 = set_copy(self_in);
429 set_update_int(MP_OBJ_TO_PTR(self), other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000430 return self;
431}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200432STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton0de386b2014-01-12 19:39:48 +0000433
Damien George58321dd2017-08-29 13:04:01 +1000434STATIC mp_obj_t set_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000435 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100436 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300437 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->set.used != 0);
Damien Georgebb4c6f32014-07-31 10:49:14 +0100438 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->set.used);
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300439#if MICROPY_PY_BUILTINS_FROZENSET
440 case MP_UNARY_OP_HASH:
441 if (MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset)) {
442 // start hash with unique value
Damien George999cedb2015-11-27 17:01:44 +0000443 mp_int_t hash = (mp_int_t)(uintptr_t)&mp_type_frozenset;
Damien George68cd3a92017-02-16 16:16:33 +1100444 size_t max = self->set.alloc;
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300445 mp_set_t *set = &self->set;
446
Damien George68cd3a92017-02-16 16:16:33 +1100447 for (size_t i = 0; i < max; i++) {
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300448 if (MP_SET_SLOT_IS_FILLED(set, i)) {
449 hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
450 }
451 }
452 return MP_OBJ_NEW_SMALL_INT(hash);
453 }
454#endif
Damien George6ac5dce2014-05-21 19:42:43 +0100455 default: return MP_OBJ_NULL; // op not supported
Damien George95004e52014-04-05 17:17:19 +0100456 }
457}
John R. Lenton0de386b2014-01-12 19:39:48 +0000458
Damien George58321dd2017-08-29 13:04:01 +1000459STATIC mp_obj_t set_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000460 mp_obj_t args[] = {lhs, rhs};
Damien George2486c4f2017-02-03 00:27:56 +1100461 #if MICROPY_PY_BUILTINS_FROZENSET
462 bool update = MP_OBJ_IS_TYPE(lhs, &mp_type_set);
463 #else
464 bool update = true;
465 #endif
Damien George2ac13642017-10-03 17:56:27 +1100466 if (op != MP_BINARY_OP_IN && !is_set_or_frozenset(rhs)) {
467 // For all ops except containment the RHS must be a set/frozenset
468 return MP_OBJ_NULL;
469 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000470 switch (op) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100471 case MP_BINARY_OP_OR:
472 return set_union(lhs, rhs);
473 case MP_BINARY_OP_XOR:
474 return set_symmetric_difference(lhs, rhs);
475 case MP_BINARY_OP_AND:
476 return set_intersect(lhs, rhs);
477 case MP_BINARY_OP_SUBTRACT:
478 return set_diff(2, args);
479 case MP_BINARY_OP_INPLACE_OR:
Damien George2486c4f2017-02-03 00:27:56 +1100480 if (update) {
481 set_update(2, args);
482 return lhs;
483 } else {
484 return set_union(lhs, rhs);
485 }
Damien Georged0a5bf32014-05-10 13:55:11 +0100486 case MP_BINARY_OP_INPLACE_XOR:
Damien George2486c4f2017-02-03 00:27:56 +1100487 if (update) {
488 set_symmetric_difference_update(lhs, rhs);
489 return lhs;
490 } else {
491 return set_symmetric_difference(lhs, rhs);
492 }
Damien Georged0a5bf32014-05-10 13:55:11 +0100493 case MP_BINARY_OP_INPLACE_AND:
Damien George2486c4f2017-02-03 00:27:56 +1100494 rhs = set_intersect_int(lhs, rhs, update);
495 if (update) {
496 return lhs;
497 } else {
498 return rhs;
499 }
Damien Georged0a5bf32014-05-10 13:55:11 +0100500 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George2486c4f2017-02-03 00:27:56 +1100501 return set_diff_int(2, args, update);
Damien Georged0a5bf32014-05-10 13:55:11 +0100502 case MP_BINARY_OP_LESS:
503 return set_issubset_proper(lhs, rhs);
504 case MP_BINARY_OP_MORE:
505 return set_issuperset_proper(lhs, rhs);
506 case MP_BINARY_OP_EQUAL:
507 return set_equal(lhs, rhs);
508 case MP_BINARY_OP_LESS_EQUAL:
509 return set_issubset(lhs, rhs);
510 case MP_BINARY_OP_MORE_EQUAL:
511 return set_issuperset(lhs, rhs);
512 case MP_BINARY_OP_IN: {
Damien George999cedb2015-11-27 17:01:44 +0000513 mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
Damien Georged0a5bf32014-05-10 13:55:11 +0100514 mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
Damien George999cedb2015-11-27 17:01:44 +0000515 return mp_obj_new_bool(elem != MP_OBJ_NULL);
Damien Georged0a5bf32014-05-10 13:55:11 +0100516 }
517 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100518 return MP_OBJ_NULL; // op not supported
John R. Lentonbe790f92014-01-12 23:09:10 +0000519 }
520}
John R. Lenton0de386b2014-01-12 19:39:48 +0000521
John R. Lenton19b14d32014-01-12 15:29:11 +0000522/******************************************************************************/
523/* set constructors & public C API */
524
525
Damien Georgecbf76742015-11-27 13:38:15 +0000526STATIC const mp_rom_map_elem_t set_locals_dict_table[] = {
527 { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&set_add_obj) },
528 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&set_clear_obj) },
529 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
530 { MP_ROM_QSTR(MP_QSTR_discard), MP_ROM_PTR(&set_discard_obj) },
531 { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
532 { MP_ROM_QSTR(MP_QSTR_difference_update), MP_ROM_PTR(&set_diff_update_obj) },
533 { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
534 { MP_ROM_QSTR(MP_QSTR_intersection_update), MP_ROM_PTR(&set_intersect_update_obj) },
535 { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
536 { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
537 { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
538 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&set_pop_obj) },
539 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&set_remove_obj) },
540 { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
541 { MP_ROM_QSTR(MP_QSTR_symmetric_difference_update), MP_ROM_PTR(&set_symmetric_difference_update_obj) },
542 { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
543 { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&set_update_obj) },
544 { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
John R. Lenton19b14d32014-01-12 15:29:11 +0000545};
546
Damien George9b196cd2014-03-26 21:47:19 +0000547STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
548
Damien George3e1a5c12014-03-29 13:43:38 +0000549const mp_obj_type_t mp_type_set = {
Damien Georgec5966122014-02-15 16:10:44 +0000550 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000551 .name = MP_QSTR_set,
Damien George97209d32014-01-07 15:58:30 +0000552 .print = set_print,
553 .make_new = set_make_new,
Damien George95004e52014-04-05 17:17:19 +0100554 .unary_op = set_unary_op,
John R. Lentonbe790f92014-01-12 23:09:10 +0000555 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000556 .getiter = set_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000557 .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000558};
559
Damien Georgefb510b32014-06-01 13:32:54 +0100560#if MICROPY_PY_BUILTINS_FROZENSET
Damien George01978642017-10-03 17:26:45 +1100561STATIC const mp_rom_map_elem_t frozenset_locals_dict_table[] = {
562 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
563 { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
564 { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
565 { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
566 { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
567 { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
568 { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
569 { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
570 { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
571};
572STATIC MP_DEFINE_CONST_DICT(frozenset_locals_dict, frozenset_locals_dict_table);
573
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300574const mp_obj_type_t mp_type_frozenset = {
575 { &mp_type_type },
576 .name = MP_QSTR_frozenset,
577 .print = set_print,
578 .make_new = set_make_new,
579 .unary_op = set_unary_op,
580 .binary_op = set_binary_op,
581 .getiter = set_getiter,
Damien George01978642017-10-03 17:26:45 +1100582 .locals_dict = (mp_obj_dict_t*)&frozenset_locals_dict,
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300583};
584#endif
585
Damien George68cd3a92017-02-16 16:16:33 +1100586mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000587 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000588 o->base.type = &mp_type_set;
Damiend99b0522013-12-21 18:17:45 +0000589 mp_set_init(&o->set, n_args);
Damien George68cd3a92017-02-16 16:16:33 +1100590 for (size_t i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000591 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000592 }
Damien George999cedb2015-11-27 17:01:44 +0000593 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000594}
595
596void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300597 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
Damien George999cedb2015-11-27 17:01:44 +0000598 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000599 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000600}
Damien George3ebd4d02014-06-01 13:46:47 +0100601
602#endif // MICROPY_PY_BUILTINS_SET