Commit 93965e72 by Damien George

py: Make map, dict, set use mp_int_t/mp_uint_t exclusively.

`Part of code cleanup, towards resolving issue #50.`
parent 1c70cbf1
 ... ... @@ -24,6 +24,7 @@ * THE SOFTWARE. */ #include #include #include ... ... @@ -35,10 +36,10 @@ // approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}] // prefixed with zero for the empty case. STATIC int doubling_primes[] = {0, 7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607}; STATIC uint32_t doubling_primes[] = {0, 7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607}; STATIC int get_doubling_prime_greater_or_equal_to(int x) { for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) { STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) { for (int i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) { if (doubling_primes[i] >= x) { return doubling_primes[i]; } ... ... @@ -51,7 +52,7 @@ STATIC int get_doubling_prime_greater_or_equal_to(int x) { /******************************************************************************/ /* map */ void mp_map_init(mp_map_t *map, int n) { void mp_map_init(mp_map_t *map, mp_uint_t n) { if (n == 0) { map->alloc = 0; map->table = NULL; ... ... @@ -64,7 +65,7 @@ void mp_map_init(mp_map_t *map, int n) { map->table_is_fixed_array = 0; } void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) { void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table) { map->alloc = n; map->used = n; map->all_keys_are_qstrs = 1; ... ... @@ -72,7 +73,7 @@ void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) { map->table = (mp_map_elem_t*)table; } mp_map_t *mp_map_new(int n) { mp_map_t *mp_map_new(mp_uint_t n) { mp_map_t *map = m_new(mp_map_t, 1); mp_map_init(map, n); return map; ... ... @@ -103,13 +104,13 @@ void mp_map_clear(mp_map_t *map) { } STATIC void mp_map_rehash(mp_map_t *map) { int old_alloc = map->alloc; mp_uint_t old_alloc = map->alloc; mp_map_elem_t *old_table = map->table; map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1); map->used = 0; map->all_keys_are_qstrs = 1; map->table = m_new0(mp_map_elem_t, map->alloc); for (int i = 0; i < old_alloc; i++) { for (mp_uint_t i = 0; i < old_alloc; i++) { if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) { mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value; } ... ... @@ -168,8 +169,8 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t } mp_uint_t hash = mp_obj_hash(index); uint pos = hash % map->alloc; uint start_pos = pos; mp_uint_t pos = hash % map->alloc; mp_uint_t start_pos = pos; mp_map_elem_t *avail_slot = NULL; for (;;) { mp_map_elem_t *slot = &map->table[pos]; ... ... @@ -242,19 +243,19 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t /******************************************************************************/ /* set */ void mp_set_init(mp_set_t *set, int n) { void mp_set_init(mp_set_t *set, mp_uint_t n) { set->alloc = n; set->used = 0; set->table = m_new0(mp_obj_t, set->alloc); } STATIC void mp_set_rehash(mp_set_t *set) { int old_alloc = set->alloc; mp_uint_t old_alloc = set->alloc; mp_obj_t *old_table = set->table; set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1); set->used = 0; set->table = m_new0(mp_obj_t, set->alloc); for (int i = 0; i < old_alloc; i++) { for (mp_uint_t i = 0; i < old_alloc; i++) { if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) { mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); } ... ... @@ -271,8 +272,8 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku } } mp_uint_t hash = mp_obj_hash(index); uint pos = hash % set->alloc; uint start_pos = pos; mp_uint_t pos = hash % set->alloc; mp_uint_t start_pos = pos; mp_obj_t *avail_slot = NULL; for (;;) { mp_obj_t elem = set->table[pos]; ... ... @@ -333,7 +334,7 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku } mp_obj_t mp_set_remove_first(mp_set_t *set) { for (uint pos = 0; pos < set->alloc; pos++) { for (mp_uint_t pos = 0; pos < set->alloc; pos++) { if (MP_SET_SLOT_IS_FILLED(set, pos)) { mp_obj_t elem = set->table[pos]; // delete element ... ... @@ -359,7 +360,7 @@ void mp_set_clear(mp_set_t *set) { #if DEBUG_PRINT void mp_map_dump(mp_map_t *map) { for (int i = 0; i < map->alloc; i++) { for (mp_uint_t i = 0; i < map->alloc; i++) { if (map->table[i].key != NULL) { mp_obj_print(map->table[i].key, PRINT_REPR); } else { ... ...
 ... ... @@ -150,9 +150,9 @@ typedef enum _mp_map_lookup_kind_t { static inline bool MP_MAP_SLOT_IS_FILLED(const mp_map_t *map, mp_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); } void mp_map_init(mp_map_t *map, int n); void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table); mp_map_t *mp_map_new(int n); void mp_map_init(mp_map_t *map, mp_uint_t n); void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table); mp_map_t *mp_map_new(mp_uint_t n); void mp_map_deinit(mp_map_t *map); void mp_map_free(mp_map_t *map); mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind); ... ... @@ -169,7 +169,7 @@ typedef struct _mp_set_t { static inline bool MP_SET_SLOT_IS_FILLED(const mp_set_t *set, mp_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); } void mp_set_init(mp_set_t *set, int n); void mp_set_init(mp_set_t *set, mp_uint_t n); mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind); mp_obj_t mp_set_remove_first(mp_set_t *set); void mp_set_clear(mp_set_t *set); ... ... @@ -386,8 +386,8 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun); mp_obj_t mp_obj_new_closure(mp_obj_t fun, uint n_closed, const mp_obj_t *closed); mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items); mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_dict(int n_args); mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items); mp_obj_t mp_obj_new_dict(mp_uint_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_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_bound_meth(mp_obj_t meth, mp_obj_t self); ... ... @@ -508,8 +508,8 @@ typedef struct _mp_obj_dict_t { mp_obj_base_t base; mp_map_t map; } mp_obj_dict_t; void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args); uint mp_obj_dict_len(mp_obj_t self_in); void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args); mp_uint_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_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); ... ...
 ... ... @@ -38,9 +38,9 @@ #include "runtime.h" #include "builtin.h" STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur); STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, mp_uint_t cur); STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in); STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs); STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { mp_obj_dict_t *self = self_in; ... ... @@ -163,7 +163,7 @@ STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) { mp_uint_t max = self->dict->map.alloc; mp_map_t *map = &self->dict->map; for (int i = self->cur; i < max; i++) { for (mp_uint_t i = self->cur; i < max; i++) { if (MP_MAP_SLOT_IS_FILLED(map, i)) { self->cur = i + 1; return &(map->table[i]); ... ... @@ -190,7 +190,7 @@ STATIC const mp_obj_type_t mp_type_dict_it = { .iternext = dict_it_iternext, }; STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) { STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, mp_uint_t cur) { mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t); o->base.type = &mp_type_dict_it; o->dict = dict; ... ... @@ -228,7 +228,7 @@ STATIC mp_obj_t dict_copy(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); // this is a classmethod STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) { STATIC mp_obj_t dict_fromkeys(mp_uint_t n_args, const mp_obj_t *args) { assert(2 <= n_args && n_args <= 3); mp_obj_t iter = mp_getiter(args[1]); mp_obj_t len = mp_obj_len_maybe(iter); ... ... @@ -278,7 +278,7 @@ STATIC mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, mp return value; } STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) { STATIC mp_obj_t dict_get(mp_uint_t n_args, const mp_obj_t *args) { assert(2 <= n_args && n_args <= 3); assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict)); ... ... @@ -289,7 +289,7 @@ STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get); STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) { STATIC mp_obj_t dict_pop(mp_uint_t n_args, const mp_obj_t *args) { assert(2 <= n_args && n_args <= 3); assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict)); ... ... @@ -301,7 +301,7 @@ STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop); STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) { STATIC mp_obj_t dict_setdefault(mp_uint_t n_args, const mp_obj_t *args) { assert(2 <= n_args && n_args <= 3); assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict)); ... ... @@ -332,7 +332,7 @@ STATIC mp_obj_t dict_popitem(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem); STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) { STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict)); mp_obj_dict_t *self = args[0]; ... ... @@ -555,18 +555,18 @@ const mp_obj_type_t mp_type_dict = { .locals_dict = (mp_obj_t)&dict_locals_dict, }; void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) { void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args) { dict->base.type = &mp_type_dict; mp_map_init(&dict->map, n_args); } mp_obj_t mp_obj_new_dict(int n_args) { mp_obj_t mp_obj_new_dict(mp_uint_t n_args) { mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t); mp_obj_dict_init(o, n_args); return o; } uint mp_obj_dict_len(mp_obj_t self_in) { mp_uint_t mp_obj_dict_len(mp_obj_t self_in) { return ((mp_obj_dict_t *)self_in)->map.used; } ... ...
 ... ... @@ -104,7 +104,7 @@ STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env, } #endif print(env, "{"); for (int i = 0; i < self->set.alloc; i++) { for (mp_uint_t i = 0; i < self->set.alloc; i++) { if (MP_SET_SLOT_IS_FILLED(&self->set, i)) { if (!first) { print(env, ", "); ... ... @@ -247,7 +247,7 @@ STATIC mp_obj_t set_diff_int(mp_uint_t n_args, const mp_obj_t *args, bool update } for (int i = 1; i < n_args; i++) { for (mp_uint_t i = 1; i < n_args; i++) { mp_obj_t other = args[i]; if (self == other) { set_clear(self); ... ... @@ -456,7 +456,7 @@ STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) { STATIC mp_obj_t set_update(mp_uint_t n_args, const mp_obj_t *args) { assert(n_args > 0); for (int i = 1; i < n_args; i++) { for (mp_uint_t i = 1; i < n_args; i++) { set_update_int(args[0], args[i]); } ... ... @@ -571,11 +571,11 @@ const mp_obj_type_t mp_type_frozenset = { }; #endif mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items) { mp_obj_set_t *o = m_new_obj(mp_obj_set_t); o->base.type = &mp_type_set; mp_set_init(&o->set, n_args); for (int i = 0; i < n_args; i++) { for (mp_uint_t i = 0; i < n_args; i++) { mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); } return o; ... ...
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!