Implemented set.discard

This commit is contained in:
John R. Lenton 2014-01-12 16:39:39 +00:00
parent 3b0bd87906
commit 2a24172cdc
4 changed files with 23 additions and 7 deletions

View File

@ -146,10 +146,10 @@ static void mp_set_rehash(mp_set_t *set) {
m_del(mp_obj_t, old_table, old_alloc);
}
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
int hash = mp_obj_hash(index);
if (set->alloc == 0) {
if (add_if_not_found) {
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
mp_set_rehash(set);
} else {
return NULL;
@ -160,7 +160,7 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
mp_obj_t elem = set->table[pos];
if (elem == MP_OBJ_NULL) {
// not in table
if (add_if_not_found) {
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
if (set->used + 1 >= set->alloc) {
// not enough room in table, rehash it
mp_set_rehash(set);
@ -176,6 +176,11 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
}
} else if (mp_obj_equal(elem, index)) {
// found it
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
set->used--;
set->table[pos] = NULL;
return elem;
}
return elem;
} else {
// not yet found, keep searching in this table

View File

@ -31,5 +31,5 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
void mp_map_clear(mp_map_t *map);
void mp_set_init(mp_set_t *set, int n);
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found);
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
void mp_set_clear(mp_set_t *set);

View File

@ -100,7 +100,7 @@ static mp_obj_t set_getiter(mp_obj_t set_in) {
static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
mp_obj_set_t *self = self_in;
mp_set_lookup(&self->set, item, true);
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
@ -129,6 +129,13 @@ static mp_obj_t set_copy(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
mp_obj_set_t *self = self_in;
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
/******************************************************************************/
/* set constructors & public C API */
@ -138,6 +145,7 @@ static const mp_method_t set_type_methods[] = {
{ "add", &set_add_obj },
{ "clear", &set_clear_obj },
{ "copy", &set_copy_obj },
{ "discard", &set_discard_obj },
{ NULL, NULL }, // end-of-list sentinel
};
@ -155,7 +163,7 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
o->base.type = &set_type;
mp_set_init(&o->set, n_args);
for (int i = 0; i < n_args; i++) {
mp_set_lookup(&o->set, items[i], true);
mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
}
return o;
}
@ -163,5 +171,5 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
mp_obj_set_t *self = self_in;
mp_set_lookup(&self->set, item, true);
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
}

View File

@ -0,0 +1,3 @@
s = {1, 2}
print(s.discard(1))
print(list(s))