objset: frozensets are hashable.

This commit is contained in:
Paul Sokolovsky 2015-08-28 22:31:31 +03:00
parent 936e25b164
commit 8b3b2d04a8
3 changed files with 25 additions and 0 deletions

View File

@ -473,6 +473,22 @@ STATIC mp_obj_t set_unary_op(mp_uint_t op, mp_obj_t 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(self->set.used);
#if MICROPY_PY_BUILTINS_FROZENSET
case MP_UNARY_OP_HASH:
if (MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset)) {
// start hash with unique value
mp_int_t hash = (mp_int_t)&mp_type_frozenset;
mp_uint_t max = self->set.alloc;
mp_set_t *set = &self->set;
for (mp_uint_t i = 0; i < max; i++) {
if (MP_SET_SLOT_IS_FILLED(set, i)) {
hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
}
}
return MP_OBJ_NEW_SMALL_INT(hash);
}
#endif
default: return MP_OBJ_NULL; // op not supported
}
}

View File

@ -15,3 +15,6 @@ print(s)
s = frozenset({3, 4, 3, 1})
print(sorted(s))
# frozensets are hashable unlike sets
print({frozenset("1"): 2})

View File

@ -5,3 +5,9 @@ print(s)
s = {3, 4, 3, 1}
print(sorted(s))
# Sets are not hashable
try:
{s: 1}
except TypeError:
print("TypeError")