py: add dict length function, and fix rt_store_set.

This commit is contained in:
Damien 2013-12-29 22:32:51 +00:00
parent 732407f1bf
commit dae7eb7226
5 changed files with 24 additions and 19 deletions

View File

@ -209,11 +209,8 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
mp_obj_t *seq_items;
mp_obj_list_get(o_in, &seq_len, &seq_items);
len = seq_len;
/* TODO
} else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
mp_obj_base_t *o = o_in;
len = o->u_map.used;
*/
len = mp_obj_dict_len(o_in);
} else {
nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
}

View File

@ -206,6 +206,7 @@ void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
// dict
extern const mp_obj_type_t dict_type;
uint mp_obj_dict_len(mp_obj_t self_in);
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
// set

View File

@ -53,13 +53,6 @@ mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
}
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
mp_obj_dict_t *self = self_in;
mp_map_lookup_helper(&self->map, key, true)->value = value;
return self_in;
}
const mp_obj_type_t dict_type = {
{ &mp_const_type },
"dict",
@ -78,3 +71,21 @@ mp_obj_t mp_obj_new_dict(int n_args) {
mp_map_init(&o->map, MP_MAP_OBJ, n_args);
return o;
}
uint mp_obj_dict_len(mp_obj_t self_in) {
mp_obj_dict_t *self = self_in;
uint len = 0;
for (int i = 0; i < self->map.alloc; i++) {
if (self->map.table[i].key != NULL) {
len += 1;
}
}
return len;
}
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
mp_obj_dict_t *self = self_in;
mp_map_lookup_helper(&self->map, key, true)->value = value;
return self_in;
}

View File

@ -737,7 +737,7 @@ mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
}
mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
mp_set_lookup(set, item, true);
mp_obj_set_store(set, item);
return set;
}

View File

@ -279,21 +279,17 @@ void mp_show_byte_code(const byte *ip, int len) {
rt_store_map(sp[unum + 1], sp[0], sp[1]);
sp += 2;
break;
*/
case MP_BC_BUILD_SET:
DECODE_UINT;
obj1 = rt_build_set(unum, sp);
sp += unum - 1;
*sp = obj1;
printf("BUILD_SET %lu", unum);
break;
case MP_BC_SET_ADD:
DECODE_UINT;
// I think it's guaranteed by the compiler that sp[unum] is a set
rt_store_set(sp[unum], sp[0]);
sp++;
printf("SET_ADD %lu", unum);
break;
*/
case MP_BC_UNPACK_SEQUENCE:
DECODE_UINT;