24
24
* THE SOFTWARE.
25
25
*/
26
26
27
+ #include <stdint.h>
27
28
#include <stdlib.h>
28
29
#include <assert.h>
29
30
35
36
36
37
// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
37
38
// 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 };
39
40
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 ++ ) {
42
43
if (doubling_primes [i ] >= x ) {
43
44
return doubling_primes [i ];
44
45
}
@@ -51,7 +52,7 @@ STATIC int get_doubling_prime_greater_or_equal_to(int x) {
51
52
/******************************************************************************/
52
53
/* map */
53
54
54
- void mp_map_init (mp_map_t * map , int n ) {
55
+ void mp_map_init (mp_map_t * map , mp_uint_t n ) {
55
56
if (n == 0 ) {
56
57
map -> alloc = 0 ;
57
58
map -> table = NULL ;
@@ -64,15 +65,15 @@ void mp_map_init(mp_map_t *map, int n) {
64
65
map -> table_is_fixed_array = 0 ;
65
66
}
66
67
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 ) {
68
69
map -> alloc = n ;
69
70
map -> used = n ;
70
71
map -> all_keys_are_qstrs = 1 ;
71
72
map -> table_is_fixed_array = 1 ;
72
73
map -> table = (mp_map_elem_t * )table ;
73
74
}
74
75
75
- mp_map_t * mp_map_new (int n ) {
76
+ mp_map_t * mp_map_new (mp_uint_t n ) {
76
77
mp_map_t * map = m_new (mp_map_t , 1 );
77
78
mp_map_init (map , n );
78
79
return map ;
@@ -103,13 +104,13 @@ void mp_map_clear(mp_map_t *map) {
103
104
}
104
105
105
106
STATIC void mp_map_rehash (mp_map_t * map ) {
106
- int old_alloc = map -> alloc ;
107
+ mp_uint_t old_alloc = map -> alloc ;
107
108
mp_map_elem_t * old_table = map -> table ;
108
109
map -> alloc = get_doubling_prime_greater_or_equal_to (map -> alloc + 1 );
109
110
map -> used = 0 ;
110
111
map -> all_keys_are_qstrs = 1 ;
111
112
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 ++ ) {
113
114
if (old_table [i ].key != MP_OBJ_NULL && old_table [i ].key != MP_OBJ_SENTINEL ) {
114
115
mp_map_lookup (map , old_table [i ].key , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND )-> value = old_table [i ].value ;
115
116
}
@@ -168,8 +169,8 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
168
169
}
169
170
170
171
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 ;
173
174
mp_map_elem_t * avail_slot = NULL ;
174
175
for (;;) {
175
176
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
242
243
/******************************************************************************/
243
244
/* set */
244
245
245
- void mp_set_init (mp_set_t * set , int n ) {
246
+ void mp_set_init (mp_set_t * set , mp_uint_t n ) {
246
247
set -> alloc = n ;
247
248
set -> used = 0 ;
248
249
set -> table = m_new0 (mp_obj_t , set -> alloc );
249
250
}
250
251
251
252
STATIC void mp_set_rehash (mp_set_t * set ) {
252
- int old_alloc = set -> alloc ;
253
+ mp_uint_t old_alloc = set -> alloc ;
253
254
mp_obj_t * old_table = set -> table ;
254
255
set -> alloc = get_doubling_prime_greater_or_equal_to (set -> alloc + 1 );
255
256
set -> used = 0 ;
256
257
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 ++ ) {
258
259
if (old_table [i ] != MP_OBJ_NULL && old_table [i ] != MP_OBJ_SENTINEL ) {
259
260
mp_set_lookup (set , old_table [i ], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND );
260
261
}
@@ -271,8 +272,8 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku
271
272
}
272
273
}
273
274
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 ;
276
277
mp_obj_t * avail_slot = NULL ;
277
278
for (;;) {
278
279
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
333
334
}
334
335
335
336
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 ++ ) {
337
338
if (MP_SET_SLOT_IS_FILLED (set , pos )) {
338
339
mp_obj_t elem = set -> table [pos ];
339
340
// delete element
@@ -359,7 +360,7 @@ void mp_set_clear(mp_set_t *set) {
359
360
360
361
#if DEBUG_PRINT
361
362
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 ++ ) {
363
364
if (map -> table [i ].key != NULL ) {
364
365
mp_obj_print (map -> table [i ].key , PRINT_REPR );
365
366
} else {
0 commit comments