py/objdict: Convert mp_uint_t to size_t where appropriate.
This commit is contained in:
parent
58d9eeb8d9
commit
1ea2f7a8ce
6
py/obj.h
6
py/obj.h
@ -632,7 +632,7 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
|
|||||||
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_uint_t n_closed, const mp_obj_t *closed);
|
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_uint_t n_closed, const mp_obj_t *closed);
|
||||||
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
|
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
|
||||||
mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
|
mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
|
||||||
mp_obj_t mp_obj_new_dict(mp_uint_t n_args);
|
mp_obj_t mp_obj_new_dict(size_t n_args);
|
||||||
mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items);
|
mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items);
|
||||||
mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
|
mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
|
||||||
mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
|
mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
|
||||||
@ -733,8 +733,8 @@ typedef struct _mp_obj_dict_t {
|
|||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
mp_map_t map;
|
mp_map_t map;
|
||||||
} mp_obj_dict_t;
|
} mp_obj_dict_t;
|
||||||
void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args);
|
void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
|
||||||
mp_uint_t mp_obj_dict_len(mp_obj_t self_in);
|
size_t mp_obj_dict_len(mp_obj_t self_in);
|
||||||
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
|
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
|
||||||
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
|
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
|
||||||
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
|
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
|
||||||
|
28
py/objdict.c
28
py/objdict.c
@ -41,11 +41,11 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg
|
|||||||
// This is a helper function to iterate through a dictionary. The state of
|
// This is a helper function to iterate through a dictionary. The state of
|
||||||
// the iteration is held in *cur and should be initialised with zero for the
|
// the iteration is held in *cur and should be initialised with zero for the
|
||||||
// first call. Will return NULL when no more elements are available.
|
// first call. Will return NULL when no more elements are available.
|
||||||
STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, mp_uint_t *cur) {
|
STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
|
||||||
mp_uint_t max = dict->map.alloc;
|
size_t max = dict->map.alloc;
|
||||||
mp_map_t *map = &dict->map;
|
mp_map_t *map = &dict->map;
|
||||||
|
|
||||||
for (mp_uint_t i = *cur; i < max; i++) {
|
for (size_t i = *cur; i < max; i++) {
|
||||||
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
|
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
|
||||||
*cur = i + 1;
|
*cur = i + 1;
|
||||||
return &(map->table[i]);
|
return &(map->table[i]);
|
||||||
@ -65,7 +65,7 @@ STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_
|
|||||||
mp_printf(print, "%q(", self->base.type->name);
|
mp_printf(print, "%q(", self->base.type->name);
|
||||||
}
|
}
|
||||||
mp_print_str(print, "{");
|
mp_print_str(print, "{");
|
||||||
mp_uint_t cur = 0;
|
size_t cur = 0;
|
||||||
mp_map_elem_t *next = NULL;
|
mp_map_elem_t *next = NULL;
|
||||||
while ((next = dict_iter_next(self, &cur)) != NULL) {
|
while ((next = dict_iter_next(self, &cur)) != NULL) {
|
||||||
if (!first) {
|
if (!first) {
|
||||||
@ -121,7 +121,7 @@ STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|||||||
if (MP_UNLIKELY(MP_OBJ_IS_TYPE(lhs_in, &mp_type_ordereddict) && MP_OBJ_IS_TYPE(rhs_in, &mp_type_ordereddict))) {
|
if (MP_UNLIKELY(MP_OBJ_IS_TYPE(lhs_in, &mp_type_ordereddict) && MP_OBJ_IS_TYPE(rhs_in, &mp_type_ordereddict))) {
|
||||||
// Iterate through both dictionaries simultaneously and compare keys and values.
|
// Iterate through both dictionaries simultaneously and compare keys and values.
|
||||||
mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
|
mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
|
||||||
mp_uint_t c1 = 0, c2 = 0;
|
size_t c1 = 0, c2 = 0;
|
||||||
mp_map_elem_t *e1 = dict_iter_next(o, &c1), *e2 = dict_iter_next(rhs, &c2);
|
mp_map_elem_t *e1 = dict_iter_next(o, &c1), *e2 = dict_iter_next(rhs, &c2);
|
||||||
for (; e1 != NULL && e2 != NULL; e1 = dict_iter_next(o, &c1), e2 = dict_iter_next(rhs, &c2)) {
|
for (; e1 != NULL && e2 != NULL; e1 = dict_iter_next(o, &c1), e2 = dict_iter_next(rhs, &c2)) {
|
||||||
if (!mp_obj_equal(e1->key, e2->key) || !mp_obj_equal(e1->value, e2->value)) {
|
if (!mp_obj_equal(e1->key, e2->key) || !mp_obj_equal(e1->value, e2->value)) {
|
||||||
@ -137,7 +137,7 @@ STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|||||||
return mp_const_false;
|
return mp_const_false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_uint_t cur = 0;
|
size_t cur = 0;
|
||||||
mp_map_elem_t *next = NULL;
|
mp_map_elem_t *next = NULL;
|
||||||
while ((next = dict_iter_next(o, &cur)) != NULL) {
|
while ((next = dict_iter_next(o, &cur)) != NULL) {
|
||||||
mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
|
mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
|
||||||
@ -196,7 +196,7 @@ typedef struct _mp_obj_dict_it_t {
|
|||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
mp_fun_1_t iternext;
|
mp_fun_1_t iternext;
|
||||||
mp_obj_t dict;
|
mp_obj_t dict;
|
||||||
mp_uint_t cur;
|
size_t cur;
|
||||||
} mp_obj_dict_it_t;
|
} mp_obj_dict_it_t;
|
||||||
|
|
||||||
STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
|
STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
|
||||||
@ -340,7 +340,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setde
|
|||||||
STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
|
STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
|
||||||
mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
|
mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
|
||||||
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
mp_uint_t cur = 0;
|
size_t cur = 0;
|
||||||
mp_map_elem_t *next = dict_iter_next(self, &cur);
|
mp_map_elem_t *next = dict_iter_next(self, &cur);
|
||||||
if (next == NULL) {
|
if (next == NULL) {
|
||||||
mp_raise_msg(&mp_type_KeyError, "popitem(): dictionary is empty");
|
mp_raise_msg(&mp_type_KeyError, "popitem(): dictionary is empty");
|
||||||
@ -367,7 +367,7 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg
|
|||||||
if (MP_OBJ_IS_DICT_TYPE(args[1])) {
|
if (MP_OBJ_IS_DICT_TYPE(args[1])) {
|
||||||
// update from other dictionary (make sure other is not self)
|
// update from other dictionary (make sure other is not self)
|
||||||
if (args[1] != args[0]) {
|
if (args[1] != args[0]) {
|
||||||
mp_uint_t cur = 0;
|
size_t cur = 0;
|
||||||
mp_map_elem_t *elem = NULL;
|
mp_map_elem_t *elem = NULL;
|
||||||
while ((elem = dict_iter_next((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
|
while ((elem = dict_iter_next((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
|
||||||
mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
|
mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
|
||||||
@ -394,7 +394,7 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update the dict with any keyword args
|
// update the dict with any keyword args
|
||||||
for (mp_uint_t i = 0; i < kwargs->alloc; i++) {
|
for (size_t i = 0; i < kwargs->alloc; i++) {
|
||||||
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
|
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
|
||||||
mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
|
mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
|
||||||
}
|
}
|
||||||
@ -423,7 +423,7 @@ typedef struct _mp_obj_dict_view_it_t {
|
|||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
mp_dict_view_kind_t kind;
|
mp_dict_view_kind_t kind;
|
||||||
mp_obj_t dict;
|
mp_obj_t dict;
|
||||||
mp_uint_t cur;
|
size_t cur;
|
||||||
} mp_obj_dict_view_it_t;
|
} mp_obj_dict_view_it_t;
|
||||||
|
|
||||||
typedef struct _mp_obj_dict_view_t {
|
typedef struct _mp_obj_dict_view_t {
|
||||||
@ -590,18 +590,18 @@ const mp_obj_type_t mp_type_ordereddict = {
|
|||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args) {
|
void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args) {
|
||||||
dict->base.type = &mp_type_dict;
|
dict->base.type = &mp_type_dict;
|
||||||
mp_map_init(&dict->map, n_args);
|
mp_map_init(&dict->map, n_args);
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_obj_t mp_obj_new_dict(mp_uint_t n_args) {
|
mp_obj_t mp_obj_new_dict(size_t n_args) {
|
||||||
mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
|
mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
|
||||||
mp_obj_dict_init(o, n_args);
|
mp_obj_dict_init(o, n_args);
|
||||||
return MP_OBJ_FROM_PTR(o);
|
return MP_OBJ_FROM_PTR(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_uint_t mp_obj_dict_len(mp_obj_t self_in) {
|
size_t mp_obj_dict_len(mp_obj_t self_in) {
|
||||||
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
return self->map.used;
|
return self->map.used;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user