summaryrefslogtreecommitdiffstatshomepage
path: root/py/map.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-12-31 00:19:28 +0000
committerDamien George <damien.p.george@gmail.com>2015-12-31 00:24:33 +0000
commit6dde019d933175b5c2608b8da2239846e1730838 (patch)
tree9fbc5445082a37893a91cdafaa190d8986637991 /py/map.c
parent4bd95f8b4407ff85c68676285c02236ee72d72cb (diff)
downloadmicropython-6dde019d933175b5c2608b8da2239846e1730838.tar.gz
micropython-6dde019d933175b5c2608b8da2239846e1730838.zip
py/map: In map lookup, check for fixed map independent of ordered map.
It's possible to have a fixed map that is properly hashed (ie not simply ordered).
Diffstat (limited to 'py/map.c')
-rw-r--r--py/map.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/py/map.c b/py/map.c
index 7900ee1dec..870579b6be 100644
--- a/py/map.c
+++ b/py/map.c
@@ -139,6 +139,11 @@ STATIC void mp_map_rehash(mp_map_t *map) {
// - returns NULL if not found, else the slot if was found in with key null and value non-null
mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
+ if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
+ // can't add/remove from a fixed array
+ return NULL;
+ }
+
// Work out if we can compare just pointers
bool compare_only_ptrs = map->all_keys_are_qstrs;
if (compare_only_ptrs) {
@@ -160,10 +165,6 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
// if the map is an ordered array then we must do a brute force linear search
if (map->is_ordered) {
- if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
- // can't add/remove from a fixed array
- return NULL;
- }
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {