py/objdict: Reuse dict-view key iterator for standard dict iterator.

It has equivalent behaviour and reusing it saves some code bytes.
This commit is contained in:
Damien George 2017-11-27 23:40:31 +11:00
parent c3bc8d7b2b
commit 2161d6b603

View File

@ -192,37 +192,6 @@ STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
} }
} }
/******************************************************************************/
/* dict iterator */
typedef struct _mp_obj_dict_it_t {
mp_obj_base_t base;
mp_fun_1_t iternext;
mp_obj_t dict;
size_t cur;
} mp_obj_dict_it_t;
STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
mp_obj_dict_it_t *self = MP_OBJ_TO_PTR(self_in);
mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);
if (next == NULL) {
return MP_OBJ_STOP_ITERATION;
} else {
return next->key;
}
}
STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
assert(sizeof(mp_obj_dict_it_t) <= sizeof(mp_obj_iter_buf_t));
mp_obj_dict_it_t *o = (mp_obj_dict_it_t*)iter_buf;
o->base.type = &mp_type_polymorph_iter;
o->iternext = dict_it_iternext;
o->dict = self_in;
o->cur = 0;
return MP_OBJ_FROM_PTR(o);
}
/******************************************************************************/ /******************************************************************************/
/* dict methods */ /* dict methods */
@ -527,6 +496,20 @@ STATIC mp_obj_t dict_values(mp_obj_t self_in) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values); STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
/******************************************************************************/
/* dict iterator */
STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t));
mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t*)iter_buf;
o->base.type = &dict_view_it_type;
o->kind = MP_DICT_VIEW_KEYS;
o->dict = self_in;
o->cur = 0;
return MP_OBJ_FROM_PTR(o);
}
/******************************************************************************/ /******************************************************************************/
/* dict constructors & public C API */ /* dict constructors & public C API */