2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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 07:14:26 +00:00
|
|
|
#include <stdbool.h>
|
2014-01-12 15:56:25 +00:00
|
|
|
#include <string.h>
|
2013-12-21 18:17:45 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-05-02 15:47:01 +01:00
|
|
|
#include "mpconfig.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "obj.h"
|
2014-01-04 20:21:15 +00:00
|
|
|
#include "runtime.h"
|
2014-01-11 12:39:33 +00:00
|
|
|
#include "runtime0.h"
|
2014-04-13 09:53:15 +01:00
|
|
|
#include "builtin.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
|
|
|
|
typedef struct _mp_obj_set_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_set_t set;
|
|
|
|
} mp_obj_set_t;
|
|
|
|
|
2014-01-12 15:17:42 +00:00
|
|
|
typedef struct _mp_obj_set_it_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_set_t *set;
|
|
|
|
machine_uint_t cur;
|
|
|
|
} mp_obj_set_it_t;
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
|
2014-01-12 15:17:42 +00:00
|
|
|
|
2014-05-10 14:02:17 +01:00
|
|
|
STATIC bool is_set_or_frozenset(mp_obj_t o) {
|
|
|
|
return MP_OBJ_IS_TYPE(o, &mp_type_set) || MP_OBJ_IS_TYPE(o, &mp_type_frozenset);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void check_set_or_frozenset(mp_obj_t o) {
|
|
|
|
if (!is_set_or_frozenset(o)) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void check_set(mp_obj_t o) {
|
|
|
|
if (!MP_OBJ_IS_TYPE(o, &mp_type_set)) {
|
|
|
|
// Emulate CPython behavior
|
|
|
|
// AttributeError: 'frozenset' object has no attribute 'add'
|
|
|
|
if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "'frozenset' has no such attribute"));
|
|
|
|
}
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
2014-05-10 14:02:17 +01:00
|
|
|
bool is_frozen = MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset);
|
2014-01-12 23:37:45 +00:00
|
|
|
if (self->set.used == 0) {
|
2014-05-10 14:02:17 +01:00
|
|
|
if (is_frozen) {
|
|
|
|
print(env, "frozen");
|
|
|
|
}
|
2014-01-12 23:37:45 +00:00
|
|
|
print(env, "set()");
|
|
|
|
return;
|
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
bool first = true;
|
2014-05-10 14:02:17 +01:00
|
|
|
if (is_frozen) {
|
|
|
|
print(env, "frozenset(");
|
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
print(env, "{");
|
|
|
|
for (int i = 0; i < self->set.alloc; i++) {
|
2014-04-05 21:53:54 +01:00
|
|
|
if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
|
2013-12-21 18:17:45 +00:00
|
|
|
if (!first) {
|
|
|
|
print(env, ", ");
|
|
|
|
}
|
|
|
|
first = false;
|
2014-01-13 17:19:16 +00:00
|
|
|
mp_obj_print_helper(print, env, self->set.table[i], PRINT_REPR);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
print(env, "}");
|
2014-05-10 14:02:17 +01:00
|
|
|
if (is_frozen) {
|
|
|
|
print(env, ")");
|
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 12:39:33 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2014-01-18 14:10:48 +00:00
|
|
|
// TODO check n_kw == 0
|
|
|
|
|
2014-01-04 20:21:15 +00:00
|
|
|
switch (n_args) {
|
|
|
|
case 0:
|
|
|
|
// return a new, empty set
|
|
|
|
return mp_obj_new_set(0, NULL);
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// 1 argument, an iterable from which we make a new set
|
2014-05-10 14:02:17 +01:00
|
|
|
mp_obj_set_t *set = mp_obj_new_set(0, NULL);
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t iterable = mp_getiter(args[0]);
|
2014-01-04 20:21:15 +00:00
|
|
|
mp_obj_t item;
|
2014-04-17 23:19:36 +01:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-04 20:21:15 +00:00
|
|
|
mp_obj_set_store(set, item);
|
|
|
|
}
|
2014-05-10 14:02:17 +01:00
|
|
|
// Set actual set/frozenset type
|
|
|
|
set->base.type = type_in;
|
2014-01-04 20:21:15 +00:00
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "set takes at most 1 argument, %d given", n_args));
|
2014-01-04 20:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 13:43:38 +00:00
|
|
|
const mp_obj_type_t mp_type_set_it = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_iterator,
|
2014-03-30 20:00:12 +01:00
|
|
|
.getiter = mp_identity,
|
2014-01-12 15:17:42 +00:00
|
|
|
.iternext = set_it_iternext,
|
|
|
|
};
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
|
2014-03-29 13:43:38 +00:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set_it));
|
2014-01-12 15:17:42 +00:00
|
|
|
mp_obj_set_it_t *self = self_in;
|
|
|
|
machine_uint_t max = self->set->set.alloc;
|
2014-04-05 21:53:54 +01:00
|
|
|
mp_set_t *set = &self->set->set;
|
2014-01-12 15:17:42 +00:00
|
|
|
|
|
|
|
for (machine_uint_t i = self->cur; i < max; i++) {
|
2014-04-05 21:53:54 +01:00
|
|
|
if (MP_SET_SLOT_IS_FILLED(set, i)) {
|
2014-01-12 15:17:42 +00:00
|
|
|
self->cur = i + 1;
|
2014-04-05 21:53:54 +01:00
|
|
|
return set->table[i];
|
2014-01-12 15:17:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 23:19:36 +01:00
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-01-12 15:17:42 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
|
2014-01-12 15:17:42 +00:00
|
|
|
mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
|
2014-03-29 13:43:38 +00:00
|
|
|
o->base.type = &mp_type_set_it;
|
2014-01-12 15:17:42 +00:00
|
|
|
o->set = (mp_obj_set_t *)set_in;
|
|
|
|
o->cur = 0;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-01-12 15:29:11 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* set methods */
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set(self_in);
|
2014-01-12 15:29:11 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
2014-01-12 16:39:39 +00:00
|
|
|
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2014-01-12 15:29:11 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
|
2014-01-12 15:29:11 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_clear(mp_obj_t self_in) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set(self_in);
|
2014-01-12 15:44:26 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
|
|
|
|
mp_set_clear(&self->set);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
|
2014-01-12 15:44:26 +00:00
|
|
|
|
2014-05-10 14:02:17 +01:00
|
|
|
STATIC mp_obj_t set_copy_as_mutable(mp_obj_t self_in) {
|
2014-01-12 15:56:25 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
|
|
|
|
mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
|
2014-03-29 13:43:38 +00:00
|
|
|
other->base.type = &mp_type_set;
|
2014-04-07 01:07:21 +01:00
|
|
|
mp_set_init(&other->set, self->set.alloc);
|
2014-01-12 15:56:25 +00:00
|
|
|
other->set.used = self->set.used;
|
|
|
|
memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
|
|
|
|
|
|
|
|
return other;
|
|
|
|
}
|
2014-05-10 14:02:17 +01:00
|
|
|
|
|
|
|
STATIC mp_obj_t set_copy(mp_obj_t self_in) {
|
|
|
|
check_set_or_frozenset(self_in);
|
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
|
|
|
|
mp_obj_set_t *other = set_copy_as_mutable(self);
|
|
|
|
other->base.type = self->base.type;
|
|
|
|
|
|
|
|
return other;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
|
2014-01-12 15:56:25 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set(self_in);
|
2014-01-12 16:39:39 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
|
2014-01-12 15:29:11 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
|
2014-01-12 17:07:17 +00:00
|
|
|
assert(n_args > 0);
|
2014-05-10 14:02:17 +01:00
|
|
|
|
2014-01-12 17:07:17 +00:00
|
|
|
mp_obj_set_t *self;
|
|
|
|
if (update) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set(args[0]);
|
2014-01-12 17:07:17 +00:00
|
|
|
self = args[0];
|
|
|
|
} else {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set_or_frozenset(args[0]);
|
|
|
|
self = set_copy_as_mutable(args[0]);
|
2014-01-12 17:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < n_args; i++) {
|
|
|
|
mp_obj_t other = args[i];
|
|
|
|
if (self == other) {
|
|
|
|
set_clear(self);
|
|
|
|
} else {
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t iter = mp_getiter(other);
|
2014-01-12 17:07:17 +00:00
|
|
|
mp_obj_t next;
|
2014-04-17 23:19:36 +01:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 17:07:17 +00:00
|
|
|
set_discard(self, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 14:02:17 +01:00
|
|
|
self->base.type = ((mp_obj_set_t*)args[0])->base.type;
|
2014-01-12 17:07:17 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_diff(uint n_args, const mp_obj_t *args) {
|
2014-01-12 17:07:17 +00:00
|
|
|
return set_diff_int(n_args, args, false);
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
|
2014-01-12 17:07:17 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_diff_update(uint n_args, const mp_obj_t *args) {
|
2014-01-12 17:07:17 +00:00
|
|
|
set_diff_int(n_args, args, true);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
|
2014-01-12 17:07:17 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
|
2014-05-10 14:02:17 +01:00
|
|
|
if (update) {
|
|
|
|
check_set(self_in);
|
|
|
|
} else {
|
|
|
|
check_set_or_frozenset(self_in);
|
|
|
|
}
|
|
|
|
|
2014-01-12 17:54:03 +00:00
|
|
|
if (self_in == other) {
|
|
|
|
return update ? mp_const_none : set_copy(self_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
mp_obj_set_t *out = mp_obj_new_set(0, NULL);
|
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t iter = mp_getiter(other);
|
2014-01-12 17:54:03 +00:00
|
|
|
mp_obj_t next;
|
2014-04-17 23:19:36 +01:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 17:54:03 +00:00
|
|
|
if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
|
|
|
|
set_add(out, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return update ? mp_const_none : out;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
|
2014-01-12 17:54:03 +00:00
|
|
|
return set_intersect_int(self_in, other, false);
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
|
2014-01-12 17:54:03 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
|
2014-01-12 17:54:03 +00:00
|
|
|
return set_intersect_int(self_in, other, true);
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
|
2014-01-12 17:54:03 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set_or_frozenset(self_in);
|
2014-01-12 18:03:21 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t iter = mp_getiter(other);
|
2014-01-12 18:03:21 +00:00
|
|
|
mp_obj_t next;
|
2014-04-17 23:19:36 +01:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 18:03:21 +00:00
|
|
|
if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
|
2014-01-12 18:03:21 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) {
|
2014-01-12 18:23:36 +00:00
|
|
|
mp_obj_set_t *self;
|
|
|
|
bool cleanup_self = false;
|
2014-05-10 14:02:17 +01:00
|
|
|
if (is_set_or_frozenset(self_in)) {
|
2014-01-12 18:23:36 +00:00
|
|
|
self = self_in;
|
|
|
|
} else {
|
2014-03-29 13:43:38 +00:00
|
|
|
self = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &self_in);
|
2014-01-12 18:23:36 +00:00
|
|
|
cleanup_self = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_set_t *other;
|
|
|
|
bool cleanup_other = false;
|
2014-05-10 14:02:17 +01:00
|
|
|
if (is_set_or_frozenset(other_in)) {
|
2014-01-12 18:23:36 +00:00
|
|
|
other = other_in;
|
|
|
|
} else {
|
2014-03-29 13:43:38 +00:00
|
|
|
other = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &other_in);
|
2014-01-12 18:23:36 +00:00
|
|
|
cleanup_other = true;
|
|
|
|
}
|
2014-01-12 23:09:10 +00:00
|
|
|
bool out = true;
|
|
|
|
if (proper && self->set.used == other->set.used) {
|
|
|
|
out = false;
|
|
|
|
} else {
|
|
|
|
mp_obj_t iter = set_getiter(self);
|
|
|
|
mp_obj_t next;
|
2014-04-17 23:19:36 +01:00
|
|
|
while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 23:09:10 +00:00
|
|
|
if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
|
|
|
|
out = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-12 18:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-10 14:02:17 +01:00
|
|
|
// TODO: Should free objects altogether
|
2014-01-12 18:23:36 +00:00
|
|
|
if (cleanup_self) {
|
|
|
|
set_clear(self);
|
|
|
|
}
|
|
|
|
if (cleanup_other) {
|
|
|
|
set_clear(other);
|
|
|
|
}
|
2014-01-12 23:09:10 +00:00
|
|
|
return MP_BOOL(out);
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 23:09:10 +00:00
|
|
|
return set_issubset_internal(self_in, other_in, false);
|
2014-01-12 18:23:36 +00:00
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
|
2014-01-12 18:23:36 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 23:09:10 +00:00
|
|
|
return set_issubset_internal(self_in, other_in, true);
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 23:09:10 +00:00
|
|
|
return set_issubset_internal(other_in, self_in, false);
|
2014-01-12 18:23:36 +00:00
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
|
2014-01-12 18:23:36 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-01-12 23:09:10 +00:00
|
|
|
return set_issubset_internal(other_in, self_in, true);
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set_or_frozenset(self_in);
|
2014-01-12 23:09:10 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
2014-05-10 14:02:17 +01:00
|
|
|
if (!is_set_or_frozenset(other_in)) {
|
2014-01-12 23:09:10 +00:00
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
mp_obj_set_t *other = other_in;
|
|
|
|
if (self->set.used != other->set.used) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
return set_issubset(self_in, other_in);
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_pop(mp_obj_t self_in) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set(self_in);
|
2014-01-12 18:23:36 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
2014-04-05 17:17:19 +01:00
|
|
|
mp_obj_t obj = mp_set_remove_first(&self->set);
|
|
|
|
if (obj == MP_OBJ_NULL) {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "pop from an empty set"));
|
2014-01-12 18:23:36 +00:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
|
2014-01-12 18:23:36 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set(self_in);
|
2014-01-12 18:23:36 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
|
2014-01-12 18:23:36 +00:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
|
2014-01-12 17:07:17 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set(self_in);
|
2014-01-12 19:39:48 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t iter = mp_getiter(other_in);
|
2014-01-12 19:39:48 +00:00
|
|
|
mp_obj_t next;
|
2014-04-17 23:19:36 +01:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 19:39:48 +00:00
|
|
|
mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
|
2014-01-12 19:39:48 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set_or_frozenset(self_in);
|
|
|
|
mp_obj_set_t *self_out = set_copy_as_mutable(self_in);
|
|
|
|
set_symmetric_difference_update(self_out, other_in);
|
|
|
|
self_out->base.type = ((mp_obj_set_t*)self_in)->base.type;
|
|
|
|
return self_out;
|
2014-01-12 19:39:48 +00:00
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
|
2014-01-12 19:39:48 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t iter = mp_getiter(other_in);
|
2014-01-12 19:39:48 +00:00
|
|
|
mp_obj_t next;
|
2014-04-17 23:19:36 +01:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-12 19:39:48 +00:00
|
|
|
mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_update(uint n_args, const mp_obj_t *args) {
|
2014-01-12 19:39:48 +00:00
|
|
|
assert(n_args > 0);
|
|
|
|
|
|
|
|
for (int i = 1; i < n_args; i++) {
|
|
|
|
set_update_int(args[0], args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
|
2014-01-12 19:39:48 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
|
2014-05-10 14:02:17 +01:00
|
|
|
check_set_or_frozenset(self_in);
|
2014-01-12 19:39:48 +00:00
|
|
|
mp_obj_set_t *self = set_copy(self_in);
|
|
|
|
set_update_int(self, other_in);
|
|
|
|
return self;
|
|
|
|
}
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
|
2014-01-12 19:39:48 +00:00
|
|
|
|
2014-04-05 17:17:19 +01:00
|
|
|
STATIC mp_obj_t set_unary_op(int op, mp_obj_t self_in) {
|
|
|
|
mp_obj_set_t *self = self_in;
|
|
|
|
switch (op) {
|
|
|
|
case MP_UNARY_OP_BOOL: return MP_BOOL(self->set.used != 0);
|
|
|
|
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->set.used);
|
2014-04-17 23:19:36 +01:00
|
|
|
default: return MP_OBJ_NOT_SUPPORTED;
|
2014-04-05 17:17:19 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-12 19:39:48 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
2014-01-12 23:09:10 +00:00
|
|
|
mp_obj_t args[] = {lhs, rhs};
|
|
|
|
switch (op) {
|
2014-05-10 13:55:11 +01: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:
|
|
|
|
return set_union(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_INPLACE_XOR:
|
|
|
|
return set_symmetric_difference(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_INPLACE_AND:
|
|
|
|
return set_intersect(lhs, rhs);
|
|
|
|
case MP_BINARY_OP_INPLACE_SUBTRACT:
|
|
|
|
return set_diff(2, args);
|
|
|
|
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);
|
|
|
|
case MP_BINARY_OP_IN: {
|
|
|
|
mp_obj_set_t *o = lhs;
|
|
|
|
mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
|
|
|
|
return MP_BOOL(elem != NULL);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NOT_SUPPORTED;
|
2014-01-12 23:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-12 19:39:48 +00:00
|
|
|
|
2014-01-12 15:29:11 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* set constructors & public C API */
|
|
|
|
|
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC const mp_map_elem_t set_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_add), (mp_obj_t)&set_add_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&set_clear_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&set_copy_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_discard), (mp_obj_t)&set_discard_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_difference), (mp_obj_t)&set_diff_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_difference_update), (mp_obj_t)&set_diff_update_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_intersection), (mp_obj_t)&set_intersect_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_intersection_update), (mp_obj_t)&set_intersect_update_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_isdisjoint), (mp_obj_t)&set_isdisjoint_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_issubset), (mp_obj_t)&set_issubset_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_issuperset), (mp_obj_t)&set_issuperset_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&set_pop_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&set_remove_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference), (mp_obj_t)&set_symmetric_difference_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference_update), (mp_obj_t)&set_symmetric_difference_update_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_union), (mp_obj_t)&set_union_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&set_update_obj },
|
2014-04-13 09:53:15 +01:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___contains__), (mp_obj_t)&mp_op_contains_obj },
|
2014-01-12 15:29:11 +00:00
|
|
|
};
|
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
|
|
|
|
|
2014-03-29 13:43:38 +00:00
|
|
|
const mp_obj_type_t mp_type_set = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_set,
|
2014-01-07 15:58:30 +00:00
|
|
|
.print = set_print,
|
|
|
|
.make_new = set_make_new,
|
2014-04-05 17:17:19 +01:00
|
|
|
.unary_op = set_unary_op,
|
2014-01-12 23:09:10 +00:00
|
|
|
.binary_op = set_binary_op,
|
2014-01-12 15:17:42 +00:00
|
|
|
.getiter = set_getiter,
|
2014-03-26 21:47:19 +00:00
|
|
|
.locals_dict = (mp_obj_t)&set_locals_dict,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2014-05-10 14:02:17 +01:00
|
|
|
#if MICROPY_ENABLE_FROZENSET
|
|
|
|
const mp_obj_type_t mp_type_frozenset = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_frozenset,
|
|
|
|
.print = set_print,
|
|
|
|
.make_new = set_make_new,
|
|
|
|
.unary_op = set_unary_op,
|
|
|
|
.binary_op = set_binary_op,
|
|
|
|
.getiter = set_getiter,
|
|
|
|
.locals_dict = (mp_obj_t)&set_locals_dict,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
|
|
|
|
mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
|
2014-03-29 13:43:38 +00:00
|
|
|
o->base.type = &mp_type_set;
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_set_init(&o->set, n_args);
|
|
|
|
for (int i = 0; i < n_args; i++) {
|
2014-01-12 16:39:39 +00:00
|
|
|
mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
|
2014-03-29 13:43:38 +00:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_set_t *self = self_in;
|
2014-01-12 16:39:39 +00:00
|
|
|
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|