summaryrefslogtreecommitdiffstatshomepage
path: root/py/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/map.c')
-rw-r--r--py/map.c62
1 files changed, 48 insertions, 14 deletions
diff --git a/py/map.c b/py/map.c
index 01209c9b74..b88181989d 100644
--- a/py/map.c
+++ b/py/map.c
@@ -9,7 +9,8 @@
#include "map.h"
// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
-static int doubling_primes[] = {7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607};
+// 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++) {
@@ -38,7 +39,32 @@ mp_map_t *mp_map_new(mp_map_kind_t kind, int n) {
return map;
}
-mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found) {
+void mp_map_clear(mp_map_t *map) {
+ map->used = 0;
+ 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->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_helper(map, old_table[i].key, true, false)->value = old_table[i].value;
+ }
+ }
+ m_del(mp_map_elem_t, old_table, old_alloc);
+}
+
+mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found, bool remove_if_found) {
bool is_map_mp_obj = (map->kind == MP_MAP_OBJ);
machine_uint_t hash;
if (is_map_mp_obj) {
@@ -46,6 +72,13 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
} else {
hash = (machine_uint_t)index;
}
+ if (map->alloc == 0) {
+ if (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];
@@ -54,17 +87,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
if (add_if_not_found) {
if (map->used + 1 >= map->alloc) {
// not enough room in table, rehash it
- 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->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_helper(map, old_table[i].key, true)->value = old_table[i].value;
- }
- }
- m_del(mp_map_elem_t, old_table, old_alloc);
+ mp_map_rehash(map);
// restart the search for the new element
pos = hash % map->alloc;
} else {
@@ -82,6 +105,16 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
elem->key = index;
}
*/
+ if (remove_if_found) {
+ map->used--;
+ /* this leaks this memory (but see dict_get_helper) */
+ mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
+ retval->key = elem->key;
+ retval->value = elem->value;
+ elem->key = NULL;
+ elem->value = NULL;
+ return retval;
+ }
return elem;
} else {
// not yet found, keep searching in this table
@@ -92,7 +125,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, bool add_if_not_found) {
mp_obj_t o = (mp_obj_t)(machine_uint_t)index;
- return mp_map_lookup_helper(map, o, add_if_not_found);
+ return mp_map_lookup_helper(map, o, add_if_not_found, false);
}
/******************************************************************************/
@@ -106,6 +139,7 @@ void mp_set_init(mp_set_t *set, int n) {
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
int hash = mp_obj_hash(index);
+ assert(set->alloc); /* FIXME: if alloc is ever 0 when doing a lookup, this'll fail: */
int pos = hash % set->alloc;
for (;;) {
mp_obj_t elem = set->table[pos];