Bring back ordereddict.move_to_end()

This commit is contained in:
Scott Shawcroft 2023-10-05 12:48:45 -07:00
parent 94c7082e9c
commit 49511b0746
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA

View File

@ -441,6 +441,52 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
STATIC mp_obj_t dict_move_to_end(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
mp_obj_dict_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_validate_type(self, &mp_type_ordereddict, MP_QSTR_self);
// parse args
enum { ARG_key, ARG_last };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_key, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE } },
{ MP_QSTR_last, MP_ARG_BOOL, {.u_bool = true } }
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t *key = args[ARG_key].u_obj;
bool last = args[ARG_last].u_bool;
mp_map_elem_t *elem = mp_map_lookup(&self->map, key, MP_MAP_LOOKUP);
if (!elem) {
mp_raise_type_arg(&mp_type_KeyError, key);
}
mp_map_elem_t tmp = *elem;
mp_map_elem_t *table = self->map.table;
mp_map_elem_t *dest, *move_begin, *move_dest;
size_t move_count;
if (last) {
mp_map_elem_t *top = &table[self->map.used];
dest = top - 1;
move_begin = elem + 1;
move_dest = elem;
move_count = top - move_begin;
} else {
dest = &table[0];
move_begin = table;
move_dest = table + 1;
move_count = elem - table;
}
memmove(move_dest, move_begin, move_count * sizeof(*elem));
*dest = tmp;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_move_to_end_obj, 1, dict_move_to_end);
#endif
/******************************************************************************/
/* dict views */
@ -604,6 +650,9 @@ STATIC const mp_rom_map_elem_t dict_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&dict_get_obj) },
{ MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&dict_items_obj) },
{ MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&dict_keys_obj) },
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
{ MP_ROM_QSTR(MP_QSTR_move_to_end), MP_ROM_PTR(&dict_move_to_end_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&dict_pop_obj) },
{ MP_ROM_QSTR(MP_QSTR_popitem), MP_ROM_PTR(&dict_popitem_obj) },
{ MP_ROM_QSTR(MP_QSTR_setdefault), MP_ROM_PTR(&dict_setdefault_obj) },