Skip to content

Commit 93965e7

Browse files
committed
py: Make map, dict, set use mp_int_t/mp_uint_t exclusively.
Part of code cleanup, towards resolving issue adafruit#50.
1 parent 1c70cbf commit 93965e7

File tree

4 files changed

+43
-42
lines changed

4 files changed

+43
-42
lines changed

py/map.c

+18-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdint.h>
2728
#include <stdlib.h>
2829
#include <assert.h>
2930

@@ -35,10 +36,10 @@
3536

3637
// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
3738
// prefixed with zero for the empty case.
38-
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};
39+
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};
3940

40-
STATIC int get_doubling_prime_greater_or_equal_to(int x) {
41-
for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
41+
STATIC mp_uint_t get_doubling_prime_greater_or_equal_to(mp_uint_t x) {
42+
for (int i = 0; i < MP_ARRAY_SIZE(doubling_primes); i++) {
4243
if (doubling_primes[i] >= x) {
4344
return doubling_primes[i];
4445
}
@@ -51,7 +52,7 @@ STATIC int get_doubling_prime_greater_or_equal_to(int x) {
5152
/******************************************************************************/
5253
/* map */
5354

54-
void mp_map_init(mp_map_t *map, int n) {
55+
void mp_map_init(mp_map_t *map, mp_uint_t n) {
5556
if (n == 0) {
5657
map->alloc = 0;
5758
map->table = NULL;
@@ -64,15 +65,15 @@ void mp_map_init(mp_map_t *map, int n) {
6465
map->table_is_fixed_array = 0;
6566
}
6667

67-
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
68+
void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table) {
6869
map->alloc = n;
6970
map->used = n;
7071
map->all_keys_are_qstrs = 1;
7172
map->table_is_fixed_array = 1;
7273
map->table = (mp_map_elem_t*)table;
7374
}
7475

75-
mp_map_t *mp_map_new(int n) {
76+
mp_map_t *mp_map_new(mp_uint_t n) {
7677
mp_map_t *map = m_new(mp_map_t, 1);
7778
mp_map_init(map, n);
7879
return map;
@@ -103,13 +104,13 @@ void mp_map_clear(mp_map_t *map) {
103104
}
104105

105106
STATIC void mp_map_rehash(mp_map_t *map) {
106-
int old_alloc = map->alloc;
107+
mp_uint_t old_alloc = map->alloc;
107108
mp_map_elem_t *old_table = map->table;
108109
map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
109110
map->used = 0;
110111
map->all_keys_are_qstrs = 1;
111112
map->table = m_new0(mp_map_elem_t, map->alloc);
112-
for (int i = 0; i < old_alloc; i++) {
113+
for (mp_uint_t i = 0; i < old_alloc; i++) {
113114
if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
114115
mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
115116
}
@@ -168,8 +169,8 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
168169
}
169170

170171
mp_uint_t hash = mp_obj_hash(index);
171-
uint pos = hash % map->alloc;
172-
uint start_pos = pos;
172+
mp_uint_t pos = hash % map->alloc;
173+
mp_uint_t start_pos = pos;
173174
mp_map_elem_t *avail_slot = NULL;
174175
for (;;) {
175176
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
242243
/******************************************************************************/
243244
/* set */
244245

245-
void mp_set_init(mp_set_t *set, int n) {
246+
void mp_set_init(mp_set_t *set, mp_uint_t n) {
246247
set->alloc = n;
247248
set->used = 0;
248249
set->table = m_new0(mp_obj_t, set->alloc);
249250
}
250251

251252
STATIC void mp_set_rehash(mp_set_t *set) {
252-
int old_alloc = set->alloc;
253+
mp_uint_t old_alloc = set->alloc;
253254
mp_obj_t *old_table = set->table;
254255
set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
255256
set->used = 0;
256257
set->table = m_new0(mp_obj_t, set->alloc);
257-
for (int i = 0; i < old_alloc; i++) {
258+
for (mp_uint_t i = 0; i < old_alloc; i++) {
258259
if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
259260
mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
260261
}
@@ -271,8 +272,8 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku
271272
}
272273
}
273274
mp_uint_t hash = mp_obj_hash(index);
274-
uint pos = hash % set->alloc;
275-
uint start_pos = pos;
275+
mp_uint_t pos = hash % set->alloc;
276+
mp_uint_t start_pos = pos;
276277
mp_obj_t *avail_slot = NULL;
277278
for (;;) {
278279
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
333334
}
334335

335336
mp_obj_t mp_set_remove_first(mp_set_t *set) {
336-
for (uint pos = 0; pos < set->alloc; pos++) {
337+
for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
337338
if (MP_SET_SLOT_IS_FILLED(set, pos)) {
338339
mp_obj_t elem = set->table[pos];
339340
// delete element
@@ -359,7 +360,7 @@ void mp_set_clear(mp_set_t *set) {
359360

360361
#if DEBUG_PRINT
361362
void mp_map_dump(mp_map_t *map) {
362-
for (int i = 0; i < map->alloc; i++) {
363+
for (mp_uint_t i = 0; i < map->alloc; i++) {
363364
if (map->table[i].key != NULL) {
364365
mp_obj_print(map->table[i].key, PRINT_REPR);
365366
} else {

py/obj.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ typedef enum _mp_map_lookup_kind_t {
150150

151151
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); }
152152

153-
void mp_map_init(mp_map_t *map, int n);
154-
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table);
155-
mp_map_t *mp_map_new(int n);
153+
void mp_map_init(mp_map_t *map, mp_uint_t n);
154+
void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table);
155+
mp_map_t *mp_map_new(mp_uint_t n);
156156
void mp_map_deinit(mp_map_t *map);
157157
void mp_map_free(mp_map_t *map);
158158
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 {
169169

170170
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); }
171171

172-
void mp_set_init(mp_set_t *set, int n);
172+
void mp_set_init(mp_set_t *set, mp_uint_t n);
173173
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
174174
mp_obj_t mp_set_remove_first(mp_set_t *set);
175175
void mp_set_clear(mp_set_t *set);
@@ -386,8 +386,8 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
386386
mp_obj_t mp_obj_new_closure(mp_obj_t fun, uint n_closed, const mp_obj_t *closed);
387387
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
388388
mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
389-
mp_obj_t mp_obj_new_dict(int n_args);
390-
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items);
389+
mp_obj_t mp_obj_new_dict(mp_uint_t n_args);
390+
mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items);
391391
mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
392392
mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
393393
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 {
508508
mp_obj_base_t base;
509509
mp_map_t map;
510510
} mp_obj_dict_t;
511-
void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args);
512-
uint mp_obj_dict_len(mp_obj_t self_in);
511+
void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args);
512+
mp_uint_t mp_obj_dict_len(mp_obj_t self_in);
513513
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
514514
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
515515
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);

py/objdict.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
#include "runtime.h"
3939
#include "builtin.h"
4040

41-
STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
41+
STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, mp_uint_t cur);
4242
STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
43-
STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs);
43+
STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
4444

4545
STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
4646
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) {
163163
mp_uint_t max = self->dict->map.alloc;
164164
mp_map_t *map = &self->dict->map;
165165

166-
for (int i = self->cur; i < max; i++) {
166+
for (mp_uint_t i = self->cur; i < max; i++) {
167167
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
168168
self->cur = i + 1;
169169
return &(map->table[i]);
@@ -190,7 +190,7 @@ STATIC const mp_obj_type_t mp_type_dict_it = {
190190
.iternext = dict_it_iternext,
191191
};
192192

193-
STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
193+
STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, mp_uint_t cur) {
194194
mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
195195
o->base.type = &mp_type_dict_it;
196196
o->dict = dict;
@@ -228,7 +228,7 @@ STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
228228
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
229229

230230
// this is a classmethod
231-
STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
231+
STATIC mp_obj_t dict_fromkeys(mp_uint_t n_args, const mp_obj_t *args) {
232232
assert(2 <= n_args && n_args <= 3);
233233
mp_obj_t iter = mp_getiter(args[1]);
234234
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
278278
return value;
279279
}
280280

281-
STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
281+
STATIC mp_obj_t dict_get(mp_uint_t n_args, const mp_obj_t *args) {
282282
assert(2 <= n_args && n_args <= 3);
283283
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
284284

@@ -289,7 +289,7 @@ STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
289289
}
290290
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
291291

292-
STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
292+
STATIC mp_obj_t dict_pop(mp_uint_t n_args, const mp_obj_t *args) {
293293
assert(2 <= n_args && n_args <= 3);
294294
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
295295

@@ -301,7 +301,7 @@ STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
301301
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
302302

303303

304-
STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
304+
STATIC mp_obj_t dict_setdefault(mp_uint_t n_args, const mp_obj_t *args) {
305305
assert(2 <= n_args && n_args <= 3);
306306
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
307307

@@ -332,7 +332,7 @@ STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
332332
}
333333
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
334334

335-
STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
335+
STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
336336
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
337337
mp_obj_dict_t *self = args[0];
338338

@@ -555,18 +555,18 @@ const mp_obj_type_t mp_type_dict = {
555555
.locals_dict = (mp_obj_t)&dict_locals_dict,
556556
};
557557

558-
void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
558+
void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args) {
559559
dict->base.type = &mp_type_dict;
560560
mp_map_init(&dict->map, n_args);
561561
}
562562

563-
mp_obj_t mp_obj_new_dict(int n_args) {
563+
mp_obj_t mp_obj_new_dict(mp_uint_t n_args) {
564564
mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
565565
mp_obj_dict_init(o, n_args);
566566
return o;
567567
}
568568

569-
uint mp_obj_dict_len(mp_obj_t self_in) {
569+
mp_uint_t mp_obj_dict_len(mp_obj_t self_in) {
570570
return ((mp_obj_dict_t *)self_in)->map.used;
571571
}
572572

py/objset.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env,
104104
}
105105
#endif
106106
print(env, "{");
107-
for (int i = 0; i < self->set.alloc; i++) {
107+
for (mp_uint_t i = 0; i < self->set.alloc; i++) {
108108
if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
109109
if (!first) {
110110
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
247247
}
248248

249249

250-
for (int i = 1; i < n_args; i++) {
250+
for (mp_uint_t i = 1; i < n_args; i++) {
251251
mp_obj_t other = args[i];
252252
if (self == other) {
253253
set_clear(self);
@@ -456,7 +456,7 @@ STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
456456
STATIC mp_obj_t set_update(mp_uint_t n_args, const mp_obj_t *args) {
457457
assert(n_args > 0);
458458

459-
for (int i = 1; i < n_args; i++) {
459+
for (mp_uint_t i = 1; i < n_args; i++) {
460460
set_update_int(args[0], args[i]);
461461
}
462462

@@ -571,11 +571,11 @@ const mp_obj_type_t mp_type_frozenset = {
571571
};
572572
#endif
573573

574-
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
574+
mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items) {
575575
mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
576576
o->base.type = &mp_type_set;
577577
mp_set_init(&o->set, n_args);
578-
for (int i = 0; i < n_args; i++) {
578+
for (mp_uint_t i = 0; i < n_args; i++) {
579579
mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
580580
}
581581
return o;

0 commit comments

Comments
 (0)