Skip to content

py/map: Add board option MICROPY_PY_MAP_ORDERED to make all dicts/maps ordered. #5323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion py/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,19 @@ void mp_map_init(mp_map_t *map, size_t n) {
map->used = 0;
map->all_keys_are_qstrs = 1;
map->is_fixed = 0;
#if !MICROPY_PY_MAP_ORDERED
map->is_ordered = 0;
#endif
}

void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
map->alloc = n;
map->used = n;
map->all_keys_are_qstrs = 1;
map->is_fixed = 1;
#if !MICROPY_PY_MAP_ORDERED
map->is_ordered = 1;
#endif
map->table = (mp_map_elem_t *)table;
}

Expand All @@ -118,6 +122,7 @@ void mp_map_clear(mp_map_t *map) {
map->table = NULL;
}

#if !MICROPY_PY_MAP_ORDERED
STATIC void mp_map_rehash(mp_map_t *map) {
size_t old_alloc = map->alloc;
size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
Expand All @@ -136,6 +141,7 @@ STATIC void mp_map_rehash(mp_map_t *map) {
}
m_del(mp_map_elem_t, old_table, old_alloc);
}
#endif

// MP_MAP_LOOKUP behaviour:
// - returns NULL if not found, else the slot it was found in with key,value non-null
Expand Down Expand Up @@ -167,7 +173,10 @@ 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 !MICROPY_PY_MAP_ORDERED
if (map->is_ordered)
#endif
{
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this uncrustify which wants to dedent all the code? It's not great. Would it fix it to add an extra set of { } around this code, outside the #if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep these were both from uncrustify breaking the build. I'll try the { }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, yes the brackets appear to have fixed the formatting.

if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Expand Down Expand Up @@ -206,6 +215,7 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
return NULL;
#endif
}
#if !MICROPY_PY_MAP_ORDERED

// map is a hash table (not an ordered array), so do a hash lookup

Expand Down Expand Up @@ -294,6 +304,7 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
}
}
}
#endif // !MICROPY_PY_MAP_ORDERED
}

/******************************************************************************/
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy