8000 py/map: In map lookup, check for fixed map independent of ordered map. · micropython/micropython@6dde019 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6dde019

Browse files
committed
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).
1 parent 4bd95f8 commit 6dde019

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

py/map.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ STATIC void mp_map_rehash(mp_map_t *map) {
139139
// - returns NULL if not found, else the slot if was found in with key null and value non-null
140140
mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
141141

142+
if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
143+
// can't add/remove from a fixed array
144+
return NULL;
145+
}
146+
142147
// Work out if we can compare just pointers
143148
bool compare_only_ptrs = map->all_keys_are_qstrs;
144149
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
160165

161166
// if the map is an ordered array then we must do a brute force linear search
162167
if (map->is_ordered) {
163-
if (map->is_fixed && lookup_kind != MP_MAP_LOOKUP) {
164-
// can't add/remove from a fixed array
165-
return NULL;
166-
}
167168
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
168169
if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
169170
if (MP_UNLIKELY(lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND)) {

0 commit comments

Comments
 (0)
0