summaryrefslogtreecommitdiffstatshomepage
path: root/py/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/map.c')
-rw-r--r--py/map.c43
1 files changed, 19 insertions, 24 deletions
diff --git a/py/map.c b/py/map.c
index b88181989d..e44cf33d26 100644
--- a/py/map.c
+++ b/py/map.c
@@ -26,21 +26,22 @@ int get_doubling_prime_greater_or_equal_to(int x) {
/******************************************************************************/
/* map */
-void mp_map_init(mp_map_t *map, mp_map_kind_t kind, int n) {
- map->kind = kind;
- map->used = 0;
+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(mp_map_kind_t kind, int n) {
+mp_map_t *mp_map_new(int n) {
mp_map_t *map = m_new(mp_map_t, 1);
- mp_map_init(map, kind, n);
+ 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);
@@ -50,30 +51,26 @@ void mp_map_clear(mp_map_t *map) {
}
}
-static void mp_map_rehash (mp_map_t *map) {
+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_helper(map, old_table[i].key, true, false)->value = old_table[i].value;
+ 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_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);
+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;
- if (is_map_mp_obj) {
- hash = mp_obj_hash(index);
- } else {
- hash = (machine_uint_t)index;
- }
+ hash = mp_obj_hash(index);
if (map->alloc == 0) {
- if (add_if_not_found) {
+ if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
mp_map_rehash(map);
} else {
return NULL;
@@ -84,7 +81,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 *elem = &map->table[pos];
if (elem->key == NULL) {
// not in table
- if (add_if_not_found) {
+ 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);
@@ -93,21 +90,24 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
} 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 || (is_map_mp_obj && mp_obj_equal(elem->key, index))) {
+ } 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 (remove_if_found) {
+ if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
map->used--;
- /* this leaks this memory (but see dict_get_helper) */
+ // 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;
@@ -123,11 +123,6 @@ 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, false);
-}
-
/******************************************************************************/
/* set */