Select Git revision
map.c 10.79 KiB
#include <stdlib.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
// 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 int get_doubling_prime_greater_or_equal_to(int x) {
for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
if (doubling_primes[i] >= x) {
return doubling_primes[i];
}
}
// ran out of primes in the table!
// return something sensible, at least make it odd
return x | 1;
}
/******************************************************************************/
/* map */
void mp_map_init(mp_map_t *map, int n) {
if (n == 0) {
map->alloc = 0;
map->table = NULL;
} else {
map->alloc = n;
map->table = m_new0(mp_map_elem_t, map->alloc);
}
map->used = 0;
map->all_keys_are_qstrs = 1;
map->table_is_fixed_array = 0;
}
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
map->alloc = n;
map->used = n;
map->all_keys_are_qstrs = 1;
map->table_is_fixed_array = 1;
map->table = (mp_map_elem_t*)table;
}
mp_map_t *mp_map_new(int n) {
mp_map_t *map = m_new(mp_map_t, 1);
mp_map_init(map, n);
return map;
}
// Differentiate from mp_map_clear() - semantics is different
void mp_map_deinit(mp_map_t *map) {
if (!map->table_is_fixed_array) {
m_del(mp_map_elem_t, map->table, map->alloc);
}
map->used = map->alloc = 0;
}
void mp_map_free(mp_map_t *map) {
mp_map_deinit(map);
m_del_obj(mp_map_t, map);
}
void mp_map_clear(mp_map_t *map) {
if (!map->table_is_fixed_array) {
m_del(mp_map_elem_t, map->table, map->alloc);