circuitpython/py/map.c

209 lines
6.7 KiB
C
Raw Normal View History

#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "obj.h"
#include "runtime0.h"
#include "map.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};
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) {
map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
map->used = 0;
map->all_keys_are_qstrs = 1;
map->table = m_new0(mp_map_elem_t, map->alloc);
}
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;
}
void mp_map_clear(mp_map_t *map) {
map->used = 0;
map->all_keys_are_qstrs = 1;
machine_uint_t a = map->alloc;
map->alloc = 0;
map->table = m_renew(mp_map_elem_t, map->table, a, map->alloc);
mp_map_elem_t nul = {NULL, NULL};
for (uint i=0; i<map->alloc; i++) {
map->table[i] = nul;
}
}
static void mp_map_rehash(mp_map_t *map) {
int 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++) {
if (old_table[i].key != NULL) {
mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
}
}
m_del(mp_map_elem_t, old_table, old_alloc);
}
mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
machine_uint_t hash;
hash = mp_obj_hash(index);
if (map->alloc == 0) {
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
mp_map_rehash(map);
} else {
return NULL;
}
}
uint pos = hash % map->alloc;
for (;;) {
mp_map_elem_t *elem = &map->table[pos];
if (elem->key == NULL) {
// not in table
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
if (map->used + 1 >= map->alloc) {
// not enough room in table, rehash it
mp_map_rehash(map);
// restart the search for the new element
pos = hash % map->alloc;
} else {
map->used += 1;
elem->key = index;
if (!MP_OBJ_IS_QSTR(index)) {
map->all_keys_are_qstrs = 0;
}
return elem;
}
} else {
return NULL;
}
} else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
// found it
/* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x
if (add_if_not_found) {
elem->key = index;
}
*/
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
2014-01-06 14:48:34 -05:00
map->used--;
// this leaks this memory (but see dict_get_helper)
2014-01-06 14:48:34 -05:00
mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
retval->key = elem->key;
retval->value = elem->value;
elem->key = NULL;
2014-01-06 15:25:51 -05:00
elem->value = NULL;
2014-01-06 14:48:34 -05:00
return retval;
}
return elem;
} else {
// not yet found, keep searching in this table
pos = (pos + 1) % map->alloc;
}
}
}
/******************************************************************************/
/* set */
void mp_set_init(mp_set_t *set, int n) {
set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
set->used = 0;
set->table = m_new0(mp_obj_t, set->alloc);
}
2014-01-12 10:44:26 -05:00
static void mp_set_rehash(mp_set_t *set) {
int 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++) {
if (old_table[i] != NULL) {
mp_set_lookup(set, old_table[i], true);
}
}
m_del(mp_obj_t, old_table, old_alloc);
}
2014-01-12 11:39:39 -05:00
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
2014-01-12 13:23:36 -05:00
int hash;
int pos;
2014-01-12 10:44:26 -05:00
if (set->alloc == 0) {
2014-01-12 13:23:36 -05:00
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
2014-01-12 10:44:26 -05:00
mp_set_rehash(set);
} else {
return NULL;
}
}
2014-01-12 13:23:36 -05:00
if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
hash = 0;
pos = 0;
} else {
hash = mp_obj_hash(index);;
pos = hash % set->alloc;
}
for (;;) {
mp_obj_t elem = set->table[pos];
if (elem == MP_OBJ_NULL) {
// not in table
2014-01-12 13:23:36 -05:00
if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
if (set->used + 1 >= set->alloc) {
// not enough room in table, rehash it
2014-01-12 10:44:26 -05:00
mp_set_rehash(set);
// restart the search for the new element
pos = hash % set->alloc;
} else {
set->used += 1;
set->table[pos] = index;
return index;
}
2014-01-12 13:23:36 -05:00
} else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
pos++;
} else {
return MP_OBJ_NULL;
}
2014-01-12 13:23:36 -05:00
} else if (lookup_kind & MP_MAP_LOOKUP_FIRST || mp_obj_equal(elem, index)) {
// found it
2014-01-12 13:23:36 -05:00
if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
2014-01-12 11:39:39 -05:00
set->used--;
set->table[pos] = NULL;
}
return elem;
} else {
// not yet found, keep searching in this table
pos = (pos + 1) % set->alloc;
}
}
}
2014-01-12 10:44:26 -05:00
void mp_set_clear(mp_set_t *set) {
set->used = 0;
machine_uint_t a = set->alloc;
set->alloc = 0;
set->table = m_renew(mp_obj_t, set->table, a, set->alloc);
for (uint i=0; i<set->alloc; i++) {
set->table[i] = NULL;
}
}