kopia lustrzana https://github.com/micropython/micropython
oops, nasty off-by-one in set_copy
rodzic
be790f94d5
commit
7244a14439
|
@ -27,6 +27,10 @@ static mp_obj_t set_it_iternext(mp_obj_t self_in);
|
||||||
|
|
||||||
void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
||||||
mp_obj_set_t *self = self_in;
|
mp_obj_set_t *self = self_in;
|
||||||
|
if (self->set.used == 0) {
|
||||||
|
print(env, "set()");
|
||||||
|
return;
|
||||||
|
}
|
||||||
bool first = true;
|
bool first = true;
|
||||||
print(env, "{");
|
print(env, "{");
|
||||||
for (int i = 0; i < self->set.alloc; i++) {
|
for (int i = 0; i < self->set.alloc; i++) {
|
||||||
|
@ -122,7 +126,7 @@ static mp_obj_t set_copy(mp_obj_t self_in) {
|
||||||
|
|
||||||
mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
|
mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
|
||||||
other->base.type = &set_type;
|
other->base.type = &set_type;
|
||||||
mp_set_init(&other->set, self->set.alloc);
|
mp_set_init(&other->set, self->set.alloc - 1);
|
||||||
other->set.used = self->set.used;
|
other->set.used = self->set.used;
|
||||||
memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
|
memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,30 @@
|
||||||
def r(s):
|
def r(s):
|
||||||
l = list(s)
|
l = list(s)
|
||||||
l.sort()
|
l.sort()
|
||||||
print(l)
|
return l
|
||||||
s = {1, 2}
|
sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}]
|
||||||
t = {2, 3}
|
for s in sets:
|
||||||
r(s | t)
|
for t in sets:
|
||||||
r(s ^ t)
|
print(s, '|', t, '=', r(s | t))
|
||||||
r(s & t)
|
print(s, '^', t, '=', r(s ^ t))
|
||||||
r(s - t)
|
print(s, '&', t, '=', r(s & t))
|
||||||
u = s.copy()
|
print(s, '-', t, '=', r(s - t))
|
||||||
u |= t
|
u = s.copy()
|
||||||
r(u)
|
u |= t
|
||||||
u = s.copy()
|
print(s, "|=", t, '-->', r(u))
|
||||||
u ^= t
|
u = s.copy()
|
||||||
r(u)
|
u ^= t
|
||||||
u = s.copy()
|
print(s, "^=", t, '-->', r(u))
|
||||||
u &= t
|
u = s.copy()
|
||||||
r(u)
|
u &= t
|
||||||
u = s.copy()
|
print(s, "&=", t, "-->", r(u))
|
||||||
u -= t
|
u = s.copy()
|
||||||
r(u)
|
u -= t
|
||||||
|
print(s, "-=", t, "-->", r(u))
|
||||||
|
|
||||||
print(s == t)
|
print(s, '==', t, '=', s == t)
|
||||||
print(s != t)
|
print(s, '!=', t, '=', s != t)
|
||||||
print(s > t)
|
print(s, '>', t, '=', s > t)
|
||||||
print(s >= t)
|
print(s, '>=', t, '=', s >= t)
|
||||||
print(s < t)
|
print(s, '<', t, '=', s < t)
|
||||||
print(s <= t)
|
print(s, '<=', t, '=', s <= t)
|
||||||
|
|
Ładowanie…
Reference in New Issue