Skip to content

Commit 5969dc5

Browse files
committed
py/map: Add board option MICROPY_PY_MAP_ORDERED to make all dicts/maps ordered.
1 parent 1662a0b commit 5969dc5

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

py/map.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@ void mp_map_init(mp_map_t *map, size_t n) {
8787
map->used = 0;
8888
map->all_keys_are_qstrs = 1;
8989
map->is_fixed = 0;
90+
#if !MICROPY_PY_MAP_ORDERED
9091
map->is_ordered = 0;
92+
#endif
9193
}
9294

9395
void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
9496
map->alloc = n;
9597
map->used = n;
9698
map->all_keys_are_qstrs = 1;
9799
map->is_fixed = 1;
100+
#if !MICROPY_PY_MAP_ORDERED
98101
map->is_ordered = 1;
102+
#endif
99103
map->table = (mp_map_elem_t *)table;
100104
}
101105

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

125+
#if !MICROPY_PY_MAP_ORDERED
121126
STATIC void mp_map_rehash(mp_map_t *map) {
122127
size_t old_alloc = map->alloc;
123128
size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
@@ -136,6 +141,7 @@ STATIC void mp_map_rehash(mp_map_t *map) {
136141
}
137142
m_del(mp_map_elem_t, old_table, old_alloc);
138143
}
144+
#endif
139145

140146
// MP_MAP_LOOKUP behaviour:
141147
// - returns NULL if not found, else the slot it was found in with key,value non-null
@@ -167,7 +173,10 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
167173
}
168174

169175
// if the map is an ordered array then we must do a brute force linear search
170-
if (map->is_ordered) {
176+
#if !MICROPY_PY_MAP_ORDERED
177+
if (map->is_ordered)
178+
#endif
179+
{
171180
for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
172181
if (elem->key == index || (!compare_only_ptrs && mp_obj_equal(elem->key, index))) {
173182
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
@@ -206,6 +215,7 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
206215
return NULL;
207216
#endif
208217
}
218+
#if !MICROPY_PY_MAP_ORDERED
209219

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

@@ -294,6 +304,7 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
294304
}
295305
}
296306
}
307+
#endif // !MICROPY_PY_MAP_ORDERED
297308
}
298309

299310
/******************************************************************************/

0 commit comments

Comments
 (0)