Browse Source

Implemented set.discard

pull/159/head
John R. Lenton 11 years ago
parent
commit
2a24172cdc
  1. 11
      py/map.c
  2. 2
      py/map.h
  3. 14
      py/objset.c
  4. 3
      tests/basics/tests/set_discard.py

11
py/map.c

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

2
py/map.h

@ -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_map_clear(mp_map_t *map);
void mp_set_init(mp_set_t *set, int n); 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); void mp_set_clear(mp_set_t *set);

14
py/objset.c

@ -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) { static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
assert(MP_OBJ_IS_TYPE(self_in, &set_type)); assert(MP_OBJ_IS_TYPE(self_in, &set_type));
mp_obj_set_t *self = self_in; 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; return mp_const_none;
} }
static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add); 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_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 */ /* set constructors & public C API */
@ -138,6 +145,7 @@ static const mp_method_t set_type_methods[] = {
{ "add", &set_add_obj }, { "add", &set_add_obj },
{ "clear", &set_clear_obj }, { "clear", &set_clear_obj },
{ "copy", &set_copy_obj }, { "copy", &set_copy_obj },
{ "discard", &set_discard_obj },
{ NULL, NULL }, // end-of-list sentinel { 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; o->base.type = &set_type;
mp_set_init(&o->set, n_args); mp_set_init(&o->set, n_args);
for (int i = 0; i < n_args; i++) { 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; 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) { void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
assert(MP_OBJ_IS_TYPE(self_in, &set_type)); assert(MP_OBJ_IS_TYPE(self_in, &set_type));
mp_obj_set_t *self = self_in; 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);
} }

3
tests/basics/tests/set_discard.py

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