blob: 376439b73e0abfe5e00f132413cc69e1d01d973c [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 *
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 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 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 George68cd3a92017-02-16 16:16:33 +110099 for (size_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 Georgee6003f42017-02-13 15:44:31 +1100132 mp_obj_t iterable = mp_getiter(args[0], NULL);
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 George68cd3a92017-02-16 16:16:33 +1100146 size_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 George68cd3a92017-02-16 16:16:33 +1100149 for (size_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
Damien Georgeae8d8672016-01-09 23:14:54 +0000159STATIC mp_obj_t set_getiter(mp_obj_t set_in, mp_obj_iter_buf_t *iter_buf) {
160 assert(sizeof(mp_obj_set_it_t) <= sizeof(mp_obj_iter_buf_t));
161 mp_obj_set_it_t *o = (mp_obj_set_it_t*)iter_buf;
Damien George8212d972016-01-03 16:27:55 +0000162 o->base.type = &mp_type_polymorph_iter;
163 o->iternext = set_it_iternext;
Damien George999cedb2015-11-27 17:01:44 +0000164 o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000165 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000166 return MP_OBJ_FROM_PTR(o);
John R. Lenton0ce03b42014-01-12 15:17:42 +0000167}
168
John R. Lenton19b14d32014-01-12 15:29:11 +0000169
170/******************************************************************************/
171/* set methods */
172
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200173STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300174 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000175 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000176 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000177 return mp_const_none;
178}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200179STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton19b14d32014-01-12 15:29:11 +0000180
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200181STATIC mp_obj_t set_clear(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300182 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000183 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000184
185 mp_set_clear(&self->set);
186
187 return mp_const_none;
188}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200189STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000190
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300191STATIC mp_obj_t set_copy_as_mutable(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000192 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000193
194 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000195 other->base.type = &mp_type_set;
Paul Sokolovsky46bd12d2014-04-07 03:07:21 +0300196 mp_set_init(&other->set, self->set.alloc);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000197 other->set.used = self->set.used;
198 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
199
Damien George999cedb2015-11-27 17:01:44 +0000200 return MP_OBJ_FROM_PTR(other);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000201}
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300202
203STATIC mp_obj_t set_copy(mp_obj_t self_in) {
204 check_set_or_frozenset(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300205
Damien George999cedb2015-11-27 17:01:44 +0000206 mp_obj_t other = set_copy_as_mutable(self_in);
207 ((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 +0300208
209 return other;
210}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200211STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
John R. Lenton3b0bd872014-01-12 15:56:25 +0000212
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200213STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300214 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000215 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000216 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
217 return mp_const_none;
218}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200219STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000220
Damien George4b72b3a2016-01-03 14:21:40 +0000221STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) {
Damien George999cedb2015-11-27 17:01:44 +0000222 mp_obj_t self;
John R. Lenton032129f2014-01-12 17:07:17 +0000223 if (update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300224 check_set(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000225 self = args[0];
226 } else {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300227 check_set_or_frozenset(args[0]);
228 self = set_copy_as_mutable(args[0]);
John R. Lenton032129f2014-01-12 17:07:17 +0000229 }
230
231
Damien George68cd3a92017-02-16 16:16:33 +1100232 for (size_t i = 1; i < n_args; i++) {
John R. Lenton032129f2014-01-12 17:07:17 +0000233 mp_obj_t other = args[i];
234 if (self == other) {
235 set_clear(self);
236 } else {
Damien Georgee6003f42017-02-13 15:44:31 +1100237 mp_obj_t iter = mp_getiter(other, NULL);
John R. Lenton032129f2014-01-12 17:07:17 +0000238 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100239 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton032129f2014-01-12 17:07:17 +0000240 set_discard(self, next);
241 }
242 }
243 }
244
Damien George999cedb2015-11-27 17:01:44 +0000245 ((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 +0000246 return self;
247}
248
Damien George4b72b3a2016-01-03 14:21:40 +0000249STATIC mp_obj_t set_diff(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000250 return set_diff_int(n_args, args, false);
251}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200252STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
John R. Lenton032129f2014-01-12 17:07:17 +0000253
Damien George4b72b3a2016-01-03 14:21:40 +0000254STATIC mp_obj_t set_diff_update(size_t n_args, const mp_obj_t *args) {
John R. Lenton032129f2014-01-12 17:07:17 +0000255 set_diff_int(n_args, args, true);
256 return mp_const_none;
257}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200258STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
John R. Lenton032129f2014-01-12 17:07:17 +0000259
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200260STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300261 if (update) {
262 check_set(self_in);
263 } else {
264 check_set_or_frozenset(self_in);
265 }
266
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000267 if (self_in == other) {
268 return update ? mp_const_none : set_copy(self_in);
269 }
270
Damien George999cedb2015-11-27 17:01:44 +0000271 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
272 mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000273
Damien Georgee6003f42017-02-13 15:44:31 +1100274 mp_obj_t iter = mp_getiter(other, NULL);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000275 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100276 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000277 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
Damien George999cedb2015-11-27 17:01:44 +0000278 set_add(MP_OBJ_FROM_PTR(out), next);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000279 }
280 }
281
282 if (update) {
283 m_del(mp_obj_t, self->set.table, self->set.alloc);
284 self->set.alloc = out->set.alloc;
285 self->set.used = out->set.used;
286 self->set.table = out->set.table;
287 }
288
Damien George999cedb2015-11-27 17:01:44 +0000289 return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000290}
291
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200292STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000293 return set_intersect_int(self_in, other, false);
294}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200295STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000296
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200297STATIC mp_obj_t set_intersect_update(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, true);
299}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200300STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000301
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200302STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300303 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000304 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton4a080672014-01-12 18:03:21 +0000305
Damien Georgeae8d8672016-01-09 23:14:54 +0000306 mp_obj_iter_buf_t iter_buf;
307 mp_obj_t iter = mp_getiter(other, &iter_buf);
John R. Lenton4a080672014-01-12 18:03:21 +0000308 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100309 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton4a080672014-01-12 18:03:21 +0000310 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
311 return mp_const_false;
312 }
313 }
314 return mp_const_true;
315}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200316STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton4a080672014-01-12 18:03:21 +0000317
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200318STATIC 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 +0000319 mp_obj_set_t *self;
320 bool cleanup_self = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300321 if (is_set_or_frozenset(self_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000322 self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000323 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000324 self = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &self_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000325 cleanup_self = true;
326 }
327
328 mp_obj_set_t *other;
329 bool cleanup_other = false;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300330 if (is_set_or_frozenset(other_in)) {
Damien George999cedb2015-11-27 17:01:44 +0000331 other = MP_OBJ_TO_PTR(other_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000332 } else {
Damien George5b3f0b72016-01-03 15:55:55 +0000333 other = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &other_in));
John R. Lentonae00d332014-01-12 18:23:36 +0000334 cleanup_other = true;
335 }
John R. Lentonbe790f92014-01-12 23:09:10 +0000336 bool out = true;
337 if (proper && self->set.used == other->set.used) {
338 out = false;
339 } else {
Damien Georgeae8d8672016-01-09 23:14:54 +0000340 mp_obj_iter_buf_t iter_buf;
341 mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self), &iter_buf);
John R. Lentonbe790f92014-01-12 23:09:10 +0000342 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100343 while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000344 if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
345 out = false;
346 break;
347 }
John R. Lentonae00d332014-01-12 18:23:36 +0000348 }
349 }
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300350 // TODO: Should free objects altogether
John R. Lentonae00d332014-01-12 18:23:36 +0000351 if (cleanup_self) {
Damien George999cedb2015-11-27 17:01:44 +0000352 set_clear(MP_OBJ_FROM_PTR(self));
John R. Lentonae00d332014-01-12 18:23:36 +0000353 }
354 if (cleanup_other) {
Damien George999cedb2015-11-27 17:01:44 +0000355 set_clear(MP_OBJ_FROM_PTR(other));
John R. Lentonae00d332014-01-12 18:23:36 +0000356 }
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300357 return mp_obj_new_bool(out);
John R. Lentonbe790f92014-01-12 23:09:10 +0000358}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200359STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000360 return set_issubset_internal(self_in, other_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000361}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200362STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lentonae00d332014-01-12 18:23:36 +0000363
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200364STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000365 return set_issubset_internal(self_in, other_in, true);
366}
367
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200368STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000369 return set_issubset_internal(other_in, self_in, false);
John R. Lentonae00d332014-01-12 18:23:36 +0000370}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200371STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lentonae00d332014-01-12 18:23:36 +0000372
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200373STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000374 return set_issubset_internal(other_in, self_in, true);
375}
376
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200377STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300378 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000379 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300380 if (!is_set_or_frozenset(other_in)) {
John R. Lentonbe790f92014-01-12 23:09:10 +0000381 return mp_const_false;
382 }
Damien George999cedb2015-11-27 17:01:44 +0000383 mp_obj_set_t *other = MP_OBJ_TO_PTR(other_in);
John R. Lentonbe790f92014-01-12 23:09:10 +0000384 if (self->set.used != other->set.used) {
385 return mp_const_false;
386 }
387 return set_issubset(self_in, other_in);
388}
389
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200390STATIC mp_obj_t set_pop(mp_obj_t self_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300391 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000392 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100393 mp_obj_t obj = mp_set_remove_first(&self->set);
394 if (obj == MP_OBJ_NULL) {
Damien George7d0d7212016-10-17 12:17:37 +1100395 mp_raise_msg(&mp_type_KeyError, "pop from an empty set");
John R. Lentonae00d332014-01-12 18:23:36 +0000396 }
John R. Lentonae00d332014-01-12 18:23:36 +0000397 return obj;
398}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200399STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lentonae00d332014-01-12 18:23:36 +0000400
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200401STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300402 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000403 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lentonae00d332014-01-12 18:23:36 +0000404 if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100405 nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lentonae00d332014-01-12 18:23:36 +0000406 }
407 return mp_const_none;
408}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200409STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
John R. Lenton032129f2014-01-12 17:07:17 +0000410
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200411STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300412 check_set(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000413 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgee6003f42017-02-13 15:44:31 +1100414 mp_obj_t iter = mp_getiter(other_in, NULL);
John R. Lenton0de386b2014-01-12 19:39:48 +0000415 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100416 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georged1cee022015-03-20 17:41:37 +0000417 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 +0000418 }
419 return mp_const_none;
420}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200421STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000422
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200423STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300424 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000425 mp_obj_t self_out = set_copy_as_mutable(self_in);
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300426 set_symmetric_difference_update(self_out, other_in);
Damien George999cedb2015-11-27 17:01:44 +0000427 ((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 +0300428 return self_out;
John R. Lenton0de386b2014-01-12 19:39:48 +0000429}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200430STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton0de386b2014-01-12 19:39:48 +0000431
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200432STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien Georgee6003f42017-02-13 15:44:31 +1100433 mp_obj_t iter = mp_getiter(other_in, NULL);
John R. Lenton0de386b2014-01-12 19:39:48 +0000434 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100435 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton0de386b2014-01-12 19:39:48 +0000436 mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
437 }
438}
439
Damien George4b72b3a2016-01-03 14:21:40 +0000440STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
Damien George2c7716f2016-09-28 11:06:18 +1000441 check_set(args[0]);
Damien George68cd3a92017-02-16 16:16:33 +1100442 for (size_t i = 1; i < n_args; i++) {
Damien George999cedb2015-11-27 17:01:44 +0000443 set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
John R. Lenton0de386b2014-01-12 19:39:48 +0000444 }
445
446 return mp_const_none;
447}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200448STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
John R. Lenton0de386b2014-01-12 19:39:48 +0000449
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200450STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300451 check_set_or_frozenset(self_in);
Damien George999cedb2015-11-27 17:01:44 +0000452 mp_obj_t self = set_copy(self_in);
453 set_update_int(MP_OBJ_TO_PTR(self), other_in);
John R. Lenton0de386b2014-01-12 19:39:48 +0000454 return self;
455}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200456STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton0de386b2014-01-12 19:39:48 +0000457
Damien Georgeecc88e92014-08-30 00:35:11 +0100458STATIC mp_obj_t set_unary_op(mp_uint_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000459 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George95004e52014-04-05 17:17:19 +0100460 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300461 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->set.used != 0);
Damien Georgebb4c6f32014-07-31 10:49:14 +0100462 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->set.used);
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300463#if MICROPY_PY_BUILTINS_FROZENSET
464 case MP_UNARY_OP_HASH:
465 if (MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset)) {
466 // start hash with unique value
Damien George999cedb2015-11-27 17:01:44 +0000467 mp_int_t hash = (mp_int_t)(uintptr_t)&mp_type_frozenset;
Damien George68cd3a92017-02-16 16:16:33 +1100468 size_t max = self->set.alloc;
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300469 mp_set_t *set = &self->set;
470
Damien George68cd3a92017-02-16 16:16:33 +1100471 for (size_t i = 0; i < max; i++) {
Paul Sokolovsky8b3b2d02015-08-28 22:31:31 +0300472 if (MP_SET_SLOT_IS_FILLED(set, i)) {
473 hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
474 }
475 }
476 return MP_OBJ_NEW_SMALL_INT(hash);
477 }
478#endif
Damien George6ac5dce2014-05-21 19:42:43 +0100479 default: return MP_OBJ_NULL; // op not supported
Damien George95004e52014-04-05 17:17:19 +0100480 }
481}
John R. Lenton0de386b2014-01-12 19:39:48 +0000482
Damien Georgeecc88e92014-08-30 00:35:11 +0100483STATIC 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 +0000484 mp_obj_t args[] = {lhs, rhs};
Damien George2486c4f2017-02-03 00:27:56 +1100485 #if MICROPY_PY_BUILTINS_FROZENSET
486 bool update = MP_OBJ_IS_TYPE(lhs, &mp_type_set);
487 #else
488 bool update = true;
489 #endif
John R. Lentonbe790f92014-01-12 23:09:10 +0000490 switch (op) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100491 case MP_BINARY_OP_OR:
492 return set_union(lhs, rhs);
493 case MP_BINARY_OP_XOR:
494 return set_symmetric_difference(lhs, rhs);
495 case MP_BINARY_OP_AND:
496 return set_intersect(lhs, rhs);
497 case MP_BINARY_OP_SUBTRACT:
498 return set_diff(2, args);
499 case MP_BINARY_OP_INPLACE_OR:
Damien George2486c4f2017-02-03 00:27:56 +1100500 if (update) {
501 set_update(2, args);
502 return lhs;
503 } else {
504 return set_union(lhs, rhs);
505 }
Damien Georged0a5bf32014-05-10 13:55:11 +0100506 case MP_BINARY_OP_INPLACE_XOR:
Damien George2486c4f2017-02-03 00:27:56 +1100507 if (update) {
508 set_symmetric_difference_update(lhs, rhs);
509 return lhs;
510 } else {
511 return set_symmetric_difference(lhs, rhs);
512 }
Damien Georged0a5bf32014-05-10 13:55:11 +0100513 case MP_BINARY_OP_INPLACE_AND:
Damien George2486c4f2017-02-03 00:27:56 +1100514 rhs = set_intersect_int(lhs, rhs, update);
515 if (update) {
516 return lhs;
517 } else {
518 return rhs;
519 }
Damien Georged0a5bf32014-05-10 13:55:11 +0100520 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George2486c4f2017-02-03 00:27:56 +1100521 return set_diff_int(2, args, update);
Damien Georged0a5bf32014-05-10 13:55:11 +0100522 case MP_BINARY_OP_LESS:
523 return set_issubset_proper(lhs, rhs);
524 case MP_BINARY_OP_MORE:
525 return set_issuperset_proper(lhs, rhs);
526 case MP_BINARY_OP_EQUAL:
527 return set_equal(lhs, rhs);
528 case MP_BINARY_OP_LESS_EQUAL:
529 return set_issubset(lhs, rhs);
530 case MP_BINARY_OP_MORE_EQUAL:
531 return set_issuperset(lhs, rhs);
532 case MP_BINARY_OP_IN: {
Damien George999cedb2015-11-27 17:01:44 +0000533 mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
Damien Georged0a5bf32014-05-10 13:55:11 +0100534 mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
Damien George999cedb2015-11-27 17:01:44 +0000535 return mp_obj_new_bool(elem != MP_OBJ_NULL);
Damien Georged0a5bf32014-05-10 13:55:11 +0100536 }
537 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100538 return MP_OBJ_NULL; // op not supported
John R. Lentonbe790f92014-01-12 23:09:10 +0000539 }
540}
John R. Lenton0de386b2014-01-12 19:39:48 +0000541
John R. Lenton19b14d32014-01-12 15:29:11 +0000542/******************************************************************************/
543/* set constructors & public C API */
544
545
Damien Georgecbf76742015-11-27 13:38:15 +0000546STATIC const mp_rom_map_elem_t set_locals_dict_table[] = {
547 { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&set_add_obj) },
548 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&set_clear_obj) },
549 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
550 { MP_ROM_QSTR(MP_QSTR_discard), MP_ROM_PTR(&set_discard_obj) },
551 { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
552 { MP_ROM_QSTR(MP_QSTR_difference_update), MP_ROM_PTR(&set_diff_update_obj) },
553 { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
554 { MP_ROM_QSTR(MP_QSTR_intersection_update), MP_ROM_PTR(&set_intersect_update_obj) },
555 { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
556 { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
557 { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
558 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&set_pop_obj) },
559 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&set_remove_obj) },
560 { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
561 { MP_ROM_QSTR(MP_QSTR_symmetric_difference_update), MP_ROM_PTR(&set_symmetric_difference_update_obj) },
562 { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
563 { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&set_update_obj) },
564 { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
John R. Lenton19b14d32014-01-12 15:29:11 +0000565};
566
Damien George9b196cd2014-03-26 21:47:19 +0000567STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
568
Damien George3e1a5c12014-03-29 13:43:38 +0000569const mp_obj_type_t mp_type_set = {
Damien Georgec5966122014-02-15 16:10:44 +0000570 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000571 .name = MP_QSTR_set,
Damien George97209d32014-01-07 15:58:30 +0000572 .print = set_print,
573 .make_new = set_make_new,
Damien George95004e52014-04-05 17:17:19 +0100574 .unary_op = set_unary_op,
John R. Lentonbe790f92014-01-12 23:09:10 +0000575 .binary_op = set_binary_op,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000576 .getiter = set_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000577 .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000578};
579
Damien Georgefb510b32014-06-01 13:32:54 +0100580#if MICROPY_PY_BUILTINS_FROZENSET
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300581const mp_obj_type_t mp_type_frozenset = {
582 { &mp_type_type },
583 .name = MP_QSTR_frozenset,
584 .print = set_print,
585 .make_new = set_make_new,
586 .unary_op = set_unary_op,
587 .binary_op = set_binary_op,
588 .getiter = set_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000589 .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300590};
591#endif
592
Damien George68cd3a92017-02-16 16:16:33 +1100593mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000594 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000595 o->base.type = &mp_type_set;
Damiend99b0522013-12-21 18:17:45 +0000596 mp_set_init(&o->set, n_args);
Damien George68cd3a92017-02-16 16:16:33 +1100597 for (size_t i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000598 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000599 }
Damien George999cedb2015-11-27 17:01:44 +0000600 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000601}
602
603void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300604 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
Damien George999cedb2015-11-27 17:01:44 +0000605 mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton2a241722014-01-12 16:39:39 +0000606 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000607}
Damien George3ebd4d02014-06-01 13:46:47 +0100608
609#endif // MICROPY_PY_BUILTINS_SET