2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-10-03 02:26:45 -04:00
|
|
|
* Copyright (c) 2013-2017 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-16 03:14:26 -04:00
|
|
|
#include <stdbool.h>
|
2014-01-12 10:56:25 -05:00
|
|
|
#include <string.h>
|
2013-12-21 13:17:45 -05:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/builtin.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-06-01 08:46:47 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_SET
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
typedef struct _mp_obj_set_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_set_t set;
|
|
|
|
} mp_obj_set_t;
|
|
|
|
|
2014-01-12 10:17:42 -05:00
|
|
|
typedef struct _mp_obj_set_it_t {
|
|
|
|
mp_obj_base_t base;
|
2016-01-03 11:27:55 -05:00
|
|
|
mp_fun_1_t iternext;
|
2014-01-12 10:17:42 -05:00
|
|
|
mp_obj_set_t *set;
|
2017-02-16 00:16:33 -05:00
|
|
|
size_t cur;
|
2014-01-12 10:17:42 -05:00
|
|
|
} mp_obj_set_it_t;
|
|
|
|
|
2014-05-10 09:02:17 -04:00
|
|
|
STATIC bool is_set_or_frozenset(mp_obj_t o) {
|
2019-01-30 02:49:52 -05:00
|
|
|
return mp_obj_is_type(o, &mp_type_set)
|
2020-02-26 23:36:53 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
|
|
|
|| mp_obj_is_type(o, &mp_type_frozenset)
|
|
|
|
#endif
|
2014-05-10 09:11:04 -04:00
|
|
|
;
|
2014-05-10 09:02:17 -04:00
|
|
|
}
|
|
|
|
|
2017-10-03 02:26:45 -04:00
|
|
|
// This macro is shorthand for mp_check_self to verify the argument is a set.
|
2019-01-30 02:49:52 -05:00
|
|
|
#define check_set(o) mp_check_self(mp_obj_is_type(o, &mp_type_set))
|
2017-10-03 02:26:45 -04:00
|
|
|
|
2016-09-27 20:55:23 -04:00
|
|
|
// This macro is shorthand for mp_check_self to verify the argument is a
|
|
|
|
// set or frozenset for methods that operate on both of these types.
|
|
|
|
#define check_set_or_frozenset(o) mp_check_self(is_set_or_frozenset(o))
|
2014-05-10 09:02:17 -04:00
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void set_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
2019-01-30 02:49:52 -05:00
|
|
|
bool is_frozen = mp_obj_is_type(self_in, &mp_type_frozenset);
|
2014-05-10 09:11:04 -04:00
|
|
|
#endif
|
2014-01-12 18:37:45 -05:00
|
|
|
if (self->set.used == 0) {
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
2014-05-10 09:02:17 -04:00
|
|
|
if (is_frozen) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "frozen");
|
2014-05-10 09:02:17 -04:00
|
|
|
}
|
2014-05-10 09:11:04 -04:00
|
|
|
#endif
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "set()");
|
2014-01-12 18:37:45 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
bool first = true;
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
2014-05-10 09:02:17 -04:00
|
|
|
if (is_frozen) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "frozenset(");
|
2014-05-10 09:02:17 -04:00
|
|
|
}
|
2014-05-10 09:11:04 -04:00
|
|
|
#endif
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "{");
|
2017-02-16 00:16:33 -05:00
|
|
|
for (size_t i = 0; i < self->set.alloc; i++) {
|
2019-01-30 05:57:29 -05:00
|
|
|
if (mp_set_slot_is_filled(&self->set, i)) {
|
2013-12-21 13:17:45 -05:00
|
|
|
if (!first) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, ", ");
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
first = false;
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_obj_print_helper(print, self->set.table[i], PRINT_REPR);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "}");
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
2014-05-10 09:02:17 -04:00
|
|
|
if (is_frozen) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, ")");
|
2014-05-10 09:02:17 -04:00
|
|
|
}
|
2014-05-10 09:11:04 -04:00
|
|
|
#endif
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2016-01-03 10:55:55 -05:00
|
|
|
STATIC 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) {
|
2014-05-11 13:28:48 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2014-01-04 15:21:15 -05:00
|
|
|
switch (n_args) {
|
2014-05-11 13:28:48 -04:00
|
|
|
case 0: {
|
|
|
|
// create a new, empty set
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *set = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
|
2014-05-11 13:28:48 -04:00
|
|
|
// set actual set/frozenset type
|
2016-01-03 10:55:55 -05:00
|
|
|
set->base.type = type;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(set);
|
2014-05-11 13:28:48 -04:00
|
|
|
}
|
2014-01-04 15:21:15 -05:00
|
|
|
|
|
|
|
case 1:
|
2014-05-11 13:28:48 -04:00
|
|
|
default: { // can only be 0 or 1 arg
|
2014-01-04 15:21:15 -05:00
|
|
|
// 1 argument, an iterable from which we make a new set
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_t set = mp_obj_new_set(0, NULL);
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t iterable = mp_getiter(args[0], NULL);
|
2014-01-04 15:21:15 -05:00
|
|
|
mp_obj_t item;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-04 15:21:15 -05:00
|
|
|
mp_obj_set_store(set, item);
|
|
|
|
}
|
2014-05-10 09:02:17 -04:00
|
|
|
// Set actual set/frozenset type
|
2020-02-26 23:36:53 -05:00
|
|
|
((mp_obj_set_t *)MP_OBJ_TO_PTR(set))->base.type = type;
|
2014-01-04 15:21:15 -05:00
|
|
|
return set;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_it_t *self = MP_OBJ_TO_PTR(self_in);
|
2017-02-16 00:16:33 -05:00
|
|
|
size_t max = self->set->set.alloc;
|
2014-04-05 16:53:54 -04:00
|
|
|
mp_set_t *set = &self->set->set;
|
2014-01-12 10:17:42 -05:00
|
|
|
|
2017-02-16 00:16:33 -05:00
|
|
|
for (size_t i = self->cur; i < max; i++) {
|
2019-01-30 05:57:29 -05:00
|
|
|
if (mp_set_slot_is_filled(set, i)) {
|
2014-01-12 10:17:42 -05:00
|
|
|
self->cur = i + 1;
|
2014-04-05 16:53:54 -04:00
|
|
|
return set->table[i];
|
2014-01-12 10:17:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 18:19:36 -04:00
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-01-12 10:17:42 -05:00
|
|
|
}
|
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
STATIC mp_obj_t set_getiter(mp_obj_t set_in, mp_obj_iter_buf_t *iter_buf) {
|
|
|
|
assert(sizeof(mp_obj_set_it_t) <= sizeof(mp_obj_iter_buf_t));
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_obj_set_it_t *o = (mp_obj_set_it_t *)iter_buf;
|
2016-01-03 11:27:55 -05:00
|
|
|
o->base.type = &mp_type_polymorph_iter;
|
|
|
|
o->iternext = set_it_iternext;
|
2015-11-27 12:01:44 -05:00
|
|
|
o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
|
2014-01-12 10:17:42 -05:00
|
|
|
o->cur = 0;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-12 10:17:42 -05:00
|
|
|
}
|
|
|
|
|
2014-01-12 10:29:11 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* set methods */
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-12 11:39:39 -05:00
|
|
|
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2014-01-12 10:29:11 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
|
2014-01-12 10:29:11 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_clear(mp_obj_t self_in) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-12 10:44:26 -05:00
|
|
|
mp_set_clear(&self->set);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
|
2014-01-12 10:44:26 -05:00
|
|
|
|
2017-10-03 02:26:45 -04:00
|
|
|
STATIC mp_obj_t set_copy(mp_obj_t self_in) {
|
|
|
|
check_set_or_frozenset(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_set_t *other = mp_obj_malloc(mp_obj_set_t, self->base.type);
|
2014-04-06 20:07:21 -04:00
|
|
|
mp_set_init(&other->set, self->set.alloc);
|
2014-01-12 10:56:25 -05:00
|
|
|
other->set.used = self->set.used;
|
|
|
|
memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(other);
|
2014-01-12 10:56:25 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
|
2014-01-12 10:56:25 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-12 11:39:39 -05:00
|
|
|
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
|
2014-01-12 10:29:11 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_t self;
|
2014-01-12 12:07:17 -05:00
|
|
|
if (update) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set(args[0]);
|
2014-01-12 12:07:17 -05:00
|
|
|
self = args[0];
|
|
|
|
} else {
|
2017-10-03 02:26:45 -04:00
|
|
|
self = set_copy(args[0]);
|
2014-01-12 12:07:17 -05:00
|
|
|
}
|
|
|
|
|
2017-02-16 00:16:33 -05:00
|
|
|
for (size_t i = 1; i < n_args; i++) {
|
2014-01-12 12:07:17 -05:00
|
|
|
mp_obj_t other = args[i];
|
|
|
|
if (self == other) {
|
|
|
|
set_clear(self);
|
|
|
|
} else {
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_set_t *self_set = &((mp_obj_set_t *)MP_OBJ_TO_PTR(self))->set;
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t iter = mp_getiter(other, NULL);
|
2014-01-12 12:07:17 -05:00
|
|
|
mp_obj_t next;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2017-10-03 02:26:45 -04:00
|
|
|
mp_set_lookup(self_set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
2014-01-12 12:07:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t set_diff(size_t n_args, const mp_obj_t *args) {
|
2014-01-12 12:07:17 -05:00
|
|
|
return set_diff_int(n_args, args, false);
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
|
2014-01-12 12:07:17 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t set_diff_update(size_t n_args, const mp_obj_t *args) {
|
2014-01-12 12:07:17 -05:00
|
|
|
set_diff_int(n_args, args, true);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
|
2014-01-12 12:07:17 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
|
2014-05-10 09:02:17 -04:00
|
|
|
if (update) {
|
|
|
|
check_set(self_in);
|
|
|
|
} else {
|
|
|
|
check_set_or_frozenset(self_in);
|
|
|
|
}
|
|
|
|
|
2014-01-12 12:54:03 -05:00
|
|
|
if (self_in == other) {
|
|
|
|
return update ? mp_const_none : set_copy(self_in);
|
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
|
2014-01-12 12:54:03 -05:00
|
|
|
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t iter = mp_getiter(other, NULL);
|
2014-01-12 12:54:03 -05:00
|
|
|
mp_obj_t next;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 12:54:03 -05:00
|
|
|
if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
|
2015-11-27 12:01:44 -05:00
|
|
|
set_add(MP_OBJ_FROM_PTR(out), next);
|
2014-01-12 12:54:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
m_del(mp_obj_t, self->set.table, self->set.alloc);
|
|
|
|
self->set.alloc = out->set.alloc;
|
|
|
|
self->set.used = out->set.used;
|
|
|
|
self->set.table = out->set.table;
|
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
|
2014-01-12 12:54:03 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
|
2014-01-12 12:54:03 -05:00
|
|
|
return set_intersect_int(self_in, other, false);
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
|
2014-01-12 12:54:03 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
|
2014-01-12 12:54:03 -05:00
|
|
|
return set_intersect_int(self_in, other, true);
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
|
2014-01-12 12:54:03 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set_or_frozenset(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-12 13:03:21 -05:00
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
mp_obj_iter_buf_t iter_buf;
|
|
|
|
mp_obj_t iter = mp_getiter(other, &iter_buf);
|
2014-01-12 13:03:21 -05:00
|
|
|
mp_obj_t next;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 13:03:21 -05:00
|
|
|
if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
|
2014-01-12 13:03:21 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) {
|
2014-01-12 13:23:36 -05:00
|
|
|
mp_obj_set_t *self;
|
|
|
|
bool cleanup_self = false;
|
2014-05-10 09:02:17 -04:00
|
|
|
if (is_set_or_frozenset(self_in)) {
|
2015-11-27 12:01:44 -05:00
|
|
|
self = MP_OBJ_TO_PTR(self_in);
|
2014-01-12 13:23:36 -05:00
|
|
|
} else {
|
2016-01-03 10:55:55 -05:00
|
|
|
self = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &self_in));
|
2014-01-12 13:23:36 -05:00
|
|
|
cleanup_self = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_set_t *other;
|
|
|
|
bool cleanup_other = false;
|
2014-05-10 09:02:17 -04:00
|
|
|
if (is_set_or_frozenset(other_in)) {
|
2015-11-27 12:01:44 -05:00
|
|
|
other = MP_OBJ_TO_PTR(other_in);
|
2014-01-12 13:23:36 -05:00
|
|
|
} else {
|
2016-01-03 10:55:55 -05:00
|
|
|
other = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &other_in));
|
2014-01-12 13:23:36 -05:00
|
|
|
cleanup_other = true;
|
|
|
|
}
|
2017-10-03 02:26:45 -04:00
|
|
|
mp_obj_t out = mp_const_true;
|
2014-01-12 18:09:10 -05:00
|
|
|
if (proper && self->set.used == other->set.used) {
|
2017-10-03 02:26:45 -04:00
|
|
|
out = mp_const_false;
|
2014-01-12 18:09:10 -05:00
|
|
|
} else {
|
2016-01-09 18:14:54 -05:00
|
|
|
mp_obj_iter_buf_t iter_buf;
|
|
|
|
mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self), &iter_buf);
|
2014-01-12 18:09:10 -05:00
|
|
|
mp_obj_t next;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 18:09:10 -05:00
|
|
|
if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
|
2017-10-03 02:26:45 -04:00
|
|
|
out = mp_const_false;
|
2014-01-12 18:09:10 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-01-12 13:23:36 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-10 09:02:17 -04:00
|
|
|
// TODO: Should free objects altogether
|
2014-01-12 13:23:36 -05:00
|
|
|
if (cleanup_self) {
|
2015-11-27 12:01:44 -05:00
|
|
|
set_clear(MP_OBJ_FROM_PTR(self));
|
2014-01-12 13:23:36 -05:00
|
|
|
}
|
|
|
|
if (cleanup_other) {
|
2015-11-27 12:01:44 -05:00
|
|
|
set_clear(MP_OBJ_FROM_PTR(other));
|
2014-01-12 13:23:36 -05:00
|
|
|
}
|
2017-10-03 02:26:45 -04:00
|
|
|
return out;
|
2014-01-12 18:09:10 -05:00
|
|
|
}
|
2019-04-14 21:14:22 -04:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 18:09:10 -05:00
|
|
|
return set_issubset_internal(self_in, other_in, false);
|
2014-01-12 13:23:36 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
|
2014-01-12 13:23:36 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 18:09:10 -05:00
|
|
|
return set_issubset_internal(self_in, other_in, true);
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 18:09:10 -05:00
|
|
|
return set_issubset_internal(other_in, self_in, false);
|
2014-01-12 13:23:36 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
|
2014-01-12 13:23:36 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 18:09:10 -05:00
|
|
|
return set_issubset_internal(other_in, self_in, true);
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
|
2017-12-18 22:01:19 -05:00
|
|
|
assert(is_set_or_frozenset(other_in));
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set_or_frozenset(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_obj_set_t *other = MP_OBJ_TO_PTR(other_in);
|
2014-01-12 18:09:10 -05:00
|
|
|
if (self->set.used != other->set.used) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
return set_issubset(self_in, other_in);
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_pop(mp_obj_t self_in) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-05 12:17:19 -04:00
|
|
|
mp_obj_t obj = mp_set_remove_first(&self->set);
|
|
|
|
if (obj == MP_OBJ_NULL) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_KeyError, MP_ERROR_TEXT("pop from an empty set"));
|
2014-01-12 13:23:36 -05:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
|
2014-01-12 13:23:36 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-12 13:23:36 -05:00
|
|
|
if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
|
2021-07-14 08:56:52 -04:00
|
|
|
mp_raise_type_arg(&mp_type_KeyError, item);
|
2014-01-12 13:23:36 -05:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
|
2014-01-12 12:07:17 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
|
2017-10-03 02:26:45 -04:00
|
|
|
check_set_or_frozenset(self_in); // can be frozenset due to call from set_symmetric_difference
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t iter = mp_getiter(other_in, NULL);
|
2014-01-12 14:39:48 -05:00
|
|
|
mp_obj_t next;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2015-03-20 13:41:37 -04:00
|
|
|
mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND);
|
2014-01-12 14:39:48 -05:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
|
2014-01-12 14:39:48 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
|
2017-10-03 02:26:45 -04:00
|
|
|
mp_obj_t self_out = set_copy(self_in);
|
2014-05-10 09:02:17 -04:00
|
|
|
set_symmetric_difference_update(self_out, other_in);
|
|
|
|
return self_out;
|
2014-01-12 14:39:48 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
|
2014-01-12 14:39:48 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t iter = mp_getiter(other_in, NULL);
|
2014-01-12 14:39:48 -05:00
|
|
|
mp_obj_t next;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 14:39:48 -05:00
|
|
|
mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
|
2016-09-27 21:06:18 -04:00
|
|
|
check_set(args[0]);
|
2017-02-16 00:16:33 -05:00
|
|
|
for (size_t i = 1; i < n_args; i++) {
|
2015-11-27 12:01:44 -05:00
|
|
|
set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
|
2014-01-12 14:39:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
|
2014-01-12 14:39:48 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-05-10 09:02:17 -04:00
|
|
|
check_set_or_frozenset(self_in);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_t self = set_copy(self_in);
|
|
|
|
set_update_int(MP_OBJ_TO_PTR(self), other_in);
|
2014-01-12 14:39:48 -05:00
|
|
|
return self;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
|
2014-01-12 14:39:48 -05:00
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
STATIC mp_obj_t set_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-05 12:17:19 -04:00
|
|
|
switch (op) {
|
2020-02-26 23:36:53 -05:00
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
return mp_obj_new_bool(self->set.used != 0);
|
|
|
|
case MP_UNARY_OP_LEN:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->set.used);
|
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
2015-08-28 15:31:31 -04:00
|
|
|
case MP_UNARY_OP_HASH:
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_type(self_in, &mp_type_frozenset)) {
|
2015-08-28 15:31:31 -04:00
|
|
|
// start hash with unique value
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_int_t hash = (mp_int_t)(uintptr_t)&mp_type_frozenset;
|
2017-02-16 00:16:33 -05:00
|
|
|
size_t max = self->set.alloc;
|
2015-08-28 15:31:31 -04:00
|
|
|
mp_set_t *set = &self->set;
|
|
|
|
|
2017-02-16 00:16:33 -05:00
|
|
|
for (size_t i = 0; i < max; i++) {
|
2019-01-30 05:57:29 -05:00
|
|
|
if (mp_set_slot_is_filled(set, i)) {
|
2015-08-28 15:31:31 -04:00
|
|
|
hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(hash);
|
|
|
|
}
|
2019-11-28 06:47:21 -05:00
|
|
|
MP_FALLTHROUGH
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-04-05 12:17:19 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-12 14:39:48 -05:00
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
STATIC mp_obj_t set_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
|
2014-01-12 18:09:10 -05:00
|
|
|
mp_obj_t args[] = {lhs, rhs};
|
2017-02-02 08:27:56 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
2019-01-30 02:49:52 -05:00
|
|
|
bool update = mp_obj_is_type(lhs, &mp_type_set);
|
2017-02-02 08:27:56 -05:00
|
|
|
#else
|
|
|
|
bool update = true;
|
|
|
|
#endif
|
2017-11-23 21:04:24 -05:00
|
|
|
if (op != MP_BINARY_OP_CONTAINS && !is_set_or_frozenset(rhs)) {
|
2017-10-03 02:56:27 -04:00
|
|
|
// For all ops except containment the RHS must be a set/frozenset
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
2014-01-12 18:09:10 -05:00
|
|
|
switch (op) {
|
2014-05-10 08:55:11 -04:00
|
|
|
case MP_BINARY_OP_OR:
|
|
|
|
return set_union(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_XOR:
|
|
|
|
return set_symmetric_difference(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_AND:
|
|
|
|
return set_intersect(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_SUBTRACT:
|
|
|
|
return set_diff(2, args);
|
|
|
|
case MP_BINARY_OP_INPLACE_OR:
|
2017-02-02 08:27:56 -05:00
|
|
|
if (update) {
|
|
|
|
set_update(2, args);
|
|
|
|
return lhs;
|
|
|
|
} else {
|
|
|
|
return set_union(lhs, rhs);
|
|
|
|
}
|
2014-05-10 08:55:11 -04:00
|
|
|
case MP_BINARY_OP_INPLACE_XOR:
|
2017-02-02 08:27:56 -05:00
|
|
|
if (update) {
|
|
|
|
set_symmetric_difference_update(lhs, rhs);
|
|
|
|
return lhs;
|
|
|
|
} else {
|
|
|
|
return set_symmetric_difference(lhs, rhs);
|
|
|
|
}
|
2014-05-10 08:55:11 -04:00
|
|
|
case MP_BINARY_OP_INPLACE_AND:
|
2017-02-02 08:27:56 -05:00
|
|
|
rhs = set_intersect_int(lhs, rhs, update);
|
|
|
|
if (update) {
|
|
|
|
return lhs;
|
|
|
|
} else {
|
|
|
|
return rhs;
|
|
|
|
}
|
2014-05-10 08:55:11 -04:00
|
|
|
case MP_BINARY_OP_INPLACE_SUBTRACT:
|
2017-02-02 08:27:56 -05:00
|
|
|
return set_diff_int(2, args, update);
|
2014-05-10 08:55:11 -04:00
|
|
|
case MP_BINARY_OP_LESS:
|
|
|
|
return set_issubset_proper(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_MORE:
|
|
|
|
return set_issuperset_proper(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_EQUAL:
|
|
|
|
return set_equal(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_LESS_EQUAL:
|
|
|
|
return set_issubset(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_MORE_EQUAL:
|
|
|
|
return set_issuperset(lhs, rhs);
|
2017-11-23 21:04:24 -05:00
|
|
|
case MP_BINARY_OP_CONTAINS: {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
|
2014-05-10 08:55:11 -04:00
|
|
|
mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
|
2015-11-27 12:01:44 -05:00
|
|
|
return mp_obj_new_bool(elem != MP_OBJ_NULL);
|
2014-05-10 08:55:11 -04:00
|
|
|
}
|
|
|
|
default:
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-01-12 18:09:10 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-12 14:39:48 -05:00
|
|
|
|
2014-01-12 10:29:11 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* set constructors & public C API */
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t set_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&set_add_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&set_clear_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_discard), MP_ROM_PTR(&set_discard_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_difference_update), MP_ROM_PTR(&set_diff_update_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_intersection_update), MP_ROM_PTR(&set_intersect_update_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&set_pop_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&set_remove_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_symmetric_difference_update), MP_ROM_PTR(&set_symmetric_difference_update_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&set_update_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
|
2014-01-12 10:29:11 -05:00
|
|
|
};
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
mp_type_set,
|
|
|
|
MP_QSTR_set,
|
2022-09-16 09:57:38 -04:00
|
|
|
MP_TYPE_FLAG_ITER_IS_GETITER,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, set_make_new,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, set_print,
|
|
|
|
unary_op, set_unary_op,
|
|
|
|
binary_op, set_binary_op,
|
2022-09-16 09:57:38 -04:00
|
|
|
iter, set_getiter,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &set_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FROZENSET
|
2017-10-03 02:26:45 -04:00
|
|
|
STATIC const mp_rom_map_elem_t frozenset_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(frozenset_locals_dict, frozenset_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
mp_type_frozenset,
|
|
|
|
MP_QSTR_frozenset,
|
2022-09-16 09:57:38 -04:00
|
|
|
MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_ITER_IS_GETITER,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, set_make_new,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, set_print,
|
|
|
|
unary_op, set_unary_op,
|
|
|
|
binary_op, set_binary_op,
|
2022-09-16 09:57:38 -04:00
|
|
|
iter, set_getiter,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &frozenset_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2014-05-10 09:02:17 -04:00
|
|
|
#endif
|
|
|
|
|
2017-02-16 00:16:33 -05:00
|
|
|
mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items) {
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_set_t *o = mp_obj_malloc(mp_obj_set_t, &mp_type_set);
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_set_init(&o->set, n_args);
|
2017-02-16 00:16:33 -05:00
|
|
|
for (size_t i = 0; i < n_args; i++) {
|
2014-01-12 11:39:39 -05:00
|
|
|
mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_type(self_in, &mp_type_set));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-12 11:39:39 -05:00
|
|
|
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
2014-06-01 08:46:47 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_PY_BUILTINS_SET
|